#include <sys/note.h>
#include <sys/scsi/scsi.h>
#include <sys/pci.h>
#include <sys/disp.h>
#include <sys/sata/sata_hba.h>
#include <sys/sata/adapters/ahci/ahcireg.h>
#include <sys/sata/adapters/ahci/ahcivar.h>
#include <sys/ddifm.h>
#include <sys/fm/protocol.h>
#include <sys/fm/util.h>
#include <sys/fm/io/ddi.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/errno.h>
#include <sys/open.h>
#include <sys/cred.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
static char ahci_ident[] = "ahci driver";
static int ahci_attach(dev_info_t *, ddi_attach_cmd_t);
static int ahci_detach(dev_info_t *, ddi_detach_cmd_t);
static int ahci_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
static int ahci_quiesce(dev_info_t *);
static int ahci_register_sata_hba_tran(ahci_ctl_t *, uint32_t);
static int ahci_unregister_sata_hba_tran(ahci_ctl_t *);
static int ahci_tran_probe_port(dev_info_t *, sata_device_t *);
static int ahci_tran_start(dev_info_t *, sata_pkt_t *spkt);
static int ahci_tran_abort(dev_info_t *, sata_pkt_t *, int);
static int ahci_tran_reset_dport(dev_info_t *, sata_device_t *);
static int ahci_tran_hotplug_port_activate(dev_info_t *, sata_device_t *);
static int ahci_tran_hotplug_port_deactivate(dev_info_t *, sata_device_t *);
#if defined(__lock_lint)
static int ahci_selftest(dev_info_t *, sata_device_t *);
#endif
static void ahci_fm_init(ahci_ctl_t *);
static void ahci_fm_fini(ahci_ctl_t *);
static int ahci_fm_error_cb(dev_info_t *, ddi_fm_error_t *, const void*);
int ahci_check_acc_handle(ddi_acc_handle_t);
int ahci_check_dma_handle(ddi_dma_handle_t);
void ahci_fm_ereport(ahci_ctl_t *, char *);
static int ahci_check_all_handle(ahci_ctl_t *);
static int ahci_check_ctl_handle(ahci_ctl_t *);
static int ahci_check_port_handle(ahci_ctl_t *, int);
static int ahci_check_slot_handle(ahci_port_t *, int);
static int ahci_setup_port_base_addresses(ahci_ctl_t *, ahci_port_t *);
static int ahci_alloc_ports_state(ahci_ctl_t *);
static void ahci_dealloc_ports_state(ahci_ctl_t *);
static int ahci_alloc_port_state(ahci_ctl_t *, uint8_t);
static void ahci_dealloc_port_state(ahci_ctl_t *, uint8_t);
static int ahci_alloc_rcvd_fis(ahci_ctl_t *, ahci_port_t *);
static void ahci_dealloc_rcvd_fis(ahci_port_t *);
static int ahci_alloc_cmd_list(ahci_ctl_t *, ahci_port_t *);
static void ahci_dealloc_cmd_list(ahci_ctl_t *, ahci_port_t *);
static int ahci_alloc_cmd_tables(ahci_ctl_t *, ahci_port_t *);
static void ahci_dealloc_cmd_tables(ahci_ctl_t *, ahci_port_t *);
static void ahci_alloc_pmult(ahci_ctl_t *, ahci_port_t *);
static void ahci_dealloc_pmult(ahci_ctl_t *, ahci_port_t *);
static int ahci_initialize_controller(ahci_ctl_t *);
static void ahci_uninitialize_controller(ahci_ctl_t *);
static int ahci_initialize_port(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
static int ahci_config_space_init(ahci_ctl_t *);
static void ahci_staggered_spin_up(ahci_ctl_t *, uint8_t);
static void ahci_drain_ports_taskq(ahci_ctl_t *);
static int ahci_rdwr_pmult(ahci_ctl_t *, ahci_addr_t *, uint8_t, uint32_t *,
uint8_t);
static int ahci_read_pmult(ahci_ctl_t *, ahci_addr_t *, uint8_t, uint32_t *);
static int ahci_write_pmult(ahci_ctl_t *, ahci_addr_t *, uint8_t, uint32_t);
static int ahci_update_pmult_pscr(ahci_ctl_t *, ahci_addr_t *,
sata_device_t *);
static int ahci_update_pmult_gscr(ahci_ctl_t *, ahci_addr_t *,
sata_pmult_gscr_t *);
static int ahci_initialize_pmult(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *,
sata_device_t *);
static int ahci_initialize_pmport(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
static int ahci_probe_pmult(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
static int ahci_probe_pmport(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *,
sata_device_t *);
static void ahci_disable_interface_pm(ahci_ctl_t *, uint8_t);
static int ahci_start_port(ahci_ctl_t *, ahci_port_t *, uint8_t);
static void ahci_find_dev_signature(ahci_ctl_t *, ahci_port_t *,
ahci_addr_t *);
static void ahci_update_sata_registers(ahci_ctl_t *, uint8_t, sata_device_t *);
static int ahci_deliver_satapkt(ahci_ctl_t *, ahci_port_t *,
ahci_addr_t *, sata_pkt_t *);
static int ahci_do_sync_start(ahci_ctl_t *, ahci_port_t *,
ahci_addr_t *, sata_pkt_t *);
static int ahci_claim_free_slot(ahci_ctl_t *, ahci_port_t *,
ahci_addr_t *, int);
static void ahci_copy_err_cnxt(sata_cmd_t *, ahci_fis_d2h_register_t *);
static void ahci_copy_ncq_err_page(sata_cmd_t *,
struct sata_ncq_error_recovery_page *);
static void ahci_copy_out_regs(sata_cmd_t *, ahci_fis_d2h_register_t *);
static void ahci_add_doneq(ahci_port_t *, sata_pkt_t *, int);
static void ahci_flush_doneq(ahci_port_t *);
static int ahci_software_reset(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
static int ahci_hba_reset(ahci_ctl_t *);
static int ahci_port_reset(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
static int ahci_pmport_reset(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
static void ahci_reject_all_abort_pkts(ahci_ctl_t *, ahci_port_t *, uint8_t);
static int ahci_reset_device_reject_pkts(ahci_ctl_t *, ahci_port_t *,
ahci_addr_t *);
static int ahci_reset_pmdevice_reject_pkts(ahci_ctl_t *, ahci_port_t *,
ahci_addr_t *);
static int ahci_reset_port_reject_pkts(ahci_ctl_t *, ahci_port_t *,
ahci_addr_t *);
static int ahci_reset_hba_reject_pkts(ahci_ctl_t *);
static int ahci_put_port_into_notrunning_state(ahci_ctl_t *, ahci_port_t *,
uint8_t);
static int ahci_restart_port_wait_till_ready(ahci_ctl_t *, ahci_port_t *,
uint8_t, int, int *);
static void ahci_mop_commands(ahci_ctl_t *, ahci_port_t *, uint32_t,
uint32_t, uint32_t, uint32_t, uint32_t);
static uint32_t ahci_get_rdlogext_data(ahci_ctl_t *, ahci_port_t *, uint8_t);
static void ahci_get_rqsense_data(ahci_ctl_t *, ahci_port_t *,
uint8_t, sata_pkt_t *);
static void ahci_fatal_error_recovery_handler(ahci_ctl_t *, ahci_port_t *,
ahci_addr_t *, uint32_t);
static void ahci_pmult_error_recovery_handler(ahci_ctl_t *, ahci_port_t *,
uint8_t, uint32_t);
static void ahci_timeout_pkts(ahci_ctl_t *, ahci_port_t *,
uint8_t, uint32_t);
static void ahci_events_handler(void *);
static void ahci_watchdog_handler(ahci_ctl_t *);
static uint_t ahci_intr(caddr_t, caddr_t);
static void ahci_port_intr(ahci_ctl_t *, ahci_port_t *, uint8_t);
static int ahci_add_intrs(ahci_ctl_t *, int);
static void ahci_rem_intrs(ahci_ctl_t *);
static void ahci_enable_all_intrs(ahci_ctl_t *);
static void ahci_disable_all_intrs(ahci_ctl_t *);
static void ahci_enable_port_intrs(ahci_ctl_t *, uint8_t);
static void ahci_disable_port_intrs(ahci_ctl_t *, uint8_t);
static int ahci_intr_cmd_cmplt(ahci_ctl_t *, ahci_port_t *, uint8_t);
static int ahci_intr_set_device_bits(ahci_ctl_t *, ahci_port_t *, uint8_t);
static int ahci_intr_ncq_events(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
static int ahci_intr_pmult_sntf_events(ahci_ctl_t *, ahci_port_t *, uint8_t);
static int ahci_intr_port_connect_change(ahci_ctl_t *, ahci_port_t *, uint8_t);
static int ahci_intr_device_mechanical_presence_status(ahci_ctl_t *,
ahci_port_t *, uint8_t);
static int ahci_intr_phyrdy_change(ahci_ctl_t *, ahci_port_t *, uint8_t);
static int ahci_intr_non_fatal_error(ahci_ctl_t *, ahci_port_t *,
uint8_t, uint32_t);
static int ahci_intr_fatal_error(ahci_ctl_t *, ahci_port_t *,
uint8_t, uint32_t);
static int ahci_intr_cold_port_detect(ahci_ctl_t *, ahci_port_t *, uint8_t);
static void ahci_get_ahci_addr(ahci_ctl_t *, sata_device_t *, ahci_addr_t *);
static int ahci_get_num_implemented_ports(uint32_t);
static void ahci_log_fatal_error_message(ahci_ctl_t *, uint8_t, uint32_t);
static void ahci_dump_commands(ahci_ctl_t *, uint8_t, uint32_t);
static void ahci_log_serror_message(ahci_ctl_t *, uint8_t, uint32_t, int);
#if AHCI_DEBUG
static void ahci_log(ahci_ctl_t *, uint_t, char *, ...);
#endif
static boolean_t ahci_em_init(ahci_ctl_t *);
static void ahci_em_fini(ahci_ctl_t *);
static void ahci_em_suspend(ahci_ctl_t *);
static void ahci_em_resume(ahci_ctl_t *);
static int ahci_em_ioctl(dev_info_t *, int, intptr_t);
static ddi_dma_attr_t buffer_dma_attr = {
DMA_ATTR_V0,
0x0ull,
0xffffffffffffffffull,
0x3fffffull,
0x2ull,
1,
1,
0xffffffffull,
0xffffffffull,
AHCI_PRDT_NUMBER,
512,
0,
};
static ddi_dma_attr_t rcvd_fis_dma_attr = {
DMA_ATTR_V0,
0x0ull,
0xffffffffffffffffull,
0xffffffffull,
0x100ull,
1,
1,
0xffffffffull,
0xffffffffull,
1,
1,
0,
};
static ddi_dma_attr_t cmd_list_dma_attr = {
DMA_ATTR_V0,
0x0ull,
0xffffffffffffffffull,
0xffffffffull,
0x400ull,
1,
1,
0xffffffffull,
0xffffffffull,
1,
1,
0,
};
static ddi_dma_attr_t cmd_table_dma_attr = {
DMA_ATTR_V0,
0x0ull,
0xffffffffffffffffull,
0xffffffffull,
0x80ull,
1,
1,
0xffffffffull,
0xffffffffull,
1,
1,
0,
};
static ddi_device_acc_attr_t accattr = {
DDI_DEVICE_ATTR_V1,
DDI_STRUCTURE_LE_ACC,
DDI_STRICTORDER_ACC,
DDI_DEFAULT_ACC
};
static struct dev_ops ahcictl_dev_ops = {
DEVO_REV,
0,
ahci_getinfo,
nulldev,
nulldev,
ahci_attach,
ahci_detach,
nodev,
NULL,
NULL,
NULL,
ahci_quiesce,
};
static sata_tran_hotplug_ops_t ahci_tran_hotplug_ops = {
SATA_TRAN_HOTPLUG_OPS_REV_1,
ahci_tran_hotplug_port_activate,
ahci_tran_hotplug_port_deactivate
};
extern struct mod_ops mod_driverops;
static struct modldrv modldrv = {
&mod_driverops,
ahci_ident,
&ahcictl_dev_ops,
};
static struct modlinkage modlinkage = {
MODREV_1,
&modldrv,
NULL
};
static clock_t ahci_watchdog_timeout = 5;
static clock_t ahci_watchdog_tick;
static size_t ahci_cmd_table_size;
int ahci_dma_prdt_number = AHCI_PRDT_NUMBER;
boolean_t ahci_msi_enabled = B_TRUE;
boolean_t ahci_buf_64bit_dma = B_TRUE;
boolean_t ahci_commu_64bit_dma = B_TRUE;
boolean_t sb600_buf_64bit_dma_disable = B_TRUE;
boolean_t sbxxx_commu_64bit_dma_disable = B_TRUE;
uint_t ahci_em_reset_delay_ms = 1;
uint_t ahci_em_reset_delay_count = 1000;
uint_t ahci_em_tx_delay_ms = 1;
uint_t ahci_em_tx_delay_count = 1000;
#if AHCI_DEBUG
uint32_t ahci_debug_flags = 0;
#else
uint32_t ahci_debug_flags = (AHCIDBG_ERRS|AHCIDBG_TIMEOUT);
#endif
#if AHCI_DEBUG
static kmutex_t ahci_log_mutex;
static char ahci_log_buf[512];
#endif
static void *ahci_statep = NULL;
int
_init(void)
{
int ret;
ret = ddi_soft_state_init(&ahci_statep, sizeof (ahci_ctl_t), 0);
if (ret != 0) {
goto err_out;
}
#if AHCI_DEBUG
mutex_init(&ahci_log_mutex, NULL, MUTEX_DRIVER, NULL);
#endif
if ((ret = sata_hba_init(&modlinkage)) != 0) {
#if AHCI_DEBUG
mutex_destroy(&ahci_log_mutex);
#endif
ddi_soft_state_fini(&ahci_statep);
goto err_out;
}
ahci_watchdog_tick = drv_usectohz(
(clock_t)ahci_watchdog_timeout * 1000000);
ret = mod_install(&modlinkage);
if (ret != 0) {
sata_hba_fini(&modlinkage);
#if AHCI_DEBUG
mutex_destroy(&ahci_log_mutex);
#endif
ddi_soft_state_fini(&ahci_statep);
goto err_out;
}
return (ret);
err_out:
cmn_err(CE_WARN, "!ahci: Module init failed");
return (ret);
}
int
_fini(void)
{
int ret;
ret = mod_remove(&modlinkage);
if (ret != 0) {
return (ret);
}
sata_hba_fini(&modlinkage);
#if AHCI_DEBUG
mutex_destroy(&ahci_log_mutex);
#endif
ddi_soft_state_fini(&ahci_statep);
return (ret);
}
int
_info(struct modinfo *modinfop)
{
return (mod_info(&modlinkage, modinfop));
}
static int
ahci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
{
ahci_ctl_t *ahci_ctlp = NULL;
int instance = ddi_get_instance(dip);
int status;
int attach_state;
uint32_t cap_status, ahci_version;
uint32_t ghc_control;
int intr_types;
int i;
pci_regspec_t *regs;
int regs_length;
int rnumber;
#if AHCI_DEBUG
int speed;
#endif
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp, "ahci_attach enter",
NULL);
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
ghc_control |= AHCI_HBA_GHC_AE;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
if (ahci_ctlp->ahcictl_timeout_id == 0)
ahci_ctlp->ahcictl_timeout_id = timeout(
(void (*)(void *))ahci_watchdog_handler,
(caddr_t)ahci_ctlp, ahci_watchdog_tick);
mutex_exit(&ahci_ctlp->ahcictl_mutex);
if (ahci_initialize_controller(ahci_ctlp) != AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PM, ahci_ctlp,
"Failed to initialize the controller "
"during DDI_RESUME", NULL);
return (DDI_FAILURE);
}
ahci_em_resume(ahci_ctlp);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_ctlp->ahcictl_flags &= ~AHCI_SUSPEND;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
attach_state = AHCI_ATTACH_STATE_NONE;
status = ddi_soft_state_zalloc(ahci_statep, instance);
if (status != DDI_SUCCESS) {
cmn_err(CE_WARN, "!ahci%d: Cannot allocate soft state",
instance);
goto err_out;
}
ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
ahci_ctlp->ahcictl_flags |= AHCI_ATTACH;
ahci_ctlp->ahcictl_dip = dip;
for (i = 0; i < AHCI_MAX_PORTS; i++) {
ahci_ctlp->ahcictl_port_to_cport[i] = 0xff;
ahci_ctlp->ahcictl_cport_to_port[i] = 0xff;
}
attach_state |= AHCI_ATTACH_STATE_STATEP_ALLOC;
ahci_fm_init(ahci_ctlp);
attach_state |= AHCI_ATTACH_STATE_FMA;
if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
DDI_PROP_DONTPASS, "reg", (int **)®s,
(uint_t *)®s_length) != DDI_PROP_SUCCESS) {
cmn_err(CE_WARN, "!ahci%d: Cannot lookup reg property",
instance);
goto err_out;
}
for (rnumber = 0; rnumber < regs_length; ++rnumber) {
if ((regs[rnumber].pci_phys_hi & PCI_REG_REG_M)
== AHCI_PCI_RNUM)
break;
}
ddi_prop_free(regs);
if (rnumber == regs_length) {
cmn_err(CE_WARN, "!ahci%d: Cannot find AHCI register set",
instance);
goto err_out;
}
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "rnumber = %d", rnumber);
status = ddi_regs_map_setup(dip,
rnumber,
(caddr_t *)&ahci_ctlp->ahcictl_ahci_addr,
0,
0,
&accattr,
&ahci_ctlp->ahcictl_ahci_acc_handle);
if (status != DDI_SUCCESS) {
cmn_err(CE_WARN, "!ahci%d: Cannot map register space",
instance);
goto err_out;
}
attach_state |= AHCI_ATTACH_STATE_REG_MAP;
ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
ghc_control |= AHCI_HBA_GHC_AE;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
ahci_version = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_VS(ahci_ctlp));
cmn_err(CE_NOTE, "!ahci%d: hba AHCI version = %x.%x", instance,
(ahci_version & 0xffff0000) >> 16,
((ahci_version & 0x0000ff00) >> 4 |
(ahci_version & 0x000000ff)));
if (!(ahci_version & 0xffff0000)) {
cmn_err(CE_WARN, "ahci%d: Don't support AHCI HBA with lower "
"than version 1.0", instance);
goto err_out;
}
cap_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_CAP(ahci_ctlp));
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "hba capabilities = 0x%x",
cap_status);
if (ahci_version >= 0x00010200) {
uint32_t cap2_status;
cap2_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_CAP2(ahci_ctlp));
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"hba capabilities extended = 0x%x", cap2_status);
if (cap2_status & AHCI_HBA_CAP2_SDS) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_SDS;
}
}
if (cap_status & AHCI_HBA_CAP_EMS) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_EMS;
ahci_ctlp->ahcictl_em_loc =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_EM_LOC(ahci_ctlp));
ahci_ctlp->ahcictl_em_ctl =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_EM_CTL(ahci_ctlp));
}
#if AHCI_DEBUG
speed = (cap_status & AHCI_HBA_CAP_ISS) >> AHCI_HBA_CAP_ISS_SHIFT;
if (speed == 0x01) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"hba interface speed support: Gen 1 (1.5Gbps)", NULL);
} else if (speed == 0x10) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"hba interface speed support: Gen 2 (3 Gbps)", NULL);
} else if (speed == 0x11) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"hba interface speed support: Gen 3 (6 Gbps)", NULL);
}
#endif
ahci_ctlp->ahcictl_num_cmd_slots =
((cap_status & AHCI_HBA_CAP_NCS) >>
AHCI_HBA_CAP_NCS_SHIFT) + 1;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "hba number of cmd slots: %d",
ahci_ctlp->ahcictl_num_cmd_slots);
ahci_ctlp->ahcictl_ports_implemented =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_PI(ahci_ctlp));
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "hba implementation of ports: 0x%x",
ahci_ctlp->ahcictl_ports_implemented);
ahci_ctlp->ahcictl_num_ports =
ddi_fls(ahci_ctlp->ahcictl_ports_implemented);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "hba number of ports: %d",
(cap_status & AHCI_HBA_CAP_NP) + 1);
ahci_ctlp->ahcictl_num_implemented_ports =
ahci_get_num_implemented_ports(
ahci_ctlp->ahcictl_ports_implemented);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"hba number of implemented ports: %d",
ahci_ctlp->ahcictl_num_implemented_ports);
if (!(cap_status & AHCI_HBA_CAP_S64A)) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_BUF_32BIT_DMA;
ahci_ctlp->ahcictl_cap |= AHCI_CAP_COMMU_32BIT_DMA;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"hba does not support 64-bit addressing", NULL);
}
if (cap_status & AHCI_HBA_CAP_SPM) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_PMULT_CBSS;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
"hba supports port multiplier (CBSS)", NULL);
if (cap_status & AHCI_HBA_CAP_FBSS) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_PMULT_FBSS;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
"hba supports FIS-based switching (FBSS)", NULL);
}
}
if (cap_status & AHCI_HBA_CAP_SCLO) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_SCLO;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
"hba supports command list override.", NULL);
}
if (cap_status & AHCI_HBA_CAP_SSNTF) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_SNTF;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
"hba supports asynchronous notification.", NULL);
}
if (pci_config_setup(dip, &ahci_ctlp->ahcictl_pci_conf_handle)
!= DDI_SUCCESS) {
cmn_err(CE_WARN, "!ahci%d: Cannot set up pci configure space",
instance);
goto err_out;
}
attach_state |= AHCI_ATTACH_STATE_PCICFG_SETUP;
if (ahci_config_space_init(ahci_ctlp) == AHCI_FAILURE) {
cmn_err(CE_WARN, "!ahci%d: ahci_config_space_init failed",
instance);
goto err_out;
}
ahci_disable_all_intrs(ahci_ctlp);
if (ddi_intr_get_supported_types(dip, &intr_types) != DDI_SUCCESS) {
cmn_err(CE_WARN, "!ahci%d: ddi_intr_get_supported_types failed",
instance);
goto err_out;
}
AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
"ddi_intr_get_supported_types() returned: 0x%x",
intr_types);
if (ahci_msi_enabled && (intr_types & DDI_INTR_TYPE_MSI)) {
if (ahci_add_intrs(ahci_ctlp, DDI_INTR_TYPE_MSI) ==
DDI_SUCCESS) {
ahci_ctlp->ahcictl_intr_type = DDI_INTR_TYPE_MSI;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
"Using MSI interrupt type", NULL);
goto intr_done;
}
AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
"MSI registration failed, "
"trying FIXED interrupts", NULL);
}
if (intr_types & DDI_INTR_TYPE_FIXED) {
if (ahci_add_intrs(ahci_ctlp, DDI_INTR_TYPE_FIXED) ==
DDI_SUCCESS) {
ahci_ctlp->ahcictl_intr_type = DDI_INTR_TYPE_FIXED;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
"Using FIXED interrupt type", NULL);
goto intr_done;
}
AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
"FIXED interrupt registration failed", NULL);
}
cmn_err(CE_WARN, "!ahci%d: Interrupt registration failed", instance);
goto err_out;
intr_done:
attach_state |= AHCI_ATTACH_STATE_INTR_ADDED;
mutex_init(&ahci_ctlp->ahcictl_mutex, NULL, MUTEX_DRIVER,
(void *)(uintptr_t)ahci_ctlp->ahcictl_intr_pri);
attach_state |= AHCI_ATTACH_STATE_MUTEX_INIT;
if (ahci_dma_prdt_number < AHCI_MIN_PRDT_NUMBER) {
ahci_dma_prdt_number = AHCI_MIN_PRDT_NUMBER;
} else if (ahci_dma_prdt_number > AHCI_MAX_PRDT_NUMBER) {
ahci_dma_prdt_number = AHCI_MAX_PRDT_NUMBER;
}
ahci_cmd_table_size = (sizeof (ahci_cmd_table_t) +
(ahci_dma_prdt_number - AHCI_PRDT_NUMBER) *
sizeof (ahci_prdt_item_t));
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ahci_attach: ahci_dma_prdt_number set by user is 0x%x,"
" ahci_cmd_table_size is 0x%x",
ahci_dma_prdt_number, ahci_cmd_table_size);
if (ahci_dma_prdt_number != AHCI_PRDT_NUMBER)
ahci_ctlp->ahcictl_buffer_dma_attr.dma_attr_sgllen =
ahci_dma_prdt_number;
ahci_ctlp->ahcictl_buffer_dma_attr = buffer_dma_attr;
ahci_ctlp->ahcictl_rcvd_fis_dma_attr = rcvd_fis_dma_attr;
ahci_ctlp->ahcictl_cmd_list_dma_attr = cmd_list_dma_attr;
ahci_ctlp->ahcictl_cmd_table_dma_attr = cmd_table_dma_attr;
if ((ahci_buf_64bit_dma == B_FALSE) ||
((ahci_ctlp->ahcictl_cap & AHCI_CAP_BUF_32BIT_DMA) &&
!(sb600_buf_64bit_dma_disable == B_FALSE &&
ahci_ctlp->ahcictl_venid == 0x1002 &&
ahci_ctlp->ahcictl_devid == 0x4380))) {
ahci_ctlp->ahcictl_buffer_dma_attr.dma_attr_addr_hi =
0xffffffffull;
}
if ((ahci_commu_64bit_dma == B_FALSE) ||
((ahci_ctlp->ahcictl_cap & AHCI_CAP_COMMU_32BIT_DMA) &&
!(sbxxx_commu_64bit_dma_disable == B_FALSE &&
ahci_ctlp->ahcictl_venid == 0x1002 &&
(ahci_ctlp->ahcictl_devid == 0x4380 ||
ahci_ctlp->ahcictl_devid == 0x4391)))) {
ahci_ctlp->ahcictl_rcvd_fis_dma_attr.dma_attr_addr_hi =
0xffffffffull;
ahci_ctlp->ahcictl_cmd_list_dma_attr.dma_attr_addr_hi =
0xffffffffull;
ahci_ctlp->ahcictl_cmd_table_dma_attr.dma_attr_addr_hi =
0xffffffffull;
}
status = ahci_alloc_ports_state(ahci_ctlp);
if (status != AHCI_SUCCESS) {
cmn_err(CE_WARN, "!ahci%d: Cannot allocate ports structure",
instance);
goto err_out;
}
attach_state |= AHCI_ATTACH_STATE_PORT_ALLOC;
status = ahci_initialize_controller(ahci_ctlp);
if (status != AHCI_SUCCESS) {
cmn_err(CE_WARN, "!ahci%d: HBA initialization failed",
instance);
goto err_out;
}
attach_state |= AHCI_ATTACH_STATE_HW_INIT;
ahci_ctlp->ahcictl_timeout_id = timeout(
(void (*)(void *))ahci_watchdog_handler,
(caddr_t)ahci_ctlp, ahci_watchdog_tick);
attach_state |= AHCI_ATTACH_STATE_TIMEOUT_ENABLED;
if (!ahci_em_init(ahci_ctlp)) {
cmn_err(CE_WARN, "!ahci%d: failed to initialize enclosure "
"services", instance);
goto err_out;
}
attach_state |= AHCI_ATTACH_STATE_ENCLOSURE;
if (ahci_register_sata_hba_tran(ahci_ctlp, cap_status)) {
cmn_err(CE_WARN, "!ahci%d: sata hba tran registration failed",
instance);
goto err_out;
}
if (ahci_check_all_handle(ahci_ctlp) != DDI_SUCCESS) {
cmn_err(CE_WARN, "!ahci%d: invalid dma/acc handles",
instance);
goto err_out;
}
ahci_ctlp->ahcictl_flags &= ~AHCI_ATTACH;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "ahci_attach success!", NULL);
return (DDI_SUCCESS);
err_out:
ahci_fm_ereport(ahci_ctlp, DDI_FM_DEVICE_NO_RESPONSE);
ddi_fm_service_impact(ahci_ctlp->ahcictl_dip, DDI_SERVICE_LOST);
if (attach_state & AHCI_ATTACH_STATE_ENCLOSURE) {
ahci_em_fini(ahci_ctlp);
}
if (attach_state & AHCI_ATTACH_STATE_TIMEOUT_ENABLED) {
mutex_enter(&ahci_ctlp->ahcictl_mutex);
(void) untimeout(ahci_ctlp->ahcictl_timeout_id);
ahci_ctlp->ahcictl_timeout_id = 0;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
}
if (attach_state & AHCI_ATTACH_STATE_HW_INIT) {
ahci_uninitialize_controller(ahci_ctlp);
}
if (attach_state & AHCI_ATTACH_STATE_PORT_ALLOC) {
ahci_dealloc_ports_state(ahci_ctlp);
}
if (attach_state & AHCI_ATTACH_STATE_MUTEX_INIT) {
mutex_destroy(&ahci_ctlp->ahcictl_mutex);
}
if (attach_state & AHCI_ATTACH_STATE_INTR_ADDED) {
ahci_rem_intrs(ahci_ctlp);
}
if (attach_state & AHCI_ATTACH_STATE_PCICFG_SETUP) {
pci_config_teardown(&ahci_ctlp->ahcictl_pci_conf_handle);
}
if (attach_state & AHCI_ATTACH_STATE_REG_MAP) {
ddi_regs_map_free(&ahci_ctlp->ahcictl_ahci_acc_handle);
}
if (attach_state & AHCI_ATTACH_STATE_FMA) {
ahci_fm_fini(ahci_ctlp);
}
if (attach_state & AHCI_ATTACH_STATE_STATEP_ALLOC) {
ddi_soft_state_free(ahci_statep, instance);
}
return (DDI_FAILURE);
}
static int
ahci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
{
ahci_ctl_t *ahci_ctlp;
int instance;
int ret;
instance = ddi_get_instance(dip);
ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_detach enter", NULL);
switch (cmd) {
case DDI_DETACH:
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_disable_all_intrs(ahci_ctlp);
mutex_exit(&ahci_ctlp->ahcictl_mutex);
ret = ahci_unregister_sata_hba_tran(ahci_ctlp);
if (ret != AHCI_SUCCESS) {
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_enable_all_intrs(ahci_ctlp);
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (DDI_FAILURE);
}
ahci_em_fini(ahci_ctlp);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
(void) untimeout(ahci_ctlp->ahcictl_timeout_id);
ahci_ctlp->ahcictl_timeout_id = 0;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
ahci_uninitialize_controller(ahci_ctlp);
ahci_rem_intrs(ahci_ctlp);
ahci_dealloc_ports_state(ahci_ctlp);
mutex_destroy(&ahci_ctlp->ahcictl_mutex);
pci_config_teardown(&ahci_ctlp->ahcictl_pci_conf_handle);
ddi_regs_map_free(&ahci_ctlp->ahcictl_ahci_acc_handle);
ahci_fm_fini(ahci_ctlp);
ddi_soft_state_free(ahci_statep, instance);
return (DDI_SUCCESS);
case DDI_SUSPEND:
mutex_enter(&ahci_ctlp->ahcictl_mutex);
if (ahci_ctlp->ahcictl_flags & AHCI_SUSPEND) {
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (DDI_SUCCESS);
}
ahci_ctlp->ahcictl_flags |= AHCI_SUSPEND;
if (ahci_ctlp->ahcictl_timeout_id) {
(void) untimeout(ahci_ctlp->ahcictl_timeout_id);
ahci_ctlp->ahcictl_timeout_id = 0;
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
ahci_em_suspend(ahci_ctlp);
ahci_drain_ports_taskq(ahci_ctlp);
ahci_uninitialize_controller(ahci_ctlp);
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
static int
ahci_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
void *arg, void **result)
{
#ifndef __lock_lint
_NOTE(ARGUNUSED(dip))
#endif
ahci_ctl_t *ahci_ctlp;
int instance;
dev_t dev;
dev = (dev_t)arg;
instance = getminor(dev);
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
if (ahci_ctlp != NULL) {
*result = ahci_ctlp->ahcictl_dip;
return (DDI_SUCCESS);
} else {
*result = NULL;
return (DDI_FAILURE);
}
case DDI_INFO_DEVT2INSTANCE:
*(int *)result = instance;
break;
default:
break;
}
return (DDI_SUCCESS);
}
static int
ahci_register_sata_hba_tran(ahci_ctl_t *ahci_ctlp, uint32_t cap_status)
{
struct sata_hba_tran *sata_hba_tran;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_register_sata_hba_tran enter", NULL);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
sata_hba_tran = kmem_zalloc(sizeof (sata_hba_tran_t), KM_SLEEP);
sata_hba_tran->sata_tran_hba_rev = SATA_TRAN_HBA_REV;
sata_hba_tran->sata_tran_hba_dip = ahci_ctlp->ahcictl_dip;
sata_hba_tran->sata_tran_hba_dma_attr =
&ahci_ctlp->ahcictl_buffer_dma_attr;
sata_hba_tran->sata_tran_hba_num_cports =
ahci_ctlp->ahcictl_num_implemented_ports;
sata_hba_tran->sata_tran_hba_features_support = SATA_CTLF_ATAPI;
if (cap_status & AHCI_HBA_CAP_PMD) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_PIO_MDRQ;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "HBA supports multiple "
"DRQ block data transfer for PIO command protocol", NULL);
}
if ((cap_status & AHCI_HBA_CAP_SNCQ) &&
!(ahci_ctlp->ahcictl_cap & AHCI_CAP_NO_MCMDLIST_NONQUEUE)) {
sata_hba_tran->sata_tran_hba_features_support |= SATA_CTLF_NCQ;
ahci_ctlp->ahcictl_cap |= AHCI_CAP_NCQ;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "HBA supports Native "
"Command Queuing", NULL);
}
if (cap_status & AHCI_HBA_CAP_SPM) {
sata_hba_tran->sata_tran_hba_features_support |=
SATA_CTLF_PORT_MULTIPLIER;
if (cap_status & AHCI_HBA_CAP_FBSS) {
sata_hba_tran->sata_tran_hba_features_support |=
SATA_CTLF_PMULT_FBS;
}
}
sata_hba_tran->sata_tran_hba_qdepth = ahci_ctlp->ahcictl_num_cmd_slots;
sata_hba_tran->sata_tran_probe_port = ahci_tran_probe_port;
sata_hba_tran->sata_tran_start = ahci_tran_start;
sata_hba_tran->sata_tran_abort = ahci_tran_abort;
sata_hba_tran->sata_tran_reset_dport = ahci_tran_reset_dport;
sata_hba_tran->sata_tran_hotplug_ops = &ahci_tran_hotplug_ops;
#ifdef __lock_lint
sata_hba_tran->sata_tran_selftest = ahci_selftest;
#endif
sata_hba_tran->sata_tran_pwrmgt_ops = NULL;
sata_hba_tran->sata_tran_ioctl = ahci_em_ioctl;
ahci_ctlp->ahcictl_sata_hba_tran = sata_hba_tran;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
if (sata_hba_attach(ahci_ctlp->ahcictl_dip, sata_hba_tran, DDI_ATTACH)
!= DDI_SUCCESS) {
kmem_free((void *)sata_hba_tran, sizeof (sata_hba_tran_t));
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_ctlp->ahcictl_sata_hba_tran = NULL;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (AHCI_FAILURE);
}
return (AHCI_SUCCESS);
}
static int
ahci_unregister_sata_hba_tran(ahci_ctl_t *ahci_ctlp)
{
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_unregister_sata_hba_tran enter", NULL);
if (sata_hba_detach(ahci_ctlp->ahcictl_dip, DDI_DETACH) !=
DDI_SUCCESS) {
return (AHCI_FAILURE);
}
kmem_free((void *)ahci_ctlp->ahcictl_sata_hba_tran,
sizeof (sata_hba_tran_t));
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_ctlp->ahcictl_sata_hba_tran = NULL;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (AHCI_SUCCESS);
}
#define SET_PORTSTR(str, addrp) \
if (AHCI_ADDR_IS_PORT(addrp)) \
(void) sprintf((str), "%d", (addrp)->aa_port); \
else if (AHCI_ADDR_IS_PMULT(addrp)) \
(void) sprintf((str), "%d (pmult)", (addrp)->aa_port); \
else \
(void) sprintf((str), "%d:%d", (addrp)->aa_port, \
(addrp)->aa_pmport);
static int
ahci_tran_probe_port(dev_info_t *dip, sata_device_t *sd)
{
ahci_ctl_t *ahci_ctlp;
ahci_port_t *ahci_portp;
ahci_addr_t addr, pmult_addr;
uint8_t cport = sd->satadev_addr.cport;
char portstr[10];
uint8_t device_type;
uint32_t port_state;
uint8_t port;
int rval = SATA_SUCCESS, rval_init;
port_state = 0;
ahci_ctlp = ddi_get_soft_state(ahci_statep, ddi_get_instance(dip));
port = ahci_ctlp->ahcictl_cport_to_port[cport];
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
ahci_get_ahci_addr(ahci_ctlp, sd, &addr);
ASSERT(AHCI_ADDR_IS_VALID(&addr));
SET_PORTSTR(portstr, &addr);
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_tran_probe_port enter: port %s", portstr);
if ((AHCI_ADDR_IS_PMULT(&addr) || AHCI_ADDR_IS_PMPORT(&addr)) &&
(ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT ||
ahci_portp->ahciport_pmult_info == NULL)) {
AHCIDBG(AHCIDBG_PMULT, ahci_ctlp,
"ahci_tran_probe_port: "
"pmult is removed from port %s", portstr);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_FAILURE);
}
if (sd->satadev_addr.qual & SATA_ADDR_PMULT_SPEC) {
AHCI_ADDR_SET_PMULT(&pmult_addr, port);
rval_init = ahci_initialize_pmult(ahci_ctlp,
ahci_portp, &pmult_addr, sd);
if (rval_init != AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_PMULT, ahci_ctlp,
"ahci_tran_probe_port: "
"pmult initialization failed.", NULL);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_FAILURE);
}
} else if (sd->satadev_addr.qual & SATA_ADDR_PMULT) {
(void) ahci_probe_pmult(ahci_ctlp, ahci_portp, &addr);
} else if (sd->satadev_addr.qual & (SATA_ADDR_PMPORT |
SATA_ADDR_DPMPORT)) {
if (ahci_probe_pmport(ahci_ctlp, ahci_portp,
&addr, sd) != AHCI_SUCCESS) {
rval = SATA_FAILURE;
goto out;
}
}
port_state = AHCIPORT_GET_STATE(ahci_portp, &addr);
switch (port_state) {
case SATA_PSTATE_FAILED:
sd->satadev_state = SATA_PSTATE_FAILED;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_probe_port: port %s PORT FAILED", portstr);
goto out;
case SATA_PSTATE_SHUTDOWN:
sd->satadev_state = SATA_PSTATE_SHUTDOWN;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_probe_port: port %s PORT SHUTDOWN", portstr);
goto out;
case SATA_PSTATE_PWROFF:
sd->satadev_state = SATA_PSTATE_PWROFF;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_probe_port: port %s PORT PWROFF", portstr);
goto out;
case SATA_PSTATE_PWRON:
sd->satadev_state = SATA_PSTATE_PWRON;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_tran_probe_port: port %s PORT PWRON", portstr);
break;
default:
sd->satadev_state = port_state;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_tran_probe_port: port %s PORT NORMAL %x",
portstr, port_state);
break;
}
device_type = AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr);
switch (device_type) {
case SATA_DTYPE_ATADISK:
sd->satadev_type = SATA_DTYPE_ATADISK;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_tran_probe_port: port %s DISK found", portstr);
break;
case SATA_DTYPE_ATAPI:
sd->satadev_type = SATA_DTYPE_ATAPI;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_tran_probe_port: port %s ATAPI found", portstr);
break;
case SATA_DTYPE_PMULT:
ASSERT(AHCI_ADDR_IS_PORT(&addr) || AHCI_ADDR_IS_PMULT(&addr));
sd->satadev_type = SATA_DTYPE_PMULT;
ASSERT(ahci_portp->ahciport_pmult_info != NULL);
sd->satadev_add_info = ahci_portp->
ahciport_pmult_info->ahcipmi_num_dev_ports;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_tran_probe_port: port %s Port Multiplier found",
portstr);
break;
case SATA_DTYPE_UNKNOWN:
sd->satadev_type = SATA_DTYPE_UNKNOWN;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_tran_probe_port: port %s Unknown device found",
portstr);
break;
default:
sd->satadev_type = SATA_DTYPE_NONE;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_tran_probe_port: port %s No device found", portstr);
break;
}
out:
if (AHCI_ADDR_IS_PORT(&addr) || AHCI_ADDR_IS_PMULT(&addr)) {
ahci_update_sata_registers(ahci_ctlp, port, sd);
} else if (AHCI_ADDR_IS_PMPORT(&addr)) {
if (port_state & SATA_STATE_READY)
if (ahci_update_pmult_pscr(ahci_ctlp,
&addr, sd) != AHCI_SUCCESS)
rval = SATA_FAILURE;
}
if ((ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) ||
(ahci_check_port_handle(ahci_ctlp, port) != DDI_SUCCESS)) {
ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
DDI_SERVICE_UNAFFECTED);
rval = SATA_FAILURE;
}
mutex_exit(&ahci_portp->ahciport_mutex);
return (rval);
}
static int
ahci_tran_start(dev_info_t *dip, sata_pkt_t *spkt)
{
ahci_ctl_t *ahci_ctlp;
ahci_port_t *ahci_portp;
ahci_addr_t addr;
uint8_t cport = spkt->satapkt_device.satadev_addr.cport;
uint8_t port;
char portstr[10];
ahci_ctlp = ddi_get_soft_state(ahci_statep, ddi_get_instance(dip));
port = ahci_ctlp->ahcictl_cport_to_port[cport];
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_tran_start enter: cport %d satapkt 0x%p",
cport, (void *)spkt);
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
ahci_get_ahci_addr(ahci_ctlp, &spkt->satapkt_device, &addr);
SET_PORTSTR(portstr, &addr);
if (AHCI_ADDR_IS_PMPORT(&addr)) {
if (ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT ||
ahci_portp->ahciport_pmult_info == NULL) {
spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
spkt->satapkt_device.satadev_type = SATA_DTYPE_NONE;
spkt->satapkt_device.satadev_state = SATA_STATE_UNKNOWN;
ahci_update_sata_registers(ahci_ctlp, port,
&spkt->satapkt_device);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start returning PORT_ERROR while "
"pmult removed: port: %s", portstr);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_PORT_ERROR);
}
if (!(AHCIPORT_GET_STATE(ahci_portp, &addr) &
SATA_STATE_READY)) {
if (!ddi_in_panic() ||
ahci_initialize_pmport(ahci_ctlp,
ahci_portp, &addr) != AHCI_SUCCESS) {
spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
spkt->satapkt_device.satadev_type =
AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr);
spkt->satapkt_device.satadev_state =
AHCIPORT_GET_STATE(ahci_portp, &addr);
ahci_update_sata_registers(ahci_ctlp, port,
&spkt->satapkt_device);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start returning PORT_ERROR "
"while sub-link is not initialized "
"at port: %s", portstr);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_PORT_ERROR);
}
}
}
if (AHCIPORT_GET_STATE(ahci_portp, &addr) & SATA_PSTATE_FAILED ||
AHCIPORT_GET_STATE(ahci_portp, &addr) & SATA_PSTATE_SHUTDOWN||
AHCIPORT_GET_STATE(ahci_portp, &addr) & SATA_PSTATE_PWROFF) {
spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
spkt->satapkt_device.satadev_state =
ahci_portp->ahciport_port_state;
ahci_update_sata_registers(ahci_ctlp, port,
&spkt->satapkt_device);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start returning PORT_ERROR while "
"port in FAILED/SHUTDOWN/PWROFF state: "
"port: %s", portstr);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_PORT_ERROR);
}
if (AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr) == SATA_DTYPE_NONE) {
spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
spkt->satapkt_device.satadev_type = SATA_DTYPE_NONE;
spkt->satapkt_device.satadev_state =
ahci_portp->ahciport_port_state;
ahci_update_sata_registers(ahci_ctlp, port,
&spkt->satapkt_device);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start returning PORT_ERROR while "
"no device attached: port: %s", portstr);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_PORT_ERROR);
}
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start returning BUSY while "
"executing READ/WRITE PORT-MULT command: "
"port: %s", portstr);
spkt->satapkt_reason = SATA_PKT_BUSY;
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_BUSY);
}
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_HOTPLUG) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start returning BUSY while "
"hot-plug in progress: port: %s", portstr);
spkt->satapkt_reason = SATA_PKT_BUSY;
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_BUSY);
}
if (spkt->satapkt_cmd.satacmd_flags.sata_clear_dev_reset) {
ahci_portp->ahciport_reset_in_progress = 0;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start [CLEAR] the "
"reset_in_progress for port: %d", port);
}
if (ahci_portp->ahciport_reset_in_progress &&
! spkt->satapkt_cmd.satacmd_flags.sata_ignore_dev_reset &&
! ddi_in_panic()) {
spkt->satapkt_reason = SATA_PKT_BUSY;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start returning BUSY while "
"reset in progress: port: %d", port);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_BUSY);
}
#ifdef AHCI_DEBUG
if (spkt->satapkt_cmd.satacmd_flags.sata_ignore_dev_reset) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start: packet 0x%p [PASSTHRU] at port %d",
spkt, port);
}
#endif
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
spkt->satapkt_reason = SATA_PKT_BUSY;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start returning BUSY while "
"mopping in progress: port: %d", port);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_BUSY);
}
if (ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) {
ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
DDI_SERVICE_UNAFFECTED);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_BUSY);
}
if (spkt->satapkt_op_mode &
(SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING)) {
if (!(spkt->satapkt_op_mode & SATA_OPMODE_POLLING) &&
servicing_interrupt()) {
spkt->satapkt_reason = SATA_PKT_BUSY;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_start returning BUSY while "
"sending SYNC mode under interrupt context: "
"port : %d", port);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_BUSY);
}
if (ahci_do_sync_start(ahci_ctlp, ahci_portp, &addr,
spkt) == AHCI_FAILURE) {
goto fail_out;
}
} else {
if (ahci_deliver_satapkt(ahci_ctlp, ahci_portp, &addr, spkt)
== AHCI_FAILURE) {
spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
goto fail_out;
}
}
AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "ahci_tran_start "
"sata tran accepted: port %s", portstr);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_ACCEPTED);
fail_out:
if (ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS ||
ahci_check_port_handle(ahci_ctlp, port) != DDI_SUCCESS) {
spkt->satapkt_device.satadev_type =
AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr);
spkt->satapkt_device.satadev_state =
AHCIPORT_GET_STATE(ahci_portp, &addr);
spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_PORT_ERROR);
}
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_tran_start "
"return QUEUE_FULL: port %d", port);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_TRAN_QUEUE_FULL);
}
static int
ahci_do_sync_start(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp, sata_pkt_t *spkt)
{
int pkt_timeout_ticks;
uint32_t timeout_tags;
int rval;
int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
uint8_t port = addrp->aa_port;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_do_sync_start enter: "
"port %d:%d spkt 0x%p", port, addrp->aa_pmport, spkt);
if (spkt->satapkt_op_mode & SATA_OPMODE_POLLING) {
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_POLLING;
if ((rval = ahci_deliver_satapkt(ahci_ctlp, ahci_portp,
addrp, spkt)) == AHCI_FAILURE) {
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_POLLING;
return (rval);
}
pkt_timeout_ticks =
drv_usectohz((clock_t)spkt->satapkt_time * 1000000);
while (spkt->satapkt_reason == SATA_PKT_BUSY) {
mutex_exit(&ahci_portp->ahciport_mutex);
ahci_port_intr(ahci_ctlp, ahci_portp, port);
mutex_enter(&ahci_portp->ahciport_mutex);
if (spkt->satapkt_reason != SATA_PKT_BUSY)
break;
mutex_exit(&ahci_portp->ahciport_mutex);
drv_usecwait(AHCI_1MS_USECS);
mutex_enter(&ahci_portp->ahciport_mutex);
pkt_timeout_ticks -= AHCI_1MS_TICKS;
if (pkt_timeout_ticks < 0) {
cmn_err(CE_WARN, "!ahci%d: ahci_do_sync_start "
"port %d satapkt 0x%p timed out\n",
instance, port, (void *)spkt);
timeout_tags = (0x1 << rval);
mutex_exit(&ahci_portp->ahciport_mutex);
ahci_timeout_pkts(ahci_ctlp, ahci_portp,
port, timeout_tags);
mutex_enter(&ahci_portp->ahciport_mutex);
}
}
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_POLLING;
return (AHCI_SUCCESS);
} else {
if ((rval = ahci_deliver_satapkt(ahci_ctlp, ahci_portp,
addrp, spkt)) == AHCI_FAILURE)
return (rval);
#if AHCI_DEBUG
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
ASSERT(rval == 0);
#endif
while (spkt->satapkt_reason == SATA_PKT_BUSY)
cv_wait(&ahci_portp->ahciport_cv,
&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
}
static int
ahci_claim_free_slot(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp, int command_type)
{
uint32_t port_cmd_issue;
uint32_t free_slots;
int slot;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_claim_free_slot enter "
"ahciport_pending_tags = 0x%x "
"ahciport_pending_ncq_tags = 0x%x",
ahci_portp->ahciport_pending_tags,
ahci_portp->ahciport_pending_ncq_tags);
free_slots = 0;
if (command_type == AHCI_NON_NCQ_CMD) {
if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_INFO|AHCIDBG_NCQ, ahci_ctlp,
"ahci_claim_free_slot: there is still pending "
"queued command(s) in the command list, "
"so no available slot for the non-queued "
"command", NULL);
return (AHCI_FAILURE);
}
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"ahci_claim_free_slot: there is still pending "
"read/write port-mult command(s) in command list, "
"so no available slot for the non-queued command",
NULL);
return (AHCI_FAILURE);
}
if ((ahci_ctlp->ahcictl_cap & AHCI_CAP_NO_MCMDLIST_NONQUEUE) &&
NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_claim_free_slot: HBA cannot support multiple-"
"use of the command list for non-queued commands",
NULL);
return (AHCI_FAILURE);
}
free_slots = (~ahci_portp->ahciport_pending_tags) &
AHCI_SLOT_MASK(ahci_ctlp);
} else if (command_type == AHCI_NCQ_CMD) {
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_INFO|AHCIDBG_NCQ, ahci_ctlp,
"ahci_claim_free_slot: there is still pending "
"non-queued command(s) in the command list, "
"so no available slot for the queued command",
NULL);
return (AHCI_FAILURE);
}
if (AHCIPORT_DEV_TYPE(ahci_portp, addrp) == SATA_DTYPE_PMULT) {
ASSERT(ahci_portp->ahciport_pmult_info != NULL);
if (!(ahci_ctlp->ahcictl_cap & AHCI_CAP_PMULT_FBSS) &&
NCQ_CMD_IN_PROGRESS(ahci_portp) &&
AHCIPORT_NCQ_PMPORT(ahci_portp) !=
addrp->aa_pmport) {
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_claim_free_slot: there is still "
"pending queued command(s) in the "
"command list for another Port Multiplier "
"port, so no available slot.", NULL);
return (AHCI_FAILURE);
}
}
free_slots = (~ahci_portp->ahciport_pending_ncq_tags) &
AHCI_NCQ_SLOT_MASK(ahci_portp);
} else if (command_type == AHCI_ERR_RETRI_CMD) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_claim_free_slot: slot 0 is allocated for REQUEST "
"SENSE or READ LOG EXT command", NULL);
slot = 0;
goto out;
} else if (command_type == AHCI_RDWR_PMULT_CMD) {
port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, addrp->aa_port));
if (port_cmd_issue != 0) {
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"ahci_claim_free_slot: there is still pending "
"command(s) in command list (0x%x/0x%x, PxCI %x),"
"so no available slot for R/W PMULT command.",
NON_NCQ_CMD_IN_PROGRESS(ahci_portp),
NCQ_CMD_IN_PROGRESS(ahci_portp),
port_cmd_issue);
return (AHCI_FAILURE);
}
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_claim_free_slot: slot 0 is allocated for "
"READ/WRITE PORTMULT command", NULL);
slot = 0;
goto out;
}
slot = ddi_ffs(free_slots) - 1;
if (slot == -1) {
AHCIDBG(AHCIDBG_VERBOSE, ahci_ctlp,
"ahci_claim_free_slot: no empty slots", NULL);
return (AHCI_FAILURE);
}
if (command_type == AHCI_NCQ_CMD) {
ahci_portp->ahciport_pending_ncq_tags |= (0x1 << slot);
if (AHCI_ADDR_IS_PMPORT(addrp)) {
ASSERT(ahci_portp->ahciport_pmult_info != NULL);
AHCIPORT_NCQ_PMPORT(ahci_portp) = addrp->aa_pmport;
}
}
ahci_portp->ahciport_pending_tags |= (0x1 << slot);
out:
AHCIDBG(AHCIDBG_VERBOSE, ahci_ctlp,
"ahci_claim_free_slot: found slot: 0x%x", slot);
return (slot);
}
static int
ahci_deliver_satapkt(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp, sata_pkt_t *spkt)
{
int cmd_slot;
sata_cmd_t *scmd;
ahci_fis_h2d_register_t *h2d_register_fisp;
ahci_cmd_table_t *cmd_table;
ahci_cmd_header_t *cmd_header;
int ncookies;
int i;
int command_type = AHCI_NON_NCQ_CMD;
int ncq_qdepth;
int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
uint8_t port, pmport;
#if AHCI_DEBUG
uint32_t *ptr;
uint8_t *ptr2;
#endif
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
port = addrp->aa_port;
pmport = addrp->aa_pmport;
spkt->satapkt_reason = SATA_PKT_BUSY;
scmd = &spkt->satapkt_cmd;
if (scmd->satacmd_cmd_reg == SATAC_READ_FPDMA_QUEUED ||
scmd->satacmd_cmd_reg == SATAC_WRITE_FPDMA_QUEUED) {
command_type = AHCI_NCQ_CMD;
ncq_qdepth = scmd->satacmd_flags.sata_max_queue_depth + 1;
if (ahci_portp->ahciport_max_ncq_tags == 0) {
ahci_portp->ahciport_max_ncq_tags = ncq_qdepth;
AHCIDBG(AHCIDBG_NCQ, ahci_ctlp,
"ahci_deliver_satapkt: port %d the max tags for "
"NCQ command is %d", port, ncq_qdepth);
} else {
if (ncq_qdepth != ahci_portp->ahciport_max_ncq_tags) {
cmn_err(CE_WARN, "!ahci%d: ahci_deliver_satapkt"
" port %d the max tag for NCQ command is "
"requested to change from %d to %d, at the"
" moment the driver doesn't support the "
"dynamic change so it's going to "
"still use the previous tag value",
instance, port,
ahci_portp->ahciport_max_ncq_tags,
ncq_qdepth);
}
}
}
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
command_type = AHCI_ERR_RETRI_CMD;
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp))
command_type = AHCI_RDWR_PMULT_CMD;
cmd_slot = ahci_claim_free_slot(ahci_ctlp, ahci_portp,
addrp, command_type);
if (cmd_slot == AHCI_FAILURE) {
AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "no free command slot", NULL);
return (AHCI_FAILURE);
}
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INFO, ahci_ctlp,
"ahci_deliver_satapkt enter: cmd_reg: 0x%x, cmd_slot: 0x%x, "
"port: %d, satapkt: 0x%p", scmd->satacmd_cmd_reg,
cmd_slot, port, (void *)spkt);
cmd_table = ahci_portp->ahciport_cmd_tables[cmd_slot];
bzero((void *)cmd_table, ahci_cmd_table_size);
h2d_register_fisp =
&(cmd_table->ahcict_command_fis.ahcifc_fis.ahcifc_h2d_register);
SET_FIS_TYPE(h2d_register_fisp, AHCI_H2D_REGISTER_FIS_TYPE);
if (AHCI_ADDR_IS_PMULT(addrp) || AHCI_ADDR_IS_PMPORT(addrp))
SET_FIS_PMP(h2d_register_fisp, pmport);
SET_FIS_CDMDEVCTL(h2d_register_fisp, 1);
SET_FIS_COMMAND(h2d_register_fisp, scmd->satacmd_cmd_reg);
SET_FIS_FEATURES(h2d_register_fisp, scmd->satacmd_features_reg);
SET_FIS_SECTOR_COUNT(h2d_register_fisp, scmd->satacmd_sec_count_lsb);
switch (scmd->satacmd_addr_type) {
case 0:
case ATA_ADDR_LBA:
case ATA_ADDR_LBA28:
SET_FIS_SECTOR(h2d_register_fisp, scmd->satacmd_lba_low_lsb);
SET_FIS_CYL_LOW(h2d_register_fisp, scmd->satacmd_lba_mid_lsb);
SET_FIS_CYL_HI(h2d_register_fisp, scmd->satacmd_lba_high_lsb);
SET_FIS_DEV_HEAD(h2d_register_fisp, scmd->satacmd_device_reg);
break;
case ATA_ADDR_LBA48:
SET_FIS_SECTOR(h2d_register_fisp, scmd->satacmd_lba_low_lsb);
SET_FIS_CYL_LOW(h2d_register_fisp, scmd->satacmd_lba_mid_lsb);
SET_FIS_CYL_HI(h2d_register_fisp, scmd->satacmd_lba_high_lsb);
SET_FIS_SECTOR_EXP(h2d_register_fisp,
scmd->satacmd_lba_low_msb);
SET_FIS_CYL_LOW_EXP(h2d_register_fisp,
scmd->satacmd_lba_mid_msb);
SET_FIS_CYL_HI_EXP(h2d_register_fisp,
scmd->satacmd_lba_high_msb);
SET_FIS_DEV_HEAD(h2d_register_fisp,
scmd->satacmd_device_reg);
SET_FIS_SECTOR_COUNT_EXP(h2d_register_fisp,
scmd->satacmd_sec_count_msb);
SET_FIS_FEATURES_EXP(h2d_register_fisp,
scmd->satacmd_features_reg_ext);
break;
}
if (command_type == AHCI_NCQ_CMD) {
SET_FIS_SECTOR_COUNT(h2d_register_fisp,
(cmd_slot << SATA_TAG_QUEUING_SHIFT));
}
ncookies = scmd->satacmd_num_dma_cookies;
AHCIDBG(AHCIDBG_PRDT, ahci_ctlp,
"ncookies = 0x%x, ahci_dma_prdt_number = 0x%x",
ncookies, ahci_dma_prdt_number);
ASSERT(ncookies <= ahci_dma_prdt_number);
ahci_portp->ahciport_prd_bytecounts[cmd_slot] = 0;
for (i = 0; i < ncookies; i++) {
cmd_table->ahcict_prdt[i].ahcipi_data_base_addr =
scmd->satacmd_dma_cookie_list[i]._dmu._dmac_la[0];
cmd_table->ahcict_prdt[i].ahcipi_data_base_addr_upper =
scmd->satacmd_dma_cookie_list[i]._dmu._dmac_la[1];
cmd_table->ahcict_prdt[i].ahcipi_descr_info =
scmd->satacmd_dma_cookie_list[i].dmac_size - 1;
ahci_portp->ahciport_prd_bytecounts[cmd_slot] +=
scmd->satacmd_dma_cookie_list[i].dmac_size;
}
AHCIDBG(AHCIDBG_PRDT, ahci_ctlp,
"ahciport_prd_bytecounts 0x%x for cmd_slot 0x%x",
ahci_portp->ahciport_prd_bytecounts[cmd_slot], cmd_slot);
if (scmd->satacmd_cmd_reg == SATAC_PACKET) {
bcopy(scmd->satacmd_acdb, cmd_table->ahcict_atapi_cmd,
SATA_ATAPI_MAX_CDB_LEN);
}
cmd_header = &ahci_portp->ahciport_cmd_list[cmd_slot];
BZERO_DESCR_INFO(cmd_header);
BZERO_PRD_BYTE_COUNT(cmd_header);
SET_PRD_TABLE_LENGTH(cmd_header, ncookies);
SET_COMMAND_FIS_LENGTH(cmd_header, AHCI_H2D_REGISTER_FIS_LENGTH);
if (AHCI_ADDR_IS_PMULT(addrp) || AHCI_ADDR_IS_PMPORT(addrp))
SET_PORT_MULTI_PORT(cmd_header, pmport);
AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "command data direction is "
"sata_data_direction = 0x%x",
scmd->satacmd_flags.sata_data_direction);
if (scmd->satacmd_cmd_reg == SATAC_PACKET)
SET_ATAPI(cmd_header, AHCI_CMDHEAD_ATAPI);
if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE)
SET_WRITE(cmd_header, AHCI_CMDHEAD_DATA_WRITE);
if (command_type != AHCI_NCQ_CMD)
SET_PREFETCHABLE(cmd_header, AHCI_CMDHEAD_PREFETCHABLE);
if (!ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
!RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp))
ahci_portp->ahciport_slot_pkts[cmd_slot] = spkt;
ahci_portp->ahciport_slot_timeout[cmd_slot] = spkt->satapkt_time;
if (ahci_portp->ahciport_slot_timeout[cmd_slot] <=
ahci_watchdog_timeout)
ahci_portp->ahciport_slot_timeout[cmd_slot] +=
ahci_watchdog_timeout;
#if AHCI_DEBUG
if (ahci_debug_flags & AHCIDBG_ATACMD &&
scmd->satacmd_cmd_reg != SATAC_PACKET ||
ahci_debug_flags & AHCIDBG_ATAPICMD &&
scmd->satacmd_cmd_reg == SATAC_PACKET) {
ahci_log(ahci_ctlp, CE_WARN, "\n");
ahci_log(ahci_ctlp, CE_WARN, "Command header&table for spkt "
"0x%p cmd_reg 0x%x port %d", spkt,
scmd->satacmd_cmd_reg, port);
ptr = (uint32_t *)cmd_header;
ahci_log(ahci_ctlp, CE_WARN,
" Command Header:%8x %8x %8x %8x",
ptr[0], ptr[1], ptr[2], ptr[3]);
ptr = (uint32_t *)h2d_register_fisp;
ahci_log(ahci_ctlp, CE_WARN,
" Command FIS: %8x %8x %8x %8x",
ptr[0], ptr[1], ptr[2], ptr[3]);
ptr2 = (uint8_t *)&(cmd_table->ahcict_atapi_cmd);
for (i = 0; i < SATA_ATAPI_MAX_CDB_LEN/8; i++)
if (ahci_debug_flags & AHCIDBG_ATAPICMD)
ahci_log(ahci_ctlp, CE_WARN,
" ATAPI command: %2x %2x %2x %2x "
"%2x %2x %2x %2x",
ptr2[8 * i], ptr2[8 * i + 1],
ptr2[8 * i + 2], ptr2[8 * i + 3],
ptr2[8 * i + 4], ptr2[8 * i + 5],
ptr2[8 * i + 6], ptr2[8 * i + 7]);
for (i = 0; i < ncookies; i++) {
ptr = (uint32_t *)&(cmd_table->ahcict_prdt[i]);
ahci_log(ahci_ctlp, CE_WARN,
" Cookie %d: %8x %8x %8x %8x",
i, ptr[0], ptr[1], ptr[2], ptr[3]);
}
}
#endif
(void) ddi_dma_sync(
ahci_portp->ahciport_cmd_tables_dma_handle[cmd_slot],
0,
ahci_cmd_table_size,
DDI_DMA_SYNC_FORDEV);
(void) ddi_dma_sync(ahci_portp->ahciport_cmd_list_dma_handle,
cmd_slot * sizeof (ahci_cmd_header_t),
sizeof (ahci_cmd_header_t),
DDI_DMA_SYNC_FORDEV);
if ((ahci_check_dma_handle(ahci_portp->
ahciport_cmd_tables_dma_handle[cmd_slot]) != DDI_FM_OK) ||
ahci_check_dma_handle(ahci_portp->
ahciport_cmd_list_dma_handle) != DDI_FM_OK) {
ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
DDI_SERVICE_UNAFFECTED);
return (AHCI_FAILURE);
}
if (command_type == AHCI_NCQ_CMD) {
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port),
(0x1 << cmd_slot));
}
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port),
(0x1 << cmd_slot));
AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "ahci_deliver_satapkt "
"exit: port %d", port);
if (ahci_check_acc_handle(ahci_ctlp->
ahcictl_ahci_acc_handle) != DDI_FM_OK) {
ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
DDI_SERVICE_UNAFFECTED);
return (AHCI_FAILURE);
}
return (cmd_slot);
}
static int
ahci_tran_abort(dev_info_t *dip, sata_pkt_t *spkt, int flag)
{
ahci_ctl_t *ahci_ctlp;
ahci_port_t *ahci_portp;
uint32_t slot_status = 0;
uint32_t aborted_tags = 0;
uint32_t finished_tags = 0;
uint8_t cport = spkt->satapkt_device.satadev_addr.cport;
uint8_t port;
int tmp_slot;
int instance = ddi_get_instance(dip);
ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
port = ahci_ctlp->ahcictl_cport_to_port[cport];
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_tran_abort enter: port %d", port);
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_tran_abort: port %d is in "
"mopping process, so just return directly ", port);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_SUCCESS);
}
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_RDWR_PMULT) {
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_tran_abort: port %d is reading/writing "
"port multiplier, so just return directly ", port);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_SUCCESS);
}
if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED |
ahci_portp->ahciport_port_state & SATA_PSTATE_SHUTDOWN |
ahci_portp->ahciport_port_state & SATA_PSTATE_PWROFF) {
spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
spkt->satapkt_device.satadev_state =
ahci_portp->ahciport_port_state;
ahci_update_sata_registers(ahci_ctlp, port,
&spkt->satapkt_device);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_abort returning SATA_FAILURE while "
"port in FAILED/SHUTDOWN/PWROFF state: "
"port: %d", port);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_FAILURE);
}
if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
spkt->satapkt_device.satadev_type = SATA_DTYPE_NONE;
spkt->satapkt_device.satadev_state =
ahci_portp->ahciport_port_state;
ahci_update_sata_registers(ahci_ctlp, port,
&spkt->satapkt_device);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_abort returning SATA_FAILURE while "
"no device attached: port: %d", port);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_FAILURE);
}
if (flag == SATA_ABORT_ALL_PACKETS) {
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
aborted_tags = ahci_portp->ahciport_pending_tags;
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
aborted_tags = ahci_portp->ahciport_pending_ncq_tags;
cmn_err(CE_NOTE, "!ahci%d: ahci port %d abort all packets",
instance, port);
} else {
aborted_tags = 0xffffffff;
for (tmp_slot = 0;
tmp_slot < ahci_ctlp->ahcictl_num_cmd_slots; tmp_slot++) {
if (ahci_portp->ahciport_slot_pkts[tmp_slot] == spkt) {
aborted_tags = (0x1 << tmp_slot);
break;
}
}
if (aborted_tags == 0xffffffff) {
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"Cannot find the aborting pkt 0x%p on the "
"pending list", (void *)spkt);
ahci_update_sata_registers(ahci_ctlp, port,
&spkt->satapkt_device);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_FAILURE);
}
cmn_err(CE_NOTE, "!ahci%d: ahci port %d abort satapkt 0x%p",
instance, port, (void *)spkt);
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
ahci_portp->ahciport_mop_in_progress++;
(void) ahci_restart_port_wait_till_ready(ahci_ctlp,
ahci_portp, port, 0, NULL);
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_tags &
~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_ncq_tags &
~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
aborted_tags &= ~finished_tags;
ahci_mop_commands(ahci_ctlp,
ahci_portp,
slot_status,
0,
0,
aborted_tags,
0);
ahci_update_sata_registers(ahci_ctlp, port, &spkt->satapkt_device);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_SUCCESS);
}
static int
ahci_reset_device_reject_pkts(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, ahci_addr_t *addrp)
{
uint32_t slot_status = 0;
uint32_t reset_tags = 0;
uint32_t finished_tags = 0;
uint8_t port = addrp->aa_port;
sata_device_t sdevice;
int ret;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_reset_device_reject_pkts on port: %d", port);
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_reset_device_reject_pkts: port %d is in "
"mopping process, so return directly ", port);
return (SATA_SUCCESS);
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
}
if (ahci_software_reset(ahci_ctlp, ahci_portp, addrp)
!= AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"Try to do a port reset after software "
"reset failed", port);
ret = ahci_port_reset(ahci_ctlp, ahci_portp, addrp);
if (ret != AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_reset_device_reject_pkts: port %d "
"failed", port);
return (SATA_FAILURE);
}
}
ahci_portp->ahciport_reset_in_progress = 1;
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
ahci_portp->ahciport_mop_in_progress++;
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
sdevice.satadev_addr.pmport = 0;
sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
sdevice.satadev_state = SATA_DSTATE_RESET |
SATA_DSTATE_PWR_ACTIVE;
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
&sdevice,
SATA_EVNT_DEVICE_RESET);
mutex_enter(&ahci_portp->ahciport_mutex);
AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
"port %d sending event up: SATA_EVNT_DEVICE_RESET", port);
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_tags &
~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_ncq_tags &
~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
reset_tags &= ~finished_tags;
ahci_mop_commands(ahci_ctlp,
ahci_portp,
slot_status,
0,
0,
0,
reset_tags);
return (SATA_SUCCESS);
}
static int
ahci_reset_pmdevice_reject_pkts(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, ahci_addr_t *addrp)
{
uint32_t finished_tags = 0, reset_tags = 0, slot_status = 0;
uint8_t port = addrp->aa_port;
uint8_t pmport = addrp->aa_pmport;
sata_device_t sdevice;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_PMULT, ahci_ctlp,
"ahci_reset_pmdevice_reject_pkts at port %d:%d", port, pmport);
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_reset_pmdevice_reject_pkts: port %d is in "
"mopping process, so return directly ", port);
return (SATA_SUCCESS);
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
}
if (ahci_software_reset(ahci_ctlp, ahci_portp, addrp)
!= AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"Try to do a port reset after software "
"reset failed", port);
return (SATA_FAILURE);
}
ahci_portp->ahciport_reset_in_progress = 1;
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
ahci_portp->ahciport_mop_in_progress++;
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
sdevice.satadev_addr.pmport = pmport;
if (AHCI_ADDR_IS_PMULT(addrp))
sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
else
sdevice.satadev_addr.qual = SATA_ADDR_DPMPORT;
sdevice.satadev_state = SATA_DSTATE_RESET |
SATA_DSTATE_PWR_ACTIVE;
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
&sdevice,
SATA_EVNT_DEVICE_RESET);
mutex_enter(&ahci_portp->ahciport_mutex);
AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
"port %d:%d sending event up: SATA_EVNT_DEVICE_RESET",
port, pmport);
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_tags &
~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_ncq_tags &
~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
reset_tags &= ~finished_tags;
AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
"reset_tags = %x, finished_tags = %x, slot_status = %x",
reset_tags, finished_tags, slot_status);
ahci_mop_commands(ahci_ctlp,
ahci_portp,
slot_status,
0,
0,
0,
reset_tags);
return (SATA_SUCCESS);
}
static int
ahci_reset_port_reject_pkts(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, ahci_addr_t *addrp)
{
uint32_t slot_status = 0;
uint32_t reset_tags = 0;
uint32_t finished_tags = 0;
uint8_t port = addrp->aa_port;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_reset_port_reject_pkts at port: %d", port);
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_reset_port_reject_pkts: port %d is in "
"mopping process, so return directly ", port);
return (SATA_SUCCESS);
}
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
ahci_portp->ahciport_mop_in_progress++;
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
}
if (ahci_restart_port_wait_till_ready(ahci_ctlp,
ahci_portp, port, AHCI_PORT_RESET|AHCI_RESET_NO_EVENTS_UP,
NULL) != AHCI_SUCCESS) {
ahci_portp->ahciport_mop_in_progress--;
if (ahci_portp->ahciport_mop_in_progress == 0)
ahci_portp->ahciport_flags &=
~AHCI_PORT_FLAG_MOPPING;
return (SATA_FAILURE);
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_tags &
~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_ncq_tags &
~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
reset_tags &= ~finished_tags;
ahci_mop_commands(ahci_ctlp,
ahci_portp,
slot_status,
0,
0,
0,
reset_tags);
return (SATA_SUCCESS);
}
static int
ahci_reset_hba_reject_pkts(ahci_ctl_t *ahci_ctlp)
{
ahci_port_t *ahci_portp;
uint32_t slot_status[AHCI_MAX_PORTS];
uint32_t reset_tags[AHCI_MAX_PORTS];
uint32_t finished_tags[AHCI_MAX_PORTS];
int port;
int ret = SATA_SUCCESS;
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_reset_hba_reject_pkts enter", NULL);
bzero(slot_status, sizeof (slot_status));
bzero(reset_tags, sizeof (reset_tags));
bzero(finished_tags, sizeof (finished_tags));
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
continue;
}
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
ahci_portp->ahciport_reset_in_progress = 1;
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status[port] = ddi_get32(
ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
reset_tags[port] = slot_status[port] &
AHCI_SLOT_MASK(ahci_ctlp);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"port %d: reset_tags = 0x%x pending_tags = 0x%x",
port, reset_tags[port],
ahci_portp->ahciport_pending_tags);
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status[port] = ddi_get32(
ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
reset_tags[port] = slot_status[port] &
AHCI_NCQ_SLOT_MASK(ahci_portp);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"port %d: reset_tags = 0x%x pending_tags = 0x%x",
port, reset_tags[port],
ahci_portp->ahciport_pending_tags);
}
mutex_exit(&ahci_portp->ahciport_mutex);
}
if (ahci_hba_reset(ahci_ctlp) != AHCI_SUCCESS) {
ret = SATA_FAILURE;
}
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
continue;
}
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_reset_hba_reject_pkts: port %d is in "
"mopping process, so return directly ", port);
mutex_exit(&ahci_portp->ahciport_mutex);
continue;
}
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
ahci_portp->ahciport_mop_in_progress++;
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags[port] =
ahci_portp->ahciport_pending_tags &
~slot_status[port] & AHCI_SLOT_MASK(ahci_ctlp);
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags[port] =
ahci_portp->ahciport_pending_ncq_tags &
~slot_status[port] & AHCI_NCQ_SLOT_MASK(ahci_portp);
reset_tags[port] &= ~finished_tags[port];
ahci_mop_commands(ahci_ctlp,
ahci_portp,
slot_status[port],
0,
0,
0,
reset_tags[port]);
mutex_exit(&ahci_portp->ahciport_mutex);
}
return (ret);
}
static int
ahci_tran_reset_dport(dev_info_t *dip, sata_device_t *sd)
{
ahci_ctl_t *ahci_ctlp;
ahci_port_t *ahci_portp;
ahci_addr_t addr;
uint8_t cport = sd->satadev_addr.cport;
uint8_t pmport = sd->satadev_addr.pmport;
uint8_t port;
int ret = SATA_SUCCESS;
int instance = ddi_get_instance(dip);
ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
port = ahci_ctlp->ahcictl_cport_to_port[cport];
ahci_portp = ahci_ctlp->ahcictl_ports[port];
ahci_get_ahci_addr(ahci_ctlp, sd, &addr);
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_tran_reset_dport enter: cport %d", cport);
switch (sd->satadev_addr.qual) {
case SATA_ADDR_PMPORT:
case SATA_ADDR_CPORT:
ahci_portp = ahci_ctlp->ahcictl_ports[port];
cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport "
"port %d reset port", instance, port);
mutex_enter(&ahci_portp->ahciport_mutex);
ret = ahci_reset_port_reject_pkts(ahci_ctlp, ahci_portp, &addr);
mutex_exit(&ahci_portp->ahciport_mutex);
break;
case SATA_ADDR_DPMPORT:
cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport "
"port %d:%d reset device", instance, port, pmport);
case SATA_ADDR_DCPORT:
if (sd->satadev_addr.qual == SATA_ADDR_DCPORT)
cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport "
"port %d reset device", instance, port);
mutex_enter(&ahci_portp->ahciport_mutex);
if (sd->satadev_addr.qual == SATA_ADDR_DCPORT &&
ahci_portp->ahciport_device_type == SATA_DTYPE_PMULT)
AHCI_ADDR_SET_PMULT(&addr, port);
if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED |
ahci_portp->ahciport_port_state & SATA_PSTATE_SHUTDOWN |
ahci_portp->ahciport_port_state & SATA_PSTATE_PWROFF) {
sd->satadev_state = ahci_portp->ahciport_port_state;
ahci_update_sata_registers(ahci_ctlp, port, sd);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_reset_dport returning SATA_FAILURE "
"while port in FAILED/SHUTDOWN/PWROFF state: "
"port: %d", port);
mutex_exit(&ahci_portp->ahciport_mutex);
ret = SATA_FAILURE;
break;
}
if (AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr) ==
SATA_DTYPE_NONE) {
sd->satadev_type = SATA_DTYPE_NONE;
sd->satadev_state = AHCIPORT_GET_STATE(ahci_portp,
&addr);
ahci_update_sata_registers(ahci_ctlp, port, sd);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_tran_reset_dport returning SATA_FAILURE "
"while no device attached: port: %d", port);
mutex_exit(&ahci_portp->ahciport_mutex);
ret = SATA_FAILURE;
break;
}
if (AHCI_ADDR_IS_PORT(&addr)) {
ret = ahci_reset_device_reject_pkts(ahci_ctlp,
ahci_portp, &addr);
} else {
ret = ahci_reset_pmdevice_reject_pkts(ahci_ctlp,
ahci_portp, &addr);
}
mutex_exit(&ahci_portp->ahciport_mutex);
break;
case SATA_ADDR_CNTRL:
cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport "
"reset the whole hba", instance);
ret = ahci_reset_hba_reject_pkts(ahci_ctlp);
break;
default:
ret = SATA_FAILURE;
}
return (ret);
}
static int
ahci_tran_hotplug_port_activate(dev_info_t *dip, sata_device_t *satadev)
{
ahci_ctl_t *ahci_ctlp;
ahci_port_t *ahci_portp;
ahci_addr_t addr;
uint8_t cport = satadev->satadev_addr.cport;
uint8_t pmport = satadev->satadev_addr.pmport;
uint8_t port;
int instance = ddi_get_instance(dip);
ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
port = ahci_ctlp->ahcictl_cport_to_port[cport];
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_tran_hotplug_port_activate enter: cport %d", cport);
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
ahci_get_ahci_addr(ahci_ctlp, satadev, &addr);
ASSERT(AHCI_ADDR_IS_PORT(&addr) || AHCI_ADDR_IS_PMPORT(&addr));
if (AHCI_ADDR_IS_PORT(&addr)) {
cmn_err(CE_NOTE, "!ahci%d: ahci port %d is activated",
instance, port);
ahci_enable_port_intrs(ahci_ctlp, port);
(void) ahci_restart_port_wait_till_ready(ahci_ctlp,
ahci_portp, port,
AHCI_PORT_RESET|AHCI_RESET_NO_EVENTS_UP,
NULL);
ahci_portp->ahciport_port_state |= SATA_PSTATE_PWRON;
ahci_portp->ahciport_port_state &= ~SATA_PSTATE_PWROFF;
ahci_portp->ahciport_port_state &= ~SATA_PSTATE_SHUTDOWN;
} else if (AHCI_ADDR_IS_PMPORT(&addr)) {
cmn_err(CE_NOTE, "!ahci%d: ahci port %d:%d is activated",
instance, port, pmport);
AHCIPORT_PMSTATE(ahci_portp, &addr) |= SATA_PSTATE_PWRON;
AHCIPORT_PMSTATE(ahci_portp, &addr) &=
~(SATA_PSTATE_PWROFF|SATA_PSTATE_SHUTDOWN);
}
satadev->satadev_state = ahci_portp->ahciport_port_state;
ahci_update_sata_registers(ahci_ctlp, port, satadev);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_SUCCESS);
}
static int
ahci_tran_hotplug_port_deactivate(dev_info_t *dip, sata_device_t *satadev)
{
ahci_ctl_t *ahci_ctlp;
ahci_port_t *ahci_portp;
ahci_addr_t addr;
uint8_t cport = satadev->satadev_addr.cport;
uint8_t pmport = satadev->satadev_addr.pmport;
uint8_t port;
uint32_t port_scontrol;
int instance = ddi_get_instance(dip);
ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
port = ahci_ctlp->ahcictl_cport_to_port[cport];
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_tran_hotplug_port_deactivate enter: cport %d", cport);
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
ahci_get_ahci_addr(ahci_ctlp, satadev, &addr);
ASSERT(AHCI_ADDR_IS_PORT(&addr) || AHCI_ADDR_IS_PMPORT(&addr));
if (AHCI_ADDR_IS_PORT(&addr)) {
cmn_err(CE_NOTE, "!ahci%d: ahci port %d is deactivated",
instance, port);
ahci_disable_port_intrs(ahci_ctlp, port);
if (ahci_portp->ahciport_device_type != SATA_DTYPE_NONE) {
ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
ahci_portp, port);
}
port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_DISABLE);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle, (uint32_t *)
AHCI_PORT_PxSCTL(ahci_ctlp, port), port_scontrol);
} else if (AHCI_ADDR_IS_PMPORT(&addr)) {
cmn_err(CE_NOTE, "!ahci%d: ahci port %d:%d is deactivated",
instance, port, pmport);
ahci_disable_port_intrs(ahci_ctlp, port);
if (AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr)
!= SATA_DTYPE_NONE)
ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
ahci_enable_port_intrs(ahci_ctlp, port);
}
AHCIPORT_SET_STATE(ahci_portp, &addr, SATA_PSTATE_SHUTDOWN);
satadev->satadev_state = SATA_PSTATE_SHUTDOWN;
ahci_update_sata_registers(ahci_ctlp, port, satadev);
mutex_exit(&ahci_portp->ahciport_mutex);
return (SATA_SUCCESS);
}
static void
ahci_reject_all_abort_pkts(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port)
{
uint32_t slot_status = 0;
uint32_t abort_tags = 0;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
"ahci_reject_all_abort_pkts at port: %d", port);
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = 0x1;
abort_tags = 0x1;
goto out;
}
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = 0x1;
abort_tags = 0x1;
goto out;
} else {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_reject_all_abort_pkts return directly "
"port %d no needs to reject any outstanding "
"commands", port);
return;
}
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
abort_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
abort_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
}
out:
if (slot_status != 0) {
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
ahci_portp->ahciport_mop_in_progress++;
ahci_mop_commands(ahci_ctlp,
ahci_portp,
slot_status,
0,
0,
abort_tags,
0);
}
}
#if defined(__lock_lint)
static int
ahci_selftest(dev_info_t *dip, sata_device_t *device)
{
return (SATA_SUCCESS);
}
#endif
static void
ahci_fm_init(ahci_ctl_t *ahci_ctlp)
{
ddi_iblock_cookie_t fm_ibc;
ahci_ctlp->ahcictl_fm_cap = ddi_getprop(DDI_DEV_T_ANY,
ahci_ctlp->ahcictl_dip,
DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "fm-capable",
DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
if (ahci_ctlp->ahcictl_fm_cap) {
accattr.devacc_attr_access = DDI_FLAGERR_ACC;
buffer_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
rcvd_fis_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
cmd_list_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
cmd_table_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
ddi_fm_init(ahci_ctlp->ahcictl_dip,
&ahci_ctlp->ahcictl_fm_cap, &fm_ibc);
if (ahci_ctlp->ahcictl_fm_cap == DDI_FM_NOT_CAPABLE) {
cmn_err(CE_WARN, "!ahci%d: fma init failed.",
ddi_get_instance(ahci_ctlp->ahcictl_dip));
return;
}
if (DDI_FM_EREPORT_CAP(ahci_ctlp->ahcictl_fm_cap) ||
DDI_FM_ERRCB_CAP(ahci_ctlp->ahcictl_fm_cap)) {
pci_ereport_setup(ahci_ctlp->ahcictl_dip);
}
if (DDI_FM_ERRCB_CAP(ahci_ctlp->ahcictl_fm_cap)) {
ddi_fm_handler_register(ahci_ctlp->ahcictl_dip,
ahci_fm_error_cb, (void *) ahci_ctlp);
}
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ahci_fm_fini: fma enabled.", NULL);
}
}
static void
ahci_fm_fini(ahci_ctl_t *ahci_ctlp)
{
if (ahci_ctlp->ahcictl_fm_cap) {
if (DDI_FM_ERRCB_CAP(ahci_ctlp->ahcictl_fm_cap)) {
ddi_fm_handler_unregister(ahci_ctlp->ahcictl_dip);
}
if (DDI_FM_EREPORT_CAP(ahci_ctlp->ahcictl_fm_cap) ||
DDI_FM_ERRCB_CAP(ahci_ctlp->ahcictl_fm_cap)) {
pci_ereport_teardown(ahci_ctlp->ahcictl_dip);
}
ddi_fm_fini(ahci_ctlp->ahcictl_dip);
accattr.devacc_attr_access = DDI_DEFAULT_ACC;
buffer_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
rcvd_fis_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
cmd_list_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
cmd_table_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ahci_fm_fini: fma disabled.", NULL);
}
}
static int
ahci_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
{
pci_ereport_post(dip, err, NULL);
return (err->fme_status);
}
int
ahci_check_acc_handle(ddi_acc_handle_t handle)
{
ddi_fm_error_t de;
ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
return (de.fme_status);
}
int
ahci_check_dma_handle(ddi_dma_handle_t handle)
{
ddi_fm_error_t de;
ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
return (de.fme_status);
}
void
ahci_fm_ereport(ahci_ctl_t *ahci_ctlp, char *detail)
{
uint64_t ena;
char buf[FM_MAX_CLASS];
(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
ena = fm_ena_generate(0, FM_ENA_FMT1);
if (DDI_FM_EREPORT_CAP(ahci_ctlp->ahcictl_fm_cap)) {
ddi_fm_ereport_post(ahci_ctlp->ahcictl_dip, buf, ena,
DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8,
FM_EREPORT_VERSION, NULL);
}
}
static int
ahci_check_all_handle(ahci_ctl_t *ahci_ctlp)
{
int port;
if (ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) {
return (DDI_FAILURE);
}
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
ahci_port_t *ahci_portp;
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port))
continue;
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
if (ahci_check_port_handle(ahci_ctlp, port) != DDI_SUCCESS) {
mutex_exit(&ahci_portp->ahciport_mutex);
return (DDI_FAILURE);
}
mutex_exit(&ahci_portp->ahciport_mutex);
}
return (DDI_SUCCESS);
}
static int
ahci_check_ctl_handle(ahci_ctl_t *ahci_ctlp)
{
if ((ahci_check_acc_handle(ahci_ctlp->
ahcictl_pci_conf_handle) != DDI_FM_OK) ||
(ahci_check_acc_handle(ahci_ctlp->
ahcictl_ahci_acc_handle) != DDI_FM_OK)) {
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
ahci_check_port_handle(ahci_ctl_t *ahci_ctlp, int port)
{
ahci_port_t *ahci_portp = ahci_ctlp->ahcictl_ports[port];
int slot;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
if ((ahci_check_dma_handle(ahci_portp->
ahciport_rcvd_fis_dma_handle) != DDI_FM_OK) ||
(ahci_check_dma_handle(ahci_portp->
ahciport_cmd_list_dma_handle) != DDI_FM_OK) ||
(ahci_check_acc_handle(ahci_portp->
ahciport_rcvd_fis_acc_handle) != DDI_FM_OK) ||
(ahci_check_acc_handle(ahci_portp->
ahciport_cmd_list_acc_handle) != DDI_FM_OK)) {
return (DDI_FAILURE);
}
for (slot = 0; slot < ahci_ctlp->ahcictl_num_cmd_slots; slot++) {
if (ahci_check_slot_handle(ahci_portp, slot)
!= DDI_SUCCESS) {
return (DDI_FAILURE);
}
}
return (DDI_SUCCESS);
}
static int
ahci_check_slot_handle(ahci_port_t *ahci_portp, int slot)
{
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
if ((ahci_check_acc_handle(ahci_portp->
ahciport_cmd_tables_acc_handle[slot]) != DDI_FM_OK) ||
(ahci_check_dma_handle(ahci_portp->
ahciport_cmd_tables_dma_handle[slot]) != DDI_FM_OK)) {
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
static int
ahci_alloc_ports_state(ahci_ctl_t *ahci_ctlp)
{
int port, cport = 0;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_alloc_ports_state enter", NULL);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"hba port %d not implemented", port);
continue;
}
ahci_ctlp->ahcictl_cport_to_port[cport] = (uint8_t)port;
ahci_ctlp->ahcictl_port_to_cport[port] =
(uint8_t)cport++;
if (ahci_alloc_port_state(ahci_ctlp, port) != AHCI_SUCCESS) {
goto err_out;
}
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (AHCI_SUCCESS);
err_out:
for (port--; port >= 0; port--) {
if (AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
ahci_dealloc_port_state(ahci_ctlp, port);
}
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (AHCI_FAILURE);
}
static void
ahci_dealloc_ports_state(ahci_ctl_t *ahci_ctlp)
{
int port;
mutex_enter(&ahci_ctlp->ahcictl_mutex);
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (AHCI_PORT_IMPLEMENTED(ahci_ctlp, port))
ahci_dealloc_port_state(ahci_ctlp, port);
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
}
static void
ahci_drain_ports_taskq(ahci_ctl_t *ahci_ctlp)
{
ahci_port_t *ahci_portp;
int port;
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
continue;
}
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
ddi_taskq_wait(ahci_portp->ahciport_event_taskq);
mutex_exit(&ahci_portp->ahciport_mutex);
}
}
static int
ahci_initialize_controller(ahci_ctl_t *ahci_ctlp)
{
ahci_port_t *ahci_portp;
ahci_addr_t addr;
int port;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_initialize_controller enter", NULL);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_disable_all_intrs(ahci_ctlp);
mutex_exit(&ahci_ctlp->ahcictl_mutex);
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
continue;
}
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
AHCI_ADDR_SET_PORT(&addr, (uint8_t)port);
if (ahci_initialize_port(ahci_ctlp, ahci_portp, &addr)
!= AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_initialize_controller: failed to "
"initialize port %d", port);
ahci_portp->ahciport_port_state = SATA_PSTATE_FAILED;
}
mutex_exit(&ahci_portp->ahciport_mutex);
}
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_enable_all_intrs(ahci_ctlp);
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (AHCI_SUCCESS);
}
static void
ahci_uninitialize_controller(ahci_ctl_t *ahci_ctlp)
{
ahci_port_t *ahci_portp;
int port;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ahci_uninitialize_controller enter", NULL);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_disable_all_intrs(ahci_ctlp);
mutex_exit(&ahci_ctlp->ahcictl_mutex);
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
continue;
}
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
ahci_disable_port_intrs(ahci_ctlp, port);
(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
ahci_portp, port);
mutex_exit(&ahci_portp->ahciport_mutex);
}
}
static void
ahci_alloc_pmult(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
{
uint32_t port_cmd_status;
uint8_t port = ahci_portp->ahciport_port_num;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
ASSERT(!(port_cmd_status & AHCI_CMD_STATUS_ST));
if (!(port_cmd_status & AHCI_CMD_STATUS_PMA)) {
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
port_cmd_status|AHCI_CMD_STATUS_PMA);
AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
"ahci_alloc_pmult: "
"PxCMD.PMA bit set at port %d.", port);
}
if (ahci_portp->ahciport_pmult_info == NULL) {
ahci_portp->ahciport_pmult_info = (ahci_pmult_info_t *)
kmem_zalloc(sizeof (ahci_pmult_info_t), KM_SLEEP);
}
ASSERT(ahci_portp->ahciport_pmult_info != NULL);
}
static void
ahci_dealloc_pmult(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
{
uint32_t port_cmd_status;
uint8_t port = ahci_portp->ahciport_port_num;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
if (port_cmd_status & AHCI_CMD_STATUS_PMA) {
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
(port_cmd_status & (~AHCI_CMD_STATUS_PMA)));
AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
"ahci_dealloc_pmult: "
"PxCMD.PMA bit cleared at port %d.", port);
}
if (ahci_portp->ahciport_pmult_info != NULL) {
kmem_free(ahci_portp->ahciport_pmult_info,
sizeof (ahci_pmult_info_t));
ahci_portp->ahciport_pmult_info = NULL;
}
}
static void
ahci_staggered_spin_up(ahci_ctl_t *ahci_ctlp, uint8_t port)
{
uint32_t cap_status;
uint32_t port_cmd_status;
ASSERT(MUTEX_HELD(&ahci_ctlp->ahcictl_ports[port]->ahciport_mutex));
cap_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_CAP(ahci_ctlp));
if (!(cap_status & AHCI_HBA_CAP_SSS))
return;
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
if (port_cmd_status & AHCI_CMD_STATUS_SUD)
return;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "Spin-up at port %d", port);
port_cmd_status |= AHCI_CMD_STATUS_SUD;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
port_cmd_status);
}
static int
ahci_initialize_port(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp)
{
uint32_t port_sstatus, port_task_file, port_cmd_status;
uint8_t port = addrp->aa_port;
boolean_t resuming = B_TRUE;
int ret;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
ASSERT(AHCI_ADDR_IS_PORT(addrp));
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_HOTPLUG) {
resuming = B_FALSE;
} else {
mutex_exit(&ahci_portp->ahciport_mutex);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
if (ahci_ctlp->ahcictl_flags & AHCI_ATTACH)
resuming = B_FALSE;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
mutex_enter(&ahci_portp->ahciport_mutex);
}
if (resuming) {
AHCIDBG(AHCIDBG_PM, ahci_ctlp,
"ahci_initialize_port: port %d "
"set PxCLB, PxCLBU, PxFB and PxFBU "
"during resume", port);
if (ahci_setup_port_base_addresses(ahci_ctlp, ahci_portp) !=
AHCI_SUCCESS)
return (AHCI_FAILURE);
}
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_initialize_port: port %d ", port);
if (port_cmd_status &
(AHCI_CMD_STATUS_ST |
AHCI_CMD_STATUS_CR |
AHCI_CMD_STATUS_FRE |
AHCI_CMD_STATUS_FR)) {
(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
ahci_portp, port);
}
ahci_staggered_spin_up(ahci_ctlp, port);
ahci_disable_port_intrs(ahci_ctlp, port);
AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_UNKNOWN);
ahci_disable_interface_pm(ahci_ctlp, port);
port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
if (SSTATUS_GET_IPM(port_sstatus) == SSTATUS_IPM_NODEV_NOPHYCOM ||
SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_NOPHYCOM ||
port_task_file & AHCI_TFD_STS_BSY ||
port_task_file & AHCI_TFD_STS_DRQ ||
ahci_ctlp->ahcictl_cap & AHCI_CAP_INIT_PORT_RESET ||
resuming != B_FALSE) {
ret = ahci_port_reset(ahci_ctlp, ahci_portp, addrp);
if (ret != AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
"ahci_initialize_port:"
"port reset failed at port %d", port);
return (AHCI_FAILURE);
}
if (AHCIPORT_GET_STATE(ahci_portp, addrp) &
SATA_PSTATE_FAILED) {
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
"ahci_initialize_port: port %d state 0x%x",
port, ahci_portp->ahciport_port_state);
return (AHCI_FAILURE);
}
}
AHCIPORT_SET_STATE(ahci_portp, addrp, SATA_STATE_READY);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "port %d is ready now.", port);
if (!resuming && AHCIPORT_DEV_TYPE(ahci_portp, addrp) !=
SATA_DTYPE_NONE)
ahci_find_dev_signature(ahci_ctlp, ahci_portp, addrp);
if (AHCIPORT_DEV_TYPE(ahci_portp, addrp) == SATA_DTYPE_NONE) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"No device connected to port %d", port);
goto out;
}
if (AHCIPORT_DEV_TYPE(ahci_portp, addrp) == SATA_DTYPE_PMULT) {
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"Port multiplier found at port %d", port);
ahci_alloc_pmult(ahci_ctlp, ahci_portp);
}
if (ahci_start_port(ahci_ctlp, ahci_portp, port)
!= AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"failed to start port %d", port);
return (AHCI_FAILURE);
}
out:
ahci_enable_port_intrs(ahci_ctlp, port);
return (AHCI_SUCCESS);
}
static int
ahci_config_space_init(ahci_ctl_t *ahci_ctlp)
{
ushort_t caps_ptr, cap_count, cap;
#if AHCI_DEBUG
ushort_t pmcap, pmcsr;
ushort_t msimc;
#endif
uint8_t revision;
ahci_ctlp->ahcictl_venid =
pci_config_get16(ahci_ctlp->ahcictl_pci_conf_handle,
PCI_CONF_VENID);
ahci_ctlp->ahcictl_devid =
pci_config_get16(ahci_ctlp->ahcictl_pci_conf_handle,
PCI_CONF_DEVID);
if (ahci_ctlp->ahcictl_venid == VIA_VENID) {
revision = pci_config_get8(ahci_ctlp->ahcictl_pci_conf_handle,
PCI_CONF_REVID);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"revision id = 0x%x", revision);
if (revision == 0x00) {
ahci_ctlp->ahcictl_buffer_dma_attr.dma_attr_align = 0x4;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"change ddi_attr_align to 0x4", NULL);
}
ahci_ctlp->ahcictl_cap |= AHCI_CAP_NO_MCMDLIST_NONQUEUE;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"VT8251 cannot use multiple command lists for "
"non-queued commands", NULL);
ahci_ctlp->ahcictl_cap |= AHCI_CAP_SRST_NO_HOSTPORT;
}
if (ahci_ctlp->ahcictl_venid == 0x1002 &&
ahci_ctlp->ahcictl_devid == 0x4380) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_BUF_32BIT_DMA;
ahci_ctlp->ahcictl_cap |= AHCI_CAP_COMMU_32BIT_DMA;
ahci_ctlp->ahcictl_cap |= AHCI_CAP_INIT_PORT_RESET;
ahci_ctlp->ahcictl_cap |= AHCI_CAP_SRST_NO_HOSTPORT;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ATI SB600 cannot do 64-bit DMA for both data buffer and "
"communication memory descriptors though CAP indicates "
"support, so force it to use 32-bit DMA", NULL);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ATI SB600 need to do a port reset during initialization",
NULL);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ATI SB600 will get software reset failure if pmport "
"is set 0xf and no port multiplier is attached", NULL);
}
if (ahci_ctlp->ahcictl_venid == 0x1002 &&
ahci_ctlp->ahcictl_devid == 0x4391) {
ahci_ctlp->ahcictl_cap |= AHCI_CAP_COMMU_32BIT_DMA;
ahci_ctlp->ahcictl_cap |= AHCI_CAP_INIT_PORT_RESET;
ahci_ctlp->ahcictl_cap |= AHCI_CAP_SRST_NO_HOSTPORT;
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ATI SB700/750 cannot do 64-bit DMA for communication "
"memory descriptors though CAP indicates support, "
"so force it to use 32-bit DMA", NULL);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ATI SB710 need to do a port reset during initialization",
NULL);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ATI SB700 will get software reset failure if pmport "
"is set 0xf and no port multiplier is attached", NULL);
}
if (pci_config_get16(ahci_ctlp->ahcictl_pci_conf_handle,
PCI_CONF_STAT) & PCI_STAT_CAP) {
caps_ptr = P2ALIGN(pci_config_get8(
ahci_ctlp->ahcictl_pci_conf_handle,
PCI_CONF_CAP_PTR), 4);
} else {
caps_ptr = PCI_CAP_NEXT_PTR_NULL;
}
for (cap_count = 0; caps_ptr != PCI_CAP_NEXT_PTR_NULL; ) {
if (++cap_count > PCI_CAP_MAX_PTR) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"too many device capabilities", NULL);
return (AHCI_FAILURE);
}
if (caps_ptr < PCI_CAP_PTR_OFF) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"capabilities pointer 0x%x out of range",
caps_ptr);
return (AHCI_FAILURE);
}
cap = pci_config_get8(ahci_ctlp->ahcictl_pci_conf_handle,
caps_ptr);
switch (cap) {
case PCI_CAP_ID_PM:
ahci_ctlp->ahcictl_cap |= AHCI_CAP_PM;
ahci_ctlp->ahcictl_pmcsr_offset = caps_ptr + PCI_PMCSR;
#if AHCI_DEBUG
pmcap = pci_config_get16(
ahci_ctlp->ahcictl_pci_conf_handle,
caps_ptr + PCI_PMCAP);
pmcsr = pci_config_get16(
ahci_ctlp->ahcictl_pci_conf_handle,
ahci_ctlp->ahcictl_pmcsr_offset);
AHCIDBG(AHCIDBG_PM, ahci_ctlp,
"Power Management capability found PCI_PMCAP "
"= 0x%x PCI_PMCSR = 0x%x", pmcap, pmcsr);
if ((pmcap & 0x3) == 0x3)
AHCIDBG(AHCIDBG_PM, ahci_ctlp,
"PCI Power Management Interface "
"spec 1.2 compliant", NULL);
#endif
break;
case PCI_CAP_ID_MSI:
#if AHCI_DEBUG
msimc = pci_config_get16(
ahci_ctlp->ahcictl_pci_conf_handle,
caps_ptr + PCI_MSI_CTRL);
AHCIDBG(AHCIDBG_MSI, ahci_ctlp,
"Message Signaled Interrupt capability found "
"MSICAP_MC.MMC = 0x%x", (msimc & 0xe) >> 1);
#endif
AHCIDBG(AHCIDBG_MSI, ahci_ctlp,
"MSI capability found", NULL);
break;
case PCI_CAP_ID_PCIX:
AHCIDBG(AHCIDBG_PM, ahci_ctlp,
"PCI-X capability found", NULL);
break;
case PCI_CAP_ID_PCI_E:
AHCIDBG(AHCIDBG_PM, ahci_ctlp,
"PCI Express capability found", NULL);
break;
case PCI_CAP_ID_MSI_X:
AHCIDBG(AHCIDBG_PM, ahci_ctlp,
"MSI-X capability found", NULL);
break;
case PCI_CAP_ID_SATA:
AHCIDBG(AHCIDBG_PM, ahci_ctlp,
"SATA capability found", NULL);
break;
case PCI_CAP_ID_VS:
AHCIDBG(AHCIDBG_PM, ahci_ctlp,
"Vendor Specific capability found", NULL);
break;
default:
AHCIDBG(AHCIDBG_PM, ahci_ctlp,
"unrecognized capability 0x%x", cap);
break;
}
caps_ptr = P2ALIGN(pci_config_get8(
ahci_ctlp->ahcictl_pci_conf_handle,
(caps_ptr + PCI_CAP_NEXT_PTR)), 4);
}
return (AHCI_SUCCESS);
}
static int
ahci_rdwr_pmult(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
uint8_t regn, uint32_t *pregv, uint8_t type)
{
ahci_port_t *ahci_portp;
ahci_addr_t pmult_addr;
sata_pkt_t *spkt;
sata_cmd_t *scmd;
sata_device_t sata_device;
uint8_t port = addrp->aa_port;
uint8_t pmport = addrp->aa_pmport;
uint8_t cport;
uint32_t intr_mask;
int rval;
char portstr[10];
SET_PORTSTR(portstr, addrp);
cport = ahci_ctlp->ahcictl_port_to_cport[port];
ahci_portp = ahci_ctlp->ahcictl_ports[port];
ASSERT(AHCI_ADDR_IS_PMPORT(addrp) || AHCI_ADDR_IS_PMULT(addrp));
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
if (ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT)
return (AHCI_FAILURE);
bzero(&sata_device, sizeof (sata_device_t));
sata_device.satadev_addr.cport = cport;
sata_device.satadev_addr.pmport = pmport;
sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
sata_device.satadev_rev = SATA_DEVICE_REV;
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
"R/W PMULT failed: R/W PMULT in progress at port %d.",
port, ahci_portp->ahciport_flags);
return (AHCI_FAILURE);
}
if (!(ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) && (
ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) ||
NCQ_CMD_IN_PROGRESS(ahci_portp) ||
NON_NCQ_CMD_IN_PROGRESS(ahci_portp))) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
"R/W PMULT failed: port %d is occupied (flags 0x%x).",
port, ahci_portp->ahciport_flags);
return (AHCI_FAILURE);
}
if (!(ahci_portp->ahciport_port_state & SATA_STATE_READY)) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
"READ/WRITE PMULT failed: "
"port-mult is removed from port %d", port);
return (AHCI_FAILURE);
}
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_RDWR_PMULT;
spkt = sata_get_rdwr_pmult_pkt(ahci_ctlp->ahcictl_dip,
&sata_device, regn, *pregv, type);
AHCI_ADDR_SET_PMULT(&pmult_addr, addrp->aa_port);
ahci_portp->ahciport_rdwr_pmult_pkt = spkt;
intr_mask = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port));
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port), 0);
rval = ahci_do_sync_start(ahci_ctlp, ahci_portp, &pmult_addr, spkt);
if (rval == AHCI_SUCCESS &&
spkt->satapkt_reason == SATA_PKT_COMPLETED) {
if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
scmd = &spkt->satapkt_cmd;
*pregv = scmd->satacmd_lba_high_lsb << 24 |
scmd->satacmd_lba_mid_lsb << 16 |
scmd->satacmd_lba_low_lsb << 8 |
scmd->satacmd_sec_count_lsb;
}
} else {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
"ahci_rdwr_pmult: cannot [%s] %s[%d] at port %s",
type == SATA_RDWR_PMULT_PKT_TYPE_READ?"Read":"Write",
AHCI_ADDR_IS_PMULT(addrp)?"gscr":"pscr", regn, portstr);
rval = AHCI_FAILURE;
}
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port), intr_mask);
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_RDWR_PMULT;
ahci_portp->ahciport_rdwr_pmult_pkt = NULL;
sata_free_rdwr_pmult_pkt(spkt);
return (rval);
}
static int
ahci_read_pmult(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
uint8_t regn, uint32_t *pregv)
{
return ahci_rdwr_pmult(ahci_ctlp, addrp, regn, pregv,
SATA_RDWR_PMULT_PKT_TYPE_READ);
}
static int
ahci_write_pmult(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
uint8_t regn, uint32_t regv)
{
return ahci_rdwr_pmult(ahci_ctlp, addrp, regn, ®v,
SATA_RDWR_PMULT_PKT_TYPE_WRITE);
}
#define READ_PMULT(addrp, r, pv, out) \
if (ahci_read_pmult(ahci_ctlp, addrp, r, pv) != AHCI_SUCCESS) \
goto out;
#define WRITE_PMULT(addrp, r, v, out) \
if (ahci_write_pmult(ahci_ctlp, addrp, r, v) != AHCI_SUCCESS) \
goto out;
static int
ahci_update_pmult_gscr(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
sata_pmult_gscr_t *sg)
{
ASSERT(MUTEX_HELD(
&ahci_ctlp->ahcictl_ports[addrp->aa_port]->ahciport_mutex));
READ_PMULT(addrp, SATA_PMULT_GSCR0, &sg->gscr0, err);
READ_PMULT(addrp, SATA_PMULT_GSCR1, &sg->gscr1, err);
READ_PMULT(addrp, SATA_PMULT_GSCR2, &sg->gscr2, err);
READ_PMULT(addrp, SATA_PMULT_GSCR64, &sg->gscr64, err);
return (AHCI_SUCCESS);
err:
return (AHCI_FAILURE);
}
static int
ahci_update_pmult_pscr(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
sata_device_t *sd)
{
ASSERT(AHCI_ADDR_IS_PMPORT(addrp));
ASSERT(MUTEX_HELD(
&ahci_ctlp->ahcictl_ports[addrp->aa_port]->ahciport_mutex));
READ_PMULT(addrp, SATA_PMULT_REG_SSTS, &sd->satadev_scr.sstatus, err);
READ_PMULT(addrp, SATA_PMULT_REG_SERR, &sd->satadev_scr.serror, err);
READ_PMULT(addrp, SATA_PMULT_REG_SCTL, &sd->satadev_scr.scontrol, err);
READ_PMULT(addrp, SATA_PMULT_REG_SACT, &sd->satadev_scr.sactive, err);
return (AHCI_SUCCESS);
err:
return (AHCI_FAILURE);
}
static int
ahci_initialize_pmult(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp, sata_device_t *sd)
{
sata_pmult_gscr_t sg;
uint32_t gscr64;
uint8_t port = addrp->aa_port;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"[Initialize] Port-multiplier at port %d.", port);
READ_PMULT(addrp, SATA_PMULT_GSCR64, &gscr64, err);
if (gscr64 & SATA_PMULT_CAP_SNOTIF) {
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"port %d: Port Multiplier supports "
"Asynchronous Notification.", port);
WRITE_PMULT(addrp, SATA_PMULT_GSCR96,
SATA_PMULT_CAP_SNOTIF, err);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSNTF(ahci_ctlp, port),
AHCI_SNOTIF_CLEAR_ALL);
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"port %d: PMult PxSNTF cleared.", port);
}
WRITE_PMULT(addrp, SATA_PMULT_GSCR33, (0x1ffff), err);
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"port %d: gscr33 mask set to %x.", port, (0x1ffff));
if (ahci_update_pmult_gscr(ahci_ctlp, addrp, &sg) != AHCI_SUCCESS)
return (AHCI_FAILURE);
mutex_exit(&ahci_portp->ahciport_mutex);
sata_register_pmult(ahci_ctlp->ahcictl_dip, sd, &sg);
mutex_enter(&ahci_portp->ahciport_mutex);
ahci_portp->ahciport_pmult_info->ahcipmi_num_dev_ports =
sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"port %d: pmult sub-port number updated to %x.", port,
ahci_portp->ahciport_pmult_info->ahcipmi_num_dev_ports);
ahci_portp->ahciport_port_state |= SATA_DSTATE_PMULT_INIT;
return (AHCI_SUCCESS);
err:
return (AHCI_FAILURE);
}
static int
ahci_initialize_pmport(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp)
{
uint32_t finished_tags = 0, reset_tags = 0, slot_status = 0;
uint8_t port = addrp->aa_port;
uint8_t pmport = addrp->aa_pmport;
int ret = AHCI_FAILURE;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
ASSERT(AHCI_ADDR_IS_PMPORT(addrp));
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_initialize_pmport: port %d:%d", port, pmport);
if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED) {
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
"ahci_initialize_pmport:"
"port %d:%d Port Multiplier is failed.",
port, pmport);
return (AHCI_FAILURE);
}
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_HOTPLUG) {
return (AHCI_FAILURE);
}
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_HOTPLUG;
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
}
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
ahci_portp->ahciport_mop_in_progress++;
AHCIPORT_SET_STATE(ahci_portp, addrp, SATA_STATE_UNKNOWN);
AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_UNKNOWN);
ahci_disable_port_intrs(ahci_ctlp, port);
if (ahci_pmport_reset(ahci_ctlp, ahci_portp, addrp) != AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
"ahci_initialize_pmport:"
"port reset failed at port %d:%d",
port, pmport);
goto out;
}
if (AHCIPORT_GET_STATE(ahci_portp, addrp) &
SATA_PSTATE_FAILED) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_initialize_pmport: port %d:%d failed. "
"state = 0x%x", port, pmport,
ahci_portp->ahciport_port_state);
goto out;
}
if (AHCIPORT_GET_DEV_TYPE(ahci_portp, addrp)
== SATA_DTYPE_NONE) {
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_initialize_pmport: No device is found "
"at port %d:%d", port, pmport);
ret = AHCI_SUCCESS;
goto out;
}
AHCIPORT_SET_STATE(ahci_portp, addrp, SATA_STATE_READY);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"port %d:%d is ready now.", port, pmport);
ahci_find_dev_signature(ahci_ctlp, ahci_portp, addrp);
ret = AHCI_SUCCESS;
out:
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_tags &
~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
finished_tags = ahci_portp->ahciport_pending_ncq_tags &
~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
reset_tags &= ~finished_tags;
ahci_mop_commands(ahci_ctlp,
ahci_portp,
slot_status,
0,
0,
0,
reset_tags);
if (ahci_ctlp->ahcictl_cap & AHCI_CAP_SNTF) {
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSNTF(ahci_ctlp, port),
AHCI_SNOTIF_CLEAR_ALL);
}
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_HOTPLUG;
ahci_enable_port_intrs(ahci_ctlp, port);
return (ret);
}
static int
ahci_probe_pmult(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp)
{
sata_device_t sdevice;
ahci_addr_t pmport_addr;
uint32_t gscr32, port_hotplug_tags;
uint32_t pmport_sstatus;
int dev_exists_now = 0, dev_existed_previously = 0;
uint8_t port = addrp->aa_port;
int npmport;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
READ_PMULT(addrp, SATA_PMULT_GSCR32, &gscr32, err);
port_hotplug_tags = gscr32 & AHCI_PMPORT_MASK(ahci_portp);
do {
npmport = ddi_ffs(port_hotplug_tags) - 1;
if (npmport == -1)
return (AHCI_SUCCESS);
AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
"hot-plug event at port %d:%d", port, npmport);
AHCI_ADDR_SET_PMPORT(&pmport_addr, port, (uint8_t)npmport);
if (AHCIPORT_GET_DEV_TYPE(ahci_portp, &pmport_addr)
!= SATA_DTYPE_NONE)
dev_existed_previously = 1;
READ_PMULT(&pmport_addr, SATA_PMULT_REG_SSTS,
&pmport_sstatus, err);
if (SSTATUS_GET_DET(pmport_sstatus) ==
SSTATUS_DET_DEVPRE_PHYCOM)
dev_exists_now = 1;
WRITE_PMULT(&pmport_addr, SATA_PMULT_REG_SERR,
AHCI_SERROR_CLEAR_ALL, err);
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport = ahci_ctlp->
ahcictl_port_to_cport[port];
sdevice.satadev_addr.qual = SATA_ADDR_PMPORT;
sdevice.satadev_addr.pmport = (uint8_t)npmport;
sdevice.satadev_state = SATA_PSTATE_PWRON;
AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
"[Existence] %d -> %d", dev_existed_previously,
dev_exists_now);
if (dev_exists_now) {
if (dev_existed_previously) {
AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
"ahci_probe_pmult: port %d:%d "
"device link lost/established",
port, npmport);
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->
sata_tran_hba_dip,
&sdevice,
SATA_EVNT_LINK_LOST|
SATA_EVNT_LINK_ESTABLISHED);
mutex_enter(&ahci_portp->ahciport_mutex);
} else {
AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
"ahci_probe_pmult: port %d:%d "
"device link established", port, npmport);
AHCIPORT_SET_STATE(ahci_portp, &pmport_addr,
SATA_STATE_UNKNOWN);
AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
"ahci_probe_pmult: port %d "
"ahciport_port_state [Cleared].", port);
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->
sata_tran_hba_dip,
&sdevice,
SATA_EVNT_LINK_ESTABLISHED);
mutex_enter(&ahci_portp->ahciport_mutex);
}
} else {
if (dev_existed_previously) {
AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
"ahci_probe_pmult: port %d:%d "
"device link lost", port, npmport);
AHCIPORT_SET_STATE(ahci_portp, &pmport_addr,
SATA_STATE_UNKNOWN);
AHCIPORT_SET_DEV_TYPE(ahci_portp, &pmport_addr,
SATA_DTYPE_NONE);
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->
sata_tran_hba_dip,
&sdevice,
SATA_EVNT_LINK_LOST);
mutex_enter(&ahci_portp->ahciport_mutex);
}
}
CLEAR_BIT(port_hotplug_tags, npmport);
} while (port_hotplug_tags != 0);
return (AHCI_SUCCESS);
err:
return (AHCI_FAILURE);
}
static int
ahci_probe_pmport(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp, sata_device_t *sd)
{
uint32_t port_state;
uint8_t port = addrp->aa_port;
ahci_addr_t addr_pmult;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE ||
ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
"ahci_tran_probe_port: "
"parent device removed, ignore event.", NULL);
return (AHCI_FAILURE);
}
port_state = ahci_portp->ahciport_port_state;
if (!(port_state & SATA_STATE_READY)) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
"ahci_tran_probe_port: "
"parent port-mult is NOT ready.", NULL);
if (ahci_restart_port_wait_till_ready(ahci_ctlp,
ahci_portp, port, AHCI_PORT_RESET, NULL) !=
AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
"ahci_tran_probe_port: "
"restart port-mult failed.", NULL);
return (AHCI_FAILURE);
}
}
if (!(port_state & SATA_DSTATE_PMULT_INIT)) {
AHCI_ADDR_SET_PMULT(&addr_pmult, addrp->aa_port);
if (ahci_initialize_pmult(ahci_ctlp, ahci_portp,
&addr_pmult, sd) != AHCI_SUCCESS)
return (AHCI_FAILURE);
}
port_state = AHCIPORT_GET_STATE(ahci_portp, addrp);
if (!(port_state & SATA_STATE_READY)) {
if (ahci_initialize_pmport(ahci_ctlp,
ahci_portp, addrp) != AHCI_SUCCESS)
return (AHCI_FAILURE);
}
return (AHCI_SUCCESS);
}
static int
ahci_software_reset(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp)
{
ahci_fis_h2d_register_t *h2d_register_fisp;
ahci_cmd_table_t *cmd_table;
ahci_cmd_header_t *cmd_header;
uint32_t port_cmd_status, port_cmd_issue, port_task_file;
int slot, loop_count;
uint8_t port = addrp->aa_port;
uint8_t pmport = addrp->aa_pmport;
int rval = AHCI_FAILURE;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"port %d:%d device software resetting (FIS)", port, pmport);
if (ahci_put_port_into_notrunning_state(ahci_ctlp, ahci_portp,
port) != AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_software_reset: cannot stop HBA port %d.", port);
goto out;
}
port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
if (port_task_file & AHCI_TFD_STS_BSY ||
port_task_file & AHCI_TFD_STS_DRQ) {
if (!(ahci_ctlp->ahcictl_cap & AHCI_CAP_SCLO)) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"PxTFD.STS.BSY/DRQ is set (PxTFD=0x%x), "
"cannot issue a software reset.", port_task_file);
goto out;
}
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"PxTFD.STS.BSY/DRQ is set, try SCLO.", NULL)
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
port_cmd_status|AHCI_CMD_STATUS_CLO);
loop_count = 0;
do {
drv_usecwait(AHCI_10MS_USECS);
if (loop_count++ > 100) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"SCLO time out. port %d is busy.", port);
goto out;
}
port_cmd_status =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
} while (port_cmd_status & AHCI_CMD_STATUS_CLO);
port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
if (port_task_file & AHCI_TFD_STS_BSY ||
port_task_file & AHCI_TFD_STS_DRQ) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"SCLO cannot clear PxTFD.STS.BSY/DRQ (PxTFD=0x%x)",
port_task_file);
goto out;
}
}
if (ahci_start_port(ahci_ctlp, ahci_portp, port)
!= AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_software_reset: cannot start AHCI port %d.", port);
goto out;
}
slot = 0;
cmd_table = ahci_portp->ahciport_cmd_tables[slot];
bzero((void *)cmd_table, ahci_cmd_table_size);
h2d_register_fisp =
&(cmd_table->ahcict_command_fis.ahcifc_fis.ahcifc_h2d_register);
SET_FIS_TYPE(h2d_register_fisp, AHCI_H2D_REGISTER_FIS_TYPE);
SET_FIS_PMP(h2d_register_fisp, pmport);
SET_FIS_DEVCTL(h2d_register_fisp, SATA_DEVCTL_SRST);
cmd_header = &ahci_portp->ahciport_cmd_list[slot];
BZERO_DESCR_INFO(cmd_header);
BZERO_PRD_BYTE_COUNT(cmd_header);
SET_COMMAND_FIS_LENGTH(cmd_header, 5);
SET_PORT_MULTI_PORT(cmd_header, pmport);
SET_CLEAR_BUSY_UPON_R_OK(cmd_header, 1);
SET_RESET(cmd_header, 1);
SET_WRITE(cmd_header, 1);
(void) ddi_dma_sync(ahci_portp->ahciport_cmd_tables_dma_handle[slot],
0,
ahci_cmd_table_size,
DDI_DMA_SYNC_FORDEV);
(void) ddi_dma_sync(ahci_portp->ahciport_cmd_list_dma_handle,
slot * sizeof (ahci_cmd_header_t),
sizeof (ahci_cmd_header_t),
DDI_DMA_SYNC_FORDEV);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port),
(0x1 << slot));
loop_count = 0;
do {
port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
if (loop_count++ > AHCI_POLLRATE_PORT_SOFTRESET) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"the first SRST FIS is timed out, "
"loop_count = %d", loop_count);
goto out;
}
drv_usecwait(AHCI_10MS_USECS);
} while (port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp) & (0x1 << slot));
AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
"ahci_software_reset: 1st loop count: %d, "
"port_cmd_issue = 0x%x, slot = 0x%x",
loop_count, port_cmd_issue, slot);
drv_usecwait(AHCI_1MS_USECS);
cmd_table = ahci_portp->ahciport_cmd_tables[slot];
bzero((void *)cmd_table, ahci_cmd_table_size);
h2d_register_fisp =
&(cmd_table->ahcict_command_fis.ahcifc_fis.ahcifc_h2d_register);
SET_FIS_TYPE(h2d_register_fisp, AHCI_H2D_REGISTER_FIS_TYPE);
SET_FIS_PMP(h2d_register_fisp, pmport);
cmd_header = &ahci_portp->ahciport_cmd_list[slot];
BZERO_DESCR_INFO(cmd_header);
BZERO_PRD_BYTE_COUNT(cmd_header);
SET_COMMAND_FIS_LENGTH(cmd_header, 5);
SET_PORT_MULTI_PORT(cmd_header, pmport);
SET_WRITE(cmd_header, 1);
(void) ddi_dma_sync(ahci_portp->ahciport_cmd_tables_dma_handle[slot],
0,
ahci_cmd_table_size,
DDI_DMA_SYNC_FORDEV);
(void) ddi_dma_sync(ahci_portp->ahciport_cmd_list_dma_handle,
slot * sizeof (ahci_cmd_header_t),
sizeof (ahci_cmd_header_t),
DDI_DMA_SYNC_FORDEV);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port),
(0x1 << slot));
loop_count = 0;
do {
port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
if (loop_count++ > AHCI_POLLRATE_PORT_SOFTRESET) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"the second SRST FIS is timed out, "
"loop_count = %d", loop_count);
goto out;
}
drv_usecwait(AHCI_10MS_USECS);
} while (port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp) & (0x1 << slot));
AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
"ahci_software_reset: 2nd loop count: %d, "
"port_cmd_issue = 0x%x, slot = 0x%x",
loop_count, port_cmd_issue, slot);
if ((ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) ||
(ahci_check_port_handle(ahci_ctlp, port) != DDI_SUCCESS)) {
ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
DDI_SERVICE_UNAFFECTED);
goto out;
}
rval = AHCI_SUCCESS;
out:
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_software_reset: %s at port %d:%d",
rval == AHCI_SUCCESS ? "succeed" : "failed",
port, pmport);
return (rval);
}
static int
ahci_port_reset(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp)
{
ahci_addr_t pmult_addr;
uint32_t port_cmd_status;
uint32_t port_scontrol, port_sstatus;
uint32_t port_task_file;
uint32_t port_state;
uint8_t port = addrp->aa_port;
int loop_count;
int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
if (AHCI_ADDR_IS_PMPORT(addrp))
return (ahci_pmport_reset(ahci_ctlp, ahci_portp, addrp));
ASSERT(AHCI_ADDR_IS_PORT(addrp));
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
"Port %d port resetting...", port);
ahci_portp->ahciport_port_state = 0;
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
if (!(port_cmd_status & AHCI_CMD_STATUS_SUD))
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_port_reset: port %d SUD bit not set", port);
port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_COMRESET);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port),
port_scontrol);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
port_cmd_status|AHCI_CMD_STATUS_FRE);
drv_usecwait(AHCI_1MS_USECS * 2);
port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_NOACTION);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port),
port_scontrol);
loop_count = 0;
for (;;) {
port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
if (SSTATUS_GET_IPM(port_sstatus) != SSTATUS_IPM_ACTIVE) {
SSTATUS_SET_DET(port_sstatus, SSTATUS_DET_NODEV);
}
if (SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_PHYCOM)
break;
if (loop_count++ > AHCI_POLLRATE_PORT_SSTATUS) {
break;
}
drv_usecwait(AHCI_10MS_USECS);
}
AHCIDBG(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
"ahci_port_reset: 1st loop count: %d, "
"port_sstatus = 0x%x port %d",
loop_count, port_sstatus, port);
if (SSTATUS_GET_DET(port_sstatus) != SSTATUS_DET_DEVPRE_PHYCOM) {
AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_NONE);
return (AHCI_SUCCESS);
}
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
AHCI_SERROR_CLEAR_ALL);
loop_count = 0;
for (;;) {
port_task_file =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
if ((port_task_file & (AHCI_TFD_STS_BSY | AHCI_TFD_STS_DRQ |
AHCI_TFD_STS_ERR)) == 0)
break;
if (loop_count++ > AHCI_POLLRATE_PORT_TFD_ERROR) {
cmn_err(CE_WARN, "!ahci%d: ahci_port_reset port %d "
"the device hardware has been initialized and "
"the power-up diagnostics failed",
instance, port);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_port_reset: "
"port %d: some or all of BSY, DRQ and ERR in "
"PxTFD.STS are not clear. We need another "
"software reset.", port);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
AHCI_SERROR_CLEAR_ALL);
AHCI_ADDR_SET_PMULT(&pmult_addr, port);
if (ahci_software_reset(ahci_ctlp, ahci_portp,
&pmult_addr) != AHCI_SUCCESS) {
AHCIPORT_SET_STATE(ahci_portp, addrp,
SATA_PSTATE_FAILED);
return (AHCI_FAILURE);
}
break;
}
drv_usecwait(AHCI_10MS_USECS);
}
AHCIDBG(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
"ahci_port_reset: 2nd loop count: %d, "
"port_task_file = 0x%x port %d",
loop_count, port_task_file, port);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
AHCI_SERROR_CLEAR_ALL);
port_state = AHCIPORT_GET_STATE(ahci_portp, addrp);
AHCIPORT_SET_STATE(ahci_portp, addrp, port_state|SATA_STATE_READY);
AHCIDBG(AHCIDBG_INFO|AHCIDBG_ERRS, ahci_ctlp,
"ahci_port_reset: succeed at port %d.", port);
return (AHCI_SUCCESS);
}
static int
ahci_pmport_reset(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp)
{
uint32_t port_scontrol, port_sstatus, port_serror;
uint32_t port_cmd_status, port_intr_status;
uint32_t port_state;
uint8_t port = addrp->aa_port;
uint8_t pmport = addrp->aa_pmport;
int loop_count;
int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
"port %d:%d: pmport resetting", port, pmport);
AHCIPORT_SET_STATE(ahci_portp, addrp, 0);
READ_PMULT(addrp, SATA_PMULT_REG_SCTL, &port_scontrol, err);
SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_COMRESET);
WRITE_PMULT(addrp, SATA_PMULT_REG_SCTL, port_scontrol, err);
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
ASSERT(port_cmd_status & AHCI_CMD_STATUS_FRE);
if (!(port_cmd_status & AHCI_CMD_STATUS_FRE))
return (AHCI_FAILURE);
drv_usecwait(AHCI_1MS_USECS*2);
READ_PMULT(addrp, SATA_PMULT_REG_SCTL, &port_scontrol, err);
SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_NOACTION);
WRITE_PMULT(addrp, SATA_PMULT_REG_SCTL, port_scontrol, err);
loop_count = 0;
do {
READ_PMULT(addrp, SATA_PMULT_REG_SSTS, &port_sstatus, err);
if (SSTATUS_GET_IPM(port_sstatus) != SSTATUS_IPM_ACTIVE) {
SSTATUS_SET_DET(port_sstatus, SSTATUS_DET_NODEV);
}
if (loop_count++ > AHCI_POLLRATE_PORT_SSTATUS) {
break;
}
drv_usecwait(AHCI_10MS_USECS);
} while (SSTATUS_GET_DET(port_sstatus) != SSTATUS_DET_DEVPRE_PHYCOM);
AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
"ahci_pmport_reset: 1st loop count: %d, "
"port_sstatus = 0x%x port %d:%d",
loop_count, port_sstatus, port, pmport);
if ((SSTATUS_GET_IPM(port_sstatus) != SSTATUS_IPM_ACTIVE) ||
(SSTATUS_GET_DET(port_sstatus) != SSTATUS_DET_DEVPRE_PHYCOM)) {
AHCIDBG(AHCIDBG_INIT|AHCIDBG_INFO, ahci_ctlp,
"ahci_pmport_reset: "
"no device attached to port %d:%d",
port, pmport);
AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_NONE);
return (AHCI_SUCCESS);
}
READ_PMULT(addrp, SATA_PMULT_REG_SERR, &port_serror, err);
if (!(port_serror & (1 << 26))) {
cmn_err(CE_WARN, "!ahci%d: ahci_pmport_reset: "
"COMINIT signal from the device not received port %d:%d",
instance, port, pmport);
AHCIPORT_SET_STATE(ahci_portp, addrp, SATA_PSTATE_FAILED);
return (AHCI_FAILURE);
}
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_IGNORE_IPMS;
WRITE_PMULT(addrp, SATA_PMULT_REG_SERR, AHCI_SERROR_CLEAR_ALL, err);
loop_count = 0;
do {
port_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port));
if (loop_count++ > AHCI_POLLRATE_PORT_TFD_ERROR) {
cmn_err(CE_WARN, "ahci_port_reset: port %d:%d "
"PxIS.IPMS is not set, we need another "
"software reset.", port, pmport);
break;
}
mutex_exit(&ahci_portp->ahciport_mutex);
delay(AHCI_10MS_TICKS);
mutex_enter(&ahci_portp->ahciport_mutex);
} while (!(port_intr_status & AHCI_INTR_STATUS_IPMS));
AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
"ahci_pmport_reset: 2st loop count: %d, "
"port_sstatus = 0x%x port %d:%d",
loop_count, port_sstatus, port, pmport);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port),
AHCI_INTR_STATUS_IPMS);
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_IGNORE_IPMS;
port_state = AHCIPORT_GET_STATE(ahci_portp, addrp);
AHCIPORT_SET_STATE(ahci_portp, addrp, port_state|SATA_STATE_READY);
AHCIDBG(AHCIDBG_INFO|AHCIDBG_ERRS, ahci_ctlp,
"ahci_pmport_reset: succeed at port %d:%d", port, pmport);
return (AHCI_SUCCESS);
err:
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_IGNORE_IPMS;
AHCIDBG(AHCIDBG_INFO|AHCIDBG_ERRS, ahci_ctlp,
"ahci_pmport_reset: failed at port %d:%d", port, pmport);
return (AHCI_FAILURE);
}
static int
ahci_hba_reset(ahci_ctl_t *ahci_ctlp)
{
ahci_port_t *ahci_portp;
uint32_t ghc_control;
uint8_t port;
int loop_count;
int rval = AHCI_SUCCESS;
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp, "HBA resetting",
NULL);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
ghc_control |= AHCI_HBA_GHC_HR;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
loop_count = 0;
do {
ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
if (loop_count++ > AHCI_POLLRATE_HBA_RESET) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"ahci hba reset is timing out, "
"ghc_control = 0x%x", ghc_control);
break;
}
drv_usecwait(AHCI_10MS_USECS);
} while (ghc_control & AHCI_HBA_GHC_HR);
AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
"ahci_hba_reset: 1st loop count: %d, "
"ghc_control = 0x%x", loop_count, ghc_control);
if (ghc_control & AHCI_HBA_GHC_HR) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"hba reset failed: HBA in a hung or locked state", NULL);
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (AHCI_FAILURE);
}
ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
ghc_control |= (AHCI_HBA_GHC_AE | AHCI_HBA_GHC_IE);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
mutex_exit(&ahci_ctlp->ahcictl_mutex);
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
continue;
}
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
ahci_staggered_spin_up(ahci_ctlp, port);
if (ahci_restart_port_wait_till_ready(ahci_ctlp, ahci_portp,
port, AHCI_PORT_RESET|AHCI_RESET_NO_EVENTS_UP, NULL) !=
AHCI_SUCCESS) {
rval = AHCI_FAILURE;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_hba_reset: port %d failed", port);
ahci_portp->ahciport_port_state = SATA_PSTATE_FAILED;
}
mutex_exit(&ahci_portp->ahciport_mutex);
}
return (rval);
}
static void
ahci_find_dev_signature(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
ahci_addr_t *addrp)
{
ahci_addr_t dev_addr;
uint32_t signature;
uint8_t port = addrp->aa_port;
uint8_t pmport = addrp->aa_pmport;
int rval;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
ASSERT(AHCI_ADDR_IS_VALID(addrp));
if (AHCI_ADDR_IS_PORT(addrp) &&
(ahci_ctlp->ahcictl_cap & AHCI_CAP_PMULT_CBSS)) {
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_find_dev_signature enter: port %d", port);
AHCI_ADDR_SET_PMULT(&dev_addr, addrp->aa_port);
} else {
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_find_dev_signature enter: port %d:%d",
port, pmport);
dev_addr = *addrp;
}
AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_UNKNOWN);
rval = ahci_software_reset(ahci_ctlp, ahci_portp, &dev_addr);
if (rval != AHCI_SUCCESS) {
if ((ahci_ctlp->ahcictl_cap & AHCI_CAP_SRST_NO_HOSTPORT) &&
(dev_addr.aa_pmport == SATA_PMULT_HOSTPORT)) {
dev_addr.aa_pmport = 0;
rval = ahci_software_reset(ahci_ctlp, ahci_portp,
&dev_addr);
}
if (rval != AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_find_dev_signature: software reset failed "
"at port %d:%d, cannot get signature.",
port, pmport);
AHCIPORT_SET_STATE(ahci_portp, addrp,
SATA_PSTATE_FAILED);
return;
}
}
if (AHCI_ADDR_IS_PORT(addrp)) {
if (ahci_put_port_into_notrunning_state(ahci_ctlp,
ahci_portp, port) != AHCI_SUCCESS) {
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_find_dev_signature: cannot stop port %d.",
port);
ahci_portp->ahciport_port_state = SATA_PSTATE_FAILED;
return;
}
}
signature = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSIG(ahci_ctlp, port));
if (AHCI_ADDR_IS_PMPORT(addrp)) {
AHCIDBG(AHCIDBG_INIT|AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"ahci_find_dev_signature: signature = 0x%x at port %d:%d",
signature, port, pmport);
} else {
AHCIDBG(AHCIDBG_INIT|AHCIDBG_INFO, ahci_ctlp,
"ahci_find_dev_signature: signature = 0x%x at port %d",
signature, port);
}
if (signature == AHCI_SIGNATURE_ATAPI && !AHCI_ADDR_IS_PORT(addrp))
signature = SATA_DTYPE_UNKNOWN;
switch (signature) {
case AHCI_SIGNATURE_DISK:
AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_ATADISK);
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"Disk is found at port: %d", port);
break;
case AHCI_SIGNATURE_ATAPI:
AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_ATAPI);
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ATAPI device is found at port: %d", port);
break;
case AHCI_SIGNATURE_PORT_MULTIPLIER:
ASSERT(AHCI_ADDR_IS_PORT(addrp));
AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_PMULT);
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"Port Multiplier is found at port: %d", port);
break;
default:
AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_UNKNOWN);
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"Unknown device is found at port: %d", port);
}
}
static void
ahci_disable_interface_pm(ahci_ctl_t *ahci_ctlp, uint8_t port)
{
uint32_t port_scontrol, port_cmd_status;
uint32_t ipm;
port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
if (ahci_ctlp->ahcictl_cap & AHCI_CAP_SDS) {
ipm = SCONTROL_IPM_DISABLE_ALL;
} else {
ipm = SCONTROL_IPM_DISABLE_BOTH;
}
SCONTROL_SET_IPM(port_scontrol, ipm);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port), port_scontrol);
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
port_cmd_status &= ~AHCI_CMD_STATUS_ALPE;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port), port_cmd_status);
}
static int
ahci_start_port(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp, uint8_t port)
{
uint32_t port_cmd_status;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_start_port: %d enter", port);
if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_start_port failed "
"the state for port %d is 0x%x",
port, ahci_portp->ahciport_port_state);
return (AHCI_FAILURE);
}
if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_start_port failed "
"no device is attached at port %d", port);
return (AHCI_FAILURE);
}
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
if (!(port_cmd_status & AHCI_CMD_STATUS_FRE)) {
port_cmd_status |= AHCI_CMD_STATUS_FRE;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
port_cmd_status);
}
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
port_cmd_status |= AHCI_CMD_STATUS_ST;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
port_cmd_status);
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_STARTED;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_start_port: "
"PxCMD.ST set to '1' at port %d", port);
return (AHCI_SUCCESS);
}
static int
ahci_setup_port_base_addresses(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
{
uint8_t port = ahci_portp->ahciport_port_num;
uint32_t port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
if (port_cmd_status & (AHCI_CMD_STATUS_ST | AHCI_CMD_STATUS_CR)) {
if (ahci_put_port_into_notrunning_state(ahci_ctlp, ahci_portp,
port) != AHCI_SUCCESS)
return (AHCI_FAILURE);
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
}
if (port_cmd_status & (AHCI_CMD_STATUS_FRE | AHCI_CMD_STATUS_FR)) {
int loop_count = 0;
port_cmd_status &= ~AHCI_CMD_STATUS_FRE;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
port_cmd_status);
for (;;) {
port_cmd_status =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
if (!(port_cmd_status & AHCI_CMD_STATUS_FR))
break;
if (loop_count++ >= AHCI_POLLRATE_PORT_IDLE_FR) {
AHCIDBG(AHCIDBG_INIT | AHCIDBG_ERRS, ahci_ctlp,
"ahci_setup_port_base_addresses: cannot "
"clear PxCMD.FR for port %d.", port);
return (AHCI_FAILURE);
}
drv_usecwait(AHCI_1MS_USECS);
}
}
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCLB(ahci_ctlp, port),
ahci_portp->ahciport_cmd_list_dma_cookie.dmac_address);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCLBU(ahci_ctlp, port),
ahci_portp->ahciport_cmd_list_dma_cookie.dmac_notused);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxFB(ahci_ctlp, port),
ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_address);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxFBU(ahci_ctlp, port),
ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_notused);
return (AHCI_SUCCESS);
}
static int
ahci_alloc_port_state(ahci_ctl_t *ahci_ctlp, uint8_t port)
{
dev_info_t *dip = ahci_ctlp->ahcictl_dip;
ahci_port_t *ahci_portp;
char taskq_name[64] = "event_handle_taskq";
ASSERT(MUTEX_HELD(&ahci_ctlp->ahcictl_mutex));
ahci_portp =
(ahci_port_t *)kmem_zalloc(sizeof (ahci_port_t), KM_SLEEP);
ahci_ctlp->ahcictl_ports[port] = ahci_portp;
ahci_portp->ahciport_port_num = port;
cv_init(&ahci_portp->ahciport_cv, NULL, CV_DRIVER, NULL);
mutex_init(&ahci_portp->ahciport_mutex, NULL, MUTEX_DRIVER,
(void *)(uintptr_t)ahci_ctlp->ahcictl_intr_pri);
mutex_enter(&ahci_portp->ahciport_mutex);
if (ahci_alloc_rcvd_fis(ahci_ctlp, ahci_portp) != AHCI_SUCCESS) {
goto err_case1;
}
if (ahci_alloc_cmd_list(ahci_ctlp, ahci_portp) != AHCI_SUCCESS) {
goto err_case2;
}
if (ahci_setup_port_base_addresses(ahci_ctlp, ahci_portp) !=
AHCI_SUCCESS) {
goto err_case3;
}
(void) snprintf(taskq_name + strlen(taskq_name),
sizeof (taskq_name) - strlen(taskq_name),
"_port%d", port);
if ((ahci_portp->ahciport_event_taskq = ddi_taskq_create(dip,
taskq_name, 2, TASKQ_DEFAULTPRI, 0)) == NULL) {
cmn_err(CE_WARN, "!ahci%d: ddi_taskq_create failed for event "
"handle", ddi_get_instance(ahci_ctlp->ahcictl_dip));
goto err_case3;
}
ahci_portp->ahciport_event_args =
kmem_zalloc(sizeof (ahci_event_arg_t), KM_SLEEP);
ahci_portp->ahciport_event_args->ahciea_addrp =
kmem_zalloc(sizeof (ahci_addr_t), KM_SLEEP);
ahci_portp->ahciport_doneq = NULL;
ahci_portp->ahciport_doneqtail = &ahci_portp->ahciport_doneq;
ahci_portp->ahciport_doneq_len = 0;
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
err_case3:
ahci_dealloc_cmd_list(ahci_ctlp, ahci_portp);
err_case2:
ahci_dealloc_rcvd_fis(ahci_portp);
err_case1:
mutex_exit(&ahci_portp->ahciport_mutex);
mutex_destroy(&ahci_portp->ahciport_mutex);
cv_destroy(&ahci_portp->ahciport_cv);
kmem_free(ahci_portp, sizeof (ahci_port_t));
return (AHCI_FAILURE);
}
static void
ahci_dealloc_port_state(ahci_ctl_t *ahci_ctlp, uint8_t port)
{
ahci_port_t *ahci_portp = ahci_ctlp->ahcictl_ports[port];
ASSERT(MUTEX_HELD(&ahci_ctlp->ahcictl_mutex));
ASSERT(ahci_portp != NULL);
mutex_enter(&ahci_portp->ahciport_mutex);
kmem_free(ahci_portp->ahciport_event_args->ahciea_addrp,
sizeof (ahci_addr_t));
ahci_portp->ahciport_event_args->ahciea_addrp = NULL;
kmem_free(ahci_portp->ahciport_event_args, sizeof (ahci_event_arg_t));
ahci_portp->ahciport_event_args = NULL;
ddi_taskq_destroy(ahci_portp->ahciport_event_taskq);
ahci_dealloc_cmd_list(ahci_ctlp, ahci_portp);
ahci_dealloc_rcvd_fis(ahci_portp);
ahci_dealloc_pmult(ahci_ctlp, ahci_portp);
mutex_exit(&ahci_portp->ahciport_mutex);
mutex_destroy(&ahci_portp->ahciport_mutex);
cv_destroy(&ahci_portp->ahciport_cv);
kmem_free(ahci_portp, sizeof (ahci_port_t));
ahci_ctlp->ahcictl_ports[port] = NULL;
}
static int
ahci_alloc_rcvd_fis(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
{
size_t rcvd_fis_size;
size_t ret_len;
uint_t cookie_count;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
rcvd_fis_size = sizeof (ahci_rcvd_fis_t);
if (ddi_dma_alloc_handle(ahci_ctlp->ahcictl_dip,
&ahci_ctlp->ahcictl_rcvd_fis_dma_attr,
DDI_DMA_SLEEP,
NULL,
&ahci_portp->ahciport_rcvd_fis_dma_handle) !=
DDI_SUCCESS) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"rcvd FIS dma handle alloc failed", NULL);
return (AHCI_FAILURE);
}
if (ddi_dma_mem_alloc(ahci_portp->ahciport_rcvd_fis_dma_handle,
rcvd_fis_size,
&accattr,
DDI_DMA_CONSISTENT,
DDI_DMA_SLEEP,
NULL,
(caddr_t *)&ahci_portp->ahciport_rcvd_fis,
&ret_len,
&ahci_portp->ahciport_rcvd_fis_acc_handle) != 0) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"rcvd FIS dma mem alloc fail", NULL);
ddi_dma_free_handle(&ahci_portp->ahciport_rcvd_fis_dma_handle);
return (AHCI_FAILURE);
}
if (ddi_dma_addr_bind_handle(ahci_portp->ahciport_rcvd_fis_dma_handle,
NULL,
(caddr_t)ahci_portp->ahciport_rcvd_fis,
rcvd_fis_size,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
DDI_DMA_SLEEP,
NULL,
&ahci_portp->ahciport_rcvd_fis_dma_cookie,
&cookie_count) != DDI_DMA_MAPPED) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"rcvd FIS dma handle bind fail", NULL);
ddi_dma_mem_free(&ahci_portp->ahciport_rcvd_fis_acc_handle);
ddi_dma_free_handle(&ahci_portp->ahciport_rcvd_fis_dma_handle);
return (AHCI_FAILURE);
}
bzero((void *)ahci_portp->ahciport_rcvd_fis, rcvd_fis_size);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "64-bit, dma address: 0x%llx",
ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_laddress);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "32-bit, dma address: 0x%x",
ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_address);
return (AHCI_SUCCESS);
}
static void
ahci_dealloc_rcvd_fis(ahci_port_t *ahci_portp)
{
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
(void) ddi_dma_unbind_handle(ahci_portp->ahciport_rcvd_fis_dma_handle);
ddi_dma_mem_free(&ahci_portp->ahciport_rcvd_fis_acc_handle);
ddi_dma_free_handle(&ahci_portp->ahciport_rcvd_fis_dma_handle);
}
static int
ahci_alloc_cmd_list(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
{
size_t cmd_list_size;
size_t ret_len;
uint_t cookie_count;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
cmd_list_size =
ahci_ctlp->ahcictl_num_cmd_slots * sizeof (ahci_cmd_header_t);
if (ddi_dma_alloc_handle(ahci_ctlp->ahcictl_dip,
&ahci_ctlp->ahcictl_cmd_list_dma_attr,
DDI_DMA_SLEEP,
NULL,
&ahci_portp->ahciport_cmd_list_dma_handle) != DDI_SUCCESS) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"cmd list dma handle alloc failed", NULL);
return (AHCI_FAILURE);
}
if (ddi_dma_mem_alloc(ahci_portp->ahciport_cmd_list_dma_handle,
cmd_list_size,
&accattr,
DDI_DMA_CONSISTENT,
DDI_DMA_SLEEP,
NULL,
(caddr_t *)&ahci_portp->ahciport_cmd_list,
&ret_len,
&ahci_portp->ahciport_cmd_list_acc_handle) != 0) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"cmd list dma mem alloc fail", NULL);
ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
return (AHCI_FAILURE);
}
if (ddi_dma_addr_bind_handle(ahci_portp->ahciport_cmd_list_dma_handle,
NULL,
(caddr_t)ahci_portp->ahciport_cmd_list,
cmd_list_size,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
DDI_DMA_SLEEP,
NULL,
&ahci_portp->ahciport_cmd_list_dma_cookie,
&cookie_count) != DDI_DMA_MAPPED) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"cmd list dma handle bind fail", NULL);
ddi_dma_mem_free(&ahci_portp->ahciport_cmd_list_acc_handle);
ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
return (AHCI_FAILURE);
}
bzero((void *)ahci_portp->ahciport_cmd_list, cmd_list_size);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "64-bit, dma address: 0x%llx",
ahci_portp->ahciport_cmd_list_dma_cookie.dmac_laddress);
AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "32-bit, dma address: 0x%x",
ahci_portp->ahciport_cmd_list_dma_cookie.dmac_address);
if (ahci_alloc_cmd_tables(ahci_ctlp, ahci_portp) != AHCI_SUCCESS) {
goto err_out;
}
return (AHCI_SUCCESS);
err_out:
(void) ddi_dma_unbind_handle(ahci_portp->ahciport_cmd_list_dma_handle);
ddi_dma_mem_free(&ahci_portp->ahciport_cmd_list_acc_handle);
ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
return (AHCI_FAILURE);
}
static void
ahci_dealloc_cmd_list(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
{
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
ahci_dealloc_cmd_tables(ahci_ctlp, ahci_portp);
(void) ddi_dma_unbind_handle(ahci_portp->ahciport_cmd_list_dma_handle);
ddi_dma_mem_free(&ahci_portp->ahciport_cmd_list_acc_handle);
ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
}
static int
ahci_alloc_cmd_tables(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
{
size_t ret_len;
ddi_dma_cookie_t cmd_table_dma_cookie;
uint_t cookie_count;
int slot;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_alloc_cmd_tables: port %d enter",
ahci_portp->ahciport_port_num);
for (slot = 0; slot < ahci_ctlp->ahcictl_num_cmd_slots; slot++) {
if (ddi_dma_alloc_handle(ahci_ctlp->ahcictl_dip,
&ahci_ctlp->ahcictl_cmd_table_dma_attr,
DDI_DMA_SLEEP,
NULL,
&ahci_portp->ahciport_cmd_tables_dma_handle[slot]) !=
DDI_SUCCESS) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"cmd table dma handle alloc failed", NULL);
goto err_out;
}
if (ddi_dma_mem_alloc(
ahci_portp->ahciport_cmd_tables_dma_handle[slot],
ahci_cmd_table_size,
&accattr,
DDI_DMA_CONSISTENT,
DDI_DMA_SLEEP,
NULL,
(caddr_t *)&ahci_portp->ahciport_cmd_tables[slot],
&ret_len,
&ahci_portp->ahciport_cmd_tables_acc_handle[slot]) != 0) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"cmd table dma mem alloc fail", NULL);
ddi_dma_free_handle(
&ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
goto err_out;
}
if (ddi_dma_addr_bind_handle(
ahci_portp->ahciport_cmd_tables_dma_handle[slot],
NULL,
(caddr_t)ahci_portp->ahciport_cmd_tables[slot],
ahci_cmd_table_size,
DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
DDI_DMA_SLEEP,
NULL,
&cmd_table_dma_cookie,
&cookie_count) != DDI_DMA_MAPPED) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"cmd table dma handle bind fail", NULL);
ddi_dma_mem_free(
&ahci_portp->ahciport_cmd_tables_acc_handle[slot]);
ddi_dma_free_handle(
&ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
goto err_out;
}
bzero((void *)ahci_portp->ahciport_cmd_tables[slot],
ahci_cmd_table_size);
SET_COMMAND_TABLE_BASE_ADDR(
(&ahci_portp->ahciport_cmd_list[slot]),
cmd_table_dma_cookie.dmac_laddress & 0xffffffffull);
#ifndef __lock_lint
SET_COMMAND_TABLE_BASE_ADDR_UPPER(
(&ahci_portp->ahciport_cmd_list[slot]),
cmd_table_dma_cookie.dmac_laddress >> 32);
#endif
}
return (AHCI_SUCCESS);
err_out:
for (slot--; slot >= 0; slot--) {
(void) ddi_dma_unbind_handle(
ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
ddi_dma_mem_free(
&ahci_portp->ahciport_cmd_tables_acc_handle[slot]);
ddi_dma_free_handle(
&ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
}
return (AHCI_FAILURE);
}
static void
ahci_dealloc_cmd_tables(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
{
int slot;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_dealloc_cmd_tables: %d enter",
ahci_portp->ahciport_port_num);
for (slot = 0; slot < ahci_ctlp->ahcictl_num_cmd_slots; slot++) {
(void) ddi_dma_unbind_handle(
ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
ddi_dma_mem_free(
&ahci_portp->ahciport_cmd_tables_acc_handle[slot]);
ddi_dma_free_handle(
&ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
}
}
static void
ahci_update_sata_registers(ahci_ctl_t *ahci_ctlp, uint8_t port,
sata_device_t *sd)
{
ASSERT(MUTEX_HELD(&ahci_ctlp->ahcictl_ports[port]->ahciport_mutex));
sd->satadev_scr.sstatus =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)(AHCI_PORT_PxSSTS(ahci_ctlp, port)));
sd->satadev_scr.serror =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)(AHCI_PORT_PxSERR(ahci_ctlp, port)));
sd->satadev_scr.scontrol =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)(AHCI_PORT_PxSCTL(ahci_ctlp, port)));
sd->satadev_scr.sactive =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)(AHCI_PORT_PxSACT(ahci_ctlp, port)));
}
static void
ahci_port_intr(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp, uint8_t port)
{
uint32_t port_intr_status;
uint32_t port_intr_enable;
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_port_intr enter: port %d", port);
mutex_enter(&ahci_portp->ahciport_mutex);
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_POLLING) {
port_intr_enable =
(AHCI_INTR_STATUS_DHRS |
AHCI_INTR_STATUS_PSS |
AHCI_INTR_STATUS_SDBS |
AHCI_INTR_STATUS_UFS |
AHCI_INTR_STATUS_PCS |
AHCI_INTR_STATUS_PRCS |
AHCI_INTR_STATUS_OFS |
AHCI_INTR_STATUS_INFS |
AHCI_INTR_STATUS_IFS |
AHCI_INTR_STATUS_HBDS |
AHCI_INTR_STATUS_HBFS |
AHCI_INTR_STATUS_TFES);
} else {
port_intr_enable = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port));
}
if (!(ahci_portp->ahciport_flags & AHCI_PORT_FLAG_IGNORE_IPMS))
port_intr_enable |= AHCI_INTR_STATUS_IPMS;
mutex_exit(&ahci_portp->ahciport_mutex);
port_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port));
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"ahci_port_intr: port %d, port_intr_status = 0x%x, "
"port_intr_enable = 0x%x",
port, port_intr_status, port_intr_enable);
port_intr_status &= port_intr_enable;
if (ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) {
ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
DDI_SERVICE_UNAFFECTED);
ddi_fm_acc_err_clear(ahci_ctlp->ahcictl_ahci_acc_handle,
DDI_FME_VERSION);
return;
}
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port),
port_intr_status);
if (port_intr_status & (AHCI_INTR_STATUS_DHRS |
AHCI_INTR_STATUS_PSS)) {
(void) ahci_intr_cmd_cmplt(ahci_ctlp,
ahci_portp, port);
}
if (port_intr_status & AHCI_INTR_STATUS_SDBS) {
(void) ahci_intr_set_device_bits(ahci_ctlp,
ahci_portp, port);
}
if (port_intr_status & AHCI_INTR_STATUS_PCS) {
(void) ahci_intr_port_connect_change(ahci_ctlp,
ahci_portp, port);
}
if (port_intr_status & AHCI_INTR_STATUS_DMPS) {
(void) ahci_intr_device_mechanical_presence_status(
ahci_ctlp, ahci_portp, port);
}
if (port_intr_status & AHCI_INTR_STATUS_PRCS) {
(void) ahci_intr_phyrdy_change(ahci_ctlp, ahci_portp,
port);
}
if (port_intr_status & (AHCI_INTR_STATUS_UFS |
AHCI_INTR_STATUS_OFS |
AHCI_INTR_STATUS_IPMS |
AHCI_INTR_STATUS_INFS)) {
(void) ahci_intr_non_fatal_error(ahci_ctlp, ahci_portp,
port, port_intr_status);
}
if (port_intr_status & (AHCI_INTR_STATUS_IFS |
AHCI_INTR_STATUS_HBDS |
AHCI_INTR_STATUS_HBFS |
AHCI_INTR_STATUS_TFES))
(void) ahci_intr_fatal_error(ahci_ctlp, ahci_portp,
port, port_intr_status);
if (port_intr_status & AHCI_INTR_STATUS_CPDS) {
(void) ahci_intr_cold_port_detect(ahci_ctlp, ahci_portp, port);
}
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_IS(ahci_ctlp), (0x1 << port));
if (ahci_check_acc_handle(ahci_ctlp->ahcictl_ahci_acc_handle) !=
DDI_FM_OK) {
ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
DDI_SERVICE_UNAFFECTED);
ddi_fm_acc_err_clear(ahci_ctlp->ahcictl_ahci_acc_handle,
DDI_FME_VERSION);
}
}
static uint_t
ahci_intr(caddr_t arg1, caddr_t arg2)
{
#ifndef __lock_lint
_NOTE(ARGUNUSED(arg2))
#endif
ahci_ctl_t *ahci_ctlp = (ahci_ctl_t *)arg1;
ahci_port_t *ahci_portp;
int32_t global_intr_status;
uint8_t port;
global_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_IS(ahci_ctlp));
if (!(global_intr_status & ahci_ctlp->ahcictl_ports_implemented)) {
return (DDI_INTR_UNCLAIMED);
}
if (ahci_check_acc_handle(ahci_ctlp->ahcictl_ahci_acc_handle) !=
DDI_FM_OK) {
ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
DDI_SERVICE_UNAFFECTED);
ddi_fm_acc_err_clear(ahci_ctlp->ahcictl_ahci_acc_handle,
DDI_FME_VERSION);
return (DDI_INTR_UNCLAIMED);
}
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
continue;
}
if (!((0x1 << port) & global_intr_status)) {
continue;
}
ahci_portp = ahci_ctlp->ahcictl_ports[port];
ahci_port_intr(ahci_ctlp, ahci_portp, port);
}
return (DDI_INTR_CLAIMED);
}
static int
ahci_intr_cmd_cmplt(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port)
{
uint32_t port_cmd_issue = 0;
uint32_t finished_tags;
int finished_slot;
sata_pkt_t *satapkt;
ahci_fis_d2h_register_t *rcvd_fisp;
#if AHCI_DEBUG
ahci_cmd_header_t *cmd_header;
uint32_t cmd_dmacount;
#endif
mutex_enter(&ahci_portp->ahciport_mutex);
if (!ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
!RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp) &&
!NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
if (ahci_check_acc_handle(ahci_ctlp->ahcictl_ahci_acc_handle)
!= DDI_FM_OK) {
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_FAILURE);
}
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
finished_tags = 0x1 & ~port_cmd_issue;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_cmd_cmplt: port %d the sata pkt for error "
"retrieval is finished, and finished_tags = 0x%x",
port, finished_tags);
} else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
finished_tags = 0x1 & ~port_cmd_issue;
AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
"ahci_intr_cmd_cmplt: port %d the sata pkt for r/w "
"port multiplier is finished, and finished_tags = 0x%x",
port, finished_tags);
} else {
finished_tags = ahci_portp->ahciport_pending_tags &
~port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp);
}
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"ahci_intr_cmd_cmplt: pending_tags = 0x%x, "
"port_cmd_issue = 0x%x finished_tags = 0x%x",
ahci_portp->ahciport_pending_tags, port_cmd_issue,
finished_tags);
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
(finished_tags == 0x1)) {
satapkt = ahci_portp->ahciport_err_retri_pkt;
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"ahci_intr_cmd_cmplt: sending up pkt 0x%p "
"with SATA_PKT_COMPLETED", (void *)satapkt);
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
goto out;
}
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp) &&
(finished_tags == 0x1)) {
satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"ahci_intr_cmd_cmplt: sending up pkt 0x%p "
"with SATA_PKT_COMPLETED", (void *)satapkt);
if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs) {
rcvd_fisp = &(ahci_portp->ahciport_rcvd_fis->
ahcirf_d2h_register_fis);
satapkt->satapkt_cmd.satacmd_status_reg =
GET_RFIS_STATUS(rcvd_fisp);
ahci_copy_out_regs(&satapkt->satapkt_cmd, rcvd_fisp);
}
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
goto out;
}
while (finished_tags) {
finished_slot = ddi_ffs(finished_tags) - 1;
if (finished_slot == -1) {
goto out;
}
satapkt = ahci_portp->ahciport_slot_pkts[finished_slot];
ASSERT(satapkt != NULL);
#if AHCI_DEBUG
cmd_dmacount =
ahci_portp->ahciport_prd_bytecounts[finished_slot];
if (cmd_dmacount) {
cmd_header =
&ahci_portp->ahciport_cmd_list[finished_slot];
AHCIDBG(AHCIDBG_INTR|AHCIDBG_PRDT, ahci_ctlp,
"ahci_intr_cmd_cmplt: port %d, "
"PRD Byte Count = 0x%x, "
"ahciport_prd_bytecounts = 0x%x", port,
cmd_header->ahcich_prd_byte_count,
cmd_dmacount);
if (cmd_header->ahcich_prd_byte_count != cmd_dmacount) {
AHCIDBG(AHCIDBG_UNDERFLOW, ahci_ctlp,
"ahci_intr_cmd_cmplt: port %d, "
"an underflow occurred", port);
}
}
#endif
if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs) {
rcvd_fisp = &(ahci_portp->ahciport_rcvd_fis->
ahcirf_d2h_register_fis);
satapkt->satapkt_cmd.satacmd_status_reg =
GET_RFIS_STATUS(rcvd_fisp);
ahci_copy_out_regs(&satapkt->satapkt_cmd, rcvd_fisp);
}
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"ahci_intr_cmd_cmplt: sending up pkt 0x%p "
"with SATA_PKT_COMPLETED", (void *)satapkt);
CLEAR_BIT(ahci_portp->ahciport_pending_tags, finished_slot);
CLEAR_BIT(finished_tags, finished_slot);
ahci_portp->ahciport_slot_pkts[finished_slot] = NULL;
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
}
out:
AHCIDBG(AHCIDBG_PKTCOMP, ahci_ctlp,
"ahci_intr_cmd_cmplt: pending_tags = 0x%x",
ahci_portp->ahciport_pending_tags);
ahci_flush_doneq(ahci_portp);
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
static int
ahci_intr_set_device_bits(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port)
{
ahci_addr_t addr;
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
"ahci_intr_set_device_bits enter: port %d", port);
AHCI_ADDR_SET_PORT(&addr, port);
(void) ahci_intr_ncq_events(ahci_ctlp, ahci_portp, &addr);
if (ahci_ctlp->ahcictl_cap & AHCI_CAP_SNTF) {
(void) ahci_intr_pmult_sntf_events(ahci_ctlp,
ahci_portp, port);
}
return (AHCI_SUCCESS);
}
static int
ahci_intr_ncq_events(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, ahci_addr_t *addrp)
{
uint32_t port_sactive;
uint32_t port_cmd_issue;
uint32_t issued_tags;
int issued_slot;
uint32_t finished_tags;
int finished_slot;
uint8_t port = addrp->aa_port;
sata_pkt_t *satapkt;
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
"ahci_intr_set_device_bits enter: port %d", port);
mutex_enter(&ahci_portp->ahciport_mutex);
if (!NCQ_CMD_IN_PROGRESS(ahci_portp)) {
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
port_sactive = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
finished_tags = ahci_portp->ahciport_pending_ncq_tags &
~port_sactive & AHCI_NCQ_SLOT_MASK(ahci_portp);
AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
"ahci_intr_set_device_bits: port %d pending_ncq_tags = 0x%x "
"port_sactive = 0x%x", port,
ahci_portp->ahciport_pending_ncq_tags, port_sactive);
AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
"ahci_intr_set_device_bits: finished_tags = 0x%x", finished_tags);
port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
issued_tags = ahci_portp->ahciport_pending_tags &
~port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp);
if (ahci_check_acc_handle(ahci_ctlp->ahcictl_ahci_acc_handle)
!= DDI_FM_OK) {
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_FAILURE);
}
AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
"ahci_intr_set_device_bits: port %d pending_tags = 0x%x "
"port_cmd_issue = 0x%x", port,
ahci_portp->ahciport_pending_tags, port_cmd_issue);
AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
"ahci_intr_set_device_bits: issued_tags = 0x%x", issued_tags);
while (issued_tags) {
issued_slot = ddi_ffs(issued_tags) - 1;
if (issued_slot == -1) {
goto next;
}
CLEAR_BIT(ahci_portp->ahciport_pending_tags, issued_slot);
CLEAR_BIT(issued_tags, issued_slot);
}
next:
while (finished_tags) {
finished_slot = ddi_ffs(finished_tags) - 1;
if (finished_slot == -1) {
goto out;
}
ASSERT(!(ahci_portp->ahciport_pending_tags &
(0x1 << finished_slot)));
satapkt = ahci_portp->ahciport_slot_pkts[finished_slot];
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
"ahci_intr_set_device_bits: sending up pkt 0x%p "
"with SATA_PKT_COMPLETED", (void *)satapkt);
CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags, finished_slot);
CLEAR_BIT(finished_tags, finished_slot);
ahci_portp->ahciport_slot_pkts[finished_slot] = NULL;
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
}
out:
AHCIDBG(AHCIDBG_PKTCOMP|AHCIDBG_NCQ, ahci_ctlp,
"ahci_intr_set_device_bits: port %d "
"pending_ncq_tags = 0x%x pending_tags = 0x%x",
port, ahci_portp->ahciport_pending_ncq_tags,
ahci_portp->ahciport_pending_tags);
ahci_flush_doneq(ahci_portp);
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
static int
ahci_intr_pmult_sntf_events(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
uint8_t port)
{
sata_device_t sdevice;
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
"ahci_intr_pmult_sntf_events enter: port %d ", port);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
if (ahci_ctlp->ahcictl_flags & AHCI_ATTACH) {
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (AHCI_SUCCESS);
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
mutex_enter(&ahci_portp->ahciport_mutex);
if (ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) {
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
ASSERT(ahci_portp->ahciport_pmult_info != NULL);
ahci_portp->ahciport_pmult_info->ahcipmi_snotif_tags =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSNTF(ahci_ctlp, port));
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSNTF(ahci_ctlp, port),
AHCI_SNOTIF_CLEAR_ALL);
if (ahci_portp->ahciport_pmult_info->ahcipmi_snotif_tags == 0) {
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_PMULT_SNTF) {
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_PMULT_SNTF;
if (ahci_portp->ahciport_reset_in_progress == 1) {
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"port %d snotif event ignored", port);
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_PMULT_SNTF;
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
"PxSNTF is set to 0x%x by port multiplier",
ahci_portp->ahciport_pmult_info->ahcipmi_snotif_tags);
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport = ahci_ctlp->
ahcictl_port_to_cport[port];
sdevice.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
sdevice.satadev_state = SATA_PSTATE_PWRON;
ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
&sdevice,
SATA_EVNT_PMULT_LINK_CHANGED);
mutex_enter(&ahci_portp->ahciport_mutex);
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_PMULT_SNTF;
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
static int
ahci_intr_port_connect_change(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port)
{
#if AHCI_DEBUG
uint32_t port_serror;
#endif
mutex_enter(&ahci_portp->ahciport_mutex);
#if AHCI_DEBUG
port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_intr_port_connect_change: port %d, "
"port_serror = 0x%x", port, port_serror);
#endif
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
SERROR_EXCHANGED_ERR);
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
static int
ahci_intr_device_mechanical_presence_status(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port)
{
uint32_t cap_status, port_cmd_status;
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_intr_device_mechanical_presence_status enter, "
"port %d", port);
cap_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_CAP(ahci_ctlp));
mutex_enter(&ahci_portp->ahciport_mutex);
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
if (!(cap_status & AHCI_HBA_CAP_SMPS) ||
!(port_cmd_status & AHCI_CMD_STATUS_MPSP)) {
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"CAP.SMPS or PxCMD.MPSP is not set, so just ignore "
"the interrupt: cap_status = 0x%x, "
"port_cmd_status = 0x%x", cap_status, port_cmd_status);
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
#if AHCI_DEBUG
if (port_cmd_status & AHCI_CMD_STATUS_MPSS) {
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"The mechanical presence switch is open: "
"port %d, port_cmd_status = 0x%x",
port, port_cmd_status);
} else {
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"The mechanical presence switch is close: "
"port %d, port_cmd_status = 0x%x",
port, port_cmd_status);
}
#endif
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
static int
ahci_intr_phyrdy_change(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port)
{
uint32_t port_sstatus = 0;
sata_device_t sdevice;
int dev_exists_now = 0;
int dev_existed_previously = 0;
ahci_addr_t port_addr;
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_intr_phyrdy_change enter, port %d", port);
mutex_enter(&ahci_portp->ahciport_mutex);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
SERROR_PHY_RDY_CHG);
mutex_exit(&ahci_portp->ahciport_mutex);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
if ((ahci_ctlp->ahcictl_sata_hba_tran == NULL) ||
(ahci_portp == NULL)) {
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return (AHCI_SUCCESS);
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
mutex_enter(&ahci_portp->ahciport_mutex);
port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
if (SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_PHYCOM) {
dev_exists_now = 1;
}
if (ahci_portp->ahciport_device_type != SATA_DTYPE_NONE) {
dev_existed_previously = 1;
}
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_NODEV) {
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_NODEV;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_phyrdy_change: port %d "
"AHCI_PORT_FLAG_NODEV is cleared", port);
if (dev_exists_now == 0)
dev_existed_previously = 1;
}
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
sdevice.satadev_addr.qual = SATA_ADDR_CPORT;
sdevice.satadev_addr.pmport = 0;
sdevice.satadev_state = SATA_PSTATE_PWRON;
ahci_portp->ahciport_port_state = SATA_PSTATE_PWRON;
AHCI_ADDR_SET_PORT(&port_addr, port);
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_HOTPLUG;
if (dev_exists_now) {
if (dev_existed_previously) {
AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
"ahci_intr_phyrdy_change port %d "
"device link lost/established", port);
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
&sdevice,
SATA_EVNT_LINK_LOST|SATA_EVNT_LINK_ESTABLISHED);
mutex_enter(&ahci_portp->ahciport_mutex);
} else {
AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
"ahci_intr_phyrdy_change: port %d "
"device link established", port);
(void) ahci_initialize_port(ahci_ctlp,
ahci_portp, &port_addr);
if (ahci_start_port(ahci_ctlp, ahci_portp, port)
!= AHCI_SUCCESS) {
sdevice.satadev_state |= SATA_PSTATE_FAILED;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_phyrdy_change: port %d failed "
"at start port", port);
}
ahci_portp->ahciport_max_ncq_tags = 0;
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
&sdevice,
SATA_EVNT_LINK_ESTABLISHED);
mutex_enter(&ahci_portp->ahciport_mutex);
}
} else {
if (dev_existed_previously) {
AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
"ahci_intr_phyrdy_change: port %d "
"device link lost", port);
ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
ahci_portp, port);
if (ahci_portp->ahciport_device_type ==
SATA_DTYPE_PMULT) {
ahci_dealloc_pmult(ahci_ctlp, ahci_portp);
}
ahci_portp->ahciport_device_type = SATA_DTYPE_NONE;
ahci_portp->ahciport_port_state = SATA_STATE_UNKNOWN;
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
&sdevice,
SATA_EVNT_LINK_LOST);
mutex_enter(&ahci_portp->ahciport_mutex);
}
}
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_HOTPLUG;
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
static int
ahci_intr_non_fatal_error(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
uint8_t port, uint32_t intr_status)
{
uint32_t port_serror;
#if AHCI_DEBUG
uint32_t port_cmd_status;
uint32_t port_cmd_issue;
uint32_t port_sactive;
int current_slot;
uint32_t current_tags;
sata_pkt_t *satapkt;
ahci_cmd_header_t *cmd_header;
uint32_t cmd_dmacount;
#endif
mutex_enter(&ahci_portp->ahciport_mutex);
port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY|AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_non_fatal_error: port %d, "
"PxSERR = 0x%x, PxIS = 0x%x ", port, port_serror, intr_status);
ahci_log_serror_message(ahci_ctlp, port, port_serror, 1);
if (intr_status & AHCI_INTR_STATUS_UFS) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci port %d has unknown FIS error", port);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
SERROR_FIS_TYPE);
}
#if AHCI_DEBUG
if (intr_status & AHCI_INTR_STATUS_IPMS) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci port %d "
"has Incorrect Port Multiplier Status error", port);
}
if (intr_status & AHCI_INTR_STATUS_OFS) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
"ahci port %d has overflow error", port);
}
if (intr_status & AHCI_INTR_STATUS_INFS) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
"ahci port %d has interface non fatal error", port);
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
current_slot = (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
AHCI_CMD_STATUS_CCS_SHIFT;
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
satapkt = ahci_portp->ahciport_err_retri_pkt;
ASSERT(satapkt != NULL);
ASSERT(current_slot == 0);
} else {
satapkt = ahci_portp->ahciport_slot_pkts[current_slot];
}
if (satapkt != NULL) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_non_fatal_error: pending_tags = 0x%x "
"cmd 0x%x", ahci_portp->ahciport_pending_tags,
satapkt->satapkt_cmd.satacmd_cmd_reg);
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_non_fatal_error: port %d, "
"satapkt 0x%p is being processed when error occurs",
port, (void *)satapkt);
cmd_dmacount =
ahci_portp->ahciport_prd_bytecounts[current_slot];
if (cmd_dmacount) {
cmd_header = &ahci_portp->
ahciport_cmd_list[current_slot];
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_non_fatal_error: port %d, "
"PRD Byte Count = 0x%x, "
"ahciport_prd_bytecounts = 0x%x", port,
cmd_header->ahcich_prd_byte_count,
cmd_dmacount);
}
}
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
port_sactive = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ|AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_non_fatal_error: pending_ncq_tags = 0x%x "
"port_sactive = 0x%x port_cmd_issue = 0x%x",
ahci_portp->ahciport_pending_ncq_tags,
port_sactive, port_cmd_issue);
current_tags = ahci_portp->ahciport_pending_ncq_tags &
port_sactive & ~port_cmd_issue &
AHCI_NCQ_SLOT_MASK(ahci_portp);
while (current_tags) {
current_slot = ddi_ffs(current_tags) - 1;
if (current_slot == -1) {
goto out;
}
satapkt = ahci_portp->ahciport_slot_pkts[current_slot];
AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ|AHCIDBG_ERRS,
ahci_ctlp, "ahci_intr_non_fatal_error: "
"port %d, satapkt 0x%p is outstanding when "
"error occurs", port, (void *)satapkt);
CLEAR_BIT(current_tags, current_slot);
}
}
out:
#endif
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
static int
ahci_intr_fatal_error(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port, uint32_t intr_status)
{
uint32_t port_cmd_status;
uint32_t port_serror;
uint32_t task_file_status;
int failed_slot;
sata_pkt_t *spkt = NULL;
uint8_t err_byte;
ahci_event_arg_t *args;
int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
uint32_t failed_tags = 0;
int task_fail_flag = 0, task_abort_flag = 0;
uint32_t slot_status;
mutex_enter(&ahci_portp->ahciport_mutex);
if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
"ahci_intr_fatal_error: port %d no device attached, "
"and just return without doing anything", port);
goto out0;
}
if (intr_status & AHCI_INTR_STATUS_TFES) {
task_file_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_fatal_error: port %d "
"task_file_status = 0x%x", port, task_file_status);
task_fail_flag = 1;
err_byte = (task_file_status & AHCI_TFD_ERR_MASK)
>> AHCI_TFD_ERR_SHIFT;
if (err_byte == SATA_ERROR_ABORT)
task_abort_flag = 1;
}
if (task_fail_flag == 1) {
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
port_cmd_status = ddi_get32(
ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
failed_slot = (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
AHCI_CMD_STATUS_CCS_SHIFT;
failed_tags = 0x1 << failed_slot;
spkt = ahci_portp->ahciport_slot_pkts[failed_slot];
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
"ahci_intr_fatal_error: spkt 0x%p is being "
"processed when fatal error occurred for port %d",
spkt, port);
if ((spkt != NULL) &&
(spkt->satapkt_cmd.satacmd_cmd_reg ==
SATAC_ID_DEVICE) &&
(task_abort_flag == 1))
goto out1;
if ((spkt != NULL) &&
(spkt->satapkt_cmd.satacmd_cmd_reg == SATAC_PACKET))
goto out1;
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(
ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
failed_tags = slot_status &
AHCI_NCQ_SLOT_MASK(ahci_portp);
}
}
ahci_log_fatal_error_message(ahci_ctlp, port, intr_status);
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_ERRPRINT;
port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
ahci_log_serror_message(ahci_ctlp, port, port_serror, 0);
if (task_fail_flag == 1) {
cmn_err(CE_WARN, "!ahci%d: ahci port %d task_file_status "
"= 0x%x", instance, port, task_file_status);
if (task_abort_flag == 1) {
cmn_err(CE_WARN, "!ahci%d: the below command (s) on "
"port %d are aborted", instance, port);
ahci_dump_commands(ahci_ctlp, port, failed_tags);
}
}
out1:
args = ahci_portp->ahciport_event_args;
args->ahciea_ctlp = (void *)ahci_ctlp;
args->ahciea_portp = (void *)ahci_portp;
args->ahciea_event = intr_status;
AHCI_ADDR_SET_PORT((ahci_addr_t *)args->ahciea_addrp, port);
if ((ddi_taskq_dispatch(ahci_portp->ahciport_event_taskq,
ahci_events_handler,
(void *)args, DDI_NOSLEEP)) != DDI_SUCCESS) {
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_ERRPRINT;
cmn_err(CE_WARN, "!ahci%d: start taskq for error recovery "
"port %d failed", instance, port);
}
out0:
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
static int
ahci_intr_cold_port_detect(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port)
{
uint32_t port_cmd_status;
sata_device_t sdevice;
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"ahci_intr_cold_port_detect enter, port %d", port);
mutex_enter(&ahci_portp->ahciport_mutex);
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
if (!(port_cmd_status & AHCI_CMD_STATUS_CPD)) {
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"port %d does not support cold presence detect, so "
"we just ignore this interrupt", port);
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"port %d device status has changed", port);
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
sdevice.satadev_addr.qual = SATA_ADDR_CPORT;
sdevice.satadev_addr.pmport = 0;
sdevice.satadev_state = SATA_PSTATE_PWRON;
if (port_cmd_status & AHCI_CMD_STATUS_CPS) {
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"port %d: a device is hot plugged", port);
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
&sdevice,
SATA_EVNT_DEVICE_ATTACHED);
mutex_enter(&ahci_portp->ahciport_mutex);
} else {
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"port %d: a device is hot unplugged", port);
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
&sdevice,
SATA_EVNT_DEVICE_DETACHED);
mutex_enter(&ahci_portp->ahciport_mutex);
}
mutex_exit(&ahci_portp->ahciport_mutex);
return (AHCI_SUCCESS);
}
static void
ahci_enable_port_intrs(ahci_ctl_t *ahci_ctlp, uint8_t port)
{
ASSERT(MUTEX_HELD(&ahci_ctlp->ahcictl_ports[port]->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_enable_port_intrs enter, port %d", port);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port),
AHCI_PORT_INTR_MASK);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_IS(ahci_ctlp), (1 << port));
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port),
(AHCI_INTR_STATUS_DHRS |
AHCI_INTR_STATUS_PSS |
AHCI_INTR_STATUS_SDBS |
AHCI_INTR_STATUS_UFS |
AHCI_INTR_STATUS_DPS |
AHCI_INTR_STATUS_PCS |
AHCI_INTR_STATUS_PRCS |
AHCI_INTR_STATUS_OFS |
AHCI_INTR_STATUS_INFS |
AHCI_INTR_STATUS_IFS |
AHCI_INTR_STATUS_HBDS |
AHCI_INTR_STATUS_HBFS |
AHCI_INTR_STATUS_TFES));
}
static void
ahci_enable_all_intrs(ahci_ctl_t *ahci_ctlp)
{
uint32_t ghc_control;
ASSERT(MUTEX_HELD(&ahci_ctlp->ahcictl_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_enable_all_intrs enter", NULL);
ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
ghc_control |= AHCI_HBA_GHC_IE;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
}
static void
ahci_disable_port_intrs(ahci_ctl_t *ahci_ctlp, uint8_t port)
{
ASSERT(ahci_ctlp->ahcictl_flags & AHCI_QUIESCE ||
MUTEX_HELD(&ahci_ctlp->ahcictl_ports[port]->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_disable_port_intrs enter, port %d", port);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port), 0);
}
static void
ahci_disable_all_intrs(ahci_ctl_t *ahci_ctlp)
{
uint32_t ghc_control;
ASSERT(ahci_ctlp->ahcictl_flags & (AHCI_ATTACH | AHCI_QUIESCE) ||
MUTEX_HELD(&ahci_ctlp->ahcictl_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_disable_all_intrs enter",
NULL);
ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
ghc_control &= ~AHCI_HBA_GHC_IE;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
}
static int
ahci_add_intrs(ahci_ctl_t *ahci_ctlp, int intr_type)
{
dev_info_t *dip = ahci_ctlp->ahcictl_dip;
int count, avail, actual;
int i, rc;
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
"ahci_add_intrs enter interrupt type 0x%x", intr_type);
rc = ddi_intr_get_nintrs(dip, intr_type, &count);
if ((rc != DDI_SUCCESS) || (count == 0)) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
"ddi_intr_get_nintrs() failed, "
"rc %d count %d\n", rc, count);
return (DDI_FAILURE);
}
rc = ddi_intr_get_navail(dip, intr_type, &avail);
if ((rc != DDI_SUCCESS) || (avail == 0)) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
"ddi_intr_get_navail() failed, "
"rc %d avail %d\n", rc, avail);
return (DDI_FAILURE);
}
#if AHCI_DEBUG
if (avail < count) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
"ddi_intr_get_nintrs returned %d, navail() returned %d",
count, avail);
}
#endif
if ((intr_type == DDI_INTR_TYPE_MSI) && (count > 1)) {
AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
"force to use one interrupt routine though the "
"HBA supports %d interrupt", count);
count = 1;
}
ahci_ctlp->ahcictl_intr_size = count * sizeof (ddi_intr_handle_t);
ahci_ctlp->ahcictl_intr_htable =
kmem_alloc(ahci_ctlp->ahcictl_intr_size, KM_SLEEP);
rc = ddi_intr_alloc(dip, ahci_ctlp->ahcictl_intr_htable,
intr_type, 0, count, &actual, DDI_INTR_ALLOC_NORMAL);
if ((rc != DDI_SUCCESS) || (actual == 0)) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
"ddi_intr_alloc() failed, rc %d count %d actual %d "
"avail %d\n", rc, count, actual, avail);
kmem_free(ahci_ctlp->ahcictl_intr_htable,
ahci_ctlp->ahcictl_intr_size);
return (DDI_FAILURE);
}
#if AHCI_DEBUG
if (actual < count) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
"Requested: %d, Received: %d", count, actual);
}
#endif
ahci_ctlp->ahcictl_intr_cnt = actual;
if (ddi_intr_get_pri(ahci_ctlp->ahcictl_intr_htable[0],
&ahci_ctlp->ahcictl_intr_pri) != DDI_SUCCESS) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
"ddi_intr_get_pri() failed", NULL);
for (i = 0; i < actual; i++) {
(void) ddi_intr_free(ahci_ctlp->ahcictl_intr_htable[i]);
}
kmem_free(ahci_ctlp->ahcictl_intr_htable,
ahci_ctlp->ahcictl_intr_size);
return (DDI_FAILURE);
}
if (ahci_ctlp->ahcictl_intr_pri >= ddi_intr_get_hilevel_pri()) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
"ahci_add_intrs: Hi level intr not supported", NULL);
for (i = 0; i < actual; i++) {
(void) ddi_intr_free(ahci_ctlp->ahcictl_intr_htable[i]);
}
kmem_free(ahci_ctlp->ahcictl_intr_htable,
sizeof (ddi_intr_handle_t));
return (DDI_FAILURE);
}
for (i = 0; i < actual; i++) {
if (ddi_intr_add_handler(ahci_ctlp->ahcictl_intr_htable[i],
ahci_intr, (caddr_t)ahci_ctlp, NULL) != DDI_SUCCESS) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
"ddi_intr_add_handler() failed", NULL);
for (i = 0; i < actual; i++) {
(void) ddi_intr_free(
ahci_ctlp->ahcictl_intr_htable[i]);
}
kmem_free(ahci_ctlp->ahcictl_intr_htable,
ahci_ctlp->ahcictl_intr_size);
return (DDI_FAILURE);
}
}
if (ddi_intr_get_cap(ahci_ctlp->ahcictl_intr_htable[0],
&ahci_ctlp->ahcictl_intr_cap) != DDI_SUCCESS) {
AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
"ddi_intr_get_cap() failed", NULL);
for (i = 0; i < actual; i++) {
(void) ddi_intr_free(
ahci_ctlp->ahcictl_intr_htable[i]);
}
kmem_free(ahci_ctlp->ahcictl_intr_htable,
ahci_ctlp->ahcictl_intr_size);
return (DDI_FAILURE);
}
if (ahci_ctlp->ahcictl_intr_cap & DDI_INTR_FLAG_BLOCK) {
(void) ddi_intr_block_enable(ahci_ctlp->ahcictl_intr_htable,
ahci_ctlp->ahcictl_intr_cnt);
} else {
for (i = 0; i < ahci_ctlp->ahcictl_intr_cnt; i++) {
(void) ddi_intr_enable(
ahci_ctlp->ahcictl_intr_htable[i]);
}
}
return (DDI_SUCCESS);
}
static void
ahci_rem_intrs(ahci_ctl_t *ahci_ctlp)
{
int x;
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_rem_intrs entered", NULL);
if ((ahci_ctlp->ahcictl_intr_type == DDI_INTR_TYPE_MSI) &&
(ahci_ctlp->ahcictl_intr_cap & DDI_INTR_FLAG_BLOCK)) {
(void) ddi_intr_block_disable(ahci_ctlp->ahcictl_intr_htable,
ahci_ctlp->ahcictl_intr_cnt);
} else {
for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
(void) ddi_intr_disable(
ahci_ctlp->ahcictl_intr_htable[x]);
}
}
for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
(void) ddi_intr_remove_handler(
ahci_ctlp->ahcictl_intr_htable[x]);
(void) ddi_intr_free(ahci_ctlp->ahcictl_intr_htable[x]);
}
kmem_free(ahci_ctlp->ahcictl_intr_htable, ahci_ctlp->ahcictl_intr_size);
}
static int
ahci_put_port_into_notrunning_state(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port)
{
uint32_t port_cmd_status;
int loop_count;
ASSERT(ahci_ctlp->ahcictl_flags & AHCI_QUIESCE ||
MUTEX_HELD(&ahci_ctlp->ahcictl_ports[port]->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_put_port_into_notrunning_state enter: port %d", port);
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
port_cmd_status &= ~AHCI_CMD_STATUS_ST;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port), port_cmd_status);
loop_count = 0;
do {
port_cmd_status =
ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
if (loop_count++ > AHCI_POLLRATE_PORT_IDLE) {
AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
"clearing port %d CMD.CR timeout, "
"port_cmd_status = 0x%x", port,
port_cmd_status);
break;
}
drv_usecwait(AHCI_10MS_USECS);
} while (port_cmd_status & AHCI_CMD_STATUS_CR);
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_STARTED;
if (port_cmd_status & AHCI_CMD_STATUS_CR) {
AHCIDBG(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
"ahci_put_port_into_notrunning_state: failed to clear "
"PxCMD.CR to '0' after loop count: %d, and "
"port_cmd_status = 0x%x", loop_count, port_cmd_status);
return (AHCI_FAILURE);
} else {
AHCIDBG(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
"ahci_put_port_into_notrunning_state: succeeded to clear "
"PxCMD.CR to '0' after loop count: %d, and "
"port_cmd_status = 0x%x", loop_count, port_cmd_status);
return (AHCI_SUCCESS);
}
}
static int
ahci_restart_port_wait_till_ready(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port, int flag, int *reset_flag)
{
uint32_t port_sstatus;
uint32_t task_file_status;
sata_device_t sdevice;
int rval;
ahci_addr_t addr_port;
ahci_pmult_info_t *pminfo = NULL;
int dev_exists_begin = 0;
int dev_exists_end = 0;
uint32_t previous_dev_type = ahci_portp->ahciport_device_type;
int npmport = 0;
uint8_t cport = ahci_ctlp->ahcictl_port_to_cport[port];
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_restart_port_wait_till_ready: port %d enter", port);
AHCI_ADDR_SET_PORT(&addr_port, port);
if (ahci_portp->ahciport_device_type != SATA_DTYPE_NONE)
dev_exists_begin = 1;
rval = ahci_put_port_into_notrunning_state(ahci_ctlp, ahci_portp,
port);
if (rval != AHCI_SUCCESS)
goto reset;
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
AHCI_SERROR_CLEAR_ALL);
task_file_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
if (!(task_file_status & (AHCI_TFD_STS_BSY | AHCI_TFD_STS_DRQ)) &&
!(flag & AHCI_PORT_RESET))
goto out;
reset:
ahci_disable_port_intrs(ahci_ctlp, port);
rval = ahci_port_reset(ahci_ctlp, ahci_portp, &addr_port);
ahci_enable_port_intrs(ahci_ctlp, port);
#ifdef AHCI_DEBUG
if (rval != AHCI_SUCCESS)
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_restart_port_wait_till_ready: port %d failed",
port);
#endif
if (reset_flag != NULL)
*reset_flag = 1;
if ((ahci_portp->ahciport_device_type != SATA_DTYPE_NONE) &&
(ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) &&
!(flag & AHCI_RESET_NO_EVENTS_UP)) {
ahci_portp->ahciport_reset_in_progress = 1;
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport =
ahci_ctlp->ahcictl_port_to_cport[port];
sdevice.satadev_addr.pmport = 0;
sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
sdevice.satadev_state = SATA_DSTATE_RESET |
SATA_DSTATE_PWR_ACTIVE;
if (ahci_ctlp->ahcictl_sata_hba_tran) {
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
&sdevice,
SATA_EVNT_DEVICE_RESET);
mutex_enter(&ahci_portp->ahciport_mutex);
}
AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
"port %d sending event up: SATA_EVNT_DEVICE_RESET", port);
} else {
ahci_portp->ahciport_reset_in_progress = 0;
}
out:
(void) ahci_start_port(ahci_ctlp, ahci_portp, port);
port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
if (SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_PHYCOM) {
dev_exists_end = 1;
}
if (dev_exists_begin == 0 && dev_exists_end == 0)
return (rval);
if (dev_exists_begin == 1 && dev_exists_end == 0) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_restart_port_wait_till_ready: port %d "
"device is removed", port);
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_NODEV;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_restart_port_wait_till_ready: port %d "
"AHCI_PORT_FLAG_NODEV flag is set", port);
mutex_exit(&ahci_portp->ahciport_mutex);
(void) ahci_intr_phyrdy_change(ahci_ctlp, ahci_portp, port);
mutex_enter(&ahci_portp->ahciport_mutex);
return (rval);
}
if (ahci_portp->ahciport_device_type == SATA_DTYPE_ATAPI)
return (rval);
ahci_disable_port_intrs(ahci_ctlp, port);
ahci_find_dev_signature(ahci_ctlp, ahci_portp, &addr_port);
ahci_enable_port_intrs(ahci_ctlp, port);
if (previous_dev_type != SATA_DTYPE_PMULT &&
ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) {
ahci_dealloc_pmult(ahci_ctlp, ahci_portp);
(void) ahci_start_port(ahci_ctlp, ahci_portp, port);
return (rval);
}
if (previous_dev_type == SATA_DTYPE_PMULT &&
ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) {
ahci_dealloc_pmult(ahci_ctlp, ahci_portp);
(void) ahci_start_port(ahci_ctlp, ahci_portp, port);
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport =
ahci_ctlp->ahcictl_port_to_cport[port];
sdevice.satadev_addr.pmport = 0;
sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
sdevice.satadev_state = SATA_DSTATE_RESET |
SATA_DSTATE_PWR_ACTIVE;
mutex_exit(&ahci_portp->ahciport_mutex);
sata_hba_event_notify(
ahci_ctlp->ahcictl_dip,
&sdevice,
SATA_EVNT_DEVICE_RESET);
mutex_enter(&ahci_portp->ahciport_mutex);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"Port multiplier is [Gone] at port %d ", port);
AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
"port %d sending event up: SATA_EVNT_DEVICE_RESET", port);
return (AHCI_SUCCESS);
}
if (ahci_portp->ahciport_device_type == SATA_DTYPE_PMULT) {
ahci_alloc_pmult(ahci_ctlp, ahci_portp);
(void) ahci_start_port(ahci_ctlp, ahci_portp, port);
}
pminfo = ahci_portp->ahciport_pmult_info;
ASSERT(pminfo != NULL);
ahci_portp->ahciport_port_state = SATA_STATE_READY;
for (npmport = 0; npmport < pminfo->ahcipmi_num_dev_ports; npmport++)
pminfo->ahcipmi_port_state[npmport] = SATA_STATE_UNKNOWN;
ahci_portp->ahciport_reset_in_progress = 1;
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport = cport;
sdevice.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
sdevice.satadev_state = SATA_DSTATE_RESET | SATA_DSTATE_PWR_ACTIVE;
sata_hba_event_notify(ahci_ctlp->ahcictl_dip, &sdevice,
SATA_EVNT_DEVICE_RESET);
return (rval);
}
static void
ahci_mop_commands(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp,
uint32_t slot_status,
uint32_t failed_tags,
uint32_t timeout_tags,
uint32_t aborted_tags,
uint32_t reset_tags)
{
uint32_t finished_tags = 0;
uint32_t unfinished_tags = 0;
int tmp_slot;
sata_pkt_t *satapkt;
int ncq_cmd_in_progress = 0;
int err_retri_cmd_in_progress = 0;
int rdwr_pmult_cmd_in_progress = 0;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_mop_commands entered: port: %d slot_status: 0x%x",
ahci_portp->ahciport_port_num, slot_status);
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_mop_commands: failed_tags: 0x%x, "
"timeout_tags: 0x%x aborted_tags: 0x%x, "
"reset_tags: 0x%x", failed_tags,
timeout_tags, aborted_tags, reset_tags);
#ifdef AHCI_DEBUG
if (ahci_debug_flags & AHCIDBG_ERRS) {
int i;
char msg_buf[200] = {0, };
for (i = 0x1f; i >= 0; i--) {
if (ahci_portp->ahciport_slot_pkts[i] != NULL)
msg_buf[i] = 'X';
else
msg_buf[i] = '.';
}
msg_buf[0x20] = '\0';
cmn_err(CE_NOTE, "port[%d] slots: %s",
ahci_portp->ahciport_port_num, msg_buf);
cmn_err(CE_NOTE, "[ERR-RT] %p [RW-PM] %p ",
(void *)ahci_portp->ahciport_err_retri_pkt,
(void *)ahci_portp->ahciport_rdwr_pmult_pkt);
}
#endif
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
finished_tags = ahci_portp->ahciport_pending_tags &
~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
unfinished_tags = slot_status &
AHCI_SLOT_MASK(ahci_ctlp) &
~failed_tags &
~aborted_tags &
~reset_tags &
~timeout_tags;
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
ncq_cmd_in_progress = 1;
finished_tags = ahci_portp->ahciport_pending_ncq_tags &
~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
unfinished_tags = slot_status &
AHCI_NCQ_SLOT_MASK(ahci_portp) &
~failed_tags &
~aborted_tags &
~reset_tags &
~timeout_tags;
} else if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
err_retri_cmd_in_progress = 1;
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_NCQ, ahci_ctlp,
"ahci_mop_commands is called for port %d while "
"REQUEST SENSE or READ LOG EXT for error retrieval "
"is being executed slot_status = 0x%x",
ahci_portp->ahciport_port_num, slot_status);
ASSERT(ahci_portp->ahciport_mop_in_progress > 1);
ASSERT(slot_status == 0x1);
} else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
rdwr_pmult_cmd_in_progress = 1;
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
"ahci_mop_commands is called for port %d while "
"READ/WRITE PORTMULT command is being executed",
ahci_portp->ahciport_port_num);
ASSERT(slot_status == 0x1);
}
#ifdef AHCI_DEBUG
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_mop_commands: finished_tags: 0x%x, "
"unfinished_tags 0x%x", finished_tags, unfinished_tags);
#endif
while (finished_tags) {
tmp_slot = ddi_ffs(finished_tags) - 1;
if (tmp_slot == -1) {
break;
}
satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "ahci_mop_commands: "
"sending up pkt 0x%p with SATA_PKT_COMPLETED",
(void *)satapkt);
if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs) {
CLEAR_BIT(finished_tags, tmp_slot);
aborted_tags |= tmp_slot;
continue;
}
if (ncq_cmd_in_progress)
CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
tmp_slot);
CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
CLEAR_BIT(finished_tags, tmp_slot);
ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
}
while (failed_tags) {
if (err_retri_cmd_in_progress) {
satapkt = ahci_portp->ahciport_err_retri_pkt;
ASSERT(satapkt != NULL);
ASSERT(failed_tags == 0x1);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
"sending up pkt 0x%p with SATA_PKT_DEV_ERROR",
(void *)satapkt);
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_DEV_ERROR);
break;
}
if (rdwr_pmult_cmd_in_progress) {
satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_mop_commands: sending up "
"rdwr pmult pkt 0x%p with SATA_PKT_DEV_ERROR",
(void *)satapkt);
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_DEV_ERROR);
break;
}
tmp_slot = ddi_ffs(failed_tags) - 1;
if (tmp_slot == -1) {
break;
}
satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
"sending up pkt 0x%p with SATA_PKT_DEV_ERROR",
(void *)satapkt);
if (ncq_cmd_in_progress)
CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
tmp_slot);
CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
CLEAR_BIT(failed_tags, tmp_slot);
ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_DEV_ERROR);
}
while (timeout_tags) {
if (err_retri_cmd_in_progress) {
satapkt = ahci_portp->ahciport_err_retri_pkt;
ASSERT(satapkt != NULL);
ASSERT(timeout_tags == 0x1);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
"sending up pkt 0x%p with SATA_PKT_TIMEOUT",
(void *)satapkt);
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_TIMEOUT);
break;
}
if (rdwr_pmult_cmd_in_progress) {
satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_mop_commands: sending up "
"rdwr pmult pkt 0x%p with SATA_PKT_TIMEOUT",
(void *)satapkt);
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_TIMEOUT);
break;
}
tmp_slot = ddi_ffs(timeout_tags) - 1;
if (tmp_slot == -1) {
break;
}
satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
"sending up pkt 0x%p with SATA_PKT_TIMEOUT",
(void *)satapkt);
if (ncq_cmd_in_progress)
CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
tmp_slot);
CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
CLEAR_BIT(timeout_tags, tmp_slot);
ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_TIMEOUT);
}
while (aborted_tags) {
if (err_retri_cmd_in_progress) {
satapkt = ahci_portp->ahciport_err_retri_pkt;
ASSERT(satapkt != NULL);
ASSERT(aborted_tags == 0x1);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
"sending up pkt 0x%p with SATA_PKT_ABORTED",
(void *)satapkt);
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_ABORTED);
break;
}
if (rdwr_pmult_cmd_in_progress) {
satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
ASSERT(satapkt != NULL);
ASSERT(aborted_tags == 0x1);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_mop_commands: sending up "
"rdwr pmult pkt 0x%p with SATA_PKT_ABORTED",
(void *)satapkt);
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_ABORTED);
break;
}
tmp_slot = ddi_ffs(aborted_tags) - 1;
if (tmp_slot == -1) {
break;
}
satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
"sending up pkt 0x%p with SATA_PKT_ABORTED",
(void *)satapkt);
if (ncq_cmd_in_progress)
CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
tmp_slot);
CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
CLEAR_BIT(aborted_tags, tmp_slot);
ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_ABORTED);
}
while (reset_tags) {
if (rdwr_pmult_cmd_in_progress) {
satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
ASSERT(satapkt != NULL);
ASSERT(aborted_tags == 0x1);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_mop_commands: sending up "
"rdwr pmult pkt 0x%p with SATA_PKT_RESET",
(void *)satapkt);
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_RESET);
break;
}
tmp_slot = ddi_ffs(reset_tags) - 1;
if (tmp_slot == -1) {
break;
}
satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
"sending up pkt 0x%p with SATA_PKT_RESET",
(void *)satapkt);
if (ncq_cmd_in_progress)
CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
tmp_slot);
CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
CLEAR_BIT(reset_tags, tmp_slot);
ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_RESET);
}
while (unfinished_tags) {
tmp_slot = ddi_ffs(unfinished_tags) - 1;
if (tmp_slot == -1) {
break;
}
satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
ASSERT(satapkt != NULL);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
"sending up pkt 0x%p with SATA_PKT_RESET",
(void *)satapkt);
if (ncq_cmd_in_progress)
CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
tmp_slot);
CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
CLEAR_BIT(unfinished_tags, tmp_slot);
ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_RESET);
}
ahci_portp->ahciport_mop_in_progress--;
ASSERT(ahci_portp->ahciport_mop_in_progress >= 0);
if (ahci_portp->ahciport_mop_in_progress == 0)
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_MOPPING;
ahci_flush_doneq(ahci_portp);
}
static uint32_t
ahci_get_rdlogext_data(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
uint8_t port)
{
sata_device_t sdevice;
sata_pkt_t *rdlog_spkt, *spkt;
ddi_dma_handle_t buf_dma_handle;
ahci_addr_t addr;
int loop_count;
int rval;
int failed_slot;
uint32_t failed_tags = 0;
struct sata_ncq_error_recovery_page *ncq_err_page;
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_NCQ, ahci_ctlp,
"ahci_get_rdlogext_data enter: port %d", port);
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
sdevice.satadev_addr.pmport = 0;
ahci_get_ahci_addr(ahci_ctlp, &sdevice, &addr);
loop_count = 0;
loop:
rdlog_spkt = sata_get_error_retrieval_pkt(ahci_ctlp->ahcictl_dip,
&sdevice, SATA_ERR_RETR_PKT_TYPE_NCQ);
if (rdlog_spkt == NULL) {
if (loop_count++ < AHCI_POLLRATE_GET_SPKT) {
drv_usecwait(AHCI_10MS_USECS);
goto loop;
}
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"failed to get rdlog spkt for port %d", port);
return (failed_tags);
}
ASSERT(rdlog_spkt->satapkt_op_mode & SATA_OPMODE_SYNCH);
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_RDLOGEXT;
ahci_portp->ahciport_err_retri_pkt = rdlog_spkt;
(void) ahci_do_sync_start(ahci_ctlp, ahci_portp, &addr, rdlog_spkt);
ahci_portp->ahciport_err_retri_pkt = NULL;
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_RDLOGEXT;
if (rdlog_spkt->satapkt_reason == SATA_PKT_COMPLETED) {
buf_dma_handle = *(ddi_dma_handle_t *)
(rdlog_spkt->satapkt_cmd.satacmd_err_ret_buf_handle);
rval = ddi_dma_sync(buf_dma_handle, 0, 0,
DDI_DMA_SYNC_FORKERNEL);
if (rval == DDI_SUCCESS) {
ncq_err_page =
(struct sata_ncq_error_recovery_page *)rdlog_spkt->
satapkt_cmd.satacmd_bp->b_un.b_addr;
failed_slot = ncq_err_page->ncq_tag;
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_get_rdlogext_data: port %d "
"failed slot %d", port, failed_slot);
if (failed_slot & NQ) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"the failed slot is not a valid tag", NULL);
goto out;
}
failed_slot &= NCQ_TAG_MASK;
spkt = ahci_portp->ahciport_slot_pkts[failed_slot];
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_get_rdlogext_data: failed spkt 0x%p",
(void *)spkt);
if (spkt == NULL) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"the failed slot spkt is NULL", NULL);
goto out;
}
failed_tags = 0x1 << failed_slot;
ahci_copy_ncq_err_page(&spkt->satapkt_cmd,
ncq_err_page);
ahci_update_sata_registers(ahci_ctlp, port,
&spkt->satapkt_device);
}
}
out:
sata_free_error_retrieval_pkt(rdlog_spkt);
return (failed_tags);
}
static void
ahci_get_rqsense_data(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
uint8_t port, sata_pkt_t *spkt)
{
sata_device_t sdevice;
sata_pkt_t *rs_spkt;
sata_cmd_t *sata_cmd;
ddi_dma_handle_t buf_dma_handle;
ahci_addr_t addr;
int loop_count;
#if AHCI_DEBUG
struct scsi_extended_sense *rqsense;
#endif
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_get_rqsense_data enter: port %d", port);
bzero((void *)&sdevice, sizeof (sata_device_t));
sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
sdevice.satadev_addr.pmport = 0;
ahci_get_ahci_addr(ahci_ctlp, &sdevice, &addr);
sata_cmd = &spkt->satapkt_cmd;
loop_count = 0;
loop:
rs_spkt = sata_get_error_retrieval_pkt(ahci_ctlp->ahcictl_dip,
&sdevice, SATA_ERR_RETR_PKT_TYPE_ATAPI);
if (rs_spkt == NULL) {
if (loop_count++ < AHCI_POLLRATE_GET_SPKT) {
drv_usecwait(AHCI_10MS_USECS);
goto loop;
}
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"failed to get rs spkt for port %d", port);
return;
}
ASSERT(rs_spkt->satapkt_op_mode & SATA_OPMODE_SYNCH);
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_RQSENSE;
ahci_portp->ahciport_err_retri_pkt = rs_spkt;
(void) ahci_do_sync_start(ahci_ctlp, ahci_portp, &addr, rs_spkt);
ahci_portp->ahciport_err_retri_pkt = NULL;
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_RQSENSE;
if (rs_spkt->satapkt_reason == SATA_PKT_COMPLETED) {
buf_dma_handle = *(ddi_dma_handle_t *)
(rs_spkt->satapkt_cmd.satacmd_err_ret_buf_handle);
(void) ddi_dma_sync(buf_dma_handle, 0, 0,
DDI_DMA_SYNC_FORKERNEL);
bcopy(rs_spkt->
satapkt_cmd.satacmd_bp->b_un.b_addr,
&sata_cmd->satacmd_rqsense,
SATA_ATAPI_MIN_RQSENSE_LEN);
#if AHCI_DEBUG
rqsense = (struct scsi_extended_sense *)
sata_cmd->satacmd_rqsense;
AHCIDBG(AHCIDBG_SENSEDATA, ahci_ctlp, "\n", NULL);
AHCIDBG(AHCIDBG_SENSEDATA, ahci_ctlp,
"Sense data for satapkt %p ATAPI cmd 0x%x",
spkt, sata_cmd->satacmd_acdb[0]);
AHCIDBG(AHCIDBG_SENSEDATA, ahci_ctlp,
" es_code 0x%x es_class 0x%x "
"es_key 0x%x es_add_code 0x%x "
"es_qual_code 0x%x",
rqsense->es_code, rqsense->es_class,
rqsense->es_key, rqsense->es_add_code,
rqsense->es_qual_code);
#endif
}
sata_free_error_retrieval_pkt(rs_spkt);
}
static void
ahci_fatal_error_recovery_handler(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, ahci_addr_t *addrp, uint32_t intr_status)
{
uint32_t port_cmd_status;
uint32_t slot_status = 0;
uint32_t failed_tags = 0;
int failed_slot;
int reset_flag = 0, flag = 0;
ahci_fis_d2h_register_t *ahci_rcvd_fisp;
sata_cmd_t *sata_cmd = NULL;
sata_pkt_t *spkt = NULL;
#if AHCI_DEBUG
ahci_cmd_header_t *cmd_header;
#endif
uint8_t port = addrp->aa_port;
int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
int rval;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_fatal_error_recovery_handler enter: port %d", port);
if (ahci_portp->ahciport_device_type == SATA_DTYPE_PMULT) {
ahci_pmult_error_recovery_handler(ahci_ctlp, ahci_portp,
port, intr_status);
flag = AHCI_PORT_RESET;
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
failed_slot = (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
AHCI_CMD_STATUS_CCS_SHIFT;
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
spkt = ahci_portp->ahciport_err_retri_pkt;
ASSERT(spkt != NULL);
} else {
spkt = ahci_portp->ahciport_slot_pkts[failed_slot];
if (spkt == NULL) {
goto next;
}
}
#if AHCI_DEBUG
if (ahci_portp->ahciport_prd_bytecounts[failed_slot]) {
cmd_header =
&ahci_portp->ahciport_cmd_list[failed_slot];
AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
"ahci_fatal_error_recovery_handler: port %d, "
"PRD Byte Count = 0x%x, "
"ahciport_prd_bytecounts = 0x%x", port,
cmd_header->ahcich_prd_byte_count,
ahci_portp->ahciport_prd_bytecounts[failed_slot]);
}
#endif
sata_cmd = &spkt->satapkt_cmd;
if (intr_status & AHCI_INTR_STATUS_TFES) {
ahci_rcvd_fisp = &(ahci_portp->ahciport_rcvd_fis->
ahcirf_d2h_register_fis);
ahci_copy_err_cnxt(sata_cmd, ahci_rcvd_fisp);
}
failed_tags = 0x1 << failed_slot;
ASSERT(failed_tags & slot_status);
ahci_update_sata_registers(ahci_ctlp, port,
&spkt->satapkt_device);
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
}
next:
#if AHCI_DEBUG
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_fatal_error_recovery_handler: port %d REQUEST SENSE "
"command or READ LOG EXT command for error data retrieval "
"failed", port);
ASSERT(slot_status == 0x1);
ASSERT(failed_slot == 0);
ASSERT(spkt->satapkt_cmd.satacmd_acdb[0] ==
SCMD_REQUEST_SENSE ||
spkt->satapkt_cmd.satacmd_cmd_reg ==
SATAC_READ_LOG_EXT);
}
#endif
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
ahci_portp->ahciport_mop_in_progress++;
rval = ahci_restart_port_wait_till_ready(ahci_ctlp, ahci_portp,
port, flag, &reset_flag);
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_ERRPRINT) {
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_ERRPRINT;
if (rval == AHCI_SUCCESS)
cmn_err(CE_WARN, "!ahci%d: error recovery for port %d "
"succeed", instance, port);
else
cmn_err(CE_WARN, "!ahci%d: error recovery for port %d "
"failed", instance, port);
}
if (reset_flag ||
ahci_portp->ahciport_device_type == SATA_DTYPE_NONE ||
spkt && spkt->satapkt_cmd.satacmd_cmd_reg == SATAC_ID_DEVICE ||
ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
goto out;
if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
failed_tags = ahci_get_rdlogext_data(ahci_ctlp,
ahci_portp, port);
goto out;
}
if (spkt && ahci_portp->ahciport_device_type == SATA_DTYPE_ATAPI)
ahci_get_rqsense_data(ahci_ctlp, ahci_portp, port, spkt);
out:
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"ahci_fatal_error_recovery_handler: port %d fatal error "
"occurred slot_status = 0x%x, pending_tags = 0x%x, "
"pending_ncq_tags = 0x%x failed_tags = 0x%x",
port, slot_status, ahci_portp->ahciport_pending_tags,
ahci_portp->ahciport_pending_ncq_tags, failed_tags);
ahci_mop_commands(ahci_ctlp,
ahci_portp,
slot_status,
failed_tags,
0,
0,
0);
}
static void
ahci_pmult_error_recovery_handler(ahci_ctl_t *ahci_ctlp,
ahci_port_t *ahci_portp, uint8_t port, uint32_t intr_status)
{
#ifndef __lock_lint
_NOTE(ARGUNUSED(intr_status))
#endif
uint32_t port_fbs_ctrl;
int loop_count = 0;
ahci_addr_t addr;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
if (!(ahci_ctlp->ahcictl_cap & AHCI_CAP_PMULT_FBSS))
return;
port_fbs_ctrl = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxFBS(ahci_ctlp, port));
if (!(port_fbs_ctrl & AHCI_FBS_EN))
return;
port_fbs_ctrl = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxFBS(ahci_ctlp, port));
if (port_fbs_ctrl & AHCI_FBS_SDE) {
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
"A Device Sepcific Error: port %d", port);
ahci_reject_all_abort_pkts(ahci_ctlp,
ahci_portp, 0);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxFBS(ahci_ctlp, port),
port_fbs_ctrl | AHCI_FBS_DEC);
loop_count = 0;
do {
port_fbs_ctrl = ddi_get32(ahci_ctlp->
ahcictl_ahci_acc_handle, (uint32_t *)
AHCI_PORT_PxFBS(ahci_ctlp, port));
if (loop_count++ > 1000)
return;
drv_usecwait(AHCI_100US_USECS);
} while (port_fbs_ctrl & AHCI_FBS_DEC);
(void) ahci_software_reset(ahci_ctlp,
ahci_portp, &addr);
} else {
cmn_err(CE_NOTE, "!FBS is not supported now.");
}
}
static void
ahci_events_handler(void *args)
{
ahci_event_arg_t *ahci_event_arg;
ahci_ctl_t *ahci_ctlp;
ahci_port_t *ahci_portp;
ahci_addr_t *addrp;
uint32_t event;
int instance;
ahci_event_arg = (ahci_event_arg_t *)args;
ahci_ctlp = ahci_event_arg->ahciea_ctlp;
ahci_portp = ahci_event_arg->ahciea_portp;
addrp = ahci_event_arg->ahciea_addrp;
event = ahci_event_arg->ahciea_event;
instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
"ahci_events_handler enter: port %d intr_status = 0x%x",
ahci_portp->ahciport_port_num, event);
mutex_enter(&ahci_portp->ahciport_mutex);
if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
"ahci_events_handler: port %d no device attached, "
"and just return without doing anything",
ahci_portp->ahciport_port_num);
if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_ERRPRINT) {
ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_ERRPRINT;
cmn_err(CE_WARN, "!ahci%d: error recovery for port %d "
"succeed", instance, ahci_portp->ahciport_port_num);
}
goto out;
}
if (event & (AHCI_INTR_STATUS_IFS |
AHCI_INTR_STATUS_HBDS |
AHCI_INTR_STATUS_HBFS |
AHCI_INTR_STATUS_TFES))
ahci_fatal_error_recovery_handler(ahci_ctlp, ahci_portp,
addrp, event);
out:
mutex_exit(&ahci_portp->ahciport_mutex);
}
static void
ahci_timeout_pkts(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
uint8_t port, uint32_t tmp_timeout_tags)
{
uint32_t slot_status = 0;
uint32_t finished_tags = 0;
uint32_t timeout_tags = 0;
AHCIDBG(AHCIDBG_TIMEOUT|AHCIDBG_ENTRY, ahci_ctlp,
"ahci_timeout_pkts enter: port %d", port);
mutex_enter(&ahci_portp->ahciport_mutex);
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp) ||
ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
}
#if AHCI_DEBUG
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_TIMEOUT, ahci_ctlp,
"ahci_timeout_pkts called while REQUEST SENSE "
"command or READ LOG EXT command for error recovery "
"timed out timeout_tags = 0x%x, slot_status = 0x%x, "
"pending_tags = 0x%x, pending_ncq_tags = 0x%x",
tmp_timeout_tags, slot_status,
ahci_portp->ahciport_pending_tags,
ahci_portp->ahciport_pending_ncq_tags);
ASSERT(slot_status == 0x1);
} else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_TIMEOUT, ahci_ctlp,
"ahci_timeout_pkts called while executing R/W PMULT "
"command timeout_tags = 0x%x, slot_status = 0x%x",
tmp_timeout_tags, slot_status);
ASSERT(slot_status == 0x1);
}
#endif
ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
ahci_portp->ahciport_mop_in_progress++;
(void) ahci_restart_port_wait_till_ready(ahci_ctlp, ahci_portp,
port, AHCI_PORT_RESET, NULL);
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
finished_tags = ahci_portp->ahciport_pending_tags &
~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
timeout_tags = tmp_timeout_tags & ~finished_tags;
AHCIDBG(AHCIDBG_TIMEOUT, ahci_ctlp,
"ahci_timeout_pkts: port %d, finished_tags = 0x%x, "
"timeout_tags = 0x%x, port_cmd_issue = 0x%x, "
"pending_tags = 0x%x ",
port, finished_tags, timeout_tags,
slot_status, ahci_portp->ahciport_pending_tags);
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
finished_tags = ahci_portp->ahciport_pending_ncq_tags &
~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
timeout_tags = tmp_timeout_tags & ~finished_tags;
AHCIDBG(AHCIDBG_TIMEOUT|AHCIDBG_NCQ, ahci_ctlp,
"ahci_timeout_pkts: port %d, finished_tags = 0x%x, "
"timeout_tags = 0x%x, port_sactive = 0x%x, "
"pending_ncq_tags = 0x%x ",
port, finished_tags, timeout_tags,
slot_status, ahci_portp->ahciport_pending_ncq_tags);
} else if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) ||
RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
timeout_tags = tmp_timeout_tags;
}
ahci_mop_commands(ahci_ctlp,
ahci_portp,
slot_status,
0,
timeout_tags,
0,
0);
mutex_exit(&ahci_portp->ahciport_mutex);
}
static void
ahci_watchdog_handler(ahci_ctl_t *ahci_ctlp)
{
ahci_port_t *ahci_portp;
sata_pkt_t *spkt;
uint32_t pending_tags;
uint32_t timeout_tags;
uint32_t port_cmd_status;
uint32_t port_sactive;
uint8_t port;
int tmp_slot;
int current_slot;
uint32_t current_tags;
int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
"ahci_watchdog_handler entered", NULL);
current_slot = 0;
current_tags = 0;
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
continue;
}
ahci_portp = ahci_ctlp->ahcictl_ports[port];
mutex_enter(&ahci_portp->ahciport_mutex);
if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
mutex_exit(&ahci_portp->ahciport_mutex);
continue;
}
if ((ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) &&
!(ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))) {
mutex_exit(&ahci_portp->ahciport_mutex);
continue;
}
pending_tags = 0;
port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) ||
RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
current_slot = 0;
pending_tags = 0x1;
} else if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
current_slot =
(port_cmd_status & AHCI_CMD_STATUS_CCS) >>
AHCI_CMD_STATUS_CCS_SHIFT;
pending_tags = ahci_portp->ahciport_pending_tags;
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
port_sactive = ddi_get32(
ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
current_tags = port_sactive &
~port_cmd_status &
AHCI_NCQ_SLOT_MASK(ahci_portp);
pending_tags = ahci_portp->ahciport_pending_ncq_tags;
}
timeout_tags = 0;
while (pending_tags) {
tmp_slot = ddi_ffs(pending_tags) - 1;
if (tmp_slot == -1) {
break;
}
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
spkt = ahci_portp->ahciport_err_retri_pkt;
else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp))
spkt = ahci_portp->ahciport_rdwr_pmult_pkt;
else
spkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
if ((spkt != NULL) && spkt->satapkt_time &&
!(spkt->satapkt_op_mode & SATA_OPMODE_POLLING)) {
ahci_portp->ahciport_slot_timeout[tmp_slot] -=
ahci_watchdog_timeout;
if (ahci_portp->ahciport_slot_timeout[tmp_slot]
> 0)
goto next;
#if AHCI_DEBUG
if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_TIMEOUT,
ahci_ctlp, "watchdog: the current "
"tags is 0x%x", current_tags);
} else {
AHCIDBG(AHCIDBG_ERRS|AHCIDBG_TIMEOUT,
ahci_ctlp, "watchdog: the current "
"slot is %d", current_slot);
}
#endif
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) &&
(tmp_slot != current_slot) ||
NCQ_CMD_IN_PROGRESS(ahci_portp) &&
((0x1 << tmp_slot) & current_tags)) {
ahci_portp->ahciport_slot_timeout \
[tmp_slot] = spkt->satapkt_time;
} else {
timeout_tags |= (0x1 << tmp_slot);
cmn_err(CE_WARN, "!ahci%d: watchdog "
"port %d satapkt 0x%p timed out\n",
instance, port, (void *)spkt);
}
}
next:
CLEAR_BIT(pending_tags, tmp_slot);
}
if (timeout_tags) {
mutex_exit(&ahci_portp->ahciport_mutex);
mutex_exit(&ahci_ctlp->ahcictl_mutex);
ahci_timeout_pkts(ahci_ctlp, ahci_portp,
port, timeout_tags);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
mutex_enter(&ahci_portp->ahciport_mutex);
}
mutex_exit(&ahci_portp->ahciport_mutex);
}
if (ahci_ctlp->ahcictl_timeout_id != 0) {
ahci_ctlp->ahcictl_timeout_id =
timeout((void (*)(void *))ahci_watchdog_handler,
(caddr_t)ahci_ctlp, ahci_watchdog_tick);
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
}
static void
ahci_copy_err_cnxt(sata_cmd_t *scmd, ahci_fis_d2h_register_t *rfisp)
{
scmd->satacmd_status_reg = GET_RFIS_STATUS(rfisp);
scmd->satacmd_error_reg = GET_RFIS_ERROR(rfisp);
scmd->satacmd_sec_count_lsb = GET_RFIS_SECTOR_COUNT(rfisp);
scmd->satacmd_lba_low_lsb = GET_RFIS_CYL_LOW(rfisp);
scmd->satacmd_lba_mid_lsb = GET_RFIS_CYL_MID(rfisp);
scmd->satacmd_lba_high_lsb = GET_RFIS_CYL_HI(rfisp);
scmd->satacmd_device_reg = GET_RFIS_DEV_HEAD(rfisp);
if (scmd->satacmd_addr_type == ATA_ADDR_LBA48) {
scmd->satacmd_sec_count_msb = GET_RFIS_SECTOR_COUNT_EXP(rfisp);
scmd->satacmd_lba_low_msb = GET_RFIS_CYL_LOW_EXP(rfisp);
scmd->satacmd_lba_mid_msb = GET_RFIS_CYL_MID_EXP(rfisp);
scmd->satacmd_lba_high_msb = GET_RFIS_CYL_HI_EXP(rfisp);
}
}
static void
ahci_copy_ncq_err_page(sata_cmd_t *scmd,
struct sata_ncq_error_recovery_page *ncq_err_page)
{
scmd->satacmd_sec_count_msb = ncq_err_page->ncq_sector_count_ext;
scmd->satacmd_sec_count_lsb = ncq_err_page->ncq_sector_count;
scmd->satacmd_lba_low_msb = ncq_err_page->ncq_sector_number_ext;
scmd->satacmd_lba_low_lsb = ncq_err_page->ncq_sector_number;
scmd->satacmd_lba_mid_msb = ncq_err_page->ncq_cyl_low_ext;
scmd->satacmd_lba_mid_lsb = ncq_err_page->ncq_cyl_low;
scmd->satacmd_lba_high_msb = ncq_err_page->ncq_cyl_high_ext;
scmd->satacmd_lba_high_lsb = ncq_err_page->ncq_cyl_high;
scmd->satacmd_device_reg = ncq_err_page->ncq_dev_head;
scmd->satacmd_status_reg = ncq_err_page->ncq_status;
scmd->satacmd_error_reg = ncq_err_page->ncq_error;
}
static void
ahci_copy_out_regs(sata_cmd_t *scmd, ahci_fis_d2h_register_t *rfisp)
{
if (scmd->satacmd_flags.sata_copy_out_sec_count_msb)
scmd->satacmd_sec_count_msb = GET_RFIS_SECTOR_COUNT_EXP(rfisp);
if (scmd->satacmd_flags.sata_copy_out_lba_low_msb)
scmd->satacmd_lba_low_msb = GET_RFIS_CYL_LOW_EXP(rfisp);
if (scmd->satacmd_flags.sata_copy_out_lba_mid_msb)
scmd->satacmd_lba_mid_msb = GET_RFIS_CYL_MID_EXP(rfisp);
if (scmd->satacmd_flags.sata_copy_out_lba_high_msb)
scmd->satacmd_lba_high_msb = GET_RFIS_CYL_HI_EXP(rfisp);
if (scmd->satacmd_flags.sata_copy_out_sec_count_lsb)
scmd->satacmd_sec_count_lsb = GET_RFIS_SECTOR_COUNT(rfisp);
if (scmd->satacmd_flags.sata_copy_out_lba_low_lsb)
scmd->satacmd_lba_low_lsb = GET_RFIS_CYL_LOW(rfisp);
if (scmd->satacmd_flags.sata_copy_out_lba_mid_lsb)
scmd->satacmd_lba_mid_lsb = GET_RFIS_CYL_MID(rfisp);
if (scmd->satacmd_flags.sata_copy_out_lba_high_lsb)
scmd->satacmd_lba_high_lsb = GET_RFIS_CYL_HI(rfisp);
if (scmd->satacmd_flags.sata_copy_out_device_reg)
scmd->satacmd_device_reg = GET_RFIS_DEV_HEAD(rfisp);
if (scmd->satacmd_flags.sata_copy_out_error_reg)
scmd->satacmd_error_reg = GET_RFIS_ERROR(rfisp);
}
static void
ahci_log_fatal_error_message(ahci_ctl_t *ahci_ctlp, uint8_t port,
uint32_t intr_status)
{
int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
if (intr_status & AHCI_INTR_STATUS_IFS)
cmn_err(CE_WARN, "!ahci%d: ahci port %d has interface fatal "
"error", instance, port);
if (intr_status & AHCI_INTR_STATUS_HBDS)
cmn_err(CE_WARN, "!ahci%d: ahci port %d has bus data error",
instance, port);
if (intr_status & AHCI_INTR_STATUS_HBFS)
cmn_err(CE_WARN, "!ahci%d: ahci port %d has bus fatal error",
instance, port);
if (intr_status & AHCI_INTR_STATUS_TFES)
cmn_err(CE_WARN, "!ahci%d: ahci port %d has task file error",
instance, port);
cmn_err(CE_WARN, "!ahci%d: ahci port %d is trying to do error "
"recovery", instance, port);
}
static void
ahci_dump_commands(ahci_ctl_t *ahci_ctlp, uint8_t port,
uint32_t slot_tags)
{
ahci_port_t *ahci_portp;
int tmp_slot;
sata_pkt_t *spkt;
sata_cmd_t cmd;
ahci_portp = ahci_ctlp->ahcictl_ports[port];
ASSERT(ahci_portp != NULL);
while (slot_tags) {
tmp_slot = ddi_ffs(slot_tags) - 1;
if (tmp_slot == -1) {
break;
}
spkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
if (spkt != NULL) {
cmd = spkt->satapkt_cmd;
cmn_err(CE_WARN, "!satapkt 0x%p: cmd_reg = 0x%x "
"features_reg = 0x%x sec_count_msb = 0x%x "
"lba_low_msb = 0x%x lba_mid_msb = 0x%x "
"lba_high_msb = 0x%x sec_count_lsb = 0x%x "
"lba_low_lsb = 0x%x lba_mid_lsb = 0x%x "
"lba_high_lsb = 0x%x device_reg = 0x%x "
"addr_type = 0x%x cmd_flags = 0x%x", (void *)spkt,
cmd.satacmd_cmd_reg, cmd.satacmd_features_reg,
cmd.satacmd_sec_count_msb, cmd.satacmd_lba_low_msb,
cmd.satacmd_lba_mid_msb, cmd.satacmd_lba_high_msb,
cmd.satacmd_sec_count_lsb, cmd.satacmd_lba_low_lsb,
cmd.satacmd_lba_mid_lsb, cmd.satacmd_lba_high_lsb,
cmd.satacmd_device_reg, cmd.satacmd_addr_type,
*((uint32_t *)&(cmd.satacmd_flags)));
}
CLEAR_BIT(slot_tags, tmp_slot);
}
}
static void
ahci_log_serror_message(ahci_ctl_t *ahci_ctlp, uint8_t port,
uint32_t port_serror, int debug_only)
{
static char err_buf[512];
static char err_msg_header[16];
char *err_msg = err_buf;
*err_buf = '\0';
*err_msg_header = '\0';
if (port_serror & SERROR_DATA_ERR_FIXED) {
err_msg = strcat(err_msg,
"\tRecovered Data Integrity Error (I)\n");
}
if (port_serror & SERROR_COMM_ERR_FIXED) {
err_msg = strcat(err_msg,
"\tRecovered Communication Error (M)\n");
}
if (port_serror & SERROR_DATA_ERR) {
err_msg = strcat(err_msg,
"\tTransient Data Integrity Error (T)\n");
}
if (port_serror & SERROR_PERSISTENT_ERR) {
err_msg = strcat(err_msg,
"\tPersistent Communication or Data Integrity Error (C)\n");
}
if (port_serror & SERROR_PROTOCOL_ERR) {
err_msg = strcat(err_msg, "\tProtocol Error (P)\n");
}
if (port_serror & SERROR_INT_ERR) {
err_msg = strcat(err_msg, "\tInternal Error (E)\n");
}
if (port_serror & SERROR_PHY_RDY_CHG) {
err_msg = strcat(err_msg, "\tPhyRdy Change (N)\n");
}
if (port_serror & SERROR_PHY_INT_ERR) {
err_msg = strcat(err_msg, "\tPhy Internal Error (I)\n");
}
if (port_serror & SERROR_COMM_WAKE) {
err_msg = strcat(err_msg, "\tComm Wake (W)\n");
}
if (port_serror & SERROR_10B_TO_8B_ERR) {
err_msg = strcat(err_msg, "\t10B to 8B Decode Error (B)\n");
}
if (port_serror & SERROR_DISPARITY_ERR) {
err_msg = strcat(err_msg, "\tDisparity Error (D)\n");
}
if (port_serror & SERROR_CRC_ERR) {
err_msg = strcat(err_msg, "\tCRC Error (C)\n");
}
if (port_serror & SERROR_HANDSHAKE_ERR) {
err_msg = strcat(err_msg, "\tHandshake Error (H)\n");
}
if (port_serror & SERROR_LINK_SEQ_ERR) {
err_msg = strcat(err_msg, "\tLink Sequence Error (S)\n");
}
if (port_serror & SERROR_TRANS_ERR) {
err_msg = strcat(err_msg,
"\tTransport state transition error (T)\n");
}
if (port_serror & SERROR_FIS_TYPE) {
err_msg = strcat(err_msg, "\tUnknown FIS Type (F)\n");
}
if (port_serror & SERROR_EXCHANGED_ERR) {
err_msg = strcat(err_msg, "\tExchanged (X)\n");
}
if (*err_msg == '\0')
return;
if (debug_only) {
(void) sprintf(err_msg_header, "port %d", port);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, err_msg_header, NULL);
AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, err_msg, NULL);
} else if (ahci_ctlp) {
cmn_err(CE_WARN, "!ahci%d: %s %s",
ddi_get_instance(ahci_ctlp->ahcictl_dip),
err_msg_header, err_msg);
sata_trace_debug(ahci_ctlp->ahcictl_dip,
"ahci%d: %s %s", ddi_get_instance(ahci_ctlp->ahcictl_dip),
err_msg_header, err_msg);
} else {
cmn_err(CE_WARN, "!ahci: %s %s", err_msg_header, err_msg);
sata_trace_debug(NULL, "ahci: %s %s", err_msg_header, err_msg);
}
}
static void
ahci_get_ahci_addr(ahci_ctl_t *ahci_ctlp, sata_device_t *sd,
ahci_addr_t *ahci_addrp)
{
sata_address_t *sata_addrp = &sd->satadev_addr;
ahci_addrp->aa_port =
ahci_ctlp->ahcictl_cport_to_port[sata_addrp->cport];
ahci_addrp->aa_pmport = sata_addrp->pmport;
switch (sata_addrp->qual) {
case SATA_ADDR_DCPORT:
case SATA_ADDR_CPORT:
ahci_addrp->aa_qual = AHCI_ADDR_PORT;
break;
case SATA_ADDR_PMULT:
case SATA_ADDR_PMULT_SPEC:
ahci_addrp->aa_qual = AHCI_ADDR_PMULT;
break;
case SATA_ADDR_DPMPORT:
case SATA_ADDR_PMPORT:
ahci_addrp->aa_qual = AHCI_ADDR_PMPORT;
break;
case SATA_ADDR_NULL:
default:
ahci_addrp->aa_qual = AHCI_ADDR_NULL;
break;
}
}
static int
ahci_get_num_implemented_ports(uint32_t ports_implemented)
{
uint8_t i;
int num = 0;
for (i = 0; i < AHCI_MAX_PORTS; i++) {
if (((uint32_t)0x1 << i) & ports_implemented)
num++;
}
return (num);
}
#if AHCI_DEBUG
static void
ahci_log(ahci_ctl_t *ahci_ctlp, uint_t level, char *fmt, ...)
{
static char name[16];
va_list ap;
mutex_enter(&ahci_log_mutex);
va_start(ap, fmt);
if (ahci_ctlp) {
(void) sprintf(name, "ahci%d: ",
ddi_get_instance(ahci_ctlp->ahcictl_dip));
} else {
(void) sprintf(name, "ahci: ");
}
(void) vsprintf(ahci_log_buf, fmt, ap);
va_end(ap);
cmn_err(level, "%s%s", name, ahci_log_buf);
mutex_exit(&ahci_log_mutex);
}
#endif
static int
ahci_quiesce(dev_info_t *dip)
{
ahci_ctl_t *ahci_ctlp;
ahci_port_t *ahci_portp;
int instance, port;
instance = ddi_get_instance(dip);
ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
if (ahci_ctlp == NULL)
return (DDI_FAILURE);
#if AHCI_DEBUG
ahci_debug_flags = 0;
#endif
ahci_ctlp->ahcictl_flags |= AHCI_QUIESCE;
ahci_disable_all_intrs(ahci_ctlp);
for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
continue;
}
ahci_portp = ahci_ctlp->ahcictl_ports[port];
ahci_disable_port_intrs(ahci_ctlp, port);
(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
ahci_portp, port);
}
ahci_ctlp->ahcictl_flags &= ~AHCI_QUIESCE;
return (DDI_SUCCESS);
}
static void
ahci_add_doneq(ahci_port_t *ahci_portp, sata_pkt_t *satapkt, int reason)
{
ASSERT(satapkt != NULL);
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
satapkt->satapkt_reason = reason;
satapkt->satapkt_hba_driver_private = NULL;
if (! (satapkt->satapkt_op_mode & SATA_OPMODE_SYNCH) &&
satapkt->satapkt_comp) {
*ahci_portp->ahciport_doneqtail = satapkt;
ahci_portp->ahciport_doneqtail =
(sata_pkt_t **)&(satapkt->satapkt_hba_driver_private);
ahci_portp->ahciport_doneq_len++;
} else if ((satapkt->satapkt_op_mode & SATA_OPMODE_SYNCH) &&
! (satapkt->satapkt_op_mode & SATA_OPMODE_POLLING))
cv_broadcast(&ahci_portp->ahciport_cv);
}
static void
ahci_flush_doneq(ahci_port_t *ahci_portp)
{
sata_pkt_t *satapkt, *next;
ASSERT(MUTEX_HELD(&ahci_portp->ahciport_mutex));
if (ahci_portp->ahciport_doneq) {
satapkt = ahci_portp->ahciport_doneq;
ahci_portp->ahciport_doneq = NULL;
ahci_portp->ahciport_doneqtail = &ahci_portp->ahciport_doneq;
ahci_portp->ahciport_doneq_len = 0;
mutex_exit(&ahci_portp->ahciport_mutex);
while (satapkt != NULL) {
next = satapkt->satapkt_hba_driver_private;
satapkt->satapkt_hba_driver_private = NULL;
(*satapkt->satapkt_comp)(satapkt);
satapkt = next;
}
mutex_enter(&ahci_portp->ahciport_mutex);
}
}
static boolean_t
ahci_em_set_led(ahci_ctl_t *ahci_ctlp, uint8_t port, ahci_em_led_state_t desire)
{
ahci_em_led_msg_t msg;
ahci_em_msg_hdr_t hdr;
uint32_t msgval, hdrval;
uint_t i, max_delay = ahci_em_tx_delay_count;
msg.alm_hba = port;
msg.alm_pminfo = 0;
msg.alm_value = 0;
if (desire & AHCI_EM_LED_IDENT_ENABLE) {
msg.alm_value |= AHCI_LED_ON << AHCI_LED_IDENT_OFF;
}
if (desire & AHCI_EM_LED_FAULT_ENABLE) {
msg.alm_value |= AHCI_LED_ON << AHCI_LED_FAULT_OFF;
}
if ((ahci_ctlp->ahcictl_em_ctl & AHCI_HBA_EM_CTL_ATTR_ALHD) == 0 &&
(desire & AHCI_EM_LED_ACTIVITY_DISABLE) == 0) {
msg.alm_value |= AHCI_LED_ON << AHCI_LED_ACTIVITY_OFF;
}
hdr.aemh_rsvd = 0;
hdr.aemh_mlen = sizeof (ahci_em_led_msg_t);
hdr.aemh_dlen = 0;
hdr.aemh_mtype = AHCI_EM_MSG_TYPE_LED;
bcopy(&msg, &msgval, sizeof (msgval));
bcopy(&hdr, &hdrval, sizeof (hdrval));
for (i = 0; i < max_delay; i++) {
uint32_t val;
val = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_EM_CTL(ahci_ctlp));
if ((val & AHCI_HBA_EM_CTL_CTL_TM) == 0)
break;
delay(drv_usectohz(ahci_em_tx_delay_ms * 1000));
}
if (i == max_delay)
return (B_FALSE);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)ahci_ctlp->ahcictl_em_tx_off, hdrval);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)(ahci_ctlp->ahcictl_em_tx_off + 4), msgval);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_EM_CTL(ahci_ctlp), AHCI_HBA_EM_CTL_CTL_TM);
for (i = 0; i < max_delay; i++) {
uint32_t val;
val = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_EM_CTL(ahci_ctlp));
if ((val & AHCI_HBA_EM_CTL_CTL_TM) == 0)
break;
delay(drv_usectohz(ahci_em_tx_delay_ms * 1000));
}
if (i == max_delay)
return (B_FALSE);
return (B_TRUE);
}
typedef struct ahci_em_led_task_arg {
ahci_ctl_t *aelta_ctl;
uint8_t aelta_port;
uint_t aelta_op;
ahci_em_led_state_t aelta_state;
uint_t aelta_ret;
kcondvar_t aelta_cv;
uint_t aelta_ref;
} ahci_em_led_task_arg_t;
static void
ahci_em_led_task_free(ahci_em_led_task_arg_t *task)
{
ASSERT3U(task->aelta_ref, ==, 0);
cv_destroy(&task->aelta_cv);
kmem_free(task, sizeof (*task));
}
static void
ahci_em_led_task(void *arg)
{
boolean_t ret, cleanup = B_FALSE;
ahci_em_led_task_arg_t *led = arg;
ahci_em_led_state_t state;
mutex_enter(&led->aelta_ctl->ahcictl_mutex);
if (led->aelta_ctl->ahcictl_em_flags != AHCI_EM_USABLE) {
led->aelta_ret = EIO;
mutex_exit(&led->aelta_ctl->ahcictl_mutex);
return;
}
state = led->aelta_ctl->ahcictl_em_state[led->aelta_port];
mutex_exit(&led->aelta_ctl->ahcictl_mutex);
switch (led->aelta_op) {
case AHCI_EM_IOC_SET_OP_ADD:
state |= led->aelta_state;
break;
case AHCI_EM_IOC_SET_OP_REM:
state &= ~led->aelta_state;
break;
case AHCI_EM_IOC_SET_OP_SET:
state = led->aelta_state;
break;
default:
led->aelta_ret = ENOTSUP;
return;
}
ret = ahci_em_set_led(led->aelta_ctl, led->aelta_port, state);
mutex_enter(&led->aelta_ctl->ahcictl_mutex);
if (ret) {
led->aelta_ctl->ahcictl_em_state[led->aelta_port] = state;
led->aelta_ret = 0;
} else {
led->aelta_ret = EIO;
led->aelta_ctl->ahcictl_em_flags |= AHCI_EM_TIMEOUT;
}
led->aelta_ref--;
if (led->aelta_ref > 0) {
cv_signal(&led->aelta_cv);
} else {
cleanup = B_TRUE;
}
mutex_exit(&led->aelta_ctl->ahcictl_mutex);
if (cleanup) {
ahci_em_led_task_free(led);
}
}
static void
ahci_em_reset(void *arg)
{
uint_t i, max_delay = ahci_em_reset_delay_count;
ahci_ctl_t *ahci_ctlp = arg;
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ASSERT(ahci_ctlp->ahcictl_em_flags & AHCI_EM_RESETTING);
if (ahci_ctlp->ahcictl_em_flags & AHCI_EM_QUIESCE) {
ahci_ctlp->ahcictl_em_flags &= ~AHCI_EM_RESETTING;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return;
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_EM_CTL(ahci_ctlp), AHCI_HBA_EM_CTL_CTL_RST);
for (i = 0; i < max_delay; i++) {
uint32_t val;
val = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
(uint32_t *)AHCI_GLOBAL_EM_CTL(ahci_ctlp));
if ((val & AHCI_HBA_EM_CTL_CTL_RST) == 0)
break;
delay(drv_usectohz(ahci_em_reset_delay_ms * 1000));
}
if (i == max_delay) {
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_ctlp->ahcictl_em_flags &= ~AHCI_EM_RESETTING;
ahci_ctlp->ahcictl_em_flags |= AHCI_EM_TIMEOUT;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
cmn_err(CE_WARN, "!ahci%d: enclosure timed out resetting",
ddi_get_instance(ahci_ctlp->ahcictl_dip));
return;
}
for (i = 0; i < ahci_ctlp->ahcictl_num_ports; i++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, i))
continue;
if (!ahci_em_set_led(ahci_ctlp, i,
ahci_ctlp->ahcictl_em_state[i])) {
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_ctlp->ahcictl_em_flags &= ~AHCI_EM_RESETTING;
ahci_ctlp->ahcictl_em_flags |= AHCI_EM_TIMEOUT;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
cmn_err(CE_WARN, "!ahci%d: enclosure timed out "
"setting port %u",
ddi_get_instance(ahci_ctlp->ahcictl_dip), i);
return;
}
}
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_ctlp->ahcictl_em_flags &= ~AHCI_EM_RESETTING;
ahci_ctlp->ahcictl_em_flags |= AHCI_EM_READY;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
}
static boolean_t
ahci_em_init(ahci_ctl_t *ahci_ctlp)
{
char name[128];
if (ahci_ctlp->ahcictl_em_loc == 0 ||
(ahci_ctlp->ahcictl_em_ctl & AHCI_HBA_EM_CTL_SUPP_LED) == 0)
return (B_TRUE);
if ((ahci_ctlp->ahcictl_em_loc & AHCI_HBA_EM_LOC_SZ_MASK) <
AHCI_EM_BUFFER_MIN) {
return (B_TRUE);
}
ahci_ctlp->ahcictl_em_tx_off = ((ahci_ctlp->ahcictl_em_loc &
AHCI_HBA_EM_LOC_OFST_MASK) >> AHCI_HBA_EM_LOC_OFST_SHIFT) * 4;
ahci_ctlp->ahcictl_em_tx_off += ahci_ctlp->ahcictl_ahci_addr;
bzero(ahci_ctlp->ahcictl_em_state,
sizeof (ahci_ctlp->ahcictl_em_state));
(void) snprintf(name, sizeof (name), "ahcti_em_taskq%d",
ddi_get_instance(ahci_ctlp->ahcictl_dip));
if ((ahci_ctlp->ahcictl_em_taskq =
ddi_taskq_create(ahci_ctlp->ahcictl_dip, name, 1,
TASKQ_DEFAULTPRI, 0)) == NULL) {
cmn_err(CE_WARN, "!ahci%d: ddi_tasq_create failed for em "
"services", ddi_get_instance(ahci_ctlp->ahcictl_dip));
return (B_FALSE);
}
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_ctlp->ahcictl_em_flags |= AHCI_EM_PRESENT | AHCI_EM_RESETTING;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
(void) ddi_taskq_dispatch(ahci_ctlp->ahcictl_em_taskq, ahci_em_reset,
ahci_ctlp, DDI_SLEEP);
return (B_TRUE);
}
static int
ahci_em_ioctl_get(ahci_ctl_t *ahci_ctlp, intptr_t arg)
{
int i;
ahci_ioc_em_get_t get;
if ((ahci_ctlp->ahcictl_em_flags & AHCI_EM_PRESENT) == 0) {
return (ENOTSUP);
}
bzero(&get, sizeof (get));
get.aiemg_nports = ahci_ctlp->ahcictl_ports_implemented;
if ((ahci_ctlp->ahcictl_em_ctl & AHCI_HBA_EM_CTL_ATTR_ALHD) == 0) {
get.aiemg_flags |= AHCI_EM_FLAG_CONTROL_ACTIVITY;
}
mutex_enter(&ahci_ctlp->ahcictl_mutex);
for (i = 0; i < ahci_ctlp->ahcictl_num_ports; i++) {
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, i)) {
continue;
}
get.aiemg_status[i] = ahci_ctlp->ahcictl_em_state[i];
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
if (ddi_copyout(&get, (void *)arg, sizeof (get), 0) != 0)
return (EFAULT);
return (0);
}
static int
ahci_em_ioctl_set(ahci_ctl_t *ahci_ctlp, intptr_t arg)
{
int ret;
ahci_ioc_em_set_t set;
ahci_em_led_task_arg_t *task;
boolean_t signal, cleanup;
if (ddi_copyin((void *)arg, &set, sizeof (set), 0) != 0)
return (EFAULT);
if (set.aiems_port > ahci_ctlp->ahcictl_num_ports)
return (EINVAL);
if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, set.aiems_port)) {
return (EINVAL);
}
if ((set.aiems_leds & ~(AHCI_EM_LED_IDENT_ENABLE |
AHCI_EM_LED_FAULT_ENABLE |
AHCI_EM_LED_ACTIVITY_DISABLE)) != 0) {
return (EINVAL);
}
switch (set.aiems_op) {
case AHCI_EM_IOC_SET_OP_ADD:
case AHCI_EM_IOC_SET_OP_REM:
case AHCI_EM_IOC_SET_OP_SET:
break;
default:
return (EINVAL);
}
if ((ahci_ctlp->ahcictl_em_flags & AHCI_EM_PRESENT) == 0) {
return (ENOTSUP);
}
if ((set.aiems_leds & AHCI_EM_LED_ACTIVITY_DISABLE) != 0 &&
((ahci_ctlp->ahcictl_em_ctl & AHCI_HBA_EM_CTL_ATTR_ALHD) != 0)) {
return (ENOTSUP);
}
task = kmem_alloc(sizeof (*task), KM_NOSLEEP_LAZY);
if (task == NULL) {
return (ENOMEM);
}
task->aelta_ctl = ahci_ctlp;
task->aelta_port = (uint8_t)set.aiems_port;
task->aelta_op = set.aiems_op;
task->aelta_state = set.aiems_leds;
cv_init(&task->aelta_cv, NULL, CV_DRIVER, NULL);
task->aelta_ref = 2;
(void) ddi_taskq_dispatch(ahci_ctlp->ahcictl_em_taskq,
ahci_em_led_task, task, DDI_SLEEP);
signal = B_FALSE;
mutex_enter(&ahci_ctlp->ahcictl_mutex);
while (task->aelta_ref > 1) {
if (cv_wait_sig(&task->aelta_cv, &ahci_ctlp->ahcictl_mutex) ==
0) {
signal = B_TRUE;
break;
}
}
task->aelta_ref--;
cleanup = (task->aelta_ref == 0);
if (signal) {
ret = EINTR;
} else {
ret = task->aelta_ret;
}
mutex_exit(&ahci_ctlp->ahcictl_mutex);
if (cleanup) {
ahci_em_led_task_free(task);
}
return (ret);
}
static int
ahci_em_ioctl(dev_info_t *dip, int cmd, intptr_t arg)
{
int inst;
ahci_ctl_t *ahci_ctlp;
inst = ddi_get_instance(dip);
if ((ahci_ctlp = ddi_get_soft_state(ahci_statep, inst)) == NULL) {
return (ENXIO);
}
switch (cmd) {
case AHCI_EM_IOC_GET:
return (ahci_em_ioctl_get(ahci_ctlp, arg));
case AHCI_EM_IOC_SET:
return (ahci_em_ioctl_set(ahci_ctlp, arg));
default:
return (ENOTTY);
}
}
static void
ahci_em_quiesce(ahci_ctl_t *ahci_ctlp)
{
mutex_enter(&ahci_ctlp->ahcictl_mutex);
if ((ahci_ctlp->ahcictl_em_flags & AHCI_EM_PRESENT) == 0) {
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return;
}
ahci_ctlp->ahcictl_em_flags |= AHCI_EM_QUIESCE;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
ddi_taskq_wait(ahci_ctlp->ahcictl_em_taskq);
}
static void
ahci_em_suspend(ahci_ctl_t *ahci_ctlp)
{
ahci_em_quiesce(ahci_ctlp);
mutex_enter(&ahci_ctlp->ahcictl_mutex);
ahci_ctlp->ahcictl_em_flags &= ~AHCI_EM_READY;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
}
static void
ahci_em_resume(ahci_ctl_t *ahci_ctlp)
{
mutex_enter(&ahci_ctlp->ahcictl_mutex);
if ((ahci_ctlp->ahcictl_em_flags & AHCI_EM_PRESENT) == 0) {
mutex_exit(&ahci_ctlp->ahcictl_mutex);
return;
}
ahci_ctlp->ahcictl_em_flags |= AHCI_EM_RESETTING;
mutex_exit(&ahci_ctlp->ahcictl_mutex);
(void) ddi_taskq_dispatch(ahci_ctlp->ahcictl_em_taskq, ahci_em_reset,
ahci_ctlp, DDI_SLEEP);
}
static void
ahci_em_fini(ahci_ctl_t *ahci_ctlp)
{
if ((ahci_ctlp->ahcictl_em_flags & AHCI_EM_PRESENT) == 0) {
return;
}
ahci_em_quiesce(ahci_ctlp);
ddi_taskq_destroy(ahci_ctlp->ahcictl_em_taskq);
ahci_ctlp->ahcictl_em_taskq = NULL;
}