ahci.c revision f8a673ad9145b262edb65a98fb3fb92027d23a05
/*
* 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 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* 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.
*
*/
/*
* 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
/*
* 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_pkt_t *);
uint8_t, sata_pkt_t *);
static void ahci_copy_ncq_err_page(sata_cmd_t *,
struct sata_ncq_error_recovery_page *);
static int ahci_hba_reset(ahci_ctl_t *);
static int ahci_reset_hba_reject_pkts(ahci_ctl_t *);
uint8_t);
uint8_t, int, int *);
uint8_t, sata_pkt_t *);
static void ahci_events_handler(void *);
static void ahci_watchdog_handler(ahci_ctl_t *);
static int ahci_add_legacy_intrs(ahci_ctl_t *);
static int ahci_add_msi_intrs(ahci_ctl_t *);
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);
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 int 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 and ahci_64bit_dma_addressed
*/
/* The number of Physical Region Descriptor Table(PRDT) in Command Table */
/* AHCI MSI is tunable */
/* 64-bit dma addressing is tunable */
#if AHCI_DEBUG
uint32_t ahci_debug_flags = 0;
/* 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;
}
if (ret != 0) {
#if AHCI_DEBUG
#endif
goto err_out;
}
/* watchdog tick */
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
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.
*/
/* 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");
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++) {
}
/*
* 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;
}
/* 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 */
#if AHCI_DEBUG
/* Get the interface speed supported by the HBA */
if (speed == 0x01) {
"hba interface speed support: Gen 1 (1.5Gbps)");
} else if (speed == 0x10) {
"hba interface speed support: Gen 2 (3 Gbps)");
} else if (speed == 0x11) {
"hba interface speed support: Gen 3 (6 Gbps)");
}
#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 */
/*
* According to the AHCI spec, CAP.NP should indicate the maximum
* number of ports supported by the HBA silicon, but we found
* this value of ICH8 chipset only indicates the number of ports
* implemented (exposed) by it. Therefore, the driver should calculate
* the potential maximum value by checking PI register, and use
* the maximum of this value and CAP.NP.
*/
/* 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");
}
!= DDI_SUCCESS) {
instance);
goto err_out;
}
/*
* Check the pci configuration space, and set caps. We also
* handle the hardware defect in this function.
*
* addressing since it doesn't support 64-bit dma though their
* registers declare they 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
*/
"Using MSI interrupt type");
goto intr_done;
}
"MSI registration failed, "
"trying FIXED interrupts");
}
if (intr_types & DDI_INTR_TYPE_FIXED) {
"Using FIXED interrupt type");
goto intr_done;
}
"FIXED interrupt registration failed");
}
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)
if ((ahci_64bit_dma_addressed == B_FALSE) ||
0xffffffffull;
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;
}
return (DDI_SUCCESS);
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) {
}
}
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. */
/* 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");
/* 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");
}
/*
* 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");
}
/* 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");
/* Detach from the SATA framework. */
DDI_SUCCESS) {
return (AHCI_FAILURE);
}
/* Deallocate sata_hba_tran. */
sizeof (sata_hba_tran_t));
return (AHCI_SUCCESS);
}
/*
* 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
{
#if AHCI_DEBUG
#endif
"ahci_tran_probe_port enter: cport: %d, "
switch (port_state) {
case SATA_PSTATE_FAILED:
"ahci_tran_probe_port: port %d PORT FAILED", port);
goto out;
case SATA_PSTATE_SHUTDOWN:
"ahci_tran_probe_port: port %d PORT SHUTDOWN", port);
goto out;
case SATA_PSTATE_PWROFF:
"ahci_tran_probe_port: port %d PORT PWROFF", port);
goto out;
case SATA_PSTATE_PWRON:
"ahci_tran_probe_port: port %d PORT PWRON", port);
break;
default:
"ahci_tran_probe_port: port %d PORT NORMAL %x",
port, port_state);
break;
}
switch (device_type) {
case SATA_DTYPE_ATADISK:
"ahci_tran_probe_port: port %d DISK found", port);
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 %d ATAPI found", port);
break;
case SATA_DTYPE_PMULT:
"ahci_tran_probe_port: port %d Port Multiplier found",
port);
break;
case SATA_DTYPE_UNKNOWN:
"ahci_tran_probe_port: port %d Unknown device found", port);
break;
default:
/* we don't support any other device types */
"ahci_tran_probe_port: port %d No device found", port);
break;
}
out:
return (SATA_SUCCESS);
}
/*
* 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
{
"ahci_tran_start enter: cport %d satapkt 0x%p",
/*
* In case the targer 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: %d", port);
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: %d", port);
return (SATA_TRAN_PORT_ERROR);
}
/*
* 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).
*/
"ahci_tran_start clearing 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);
}
"ahci_tran_start returning BUSY while "
"mopping in progress: port: %d", port);
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) {
"return QUEUE_FULL: port %d", port);
return (SATA_TRAN_QUEUE_FULL);
}
} else {
/* Async start, using interrupt */
== AHCI_FAILURE) {
"returning QUEUE_FULL: port %d", port);
return (SATA_TRAN_QUEUE_FULL);
}
}
"sata tran accepted: port %d", port);
return (SATA_TRAN_ACCEPTED);
}
/*
* 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);
}
}
if (satapkt) { \
/* \
* We set the satapkt_reason in both sync and \
* non-sync cases. \
*/ \
} \
if (satapkt && \
satapkt->satapkt_comp) { \
} else { \
if (satapkt && \
}
/*
* Searches for and claims a free command slot.
*
* Returns:
*
* AHCI_FAILURE if failed
* 1. if no empty slot left
* 2. non-queued command requested while queued command(s) is outstanding
* 3. queued command requested whild non-queued command(s) is outstanding
* 4. HBA doesn't support multiple-use of command list while already a
* non-queued command is oustanding
*
* claimed slot number if succeeded
*
* NOTE: it will always return slot 0 during error recovery process for
* REQUEST SENSE or READ LOG EXT command to simplify the algorithm.
*
* 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");
return (AHCI_FAILURE);
}
"ahci_claim_free_slot: HBA cannot support multiple-"
"use of the command list for non-queued commands");
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");
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");
slot = 0;
goto out;
}
if (slot == -1) {
"ahci_claim_free_slot: no empty slots");
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) {
}
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 */
}
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
*/
/* 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. 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 */
"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[]. */
/*
* We are overloading satapkt_hba_driver_private with
* watched_cycle count.
*/
#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),
/* 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);
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);
}
/*
* 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.
*
* 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_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 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 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_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)
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 ret = SATA_SUCCESS;
"ahci_reset_hba_reject_pkts enter");
continue;
}
if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
}
}
ret = SATA_FAILURE;
goto out;
}
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_port enter: cport: %d", cport);
case SATA_ADDR_CPORT:
/* Port reset */
break;
case SATA_ADDR_DCPORT:
/* Device reset */
/*
* 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.
*/
"ahci_tran_reset_dport returning SATA_FAILURE "
"while no device attached: port: %d", port);
ret = SATA_FAILURE;
break;
}
ahci_portp, port);
break;
case SATA_ADDR_CNTRL:
/* Reset the whole controller */
break;
case SATA_ADDR_PMPORT:
case SATA_ADDR_DPMPORT:
"ahci_tran_reset_dport: port multiplier will be "
"supported later");
/* FALLTHRU */
default:
ret = SATA_FAILURE;
}
return (ret);
}
/*
* Called by sata framework to activate a port as part of hotplug.
* (cfgadm -c connect satax/y)
* Note: Not port-mult aware.
*/
static int
{
"ahci_tran_hotplug_port_activate cport %d enter", cport);
/*
* 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
*/
return (SATA_SUCCESS);
}
/*
* Called by sata framework to deactivate a port as part of hotplug.
* (cfgadm -c disconnect satax/y)
* Note: Not port-mult aware.
*/
static int
{
"ahci_tran_hotplug_port_deactivate cport %d enter", cport);
/* Disable the interrupts on the port */
goto phy_offline;
}
/* First to abort all the pending commands */
/* Then stop the port */
ahci_portp, port);
/* Next put the PHY offline */
/* Update ahciport_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 on port: %d", port);
/*
* 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
/*
* Allocate the ports structure, only called by ahci_attach
*/
static int
{
"ahci_alloc_ports_state enter");
/* 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");
/*
* Indicate that system software is AHCI aware by setting
* GHC.AE to 1
*/
/* 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");
/* 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);
}
}
/*
* The routine is to initialize the 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.
*
* WARNING!!! ahciport_mutex should be acquired before the function is called.
*/
static int
{
"ahci_initialize_port: port %d "
/*
* Check whether the port is in NotRunning state, if not,
* put the port in NotRunning state
*/
if (!(port_cmd_status &
AHCI_CMD_STATUS_FR))) {
goto next;
}
return (AHCI_FAILURE);
next:
"port %d is in NotRunning state now", port);
/*
* 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.
*/
/* Try to get the device signature */
} else {
/*
* 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 "
"reset the port during resume", port);
"ahci_initialize_port: port %d "
"set PxCLB, PxCLBU, PxFB and PxFBU "
"during resume", port);
/* Config Port Received FIS Base Address */
/* Config Port Command List Base Address */
}
/* Disable the interface power management */
/* Return directly if no device connected */
"No device connected to port %d", port);
goto out;
}
/* Try to start the port */
!= AHCI_SUCCESS) {
"failed to start port %d", port);
return (AHCI_FAILURE);
}
out:
/* Enable port interrupts */
return (AHCI_SUCCESS);
}
/*
* Check the hardware defects and the power management 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.
*/
"revision id = 0x%x", revision);
if (revision == 0x00) {
"change ddi_attr_align to 0x4");
}
"VT8251 cannot use multiple command lists for "
"non-queued commands");
}
/*
* ATI SB600 (1002,4380) and SB700/750 (1002,4391) AHCI chipsets don't
* support 64-bit DMA addressing though they declare the support,
* so we need to set AHCI_CAP_32BIT_DMA flag to force 32-bit DMA.
*/
"ATI SB600/700/750 cannot do 64-bit DMA though CAP "
"indicates support, so force it to use 32-bit DMA");
}
/*
* 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");
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");
#endif
break;
case PCI_CAP_ID_MSI:
break;
case PCI_CAP_ID_PCIX:
"PCI-X capability found");
break;
case PCI_CAP_ID_PCI_E:
"PCI Express capability found");
break;
case PCI_CAP_ID_MSI_X:
"MSI-X capability found");
break;
case PCI_CAP_ID_SATA:
"SATA capability found");
break;
case PCI_CAP_ID_VS:
"Vendor Specific capability found");
break;
default:
"unrecognized capability 0x%x", cap);
break;
}
/*
* Get next capabilities pointer and clear bits 0,1.
*/
}
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 and PxCMD.FRE should be
* set before the function is called.
*/
static int
{
int slot, loop_count;
"Port %d device resetting", port);
/* First clear PxCMD.ST */
ahci_portp, port);
/* Then re-set PxCMD.ST */
/* Check PxTFD.STS.BSY and PxTFD.STS.DRQ */
if (port_task_file & AHCI_TFD_STS_BSY ||
if (!(port_cmd_status & AHCI_CMD_STATUS_CLO)) {
"PxTFD.STS.BSY or PxTFD.STS.DRQ is still set, "
"but PxCMD.CLO isn't supported, so a port "
"reset is needed.");
return (AHCI_FAILURE);
}
}
if (slot == AHCI_FAILURE) {
"ahci_software_reset: no free slot");
return (AHCI_FAILURE);
}
/* 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) {
break;
}
/* Wait for 10 millisec */
"ahci_software_reset: 1st loop count: %d, "
"port_cmd_issue = 0x%x, slot = 0x%x",
/* 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) {
break;
}
/* Wait for 10 millisec */
"ahci_software_reset: 2nd loop count: %d, "
"port_cmd_issue = 0x%x, slot = 0x%x",
return (AHCI_SUCCESS);
}
/*
* 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.
*
* Staggered spin-up is an optional feature in SATA II, and it enables an HBA
* to individually spin-up attached devices. Please refer to chapter 10.9 of
* AHCI 1.0 spec.
*/
/*
* WARNING!!! ahciport_mutex should be acquired, and PxCMD.ST should be also
* cleared before the function is called.
*/
static int
{
#if AHCI_DEBUG
#endif
int loop_count;
int rval = AHCI_SUCCESS;
"Port %d port resetting...", port);
if (cap_status & AHCI_HBA_CAP_SSS) {
/*
* HBA support staggered spin-up, if the port has
* not spin up yet, then force it to do spin-up
*/
if (!(port_cmd_status & AHCI_CMD_STATUS_SUD)) {
if (!(ahci_portp->ahciport_flags
& AHCI_PORT_FLAG_SPINUP)) {
"Port %d PxCMD.SUD is zero, force "
"it to do spin-up", port);
}
}
} else {
/*
* HBA doesn't support stagger spin-up, force it
* to do normal COMRESET
*/
if (ahci_portp->ahciport_flags &
"HBA does not support staggered spin-up "
"force it to do normal COMRESET");
}
}
/* Do normal COMRESET */
"ahci_port_reset: do normal COMRESET", port);
/*
* According to the spec, SUD bit should be set here,
* but JMicron JMB363 doesn't follow it, so remove
* the assertion, and just print a debug message.
*/
#if AHCI_DEBUG
if (!(port_cmd_status & AHCI_CMD_STATUS_SUD))
"port %d SUD bit not set", port)
#endif
/* 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 */
} else {
/* Do staggered spin-up */
/* PxSCTL.DET must be 0 */
/* 0 -> 1 edge */
/* Set PxCMD.SUD to 1 */
/* Enable PxCMD.FRE to read device */
}
/*
* 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.
*/
goto out;
}
/* Now we can make sure there is a device connected to the port */
/* a COMINIT signal is supposed to be received */
if (!(port_intr_status & AHCI_INTR_STATUS_PCS)) {
"COMINIT signal from the device not received",
rval = AHCI_FAILURE;
goto out;
}
/*
* 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.
*/
/*
* Next check whether COMRESET is completed successfully
*/
loop_count = 0;
do {
/*
* The Error bit '1' means COMRESET is finished successfully
* The device hardware has been initialized and the power-up
* diagnostics successfully completed.
*/
if (((port_task_file & AHCI_TFD_ERR_MASK)
>> AHCI_TFD_ERR_SHIFT) == 0x1) {
#if AHCI_DEBUG
"COMRESET success, D2H register FIS "
"post to received FIS structure "
"port %d signature = 0x%x",
#endif
goto out_check;
}
if (loop_count++ > AHCI_POLLRATE_PORT_TFD_ERROR) {
/*
* We are effectively timing out after 11 sec.
*/
break;
}
/* Wait for 10 millisec */
} while (((port_task_file & AHCI_TFD_ERR_MASK)
>> AHCI_TFD_ERR_SHIFT) != 0x1);
"count: %d, port_task_file = 0x%x port %d",
"has been initialized and the power-up diagnostics failed",
rval = AHCI_FAILURE;
out:
/* Clear port serror register for the port */
return (rval);
/*
* Check device status, if keep busy or COMRESET error
* do device reset to patch some SATA disks' issue
*
* For VT8251, sometimes need to do the device reset
*/
if ((port_task_file & AHCI_TFD_STS_BSY) ||
(port_task_file & AHCI_TFD_STS_DRQ)) {
"need to do device reset", port);
if (port_task_file & AHCI_TFD_STS_BSY ||
"port_task_file = 0x%x", instance,
rval = AHCI_FAILURE;
}
}
goto out;
}
/*
* 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;
/* 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");
return (AHCI_FAILURE);
}
/* Only check implemented ports */
continue;
}
!= AHCI_SUCCESS) {
rval = AHCI_FAILURE;
"ahci_hba_reset: port %d failed", port);
}
}
/*
* Indicate that system software is AHCI aware by setting
* GHC.AE to 1
*/
return (rval);
}
/*
* This routine is only called from AHCI_ATTACH or phyrdy change
* case. It first calls port reset to initialize port, probe port and probe
* device, then try to read PxSIG register to find the type of device
* attached to the port.
*
* WARNING!!! ahciport_mutex should be acquired before the function
* is called. And the port interrupt is disabled.
*/
static void
{
"ahci_find_dev_signature enter: port %d", port);
/* Call port reset to check link status and get device signature */
"ahci_find_dev_signature: No device is found "
"at port %d", port);
return;
}
/* Check the port state */
"ahci_find_dev_signature: port %d state 0x%x",
return;
}
"ahci_find_dev_signature: port %d signature = 0x%x",
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 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)) {
}
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
{
/* Intialize 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;
}
"_port%d", port);
/* Create the taskq for the port */
goto err_case3;
}
/* Allocate the argument for the taskq */
goto err_case4;
return (AHCI_SUCCESS);
return (AHCI_FAILURE);
}
/*
* Reverse of ahci_dealloc_port_state().
*
* WARNING!!! ahcictl_mutex should be acquired before the function
* is called.
*/
static void
{
}
/*
* 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");
return (AHCI_FAILURE);
}
&accattr,
NULL,
&ret_len,
"rcvd FIS dma mem alloc fail");
/* error.. free the dma handle. */
return (AHCI_FAILURE);
}
NULL,
NULL,
&cookie_count) != DDI_DMA_MAPPED) {
"rcvd FIS dma handle bind fail");
/* error.. free the dma handle & free the memory. */
return (AHCI_FAILURE);
}
/* Config Port Received FIS Base Address */
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");
return (AHCI_FAILURE);
}
&accattr,
NULL,
&ret_len,
"cmd list dma mem alloc fail");
/* error.. free the dma handle. */
return (AHCI_FAILURE);
}
NULL,
NULL,
&cookie_count) != DDI_DMA_MAPPED) {
"cmd list dma handle bind fail");
/* error.. free the dma handle & free the memory. */
return (AHCI_FAILURE);
}
/* Config Port Command List Base Address */
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");
goto err_out;
}
if (ddi_dma_mem_alloc(
&accattr,
NULL,
&ret_len,
NULL) {
"cmd table dma mem alloc fail");
/* error.. free the dma handle. */
goto err_out;
}
NULL,
NULL,
&cookie_count) != DDI_DMA_MAPPED) {
"cmd table dma handle bind fail");
/* 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. */
}
}
/*
* WARNING!!! ahciport_mutex should be acquired before the function
* is 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 */
goto next;
}
/*
* port_intr_enable indicates that the corresponding interrrupt
* reporting is enabled.
*/
next:
/*
* 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",
/* 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 three
* kinds of non-fatal errors at the time being:
*
* PxIS.UFS - Unknown FIS Error
* PxIS.OFS - Overflow Error
* PxIS.INFS - Interface Non-Fatal 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 */
}
/*
* 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);
}
/* 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 (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 {
}
"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;
}
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
* 2. Asynchronous notification
*
* 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.
*
* Asynchronous Notification is a feature in SATA II, which allows an
* ATAPI device to 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.
*/
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.
*/
"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);
}
/*
* 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) {
/* Things are fine now. The loss was temporary. */
"ahci_intr_phyrdy_change port %d "
"device link lost/established", port);
&sdevice,
} else {
"ahci_intr_phyrdy_change: port %d "
"device link established", port);
/* A new device has been detected. */
/* 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) {
"ahci_intr_phyrdy_change: port %d "
"device link lost", port);
ahci_portp, port);
/* 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.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_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;
/*
* 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 "
}
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 "
if (intr_status & AHCI_INTR_STATUS_TFES) {
/*
* Won't emit the error message if it is an IDENTIFY
* DEVICE command sent to an ATAPI device.
*/
SATAC_ID_DEVICE) &&
(err_byte == SATA_ERROR_ABORT))
goto out1;
/*
* Won't emit the error message if it is an ATAPI PACKET
* command
*/
goto out1;
}
}
out1:
/* Prepare the argument for the taskq */
/* Start the taskq to handle error recovery */
"failed", instance);
}
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
{
ghc_control &= ~ AHCI_HBA_GHC_IE;
}
/*
* Handle INTx and legacy interrupts.
*/
static int
{
"ahci_add_legacy_intrs enter");
/* get number of interrupts. */
"ddi_intr_get_nintrs() failed, "
return (DDI_FAILURE);
}
/* Allocate an array of interrupt handles. */
/* call ddi_intr_alloc(). */
"ddi_intr_alloc() failed, rc %d\n", rc);
return (DDI_FAILURE);
}
for (x = 0; x < actual; x++) {
}
return (DDI_FAILURE);
}
/* Get intr priority. */
"ddi_intr_get_pri() failed");
for (x = 0; x < actual; x++) {
}
return (DDI_FAILURE);
}
/* Test for high level interrupt. */
"ahci_add_legacy_intrs: Hi level intr not supported");
for (x = 0; x < actual; x++) {
}
sizeof (ddi_intr_handle_t));
return (DDI_FAILURE);
}
/* Call ddi_intr_add_handler(). */
for (x = 0; x < actual; x++) {
"ddi_intr_add_handler() failed");
for (y = 0; y < actual; y++) {
(void) ddi_intr_free(
ahci_ctlp->ahcictl_intr_htable[y]);
}
return (DDI_FAILURE);
}
}
/* Call ddi_intr_enable() for legacy interrupts. */
for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
}
return (DDI_SUCCESS);
}
/*
* Handle MSI interrupts.
*/
static int
{
"ahci_add_msi_intrs enter");
/* 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_nvail returned %d, navail() returned %d",
}
#endif
/* Allocate an array of interrupt handles. */
/* call ddi_intr_alloc(). */
"ddi_intr_alloc() failed, rc %d\n", rc);
return (DDI_FAILURE);
}
/* use interrupt count returned */
#if AHCI_DEBUG
}
#endif
/*
* Get priority for first msi, assume remaining are all the same.
*/
"ddi_intr_get_pri() failed");
/* Free already allocated intr. */
for (y = 0; y < actual; y++) {
}
return (DDI_FAILURE);
}
/* Test for high level interrupt. */
"ahci_add_msi_intrs: Hi level intr not supported");
/* Free already allocated intr. */
for (y = 0; y < actual; y++) {
}
sizeof (ddi_intr_handle_t));
return (DDI_FAILURE);
}
/* Call ddi_intr_add_handler(). */
for (x = 0; x < actual; x++) {
"ddi_intr_add_handler() failed");
/* Free already allocated intr. */
for (y = 0; y < actual; y++) {
(void) ddi_intr_free(
ahci_ctlp->ahcictl_intr_htable[y]);
}
return (DDI_FAILURE);
}
}
/* Call ddi_intr_block_enable() for MSI. */
} else {
/* Call ddi_intr_enable() for MSI non block enable. */
for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
(void) ddi_intr_enable(
ahci_ctlp->ahcictl_intr_htable[x]);
}
}
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 */
drv_usecwait(10000);
} 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 six scenarios:
* 1. Initialize the port
* 2. To abort the packet(s)
* 3. To reset the port
* 4. To activate the port
* 5. Fatal error recovery
* 6. 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;
"ahci_restart_port_wait_till_ready: port %d enter", port);
if ((flag != AHCI_PORT_INIT) &&
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_dport_reset,
* 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
*/
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_RESET", port);
} else {
}
out:
/* Start the port if not in initialization phase */
if (flag & AHCI_PORT_INIT)
return (rval);
/* SStatus tells the presence of device. */
dev_exists_end = 1;
}
/* 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);
}
/*
* 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;
"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,
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",
}
/* 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 (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 (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 (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 (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 */
/*
* 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");
goto out;
}
"ahci_get_rdlogext_data: failed spkt 0x%p",
(void *)spkt);
"the failed slot spkt is 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 */
/*
* 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;
#if AHCI_DEBUG
#endif
"ahci_fatal_error_recovery_handler enter: port %d", port);
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
/*
* 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 */
}
/*
* Handle events - fatal error recovery
*/
static void
ahci_events_handler(void *args)
{
"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", port);
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",
}
#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;
/* max number of cycles this packet should survive */
int max_life_cycles;
/* how many cycles this packet survived so far */
int watched_cycles;
"ahci_watchdog_handler entered");
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
/*
* We are overloading satapkt_hba_driver_private
* with watched_cycle count.
*
* If a packet has survived for more than it's
* max life cycles, it is a candidate for time
* out.
*/
watched_cycles = (int)(intptr_t)
ahci_watchdog_timeout - 1) /
(void *)(intptr_t)watched_cycles;
if (watched_cycles <= max_life_cycles)
goto next;
ahci_ctlp, "the current slot is %d",
/*
* 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) ||
(void *)(intptr_t)0;
} 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)
}
/*
* 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) {
}
return;
if (debug_only) {
} else if (ahci_ctlp) {
} else {
}
}
/*
* 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);
}