ahci.c revision 2ac302890e472bf0c11db192dd18f12ded6043f6
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
/*
* AHCI (Advanced Host Controller Interface) SATA HBA Driver
*
* Power Management Support
* ------------------------
*
* support Suspend to RAM on X86 feature. Device power management isn't
* implemented, link power management is disabled, and hot plug isn't
* allowed during the period from suspend to resume.
*
* For s/r support, the ahci driver only need to implement DDI_SUSPEND
* and DDI_RESUME entries, and don't need to take care of new requests
* sent down after suspend because the target driver (sd) has already
* handled these conditions, and blocked these requests. For the detailed
* information, please check with sdopen, sdclose and sdioctl routines.
*
*/
/*
* FMA header files
*/
/*
* This is the string displayed by modinfo, etc.
* Make sure you keep the version ID up to date!
*/
static char ahci_ident[] = "ahci driver";
/*
* Function prototypes for driver entry points
*/
static int ahci_quiesce(dev_info_t *);
/*
* Function prototypes for SATA Framework interfaces
*/
static int ahci_unregister_sata_hba_tran(ahci_ctl_t *);
#if defined(__lock_lint)
#endif
/*
* FMA Prototypes
*/
static void ahci_fm_init(ahci_ctl_t *);
static void ahci_fm_fini(ahci_ctl_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);
/*
* Local function prototypes
*/
static int ahci_alloc_ports_state(ahci_ctl_t *);
static void ahci_dealloc_ports_state(ahci_ctl_t *);
static void ahci_dealloc_rcvd_fis(ahci_port_t *);
static int ahci_initialize_controller(ahci_ctl_t *);
static void ahci_uninitialize_controller(ahci_ctl_t *);
static int ahci_config_space_init(ahci_ctl_t *);
static void ahci_drain_ports_taskq(ahci_ctl_t *);
uint8_t);
sata_device_t *);
sata_device_t *);
sata_device_t *);
ahci_addr_t *);
ahci_addr_t *, sata_pkt_t *);
ahci_addr_t *, sata_pkt_t *);
ahci_addr_t *, int);
static void ahci_copy_ncq_err_page(sata_cmd_t *,
struct sata_ncq_error_recovery_page *);
static void ahci_flush_doneq(ahci_port_t *);
static int ahci_hba_reset(ahci_ctl_t *);
ahci_addr_t *);
ahci_addr_t *);
ahci_addr_t *);
static int ahci_reset_hba_reject_pkts(ahci_ctl_t *);
uint8_t);
uint8_t, int, int *);
uint8_t, sata_pkt_t *);
ahci_addr_t *, uint32_t);
static void ahci_events_handler(void *);
static void ahci_watchdog_handler(ahci_ctl_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 int ahci_intr_device_mechanical_presence_status(ahci_ctl_t *,
ahci_port_t *, uint8_t);
static int ahci_get_num_implemented_ports(uint32_t);
#if AHCI_DEBUG
#endif
/*
* DMA attributes for the data buffer
*
* dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
* does not support 64-bit addressing
*/
static ddi_dma_attr_t buffer_dma_attr = {
DMA_ATTR_V0, /* dma_attr_version */
0x0ull, /* dma_attr_addr_lo: lowest bus address */
0xffffffffffffffffull, /* dma_attr_addr_hi: highest bus address */
0x3fffffull, /* dma_attr_count_max i.e. for one cookie */
0x2ull, /* dma_attr_align: word aligned */
1, /* dma_attr_burstsizes */
1, /* dma_attr_minxfer */
0xffffffffull, /* dma_attr_maxxfer i.e. includes all cookies */
0xffffffffull, /* dma_attr_seg */
AHCI_PRDT_NUMBER, /* dma_attr_sgllen */
512, /* dma_attr_granular */
0, /* dma_attr_flags */
};
/*
* DMA attributes for the rcvd FIS
*
* dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
* does not support 64-bit addressing
*/
static ddi_dma_attr_t rcvd_fis_dma_attr = {
DMA_ATTR_V0, /* dma_attr_version */
0x0ull, /* dma_attr_addr_lo: lowest bus address */
0xffffffffffffffffull, /* dma_attr_addr_hi: highest bus address */
0xffffffffull, /* dma_attr_count_max i.e. for one cookie */
0x100ull, /* dma_attr_align: 256-byte aligned */
1, /* dma_attr_burstsizes */
1, /* dma_attr_minxfer */
0xffffffffull, /* dma_attr_maxxfer i.e. includes all cookies */
0xffffffffull, /* dma_attr_seg */
1, /* dma_attr_sgllen */
1, /* dma_attr_granular */
0, /* dma_attr_flags */
};
/*
* DMA attributes for the command list
*
* dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
* does not support 64-bit addressing
*/
static ddi_dma_attr_t cmd_list_dma_attr = {
DMA_ATTR_V0, /* dma_attr_version */
0x0ull, /* dma_attr_addr_lo: lowest bus address */
0xffffffffffffffffull, /* dma_attr_addr_hi: highest bus address */
0xffffffffull, /* dma_attr_count_max i.e. for one cookie */
0x400ull, /* dma_attr_align: 1K-byte aligned */
1, /* dma_attr_burstsizes */
1, /* dma_attr_minxfer */
0xffffffffull, /* dma_attr_maxxfer i.e. includes all cookies */
0xffffffffull, /* dma_attr_seg */
1, /* dma_attr_sgllen */
1, /* dma_attr_granular */
0, /* dma_attr_flags */
};
/*
* DMA attributes for cmd tables
*
* dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
* does not support 64-bit addressing
*/
static ddi_dma_attr_t cmd_table_dma_attr = {
DMA_ATTR_V0, /* dma_attr_version */
0x0ull, /* dma_attr_addr_lo: lowest bus address */
0xffffffffffffffffull, /* dma_attr_addr_hi: highest bus address */
0xffffffffull, /* dma_attr_count_max i.e. for one cookie */
0x80ull, /* dma_attr_align: 128-byte aligned */
1, /* dma_attr_burstsizes */
1, /* dma_attr_minxfer */
0xffffffffull, /* dma_attr_maxxfer i.e. includes all cookies */
0xffffffffull, /* dma_attr_seg */
1, /* dma_attr_sgllen */
1, /* dma_attr_granular */
0, /* dma_attr_flags */
};
/* Device access attributes */
static ddi_device_acc_attr_t accattr = {
};
static struct dev_ops ahcictl_dev_ops = {
DEVO_REV, /* devo_rev */
0, /* refcnt */
ahci_getinfo, /* info */
nulldev, /* identify */
nulldev, /* probe */
ahci_attach, /* attach */
ahci_detach, /* detach */
nodev, /* no reset */
(struct cb_ops *)0, /* driver operations */
NULL, /* bus operations */
NULL, /* power */
ahci_quiesce, /* quiesce */
};
static sata_tran_hotplug_ops_t ahci_tran_hotplug_ops = {
};
extern struct mod_ops mod_driverops;
&mod_driverops, /* driverops */
ahci_ident, /* short description */
&ahcictl_dev_ops, /* driver ops */
};
static struct modlinkage modlinkage = {
&modldrv,
};
/* The following variables are watchdog handler related */
static clock_t ahci_watchdog_tick;
/*
* This static variable indicates the size of command table,
* and it's changeable with prdt number, which ahci_dma_prdt_number
* indicates.
*/
static size_t ahci_cmd_table_size;
/*
*
* ahci_dma_prdt_number
* ahci_msi_enabled
* ahci_buf_64bit_dma
* ahci_commu_64bit_dma
*/
/* The number of Physical Region Descriptor Table(PRDT) in Command Table */
/* AHCI MSI is tunable */
/*
* 64-bit dma addressing for data buffer is tunable
*
* The variable controls only the below value:
* DBAU (upper 32-bits physical address of data block)
*/
/*
* 64-bit dma addressing for communication system descriptors is tunable
*
* The variable controls the below three values:
*
* PxCLBU (upper 32-bits for the command list base physical address)
* PxFBU (upper 32-bits for the received FIS base physical address)
* CTBAU (upper 32-bits of command table base)
*/
/*
* chipset. If the users want to have a try with 64-bit dma, please change
* the below variable value to enable it.
*/
/*
* SB600/700/710/750/800. If the users want to have a try with 64-bit dma,
* please change the below value to enable it.
*/
/*
* End of global tunable variable definition
*/
#if AHCI_DEBUG
uint32_t ahci_debug_flags = 0;
#else
#endif
#if AHCI_DEBUG
/* The following is needed for ahci_log() */
static kmutex_t ahci_log_mutex;
static char ahci_log_buf[512];
#endif
/* Opaque state pointer initialized by ddi_soft_state_init() */
static void *ahci_statep = NULL;
/*
* ahci module initialization.
*/
int
_init(void)
{
int ret;
if (ret != 0) {
goto err_out;
}
#if AHCI_DEBUG
#endif
#if AHCI_DEBUG
#endif
goto err_out;
}
/* watchdog tick */
if (ret != 0) {
#if AHCI_DEBUG
#endif
goto err_out;
}
return (ret);
return (ret);
}
/*
* ahci module uninitialize.
*/
int
_fini(void)
{
int ret;
if (ret != 0) {
return (ret);
}
/* Remove the resources allocated in _init(). */
#if AHCI_DEBUG
#endif
return (ret);
}
/*
* _info entry point
*/
int
{
}
/*
* The attach entry point for dev_ops.
*/
static int
{
int status;
int attach_state;
int intr_types;
int i;
int regs_length;
int rnumber;
#if AHCI_DEBUG
int speed;
#endif
NULL);
switch (cmd) {
case DDI_ATTACH:
break;
case DDI_RESUME:
/*
* During DDI_RESUME, the hardware state of the device
* (power may have been removed from the device) must be
* restored, allow pending requests to continue, and
* service new requests.
*/
/*
* GHC.AE must be set to 1 before any other AHCI register
* is accessed
*/
/* Restart watch thread */
if (ahci_ctlp->ahcictl_timeout_id == 0)
(void (*)(void *))ahci_watchdog_handler,
/*
* Re-initialize the controller and enable the interrupts and
* restart all the ports.
*
* Note that so far we don't support hot-plug during
*/
"Failed to initialize the controller "
"during DDI_RESUME", NULL);
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
/* Allocate soft state */
if (status != DDI_SUCCESS) {
instance);
goto err_out;
}
for (i = 0; i < AHCI_MAX_PORTS; i++) {
}
/* Initialize FMA properties */
/*
* Now map the AHCI base address; which includes global
* registers and port control registers
*
* According to the spec, the AHCI Base Address is BAR5,
* but BAR0-BAR4 are optional, so we need to check which
* rnumber is used for BAR5.
*/
/*
* search through DDI "reg" property for the AHCI register set
*/
instance);
goto err_out;
}
/* AHCI Base Address is located at 0x24 offset */
== AHCI_PCI_RNUM)
break;
}
if (rnumber == regs_length) {
instance);
goto err_out;
}
0,
0,
&accattr,
if (status != DDI_SUCCESS) {
instance);
goto err_out;
}
/*
* GHC.AE must be set to 1 before any other AHCI register
* is accessed
*/
/* Get the AHCI version information */
(ahci_version & 0x000000ff)));
/* We don't support controllers whose versions are lower than 1.0 */
if (!(ahci_version & 0xffff0000)) {
"than version 1.0", instance);
goto err_out;
}
/* Get the HBA capabilities information */
/* CAP2 (HBA Capabilities Extended) is available since AHCI spec 1.2 */
if (ahci_version >= 0x00010200) {
/* Get the HBA capabilities extended information */
"hba capabilities extended = 0x%x", cap2_status);
}
#if AHCI_DEBUG
/* Get the interface speed supported by the HBA */
if (speed == 0x01) {
"hba interface speed support: Gen 1 (1.5Gbps)", NULL);
} else if (speed == 0x10) {
"hba interface speed support: Gen 2 (3 Gbps)", NULL);
} else if (speed == 0x11) {
"hba interface speed support: Gen 3 (6 Gbps)", NULL);
}
#endif
/* Get the number of command slots supported by the HBA */
((cap_status & AHCI_HBA_CAP_NCS) >>
AHCI_HBA_CAP_NCS_SHIFT) + 1;
/* Get the bit map which indicates ports implemented by the HBA */
/* Max port number implemented */
/* Get the number of implemented ports by the HBA */
"hba number of implemented ports: %d",
/* Check whether HBA supports 64bit DMA addressing */
if (!(cap_status & AHCI_HBA_CAP_S64A)) {
"hba does not support 64-bit addressing", NULL);
}
/* Checking for the support of Port Multiplier */
if (cap_status & AHCI_HBA_CAP_SPM) {
"hba supports port multiplier (CBSS)", NULL);
/* Support FIS-based switching ? */
if (cap_status & AHCI_HBA_CAP_FBSS) {
"hba supports FIS-based switching (FBSS)", NULL);
}
}
/* Checking for Support Command List Override */
if (cap_status & AHCI_HBA_CAP_SCLO) {
"hba supports command list override.", NULL);
}
/* Checking for Asynchronous Notification */
if (cap_status & AHCI_HBA_CAP_SSNTF) {
"hba supports asynchronous notification.", NULL);
}
!= DDI_SUCCESS) {
instance);
goto err_out;
}
/*
* Check the pci configuration space, and set caps. We also
* handle the hardware defect in this function.
*
* For example, force ATI SB600 to use 32-bit dma addressing
* since it doesn't support 64-bit dma though its CAP register
* declares it support.
*/
instance);
goto err_out;
}
/*
* Disable the whole controller interrupts before adding
* interrupt handlers(s).
*/
/* Get supported interrupt types */
instance);
goto err_out;
}
"ddi_intr_get_supported_types() returned: 0x%x",
/*
* Try MSI first, but fall back to FIXED if failed
*/
DDI_SUCCESS) {
"Using MSI interrupt type", NULL);
goto intr_done;
}
"MSI registration failed, "
"trying FIXED interrupts", NULL);
}
if (intr_types & DDI_INTR_TYPE_FIXED) {
DDI_SUCCESS) {
"Using FIXED interrupt type", NULL);
goto intr_done;
}
"FIXED interrupt registration failed", NULL);
}
goto err_out;
/* Initialize the controller mutex */
if (ahci_dma_prdt_number < AHCI_MIN_PRDT_NUMBER) {
} else if (ahci_dma_prdt_number > AHCI_MAX_PRDT_NUMBER) {
}
ahci_cmd_table_size = (sizeof (ahci_cmd_table_t) +
sizeof (ahci_prdt_item_t));
"ahci_attach: ahci_dma_prdt_number set by user is 0x%x,"
" ahci_cmd_table_size is 0x%x",
if (ahci_dma_prdt_number != AHCI_PRDT_NUMBER)
/*
* enable 64bit dma for data buffer for SB600 if
* sb600_buf_64bit_dma_disable is B_FALSE
*/
if ((ahci_buf_64bit_dma == B_FALSE) ||
!(sb600_buf_64bit_dma_disable == B_FALSE &&
0xffffffffull;
}
/*
* enable 64bit dma for command buffer for SB600/700/710/800
* if sbxxx_commu_64bit_dma_disable is B_FALSE
*/
if ((ahci_commu_64bit_dma == B_FALSE) ||
!(sbxxx_commu_64bit_dma_disable == B_FALSE &&
0xffffffffull;
0xffffffffull;
0xffffffffull;
}
/* Allocate the ports structure */
if (status != AHCI_SUCCESS) {
instance);
goto err_out;
}
/*
* Initialize the controller and ports.
*/
if (status != AHCI_SUCCESS) {
instance);
goto err_out;
}
/* Start one thread to check packet timeouts */
(void (*)(void *))ahci_watchdog_handler,
instance);
goto err_out;
}
/* Check all handles at the end of the attach operation. */
instance);
goto err_out;
}
return (DDI_SUCCESS);
/* FMA message */
ahci_ctlp->ahcictl_timeout_id = 0;
}
if (attach_state & AHCI_ATTACH_STATE_HW_INIT) {
}
if (attach_state & AHCI_ATTACH_STATE_PORT_ALLOC) {
}
if (attach_state & AHCI_ATTACH_STATE_MUTEX_INIT) {
}
if (attach_state & AHCI_ATTACH_STATE_INTR_ADDED) {
}
}
if (attach_state & AHCI_ATTACH_STATE_REG_MAP) {
}
if (attach_state & AHCI_ATTACH_STATE_FMA) {
}
}
return (DDI_FAILURE);
}
/*
* The detach entry point for dev_ops.
*/
static int
{
int instance;
int ret;
switch (cmd) {
case DDI_DETACH:
/* disable the interrupts for an uninterrupted detach */
/* unregister from the sata framework. */
if (ret != AHCI_SUCCESS) {
return (DDI_FAILURE);
}
/* stop the watchdog handler */
ahci_ctlp->ahcictl_timeout_id = 0;
/* uninitialize the controller */
/* remove the interrupts */
/* deallocate the ports structures */
/* destroy mutex */
/* teardown the pci config */
/* remove the reg maps. */
/* release fma resource */
/* free the soft state. */
return (DDI_SUCCESS);
case DDI_SUSPEND:
/*
* The steps associated with suspension must include putting
* the underlying device into a quiescent state so that it
* will not generate interrupts or modify or access memory.
*/
return (DDI_SUCCESS);
}
/* stop the watchdog handler */
if (ahci_ctlp->ahcictl_timeout_id) {
ahci_ctlp->ahcictl_timeout_id = 0;
}
/*
* drain the taskq
*/
/*
* Disable the interrupts and stop all the ports.
*/
return (DDI_SUCCESS);
default:
return (DDI_FAILURE);
}
}
/*
* The info entry point for dev_ops.
*
*/
static int
{
#ifndef __lock_lint
#endif /* __lock_lint */
int instance;
switch (infocmd) {
case DDI_INFO_DEVT2DEVINFO:
return (DDI_SUCCESS);
} else {
return (DDI_FAILURE);
}
case DDI_INFO_DEVT2INSTANCE:
break;
default:
break;
}
return (DDI_SUCCESS);
}
/*
* Registers the ahci with sata framework.
*/
static int
{
struct sata_hba_tran *sata_hba_tran;
"ahci_register_sata_hba_tran enter", NULL);
/* Allocate memory for the sata_hba_tran */
/* Report the number of implemented ports */
/* Support ATAPI device */
/* Get the data transfer capability for PIO command by the HBA */
if (cap_status & AHCI_HBA_CAP_PMD) {
"DRQ block data transfer for PIO command protocol", NULL);
}
/*
* is not supported by AHCI (including the READ QUEUED (EXT), WRITE
* QUEUED (EXT), and SERVICE commands). Queued operations are
* supported in AHCI using the READ FPDMA QUEUED and WRITE FPDMA
* QUEUED commands when the HBA and device support native command
* queuing(NCQ).
*
* SATA_CTLF_NCQ will be set to sata_tran_hba_features_support if the
* CAP register of the HBA indicates NCQ is supported.
*
* SATA_CTLF_NCQ cannot be set if AHCI_CAP_NO_MCMDLIST_NONQUEUE is
* set because the previous register content of PxCI can be re-written
* in the register write.
*/
if ((cap_status & AHCI_HBA_CAP_SNCQ) &&
"Command Queuing", NULL);
}
/* Support port multiplier? */
if (cap_status & AHCI_HBA_CAP_SPM) {
/* Support FIS-based switching for port multiplier? */
if (cap_status & AHCI_HBA_CAP_FBSS) {
}
}
/* Report the number of command slots */
#ifdef __lock_lint
#endif
/*
* When SATA framework adds support for pwrmgt the
* pwrmgt_ops needs to be updated
*/
/* Attach it to SATA framework */
!= DDI_SUCCESS) {
return (AHCI_FAILURE);
}
return (AHCI_SUCCESS);
}
/*
* Unregisters the ahci with sata framework.
*/
static int
{
"ahci_unregister_sata_hba_tran enter", NULL);
/* Detach from the SATA framework. */
DDI_SUCCESS) {
return (AHCI_FAILURE);
}
/* Deallocate sata_hba_tran. */
sizeof (sata_hba_tran_t));
return (AHCI_SUCCESS);
}
if (AHCI_ADDR_IS_PORT(addrp)) \
else if (AHCI_ADDR_IS_PMULT(addrp)) \
else \
/*
* ahci_tran_probe_port is called by SATA framework. It returns port state,
* port status registers and an attached device type via sata_device
* structure.
*
* We return the cached information from a previous hardware probe. The
* actual hardware probing itself was done either from within
* ahci_initialize_controller() during the driver attach or from a phy
* ready change interrupt handler.
*/
static int
{
char portstr[10];
"ahci_tran_probe_port enter: port %s", portstr);
/* port mutliplier is removed. */
"ahci_tran_probe_port: "
"pmult is removed from port %s", portstr);
return (SATA_FAILURE);
}
/*
* The sata_device may refer to
* 1. A controller port.
* A controller port should be ready here.
* 2. A port multiplier.
* SATA_ADDR_PMULT_SPEC - if it is not initialized yet, initialize
* it and register the port multiplier to the framework.
* SATA_ADDR_PMULT - check the status of all its device ports.
* 3. A port multiplier port.
* If it has not been initialized, initialized it.
*
* A port multiplier or a port multiplier port may require some
* initialization because we cannot do these time-consuming jobs in an
* interrupt context.
*/
/* Initialize registers on a port multiplier */
if (rval_init != AHCI_SUCCESS) {
"ahci_tran_probe_port: "
"pmult initialization failed.", NULL);
return (SATA_FAILURE);
}
/* Check pmports hotplug events */
rval = SATA_FAILURE;
goto out;
}
}
/* Update port state and device type */
switch (port_state) {
case SATA_PSTATE_FAILED:
"ahci_tran_probe_port: port %s PORT FAILED", portstr);
goto out;
case SATA_PSTATE_SHUTDOWN:
"ahci_tran_probe_port: port %s PORT SHUTDOWN", portstr);
goto out;
case SATA_PSTATE_PWROFF:
"ahci_tran_probe_port: port %s PORT PWROFF", portstr);
goto out;
case SATA_PSTATE_PWRON:
"ahci_tran_probe_port: port %s PORT PWRON", portstr);
break;
default:
"ahci_tran_probe_port: port %s PORT NORMAL %x",
break;
}
switch (device_type) {
case SATA_DTYPE_ATADISK:
"ahci_tran_probe_port: port %s DISK found", portstr);
break;
case SATA_DTYPE_ATAPI:
/*
* HBA driver only knows it's an ATAPI device, and don't know
* type need to be determined by checking IDENTIFY PACKET
* DEVICE data
*/
"ahci_tran_probe_port: port %s ATAPI found", portstr);
break;
case SATA_DTYPE_PMULT:
/* Update the number of pmports. */
"ahci_tran_probe_port: port %s Port Multiplier found",
portstr);
break;
case SATA_DTYPE_UNKNOWN:
"ahci_tran_probe_port: port %s Unknown device found",
portstr);
break;
default:
/* we don't support any other device types */
"ahci_tran_probe_port: port %s No device found", portstr);
break;
}
out:
} else if (AHCI_ADDR_IS_PMPORT(&addr)) {
if (port_state & SATA_STATE_READY)
rval = SATA_FAILURE;
}
/* Check handles for the sata registers access */
rval = SATA_FAILURE;
}
return (rval);
}
/*
* There are four operation modes in sata framework:
* SATA_OPMODE_INTERRUPTS
* SATA_OPMODE_POLLING
* SATA_OPMODE_ASYNCH
* SATA_OPMODE_SYNCH
*
* Their combined meanings as following:
*
* SATA_OPMODE_SYNCH
* The command has to be completed before sata_tran_start functions returns.
* Either interrupts or polling could be used - it's up to the driver.
* Mode used currently for internal, sata-module initiated operations.
*
* SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS
* It is the same as the one above.
*
* SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING
* The command has to be completed before sata_tran_start function returns.
* No interrupt used, polling only. This should be the mode used for scsi
* packets with FLAG_NOINTR.
*
* SATA_OPMODE_ASYNCH | SATA_OPMODE_INTERRUPTS
* The command may be queued (callback function specified). Interrupts could
* be used. It's normal operation mode.
*/
/*
* Called by sata framework to transport a sata packet down stream.
*/
static int
{
char portstr[10];
"ahci_tran_start enter: cport %d satapkt 0x%p",
/* Sanity check */
if (AHCI_ADDR_IS_PMPORT(&addr)) {
&spkt->satapkt_device);
"ahci_tran_start returning PORT_ERROR while "
"pmult removed: port: %s", portstr);
return (SATA_TRAN_PORT_ERROR);
}
SATA_STATE_READY)) {
if (!ddi_in_panic() ||
&spkt->satapkt_device);
"ahci_tran_start returning PORT_ERROR "
"while sub-link is not initialized "
"at port: %s", portstr);
return (SATA_TRAN_PORT_ERROR);
}
}
}
/*
* In case the target driver would send the packet before
* sata framework can have the opportunity to process those
* event reports.
*/
&spkt->satapkt_device);
"ahci_tran_start returning PORT_ERROR while "
"port: %s", portstr);
return (SATA_TRAN_PORT_ERROR);
}
/*
* ahci_intr_phyrdy_change() may have rendered it to
* SATA_DTYPE_NONE.
*/
&spkt->satapkt_device);
"ahci_tran_start returning PORT_ERROR while "
"no device attached: port: %s", portstr);
return (SATA_TRAN_PORT_ERROR);
}
/* R/W PMULT command will occupy the whole HBA port */
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
"ahci_tran_start returning BUSY while "
"port: %s", portstr);
return (SATA_TRAN_BUSY);
}
"ahci_tran_start returning BUSY while "
"hot-plug in progress: port: %s", portstr);
return (SATA_TRAN_BUSY);
}
/*
* SATA HBA driver should remember that a device was reset and it
* is supposed to reject any packets which do not specify either
* SATA_IGNORE_DEV_RESET_STATE or SATA_CLEAR_DEV_RESET_STATE.
*
* This is to prevent a race condition when a device was arbitrarily
* reset by the HBA driver (and lost it's setting) and a target
* driver sending some commands to a device before the sata framework
* has a chance to restore the device setting (such as cache enable/
* disable or other resettable stuff).
*/
/*
* It is unnecessary to use specific flags to indicate
* reset_in_progress for a pmport. While mopping, all command will be
* mopped so that the entire HBA port is being dealt as a single
* object.
*/
"ahci_tran_start [CLEAR] the "
"reset_in_progress for port: %d", port);
}
if (ahci_portp->ahciport_reset_in_progress &&
! ddi_in_panic()) {
"ahci_tran_start returning BUSY while "
"reset in progress: port: %d", port);
return (SATA_TRAN_BUSY);
}
#ifdef AHCI_DEBUG
"ahci_tran_start: packet 0x%p [PASSTHRU] at port %d",
}
#endif
"ahci_tran_start returning BUSY while "
"mopping in progress: port: %d", port);
return (SATA_TRAN_BUSY);
}
return (SATA_TRAN_BUSY);
}
if (spkt->satapkt_op_mode &
/*
* If a SYNC command to be executed in interrupt context,
* bounce it back to sata module.
*/
servicing_interrupt()) {
"ahci_tran_start returning BUSY while "
"sending SYNC mode under interrupt context: "
"port : %d", port);
return (SATA_TRAN_BUSY);
}
/* We need to do the sync start now */
spkt) == AHCI_FAILURE) {
goto fail_out;
}
} else {
/* Async start, using interrupt */
== AHCI_FAILURE) {
goto fail_out;
}
}
"sata tran accepted: port %s", portstr);
return (SATA_TRAN_ACCEPTED);
/*
* Failed to deliver packet to the controller.
* Check if it's caused by invalid handles.
*/
return (SATA_TRAN_PORT_ERROR);
}
"return QUEUE_FULL: port %d", port);
return (SATA_TRAN_QUEUE_FULL);
}
/*
* SATA_OPMODE_SYNCH flag is set
*
* If SATA_OPMODE_POLLING flag is set, then we must poll the command
* without interrupt, otherwise we can still use the interrupt.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
int pkt_timeout_ticks;
int rval;
return (rval);
}
/* Simulate the interrupt */
if (pkt_timeout_ticks < 0) {
"port %d satapkt 0x%p timed out\n",
port, timeout_tags);
}
}
return (AHCI_SUCCESS);
} else {
return (rval);
#if AHCI_DEBUG
/*
* Note that the driver always uses the slot 0 to deliver
* REQUEST SENSE or READ LOG EXT command
*/
#endif
return (AHCI_SUCCESS);
}
}
/*
* Searches for and claims a free command slot.
*
* Returns value:
*
* AHCI_FAILURE returned only if
* 1. No empty slot left
* 2. Non-queued command requested while queued command(s) is outstanding
* 3. Queued command requested while non-queued command(s) is outstanding
* 4. HBA doesn't support multiple-use of command list while already a
* non-queued command is oustanding
* 5. Queued command requested while some queued command(s) has been
* outstanding on a different port multiplier port. (AHCI spec 1.2,
* 9.1.2)
*
* claimed slot number returned if succeeded
*
* NOTE: it will always return slot 0 for following commands to simplify the
* algorithm.
* 1. REQUEST SENSE or READ LOG EXT command during error recovery process
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
int slot;
"ahciport_pending_tags = 0x%x "
"ahciport_pending_ncq_tags = 0x%x",
/*
* According to the AHCI spec, system software is responsible to
* ensure that queued and non-queued commands are not mixed in
* the command list.
*/
if (command_type == AHCI_NON_NCQ_CMD) {
/* Non-NCQ command request */
if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
"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)) {
"ahci_claim_free_slot: there is still pending "
"so no available slot for the non-queued command",
NULL);
return (AHCI_FAILURE);
}
"ahci_claim_free_slot: HBA cannot support multiple-"
"use of the command list for non-queued commands",
NULL);
return (AHCI_FAILURE);
}
} else if (command_type == AHCI_NCQ_CMD) {
/* NCQ command request */
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
"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);
}
/*
* NCQ commands cannot be sent to different port multiplier
* ports in Command-Based Switching mode
*/
/*
* NOTE: In Command-Based Switching mode, AHCI controller
* usually reports a 'Handshake Error' when multiple NCQ
* commands are outstanding simultaneously.
*/
"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);
}
}
} else if (command_type == AHCI_ERR_RETRI_CMD) {
/* Error retrieval command request */
"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) {
/*
* An extra check on PxCI. Sometimes PxCI bits may not be
* cleared during hot-plug or error recovery process.
*/
if (port_cmd_issue != 0) {
"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.",
return (AHCI_FAILURE);
}
"ahci_claim_free_slot: slot 0 is allocated for "
slot = 0;
goto out;
}
if (slot == -1) {
"ahci_claim_free_slot: no empty slots", NULL);
return (AHCI_FAILURE);
}
/*
* According to the AHCI spec, to allow a simple mechanism for the
* HBA to map command list slots to queue entries, software must
* match the tag number it uses to the slot it is placing the command
* in. For example, if a queued command is placed in slot 5, the tag
* for that command must be 5.
*/
if (command_type == AHCI_NCQ_CMD) {
if (AHCI_ADDR_IS_PMPORT(addrp)) {
}
}
out:
"ahci_claim_free_slot: found slot: 0x%x", slot);
return (slot);
}
/*
* Builds the Command Table for the sata packet and delivers it to controller.
*
* Returns:
* slot number if we can obtain a slot successfully
* otherwise, return AHCI_FAILURE
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
int cmd_slot;
int ncookies;
int i;
int command_type = AHCI_NON_NCQ_CMD;
int ncq_qdepth;
#if AHCI_DEBUG
#endif
/* Check if the command is a NCQ command */
/*
* When NCQ is support, system software must determine the
* maximum tag allowed by the device and the HBA, and it
* must use a value not beyond of the lower bound of the two.
*
* Sata module is going to calculate the qdepth and send
* down to HBA driver via sata_cmd.
*/
/*
* At the moment, the driver doesn't support the dynamic
* setting of the maximum ncq depth, and the value can be
* set either during the attach or after hot-plug insertion.
*/
if (ahci_portp->ahciport_max_ncq_tags == 0) {
"ahci_deliver_satapkt: port %d the max tags for "
} else {
" 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",
}
}
}
/* Check if the command is an error retrieval command */
/* Check if there is an empty command slot */
if (cmd_slot == AHCI_FAILURE) {
return (AHCI_FAILURE);
}
"ahci_deliver_satapkt enter: cmd_reg: 0x%x, cmd_slot: 0x%x, "
/* For data transfer operations, it is the H2D Register FIS */
/*
* PMP field only make sense when target is a port multiplier or a
* device behind a port multiplier. Otherwise should set it to 0.
*/
switch (scmd->satacmd_addr_type) {
case 0:
/*
* satacmd_addr_type will be 0 for the commands below:
* ATAPI command
* SATAC_IDLE_IM
* SATAC_STANDBY_IM
* SATAC_DOWNLOAD_MICROCODE
* SATAC_FLUSH_CACHE
* SATAC_SET_FEATURES
* SATAC_SMART
* SATAC_ID_PACKET_DEVICE
* SATAC_ID_DEVICE
* SATAC_READ_PORTMULT
* SATAC_WRITE_PORTMULT
*/
/* FALLTHRU */
case ATA_ADDR_LBA:
/* FALLTHRU */
case ATA_ADDR_LBA28:
/* LBA[7:0] */
/* LBA[15:8] */
/* LBA[23:16] */
/* LBA [27:24] (also called dev_head) */
break;
case ATA_ADDR_LBA48:
/* LBA[7:0] */
/* LBA[15:8] */
/* LBA[23:16] */
/* LBA [31:24] */
/* LBA [39:32] */
/* LBA [47:40] */
/* Set dev_head */
/* Set the extended sector count and features */
break;
}
/*
* filled into features field, and sector count 8:15 is filled into
* features (exp) field. The hba driver doesn't need to anything
* special with regard to this, since sata framework has already
* done so.
*
* However the driver needs to make sure TAG is filled into sector
* field.
*/
if (command_type == AHCI_NCQ_CMD) {
(cmd_slot << SATA_TAG_QUEUING_SHIFT));
}
"ncookies = 0x%x, ahci_dma_prdt_number = 0x%x",
/* *** now fill the scatter gather list ******* */
for (i = 0; i < ncookies; i++) {
}
"ahciport_prd_bytecounts 0x%x for cmd_slot 0x%x",
/* The ACMD field is filled in for ATAPI command */
}
/* Set Command Header in Command List */
/* Set the number of entries in the PRD table */
/* Set the length of the command in the CFIS area */
/*
* PMP field only make sense when target is a port multiplier or a
* device behind a port multiplier. Otherwise should set it to 0.
*/
"sata_data_direction = 0x%x",
/* Set A bit if it is an ATAPI command */
/* Set W bit if data is going to the device */
/*
* Set the prefetchable bit - this bit is only valid if the PRDTL
* field is non-zero or the ATAPI 'A' bit is set in the command
* header. This bit cannot be set when using native command
* queuing commands or when using FIS-based switching with a Port
* multiplier.
*/
if (command_type != AHCI_NCQ_CMD)
/*
* Now remember the sata packet in ahciport_slot_pkts[].
* Error retrieval command and r/w port multiplier command will
* be stored specifically for each port.
*/
if (!ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
/*
* Keep the timeout value
*/
/*
* If the intial timout is less than 1 tick, then make it longer by
* 1 tick to avoid immediate timeout
*/
#if AHCI_DEBUG
if (ahci_debug_flags & AHCIDBG_ATACMD &&
/* Dump the command header and table */
"0x%p cmd_reg 0x%x port %d", spkt,
" Command Header:%8x %8x %8x %8x",
/* Dump the H2D register FIS */
" Command FIS: %8x %8x %8x %8x",
/* Dump the ACMD register FIS */
for (i = 0; i < SATA_ATAPI_MAX_CDB_LEN/8; i++)
if (ahci_debug_flags & AHCIDBG_ATAPICMD)
" ATAPI command: %2x %2x %2x %2x "
"%2x %2x %2x %2x",
/* Dump the PRDT */
for (i = 0; i < ncookies; i++) {
" Cookie %d: %8x %8x %8x %8x",
}
}
#endif
(void) ddi_dma_sync(
0,
cmd_slot * sizeof (ahci_cmd_header_t),
sizeof (ahci_cmd_header_t),
if ((ahci_check_dma_handle(ahci_portp->
return (AHCI_FAILURE);
}
/* Set the corresponding bit in the PxSACT.DS for queued command */
if (command_type == AHCI_NCQ_CMD) {
(0x1 << cmd_slot));
}
/* Indicate to the HBA that a command is active. */
(0x1 << cmd_slot));
"exit: port %d", port);
ahcictl_ahci_acc_handle) != DDI_FM_OK) {
return (AHCI_FAILURE);
}
return (cmd_slot);
}
/*
* Called by the sata framework to abort the previously sent packet(s).
*
* Reset device to abort commands.
*/
static int
{
uint32_t slot_status = 0;
uint32_t aborted_tags = 0;
uint32_t finished_tags = 0;
int tmp_slot;
"ahci_tran_abort enter: port %d", port);
/*
* If AHCI_PORT_FLAG_MOPPING flag is set, it means all the pending
* commands are being mopped, therefore there is nothing else to do
*/
"ahci_tran_abort: port %d is in "
"mopping process, so just return directly ", port);
return (SATA_SUCCESS);
}
/*
* If AHCI_PORT_FLAG_RDWR_PMULT flag is set, it means a R/W PMULT
* command is being executed so no other commands is outstanding,
* nothing to do.
*/
"port multiplier, so just return directly ", port);
return (SATA_SUCCESS);
}
/*
* In case the targer driver would send the request before
* sata framework can have the opportunity to process those
* event reports.
*/
&spkt->satapkt_device);
"ahci_tran_abort returning SATA_FAILURE while "
"port: %d", port);
return (SATA_FAILURE);
}
/*
* ahci_intr_phyrdy_change() may have rendered it to
* AHCI_PORT_TYPE_NODEV.
*/
&spkt->satapkt_device);
"ahci_tran_abort returning SATA_FAILURE while "
"no device attached: port: %d", port);
return (SATA_FAILURE);
}
if (flag == SATA_ABORT_ALL_PACKETS) {
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
} else {
aborted_tags = 0xffffffff;
/*
* Aborting one specific packet, first search the
* ahciport_slot_pkts[] list for matching spkt.
*/
for (tmp_slot = 0;
break;
}
}
if (aborted_tags == 0xffffffff) {
/* request packet is not on the pending list */
"Cannot find the aborting pkt 0x%p on the "
"pending list", (void *)spkt);
&spkt->satapkt_device);
return (SATA_FAILURE);
}
}
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
/*
* To abort the packet(s), first we are trying to clear PxCMD.ST
* to stop the port, and if the port can be stopped
* successfully with PxTFD.STS.BSY and PxTFD.STS.DRQ cleared to '0',
* then we just send back the aborted packet(s) with ABORTED flag
* and then restart the port by setting PxCMD.ST and PxCMD.FRE.
* If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to '1', then we
* perform a COMRESET.
*/
/*
* Compute which have finished and which need to be retried.
*
* The finished tags are ahciport_pending_tags/ahciport_pending_ncq_tags
* minus the slot_status. The aborted_tags has to be deducted by
* finished_tags since we can't possibly abort a tag which had finished
* already.
*/
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
0, /* failed tags */
0, /* timeout tags */
0); /* reset tags */
return (SATA_SUCCESS);
}
/*
* Used to do device reset and reject all the pending packets on a device
* during the reset operation.
*
* NOTE: ONLY called by ahci_tran_reset_dport
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
uint32_t slot_status = 0;
uint32_t reset_tags = 0;
uint32_t finished_tags = 0;
int ret;
"ahci_reset_device_reject_pkts on port: %d", port);
/*
* If AHCI_PORT_FLAG_MOPPING flag is set, it means all the pending
* commands are being mopped, therefore there is nothing else to do
*/
"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)) {
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
}
!= AHCI_SUCCESS) {
"Try to do a port reset after software "
"reset failed", port);
if (ret != AHCI_SUCCESS) {
"ahci_reset_device_reject_pkts: port %d "
"failed", port);
return (SATA_FAILURE);
}
}
/* Set the reset in progress flag */
/* Indicate to the framework that a reset has happened */
&sdevice,
"port %d sending event up: SATA_EVNT_DEVICE_RESET", port);
/* Next try to mop the pending commands */
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
reset_tags &= ~finished_tags;
0, /* failed tags */
0, /* timeout tags */
0, /* aborted tags */
reset_tags); /* reset tags */
return (SATA_SUCCESS);
}
/*
* Used to do device reset and reject all the pending packets on a device
* during the reset operation.
*
* NOTE: ONLY called by ahci_tran_reset_dport
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
"ahci_reset_pmdevice_reject_pkts: port %d is in "
"mopping process, so return directly ", port);
return (SATA_SUCCESS);
}
/* Checking for outstanding commands */
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
}
/* Issue SOFTWARE reset command. */
!= AHCI_SUCCESS) {
"Try to do a port reset after software "
"reset failed", port);
return (SATA_FAILURE);
}
/* Set the reset in progress flag */
/* Indicate to the framework that a reset has happened */
if (AHCI_ADDR_IS_PMULT(addrp))
else
&sdevice,
"port %d:%d sending event up: SATA_EVNT_DEVICE_RESET",
/* Next try to mop the pending commands */
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
reset_tags &= ~finished_tags;
"reset_tags = %x, finished_tags = %x, slot_status = %x",
/*
* NOTE: Because PxCI be only erased by unset PxCMD.ST bit, so even we
* try to reset a single device behind a port multiplier will
* terminate all the commands on that HBA port. We need mop these
* commands as well.
*/
0, /* failed tags */
0, /* timeout tags */
0, /* aborted tags */
reset_tags); /* reset tags */
return (SATA_SUCCESS);
}
/*
* Used to do port reset and reject all the pending packets on a port during
* the reset operation.
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
uint32_t slot_status = 0;
uint32_t reset_tags = 0;
uint32_t finished_tags = 0;
"ahci_reset_port_reject_pkts at port: %d", port);
/*
* If AHCI_PORT_FLAG_MOPPING flag is set, it means all the pending
* commands are being mopped, therefore there is nothing else to do
*/
"ahci_reset_port_reject_pkts: port %d is in "
"mopping process, so return directly ", port);
return (SATA_SUCCESS);
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
}
NULL) != AHCI_SUCCESS) {
/* Clear mop flag */
if (ahci_portp->ahciport_mop_in_progress == 0)
return (SATA_FAILURE);
}
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
reset_tags &= ~finished_tags;
0, /* failed tags */
0, /* timeout tags */
0, /* aborted tags */
reset_tags); /* reset tags */
return (SATA_SUCCESS);
}
/*
* Used to do hba reset and reject all the pending packets on all ports
* during the reset operation.
*/
static int
{
int port;
int ret = SATA_SUCCESS;
"ahci_reset_hba_reject_pkts enter", NULL);
continue;
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
"port %d: reset_tags = 0x%x pending_tags = 0x%x",
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
"port %d: reset_tags = 0x%x pending_tags = 0x%x",
}
}
ret = SATA_FAILURE;
}
continue;
}
/*
* To prevent recursive enter to ahci_mop_commands, we need
* check AHCI_PORT_FLAG_MOPPING flag.
*/
"ahci_reset_hba_reject_pkts: port %d is in "
"mopping process, so return directly ", port);
continue;
}
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
0, /* failed tags */
0, /* timeout tags */
0, /* aborted tags */
}
out:
return (ret);
}
/*
* Called by sata framework to reset a port(s) or device.
*/
static int
{
int ret = SATA_SUCCESS;
"ahci_tran_reset_dport enter: cport %d", cport);
case SATA_ADDR_PMPORT:
/*
* If we want to issue a COMRESET on a pmport, we need to
* reject the outstanding commands on that pmport. According
* to AHCI spec, PxCI register could only be cleared by
* clearing PxCMD.ST, which will halt the controller port - as
* well as other pmports.
*
* Therefore we directly reset the controller port for
* simplicity. ahci_tran_probe_port() will handle reset stuff
* like initializing the given pmport.
*/
/* FALLTHRU */
case SATA_ADDR_CPORT:
/* Port reset */
break;
case SATA_ADDR_DPMPORT:
/* FALLTHRU */
case SATA_ADDR_DCPORT:
/* Device reset */
/*
* software reset request must be sent to SATA_PMULT_HOSTPORT
* if target is a port multiplier:
*/
/*
* In case the targer driver would send the request
* before sata framework can have the opportunity to
* process those event reports.
*/
"ahci_tran_reset_dport returning SATA_FAILURE "
"port: %d", port);
ret = SATA_FAILURE;
break;
}
/*
* ahci_intr_phyrdy_change() may have rendered it to
* AHCI_PORT_TYPE_NODEV.
*/
&addr);
"ahci_tran_reset_dport returning SATA_FAILURE "
"while no device attached: port: %d", port);
ret = SATA_FAILURE;
break;
}
if (AHCI_ADDR_IS_PORT(&addr)) {
ahci_portp, &addr);
} else {
ahci_portp, &addr);
}
break;
case SATA_ADDR_CNTRL:
/* Reset the whole controller */
"reset the whole hba", instance);
break;
default:
ret = SATA_FAILURE;
}
return (ret);
}
/*
* Called by sata framework to activate a port as part of hotplug.
* (cfgadm -c connect satax/y)
* Support port multiplier.
*/
static int
{
"ahci_tran_hotplug_port_activate enter: cport %d", cport);
if (AHCI_ADDR_IS_PORT(&addr)) {
/* Enable the interrupts on the port */
/*
* Reset the port so that the PHY communication would be
* re-established. But this reset is an internal operation
* and the sata module doesn't need to know about it.
* Moreover, the port with a device attached will be started
* too.
*/
NULL);
/*
* Need to check the link status and device status of the port
* and consider raising power if the port was in D3 state
*/
} else if (AHCI_ADDR_IS_PMPORT(&addr)) {
/* AHCI_ADDR_PMPORT */
}
return (SATA_SUCCESS);
}
/*
* Called by sata framework to deactivate a port as part of hotplug.
* (cfgadm -c disconnect satax/y)
* Support port multiplier.
*/
static int
{
"ahci_tran_hotplug_port_deactivate enter: cport %d", cport);
if (AHCI_ADDR_IS_PORT(&addr)) {
/* Disable the interrupts on the port */
/* First to abort all the pending commands */
/* Then stop the port */
ahci_portp, port);
}
/* Next put the PHY offline */
} else if (AHCI_ADDR_IS_PMPORT(&addr)) {
!= SATA_DTYPE_NONE)
/* Re-enable the interrupts for the other pmports */
}
/* Update port state */
return (SATA_SUCCESS);
}
/*
* To be used to mark all the outstanding pkts with SATA_PKT_ABORTED
* when a device is unplugged or a port is deactivated.
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static void
{
uint32_t slot_status = 0;
uint32_t abort_tags = 0;
"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;
}
/*
* When AHCI_PORT_FLAG_MOPPING is set, we need to check whether a
* REQUEST SENSE command or READ LOG EXT command is delivered to HBA
* to get the error data, if yes when the device is removed, the
* command needs to be aborted too.
*/
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
slot_status = 0x1;
abort_tags = 0x1;
goto out;
} else {
"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)) {
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
}
out:
/* No need to do mop when there is no outstanding commands */
if (slot_status != 0) {
0, /* failed tags */
0, /* timeout tags */
abort_tags, /* aborting tags */
0); /* reset tags */
}
}
#if defined(__lock_lint)
static int
{
return (SATA_SUCCESS);
}
#endif
/*
* Initialize fma capabilities and register with IO fault services.
*/
static void
{
/*
* Need to change iblock to priority for new MSI intr
*/
/* Only register with IO Fault Services if we have some capability */
if (ahci_ctlp->ahcictl_fm_cap) {
/* Adjust access and dma attributes for FMA */
/*
* Register capabilities with IO Fault Services.
* ahcictl_fm_cap will be updated to indicate
* capabilities actually supported (not requested.)
*/
return;
}
/*
* Initialize pci ereport capabilities if ereport
* capable (should always be.)
*/
}
/*
* Register error callback if error callback capable.
*/
ahci_fm_error_cb, (void *) ahci_ctlp);
}
"ahci_fm_fini: fma enabled.", NULL);
}
}
/*
* Releases fma capabilities and un-registers with IO fault services.
*/
static void
{
/* Only unregister FMA capabilities if registered */
if (ahci_ctlp->ahcictl_fm_cap) {
/*
* Un-register error callback if error callback capable.
*/
}
/*
* Release any resources allocated by pci_ereport_setup()
*/
}
/* Unregister from IO Fault Services */
/* Adjust access and dma attributes for FMA */
"ahci_fm_fini: fma disabled.", NULL);
}
}
/*ARGSUSED*/
static int
{
/*
* as the driver can always deal with an error in any dma or
* access handle, we can just return the fme_status value.
*/
return (err->fme_status);
}
int
{
return (de.fme_status);
}
int
{
return (de.fme_status);
}
/*
* Generate an ereport
*/
void
{
char buf[FM_MAX_CLASS];
}
}
/*
* Check if all handles are correctly allocated. This function is only called
* by ahci_attach(), so we do not need hold any mutex here.
*/
static int
{
int port;
return (DDI_FAILURE);
}
continue;
return (DDI_FAILURE);
}
}
return (DDI_SUCCESS);
}
/*
* Check the access handles for the controller. Note that
* ahcictl_pci_conf_handle is only used in attach process.
*/
static int
{
if ((ahci_check_acc_handle(ahci_ctlp->
ahcictl_pci_conf_handle) != DDI_FM_OK) ||
ahcictl_ahci_acc_handle) != DDI_FM_OK)) {
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
/*
* Check the DMA handles and the access handles of a controller port.
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
int slot;
if ((ahci_check_dma_handle(ahci_portp->
return (DDI_FAILURE);
}
slot ++) {
!= DDI_SUCCESS) {
return (DDI_FAILURE);
}
}
return (DDI_SUCCESS);
}
/*
* Check the DMA handles and the access handles of a cmd table slot.
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
if ((ahci_check_acc_handle(ahci_portp->
return (DDI_FAILURE);
}
return (DDI_SUCCESS);
}
/*
* Allocate the ports structure, only called by ahci_attach
*/
static int
{
"ahci_alloc_ports_state enter", NULL);
/* Allocate structures only for the implemented ports */
"hba port %d not implemented", port);
continue;
}
goto err_out;
}
}
return (AHCI_SUCCESS);
}
}
return (AHCI_FAILURE);
}
/*
* Reverse of ahci_alloc_ports_state(), only called by ahci_detach
*/
static void
{
int port;
/* if this port is implemented by the HBA */
}
}
/*
* Drain the taskq.
*/
static void
{
int port;
continue;
}
}
}
/*
* Initialize the controller and all ports. And then try to start the ports
* if there are devices attached.
*
* This routine can be called from three seperate cases: DDI_ATTACH,
* PM_LEVEL_D0 and DDI_RESUME. The DDI_ATTACH case is different from
* other two cases; device signature probing are attempted only during
* DDI_ATTACH case.
*
* WARNING!!! Disable the whole controller's interrupts before calling and
* the interrupts will be enabled upon successfully return.
*/
static int
{
int port;
"ahci_initialize_controller enter", NULL);
/* Initialize the implemented ports and structures */
continue;
}
/*
* Ensure that the controller is not in the running state
* by checking every implemented port's PxCMD register
*/
!= AHCI_SUCCESS) {
"ahci_initialize_controller: failed to "
"initialize port %d", port);
/*
* Set the port state to SATA_PSTATE_FAILED if
* failed to initialize it.
*/
}
}
/* Enable the whole controller interrupts */
return (AHCI_SUCCESS);
}
/*
* Reverse of ahci_initialize_controller()
*
* We only need to stop the ports and disable the interrupt.
*/
static void
{
int port;
"ahci_uninitialize_controller enter", NULL);
/* disable all the interrupts. */
continue;
}
/* Stop the port by clearing PxCMD.ST */
/*
* Here we must disable the port interrupt because
* ahci_disable_all_intrs only clear GHC.IE, and IS
* register will be still set if PxIE is enabled.
* When ahci shares one IRQ with other drivers, the
* intr handler may claim the intr mistakenly.
*/
ahci_portp, port);
}
}
/*
* ahci_alloc_pmult()
* 1. Setting HBA port registers which are necessary for a port multiplier.
* (Set PxCMD.PMA while PxCMD.ST is '0')
* 2. Allocate ahci_pmult_info structure.
*
* NOTE: Must stop port before the function is called.
* WARNING!!! ahciport_mutex should be acquired before the function is
* called.
*/
static void
{
/* The port must have been stopped before. */
if (!(port_cmd_status & AHCI_CMD_STATUS_PMA)) {
/* set PMA bit */
"ahci_alloc_pmult: "
"PxCMD.PMA bit set at port %d.", port);
}
/* Allocate port multiplier information structure */
}
}
/*
* ahci_dealloc_pmult()
* 1. Clearing related registers when a port multiplier is detached.
* (Clear PxCMD.PMA while PxCMD.ST is '0')
* 2. Deallocate ahci_pmult_info structure.
*
* NOTE: Must stop port before the function is called.
* WARNING!!! ahciport_mutex should be acquired before the function is
* called.
*/
static void
{
if (port_cmd_status & AHCI_CMD_STATUS_PMA) {
/* Clear PMA bit */
(port_cmd_status & (~AHCI_CMD_STATUS_PMA)));
"ahci_dealloc_pmult: "
"PxCMD.PMA bit cleared at port %d.", port);
}
/* Release port multiplier information structure */
sizeof (ahci_pmult_info_t));
}
}
/*
* Staggered Spin-up.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static void
{
/* Check for staggered spin-up support */
if (!(cap_status & AHCI_HBA_CAP_SSS))
return;
/* If PxCMD.SUD == 1, no staggered spin-up is needed */
return;
/* Set PxCMD.SUD */
}
/*
* The routine is to initialize a port. First put the port in NOTRunning
* state, then enable port interrupt and clear Serror register. And under
* AHCI_ATTACH case, find device signature and then try to start the port.
*
* Called by
* 1. ahci_initialize_controller
* 2. ahci_intr_phyrdy_change (hotplug)
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
int ret;
/* AHCI_ADDR_PORT: We've no idea of the attached device here. */
/*
* attached devices during DDI_ATTACH. In fact, the device can be
* changed during power state changes, but at the time being, we
* don't support the situation.
*/
} else {
/* check for DDI_RESUME case */
}
if (resuming) {
/*
* During the resume, we need to set the PxCLB, PxCLBU, PxFB
* and PxFBU registers in case these registers were cleared
* during the suspend.
*/
"ahci_initialize_port: port %d "
"set PxCLB, PxCLBU, PxFB and PxFBU "
"during resume", port);
return (AHCI_FAILURE);
}
"ahci_initialize_port: port %d ", port);
/*
* Check whether the port is in NotRunning state, if not,
* put the port in NotRunning state
*/
if (port_cmd_status &
ahci_portp, port);
}
/* Make sure the drive is spun-up */
/* Disable interrupt */
/* Device is unknown at first */
/* Disable the interface power management */
/* Check physcial link status */
/* Check interface status */
/* Check whether port reset must be executed */
/* Always reset port on RESUME */
/* Something went wrong, we need do some reset things */
/* Does port reset succeed on HBA port? */
if (ret != AHCI_SUCCESS) {
"ahci_initialize_port:"
"port reset failed at port %d", port);
return (AHCI_FAILURE);
}
/* Is port failed? */
"ahci_initialize_port: port %d state 0x%x",
return (AHCI_FAILURE);
}
}
/*
* Try to get the device signature if the port is not empty.
*/
/* Return directly if no device connected */
"No device connected to port %d", port);
goto out;
}
/* If this is a port multiplier, we need do some initialization */
"Port multiplier found at port %d", port);
}
/* Try to start the port */
!= AHCI_SUCCESS) {
"failed to start port %d", port);
return (AHCI_FAILURE);
}
out:
/* Enable port interrupts */
return (AHCI_SUCCESS);
}
/*
* Handle hardware defect, and check the capabilities. For example,
* power management capabilty and MSI capability.
*/
static int
{
#if AHCI_DEBUG
#endif
/*
* Modify dma_attr_align of ahcictl_buffer_dma_attr. For VT8251, those
* controllers with 0x00 revision id work on 4-byte aligned buffer,
* which is a bug and was fixed after 0x00 revision id controllers.
*
* Moreover, VT8251 cannot use multiple command slots in the command
* list for non-queued commands because the previous register content
* of PxCI can be re-written in the register write, so a flag will be
* set to record this defect - AHCI_CAP_NO_MCMDLIST_NONQUEUE.
*
* For VT8251, software reset also has the same defect as the below
* is filled in pmport field. Therefore, another software reset need
* to be done with 0 filled in pmport field.
*/
"revision id = 0x%x", revision);
if (revision == 0x00) {
"change ddi_attr_align to 0x4", NULL);
}
"VT8251 cannot use multiple command lists for "
"non-queued commands", NULL);
}
/*
* DMA addressing for communication memory descriptors though S64A bit
* of CAP register declares it supports. Even though 64-bit DMA for
* data buffer works on ASUS M2A-VM with newer BIOS, three other
* motherboards are known not, so both AHCI_CAP_BUF_32BIT_DMA and
* AHCI_CAP_COMMU_32BIT_DMA are set for this controller.
*
* Due to certain hardware issue, the chipset must do port reset during
* initialization, otherwise, when retrieving device signature,
* software reset will get time out. So AHCI_CAP_INIT_PORT_RESET flag
* need to set.
*
* For this chipset software reset will get failure if the pmport of
* Register FIS was set with SATA_PMULT_HOSTPORT (0xf) and no port
* multiplier is connected to the port. In order to fix the issue,
* AHCI_CAP_SRST_NO_HOSTPORT flag need to be set, and once software
* reset got failure, the driver will try to do another software reset
* with pmport 0.
*/
"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);
"ATI SB600 need to do a port reset during initialization",
NULL);
"ATI SB600 will get software reset failure if pmport "
"is set 0xf and no port multiplier is attached", NULL);
}
/*
* vendor ID and device ID (0x1002,0x4391).
*
* SB700/750 AHCI chipset on some boards doesn't support 64-bit
* DMA addressing for communication memory descriptors though S64A bit
* of CAP register declares the support. However, it does support
* 64-bit DMA for data buffer. So only AHCI_CAP_COMMU_32BIT_DMA is
* set for this controller.
*
* SB710 has the same initialization issue as SB600, so it also need
* a port reset. That is AHCI_CAP_INIT_PORT_RESET need to set for it.
*
* SB700 also has the same issue about software reset, and thus
* AHCI_CAP_SRST_NO_HOSTPORT flag also is needed.
*/
"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);
"ATI SB710 need to do a port reset during initialization",
NULL);
"ATI SB700 will get software reset failure if pmport "
"is set 0xf and no port multiplier is attached", NULL);
}
/*
* Check if capabilities list is supported and if so,
* get initial capabilities pointer and clear bits 0,1.
*/
PCI_CONF_STAT) & PCI_STAT_CAP) {
PCI_CONF_CAP_PTR), 4);
} else {
}
/*
* Walk capabilities if supported.
*/
/*
* Check that we haven't exceeded the maximum number of
* capabilities and that the pointer is in a valid range.
*/
if (++cap_count > PCI_CAP_MAX_PTR) {
"too many device capabilities", NULL);
return (AHCI_FAILURE);
}
if (caps_ptr < PCI_CAP_PTR_OFF) {
"capabilities pointer 0x%x out of range",
caps_ptr);
return (AHCI_FAILURE);
}
/*
* Get next capability and check that it is valid.
* For now, we only support power management.
*/
caps_ptr);
switch (cap) {
case PCI_CAP_ID_PM:
/* power management supported */
/* Save PMCSR offset */
#if AHCI_DEBUG
"Power Management capability found PCI_PMCAP "
"PCI Power Management Interface "
"spec 1.2 compliant", NULL);
#endif
break;
case PCI_CAP_ID_MSI:
#if AHCI_DEBUG
caps_ptr + PCI_MSI_CTRL);
"Message Signaled Interrupt capability found "
#endif
"MSI capability found", NULL);
break;
case PCI_CAP_ID_PCIX:
"PCI-X capability found", NULL);
break;
case PCI_CAP_ID_PCI_E:
"PCI Express capability found", NULL);
break;
case PCI_CAP_ID_MSI_X:
"MSI-X capability found", NULL);
break;
case PCI_CAP_ID_SATA:
"SATA capability found", NULL);
break;
case PCI_CAP_ID_VS:
"Vendor Specific capability found", NULL);
break;
default:
"unrecognized capability 0x%x", cap);
break;
}
/*
* Get next capabilities pointer and clear bits 0,1.
*/
}
return (AHCI_SUCCESS);
}
/*
* PORTMULT command. SYNC & POLLING mode is used.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
int rval;
char portstr[10];
/* Check the existence of the port multiplier */
return (AHCI_FAILURE);
/*
* Make sure no command is outstanding here. All R/W PMULT requests
* come from
*
* 1. ahci_attach()
* The port should be empty.
*
* 2. ahci_tran_probe_port()
* Any request from SATA framework (via ahci_tran_start) should be
* rejected if R/W PMULT command is outstanding.
*
* If we are doing mopping, do not check those flags because no
* command will be actually outstanding.
*
* If the port has been occupied by any other commands, the probe
* function will return a SATA_RETRY. SATA framework will retry
* later.
*/
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
"R/W PMULT failed: R/W PMULT in progress at port %d.",
return (AHCI_FAILURE);
}
"R/W PMULT failed: port %d is occupied (flags 0x%x).",
return (AHCI_FAILURE);
}
/*
* The port multiplier is gone. This may happen when
* 1. Cutting off the power of an enclosure. The device lose the power
* before port multiplier.
* 2. Disconnecting the port multiplier during hot-plugging a sub-drive.
*
* The issued command should be aborted and the following command
* should not be continued.
*/
"port-mult is removed from port %d", port);
return (AHCI_FAILURE);
}
/*
* of the port multiplier.
*/
/* No interrupt here. Store the interrupt enable mask. */
if (rval == AHCI_SUCCESS &&
if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
}
} else {
/* Failed or not completed. */
"ahci_rdwr_pmult: cannot [%s] %s[%d] at port %s",
rval = AHCI_FAILURE;
}
out:
/* Restore the interrupt mask */
return (rval);
}
static int
{
}
static int
{
}
goto out;
goto out;
/*
* ahci_update_pmult_gscr()
* ahci_update_pmult_pscr()
*
* WARNING!!! ahciport_mutex should be acquired before those functions
* get called.
*/
static int
{
return (AHCI_SUCCESS);
err: /* R/W PMULT error */
return (AHCI_FAILURE);
}
static int
{
return (AHCI_SUCCESS);
err: /* R/W PMULT error */
return (AHCI_FAILURE);
}
/*
* ahci_initialize_pmult()
*
* Initialize a port multiplier, including
* 1. Enable FEATURES register at port multiplier. (SATA Chp.16)
* 2. Redefine MASK register. (SATA Chap 16.?)
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
"[Initialize] Port-multiplier at port %d.", port);
/*
* Enable features of port multiplier. Currently only
* Asynchronous Notification is enabled.
*/
/* Check gscr64 for supported features. */
if (gscr64 & SATA_PMULT_CAP_SNOTIF) {
"port %d: Port Multiplier supports "
"Asynchronous Notification.", port);
/* Write to gscr96 to enabled features */
"port %d: PMult PxSNTF cleared.", port);
}
/*
* Now we need to update gscr33 register to enable hot-plug interrupt
* for sub devices behind port multiplier.
*/
/*
* Fetch the number of device ports of the port multiplier
*/
return (AHCI_FAILURE);
/* Register the port multiplier to SATA Framework. */
"port %d: pmult sub-port number updated to %x.", port,
/* Till now port-mult is successfully initialized */
return (AHCI_SUCCESS);
err: /* R/W PMULT error */
return (AHCI_FAILURE);
}
/*
* Initialize a port multiplier port. According to spec, firstly we need
* issue a COMRESET, then a software reset to get its signature.
*
* NOTE: This function should only be called in ahci_probe_pmport()
* WARNING!!! ahciport_mutex should be acquired before the function.
*/
static int
{
int ret = AHCI_FAILURE;
/* Check HBA port state */
"ahci_initialize_pmport:"
"port %d:%d Port Multiplier is failed.",
return (AHCI_FAILURE);
}
return (AHCI_FAILURE);
}
/* Checking for outstanding commands */
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
}
/* Clear status */
/* Firstly assume an unknown device */
/* port reset is necessary for port multiplier port */
"ahci_initialize_pmport:"
"port reset failed at port %d:%d",
goto out;
}
/* Is port failed? */
"ahci_initialize_pmport: port %d:%d failed. "
goto out;
}
/* Is there any device attached? */
== SATA_DTYPE_NONE) {
/* Do not waste time on an empty port */
"ahci_initialize_pmport: No device is found "
ret = AHCI_SUCCESS;
goto out;
}
/*
* Till now we can assure a device attached to that HBA port and work
* correctly. Now try to get the device signature. This is an optional
* step. If failed, unknown device is assumed, then SATA module will
* continue to use IDENTIFY DEVICE to get the information of the
* device.
*/
ret = AHCI_SUCCESS;
out:
/* Next try to mop the pending commands */
else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
reset_tags &= ~finished_tags;
0, /* failed tags */
0, /* timeout tags */
0, /* aborted tags */
reset_tags); /* reset tags */
/* Clear PxSNTF register if supported. */
}
return (ret);
}
/*
* ahci_probe_pmult()
*
* This function will be called to probe a port multiplier, which will
* handle hotplug events on port multiplier ports.
*
* NOTE: Only called from ahci_tran_probe_port()
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
int dev_exists_now = 0, dev_existed_previously = 0;
int npmport;
/* The bits in GSCR32 refers to the pmport that has a hot-plug event. */
do {
if (npmport == -1)
/* no pending hot plug events. */
return (AHCI_SUCCESS);
/* Check previous device at that port */
!= SATA_DTYPE_NONE)
/* PxSStatus tells the presence of device. */
&pmport_sstatus, err);
if (SSTATUS_GET_DET(pmport_sstatus) ==
dev_exists_now = 1;
/*
* Clear PxSERR is critical. The transition from 0 to 1 will
* emit a FIS which generates an asynchronous notification
* event at controller. If we fail to clear the PxSERR, the
* Async Notif events will no longer be activated on this
* pmport.
*/
"[Existence] %d -> %d", dev_existed_previously,
if (dev_exists_now) {
if (dev_existed_previously) {
/* Link (may) not change: Exist -> Exist * */
"ahci_probe_pmult: port %d:%d "
"device link lost/established",
&sdevice,
} else {
/* Link change: None -> Exist */
"ahci_probe_pmult: port %d:%d "
/* Clear port state */
"ahci_probe_pmult: port %d "
"ahciport_port_state [Cleared].", port);
&sdevice,
}
} else { /* No device exists now */
if (dev_existed_previously) {
/* Link change: Exist -> None */
"ahci_probe_pmult: port %d:%d "
/* An existing device is lost. */
&sdevice,
}
}
} while (port_hotplug_tags != 0);
return (AHCI_SUCCESS);
err: /* R/W PMULT error */
return (AHCI_FAILURE);
}
/*
* Probe and initialize a port multiplier port.
* A port multiplier port could only be initilaizer here.
*
* WARNING!!! ahcictl_mutex should be acquired before the function
* is called.
*/
static int
{
/*
* Check the parent - port multiplier first.
*/
/*
* Parent port multiplier might have been removed. This event will be
* ignored and failure.
*/
"ahci_tran_probe_port: "
"parent device removed, ignore event.", NULL);
return (AHCI_FAILURE);
}
/* The port is ready? */
if (!(port_state & SATA_STATE_READY)) {
"ahci_tran_probe_port: "
"parent port-mult is NOT ready.", NULL);
AHCI_SUCCESS) {
"ahci_tran_probe_port: "
"restart port-mult failed.", NULL);
return (AHCI_FAILURE);
}
}
/*
* If port-mult is restarted due to some reason, we need
* re-initialized the PMult.
*/
if (!(port_state & SATA_DSTATE_PMULT_INIT)) {
/* Initialize registers on a port multiplier */
return (AHCI_FAILURE);
}
/*
* Then we check the port-mult port
*/
/* Is this pmport initialized? */
if (!(port_state & SATA_STATE_READY)) {
/* ahci_initialize_pmport() will set READY state */
return (AHCI_FAILURE);
}
return (AHCI_SUCCESS);
}
/*
* AHCI device reset ...; a single device on one of the ports is reset,
* but the HBA and physical communication remain intact. This is the
* least intrusive.
*
* When issuing a software reset sequence, there should not be other
* commands in the command list, so we will first clear and then re-set
* PxCMD.ST to clear PxCI. And before issuing the software reset,
* the port must be idle and PxTFD.STS.BSY and PxTFD.STS.DRQ must be
* cleared unless command list override (PxCMD.CLO) is supported.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
int slot, loop_count;
int rval = AHCI_FAILURE;
/* First clear PxCMD.ST (AHCI v1.2 10.4.1) */
port) != AHCI_SUCCESS) {
"ahci_software_reset: cannot stop HBA port %d.", port);
goto out;
}
/* Check PxTFD.STS.BSY and PxTFD.STS.DRQ */
if (port_task_file & AHCI_TFD_STS_BSY ||
"PxTFD.STS.BSY/DRQ is set (PxTFD=0x%x), "
"cannot issue a software reset.", port_task_file);
goto out;
}
/*
* If HBA Support CLO, as Command List Override (CAP.SCLO is
* set), PxCMD.CLO bit should be set before set PxCMD.ST, in
* order to clear PxTFD.STS.BSY and PxTFD.STS.DRQ.
*/
"PxTFD.STS.BSY/DRQ is set, try SCLO.", NULL)
/* Waiting till PxCMD.SCLO bit is cleared */
loop_count = 0;
do {
/* Wait for 10 millisec */
/* We are effectively timing out after 1 sec. */
if (loop_count++ > 100) {
"SCLO time out. port %d is busy.", port);
goto out;
}
} while (port_cmd_status & AHCI_CMD_STATUS_CLO);
/* Re-check */
if (port_task_file & AHCI_TFD_STS_BSY ||
"SCLO cannot clear PxTFD.STS.BSY/DRQ (PxTFD=0x%x)",
goto out;
}
}
/* Then start port */
!= AHCI_SUCCESS) {
"ahci_software_reset: cannot start AHCI port %d.", port);
goto out;
}
/*
* When ahci_port.ahciport_mop_in_progress is set, A non-zero
* ahci_port.ahciport_pending_ncq_tags may fail
* ahci_claim_free_slot(). Actually according to spec, by clearing
* PxCMD.ST there is no command outstanding while executing software
* reseting. Hence we directly use slot 0 instead of
* ahci_claim_free_slot().
*/
slot = 0;
/* Now send the first H2D Register FIS with SRST set to 1 */
/* Set Command Header in Command List */
0,
slot * sizeof (ahci_cmd_header_t),
sizeof (ahci_cmd_header_t),
/* Indicate to the HBA that a command is active. */
(0x1 << slot));
loop_count = 0;
/* Loop till the first command is finished */
do {
/* We are effectively timing out after 1 sec. */
if (loop_count++ > AHCI_POLLRATE_PORT_SOFTRESET) {
"the first SRST FIS is timed out, "
"loop_count = %d", loop_count);
goto out;
}
/* Wait for 10 millisec */
"ahci_software_reset: 1st loop count: %d, "
"port_cmd_issue = 0x%x, slot = 0x%x",
/* According to ATA spec, we need wait at least 5 microsecs here. */
/* Now send the second H2D Register FIS with SRST cleard to zero */
/* Set Command Header in Command List */
0,
slot * sizeof (ahci_cmd_header_t),
sizeof (ahci_cmd_header_t),
/* Indicate to the HBA that a command is active. */
(0x1 << slot));
loop_count = 0;
/* Loop till the second command is finished */
do {
/* We are effectively timing out after 1 sec. */
if (loop_count++ > AHCI_POLLRATE_PORT_SOFTRESET) {
"the second SRST FIS is timed out, "
"loop_count = %d", loop_count);
goto out;
}
/* Wait for 10 millisec */
"ahci_software_reset: 2nd loop count: %d, "
"port_cmd_issue = 0x%x, slot = 0x%x",
goto out;
}
rval = AHCI_SUCCESS;
out:
"ahci_software_reset: %s at port %d:%d",
return (rval);
}
/*
* AHCI port reset ...; the physical communication between the HBA and device
* on a port are disabled. This is more intrusive.
*
* When an HBA or port reset occurs, Phy communication is going to
* be re-established with the device through a COMRESET followed by the
* normal out-of-band communication sequence defined in Serial ATA. At
* the end of reset, the device, if working properly, will send a D2H
* Register FIS, which contains the device signature. When the HBA receives
* this FIS, it updates PxTFD.STS and PxTFD.ERR register fields, and updates
* the PxSIG register with the signature.
*
* WARNING!!! ahciport_mutex should be acquired, and PxCMD.ST should be also
* cleared before the function is called.
*/
static int
{
int loop_count;
/* Target is a port multiplier port? */
if (AHCI_ADDR_IS_PMPORT(addrp))
/* Otherwise it must be an HBA port. */
"Port %d port resetting...", port);
/*
* According to the spec, SUD bit should be set here,
* but JMicron JMB363 doesn't follow it, so print
* a debug message.
*/
if (!(port_cmd_status & AHCI_CMD_STATUS_SUD))
"ahci_port_reset: port %d SUD bit not set", port);
/* Enable PxCMD.FRE to read device */
/*
* Give time for COMRESET to percolate, according to the AHCI
* spec, software shall wait at least 1 millisecond before
* clearing PxSCTL.DET
*/
/* Fetch the SCONTROL again and rewrite the DET part with 0 */
/*
* The port enters P:StartComm state, and HBA tells link layer to
* start communication, which involves sending COMRESET to device.
* And the HBA resets PxTFD.STS to 7Fh.
*
* When a COMINIT is received from the device, then the port enters
* P:ComInit state. And HBA sets PxTFD.STS to FFh or 80h. HBA sets
* PxSSTS.DET to 1h to indicate a device is detected but communication
* is not yet established. HBA sets PxSERR.DIAG.X to '1' to indicate
* a COMINIT has been received.
*/
/*
* The DET field is valid only if IPM field indicates
* that the interface is in active state.
*/
loop_count = 0;
do {
/*
* If the interface is not active, the DET field
* is considered not accurate. So we want to
* continue looping.
*/
}
if (loop_count++ > AHCI_POLLRATE_PORT_SSTATUS) {
/*
* We are effectively timing out after 0.1 sec.
*/
break;
}
/* Wait for 10 millisec */
"ahci_port_reset: 1st loop count: %d, "
"port_sstatus = 0x%x port %d",
/*
* Either the port is not active or there
* is no device present.
*/
return (AHCI_SUCCESS);
}
/* Now we can make sure there is a device connected to the port */
/*
* A COMINIT signal is supposed to be received
* PxSERR.DIAG.X or PxIS.PCS should be set
*/
if (!(port_intr_status & AHCI_INTR_STATUS_PCS) &&
!(port_serror & SERROR_EXCHANGED_ERR)) {
"COMINIT signal from the device not received",
return (AHCI_FAILURE);
}
/*
* According to the spec, when PxSCTL.DET is set to 0h, upon
* receiving a COMINIT from the attached device, PxTFD.STS.BSY
* shall be set to '1' by the HBA.
*
* However, we found JMicron JMB363 doesn't follow this, so
* remove this check, and just print a debug message.
*/
#if AHCI_DEBUG
if (!(port_task_file & AHCI_TFD_STS_BSY)) {
"port %d BSY bit is not set after COMINIT signal "
"is received", port);
}
#endif
/*
* PxSERR.DIAG.X has to be cleared in order to update PxTFD with
* the D2H FIS received by HBA.
*/
/*
* Devices should return a FIS contains its signature to HBA after
* COMINIT signal. Check whether a D2H FIS is received by polling
* PxTFD.STS.ERR bit.
*/
loop_count = 0;
do {
/* Wait for 10 millisec */
if (loop_count++ > AHCI_POLLRATE_PORT_TFD_ERROR) {
/*
* We are effectively timing out after 11 sec.
*/
"the device hardware has been initialized and "
"the power-up diagnostics failed",
"port %d PxTFD.STS.ERR is not set, we need another "
"software reset.", port);
/* Clear port serror register for the port */
/* Try another software reset. */
&pmult_addr) != AHCI_SUCCESS) {
return (AHCI_FAILURE);
}
break;
}
/* Wait for 10 millisec */
/*
* The Error bit '1' means COMRESET is finished successfully
* The device hardware has been initialized and the power-up
* diagnostics successfully completed. The device requests
* that the Transport layer transmit a Register - D2H FIS to
* the host. (SATA spec 11.5, v2.6)
*/
} while (((port_task_file & AHCI_TFD_ERR_MASK)
>> AHCI_TFD_ERR_SHIFT) != AHCI_TFD_ERR_SGS);
"ahci_port_reset: 2nd loop count: %d, "
"port_task_file = 0x%x port %d",
/* Clear port serror register for the port */
/* Set port as ready */
"ahci_port_reset: succeed at port %d.", port);
return (AHCI_SUCCESS);
}
/*
* COMRESET on a port multiplier port.
*
* NOTE: Only called in ahci_port_reset()
*/
static int
{
int loop_count;
/* Initialize pmport state */
/* PxCMD.FRE should be set before. */
if (!(port_cmd_status & AHCI_CMD_STATUS_FRE))
return (AHCI_FAILURE);
/*
* Give time for COMRESET to percolate, according to the AHCI
* spec, software shall wait at least 1 millisecond before
* clearing PxSCTL.DET
*/
/*
* Fetch the SCONTROL again and rewrite the DET part with 0
* This will generate an Asychronous Notification events.
*/
/*
* The port enters P:StartComm state, and HBA tells link layer to
* start communication, which involves sending COMRESET to device.
* And the HBA resets PxTFD.STS to 7Fh.
*
* When a COMINIT is received from the device, then the port enters
* P:ComInit state. And HBA sets PxTFD.STS to FFh or 80h. HBA sets
* PxSSTS.DET to 1h to indicate a device is detected but communication
* is not yet established. HBA sets PxSERR.DIAG.X to '1' to indicate
* a COMINIT has been received.
*/
/*
* The DET field is valid only if IPM field indicates
* that the interface is in active state.
*/
loop_count = 0;
do {
/*
* If the interface is not active, the DET field
* is considered not accurate. So we want to
* continue looping.
*/
}
if (loop_count++ > AHCI_POLLRATE_PORT_SSTATUS) {
/*
* We are effectively timing out after 0.1 sec.
*/
break;
}
/* Wait for 10 millisec */
"ahci_pmport_reset: 1st loop count: %d, "
"port_sstatus = 0x%x port %d:%d",
/*
* Either the port is not active or there
* is no device present.
*/
"ahci_pmport_reset: "
"no device attached to port %d:%d",
return (AHCI_SUCCESS);
}
/* Now we can make sure there is a device connected to the port */
/* COMINIT signal is supposed to be received (PxSERR.DIAG.X = '1') */
"COMINIT signal from the device not received port %d:%d",
return (AHCI_FAILURE);
}
/*
* After clear PxSERR register, we will receive a D2H FIS.
* Normally this FIS will cause a IPMS error according to AHCI spec
* v1.2 because there is no command outstanding for it. So we need
* to ignore this error.
*/
/* Now we need to check the D2H FIS by checking IPMS error. */
loop_count = 0;
do {
if (loop_count++ > AHCI_POLLRATE_PORT_TFD_ERROR) {
/*
* No D2H FIS received. This is possible according
* to SATA 2.6 spec.
*/
"PxIS.IPMS is not set, we need another "
break;
}
/* Wait for 10 millisec */
} while (!(port_intr_status & AHCI_INTR_STATUS_IPMS));
"ahci_pmport_reset: 2st loop count: %d, "
"port_sstatus = 0x%x port %d:%d",
/* Clear IPMS */
/* This pmport is now ready for ahci_tran_start() */
return (AHCI_SUCCESS);
err: /* R/W PMULT error */
/* IPMS flags might be set before. */
return (AHCI_FAILURE);
}
/*
* AHCI HBA reset ...; the entire HBA is reset, and all ports are disabled.
* This is the most intrusive.
*
* When an HBA reset occurs, Phy communication will be re-established with
* the device through a COMRESET followed by the normal out-of-band
* communication sequence defined in Serial ATA. At the end of reset, the
* device, if working properly, will send a D2H Register FIS, which contains
* the device signature. When the HBA receives this FIS, it updates PxTFD.STS
* and PxTFD.ERR register fields, and updates the PxSIG register with the
* signature.
*
* Remember to set GHC.AE to 1 before calling ahci_hba_reset.
*/
static int
{
int loop_count;
int rval = AHCI_SUCCESS;
NULL);
/* Setting GHC.HR to 1, remember GHC.AE is already set to 1 before */
/*
* Wait until HBA Reset complete or timeout
*/
loop_count = 0;
do {
if (loop_count++ > AHCI_POLLRATE_HBA_RESET) {
"ahci hba reset is timing out, "
"ghc_control = 0x%x", ghc_control);
/* We are effectively timing out after 1 sec. */
break;
}
/* Wait for 10 millisec */
} while (ghc_control & AHCI_HBA_GHC_HR);
"ahci_hba_reset: 1st loop count: %d, "
if (ghc_control & AHCI_HBA_GHC_HR) {
/* The hba is not reset for some reasons */
"hba reset failed: HBA in a hung or locked state", NULL);
return (AHCI_FAILURE);
}
/*
* HBA reset will clear (AHCI Spec v1.2 10.4.3) GHC.IE / GHC.AE
*/
/* Only check implemented ports */
continue;
}
/* Make sure the drive is spun-up */
AHCI_SUCCESS) {
rval = AHCI_FAILURE;
"ahci_hba_reset: port %d failed", port);
/*
* Set the port state to SATA_PSTATE_FAILED if
* failed to initialize it.
*/
}
}
return (rval);
}
/*
* This routine is only called from AHCI_ATTACH or phyrdy change
* case. It first calls software reset, then stop the port and try to
* read PxSIG register to find the type of device attached to the port.
*
* The caller should make sure a valid device exists on specified port and
* physical communication has been established so that the signature could
* be retrieved by software reset.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called. And the port interrupt is disabled.
*/
static void
{
int rval;
/*
* If the HBA doesn't support port multiplier, then the driver
* doesn't need to bother to check port multiplier device.
*
* The second port of ICH7 on ASUS P5W DH deluxe motherboard is
* connected to Silicon Image 4723, to which the two sata drives
* attached can be set with RAID1, RAID0 or Spanning mode.
*
* We found software reset will get failure if port multiplier address
* 0xf is used by software reset, so just ignore the check since
* ICH7 doesn't support port multiplier device at all.
*/
if (AHCI_ADDR_IS_PORT(addrp) &&
"ahci_find_dev_signature enter: port %d", port);
/*
* NOTE: when the ahci address is a HBA port, we do not know
* it is a device or a port multiplier that attached. we need
* try a software reset at port multiplier address (0xf
* pmport)
*/
} else {
"ahci_find_dev_signature enter: port %d:%d",
}
/* Assume it is unknown. */
/* Issue a software reset to get the signature */
if (rval != AHCI_SUCCESS) {
/*
* Try to do software reset again with pmport set with 0 if
* the controller is set with AHCI_CAP_SRST_NO_HOSTPORT and
* the original pmport is set with SATA_PMULT_HOSTPORT (0xf)
*/
&dev_addr);
}
if (rval != AHCI_SUCCESS) {
"ahci_find_dev_signature: software reset failed "
"at port %d:%d, cannot get signature.",
return;
}
}
/*
* ahci_software_reset has started the port, so we need manually stop
* the port again.
*/
if (AHCI_ADDR_IS_PORT(addrp)) {
"ahci_find_dev_signature: cannot stop port %d.",
port);
return;
}
}
/* Now we can make sure that a valid signature is received. */
if (AHCI_ADDR_IS_PMPORT(addrp)) {
"ahci_find_dev_signature: signature = 0x%x at port %d:%d",
} else {
"ahci_find_dev_signature: signature = 0x%x at port %d",
}
/* NOTE: Only support ATAPI device at controller port. */
switch (signature) {
case AHCI_SIGNATURE_DISK:
"Disk is found at port: %d", port);
break;
case AHCI_SIGNATURE_ATAPI:
"ATAPI device is found at port: %d", port);
break;
/* Port Multiplier cannot recursively attached. */
"Port Multiplier is found at port: %d", port);
break;
default:
"Unknown device is found at port: %d", port);
}
}
/*
* According to the spec, to reliably detect hot plug removals, software
* must disable interface power management. Software should perform the
* following initialization on a port after a device is attached:
* Set PxSCTL.IPM to 3h to disable interface state transitions
* Set PxCMD.ALPE to '0' to disable aggressive power management
* Disable device initiated interface power management by SET FEATURE
*
* We can ignore the last item because by default the feature is disabled
*/
static void
{
}
/*
* Start the port - set PxCMD.ST to 1, if PxCMD.FRE is not set
* to 1, then set it firstly.
*
* Each port contains two major DMA engines. One DMA engine walks through
* the command list, and is controlled by PxCMD.ST. The second DMA engine
* copies received FISes into system memory, and is controlled by PxCMD.FRE.
*
* Software shall not set PxCMD.ST to '1' until it verifies that PxCMD.CR
* is '0' and has set PxCMD.FRE is '1'. And software shall not clear
* PxCMD.FRE while PxCMD.ST or PxCMD.CR is set '1'.
*
* Software shall not set PxCMD.ST to '1' unless a functional device is
* present on the port(as determined by PxTFD.STS.BSY = '0',
* PxTFD.STS.DRQ = '0', and PxSSTS.DET = 3h).
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
"the state for port %d is 0x%x",
return (AHCI_FAILURE);
}
"no device is attached at port %d", port);
return (AHCI_FAILURE);
}
/* First to set PxCMD.FRE before setting PxCMD.ST. */
if (!(port_cmd_status & AHCI_CMD_STATUS_FRE)) {
}
"PxCMD.ST set to '1' at port %d", port);
return (AHCI_SUCCESS);
}
/*
* Setup PxCLB, PxCLBU, PxFB, and PxFBU for particular port. First, we need
* to make sure PxCMD.ST, PxCMD.CR, PxCMD.FRE, and PxCMD.FR are all cleared.
* Then set PxCLB, PxCLBU, PxFB, and PxFBU.
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
/* Step 1: Make sure both PxCMD.ST and PxCMD.CR are cleared. */
port) != AHCI_SUCCESS)
return (AHCI_FAILURE);
}
/* Step 2: Make sure both PxCMD.FRE and PxCMD.FR are cleared. */
int loop_count = 0;
/* Clear PxCMD.FRE */
/* Wait until PxCMD.FR is cleared */
for (;;) {
if (!(port_cmd_status & AHCI_CMD_STATUS_FR))
break;
if (loop_count++ >= AHCI_POLLRATE_PORT_IDLE_FR) {
"ahci_setup_port_base_addresses: cannot "
"clear PxCMD.FR for port %d.", port);
/*
* We are effectively timing out after 0.5 sec.
* This value is specified in AHCI spec.
*/
return (AHCI_FAILURE);
}
/* Wait for 1 millisec */
}
}
/* Step 3: Config Port Command List Base Address */
/* Step 4: Config Port Received FIS Base Address */
return (AHCI_SUCCESS);
}
/*
* Allocate the ahci_port_t including Received FIS and Command List.
* The argument - port is the physical port number, and not logical
* port number seen by the SATA framework.
*
* WARNING!!! ahcictl_mutex should be acquired before the function
* is called.
*/
static int
{
/* Initialize the port condition variable */
/* Initialize the port mutex */
/*
* Allocate memory for received FIS structure and
* command list for this port
*/
goto err_case1;
}
goto err_case2;
}
/* Setup PxCMD.CLB, PxCMD.CLBU, PxCMD.FB, and PxCMD.FBU */
AHCI_SUCCESS) {
goto err_case3;
}
"_port%d", port);
/* Create the taskq for the port */
goto err_case3;
}
/* Allocate the argument for the taskq */
goto err_case4;
/* Initialize the done queue */
ahci_portp->ahciport_doneq_len = 0;
return (AHCI_SUCCESS);
return (AHCI_FAILURE);
}
/*
* Reverse of ahci_alloc_port_state().
*
* WARNING!!! ahcictl_mutex should be acquired before the function
* is called.
*/
static void
{
sizeof (ahci_addr_t));
}
/*
* Allocates memory for the Received FIS Structure
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
rcvd_fis_size = sizeof (ahci_rcvd_fis_t);
/* allocate rcvd FIS dma handle. */
NULL,
DDI_SUCCESS) {
"rcvd FIS dma handle alloc failed", NULL);
return (AHCI_FAILURE);
}
&accattr,
NULL,
&ret_len,
"rcvd FIS dma mem alloc fail", NULL);
/* error.. free the dma handle. */
return (AHCI_FAILURE);
}
NULL,
NULL,
&cookie_count) != DDI_DMA_MAPPED) {
"rcvd FIS dma handle bind fail", NULL);
/* error.. free the dma handle & free the memory. */
return (AHCI_FAILURE);
}
return (AHCI_SUCCESS);
}
/*
* Deallocates the Received FIS Structure
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static void
{
/* Unbind the cmd list dma handle first. */
/* Then free the underlying memory. */
/* Now free the handle itself. */
}
/*
* Allocates memory for the Command List, which contains up to 32 entries.
* Each entry contains a command header, which is a 32-byte structure that
* includes the pointer to the command table.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
/* allocate cmd list dma handle. */
NULL,
"cmd list dma handle alloc failed", NULL);
return (AHCI_FAILURE);
}
&accattr,
NULL,
&ret_len,
"cmd list dma mem alloc fail", NULL);
/* error.. free the dma handle. */
return (AHCI_FAILURE);
}
NULL,
NULL,
&cookie_count) != DDI_DMA_MAPPED) {
"cmd list dma handle bind fail", NULL);
/* error.. free the dma handle & free the memory. */
return (AHCI_FAILURE);
}
goto err_out;
}
return (AHCI_SUCCESS);
/* Unbind the cmd list dma handle first. */
/* Then free the underlying memory. */
/* Now free the handle itself. */
return (AHCI_FAILURE);
}
/*
* Deallocates the Command List
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static void
{
/* First dealloc command table */
/* Unbind the cmd list dma handle first. */
/* Then free the underlying memory. */
/* Now free the handle itself. */
}
/*
* Allocates memory for all Command Tables, which contains Command FIS,
* ATAPI Command and Physical Region Descriptor Table.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
int slot;
"ahci_alloc_cmd_tables: port %d enter",
/* Allocate cmd table dma handle. */
NULL,
DDI_SUCCESS) {
"cmd table dma handle alloc failed", NULL);
goto err_out;
}
if (ddi_dma_mem_alloc(
&accattr,
NULL,
&ret_len,
NULL) {
"cmd table dma mem alloc fail", NULL);
/* error.. free the dma handle. */
goto err_out;
}
NULL,
NULL,
&cookie_count) != DDI_DMA_MAPPED) {
"cmd table dma handle bind fail", NULL);
/* error.. free the dma handle & free the memory. */
goto err_out;
}
/* Config Port Command Table Base Address */
#ifndef __lock_lint
#endif
}
return (AHCI_SUCCESS);
/* Unbind the cmd table dma handle first */
(void) ddi_dma_unbind_handle(
/* Then free the underlying memory */
/* Now free the handle itself */
}
return (AHCI_FAILURE);
}
/*
* Deallocates memory for all Command Tables.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static void
{
int slot;
"ahci_dealloc_cmd_tables: %d enter",
/* Unbind the cmd table dma handle first. */
(void) ddi_dma_unbind_handle(
/* Then free the underlying memory. */
/* Now free the handle itself. */
}
}
/*
* Update SATA registers at controller ports
*
* WARNING!!! ahciport_mutex should be acquired before those functions
* get called.
*/
static void
{
}
/*
* For poll mode, ahci_port_intr will be called to emulate the interrupt
*/
static void
{
"ahci_port_intr enter: port %d", port);
/* For SATA_OPMODE_POLLING commands */
} else {
/*
* port_intr_enable indicates that the corresponding interrrupt
* reporting is enabled.
*/
}
/* IPMS error in port reset should be ignored according AHCI spec. */
/*
* port_intr_stats indicates that the corresponding interrupt
* condition is active.
*/
"ahci_port_intr: port %d, port_intr_status = 0x%x, "
"port_intr_enable = 0x%x",
/*
* Pending interrupt events are indicated by the PxIS register.
* Make sure we don't miss any event.
*/
return;
}
/* First clear the port interrupts status */
/* Check the completed non-queued commands */
if (port_intr_status & (AHCI_INTR_STATUS_DHRS |
(void) ahci_intr_cmd_cmplt(ahci_ctlp,
ahci_portp, port);
}
/* Check the completed queued commands */
if (port_intr_status & AHCI_INTR_STATUS_SDBS) {
(void) ahci_intr_set_device_bits(ahci_ctlp,
ahci_portp, port);
}
/* Check the port connect change status interrupt bit */
if (port_intr_status & AHCI_INTR_STATUS_PCS) {
ahci_portp, port);
}
/* Check the device mechanical presence status interrupt bit */
if (port_intr_status & AHCI_INTR_STATUS_DMPS) {
}
/* Check the PhyRdy change status interrupt bit */
if (port_intr_status & AHCI_INTR_STATUS_PRCS) {
port);
}
/*
* Check the non-fatal error interrupt bits, there are four
* kinds of non-fatal errors at the time being:
*
* PxIS.UFS - Unknown FIS Error
* PxIS.OFS - Overflow Error
* PxIS.INFS - Interface Non-Fatal Error
* PxIS.IPMS - Incorrect Port Multiplier Status Error
*
* For these non-fatal errors, the HBA can continue to operate,
* so the driver just log the error messages.
*/
if (port_intr_status & (AHCI_INTR_STATUS_UFS |
}
/*
* Check the fatal error interrupt bits, there are four kinds
* of fatal errors for AHCI controllers:
*
* PxIS.HBFS - Host Bus Fatal Error
* PxIS.HBDS - Host Bus Data Error
* PxIS.IFS - Interface Fatal Error
* PxIS.TFES - Task File Error
*
* The fatal error means the HBA can not recover from it by
* itself, and it will try to abort the transfer, and the software
* must intervene to restart the port.
*/
if (port_intr_status & (AHCI_INTR_STATUS_IFS |
/* Check the cold port detect interrupt bit */
if (port_intr_status & AHCI_INTR_STATUS_CPDS) {
}
/* Second clear the corresponding bit in IS.IPS */
/* Try to recover at the end of the interrupt handler. */
DDI_FM_OK) {
}
}
/*
* Interrupt service handler
*/
static uint_t
{
#ifndef __lock_lint
#endif
/* LINTED */
/*
* global_intr_status indicates that the corresponding port has
* an interrupt pending.
*/
/* The interrupt is not ours */
return (DDI_INTR_UNCLAIMED);
}
/*
* Check the handle after reading global_intr_status - we don't want
* to miss any port with pending interrupts.
*/
DDI_FM_OK) {
return (DDI_INTR_UNCLAIMED);
}
/* Loop for all the ports */
continue;
}
continue;
}
/* Call ahci_port_intr */
}
return (DDI_INTR_CLAIMED);
}
/*
* For non-queued commands, when the corresponding bit in the PxCI register
* is cleared, it means the command is completed successfully. And according
* to the HBA state machine, there are three conditions which possibly will
* try to clear the PxCI register bit.
* 1. Receive one D2H Register FIS which is with 'I' bit set
* 2. Update PIO Setup FIS
* 3. Transmit a command and receive R_OK if CTBA.C is set (software reset)
*
* Process completed non-queued commands when the interrupt status bit -
* AHCI_INTR_STATUS_DHRS or AHCI_INTR_STATUS_PSS is set.
*
* AHCI_INTR_STATUS_DHRS means a D2H Register FIS has been received
* with the 'I' bit set. And the following commands will send thus
* FIS with 'I' bit set upon the successful completion:
* 1. Non-data commands
* 2. DMA data-in command
* 3. DMA data-out command
* 4. PIO data-out command
* 5. PACKET non-data commands
* 6. PACKET PIO data-in command
* 7. PACKET PIO data-out command
* 8. PACKET DMA data-in command
* 9. PACKET DMA data-out command
*
* AHCI_INTR_STATUS_PSS means a PIO Setup FIS has been received
* with the 'I' bit set. And the following commands will send this
* FIS upon the successful completion:
* 1. PIO data-in command
*/
static int
{
uint32_t port_cmd_issue = 0;
int finished_slot;
#if AHCI_DEBUG
#endif
if (!ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
/*
* Spurious interrupt. Nothing to be done.
*/
return (AHCI_SUCCESS);
}
/* If the PxCI corrupts, don't complete the commmands. */
!= DDI_FM_OK) {
return (AHCI_FAILURE);
}
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
/* Slot 0 is always used during error recovery */
"ahci_intr_cmd_cmplt: port %d the sata pkt for error "
"retrieval is finished, and finished_tags = 0x%x",
} else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
"ahci_intr_cmd_cmplt: port %d the sata pkt for r/w "
"port multiplier is finished, and finished_tags = 0x%x",
} else {
}
"ahci_intr_cmd_cmplt: pending_tags = 0x%x, "
"port_cmd_issue = 0x%x finished_tags = 0x%x",
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
(finished_tags == 0x1)) {
"ahci_intr_cmd_cmplt: sending up pkt 0x%p "
"with SATA_PKT_COMPLETED", (void *)satapkt);
goto out;
}
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp) &&
(finished_tags == 0x1)) {
"ahci_intr_cmd_cmplt: sending up pkt 0x%p "
"with SATA_PKT_COMPLETED", (void *)satapkt);
/* READ PORTMULT need copy out FIS content. */
}
goto out;
}
while (finished_tags) {
if (finished_slot == -1) {
goto out;
}
#if AHCI_DEBUG
/*
* For non-native queued commands, the PRD byte count field
* shall contain an accurate count of the number of bytes
* transferred for the command before the PxCI bit is cleared
* to '0' for the command.
*
* The purpose of this field is to let software know how many
* bytes transferred for a given operation in order to
* determine if underflow occurred. When issuing native command
* queuing commands, this field should not be used and is not
* required to be valid since in this case underflow is always
* illegal.
*
* For data reads, the HBA will update its PRD byte count with
* the total number of bytes received from the last FIS, and
* may be able to continue normally. For data writes, the
* device will detect an error, and HBA most likely will get
* a fatal error.
*
* Therefore, here just put code to debug part. And please
* refer to the comment above ahci_intr_fatal_error for the
* definition of underflow error.
*/
if (cmd_dmacount) {
"ahci_intr_cmd_cmplt: port %d, "
"PRD Byte Count = 0x%x, "
"ahciport_prd_bytecounts = 0x%x", port,
"ahci_intr_cmd_cmplt: port %d, "
"an underflow occurred", port);
}
}
#endif
/*
* For SATAC_SMART command with SATA_SMART_RETURN_STATUS
* feature, sata_special_regs flag will be set, and the
* driver should copy the status and the other corresponding
* register values in the D2H Register FIS received (It's
* working on Non-data protocol) from the device back to
* the sata_cmd.
*
* For every AHCI port, there is only one Received FIS
* structure, which contains the FISes received from the
* device, So we're trying to copy the content of D2H
* Register FIS in the Received FIS structure back to
* the sata_cmd.
*/
}
"ahci_intr_cmd_cmplt: sending up pkt 0x%p "
"with SATA_PKT_COMPLETED", (void *)satapkt);
}
out:
"ahci_intr_cmd_cmplt: pending_tags = 0x%x",
return (AHCI_SUCCESS);
}
/*
* AHCI_INTR_STATUS_SDBS means a Set Device Bits FIS has been received
* with the 'I' bit set and has been copied into system memory. It will
* be sent under the following situations:
*
* 1. NCQ command is completed
*
* via the Set Device Bits FIS. When such event is generated, the software
* needs to read PxSACT register and compares the current value to the
* list of commands previously issue by software. ahciport_pending_ncq_tags
* keeps the tags of previously issued commands.
*
* 2. Asynchronous Notification
*
* Asynchronous Notification is a feature in SATA spec 2.6.
*
* 1) ATAPI device will send a signal to the host when media is inserted or
* removed and avoids polling the device for media changes. The signal
* sent to the host is a Set Device Bits FIS with the 'I' and 'N' bits
* set to '1'. At the moment, it's not supported yet.
*
* 2) Port multiplier will send a signal to the host when a hot plug event
* has occured on a port multiplier port. It is used when command based
* switching is employed. This is handled by ahci_intr_pmult_sntf_events()
*/
static int
{
"ahci_intr_set_device_bits enter: port %d", port);
/* Initialize HBA port address */
/* NCQ plug handler */
/* Check port multiplier's asynchronous notification events */
(void) ahci_intr_pmult_sntf_events(ahci_ctlp,
ahci_portp, port);
}
/* ATAPI events is not supported yet */
return (AHCI_SUCCESS);
}
/*
* NCQ interrupt handler. Called upon a NCQ command is completed.
* Only be called from ahci_intr_set_device_bits().
*
* WARNING!!! ahciport_mutex should be acquired before the function is
* called.
*/
static int
{
int issued_slot;
int finished_slot;
"ahci_intr_set_device_bits enter: port %d", port);
if (!NCQ_CMD_IN_PROGRESS(ahci_portp)) {
return (AHCI_SUCCESS);
}
/*
* First the handler got which commands are finished by checking
* PxSACT register
*/
"ahci_intr_set_device_bits: port %d pending_ncq_tags = 0x%x "
"port_sactive = 0x%x", port,
"ahci_intr_set_device_bits: finished_tags = 0x%x", finished_tags);
/*
* For NCQ commands, the software can determine which command has
* already been transmitted to the device by checking PxCI register.
*/
!= DDI_FM_OK) {
return (AHCI_FAILURE);
}
"ahci_intr_set_device_bits: port %d pending_tags = 0x%x "
"port_cmd_issue = 0x%x", port,
"ahci_intr_set_device_bits: issued_tags = 0x%x", issued_tags);
/*
* Clear ahciport_pending_tags bit when the corresponding command
* is already sent down to the device.
*/
while (issued_tags) {
if (issued_slot == -1) {
goto next;
}
}
next:
while (finished_tags) {
if (finished_slot == -1) {
goto out;
}
/* The command is certainly transmitted to the device */
(0x1 << finished_slot)));
"ahci_intr_set_device_bits: sending up pkt 0x%p "
"with SATA_PKT_COMPLETED", (void *)satapkt);
}
out:
"ahci_intr_set_device_bits: port %d "
"pending_ncq_tags = 0x%x pending_tags = 0x%x",
return (AHCI_SUCCESS);
}
/*
* Port multiplier asynchronous notification event handler. Called upon a
*
* The async-notification event will only be recorded by ahcipmi_snotif_tags
* here and will be handled by ahci_probe_pmult().
*
* NOTE: called only from ahci_port_intr().
*/
static int
{
"ahci_intr_pmult_sntf_events enter: port %d ", port);
/* no hot-plug while attaching process */
return (AHCI_SUCCESS);
}
return (AHCI_SUCCESS);
}
return (AHCI_SUCCESS);
}
/* Port Multiplier sub-device hot-plug handler */
if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
return (AHCI_SUCCESS);
}
/* Not allowed to re-enter. */
return (AHCI_SUCCESS);
}
/*
* NOTE:
* Even if Asynchronous Notification is supported (and enabled) by
* both controller and the port multiplier, the content of PxSNTF
* register is always set to 0x8000 by async notification event. We
* need to check GSCR[32] on the port multiplier to find out the
* owner of this event.
* This is not accord with SATA spec 2.6 and needs further
* clarification.
*/
/* hot-plug will not reported while reseting. */
"port %d snotif event ignored", port);
return (AHCI_SUCCESS);
}
"PxSNTF is set to 0x%x by port multiplier",
/*
* Now we need do some necessary operation and inform SATA framework
*/
/* Just reject packets, do not stop that port. */
&sdevice,
return (AHCI_SUCCESS);
}
/*
* 1=Change in Current Connect Status. 0=No change in Current Connect Status.
* This bit reflects the state of PxSERR.DIAG.X. This bit is only cleared
* when PxSERR.DIAG.X is cleared. When PxSERR.DIAG.X is set to one, it
* indicates a COMINIT signal was received.
*
* Hot plug insertion is detected by reception of a COMINIT signal from the
* device. On reception of unsolicited COMINIT, the HBA shall generate a
* COMRESET. If the COMINIT is in responce to a COMRESET, then the HBA shall
* begin the normal communication negotiation sequence as outlined in the
* Serial ATA 1.0a specification. When a COMRESET is sent to the device the
* PxSSTS.DET field shall be cleared to 0h. When a COMINIT is received, the
* PxSSTS.DET field shall be set to 1h. When the communication negotiation
* sequence is complete and PhyRdy is true the PxSSTS.DET field shall be set
* to 3h. Therefore, at the moment the ahci driver is going to check PhyRdy
* to handle hot plug insertion. In this interrupt handler, just do nothing
* but print some log message and clear the bit.
*/
static int
{
#if AHCI_DEBUG
#endif
#if AHCI_DEBUG
"ahci_intr_port_connect_change: port %d, "
#endif
/* Clear PxSERR.DIAG.X to clear the interrupt bit */
return (AHCI_SUCCESS);
}
/*
* Hot Plug Operation for platforms that support Mechanical Presence
* Switches.
*
* When set, it indicates that a mechanical presence switch attached to this
* port has been opened or closed, which may lead to a change in the connection
* state of the device. This bit is only valid if both CAP.SMPS and PxCMD.MPSP
* are set to '1'.
*
* At the moment, this interrupt is not needed and disabled and we just log
* the debug message.
*/
static int
{
"ahci_intr_device_mechanical_presence_status enter, "
"port %d", port);
if (!(cap_status & AHCI_HBA_CAP_SMPS) ||
!(port_cmd_status & AHCI_CMD_STATUS_MPSP)) {
"CAP.SMPS or PxCMD.MPSP is not set, so just ignore "
"the interrupt: cap_status = 0x%x, "
return (AHCI_SUCCESS);
}
#if AHCI_DEBUG
if (port_cmd_status & AHCI_CMD_STATUS_MPSS) {
"The mechanical presence switch is open: "
"port %d, port_cmd_status = 0x%x",
} else {
"The mechanical presence switch is close: "
"port %d, port_cmd_status = 0x%x",
}
#endif
return (AHCI_SUCCESS);
}
/*
* Native Hot Plug Support.
*
* When set, it indicates that the internal PHYRDY signal changed state.
* This bit reflects the state of PxSERR.DIAG.N.
*
* There are three kinds of conditions to generate this interrupt event:
* 1. a device is inserted
* 2. a device is disconnected
* management state
*
* If inteface power management is enabled for a port, the PxSERR.DIAG.N
* bit may be set due to the link entering the Partial or Slumber power
* management state, rather than due to a hot plug insertion or removal
* event. So far, the interface power management is disabled, so the
* driver can reliably get removal detection notification via the
* PxSERR.DIAG.N bit.
*/
static int
{
int dev_exists_now = 0;
int dev_existed_previously = 0;
"ahci_intr_phyrdy_change enter, port %d", port);
/* Clear PxSERR.DIAG.N to clear the interrupt bit */
(ahci_portp == NULL)) {
/* The whole controller setup is not yet done. */
return (AHCI_SUCCESS);
}
/* SStatus tells the presence of device. */
dev_exists_now = 1;
}
}
"ahci_intr_phyrdy_change: port %d "
"AHCI_PORT_FLAG_NODEV is cleared", port);
if (dev_exists_now == 0)
}
if (dev_exists_now) {
if (dev_existed_previously) { /* 1 -> 1 */
/* Things are fine now. The loss was temporary. */
"ahci_intr_phyrdy_change port %d "
"device link lost/established", port);
&sdevice,
} else { /* 0 -> 1 */
"ahci_intr_phyrdy_change: port %d "
"device link established", port);
/*
* A new device has been detected. The new device
* might be a port multiplier instead of a drive, so
* we cannot update the signature directly.
*/
(void) ahci_initialize_port(ahci_ctlp,
ahci_portp, &port_addr);
/* Try to start the port */
!= AHCI_SUCCESS) {
"ahci_intr_phyrdy_change: port %d failed "
"at start port", port);
}
/* Clear the max queue depth for inserted device */
&sdevice,
}
} else { /* No device exists now */
if (dev_existed_previously) { /* 1 -> 0 */
"ahci_intr_phyrdy_change: port %d "
"device link lost", port);
ahci_portp, port);
if (ahci_portp->ahciport_device_type ==
}
/* An existing device is lost. */
&sdevice,
}
}
return (AHCI_SUCCESS);
}
/*
* PxIS.UFS - Unknown FIS Error
*
* This interrupt event means an unknown FIS was received and has been
* copied into system memory. An unknown FIS is not considered an illegal
* FIS, unless the length received is more than 64 bytes. If an unknown
* FIS arrives with length <= 64 bytes, it is posted and the HBA continues
* normal operation. If the unknown FIS is more than 64 bytes, then it
* won't be posted to memory and PxSERR.ERR.P will be set, which is then
* a fatal error.
*
* PxIS.IPMS - Incorrect Port Multiplier Status
*
* IPMS Indicates that the HBA received a FIS from a device that did not
* have a command outstanding. The IPMS bit may be set during enumeration
* of devices on a Port Multiplier due to the normal Port Multiplier
* enumeration process. It is recommended that IPMS only be used after
* enumeration is complete on the Port Multiplier (copied from spec).
*
* PxIS.OFS - Overflow Error
*
* Command list overflow is defined as software building a command table
* that has fewer total bytes than the transaction given to the device.
* On device writes, the HBA will run out of data, and on reads, there
* will be no room to put the data.
*
* For an overflow on data read, either PIO or DMA, the HBA will set
* PxIS.OFS, and the HBA will do a best effort to continue, and it's a
* non-fatal error when the HBA can continues. Sometimes, it will cause
* a fatal error and need the software to do something.
*
* For an overflow on data write, setting PxIS.OFS is optional for both
* DMA and PIO, and it's a fatal error, and a COMRESET is required by
* software to clean up from this serious error.
*
* PxIS.INFS - Interface Non-Fatal Error
*
* This interrupt event indicates that the HBA encountered an error on
* the Serial ATA interface but was able to continue operation. The kind
* of error usually occurred during a non-Data FIS, and under this condition
* the FIS will be re-transmitted by HBA automatically.
*
* When the FMA is implemented, there should be a stat structure to
* record how many every kind of error happens.
*/
static int
{
#if AHCI_DEBUG
int current_slot;
#endif
"ahci_intr_non_fatal_error: port %d, "
if (intr_status & AHCI_INTR_STATUS_UFS) {
"ahci port %d has unknown FIS error", port);
/* Clear the interrupt bit by clearing PxSERR.DIAG.F */
}
#if AHCI_DEBUG
if (intr_status & AHCI_INTR_STATUS_IPMS) {
"has Incorrect Port Multiplier Status error", port);
}
if (intr_status & AHCI_INTR_STATUS_OFS) {
"ahci port %d has overflow error", port);
}
if (intr_status & AHCI_INTR_STATUS_INFS) {
"ahci port %d has interface non fatal error", port);
}
/*
* Record the error occurred command's slot.
*/
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
ASSERT(current_slot == 0);
} else {
}
"ahci_intr_non_fatal_error: pending_tags = 0x%x "
"ahci_intr_non_fatal_error: port %d, "
"satapkt 0x%p is being processed when error occurs",
/*
* PRD Byte Count field of command header is not
* required to reflect the total number of bytes
* transferred when an overflow occurs, so here
* just log the value.
*/
if (cmd_dmacount) {
cmd_header = &ahci_portp->
"ahci_intr_non_fatal_error: port %d, "
"PRD Byte Count = 0x%x, "
"ahciport_prd_bytecounts = 0x%x", port,
}
}
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
/*
* For queued command, list those command which have already
* been transmitted to the device and still not completed.
*/
"ahci_intr_non_fatal_error: pending_ncq_tags = 0x%x "
"port_sactive = 0x%x port_cmd_issue = 0x%x",
while (current_tags) {
if (current_slot == -1) {
goto out;
}
ahci_ctlp, "ahci_intr_non_fatal_error: "
"port %d, satapkt 0x%p is outstanding when "
}
}
out:
#endif
return (AHCI_SUCCESS);
}
/*
* According to the AHCI spec, the error types include system memory
* errors, interface errors, port multiplier errors, device errors,
* command list overflow, command list underflow, native command
* queuing tag errors and pio data transfer errors.
*
* System memory errors such as target abort, master abort, and parity
* may cause the host to stop, and they are serious errors and needed
* to be recovered with software intervention. When system software
* has given a pointer to the HBA that doesn't exist in physical memory,
* data error such as CRC or parity occurs, the HBA aborts the transfer
* (if necessary) and PxIS.HBDS will be set.
*
* Interface errors are errors that occur due to electrical issues on
* the interface, or protocol miscommunication between the device and
* HBA, and the respective PxSERR register bit will be set. And PxIS.IFS
* (fatal) or PxIS.INFS (non-fatal) will be set. The conditions that
* 1. in PxSERR.ERR, P bit is set to '1'
* 2. in PxSERR.DIAG, C or H bit is set to '1'
* 3. PhyRdy drop unexpectly, N bit is set to '1'
* If the error occurred during a non-data FIS, the FIS must be
* retransmitted, and the error is non-fatal and PxIS.INFS is set. If
* the error occurred during a data FIS, the transfer will stop, so
* the error is fatal and PxIS.IFS is set.
*
* When a FIS arrives that updates the taskfile, the HBA checks to see
* if PxTFD.STS.ERR is set. If yes, PxIS.TFES will be set and the HBA
* stops processing any more commands.
*
* Command list overflow is defined as software building a command table
* that has fewer total bytes than the transaction given to the device.
* On device writes, the HBA will run out of data, and on reads, there
* will be no room to put the data. For an overflow on data read, either
* PIO or DMA, the HBA will set PxIS.OFS, and it's a non-fatal error.
* For an overflow on data write, setting PxIS.OFS is optional for both
* DMA and PIO, and a COMRESET is required by software to clean up from
* this serious error.
*
* Command list underflow is defined as software building a command
* table that has more total bytes than the transaction given to the
* device. For data writes, both PIO and DMA, the device will detect
* an error and end the transfer. And these errors are most likely going
* to be fatal errors that will cause the port to be restarted. For
* data reads, the HBA updates its PRD byte count, and may be
* able to continue normally, but is not required to. And The HBA is
* not required to detect underflow conditions for native command
* queuing command.
*
* The HBA does not actively check incoming DMA Setup FISes to ensure
* that the PxSACT register bit for that slot is set. Existing error
* mechanisms, such as host bus failure, or bad protocol, are used to
* recover from this case.
*
* In accordance with Serial ATA 1.0a, DATA FISes prior to the final
* DATA FIS must be an integral number of Dwords. If the HBA receives
* a request which is not an integral number of Dwords, the HBA
* set PxSERR.ERR.P to '1', set PxIS.IFS to '1' and stop running until
* software restarts the port. And the HBA ensures that the size
* of the DATA FIS received during a PIO command matches the size in
* the Transfer Cound field of the preceding PIO Setup FIS, if not, the
* HBA sets PxSERR.ERR.P to '1', set PxIS.IFS to '1', and then
* stop running until software restarts the port.
*/
/*
* the fatal errors include PxIS.IFS, PxIS.HBDS, PxIS.HBFS and PxIS.TFES.
*
* PxIS.IFS indicates that the hba encountered an error on the serial ata
* interface which caused the transfer to stop.
*
* PxIS.HBDS indicates that the hba encountered a data error
*
* PxIS.HBFS indicates that the hba encountered a host bus error that it
* cannot recover from, such as a bad software pointer.
*
* PxIS.TFES is set whenever the status register is updated by the device
* and the error bit (bit 0) is set.
*/
static int
{
int failed_slot;
uint32_t failed_tags = 0;
int task_fail_flag = 0, task_abort_flag = 0;
/*
* ahci_intr_phyrdy_change() may have rendered it to
* SATA_DTYPE_NONE.
*/
"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) {
"ahci_intr_fatal_error: port %d "
task_fail_flag = 1;
if (err_byte == SATA_ERROR_ABORT)
task_abort_flag = 1;
}
/*
* Here we just log the fatal error info in interrupt context.
* Misc recovery processing will be handled in task queue.
*/
if (task_fail_flag == 1) {
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
/*
* Read PxCMD.CCS to determine the slot that the HBA
* was processing when the error occurred.
*/
"ahci_intr_fatal_error: spkt 0x%p is being "
"processed when fatal error occurred for port %d",
/*
* Won't emit the error message if it is an IDENTIFY
* DEVICE command sent to an ATAPI device.
*/
SATAC_ID_DEVICE) &&
(task_abort_flag == 1))
goto out1;
/*
* Won't emit the error message if it is an ATAPI PACKET
* command
*/
goto out1;
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
}
}
/* print the fatal error type */
/* print PxSERR related error message */
/* print task file register value */
if (task_fail_flag == 1) {
if (task_abort_flag == 1) {
}
}
out1:
/* Prepare the argument for the taskq */
/* Start the taskq to handle error recovery */
}
out0:
return (AHCI_SUCCESS);
}
/*
* Hot Plug Operation for platforms that support Cold Presence Detect.
*
* When set, a device status has changed as detected by the cold presence
* detect logic. This bit can either be set due to a non-connected port
* receiving a device, or a connected port having its device removed.
* This bit is only valid if the port supports cold presence detect as
* indicated by PxCMD.CPD set to '1'.
*
* At the moment, this interrupt is not needed and disabled and we just
* log the debug message.
*/
static int
{
"ahci_intr_cold_port_detect enter, port %d", port);
if (!(port_cmd_status & AHCI_CMD_STATUS_CPD)) {
"port %d does not support cold presence detect, so "
"we just ignore this interrupt", port);
return (AHCI_SUCCESS);
}
"port %d device status has changed", port);
if (port_cmd_status & AHCI_CMD_STATUS_CPS) {
"port %d: a device is hot plugged", port);
&sdevice,
} else {
"port %d: a device is hot unplugged", port);
&sdevice,
}
return (AHCI_SUCCESS);
}
/*
* Enable the interrupts for a particular port.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static void
{
"ahci_enable_port_intrs enter, port %d", port);
/*
* Clear port interrupt status before enabling interrupt
*/
/*
* Clear the pending bit from IS.IPS
*/
/*
* Enable the following interrupts:
* Device to Host Register FIS Interrupt (DHRS)
* PIO Setup FIS Interrupt (PSS)
* Set Device Bits Interrupt (SDBS)
* Unknown FIS Interrupt (UFS)
* Port Connect Change Status (PCS)
* PhyRdy Change Status (PRCS)
* Overflow Status (OFS)
* Interface Non-fatal Error Status (INFS)
* Interface Fatal Error Status (IFS)
* Host Bus Data Error Status (HBDS)
* Host Bus Fatal Error Status (HBFS)
* Task File Error Status (TFES)
*/
}
/*
* Enable interrupts for all the ports.
*
* WARNING!!! ahcictl_mutex should be acquired before the function
* is called.
*/
static void
{
}
/*
* Disable interrupts for a particular port.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static void
{
"ahci_disable_port_intrs enter, port %d", port);
}
/*
* Disable interrupts for the whole HBA.
*
* The global bit is cleared, then all interrupt sources from all
* ports are disabled.
*
* WARNING!!! ahcictl_mutex should be acquired before the function
* is called.
*/
static void
{
NULL);
ghc_control &= ~ AHCI_HBA_GHC_IE;
}
/*
* Handle FIXED or MSI interrupts.
*/
/*
* According to AHCI spec, the HBA may support several interrupt modes:
* * pin based interrupts (FIXED)
* * single MSI message interrupts
* * multiple MSI based message interrupts
*
* For pin based interrupts, the software interrupt handler need to check IS
* register to find out which port has pending interrupts. And then check
* PxIS register to find out which interrupt events happened on that port.
*
* For single MSI message interrupts, MSICAP.MC.MSIE is set with '1', and
* MSICAP.MC.MME is set with '0'. This mode is similar to pin based interrupts
* in that software interrupt handler need to check IS register to determine
* which port triggered the interrupts since it uses a single message for all
* port interrupts.
*
* HBA may optionally support multiple MSI message for better performance. In
* this mode, each port may have its own interrupt message, and thus generation
* of interrupts is no longer controlled through the IS register. MSICAP.MC.MMC
* represents a power-of-2 wrapper on the number of implemented ports, and
* the mapping of ports to interrupts is done in a 1-1 relationship, up to the
* maximum number of assigned interrupts. When the number of MSI messages
* allocated is less than the number requested, then hardware may have two
* implementation behaviors:
* * assign each ports its own interrupt and then force all additional
* ports to share the last interrupt message, and this condition is
* indicated by clearing GHC.MRSM to '0'
* * revert to single MSI mode, indicated by setting GHC.MRSM to '1'
* When multiple-message MSI is enabled, hardware will still set IS register
* as single message case. And this IS register may be used by software when
* fewer than the requested number of messages is granted in order to determine
* which port had the interrupt.
*
* Note: The current ahci driver only supports the first two interrupt modes:
* pin based interrupts and single MSI message interrupts, and the reason
* is indicated in below code.
*/
static int
{
int i, rc;
"ahci_add_intrs enter interrupt type 0x%x", intr_type);
/* get number of interrupts. */
"ddi_intr_get_nintrs() failed, "
return (DDI_FAILURE);
}
/* get number of available interrupts. */
"ddi_intr_get_navail() failed, "
return (DDI_FAILURE);
}
#if AHCI_DEBUG
"ddi_intr_get_nintrs returned %d, navail() returned %d",
}
#endif
/*
* Note: So far Solaris restricts the maximum number of messages for
* x86 to 2, that is avail is 2, so here we set the count with 1 to
* force the driver to use single MSI message interrupt. In future if
* Solaris remove the restriction, then we need to delete the below
* code and try to use multiple interrupt routine to gain better
* performance.
*/
"force to use one interrupt routine though the "
"HBA supports %d interrupt", count);
count = 1;
}
/* Allocate an array of interrupt handles. */
/* call ddi_intr_alloc(). */
"ddi_intr_alloc() failed, rc %d count %d actual %d "
return (DDI_FAILURE);
}
/* use interrupt count returned */
#if AHCI_DEBUG
}
#endif
/*
* Get priority for first, assume remaining are all the same.
*/
"ddi_intr_get_pri() failed", NULL);
/* Free already allocated intr. */
for (i = 0; i < actual; i++) {
}
return (DDI_FAILURE);
}
/* Test for high level interrupt. */
"ahci_add_intrs: Hi level intr not supported", NULL);
/* Free already allocated intr. */
for (i = 0; i < actual; i++) {
}
sizeof (ddi_intr_handle_t));
return (DDI_FAILURE);
}
/* Call ddi_intr_add_handler(). */
for (i = 0; i < actual; i++) {
"ddi_intr_add_handler() failed", NULL);
/* Free already allocated intr. */
for (i = 0; i < actual; i++) {
(void) ddi_intr_free(
ahci_ctlp->ahcictl_intr_htable[i]);
}
return (DDI_FAILURE);
}
}
"ddi_intr_get_cap() failed", NULL);
/* Free already allocated intr. */
for (i = 0; i < actual; i++) {
(void) ddi_intr_free(
ahci_ctlp->ahcictl_intr_htable[i]);
}
return (DDI_FAILURE);
}
/* Call ddi_intr_block_enable() for MSI. */
} else {
/* Call ddi_intr_enable() for FIXED or MSI non block enable. */
for (i = 0; i < ahci_ctlp->ahcictl_intr_cnt; i++) {
(void) ddi_intr_enable(
ahci_ctlp->ahcictl_intr_htable[i]);
}
}
return (DDI_SUCCESS);
}
/*
* Removes the registered interrupts irrespective of whether they
* were legacy or MSI.
*
* WARNING!!! The controller interrupts must be disabled before calling
* this routine.
*/
static void
{
int x;
/* Disable all interrupts. */
/* Call ddi_intr_block_disable(). */
} else {
for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
(void) ddi_intr_disable(
ahci_ctlp->ahcictl_intr_htable[x]);
}
}
/* Call ddi_intr_remove_handler(). */
for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
(void) ddi_intr_remove_handler(
ahci_ctlp->ahcictl_intr_htable[x]);
}
}
/*
* This routine tries to put port into P:NotRunning state by clearing
* PxCMD.ST. HBA will clear PxCI to 0h, PxSACT to 0h, PxCMD.CCS to 0h
* and PxCMD.CR to '0'.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static int
{
int loop_count;
"ahci_put_port_into_notrunning_state enter: port %d", port);
/* Wait until PxCMD.CR is cleared */
loop_count = 0;
do {
if (loop_count++ > AHCI_POLLRATE_PORT_IDLE) {
"clearing port %d CMD.CR timeout, "
"port_cmd_status = 0x%x", port,
/*
* We are effectively timing out after 0.5 sec.
* This value is specified in AHCI spec.
*/
break;
}
/* Wait for 10 millisec */
} while (port_cmd_status & AHCI_CMD_STATUS_CR);
if (port_cmd_status & AHCI_CMD_STATUS_CR) {
"ahci_put_port_into_notrunning_state: failed to clear "
"PxCMD.CR to '0' after loop count: %d, and "
return (AHCI_FAILURE);
} else {
"ahci_put_port_into_notrunning_state: succeeded to clear "
"PxCMD.CR to '0' after loop count: %d, and "
return (AHCI_SUCCESS);
}
}
/*
* First clear PxCMD.ST, and then check PxTFD. If both PxTFD.STS.BSY
* and PxTFD.STS.DRQ cleared to '0', it means the device is in a
* stable state, then set PxCMD.ST to '1' to start the port directly.
* If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to '1', then issue a
* COMRESET to the device to put it in an idle state.
*
* The fifth argument returns whether the port reset is involved during
* the process.
*
* The routine will be called under following scenarios:
* + To reset the HBA
* + To abort the packet(s)
* + To reset the port
* + To activate the port
* + Fatal error recovery
* + To abort the timeout packet(s)
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called. And ahciport_mutex will be released before the reset
* event is reported to sata module by calling sata_hba_event_notify,
* and then be acquired again later.
*
* NOTES!!! During this procedure, PxSERR register will be cleared, and
* according to the spec, the clearance of three bits will also clear
* three interrupt status bits.
* 1. PxSERR.DIAG.F will clear PxIS.UFS
* 2. PxSERR.DIAG.X will clear PxIS.PCS
* 3. PxSERR.DIAG.N will clear PxIS.PRCS
*
* Among these three interrupt events, the driver needs to take care of
* PxIS.PRCS, which is the hot plug event. When the driver found out
* a device was unplugged, it will call the interrupt handler.
*/
static int
{
int rval;
int dev_exists_begin = 0;
int dev_exists_end = 0;
int npmport = 0;
"ahci_restart_port_wait_till_ready: port %d enter", port);
dev_exists_begin = 1;
/* First clear PxCMD.ST */
port);
if (rval != AHCI_SUCCESS)
/*
* If PxCMD.CR does not clear within a reasonable time, it
* may assume the interface is in a hung condition and may
* continue with issuing the port reset.
*/
goto reset;
/* Then clear PxSERR */
/* Then get PxTFD */
/*
* Check whether the device is in a stable status, if yes,
* then start the port directly. However for ahci_tran_reset_dport,
* we may have to perform a port reset.
*/
!(flag & AHCI_PORT_RESET))
goto out;
/*
* If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to '1', then issue
* a COMRESET to the device
*/
#ifdef AHCI_DEBUG
if (rval != AHCI_SUCCESS)
"ahci_restart_port_wait_till_ready: port %d failed",
port);
#endif
if (reset_flag != NULL)
*reset_flag = 1;
/* Indicate to the framework that a reset has happened. */
!(flag & AHCI_RESET_NO_EVENTS_UP)) {
/* Set the reset in progress flag */
if (ahci_ctlp->ahcictl_sata_hba_tran) {
&sdevice,
}
"port %d sending event up: SATA_EVNT_DEVICE_RESET", port);
} else {
}
out:
/* SStatus tells the presence of device. */
dev_exists_end = 1;
}
return (rval);
/* Check whether a hot plug event happened */
"ahci_restart_port_wait_till_ready: port %d "
"device is removed", port);
"ahci_restart_port_wait_till_ready: port %d "
"AHCI_PORT_FLAG_NODEV flag is set", port);
return (rval);
}
/* 0/1 -> 1 : device may change */
/*
* May be called by ahci_fatal_error_recovery_handler, so
* don't issue software if the previous device is ATAPI.
*/
return (rval);
/*
* The COMRESET will make port multiplier enter legacy mode.
* Issue a software reset to make it work again.
*/
/*
* Following codes are specific for the port multiplier
*/
if (previous_dev_type != SATA_DTYPE_PMULT &&
/* in case previous_dev_type is corrupt */
return (rval);
}
/* Device change: PMult -> Non-PMult */
if (previous_dev_type == SATA_DTYPE_PMULT &&
/*
* This might happen because
* 1. Software reset failed. Port multiplier is not correctly
* enumerated.
* 2. Another non-port-multiplier device is attached. Perhaps
* the port multiplier was replaced by another device by
* whatever reason, but AHCI driver missed hot-plug event.
*
* Now that the port has been initialized, we just need to
* update the port structure according new device, then report
* and wait SATA framework to probe new device.
*/
/* Force to release pmult resource */
&sdevice,
"Port multiplier is [Gone] at port %d ", port);
"port %d sending event up: SATA_EVNT_DEVICE_RESET", port);
return (AHCI_SUCCESS);
}
/* Device change: Non-PMult -> PMult */
/* NOTE: The PxCMD.PMA may be cleared by HBA reset. */
}
/* Device (may) change: PMult -> PMult */
/*
* First initialize port multiplier. Set state to READY and wait for
* probe entry point to initialize it
*/
/*
* It's a little complicated while target is a port multiplier. we
* need to COMRESET all pmports behind that PMult otherwise those
* sub-links between the PMult and the sub-devices will be in an
* to those sub-devices will be rejected by Link-Fatal-Error.
*/
/*
* The PxSNTF will be set soon after the pmult is plugged. While the
* pmult itself is attaching, sata_hba_event_notfiy will fail. so we
* simply mark every sub-port as 'unknown', then ahci_probe_pmport
* will initialized it.
*/
/* Report reset event. */
return (rval);
}
/*
* This routine may be called under four scenarios:
* a) do the recovery from fatal error
* b) or we need to timeout some commands
* c) or we need to abort some commands
* d) or we need reset device/port/controller
*
* In all these scenarios, we need to send any pending unfinished
* commands up to sata framework.
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static void
{
uint32_t finished_tags = 0;
uint32_t unfinished_tags = 0;
int tmp_slot;
int ncq_cmd_in_progress = 0;
int err_retri_cmd_in_progress = 0;
int rdwr_pmult_cmd_in_progress = 0;
"ahci_mop_commands entered: port: %d slot_status: 0x%x",
"ahci_mop_commands: failed_tags: 0x%x, "
"timeout_tags: 0x%x aborted_tags: 0x%x, "
"reset_tags: 0x%x", failed_tags,
#ifdef AHCI_DEBUG
if (ahci_debug_flags & AHCIDBG_ERRS) {
int i;
char msg_buf[200] = {0, };
for (i = 0x1f; i >= 0; i--) {
msg_buf[i] = 'X';
else
msg_buf[i] = '.';
}
(void *)ahci_portp->ahciport_err_retri_pkt,
(void *)ahci_portp->ahciport_rdwr_pmult_pkt);
}
#endif
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
~failed_tags &
~aborted_tags &
~reset_tags &
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
ncq_cmd_in_progress = 1;
~failed_tags &
~aborted_tags &
~reset_tags &
} else if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
/*
* When AHCI_PORT_FLAG_RQSENSE or AHCI_PORT_FLAG_RDLOGEXT is
* set, it means REQUEST SENSE or READ LOG EXT command doesn't
* complete successfully due to one of the following three
* conditions:
*
* 1. Fatal error - failed_tags includes its slot
* 2. Timed out - timeout_tags includes its slot
* 3. Aborted when hot unplug - aborted_tags includes its
* slot
*
* Please note that the command is always sent down in Slot 0
*/
"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",
} else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
"ahci_mop_commands is called for port %d while "
}
#ifdef AHCI_DEBUG
"ahci_mop_commands: finished_tags: 0x%x, "
#endif
/* Send up finished packets with SATA_PKT_COMPLETED */
while (finished_tags) {
if (tmp_slot == -1) {
break;
}
"sending up pkt 0x%p with SATA_PKT_COMPLETED",
(void *)satapkt);
/*
* Cannot fetch the return register content since the port
* was restarted, so the corresponding tag will be set to
* aborted tags.
*/
aborted_tags |= tmp_slot;
continue;
}
if (ncq_cmd_in_progress)
tmp_slot);
}
/* Send up failed packets with SATA_PKT_DEV_ERROR. */
while (failed_tags) {
if (err_retri_cmd_in_progress) {
"sending up pkt 0x%p with SATA_PKT_DEV_ERROR",
(void *)satapkt);
break;
}
if (rdwr_pmult_cmd_in_progress) {
"ahci_mop_commands: sending up "
"rdwr pmult pkt 0x%p with SATA_PKT_DEV_ERROR",
(void *)satapkt);
break;
}
if (tmp_slot == -1) {
break;
}
"sending up pkt 0x%p with SATA_PKT_DEV_ERROR",
(void *)satapkt);
if (ncq_cmd_in_progress)
tmp_slot);
}
/* Send up timeout packets with SATA_PKT_TIMEOUT. */
while (timeout_tags) {
if (err_retri_cmd_in_progress) {
"sending up pkt 0x%p with SATA_PKT_TIMEOUT",
(void *)satapkt);
break;
}
if (rdwr_pmult_cmd_in_progress) {
"ahci_mop_commands: sending up "
"rdwr pmult pkt 0x%p with SATA_PKT_TIMEOUT",
(void *)satapkt);
break;
}
if (tmp_slot == -1) {
break;
}
"sending up pkt 0x%p with SATA_PKT_TIMEOUT",
(void *)satapkt);
if (ncq_cmd_in_progress)
tmp_slot);
}
/* Send up aborted packets with SATA_PKT_ABORTED */
while (aborted_tags) {
if (err_retri_cmd_in_progress) {
"sending up pkt 0x%p with SATA_PKT_ABORTED",
(void *)satapkt);
break;
}
if (rdwr_pmult_cmd_in_progress) {
"ahci_mop_commands: sending up "
"rdwr pmult pkt 0x%p with SATA_PKT_ABORTED",
(void *)satapkt);
break;
}
if (tmp_slot == -1) {
break;
}
"sending up pkt 0x%p with SATA_PKT_ABORTED",
(void *)satapkt);
if (ncq_cmd_in_progress)
tmp_slot);
}
/* Send up reset packets with SATA_PKT_RESET. */
while (reset_tags) {
if (rdwr_pmult_cmd_in_progress) {
"ahci_mop_commands: sending up "
"rdwr pmult pkt 0x%p with SATA_PKT_RESET",
(void *)satapkt);
break;
}
if (tmp_slot == -1) {
break;
}
"sending up pkt 0x%p with SATA_PKT_RESET",
(void *)satapkt);
if (ncq_cmd_in_progress)
tmp_slot);
}
/* Send up unfinished packets with SATA_PKT_RESET */
while (unfinished_tags) {
if (tmp_slot == -1) {
break;
}
"sending up pkt 0x%p with SATA_PKT_RESET",
(void *)satapkt);
if (ncq_cmd_in_progress)
tmp_slot);
}
if (ahci_portp->ahciport_mop_in_progress == 0)
}
/*
* This routine is going to first request a READ LOG EXT sata pkt from sata
* module, and then deliver it to the HBA to get the ncq failure context.
* The return value is the exactly failed tags.
*/
static uint32_t
{
int loop_count;
int rval;
int failed_slot;
uint32_t failed_tags = 0;
struct sata_ncq_error_recovery_page *ncq_err_page;
"ahci_get_rdlogext_data enter: port %d", port);
/* Prepare the sdevice data */
/* Translate sata_device.satadev_addr -> ahci_addr */
/*
* Call the sata hba interface to get a rdlog spkt
*/
loop_count = 0;
loop:
if (rdlog_spkt == NULL) {
if (loop_count++ < AHCI_POLLRATE_GET_SPKT) {
/* Sleep for a while */
goto loop;
}
/* Timed out after 1s */
"failed to get rdlog spkt for port %d", port);
return (failed_tags);
}
/*
* This flag is used to handle the specific error recovery when the
* READ LOG EXT command gets a failure (fatal error or time-out).
*/
/*
* This start is not supposed to fail because after port is restarted,
* the whole command list is empty.
*/
/* Remove the flag after READ LOG EXT command is completed */
/* Update the request log data */
buf_dma_handle = *(ddi_dma_handle_t *)
if (rval == DDI_SUCCESS) {
(struct sata_ncq_error_recovery_page *)rdlog_spkt->
/* Get the failed tag */
"ahci_get_rdlogext_data: port %d "
if (failed_slot & NQ) {
"the failed slot is not a valid tag", NULL);
goto out;
}
"ahci_get_rdlogext_data: failed spkt 0x%p",
(void *)spkt);
"the failed slot spkt is NULL", NULL);
goto out;
}
/* Fill out the error context */
&spkt->satapkt_device);
}
}
out:
return (failed_tags);
}
/*
* This routine is going to first request a REQUEST SENSE sata pkt from sata
* module, and then deliver it to the HBA to get the sense data and copy
* the sense data back to the orignal failed sata pkt, and free the REQUEST
* SENSE sata pkt later.
*/
static void
{
int loop_count;
#if AHCI_DEBUG
struct scsi_extended_sense *rqsense;
#endif
"ahci_get_rqsense_data enter: port %d", port);
/* Prepare the sdevice data */
/* Translate sata_device.satadev_addr -> ahci_addr */
/*
* Call the sata hba interface to get a rs spkt
*/
loop_count = 0;
loop:
if (loop_count++ < AHCI_POLLRATE_GET_SPKT) {
/* Sleep for a while */
goto loop;
}
/* Timed out after 1s */
"failed to get rs spkt for port %d", port);
return;
}
/*
* This flag is used to handle the specific error recovery when the
* REQUEST SENSE command gets a faiure (fatal error or time-out).
*/
/*
* This start is not supposed to fail because after port is restarted,
* the whole command list is empty.
*/
/* Remove the flag after REQUEST SENSE command is completed */
/* Update the request sense data */
buf_dma_handle = *(ddi_dma_handle_t *)
(void) ddi_dma_sync(buf_dma_handle, 0, 0,
/* Copy the request sense data */
#if AHCI_DEBUG
rqsense = (struct scsi_extended_sense *)
/* Dump the sense data */
"Sense data for satapkt %p ATAPI cmd 0x%x",
" es_code 0x%x es_class 0x%x "
"es_key 0x%x es_add_code 0x%x "
"es_qual_code 0x%x",
#endif
}
}
/*
* Fatal errors will cause the HBA to enter the ERR: Fatal state. To recover,
* the port must be restarted. When the HBA detects thus error, it may try
* to abort a transfer. And if the transfer was aborted, the device is
* expected to send a D2H Register FIS with PxTFD.STS.ERR set to '1' and both
* PxTFD.STS.BSY and PxTFD.STS.DRQ cleared to '0'. Then system software knows
* that the device is in a stable status and transfers may be restarted without
* issuing a COMRESET to the device. If PxTFD.STS.BSY or PxTFD.STS.DRQ is set,
* then the software will send the COMRESET to do the port reset.
*
* Software should perform the appropriate error recovery actions based on
* whether non-queued commands were being issued or natived command queuing
* commands were being issued.
*
* And software will complete the command that had the error with error mark
* to higher level software.
*
* Fatal errors include the following:
* PxIS.IFS - Interface Fatal Error Status
* PxIS.HBDS - Host Bus Data Error Status
* PxIS.HBFS - Host Bus Fatal Error Status
* PxIS.TFES - Task File Error Status
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static void
{
uint32_t slot_status = 0;
uint32_t failed_tags = 0;
int failed_slot;
int reset_flag = 0, flag = 0;
#if AHCI_DEBUG
#endif
int rval;
"ahci_fatal_error_recovery_handler enter: port %d", port);
/* Port multiplier error */
/* FBS code is neither completed nor tested. */
port, intr_status);
/* Force a port reset */
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
/* Read PxCI to see which commands are still outstanding */
/*
* Read PxCMD.CCS to determine the slot that the HBA
* was processing when the error occurred.
*/
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
} else {
/* May happen when interface errors occur? */
goto next;
}
}
#if AHCI_DEBUG
/*
* Debugging purpose...
*/
"ahci_fatal_error_recovery_handler: port %d, "
"PRD Byte Count = 0x%x, "
"ahciport_prd_bytecounts = 0x%x", port,
}
#endif
/* Fill out the status and error registers for PxIS.TFES */
if (intr_status & AHCI_INTR_STATUS_TFES) {
/* Copy the error context back to the sata_cmd */
}
/* The failed command must be one of the outstanding commands */
/* Update the sata registers, especially PxSERR register */
&spkt->satapkt_device);
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
/* Read PxSACT to see which commands are still outstanding */
}
next:
#if AHCI_DEBUG
/*
* When AHCI_PORT_FLAG_RQSENSE or AHCI_PORT_FLAG_RDLOGEXT flag is
* set, it means a fatal error happened after REQUEST SENSE command
* or READ LOG EXT command is delivered to the HBA during the error
* recovery process. At this time, the only outstanding command is
* supposed to be REQUEST SENSE command or READ LOG EXT command.
*/
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
"ahci_fatal_error_recovery_handler: port %d REQUEST SENSE "
"command or READ LOG EXT command for error data retrieval "
"failed", port);
ASSERT(failed_slot == 0);
}
#endif
if (rval == AHCI_SUCCESS)
else
}
/*
* Won't retrieve error information:
* 1. Port reset was involved to recover
* 2. Device is gone
* 3. IDENTIFY DEVICE command sent to ATAPI device
* 4. REQUEST SENSE or READ LOG EXT command during error recovery
*/
if (reset_flag ||
goto out;
/*
* Deliver READ LOG EXT to gather information about the error when
* a COMRESET has not been performed as part of the error recovery
* during NCQ command processing.
*/
if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
ahci_portp, port);
goto out;
}
/*
* Deliver REQUEST SENSE for ATAPI command to gather information about
* the error when a COMRESET has not been performed as part of the
* error recovery.
*/
out:
"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",
failed_tags, /* failed tags */
0, /* timeout tags */
0, /* aborted tags */
0); /* reset tags */
}
/*
* Used to recovery a PMULT pmport fatal error under FIS-based switching.
* 1. device specific.PxFBS.SDE=1
* 2. Non-Deivce specific.
* Nothing will be done when Command-based switching is employed.
*
* Currently code is neither completed nor tested.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static void
{
#ifndef __lock_lint
#endif
int loop_count = 0;
/* Nothing will be done under Command-based switching. */
return;
if (!(port_fbs_ctrl & AHCI_FBS_EN))
/* FBS is not enabled. */
return;
/* Problem's getting complicated now. */
/*
* If FIS-based switching is used, we need to check
* the PxFBS to see the error type.
*/
/* Refer to spec(v1.2) 9.3.6.1 */
if (port_fbs_ctrl & AHCI_FBS_SDE) {
"A Device Sepcific Error: port %d", port);
/*
* Controller has paused commands for all other
* sub-devices until PxFBS.DEC is set.
*/
ahci_portp, 0);
/*
* Wait controller clear PxFBS.DEC,
* then we can continue.
*/
loop_count = 0;
do {
if (loop_count++ > 1000)
/*
* Esclate the error. Follow
* non-device specific error
* procedure.
*/
return;
} while (port_fbs_ctrl & AHCI_FBS_DEC);
/*
* Issue a software reset to ensure drive is in
* a known state.
*/
(void) ahci_software_reset(ahci_ctlp,
ahci_portp, &addr);
} else {
/* Process Non-Device Specific Error. */
/* This will be handled later on. */
}
}
/*
* Handle events - fatal error recovery
*/
static void
ahci_events_handler(void *args)
{
int instance;
"ahci_events_handler enter: port %d intr_status = 0x%x",
/*
* ahci_intr_phyrdy_change() may have rendered it to
* SATA_DTYPE_NONE.
*/
"ahci_events_handler: port %d no device attached, "
"and just return without doing anything",
}
goto out;
}
if (event & (AHCI_INTR_STATUS_IFS |
out:
}
/*
* ahci_watchdog_handler() and ahci_do_sync_start will call us if they
* detect there are some commands which are timed out.
*/
static void
{
uint32_t slot_status = 0;
uint32_t finished_tags = 0;
uint32_t timeout_tags = 0;
"ahci_timeout_pkts enter: port %d", port);
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
/* Read PxCI to see which commands are still outstanding */
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
/* Read PxSACT to see which commands are still outstanding */
}
#if AHCI_DEBUG
/*
* When AHCI_PORT_FLAG_RQSENSE or AHCI_PORT_FLAG_RDLOGEXT flag is
* set, it means a fatal error happened after REQUEST SENSE command
* or READ LOG EXT command is delivered to the HBA during the error
* recovery process. At this time, the only outstanding command is
* supposed to be REQUEST SENSE command or READ LOG EXT command.
*/
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
"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",
} else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
"ahci_timeout_pkts called while executing R/W PMULT "
"command timeout_tags = 0x%x, slot_status = 0x%x",
}
#endif
/*
* Re-identify timeout tags because some previously checked commands
* could already complete.
*/
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
"ahci_timeout_pkts: port %d, finished_tags = 0x%x, "
"timeout_tags = 0x%x, port_cmd_issue = 0x%x, "
"pending_tags = 0x%x ",
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
"ahci_timeout_pkts: port %d, finished_tags = 0x%x, "
"timeout_tags = 0x%x, port_sactive = 0x%x, "
"pending_ncq_tags = 0x%x ",
} else if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) ||
}
0, /* failed tags */
timeout_tags, /* timeout tags */
0, /* aborted tags */
0); /* reset tags */
}
/*
* Watchdog handler kicks in every 5 seconds to timeout any commands pending
* for long time.
*/
static void
{
int tmp_slot;
int current_slot;
"ahci_watchdog_handler entered", NULL);
continue;
}
continue;
}
/* Skip the check for those ports in error recovery */
continue;
}
pending_tags = 0;
if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) ||
current_slot = 0;
pending_tags = 0x1;
} else if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
}
timeout_tags = 0;
while (pending_tags) {
if (tmp_slot == -1) {
break;
}
else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp))
else
/*
* If a packet has survived for more than it's
* max life cycles, it is a candidate for time
* out.
*/
> 0)
goto next;
#if AHCI_DEBUG
if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
ahci_ctlp, "watchdog: the current "
"tags is 0x%x", current_tags);
} else {
ahci_ctlp, "watchdog: the current "
"slot is %d", current_slot);
}
#endif
/*
* We need to check whether the HBA has
* begun to execute the command, if not,
* then re-set the timer of the command.
*/
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) &&
(tmp_slot != current_slot) ||
} else {
"port %d satapkt 0x%p timed out\n",
}
}
next:
}
if (timeout_tags) {
port, timeout_tags);
}
}
/* Re-install the watchdog timeout handler */
if (ahci_ctlp->ahcictl_timeout_id != 0) {
timeout((void (*)(void *))ahci_watchdog_handler,
}
}
/*
* Fill the error context into sata_cmd for non-queued command error.
*/
static void
{
}
}
/*
* Fill the ncq error page into sata_cmd for queued command error.
*/
static void
struct sata_ncq_error_recovery_page *ncq_err_page)
{
}
/*
* Put the respective register value to sata_cmd_t for satacmd_flags.
*/
static void
{
}
static void
{
if (intr_status & AHCI_INTR_STATUS_IFS)
if (intr_status & AHCI_INTR_STATUS_HBDS)
if (intr_status & AHCI_INTR_STATUS_HBFS)
if (intr_status & AHCI_INTR_STATUS_TFES)
}
static void
{
int tmp_slot;
while (slot_tags) {
if (tmp_slot == -1) {
break;
}
"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,
}
}
/*
* Dump the serror message to the log.
*/
static void
{
static char err_buf[512];
static char err_msg_header[16];
*err_buf = '\0';
*err_msg_header = '\0';
if (port_serror & SERROR_DATA_ERR_FIXED) {
"\tRecovered Data Integrity Error (I)\n");
}
if (port_serror & SERROR_COMM_ERR_FIXED) {
"\tRecovered Communication Error (M)\n");
}
if (port_serror & SERROR_DATA_ERR) {
"\tTransient Data Integrity Error (T)\n");
}
if (port_serror & SERROR_PERSISTENT_ERR) {
"\tPersistent Communication or Data Integrity Error (C)\n");
}
if (port_serror & SERROR_PROTOCOL_ERR) {
}
if (port_serror & SERROR_INT_ERR) {
}
if (port_serror & SERROR_PHY_RDY_CHG) {
}
if (port_serror & SERROR_PHY_INT_ERR) {
}
if (port_serror & SERROR_COMM_WAKE) {
}
if (port_serror & SERROR_10B_TO_8B_ERR) {
}
if (port_serror & SERROR_DISPARITY_ERR) {
}
if (port_serror & SERROR_CRC_ERR) {
}
if (port_serror & SERROR_HANDSHAKE_ERR) {
}
if (port_serror & SERROR_LINK_SEQ_ERR) {
}
if (port_serror & SERROR_TRANS_ERR) {
"\tTransport state transition error (T)\n");
}
if (port_serror & SERROR_FIS_TYPE) {
}
if (port_serror & SERROR_EXCHANGED_ERR) {
}
if (*err_msg == '\0')
return;
if (debug_only) {
} else if (ahci_ctlp) {
/* sata trace debug */
} else {
/* sata trace debug */
}
}
/*
* Translate the sata_address_t type into the ahci_addr_t type.
* sata_device.satadev_addr structure is used as source.
*/
static void
{
switch (sata_addrp->qual) {
case SATA_ADDR_DCPORT:
case SATA_ADDR_CPORT:
break;
case SATA_ADDR_PMULT:
case SATA_ADDR_PMULT_SPEC:
break;
case SATA_ADDR_DPMPORT:
case SATA_ADDR_PMPORT:
break;
case SATA_ADDR_NULL:
default:
/* something went wrong */
break;
}
}
/*
* This routine is to calculate the total number of ports implemented
* by the HBA.
*/
static int
{
uint8_t i;
int num = 0;
for (i = 0; i < AHCI_MAX_PORTS; i++) {
num++;
}
return (num);
}
#if AHCI_DEBUG
static void
{
static char name[16];
if (ahci_ctlp) {
} else {
}
}
#endif
/*
* quiesce(9E) entry point.
*
* This function is called when the system is single-threaded at high
* PIL with preemption disabled. Therefore, this function must not be
* blocked.
*
* This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
* DDI_FAILURE indicates an error condition and should almost never happen.
*/
static int
{
return (DDI_FAILURE);
#if AHCI_DEBUG
ahci_debug_flags = 0;
#endif
/* disable all the interrupts. */
continue;
}
/*
* Stop the port by clearing PxCMD.ST
*
* Here we must disable the port interrupt because
* ahci_disable_all_intrs only clear GHC.IE, and IS
* register will be still set if PxIE is enabled.
* When ahci shares one IRQ with other drivers, the
* intr handler may claim the intr mistakenly.
*/
ahci_portp, port);
}
return (DDI_SUCCESS);
}
/*
* The function will add a sata packet to the done queue.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static void
{
/* set the reason for all packets */
satapkt->satapkt_comp) {
/*
* only add to queue when mode is not synch and there is
* completion callback
*/
/*
*/
}
/*
* The function will call completion callback of sata packet on the
* completed queue
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called.
*/
static void
{
if (ahci_portp->ahciport_doneq) {
ahci_portp->ahciport_doneq_len = 0;
/* Call the callback */
}
}
}