12fcbc377Syt /*
22fcbc377Syt  * CDDL HEADER START
32fcbc377Syt  *
42fcbc377Syt  * The contents of this file are subject to the terms of the
52fcbc377Syt  * Common Development and Distribution License (the "License").
62fcbc377Syt  * You may not use this file except in compliance with the License.
72fcbc377Syt  *
82fcbc377Syt  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92fcbc377Syt  * or http://www.opensolaris.org/os/licensing.
102fcbc377Syt  * See the License for the specific language governing permissions
112fcbc377Syt  * and limitations under the License.
122fcbc377Syt  *
132fcbc377Syt  * When distributing Covered Code, include this CDDL HEADER in each
142fcbc377Syt  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152fcbc377Syt  * If applicable, add the following below this CDDL HEADER, with the
162fcbc377Syt  * fields enclosed by brackets "[]" replaced with your own identifying
172fcbc377Syt  * information: Portions Copyright [yyyy] [name of copyright owner]
182fcbc377Syt  *
192fcbc377Syt  * CDDL HEADER END
202fcbc377Syt  */
212fcbc377Syt 
222fcbc377Syt /*
232c742e1fSying tian - Beijing China  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
242fcbc377Syt  * Use is subject to license terms.
252fcbc377Syt  */
262fcbc377Syt 
272fcbc377Syt /*
282fcbc377Syt  * AHCI (Advanced Host Controller Interface) SATA HBA Driver
2913bcbb7aSyt  *
3013bcbb7aSyt  * Power Management Support
3113bcbb7aSyt  * ------------------------
3213bcbb7aSyt  *
3313bcbb7aSyt  * At the moment, the ahci driver only implements suspend/resume to
3413bcbb7aSyt  * support Suspend to RAM on X86 feature. Device power management isn't
3513bcbb7aSyt  * implemented, link power management is disabled, and hot plug isn't
3613bcbb7aSyt  * allowed during the period from suspend to resume.
3713bcbb7aSyt  *
3813bcbb7aSyt  * For s/r support, the ahci driver only need to implement DDI_SUSPEND
3913bcbb7aSyt  * and DDI_RESUME entries, and don't need to take care of new requests
4013bcbb7aSyt  * sent down after suspend because the target driver (sd) has already
4113bcbb7aSyt  * handled these conditions, and blocked these requests. For the detailed
4213bcbb7aSyt  * information, please check with sdopen, sdclose and sdioctl routines.
4313bcbb7aSyt  *
442fcbc377Syt  */
452fcbc377Syt 
46689d74b0Syt #include <sys/note.h>
472fcbc377Syt #include <sys/scsi/scsi.h>
482fcbc377Syt #include <sys/pci.h>
49b2e3645aSying tian - Beijing China #include <sys/disp.h>
502fcbc377Syt #include <sys/sata/sata_hba.h>
512fcbc377Syt #include <sys/sata/adapters/ahci/ahcireg.h>
522fcbc377Syt #include <sys/sata/adapters/ahci/ahcivar.h>
532fcbc377Syt 
5419397407SSherry Moore /*
5519397407SSherry Moore  * This is the string displayed by modinfo, etc.
5619397407SSherry Moore  * Make sure you keep the version ID up to date!
5719397407SSherry Moore  */
5819397407SSherry Moore static char ahci_ident[] = "ahci driver";
5919397407SSherry Moore 
602fcbc377Syt /*
612fcbc377Syt  * Function prototypes for driver entry points
622fcbc377Syt  */
632fcbc377Syt static	int ahci_attach(dev_info_t *, ddi_attach_cmd_t);
642fcbc377Syt static	int ahci_detach(dev_info_t *, ddi_detach_cmd_t);
652fcbc377Syt static	int ahci_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
6619397407SSherry Moore static	int ahci_quiesce(dev_info_t *);
672fcbc377Syt 
682fcbc377Syt /*
692fcbc377Syt  * Function prototypes for SATA Framework interfaces
702fcbc377Syt  */
712fcbc377Syt static	int ahci_register_sata_hba_tran(ahci_ctl_t *, uint32_t);
722fcbc377Syt static	int ahci_unregister_sata_hba_tran(ahci_ctl_t *);
732fcbc377Syt 
742fcbc377Syt static	int ahci_tran_probe_port(dev_info_t *, sata_device_t *);
752fcbc377Syt static	int ahci_tran_start(dev_info_t *, sata_pkt_t *spkt);
762fcbc377Syt static	int ahci_tran_abort(dev_info_t *, sata_pkt_t *, int);
772fcbc377Syt static	int ahci_tran_reset_dport(dev_info_t *, sata_device_t *);
782fcbc377Syt static	int ahci_tran_hotplug_port_activate(dev_info_t *, sata_device_t *);
792fcbc377Syt static	int ahci_tran_hotplug_port_deactivate(dev_info_t *, sata_device_t *);
802fcbc377Syt #if defined(__lock_lint)
812fcbc377Syt static	int ahci_selftest(dev_info_t *, sata_device_t *);
822fcbc377Syt #endif
832fcbc377Syt 
842fcbc377Syt /*
852fcbc377Syt  * Local function prototypes
862fcbc377Syt  */
8768d33a25Syt static	int ahci_alloc_ports_state(ahci_ctl_t *);
8868d33a25Syt static	void ahci_dealloc_ports_state(ahci_ctl_t *);
892fcbc377Syt static	int ahci_alloc_port_state(ahci_ctl_t *, uint8_t);
902fcbc377Syt static	void ahci_dealloc_port_state(ahci_ctl_t *, uint8_t);
912fcbc377Syt static	int ahci_alloc_rcvd_fis(ahci_ctl_t *, ahci_port_t *, uint8_t);
92689d74b0Syt static	void ahci_dealloc_rcvd_fis(ahci_port_t *);
932fcbc377Syt static	int ahci_alloc_cmd_list(ahci_ctl_t *, ahci_port_t *, uint8_t);
942fcbc377Syt static	void ahci_dealloc_cmd_list(ahci_ctl_t *, ahci_port_t *);
952fcbc377Syt static  int ahci_alloc_cmd_tables(ahci_ctl_t *, ahci_port_t *);
962fcbc377Syt static  void ahci_dealloc_cmd_tables(ahci_ctl_t *, ahci_port_t *);
972fcbc377Syt 
9868d33a25Syt static	int ahci_initialize_controller(ahci_ctl_t *);
9968d33a25Syt static	void ahci_uninitialize_controller(ahci_ctl_t *);
1002fcbc377Syt static	int ahci_initialize_port(ahci_ctl_t *, ahci_port_t *, uint8_t);
10113bcbb7aSyt static	int ahci_config_space_init(ahci_ctl_t *);
10268d33a25Syt 
103f8a673adSying tian - Beijing China static	void ahci_drain_ports_taskq(ahci_ctl_t *);
10413bcbb7aSyt static	void ahci_disable_interface_pm(ahci_ctl_t *, uint8_t);
10568d33a25Syt static	int ahci_start_port(ahci_ctl_t *, ahci_port_t *, uint8_t);
10668d33a25Syt static	void ahci_find_dev_signature(ahci_ctl_t *, ahci_port_t *, uint8_t);
1072fcbc377Syt static	void ahci_update_sata_registers(ahci_ctl_t *, uint8_t, sata_device_t *);
1082fcbc377Syt static	int ahci_deliver_satapkt(ahci_ctl_t *, ahci_port_t *,
1092fcbc377Syt     uint8_t, sata_pkt_t *);
11068d33a25Syt static	int ahci_do_sync_start(ahci_ctl_t *, ahci_port_t *,
11168d33a25Syt     uint8_t, sata_pkt_t *);
11282263d52Syt static	int ahci_claim_free_slot(ahci_ctl_t *, ahci_port_t *, int);
11368d33a25Syt static  void ahci_copy_err_cnxt(sata_cmd_t *, ahci_fis_d2h_register_t *);
11482263d52Syt static	void ahci_copy_ncq_err_page(sata_cmd_t *,
11582263d52Syt     struct sata_ncq_error_recovery_page *);
1162fcbc377Syt static	void ahci_copy_out_regs(sata_cmd_t *, ahci_fis_d2h_register_t *);
1172fcbc377Syt 
1182fcbc377Syt static	int ahci_software_reset(ahci_ctl_t *, ahci_port_t *, uint8_t);
1192fcbc377Syt static	int ahci_hba_reset(ahci_ctl_t *);
1202fcbc377Syt static	int ahci_port_reset(ahci_ctl_t *, ahci_port_t *, uint8_t);
1212fcbc377Syt static	void ahci_reject_all_abort_pkts(ahci_ctl_t *, ahci_port_t *, uint8_t);
1222fcbc377Syt static	int ahci_reset_device_reject_pkts(ahci_ctl_t *, ahci_port_t *, uint8_t);
1232fcbc377Syt static	int ahci_reset_port_reject_pkts(ahci_ctl_t *, ahci_port_t *, uint8_t);
1242fcbc377Syt static	int ahci_reset_hba_reject_pkts(ahci_ctl_t *);
12568d33a25Syt static	int ahci_put_port_into_notrunning_state(ahci_ctl_t *, ahci_port_t *,
1262fcbc377Syt     uint8_t);
1272fcbc377Syt static	int ahci_restart_port_wait_till_ready(ahci_ctl_t *, ahci_port_t *,
12868d33a25Syt     uint8_t, int, int *);
129689d74b0Syt static	void ahci_mop_commands(ahci_ctl_t *, ahci_port_t *, uint32_t,
130689d74b0Syt     uint32_t, uint32_t, uint32_t, uint32_t);
13182263d52Syt static	uint32_t ahci_get_rdlogext_data(ahci_ctl_t *, ahci_port_t *, uint8_t);
13268d33a25Syt static void ahci_get_rqsense_data(ahci_ctl_t *, ahci_port_t *,
13368d33a25Syt     uint8_t, sata_pkt_t *);
13468d33a25Syt static	void ahci_fatal_error_recovery_handler(ahci_ctl_t *, ahci_port_t *,
13582263d52Syt     uint8_t, uint32_t);
13668d33a25Syt static	void ahci_timeout_pkts(ahci_ctl_t *, ahci_port_t *,
13782263d52Syt     uint8_t, uint32_t);
13868d33a25Syt static	void ahci_events_handler(void *);
1392fcbc377Syt static	void ahci_watchdog_handler(ahci_ctl_t *);
1402fcbc377Syt 
1412fcbc377Syt static	uint_t ahci_intr(caddr_t, caddr_t);
14282263d52Syt static	void ahci_port_intr(ahci_ctl_t *, ahci_port_t *, uint8_t);
1432c742e1fSying tian - Beijing China static	int ahci_add_intrs(ahci_ctl_t *, int);
1442fcbc377Syt static	void ahci_rem_intrs(ahci_ctl_t *);
1452fcbc377Syt static	void ahci_enable_all_intrs(ahci_ctl_t *);
1462fcbc377Syt static	void ahci_disable_all_intrs(ahci_ctl_t *);
147689d74b0Syt static	void ahci_enable_port_intrs(ahci_ctl_t *, uint8_t);
148689d74b0Syt static	void ahci_disable_port_intrs(ahci_ctl_t *, uint8_t);
1492fcbc377Syt 
150689d74b0Syt static  int ahci_intr_cmd_cmplt(ahci_ctl_t *, ahci_port_t *, uint8_t);
15168d33a25Syt static	int ahci_intr_set_device_bits(ahci_ctl_t *, ahci_port_t *, uint8_t);
1522fcbc377Syt static	int ahci_intr_port_connect_change(ahci_ctl_t *, ahci_port_t *, uint8_t);
1532fcbc377Syt static	int ahci_intr_device_mechanical_presence_status(ahci_ctl_t *,
1542fcbc377Syt     ahci_port_t *, uint8_t);
1552fcbc377Syt static	int ahci_intr_phyrdy_change(ahci_ctl_t *, ahci_port_t *, uint8_t);
15668d33a25Syt static	int ahci_intr_non_fatal_error(ahci_ctl_t *, ahci_port_t *,
15768d33a25Syt     uint8_t, uint32_t);
15868d33a25Syt static  int ahci_intr_fatal_error(ahci_ctl_t *, ahci_port_t *,
15982263d52Syt     uint8_t, uint32_t);
1602fcbc377Syt static	int ahci_intr_cold_port_detect(ahci_ctl_t *, ahci_port_t *, uint8_t);
1612fcbc377Syt 
1622fcbc377Syt static	int ahci_get_num_implemented_ports(uint32_t);
16368d33a25Syt static  void ahci_log_fatal_error_message(ahci_ctl_t *, uint8_t port,
16468d33a25Syt     uint32_t);
165a9440e8dSyt static	void ahci_log_serror_message(ahci_ctl_t *, uint8_t, uint32_t, int);
166689d74b0Syt #if AHCI_DEBUG
1672fcbc377Syt static	void ahci_log(ahci_ctl_t *, uint_t, char *, ...);
168689d74b0Syt #endif
1692fcbc377Syt 
1702fcbc377Syt 
1712fcbc377Syt /*
1722fcbc377Syt  * DMA attributes for the data buffer
1732fcbc377Syt  *
1742fcbc377Syt  * dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
1752fcbc377Syt  * does not support 64-bit addressing
1762fcbc377Syt  */
1772fcbc377Syt static ddi_dma_attr_t buffer_dma_attr = {
1782fcbc377Syt 	DMA_ATTR_V0,		/* dma_attr_version */
17968d33a25Syt 	0x0ull,			/* dma_attr_addr_lo: lowest bus address */
1802fcbc377Syt 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
1812fcbc377Syt 	0x3fffffull,		/* dma_attr_count_max i.e. for one cookie */
18268d33a25Syt 	0x2ull,			/* dma_attr_align: word aligned */
1832fcbc377Syt 	1,			/* dma_attr_burstsizes */
1842fcbc377Syt 	1,			/* dma_attr_minxfer */
1852fcbc377Syt 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
1862fcbc377Syt 	0xffffffffull,		/* dma_attr_seg */
1872fcbc377Syt 	AHCI_PRDT_NUMBER,	/* dma_attr_sgllen */
1882fcbc377Syt 	512,			/* dma_attr_granular */
1892fcbc377Syt 	0,			/* dma_attr_flags */
1902fcbc377Syt };
1912fcbc377Syt 
1922fcbc377Syt /*
1932fcbc377Syt  * DMA attributes for the rcvd FIS
1942fcbc377Syt  *
1952fcbc377Syt  * dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
1962fcbc377Syt  * does not support 64-bit addressing
1972fcbc377Syt  */
1982fcbc377Syt static ddi_dma_attr_t rcvd_fis_dma_attr = {
1992fcbc377Syt 	DMA_ATTR_V0,		/* dma_attr_version */
20068d33a25Syt 	0x0ull,			/* dma_attr_addr_lo: lowest bus address */
2012fcbc377Syt 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
2022fcbc377Syt 	0xffffffffull,		/* dma_attr_count_max i.e. for one cookie */
20368d33a25Syt 	0x100ull,		/* dma_attr_align: 256-byte aligned */
2042fcbc377Syt 	1,			/* dma_attr_burstsizes */
2052fcbc377Syt 	1,			/* dma_attr_minxfer */
2062fcbc377Syt 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
2072fcbc377Syt 	0xffffffffull,		/* dma_attr_seg */
2082fcbc377Syt 	1,			/* dma_attr_sgllen */
2092fcbc377Syt 	1,			/* dma_attr_granular */
2102fcbc377Syt 	0,			/* dma_attr_flags */
2112fcbc377Syt };
2122fcbc377Syt 
2132fcbc377Syt /*
2142fcbc377Syt  * DMA attributes for the command list
2152fcbc377Syt  *
2162fcbc377Syt  * dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
2172fcbc377Syt  * does not support 64-bit addressing
2182fcbc377Syt  */
2192fcbc377Syt static ddi_dma_attr_t cmd_list_dma_attr = {
2202fcbc377Syt 	DMA_ATTR_V0,		/* dma_attr_version */
22168d33a25Syt 	0x0ull,			/* dma_attr_addr_lo: lowest bus address */
2222fcbc377Syt 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
2232fcbc377Syt 	0xffffffffull,		/* dma_attr_count_max i.e. for one cookie */
22468d33a25Syt 	0x400ull,		/* dma_attr_align: 1K-byte aligned */
2252fcbc377Syt 	1,			/* dma_attr_burstsizes */
2262fcbc377Syt 	1,			/* dma_attr_minxfer */
2272fcbc377Syt 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
2282fcbc377Syt 	0xffffffffull,		/* dma_attr_seg */
2292fcbc377Syt 	1,			/* dma_attr_sgllen */
2302fcbc377Syt 	1,			/* dma_attr_granular */
2312fcbc377Syt 	0,			/* dma_attr_flags */
2322fcbc377Syt };
2332fcbc377Syt 
2342fcbc377Syt /*
2352fcbc377Syt  * DMA attributes for cmd tables
2362fcbc377Syt  *
2372fcbc377Syt  * dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
2382fcbc377Syt  * does not support 64-bit addressing
2392fcbc377Syt  */
2402fcbc377Syt static ddi_dma_attr_t cmd_table_dma_attr = {
2412fcbc377Syt 	DMA_ATTR_V0,		/* dma_attr_version */
24268d33a25Syt 	0x0ull,			/* dma_attr_addr_lo: lowest bus address */
2432fcbc377Syt 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
2442fcbc377Syt 	0xffffffffull,		/* dma_attr_count_max i.e. for one cookie */
24568d33a25Syt 	0x80ull,		/* dma_attr_align: 128-byte aligned */
2462fcbc377Syt 	1,			/* dma_attr_burstsizes */
2472fcbc377Syt 	1,			/* dma_attr_minxfer */
2482fcbc377Syt 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
2492fcbc377Syt 	0xffffffffull,		/* dma_attr_seg */
2502fcbc377Syt 	1,			/* dma_attr_sgllen */
2512fcbc377Syt 	1,			/* dma_attr_granular */
2522fcbc377Syt 	0,			/* dma_attr_flags */
2532fcbc377Syt };
2542fcbc377Syt 
2552fcbc377Syt 
2562fcbc377Syt /* Device access attributes */
2572fcbc377Syt static ddi_device_acc_attr_t accattr = {
2582fcbc377Syt 	DDI_DEVICE_ATTR_V0,
2592fcbc377Syt 	DDI_STRUCTURE_LE_ACC,
2602fcbc377Syt 	DDI_STRICTORDER_ACC
2612fcbc377Syt };
2622fcbc377Syt 
2632fcbc377Syt 
2642fcbc377Syt static struct dev_ops ahcictl_dev_ops = {
2652fcbc377Syt 	DEVO_REV,		/* devo_rev */
2662fcbc377Syt 	0,			/* refcnt  */
2672fcbc377Syt 	ahci_getinfo,		/* info */
2682fcbc377Syt 	nulldev,		/* identify */
2692fcbc377Syt 	nulldev,		/* probe */
2702fcbc377Syt 	ahci_attach,		/* attach */
2712fcbc377Syt 	ahci_detach,		/* detach */
2722fcbc377Syt 	nodev,			/* no reset */
2732fcbc377Syt 	(struct cb_ops *)0,	/* driver operations */
2742fcbc377Syt 	NULL,			/* bus operations */
27519397407SSherry Moore 	NULL,			/* power */
27619397407SSherry Moore 	ahci_quiesce,		/* quiesce */
2772fcbc377Syt };
2782fcbc377Syt 
2792fcbc377Syt static sata_tran_hotplug_ops_t ahci_tran_hotplug_ops = {
2802fcbc377Syt 	SATA_TRAN_HOTPLUG_OPS_REV_1,
2812fcbc377Syt 	ahci_tran_hotplug_port_activate,
2822fcbc377Syt 	ahci_tran_hotplug_port_deactivate
2832fcbc377Syt };
2842fcbc377Syt 
2852fcbc377Syt extern struct mod_ops mod_driverops;
2862fcbc377Syt 
2872fcbc377Syt static  struct modldrv modldrv = {
2882fcbc377Syt 	&mod_driverops,		/* driverops */
28919397407SSherry Moore 	ahci_ident,		/* short description */
2902fcbc377Syt 	&ahcictl_dev_ops,	/* driver ops */
2912fcbc377Syt };
2922fcbc377Syt 
2932fcbc377Syt static  struct modlinkage modlinkage = {
2942fcbc377Syt 	MODREV_1,
2952fcbc377Syt 	&modldrv,
2962fcbc377Syt 	NULL
2972fcbc377Syt };
2982fcbc377Syt 
299db2cce03Sying tian - Beijing China /* The following variables are watchdog handler related */
3002fcbc377Syt static int ahci_watchdog_timeout = 5; /* 5 seconds */
3012fcbc377Syt static int ahci_watchdog_tick;
3022fcbc377Syt 
303db2cce03Sying tian - Beijing China /*
304db2cce03Sying tian - Beijing China  * This static variable indicates the size of command table,
305db2cce03Sying tian - Beijing China  * and it's changeable with prdt number, which ahci_dma_prdt_number
306db2cce03Sying tian - Beijing China  * indicates.
307db2cce03Sying tian - Beijing China  */
3082fcbc377Syt static size_t ahci_cmd_table_size;
3092fcbc377Syt 
310db2cce03Sying tian - Beijing China /*
311db2cce03Sying tian - Beijing China  * ahci_dma_prdt_number, ahci_msi_enabled and ahci_64bit_dma_addressed
312db2cce03Sying tian - Beijing China  * are global variables, and can be changed via /etc/system file.
313db2cce03Sying tian - Beijing China  */
314db2cce03Sying tian - Beijing China 
3152fcbc377Syt /* The number of Physical Region Descriptor Table(PRDT) in Command Table */
3162fcbc377Syt int ahci_dma_prdt_number = AHCI_PRDT_NUMBER;
3172fcbc377Syt 
318db2cce03Sying tian - Beijing China /* AHCI MSI is tunable */
3192c742e1fSying tian - Beijing China boolean_t ahci_msi_enabled = B_TRUE;
3202fcbc377Syt 
321db2cce03Sying tian - Beijing China /* 64-bit dma addressing is tunable */
322db2cce03Sying tian - Beijing China boolean_t ahci_64bit_dma_addressed = B_TRUE;
323db2cce03Sying tian - Beijing China 
3242fcbc377Syt #if AHCI_DEBUG
3252fcbc377Syt uint32_t ahci_debug_flags = 0;
326689d74b0Syt 
327689d74b0Syt /* The following is needed for ahci_log() */
328689d74b0Syt static kmutex_t ahci_log_mutex;
329689d74b0Syt static char ahci_log_buf[512];
3302fcbc377Syt #endif
3312fcbc377Syt 
3322fcbc377Syt /* Opaque state pointer initialized by ddi_soft_state_init() */
3332fcbc377Syt static void *ahci_statep = NULL;
3342fcbc377Syt 
3352fcbc377Syt /*
3362fcbc377Syt  *  ahci module initialization.
3372fcbc377Syt  */
3382fcbc377Syt int
3392fcbc377Syt _init(void)
3402fcbc377Syt {
3412fcbc377Syt 	int	ret;
3422fcbc377Syt 
3432fcbc377Syt 	ret = ddi_soft_state_init(&ahci_statep, sizeof (ahci_ctl_t), 0);
3442fcbc377Syt 	if (ret != 0) {
3452fcbc377Syt 		goto err_out;
3462fcbc377Syt 	}
3472fcbc377Syt 
348689d74b0Syt #if AHCI_DEBUG
3492fcbc377Syt 	mutex_init(&ahci_log_mutex, NULL, MUTEX_DRIVER, NULL);
350689d74b0Syt #endif
3512fcbc377Syt 
3522fcbc377Syt 	if ((ret = sata_hba_init(&modlinkage)) != 0) {
353689d74b0Syt #if AHCI_DEBUG
3542fcbc377Syt 		mutex_destroy(&ahci_log_mutex);
355689d74b0Syt #endif
3562fcbc377Syt 		ddi_soft_state_fini(&ahci_statep);
3572fcbc377Syt 		goto err_out;
3582fcbc377Syt 	}
3592fcbc377Syt 
3602fcbc377Syt 	ret = mod_install(&modlinkage);
3612fcbc377Syt 	if (ret != 0) {
3622fcbc377Syt 		sata_hba_fini(&modlinkage);
363689d74b0Syt #if AHCI_DEBUG
3642fcbc377Syt 		mutex_destroy(&ahci_log_mutex);
365689d74b0Syt #endif
3662fcbc377Syt 		ddi_soft_state_fini(&ahci_statep);
3672fcbc377Syt 		goto err_out;
3682fcbc377Syt 	}
3692fcbc377Syt 
3702fcbc377Syt 	/* watchdog tick */
3712fcbc377Syt 	ahci_watchdog_tick = drv_usectohz(
3722fcbc377Syt 	    (clock_t)ahci_watchdog_timeout * 1000000);
3732fcbc377Syt 	return (ret);
3742fcbc377Syt 
3752fcbc377Syt err_out:
376a9440e8dSyt 	cmn_err(CE_WARN, "!ahci: Module init failed");
3772fcbc377Syt 	return (ret);
3782fcbc377Syt }
3792fcbc377Syt 
3802fcbc377Syt /*
3812fcbc377Syt  * ahci module uninitialize.
3822fcbc377Syt  */
3832fcbc377Syt int
3842fcbc377Syt _fini(void)
3852fcbc377Syt {
3862fcbc377Syt 	int	ret;
3872fcbc377Syt 
3882fcbc377Syt 	ret = mod_remove(&modlinkage);
3892fcbc377Syt 	if (ret != 0) {
3902fcbc377Syt 		return (ret);
3912fcbc377Syt 	}
3922fcbc377Syt 
3932fcbc377Syt 	/* Remove the resources allocated in _init(). */
3942fcbc377Syt 	sata_hba_fini(&modlinkage);
395689d74b0Syt #if AHCI_DEBUG
3962fcbc377Syt 	mutex_destroy(&ahci_log_mutex);
397689d74b0Syt #endif
3982fcbc377Syt 	ddi_soft_state_fini(&ahci_statep);
3992fcbc377Syt 
4002fcbc377Syt 	return (ret);
4012fcbc377Syt }
4022fcbc377Syt 
4032fcbc377Syt /*
4042fcbc377Syt  * _info entry point
4052fcbc377Syt  */
4062fcbc377Syt int
4072fcbc377Syt _info(struct modinfo *modinfop)
4082fcbc377Syt {
4092fcbc377Syt 	return (mod_info(&modlinkage, modinfop));
4102fcbc377Syt }
4112fcbc377Syt 
4122fcbc377Syt /*
4132fcbc377Syt  * The attach entry point for dev_ops.
4142fcbc377Syt  */
4152fcbc377Syt static int
4162fcbc377Syt ahci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
4172fcbc377Syt {
4182fcbc377Syt 	ahci_ctl_t *ahci_ctlp;
4192fcbc377Syt 	int instance = ddi_get_instance(dip);
4202fcbc377Syt 	int status;
4212fcbc377Syt 	int attach_state;
4222fcbc377Syt 	uint32_t cap_status, ahci_version;
4232fcbc377Syt 	int intr_types;
42468d33a25Syt 	int i;
42595c11c1fSyt 	pci_regspec_t *regs;
42695c11c1fSyt 	int regs_length;
42795c11c1fSyt 	int rnumber;
42868d33a25Syt #if AHCI_DEBUG
42968d33a25Syt 	int speed;
43068d33a25Syt #endif
4312fcbc377Syt 
4322fcbc377Syt 	AHCIDBG0(AHCIDBG_INIT|AHCIDBG_ENTRY, NULL, "ahci_attach enter");
4332fcbc377Syt 
4342fcbc377Syt 	switch (cmd) {
4352fcbc377Syt 	case DDI_ATTACH:
4362fcbc377Syt 		break;
4372fcbc377Syt 
4382fcbc377Syt 	case DDI_RESUME:
43913bcbb7aSyt 
44013bcbb7aSyt 		/*
44113bcbb7aSyt 		 * During DDI_RESUME, the hardware state of the device
44213bcbb7aSyt 		 * (power may have been removed from the device) must be
44313bcbb7aSyt 		 * restored, allow pending requests to continue, and
44413bcbb7aSyt 		 * service new requests.
44513bcbb7aSyt 		 */
44613bcbb7aSyt 		ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
44713bcbb7aSyt 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
44813bcbb7aSyt 
44913bcbb7aSyt 		/* Restart watch thread */
45013bcbb7aSyt 		if (ahci_ctlp->ahcictl_timeout_id == 0)
45113bcbb7aSyt 			ahci_ctlp->ahcictl_timeout_id = timeout(
45213bcbb7aSyt 			    (void (*)(void *))ahci_watchdog_handler,
45313bcbb7aSyt 			    (caddr_t)ahci_ctlp, ahci_watchdog_tick);
45413bcbb7aSyt 
45513bcbb7aSyt 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
45613bcbb7aSyt 
45713bcbb7aSyt 		/*
45813bcbb7aSyt 		 * Re-initialize the controller and enable the interrupts and
45913bcbb7aSyt 		 * restart all the ports.
46013bcbb7aSyt 		 *
46113bcbb7aSyt 		 * Note that so far we don't support hot-plug during
46213bcbb7aSyt 		 * suspend/resume.
46313bcbb7aSyt 		 */
46413bcbb7aSyt 		if (ahci_initialize_controller(ahci_ctlp) != AHCI_SUCCESS) {
465a9440e8dSyt 			AHCIDBG0(AHCIDBG_ERRS|AHCIDBG_PM, ahci_ctlp,
466a9440e8dSyt 			    "Failed to initialize the controller "
467a9440e8dSyt 			    "during DDI_RESUME");
46813bcbb7aSyt 			return (DDI_FAILURE);
46913bcbb7aSyt 		}
47013bcbb7aSyt 
47113bcbb7aSyt 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
47213bcbb7aSyt 		ahci_ctlp->ahcictl_flags &= ~ AHCI_SUSPEND;
47313bcbb7aSyt 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
47413bcbb7aSyt 
47513bcbb7aSyt 		return (DDI_SUCCESS);
4762fcbc377Syt 
4772fcbc377Syt 	default:
4782fcbc377Syt 		return (DDI_FAILURE);
4792fcbc377Syt 	}
4802fcbc377Syt 
4812fcbc377Syt 	attach_state = AHCI_ATTACH_STATE_NONE;
4822fcbc377Syt 
4832fcbc377Syt 	/* Allocate soft state */
4842fcbc377Syt 	status = ddi_soft_state_zalloc(ahci_statep, instance);
4852fcbc377Syt 	if (status != DDI_SUCCESS) {
486a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: Cannot allocate soft state",
487a9440e8dSyt 		    instance);
4882fcbc377Syt 		goto err_out;
4892fcbc377Syt 	}
4902fcbc377Syt 
4912fcbc377Syt 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
49213bcbb7aSyt 	ahci_ctlp->ahcictl_flags |= AHCI_ATTACH;
4932fcbc377Syt 	ahci_ctlp->ahcictl_dip = dip;
4942fcbc377Syt 
49568d33a25Syt 	/* Initialize the cport/port mapping */
49668d33a25Syt 	for (i = 0; i < AHCI_MAX_PORTS; i++) {
49768d33a25Syt 		ahci_ctlp->ahcictl_port_to_cport[i] = 0xff;
49868d33a25Syt 		ahci_ctlp->ahcictl_cport_to_port[i] = 0xff;
49968d33a25Syt 	}
50068d33a25Syt 
5012fcbc377Syt 	attach_state |= AHCI_ATTACH_STATE_STATEP_ALLOC;
5022fcbc377Syt 
5032fcbc377Syt 	/*
5042fcbc377Syt 	 * Now map the AHCI base address; which includes global
5052fcbc377Syt 	 * registers and port control registers
50695c11c1fSyt 	 *
50795c11c1fSyt 	 * According to the spec, the AHCI Base Address is BAR5,
50813bcbb7aSyt 	 * but BAR0-BAR4 are optional, so we need to check which
50913bcbb7aSyt 	 * rnumber is used for BAR5.
51095c11c1fSyt 	 */
51195c11c1fSyt 
51295c11c1fSyt 	/*
51395c11c1fSyt 	 * search through DDI "reg" property for the AHCI register set
5142fcbc377Syt 	 */
51595c11c1fSyt 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
51695c11c1fSyt 	    DDI_PROP_DONTPASS, "reg", (int **)&regs,
51795c11c1fSyt 	    (uint_t *)&regs_length) != DDI_PROP_SUCCESS) {
518a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: Cannot lookup reg property",
519a9440e8dSyt 		    instance);
52095c11c1fSyt 		goto err_out;
52195c11c1fSyt 	}
52295c11c1fSyt 
52395c11c1fSyt 	/* AHCI Base Address is located at 0x24 offset */
52495c11c1fSyt 	for (rnumber = 0; rnumber < regs_length; ++rnumber) {
52595c11c1fSyt 		if ((regs[rnumber].pci_phys_hi & PCI_REG_REG_M)
52695c11c1fSyt 		    == AHCI_PCI_RNUM)
52795c11c1fSyt 			break;
52895c11c1fSyt 	}
52995c11c1fSyt 
53095c11c1fSyt 	ddi_prop_free(regs);
53195c11c1fSyt 
53295c11c1fSyt 	if (rnumber == regs_length) {
533a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: Cannot find AHCI register set",
534a9440e8dSyt 		    instance);
53595c11c1fSyt 		goto err_out;
53695c11c1fSyt 	}
53795c11c1fSyt 
53895c11c1fSyt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "rnumber = %d", rnumber);
53995c11c1fSyt 
5402fcbc377Syt 	status = ddi_regs_map_setup(dip,
54195c11c1fSyt 	    rnumber,
5422fcbc377Syt 	    (caddr_t *)&ahci_ctlp->ahcictl_ahci_addr,
5432fcbc377Syt 	    0,
5442fcbc377Syt 	    0,
5452fcbc377Syt 	    &accattr,
5462fcbc377Syt 	    &ahci_ctlp->ahcictl_ahci_acc_handle);
5472fcbc377Syt 	if (status != DDI_SUCCESS) {
548a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: Cannot map register space",
549a9440e8dSyt 		    instance);
5502fcbc377Syt 		goto err_out;
5512fcbc377Syt 	}
5522fcbc377Syt 
5532fcbc377Syt 	attach_state |= AHCI_ATTACH_STATE_REG_MAP;
5542fcbc377Syt 
5552fcbc377Syt 	/* Get the AHCI version information */
5562fcbc377Syt 	ahci_version = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5572fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_VS(ahci_ctlp));
5582fcbc377Syt 
559a9440e8dSyt 	cmn_err(CE_NOTE, "!ahci%d: hba AHCI version = %x.%x", instance,
5602fcbc377Syt 	    (ahci_version & 0xffff0000) >> 16,
5612fcbc377Syt 	    ((ahci_version & 0x0000ff00) >> 4 |
5622fcbc377Syt 	    (ahci_version & 0x000000ff)));
5632fcbc377Syt 
5642fcbc377Syt 	/* We don't support controllers whose versions are lower than 1.0 */
5652fcbc377Syt 	if (!(ahci_version & 0xffff0000)) {
566a9440e8dSyt 		cmn_err(CE_WARN, "ahci%d: Don't support AHCI HBA with lower "
567a9440e8dSyt 		    "than version 1.0", instance);
5682fcbc377Syt 		goto err_out;
5692fcbc377Syt 	}
5702fcbc377Syt 
5712fcbc377Syt 	/* Get the HBA capabilities information */
5722fcbc377Syt 	cap_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5732fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_CAP(ahci_ctlp));
5742fcbc377Syt 
5752fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "hba capabilites = 0x%x",
5762fcbc377Syt 	    cap_status);
5772fcbc377Syt 
57868d33a25Syt #if AHCI_DEBUG
57968d33a25Syt 	/* Get the interface speed supported by the HBA */
58068d33a25Syt 	speed = (cap_status & AHCI_HBA_CAP_ISS) >> AHCI_HBA_CAP_ISS_SHIFT;
58168d33a25Syt 	if (speed == 0x01) {
58268d33a25Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
58368d33a25Syt 		    "hba interface speed support: Gen 1 (1.5Gbps)");
58468d33a25Syt 	} else if (speed == 0x10) {
58568d33a25Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
58668d33a25Syt 		    "hba interface speed support: Gen 2 (3 Gbps)");
58768d33a25Syt 	} else if (speed == 0x11) {
58868d33a25Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
58968d33a25Syt 		    "hba interface speed support: Gen 3 (6 Gbps)");
59068d33a25Syt 	}
59168d33a25Syt #endif
59268d33a25Syt 
5932fcbc377Syt 	/* Get the number of command slots supported by the HBA */
5942fcbc377Syt 	ahci_ctlp->ahcictl_num_cmd_slots =
5952fcbc377Syt 	    ((cap_status & AHCI_HBA_CAP_NCS) >>
5962fcbc377Syt 	    AHCI_HBA_CAP_NCS_SHIFT) + 1;
5972fcbc377Syt 
59868d33a25Syt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "hba number of cmd slots: %d",
5992fcbc377Syt 	    ahci_ctlp->ahcictl_num_cmd_slots);
6002fcbc377Syt 
6012fcbc377Syt 	/* Get the bit map which indicates ports implemented by the HBA */
6022fcbc377Syt 	ahci_ctlp->ahcictl_ports_implemented =
6032fcbc377Syt 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
6042fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_PI(ahci_ctlp));
6052fcbc377Syt 
6062fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "hba implementation of ports: 0x%x",
6072fcbc377Syt 	    ahci_ctlp->ahcictl_ports_implemented);
6082fcbc377Syt 
60909121340Syt 	/*
61009121340Syt 	 * According to the AHCI spec, CAP.NP should indicate the maximum
61109121340Syt 	 * number of ports supported by the HBA silicon, but we found
61209121340Syt 	 * this value of ICH8 chipset only indicates the number of ports
61309121340Syt 	 * implemented (exposed) by it. Therefore, the driver should calculate
61409121340Syt 	 * the potential maximum value by checking PI register, and use
61509121340Syt 	 * the maximum of this value and CAP.NP.
61609121340Syt 	 */
61709121340Syt 	ahci_ctlp->ahcictl_num_ports = max(
61809121340Syt 	    (cap_status & AHCI_HBA_CAP_NP) + 1,
61909121340Syt 	    ddi_fls(ahci_ctlp->ahcictl_ports_implemented));
62009121340Syt 
62109121340Syt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "hba number of ports: %d",
62209121340Syt 	    ahci_ctlp->ahcictl_num_ports);
62309121340Syt 
6242fcbc377Syt 	/* Get the number of implemented ports by the HBA */
6252fcbc377Syt 	ahci_ctlp->ahcictl_num_implemented_ports =
6262fcbc377Syt 	    ahci_get_num_implemented_ports(
6272fcbc377Syt 	    ahci_ctlp->ahcictl_ports_implemented);
6282fcbc377Syt 
6292fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp,
63068d33a25Syt 	    "hba number of implemented ports: %d",
6312fcbc377Syt 	    ahci_ctlp->ahcictl_num_implemented_ports);
6322fcbc377Syt 
633a9440e8dSyt 	/* Check whether HBA supports 64bit DMA addressing */
6342fcbc377Syt 	if (!(cap_status & AHCI_HBA_CAP_S64A)) {
635a9440e8dSyt 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_32BIT_DMA;
6362fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
6372fcbc377Syt 		    "hba does not support 64-bit addressing");
6382fcbc377Syt 	}
6392fcbc377Syt 
640*0a4c4cecSXiao-Yu Zhang 	/* Checking for Support Command List Override */
641*0a4c4cecSXiao-Yu Zhang 	if (cap_status & AHCI_HBA_CAP_SCLO) {
642*0a4c4cecSXiao-Yu Zhang 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_SCLO;
643*0a4c4cecSXiao-Yu Zhang 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
644*0a4c4cecSXiao-Yu Zhang 		    "hba supports command list override.");
645*0a4c4cecSXiao-Yu Zhang 	}
646*0a4c4cecSXiao-Yu Zhang 
6472fcbc377Syt 	if (pci_config_setup(dip, &ahci_ctlp->ahcictl_pci_conf_handle)
6482fcbc377Syt 	    != DDI_SUCCESS) {
649a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: Cannot set up pci configure space",
650a9440e8dSyt 		    instance);
6512fcbc377Syt 		goto err_out;
6522fcbc377Syt 	}
6532fcbc377Syt 
65468d33a25Syt 	attach_state |= AHCI_ATTACH_STATE_PCICFG_SETUP;
65568d33a25Syt 
656db2cce03Sying tian - Beijing China 	/*
657db2cce03Sying tian - Beijing China 	 * Check the pci configuration space, and set caps. We also
658db2cce03Sying tian - Beijing China 	 * handle the hardware defect in this function.
659db2cce03Sying tian - Beijing China 	 *
660db2cce03Sying tian - Beijing China 	 * For example, force ATI SB600/SB700/SB750 to use 32-bit dma
661db2cce03Sying tian - Beijing China 	 * addressing since it doesn't support 64-bit dma though their
662db2cce03Sying tian - Beijing China 	 * registers declare they support.
663db2cce03Sying tian - Beijing China 	 */
66413bcbb7aSyt 	if (ahci_config_space_init(ahci_ctlp) == AHCI_FAILURE) {
665a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: ahci_config_space_init failed",
666a9440e8dSyt 		    instance);
66713bcbb7aSyt 		goto err_out;
66868d33a25Syt 	}
6692fcbc377Syt 
6702fcbc377Syt 	/*
6712fcbc377Syt 	 * Disable the whole controller interrupts before adding
6722fcbc377Syt 	 * interrupt handlers(s).
6732fcbc377Syt 	 */
6742fcbc377Syt 	ahci_disable_all_intrs(ahci_ctlp);
6752fcbc377Syt 
6762fcbc377Syt 	/* Get supported interrupt types */
6772fcbc377Syt 	if (ddi_intr_get_supported_types(dip, &intr_types) != DDI_SUCCESS) {
678a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: ddi_intr_get_supported_types failed",
679a9440e8dSyt 		    instance);
6802fcbc377Syt 		goto err_out;
6812fcbc377Syt 	}
6822fcbc377Syt 
6832fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
6842fcbc377Syt 	    "ddi_intr_get_supported_types() returned: 0x%x",
6852fcbc377Syt 	    intr_types);
6862fcbc377Syt 
6872fcbc377Syt 	if (ahci_msi_enabled && (intr_types & DDI_INTR_TYPE_MSI)) {
6882fcbc377Syt 		/*
6892fcbc377Syt 		 * Try MSI first, but fall back to FIXED if failed
6902fcbc377Syt 		 */
6912c742e1fSying tian - Beijing China 		if (ahci_add_intrs(ahci_ctlp, DDI_INTR_TYPE_MSI) ==
6922c742e1fSying tian - Beijing China 		    DDI_SUCCESS) {
6932fcbc377Syt 			ahci_ctlp->ahcictl_intr_type = DDI_INTR_TYPE_MSI;
6942fcbc377Syt 			AHCIDBG0(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
6952fcbc377Syt 			    "Using MSI interrupt type");
6962fcbc377Syt 			goto intr_done;
6972fcbc377Syt 		}
6982fcbc377Syt 
6992fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
7002fcbc377Syt 		    "MSI registration failed, "
7012fcbc377Syt 		    "trying FIXED interrupts");
7022fcbc377Syt 	}
7032fcbc377Syt 
7042fcbc377Syt 	if (intr_types & DDI_INTR_TYPE_FIXED) {
7052c742e1fSying tian - Beijing China 		if (ahci_add_intrs(ahci_ctlp, DDI_INTR_TYPE_FIXED) ==
7062c742e1fSying tian - Beijing China 		    DDI_SUCCESS) {
7072fcbc377Syt 			ahci_ctlp->ahcictl_intr_type = DDI_INTR_TYPE_FIXED;
708a9440e8dSyt 			AHCIDBG0(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
7092fcbc377Syt 			    "Using FIXED interrupt type");
7102fcbc377Syt 			goto intr_done;
7112fcbc377Syt 		}
7122fcbc377Syt 
7132fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
7142fcbc377Syt 		    "FIXED interrupt registration failed");
7152fcbc377Syt 	}
7162fcbc377Syt 
717a9440e8dSyt 	cmn_err(CE_WARN, "!ahci%d: Interrupt registration failed", instance);
7182fcbc377Syt 
7192fcbc377Syt 	goto err_out;
7202fcbc377Syt 
7212fcbc377Syt intr_done:
7222fcbc377Syt 
7232fcbc377Syt 	attach_state |= AHCI_ATTACH_STATE_INTR_ADDED;
7242fcbc377Syt 
7252fcbc377Syt 	/* Initialize the controller mutex */
7262fcbc377Syt 	mutex_init(&ahci_ctlp->ahcictl_mutex, NULL, MUTEX_DRIVER,
7272fcbc377Syt 	    (void *)(uintptr_t)ahci_ctlp->ahcictl_intr_pri);
7282fcbc377Syt 
7292fcbc377Syt 	attach_state |= AHCI_ATTACH_STATE_MUTEX_INIT;
7302fcbc377Syt 
7312fcbc377Syt 	if (ahci_dma_prdt_number < AHCI_MIN_PRDT_NUMBER) {
7322fcbc377Syt 		ahci_dma_prdt_number = AHCI_MIN_PRDT_NUMBER;
7332fcbc377Syt 	} else if (ahci_dma_prdt_number > AHCI_MAX_PRDT_NUMBER) {
7342fcbc377Syt 		ahci_dma_prdt_number = AHCI_MAX_PRDT_NUMBER;
7352fcbc377Syt 	}
7362fcbc377Syt 
7372fcbc377Syt 	ahci_cmd_table_size = (sizeof (ahci_cmd_table_t) +
7382fcbc377Syt 	    (ahci_dma_prdt_number - AHCI_PRDT_NUMBER) *
7392fcbc377Syt 	    sizeof (ahci_prdt_item_t));
7402fcbc377Syt 
7412fcbc377Syt 	AHCIDBG2(AHCIDBG_INIT, ahci_ctlp,
7422fcbc377Syt 	    "ahci_attach: ahci_dma_prdt_number set by user is 0x%x,"
7432fcbc377Syt 	    " ahci_cmd_table_size is 0x%x",
7442fcbc377Syt 	    ahci_dma_prdt_number, ahci_cmd_table_size);
7452fcbc377Syt 
7462fcbc377Syt 	if (ahci_dma_prdt_number != AHCI_PRDT_NUMBER)
7472fcbc377Syt 		ahci_ctlp->ahcictl_buffer_dma_attr.dma_attr_sgllen =
7482fcbc377Syt 		    ahci_dma_prdt_number;
7492fcbc377Syt 
750a9440e8dSyt 	ahci_ctlp->ahcictl_buffer_dma_attr = buffer_dma_attr;
751a9440e8dSyt 	ahci_ctlp->ahcictl_rcvd_fis_dma_attr = rcvd_fis_dma_attr;
752a9440e8dSyt 	ahci_ctlp->ahcictl_cmd_list_dma_attr = cmd_list_dma_attr;
753a9440e8dSyt 	ahci_ctlp->ahcictl_cmd_table_dma_attr = cmd_table_dma_attr;
754a9440e8dSyt 
755db2cce03Sying tian - Beijing China 	if ((ahci_64bit_dma_addressed == B_FALSE) ||
756db2cce03Sying tian - Beijing China 	    (ahci_ctlp->ahcictl_cap & AHCI_CAP_32BIT_DMA)) {
757a9440e8dSyt 		ahci_ctlp->ahcictl_buffer_dma_attr.dma_attr_addr_hi =
758a9440e8dSyt 		    0xffffffffull;
759a9440e8dSyt 		ahci_ctlp->ahcictl_rcvd_fis_dma_attr.dma_attr_addr_hi =
760a9440e8dSyt 		    0xffffffffull;
761a9440e8dSyt 		ahci_ctlp->ahcictl_cmd_list_dma_attr.dma_attr_addr_hi =
762a9440e8dSyt 		    0xffffffffull;
763a9440e8dSyt 		ahci_ctlp->ahcictl_cmd_table_dma_attr.dma_attr_addr_hi =
764a9440e8dSyt 		    0xffffffffull;
765a9440e8dSyt 	}
766a9440e8dSyt 
76768d33a25Syt 	/* Allocate the ports structure */
76868d33a25Syt 	status = ahci_alloc_ports_state(ahci_ctlp);
76968d33a25Syt 	if (status != AHCI_SUCCESS) {
770a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: Cannot allocate ports structure",
771a9440e8dSyt 		    instance);
77268d33a25Syt 		goto err_out;
77368d33a25Syt 	}
77468d33a25Syt 
77568d33a25Syt 	attach_state |= AHCI_ATTACH_STATE_PORT_ALLOC;
77668d33a25Syt 
7772fcbc377Syt 	/*
77868d33a25Syt 	 * Initialize the controller and ports.
7792fcbc377Syt 	 */
7802fcbc377Syt 	status = ahci_initialize_controller(ahci_ctlp);
7812fcbc377Syt 	if (status != AHCI_SUCCESS) {
782a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: HBA initialization failed",
783a9440e8dSyt 		    instance);
7842fcbc377Syt 		goto err_out;
7852fcbc377Syt 	}
7862fcbc377Syt 
7872fcbc377Syt 	attach_state |= AHCI_ATTACH_STATE_HW_INIT;
7882fcbc377Syt 
7892fcbc377Syt 	/* Start one thread to check packet timeouts */
7902fcbc377Syt 	ahci_ctlp->ahcictl_timeout_id = timeout(
7912fcbc377Syt 	    (void (*)(void *))ahci_watchdog_handler,
7922fcbc377Syt 	    (caddr_t)ahci_ctlp, ahci_watchdog_tick);
7932fcbc377Syt 
7942fcbc377Syt 	attach_state |= AHCI_ATTACH_STATE_TIMEOUT_ENABLED;
7952fcbc377Syt 
7962fcbc377Syt 	if (ahci_register_sata_hba_tran(ahci_ctlp, cap_status)) {
797a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: sata hba tran registration failed",
798a9440e8dSyt 		    instance);
7992fcbc377Syt 		goto err_out;
8002fcbc377Syt 	}
8012fcbc377Syt 
80213bcbb7aSyt 	ahci_ctlp->ahcictl_flags &= ~AHCI_ATTACH;
80313bcbb7aSyt 
8042fcbc377Syt 	AHCIDBG0(AHCIDBG_INIT, ahci_ctlp, "ahci_attach success!");
8052fcbc377Syt 
8062fcbc377Syt 	return (DDI_SUCCESS);
8072fcbc377Syt 
8082fcbc377Syt err_out:
8092fcbc377Syt 	if (attach_state & AHCI_ATTACH_STATE_TIMEOUT_ENABLED) {
8102fcbc377Syt 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
8112fcbc377Syt 		(void) untimeout(ahci_ctlp->ahcictl_timeout_id);
8122fcbc377Syt 		ahci_ctlp->ahcictl_timeout_id = 0;
8132fcbc377Syt 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
8142fcbc377Syt 	}
8152fcbc377Syt 
8162fcbc377Syt 	if (attach_state & AHCI_ATTACH_STATE_HW_INIT) {
81768d33a25Syt 		ahci_uninitialize_controller(ahci_ctlp);
8182fcbc377Syt 	}
8192fcbc377Syt 
82068d33a25Syt 	if (attach_state & AHCI_ATTACH_STATE_PORT_ALLOC) {
82168d33a25Syt 		ahci_dealloc_ports_state(ahci_ctlp);
82268d33a25Syt 	}
82368d33a25Syt 
8242fcbc377Syt 	if (attach_state & AHCI_ATTACH_STATE_MUTEX_INIT) {
8252fcbc377Syt 		mutex_destroy(&ahci_ctlp->ahcictl_mutex);
8262fcbc377Syt 	}
8272fcbc377Syt 
8282fcbc377Syt 	if (attach_state & AHCI_ATTACH_STATE_INTR_ADDED) {
8292fcbc377Syt 		ahci_rem_intrs(ahci_ctlp);
8302fcbc377Syt 	}
8312fcbc377Syt 
83268d33a25Syt 	if (attach_state & AHCI_ATTACH_STATE_PCICFG_SETUP) {
83368d33a25Syt 		pci_config_teardown(&ahci_ctlp->ahcictl_pci_conf_handle);
83468d33a25Syt 	}
83568d33a25Syt 
8362fcbc377Syt 	if (attach_state & AHCI_ATTACH_STATE_REG_MAP) {
8372fcbc377Syt 		ddi_regs_map_free(&ahci_ctlp->ahcictl_ahci_acc_handle);
8382fcbc377Syt 	}
8392fcbc377Syt 
8402fcbc377Syt 	if (attach_state & AHCI_ATTACH_STATE_STATEP_ALLOC) {
8412fcbc377Syt 		ddi_soft_state_free(ahci_statep, instance);
8422fcbc377Syt 	}
8432fcbc377Syt 
8442fcbc377Syt 	return (DDI_FAILURE);
8452fcbc377Syt }
8462fcbc377Syt 
8472fcbc377Syt /*
8482fcbc377Syt  * The detach entry point for dev_ops.
8492fcbc377Syt  */
8502fcbc377Syt static int
8512fcbc377Syt ahci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
8522fcbc377Syt {
8532fcbc377Syt 	ahci_ctl_t *ahci_ctlp;
8542fcbc377Syt 	int instance;
8552fcbc377Syt 	int ret;
8562fcbc377Syt 
8572fcbc377Syt 	instance = ddi_get_instance(dip);
8582fcbc377Syt 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
8592fcbc377Syt 
8602fcbc377Syt 	AHCIDBG0(AHCIDBG_ENTRY, ahci_ctlp, "ahci_detach enter");
8612fcbc377Syt 
8622fcbc377Syt 	switch (cmd) {
8632fcbc377Syt 	case DDI_DETACH:
86413bcbb7aSyt 
8652fcbc377Syt 		/* disable the interrupts for an uninterrupted detach */
8662fcbc377Syt 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
8672fcbc377Syt 		ahci_disable_all_intrs(ahci_ctlp);
8682fcbc377Syt 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
8692fcbc377Syt 
8702fcbc377Syt 		/* unregister from the sata framework. */
8712fcbc377Syt 		ret = ahci_unregister_sata_hba_tran(ahci_ctlp);
8722fcbc377Syt 		if (ret != AHCI_SUCCESS) {
8732fcbc377Syt 			mutex_enter(&ahci_ctlp->ahcictl_mutex);
8742fcbc377Syt 			ahci_enable_all_intrs(ahci_ctlp);
8752fcbc377Syt 			mutex_exit(&ahci_ctlp->ahcictl_mutex);
8762fcbc377Syt 			return (DDI_FAILURE);
8772fcbc377Syt 		}
8782fcbc377Syt 
8792fcbc377Syt 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
8802fcbc377Syt 
8812fcbc377Syt 		/* stop the watchdog handler */
8822fcbc377Syt 		(void) untimeout(ahci_ctlp->ahcictl_timeout_id);
8832fcbc377Syt 		ahci_ctlp->ahcictl_timeout_id = 0;
8842fcbc377Syt 
88568d33a25Syt 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
88668d33a25Syt 
88768d33a25Syt 		/* uninitialize the controller */
88868d33a25Syt 		ahci_uninitialize_controller(ahci_ctlp);
8892fcbc377Syt 
89068d33a25Syt 		/* remove the interrupts */
89168d33a25Syt 		ahci_rem_intrs(ahci_ctlp);
8922fcbc377Syt 
89368d33a25Syt 		/* deallocate the ports structures */
89468d33a25Syt 		ahci_dealloc_ports_state(ahci_ctlp);
89568d33a25Syt 
89668d33a25Syt 		/* destroy mutex */
8972fcbc377Syt 		mutex_destroy(&ahci_ctlp->ahcictl_mutex);
8982fcbc377Syt 
89968d33a25Syt 		/* teardown the pci config */
90068d33a25Syt 		pci_config_teardown(&ahci_ctlp->ahcictl_pci_conf_handle);
9012fcbc377Syt 
9022fcbc377Syt 		/* remove the reg maps. */
9032fcbc377Syt 		ddi_regs_map_free(&ahci_ctlp->ahcictl_ahci_acc_handle);
9042fcbc377Syt 
9052fcbc377Syt 		/* free the soft state. */
9062fcbc377Syt 		ddi_soft_state_free(ahci_statep, instance);
9072fcbc377Syt 
9082fcbc377Syt 		return (DDI_SUCCESS);
9092fcbc377Syt 
9102fcbc377Syt 	case DDI_SUSPEND:
91113bcbb7aSyt 
91213bcbb7aSyt 		/*
91313bcbb7aSyt 		 * The steps associated with suspension must include putting
91413bcbb7aSyt 		 * the underlying device into a quiescent state so that it
91513bcbb7aSyt 		 * will not generate interrupts or modify or access memory.
91613bcbb7aSyt 		 */
91713bcbb7aSyt 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
91813bcbb7aSyt 		if (ahci_ctlp->ahcictl_flags & AHCI_SUSPEND) {
91913bcbb7aSyt 			mutex_exit(&ahci_ctlp->ahcictl_mutex);
92013bcbb7aSyt 			return (DDI_SUCCESS);
92113bcbb7aSyt 		}
92213bcbb7aSyt 
92313bcbb7aSyt 		ahci_ctlp->ahcictl_flags |= AHCI_SUSPEND;
92413bcbb7aSyt 
92513bcbb7aSyt 		/* stop the watchdog handler */
92613bcbb7aSyt 		if (ahci_ctlp->ahcictl_timeout_id) {
92713bcbb7aSyt 			(void) untimeout(ahci_ctlp->ahcictl_timeout_id);
92813bcbb7aSyt 			ahci_ctlp->ahcictl_timeout_id = 0;
92913bcbb7aSyt 		}
93013bcbb7aSyt 
93113bcbb7aSyt 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
93213bcbb7aSyt 
93313bcbb7aSyt 		/*
93413bcbb7aSyt 		 * drain the taskq
93513bcbb7aSyt 		 */
936f8a673adSying tian - Beijing China 		ahci_drain_ports_taskq(ahci_ctlp);
93713bcbb7aSyt 
93813bcbb7aSyt 		/*
93913bcbb7aSyt 		 * Disable the interrupts and stop all the ports.
94013bcbb7aSyt 		 */
94113bcbb7aSyt 		ahci_uninitialize_controller(ahci_ctlp);
94213bcbb7aSyt 
94313bcbb7aSyt 		return (DDI_SUCCESS);
9442fcbc377Syt 
9452fcbc377Syt 	default:
9462fcbc377Syt 		return (DDI_FAILURE);
9472fcbc377Syt 	}
9482fcbc377Syt }
9492fcbc377Syt 
9502fcbc377Syt /*
9512fcbc377Syt  * The info entry point for dev_ops.
9522fcbc377Syt  *
9532fcbc377Syt  */
9542fcbc377Syt static int
9552fcbc377Syt ahci_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
9562fcbc377Syt 		    void *arg, void **result)
9572fcbc377Syt {
9582fcbc377Syt #ifndef __lock_lint
9592fcbc377Syt 	_NOTE(ARGUNUSED(dip))
9602fcbc377Syt #endif /* __lock_lint */
9612fcbc377Syt 
9622fcbc377Syt 	ahci_ctl_t *ahci_ctlp;
9632fcbc377Syt 	int instance;
9642fcbc377Syt 	dev_t dev;
9652fcbc377Syt 
9662fcbc377Syt 	dev = (dev_t)arg;
9672fcbc377Syt 	instance = getminor(dev);
9682fcbc377Syt 
9692fcbc377Syt 	switch (infocmd) {
9702fcbc377Syt 		case DDI_INFO_DEVT2DEVINFO:
9712fcbc377Syt 			ahci_ctlp = ddi_get_soft_state(ahci_statep,  instance);
9722fcbc377Syt 			if (ahci_ctlp != NULL) {
9732fcbc377Syt 				*result = ahci_ctlp->ahcictl_dip;
9742fcbc377Syt 				return (DDI_SUCCESS);
9752fcbc377Syt 			} else {
9762fcbc377Syt 				*result = NULL;
9772fcbc377Syt 				return (DDI_FAILURE);
9782fcbc377Syt 			}
9792fcbc377Syt 		case DDI_INFO_DEVT2INSTANCE:
9802fcbc377Syt 			*(int *)result = instance;
9812fcbc377Syt 			break;
9822fcbc377Syt 		default:
9832fcbc377Syt 			break;
9842fcbc377Syt 	}
9852fcbc377Syt 
9862fcbc377Syt 	return (DDI_SUCCESS);
9872fcbc377Syt }
9882fcbc377Syt 
9892fcbc377Syt /*
9902fcbc377Syt  * Registers the ahci with sata framework.
9912fcbc377Syt  */
9922fcbc377Syt static int
9932fcbc377Syt ahci_register_sata_hba_tran(ahci_ctl_t *ahci_ctlp, uint32_t cap_status)
9942fcbc377Syt {
9952fcbc377Syt 	struct 	sata_hba_tran	*sata_hba_tran;
9962fcbc377Syt 
9972fcbc377Syt 	AHCIDBG0(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
9982fcbc377Syt 	    "ahci_register_sata_hba_tran enter");
9992fcbc377Syt 
10002fcbc377Syt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
10012fcbc377Syt 
10022fcbc377Syt 	/* Allocate memory for the sata_hba_tran  */
10032fcbc377Syt 	sata_hba_tran = kmem_zalloc(sizeof (sata_hba_tran_t), KM_SLEEP);
10042fcbc377Syt 
100568d33a25Syt 	sata_hba_tran->sata_tran_hba_rev = SATA_TRAN_HBA_REV_2;
10062fcbc377Syt 	sata_hba_tran->sata_tran_hba_dip = ahci_ctlp->ahcictl_dip;
10072fcbc377Syt 	sata_hba_tran->sata_tran_hba_dma_attr =
10082fcbc377Syt 	    &ahci_ctlp->ahcictl_buffer_dma_attr;
10092fcbc377Syt 
10102fcbc377Syt 	/* Report the number of implemented ports */
10112fcbc377Syt 	sata_hba_tran->sata_tran_hba_num_cports =
10122fcbc377Syt 	    ahci_ctlp->ahcictl_num_implemented_ports;
10132fcbc377Syt 
101468d33a25Syt 	/* Support ATAPI device */
101568d33a25Syt 	sata_hba_tran->sata_tran_hba_features_support = SATA_CTLF_ATAPI;
10162fcbc377Syt 
10172fcbc377Syt 	/* Get the data transfer capability for PIO command by the HBA */
10182fcbc377Syt 	if (cap_status & AHCI_HBA_CAP_PMD) {
101982263d52Syt 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_PIO_MDRQ;
10202fcbc377Syt 		AHCIDBG0(AHCIDBG_INFO, ahci_ctlp, "HBA supports multiple "
10212fcbc377Syt 		    "DRQ block data transfer for PIO command protocol");
10222fcbc377Syt 	}
10232fcbc377Syt 
102482263d52Syt 	/*
102582263d52Syt 	 * According to the AHCI spec, the ATA/ATAPI-7 queued feature set
102682263d52Syt 	 * is not supported by AHCI (including the READ QUEUED (EXT), WRITE
102782263d52Syt 	 * QUEUED (EXT), and SERVICE commands). Queued operations are
102882263d52Syt 	 * supported in AHCI using the READ FPDMA QUEUED and WRITE FPDMA
102982263d52Syt 	 * QUEUED commands when the HBA and device support native command
103082263d52Syt 	 * queuing(NCQ).
103182263d52Syt 	 *
103282263d52Syt 	 * SATA_CTLF_NCQ will be set to sata_tran_hba_features_support if the
103382263d52Syt 	 * CAP register of the HBA indicates NCQ is supported.
103482263d52Syt 	 *
103582263d52Syt 	 * SATA_CTLF_NCQ cannot be set if AHCI_CAP_NO_MCMDLIST_NONQUEUE is
103682263d52Syt 	 * set because the previous register content of PxCI can be re-written
103782263d52Syt 	 * in the register write.
103882263d52Syt 	 */
103982263d52Syt 	if ((cap_status & AHCI_HBA_CAP_SNCQ) &&
104082263d52Syt 	    !(ahci_ctlp->ahcictl_cap & AHCI_CAP_NO_MCMDLIST_NONQUEUE)) {
104182263d52Syt 		sata_hba_tran->sata_tran_hba_features_support |= SATA_CTLF_NCQ;
104282263d52Syt 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_NCQ;
104382263d52Syt 		AHCIDBG0(AHCIDBG_INFO, ahci_ctlp, "HBA supports Native "
104482263d52Syt 		    "Command Queuing");
104582263d52Syt 	}
104682263d52Syt 
10472fcbc377Syt 	/* Report the number of command slots */
10482fcbc377Syt 	sata_hba_tran->sata_tran_hba_qdepth = ahci_ctlp->ahcictl_num_cmd_slots;
10492fcbc377Syt 
10502fcbc377Syt 	sata_hba_tran->sata_tran_probe_port = ahci_tran_probe_port;
10512fcbc377Syt 	sata_hba_tran->sata_tran_start = ahci_tran_start;
10522fcbc377Syt 	sata_hba_tran->sata_tran_abort = ahci_tran_abort;
10532fcbc377Syt 	sata_hba_tran->sata_tran_reset_dport = ahci_tran_reset_dport;
10542fcbc377Syt 	sata_hba_tran->sata_tran_hotplug_ops = &ahci_tran_hotplug_ops;
10552fcbc377Syt #ifdef __lock_lint
10562fcbc377Syt 	sata_hba_tran->sata_tran_selftest = ahci_selftest;
10572fcbc377Syt #endif
10582fcbc377Syt 	/*
10592fcbc377Syt 	 * When SATA framework adds support for pwrmgt the
10602fcbc377Syt 	 * pwrmgt_ops needs to be updated
10612fcbc377Syt 	 */
10622fcbc377Syt 	sata_hba_tran->sata_tran_pwrmgt_ops = NULL;
10632fcbc377Syt 	sata_hba_tran->sata_tran_ioctl = NULL;
10642fcbc377Syt 
10652fcbc377Syt 	ahci_ctlp->ahcictl_sata_hba_tran = sata_hba_tran;
10662fcbc377Syt 
10672fcbc377Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
10682fcbc377Syt 
10692fcbc377Syt 	/* Attach it to SATA framework */
10702fcbc377Syt 	if (sata_hba_attach(ahci_ctlp->ahcictl_dip, sata_hba_tran, DDI_ATTACH)
10712fcbc377Syt 	    != DDI_SUCCESS) {
10722fcbc377Syt 		kmem_free((void *)sata_hba_tran, sizeof (sata_hba_tran_t));
10732fcbc377Syt 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
10742fcbc377Syt 		ahci_ctlp->ahcictl_sata_hba_tran = NULL;
10752fcbc377Syt 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
10762fcbc377Syt 		return (AHCI_FAILURE);
10772fcbc377Syt 	}
10782fcbc377Syt 
10792fcbc377Syt 	return (AHCI_SUCCESS);
10802fcbc377Syt }
10812fcbc377Syt 
10822fcbc377Syt /*
10832fcbc377Syt  * Unregisters the ahci with sata framework.
10842fcbc377Syt  */
10852fcbc377Syt static int
10862fcbc377Syt ahci_unregister_sata_hba_tran(ahci_ctl_t *ahci_ctlp)
10872fcbc377Syt {
10882fcbc377Syt 	AHCIDBG0(AHCIDBG_ENTRY, ahci_ctlp,
10892fcbc377Syt 	    "ahci_unregister_sata_hba_tran enter");
10902fcbc377Syt 
10912fcbc377Syt 	/* Detach from the SATA framework. */
10922fcbc377Syt 	if (sata_hba_detach(ahci_ctlp->ahcictl_dip, DDI_DETACH) !=
10932fcbc377Syt 	    DDI_SUCCESS) {
10942fcbc377Syt 		return (AHCI_FAILURE);
10952fcbc377Syt 	}
10962fcbc377Syt 
10972fcbc377Syt 	/* Deallocate sata_hba_tran. */
10982fcbc377Syt 	kmem_free((void *)ahci_ctlp->ahcictl_sata_hba_tran,
10992fcbc377Syt 	    sizeof (sata_hba_tran_t));
11002fcbc377Syt 
11012fcbc377Syt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
11022fcbc377Syt 	ahci_ctlp->ahcictl_sata_hba_tran = NULL;
11032fcbc377Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
11042fcbc377Syt 
11052fcbc377Syt 	return (AHCI_SUCCESS);
11062fcbc377Syt }
11072fcbc377Syt 
11082fcbc377Syt /*
11092fcbc377Syt  * ahci_tran_probe_port is called by SATA framework. It returns port state,
11102fcbc377Syt  * port status registers and an attached device type via sata_device
11112fcbc377Syt  * structure.
11122fcbc377Syt  *
11132fcbc377Syt  * We return the cached information from a previous hardware probe. The
11142fcbc377Syt  * actual hardware probing itself was done either from within
11152fcbc377Syt  * ahci_initialize_controller() during the driver attach or from a phy
11162fcbc377Syt  * ready change interrupt handler.
11172fcbc377Syt  */
11182fcbc377Syt static int
11192fcbc377Syt ahci_tran_probe_port(dev_info_t *dip, sata_device_t *sd)
11202fcbc377Syt {
11212fcbc377Syt 	ahci_ctl_t *ahci_ctlp;
11222fcbc377Syt 	ahci_port_t *ahci_portp;
11232fcbc377Syt 	uint8_t	cport = sd->satadev_addr.cport;
1124689d74b0Syt #if AHCI_DEBUG
11252fcbc377Syt 	uint8_t pmport = sd->satadev_addr.pmport;
11262fcbc377Syt 	uint8_t qual = sd->satadev_addr.qual;
1127689d74b0Syt #endif
11282fcbc377Syt 	uint8_t	device_type;
11292fcbc377Syt 	uint32_t port_state;
11302fcbc377Syt 	uint8_t port;
11312fcbc377Syt 
11322fcbc377Syt 	ahci_ctlp = ddi_get_soft_state(ahci_statep, ddi_get_instance(dip));
11332fcbc377Syt 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
11342fcbc377Syt 
11352fcbc377Syt 	AHCIDBG3(AHCIDBG_ENTRY, ahci_ctlp,
113668d33a25Syt 	    "ahci_tran_probe_port enter: cport: %d, "
113768d33a25Syt 	    "pmport: %d, qual: %d", cport, pmport, qual);
11382fcbc377Syt 
11392fcbc377Syt 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
11402fcbc377Syt 
11412fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
11422fcbc377Syt 
11432fcbc377Syt 	port_state = ahci_portp->ahciport_port_state;
11442fcbc377Syt 	switch (port_state) {
11452fcbc377Syt 
11462fcbc377Syt 	case SATA_PSTATE_FAILED:
11472fcbc377Syt 		sd->satadev_state = SATA_PSTATE_FAILED;
11482fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
114968d33a25Syt 		    "ahci_tran_probe_port: port %d PORT FAILED", port);
11502fcbc377Syt 		goto out;
11512fcbc377Syt 
11522fcbc377Syt 	case SATA_PSTATE_SHUTDOWN:
11532fcbc377Syt 		sd->satadev_state = SATA_PSTATE_SHUTDOWN;
11542fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
115568d33a25Syt 		    "ahci_tran_probe_port: port %d PORT SHUTDOWN", port);
11562fcbc377Syt 		goto out;
11572fcbc377Syt 
11582fcbc377Syt 	case SATA_PSTATE_PWROFF:
11592fcbc377Syt 		sd->satadev_state = SATA_PSTATE_PWROFF;
11602fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
116168d33a25Syt 		    "ahci_tran_probe_port: port %d PORT PWROFF", port);
11622fcbc377Syt 		goto out;
11632fcbc377Syt 
11642fcbc377Syt 	case SATA_PSTATE_PWRON:
11652fcbc377Syt 		sd->satadev_state = SATA_PSTATE_PWRON;
11662fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
116768d33a25Syt 		    "ahci_tran_probe_port: port %d PORT PWRON", port);
11682fcbc377Syt 		break;
11692fcbc377Syt 
11702fcbc377Syt 	default:
11712fcbc377Syt 		sd->satadev_state = port_state;
11722fcbc377Syt 		AHCIDBG2(AHCIDBG_INFO, ahci_ctlp,
117368d33a25Syt 		    "ahci_tran_probe_port: port %d PORT NORMAL %x",
11742fcbc377Syt 		    port, port_state);
11752fcbc377Syt 		break;
11762fcbc377Syt 	}
11772fcbc377Syt 
11782fcbc377Syt 	device_type = ahci_portp->ahciport_device_type;
11792fcbc377Syt 
11802fcbc377Syt 	switch (device_type) {
11812fcbc377Syt 
11822fcbc377Syt 	case SATA_DTYPE_ATADISK:
11832fcbc377Syt 		sd->satadev_type = SATA_DTYPE_ATADISK;
11842fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
118568d33a25Syt 		    "ahci_tran_probe_port: port %d DISK found", port);
11862fcbc377Syt 		break;
11872fcbc377Syt 
118838547057Sying tian - Beijing China 	case SATA_DTYPE_ATAPI:
118938547057Sying tian - Beijing China 		/*
119038547057Sying tian - Beijing China 		 * HBA driver only knows it's an ATAPI device, and don't know
1191f8a673adSying tian - Beijing China 		 * it's CD/DVD, tape or ATAPI disk because the ATAPI device
1192f8a673adSying tian - Beijing China 		 * type need to be determined by checking IDENTIFY PACKET
1193f8a673adSying tian - Beijing China 		 * DEVICE data
119438547057Sying tian - Beijing China 		 */
119538547057Sying tian - Beijing China 		sd->satadev_type = SATA_DTYPE_ATAPI;
11962fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
119768d33a25Syt 		    "ahci_tran_probe_port: port %d ATAPI found", port);
11982fcbc377Syt 		break;
11992fcbc377Syt 
12002fcbc377Syt 	case SATA_DTYPE_PMULT:
12012fcbc377Syt 		sd->satadev_type = SATA_DTYPE_PMULT;
12022fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
120368d33a25Syt 		    "ahci_tran_probe_port: port %d Port Multiplier found",
12042fcbc377Syt 		    port);
12052fcbc377Syt 		break;
12062fcbc377Syt 
12072fcbc377Syt 	case SATA_DTYPE_UNKNOWN:
12082fcbc377Syt 		sd->satadev_type = SATA_DTYPE_UNKNOWN;
12092fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
121068d33a25Syt 		    "ahci_tran_probe_port: port %d Unknown device found", port);
12112fcbc377Syt 		break;
12122fcbc377Syt 
12132fcbc377Syt 	default:
12142fcbc377Syt 		/* we don't support any other device types */
12152fcbc377Syt 		sd->satadev_type = SATA_DTYPE_NONE;
12162fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
121768d33a25Syt 		    "ahci_tran_probe_port: port %d No device found", port);
12182fcbc377Syt 		break;
12192fcbc377Syt 	}
12202fcbc377Syt 
12212fcbc377Syt out:
12222fcbc377Syt 	ahci_update_sata_registers(ahci_ctlp, port, sd);
12232fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
12242fcbc377Syt 
12252fcbc377Syt 	return (SATA_SUCCESS);
12262fcbc377Syt }
12272fcbc377Syt 
122868d33a25Syt /*
122968d33a25Syt  * There are four operation modes in sata framework:
123068d33a25Syt  * SATA_OPMODE_INTERRUPTS
123168d33a25Syt  * SATA_OPMODE_POLLING
123268d33a25Syt  * SATA_OPMODE_ASYNCH
123368d33a25Syt  * SATA_OPMODE_SYNCH
123468d33a25Syt  *
123568d33a25Syt  * Their combined meanings as following:
123668d33a25Syt  *
123768d33a25Syt  * SATA_OPMODE_SYNCH
123868d33a25Syt  * The command has to be completed before sata_tran_start functions returns.
123968d33a25Syt  * Either interrupts or polling could be used - it's up to the driver.
124068d33a25Syt  * Mode used currently for internal, sata-module initiated operations.
124168d33a25Syt  *
124268d33a25Syt  * SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS
124368d33a25Syt  * It is the same as the one above.
124468d33a25Syt  *
124568d33a25Syt  * SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING
124668d33a25Syt  * The command has to be completed before sata_tran_start function returns.
124768d33a25Syt  * No interrupt used, polling only. This should be the mode used for scsi
124868d33a25Syt  * packets with FLAG_NOINTR.
124968d33a25Syt  *
125068d33a25Syt  * SATA_OPMODE_ASYNCH | SATA_OPMODE_INTERRUPTS
125168d33a25Syt  * The command may be queued (callback function specified). Interrupts could
125268d33a25Syt  * be used. It's normal operation mode.
125368d33a25Syt  */
12542fcbc377Syt /*
12552fcbc377Syt  * Called by sata framework to transport a sata packet down stream.
12562fcbc377Syt  */
12572fcbc377Syt static int
12582fcbc377Syt ahci_tran_start(dev_info_t *dip, sata_pkt_t *spkt)
12592fcbc377Syt {
12602fcbc377Syt 	ahci_ctl_t *ahci_ctlp;
12612fcbc377Syt 	ahci_port_t *ahci_portp;
12622fcbc377Syt 	uint8_t	cport = spkt->satapkt_device.satadev_addr.cport;
12632fcbc377Syt 	uint8_t port;
12642fcbc377Syt 
12652fcbc377Syt 	ahci_ctlp = ddi_get_soft_state(ahci_statep, ddi_get_instance(dip));
12662fcbc377Syt 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
12672fcbc377Syt 
12682fcbc377Syt 	AHCIDBG2(AHCIDBG_ENTRY, ahci_ctlp,
12692fcbc377Syt 	    "ahci_tran_start enter: cport %d satapkt 0x%p",
12702fcbc377Syt 	    cport, (void *)spkt);
12712fcbc377Syt 
12722fcbc377Syt 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
12732fcbc377Syt 
12742fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
12752fcbc377Syt 
12762fcbc377Syt 	if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED |
12772fcbc377Syt 	    ahci_portp->ahciport_port_state & SATA_PSTATE_SHUTDOWN |
12782fcbc377Syt 	    ahci_portp->ahciport_port_state & SATA_PSTATE_PWROFF) {
12792fcbc377Syt 		/*
12802fcbc377Syt 		 * In case the targer driver would send the packet before
12812fcbc377Syt 		 * sata framework can have the opportunity to process those
12822fcbc377Syt 		 * event reports.
12832fcbc377Syt 		 */
12842fcbc377Syt 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
12852fcbc377Syt 		spkt->satapkt_device.satadev_state =
12862fcbc377Syt 		    ahci_portp->ahciport_port_state;
12872fcbc377Syt 		ahci_update_sata_registers(ahci_ctlp, port,
12882fcbc377Syt 		    &spkt->satapkt_device);
12892fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
12902fcbc377Syt 		    "ahci_tran_start returning PORT_ERROR while "
12912fcbc377Syt 		    "port in FAILED/SHUTDOWN/PWROFF state: "
129268d33a25Syt 		    "port: %d", port);
12932fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
12942fcbc377Syt 		return (SATA_TRAN_PORT_ERROR);
12952fcbc377Syt 	}
12962fcbc377Syt 
12972fcbc377Syt 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
12982fcbc377Syt 		/*
12992fcbc377Syt 		 * ahci_intr_phyrdy_change() may have rendered it to
13002fcbc377Syt 		 * SATA_DTYPE_NONE.
13012fcbc377Syt 		 */
13022fcbc377Syt 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
13032fcbc377Syt 		spkt->satapkt_device.satadev_type = SATA_DTYPE_NONE;
13042fcbc377Syt 		spkt->satapkt_device.satadev_state =
13052fcbc377Syt 		    ahci_portp->ahciport_port_state;
13062fcbc377Syt 		ahci_update_sata_registers(ahci_ctlp, port,
13072fcbc377Syt 		    &spkt->satapkt_device);
13082fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
13092fcbc377Syt 		    "ahci_tran_start returning PORT_ERROR while "
131068d33a25Syt 		    "no device attached: port: %d", port);
13112fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
13122fcbc377Syt 		return (SATA_TRAN_PORT_ERROR);
13132fcbc377Syt 	}
13142fcbc377Syt 
13152fcbc377Syt 	/*
13162fcbc377Syt 	 * SATA HBA driver should remember that a device was reset and it
13172fcbc377Syt 	 * is supposed to reject any packets which do not specify either
13182fcbc377Syt 	 * SATA_IGNORE_DEV_RESET_STATE or SATA_CLEAR_DEV_RESET_STATE.
13192fcbc377Syt 	 *
13202fcbc377Syt 	 * This is to prevent a race condition when a device was arbitrarily
13212fcbc377Syt 	 * reset by the HBA driver (and lost it's setting) and a target
13222fcbc377Syt 	 * driver sending some commands to a device before the sata framework
13232fcbc377Syt 	 * has a chance to restore the device setting (such as cache enable/
13242fcbc377Syt 	 * disable or other resettable stuff).
13252fcbc377Syt 	 */
13262fcbc377Syt 	if (spkt->satapkt_cmd.satacmd_flags.sata_clear_dev_reset) {
13272fcbc377Syt 		ahci_portp->ahciport_reset_in_progress = 0;
13282fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
13292fcbc377Syt 		    "ahci_tran_start clearing the "
133068d33a25Syt 		    "reset_in_progress for port: %d", port);
13312fcbc377Syt 	}
13322fcbc377Syt 
13332fcbc377Syt 	if (ahci_portp->ahciport_reset_in_progress &&
13342fcbc377Syt 	    ! spkt->satapkt_cmd.satacmd_flags.sata_ignore_dev_reset &&
13352fcbc377Syt 	    ! ddi_in_panic()) {
13362fcbc377Syt 		spkt->satapkt_reason = SATA_PKT_BUSY;
13372fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
13382fcbc377Syt 		    "ahci_tran_start returning BUSY while "
133968d33a25Syt 		    "reset in progress: port: %d", port);
13402fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
13412fcbc377Syt 		return (SATA_TRAN_BUSY);
13422fcbc377Syt 	}
13432fcbc377Syt 
134468d33a25Syt 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
13452fcbc377Syt 		spkt->satapkt_reason = SATA_PKT_BUSY;
13462fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
13472fcbc377Syt 		    "ahci_tran_start returning BUSY while "
134868d33a25Syt 		    "mopping in progress: port: %d", port);
13492fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
13502fcbc377Syt 		return (SATA_TRAN_BUSY);
13512fcbc377Syt 	}
13522fcbc377Syt 
13532fcbc377Syt 	if (spkt->satapkt_op_mode &
135468d33a25Syt 	    (SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING)) {
1355b2e3645aSying tian - Beijing China 		/*
1356b2e3645aSying tian - Beijing China 		 * If a SYNC command to be executed in interrupt context,
1357b2e3645aSying tian - Beijing China 		 * bounce it back to sata module.
1358b2e3645aSying tian - Beijing China 		 */
1359b2e3645aSying tian - Beijing China 		if (!(spkt->satapkt_op_mode & SATA_OPMODE_POLLING) &&
1360b2e3645aSying tian - Beijing China 		    servicing_interrupt()) {
1361b2e3645aSying tian - Beijing China 			spkt->satapkt_reason = SATA_PKT_BUSY;
1362b2e3645aSying tian - Beijing China 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
1363b2e3645aSying tian - Beijing China 			    "ahci_tran_start returning BUSY while "
1364b2e3645aSying tian - Beijing China 			    "sending SYNC mode under interrupt context: "
1365b2e3645aSying tian - Beijing China 			    "port : %d", port);
1366b2e3645aSying tian - Beijing China 			mutex_exit(&ahci_portp->ahciport_mutex);
1367b2e3645aSying tian - Beijing China 			return (SATA_TRAN_BUSY);
1368b2e3645aSying tian - Beijing China 		}
1369b2e3645aSying tian - Beijing China 
137068d33a25Syt 		/* We need to do the sync start now */
137168d33a25Syt 		if (ahci_do_sync_start(ahci_ctlp, ahci_portp, port,
137268d33a25Syt 		    spkt) == AHCI_FAILURE) {
137368d33a25Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_tran_start "
137468d33a25Syt 			    "return QUEUE_FULL: port %d", port);
137568d33a25Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
137668d33a25Syt 			return (SATA_TRAN_QUEUE_FULL);
137768d33a25Syt 		}
137868d33a25Syt 	} else {
137968d33a25Syt 		/* Async start, using interrupt */
138068d33a25Syt 		if (ahci_deliver_satapkt(ahci_ctlp, ahci_portp, port, spkt)
138168d33a25Syt 		    == AHCI_FAILURE) {
138268d33a25Syt 			spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
138368d33a25Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_tran_start "
138468d33a25Syt 			    "returning QUEUE_FULL: port %d", port);
138568d33a25Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
138668d33a25Syt 			return (SATA_TRAN_QUEUE_FULL);
138768d33a25Syt 		}
13882fcbc377Syt 	}
13892fcbc377Syt 
13902fcbc377Syt 	AHCIDBG1(AHCIDBG_INFO, ahci_ctlp, "ahci_tran_start "
13912fcbc377Syt 	    "sata tran accepted: port %d", port);
13922fcbc377Syt 
139368d33a25Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
13942fcbc377Syt 	return (SATA_TRAN_ACCEPTED);
13952fcbc377Syt }
13962fcbc377Syt 
139768d33a25Syt /*
139868d33a25Syt  * SATA_OPMODE_SYNCH flag is set
139968d33a25Syt  *
140068d33a25Syt  * If SATA_OPMODE_POLLING flag is set, then we must poll the command
140168d33a25Syt  * without interrupt, otherwise we can still use the interrupt.
140268d33a25Syt  *
140368d33a25Syt  * WARNING!!! ahciport_mutex should be acquired before the function
140468d33a25Syt  * is called.
140568d33a25Syt  */
140668d33a25Syt static int
140768d33a25Syt ahci_do_sync_start(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
140868d33a25Syt     uint8_t port, sata_pkt_t *spkt)
140968d33a25Syt {
141068d33a25Syt 	int pkt_timeout_ticks;
141168d33a25Syt 	uint32_t timeout_tags;
141268d33a25Syt 	int rval;
1413a9440e8dSyt 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
141468d33a25Syt 
141568d33a25Syt 	AHCIDBG2(AHCIDBG_ENTRY, ahci_ctlp, "ahci_do_sync_start enter: "
141668d33a25Syt 	    "port %d spkt 0x%p", port, spkt);
141768d33a25Syt 
141868d33a25Syt 	if (spkt->satapkt_op_mode & SATA_OPMODE_POLLING) {
141968d33a25Syt 		ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_POLLING;
142068d33a25Syt 		if ((rval = ahci_deliver_satapkt(ahci_ctlp, ahci_portp,
142168d33a25Syt 		    port, spkt)) == AHCI_FAILURE) {
142268d33a25Syt 			ahci_portp->ahciport_flags &= ~ AHCI_PORT_FLAG_POLLING;
142368d33a25Syt 			return (rval);
142468d33a25Syt 		}
142568d33a25Syt 
142668d33a25Syt 		pkt_timeout_ticks =
142768d33a25Syt 		    drv_usectohz((clock_t)spkt->satapkt_time * 1000000);
142868d33a25Syt 
142968d33a25Syt 		while (spkt->satapkt_reason == SATA_PKT_BUSY) {
143068d33a25Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
143168d33a25Syt 
143268d33a25Syt 			/* Simulate the interrupt */
143382263d52Syt 			ahci_port_intr(ahci_ctlp, ahci_portp, port);
143468d33a25Syt 
143568d33a25Syt 			drv_usecwait(AHCI_1MS_USECS);
143668d33a25Syt 
143768d33a25Syt 			mutex_enter(&ahci_portp->ahciport_mutex);
143868d33a25Syt 			pkt_timeout_ticks -= AHCI_1MS_TICKS;
143968d33a25Syt 			if (pkt_timeout_ticks < 0) {
1440a9440e8dSyt 				cmn_err(CE_WARN, "!ahci%d: ahci_do_sync_start "
144168d33a25Syt 				    "port %d satapkt 0x%p timed out\n",
1442a9440e8dSyt 				    instance, port, (void *)spkt);
144368d33a25Syt 				timeout_tags = (0x1 << rval);
144468d33a25Syt 				mutex_exit(&ahci_portp->ahciport_mutex);
144568d33a25Syt 				ahci_timeout_pkts(ahci_ctlp, ahci_portp,
144682263d52Syt 				    port, timeout_tags);
144768d33a25Syt 				mutex_enter(&ahci_portp->ahciport_mutex);
144868d33a25Syt 			}
144968d33a25Syt 		}
145068d33a25Syt 		ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_POLLING;
145168d33a25Syt 		return (AHCI_SUCCESS);
145268d33a25Syt 
145368d33a25Syt 	} else {
145468d33a25Syt 		if ((rval = ahci_deliver_satapkt(ahci_ctlp, ahci_portp,
145568d33a25Syt 		    port, spkt)) == AHCI_FAILURE)
145668d33a25Syt 			return (rval);
145768d33a25Syt 
145882263d52Syt #if AHCI_DEBUG
145982263d52Syt 		/*
146082263d52Syt 		 * Note that the driver always uses the slot 0 to deliver
146182263d52Syt 		 * REQUEST SENSE or READ LOG EXT command
146282263d52Syt 		 */
146382263d52Syt 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
146482263d52Syt 			ASSERT(rval == 0);
146582263d52Syt #endif
146682263d52Syt 
146768d33a25Syt 		while (spkt->satapkt_reason == SATA_PKT_BUSY)
146868d33a25Syt 			cv_wait(&ahci_portp->ahciport_cv,
146968d33a25Syt 			    &ahci_portp->ahciport_mutex);
147068d33a25Syt 
147168d33a25Syt 		return (AHCI_SUCCESS);
147268d33a25Syt 	}
147368d33a25Syt }
147468d33a25Syt 
14752fcbc377Syt #define	SENDUP_PACKET(ahci_portp, satapkt, reason)			\
14762fcbc377Syt 	if (satapkt) {							\
14772fcbc377Syt 		satapkt->satapkt_reason = reason;			\
14782fcbc377Syt 		/*							\
14792fcbc377Syt 		 * We set the satapkt_reason in both sync and		\
14802fcbc377Syt 		 * non-sync cases.					\
14812fcbc377Syt 		 */							\
14822fcbc377Syt 	}								\
14832fcbc377Syt 	if (satapkt &&							\
148468d33a25Syt 	    ! (satapkt->satapkt_op_mode & SATA_OPMODE_SYNCH) &&		\
14852fcbc377Syt 	    satapkt->satapkt_comp) {					\
14862fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);		\
14872fcbc377Syt 		(*satapkt->satapkt_comp)(satapkt);			\
14882fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);		\
148968d33a25Syt 	} else {							\
149068d33a25Syt 		if (satapkt &&						\
149168d33a25Syt 		    (satapkt->satapkt_op_mode & SATA_OPMODE_SYNCH) &&	\
149268d33a25Syt 		    ! (satapkt->satapkt_op_mode & SATA_OPMODE_POLLING))	\
1493f8a673adSying tian - Beijing China 			cv_broadcast(&ahci_portp->ahciport_cv);		\
14942fcbc377Syt 	}
14952fcbc377Syt 
14962fcbc377Syt /*
149782263d52Syt  * Searches for and claims a free command slot.
149882263d52Syt  *
149982263d52Syt  * Returns:
15002fcbc377Syt  *
150182263d52Syt  * AHCI_FAILURE if failed
150282263d52Syt  *	1. if no empty slot left
150382263d52Syt  *	2. non-queued command requested while queued command(s) is outstanding
150482263d52Syt  *	3. queued command requested whild non-queued command(s) is outstanding
150582263d52Syt  *	4. HBA doesn't support multiple-use of command list while already a
150682263d52Syt  *	non-queued command is oustanding
150782263d52Syt  *
150882263d52Syt  * claimed slot number if succeeded
150982263d52Syt  *
151082263d52Syt  * NOTE: it will always return slot 0 during error recovery process for
151182263d52Syt  * REQUEST SENSE or READ LOG EXT command to simplify the algorithm.
15122fcbc377Syt  *
15132fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
15142fcbc377Syt  * is called.
15152fcbc377Syt  */
15162fcbc377Syt static int
151782263d52Syt ahci_claim_free_slot(ahci_ctl_t *ahci_ctlp,
151882263d52Syt     ahci_port_t *ahci_portp, int command_type)
15192fcbc377Syt {
15202fcbc377Syt 	uint32_t free_slots;
15212fcbc377Syt 	int slot;
15222fcbc377Syt 
152382263d52Syt 	AHCIDBG2(AHCIDBG_ENTRY, ahci_ctlp, "ahci_claim_free_slot enter "
152482263d52Syt 	    "ahciport_pending_tags = 0x%x "
152582263d52Syt 	    "ahciport_pending_ncq_tags = 0x%x",
152682263d52Syt 	    ahci_portp->ahciport_pending_tags,
152782263d52Syt 	    ahci_portp->ahciport_pending_ncq_tags);
15282fcbc377Syt 
152982263d52Syt 	/*
153082263d52Syt 	 * According to the AHCI spec, system software is responsible to
153182263d52Syt 	 * ensure that queued and non-queued commands are not mixed in
153282263d52Syt 	 * the command list.
153382263d52Syt 	 */
153482263d52Syt 	if (command_type == AHCI_NON_NCQ_CMD) {
153582263d52Syt 		/* Non-NCQ command request */
153682263d52Syt 		if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
153782263d52Syt 			AHCIDBG0(AHCIDBG_INFO|AHCIDBG_NCQ, ahci_ctlp,
153882263d52Syt 			    "ahci_claim_free_slot: there is still pending "
153982263d52Syt 			    "queued command(s) in the command list, "
154082263d52Syt 			    "so no available slot for the non-queued "
154182263d52Syt 			    "command");
154282263d52Syt 			return (AHCI_FAILURE);
154382263d52Syt 		}
154482263d52Syt 		if ((ahci_ctlp->ahcictl_cap & AHCI_CAP_NO_MCMDLIST_NONQUEUE) &&
154582263d52Syt 		    NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
154682263d52Syt 			AHCIDBG0(AHCIDBG_INFO, ahci_ctlp,
154782263d52Syt 			    "ahci_claim_free_slot: HBA cannot support multiple-"
154882263d52Syt 			    "use of the command list for non-queued commands");
154982263d52Syt 			return (AHCI_FAILURE);
155082263d52Syt 		}
155182263d52Syt 		free_slots = (~ahci_portp->ahciport_pending_tags) &
155282263d52Syt 		    AHCI_SLOT_MASK(ahci_ctlp);
155382263d52Syt 	} else if (command_type == AHCI_NCQ_CMD) {
155482263d52Syt 		/* NCQ command request */
155582263d52Syt 		if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
155682263d52Syt 			AHCIDBG0(AHCIDBG_INFO|AHCIDBG_NCQ, ahci_ctlp,
155782263d52Syt 			    "ahci_claim_free_slot: there is still pending "
155882263d52Syt 			    "non-queued command(s) in the command list, "
155982263d52Syt 			    "so no available slot for the queued command");
156082263d52Syt 			return (AHCI_FAILURE);
156182263d52Syt 		}
156282263d52Syt 		free_slots = (~ahci_portp->ahciport_pending_ncq_tags) &
156382263d52Syt 		    AHCI_NCQ_SLOT_MASK(ahci_portp);
156482263d52Syt 	} else if (command_type == AHCI_ERR_RETRI_CMD) {
156582263d52Syt 		/* Error retrieval command request */
156682263d52Syt 		AHCIDBG0(AHCIDBG_ERRS, ahci_ctlp,
156782263d52Syt 		    "ahci_claim_free_slot: slot 0 is allocated for REQUEST "
156882263d52Syt 		    "SENSE or READ LOG EXT command");
156982263d52Syt 		slot = 0;
157082263d52Syt 		goto out;
157182263d52Syt 	}
15722fcbc377Syt 
15732fcbc377Syt 	slot = ddi_ffs(free_slots) - 1;
15742fcbc377Syt 	if (slot == -1) {
15752fcbc377Syt 		AHCIDBG0(AHCIDBG_VERBOSE, ahci_ctlp,
15762fcbc377Syt 		    "ahci_claim_free_slot: no empty slots");
15772fcbc377Syt 		return (AHCI_FAILURE);
15782fcbc377Syt 	}
15792fcbc377Syt 
158082263d52Syt 	/*
158182263d52Syt 	 * According to the AHCI spec, to allow a simple mechanism for the
158282263d52Syt 	 * HBA to map command list slots to queue entries, software must
158382263d52Syt 	 * match the tag number it uses to the slot it is placing the command
158482263d52Syt 	 * in. For example, if a queued command is placed in slot 5, the tag
158582263d52Syt 	 * for that command must be 5.
158682263d52Syt 	 */
158782263d52Syt 	if (command_type == AHCI_NCQ_CMD) {
158882263d52Syt 		ahci_portp->ahciport_pending_ncq_tags |= (0x1 << slot);
158982263d52Syt 	}
159082263d52Syt 
15912fcbc377Syt 	ahci_portp->ahciport_pending_tags |= (0x1 << slot);
15922fcbc377Syt 
159382263d52Syt out:
15942fcbc377Syt 	AHCIDBG1(AHCIDBG_VERBOSE, ahci_ctlp,
15952fcbc377Syt 	    "ahci_claim_free_slot: found slot: 0x%x", slot);
15962fcbc377Syt 
15972fcbc377Syt 	return (slot);
15982fcbc377Syt }
15992fcbc377Syt 
16002fcbc377Syt /*
16012fcbc377Syt  * Builds the Command Table for the sata packet and delivers it to controller.
16022fcbc377Syt  *
16032fcbc377Syt  * Returns:
16042fcbc377Syt  * 	slot number if we can obtain a slot successfully
16052fcbc377Syt  *	otherwise, return AHCI_FAILURE
16062fcbc377Syt  *
16072fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function is called.
16082fcbc377Syt  */
16092fcbc377Syt static int
16102fcbc377Syt ahci_deliver_satapkt(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
16112fcbc377Syt     uint8_t port, sata_pkt_t *spkt)
16122fcbc377Syt {
161368d33a25Syt 	int cmd_slot;
161468d33a25Syt 	sata_cmd_t *scmd;
16152fcbc377Syt 	ahci_fis_h2d_register_t *h2d_register_fisp;
16162fcbc377Syt 	ahci_cmd_table_t *cmd_table;
16172fcbc377Syt 	ahci_cmd_header_t *cmd_header;
16182fcbc377Syt 	int ncookies;
16192fcbc377Syt 	int i;
162082263d52Syt 	int command_type = AHCI_NON_NCQ_CMD;
162182263d52Syt 	int ncq_qdepth;
1622a9440e8dSyt 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
162368d33a25Syt #if AHCI_DEBUG
162468d33a25Syt 	uint32_t *ptr;
162568d33a25Syt 	uint8_t *ptr2;
162668d33a25Syt #endif
16272fcbc377Syt 
16282fcbc377Syt 	spkt->satapkt_reason = SATA_PKT_BUSY;
16292fcbc377Syt 
163068d33a25Syt 	scmd = &spkt->satapkt_cmd;
16312fcbc377Syt 
163282263d52Syt 	/* Check if the command is a NCQ command */
163382263d52Syt 	if (scmd->satacmd_cmd_reg == SATAC_READ_FPDMA_QUEUED ||
163482263d52Syt 	    scmd->satacmd_cmd_reg == SATAC_WRITE_FPDMA_QUEUED) {
163582263d52Syt 		command_type = AHCI_NCQ_CMD;
163682263d52Syt 
163782263d52Syt 		/*
163882263d52Syt 		 * When NCQ is support, system software must determine the
163982263d52Syt 		 * maximum tag allowed by the device and the HBA, and it
164082263d52Syt 		 * must use a value not beyond of the lower bound of the two.
164182263d52Syt 		 *
164282263d52Syt 		 * Sata module is going to calculate the qdepth and send
164382263d52Syt 		 * down to HBA driver via sata_cmd.
164482263d52Syt 		 */
164582263d52Syt 		ncq_qdepth = scmd->satacmd_flags.sata_max_queue_depth + 1;
164682263d52Syt 
164782263d52Syt 		/*
164882263d52Syt 		 * At the moment, the driver doesn't support the dynamic
164982263d52Syt 		 * setting of the maximum ncq depth, and the value can be
165082263d52Syt 		 * set either during the attach or after hot-plug insertion.
165182263d52Syt 		 */
165282263d52Syt 		if (ahci_portp->ahciport_max_ncq_tags == 0) {
165382263d52Syt 			ahci_portp->ahciport_max_ncq_tags = ncq_qdepth;
165482263d52Syt 			AHCIDBG2(AHCIDBG_NCQ, ahci_ctlp,
165582263d52Syt 			    "ahci_deliver_satapkt: port %d the max tags for "
165682263d52Syt 			    "NCQ command is %d", port, ncq_qdepth);
165782263d52Syt 		} else {
165882263d52Syt 			if (ncq_qdepth != ahci_portp->ahciport_max_ncq_tags) {
1659a9440e8dSyt 				cmn_err(CE_WARN, "!ahci%d: ahci_deliver_satapkt"
1660a9440e8dSyt 				    " port %d the max tag for NCQ command is "
166182263d52Syt 				    "requested to change from %d to %d, at the"
166282263d52Syt 				    " moment the driver doesn't support the "
166382263d52Syt 				    "dynamic change so it's going to "
1664a9440e8dSyt 				    "still use the previous tag value",
1665a9440e8dSyt 				    instance, port,
166682263d52Syt 				    ahci_portp->ahciport_max_ncq_tags,
166782263d52Syt 				    ncq_qdepth);
166882263d52Syt 			}
166982263d52Syt 		}
167082263d52Syt 	}
167182263d52Syt 
167282263d52Syt 	/* Check if the command is an error retrieval command */
167382263d52Syt 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
167482263d52Syt 		command_type = AHCI_ERR_RETRI_CMD;
167582263d52Syt 
167682263d52Syt 	/* Check if there is an empty command slot */
167782263d52Syt 	cmd_slot = ahci_claim_free_slot(ahci_ctlp, ahci_portp, command_type);
167868d33a25Syt 	if (cmd_slot == AHCI_FAILURE) {
167982263d52Syt 		AHCIDBG0(AHCIDBG_INFO, ahci_ctlp, "no free command slot");
16802fcbc377Syt 		return (AHCI_FAILURE);
16812fcbc377Syt 	}
16822fcbc377Syt 
16832fcbc377Syt 	AHCIDBG4(AHCIDBG_ENTRY|AHCIDBG_INFO, ahci_ctlp,
168468d33a25Syt 	    "ahci_deliver_satapkt enter: cmd_reg: 0x%x, cmd_slot: 0x%x, "
168568d33a25Syt 	    "port: %d, satapkt: 0x%p", scmd->satacmd_cmd_reg,
168668d33a25Syt 	    cmd_slot, port, (void *)spkt);
16872fcbc377Syt 
168868d33a25Syt 	cmd_table = ahci_portp->ahciport_cmd_tables[cmd_slot];
16892fcbc377Syt 	bzero((void *)cmd_table, ahci_cmd_table_size);
16902fcbc377Syt 
169182263d52Syt 	/* For data transfer operations, it is the H2D Register FIS */
16922fcbc377Syt 	h2d_register_fisp =
16932fcbc377Syt 	    &(cmd_table->ahcict_command_fis.ahcifc_fis.ahcifc_h2d_register);
16942fcbc377Syt 
16952fcbc377Syt 	SET_FIS_TYPE(h2d_register_fisp, AHCI_H2D_REGISTER_FIS_TYPE);
16962fcbc377Syt 	if ((spkt->satapkt_device.satadev_addr.qual == SATA_ADDR_PMPORT) ||
16972fcbc377Syt 	    (spkt->satapkt_device.satadev_addr.qual == SATA_ADDR_DPMPORT)) {
16982fcbc377Syt 		SET_FIS_PMP(h2d_register_fisp,
16992fcbc377Syt 		    spkt->satapkt_device.satadev_addr.pmport);
17002fcbc377Syt 	}
17012fcbc377Syt 
17022fcbc377Syt 	SET_FIS_CDMDEVCTL(h2d_register_fisp, 1);
170368d33a25Syt 	SET_FIS_COMMAND(h2d_register_fisp, scmd->satacmd_cmd_reg);
170468d33a25Syt 	SET_FIS_FEATURES(h2d_register_fisp, scmd->satacmd_features_reg);
170568d33a25Syt 	SET_FIS_SECTOR_COUNT(h2d_register_fisp, scmd->satacmd_sec_count_lsb);
17062fcbc377Syt 
170768d33a25Syt 	switch (scmd->satacmd_addr_type) {
17082fcbc377Syt 
170982263d52Syt 	case 0:
171082263d52Syt 		/*
171182263d52Syt 		 * satacmd_addr_type will be 0 for the commands below:
171282263d52Syt 		 * 	ATAPI command
171382263d52Syt 		 * 	SATAC_IDLE_IM
171482263d52Syt 		 * 	SATAC_STANDBY_IM
171582263d52Syt 		 * 	SATAC_DOWNLOAD_MICROCODE
171682263d52Syt 		 * 	SATAC_FLUSH_CACHE
171782263d52Syt 		 * 	SATAC_SET_FEATURES
171882263d52Syt 		 * 	SATAC_SMART
171982263d52Syt 		 * 	SATAC_ID_PACKET_DEVICE
172082263d52Syt 		 * 	SATAC_ID_DEVICE
172182263d52Syt 		 */
1722689d74b0Syt 		/* FALLTHRU */
172382263d52Syt 
17242fcbc377Syt 	case ATA_ADDR_LBA:
1725689d74b0Syt 		/* FALLTHRU */
17262fcbc377Syt 
17272fcbc377Syt 	case ATA_ADDR_LBA28:
17282fcbc377Syt 		/* LBA[7:0] */
172968d33a25Syt 		SET_FIS_SECTOR(h2d_register_fisp, scmd->satacmd_lba_low_lsb);
17302fcbc377Syt 
17312fcbc377Syt 		/* LBA[15:8] */
173268d33a25Syt 		SET_FIS_CYL_LOW(h2d_register_fisp, scmd->satacmd_lba_mid_lsb);
17332fcbc377Syt 
17342fcbc377Syt 		/* LBA[23:16] */
173568d33a25Syt 		SET_FIS_CYL_HI(h2d_register_fisp, scmd->satacmd_lba_high_lsb);
17362fcbc377Syt 
17372fcbc377Syt 		/* LBA [27:24] (also called dev_head) */
173868d33a25Syt 		SET_FIS_DEV_HEAD(h2d_register_fisp, scmd->satacmd_device_reg);
17392fcbc377Syt 
17402fcbc377Syt 		break;
17412fcbc377Syt 
17422fcbc377Syt 	case ATA_ADDR_LBA48:
17432fcbc377Syt 		/* LBA[7:0] */
174468d33a25Syt 		SET_FIS_SECTOR(h2d_register_fisp, scmd->satacmd_lba_low_lsb);
17452fcbc377Syt 
17462fcbc377Syt 		/* LBA[15:8] */
174768d33a25Syt 		SET_FIS_CYL_LOW(h2d_register_fisp, scmd->satacmd_lba_mid_lsb);
17482fcbc377Syt 
17492fcbc377Syt 		/* LBA[23:16] */
175068d33a25Syt 		SET_FIS_CYL_HI(h2d_register_fisp, scmd->satacmd_lba_high_lsb);
17512fcbc377Syt 
17522fcbc377Syt 		/* LBA [31:24] */
17532fcbc377Syt 		SET_FIS_SECTOR_EXP(h2d_register_fisp,
175468d33a25Syt 		    scmd->satacmd_lba_low_msb);
17552fcbc377Syt 
17562fcbc377Syt 		/* LBA [39:32] */
17572fcbc377Syt 		SET_FIS_CYL_LOW_EXP(h2d_register_fisp,
175868d33a25Syt 		    scmd->satacmd_lba_mid_msb);
17592fcbc377Syt 
17602fcbc377Syt 		/* LBA [47:40] */
17612fcbc377Syt 		SET_FIS_CYL_HI_EXP(h2d_register_fisp,
176268d33a25Syt 		    scmd->satacmd_lba_high_msb);
17632fcbc377Syt 
17642fcbc377Syt 		/* Set dev_head */
17652fcbc377Syt 		SET_FIS_DEV_HEAD(h2d_register_fisp,
176668d33a25Syt 		    scmd->satacmd_device_reg);
17672fcbc377Syt 
17682fcbc377Syt 		/* Set the extended sector count and features */
17692fcbc377Syt 		SET_FIS_SECTOR_COUNT_EXP(h2d_register_fisp,
177068d33a25Syt 		    scmd->satacmd_sec_count_msb);
17712fcbc377Syt 		SET_FIS_FEATURES_EXP(h2d_register_fisp,
177268d33a25Syt 		    scmd->satacmd_features_reg_ext);
17732fcbc377Syt 		break;
17742fcbc377Syt 	}
17752fcbc377Syt 
177682263d52Syt 	/*
177782263d52Syt 	 * For NCQ command (READ/WRITE FPDMA QUEUED), sector count 7:0 is
177882263d52Syt 	 * filled into features field, and sector count 8:15 is filled into
177982263d52Syt 	 * features (exp) field. TAG is filled into sector field.
178082263d52Syt 	 */
178182263d52Syt 	if (command_type == AHCI_NCQ_CMD) {
178282263d52Syt 		SET_FIS_FEATURES(h2d_register_fisp,
178382263d52Syt 		    scmd->satacmd_sec_count_lsb);
178482263d52Syt 		SET_FIS_FEATURES_EXP(h2d_register_fisp,
178582263d52Syt 		    scmd->satacmd_sec_count_msb);
178682263d52Syt 
178782263d52Syt 		SET_FIS_SECTOR_COUNT(h2d_register_fisp,
178882263d52Syt 		    (cmd_slot << SATA_TAG_QUEUING_SHIFT));
178982263d52Syt 	}
179082263d52Syt 
179168d33a25Syt 	ncookies = scmd->satacmd_num_dma_cookies;
179238547057Sying tian - Beijing China 	AHCIDBG2(AHCIDBG_PRDT, ahci_ctlp,
17932fcbc377Syt 	    "ncookies = 0x%x, ahci_dma_prdt_number = 0x%x",
17942fcbc377Syt 	    ncookies, ahci_dma_prdt_number);
17952fcbc377Syt 
17962fcbc377Syt 	ASSERT(ncookies <= ahci_dma_prdt_number);
179738547057Sying tian - Beijing China 	ahci_portp->ahciport_prd_bytecounts[cmd_slot] = 0;
17982fcbc377Syt 
17992fcbc377Syt 	/* *** now fill the scatter gather list ******* */
18002fcbc377Syt 	for (i = 0; i < ncookies; i++) {
18012fcbc377Syt 		cmd_table->ahcict_prdt[i].ahcipi_data_base_addr =
180268d33a25Syt 		    scmd->satacmd_dma_cookie_list[i]._dmu._dmac_la[0];
18032fcbc377Syt 		cmd_table->ahcict_prdt[i].ahcipi_data_base_addr_upper =
180468d33a25Syt 		    scmd->satacmd_dma_cookie_list[i]._dmu._dmac_la[1];
18052fcbc377Syt 		cmd_table->ahcict_prdt[i].ahcipi_descr_info =
180668d33a25Syt 		    scmd->satacmd_dma_cookie_list[i].dmac_size - 1;
180738547057Sying tian - Beijing China 		ahci_portp->ahciport_prd_bytecounts[cmd_slot] +=
180838547057Sying tian - Beijing China 		    scmd->satacmd_dma_cookie_list[i].dmac_size;
180968d33a25Syt 	}
181068d33a25Syt 
181138547057Sying tian - Beijing China 	AHCIDBG2(AHCIDBG_PRDT, ahci_ctlp,
181238547057Sying tian - Beijing China 	    "ahciport_prd_bytecounts 0x%x for cmd_slot 0x%x",
181338547057Sying tian - Beijing China 	    ahci_portp->ahciport_prd_bytecounts[cmd_slot], cmd_slot);
181438547057Sying tian - Beijing China 
181568d33a25Syt 	/* The ACMD field is filled in for ATAPI command */
181668d33a25Syt 	if (scmd->satacmd_cmd_reg == SATAC_PACKET) {
181768d33a25Syt 		bcopy(scmd->satacmd_acdb, cmd_table->ahcict_atapi_cmd,
181868d33a25Syt 		    SATA_ATAPI_MAX_CDB_LEN);
18192fcbc377Syt 	}
18202fcbc377Syt 
18212fcbc377Syt 	/* Set Command Header in Command List */
182268d33a25Syt 	cmd_header = &ahci_portp->ahciport_cmd_list[cmd_slot];
18232fcbc377Syt 	BZERO_DESCR_INFO(cmd_header);
18242fcbc377Syt 	BZERO_PRD_BYTE_COUNT(cmd_header);
182568d33a25Syt 
182668d33a25Syt 	/* Set the number of entries in the PRD table */
18272fcbc377Syt 	SET_PRD_TABLE_LENGTH(cmd_header, ncookies);
182868d33a25Syt 
182968d33a25Syt 	/* Set the length of the command in the CFIS area */
18302fcbc377Syt 	SET_COMMAND_FIS_LENGTH(cmd_header, AHCI_H2D_REGISTER_FIS_LENGTH);
18312fcbc377Syt 
18322fcbc377Syt 	AHCIDBG1(AHCIDBG_INFO, ahci_ctlp, "command data direction is "
18332fcbc377Syt 	    "sata_data_direction = 0x%x",
183468d33a25Syt 	    scmd->satacmd_flags.sata_data_direction);
183568d33a25Syt 
183668d33a25Syt 	/* Set A bit if it is an ATAPI command */
183768d33a25Syt 	if (scmd->satacmd_cmd_reg == SATAC_PACKET)
183868d33a25Syt 		SET_ATAPI(cmd_header, AHCI_CMDHEAD_ATAPI);
18392fcbc377Syt 
184068d33a25Syt 	/* Set W bit if data is going to the device */
184168d33a25Syt 	if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE)
18422fcbc377Syt 		SET_WRITE(cmd_header, AHCI_CMDHEAD_DATA_WRITE);
18432fcbc377Syt 
184468d33a25Syt 	/*
184568d33a25Syt 	 * Set the prefetchable bit - this bit is only valid if the PRDTL
184668d33a25Syt 	 * field is non-zero or the ATAPI 'A' bit is set in the command
184768d33a25Syt 	 * header. This bit cannot be set when using native command
184868d33a25Syt 	 * queuing commands or when using FIS-based switching with a Port
184982263d52Syt 	 * multiplier.
185068d33a25Syt 	 */
185182263d52Syt 	if (command_type != AHCI_NCQ_CMD)
185282263d52Syt 		SET_PREFETCHABLE(cmd_header, AHCI_CMDHEAD_PREFETCHABLE);
18532fcbc377Syt 
18542fcbc377Syt 	/* Now remember the sata packet in ahciport_slot_pkts[]. */
185582263d52Syt 	if (!ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
185682263d52Syt 		ahci_portp->ahciport_slot_pkts[cmd_slot] = spkt;
18572fcbc377Syt 
18582fcbc377Syt 	/*
18592fcbc377Syt 	 * We are overloading satapkt_hba_driver_private with
18602fcbc377Syt 	 * watched_cycle count.
18612fcbc377Syt 	 */
18622fcbc377Syt 	spkt->satapkt_hba_driver_private = (void *)(intptr_t)0;
18632fcbc377Syt 
186468d33a25Syt #if AHCI_DEBUG
186538547057Sying tian - Beijing China 	if (ahci_debug_flags & AHCIDBG_ATACMD &&
186638547057Sying tian - Beijing China 	    scmd->satacmd_cmd_reg != SATAC_PACKET ||
186738547057Sying tian - Beijing China 	    ahci_debug_flags & AHCIDBG_ATAPICMD &&
186838547057Sying tian - Beijing China 	    scmd->satacmd_cmd_reg == SATAC_PACKET) {
186938547057Sying tian - Beijing China 
187038547057Sying tian - Beijing China 		/* Dump the command header and table */
187138547057Sying tian - Beijing China 		ahci_log(ahci_ctlp, CE_WARN, "\n");
187238547057Sying tian - Beijing China 		ahci_log(ahci_ctlp, CE_WARN, "Command header&table for spkt "
187338547057Sying tian - Beijing China 		    "0x%p cmd_reg 0x%x port %d", spkt,
187438547057Sying tian - Beijing China 		    scmd->satacmd_cmd_reg, port);
187538547057Sying tian - Beijing China 		ptr = (uint32_t *)cmd_header;
187638547057Sying tian - Beijing China 		ahci_log(ahci_ctlp, CE_WARN,
187738547057Sying tian - Beijing China 		    "  Command Header:%8x %8x %8x %8x",
187838547057Sying tian - Beijing China 		    ptr[0], ptr[1], ptr[2], ptr[3]);
187938547057Sying tian - Beijing China 
188038547057Sying tian - Beijing China 		/* Dump the H2D register FIS */
188138547057Sying tian - Beijing China 		ptr = (uint32_t *)h2d_register_fisp;
188238547057Sying tian - Beijing China 		ahci_log(ahci_ctlp, CE_WARN,
188338547057Sying tian - Beijing China 		    "  Command FIS:   %8x %8x %8x %8x",
188438547057Sying tian - Beijing China 		    ptr[0], ptr[1], ptr[2], ptr[3]);
188538547057Sying tian - Beijing China 
188638547057Sying tian - Beijing China 		/* Dump the ACMD register FIS */
188738547057Sying tian - Beijing China 		ptr2 = (uint8_t *)&(cmd_table->ahcict_atapi_cmd);
188838547057Sying tian - Beijing China 		for (i = 0; i < SATA_ATAPI_MAX_CDB_LEN/8; i++)
188938547057Sying tian - Beijing China 			if (ahci_debug_flags & AHCIDBG_ATAPICMD)
189038547057Sying tian - Beijing China 				ahci_log(ahci_ctlp, CE_WARN,
189138547057Sying tian - Beijing China 				    "  ATAPI command: %2x %2x %2x %2x "
189238547057Sying tian - Beijing China 				    "%2x %2x %2x %2x",
189338547057Sying tian - Beijing China 				    ptr2[8 * i], ptr2[8 * i + 1],
189438547057Sying tian - Beijing China 				    ptr2[8 * i + 2], ptr2[8 * i + 3],
189538547057Sying tian - Beijing China 				    ptr2[8 * i + 4], ptr2[8 * i + 5],
189638547057Sying tian - Beijing China 				    ptr2[8 * i + 6], ptr2[8 * i + 7]);
189738547057Sying tian - Beijing China 
189838547057Sying tian - Beijing China 		/* Dump the PRDT */
189938547057Sying tian - Beijing China 		for (i = 0; i < ncookies; i++) {
190038547057Sying tian - Beijing China 			ptr = (uint32_t *)&(cmd_table->ahcict_prdt[i]);
190168d33a25Syt 			ahci_log(ahci_ctlp, CE_WARN,
190238547057Sying tian - Beijing China 			    "  Cookie %d:      %8x %8x %8x %8x",
190338547057Sying tian - Beijing China 			    i, ptr[0], ptr[1], ptr[2], ptr[3]);
190438547057Sying tian - Beijing China 		}
190568d33a25Syt 	}
190668d33a25Syt #endif
190768d33a25Syt 
190868d33a25Syt 	(void) ddi_dma_sync(
190968d33a25Syt 	    ahci_portp->ahciport_cmd_tables_dma_handle[cmd_slot],
19102fcbc377Syt 	    0,
19112fcbc377Syt 	    ahci_cmd_table_size,
19122fcbc377Syt 	    DDI_DMA_SYNC_FORDEV);
19132fcbc377Syt 
19142fcbc377Syt 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_list_dma_handle,
191568d33a25Syt 	    cmd_slot * sizeof (ahci_cmd_header_t),
19162fcbc377Syt 	    sizeof (ahci_cmd_header_t),
19172fcbc377Syt 	    DDI_DMA_SYNC_FORDEV);
19182fcbc377Syt 
191982263d52Syt 	/* Set the corresponding bit in the PxSACT.DS for queued command */
192082263d52Syt 	if (command_type == AHCI_NCQ_CMD) {
192182263d52Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
192282263d52Syt 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port),
192382263d52Syt 		    (0x1 << cmd_slot));
192482263d52Syt 	}
192582263d52Syt 
19262fcbc377Syt 	/* Indicate to the HBA that a command is active. */
19272fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
19282fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port),
192968d33a25Syt 	    (0x1 << cmd_slot));
19302fcbc377Syt 
19312fcbc377Syt 	AHCIDBG1(AHCIDBG_INFO, ahci_ctlp, "ahci_deliver_satapkt "
19322fcbc377Syt 	    "exit: port %d", port);
19332fcbc377Syt 
193468d33a25Syt 	return (cmd_slot);
19352fcbc377Syt }
19362fcbc377Syt 
19372fcbc377Syt /*
19382fcbc377Syt  * Called by the sata framework to abort the previously sent packet(s).
19392fcbc377Syt  *
19402fcbc377Syt  * Reset device to abort commands.
19412fcbc377Syt  */
19422fcbc377Syt static int
19432fcbc377Syt ahci_tran_abort(dev_info_t *dip, sata_pkt_t *spkt, int flag)
19442fcbc377Syt {
19452fcbc377Syt 	ahci_ctl_t *ahci_ctlp;
19462fcbc377Syt 	ahci_port_t *ahci_portp;
194782263d52Syt 	uint32_t slot_status = 0;
194882263d52Syt 	uint32_t aborted_tags = 0;
194982263d52Syt 	uint32_t finished_tags = 0;
19502fcbc377Syt 	uint8_t cport = spkt->satapkt_device.satadev_addr.cport;
19512fcbc377Syt 	uint8_t port;
19522fcbc377Syt 	int tmp_slot;
1953a9440e8dSyt 	int instance = ddi_get_instance(dip);
19542fcbc377Syt 
1955a9440e8dSyt 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
19562fcbc377Syt 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
19572fcbc377Syt 
19582fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
195968d33a25Syt 	    "ahci_tran_abort enter: port %d", port);
19602fcbc377Syt 
19612fcbc377Syt 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
19622fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
19632fcbc377Syt 
19642fcbc377Syt 	/*
196568d33a25Syt 	 * If AHCI_PORT_FLAG_MOPPING flag is set, it means all the pending
196668d33a25Syt 	 * commands are being mopped, therefore there is nothing else to do
19672fcbc377Syt 	 */
196868d33a25Syt 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
19692fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
19702fcbc377Syt 		    "ahci_tran_abort: port %d is in "
19712fcbc377Syt 		    "mopping process, so just return directly ", port);
19722fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
19732fcbc377Syt 		return (SATA_SUCCESS);
19742fcbc377Syt 	}
19752fcbc377Syt 
19762fcbc377Syt 	if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED |
19772fcbc377Syt 	    ahci_portp->ahciport_port_state & SATA_PSTATE_SHUTDOWN |
19782fcbc377Syt 	    ahci_portp->ahciport_port_state & SATA_PSTATE_PWROFF) {
19792fcbc377Syt 		/*
19802fcbc377Syt 		 * In case the targer driver would send the request before
19812fcbc377Syt 		 * sata framework can have the opportunity to process those
19822fcbc377Syt 		 * event reports.
19832fcbc377Syt 		 */
19842fcbc377Syt 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
19852fcbc377Syt 		spkt->satapkt_device.satadev_state =
19862fcbc377Syt 		    ahci_portp->ahciport_port_state;
19872fcbc377Syt 		ahci_update_sata_registers(ahci_ctlp, port,
19882fcbc377Syt 		    &spkt->satapkt_device);
19892fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
19902fcbc377Syt 		    "ahci_tran_abort returning SATA_FAILURE while "
19912fcbc377Syt 		    "port in FAILED/SHUTDOWN/PWROFF state: "
199268d33a25Syt 		    "port: %d", port);
19932fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
19942fcbc377Syt 		return (SATA_FAILURE);
19952fcbc377Syt 	}
19962fcbc377Syt 
19972fcbc377Syt 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
19982fcbc377Syt 		/*
19992fcbc377Syt 		 * ahci_intr_phyrdy_change() may have rendered it to
20002fcbc377Syt 		 * AHCI_PORT_TYPE_NODEV.
20012fcbc377Syt 		 */
20022fcbc377Syt 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
20032fcbc377Syt 		spkt->satapkt_device.satadev_type = SATA_DTYPE_NONE;
20042fcbc377Syt 		spkt->satapkt_device.satadev_state =
20052fcbc377Syt 		    ahci_portp->ahciport_port_state;
20062fcbc377Syt 		ahci_update_sata_registers(ahci_ctlp, port,
20072fcbc377Syt 		    &spkt->satapkt_device);
20082fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
20092fcbc377Syt 		    "ahci_tran_abort returning SATA_FAILURE while "
201068d33a25Syt 		    "no device attached: port: %d", port);
20112fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
20122fcbc377Syt 		return (SATA_FAILURE);
20132fcbc377Syt 	}
20142fcbc377Syt 
20152fcbc377Syt 	if (flag == SATA_ABORT_ALL_PACKETS) {
201682263d52Syt 		if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
201782263d52Syt 			aborted_tags = ahci_portp->ahciport_pending_tags;
2018a9440e8dSyt 		else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
201982263d52Syt 			aborted_tags = ahci_portp->ahciport_pending_ncq_tags;
202082263d52Syt 
2021a9440e8dSyt 		cmn_err(CE_NOTE, "!ahci%d: ahci port %d abort all packets",
2022a9440e8dSyt 		    instance, port);
20232fcbc377Syt 	} else {
20242fcbc377Syt 		aborted_tags = 0xffffffff;
20252fcbc377Syt 		/*
202682263d52Syt 		 * Aborting one specific packet, first search the
20272fcbc377Syt 		 * ahciport_slot_pkts[] list for matching spkt.
20282fcbc377Syt 		 */
20292fcbc377Syt 		for (tmp_slot = 0;
20302fcbc377Syt 		    tmp_slot < ahci_ctlp->ahcictl_num_cmd_slots; tmp_slot++) {
20312fcbc377Syt 			if (ahci_portp->ahciport_slot_pkts[tmp_slot] == spkt) {
20322fcbc377Syt 				aborted_tags = (0x1 << tmp_slot);
20332fcbc377Syt 				break;
20342fcbc377Syt 			}
20352fcbc377Syt 		}
20362fcbc377Syt 
20372fcbc377Syt 		if (aborted_tags == 0xffffffff) {
20382fcbc377Syt 			/* request packet is not on the pending list */
20392fcbc377Syt 			AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
20402fcbc377Syt 			    "Cannot find the aborting pkt 0x%p on the "
20412fcbc377Syt 			    "pending list", (void *)spkt);
20422fcbc377Syt 			ahci_update_sata_registers(ahci_ctlp, port,
20432fcbc377Syt 			    &spkt->satapkt_device);
20442fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
20452fcbc377Syt 			return (SATA_FAILURE);
20462fcbc377Syt 		}
2047a9440e8dSyt 		cmn_err(CE_NOTE, "!ahci%d: ahci port %d abort satapkt 0x%p",
2048a9440e8dSyt 		    instance, port, (void *)spkt);
20492fcbc377Syt 	}
20502fcbc377Syt 
205182263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
205282263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
205382263d52Syt 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
2054a9440e8dSyt 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
205582263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
205682263d52Syt 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
20572fcbc377Syt 
205868d33a25Syt 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
205968d33a25Syt 	ahci_portp->ahciport_mop_in_progress++;
20602fcbc377Syt 
20612fcbc377Syt 	/*
20622fcbc377Syt 	 * To abort the packet(s), first we are trying to clear PxCMD.ST
206368d33a25Syt 	 * to stop the port, and if the port can be stopped
20642fcbc377Syt 	 * successfully with PxTFD.STS.BSY and PxTFD.STS.DRQ cleared to '0',
20652fcbc377Syt 	 * then we just send back the aborted packet(s) with ABORTED flag
20662fcbc377Syt 	 * and then restart the port by setting PxCMD.ST and PxCMD.FRE.
20672fcbc377Syt 	 * If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to '1', then we
20682fcbc377Syt 	 * perform a COMRESET.
20692fcbc377Syt 	 */
20702fcbc377Syt 	(void) ahci_restart_port_wait_till_ready(ahci_ctlp,
207168d33a25Syt 	    ahci_portp, port, NULL, NULL);
20722fcbc377Syt 
20732fcbc377Syt 	/*
20742fcbc377Syt 	 * Compute which have finished and which need to be retried.
20752fcbc377Syt 	 *
207682263d52Syt 	 * The finished tags are ahciport_pending_tags/ahciport_pending_ncq_tags
207782263d52Syt 	 * minus the slot_status. The aborted_tags has to be deducted by
207882263d52Syt 	 * finished_tags since we can't possibly abort a tag which had finished
207982263d52Syt 	 * already.
20802fcbc377Syt 	 */
208182263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
208282263d52Syt 		finished_tags = ahci_portp->ahciport_pending_tags &
208382263d52Syt 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2084a9440e8dSyt 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
208582263d52Syt 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
208682263d52Syt 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
20872fcbc377Syt 
20882fcbc377Syt 	aborted_tags &= ~finished_tags;
20892fcbc377Syt 
20902fcbc377Syt 	ahci_mop_commands(ahci_ctlp,
20912fcbc377Syt 	    ahci_portp,
20922fcbc377Syt 	    slot_status,
20932fcbc377Syt 	    0, /* failed tags */
20942fcbc377Syt 	    0, /* timeout tags */
20952fcbc377Syt 	    aborted_tags,
20962fcbc377Syt 	    0); /* reset tags */
20972fcbc377Syt 
20982fcbc377Syt 	ahci_update_sata_registers(ahci_ctlp, port, &spkt->satapkt_device);
20992fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
21002fcbc377Syt 
21012fcbc377Syt 	return (SATA_SUCCESS);
21022fcbc377Syt }
21032fcbc377Syt 
21042fcbc377Syt /*
21052fcbc377Syt  * Used to do device reset and reject all the pending packets on a device
21062fcbc377Syt  * during the reset operation.
21072fcbc377Syt  *
21082fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function is called.
21092fcbc377Syt  */
21102fcbc377Syt static int
21112fcbc377Syt ahci_reset_device_reject_pkts(ahci_ctl_t *ahci_ctlp,
21122fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
21132fcbc377Syt {
211482263d52Syt 	uint32_t slot_status = 0;
211582263d52Syt 	uint32_t reset_tags = 0;
211682263d52Syt 	uint32_t finished_tags = 0;
21172fcbc377Syt 	sata_device_t sdevice;
21182fcbc377Syt 	int ret;
21192fcbc377Syt 
21202fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
21212fcbc377Syt 	    "ahci_reset_device_reject_pkts on port: %d", port);
21222fcbc377Syt 
21232fcbc377Syt 	/*
212468d33a25Syt 	 * If AHCI_PORT_FLAG_MOPPING flag is set, it means all the pending
212568d33a25Syt 	 * commands are being mopped, therefore there is nothing else to do
21262fcbc377Syt 	 */
212768d33a25Syt 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
21282fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
21292fcbc377Syt 		    "ahci_reset_device_reject_pkts: port %d is in "
21302fcbc377Syt 		    "mopping process, so return directly ", port);
21312fcbc377Syt 		return (SATA_SUCCESS);
21322fcbc377Syt 	}
21332fcbc377Syt 
213482263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
213582263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
213682263d52Syt 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
213782263d52Syt 		reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2138a9440e8dSyt 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
213982263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
214082263d52Syt 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
214182263d52Syt 		reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
214282263d52Syt 	}
21432fcbc377Syt 
21442fcbc377Syt 	if (ahci_software_reset(ahci_ctlp, ahci_portp, port)
21452fcbc377Syt 	    != AHCI_SUCCESS) {
21462fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
21472fcbc377Syt 		    "Try to do a port reset after software "
21482fcbc377Syt 		    "reset failed", port);
21492fcbc377Syt 		ret = ahci_port_reset(ahci_ctlp, ahci_portp, port);
21502fcbc377Syt 		if (ret != AHCI_SUCCESS) {
21512fcbc377Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
21522fcbc377Syt 			    "ahci_reset_device_reject_pkts: port %d "
21532fcbc377Syt 			    "failed", port);
21542fcbc377Syt 			return (SATA_FAILURE);
21552fcbc377Syt 		}
21562fcbc377Syt 	}
21572fcbc377Syt 	/* Set the reset in progress flag */
21582fcbc377Syt 	ahci_portp->ahciport_reset_in_progress = 1;
21592fcbc377Syt 
216068d33a25Syt 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
216168d33a25Syt 	ahci_portp->ahciport_mop_in_progress++;
21622fcbc377Syt 
21632fcbc377Syt 	/* Indicate to the framework that a reset has happened */
21642fcbc377Syt 	bzero((void *)&sdevice, sizeof (sata_device_t));
216509121340Syt 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
216668d33a25Syt 	sdevice.satadev_addr.pmport = 0;
216768d33a25Syt 	sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
21682fcbc377Syt 
21692fcbc377Syt 	sdevice.satadev_state = SATA_DSTATE_RESET |
21702fcbc377Syt 	    SATA_DSTATE_PWR_ACTIVE;
21712fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
21722fcbc377Syt 	sata_hba_event_notify(
21732fcbc377Syt 	    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
21742fcbc377Syt 	    &sdevice,
21752fcbc377Syt 	    SATA_EVNT_DEVICE_RESET);
21762fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
21772fcbc377Syt 
21782fcbc377Syt 	AHCIDBG1(AHCIDBG_EVENT, ahci_ctlp,
21792fcbc377Syt 	    "port %d sending event up: SATA_EVNT_RESET", port);
21802fcbc377Syt 
21812fcbc377Syt 	/* Next try to mop the pending commands */
218282263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
218382263d52Syt 		finished_tags = ahci_portp->ahciport_pending_tags &
218482263d52Syt 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2185a9440e8dSyt 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
218682263d52Syt 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
218782263d52Syt 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
21882fcbc377Syt 
21892fcbc377Syt 	reset_tags &= ~finished_tags;
21902fcbc377Syt 
21912fcbc377Syt 	ahci_mop_commands(ahci_ctlp,
21922fcbc377Syt 	    ahci_portp,
21932fcbc377Syt 	    slot_status,
21942fcbc377Syt 	    0, /* failed tags */
21952fcbc377Syt 	    0, /* timeout tags */
21962fcbc377Syt 	    0, /* aborted tags */
21972fcbc377Syt 	    reset_tags); /* reset tags */
21982fcbc377Syt 
21992fcbc377Syt 	return (SATA_SUCCESS);
22002fcbc377Syt }
22012fcbc377Syt 
22022fcbc377Syt /*
22032fcbc377Syt  * Used to do port reset and reject all the pending packets on a port during
22042fcbc377Syt  * the reset operation.
22052fcbc377Syt  *
22062fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function is called.
22072fcbc377Syt  */
22082fcbc377Syt static int
22092fcbc377Syt ahci_reset_port_reject_pkts(ahci_ctl_t *ahci_ctlp,
22102fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
22112fcbc377Syt {
221282263d52Syt 	uint32_t slot_status = 0;
221382263d52Syt 	uint32_t reset_tags = 0;
221482263d52Syt 	uint32_t finished_tags = 0;
22152fcbc377Syt 
22162fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
22172fcbc377Syt 	    "ahci_reset_port_reject_pkts on port: %d", port);
22182fcbc377Syt 
22192fcbc377Syt 	/*
222068d33a25Syt 	 * If AHCI_PORT_FLAG_MOPPING flag is set, it means all the pending
222168d33a25Syt 	 * commands are being mopped, therefore there is nothing else to do
22222fcbc377Syt 	 */
222368d33a25Syt 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
22242fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
22252fcbc377Syt 		    "ahci_reset_port_reject_pkts: port %d is in "
22262fcbc377Syt 		    "mopping process, so return directly ", port);
22272fcbc377Syt 		return (SATA_SUCCESS);
22282fcbc377Syt 	}
22292fcbc377Syt 
223068d33a25Syt 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
223168d33a25Syt 	ahci_portp->ahciport_mop_in_progress++;
22322fcbc377Syt 
223382263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
223482263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
223582263d52Syt 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
223682263d52Syt 		reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2237a9440e8dSyt 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
223882263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
223982263d52Syt 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
224082263d52Syt 		reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
224182263d52Syt 	}
22422fcbc377Syt 
22432fcbc377Syt 	if (ahci_restart_port_wait_till_ready(ahci_ctlp,
224482263d52Syt 	    ahci_portp, port, AHCI_PORT_RESET|AHCI_RESET_NO_EVENTS_UP,
224582263d52Syt 	    NULL) != AHCI_SUCCESS)
22462fcbc377Syt 		return (SATA_FAILURE);
22472fcbc377Syt 
224882263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
224982263d52Syt 		finished_tags = ahci_portp->ahciport_pending_tags &
225082263d52Syt 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2251a9440e8dSyt 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
225282263d52Syt 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
225382263d52Syt 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
22542fcbc377Syt 
22552fcbc377Syt 	reset_tags &= ~finished_tags;
22562fcbc377Syt 
22572fcbc377Syt 	ahci_mop_commands(ahci_ctlp,
22582fcbc377Syt 	    ahci_portp,
22592fcbc377Syt 	    slot_status,
22602fcbc377Syt 	    0, /* failed tags */
22612fcbc377Syt 	    0, /* timeout tags */
22622fcbc377Syt 	    0, /* aborted tags */
22632fcbc377Syt 	    reset_tags); /* reset tags */
22642fcbc377Syt 
22652fcbc377Syt 	return (SATA_SUCCESS);
22662fcbc377Syt }
22672fcbc377Syt 
22682fcbc377Syt /*
22692fcbc377Syt  * Used to do hba reset and reject all the pending packets on all ports
22702fcbc377Syt  * during the reset operation.
22712fcbc377Syt  */
22722fcbc377Syt static int
22732fcbc377Syt ahci_reset_hba_reject_pkts(ahci_ctl_t *ahci_ctlp)
22742fcbc377Syt {
22752fcbc377Syt 	ahci_port_t *ahci_portp;
22762fcbc377Syt 	uint32_t slot_status[AHCI_MAX_PORTS];
22772fcbc377Syt 	uint32_t reset_tags[AHCI_MAX_PORTS];
22782fcbc377Syt 	uint32_t finished_tags[AHCI_MAX_PORTS];
22792fcbc377Syt 	uint8_t port;
22802fcbc377Syt 	int ret = SATA_SUCCESS;
22812fcbc377Syt 
22822fcbc377Syt 	AHCIDBG0(AHCIDBG_ENTRY, ahci_ctlp,
22832fcbc377Syt 	    "ahci_reset_hba_reject_pkts enter");
22842fcbc377Syt 
22852fcbc377Syt 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
22862fcbc377Syt 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
22872fcbc377Syt 			continue;
22882fcbc377Syt 		}
22892fcbc377Syt 
22902fcbc377Syt 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
22912fcbc377Syt 
22922fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
229382263d52Syt 		if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
229482263d52Syt 			slot_status[port] = ddi_get32(
229582263d52Syt 			    ahci_ctlp->ahcictl_ahci_acc_handle,
229682263d52Syt 			    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
229782263d52Syt 			reset_tags[port] = slot_status[port] &
229882263d52Syt 			    AHCI_SLOT_MASK(ahci_ctlp);
2299a9440e8dSyt 		} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
230082263d52Syt 			slot_status[port] = ddi_get32(
230182263d52Syt 			    ahci_ctlp->ahcictl_ahci_acc_handle,
230282263d52Syt 			    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
230382263d52Syt 			reset_tags[port] = slot_status[port] &
230482263d52Syt 			    AHCI_NCQ_SLOT_MASK(ahci_portp);
230582263d52Syt 		}
23062fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
23072fcbc377Syt 	}
23082fcbc377Syt 
23092fcbc377Syt 	if (ahci_hba_reset(ahci_ctlp) != AHCI_SUCCESS) {
23102fcbc377Syt 		ret = SATA_FAILURE;
23112fcbc377Syt 		goto out;
23122fcbc377Syt 	}
23132fcbc377Syt 
23142fcbc377Syt 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
23152fcbc377Syt 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
23162fcbc377Syt 			continue;
23172fcbc377Syt 		}
23182fcbc377Syt 
23192fcbc377Syt 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
23202fcbc377Syt 
23212fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
23222fcbc377Syt 		/*
23232fcbc377Syt 		 * To prevent recursive enter to ahci_mop_commands, we need
232468d33a25Syt 		 * check AHCI_PORT_FLAG_MOPPING flag.
23252fcbc377Syt 		 */
232668d33a25Syt 		if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
23272fcbc377Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
23282fcbc377Syt 			    "ahci_reset_hba_reject_pkts: port %d is in "
23292fcbc377Syt 			    "mopping process, so return directly ", port);
23302fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
23312fcbc377Syt 			continue;
23322fcbc377Syt 		}
23332fcbc377Syt 
233468d33a25Syt 		ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
233568d33a25Syt 		ahci_portp->ahciport_mop_in_progress++;
23362fcbc377Syt 
233782263d52Syt 		if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
233882263d52Syt 			finished_tags[port]  =
233982263d52Syt 			    ahci_portp->ahciport_pending_tags &
234082263d52Syt 			    ~slot_status[port] & AHCI_SLOT_MASK(ahci_ctlp);
2341a9440e8dSyt 		else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
234282263d52Syt 			finished_tags[port] =
234382263d52Syt 			    ahci_portp->ahciport_pending_ncq_tags &
234482263d52Syt 			    ~slot_status[port] & AHCI_NCQ_SLOT_MASK(ahci_portp);
23452fcbc377Syt 
23462fcbc377Syt 		reset_tags[port] &= ~finished_tags[port];
23472fcbc377Syt 
23482fcbc377Syt 		ahci_mop_commands(ahci_ctlp,
23492fcbc377Syt 		    ahci_portp,
23502fcbc377Syt 		    slot_status[port],
23512fcbc377Syt 		    0, /* failed tags */
23522fcbc377Syt 		    0, /* timeout tags */
23532fcbc377Syt 		    0, /* aborted tags */
23542fcbc377Syt 		    reset_tags[port]); /* reset tags */
235568d33a25Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
23562fcbc377Syt 	}
23572fcbc377Syt out:
23582fcbc377Syt 	return (ret);
23592fcbc377Syt }
23602fcbc377Syt 
23612fcbc377Syt /*
23622fcbc377Syt  * Called by sata framework to reset a port(s) or device.
23632fcbc377Syt  */
23642fcbc377Syt static int
23652fcbc377Syt ahci_tran_reset_dport(dev_info_t *dip, sata_device_t *sd)
23662fcbc377Syt {
23672fcbc377Syt 	ahci_ctl_t *ahci_ctlp;
23682fcbc377Syt 	ahci_port_t *ahci_portp;
23692fcbc377Syt 	uint8_t cport = sd->satadev_addr.cport;
23702fcbc377Syt 	uint8_t port;
23712fcbc377Syt 	int ret = SATA_SUCCESS;
2372a9440e8dSyt 	int instance = ddi_get_instance(dip);
23732fcbc377Syt 
2374a9440e8dSyt 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
23752fcbc377Syt 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
23762fcbc377Syt 
23772fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
237868d33a25Syt 	    "ahci_tran_reset_port enter: cport: %d", cport);
23792fcbc377Syt 
23802fcbc377Syt 	switch (sd->satadev_addr.qual) {
23812fcbc377Syt 	case SATA_ADDR_CPORT:
23822fcbc377Syt 		/* Port reset */
23832fcbc377Syt 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
2384a9440e8dSyt 		cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport port %d "
2385a9440e8dSyt 		    "reset port", instance, port);
23862fcbc377Syt 
23872fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
23882fcbc377Syt 		ret = ahci_reset_port_reject_pkts(ahci_ctlp, ahci_portp, port);
23892fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
23902fcbc377Syt 
23912fcbc377Syt 		break;
23922fcbc377Syt 
23932fcbc377Syt 	case SATA_ADDR_DCPORT:
23942fcbc377Syt 		/* Device reset */
23952fcbc377Syt 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
2396a9440e8dSyt 		cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport port %d "
2397a9440e8dSyt 		    "reset device", instance, port);
23982fcbc377Syt 
23992fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
24002fcbc377Syt 		if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED |
24012fcbc377Syt 		    ahci_portp->ahciport_port_state & SATA_PSTATE_SHUTDOWN |
24022fcbc377Syt 		    ahci_portp->ahciport_port_state & SATA_PSTATE_PWROFF) {
24032fcbc377Syt 			/*
24042fcbc377Syt 			 * In case the targer driver would send the request
24052fcbc377Syt 			 * before sata framework can have the opportunity to
24062fcbc377Syt 			 * process those event reports.
24072fcbc377Syt 			 */
24082fcbc377Syt 			sd->satadev_state = ahci_portp->ahciport_port_state;
24092fcbc377Syt 			ahci_update_sata_registers(ahci_ctlp, port, sd);
24102fcbc377Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
24112fcbc377Syt 			    "ahci_tran_reset_dport returning SATA_FAILURE "
24122fcbc377Syt 			    "while port in FAILED/SHUTDOWN/PWROFF state: "
241368d33a25Syt 			    "port: %d", port);
24142fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
24152fcbc377Syt 			ret = SATA_FAILURE;
24162fcbc377Syt 			break;
24172fcbc377Syt 		}
24182fcbc377Syt 
24192fcbc377Syt 		if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
24202fcbc377Syt 			/*
24212fcbc377Syt 			 * ahci_intr_phyrdy_change() may have rendered it to
24222fcbc377Syt 			 * AHCI_PORT_TYPE_NODEV.
24232fcbc377Syt 			 */
24242fcbc377Syt 			sd->satadev_type = SATA_DTYPE_NONE;
24252fcbc377Syt 			sd->satadev_state = ahci_portp->ahciport_port_state;
24262fcbc377Syt 			ahci_update_sata_registers(ahci_ctlp, port, sd);
24272fcbc377Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
24282fcbc377Syt 			    "ahci_tran_reset_dport returning SATA_FAILURE "
242968d33a25Syt 			    "while no device attached: port: %d", port);
24302fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
24312fcbc377Syt 			ret = SATA_FAILURE;
24322fcbc377Syt 			break;
24332fcbc377Syt 		}
24342fcbc377Syt 
24352fcbc377Syt 		ret = ahci_reset_device_reject_pkts(ahci_ctlp,
24362fcbc377Syt 		    ahci_portp, port);
24372fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
24382fcbc377Syt 		break;
24392fcbc377Syt 
24402fcbc377Syt 	case SATA_ADDR_CNTRL:
24412fcbc377Syt 		/* Reset the whole controller */
2442a9440e8dSyt 		cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport port %d "
2443a9440e8dSyt 		    "reset the whole hba", instance, port);
24442fcbc377Syt 		ret = ahci_reset_hba_reject_pkts(ahci_ctlp);
24452fcbc377Syt 		break;
24462fcbc377Syt 
24472fcbc377Syt 	case SATA_ADDR_PMPORT:
24482fcbc377Syt 	case SATA_ADDR_DPMPORT:
24492fcbc377Syt 		AHCIDBG0(AHCIDBG_INFO, ahci_ctlp,
245068d33a25Syt 		    "ahci_tran_reset_dport: port multiplier will be "
245168d33a25Syt 		    "supported later");
2452689d74b0Syt 		/* FALLTHRU */
24532fcbc377Syt 	default:
24542fcbc377Syt 		ret = SATA_FAILURE;
24552fcbc377Syt 	}
24562fcbc377Syt 
24572fcbc377Syt 	return (ret);
24582fcbc377Syt }
24592fcbc377Syt 
24602fcbc377Syt /*
24612fcbc377Syt  * Called by sata framework to activate a port as part of hotplug.
24622fcbc377Syt  * (cfgadm -c connect satax/y)
24632fcbc377Syt  * Note: Not port-mult aware.
24642fcbc377Syt  */
24652fcbc377Syt static int
24662fcbc377Syt ahci_tran_hotplug_port_activate(dev_info_t *dip, sata_device_t *satadev)
24672fcbc377Syt {
24682fcbc377Syt 	ahci_ctl_t *ahci_ctlp;
24692fcbc377Syt 	ahci_port_t *ahci_portp;
24702fcbc377Syt 	uint8_t	cport = satadev->satadev_addr.cport;
24712fcbc377Syt 	uint8_t port;
2472a9440e8dSyt 	int instance = ddi_get_instance(dip);
24732fcbc377Syt 
2474a9440e8dSyt 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
24752fcbc377Syt 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
24762fcbc377Syt 
24772fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
24782fcbc377Syt 	    "ahci_tran_hotplug_port_activate cport %d enter", cport);
24792fcbc377Syt 
24802fcbc377Syt 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
24812fcbc377Syt 
24822fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
2483689d74b0Syt 	ahci_enable_port_intrs(ahci_ctlp, port);
2484a9440e8dSyt 	cmn_err(CE_NOTE, "!ahci%d: ahci port %d is activated", instance, port);
24852fcbc377Syt 
24862fcbc377Syt 	/*
24872fcbc377Syt 	 * Reset the port so that the PHY communication would be re-established.
248868d33a25Syt 	 * But this reset is an internal operation and the sata module doesn't
248968d33a25Syt 	 * need to know about it. Moreover, the port with a device attached will
249068d33a25Syt 	 * be started too.
24912fcbc377Syt 	 */
24922fcbc377Syt 	(void) ahci_restart_port_wait_till_ready(ahci_ctlp,
249368d33a25Syt 	    ahci_portp, port,
249468d33a25Syt 	    AHCI_PORT_RESET|AHCI_RESET_NO_EVENTS_UP,
249568d33a25Syt 	    NULL);
24962fcbc377Syt 
24972fcbc377Syt 	/*
24982fcbc377Syt 	 * Need to check the link status and device status of the port
24992fcbc377Syt 	 * and consider raising power if the port was in D3 state
25002fcbc377Syt 	 */
250168d33a25Syt 	ahci_portp->ahciport_port_state |= SATA_PSTATE_PWRON;
250268d33a25Syt 	ahci_portp->ahciport_port_state &= ~SATA_PSTATE_PWROFF;
250368d33a25Syt 	ahci_portp->ahciport_port_state &= ~SATA_PSTATE_SHUTDOWN;
250468d33a25Syt 
250568d33a25Syt 	satadev->satadev_state = ahci_portp->ahciport_port_state;
25062fcbc377Syt 
25072fcbc377Syt 	ahci_update_sata_registers(ahci_ctlp, port, satadev);
25082fcbc377Syt 
25092fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
25102fcbc377Syt 	return (SATA_SUCCESS);
25112fcbc377Syt }
25122fcbc377Syt 
25132fcbc377Syt /*
25142fcbc377Syt  * Called by sata framework to deactivate a port as part of hotplug.
25152fcbc377Syt  * (cfgadm -c disconnect satax/y)
25162fcbc377Syt  * Note: Not port-mult aware.
25172fcbc377Syt  */
25182fcbc377Syt static int
25192fcbc377Syt ahci_tran_hotplug_port_deactivate(dev_info_t *dip, sata_device_t *satadev)
25202fcbc377Syt {
25212fcbc377Syt 	ahci_ctl_t *ahci_ctlp;
25222fcbc377Syt 	ahci_port_t *ahci_portp;
25232fcbc377Syt 	uint8_t cport = satadev->satadev_addr.cport;
25242fcbc377Syt 	uint8_t port;
252568d33a25Syt 	uint32_t port_scontrol;
2526a9440e8dSyt 	int instance = ddi_get_instance(dip);
25272fcbc377Syt 
2528a9440e8dSyt 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
25292fcbc377Syt 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
25302fcbc377Syt 
25312fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
25322fcbc377Syt 	    "ahci_tran_hotplug_port_deactivate cport %d enter", cport);
25332fcbc377Syt 
25342fcbc377Syt 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
25352fcbc377Syt 
25362fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
2537a9440e8dSyt 	cmn_err(CE_NOTE, "!ahci%d: ahci port %d is deactivated",
2538a9440e8dSyt 	    instance, port);
25392fcbc377Syt 
254068d33a25Syt 	/* Disable the interrupts on the port */
2541689d74b0Syt 	ahci_disable_port_intrs(ahci_ctlp, port);
25422fcbc377Syt 
254368d33a25Syt 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
254468d33a25Syt 		goto phy_offline;
254568d33a25Syt 	}
254668d33a25Syt 
25472fcbc377Syt 	/* First to abort all the pending commands */
25482fcbc377Syt 	ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
25492fcbc377Syt 
255068d33a25Syt 	/* Then stop the port */
255168d33a25Syt 	(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
25522fcbc377Syt 	    ahci_portp, port);
25532fcbc377Syt 
255468d33a25Syt 	/* Next put the PHY offline */
255568d33a25Syt 
255668d33a25Syt phy_offline:
255768d33a25Syt 	port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
255868d33a25Syt 	    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
255982263d52Syt 	SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_DISABLE);
256068d33a25Syt 
25612fcbc377Syt 	/* Update ahciport_port_state */
25622fcbc377Syt 	ahci_portp->ahciport_port_state = SATA_PSTATE_SHUTDOWN;
256368d33a25Syt 	satadev->satadev_state = ahci_portp->ahciport_port_state;
25642fcbc377Syt 
25652fcbc377Syt 	ahci_update_sata_registers(ahci_ctlp, port, satadev);
25662fcbc377Syt 
25672fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
25682fcbc377Syt 	return (SATA_SUCCESS);
25692fcbc377Syt }
25702fcbc377Syt 
25712fcbc377Syt /*
257268d33a25Syt  * To be used to mark all the outstanding pkts with SATA_PKT_ABORTED
25732fcbc377Syt  * when a device is unplugged or a port is deactivated.
25742fcbc377Syt  *
25752fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function is called.
25762fcbc377Syt  */
25772fcbc377Syt static void
25782fcbc377Syt ahci_reject_all_abort_pkts(ahci_ctl_t *ahci_ctlp,
25792fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
25802fcbc377Syt {
258182263d52Syt 	uint32_t slot_status = 0;
258282263d52Syt 	uint32_t abort_tags = 0;
25832fcbc377Syt 
258468d33a25Syt 	AHCIDBG1(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
25852fcbc377Syt 	    "ahci_reject_all_abort_pkts on port: %d", port);
25862fcbc377Syt 
258782263d52Syt 	/*
258882263d52Syt 	 * When AHCI_PORT_FLAG_MOPPING is set, we need to check whether a
258982263d52Syt 	 * REQUEST SENSE command or READ LOG EXT command is delivered to HBA
259082263d52Syt 	 * to get the error data, if yes when the device is removed, the
259182263d52Syt 	 * command needs to be aborted too.
259282263d52Syt 	 */
259382263d52Syt 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
259482263d52Syt 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
259582263d52Syt 			slot_status = 0x1;
259682263d52Syt 			abort_tags = 0x1;
259782263d52Syt 			goto out;
259882263d52Syt 		} else {
259982263d52Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
260082263d52Syt 			    "ahci_reject_all_abort_pkts return directly "
260182263d52Syt 			    "port %d no needs to reject any outstanding "
260282263d52Syt 			    "commands", port);
260382263d52Syt 			return;
260482263d52Syt 		}
260582263d52Syt 	}
26062fcbc377Syt 
260782263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
260882263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
260982263d52Syt 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
261082263d52Syt 		abort_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2611a9440e8dSyt 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
261282263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
261382263d52Syt 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
261482263d52Syt 		abort_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
261582263d52Syt 	}
261682263d52Syt 
261782263d52Syt out:
261868d33a25Syt 	/* No need to do mop when there is no outstanding commands */
261968d33a25Syt 	if (slot_status != 0) {
262068d33a25Syt 		ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
262168d33a25Syt 		ahci_portp->ahciport_mop_in_progress++;
26222fcbc377Syt 
262368d33a25Syt 		ahci_mop_commands(ahci_ctlp,
262468d33a25Syt 		    ahci_portp,
262568d33a25Syt 		    slot_status,
262668d33a25Syt 		    0, /* failed tags */
262768d33a25Syt 		    0, /* timeout tags */
262868d33a25Syt 		    abort_tags, /* aborting tags */
262968d33a25Syt 		    0); /* reset tags */
263068d33a25Syt 	}
263168d33a25Syt }
263268d33a25Syt 
263368d33a25Syt #if defined(__lock_lint)
263468d33a25Syt static int
263568d33a25Syt ahci_selftest(dev_info_t *dip, sata_device_t *device)
263668d33a25Syt {
26372fcbc377Syt 	return (SATA_SUCCESS);
26382fcbc377Syt }
26392fcbc377Syt #endif
26402fcbc377Syt 
26412fcbc377Syt /*
264268d33a25Syt  * Allocate the ports structure, only called by ahci_attach
264368d33a25Syt  */
264468d33a25Syt static int
264568d33a25Syt ahci_alloc_ports_state(ahci_ctl_t *ahci_ctlp)
264668d33a25Syt {
2647f68cbde1Syt 	int port, cport = 0;
264868d33a25Syt 
264968d33a25Syt 	AHCIDBG0(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
265068d33a25Syt 	    "ahci_alloc_ports_state enter");
265168d33a25Syt 
265268d33a25Syt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
265368d33a25Syt 
265468d33a25Syt 	/* Allocate structures only for the implemented ports */
2655f68cbde1Syt 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
265668d33a25Syt 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
265768d33a25Syt 			AHCIDBG1(AHCIDBG_INIT, ahci_ctlp,
265868d33a25Syt 			    "hba port %d not implemented", port);
265968d33a25Syt 			continue;
266068d33a25Syt 		}
266168d33a25Syt 
266268d33a25Syt 		ahci_ctlp->ahcictl_cport_to_port[cport] = (uint8_t)port;
2663f68cbde1Syt 		ahci_ctlp->ahcictl_port_to_cport[port] =
2664f68cbde1Syt 		    (uint8_t)cport++;
266568d33a25Syt 
266668d33a25Syt 		if (ahci_alloc_port_state(ahci_ctlp, port) != AHCI_SUCCESS) {
266768d33a25Syt 			goto err_out;
266868d33a25Syt 		}
266968d33a25Syt 	}
267068d33a25Syt 
267168d33a25Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
267268d33a25Syt 	return (AHCI_SUCCESS);
267368d33a25Syt 
267468d33a25Syt err_out:
267568d33a25Syt 	for (port--; port >= 0; port--) {
267668d33a25Syt 		if (AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
267768d33a25Syt 			ahci_dealloc_port_state(ahci_ctlp, port);
267868d33a25Syt 		}
267968d33a25Syt 	}
268068d33a25Syt 
268168d33a25Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
268268d33a25Syt 	return (AHCI_FAILURE);
268368d33a25Syt }
268468d33a25Syt 
268568d33a25Syt /*
268668d33a25Syt  * Reverse of ahci_alloc_ports_state(), only called by ahci_detach
268768d33a25Syt  */
268868d33a25Syt static void
268968d33a25Syt ahci_dealloc_ports_state(ahci_ctl_t *ahci_ctlp)
269068d33a25Syt {
269168d33a25Syt 	int port;
269268d33a25Syt 
269368d33a25Syt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
269468d33a25Syt 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
269568d33a25Syt 		/* if this port is implemented by the HBA */
269668d33a25Syt 		if (AHCI_PORT_IMPLEMENTED(ahci_ctlp, port))
269768d33a25Syt 			ahci_dealloc_port_state(ahci_ctlp, port);
269868d33a25Syt 	}
269968d33a25Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
270068d33a25Syt }
270168d33a25Syt 
2702f8a673adSying tian - Beijing China /*
2703f8a673adSying tian - Beijing China  * Drain the taskq.
2704f8a673adSying tian - Beijing China  */
2705f8a673adSying tian - Beijing China static void
2706f8a673adSying tian - Beijing China ahci_drain_ports_taskq(ahci_ctl_t *ahci_ctlp)
2707f8a673adSying tian - Beijing China {
2708f8a673adSying tian - Beijing China 	ahci_port_t *ahci_portp;
2709f8a673adSying tian - Beijing China 	int port;
2710f8a673adSying tian - Beijing China 
2711f8a673adSying tian - Beijing China 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
2712f8a673adSying tian - Beijing China 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
2713f8a673adSying tian - Beijing China 			continue;
2714f8a673adSying tian - Beijing China 		}
2715f8a673adSying tian - Beijing China 
2716f8a673adSying tian - Beijing China 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
2717f8a673adSying tian - Beijing China 
2718f8a673adSying tian - Beijing China 		mutex_enter(&ahci_portp->ahciport_mutex);
2719f8a673adSying tian - Beijing China 		ddi_taskq_wait(ahci_portp->ahciport_event_taskq);
2720f8a673adSying tian - Beijing China 		mutex_exit(&ahci_portp->ahciport_mutex);
2721f8a673adSying tian - Beijing China 	}
2722f8a673adSying tian - Beijing China }
2723f8a673adSying tian - Beijing China 
272468d33a25Syt /*
272513bcbb7aSyt  * Initialize the controller and all ports. And then try to start the ports
272613bcbb7aSyt  * if there are devices attached.
27272fcbc377Syt  *
27282fcbc377Syt  * This routine can be called from three seperate cases: DDI_ATTACH,
27292fcbc377Syt  * PM_LEVEL_D0 and DDI_RESUME. The DDI_ATTACH case is different from
273068d33a25Syt  * other two cases; device signature probing are attempted only during
273168d33a25Syt  * DDI_ATTACH case.
27322fcbc377Syt  *
27332fcbc377Syt  * WARNING!!! Disable the whole controller's interrupts before calling and
27342fcbc377Syt  * the interrupts will be enabled upon successfully return.
27352fcbc377Syt  */
27362fcbc377Syt static int
27372fcbc377Syt ahci_initialize_controller(ahci_ctl_t *ahci_ctlp)
27382fcbc377Syt {
27392fcbc377Syt 	ahci_port_t *ahci_portp;
27402fcbc377Syt 	uint32_t ghc_control;
274168d33a25Syt 	int port;
27422fcbc377Syt 
27432fcbc377Syt 	AHCIDBG0(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
27442fcbc377Syt 	    "ahci_initialize_controller enter");
27452fcbc377Syt 
27462fcbc377Syt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
27472fcbc377Syt 
27482fcbc377Syt 	/*
27492fcbc377Syt 	 * Indicate that system software is AHCI aware by setting
27502fcbc377Syt 	 * GHC.AE to 1
27512fcbc377Syt 	 */
27522fcbc377Syt 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
27532fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
27542fcbc377Syt 
27552fcbc377Syt 	ghc_control |= AHCI_HBA_GHC_AE;
27562fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
27572fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp),
27582fcbc377Syt 	    ghc_control);
27592fcbc377Syt 
276082263d52Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
276182263d52Syt 
27622fcbc377Syt 	/* Initialize the implemented ports and structures */
27632fcbc377Syt 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
27642fcbc377Syt 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
27652fcbc377Syt 			continue;
27662fcbc377Syt 		}
27672fcbc377Syt 
27682fcbc377Syt 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
27692fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
27702fcbc377Syt 
27712fcbc377Syt 		/*
27722fcbc377Syt 		 * Ensure that the controller is not in the running state
27732fcbc377Syt 		 * by checking every implemented port's PxCMD register
27742fcbc377Syt 		 */
27752fcbc377Syt 		if (ahci_initialize_port(ahci_ctlp, ahci_portp, port)
27762fcbc377Syt 		    != AHCI_SUCCESS) {
27772fcbc377Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
27782fcbc377Syt 			    "ahci_initialize_controller: failed to "
27792fcbc377Syt 			    "initialize port %d", port);
27802fcbc377Syt 			/*
27812fcbc377Syt 			 * Set the port state to SATA_PSTATE_FAILED if
27822fcbc377Syt 			 * failed to initialize it.
27832fcbc377Syt 			 */
278468d33a25Syt 			ahci_portp->ahciport_port_state = SATA_PSTATE_FAILED;
27852fcbc377Syt 		}
27862fcbc377Syt 
27872fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
27882fcbc377Syt 	}
27892fcbc377Syt 
27902fcbc377Syt 	/* Enable the whole controller interrupts */
279182263d52Syt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
27922fcbc377Syt 	ahci_enable_all_intrs(ahci_ctlp);
27932fcbc377Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
27942fcbc377Syt 
27952fcbc377Syt 	return (AHCI_SUCCESS);
27962fcbc377Syt }
27972fcbc377Syt 
27982fcbc377Syt /*
279968d33a25Syt  * Reverse of ahci_initialize_controller()
28002fcbc377Syt  *
280113bcbb7aSyt  * We only need to stop the ports and disable the interrupt.
28022fcbc377Syt  */
28032fcbc377Syt static void
280468d33a25Syt ahci_uninitialize_controller(ahci_ctl_t *ahci_ctlp)
28052fcbc377Syt {
280613bcbb7aSyt 	ahci_port_t *ahci_portp;
280713bcbb7aSyt 	int port;
280813bcbb7aSyt 
28092fcbc377Syt 	AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
281068d33a25Syt 	    "ahci_uninitialize_controller enter");
28112fcbc377Syt 
28122fcbc377Syt 	/* disable all the interrupts. */
281313bcbb7aSyt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
28142fcbc377Syt 	ahci_disable_all_intrs(ahci_ctlp);
281513bcbb7aSyt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
281613bcbb7aSyt 
281713bcbb7aSyt 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
281813bcbb7aSyt 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
281913bcbb7aSyt 			continue;
282013bcbb7aSyt 		}
282113bcbb7aSyt 
282213bcbb7aSyt 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
282313bcbb7aSyt 
282413bcbb7aSyt 		/* Stop the port by clearing PxCMD.ST */
282513bcbb7aSyt 		mutex_enter(&ahci_portp->ahciport_mutex);
282613bcbb7aSyt 
282713bcbb7aSyt 		/*
282813bcbb7aSyt 		 * Here we must disable the port interrupt because
282913bcbb7aSyt 		 * ahci_disable_all_intrs only clear GHC.IE, and IS
283013bcbb7aSyt 		 * register will be still set if PxIE is enabled.
283113bcbb7aSyt 		 * When ahci shares one IRQ with other drivers, the
283213bcbb7aSyt 		 * intr handler may claim the intr mistakenly.
283313bcbb7aSyt 		 */
2834689d74b0Syt 		ahci_disable_port_intrs(ahci_ctlp, port);
283513bcbb7aSyt 		(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
283613bcbb7aSyt 		    ahci_portp, port);
283713bcbb7aSyt 		mutex_exit(&ahci_portp->ahciport_mutex);
283813bcbb7aSyt 	}
28392fcbc377Syt }
28402fcbc377Syt 
28412fcbc377Syt /*
28422fcbc377Syt  * The routine is to initialize the port. First put the port in NOTRunning
28432fcbc377Syt  * state, then enable port interrupt and clear Serror register. And under
28442fcbc377Syt  * AHCI_ATTACH case, find device signature and then try to start the port.
28452fcbc377Syt  *
284682263d52Syt  * WARNING!!! ahciport_mutex should be acquired before the function is called.
28472fcbc377Syt  */
28482fcbc377Syt static int
28492fcbc377Syt ahci_initialize_port(ahci_ctl_t *ahci_ctlp,
28502fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
28512fcbc377Syt {
2852*0a4c4cecSXiao-Yu Zhang 	uint32_t port_sstatus, port_task_file, port_cmd_status;
2853*0a4c4cecSXiao-Yu Zhang 	int ret;
28542fcbc377Syt 
28552fcbc377Syt 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
28562fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
28572fcbc377Syt 
28582fcbc377Syt 	AHCIDBG2(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
28592fcbc377Syt 	    "ahci_initialize_port: port %d "
28602fcbc377Syt 	    "port_cmd_status = 0x%x", port, port_cmd_status);
28612fcbc377Syt 	/*
28622fcbc377Syt 	 * Check whether the port is in NotRunning state, if not,
28632fcbc377Syt 	 * put the port in NotRunning state
28642fcbc377Syt 	 */
2865*0a4c4cecSXiao-Yu Zhang 	if (port_cmd_status &
28662fcbc377Syt 	    (AHCI_CMD_STATUS_ST |
28672fcbc377Syt 	    AHCI_CMD_STATUS_CR |
28682fcbc377Syt 	    AHCI_CMD_STATUS_FRE |
2869*0a4c4cecSXiao-Yu Zhang 	    AHCI_CMD_STATUS_FR)) {
2870*0a4c4cecSXiao-Yu Zhang 		(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
2871*0a4c4cecSXiao-Yu Zhang 		    ahci_portp, port);
28722fcbc377Syt 	}
28732fcbc377Syt 
2874*0a4c4cecSXiao-Yu Zhang 	/* Device is unknown at first */
2875*0a4c4cecSXiao-Yu Zhang 	ahci_portp->ahciport_device_type = SATA_DTYPE_UNKNOWN;
28762fcbc377Syt 
2877*0a4c4cecSXiao-Yu Zhang 	port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2878*0a4c4cecSXiao-Yu Zhang 	    (uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
2879*0a4c4cecSXiao-Yu Zhang 	port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2880*0a4c4cecSXiao-Yu Zhang 	    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
2881*0a4c4cecSXiao-Yu Zhang 
2882*0a4c4cecSXiao-Yu Zhang 	/* Check physcial link status */
2883*0a4c4cecSXiao-Yu Zhang 	if (SSTATUS_GET_IPM(port_sstatus) == SSTATUS_IPM_NODEV_NOPHYCOM ||
2884*0a4c4cecSXiao-Yu Zhang 	    SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_NOPHYCOM ||
2885*0a4c4cecSXiao-Yu Zhang 
2886*0a4c4cecSXiao-Yu Zhang 	    /* Check interface status */
2887*0a4c4cecSXiao-Yu Zhang 	    port_task_file & AHCI_TFD_STS_BSY ||
2888*0a4c4cecSXiao-Yu Zhang 	    port_task_file & AHCI_TFD_STS_DRQ) {
2889*0a4c4cecSXiao-Yu Zhang 
2890*0a4c4cecSXiao-Yu Zhang 		/* Incorrect task file state, we need to reset port */
2891*0a4c4cecSXiao-Yu Zhang 		ret = ahci_port_reset(ahci_ctlp, ahci_portp, port);
2892*0a4c4cecSXiao-Yu Zhang 
2893*0a4c4cecSXiao-Yu Zhang 		/* Does port reset succeed on HBA port? */
2894*0a4c4cecSXiao-Yu Zhang 		if (ret != AHCI_SUCCESS) {
2895*0a4c4cecSXiao-Yu Zhang 			AHCIDBG1(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
2896*0a4c4cecSXiao-Yu Zhang 			    "ahci_initialize_port:"
2897*0a4c4cecSXiao-Yu Zhang 			    "port reset faild at port %d", port);
2898*0a4c4cecSXiao-Yu Zhang 			return (AHCI_FAILURE);
2899*0a4c4cecSXiao-Yu Zhang 		}
2900*0a4c4cecSXiao-Yu Zhang 
2901*0a4c4cecSXiao-Yu Zhang 		/* Is port failed? */
2902*0a4c4cecSXiao-Yu Zhang 		if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED) {
2903*0a4c4cecSXiao-Yu Zhang 			AHCIDBG2(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
2904*0a4c4cecSXiao-Yu Zhang 			    "ahci_initialize_port: port %d state 0x%x",
2905*0a4c4cecSXiao-Yu Zhang 			    port, ahci_portp->ahciport_port_state);
2906*0a4c4cecSXiao-Yu Zhang 			return (AHCI_FAILURE);
2907*0a4c4cecSXiao-Yu Zhang 		}
2908*0a4c4cecSXiao-Yu Zhang 
2909*0a4c4cecSXiao-Yu Zhang 		/* Is there any device attached? */
2910*0a4c4cecSXiao-Yu Zhang 		if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
2911*0a4c4cecSXiao-Yu Zhang 
2912*0a4c4cecSXiao-Yu Zhang 			/* Do not waste time on empty port */
2913*0a4c4cecSXiao-Yu Zhang 			AHCIDBG1(AHCIDBG_INIT|AHCIDBG_INFO, ahci_ctlp,
2914*0a4c4cecSXiao-Yu Zhang 			    "ahci_initialize_port: No device is found "
2915*0a4c4cecSXiao-Yu Zhang 			    "at port %d", port);
2916*0a4c4cecSXiao-Yu Zhang 			goto out;
2917*0a4c4cecSXiao-Yu Zhang 		}
2918*0a4c4cecSXiao-Yu Zhang 	}
2919*0a4c4cecSXiao-Yu Zhang 	ahci_portp->ahciport_port_state = SATA_STATE_READY;
2920*0a4c4cecSXiao-Yu Zhang 
2921*0a4c4cecSXiao-Yu Zhang 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "port %d is ready now.", port);
29222fcbc377Syt 
29232fcbc377Syt 	/*
292468d33a25Syt 	 * At the time being, only probe ports/devices and get the types of
292513bcbb7aSyt 	 * attached devices during DDI_ATTACH. In fact, the device can be
292613bcbb7aSyt 	 * changed during power state changes, but at the time being, we
292713bcbb7aSyt 	 * don't support the situation.
29282fcbc377Syt 	 */
29292fcbc377Syt 	if (ahci_ctlp->ahcictl_flags & AHCI_ATTACH) {
2930*0a4c4cecSXiao-Yu Zhang 		/*
2931*0a4c4cecSXiao-Yu Zhang 		 * Till now we can assure a device attached to that HBA port
2932*0a4c4cecSXiao-Yu Zhang 		 * and work correctly. Now try to get the device signature.
2933*0a4c4cecSXiao-Yu Zhang 		 */
293468d33a25Syt 		ahci_find_dev_signature(ahci_ctlp, ahci_portp, port);
293513bcbb7aSyt 	} else {
29362fcbc377Syt 
293713bcbb7aSyt 		/*
293813bcbb7aSyt 		 * During the resume, we need to set the PxCLB, PxCLBU, PxFB
293913bcbb7aSyt 		 * and PxFBU registers in case these registers were cleared
294013bcbb7aSyt 		 * during the suspend.
294113bcbb7aSyt 		 */
294213bcbb7aSyt 		AHCIDBG1(AHCIDBG_PM, ahci_ctlp,
294313bcbb7aSyt 		    "ahci_initialize_port: port %d "
294413bcbb7aSyt 		    "reset the port during resume", port);
294513bcbb7aSyt 		(void) ahci_port_reset(ahci_ctlp, ahci_portp, port);
29462fcbc377Syt 
294713bcbb7aSyt 		AHCIDBG1(AHCIDBG_PM, ahci_ctlp,
294813bcbb7aSyt 		    "ahci_initialize_port: port %d "
294913bcbb7aSyt 		    "set PxCLB, PxCLBU, PxFB and PxFBU "
295013bcbb7aSyt 		    "during resume", port);
295113bcbb7aSyt 
295213bcbb7aSyt 		/* Config Port Received FIS Base Address */
295313bcbb7aSyt 		ddi_put64(ahci_ctlp->ahcictl_ahci_acc_handle,
295413bcbb7aSyt 		    (uint64_t *)AHCI_PORT_PxFB(ahci_ctlp, port),
295513bcbb7aSyt 		    ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_laddress);
295613bcbb7aSyt 
295713bcbb7aSyt 		/* Config Port Command List Base Address */
295813bcbb7aSyt 		ddi_put64(ahci_ctlp->ahcictl_ahci_acc_handle,
295913bcbb7aSyt 		    (uint64_t *)AHCI_PORT_PxCLB(ahci_ctlp, port),
296013bcbb7aSyt 		    ahci_portp->ahciport_cmd_list_dma_cookie.dmac_laddress);
296113bcbb7aSyt 	}
296213bcbb7aSyt 
296313bcbb7aSyt 	/* Disable the interface power management */
296413bcbb7aSyt 	ahci_disable_interface_pm(ahci_ctlp, port);
296513bcbb7aSyt 
296613bcbb7aSyt 	/* Return directly if no device connected */
296713bcbb7aSyt 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
296813bcbb7aSyt 		AHCIDBG1(AHCIDBG_INIT, ahci_ctlp,
296913bcbb7aSyt 		    "No device connected to port %d", port);
297013bcbb7aSyt 		goto out;
297113bcbb7aSyt 	}
297213bcbb7aSyt 
297313bcbb7aSyt 	/* Try to start the port */
297413bcbb7aSyt 	if (ahci_start_port(ahci_ctlp, ahci_portp, port)
297513bcbb7aSyt 	    != AHCI_SUCCESS) {
297613bcbb7aSyt 		AHCIDBG1(AHCIDBG_INIT, ahci_ctlp,
297713bcbb7aSyt 		    "failed to start port %d", port);
297813bcbb7aSyt 		return (AHCI_FAILURE);
29792fcbc377Syt 	}
298068d33a25Syt out:
298168d33a25Syt 	/* Enable port interrupts */
2982689d74b0Syt 	ahci_enable_port_intrs(ahci_ctlp, port);
29832fcbc377Syt 
29842fcbc377Syt 	return (AHCI_SUCCESS);
29852fcbc377Syt }
29862fcbc377Syt 
298713bcbb7aSyt /*
2988db2cce03Sying tian - Beijing China  *  Check the hardware defects and the power management capability.
298913bcbb7aSyt  */
299013bcbb7aSyt static int
299113bcbb7aSyt ahci_config_space_init(ahci_ctl_t *ahci_ctlp)
299213bcbb7aSyt {
2993a9440e8dSyt 	ushort_t venid, devid;
2994689d74b0Syt 	ushort_t caps_ptr, cap_count, cap;
2995689d74b0Syt #if AHCI_DEBUG
2996689d74b0Syt 	ushort_t pmcap, pmcsr;
29972c742e1fSying tian - Beijing China 	ushort_t msimc;
2998689d74b0Syt #endif
299913bcbb7aSyt 	uint8_t revision;
300013bcbb7aSyt 
300113bcbb7aSyt 	venid = pci_config_get16(ahci_ctlp->ahcictl_pci_conf_handle,
300213bcbb7aSyt 	    PCI_CONF_VENID);
300313bcbb7aSyt 
3004a9440e8dSyt 	devid = pci_config_get16(ahci_ctlp->ahcictl_pci_conf_handle,
3005a9440e8dSyt 	    PCI_CONF_DEVID);
3006a9440e8dSyt 
300713bcbb7aSyt 	/*
300813bcbb7aSyt 	 * Modify dma_attr_align of ahcictl_buffer_dma_attr. For VT8251, those
300913bcbb7aSyt 	 * controllers with 0x00 revision id work on 4-byte aligned buffer,
301013bcbb7aSyt 	 * which is a bug and was fixed after 0x00 revision id controllers.
301113bcbb7aSyt 	 *
301213bcbb7aSyt 	 * Moreover, VT8251 cannot use multiple command slots in the command
301313bcbb7aSyt 	 * list for non-queued commands because the previous register content
301413bcbb7aSyt 	 * of PxCI can be re-written in the register write, so a flag will be
301513bcbb7aSyt 	 * set to record this defect - AHCI_CAP_NO_MCMDLIST_NONQUEUE.
301613bcbb7aSyt 	 */
301713bcbb7aSyt 	if (venid == VIA_VENID) {
301813bcbb7aSyt 		revision = pci_config_get8(ahci_ctlp->ahcictl_pci_conf_handle,
301913bcbb7aSyt 		    PCI_CONF_REVID);
302013bcbb7aSyt 		AHCIDBG1(AHCIDBG_INIT, ahci_ctlp,
302113bcbb7aSyt 		    "revision id = 0x%x", revision);
302213bcbb7aSyt 		if (revision == 0x00) {
302313bcbb7aSyt 			ahci_ctlp->ahcictl_buffer_dma_attr.dma_attr_align = 0x4;
302413bcbb7aSyt 			AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
302513bcbb7aSyt 			    "change ddi_attr_align to 0x4");
302613bcbb7aSyt 		}
302713bcbb7aSyt 
302813bcbb7aSyt 		ahci_ctlp->ahcictl_cap = AHCI_CAP_NO_MCMDLIST_NONQUEUE;
302913bcbb7aSyt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
303013bcbb7aSyt 		    "VT8251 cannot use multiple command lists for "
303113bcbb7aSyt 		    "non-queued commands");
303213bcbb7aSyt 	}
303313bcbb7aSyt 
3034a9440e8dSyt 	/*
3035db2cce03Sying tian - Beijing China 	 * ATI SB600 (1002,4380) and SB700/750 (1002,4391) AHCI chipsets don't
3036db2cce03Sying tian - Beijing China 	 * support 64-bit DMA addressing though they declare the support,
3037db2cce03Sying tian - Beijing China 	 * so we need to set AHCI_CAP_32BIT_DMA flag to force 32-bit DMA.
3038a9440e8dSyt 	 */
3039db2cce03Sying tian - Beijing China 	if (venid == 0x1002 && (devid == 0x4380 || devid == 0x4391)) {
3040a9440e8dSyt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
3041db2cce03Sying tian - Beijing China 		    "ATI SB600/700/750 cannot do 64-bit DMA though CAP "
3042db2cce03Sying tian - Beijing China 		    "indicates support, so force it to use 32-bit DMA");
3043a9440e8dSyt 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_32BIT_DMA;
3044a9440e8dSyt 	}
3045a9440e8dSyt 
3046c6a9dbb6SXiao-Yu Zhang 	/* ASUS M3N-HT (NVidia 780a) does not support MSI */
3047c6a9dbb6SXiao-Yu Zhang 	if (venid == 0x10de && devid == 0x0ad4) {
3048c6a9dbb6SXiao-Yu Zhang 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
3049c6a9dbb6SXiao-Yu Zhang 		    "ASUS M3N-HT (NVidia 780a) does not support MSI "
3050c6a9dbb6SXiao-Yu Zhang 		    "interrupts, so force it to use fixed interrupts.");
3051c6a9dbb6SXiao-Yu Zhang 		ahci_msi_enabled = B_FALSE;
3052c6a9dbb6SXiao-Yu Zhang 	}
3053c6a9dbb6SXiao-Yu Zhang 
305413bcbb7aSyt 	/*
305513bcbb7aSyt 	 * Check if capabilities list is supported and if so,
305613bcbb7aSyt 	 * get initial capabilities pointer and clear bits 0,1.
305713bcbb7aSyt 	 */
305813bcbb7aSyt 	if (pci_config_get16(ahci_ctlp->ahcictl_pci_conf_handle,
305913bcbb7aSyt 	    PCI_CONF_STAT) & PCI_STAT_CAP) {
306013bcbb7aSyt 		caps_ptr = P2ALIGN(pci_config_get8(
306113bcbb7aSyt 		    ahci_ctlp->ahcictl_pci_conf_handle,
306213bcbb7aSyt 		    PCI_CONF_CAP_PTR), 4);
306313bcbb7aSyt 	} else {
306413bcbb7aSyt 		caps_ptr = PCI_CAP_NEXT_PTR_NULL;
306513bcbb7aSyt 	}
306613bcbb7aSyt 
306713bcbb7aSyt 	/*
306813bcbb7aSyt 	 * Walk capabilities if supported.
306913bcbb7aSyt 	 */
307013bcbb7aSyt 	for (cap_count = 0; caps_ptr != PCI_CAP_NEXT_PTR_NULL; ) {
307113bcbb7aSyt 
307213bcbb7aSyt 		/*
307313bcbb7aSyt 		 * Check that we haven't exceeded the maximum number of
307413bcbb7aSyt 		 * capabilities and that the pointer is in a valid range.
307513bcbb7aSyt 		 */
307613bcbb7aSyt 		if (++cap_count > PCI_CAP_MAX_PTR) {
307713bcbb7aSyt 			AHCIDBG0(AHCIDBG_ERRS, ahci_ctlp,
307813bcbb7aSyt 			    "too many device capabilities");
307913bcbb7aSyt 			return (AHCI_FAILURE);
308013bcbb7aSyt 		}
308113bcbb7aSyt 		if (caps_ptr < PCI_CAP_PTR_OFF) {
308213bcbb7aSyt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
308313bcbb7aSyt 			    "capabilities pointer 0x%x out of range",
308413bcbb7aSyt 			    caps_ptr);
308513bcbb7aSyt 			return (AHCI_FAILURE);
308613bcbb7aSyt 		}
308713bcbb7aSyt 
308813bcbb7aSyt 		/*
308913bcbb7aSyt 		 * Get next capability and check that it is valid.
309013bcbb7aSyt 		 * For now, we only support power management.
309113bcbb7aSyt 		 */
309213bcbb7aSyt 		cap = pci_config_get8(ahci_ctlp->ahcictl_pci_conf_handle,
309313bcbb7aSyt 		    caps_ptr);
309413bcbb7aSyt 		switch (cap) {
309513bcbb7aSyt 		case PCI_CAP_ID_PM:
309613bcbb7aSyt 
309713bcbb7aSyt 			/* power management supported */
309813bcbb7aSyt 			ahci_ctlp->ahcictl_cap |= AHCI_CAP_PM;
309913bcbb7aSyt 
310013bcbb7aSyt 			/* Save PMCSR offset */
310113bcbb7aSyt 			ahci_ctlp->ahcictl_pmcsr_offset = caps_ptr + PCI_PMCSR;
310213bcbb7aSyt 
3103689d74b0Syt #if AHCI_DEBUG
310413bcbb7aSyt 			pmcap = pci_config_get16(
310513bcbb7aSyt 			    ahci_ctlp->ahcictl_pci_conf_handle,
310613bcbb7aSyt 			    caps_ptr + PCI_PMCAP);
310713bcbb7aSyt 			pmcsr = pci_config_get16(
310813bcbb7aSyt 			    ahci_ctlp->ahcictl_pci_conf_handle,
310913bcbb7aSyt 			    ahci_ctlp->ahcictl_pmcsr_offset);
311013bcbb7aSyt 			AHCIDBG2(AHCIDBG_PM, ahci_ctlp,
311113bcbb7aSyt 			    "Power Management capability found PCI_PMCAP "
311213bcbb7aSyt 			    "= 0x%x PCI_PMCSR = 0x%x", pmcap, pmcsr);
311313bcbb7aSyt 			if ((pmcap & 0x3) == 0x3)
311413bcbb7aSyt 				AHCIDBG0(AHCIDBG_PM, ahci_ctlp,
311513bcbb7aSyt 				    "PCI Power Management Interface "
311613bcbb7aSyt 				    "spec 1.2 compliant");
311713bcbb7aSyt #endif
311813bcbb7aSyt 			break;
311913bcbb7aSyt 
312013bcbb7aSyt 		case PCI_CAP_ID_MSI:
31212c742e1fSying tian - Beijing China #if AHCI_DEBUG
31222c742e1fSying tian - Beijing China 			msimc = pci_config_get16(
31232c742e1fSying tian - Beijing China 			    ahci_ctlp->ahcictl_pci_conf_handle,
31242c742e1fSying tian - Beijing China 			    caps_ptr + PCI_MSI_CTRL);
31252c742e1fSying tian - Beijing China 			AHCIDBG1(AHCIDBG_MSI, ahci_ctlp,
31262c742e1fSying tian - Beijing China 			    "Message Signaled Interrupt capability found "
31272c742e1fSying tian - Beijing China 			    "MSICAP_MC.MMC = 0x%x", (msimc & 0xe) >> 1);
31282c742e1fSying tian - Beijing China #endif
31292c742e1fSying tian - Beijing China 			AHCIDBG0(AHCIDBG_MSI, ahci_ctlp,
31302c742e1fSying tian - Beijing China 			    "MSI capability found");
313113bcbb7aSyt 			break;
313213bcbb7aSyt 
313313bcbb7aSyt 		case PCI_CAP_ID_PCIX:
313413bcbb7aSyt 			AHCIDBG0(AHCIDBG_PM, ahci_ctlp,
313513bcbb7aSyt 			    "PCI-X capability found");
313613bcbb7aSyt 			break;
313713bcbb7aSyt 
313813bcbb7aSyt 		case PCI_CAP_ID_PCI_E:
313913bcbb7aSyt 			AHCIDBG0(AHCIDBG_PM, ahci_ctlp,
314013bcbb7aSyt 			    "PCI Express capability found");
314113bcbb7aSyt 			break;
314213bcbb7aSyt 
314313bcbb7aSyt 		case PCI_CAP_ID_MSI_X:
314413bcbb7aSyt 			AHCIDBG0(AHCIDBG_PM, ahci_ctlp,
314513bcbb7aSyt 			    "MSI-X capability found");
314613bcbb7aSyt 			break;
314713bcbb7aSyt 
314813bcbb7aSyt 		case PCI_CAP_ID_SATA:
314913bcbb7aSyt 			AHCIDBG0(AHCIDBG_PM, ahci_ctlp,
315013bcbb7aSyt 			    "SATA capability found");
315113bcbb7aSyt 			break;
315213bcbb7aSyt 
3153a9440e8dSyt 		case PCI_CAP_ID_VS:
3154a9440e8dSyt 			AHCIDBG0(AHCIDBG_PM, ahci_ctlp,
3155a9440e8dSyt 			    "Vendor Specific capability found");
3156a9440e8dSyt 			break;
3157a9440e8dSyt 
315813bcbb7aSyt 		default:
315913bcbb7aSyt 			AHCIDBG1(AHCIDBG_PM, ahci_ctlp,
316013bcbb7aSyt 			    "unrecognized capability 0x%x", cap);
316113bcbb7aSyt 			break;
316213bcbb7aSyt 		}
316313bcbb7aSyt 
316413bcbb7aSyt 		/*
316513bcbb7aSyt 		 * Get next capabilities pointer and clear bits 0,1.
316613bcbb7aSyt 		 */
316713bcbb7aSyt 		caps_ptr = P2ALIGN(pci_config_get8(
316813bcbb7aSyt 		    ahci_ctlp->ahcictl_pci_conf_handle,
316913bcbb7aSyt 		    (caps_ptr + PCI_CAP_NEXT_PTR)), 4);
317013bcbb7aSyt 	}
317113bcbb7aSyt 
317213bcbb7aSyt 	return (AHCI_SUCCESS);
317313bcbb7aSyt }
317413bcbb7aSyt 
31752fcbc377Syt /*
31762fcbc377Syt  * AHCI device reset ...; a single device on one of the ports is reset,
31772fcbc377Syt  * but the HBA and physical communication remain intact. This is the
31782fcbc377Syt  * least intrusive.
31792fcbc377Syt  *
31802fcbc377Syt  * When issuing a software reset sequence, there should not be other
31812fcbc377Syt  * commands in the command list, so we will first clear and then re-set
31822fcbc377Syt  * PxCMD.ST to clear PxCI. And before issuing the software reset,
31832fcbc377Syt  * the port must be idle and PxTFD.STS.BSY and PxTFD.STS.DRQ must be
3184b2e3645aSying tian - Beijing China  * cleared unless command list override (PxCMD.CLO) is supported.
31852fcbc377Syt  *
31862fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired and PxCMD.FRE should be
31872fcbc377Syt  * set before the function is called.
31882fcbc377Syt  */
31892fcbc377Syt static int
31902fcbc377Syt ahci_software_reset(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
31912fcbc377Syt     uint8_t port)
31922fcbc377Syt {
31932fcbc377Syt 	ahci_fis_h2d_register_t *h2d_register_fisp;
31942fcbc377Syt 	ahci_cmd_table_t *cmd_table;
31952fcbc377Syt 	ahci_cmd_header_t *cmd_header;
319668d33a25Syt 	uint32_t port_cmd_status, port_cmd_issue, port_task_file;
31972fcbc377Syt 	int slot, loop_count;
3198*0a4c4cecSXiao-Yu Zhang 	int rval = AHCI_FAILURE;
31992fcbc377Syt 
32002fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
32012fcbc377Syt 	    "Port %d device resetting", port);
32022fcbc377Syt 
3203*0a4c4cecSXiao-Yu Zhang 	/* First clear PxCMD.ST (AHCI v1.2 10.4.1) */
3204*0a4c4cecSXiao-Yu Zhang 	if (ahci_put_port_into_notrunning_state(ahci_ctlp, ahci_portp,
3205*0a4c4cecSXiao-Yu Zhang 	    port) != AHCI_SUCCESS) {
3206*0a4c4cecSXiao-Yu Zhang 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
3207*0a4c4cecSXiao-Yu Zhang 		    "ahci_software_reset: cannot stop HBA port %d.", port);
3208*0a4c4cecSXiao-Yu Zhang 		goto out;
3209*0a4c4cecSXiao-Yu Zhang 	}
32102fcbc377Syt 
32112fcbc377Syt 	/* Check PxTFD.STS.BSY and PxTFD.STS.DRQ */
32122fcbc377Syt 	port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
32132fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
32142fcbc377Syt 
32152fcbc377Syt 	if (port_task_file & AHCI_TFD_STS_BSY ||
32162fcbc377Syt 	    port_task_file & AHCI_TFD_STS_DRQ) {
3217*0a4c4cecSXiao-Yu Zhang 		if (!(ahci_ctlp->ahcictl_cap & AHCI_CAP_SCLO)) {
3218*0a4c4cecSXiao-Yu Zhang 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
3219*0a4c4cecSXiao-Yu Zhang 			    "PxTFD.STS.BSY/DRQ is set (PxTFD=0x%x), "
3220*0a4c4cecSXiao-Yu Zhang 			    "cannot issue a software reset.", port_task_file);
3221*0a4c4cecSXiao-Yu Zhang 			goto out;
3222*0a4c4cecSXiao-Yu Zhang 		}
3223*0a4c4cecSXiao-Yu Zhang 
3224*0a4c4cecSXiao-Yu Zhang 		/*
3225*0a4c4cecSXiao-Yu Zhang 		 * If HBA Support CLO, as Command List Override (CAP.SCLO is
3226*0a4c4cecSXiao-Yu Zhang 		 * set), PxCMD.CLO bit should be set before set PxCMD.ST, in
3227*0a4c4cecSXiao-Yu Zhang 		 * order to clear PxTFD.STS.BSY and PxTFD.STS.DRQ.
3228*0a4c4cecSXiao-Yu Zhang 		 */
3229*0a4c4cecSXiao-Yu Zhang 		AHCIDBG0(AHCIDBG_ERRS, ahci_ctlp,
3230*0a4c4cecSXiao-Yu Zhang 		    "PxTFD.STS.BSY/DRQ is set, try SCLO.")
3231*0a4c4cecSXiao-Yu Zhang 
3232b2e3645aSying tian - Beijing China 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3233b2e3645aSying tian - Beijing China 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
3234*0a4c4cecSXiao-Yu Zhang 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
3235*0a4c4cecSXiao-Yu Zhang 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
3236*0a4c4cecSXiao-Yu Zhang 		    port_cmd_status|AHCI_CMD_STATUS_CLO);
3237*0a4c4cecSXiao-Yu Zhang 
3238*0a4c4cecSXiao-Yu Zhang 		/* Waiting till PxCMD.SCLO bit is cleared */
3239*0a4c4cecSXiao-Yu Zhang 		loop_count = 0;
3240*0a4c4cecSXiao-Yu Zhang 		do {
3241*0a4c4cecSXiao-Yu Zhang 			/* Wait for 10 millisec */
3242*0a4c4cecSXiao-Yu Zhang 			drv_usecwait(AHCI_10MS_USECS);
3243*0a4c4cecSXiao-Yu Zhang 
3244*0a4c4cecSXiao-Yu Zhang 			/* We are effectively timing out after 1 sec. */
3245*0a4c4cecSXiao-Yu Zhang 			if (loop_count++ > 100) {
3246*0a4c4cecSXiao-Yu Zhang 				AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
3247*0a4c4cecSXiao-Yu Zhang 				    "SCLO time out. port %d is busy.", port);
3248*0a4c4cecSXiao-Yu Zhang 				goto out;
3249*0a4c4cecSXiao-Yu Zhang 			}
3250*0a4c4cecSXiao-Yu Zhang 
3251*0a4c4cecSXiao-Yu Zhang 			port_cmd_status =
3252*0a4c4cecSXiao-Yu Zhang 			    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3253*0a4c4cecSXiao-Yu Zhang 			    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
3254*0a4c4cecSXiao-Yu Zhang 		} while (port_cmd_status & AHCI_CMD_STATUS_CLO);
3255*0a4c4cecSXiao-Yu Zhang 
3256*0a4c4cecSXiao-Yu Zhang 		/* Re-check */
3257*0a4c4cecSXiao-Yu Zhang 		port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3258*0a4c4cecSXiao-Yu Zhang 		    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
3259*0a4c4cecSXiao-Yu Zhang 		if (port_task_file & AHCI_TFD_STS_BSY ||
3260*0a4c4cecSXiao-Yu Zhang 		    port_task_file & AHCI_TFD_STS_DRQ) {
3261*0a4c4cecSXiao-Yu Zhang 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
3262*0a4c4cecSXiao-Yu Zhang 			    "SCLO cannot clear PxTFD.STS.BSY/DRQ (PxTFD=0x%x)",
3263*0a4c4cecSXiao-Yu Zhang 			    port_task_file);
3264*0a4c4cecSXiao-Yu Zhang 			goto out;
32652fcbc377Syt 		}
32662fcbc377Syt 	}
32672fcbc377Syt 
3268*0a4c4cecSXiao-Yu Zhang 	/* Then start port */
3269*0a4c4cecSXiao-Yu Zhang 	if (ahci_start_port(ahci_ctlp, ahci_portp, port)
3270*0a4c4cecSXiao-Yu Zhang 	    != AHCI_SUCCESS) {
3271*0a4c4cecSXiao-Yu Zhang 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
3272*0a4c4cecSXiao-Yu Zhang 		    "ahci_software_reset: cannot start AHCI port %d.", port);
3273*0a4c4cecSXiao-Yu Zhang 		goto out;
32742fcbc377Syt 	}
32752fcbc377Syt 
3276*0a4c4cecSXiao-Yu Zhang 	/*
3277*0a4c4cecSXiao-Yu Zhang 	 * When ahci_port.ahciport_mop_in_progress is set, A non-zero
3278*0a4c4cecSXiao-Yu Zhang 	 * ahci_port.ahciport_pending_ncq_tags may fail
3279*0a4c4cecSXiao-Yu Zhang 	 * ahci_claim_free_slot(). Actually according to spec, by clearing
3280*0a4c4cecSXiao-Yu Zhang 	 * PxCMD.ST there is no command outstanding while executing software
3281*0a4c4cecSXiao-Yu Zhang 	 * reseting. Hence we directly use slot 0 instead of
3282*0a4c4cecSXiao-Yu Zhang 	 * ahci_claim_free_slot().
3283*0a4c4cecSXiao-Yu Zhang 	 */
3284*0a4c4cecSXiao-Yu Zhang 	slot = 0;
3285*0a4c4cecSXiao-Yu Zhang 
3286b2e3645aSying tian - Beijing China 	/* Now send the first H2D Register FIS with SRST set to 1 */
32872fcbc377Syt 	cmd_table = ahci_portp->ahciport_cmd_tables[slot];
32882fcbc377Syt 	bzero((void *)cmd_table, ahci_cmd_table_size);
32892fcbc377Syt 
32902fcbc377Syt 	h2d_register_fisp =
32912fcbc377Syt 	    &(cmd_table->ahcict_command_fis.ahcifc_fis.ahcifc_h2d_register);
32922fcbc377Syt 
32932fcbc377Syt 	SET_FIS_TYPE(h2d_register_fisp, AHCI_H2D_REGISTER_FIS_TYPE);
32942fcbc377Syt 	SET_FIS_DEVCTL(h2d_register_fisp, SATA_DEVCTL_SRST);
32952fcbc377Syt 
32962fcbc377Syt 	/* Set Command Header in Command List */
32972fcbc377Syt 	cmd_header = &ahci_portp->ahciport_cmd_list[slot];
32982fcbc377Syt 	BZERO_DESCR_INFO(cmd_header);
32992fcbc377Syt 	BZERO_PRD_BYTE_COUNT(cmd_header);
33002fcbc377Syt 	SET_COMMAND_FIS_LENGTH(cmd_header, 5);
33012fcbc377Syt 
33022fcbc377Syt 	SET_CLEAR_BUSY_UPON_R_OK(cmd_header, 1);
33032fcbc377Syt 	SET_RESET(cmd_header, 1);
33042fcbc377Syt 	SET_WRITE(cmd_header, 1);
33052fcbc377Syt 
33062fcbc377Syt 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_tables_dma_handle[slot],
33072fcbc377Syt 	    0,
33082fcbc377Syt 	    ahci_cmd_table_size,
33092fcbc377Syt 	    DDI_DMA_SYNC_FORDEV);
33102fcbc377Syt 
33112fcbc377Syt 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_list_dma_handle,
33122fcbc377Syt 	    slot * sizeof (ahci_cmd_header_t),
33132fcbc377Syt 	    sizeof (ahci_cmd_header_t),
33142fcbc377Syt 	    DDI_DMA_SYNC_FORDEV);
33152fcbc377Syt 
33162fcbc377Syt 	/* Indicate to the HBA that a command is active. */
33172fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
33182fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port),
33192fcbc377Syt 	    (0x1 << slot));
33202fcbc377Syt 
33212fcbc377Syt 	loop_count = 0;
33222fcbc377Syt 
33232fcbc377Syt 	/* Loop till the first command is finished */
33242fcbc377Syt 	do {
33252fcbc377Syt 		port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
33262fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
33272fcbc377Syt 
3328b2e3645aSying tian - Beijing China 		/* We are effectively timing out after 1 sec. */
33292fcbc377Syt 		if (loop_count++ > AHCI_POLLRATE_PORT_SOFTRESET) {
3330*0a4c4cecSXiao-Yu Zhang 			goto out;
33312fcbc377Syt 		}
33322fcbc377Syt 		/* Wait for 10 millisec */
33332fcbc377Syt 		delay(AHCI_10MS_TICKS);
33342fcbc377Syt 	} while (port_cmd_issue	& AHCI_SLOT_MASK(ahci_ctlp) & (0x1 << slot));
33352fcbc377Syt 
33362fcbc377Syt 	AHCIDBG3(AHCIDBG_POLL_LOOP, ahci_ctlp,
33372fcbc377Syt 	    "ahci_software_reset: 1st loop count: %d, "
33382fcbc377Syt 	    "port_cmd_issue = 0x%x, slot = 0x%x",
33392fcbc377Syt 	    loop_count, port_cmd_issue, slot);
33402fcbc377Syt 
3341b2e3645aSying tian - Beijing China 	/* Now send the second H2D Register FIS with SRST cleard to zero */
33422fcbc377Syt 	cmd_table = ahci_portp->ahciport_cmd_tables[slot];
33432fcbc377Syt 	bzero((void *)cmd_table, ahci_cmd_table_size);
33442fcbc377Syt 
33452fcbc377Syt 	h2d_register_fisp =
33462fcbc377Syt 	    &(cmd_table->ahcict_command_fis.ahcifc_fis.ahcifc_h2d_register);
33472fcbc377Syt 
33482fcbc377Syt 	SET_FIS_TYPE(h2d_register_fisp, AHCI_H2D_REGISTER_FIS_TYPE);
33492fcbc377Syt 
33502fcbc377Syt 	/* Set Command Header in Command List */
33512fcbc377Syt 	cmd_header = &ahci_portp->ahciport_cmd_list[slot];
33522fcbc377Syt 	BZERO_DESCR_INFO(cmd_header);
33532fcbc377Syt 	BZERO_PRD_BYTE_COUNT(cmd_header);
33542fcbc377Syt 	SET_COMMAND_FIS_LENGTH(cmd_header, 5);
33552fcbc377Syt 
33562fcbc377Syt 	SET_WRITE(cmd_header, 1);
33572fcbc377Syt 
33582fcbc377Syt 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_tables_dma_handle[slot],
33592fcbc377Syt 	    0,
33602fcbc377Syt 	    ahci_cmd_table_size,
33612fcbc377Syt 	    DDI_DMA_SYNC_FORDEV);
33622fcbc377Syt 
33632fcbc377Syt 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_list_dma_handle,
33642fcbc377Syt 	    slot * sizeof (ahci_cmd_header_t),
33652fcbc377Syt 	    sizeof (ahci_cmd_header_t),
33662fcbc377Syt 	    DDI_DMA_SYNC_FORDEV);
33672fcbc377Syt 
33682fcbc377Syt 	/* Indicate to the HBA that a command is active. */
33692fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
33702fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port),
33712fcbc377Syt 	    (0x1 << slot));
33722fcbc377Syt 
33732fcbc377Syt 	loop_count = 0;
33742fcbc377Syt 
33752fcbc377Syt 	/* Loop till the second command is finished */
33762fcbc377Syt 	do {
33772fcbc377Syt 		port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
33782fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
33792fcbc377Syt 
3380b2e3645aSying tian - Beijing China 		/* We are effectively timing out after 1 sec. */
33812fcbc377Syt 		if (loop_count++ > AHCI_POLLRATE_PORT_SOFTRESET) {
3382*0a4c4cecSXiao-Yu Zhang 			goto out;
33832fcbc377Syt 		}
33842fcbc377Syt 		/* Wait for 10 millisec */
33852fcbc377Syt 		delay(AHCI_10MS_TICKS);
33862fcbc377Syt 	} while (port_cmd_issue	& AHCI_SLOT_MASK(ahci_ctlp) & (0x1 << slot));
33872fcbc377Syt 
33882fcbc377Syt 	AHCIDBG3(AHCIDBG_POLL_LOOP, ahci_ctlp,
33892fcbc377Syt 	    "ahci_software_reset: 2nd loop count: %d, "
33902fcbc377Syt 	    "port_cmd_issue = 0x%x, slot = 0x%x",
33912fcbc377Syt 	    loop_count, port_cmd_issue, slot);
33922fcbc377Syt 
33932fcbc377Syt 	CLEAR_BIT(ahci_portp->ahciport_pending_tags, slot);
33942fcbc377Syt 
3395*0a4c4cecSXiao-Yu Zhang 	rval = AHCI_SUCCESS;
3396*0a4c4cecSXiao-Yu Zhang out:
3397*0a4c4cecSXiao-Yu Zhang 	AHCIDBG2(AHCIDBG_ERRS, ahci_ctlp,
3398*0a4c4cecSXiao-Yu Zhang 	    "ahci_software_reset: %s at port %d",
3399*0a4c4cecSXiao-Yu Zhang 	    rval == AHCI_SUCCESS ? "succeed" : "failed",
3400*0a4c4cecSXiao-Yu Zhang 	    port);
3401*0a4c4cecSXiao-Yu Zhang 
3402*0a4c4cecSXiao-Yu Zhang 	return (rval);
34032fcbc377Syt }
34042fcbc377Syt 
34052fcbc377Syt /*
34062fcbc377Syt  * AHCI port reset ...; the physical communication between the HBA and device
34072fcbc377Syt  * on a port are disabled. This is more intrusive.
34082fcbc377Syt  *
340968d33a25Syt  * When an HBA or port reset occurs, Phy communication is going to
34102fcbc377Syt  * be re-established with the device through a COMRESET followed by the
34112fcbc377Syt  * normal out-of-band communication sequence defined in Serial ATA. AT
34122fcbc377Syt  * the end of reset, the device, if working properly, will send a D2H
34132fcbc377Syt  * Register FIS, which contains the device signature. When the HBA receives
34142fcbc377Syt  * this FIS, it updates PxTFD.STS and PxTFD.ERR register fields, and updates
34152fcbc377Syt  * the PxSIG register with the signature.
34162fcbc377Syt  *
34172fcbc377Syt  * Staggered spin-up is an optional feature in SATA II, and it enables an HBA
34182fcbc377Syt  * to individually spin-up attached devices. Please refer to chapter 10.9 of
341968d33a25Syt  * AHCI 1.0 spec.
34202fcbc377Syt  */
34212fcbc377Syt /*
342282263d52Syt  * WARNING!!! ahciport_mutex should be acquired, and PxCMD.ST should be also
342382263d52Syt  * cleared before the function is called.
34242fcbc377Syt  */
34252fcbc377Syt static int
34262fcbc377Syt ahci_port_reset(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp, uint8_t port)
34272fcbc377Syt {
34282fcbc377Syt 	uint32_t cap_status, port_cmd_status;
3429*0a4c4cecSXiao-Yu Zhang 	uint32_t port_scontrol, port_sstatus, port_serror;
3430689d74b0Syt 	uint32_t port_intr_status, port_task_file;
3431*0a4c4cecSXiao-Yu Zhang 
34322fcbc377Syt 	int loop_count;
3433a9440e8dSyt 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
34342fcbc377Syt 
34352fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
34362fcbc377Syt 	    "Port %d port resetting...", port);
343768d33a25Syt 	ahci_portp->ahciport_port_state = 0;
3438*0a4c4cecSXiao-Yu Zhang 	ahci_portp->ahciport_device_type = SATA_DTYPE_UNKNOWN;
34392fcbc377Syt 
34402fcbc377Syt 	cap_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
34412fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_CAP(ahci_ctlp));
34422fcbc377Syt 
34432fcbc377Syt 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
34442fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
34452fcbc377Syt 
34462fcbc377Syt 	if (cap_status & AHCI_HBA_CAP_SSS) {
34472fcbc377Syt 		/*
34482fcbc377Syt 		 * HBA support staggered spin-up, if the port has
34492fcbc377Syt 		 * not spin up yet, then force it to do spin-up
34502fcbc377Syt 		 */
34512fcbc377Syt 		if (!(port_cmd_status & AHCI_CMD_STATUS_SUD)) {
34522fcbc377Syt 			if (!(ahci_portp->ahciport_flags
345368d33a25Syt 			    & AHCI_PORT_FLAG_SPINUP)) {
34542fcbc377Syt 				AHCIDBG1(AHCIDBG_INIT, ahci_ctlp,
34552fcbc377Syt 				    "Port %d PxCMD.SUD is zero, force "
34562fcbc377Syt 				    "it to do spin-up", port);
34572fcbc377Syt 				ahci_portp->ahciport_flags |=
345868d33a25Syt 				    AHCI_PORT_FLAG_SPINUP;
34592fcbc377Syt 			}
34602fcbc377Syt 		}
34612fcbc377Syt 	} else {
34622fcbc377Syt 		/*
34632fcbc377Syt 		 * HBA doesn't support stagger spin-up, force it
34642fcbc377Syt 		 * to do normal COMRESET
34652fcbc377Syt 		 */
34662fcbc377Syt 		if (ahci_portp->ahciport_flags &
346768d33a25Syt 		    AHCI_PORT_FLAG_SPINUP) {
34682fcbc377Syt 			AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
34692fcbc377Syt 			    "HBA does not support staggered spin-up "
34702fcbc377Syt 			    "force it to do normal COMRESET");
34712fcbc377Syt 			ahci_portp->ahciport_flags &=
347268d33a25Syt 			    ~AHCI_PORT_FLAG_SPINUP;
34732fcbc377Syt 		}
34742fcbc377Syt 	}
34752fcbc377Syt 
347668d33a25Syt 	if (!(ahci_portp->ahciport_flags & AHCI_PORT_FLAG_SPINUP)) {
34772fcbc377Syt 		/* Do normal COMRESET */
34782fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
34792fcbc377Syt 		    "ahci_port_reset: do normal COMRESET", port);
34802fcbc377Syt 
348195c11c1fSyt 		/*
348295c11c1fSyt 		 * According to the spec, SUD bit should be set here,
348395c11c1fSyt 		 * but JMicron JMB363 doesn't follow it, so remove
348495c11c1fSyt 		 * the assertion, and just print a debug message.
348595c11c1fSyt 		 */
348695c11c1fSyt #if AHCI_DEBUG
348795c11c1fSyt 		if (!(port_cmd_status & AHCI_CMD_STATUS_SUD))
348895c11c1fSyt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
348995c11c1fSyt 			    "port %d SUD bit not set", port)
349095c11c1fSyt #endif
34912fcbc377Syt 
34922fcbc377Syt 		port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
34932fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
349482263d52Syt 		SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_COMRESET);
34952fcbc377Syt 
34962fcbc377Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
34972fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port),
34982fcbc377Syt 		    port_scontrol);
34992fcbc377Syt 
35002fcbc377Syt 		/* Enable PxCMD.FRE to read device */
35012fcbc377Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
35022fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
35032fcbc377Syt 		    port_cmd_status|AHCI_CMD_STATUS_FRE);
35042fcbc377Syt 
350568d33a25Syt 		/*
350668d33a25Syt 		 * Give time for COMRESET to percolate, according to the AHCI
350768d33a25Syt 		 * spec, software shall wait at least 1 millisecond before
350868d33a25Syt 		 * clearing PxSCTL.DET
350968d33a25Syt 		 */
35102fcbc377Syt 		delay(AHCI_1MS_TICKS*2);
35112fcbc377Syt 
35122fcbc377Syt 		/* Fetch the SCONTROL again and rewrite the DET part with 0 */
35132fcbc377Syt 		port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
35142fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
351582263d52Syt 		SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_NOACTION);
35162fcbc377Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
35172fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port),
35182fcbc377Syt 		    port_scontrol);
35192fcbc377Syt 	} else {
35202fcbc377Syt 		/* Do staggered spin-up */
35212fcbc377Syt 		port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
35222fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
352382263d52Syt 		SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_NOACTION);
35242fcbc377Syt 
35252fcbc377Syt 		/* PxSCTL.DET must be 0 */
35262fcbc377Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
35272fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port),
35282fcbc377Syt 		    port_scontrol);
35292fcbc377Syt 
35302fcbc377Syt 		port_cmd_status &= ~AHCI_CMD_STATUS_SUD;
35312fcbc377Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
35322fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
35332fcbc377Syt 		    port_cmd_status);
35342fcbc377Syt 
35352fcbc377Syt 		/* 0 -> 1 edge */
35362fcbc377Syt 		delay(AHCI_1MS_TICKS*2);
35372fcbc377Syt 
35382fcbc377Syt 		/* Set PxCMD.SUD to 1 */
35392fcbc377Syt 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
35402fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
35412fcbc377Syt 		port_cmd_status |= AHCI_CMD_STATUS_SUD;
35422fcbc377Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
35432fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
35442fcbc377Syt 		    port_cmd_status);
35452fcbc377Syt 
35462fcbc377Syt 		/* Enable PxCMD.FRE to read device */
35472fcbc377Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
35482fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
35492fcbc377Syt 		    port_cmd_status|AHCI_CMD_STATUS_FRE);
35502fcbc377Syt 	}
35512fcbc377Syt 
35522fcbc377Syt 	/*
355368d33a25Syt 	 * The port enters P:StartComm state, and HBA tells link layer to
355468d33a25Syt 	 * start communication, which involves sending COMRESET to device.
355568d33a25Syt 	 * And the HBA resets PxTFD.STS to 7Fh.
355668d33a25Syt 	 *
355768d33a25Syt 	 * When a COMINIT is received from the device, then the port enters
355868d33a25Syt 	 * P:ComInit state. And HBA sets PxTFD.STS to FFh or 80h. HBA sets
355968d33a25Syt 	 * PxSSTS.DET to 1h to indicate a device is detected but communication
356068d33a25Syt 	 * is not yet established. HBA sets PxSERR.DIAG.X to '1' to indicate
356168d33a25Syt 	 * a COMINIT has been received.
35622fcbc377Syt 	 */
35632fcbc377Syt 	/*
35642fcbc377Syt 	 * The DET field is valid only if IPM field indicates
35652fcbc377Syt 	 * that the interface is in active state.
35662fcbc377Syt 	 */
35672fcbc377Syt 	loop_count = 0;
35682fcbc377Syt 	do {
35692fcbc377Syt 		port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
35702fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
35712fcbc377Syt 
357282263d52Syt 		if (SSTATUS_GET_IPM(port_sstatus) != SSTATUS_IPM_ACTIVE) {
35732fcbc377Syt 			/*
35742fcbc377Syt 			 * If the interface is not active, the DET field
35752fcbc377Syt 			 * is considered not accurate. So we want to
35762fcbc377Syt 			 * continue looping.
35772fcbc377Syt 			 */
357882263d52Syt 			SSTATUS_SET_DET(port_sstatus, SSTATUS_DET_NODEV);
35792fcbc377Syt 		}
35802fcbc377Syt 
35812fcbc377Syt 		if (loop_count++ > AHCI_POLLRATE_PORT_SSTATUS) {
35822fcbc377Syt 			/*
35832fcbc377Syt 			 * We are effectively timing out after 0.1 sec.
35842fcbc377Syt 			 */
35852fcbc377Syt 			break;
35862fcbc377Syt 		}
35872fcbc377Syt 
35882fcbc377Syt 		/* Wait for 10 millisec */
35892fcbc377Syt 		delay(AHCI_10MS_TICKS);
359082263d52Syt 	} while (SSTATUS_GET_DET(port_sstatus) != SSTATUS_DET_DEVPRE_PHYCOM);
35912fcbc377Syt 
359268d33a25Syt 	AHCIDBG3(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
359368d33a25Syt 	    "ahci_port_reset: 1st loop count: %d, "
359468d33a25Syt 	    "port_sstatus = 0x%x port %d",
359568d33a25Syt 	    loop_count, port_sstatus, port);
35962fcbc377Syt 
359782263d52Syt 	if ((SSTATUS_GET_IPM(port_sstatus) != SSTATUS_IPM_ACTIVE) ||
359882263d52Syt 	    (SSTATUS_GET_DET(port_sstatus) != SSTATUS_DET_DEVPRE_PHYCOM)) {
35992fcbc377Syt 		/*
36002fcbc377Syt 		 * Either the port is not active or there
36012fcbc377Syt 		 * is no device present.
36022fcbc377Syt 		 */
36032fcbc377Syt 		ahci_portp->ahciport_device_type = SATA_DTYPE_NONE;
3604*0a4c4cecSXiao-Yu Zhang 		return (AHCI_SUCCESS);
36052fcbc377Syt 	}
36062fcbc377Syt 
360768d33a25Syt 	/* Now we can make sure there is a device connected to the port */
360868d33a25Syt 	port_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
360968d33a25Syt 	    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port));
3610*0a4c4cecSXiao-Yu Zhang 	port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3611*0a4c4cecSXiao-Yu Zhang 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
36122fcbc377Syt 
3613*0a4c4cecSXiao-Yu Zhang 	/*
3614*0a4c4cecSXiao-Yu Zhang 	 * A COMINIT signal is supposed to be received
3615*0a4c4cecSXiao-Yu Zhang 	 * PxSERR.DIAG.X or PxIS.PCS should be set
3616*0a4c4cecSXiao-Yu Zhang 	 */
3617*0a4c4cecSXiao-Yu Zhang 	if (!(port_intr_status & AHCI_INTR_STATUS_PCS) &&
3618*0a4c4cecSXiao-Yu Zhang 	    !(port_serror & SERROR_EXCHANGED_ERR)) {
3619a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: ahci_port_reset port %d "
3620a9440e8dSyt 		    "COMINIT signal from the device not received",
3621a9440e8dSyt 		    instance, port);
362268d33a25Syt 		ahci_portp->ahciport_port_state |= SATA_PSTATE_FAILED;
3623*0a4c4cecSXiao-Yu Zhang 		return (AHCI_FAILURE);
362468d33a25Syt 	}
36252fcbc377Syt 
362695c11c1fSyt 	/*
362795c11c1fSyt 	 * According to the spec, when PxSCTL.DET is set to 0h, upon
362895c11c1fSyt 	 * receiving a COMINIT from the attached device, PxTFD.STS.BSY
362995c11c1fSyt 	 * shall be set to '1' by the HBA.
363095c11c1fSyt 	 *
363195c11c1fSyt 	 * However, we found JMicron JMB363 doesn't follow this, so
363295c11c1fSyt 	 * remove this check, and just print a debug message.
363395c11c1fSyt 	 */
363495c11c1fSyt #if AHCI_DEBUG
363568d33a25Syt 	port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
363668d33a25Syt 	    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
363768d33a25Syt 	if (!(port_task_file & AHCI_TFD_STS_BSY)) {
363895c11c1fSyt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_port_reset: "
363995c11c1fSyt 		    "port %d BSY bit is not set after COMINIT signal "
364095c11c1fSyt 		    "is received", port);
364168d33a25Syt 	}
364295c11c1fSyt #endif
36432fcbc377Syt 
364468d33a25Syt 	/*
364568d33a25Syt 	 * PxSERR.DIAG.X has to be cleared in order to update PxTFD with
364668d33a25Syt 	 * the D2H FIS received by HBA.
364768d33a25Syt 	 */
364868d33a25Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
364968d33a25Syt 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
365082263d52Syt 	    SERROR_EXCHANGED_ERR);
36512fcbc377Syt 
365268d33a25Syt 	/*
3653*0a4c4cecSXiao-Yu Zhang 	 * Devices should return a FIS contains its signature to HBA after
3654*0a4c4cecSXiao-Yu Zhang 	 * COMINIT signal. Check whether a D2H FIS is received by polling
3655*0a4c4cecSXiao-Yu Zhang 	 * PxTFD.STS.ERR bit.
365668d33a25Syt 	 */
365768d33a25Syt 	loop_count = 0;
365868d33a25Syt 	do {
3659*0a4c4cecSXiao-Yu Zhang 		/* Wait for 10 millisec */
3660*0a4c4cecSXiao-Yu Zhang 		delay(AHCI_10MS_TICKS);
36612fcbc377Syt 
366268d33a25Syt 		if (loop_count++ > AHCI_POLLRATE_PORT_TFD_ERROR) {
366368d33a25Syt 			/*
366468d33a25Syt 			 * We are effectively timing out after 11 sec.
366568d33a25Syt 			 */
3666*0a4c4cecSXiao-Yu Zhang 			cmn_err(CE_WARN, "!ahci%d: ahci_port_reset port %d "
3667*0a4c4cecSXiao-Yu Zhang 			    "the device hardware has been initialized and "
3668*0a4c4cecSXiao-Yu Zhang 			    "the power-up diagnostics failed",
3669*0a4c4cecSXiao-Yu Zhang 			    instance, port);
3670*0a4c4cecSXiao-Yu Zhang 
3671*0a4c4cecSXiao-Yu Zhang 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_port_reset: "
3672*0a4c4cecSXiao-Yu Zhang 			    "port %d PxTFD.STS.ERR is not set, we need another "
3673*0a4c4cecSXiao-Yu Zhang 			    "software reset.", port);
3674*0a4c4cecSXiao-Yu Zhang 
3675*0a4c4cecSXiao-Yu Zhang 			/* Clear port serror register for the port */
3676*0a4c4cecSXiao-Yu Zhang 			ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
3677*0a4c4cecSXiao-Yu Zhang 			    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
3678*0a4c4cecSXiao-Yu Zhang 			    AHCI_SERROR_CLEAR_ALL);
3679*0a4c4cecSXiao-Yu Zhang 
3680*0a4c4cecSXiao-Yu Zhang 			/* Try another software reset. */
3681*0a4c4cecSXiao-Yu Zhang 			if (ahci_software_reset(ahci_ctlp, ahci_portp,
3682*0a4c4cecSXiao-Yu Zhang 			    port) != AHCI_SUCCESS) {
3683*0a4c4cecSXiao-Yu Zhang 				ahci_portp->ahciport_port_state |=
3684*0a4c4cecSXiao-Yu Zhang 				    SATA_PSTATE_FAILED;
3685*0a4c4cecSXiao-Yu Zhang 				return (AHCI_FAILURE);
3686*0a4c4cecSXiao-Yu Zhang 			}
368768d33a25Syt 			break;
368868d33a25Syt 		}
36892fcbc377Syt 
3690*0a4c4cecSXiao-Yu Zhang 		/*
3691*0a4c4cecSXiao-Yu Zhang 		 * The Error bit '1' means COMRESET is finished successfully
3692*0a4c4cecSXiao-Yu Zhang 		 * The device hardware has been initialized and the power-up
3693*0a4c4cecSXiao-Yu Zhang 		 * diagnostics successfully completed. The device requests
3694*0a4c4cecSXiao-Yu Zhang 		 * that the Transport layer transmit a Register - D2H FIS to
3695*0a4c4cecSXiao-Yu Zhang 		 * the host. (SATA spec 11.5, v2.6)
3696*0a4c4cecSXiao-Yu Zhang 		 */
3697*0a4c4cecSXiao-Yu Zhang 		port_task_file =
3698*0a4c4cecSXiao-Yu Zhang 		    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3699*0a4c4cecSXiao-Yu Zhang 		    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
370068d33a25Syt 	} while (((port_task_file & AHCI_TFD_ERR_MASK)
3701*0a4c4cecSXiao-Yu Zhang 	    >> AHCI_TFD_ERR_SHIFT) != AHCI_TFD_ERR_SGS);
37022fcbc377Syt 
3703*0a4c4cecSXiao-Yu Zhang 	AHCIDBG3(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
3704*0a4c4cecSXiao-Yu Zhang 	    "ahci_port_reset: 2nd loop count: %d, "
3705*0a4c4cecSXiao-Yu Zhang 	    "port_task_file = 0x%x port %d",
370668d33a25Syt 	    loop_count, port_task_file, port);
37072fcbc377Syt 
370868d33a25Syt 	/* Clear port serror register for the port */
370968d33a25Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
371068d33a25Syt 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
371168d33a25Syt 	    AHCI_SERROR_CLEAR_ALL);
371268d33a25Syt 
3713*0a4c4cecSXiao-Yu Zhang 	/* Set port as ready */
3714*0a4c4cecSXiao-Yu Zhang 	ahci_portp->ahciport_port_state |= SATA_STATE_READY;
371568d33a25Syt 
3716*0a4c4cecSXiao-Yu Zhang 	AHCIDBG1(AHCIDBG_INFO|AHCIDBG_ERRS, ahci_ctlp,
3717*0a4c4cecSXiao-Yu Zhang 	    "ahci_port_reset: succeed at port %d.", port);
3718*0a4c4cecSXiao-Yu Zhang 	return (AHCI_SUCCESS);
37192fcbc377Syt }
37202fcbc377Syt 
37212fcbc377Syt /*
37222fcbc377Syt  * AHCI HBA reset ...; the entire HBA is reset, and all ports are disabled.
37232fcbc377Syt  * This is the most intrusive.
37242fcbc377Syt  *
372568d33a25Syt  * When an HBA reset occurs, Phy communication will be re-established with
372668d33a25Syt  * the device through a COMRESET followed by the normal out-of-band
372768d33a25Syt  * communication sequence defined in Serial ATA. AT the end of reset, the
372868d33a25Syt  * device, if working properly, will send a D2H Register FIS, which contains
372968d33a25Syt  * the device signature. When the HBA receives this FIS, it updates PxTFD.STS
373068d33a25Syt  * and PxTFD.ERR register fields, and updates the PxSIG register with the
373168d33a25Syt  * signature.
37322fcbc377Syt  *
37332fcbc377Syt  * Remember to set GHC.AE to 1 before calling ahci_hba_reset.
37342fcbc377Syt  */
37352fcbc377Syt static int
37362fcbc377Syt ahci_hba_reset(ahci_ctl_t *ahci_ctlp)
37372fcbc377Syt {
37382fcbc377Syt 	ahci_port_t *ahci_portp;
37392fcbc377Syt 	uint32_t ghc_control;
37402fcbc377Syt 	uint8_t port;
37412fcbc377Syt 	int loop_count;
37422fcbc377Syt 	int rval = AHCI_SUCCESS;
37432fcbc377Syt 
37442fcbc377Syt 	AHCIDBG0(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp, "HBA resetting");
37452fcbc377Syt 
374668d33a25Syt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
374768d33a25Syt 
37482fcbc377Syt 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
37492fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
37502fcbc377Syt 
37512fcbc377Syt 	/* Setting GHC.HR to 1, remember GHC.AE is already set to 1 before */
37522fcbc377Syt 	ghc_control |= AHCI_HBA_GHC_HR;
37532fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
37542fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
37552fcbc377Syt 
37562fcbc377Syt 	/*
37572fcbc377Syt 	 * Wait until HBA Reset complete or timeout
37582fcbc377Syt 	 */
37592fcbc377Syt 	loop_count = 0;
37602fcbc377Syt 	do {
37612fcbc377Syt 		ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
37622fcbc377Syt 		    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
37632fcbc377Syt 
37642fcbc377Syt 		if (loop_count++ > AHCI_POLLRATE_HBA_RESET) {
37652fcbc377Syt 			AHCIDBG1(AHCIDBG_INIT, ahci_ctlp,
37662fcbc377Syt 			    "ahci hba reset is timing out, "
37672fcbc377Syt 			    "ghc_control = 0x%x", ghc_control);
37682fcbc377Syt 			/* We are effectively timing out after 1 sec. */
37692fcbc377Syt 			break;
37702fcbc377Syt 		}
37712fcbc377Syt 
37722fcbc377Syt 		/* Wait for 10 millisec */
37732fcbc377Syt 		delay(AHCI_10MS_TICKS);
37742fcbc377Syt 	} while (ghc_control & AHCI_HBA_GHC_HR);
37752fcbc377Syt 
37762fcbc377Syt 	AHCIDBG2(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
37772fcbc377Syt 	    "ahci_hba_reset: 1st loop count: %d, "
37782fcbc377Syt 	    "ghc_control = 0x%x", loop_count, ghc_control);
37792fcbc377Syt 
37802fcbc377Syt 	if (ghc_control & AHCI_HBA_GHC_HR) {
37812fcbc377Syt 		/* The hba is not reset for some reasons */
37822fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
37832fcbc377Syt 		    "hba reset failed: HBA in a hung or locked state");
378468d33a25Syt 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
37852fcbc377Syt 		return (AHCI_FAILURE);
37862fcbc377Syt 	}
37872fcbc377Syt 
37882fcbc377Syt 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
37892fcbc377Syt 		/* Only check implemented ports */
37902fcbc377Syt 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
37912fcbc377Syt 			continue;
37922fcbc377Syt 		}
37932fcbc377Syt 
37942fcbc377Syt 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
37952fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
37962fcbc377Syt 
37972fcbc377Syt 		if (ahci_port_reset(ahci_ctlp, ahci_portp, port)
37982fcbc377Syt 		    != AHCI_SUCCESS) {
37992fcbc377Syt 			rval = AHCI_FAILURE;
38002fcbc377Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
38012fcbc377Syt 			    "ahci_hba_reset: port %d failed", port);
38022fcbc377Syt 		}
38032fcbc377Syt 
38042fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
38052fcbc377Syt 	}
38062fcbc377Syt 
38072fcbc377Syt 	/*
38082fcbc377Syt 	 * Indicate that system software is AHCI aware by setting
38092fcbc377Syt 	 * GHC.AE to 1
38102fcbc377Syt 	 */
38112fcbc377Syt 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
38122fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
38132fcbc377Syt 
38142fcbc377Syt 	ghc_control |= AHCI_HBA_GHC_AE;
38152fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
38162fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
38172fcbc377Syt 
381868d33a25Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
381968d33a25Syt 
38202fcbc377Syt 	return (rval);
38212fcbc377Syt }
38222fcbc377Syt 
38232fcbc377Syt /*
38242fcbc377Syt  * This routine is only called from AHCI_ATTACH or phyrdy change
3825*0a4c4cecSXiao-Yu Zhang  * case. It first calls software reset, then stop the port and try to
3826*0a4c4cecSXiao-Yu Zhang  * read PxSIG register to find the type of device attached to the port.
3827*0a4c4cecSXiao-Yu Zhang  *
3828*0a4c4cecSXiao-Yu Zhang  * The caller should make sure a valid device exists on specified port and
3829*0a4c4cecSXiao-Yu Zhang  * physical communication has been established so that the signature could
3830*0a4c4cecSXiao-Yu Zhang  * be retrieved by software reset.
38312fcbc377Syt  *
38322fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
383368d33a25Syt  * is called. And the port interrupt is disabled.
38342fcbc377Syt  */
383568d33a25Syt static void
38362fcbc377Syt ahci_find_dev_signature(ahci_ctl_t *ahci_ctlp,
38372fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
38382fcbc377Syt {
38392fcbc377Syt 	uint32_t signature;
38402fcbc377Syt 
38412fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
384268d33a25Syt 	    "ahci_find_dev_signature enter: port %d", port);
38432fcbc377Syt 
384468d33a25Syt 	ahci_portp->ahciport_device_type = SATA_DTYPE_UNKNOWN;
38452fcbc377Syt 
3846*0a4c4cecSXiao-Yu Zhang 	/* Issue a software reset to get the signature */
3847*0a4c4cecSXiao-Yu Zhang 	if (ahci_software_reset(ahci_ctlp, ahci_portp, port)
3848*0a4c4cecSXiao-Yu Zhang 	    != AHCI_SUCCESS) {
384968d33a25Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
3850*0a4c4cecSXiao-Yu Zhang 		    "ahci_find_dev_signature: software reset failed "
3851*0a4c4cecSXiao-Yu Zhang 		    "at port %d. cannot get signature.", port);
3852*0a4c4cecSXiao-Yu Zhang 		ahci_portp->ahciport_port_state = SATA_PSTATE_FAILED;
385368d33a25Syt 		return;
38542fcbc377Syt 	}
38552fcbc377Syt 
3856*0a4c4cecSXiao-Yu Zhang 	/*
3857*0a4c4cecSXiao-Yu Zhang 	 * ahci_software_reset has started the port, so we need manually stop
3858*0a4c4cecSXiao-Yu Zhang 	 * the port again.
3859*0a4c4cecSXiao-Yu Zhang 	 */
3860*0a4c4cecSXiao-Yu Zhang 	if (ahci_put_port_into_notrunning_state(ahci_ctlp, ahci_portp, port)
3861*0a4c4cecSXiao-Yu Zhang 	    != AHCI_SUCCESS) {
3862*0a4c4cecSXiao-Yu Zhang 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
3863*0a4c4cecSXiao-Yu Zhang 		    "ahci_find_dev_signature: cannot stop port %d.", port);
3864*0a4c4cecSXiao-Yu Zhang 		ahci_portp->ahciport_port_state = SATA_PSTATE_FAILED;
386568d33a25Syt 		return;
386668d33a25Syt 	}
38672fcbc377Syt 
3868*0a4c4cecSXiao-Yu Zhang 	/* Now we can make sure that a valid signature is received. */
38692fcbc377Syt 	signature = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
38702fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxSIG(ahci_ctlp, port));
38712fcbc377Syt 
38722fcbc377Syt 	AHCIDBG2(AHCIDBG_INIT|AHCIDBG_INFO, ahci_ctlp,
387368d33a25Syt 	    "ahci_find_dev_signature: port %d signature = 0x%x",
387468d33a25Syt 	    port, signature);
38752fcbc377Syt 
38762fcbc377Syt 	switch (signature) {
38772fcbc377Syt 
38782fcbc377Syt 	case AHCI_SIGNATURE_DISK:
38792fcbc377Syt 		ahci_portp->ahciport_device_type = SATA_DTYPE_ATADISK;
38802fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
38812fcbc377Syt 		    "Disk is found at port: %d", port);
38822fcbc377Syt 		break;
38832fcbc377Syt 
38842fcbc377Syt 	case AHCI_SIGNATURE_ATAPI:
388538547057Sying tian - Beijing China 		ahci_portp->ahciport_device_type = SATA_DTYPE_ATAPI;
38862fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
38872fcbc377Syt 		    "ATAPI device is found at port: %d", port);
38882fcbc377Syt 		break;
38892fcbc377Syt 
38902fcbc377Syt 	case AHCI_SIGNATURE_PORT_MULTIPLIER:
38912fcbc377Syt 		ahci_portp->ahciport_device_type = SATA_DTYPE_PMULT;
38922fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
38932fcbc377Syt 		    "Port Multiplier is found at port: %d", port);
38942fcbc377Syt 		break;
38952fcbc377Syt 
38962fcbc377Syt 	default:
38972fcbc377Syt 		ahci_portp->ahciport_device_type = SATA_DTYPE_UNKNOWN;
38982fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp,
38992fcbc377Syt 		    "Unknown device is found at port: %d", port);
39002fcbc377Syt 	}
39012fcbc377Syt }
39022fcbc377Syt 
390313bcbb7aSyt /*
390413bcbb7aSyt  * According to the spec, to reliably detect hot plug removals, software
390513bcbb7aSyt  * must disable interface power management. Software should perform the
390613bcbb7aSyt  * following initialization on a port after a device is attached:
390713bcbb7aSyt  *   Set PxSCTL.IPM to 3h to disable interface state transitions
390813bcbb7aSyt  *   Set PxCMD.ALPE to '0' to disable aggressive power management
390913bcbb7aSyt  *   Disable device initiated interface power management by SET FEATURE
391013bcbb7aSyt  *
391113bcbb7aSyt  * We can ignore the last item because by default the feature is disabled
391213bcbb7aSyt  */
391313bcbb7aSyt static void
391413bcbb7aSyt ahci_disable_interface_pm(ahci_ctl_t *ahci_ctlp, uint8_t port)
391513bcbb7aSyt {
391613bcbb7aSyt 	uint32_t port_scontrol, port_cmd_status;
391713bcbb7aSyt 
391813bcbb7aSyt 	port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
391913bcbb7aSyt 	    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
392013bcbb7aSyt 	SCONTROL_SET_IPM(port_scontrol, SCONTROL_IPM_DISABLE_BOTH);
392113bcbb7aSyt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
392213bcbb7aSyt 	    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port), port_scontrol);
392313bcbb7aSyt 
392413bcbb7aSyt 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
392513bcbb7aSyt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
392613bcbb7aSyt 	port_cmd_status &= ~AHCI_CMD_STATUS_ALPE;
392713bcbb7aSyt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
392813bcbb7aSyt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port), port_cmd_status);
392913bcbb7aSyt }
393013bcbb7aSyt 
39312fcbc377Syt /*
393268d33a25Syt  * Start the port - set PxCMD.ST to 1, if PxCMD.FRE is not set
393368d33a25Syt  * to 1, then set it firstly.
393468d33a25Syt  *
393568d33a25Syt  * Each port contains two major DMA engines. One DMA engine walks through
393668d33a25Syt  * the command list, and is controlled by PxCMD.ST. The second DMA engine
393768d33a25Syt  * copies received FISes into system memory, and is controlled by PxCMD.FRE.
393868d33a25Syt  *
393968d33a25Syt  * Software shall not set PxCMD.ST to '1' until it verifies that PxCMD.CR
394068d33a25Syt  * is '0' and has set PxCMD.FRE is '1'. And software shall not clear
394168d33a25Syt  * PxCMD.FRE while PxCMD.ST or PxCMD.CR is set '1'.
394268d33a25Syt  *
394368d33a25Syt  * Software shall not set PxCMD.ST to '1' unless a functional device is
394468d33a25Syt  * present on the port(as determined by PxTFD.STS.BSY = '0',
394568d33a25Syt  * PxTFD.STS.DRQ = '0', and PxSSTS.DET = 3h).
39462fcbc377Syt  *
39472fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
39482fcbc377Syt  * is called.
39492fcbc377Syt  */
39502fcbc377Syt static int
395168d33a25Syt ahci_start_port(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp, uint8_t port)
39522fcbc377Syt {
395368d33a25Syt 	uint32_t port_cmd_status;
39542fcbc377Syt 
395568d33a25Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp, "ahci_start_port: %d enter", port);
39562fcbc377Syt 
395768d33a25Syt 	if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED) {
395868d33a25Syt 		AHCIDBG2(AHCIDBG_ERRS, ahci_ctlp, "ahci_start_port failed "
395968d33a25Syt 		    "the state for port %d is 0x%x",
396068d33a25Syt 		    port, ahci_portp->ahciport_port_state);
39612fcbc377Syt 		return (AHCI_FAILURE);
396268d33a25Syt 	}
39632fcbc377Syt 
396468d33a25Syt 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
396568d33a25Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_start_port failed "
396668d33a25Syt 		    "no device is attached at port %d", port);
396768d33a25Syt 		return (AHCI_FAILURE);
396868d33a25Syt 	}
39692fcbc377Syt 
397068d33a25Syt 	/* First to set PxCMD.FRE before setting PxCMD.ST. */
397168d33a25Syt 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
397268d33a25Syt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
39732fcbc377Syt 
397468d33a25Syt 	if (!(port_cmd_status & AHCI_CMD_STATUS_FRE)) {
397568d33a25Syt 		port_cmd_status |= AHCI_CMD_STATUS_FRE;
39762fcbc377Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
39772fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
39782fcbc377Syt 		    port_cmd_status);
39792fcbc377Syt 	}
398068d33a25Syt 
398168d33a25Syt 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
398268d33a25Syt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
398368d33a25Syt 
398468d33a25Syt 	port_cmd_status |= AHCI_CMD_STATUS_ST;
398568d33a25Syt 
398668d33a25Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
398768d33a25Syt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
398868d33a25Syt 	    port_cmd_status);
398968d33a25Syt 
399068d33a25Syt 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_STARTED;
399168d33a25Syt 
3992*0a4c4cecSXiao-Yu Zhang 	AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_start_port: "
3993*0a4c4cecSXiao-Yu Zhang 	    "PxCMD.ST set to '1' at port %d", port);
3994*0a4c4cecSXiao-Yu Zhang 
399568d33a25Syt 	return (AHCI_SUCCESS);
39962fcbc377Syt }
39972fcbc377Syt 
39982fcbc377Syt /*
39992fcbc377Syt  * Allocate the ahci_port_t including Received FIS and Command List.
40002fcbc377Syt  * The argument - port is the physical port number, and not logical
40012fcbc377Syt  * port number seen by the SATA framework.
40022fcbc377Syt  *
40032fcbc377Syt  * WARNING!!! ahcictl_mutex should be acquired before the function
40042fcbc377Syt  * is called.
40052fcbc377Syt  */
40062fcbc377Syt static int
40072fcbc377Syt ahci_alloc_port_state(ahci_ctl_t *ahci_ctlp, uint8_t port)
40082fcbc377Syt {
4009f8a673adSying tian - Beijing China 	dev_info_t *dip = ahci_ctlp->ahcictl_dip;
40102fcbc377Syt 	ahci_port_t *ahci_portp;
4011f8a673adSying tian - Beijing China 	char taskq_name[64] = "event_handle_taskq";
40122fcbc377Syt 
40132fcbc377Syt 	ahci_portp =
40142fcbc377Syt 	    (ahci_port_t *)kmem_zalloc(sizeof (ahci_port_t), KM_SLEEP);
40152fcbc377Syt 
40162fcbc377Syt 	ahci_ctlp->ahcictl_ports[port] = ahci_portp;
40172fcbc377Syt 	ahci_portp->ahciport_port_num = port;
40182fcbc377Syt 
401968d33a25Syt 	/* Intialize the port condition variable */
402068d33a25Syt 	cv_init(&ahci_portp->ahciport_cv, NULL, CV_DRIVER, NULL);
402168d33a25Syt 
402268d33a25Syt 	/* Initialize the port mutex */
40232fcbc377Syt 	mutex_init(&ahci_portp->ahciport_mutex, NULL, MUTEX_DRIVER,
40242fcbc377Syt 	    (void *)(uintptr_t)ahci_ctlp->ahcictl_intr_pri);
402568d33a25Syt 
40262fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
40272fcbc377Syt 
40282fcbc377Syt 	/*
40292fcbc377Syt 	 * Allocate memory for received FIS structure and
40302fcbc377Syt 	 * command list for this port
40312fcbc377Syt 	 */
40322fcbc377Syt 	if (ahci_alloc_rcvd_fis(ahci_ctlp, ahci_portp, port) != AHCI_SUCCESS) {
40332fcbc377Syt 		goto err_case1;
40342fcbc377Syt 	}
40352fcbc377Syt 
40362fcbc377Syt 	if (ahci_alloc_cmd_list(ahci_ctlp, ahci_portp, port) != AHCI_SUCCESS) {
40372fcbc377Syt 		goto err_case2;
40382fcbc377Syt 	}
40392fcbc377Syt 
4040f8a673adSying tian - Beijing China 	(void) snprintf(taskq_name + strlen(taskq_name),
4041f8a673adSying tian - Beijing China 	    sizeof (taskq_name) - strlen(taskq_name),
4042f8a673adSying tian - Beijing China 	    "_port%d", port);
4043f8a673adSying tian - Beijing China 
4044f8a673adSying tian - Beijing China 	/* Create the taskq for the port */
4045f8a673adSying tian - Beijing China 	if ((ahci_portp->ahciport_event_taskq = ddi_taskq_create(dip,
4046f8a673adSying tian - Beijing China 	    taskq_name, 2, TASKQ_DEFAULTPRI, 0)) == NULL) {
4047f8a673adSying tian - Beijing China 		cmn_err(CE_WARN, "!ahci%d: ddi_taskq_create failed for event "
4048f8a673adSying tian - Beijing China 		    "handle", ddi_get_instance(ahci_ctlp->ahcictl_dip));
4049f8a673adSying tian - Beijing China 		goto err_case3;
4050f8a673adSying tian - Beijing China 	}
4051f8a673adSying tian - Beijing China 
4052f8a673adSying tian - Beijing China 	/* Allocate the argument for the taskq */
405368d33a25Syt 	ahci_portp->ahciport_event_args =
405468d33a25Syt 	    kmem_zalloc(sizeof (ahci_event_arg_t), KM_SLEEP);
405568d33a25Syt 
405668d33a25Syt 	if (ahci_portp->ahciport_event_args == NULL)
4057f8a673adSying tian - Beijing China 		goto err_case4;
405868d33a25Syt 
40592fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
40602fcbc377Syt 
40612fcbc377Syt 	return (AHCI_SUCCESS);
40622fcbc377Syt 
4063f8a673adSying tian - Beijing China err_case4:
4064f8a673adSying tian - Beijing China 	ddi_taskq_destroy(ahci_portp->ahciport_event_taskq);
4065f8a673adSying tian - Beijing China 
406668d33a25Syt err_case3:
406768d33a25Syt 	ahci_dealloc_cmd_list(ahci_ctlp, ahci_portp);
406868d33a25Syt 
40692fcbc377Syt err_case2:
4070689d74b0Syt 	ahci_dealloc_rcvd_fis(ahci_portp);
40712fcbc377Syt 
40722fcbc377Syt err_case1:
40732fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
40742fcbc377Syt 	mutex_destroy(&ahci_portp->ahciport_mutex);
407568d33a25Syt 	cv_destroy(&ahci_portp->ahciport_cv);
40762fcbc377Syt 
40772fcbc377Syt 	kmem_free(ahci_portp, sizeof (ahci_port_t));
40782fcbc377Syt 
40792fcbc377Syt 	return (AHCI_FAILURE);
40802fcbc377Syt }
40812fcbc377Syt 
40822fcbc377Syt /*
40832fcbc377Syt  * Reverse of ahci_dealloc_port_state().
40842fcbc377Syt  *
40852fcbc377Syt  * WARNING!!! ahcictl_mutex should be acquired before the function
40862fcbc377Syt  * is called.
40872fcbc377Syt  */
40882fcbc377Syt static void
40892fcbc377Syt ahci_dealloc_port_state(ahci_ctl_t *ahci_ctlp, uint8_t port)
40902fcbc377Syt {
40912fcbc377Syt 	ahci_port_t *ahci_portp = ahci_ctlp->ahcictl_ports[port];
40922fcbc377Syt 
40932fcbc377Syt 	ASSERT(ahci_portp != NULL);
40942fcbc377Syt 
40952fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
409668d33a25Syt 	kmem_free(ahci_portp->ahciport_event_args, sizeof (ahci_event_arg_t));
409768d33a25Syt 	ahci_portp->ahciport_event_args = NULL;
4098f8a673adSying tian - Beijing China 	ddi_taskq_destroy(ahci_portp->ahciport_event_taskq);
40992fcbc377Syt 	ahci_dealloc_cmd_list(ahci_ctlp, ahci_portp);
4100689d74b0Syt 	ahci_dealloc_rcvd_fis(ahci_portp);
41012fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
41022fcbc377Syt 
41032fcbc377Syt 	mutex_destroy(&ahci_portp->ahciport_mutex);
410468d33a25Syt 	cv_destroy(&ahci_portp->ahciport_cv);
41052fcbc377Syt 
41062fcbc377Syt 	kmem_free(ahci_portp, sizeof (ahci_port_t));
41072fcbc377Syt 
41082fcbc377Syt 	ahci_ctlp->ahcictl_ports[port] = NULL;
41092fcbc377Syt }
41102fcbc377Syt 
41112fcbc377Syt /*
41122fcbc377Syt  * Allocates memory for the Received FIS Structure
41132fcbc377Syt  *
41142fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
41152fcbc377Syt  * is called.
41162fcbc377Syt  */
41172fcbc377Syt static int
41182fcbc377Syt ahci_alloc_rcvd_fis(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
41192fcbc377Syt     uint8_t port)
41202fcbc377Syt {
41212fcbc377Syt 	size_t rcvd_fis_size;
41222fcbc377Syt 	size_t ret_len;
41232fcbc377Syt 	uint_t cookie_count;
41242fcbc377Syt 
41252fcbc377Syt 	rcvd_fis_size = sizeof (ahci_rcvd_fis_t);
41262fcbc377Syt 
41272fcbc377Syt 	/* allocate rcvd FIS dma handle. */
41282fcbc377Syt 	if (ddi_dma_alloc_handle(ahci_ctlp->ahcictl_dip,
41292fcbc377Syt 	    &ahci_ctlp->ahcictl_rcvd_fis_dma_attr,
41302fcbc377Syt 	    DDI_DMA_SLEEP,
41312fcbc377Syt 	    NULL,
41322fcbc377Syt 	    &ahci_portp->ahciport_rcvd_fis_dma_handle) !=
41332fcbc377Syt 	    DDI_SUCCESS) {
41342fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
41352fcbc377Syt 		    "rcvd FIS dma handle alloc failed");
41362fcbc377Syt 
41372fcbc377Syt 		return (AHCI_FAILURE);
41382fcbc377Syt 	}
41392fcbc377Syt 
41402fcbc377Syt 	if (ddi_dma_mem_alloc(ahci_portp->ahciport_rcvd_fis_dma_handle,
41412fcbc377Syt 	    rcvd_fis_size,
41422fcbc377Syt 	    &accattr,
41432fcbc377Syt 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
41442fcbc377Syt 	    DDI_DMA_SLEEP,
41452fcbc377Syt 	    NULL,
41462fcbc377Syt 	    (caddr_t *)&ahci_portp->ahciport_rcvd_fis,
41472fcbc377Syt 	    &ret_len,
41482fcbc377Syt 	    &ahci_portp->ahciport_rcvd_fis_acc_handle) != NULL) {
41492fcbc377Syt 
41502fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
41512fcbc377Syt 		    "rcvd FIS dma mem alloc fail");
41522fcbc377Syt 		/* error.. free the dma handle. */
41532fcbc377Syt 		ddi_dma_free_handle(&ahci_portp->ahciport_rcvd_fis_dma_handle);
41542fcbc377Syt 		return (AHCI_FAILURE);
41552fcbc377Syt 	}
41562fcbc377Syt 
41572fcbc377Syt 	if (ddi_dma_addr_bind_handle(ahci_portp->ahciport_rcvd_fis_dma_handle,
41582fcbc377Syt 	    NULL,
41592fcbc377Syt 	    (caddr_t)ahci_portp->ahciport_rcvd_fis,
41602fcbc377Syt 	    rcvd_fis_size,
41612fcbc377Syt 	    DDI_DMA_CONSISTENT,
41622fcbc377Syt 	    DDI_DMA_SLEEP,
41632fcbc377Syt 	    NULL,
416413bcbb7aSyt 	    &ahci_portp->ahciport_rcvd_fis_dma_cookie,
41652fcbc377Syt 	    &cookie_count) !=  DDI_DMA_MAPPED) {
41662fcbc377Syt 
41672fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
41682fcbc377Syt 		    "rcvd FIS dma handle bind fail");
41692fcbc377Syt 		/*  error.. free the dma handle & free the memory. */
41702fcbc377Syt 		ddi_dma_mem_free(&ahci_portp->ahciport_rcvd_fis_acc_handle);
41712fcbc377Syt 		ddi_dma_free_handle(&ahci_portp->ahciport_rcvd_fis_dma_handle);
41722fcbc377Syt 		return (AHCI_FAILURE);
41732fcbc377Syt 	}
41742fcbc377Syt 
41752fcbc377Syt 	bzero((void *)ahci_portp->ahciport_rcvd_fis, rcvd_fis_size);
41762fcbc377Syt 
41772fcbc377Syt 	/* Config Port Received FIS Base Address */
41782fcbc377Syt 	ddi_put64(ahci_ctlp->ahcictl_ahci_acc_handle,
41792fcbc377Syt 	    (uint64_t *)AHCI_PORT_PxFB(ahci_ctlp, port),
418013bcbb7aSyt 	    ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_laddress);
41812fcbc377Syt 
41822fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "64-bit, dma address: 0x%llx",
418313bcbb7aSyt 	    ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_laddress);
41842fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "32-bit, dma address: 0x%x",
418513bcbb7aSyt 	    ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_address);
41862fcbc377Syt 
41872fcbc377Syt 	return (AHCI_SUCCESS);
41882fcbc377Syt }
41892fcbc377Syt 
41902fcbc377Syt /*
41912fcbc377Syt  * Deallocates the Received FIS Structure
41922fcbc377Syt  *
41932fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
41942fcbc377Syt  * is called.
41952fcbc377Syt  */
41962fcbc377Syt static void
4197689d74b0Syt ahci_dealloc_rcvd_fis(ahci_port_t *ahci_portp)
41982fcbc377Syt {
41992fcbc377Syt 	/* Unbind the cmd list dma handle first. */
42002fcbc377Syt 	(void) ddi_dma_unbind_handle(ahci_portp->ahciport_rcvd_fis_dma_handle);
42012fcbc377Syt 
42022fcbc377Syt 	/* Then free the underlying memory. */
42032fcbc377Syt 	ddi_dma_mem_free(&ahci_portp->ahciport_rcvd_fis_acc_handle);
42042fcbc377Syt 
42052fcbc377Syt 	/* Now free the handle itself. */
42062fcbc377Syt 	ddi_dma_free_handle(&ahci_portp->ahciport_rcvd_fis_dma_handle);
42072fcbc377Syt }
42082fcbc377Syt 
42092fcbc377Syt /*
42102fcbc377Syt  * Allocates memory for the Command List, which contains up to 32 entries.
42112fcbc377Syt  * Each entry contains a command header, which is a 32-byte structure that
42122fcbc377Syt  * includes the pointer to the command table.
42132fcbc377Syt  *
42142fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
42152fcbc377Syt  * is called.
42162fcbc377Syt  */
42172fcbc377Syt static int
42182fcbc377Syt ahci_alloc_cmd_list(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
42192fcbc377Syt     uint8_t port)
42202fcbc377Syt {
42212fcbc377Syt 	size_t cmd_list_size;
42222fcbc377Syt 	size_t ret_len;
42232fcbc377Syt 	uint_t cookie_count;
42242fcbc377Syt 
42252fcbc377Syt 	cmd_list_size =
42262fcbc377Syt 	    ahci_ctlp->ahcictl_num_cmd_slots * sizeof (ahci_cmd_header_t);
42272fcbc377Syt 
42282fcbc377Syt 	/* allocate cmd list dma handle. */
42292fcbc377Syt 	if (ddi_dma_alloc_handle(ahci_ctlp->ahcictl_dip,
42302fcbc377Syt 	    &ahci_ctlp->ahcictl_cmd_list_dma_attr,
42312fcbc377Syt 	    DDI_DMA_SLEEP,
42322fcbc377Syt 	    NULL,
42332fcbc377Syt 	    &ahci_portp->ahciport_cmd_list_dma_handle) != DDI_SUCCESS) {
42342fcbc377Syt 
42352fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
42362fcbc377Syt 		    "cmd list dma handle alloc failed");
42372fcbc377Syt 		return (AHCI_FAILURE);
42382fcbc377Syt 	}
42392fcbc377Syt 
42402fcbc377Syt 	if (ddi_dma_mem_alloc(ahci_portp->ahciport_cmd_list_dma_handle,
42412fcbc377Syt 	    cmd_list_size,
42422fcbc377Syt 	    &accattr,
42432fcbc377Syt 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
42442fcbc377Syt 	    DDI_DMA_SLEEP,
42452fcbc377Syt 	    NULL,
42462fcbc377Syt 	    (caddr_t *)&ahci_portp->ahciport_cmd_list,
42472fcbc377Syt 	    &ret_len,
42482fcbc377Syt 	    &ahci_portp->ahciport_cmd_list_acc_handle) != NULL) {
42492fcbc377Syt 
42502fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
42512fcbc377Syt 		    "cmd list dma mem alloc fail");
42522fcbc377Syt 		/* error.. free the dma handle. */
42532fcbc377Syt 		ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
42542fcbc377Syt 		return (AHCI_FAILURE);
42552fcbc377Syt 	}
42562fcbc377Syt 
42572fcbc377Syt 	if (ddi_dma_addr_bind_handle(ahci_portp->ahciport_cmd_list_dma_handle,
42582fcbc377Syt 	    NULL,
42592fcbc377Syt 	    (caddr_t)ahci_portp->ahciport_cmd_list,
42602fcbc377Syt 	    cmd_list_size,
42612fcbc377Syt 	    DDI_DMA_CONSISTENT,
42622fcbc377Syt 	    DDI_DMA_SLEEP,
42632fcbc377Syt 	    NULL,
426413bcbb7aSyt 	    &ahci_portp->ahciport_cmd_list_dma_cookie,
42652fcbc377Syt 	    &cookie_count) !=  DDI_DMA_MAPPED) {
42662fcbc377Syt 
42672fcbc377Syt 		AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
42682fcbc377Syt 		    "cmd list dma handle bind fail");
42692fcbc377Syt 		/*  error.. free the dma handle & free the memory. */
42702fcbc377Syt 		ddi_dma_mem_free(&ahci_portp->ahciport_cmd_list_acc_handle);
42712fcbc377Syt 		ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
42722fcbc377Syt 		return (AHCI_FAILURE);
42732fcbc377Syt 	}
42742fcbc377Syt 
42752fcbc377Syt 	bzero((void *)ahci_portp->ahciport_cmd_list, cmd_list_size);
42762fcbc377Syt 
42772fcbc377Syt 	/* Config Port Command List Base Address */
42782fcbc377Syt 	ddi_put64(ahci_ctlp->ahcictl_ahci_acc_handle,
42792fcbc377Syt 	    (uint64_t *)AHCI_PORT_PxCLB(ahci_ctlp, port),
428013bcbb7aSyt 	    ahci_portp->ahciport_cmd_list_dma_cookie.dmac_laddress);
42812fcbc377Syt 
42822fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "64-bit, dma address: 0x%llx",
428313bcbb7aSyt 	    ahci_portp->ahciport_cmd_list_dma_cookie.dmac_laddress);
42842fcbc377Syt 
42852fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT, ahci_ctlp, "32-bit, dma address: 0x%x",
428613bcbb7aSyt 	    ahci_portp->ahciport_cmd_list_dma_cookie.dmac_address);
42872fcbc377Syt 
42882fcbc377Syt 	if (ahci_alloc_cmd_tables(ahci_ctlp, ahci_portp) != AHCI_SUCCESS) {
4289e2decd04Syt 		goto err_out;
42902fcbc377Syt 	}
42912fcbc377Syt 
42922fcbc377Syt 	return (AHCI_SUCCESS);
4293e2decd04Syt 
4294e2decd04Syt err_out:
4295e2decd04Syt 	/* Unbind the cmd list dma handle first. */
4296e2decd04Syt 	(void) ddi_dma_unbind_handle(ahci_portp->ahciport_cmd_list_dma_handle);
4297e2decd04Syt 
4298e2decd04Syt 	/* Then free the underlying memory. */
4299e2decd04Syt 	ddi_dma_mem_free(&ahci_portp->ahciport_cmd_list_acc_handle);
4300e2decd04Syt 
4301e2decd04Syt 	/* Now free the handle itself. */
4302e2decd04Syt 	ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
4303e2decd04Syt 
4304e2decd04Syt 	return (AHCI_FAILURE);
43052fcbc377Syt }
43062fcbc377Syt 
43072fcbc377Syt /*
43082fcbc377Syt  * Deallocates the Command List
43092fcbc377Syt  *
43102fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
43112fcbc377Syt  * is called.
43122fcbc377Syt  */
43132fcbc377Syt static void
43142fcbc377Syt ahci_dealloc_cmd_list(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
43152fcbc377Syt {
43162fcbc377Syt 	/* First dealloc command table */
43172fcbc377Syt 	ahci_dealloc_cmd_tables(ahci_ctlp, ahci_portp);
43182fcbc377Syt 
43192fcbc377Syt 	/* Unbind the cmd list dma handle first. */
43202fcbc377Syt 	(void) ddi_dma_unbind_handle(ahci_portp->ahciport_cmd_list_dma_handle);
43212fcbc377Syt 
43222fcbc377Syt 	/* Then free the underlying memory. */
43232fcbc377Syt 	ddi_dma_mem_free(&ahci_portp->ahciport_cmd_list_acc_handle);
43242fcbc377Syt 
43252fcbc377Syt 	/* Now free the handle itself. */
43262fcbc377Syt 	ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
43272fcbc377Syt }
43282fcbc377Syt 
43292fcbc377Syt /*
43302fcbc377Syt  * Allocates memory for all Command Tables, which contains Command FIS,
43312fcbc377Syt  * ATAPI Command and Physical Region Descriptor Table.
43322fcbc377Syt  *
43332fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
43342fcbc377Syt  * is called.
43352fcbc377Syt  */
43362fcbc377Syt static int
43372fcbc377Syt ahci_alloc_cmd_tables(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
43382fcbc377Syt {
43392fcbc377Syt 	size_t ret_len;
43402fcbc377Syt 	ddi_dma_cookie_t cmd_table_dma_cookie;
43412fcbc377Syt 	uint_t cookie_count;
43422fcbc377Syt 	int slot;
43432fcbc377Syt 
43442fcbc377Syt 	AHCIDBG1(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
43452fcbc377Syt 	    "ahci_alloc_cmd_tables: port %d enter",
43462fcbc377Syt 	    ahci_portp->ahciport_port_num);
43472fcbc377Syt 
43482fcbc377Syt 	for (slot = 0; slot < ahci_ctlp->ahcictl_num_cmd_slots; slot++) {
43492fcbc377Syt 		/* Allocate cmd table dma handle. */
43502fcbc377Syt 		if (ddi_dma_alloc_handle(ahci_ctlp->ahcictl_dip,
43512fcbc377Syt 		    &ahci_ctlp->ahcictl_cmd_table_dma_attr,
43522fcbc377Syt 		    DDI_DMA_SLEEP,
43532fcbc377Syt 		    NULL,
43542fcbc377Syt 		    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]) !=
43552fcbc377Syt 		    DDI_SUCCESS) {
43562fcbc377Syt 
43572fcbc377Syt 			AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
43582fcbc377Syt 			    "cmd table dma handle alloc failed");
43592fcbc377Syt 
43602fcbc377Syt 			goto err_out;
43612fcbc377Syt 		}
43622fcbc377Syt 
43632fcbc377Syt 		if (ddi_dma_mem_alloc(
43642fcbc377Syt 		    ahci_portp->ahciport_cmd_tables_dma_handle[slot],
43652fcbc377Syt 		    ahci_cmd_table_size,
43662fcbc377Syt 		    &accattr,
43672fcbc377Syt 		    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
43682fcbc377Syt 		    DDI_DMA_SLEEP,
43692fcbc377Syt 		    NULL,
43702fcbc377Syt 		    (caddr_t *)&ahci_portp->ahciport_cmd_tables[slot],
43712fcbc377Syt 		    &ret_len,
43722fcbc377Syt 		    &ahci_portp->ahciport_cmd_tables_acc_handle[slot]) !=
43732fcbc377Syt 		    NULL) {
43742fcbc377Syt 
43752fcbc377Syt 			AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
43762fcbc377Syt 			    "cmd table dma mem alloc fail");
43772fcbc377Syt 
43782fcbc377Syt 			/* error.. free the dma handle. */
43792fcbc377Syt 			ddi_dma_free_handle(
43802fcbc377Syt 			    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
43812fcbc377Syt 			goto err_out;
43822fcbc377Syt 		}
43832fcbc377Syt 
43842fcbc377Syt 		if (ddi_dma_addr_bind_handle(
43852fcbc377Syt 		    ahci_portp->ahciport_cmd_tables_dma_handle[slot],
43862fcbc377Syt 		    NULL,
43872fcbc377Syt 		    (caddr_t)ahci_portp->ahciport_cmd_tables[slot],
43882fcbc377Syt 		    ahci_cmd_table_size,
43892fcbc377Syt 		    DDI_DMA_CONSISTENT,
43902fcbc377Syt 		    DDI_DMA_SLEEP,
43912fcbc377Syt 		    NULL,
43922fcbc377Syt 		    &cmd_table_dma_cookie,
43932fcbc377Syt 		    &cookie_count) !=  DDI_DMA_MAPPED) {
43942fcbc377Syt 
43952fcbc377Syt 			AHCIDBG0(AHCIDBG_INIT, ahci_ctlp,
43962fcbc377Syt 			    "cmd table dma handle bind fail");
43972fcbc377Syt 			/*  error.. free the dma handle & free the memory. */
43982fcbc377Syt 			ddi_dma_mem_free(
43992fcbc377Syt 			    &ahci_portp->ahciport_cmd_tables_acc_handle[slot]);
44002fcbc377Syt 			ddi_dma_free_handle(
44012fcbc377Syt 			    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
44022fcbc377Syt 			goto err_out;
44032fcbc377Syt 		}
44042fcbc377Syt 
44052fcbc377Syt 		bzero((void *)ahci_portp->ahciport_cmd_tables[slot],
44062fcbc377Syt 		    ahci_cmd_table_size);
44072fcbc377Syt 
44082fcbc377Syt 		/* Config Port Command Table Base Address */
44092fcbc377Syt 		SET_COMMAND_TABLE_BASE_ADDR(
44102fcbc377Syt 		    (&ahci_portp->ahciport_cmd_list[slot]),
44112fcbc377Syt 		    cmd_table_dma_cookie.dmac_laddress & 0xffffffffull);
44122fcbc377Syt 
44132fcbc377Syt #ifndef __lock_lint
44142fcbc377Syt 		SET_COMMAND_TABLE_BASE_ADDR_UPPER(
44152fcbc377Syt 		    (&ahci_portp->ahciport_cmd_list[slot]),
44162fcbc377Syt 		    cmd_table_dma_cookie.dmac_laddress >> 32);
4417689d74b0Syt #endif
44182fcbc377Syt 	}
44192fcbc377Syt 
44202fcbc377Syt 	return (AHCI_SUCCESS);
44212fcbc377Syt err_out:
44222fcbc377Syt 
44232fcbc377Syt 	for (slot--; slot >= 0; slot--) {
44242fcbc377Syt 		/* Unbind the cmd table dma handle first */
44252fcbc377Syt 		(void) ddi_dma_unbind_handle(
44262fcbc377Syt 		    ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
44272fcbc377Syt 
44282fcbc377Syt 		/* Then free the underlying memory */
44292fcbc377Syt 		ddi_dma_mem_free(
44302fcbc377Syt 		    &ahci_portp->ahciport_cmd_tables_acc_handle[slot]);
44312fcbc377Syt 
44322fcbc377Syt 		/* Now free the handle itself */
44332fcbc377Syt 		ddi_dma_free_handle(
44342fcbc377Syt 		    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
44352fcbc377Syt 	}
44362fcbc377Syt 
44372fcbc377Syt 	return (AHCI_FAILURE);
44382fcbc377Syt }
44392fcbc377Syt 
44402fcbc377Syt /*
44412fcbc377Syt  * Deallocates memory for all Command Tables.
44422fcbc377Syt  *
44432fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
44442fcbc377Syt  * is called.
44452fcbc377Syt  */
44462fcbc377Syt static void
44472fcbc377Syt ahci_dealloc_cmd_tables(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
44482fcbc377Syt {
44492fcbc377Syt 	int slot;
44502fcbc377Syt 
44512fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
44522fcbc377Syt 	    "ahci_dealloc_cmd_tables: %d enter",
44532fcbc377Syt 	    ahci_portp->ahciport_port_num);
44542fcbc377Syt 
44552fcbc377Syt 	for (slot = 0; slot < ahci_ctlp->ahcictl_num_cmd_slots; slot++) {
44562fcbc377Syt 		/* Unbind the cmd table dma handle first. */
44572fcbc377Syt 		(void) ddi_dma_unbind_handle(
44582fcbc377Syt 		    ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
44592fcbc377Syt 
44602fcbc377Syt 		/* Then free the underlying memory. */
44612fcbc377Syt 		ddi_dma_mem_free(
44622fcbc377Syt 		    &ahci_portp->ahciport_cmd_tables_acc_handle[slot]);
44632fcbc377Syt 
44642fcbc377Syt 		/* Now free the handle itself. */
44652fcbc377Syt 		ddi_dma_free_handle(
44662fcbc377Syt 		    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
44672fcbc377Syt 	}
44682fcbc377Syt }
44692fcbc377Syt 
44702fcbc377Syt /*
44712fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
44722fcbc377Syt  * is called.
44732fcbc377Syt  */
44742fcbc377Syt static void
44752fcbc377Syt ahci_update_sata_registers(ahci_ctl_t *ahci_ctlp, uint8_t port,
44762fcbc377Syt     sata_device_t *sd)
44772fcbc377Syt {
44782fcbc377Syt 	sd->satadev_scr.sstatus =
44792fcbc377Syt 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
44802fcbc377Syt 	    (uint32_t *)(AHCI_PORT_PxSSTS(ahci_ctlp, port)));
44812fcbc377Syt 	sd->satadev_scr.serror =
44822fcbc377Syt 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
44832fcbc377Syt 	    (uint32_t *)(AHCI_PORT_PxSERR(ahci_ctlp, port)));
44842fcbc377Syt 	sd->satadev_scr.scontrol =
44852fcbc377Syt 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
44862fcbc377Syt 	    (uint32_t *)(AHCI_PORT_PxSCTL(ahci_ctlp, port)));
44872fcbc377Syt 	sd->satadev_scr.sactive =
44882fcbc377Syt 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
44892fcbc377Syt 	    (uint32_t *)(AHCI_PORT_PxSACT(ahci_ctlp, port)));
44902fcbc377Syt }
44912fcbc377Syt 
44922fcbc377Syt /*
449368d33a25Syt  * For poll mode, ahci_port_intr will be called to emulate the interrupt
44942fcbc377Syt  */
44952fcbc377Syt static void
449682263d52Syt ahci_port_intr(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp, uint8_t port)
44972fcbc377Syt {
449868d33a25Syt 	uint32_t port_intr_status;
449968d33a25Syt 	uint32_t port_intr_enable;
450068d33a25Syt 
450168d33a25Syt 	AHCIDBG1(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
450268d33a25Syt 	    "ahci_port_intr enter: port %d", port);
450368d33a25Syt 
450468d33a25Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
450568d33a25Syt 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_POLLING) {
450668d33a25Syt 		/* For SATA_OPMODE_POLLING commands */
450768d33a25Syt 		port_intr_enable =
450868d33a25Syt 		    (AHCI_INTR_STATUS_DHRS |
450968d33a25Syt 		    AHCI_INTR_STATUS_PSS |
451068d33a25Syt 		    AHCI_INTR_STATUS_SDBS |
451168d33a25Syt 		    AHCI_INTR_STATUS_UFS |
451268d33a25Syt 		    AHCI_INTR_STATUS_PCS |
451368d33a25Syt 		    AHCI_INTR_STATUS_PRCS |
451468d33a25Syt 		    AHCI_INTR_STATUS_OFS |
451568d33a25Syt 		    AHCI_INTR_STATUS_INFS |
451668d33a25Syt 		    AHCI_INTR_STATUS_IFS |
451768d33a25Syt 		    AHCI_INTR_STATUS_HBDS |
451868d33a25Syt 		    AHCI_INTR_STATUS_HBFS |
451968d33a25Syt 		    AHCI_INTR_STATUS_TFES);
452068d33a25Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
452168d33a25Syt 		goto next;
452268d33a25Syt 	}
452368d33a25Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
452468d33a25Syt 
452568d33a25Syt 	/*
452668d33a25Syt 	 * port_intr_enable indicates that the corresponding interrrupt
452768d33a25Syt 	 * reporting is enabled.
452868d33a25Syt 	 */
452968d33a25Syt 	port_intr_enable = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
453068d33a25Syt 	    (uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port));
453168d33a25Syt next:
453268d33a25Syt 	/*
453368d33a25Syt 	 * port_intr_stats indicates that the corresponding interrupt
453468d33a25Syt 	 * condition is active.
453568d33a25Syt 	 */
453668d33a25Syt 	port_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
453768d33a25Syt 	    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port));
453868d33a25Syt 
453982263d52Syt 	AHCIDBG3(AHCIDBG_INTR, ahci_ctlp,
454068d33a25Syt 	    "ahci_port_intr: port %d, port_intr_status = 0x%x, "
454182263d52Syt 	    "port_intr_enable = 0x%x",
454282263d52Syt 	    port, port_intr_status, port_intr_enable);
454368d33a25Syt 
454468d33a25Syt 	port_intr_status &= port_intr_enable;
454568d33a25Syt 
454668d33a25Syt 	/* First clear the port interrupts status */
454768d33a25Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
454868d33a25Syt 	    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port),
454968d33a25Syt 	    port_intr_status);
455068d33a25Syt 
455182263d52Syt 	/* Check the completed non-queued commands */
455268d33a25Syt 	if (port_intr_status & (AHCI_INTR_STATUS_DHRS |
455368d33a25Syt 	    AHCI_INTR_STATUS_PSS)) {
455468d33a25Syt 		(void) ahci_intr_cmd_cmplt(ahci_ctlp,
4555689d74b0Syt 		    ahci_portp, port);
455668d33a25Syt 	}
455768d33a25Syt 
455882263d52Syt 	/* Check the completed queued commands */
455968d33a25Syt 	if (port_intr_status & AHCI_INTR_STATUS_SDBS) {
456068d33a25Syt 		(void) ahci_intr_set_device_bits(ahci_ctlp,
456168d33a25Syt 		    ahci_portp, port);
456268d33a25Syt 	}
456368d33a25Syt 
456468d33a25Syt 	/* Check the port connect change status interrupt bit */
456568d33a25Syt 	if (port_intr_status & AHCI_INTR_STATUS_PCS) {
456668d33a25Syt 		(void) ahci_intr_port_connect_change(ahci_ctlp,
456768d33a25Syt 		    ahci_portp, port);
456868d33a25Syt 	}
456968d33a25Syt 
457068d33a25Syt 	/* Check the device mechanical presence status interrupt bit */
457168d33a25Syt 	if (port_intr_status & AHCI_INTR_STATUS_DMPS) {
457268d33a25Syt 		(void) ahci_intr_device_mechanical_presence_status(
457368d33a25Syt 		    ahci_ctlp, ahci_portp, port);
457468d33a25Syt 	}
457568d33a25Syt 
457668d33a25Syt 	/* Check the PhyRdy change status interrupt bit */
457768d33a25Syt 	if (port_intr_status & AHCI_INTR_STATUS_PRCS) {
457868d33a25Syt 		(void) ahci_intr_phyrdy_change(ahci_ctlp, ahci_portp,
457968d33a25Syt 		    port);
45802fcbc377Syt 	}
458168d33a25Syt 
458268d33a25Syt 	/*
458368d33a25Syt 	 * Check the non-fatal error interrupt bits, there are three
458468d33a25Syt 	 * kinds of non-fatal errors at the time being:
458568d33a25Syt 	 *
458668d33a25Syt 	 *    PxIS.UFS - Unknown FIS Error
458768d33a25Syt 	 *    PxIS.OFS - Overflow Error
458868d33a25Syt 	 *    PxIS.INFS - Interface Non-Fatal Error
458968d33a25Syt 	 *
459068d33a25Syt 	 * For these non-fatal errors, the HBA can continue to operate,
459168d33a25Syt 	 * so the driver just log the error messages.
459268d33a25Syt 	 */
459368d33a25Syt 	if (port_intr_status & (AHCI_INTR_STATUS_UFS |
459468d33a25Syt 	    AHCI_INTR_STATUS_OFS |
459568d33a25Syt 	    AHCI_INTR_STATUS_INFS)) {
459668d33a25Syt 		(void) ahci_intr_non_fatal_error(ahci_ctlp, ahci_portp,
459768d33a25Syt 		    port, port_intr_status);
459868d33a25Syt 	}
459968d33a25Syt 
460068d33a25Syt 	/*
460168d33a25Syt 	 * Check the fatal error interrupt bits, there are four kinds
460268d33a25Syt 	 * of fatal errors for AHCI controllers:
460368d33a25Syt 	 *
460468d33a25Syt 	 *    PxIS.HBFS - Host Bus Fatal Error
460568d33a25Syt 	 *    PxIS.HBDS - Host Bus Data Error
460668d33a25Syt 	 *    PxIS.IFS - Interface Fatal Error
460768d33a25Syt 	 *    PxIS.TFES - Task File Error
460868d33a25Syt 	 *
460968d33a25Syt 	 * The fatal error means the HBA can not recover from it by
461068d33a25Syt 	 * itself, and it will try to abort the transfer, and the software
461168d33a25Syt 	 * must intervene to restart the port.
461268d33a25Syt 	 */
461368d33a25Syt 	if (port_intr_status & (AHCI_INTR_STATUS_IFS |
461468d33a25Syt 	    AHCI_INTR_STATUS_HBDS |
461568d33a25Syt 	    AHCI_INTR_STATUS_HBFS |
461668d33a25Syt 	    AHCI_INTR_STATUS_TFES))
461768d33a25Syt 		(void) ahci_intr_fatal_error(ahci_ctlp, ahci_portp,
461882263d52Syt 		    port, port_intr_status);
461968d33a25Syt 
462068d33a25Syt 	/* Check the cold port detect interrupt bit */
462168d33a25Syt 	if (port_intr_status & AHCI_INTR_STATUS_CPDS) {
462268d33a25Syt 		(void) ahci_intr_cold_port_detect(ahci_ctlp, ahci_portp, port);
462368d33a25Syt 	}
462468d33a25Syt 
462568d33a25Syt 	/* Second clear the corresponding bit in IS.IPS */
462668d33a25Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
462768d33a25Syt 	    (uint32_t *)AHCI_GLOBAL_IS(ahci_ctlp), (0x1 << port));
46282fcbc377Syt }
46292fcbc377Syt 
46302fcbc377Syt /*
46312fcbc377Syt  * Interrupt service handler
46322fcbc377Syt  */
46332fcbc377Syt static uint_t
46342fcbc377Syt ahci_intr(caddr_t arg1, caddr_t arg2)
46352fcbc377Syt {
4636689d74b0Syt #ifndef __lock_lint
4637689d74b0Syt 	_NOTE(ARGUNUSED(arg2))
4638689d74b0Syt #endif
4639689d74b0Syt 	/* LINTED */
46402fcbc377Syt 	ahci_ctl_t *ahci_ctlp = (ahci_ctl_t *)arg1;
46412fcbc377Syt 	ahci_port_t *ahci_portp;
464268d33a25Syt 	int32_t global_intr_status;
46432fcbc377Syt 	uint8_t port;
46442fcbc377Syt 
46452fcbc377Syt 	/*
46462fcbc377Syt 	 * global_intr_status indicates that the corresponding port has
46472fcbc377Syt 	 * an interrupt pending.
46482fcbc377Syt 	 */
46492fcbc377Syt 	global_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
46502fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_IS(ahci_ctlp));
46512fcbc377Syt 
46522fcbc377Syt 	if (!(global_intr_status & ahci_ctlp->ahcictl_ports_implemented)) {
46532fcbc377Syt 		/* The interrupt is not ours */
46542fcbc377Syt 		return (DDI_INTR_UNCLAIMED);
46552fcbc377Syt 	}
46562fcbc377Syt 
46572fcbc377Syt 	/* Loop for all the ports */
46582fcbc377Syt 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
46592fcbc377Syt 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
46602fcbc377Syt 			continue;
46612fcbc377Syt 		}
46622fcbc377Syt 		if (!((0x1 << port) & global_intr_status)) {
46632fcbc377Syt 			continue;
46642fcbc377Syt 		}
46652fcbc377Syt 
46662fcbc377Syt 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
46672fcbc377Syt 
466868d33a25Syt 		/* Call ahci_port_intr */
466982263d52Syt 		ahci_port_intr(ahci_ctlp, ahci_portp, port);
46702fcbc377Syt 	}
46712fcbc377Syt 
46722fcbc377Syt 	return (DDI_INTR_CLAIMED);
46732fcbc377Syt }
46742fcbc377Syt 
46752fcbc377Syt /*
467668d33a25Syt  * For non-queued commands, when the corresponding bit in the PxCI register
467768d33a25Syt  * is cleared, it means the command is completed successfully. And according
467868d33a25Syt  * to the HBA state machine, there are three conditions which possibly will
467968d33a25Syt  * try to clear the PxCI register bit.
468068d33a25Syt  *	1. Receive one D2H Register FIS which is with 'I' bit set
468168d33a25Syt  *	2. Update PIO Setup FIS
468268d33a25Syt  *	3. Transmit a command and receive R_OK if CTBA.C is set (software reset)
468368d33a25Syt  *
468482263d52Syt  * Process completed non-queued commands when the interrupt status bit -
468582263d52Syt  * AHCI_INTR_STATUS_DHRS or AHCI_INTR_STATUS_PSS is set.
46862fcbc377Syt  *
468768d33a25Syt  * AHCI_INTR_STATUS_DHRS means a D2H Register FIS has been received
468868d33a25Syt  * with the 'I' bit set. And the following commands will send thus
468968d33a25Syt  * FIS with 'I' bit set upon the successful completion:
46902fcbc377Syt  * 	1. Non-data commands
46912fcbc377Syt  * 	2. DMA data-in command
46922fcbc377Syt  * 	3. DMA data-out command
46932fcbc377Syt  * 	4. PIO data-out command
469468d33a25Syt  *	5. PACKET non-data commands
469568d33a25Syt  *	6. PACKET PIO data-in command
469668d33a25Syt  *	7. PACKET PIO data-out command
469768d33a25Syt  *	8. PACKET DMA data-in command
469868d33a25Syt  *	9. PACKET DMA data-out command
469968d33a25Syt  *
470068d33a25Syt  * AHCI_INTR_STATUS_PSS means a PIO Setup FIS has been received
470168d33a25Syt  * with the 'I' bit set. And the following commands will send this
470268d33a25Syt  * FIS upon the successful completion:
470368d33a25Syt  * 	1. PIO data-in command
47042fcbc377Syt  */
47052fcbc377Syt static int
470682263d52Syt ahci_intr_cmd_cmplt(ahci_ctl_t *ahci_ctlp,
4707689d74b0Syt     ahci_port_t *ahci_portp, uint8_t port)
47082fcbc377Syt {
470968d33a25Syt 	uint32_t port_cmd_issue = 0;
47102fcbc377Syt 	uint32_t finished_tags;
471168d33a25Syt 	int finished_slot;
47122fcbc377Syt 	sata_pkt_t *satapkt;
47132fcbc377Syt 	ahci_fis_d2h_register_t *rcvd_fisp;
471438547057Sying tian - Beijing China #if AHCI_DEBUG
471538547057Sying tian - Beijing China 	ahci_cmd_header_t *cmd_header;
471638547057Sying tian - Beijing China 	uint32_t cmd_dmacount;
471738547057Sying tian - Beijing China #endif
47182fcbc377Syt 
471968d33a25Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
472082263d52Syt 
472182263d52Syt 	if (!ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
472282263d52Syt 	    !NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
47232fcbc377Syt 		/*
47242fcbc377Syt 		 * Spurious interrupt. Nothing to be done.
47252fcbc377Syt 		 */
47262fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
47272fcbc377Syt 		return (AHCI_SUCCESS);
47282fcbc377Syt 	}
47292fcbc377Syt 
473068d33a25Syt 	port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
473168d33a25Syt 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
47322fcbc377Syt 
473382263d52Syt 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
473482263d52Syt 		/* Slot 0 is always used during error recovery */
473582263d52Syt 		finished_tags = 0x1 & ~port_cmd_issue;
473682263d52Syt 		AHCIDBG2(AHCIDBG_ERRS, ahci_ctlp,
473782263d52Syt 		    "ahci_intr_cmd_cmplt: port %d the sata pkt for error "
473882263d52Syt 		    "retrieval is finished, and finished_tags = 0x%x",
473982263d52Syt 		    port, finished_tags);
474068d33a25Syt 	} else {
474168d33a25Syt 		finished_tags = ahci_portp->ahciport_pending_tags &
474268d33a25Syt 		    ~port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp);
47432fcbc377Syt 	}
47442fcbc377Syt 
474568d33a25Syt 	AHCIDBG3(AHCIDBG_INTR, ahci_ctlp,
474682263d52Syt 	    "ahci_intr_cmd_cmplt: pending_tags = 0x%x, "
474782263d52Syt 	    "port_cmd_issue = 0x%x finished_tags = 0x%x",
474868d33a25Syt 	    ahci_portp->ahciport_pending_tags, port_cmd_issue,
474982263d52Syt 	    finished_tags);
47502fcbc377Syt 
475182263d52Syt 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
475282263d52Syt 	    (finished_tags == 0x1)) {
475382263d52Syt 		satapkt = ahci_portp->ahciport_err_retri_pkt;
475482263d52Syt 		ASSERT(satapkt != NULL);
475582263d52Syt 
475682263d52Syt 		AHCIDBG1(AHCIDBG_INTR, ahci_ctlp,
475782263d52Syt 		    "ahci_intr_cmd_cmplt: sending up pkt 0x%p "
475882263d52Syt 		    "with SATA_PKT_COMPLETED", (void *)satapkt);
475982263d52Syt 
476082263d52Syt 		SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_COMPLETED);
476182263d52Syt 		goto out;
476282263d52Syt 	}
47632fcbc377Syt 
476468d33a25Syt 	while (finished_tags) {
476568d33a25Syt 		finished_slot = ddi_ffs(finished_tags) - 1;
476668d33a25Syt 		if (finished_slot == -1) {
476768d33a25Syt 			goto out;
476868d33a25Syt 		}
47692fcbc377Syt 
477068d33a25Syt 		satapkt = ahci_portp->ahciport_slot_pkts[finished_slot];
477168d33a25Syt 		ASSERT(satapkt != NULL);
477238547057Sying tian - Beijing China #if AHCI_DEBUG
477338547057Sying tian - Beijing China 		/*
477438547057Sying tian - Beijing China 		 * For non-native queued commands, the PRD byte count field
477538547057Sying tian - Beijing China 		 * shall contain an accurate count of the number of bytes
477638547057Sying tian - Beijing China 		 * transferred for the command before the PxCI bit is cleared
477738547057Sying tian - Beijing China 		 * to '0' for the command.
477838547057Sying tian - Beijing China 		 *
477938547057Sying tian - Beijing China 		 * The purpose of this field is to let software know how many
478038547057Sying tian - Beijing China 		 * bytes transferred for a given operation in order to
478138547057Sying tian - Beijing China 		 * determine if underflow occurred. When issuing native command
478238547057Sying tian - Beijing China 		 * queuing commands, this field should not be used and is not
478338547057Sying tian - Beijing China 		 * required to be valid since in this case underflow is always
478438547057Sying tian - Beijing China 		 * illegal.
478538547057Sying tian - Beijing China 		 *
478638547057Sying tian - Beijing China 		 * For data reads, the HBA will update its PRD byte count with
478738547057Sying tian - Beijing China 		 * the total number of bytes received from the last FIS, and
478838547057Sying tian - Beijing China 		 * may be able to continue normally. For data writes, the
478938547057Sying tian - Beijing China 		 * device will detect an error, and HBA most likely will get
479038547057Sying tian - Beijing China 		 * a fatal error.
479138547057Sying tian - Beijing China 		 *
479238547057Sying tian - Beijing China 		 * Therefore, here just put code to debug part. And please
479338547057Sying tian - Beijing China 		 * refer to the comment above ahci_intr_fatal_error for the
479438547057Sying tian - Beijing China 		 * definition of underflow error.
479538547057Sying tian - Beijing China 		 */
479638547057Sying tian - Beijing China 		cmd_dmacount =
479738547057Sying tian - Beijing China 		    ahci_portp->ahciport_prd_bytecounts[finished_slot];
479838547057Sying tian - Beijing China 		if (cmd_dmacount) {
479938547057Sying tian - Beijing China 			cmd_header =
480038547057Sying tian - Beijing China 			    &ahci_portp->ahciport_cmd_list[finished_slot];
480138547057Sying tian - Beijing China 			AHCIDBG3(AHCIDBG_INTR|AHCIDBG_PRDT, ahci_ctlp,
480238547057Sying tian - Beijing China 			    "ahci_intr_cmd_cmplt: port %d, "
480338547057Sying tian - Beijing China 			    "PRD Byte Count = 0x%x, "
480438547057Sying tian - Beijing China 			    "ahciport_prd_bytecounts = 0x%x", port,
480538547057Sying tian - Beijing China 			    cmd_header->ahcich_prd_byte_count,
480638547057Sying tian - Beijing China 			    cmd_dmacount);
480738547057Sying tian - Beijing China 
480838547057Sying tian - Beijing China 			if (cmd_header->ahcich_prd_byte_count != cmd_dmacount) {
480938547057Sying tian - Beijing China 				AHCIDBG1(AHCIDBG_UNDERFLOW, ahci_ctlp,
481038547057Sying tian - Beijing China 				    "ahci_intr_cmd_cmplt: port %d, "
481138547057Sying tian - Beijing China 				    "an underflow occurred", port);
481238547057Sying tian - Beijing China 			}
481338547057Sying tian - Beijing China 		}
481438547057Sying tian - Beijing China #endif
48152fcbc377Syt 
48162fcbc377Syt 		/*
481768d33a25Syt 		 * For SATAC_SMART command with SATA_SMART_RETURN_STATUS
481868d33a25Syt 		 * feature, sata_special_regs flag will be set, and the
481968d33a25Syt 		 * driver should copy the status and the other corresponding
482068d33a25Syt 		 * register values in the D2H Register FIS received (It's
482168d33a25Syt 		 * working on Non-data protocol) from the device back to
482268d33a25Syt 		 * the sata_cmd.
482368d33a25Syt 		 *
482468d33a25Syt 		 * For every AHCI port, there is only one Received FIS
482568d33a25Syt 		 * structure, which contains the FISes received from the
482668d33a25Syt 		 * device, So we're trying to copy the content of D2H
482768d33a25Syt 		 * Register FIS in the Received FIS structure back to
482868d33a25Syt 		 * the sata_cmd.
48292fcbc377Syt 		 */
483068d33a25Syt 		if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs) {
483168d33a25Syt 			rcvd_fisp = &(ahci_portp->ahciport_rcvd_fis->
483268d33a25Syt 			    ahcirf_d2h_register_fis);
483368d33a25Syt 			satapkt->satapkt_cmd.satacmd_status_reg =
483468d33a25Syt 			    GET_RFIS_STATUS(rcvd_fisp);
483568d33a25Syt 			ahci_copy_out_regs(&satapkt->satapkt_cmd, rcvd_fisp);
483668d33a25Syt 		}
48372fcbc377Syt 
483868d33a25Syt 		AHCIDBG1(AHCIDBG_INTR, ahci_ctlp,
483968d33a25Syt 		    "ahci_intr_cmd_cmplt: sending up pkt 0x%p "
484068d33a25Syt 		    "with SATA_PKT_COMPLETED", (void *)satapkt);
48412fcbc377Syt 
484268d33a25Syt 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, finished_slot);
484368d33a25Syt 		CLEAR_BIT(finished_tags, finished_slot);
484468d33a25Syt 		ahci_portp->ahciport_slot_pkts[finished_slot] = NULL;
48452fcbc377Syt 
484668d33a25Syt 		SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_COMPLETED);
48472fcbc377Syt 	}
48482fcbc377Syt out:
48492fcbc377Syt 	AHCIDBG1(AHCIDBG_PKTCOMP, ahci_ctlp,
485068d33a25Syt 	    "ahci_intr_cmd_cmplt: pending_tags = 0x%x",
48512fcbc377Syt 	    ahci_portp->ahciport_pending_tags);
48522fcbc377Syt 
48532fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
48542fcbc377Syt 
48552fcbc377Syt 	return (AHCI_SUCCESS);
48562fcbc377Syt }
48572fcbc377Syt 
48582fcbc377Syt /*
485968d33a25Syt  * AHCI_INTR_STATUS_SDBS means a Set Device Bits FIS has been received
486082263d52Syt  * with the 'I' bit set and has been copied into system memory. It will
486182263d52Syt  * be sent under the following situations:
486282263d52Syt  *
486382263d52Syt  * 	1. NCQ command is completed
486482263d52Syt  * 	2. Asynchronous notification
486582263d52Syt  *
486682263d52Syt  * The completion of NCQ commands (READ/WRITE FPDMA QUEUED) is performed
486782263d52Syt  * via the Set Device Bits FIS. When such event is generated, the software
486882263d52Syt  * needs to read PxSACT register and compares the current value to the
486982263d52Syt  * list of commands previously issue by software. ahciport_pending_ncq_tags
487082263d52Syt  * keeps the tags of previously issued commands.
48712fcbc377Syt  *
487268d33a25Syt  * Asynchronous Notification is a feature in SATA II, which allows an
487368d33a25Syt  * ATAPI device to send a signal to the host when media is inserted or
487468d33a25Syt  * removed and avoids polling the device for media changes. The signal
487568d33a25Syt  * sent to the host is a Set Device Bits FIS with the 'I' and 'N' bits
487682263d52Syt  * set to '1'. At the moment, it's not supported yet.
48772fcbc377Syt  */
48782fcbc377Syt static int
487968d33a25Syt ahci_intr_set_device_bits(ahci_ctl_t *ahci_ctlp,
48802fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
48812fcbc377Syt {
488282263d52Syt 	uint32_t port_sactive;
488382263d52Syt 	uint32_t port_cmd_issue;
488482263d52Syt 	uint32_t issued_tags;
488582263d52Syt 	int issued_slot;
488682263d52Syt 	uint32_t finished_tags;
488782263d52Syt 	int finished_slot;
488882263d52Syt 	sata_pkt_t *satapkt;
48892fcbc377Syt 
489082263d52Syt 	AHCIDBG1(AHCIDBG_ENTRY|AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
489168d33a25Syt 	    "ahci_intr_set_device_bits enter: port %d", port);
48922fcbc377Syt 
489382263d52Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
489482263d52Syt 	if (!NCQ_CMD_IN_PROGRESS(ahci_portp)) {
489582263d52Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
489682263d52Syt 		return (AHCI_SUCCESS);
489782263d52Syt 	}
489882263d52Syt 
489982263d52Syt 	/*
490082263d52Syt 	 * First the handler got which commands are finished by checking
490182263d52Syt 	 * PxSACT register
490282263d52Syt 	 */
490382263d52Syt 	port_sactive = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
490482263d52Syt 	    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
490568d33a25Syt 
490682263d52Syt 	finished_tags = ahci_portp->ahciport_pending_ncq_tags &
490782263d52Syt 	    ~port_sactive & AHCI_NCQ_SLOT_MASK(ahci_portp);
490882263d52Syt 
490982263d52Syt 	AHCIDBG3(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
491082263d52Syt 	    "ahci_intr_set_device_bits: port %d pending_ncq_tags = 0x%x "
491182263d52Syt 	    "port_sactive = 0x%x", port,
491282263d52Syt 	    ahci_portp->ahciport_pending_ncq_tags, port_sactive);
491382263d52Syt 
491482263d52Syt 	AHCIDBG1(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
491582263d52Syt 	    "ahci_intr_set_device_bits: finished_tags = 0x%x", finished_tags);
491682263d52Syt 
491782263d52Syt 	/*
491882263d52Syt 	 * For NCQ commands, the software can determine which command has
491982263d52Syt 	 * already been transmitted to the device by checking PxCI register.
492082263d52Syt 	 */
492182263d52Syt 	port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
492282263d52Syt 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
492382263d52Syt 
492482263d52Syt 	issued_tags = ahci_portp->ahciport_pending_tags &
492582263d52Syt 	    ~port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp);
492682263d52Syt 
492782263d52Syt 	AHCIDBG3(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
492882263d52Syt 	    "ahci_intr_set_device_bits: port %d pending_tags = 0x%x "
492982263d52Syt 	    "port_cmd_issue = 0x%x", port,
493082263d52Syt 	    ahci_portp->ahciport_pending_tags, port_cmd_issue);
493182263d52Syt 
493282263d52Syt 	AHCIDBG1(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
493382263d52Syt 	    "ahci_intr_set_device_bits: issued_tags = 0x%x", issued_tags);
493482263d52Syt 
493582263d52Syt 	/*
493682263d52Syt 	 * Clear ahciport_pending_tags bit when the corresponding command
493782263d52Syt 	 * is already sent down to the device.
493882263d52Syt 	 */
493982263d52Syt 	while (issued_tags) {
494082263d52Syt 		issued_slot = ddi_ffs(issued_tags) - 1;
494182263d52Syt 		if (issued_slot == -1) {
494282263d52Syt 			goto next;
494382263d52Syt 		}
494482263d52Syt 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, issued_slot);
494582263d52Syt 		CLEAR_BIT(issued_tags, issued_slot);
494682263d52Syt 	}
494782263d52Syt 
494882263d52Syt next:
494982263d52Syt 	while (finished_tags) {
495082263d52Syt 		finished_slot = ddi_ffs(finished_tags) - 1;
495182263d52Syt 		if (finished_slot == -1) {
495282263d52Syt 			goto out;
495382263d52Syt 		}
495482263d52Syt 
495582263d52Syt 		/* The command is certainly transmitted to the device */
495682263d52Syt 		ASSERT(!(ahci_portp->ahciport_pending_tags &
495782263d52Syt 		    (0x1 << finished_slot)));
495882263d52Syt 
495982263d52Syt 		satapkt = ahci_portp->ahciport_slot_pkts[finished_slot];
496082263d52Syt 		ASSERT(satapkt != NULL);
496182263d52Syt 
496282263d52Syt 		AHCIDBG1(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
496382263d52Syt 		    "ahci_intr_set_device_bits: sending up pkt 0x%p "
496482263d52Syt 		    "with SATA_PKT_COMPLETED", (void *)satapkt);
496582263d52Syt 
496682263d52Syt 		CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags, finished_slot);
496782263d52Syt 		CLEAR_BIT(finished_tags, finished_slot);
496882263d52Syt 		ahci_portp->ahciport_slot_pkts[finished_slot] = NULL;
496982263d52Syt 
497082263d52Syt 		SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_COMPLETED);
497168d33a25Syt 	}
497282263d52Syt out:
497382263d52Syt 	AHCIDBG3(AHCIDBG_PKTCOMP|AHCIDBG_NCQ, ahci_ctlp,
497482263d52Syt 	    "ahci_intr_set_device_bits: port %d "
497582263d52Syt 	    "pending_ncq_tags = 0x%x pending_tags = 0x%x",
497682263d52Syt 	    port, ahci_portp->ahciport_pending_ncq_tags,
497782263d52Syt 	    ahci_portp->ahciport_pending_tags);
497882263d52Syt 
497982263d52Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
49802fcbc377Syt 
49812fcbc377Syt 	return (AHCI_SUCCESS);
49822fcbc377Syt }
49832fcbc377Syt 
49842fcbc377Syt /*
498568d33a25Syt  * 1=Change in Current Connect Status. 0=No change in Current Connect Status.
498668d33a25Syt  * This bit reflects the state of PxSERR.DIAG.X. This bit is only cleared
498768d33a25Syt  * when PxSERR.DIAG.X is cleared. When PxSERR.DIAG.X is set to one, it
498868d33a25Syt  * indicates a COMINIT signal was received.
49892fcbc377Syt  *
499068d33a25Syt  * Hot plug insertion is detected by reception of a COMINIT signal from the
499168d33a25Syt  * device. On reception of unsolicited COMINIT, the HBA shall generate a
499268d33a25Syt  * COMRESET. If the COMINIT is in responce to a COMRESET, then the HBA shall
499368d33a25Syt  * begin the normal communication negotiation sequence as outlined in the
499468d33a25Syt  * Serial ATA 1.0a specification. When a COMRESET is sent to the device the
499568d33a25Syt  * PxSSTS.DET field shall be cleared to 0h. When a COMINIT is received, the
499668d33a25Syt  * PxSSTS.DET field shall be set to 1h. When the communication negotiation
499768d33a25Syt  * sequence is complete and PhyRdy is true the PxSSTS.DET field	shall be set
499868d33a25Syt  * to 3h. Therefore, at the moment the ahci driver is going to check PhyRdy
499968d33a25Syt  * to handle hot plug insertion. In this interrupt handler, just do nothing
500068d33a25Syt  * but print some log message and clear the bit.
50012fcbc377Syt  */
50022fcbc377Syt static int
500368d33a25Syt ahci_intr_port_connect_change(ahci_ctl_t *ahci_ctlp,
50042fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
50052fcbc377Syt {
5006689d74b0Syt #if AHCI_DEBUG
50072fcbc377Syt 	uint32_t port_serror;
5008689d74b0Syt #endif
50092fcbc377Syt 
50102fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
50112fcbc377Syt 
5012689d74b0Syt #if AHCI_DEBUG
50132fcbc377Syt 	port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
50142fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
50152fcbc377Syt 
50162fcbc377Syt 	AHCIDBG2(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
501768d33a25Syt 	    "ahci_intr_port_connect_change: port %d, "
501868d33a25Syt 	    "port_serror = 0x%x", port, port_serror);
5019689d74b0Syt #endif
50202fcbc377Syt 
502168d33a25Syt 	/* Clear PxSERR.DIAG.X to clear the interrupt bit */
50222fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
50232fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
502482263d52Syt 	    SERROR_EXCHANGED_ERR);
50252fcbc377Syt 
50262fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
50272fcbc377Syt 
50282fcbc377Syt 	return (AHCI_SUCCESS);
50292fcbc377Syt }
50302fcbc377Syt 
50312fcbc377Syt /*
50322fcbc377Syt  * Hot Plug Operation for platforms that support Mechanical Presence
50332fcbc377Syt  * Switches.
50342fcbc377Syt  *
50352fcbc377Syt  * When set, it indicates that a mechanical presence switch attached to this
50362fcbc377Syt  * port has been opened or closed, which may lead to a change in the connection
50372fcbc377Syt  * state of the device. This bit is only valid if both CAP.SMPS and PxCMD.MPSP
50382fcbc377Syt  * are set to '1'.
50392fcbc377Syt  *
504068d33a25Syt  * At the moment, this interrupt is not needed and disabled and we just log
50412fcbc377Syt  * the debug message.
50422fcbc377Syt  */
50432fcbc377Syt static int
50442fcbc377Syt ahci_intr_device_mechanical_presence_status(ahci_ctl_t *ahci_ctlp,
50452fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
50462fcbc377Syt {
50472fcbc377Syt 	uint32_t cap_status, port_cmd_status;
50482fcbc377Syt 
50492fcbc377Syt 	AHCIDBG1(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
50502fcbc377Syt 	    "ahci_intr_device_mechanical_presence_status enter, "
50512fcbc377Syt 	    "port %d", port);
50522fcbc377Syt 
50532fcbc377Syt 	cap_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
50542fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_CAP(ahci_ctlp));
50552fcbc377Syt 
50562fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
50572fcbc377Syt 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
50582fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
50592fcbc377Syt 
50602fcbc377Syt 	if (!(cap_status & AHCI_HBA_CAP_SMPS) ||
50612fcbc377Syt 	    !(port_cmd_status & AHCI_CMD_STATUS_MPSP)) {
50622fcbc377Syt 		AHCIDBG2(AHCIDBG_INTR, ahci_ctlp,
50632fcbc377Syt 		    "CAP.SMPS or PxCMD.MPSP is not set, so just ignore "
50642fcbc377Syt 		    "the interrupt: cap_status = 0x%x, "
50652fcbc377Syt 		    "port_cmd_status = 0x%x", cap_status, port_cmd_status);
50662fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
50672fcbc377Syt 
50682fcbc377Syt 		return (AHCI_SUCCESS);
50692fcbc377Syt 	}
50702fcbc377Syt 
5071689d74b0Syt #if AHCI_DEBUG
50722fcbc377Syt 	if (port_cmd_status & AHCI_CMD_STATUS_MPSS) {
50732fcbc377Syt 		AHCIDBG2(AHCIDBG_INTR, ahci_ctlp,
50742fcbc377Syt 		    "The mechanical presence switch is open: "
50752fcbc377Syt 		    "port %d, port_cmd_status = 0x%x",
50762fcbc377Syt 		    port, port_cmd_status);
50772fcbc377Syt 	} else {
50782fcbc377Syt 		AHCIDBG2(AHCIDBG_INTR, ahci_ctlp,
50792fcbc377Syt 		    "The mechanical presence switch is close: "
50802fcbc377Syt 		    "port %d, port_cmd_status = 0x%x",
50812fcbc377Syt 		    port, port_cmd_status);
50822fcbc377Syt 	}
5083689d74b0Syt #endif
50842fcbc377Syt 
50852fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
50862fcbc377Syt 
50872fcbc377Syt 	return (AHCI_SUCCESS);
50882fcbc377Syt }
50892fcbc377Syt 
50902fcbc377Syt /*
50912fcbc377Syt  * Native Hot Plug Support.
50922fcbc377Syt  *
50932fcbc377Syt  * When set, it indicates that the internal PHYRDY signal changed state.
50942fcbc377Syt  * This bit reflects the state of PxSERR.DIAG.N.
509568d33a25Syt  *
509668d33a25Syt  * There are three kinds of conditions to generate this interrupt event:
509768d33a25Syt  * 1. a device is inserted
509868d33a25Syt  * 2. a device is disconnected
509968d33a25Syt  * 3. when the link enters/exits a Partial or Slumber interface power
510068d33a25Syt  *    management state
510168d33a25Syt  *
510268d33a25Syt  * If inteface power management is enabled for a port, the PxSERR.DIAG.N
510368d33a25Syt  * bit may be set due to the link entering the Partial or Slumber power
510468d33a25Syt  * management state, rather than due to a hot plug insertion or removal
510513bcbb7aSyt  * event. So far, the interface power management is disabled, so the
510613bcbb7aSyt  * driver can reliably get removal detection notification via the
510713bcbb7aSyt  * PxSERR.DIAG.N bit.
51082fcbc377Syt  */
51092fcbc377Syt static int
51102fcbc377Syt ahci_intr_phyrdy_change(ahci_ctl_t *ahci_ctlp,
51112fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
51122fcbc377Syt {
51132fcbc377Syt 	uint32_t port_sstatus = 0; /* No dev present & PHY not established. */
51142fcbc377Syt 	sata_device_t sdevice;
51152fcbc377Syt 	int dev_exists_now = 0;
51162fcbc377Syt 	int dev_existed_previously = 0;
51172fcbc377Syt 
51182fcbc377Syt 	AHCIDBG1(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
51192fcbc377Syt 	    "ahci_intr_phyrdy_change enter, port %d", port);
51202fcbc377Syt 
51212fcbc377Syt 	/* Clear PxSERR.DIAG.N to clear the interrupt bit */
51222fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
51232fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
51242fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
512582263d52Syt 	    SERROR_PHY_RDY_CHG);
51262fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
51272fcbc377Syt 
51282fcbc377Syt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
51292fcbc377Syt 	if ((ahci_ctlp->ahcictl_sata_hba_tran == NULL) ||
51302fcbc377Syt 	    (ahci_portp == NULL)) {
51312fcbc377Syt 		/* The whole controller setup is not yet done. */
51322fcbc377Syt 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
51332fcbc377Syt 		return (AHCI_SUCCESS);
51342fcbc377Syt 	}
51352fcbc377Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
51362fcbc377Syt 
51372fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
51382fcbc377Syt 
51392fcbc377Syt 	/* SStatus tells the presence of device. */
51402fcbc377Syt 	port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
51412fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
51422fcbc377Syt 
514382263d52Syt 	if (SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_PHYCOM) {
514468d33a25Syt 		dev_exists_now = 1;
514568d33a25Syt 	}
51462fcbc377Syt 
51472fcbc377Syt 	if (ahci_portp->ahciport_device_type != SATA_DTYPE_NONE) {
51482fcbc377Syt 		dev_existed_previously = 1;
51492fcbc377Syt 	}
51502fcbc377Syt 
515182263d52Syt 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_NODEV) {
515282263d52Syt 		ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_NODEV;
515382263d52Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
515482263d52Syt 		    "ahci_intr_phyrdy_change: port %d "
515582263d52Syt 		    "AHCI_PORT_FLAG_NODEV is cleared", port);
515682263d52Syt 		if (dev_exists_now == 0)
515782263d52Syt 			dev_existed_previously = 1;
515882263d52Syt 	}
515982263d52Syt 
51602fcbc377Syt 	bzero((void *)&sdevice, sizeof (sata_device_t));
516109121340Syt 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
51622fcbc377Syt 	sdevice.satadev_addr.qual = SATA_ADDR_CPORT;
51632fcbc377Syt 	sdevice.satadev_addr.pmport = 0;
51642fcbc377Syt 	sdevice.satadev_state = SATA_PSTATE_PWRON;
51652fcbc377Syt 	ahci_portp->ahciport_port_state = SATA_PSTATE_PWRON;
51662fcbc377Syt 
51672fcbc377Syt 	if (dev_exists_now) {
51682fcbc377Syt 		if (dev_existed_previously) {
51692fcbc377Syt 			/* Things are fine now. The loss was temporary. */
517068d33a25Syt 			AHCIDBG1(AHCIDBG_EVENT, ahci_ctlp,
517168d33a25Syt 			    "ahci_intr_phyrdy_change  port %d "
51722fcbc377Syt 			    "device link lost/established", port);
51732fcbc377Syt 
51742fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
51752fcbc377Syt 			sata_hba_event_notify(
51762fcbc377Syt 			    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
51772fcbc377Syt 			    &sdevice,
51782fcbc377Syt 			    SATA_EVNT_LINK_LOST|SATA_EVNT_LINK_ESTABLISHED);
51792fcbc377Syt 			mutex_enter(&ahci_portp->ahciport_mutex);
51802fcbc377Syt 
51812fcbc377Syt 		} else {
518268d33a25Syt 			AHCIDBG1(AHCIDBG_EVENT, ahci_ctlp,
518368d33a25Syt 			    "ahci_intr_phyrdy_change: port %d "
51842fcbc377Syt 			    "device link established", port);
51852fcbc377Syt 
51862fcbc377Syt 			/* A new device has been detected. */
5187*0a4c4cecSXiao-Yu Zhang 			(void) ahci_port_reset(ahci_ctlp, ahci_portp, port);
518868d33a25Syt 			ahci_find_dev_signature(ahci_ctlp, ahci_portp, port);
51892fcbc377Syt 
519068d33a25Syt 			/* Try to start the port */
519168d33a25Syt 			if (ahci_start_port(ahci_ctlp, ahci_portp, port)
519268d33a25Syt 			    != AHCI_SUCCESS) {
519368d33a25Syt 				sdevice.satadev_state |= SATA_PSTATE_FAILED;
51942fcbc377Syt 				AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
519568d33a25Syt 				    "ahci_intr_phyrdy_change: port %d failed "
51962fcbc377Syt 				    "at start port", port);
51972fcbc377Syt 			}
51982fcbc377Syt 
519982263d52Syt 			/* Clear the max queue depth for inserted device */
520082263d52Syt 			ahci_portp->ahciport_max_ncq_tags = 0;
520182263d52Syt 
52022fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
52032fcbc377Syt 			sata_hba_event_notify(
52042fcbc377Syt 			    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
52052fcbc377Syt 			    &sdevice,
52062fcbc377Syt 			    SATA_EVNT_LINK_ESTABLISHED);
52072fcbc377Syt 			mutex_enter(&ahci_portp->ahciport_mutex);
52082fcbc377Syt 
52092fcbc377Syt 		}
52102fcbc377Syt 	} else { /* No device exists now */
52112fcbc377Syt 
52122fcbc377Syt 		if (dev_existed_previously) {
521368d33a25Syt 			AHCIDBG1(AHCIDBG_EVENT, ahci_ctlp,
521468d33a25Syt 			    "ahci_intr_phyrdy_change: port %d "
52152fcbc377Syt 			    "device link lost", port);
52162fcbc377Syt 
52172fcbc377Syt 			ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
521868d33a25Syt 			(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
52192fcbc377Syt 			    ahci_portp, port);
52202fcbc377Syt 
52212fcbc377Syt 			/* An existing device is lost. */
52222fcbc377Syt 			ahci_portp->ahciport_device_type = SATA_DTYPE_NONE;
52232fcbc377Syt 
52242fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
52252fcbc377Syt 			sata_hba_event_notify(
52262fcbc377Syt 			    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
52272fcbc377Syt 			    &sdevice,
52282fcbc377Syt 			    SATA_EVNT_LINK_LOST);
52292fcbc377Syt 			mutex_enter(&ahci_portp->ahciport_mutex);
52302fcbc377Syt 		}
52312fcbc377Syt 	}
52322fcbc377Syt 
52332fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
52342fcbc377Syt 
52352fcbc377Syt 	return (AHCI_SUCCESS);
52362fcbc377Syt }
52372fcbc377Syt 
52382fcbc377Syt /*
523968d33a25Syt  * PxIS.UFS - Unknown FIS Error
52402fcbc377Syt  *
524168d33a25Syt  * This interrupt event means an unknown FIS was received and has been
524268d33a25Syt  * copied into system memory. An unknown FIS is not considered an illegal
524368d33a25Syt  * FIS, unless the length received is more than 64 bytes. If an unknown
524468d33a25Syt  * FIS arrives with length <= 64 bytes, it is posted and the HBA continues
524568d33a25Syt  * normal operation. If the unknown FIS is more than 64 bytes, then it
524668d33a25Syt  * won't be posted to memory and PxSERR.ERR.P will be set, which is then
524768d33a25Syt  * a fatal error.
52482fcbc377Syt  *
524968d33a25Syt  * PxIS.OFS - Overflow Error
52502fcbc377Syt  *
525168d33a25Syt  * Command list overflow is defined as software building a command table
525268d33a25Syt  * that has fewer total bytes than the transaction given to the device.
525368d33a25Syt  * On device writes, the HBA will run out of data, and on reads, there
525468d33a25Syt  * will be no room to put the data.
52552fcbc377Syt  *
525668d33a25Syt  * For an overflow on data read, either PIO or DMA, the HBA will set
525768d33a25Syt  * PxIS.OFS, and the HBA will do a best effort to continue, and it's a
525868d33a25Syt  * non-fatal error when the HBA can continues. Sometimes, it will cause
525968d33a25Syt  * a fatal error and need the software to do something.
52602fcbc377Syt  *
526168d33a25Syt  * For an overflow on data write, setting PxIS.OFS is optional for both
526268d33a25Syt  * DMA and PIO, and it's a fatal error, and a COMRESET is required by
526368d33a25Syt  * software to clean up from this serious error.
52642fcbc377Syt  *
526568d33a25Syt  * PxIS.INFS - Interface Non-Fatal Error
526668d33a25Syt  *
526768d33a25Syt  * This interrupt event indicates that the HBA encountered an error on
526868d33a25Syt  * the Serial ATA interface but was able to continue operation. The kind
526968d33a25Syt  * of error usually occurred during a non-Data FIS, and under this condition
527068d33a25Syt  * the FIS will be re-transmitted by HBA automatically.
52712fcbc377Syt  *
527268d33a25Syt  * When the FMA is implemented, there should be a stat structure to
527368d33a25Syt  * record how many every kind of error happens.
52742fcbc377Syt  */
52752fcbc377Syt static int
527668d33a25Syt ahci_intr_non_fatal_error(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
527768d33a25Syt     uint8_t port, uint32_t intr_status)
52782fcbc377Syt {
52792fcbc377Syt 	uint32_t port_serror;
528068d33a25Syt #if AHCI_DEBUG
52812fcbc377Syt 	uint32_t port_cmd_status;
528282263d52Syt 	uint32_t port_cmd_issue;
528382263d52Syt 	uint32_t port_sactive;
52842fcbc377Syt 	int current_slot;
528582263d52Syt 	uint32_t current_tags;
528668d33a25Syt 	sata_pkt_t *satapkt;
528738547057Sying tian - Beijing China 	ahci_cmd_header_t *cmd_header;
528838547057Sying tian - Beijing China 	uint32_t cmd_dmacount;
528968d33a25Syt #endif
52902fcbc377Syt 
52912fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
52922fcbc377Syt 
52932fcbc377Syt 	port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
52942fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
52952fcbc377Syt 
529668d33a25Syt 	AHCIDBG2(AHCIDBG_INTR|AHCIDBG_ENTRY|AHCIDBG_ERRS, ahci_ctlp,
529768d33a25Syt 	    "ahci_intr_non_fatal_error: port %d, "
52982fcbc377Syt 	    "port_serror = 0x%x", port, port_serror);
52992fcbc377Syt 
5300a9440e8dSyt 	ahci_log_serror_message(ahci_ctlp, port, port_serror, 1);
530168d33a25Syt 
530268d33a25Syt 	if (intr_status & AHCI_INTR_STATUS_UFS) {
530368d33a25Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
530468d33a25Syt 		    "ahci port %d has unknown FIS error", port);
53052fcbc377Syt 
530668d33a25Syt 		/* Clear the interrupt bit by clearing PxSERR.DIAG.F */
530768d33a25Syt 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
530868d33a25Syt 		    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
530982263d52Syt 		    SERROR_FIS_TYPE);
531068d33a25Syt 	}
531168d33a25Syt 
531268d33a25Syt #if AHCI_DEBUG
531368d33a25Syt 	if (intr_status & AHCI_INTR_STATUS_OFS) {
531468d33a25Syt 		AHCIDBG1(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
531568d33a25Syt 		    "ahci port %d has overflow error", port);
531668d33a25Syt 	}
531768d33a25Syt 
531868d33a25Syt 	if (intr_status & AHCI_INTR_STATUS_INFS) {
531968d33a25Syt 		AHCIDBG1(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
532068d33a25Syt 		    "ahci port %d has interface non fatal error", port);
532168d33a25Syt 	}
53222fcbc377Syt 
53232fcbc377Syt 	/*
53242fcbc377Syt 	 * Record the error occurred command's slot.
53252fcbc377Syt 	 */
532682263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
532782263d52Syt 	    ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
532882263d52Syt 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
532982263d52Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
533082263d52Syt 
533182263d52Syt 		current_slot = (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
533282263d52Syt 		    AHCI_CMD_STATUS_CCS_SHIFT;
53332fcbc377Syt 
533482263d52Syt 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
533582263d52Syt 			satapkt = ahci_portp->ahciport_err_retri_pkt;
533682263d52Syt 			ASSERT(satapkt != NULL);
533782263d52Syt 			ASSERT(current_slot == 0);
533882263d52Syt 		} else {
533982263d52Syt 			satapkt = ahci_portp->ahciport_slot_pkts[current_slot];
534082263d52Syt 		}
53412fcbc377Syt 
534282263d52Syt 		if (satapkt != NULL) {
534382263d52Syt 			AHCIDBG2(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
534482263d52Syt 			    "ahci_intr_non_fatal_error: pending_tags = 0x%x "
534582263d52Syt 			    "cmd 0x%x", ahci_portp->ahciport_pending_tags,
534682263d52Syt 			    satapkt->satapkt_cmd.satacmd_cmd_reg);
53472fcbc377Syt 
534882263d52Syt 			AHCIDBG2(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
534982263d52Syt 			    "ahci_intr_non_fatal_error: port %d, "
535082263d52Syt 			    "satapkt 0x%p is being processed when error occurs",
535182263d52Syt 			    port, (void *)satapkt);
535238547057Sying tian - Beijing China 
535338547057Sying tian - Beijing China 			/*
535438547057Sying tian - Beijing China 			 * PRD Byte Count field of command header is not
535538547057Sying tian - Beijing China 			 * required to reflect the total number of bytes
535638547057Sying tian - Beijing China 			 * transferred when an overflow occurs, so here
535738547057Sying tian - Beijing China 			 * just log the value.
535838547057Sying tian - Beijing China 			 */
535938547057Sying tian - Beijing China 			cmd_dmacount =
536038547057Sying tian - Beijing China 			    ahci_portp->ahciport_prd_bytecounts[current_slot];
536138547057Sying tian - Beijing China 			if (cmd_dmacount) {
536238547057Sying tian - Beijing China 				cmd_header = &ahci_portp->
536338547057Sying tian - Beijing China 				    ahciport_cmd_list[current_slot];
536438547057Sying tian - Beijing China 				AHCIDBG3(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
536538547057Sying tian - Beijing China 				    "ahci_intr_non_fatal_error: port %d, "
536638547057Sying tian - Beijing China 				    "PRD Byte Count = 0x%x, "
536738547057Sying tian - Beijing China 				    "ahciport_prd_bytecounts = 0x%x", port,
536838547057Sying tian - Beijing China 				    cmd_header->ahcich_prd_byte_count,
536938547057Sying tian - Beijing China 				    cmd_dmacount);
537038547057Sying tian - Beijing China 			}
537182263d52Syt 		}
5372a9440e8dSyt 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
537382263d52Syt 		/*
537482263d52Syt 		 * For queued command, list those command which have already
537582263d52Syt 		 * been transmitted to the device and still not completed.
537682263d52Syt 		 */
537782263d52Syt 		port_sactive = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
537882263d52Syt 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
537982263d52Syt 
538082263d52Syt 		port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
538182263d52Syt 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
538282263d52Syt 
538382263d52Syt 		AHCIDBG3(AHCIDBG_INTR|AHCIDBG_NCQ|AHCIDBG_ERRS, ahci_ctlp,
538482263d52Syt 		    "ahci_intr_non_fatal_error: pending_ncq_tags = 0x%x "
538582263d52Syt 		    "port_sactive = 0x%x port_cmd_issue = 0x%x",
538682263d52Syt 		    ahci_portp->ahciport_pending_ncq_tags,
538782263d52Syt 		    port_sactive, port_cmd_issue);
538882263d52Syt 
538982263d52Syt 		current_tags = ahci_portp->ahciport_pending_ncq_tags &
539082263d52Syt 		    port_sactive & ~port_cmd_issue &
539182263d52Syt 		    AHCI_NCQ_SLOT_MASK(ahci_portp);
539282263d52Syt 
539382263d52Syt 		while (current_tags) {
539482263d52Syt 			current_slot = ddi_ffs(current_tags) - 1;
539582263d52Syt 			if (current_slot == -1) {
539682263d52Syt 				goto out;
539782263d52Syt 			}
539882263d52Syt 
539982263d52Syt 			satapkt = ahci_portp->ahciport_slot_pkts[current_slot];
540082263d52Syt 			AHCIDBG2(AHCIDBG_INTR|AHCIDBG_NCQ|AHCIDBG_ERRS,
540182263d52Syt 			    ahci_ctlp, "ahci_intr_non_fatal_error: "
540282263d52Syt 			    "port %d, satapkt 0x%p is outstanding when "
540382263d52Syt 			    "error occurs", port, (void *)satapkt);
540482263d52Syt 		}
54052fcbc377Syt 	}
540682263d52Syt out:
54072fcbc377Syt #endif
54082fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
54092fcbc377Syt 
54102fcbc377Syt 	return (AHCI_SUCCESS);
54112fcbc377Syt }
54122fcbc377Syt 
54132fcbc377Syt /*
541468d33a25Syt  * According to the AHCI spec, the error types include system memory
541568d33a25Syt  * errors, interface errors, port multiplier errors, device errors,
541668d33a25Syt  * command list overflow, command list underflow, native command
541768d33a25Syt  * queuing tag errors and pio data transfer errors.
541868d33a25Syt  *
541968d33a25Syt  * System memory errors such as target abort, master abort, and parity
542068d33a25Syt  * may cause the host to stop, and they are serious errors and needed
542168d33a25Syt  * to be recovered with software intervention. When system software
542268d33a25Syt  * has given a pointer to the HBA that doesn't exist in physical memory,
542368d33a25Syt  * a master/target abort error occurs, and PxIS.HBFS will be set. A
542468d33a25Syt  * data error such as CRC or parity occurs, the HBA aborts the transfer
542568d33a25Syt  * (if necessary) and PxIS.HBDS will be set.
542668d33a25Syt  *
542768d33a25Syt  * Interface errors are errors that occur due to electrical issues on
542868d33a25Syt  * the interface, or protocol miscommunication between the device and
542968d33a25Syt  * HBA, and the respective PxSERR register bit will be set. And PxIS.IFS
543068d33a25Syt  * (fatal) or PxIS.INFS (non-fatal) will be set. The conditions that
543168d33a25Syt  * causes PxIS.IFS/PxIS.INFS to be set are
543268d33a25Syt  * 	1. in PxSERR.ERR, P bit is set to '1'
543368d33a25Syt  *	2. in PxSERR.DIAG, C or H bit is set to '1'
543468d33a25Syt  *	3. PhyRdy drop unexpectly, N bit is set to '1'
543568d33a25Syt  * If the error occurred during a non-data FIS, the FIS must be
543668d33a25Syt  * retransmitted, and the error is non-fatal and PxIS.INFS is set. If
543768d33a25Syt  * the error occurred during a data FIS, the transfer will stop, so
543868d33a25Syt  * the error is fatal and PxIS.IFS is set.
543968d33a25Syt  *
544068d33a25Syt  * When a FIS arrives that updates the taskfile, the HBA checks to see
544168d33a25Syt  * if PxTFD.STS.ERR is set. If yes, PxIS.TFES will be set and the HBA
544268d33a25Syt  * stops processing any more commands.
54432fcbc377Syt  *
544468d33a25Syt  * Command list overflow is defined as software building a command table
544568d33a25Syt  * that has fewer total bytes than the transaction given to the device.
544668d33a25Syt  * On device writes, the HBA will run out of data, and on reads, there
544768d33a25Syt  * will be no room to put the data. For an overflow on data read, either
544868d33a25Syt  * PIO or DMA, the HBA will set PxIS.OFS, and it's a non-fatal error.
544968d33a25Syt  * For an overflow on data write, setting PxIS.OFS is optional for both
545068d33a25Syt  * DMA and PIO, and a COMRESET is required by software to clean up from
545168d33a25Syt  * this serious error.
54522fcbc377Syt  *
545368d33a25Syt  * Command list underflow is defined as software building a command
545468d33a25Syt  * table that has more total bytes than the transaction given to the
545568d33a25Syt  * device. For data writes, both PIO and DMA, the device will detect
545668d33a25Syt  * an error and end the transfer. And these errors are most likely going
545768d33a25Syt  * to be fatal errors that will cause the port to be restarted. For
545868d33a25Syt  * data reads, the HBA updates its PRD byte count, and may be
545968d33a25Syt  * able to continue normally, but is not required to. And The HBA is
546068d33a25Syt  * not required to detect underflow conditions for native command
546168d33a25Syt  * queuing command.
546268d33a25Syt  *
546368d33a25Syt  * The HBA does not actively check incoming DMA Setup FISes to ensure
546468d33a25Syt  * that the PxSACT register bit for that slot is set. Existing error
546568d33a25Syt  * mechanisms, such as host bus failure, or bad protocol, are used to
546668d33a25Syt  * recover from this case.
546768d33a25Syt  *
546868d33a25Syt  * In accordance with Serial ATA 1.0a, DATA FISes prior to the final
546968d33a25Syt  * DATA FIS must be an integral number of Dwords. If the HBA receives
547068d33a25Syt  * a request which is not an integral number of Dwords, the HBA
547168d33a25Syt  * set PxSERR.ERR.P to '1', set PxIS.IFS to '1' and stop running until
547268d33a25Syt  * software restarts the port. And the HBA ensures that the size
547368d33a25Syt  * of the DATA FIS received during a PIO command matches the size in
547468d33a25Syt  * the Transfer Cound field of the preceding PIO Setup FIS, if not, the
547568d33a25Syt  * HBA sets PxSERR.ERR.P to '1', set PxIS.IFS to '1', and then
547668d33a25Syt  * stop running until software restarts the port.
54772fcbc377Syt  */
54782fcbc377Syt /*
547968d33a25Syt  * the fatal errors include PxIS.IFS, PxIS.HBDS, PxIS.HBFS and PxIS.TFES.
548068d33a25Syt  *
548168d33a25Syt  * PxIS.IFS indicates that the hba encountered an error on the serial ata
548268d33a25Syt  * interface which caused the transfer to stop.
54832fcbc377Syt  *
548468d33a25Syt  * PxIS.HBDS indicates that the hba encountered a data error
548568d33a25Syt  * (uncorrectable ecc/parity) when reading from or writing to system memory.
548668d33a25Syt  *
548768d33a25Syt  * PxIS.HBFS indicates that the hba encountered a host bus error that it
548868d33a25Syt  * cannot recover from, such as a bad software pointer.
548968d33a25Syt  *
549068d33a25Syt  * PxIS.TFES is set whenever the status register is updated by the device
549168d33a25Syt  * and the error bit (bit 0) is set.
54922fcbc377Syt  */
54932fcbc377Syt static int
549482263d52Syt ahci_intr_fatal_error(ahci_ctl_t *ahci_ctlp,
549582263d52Syt     ahci_port_t *ahci_portp, uint8_t port, uint32_t intr_status)
54962fcbc377Syt {
549768d33a25Syt 	uint32_t port_cmd_status;
54982fcbc377Syt 	uint32_t port_serror;
549968d33a25Syt 	uint32_t task_file_status;
550068d33a25Syt 	int failed_slot;
550182263d52Syt 	sata_pkt_t *spkt = NULL;
550268d33a25Syt 	uint8_t err_byte;
550368d33a25Syt 	ahci_event_arg_t *args;
5504a9440e8dSyt 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
55052fcbc377Syt 
55062fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
55072fcbc377Syt 
550868d33a25Syt 	/*
550968d33a25Syt 	 * ahci_intr_phyrdy_change() may have rendered it to
551068d33a25Syt 	 * SATA_DTYPE_NONE.
551168d33a25Syt 	 */
551268d33a25Syt 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
551368d33a25Syt 		AHCIDBG1(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
551468d33a25Syt 		    "ahci_intr_fatal_error: port %d no device attached, "
551568d33a25Syt 		    "and just return without doing anything", port);
551668d33a25Syt 		goto out0;
551768d33a25Syt 	}
55182fcbc377Syt 
5519f8a673adSying tian - Beijing China 	if (intr_status & AHCI_INTR_STATUS_TFES) {
5520f8a673adSying tian - Beijing China 		task_file_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5521f8a673adSying tian - Beijing China 		    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
5522f8a673adSying tian - Beijing China 		AHCIDBG2(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
5523f8a673adSying tian - Beijing China 		    "ahci_intr_fatal_error: port %d "
5524f8a673adSying tian - Beijing China 		    "task_file_status = 0x%x", port, task_file_status);
5525f8a673adSying tian - Beijing China 	}
5526f8a673adSying tian - Beijing China 
552782263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
552882263d52Syt 		/*
552982263d52Syt 		 * Read PxCMD.CCS to determine the slot that the HBA
553082263d52Syt 		 * was processing when the error occurred.
553182263d52Syt 		 */
553282263d52Syt 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
553382263d52Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
553482263d52Syt 		failed_slot = (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
553582263d52Syt 		    AHCI_CMD_STATUS_CCS_SHIFT;
55362fcbc377Syt 
553782263d52Syt 		spkt = ahci_portp->ahciport_slot_pkts[failed_slot];
553868d33a25Syt 		AHCIDBG2(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
553982263d52Syt 		    "ahci_intr_fatal_error: spkt 0x%p is being processed when "
554082263d52Syt 		    "fatal error occurred for port %d", spkt, port);
55412fcbc377Syt 
554282263d52Syt 		if (intr_status & AHCI_INTR_STATUS_TFES) {
554382263d52Syt 			err_byte = (task_file_status & AHCI_TFD_ERR_MASK)
554482263d52Syt 			    >> AHCI_TFD_ERR_SHIFT;
55452fcbc377Syt 
554682263d52Syt 			/*
554782263d52Syt 			 * Won't emit the error message if it is an IDENTIFY
554882263d52Syt 			 * DEVICE command sent to an ATAPI device.
554982263d52Syt 			 */
555082263d52Syt 			if ((spkt != NULL) &&
555182263d52Syt 			    (spkt->satapkt_cmd.satacmd_cmd_reg ==
555282263d52Syt 			    SATAC_ID_DEVICE) &&
555382263d52Syt 			    (err_byte == SATA_ERROR_ABORT))
555482263d52Syt 				goto out1;
555582263d52Syt 
555682263d52Syt 			/*
555782263d52Syt 			 * Won't emit the error message if it is an ATAPI PACKET
555882263d52Syt 			 * command
555982263d52Syt 			 */
556082263d52Syt 			if ((spkt != NULL) &&
556182263d52Syt 			    (spkt->satapkt_cmd.satacmd_cmd_reg == SATAC_PACKET))
556282263d52Syt 				goto out1;
556382263d52Syt 		}
556468d33a25Syt 	}
55652fcbc377Syt 
55667095af19Sying tian - Beijing China 	/* print the fatal error type */
556768d33a25Syt 	ahci_log_fatal_error_message(ahci_ctlp, port, intr_status);
55682fcbc377Syt 	port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
55692fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
55707095af19Sying tian - Beijing China 
55717095af19Sying tian - Beijing China 	/* print PxSERR related error message */
5572a9440e8dSyt 	ahci_log_serror_message(ahci_ctlp, port, port_serror, 0);
55737095af19Sying tian - Beijing China 
55747095af19Sying tian - Beijing China 	/* print task file register value */
55757095af19Sying tian - Beijing China 	if (intr_status & AHCI_INTR_STATUS_TFES) {
55767095af19Sying tian - Beijing China 		cmn_err(CE_WARN, "!ahci%d: ahci port %d task_file_status "
55777095af19Sying tian - Beijing China 		    "= 0x%x", instance, port, task_file_status);
55787095af19Sying tian - Beijing China 	}
55797095af19Sying tian - Beijing China 
5580a9440e8dSyt out1:
558168d33a25Syt 	/* Prepare the argument for the taskq */
558268d33a25Syt 	args = ahci_portp->ahciport_event_args;
558368d33a25Syt 	args->ahciea_ctlp = (void *)ahci_ctlp;
558468d33a25Syt 	args->ahciea_portp = (void *)ahci_portp;
558568d33a25Syt 	args->ahciea_event = intr_status;
558668d33a25Syt 
558768d33a25Syt 	/* Start the taskq to handle error recovery */
5588f8a673adSying tian - Beijing China 	if ((ddi_taskq_dispatch(ahci_portp->ahciport_event_taskq,
558968d33a25Syt 	    ahci_events_handler,
559068d33a25Syt 	    (void *)args, DDI_NOSLEEP)) != DDI_SUCCESS) {
5591a9440e8dSyt 		cmn_err(CE_WARN, "!ahci%d: ahci start taskq for event handler "
5592a9440e8dSyt 		    "failed", instance);
559368d33a25Syt 	}
559468d33a25Syt out0:
55952fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
55962fcbc377Syt 
55972fcbc377Syt 	return (AHCI_SUCCESS);
55982fcbc377Syt }
55992fcbc377Syt 
56002fcbc377Syt /*
56012fcbc377Syt  * Hot Plug Operation for platforms that support Cold Presence Detect.
56022fcbc377Syt  *
56032fcbc377Syt  * When set, a device status has changed as detected by the cold presence
56042fcbc377Syt  * detect logic. This bit can either be set due to a non-connected port
56052fcbc377Syt  * receiving a device, or a connected port having its device removed.
56062fcbc377Syt  * This bit is only valid if the port supports cold presence detect as
56072fcbc377Syt  * indicated by PxCMD.CPD set to '1'.
56082fcbc377Syt  *
560968d33a25Syt  * At the moment, this interrupt is not needed and disabled and we just
561068d33a25Syt  * log the debug message.
56112fcbc377Syt  */
56122fcbc377Syt static int
56132fcbc377Syt ahci_intr_cold_port_detect(ahci_ctl_t *ahci_ctlp,
56142fcbc377Syt     ahci_port_t *ahci_portp, uint8_t port)
56152fcbc377Syt {
56162fcbc377Syt 	uint32_t port_cmd_status;
56172fcbc377Syt 	sata_device_t sdevice;
56182fcbc377Syt 
56192fcbc377Syt 	AHCIDBG1(AHCIDBG_INTR, ahci_ctlp,
56202fcbc377Syt 	    "ahci_intr_cold_port_detect enter, port %d", port);
56212fcbc377Syt 
56222fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
56232fcbc377Syt 
56242fcbc377Syt 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
56252fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
56262fcbc377Syt 	if (!(port_cmd_status & AHCI_CMD_STATUS_CPD)) {
56272fcbc377Syt 		AHCIDBG1(AHCIDBG_INTR, ahci_ctlp,
56282fcbc377Syt 		    "port %d does not support cold presence detect, so "
56292fcbc377Syt 		    "we just ignore this interrupt", port);
56302fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
56312fcbc377Syt 		return (AHCI_SUCCESS);
56322fcbc377Syt 	}
56332fcbc377Syt 
56342fcbc377Syt 	AHCIDBG1(AHCIDBG_INTR, ahci_ctlp,
56352fcbc377Syt 	    "port %d device status has changed", port);
56362fcbc377Syt 
56372fcbc377Syt 	bzero((void *)&sdevice, sizeof (sata_device_t));
563809121340Syt 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
56392fcbc377Syt 	sdevice.satadev_addr.qual = SATA_ADDR_CPORT;
56402fcbc377Syt 	sdevice.satadev_addr.pmport = 0;
56412fcbc377Syt 	sdevice.satadev_state = SATA_PSTATE_PWRON;
56422fcbc377Syt 
56432fcbc377Syt 	if (port_cmd_status & AHCI_CMD_STATUS_CPS) {
56442fcbc377Syt 		AHCIDBG1(AHCIDBG_INTR, ahci_ctlp,
56452fcbc377Syt 		    "port %d: a device is hot plugged", port);
56462fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
56472fcbc377Syt 		sata_hba_event_notify(
56482fcbc377Syt 		    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
56492fcbc377Syt 		    &sdevice,
56502fcbc377Syt 		    SATA_EVNT_DEVICE_ATTACHED);
56512fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
56522fcbc377Syt 
56532fcbc377Syt 	} else {
56542fcbc377Syt 		AHCIDBG1(AHCIDBG_INTR, ahci_ctlp,
56552fcbc377Syt 		    "port %d: a device is hot unplugged", port);
56562fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
56572fcbc377Syt 		sata_hba_event_notify(
56582fcbc377Syt 		    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
56592fcbc377Syt 		    &sdevice,
56602fcbc377Syt 		    SATA_EVNT_DEVICE_DETACHED);
56612fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
56622fcbc377Syt 	}
56632fcbc377Syt 
56642fcbc377Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
56652fcbc377Syt 
56662fcbc377Syt 	return (AHCI_SUCCESS);
56672fcbc377Syt }
56682fcbc377Syt 
56692fcbc377Syt /*
56702fcbc377Syt  * Enable the interrupts for a particular port.
56712fcbc377Syt  *
56722fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
56732fcbc377Syt  * is called.
56742fcbc377Syt  */
56752fcbc377Syt static void
5676689d74b0Syt ahci_enable_port_intrs(ahci_ctl_t *ahci_ctlp, uint8_t port)
56772fcbc377Syt {
56782fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
56792fcbc377Syt 	    "ahci_enable_port_intrs enter, port %d", port);
56802fcbc377Syt 
56812fcbc377Syt 	/*
56822fcbc377Syt 	 * Clear port interrupt status before enabling interrupt
56832fcbc377Syt 	 */
56842fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
56852fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port),
56862fcbc377Syt 	    AHCI_PORT_INTR_MASK);
56872fcbc377Syt 
56882fcbc377Syt 	/*
56892fcbc377Syt 	 * Clear the pending bit from IS.IPS
56902fcbc377Syt 	 */
56912fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
56922fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_IS(ahci_ctlp), (1 << port));
56932fcbc377Syt 
56942fcbc377Syt 	/*
56952fcbc377Syt 	 * Enable the following interrupts:
56962fcbc377Syt 	 *	Device to Host Register FIS Interrupt (DHRS)
56972fcbc377Syt 	 *	PIO Setup FIS Interrupt (PSS)
569882263d52Syt 	 *	Set Device Bits Interrupt (SDBS)
56992fcbc377Syt 	 *	Unknown FIS Interrupt (UFS)
57002fcbc377Syt 	 *	Port Connect Change Status (PCS)
57012fcbc377Syt 	 *	PhyRdy Change Status (PRCS)
57022fcbc377Syt 	 *	Overflow Status (OFS)
57032fcbc377Syt 	 *	Interface Non-fatal Error Status (INFS)
57042fcbc377Syt 	 *	Interface Fatal Error Status (IFS)
57052fcbc377Syt 	 *	Host Bus Data Error Status (HBDS)
57062fcbc377Syt 	 *	Host Bus Fatal Error Status (HBFS)
57072fcbc377Syt 	 *	Task File Error Status (TFES)
57082fcbc377Syt 	 */
57092fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
57102fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port),
57112fcbc377Syt 	    (AHCI_INTR_STATUS_DHRS |
57122fcbc377Syt 	    AHCI_INTR_STATUS_PSS |
571382263d52Syt 	    AHCI_INTR_STATUS_SDBS |
57142fcbc377Syt 	    AHCI_INTR_STATUS_UFS |
571568d33a25Syt 	    AHCI_INTR_STATUS_DPS |
57162fcbc377Syt 	    AHCI_INTR_STATUS_PCS |
57172fcbc377Syt 	    AHCI_INTR_STATUS_PRCS |
57182fcbc377Syt 	    AHCI_INTR_STATUS_OFS |
57192fcbc377Syt 	    AHCI_INTR_STATUS_INFS |
57202fcbc377Syt 	    AHCI_INTR_STATUS_IFS |
57212fcbc377Syt 	    AHCI_INTR_STATUS_HBDS |
57222fcbc377Syt 	    AHCI_INTR_STATUS_HBFS |
57232fcbc377Syt 	    AHCI_INTR_STATUS_TFES));
57242fcbc377Syt }
57252fcbc377Syt 
57262fcbc377Syt /*
57272fcbc377Syt  * Enable interrupts for all the ports.
57282fcbc377Syt  *
57292fcbc377Syt  * WARNING!!! ahcictl_mutex should be acquired before the function
57302fcbc377Syt  * is called.
57312fcbc377Syt  */
57322fcbc377Syt static void
57332fcbc377Syt ahci_enable_all_intrs(ahci_ctl_t *ahci_ctlp)
57342fcbc377Syt {
57352fcbc377Syt 	uint32_t ghc_control;
57362fcbc377Syt 
57372fcbc377Syt 	AHCIDBG0(AHCIDBG_ENTRY, ahci_ctlp, "ahci_enable_all_intrs enter");
57382fcbc377Syt 
57392fcbc377Syt 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
57402fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
57412fcbc377Syt 
57422fcbc377Syt 	ghc_control |= AHCI_HBA_GHC_IE;
57432fcbc377Syt 
57442fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
57452fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
57462fcbc377Syt }
57472fcbc377Syt 
57482fcbc377Syt /*
57492fcbc377Syt  * Disable interrupts for a particular port.
57502fcbc377Syt  *
57512fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
57522fcbc377Syt  * is called.
57532fcbc377Syt  */
57542fcbc377Syt static void
5755689d74b0Syt ahci_disable_port_intrs(ahci_ctl_t *ahci_ctlp, uint8_t port)
57562fcbc377Syt {
57572fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
57582fcbc377Syt 	    "ahci_disable_port_intrs enter, port %d", port);
57592fcbc377Syt 
57602fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
57612fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port), 0);
57622fcbc377Syt }
57632fcbc377Syt 
57642fcbc377Syt /*
57652fcbc377Syt  * Disable interrupts for the whole HBA.
57662fcbc377Syt  *
57672fcbc377Syt  * The global bit is cleared, then all interrupt sources from all
57682fcbc377Syt  * ports are disabled.
57692fcbc377Syt  *
57702fcbc377Syt  * WARNING!!! ahcictl_mutex should be acquired before the function
57712fcbc377Syt  * is called.
57722fcbc377Syt  */
57732fcbc377Syt static void
57742fcbc377Syt ahci_disable_all_intrs(ahci_ctl_t *ahci_ctlp)
57752fcbc377Syt {
57762fcbc377Syt 	uint32_t ghc_control;
57772fcbc377Syt 
57782fcbc377Syt 	AHCIDBG0(AHCIDBG_ENTRY, ahci_ctlp, "ahci_disable_all_intrs enter");
57792fcbc377Syt 
57802fcbc377Syt 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
57812fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
57822fcbc377Syt 
57832fcbc377Syt 	ghc_control &= ~ AHCI_HBA_GHC_IE;
57842fcbc377Syt 
57852fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
57862fcbc377Syt 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
57872fcbc377Syt }
57882fcbc377Syt 
57892fcbc377Syt /*
57902c742e1fSying tian - Beijing China  * Handle FIXED or MSI interrupts.
57912fcbc377Syt  */
57922fcbc377Syt /*
57932c742e1fSying tian - Beijing China  * According to AHCI spec, the HBA may support several interrupt modes:
57942c742e1fSying tian - Beijing China  *	* pin based interrupts (FIXED)
57952c742e1fSying tian - Beijing China  *	* single MSI message interrupts
57962c742e1fSying tian - Beijing China  *	* multiple MSI based message interrupts
57972c742e1fSying tian - Beijing China  *
57982c742e1fSying tian - Beijing China  * For pin based interrupts, the software interrupt handler need to check IS
57992c742e1fSying tian - Beijing China  * register to find out which port has pending interrupts. And then check
58002c742e1fSying tian - Beijing China  * PxIS register to find out which interrupt events happened on that port.
58012c742e1fSying tian - Beijing China  *
58022c742e1fSying tian - Beijing China  * For single MSI message interrupts, MSICAP.MC.MSIE is set with '1', and
58032c742e1fSying tian - Beijing China  * MSICAP.MC.MME is set with '0'. This mode is similar to pin based interrupts
58042c742e1fSying tian - Beijing China  * in that software interrupt handler need to check IS register to determine
58052c742e1fSying tian - Beijing China  * which port triggered the interrupts since it uses a single message for all
58062c742e1fSying tian - Beijing China  * port interrupts.
58072c742e1fSying tian - Beijing China  *
58082c742e1fSying tian - Beijing China  * HBA may optionally support multiple MSI message for better performance. In
58092c742e1fSying tian - Beijing China  * this mode, each port may have its own interrupt message, and thus generation
58102c742e1fSying tian - Beijing China  * of interrupts is no longer controlled through the IS register. MSICAP.MC.MMC
58112c742e1fSying tian - Beijing China  * represents a power-of-2 wrapper on the number of implemented ports, and
58122c742e1fSying tian - Beijing China  * the mapping of ports to interrupts is done in a 1-1 relationship, up to the
58132c742e1fSying tian - Beijing China  * maximum number of assigned interrupts. When the number of MSI messages
58142c742e1fSying tian - Beijing China  * allocated is less than the number requested, then hardware may have two
58152c742e1fSying tian - Beijing China  * implementation behaviors:
58162c742e1fSying tian - Beijing China  *	* assign each ports its own interrupt and then force all additional
58172c742e1fSying tian - Beijing China  *	  ports to share the last interrupt message, and this condition is
58182c742e1fSying tian - Beijing China  *	  indicated by clearing GHC.MRSM to '0'
58192c742e1fSying tian - Beijing China  *	* revert to single MSI mode, indicated by setting GHC.MRSM to '1'
58202c742e1fSying tian - Beijing China  * When multiple-message MSI is enabled, hardware will still set IS register
58212c742e1fSying tian - Beijing China  * as single message case. And this IS register may be used by software when
58222c742e1fSying tian - Beijing China  * fewer than the requested number of messages is granted in order to determine
58232c742e1fSying tian - Beijing China  * which port had the interrupt.
58242c742e1fSying tian - Beijing China  *
58252c742e1fSying tian - Beijing China  * Note: The current ahci driver only supports the first two interrupt modes:
58262c742e1fSying tian - Beijing China  * pin based interrupts and single MSI message interrupts, and the reason
58272c742e1fSying tian - Beijing China  * is indicated in below code.
58282fcbc377Syt  */
58292fcbc377Syt static int
58302c742e1fSying tian - Beijing China ahci_add_intrs(ahci_ctl_t *ahci_ctlp, int intr_type)
58312fcbc377Syt {
58322fcbc377Syt 	dev_info_t *dip = ahci_ctlp->ahcictl_dip;
58332fcbc377Syt 	int		count, avail, actual;
58342c742e1fSying tian - Beijing China 	int		i, rc;
58352fcbc377Syt 
58362c742e1fSying tian - Beijing China 	AHCIDBG1(AHCIDBG_ENTRY|AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
58372c742e1fSying tian - Beijing China 	    "ahci_add_intrs enter interrupt type 0x%x", intr_type);
58382fcbc377Syt 
58392fcbc377Syt 	/* get number of interrupts. */
58402c742e1fSying tian - Beijing China 	rc = ddi_intr_get_nintrs(dip, intr_type, &count);
58412fcbc377Syt 	if ((rc != DDI_SUCCESS) || (count == 0)) {
58422fcbc377Syt 		AHCIDBG2(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
58432fcbc377Syt 		    "ddi_intr_get_nintrs() failed, "
58442fcbc377Syt 		    "rc %d count %d\n", rc, count);
58452fcbc377Syt 		return (DDI_FAILURE);
58462fcbc377Syt 	}
58472fcbc377Syt 
58482fcbc377Syt 	/* get number of available interrupts. */
58492c742e1fSying tian - Beijing China 	rc = ddi_intr_get_navail(dip, intr_type, &avail);
58502fcbc377Syt 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
58512fcbc377Syt 		AHCIDBG2(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
58522fcbc377Syt 		    "ddi_intr_get_navail() failed, "
58532fcbc377Syt 		    "rc %d avail %d\n", rc, avail);
58542fcbc377Syt 		return (DDI_FAILURE);
58552fcbc377Syt 	}
58562fcbc377Syt 
5857689d74b0Syt #if AHCI_DEBUG
58582fcbc377Syt 	if (avail < count) {
58592fcbc377Syt 		AHCIDBG2(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
58602c742e1fSying tian - Beijing China 		    "ddi_intr_get_nintrs returned %d, navail() returned %d",
58612fcbc377Syt 		    count, avail);
58622fcbc377Syt 	}
5863689d74b0Syt #endif
58642fcbc377Syt 
58652c742e1fSying tian - Beijing China 	/*
58662c742e1fSying tian - Beijing China 	 * Note: So far Solaris restricts the maximum number of messages for
58672c742e1fSying tian - Beijing China 	 * x86 to 2, that is avail is 2, so here we set the count with 1 to
58682c742e1fSying tian - Beijing China 	 * force the driver to use single MSI message interrupt. In future if
58692c742e1fSying tian - Beijing China 	 * Solaris remove the restriction, then we need to delete the below
58702c742e1fSying tian - Beijing China 	 * code and try to use multiple interrupt routine to gain better
58712c742e1fSying tian - Beijing China 	 * performance.
58722c742e1fSying tian - Beijing China 	 */
58732c742e1fSying tian - Beijing China 	if ((intr_type == DDI_INTR_TYPE_MSI) && (count > 1)) {
58742c742e1fSying tian - Beijing China 		AHCIDBG1(AHCIDBG_INTR, ahci_ctlp,
58752c742e1fSying tian - Beijing China 		    "force to use one interrupt routine though the "
58762c742e1fSying tian - Beijing China 		    "HBA supports %d interrupt", count);
58772c742e1fSying tian - Beijing China 		count = 1;
58782c742e1fSying tian - Beijing China 	}
58792c742e1fSying tian - Beijing China 
58802fcbc377Syt 	/* Allocate an array of interrupt handles. */
58812fcbc377Syt 	ahci_ctlp->ahcictl_intr_size = count * sizeof (ddi_intr_handle_t);
58822fcbc377Syt 	ahci_ctlp->ahcictl_intr_htable =
58832fcbc377Syt 	    kmem_alloc(ahci_ctlp->ahcictl_intr_size, KM_SLEEP);
58842fcbc377Syt 
58852fcbc377Syt 	/* call ddi_intr_alloc(). */
58862fcbc377Syt 	rc = ddi_intr_alloc(dip, ahci_ctlp->ahcictl_intr_htable,
58872c742e1fSying tian - Beijing China 	    intr_type, 0, count, &actual, DDI_INTR_ALLOC_NORMAL);
58882fcbc377Syt 
58892fcbc377Syt 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
58902c742e1fSying tian - Beijing China 		AHCIDBG4(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
58912c742e1fSying tian - Beijing China 		    "ddi_intr_alloc() failed, rc %d count %d actual %d "
58922c742e1fSying tian - Beijing China 		    "avail %d\n", rc, count, actual, avail);
58932fcbc377Syt 		kmem_free(ahci_ctlp->ahcictl_intr_htable,
58942fcbc377Syt 		    ahci_ctlp->ahcictl_intr_size);
58952fcbc377Syt 		return (DDI_FAILURE);
58962fcbc377Syt 	}
58972fcbc377Syt 
58982fcbc377Syt 	/* use interrupt count returned */
5899689d74b0Syt #if AHCI_DEBUG
59002fcbc377Syt 	if (actual < count) {
59012fcbc377Syt 		AHCIDBG2(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
59022fcbc377Syt 		    "Requested: %d, Received: %d", count, actual);
59032fcbc377Syt 	}
5904689d74b0Syt #endif
59052fcbc377Syt 
59062fcbc377Syt 	ahci_ctlp->ahcictl_intr_cnt = actual;
59072fcbc377Syt 
59082fcbc377Syt 	/*
59092c742e1fSying tian - Beijing China 	 * Get priority for first, assume remaining are all the same.
59102fcbc377Syt 	 */
59112fcbc377Syt 	if (ddi_intr_get_pri(ahci_ctlp->ahcictl_intr_htable[0],
59122fcbc377Syt 	    &ahci_ctlp->ahcictl_intr_pri) != DDI_SUCCESS) {
59132fcbc377Syt 		AHCIDBG0(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
59142fcbc377Syt 		    "ddi_intr_get_pri() failed");
59152fcbc377Syt 
59162fcbc377Syt 		/* Free already allocated intr. */
59172c742e1fSying tian - Beijing China 		for (i = 0; i < actual; i++) {
59182c742e1fSying tian - Beijing China 			(void) ddi_intr_free(ahci_ctlp->ahcictl_intr_htable[i]);
59192fcbc377Syt 		}
59202fcbc377Syt 
59212fcbc377Syt 		kmem_free(ahci_ctlp->ahcictl_intr_htable,
59222fcbc377Syt 		    ahci_ctlp->ahcictl_intr_size);
59232fcbc377Syt 		return (DDI_FAILURE);
59242fcbc377Syt 	}
59252fcbc377Syt 
59262fcbc377Syt 	/* Test for high level interrupt. */
59272fcbc377Syt 	if (ahci_ctlp->ahcictl_intr_pri >= ddi_intr_get_hilevel_pri()) {
59282fcbc377Syt 		AHCIDBG0(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
59292c742e1fSying tian - Beijing China 		    "ahci_add_intrs: Hi level intr not supported");
59302fcbc377Syt 
59312fcbc377Syt 		/* Free already allocated intr. */
59322c742e1fSying tian - Beijing China 		for (i = 0; i < actual; i++) {
59332c742e1fSying tian - Beijing China 			(void) ddi_intr_free(ahci_ctlp->ahcictl_intr_htable[i]);
59342fcbc377Syt 		}
59352fcbc377Syt 
59362fcbc377Syt 		kmem_free(ahci_ctlp->ahcictl_intr_htable,
59372fcbc377Syt 		    sizeof (ddi_intr_handle_t));
59382fcbc377Syt 
59392fcbc377Syt 		return (DDI_FAILURE);
59402fcbc377Syt 	}
59412fcbc377Syt 
59422fcbc377Syt 	/* Call ddi_intr_add_handler(). */
59432c742e1fSying tian - Beijing China 	for (i = 0; i < actual; i++) {
59442c742e1fSying tian - Beijing China 		if (ddi_intr_add_handler(ahci_ctlp->ahcictl_intr_htable[i],
59452fcbc377Syt 		    ahci_intr, (caddr_t)ahci_ctlp, NULL) != DDI_SUCCESS) {
59462fcbc377Syt 			AHCIDBG0(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
59472fcbc377Syt 			    "ddi_intr_add_handler() failed");
59482fcbc377Syt 
59492fcbc377Syt 			/* Free already allocated intr. */
59502c742e1fSying tian - Beijing China 			for (i = 0; i < actual; i++) {
59512fcbc377Syt 				(void) ddi_intr_free(
59522c742e1fSying tian - Beijing China 				    ahci_ctlp->ahcictl_intr_htable[i]);
59532fcbc377Syt 			}
59542fcbc377Syt 
59552fcbc377Syt 			kmem_free(ahci_ctlp->ahcictl_intr_htable,
59562fcbc377Syt 			    ahci_ctlp->ahcictl_intr_size);
59572fcbc377Syt 			return (DDI_FAILURE);
59582fcbc377Syt 		}
59592fcbc377Syt 	}
59602fcbc377Syt 
59612c742e1fSying tian - Beijing China 	if (ddi_intr_get_cap(ahci_ctlp->ahcictl_intr_htable[0],
59622c742e1fSying tian - Beijing China 	    &ahci_ctlp->ahcictl_intr_cap) != DDI_SUCCESS) {
59632c742e1fSying tian - Beijing China 		AHCIDBG0(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
59642c742e1fSying tian - Beijing China 		    "ddi_intr_get_cap() failed");
59652fcbc377Syt 
59662c742e1fSying tian - Beijing China 		/* Free already allocated intr. */
59672c742e1fSying tian - Beijing China 		for (i = 0; i < actual; i++) {
59682c742e1fSying tian - Beijing China 			(void) ddi_intr_free(
59692c742e1fSying tian - Beijing China 			    ahci_ctlp->ahcictl_intr_htable[i]);
59702c742e1fSying tian - Beijing China 		}
59712c742e1fSying tian - Beijing China 
59722c742e1fSying tian - Beijing China 		kmem_free(ahci_ctlp->ahcictl_intr_htable,
59732c742e1fSying tian - Beijing China 		    ahci_ctlp->ahcictl_intr_size);
59742c742e1fSying tian - Beijing China 		return (DDI_FAILURE);
59752c742e1fSying tian - Beijing China 	}
59762fcbc377Syt 
59772fcbc377Syt 	if (ahci_ctlp->ahcictl_intr_cap & DDI_INTR_FLAG_BLOCK) {
59782fcbc377Syt 		/* Call ddi_intr_block_enable() for MSI. */
59792fcbc377Syt 		(void) ddi_intr_block_enable(ahci_ctlp->ahcictl_intr_htable,
59802fcbc377Syt 		    ahci_ctlp->ahcictl_intr_cnt);
59812fcbc377Syt 	} else {
59822c742e1fSying tian - Beijing China 		/* Call ddi_intr_enable() for FIXED or MSI non block enable. */
59832c742e1fSying tian - Beijing China 		for (i = 0; i < ahci_ctlp->ahcictl_intr_cnt; i++) {
59842fcbc377Syt 			(void) ddi_intr_enable(
59852c742e1fSying tian - Beijing China 			    ahci_ctlp->ahcictl_intr_htable[i]);
59862fcbc377Syt 		}
59872fcbc377Syt 	}
59882fcbc377Syt 
59892fcbc377Syt 	return (DDI_SUCCESS);
59902fcbc377Syt }
59912fcbc377Syt 
59922fcbc377Syt /*
59932fcbc377Syt  * Removes the registered interrupts irrespective of whether they
59942fcbc377Syt  * were legacy or MSI.
59952fcbc377Syt  *
59962fcbc377Syt  * WARNING!!! The controller interrupts must be disabled before calling
59972fcbc377Syt  * this routine.
59982fcbc377Syt  */
59992fcbc377Syt static void
60002fcbc377Syt ahci_rem_intrs(ahci_ctl_t *ahci_ctlp)
60012fcbc377Syt {
60022fcbc377Syt 	int x;
60032fcbc377Syt 
60042fcbc377Syt 	AHCIDBG0(AHCIDBG_ENTRY, ahci_ctlp, "ahci_rem_intrs entered");
60052fcbc377Syt 
60062fcbc377Syt 	/* Disable all interrupts. */
60072fcbc377Syt 	if ((ahci_ctlp->ahcictl_intr_type == DDI_INTR_TYPE_MSI) &&
60082fcbc377Syt 	    (ahci_ctlp->ahcictl_intr_cap & DDI_INTR_FLAG_BLOCK)) {
60092fcbc377Syt 		/* Call ddi_intr_block_disable(). */
60102fcbc377Syt 		(void) ddi_intr_block_disable(ahci_ctlp->ahcictl_intr_htable,
60112fcbc377Syt 		    ahci_ctlp->ahcictl_intr_cnt);
60122fcbc377Syt 	} else {
60132fcbc377Syt 		for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
60142fcbc377Syt 			(void) ddi_intr_disable(
60152fcbc377Syt 			    ahci_ctlp->ahcictl_intr_htable[x]);
60162fcbc377Syt 		}
60172fcbc377Syt 	}
60182fcbc377Syt 
60192fcbc377Syt 	/* Call ddi_intr_remove_handler(). */
60202fcbc377Syt 	for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
60212fcbc377Syt 		(void) ddi_intr_remove_handler(
60222fcbc377Syt 		    ahci_ctlp->ahcictl_intr_htable[x]);
60232fcbc377Syt 		(void) ddi_intr_free(ahci_ctlp->ahcictl_intr_htable[x]);
60242fcbc377Syt 	}
60252fcbc377Syt 
60262fcbc377Syt 	kmem_free(ahci_ctlp->ahcictl_intr_htable, ahci_ctlp->ahcictl_intr_size);
60272fcbc377Syt }
60282fcbc377Syt 
60292fcbc377Syt /*
603068d33a25Syt  * This routine tries to put port into P:NotRunning state by clearing
603168d33a25Syt  * PxCMD.ST. HBA will clear PxCI to 0h, PxSACT to 0h, PxCMD.CCS to 0h
603268d33a25Syt  * and PxCMD.CR to '0'.
60332fcbc377Syt  *
60342fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
60352fcbc377Syt  * is called.
60362fcbc377Syt  */
60372fcbc377Syt static int
603868d33a25Syt ahci_put_port_into_notrunning_state(ahci_ctl_t *ahci_ctlp,
603968d33a25Syt     ahci_port_t *ahci_portp, uint8_t port)
60402fcbc377Syt {
60412fcbc377Syt 	uint32_t port_cmd_status;
60422fcbc377Syt 	int loop_count;
60432fcbc377Syt 
60442fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
604568d33a25Syt 	    "ahci_put_port_into_notrunning_state enter: port %d", port);
60462fcbc377Syt 
60472fcbc377Syt 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
60482fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
60492fcbc377Syt 
60502fcbc377Syt 	port_cmd_status &= ~AHCI_CMD_STATUS_ST;
60512fcbc377Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
60522fcbc377Syt 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port), port_cmd_status);
60532fcbc377Syt 
60542fcbc377Syt 	/* Wait until PxCMD.CR is cleared */
60552fcbc377Syt 	loop_count = 0;
60562fcbc377Syt 	do {
60572fcbc377Syt 		port_cmd_status =
60582fcbc377Syt 		    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
60592fcbc377Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
60602fcbc377Syt 
60612fcbc377Syt 		if (loop_count++ > AHCI_POLLRATE_PORT_IDLE) {
60622fcbc377Syt 			AHCIDBG2(AHCIDBG_INIT, ahci_ctlp,
60632fcbc377Syt 			    "clearing port %d CMD.CR timeout, "
60642fcbc377Syt 			    "port_cmd_status = 0x%x", port,
60652fcbc377Syt 			    port_cmd_status);
60662fcbc377Syt 			/*
60672fcbc377Syt 			 * We are effectively timing out after 0.5 sec.
60682fcbc377Syt 			 * This value is specified in AHCI spec.
60692fcbc377Syt 			 */
60702fcbc377Syt 			break;
60712fcbc377Syt 		}
60722fcbc377Syt 
6073689d74b0Syt 		/* Wait for 10 millisec */
607419397407SSherry Moore 		drv_usecwait(10000);
60752fcbc377Syt 	} while (port_cmd_status & AHCI_CMD_STATUS_CR);
60762fcbc377Syt 
607768d33a25Syt 	ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_STARTED;
60782fcbc377Syt 
607968d33a25Syt 	if (port_cmd_status & AHCI_CMD_STATUS_CR) {
60802fcbc377Syt 		AHCIDBG2(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
608168d33a25Syt 		    "ahci_put_port_into_notrunning_state: failed to clear "
608268d33a25Syt 		    "PxCMD.CR to '0' after loop count: %d, and "
608368d33a25Syt 		    "port_cmd_status = 0x%x", loop_count, port_cmd_status);
60842fcbc377Syt 		return (AHCI_FAILURE);
60852fcbc377Syt 	} else {
608668d33a25Syt 		AHCIDBG2(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
608768d33a25Syt 		    "ahci_put_port_into_notrunning_state: succeeded to clear "
608868d33a25Syt 		    "PxCMD.CR to '0' after loop count: %d, and "
608968d33a25Syt 		    "port_cmd_status = 0x%x", loop_count, port_cmd_status);
60902fcbc377Syt 		return (AHCI_SUCCESS);
60912fcbc377Syt 	}
60922fcbc377Syt }
60932fcbc377Syt 
60942fcbc377Syt /*
609568d33a25Syt  * First clear PxCMD.ST, and then check PxTFD. If both PxTFD.STS.BSY
609668d33a25Syt  * and PxTFD.STS.DRQ cleared to '0', it means the device is in a
609768d33a25Syt  * stable state, then set PxCMD.ST to '1' to start the port directly.
609868d33a25Syt  * If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to '1', then issue a
609968d33a25Syt  * COMRESET to the device to put it in an idle state.
610068d33a25Syt  *
610168d33a25Syt  * The fifth argument returns whether the port reset is involved during
610268d33a25Syt  * the process.
610368d33a25Syt  *
6104*0a4c4cecSXiao-Yu Zhang  * The routine will be called under following scenarios:
6105*0a4c4cecSXiao-Yu Zhang  *	+ To abort the packet(s)
6106*0a4c4cecSXiao-Yu Zhang  *	+ To reset the port
6107*0a4c4cecSXiao-Yu Zhang  *	+ To activate the port
6108*0a4c4cecSXiao-Yu Zhang  *	+ Fatal error recovery
6109*0a4c4cecSXiao-Yu Zhang  *	+ To abort the timeout packet(s)
61102fcbc377Syt  *
61112fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function
611268d33a25Syt  * is called. And ahciport_mutex will be released before the reset
611368d33a25Syt  * event is reported to sata module by calling sata_hba_event_notify,
611468d33a25Syt  * and then be acquired again later.
611582263d52Syt  *
611682263d52Syt  * NOTES!!! During this procedure, PxSERR register will be cleared, and
611782263d52Syt  * according to the spec, the clearance of three bits will also clear
611882263d52Syt  * three interrupt status bits.
611982263d52Syt  *	1. PxSERR.DIAG.F will clear PxIS.UFS
612082263d52Syt  *	2. PxSERR.DIAG.X will clear PxIS.PCS
612182263d52Syt  *	3. PxSERR.DIAG.N will clear PxIS.PRCS
612282263d52Syt  *
612382263d52Syt  * Among these three interrupt events, the driver needs to take care of
612482263d52Syt  * PxIS.PRCS, which is the hot plug event. When the driver found out
612582263d52Syt  * a device was unplugged, it will call the interrupt handler.
61262fcbc377Syt  */
61272fcbc377Syt static int
61282fcbc377Syt ahci_restart_port_wait_till_ready(ahci_ctl_t *ahci_ctlp,
612968d33a25Syt     ahci_port_t *ahci_portp, uint8_t port, int flag, int *reset_flag)
61302fcbc377Syt {
613182263d52Syt 	uint32_t port_sstatus;
61322fcbc377Syt 	uint32_t task_file_status;
61332fcbc377Syt 	sata_device_t sdevice;
61342fcbc377Syt 	int rval;
613582263d52Syt 	int dev_exists_begin = 0;
613682263d52Syt 	int dev_exists_end = 0;
61372fcbc377Syt 
61382fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
61392fcbc377Syt 	    "ahci_restart_port_wait_till_ready: port %d enter", port);
61402fcbc377Syt 
6141*0a4c4cecSXiao-Yu Zhang 	if (ahci_portp->ahciport_device_type != SATA_DTYPE_NONE)
614282263d52Syt 		dev_exists_begin = 1;
61432fcbc377Syt 
614482263d52Syt 	/* First clear PxCMD.ST */
614568d33a25Syt 	rval = ahci_put_port_into_notrunning_state(ahci_ctlp, ahci_portp,
614668d33a25Syt 	    port);
614768d33a25Syt 	if (rval != AHCI_SUCCESS)
614868d33a25Syt 		/*
614968d33a25Syt 		 * If PxCMD.CR does not clear within a reasonable time, it
615068d33a25Syt 		 * may assume the interface is in a hung condition and may
615168d33a25Syt 		 * continue with issuing the port reset.
615268d33a25Syt 		 */
615368d33a25Syt 		goto reset;
61542fcbc377Syt 
615568d33a25Syt 	/* Then clear PxSERR */
615668d33a25Syt 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
615768d33a25Syt 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
615868d33a25Syt 	    AHCI_SERROR_CLEAR_ALL);
61592fcbc377Syt 
616082263d52Syt 	/* Then get PxTFD */
616168d33a25Syt 	task_file_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
616268d33a25Syt 	    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
61632fcbc377Syt 
616468d33a25Syt 	/*
616568d33a25Syt 	 * Check whether the device is in a stable status, if yes,
616668d33a25Syt 	 * then start the port directly. However for ahci_tran_dport_reset,
616768d33a25Syt 	 * we may have to perform a port reset.
616868d33a25Syt 	 */
616968d33a25Syt 	if (!(task_file_status & (AHCI_TFD_STS_BSY | AHCI_TFD_STS_DRQ)) &&
617068d33a25Syt 	    !(flag & AHCI_PORT_RESET))
61712fcbc377Syt 		goto out;
61722fcbc377Syt 
617368d33a25Syt reset:
617468d33a25Syt 	/*
617568d33a25Syt 	 * If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to '1', then issue
617668d33a25Syt 	 * a COMRESET to the device
617768d33a25Syt 	 */
61782fcbc377Syt 	rval = ahci_port_reset(ahci_ctlp, ahci_portp, port);
617968d33a25Syt 
618068d33a25Syt 	if (reset_flag != NULL)
618168d33a25Syt 		*reset_flag = 1;
61822fcbc377Syt 
61832fcbc377Syt 	/* Indicate to the framework that a reset has happened. */
618482263d52Syt 	if ((ahci_portp->ahciport_device_type != SATA_DTYPE_NONE) &&
618582263d52Syt 	    !(flag & AHCI_RESET_NO_EVENTS_UP)) {
618682263d52Syt 		/* Set the reset in progress flag */
618782263d52Syt 		ahci_portp->ahciport_reset_in_progress = 1;
61882fcbc377Syt 
61892fcbc377Syt 		bzero((void *)&sdevice, sizeof (sata_device_t));
619009121340Syt 		sdevice.satadev_addr.cport =
619109121340Syt 		    ahci_ctlp->ahcictl_port_to_cport[port];
619268d33a25Syt 		sdevice.satadev_addr.pmport = 0;
619368d33a25Syt 		sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
61942fcbc377Syt 
61952fcbc377Syt 		sdevice.satadev_state = SATA_DSTATE_RESET |
61962fcbc377Syt 		    SATA_DSTATE_PWR_ACTIVE;
61972fcbc377Syt 		if (ahci_ctlp->ahcictl_sata_hba_tran) {
61982fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
61992fcbc377Syt 			sata_hba_event_notify(
62002fcbc377Syt 			    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
62012fcbc377Syt 			    &sdevice,
62022fcbc377Syt 			    SATA_EVNT_DEVICE_RESET);
62032fcbc377Syt 			mutex_enter(&ahci_portp->ahciport_mutex);
62042fcbc377Syt 		}
62052fcbc377Syt 
620668d33a25Syt 		AHCIDBG1(AHCIDBG_EVENT, ahci_ctlp,
620768d33a25Syt 		    "port %d sending event up: SATA_EVNT_RESET", port);
620882263d52Syt 	} else {
620982263d52Syt 		ahci_portp->ahciport_reset_in_progress = 0;
62102fcbc377Syt 	}
621182263d52Syt 
62122fcbc377Syt out:
6213a9440e8dSyt 	(void) ahci_start_port(ahci_ctlp, ahci_portp, port);
621482263d52Syt 
621582263d52Syt 	/* SStatus tells the presence of device. */
621682263d52Syt 	port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
621782263d52Syt 	    (uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
621882263d52Syt 
621982263d52Syt 	if (SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_PHYCOM) {
622082263d52Syt 		dev_exists_end = 1;
622182263d52Syt 		ASSERT(ahci_portp->ahciport_device_type != SATA_DTYPE_NONE);
622282263d52Syt 	}
622382263d52Syt 
622482263d52Syt 	/* Check whether a hot plug event happened */
622582263d52Syt 	if (dev_exists_begin == 1 && dev_exists_end == 0) {
622682263d52Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
622782263d52Syt 		    "ahci_restart_port_wait_till_ready: port %d "
622882263d52Syt 		    "device is removed", port);
622982263d52Syt 		ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_NODEV;
623082263d52Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
623182263d52Syt 		    "ahci_restart_port_wait_till_ready: port %d "
623282263d52Syt 		    "AHCI_PORT_FLAG_NODEV flag is set", port);
623382263d52Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
623482263d52Syt 		(void) ahci_intr_phyrdy_change(ahci_ctlp, ahci_portp, port);
623582263d52Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
62362fcbc377Syt 	}
62372fcbc377Syt 
62382fcbc377Syt 	return (rval);
62392fcbc377Syt }
62402fcbc377Syt 
62412fcbc377Syt /*
624268d33a25Syt  * This routine may be called under four scenarios:
624368d33a25Syt  *	a) do the recovery from fatal error
62442fcbc377Syt  *	b) or we need to timeout some commands
62452fcbc377Syt  *	c) or we need to abort some commands
62462fcbc377Syt  *	d) or we need reset device/port/controller
62472fcbc377Syt  *
62482fcbc377Syt  * In all these scenarios, we need to send any pending unfinished
62492fcbc377Syt  * commands up to sata framework.
62502fcbc377Syt  *
625168d33a25Syt  * WARNING!!! ahciport_mutex should be acquired before the function is called.
62522fcbc377Syt  */
62532fcbc377Syt static void
62542fcbc377Syt ahci_mop_commands(ahci_ctl_t *ahci_ctlp,
62552fcbc377Syt     ahci_port_t *ahci_portp,
62562fcbc377Syt     uint32_t slot_status,
62572fcbc377Syt     uint32_t failed_tags,
62582fcbc377Syt     uint32_t timeout_tags,
62592fcbc377Syt     uint32_t aborted_tags,
62602fcbc377Syt     uint32_t reset_tags)
62612fcbc377Syt {
626282263d52Syt 	uint32_t finished_tags = 0;
626382263d52Syt 	uint32_t unfinished_tags = 0;
62642fcbc377Syt 	int tmp_slot;
62652fcbc377Syt 	sata_pkt_t *satapkt;
626682263d52Syt 	int ncq_cmd_in_progress = 0;
626782263d52Syt 	int err_retri_cmd_in_progress = 0;
62682fcbc377Syt 
62692fcbc377Syt 	AHCIDBG2(AHCIDBG_ERRS|AHCIDBG_ENTRY, ahci_ctlp,
62702fcbc377Syt 	    "ahci_mop_commands entered: port: %d slot_status: 0x%x",
6271689d74b0Syt 	    ahci_portp->ahciport_port_num, slot_status);
62722fcbc377Syt 
62732fcbc377Syt 	AHCIDBG4(AHCIDBG_ERRS|AHCIDBG_ENTRY, ahci_ctlp,
62742fcbc377Syt 	    "ahci_mop_commands: failed_tags: 0x%x, "
62752fcbc377Syt 	    "timeout_tags: 0x%x aborted_tags: 0x%x, "
62762fcbc377Syt 	    "reset_tags: 0x%x", failed_tags,
62772fcbc377Syt 	    timeout_tags, aborted_tags, reset_tags);
62782fcbc377Syt 
627982263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
628082263d52Syt 		finished_tags = ahci_portp->ahciport_pending_tags &
628182263d52Syt 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
62822fcbc377Syt 
628382263d52Syt 		unfinished_tags = slot_status &
628482263d52Syt 		    AHCI_SLOT_MASK(ahci_ctlp) &
628582263d52Syt 		    ~failed_tags &
628682263d52Syt 		    ~aborted_tags &
628782263d52Syt 		    ~reset_tags &
628882263d52Syt 		    ~timeout_tags;
6289a9440e8dSyt 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
629082263d52Syt 		ncq_cmd_in_progress = 1;
629182263d52Syt 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
629282263d52Syt 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
629382263d52Syt 
629482263d52Syt 		unfinished_tags = slot_status &
629582263d52Syt 		    AHCI_NCQ_SLOT_MASK(ahci_portp) &
629682263d52Syt 		    ~failed_tags &
629782263d52Syt 		    ~aborted_tags &
629882263d52Syt 		    ~reset_tags &
629982263d52Syt 		    ~timeout_tags;
6300a9440e8dSyt 	} else if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
63012fcbc377Syt 
6302a9440e8dSyt 		/*
6303a9440e8dSyt 		 * When AHCI_PORT_FLAG_RQSENSE or AHCI_PORT_FLAG_RDLOGEXT is
6304a9440e8dSyt 		 * set, it means REQUEST SENSE or READ LOG EXT command doesn't
6305a9440e8dSyt 		 * complete successfully due to one of the following three
6306a9440e8dSyt 		 * conditions:
6307a9440e8dSyt 		 *
6308a9440e8dSyt 		 *	1. Fatal error - failed_tags includes its slot
6309a9440e8dSyt 		 *	2. Timed out - timeout_tags includes its slot
6310a9440e8dSyt 		 *	3. Aborted when hot unplug - aborted_tags includes its
6311a9440e8dSyt 		 *	   slot
6312a9440e8dSyt 		 *
6313a9440e8dSyt 		 * Please note that the command is always sent down in Slot 0
6314a9440e8dSyt 		 */
631582263d52Syt 		err_retri_cmd_in_progress = 1;
6316f8a673adSying tian - Beijing China 		AHCIDBG2(AHCIDBG_ERRS|AHCIDBG_NCQ, ahci_ctlp,
631782263d52Syt 		    "ahci_mop_commands is called for port %d while "
631882263d52Syt 		    "REQUEST SENSE or READ LOG EXT for error retrieval "
6319f8a673adSying tian - Beijing China 		    "is being executed slot_status = 0x%x",
6320f8a673adSying tian - Beijing China 		    ahci_portp->ahciport_port_num, slot_status);
632168d33a25Syt 		ASSERT(ahci_portp->ahciport_mop_in_progress > 1);
632282263d52Syt 		ASSERT(slot_status == 0x1);
63232fcbc377Syt 	}
63242fcbc377Syt 
632568d33a25Syt 	/* Send up finished packets with SATA_PKT_COMPLETED */
63262fcbc377Syt 	while (finished_tags) {
63272fcbc377Syt 		tmp_slot = ddi_ffs(finished_tags) - 1;
63282fcbc377Syt 		if (tmp_slot == -1) {
63292fcbc377Syt 			break;
63302fcbc377Syt 		}
63312fcbc377Syt 
63322fcbc377Syt 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
63332fcbc377Syt 		ASSERT(satapkt != NULL);
63342fcbc377Syt 
63352fcbc377Syt 		AHCIDBG1(AHCIDBG_INFO, ahci_ctlp, "ahci_mop_commands: "
63362fcbc377Syt 		    "sending up pkt 0x%p with SATA_PKT_COMPLETED",
63372fcbc377Syt 		    (void *)satapkt);
63382fcbc377Syt 
633968d33a25Syt 		/*
634068d33a25Syt 		 * Cannot fetch the return register content since the port
634168d33a25Syt 		 * was restarted, so the corresponding tag will be set to
634268d33a25Syt 		 * aborted tags.
634368d33a25Syt 		 */
634468d33a25Syt 		if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs) {
634568d33a25Syt 			CLEAR_BIT(finished_tags, tmp_slot);
634668d33a25Syt 			aborted_tags |= tmp_slot;
634768d33a25Syt 			continue;
634868d33a25Syt 		}
634968d33a25Syt 
635082263d52Syt 		if (ncq_cmd_in_progress)
635182263d52Syt 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
635282263d52Syt 			    tmp_slot);
63532fcbc377Syt 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
63542fcbc377Syt 		CLEAR_BIT(finished_tags, tmp_slot);
63552fcbc377Syt 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
63562fcbc377Syt 
63572fcbc377Syt 		SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_COMPLETED);
63582fcbc377Syt 	}
63592fcbc377Syt 
636068d33a25Syt 	/* Send up failed packets with SATA_PKT_DEV_ERROR. */
63612fcbc377Syt 	while (failed_tags) {
636282263d52Syt 		if (err_retri_cmd_in_progress) {
636382263d52Syt 			satapkt = ahci_portp->ahciport_err_retri_pkt;
636482263d52Syt 			ASSERT(satapkt != NULL);
636582263d52Syt 			ASSERT(failed_tags == 0x1);
636682263d52Syt 
636782263d52Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
636882263d52Syt 			    "sending up pkt 0x%p with SATA_PKT_DEV_ERROR",
636982263d52Syt 			    (void *)satapkt);
637082263d52Syt 			SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_DEV_ERROR);
637182263d52Syt 			break;
637282263d52Syt 		}
637382263d52Syt 
63742fcbc377Syt 		tmp_slot = ddi_ffs(failed_tags) - 1;
63752fcbc377Syt 		if (tmp_slot == -1) {
63762fcbc377Syt 			break;
63772fcbc377Syt 		}
63782fcbc377Syt 
63792fcbc377Syt 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
63802fcbc377Syt 		ASSERT(satapkt != NULL);
63812fcbc377Syt 
63822fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
63832fcbc377Syt 		    "sending up pkt 0x%p with SATA_PKT_DEV_ERROR",
63842fcbc377Syt 		    (void *)satapkt);
63852fcbc377Syt 
638682263d52Syt 		if (ncq_cmd_in_progress)
638782263d52Syt 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
638882263d52Syt 			    tmp_slot);
63892fcbc377Syt 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
63902fcbc377Syt 		CLEAR_BIT(failed_tags, tmp_slot);
63912fcbc377Syt 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
63922fcbc377Syt 
63932fcbc377Syt 		SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_DEV_ERROR);
63942fcbc377Syt 	}
63952fcbc377Syt 
639668d33a25Syt 	/* Send up timeout packets with SATA_PKT_TIMEOUT. */
63972fcbc377Syt 	while (timeout_tags) {
639882263d52Syt 		if (err_retri_cmd_in_progress) {
639982263d52Syt 			satapkt = ahci_portp->ahciport_err_retri_pkt;
640082263d52Syt 			ASSERT(satapkt != NULL);
640182263d52Syt 			ASSERT(timeout_tags == 0x1);
640282263d52Syt 
640382263d52Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
640482263d52Syt 			    "sending up pkt 0x%p with SATA_PKT_TIMEOUT",
640582263d52Syt 			    (void *)satapkt);
640682263d52Syt 			SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_TIMEOUT);
640782263d52Syt 			break;
640882263d52Syt 		}
640982263d52Syt 
64102fcbc377Syt 		tmp_slot = ddi_ffs(timeout_tags) - 1;
64112fcbc377Syt 		if (tmp_slot == -1) {
64122fcbc377Syt 			break;
64132fcbc377Syt 		}
64142fcbc377Syt 
64152fcbc377Syt 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
64162fcbc377Syt 		ASSERT(satapkt != NULL);
64172fcbc377Syt 
64182fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
64192fcbc377Syt 		    "sending up pkt 0x%p with SATA_PKT_TIMEOUT",
64202fcbc377Syt 		    (void *)satapkt);
64212fcbc377Syt 
642282263d52Syt 		if (ncq_cmd_in_progress)
642382263d52Syt 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
642482263d52Syt 			    tmp_slot);
64252fcbc377Syt 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
64262fcbc377Syt 		CLEAR_BIT(timeout_tags, tmp_slot);
64272fcbc377Syt 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
64282fcbc377Syt 
64292fcbc377Syt 		SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_TIMEOUT);
64302fcbc377Syt 	}
64312fcbc377Syt 
643268d33a25Syt 	/* Send up aborted packets with SATA_PKT_ABORTED */
64332fcbc377Syt 	while (aborted_tags) {
643482263d52Syt 		if (err_retri_cmd_in_progress) {
643582263d52Syt 			satapkt = ahci_portp->ahciport_err_retri_pkt;
643682263d52Syt 			ASSERT(satapkt != NULL);
643782263d52Syt 			ASSERT(aborted_tags == 0x1);
643882263d52Syt 
643982263d52Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
644082263d52Syt 			    "sending up pkt 0x%p with SATA_PKT_ABORTED",
644182263d52Syt 			    (void *)satapkt);
644282263d52Syt 			SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_ABORTED);
644382263d52Syt 			break;
644482263d52Syt 		}
644582263d52Syt 
64462fcbc377Syt 		tmp_slot = ddi_ffs(aborted_tags) - 1;
64472fcbc377Syt 		if (tmp_slot == -1) {
64482fcbc377Syt 			break;
64492fcbc377Syt 		}
64502fcbc377Syt 
64512fcbc377Syt 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
64522fcbc377Syt 		ASSERT(satapkt != NULL);
64532fcbc377Syt 
64542fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
64552fcbc377Syt 		    "sending up pkt 0x%p with SATA_PKT_ABORTED",
64562fcbc377Syt 		    (void *)satapkt);
64572fcbc377Syt 
645882263d52Syt 		if (ncq_cmd_in_progress)
645982263d52Syt 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
646082263d52Syt 			    tmp_slot);
64612fcbc377Syt 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
64622fcbc377Syt 		CLEAR_BIT(aborted_tags, tmp_slot);
64632fcbc377Syt 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
64642fcbc377Syt 
64652fcbc377Syt 		SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_ABORTED);
64662fcbc377Syt 	}
64672fcbc377Syt 
646868d33a25Syt 	/* Send up reset packets with SATA_PKT_RESET. */
64692fcbc377Syt 	while (reset_tags) {
64702fcbc377Syt 		tmp_slot = ddi_ffs(reset_tags) - 1;
64712fcbc377Syt 		if (tmp_slot == -1) {
64722fcbc377Syt 			break;
64732fcbc377Syt 		}
64742fcbc377Syt 
64752fcbc377Syt 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
64762fcbc377Syt 		ASSERT(satapkt != NULL);
64772fcbc377Syt 
64782fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
64792fcbc377Syt 		    "sending up pkt 0x%p with SATA_PKT_RESET",
64802fcbc377Syt 		    (void *)satapkt);
64812fcbc377Syt 
648282263d52Syt 		if (ncq_cmd_in_progress)
648382263d52Syt 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
648482263d52Syt 			    tmp_slot);
64852fcbc377Syt 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
64862fcbc377Syt 		CLEAR_BIT(reset_tags, tmp_slot);
64872fcbc377Syt 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
64882fcbc377Syt 
64892fcbc377Syt 		SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_RESET);
64902fcbc377Syt 	}
64912fcbc377Syt 
649268d33a25Syt 	/* Send up unfinished packets with SATA_PKT_RESET */
64932fcbc377Syt 	while (unfinished_tags) {
64942fcbc377Syt 		tmp_slot = ddi_ffs(unfinished_tags) - 1;
64952fcbc377Syt 		if (tmp_slot == -1) {
64962fcbc377Syt 			break;
64972fcbc377Syt 		}
64982fcbc377Syt 
64992fcbc377Syt 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
65002fcbc377Syt 		ASSERT(satapkt != NULL);
65012fcbc377Syt 
65022fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
650368d33a25Syt 		    "sending up pkt 0x%p with SATA_PKT_RESET",
65042fcbc377Syt 		    (void *)satapkt);
65052fcbc377Syt 
650682263d52Syt 		if (ncq_cmd_in_progress)
650782263d52Syt 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
650882263d52Syt 			    tmp_slot);
65092fcbc377Syt 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
65102fcbc377Syt 		CLEAR_BIT(unfinished_tags, tmp_slot);
65112fcbc377Syt 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
65122fcbc377Syt 
651368d33a25Syt 		SENDUP_PACKET(ahci_portp, satapkt, SATA_PKT_RESET);
65142fcbc377Syt 	}
65152fcbc377Syt 
651668d33a25Syt 	ahci_portp->ahciport_mop_in_progress--;
651768d33a25Syt 	ASSERT(ahci_portp->ahciport_mop_in_progress >= 0);
65182fcbc377Syt 
651968d33a25Syt 	if (ahci_portp->ahciport_mop_in_progress == 0)
652068d33a25Syt 		ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_MOPPING;
652168d33a25Syt }
65222fcbc377Syt 
652382263d52Syt /*
652482263d52Syt  * This routine is going to first request a READ LOG EXT sata pkt from sata
652582263d52Syt  * module, and then deliver it to the HBA to get the ncq failure context.
652682263d52Syt  * The return value is the exactly failed tags.
652782263d52Syt  */
652882263d52Syt static uint32_t
652982263d52Syt ahci_get_rdlogext_data(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
653082263d52Syt     uint8_t port)
653182263d52Syt {
653282263d52Syt 	sata_device_t	sdevice;
653382263d52Syt 	sata_pkt_t	*rdlog_spkt, *spkt;
653482263d52Syt 	ddi_dma_handle_t buf_dma_handle;
653582263d52Syt 	int		loop_count;
653682263d52Syt 	int		rval;
653782263d52Syt 	int		failed_slot;
653882263d52Syt 	uint32_t	failed_tags = 0;
653982263d52Syt 	struct sata_ncq_error_recovery_page *ncq_err_page;
654082263d52Syt 
654182263d52Syt 	AHCIDBG1(AHCIDBG_ENTRY|AHCIDBG_NCQ, ahci_ctlp,
654282263d52Syt 	    "ahci_get_rdlogext_data enter: port %d", port);
654382263d52Syt 
654482263d52Syt 	/* Prepare the sdevice data */
654582263d52Syt 	bzero((void *)&sdevice, sizeof (sata_device_t));
654682263d52Syt 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
654782263d52Syt 
654882263d52Syt 	sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
654982263d52Syt 	sdevice.satadev_addr.pmport = 0;
655082263d52Syt 
655182263d52Syt 	/*
655282263d52Syt 	 * Call the sata hba interface to get a rdlog spkt
655382263d52Syt 	 */
655482263d52Syt 	loop_count = 0;
655582263d52Syt loop:
655682263d52Syt 	rdlog_spkt = sata_get_error_retrieval_pkt(ahci_ctlp->ahcictl_dip,
655782263d52Syt 	    &sdevice, SATA_ERR_RETR_PKT_TYPE_NCQ);
655882263d52Syt 	if (rdlog_spkt == NULL) {
655982263d52Syt 		if (loop_count++ < AHCI_POLLRATE_GET_SPKT) {
656082263d52Syt 			/* Sleep for a while */
656182263d52Syt 			delay(AHCI_10MS_TICKS);
656282263d52Syt 			goto loop;
656382263d52Syt 		}
656482263d52Syt 		/* Timed out after 1s */
656582263d52Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
656682263d52Syt 		    "failed to get rdlog spkt for port %d", port);
656782263d52Syt 		return (failed_tags);
656882263d52Syt 	}
656982263d52Syt 
657082263d52Syt 	ASSERT(rdlog_spkt->satapkt_op_mode & SATA_OPMODE_SYNCH);
657182263d52Syt 
657282263d52Syt 	/*
657382263d52Syt 	 * This flag is used to handle the specific error recovery when the
657482263d52Syt 	 * READ LOG EXT command gets a failure (fatal error or time-out).
657582263d52Syt 	 */
657682263d52Syt 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_RDLOGEXT;
657782263d52Syt 
657882263d52Syt 	/*
657982263d52Syt 	 * This start is not supposed to fail because after port is restarted,
658082263d52Syt 	 * the whole command list is empty.
658182263d52Syt 	 */
658282263d52Syt 	ahci_portp->ahciport_err_retri_pkt = rdlog_spkt;
658382263d52Syt 	(void) ahci_do_sync_start(ahci_ctlp, ahci_portp, port, rdlog_spkt);
658482263d52Syt 	ahci_portp->ahciport_err_retri_pkt = NULL;
658582263d52Syt 
658682263d52Syt 	/* Remove the flag after READ LOG EXT command is completed */
658782263d52Syt 	ahci_portp->ahciport_flags &= ~ AHCI_PORT_FLAG_RDLOGEXT;
658882263d52Syt 
658982263d52Syt 	if (rdlog_spkt->satapkt_reason == SATA_PKT_COMPLETED) {
659082263d52Syt 		/* Update the request log data */
659182263d52Syt 		buf_dma_handle = *(ddi_dma_handle_t *)
659282263d52Syt 		    (rdlog_spkt->satapkt_cmd.satacmd_err_ret_buf_handle);
659382263d52Syt 		rval = ddi_dma_sync(buf_dma_handle, 0, 0,
659482263d52Syt 		    DDI_DMA_SYNC_FORKERNEL);
659582263d52Syt 		if (rval == DDI_SUCCESS) {
659682263d52Syt 			ncq_err_page =
659782263d52Syt 			    (struct sata_ncq_error_recovery_page *)rdlog_spkt->
659882263d52Syt 			    satapkt_cmd.satacmd_bp->b_un.b_addr;
659982263d52Syt 
660082263d52Syt 			/* Get the failed tag */
660182263d52Syt 			failed_slot = ncq_err_page->ncq_tag;
660282263d52Syt 			AHCIDBG2(AHCIDBG_ERRS, ahci_ctlp,
660382263d52Syt 			    "ahci_get_rdlogext_data: port %d "
660482263d52Syt 			    "failed slot %d", port, failed_slot);
660582263d52Syt 			if (failed_slot & NQ) {
660682263d52Syt 				AHCIDBG0(AHCIDBG_ERRS, ahci_ctlp,
660782263d52Syt 				    "the failed slot is not a valid tag");
660882263d52Syt 				goto out;
660982263d52Syt 			}
661082263d52Syt 
661182263d52Syt 			failed_slot &= NCQ_TAG_MASK;
661282263d52Syt 			spkt = ahci_portp->ahciport_slot_pkts[failed_slot];
661382263d52Syt 			AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
661482263d52Syt 			    "ahci_get_rdlogext_data: failed spkt 0x%p",
661582263d52Syt 			    (void *)spkt);
661682263d52Syt 			if (spkt == NULL) {
661782263d52Syt 				AHCIDBG0(AHCIDBG_ERRS, ahci_ctlp,
661882263d52Syt 				    "the failed slot spkt is NULL")
661982263d52Syt 				goto out;
662082263d52Syt 			}
662182263d52Syt 
662282263d52Syt 			failed_tags = 0x1 << failed_slot;
662382263d52Syt 
662482263d52Syt 			/* Fill out the error context */
662582263d52Syt 			ahci_copy_ncq_err_page(&spkt->satapkt_cmd,
662682263d52Syt 			    ncq_err_page);
662782263d52Syt 			ahci_update_sata_registers(ahci_ctlp, port,
662882263d52Syt 			    &spkt->satapkt_device);
662982263d52Syt 		}
663082263d52Syt 	}
663182263d52Syt out:
663282263d52Syt 	sata_free_error_retrieval_pkt(rdlog_spkt);
663382263d52Syt 
663482263d52Syt 	return (failed_tags);
663582263d52Syt }
663682263d52Syt 
663768d33a25Syt /*
663868d33a25Syt  * This routine is going to first request a REQUEST SENSE sata pkt from sata
663968d33a25Syt  * module, and then deliver it to the HBA to get the sense data and copy
664068d33a25Syt  * the sense data back to the orignal failed sata pkt, and free the REQUEST
664168d33a25Syt  * SENSE sata pkt later.
664268d33a25Syt  */
664368d33a25Syt static void
664468d33a25Syt ahci_get_rqsense_data(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
664568d33a25Syt     uint8_t port, sata_pkt_t *spkt)
664668d33a25Syt {
664768d33a25Syt 	sata_device_t	sdevice;
664868d33a25Syt 	sata_pkt_t	*rs_spkt;
664968d33a25Syt 	sata_cmd_t	*sata_cmd;
665068d33a25Syt 	ddi_dma_handle_t buf_dma_handle;
665168d33a25Syt 	int		loop_count;
665268d33a25Syt #if AHCI_DEBUG
665368d33a25Syt 	struct scsi_extended_sense *rqsense;
665468d33a25Syt #endif
665568d33a25Syt 
665668d33a25Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
665768d33a25Syt 	    "ahci_get_rqsense_data enter: port %d", port);
665868d33a25Syt 
665968d33a25Syt 	/* Prepare the sdevice data */
666068d33a25Syt 	bzero((void *)&sdevice, sizeof (sata_device_t));
666168d33a25Syt 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
666268d33a25Syt 
666368d33a25Syt 	sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
666468d33a25Syt 	sdevice.satadev_addr.pmport = 0;
666568d33a25Syt 
666668d33a25Syt 	sata_cmd = &spkt->satapkt_cmd;
666768d33a25Syt 
666868d33a25Syt 	/*
666968d33a25Syt 	 * Call the sata hba interface to get a rs spkt
667068d33a25Syt 	 */
667168d33a25Syt 	loop_count = 0;
667268d33a25Syt loop:
667368d33a25Syt 	rs_spkt = sata_get_error_retrieval_pkt(ahci_ctlp->ahcictl_dip,
667468d33a25Syt 	    &sdevice, SATA_ERR_RETR_PKT_TYPE_ATAPI);
667568d33a25Syt 	if (rs_spkt == NULL) {
667668d33a25Syt 		if (loop_count++ < AHCI_POLLRATE_GET_SPKT) {
667768d33a25Syt 			/* Sleep for a while */
667868d33a25Syt 			delay(AHCI_10MS_TICKS);
667968d33a25Syt 			goto loop;
668068d33a25Syt 
668168d33a25Syt 		}
668268d33a25Syt 		/* Timed out after 1s */
668382263d52Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
668468d33a25Syt 		    "failed to get rs spkt for port %d", port);
668568d33a25Syt 		return;
668668d33a25Syt 	}
668768d33a25Syt 
668868d33a25Syt 	ASSERT(rs_spkt->satapkt_op_mode & SATA_OPMODE_SYNCH);
668968d33a25Syt 
669068d33a25Syt 	/*
669168d33a25Syt 	 * This flag is used to handle the specific error recovery when the
669282263d52Syt 	 * REQUEST SENSE command gets a faiure (fatal error or time-out).
669368d33a25Syt 	 */
669468d33a25Syt 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_RQSENSE;
669568d33a25Syt 
669668d33a25Syt 	/*
669768d33a25Syt 	 * This start is not supposed to fail because after port is restarted,
669882263d52Syt 	 * the whole command list is empty.
669968d33a25Syt 	 */
670082263d52Syt 	ahci_portp->ahciport_err_retri_pkt = rs_spkt;
670182263d52Syt 	(void) ahci_do_sync_start(ahci_ctlp, ahci_portp, port, rs_spkt);
670282263d52Syt 	ahci_portp->ahciport_err_retri_pkt = NULL;
670368d33a25Syt 
670468d33a25Syt 	/* Remove the flag after REQUEST SENSE command is completed */
670568d33a25Syt 	ahci_portp->ahciport_flags &= ~ AHCI_PORT_FLAG_RQSENSE;
670668d33a25Syt 
670768d33a25Syt 	if (rs_spkt->satapkt_reason == SATA_PKT_COMPLETED) {
670868d33a25Syt 		/* Update the request sense data */
670968d33a25Syt 		buf_dma_handle = *(ddi_dma_handle_t *)
671068d33a25Syt 		    (rs_spkt->satapkt_cmd.satacmd_err_ret_buf_handle);
671182263d52Syt 		(void) ddi_dma_sync(buf_dma_handle, 0, 0,
671268d33a25Syt 		    DDI_DMA_SYNC_FORKERNEL);
671382263d52Syt 		/* Copy the request sense data */
671482263d52Syt 		bcopy(rs_spkt->
671582263d52Syt 		    satapkt_cmd.satacmd_bp->b_un.b_addr,
671682263d52Syt 		    &sata_cmd->satacmd_rqsense,
671782263d52Syt 		    SATA_ATAPI_MIN_RQSENSE_LEN);
671868d33a25Syt #if AHCI_DEBUG
671982263d52Syt 		rqsense = (struct scsi_extended_sense *)
672082263d52Syt 		    sata_cmd->satacmd_rqsense;
672182263d52Syt 
672282263d52Syt 		/* Dump the sense data */
672382263d52Syt 		AHCIDBG0(AHCIDBG_SENSEDATA, ahci_ctlp, "\n");
672482263d52Syt 		AHCIDBG2(AHCIDBG_SENSEDATA, ahci_ctlp,
672582263d52Syt 		    "Sense data for satapkt %p ATAPI cmd 0x%x",
672682263d52Syt 		    spkt, sata_cmd->satacmd_acdb[0]);
672782263d52Syt 		AHCIDBG5(AHCIDBG_SENSEDATA, ahci_ctlp,
672882263d52Syt 		    "  es_code 0x%x es_class 0x%x "
672982263d52Syt 		    "es_key 0x%x es_add_code 0x%x "
673082263d52Syt 		    "es_qual_code 0x%x",
673182263d52Syt 		    rqsense->es_code, rqsense->es_class,
673282263d52Syt 		    rqsense->es_key, rqsense->es_add_code,
673382263d52Syt 		    rqsense->es_qual_code);
673468d33a25Syt #endif
673568d33a25Syt 	}
673668d33a25Syt 
673768d33a25Syt 	sata_free_error_retrieval_pkt(rs_spkt);
67382fcbc377Syt }
67392fcbc377Syt 
67402fcbc377Syt /*
67412fcbc377Syt  * Fatal errors will cause the HBA to enter the ERR: Fatal state. To recover,
674268d33a25Syt  * the port must be restarted. When the HBA detects thus error, it may try
674368d33a25Syt  * to abort a transfer. And if the transfer was aborted, the device is
674468d33a25Syt  * expected to send a D2H Register FIS with PxTFD.STS.ERR set to '1' and both
674568d33a25Syt  * PxTFD.STS.BSY and PxTFD.STS.DRQ cleared to '0'. Then system software knows
674668d33a25Syt  * that the device is in a stable status and transfers may be restarted without
674768d33a25Syt  * issuing a COMRESET to the device. If PxTFD.STS.BSY or PxTFD.STS.DRQ is set,
674868d33a25Syt  * then the software will send the COMRESET to do the port reset.
674968d33a25Syt  *
675068d33a25Syt  * Software should perform the appropriate error recovery actions based on
675168d33a25Syt  * whether non-queued commands were being issued or natived command queuing
675268d33a25Syt  * commands were being issued.
675368d33a25Syt  *
675468d33a25Syt  * And software will complete the command that had the error with error mark
675568d33a25Syt  * to higher level software.
67562fcbc377Syt  *
67572fcbc377Syt  * Fatal errors include the following:
675838547057Sying tian - Beijing China  *	PxIS.IFS - Interface Fatal Error Status
675938547057Sying tian - Beijing China  *	PxIS.HBDS - Host Bus Data Error Status
676038547057Sying tian - Beijing China  *	PxIS.HBFS - Host Bus Fatal Error Status
67612fcbc377Syt  *	PxIS.TFES - Task File Error Status
67622fcbc377Syt  *
67632fcbc377Syt  * WARNING!!! ahciport_mutex should be acquired before the function is called.
67642fcbc377Syt  */
676568d33a25Syt static void
676668d33a25Syt ahci_fatal_error_recovery_handler(ahci_ctl_t *ahci_ctlp,
676782263d52Syt     ahci_port_t *ahci_portp, uint8_t port, uint32_t intr_status)
67682fcbc377Syt {
676982263d52Syt 	uint32_t	port_cmd_status;
677082263d52Syt 	uint32_t	slot_status = 0;
677168d33a25Syt 	uint32_t	failed_tags = 0;
677268d33a25Syt 	int		failed_slot;
677368d33a25Syt 	int		reset_flag = 0;
677468d33a25Syt 	ahci_fis_d2h_register_t	*ahci_rcvd_fisp;
677582263d52Syt 	sata_cmd_t	*sata_cmd = NULL;
677682263d52Syt 	sata_pkt_t	*spkt = NULL;
677738547057Sying tian - Beijing China #if AHCI_DEBUG
677838547057Sying tian - Beijing China 	ahci_cmd_header_t *cmd_header;
677938547057Sying tian - Beijing China #endif
67802fcbc377Syt 
67812fcbc377Syt 	AHCIDBG1(AHCIDBG_ENTRY, ahci_ctlp,
678268d33a25Syt 	    "ahci_fatal_error_recovery_handler enter: port %d", port);
67832fcbc377Syt 
678482263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
678582263d52Syt 	    ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
67862fcbc377Syt 
678782263d52Syt 		/* Read PxCI to see which commands are still outstanding */
678882263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
678982263d52Syt 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
67902fcbc377Syt 
679182263d52Syt 		/*
679282263d52Syt 		 * Read PxCMD.CCS to determine the slot that the HBA
679382263d52Syt 		 * was processing when the error occurred.
679482263d52Syt 		 */
679582263d52Syt 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
679682263d52Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
679782263d52Syt 		failed_slot = (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
679882263d52Syt 		    AHCI_CMD_STATUS_CCS_SHIFT;
67992fcbc377Syt 
680082263d52Syt 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
680182263d52Syt 			spkt = ahci_portp->ahciport_err_retri_pkt;
680282263d52Syt 			ASSERT(spkt != NULL);
680382263d52Syt 		} else {
680482263d52Syt 			spkt = ahci_portp->ahciport_slot_pkts[failed_slot];
680582263d52Syt 			if (spkt == NULL) {
680682263d52Syt 				/* May happen when interface errors occur? */
680782263d52Syt 				goto next;
680882263d52Syt 			}
680982263d52Syt 		}
68102fcbc377Syt 
681138547057Sying tian - Beijing China #if AHCI_DEBUG
681238547057Sying tian - Beijing China 		/*
681338547057Sying tian - Beijing China 		 * Debugging purpose...
681438547057Sying tian - Beijing China 		 */
681538547057Sying tian - Beijing China 		if (ahci_portp->ahciport_prd_bytecounts[failed_slot]) {
681638547057Sying tian - Beijing China 			cmd_header =
681738547057Sying tian - Beijing China 			    &ahci_portp->ahciport_cmd_list[failed_slot];
681838547057Sying tian - Beijing China 			AHCIDBG3(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
681938547057Sying tian - Beijing China 			    "ahci_fatal_error_recovery_handler: port %d, "
682038547057Sying tian - Beijing China 			    "PRD Byte Count = 0x%x, "
682138547057Sying tian - Beijing China 			    "ahciport_prd_bytecounts = 0x%x", port,
682238547057Sying tian - Beijing China 			    cmd_header->ahcich_prd_byte_count,
682338547057Sying tian - Beijing China 			    ahci_portp->ahciport_prd_bytecounts[failed_slot]);
682438547057Sying tian - Beijing China 		}
682538547057Sying tian - Beijing China #endif
682638547057Sying tian - Beijing China 
682782263d52Syt 		sata_cmd = &spkt->satapkt_cmd;
68282fcbc377Syt 
682982263d52Syt 		/* Fill out the status and error registers for PxIS.TFES */
683082263d52Syt 		if (intr_status & AHCI_INTR_STATUS_TFES) {
683182263d52Syt 			ahci_rcvd_fisp = &(ahci_portp->ahciport_rcvd_fis->
683282263d52Syt 			    ahcirf_d2h_register_fis);
683382263d52Syt 
683482263d52Syt 			/* Copy the error context back to the sata_cmd */
683582263d52Syt 			ahci_copy_err_cnxt(sata_cmd, ahci_rcvd_fisp);
683682263d52Syt 		}
68372fcbc377Syt 
683882263d52Syt 		/* The failed command must be one of the outstanding commands */
683982263d52Syt 		failed_tags = 0x1 << failed_slot;
684082263d52Syt 		ASSERT(failed_tags & slot_status);
684182263d52Syt 
684282263d52Syt 		/* Update the sata registers, especially PxSERR register */
684382263d52Syt 		ahci_update_sata_registers(ahci_ctlp, port,
684482263d52Syt 		    &spkt->satapkt_device);
68452fcbc377Syt 
6846a9440e8dSyt 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
684782263d52Syt 		/* Read PxSACT to see which commands are still outstanding */
684882263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
684982263d52Syt 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
685082263d52Syt 	}
685168d33a25Syt next:
68522fcbc377Syt 
685368d33a25Syt #if AHCI_DEBUG
685468d33a25Syt 	/*
685582263d52Syt 	 * When AHCI_PORT_FLAG_RQSENSE or AHCI_PORT_FLAG_RDLOGEXT flag is
685682263d52Syt 	 * set, it means a fatal error happened after REQUEST SENSE command
685782263d52Syt 	 * or READ LOG EXT command is delivered to the HBA during the error
685882263d52Syt 	 * recovery process. At this time, the only outstanding command is
685982263d52Syt 	 * supposed to be REQUEST SENSE command or READ LOG EXT command.
686068d33a25Syt 	 */
686182263d52Syt 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
68622fcbc377Syt 		AHCIDBG1(AHCIDBG_ERRS, ahci_ctlp,
686368d33a25Syt 		    "ahci_fatal_error_recovery_handler: port %d REQUEST SENSE "
686482263d52Syt 		    "command or READ LOG EXT command for error data retrieval "
686582263d52Syt 		    "failed", port);
686682263d52Syt 		ASSERT(slot_status == 0x1);
6867f8a673adSying tian - Beijing China 		ASSERT(failed_slot == 0);
686882263d52Syt 		ASSERT(spkt->satapkt_cmd.satacmd_acdb[0] ==
686982263d52Syt 		    SCMD_REQUEST_SENSE ||
687082263d52Syt 		    spkt->satapkt_cmd.satacmd_cmd_reg ==
687182263d52Syt 		    SATAC_READ_LOG_EXT);
68722fcbc377Syt 	}
687368d33a25Syt #endif
68742fcbc377Syt 
687568d33a25Syt 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
687668d33a25Syt 	ahci_portp->ahciport_mop_in_progress++;
68772fcbc377Syt 
687868d33a25Syt 	(void) ahci_restart_port_wait_till_ready(ahci_ctlp, ahci_portp,
687968d33a25Syt 	    port, NULL, &reset_flag);
68802fcbc377Syt 
688168d33a25Syt 	/*
688268d33a25Syt 	 * Won't retrieve error information:
688368d33a25Syt 	 * 1. Port reset was involved to recover
688482263d52Syt 	 * 2. Device is gone
688568d33a25Syt 	 * 3. IDENTIFY DEVICE command sent to ATAPI device
688682263d52Syt 	 * 4. REQUEST SENSE or READ LOG EXT command during error recovery
688768d33a25Syt 	 */
688882263d52Syt 	if (reset_flag ||
688982263d52Syt 	    ahci_portp->ahciport_device_type == SATA_DTYPE_NONE ||
689082263d52Syt 	    spkt && spkt->satapkt_cmd.satacmd_cmd_reg == SATAC_ID_DEVICE ||
689182263d52Syt 	    ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
689268d33a25Syt 		goto out;
68932fcbc377Syt 
689482263d52Syt 	/*
689582263d52Syt 	 * Deliver READ LOG EXT to gather information about the error when
689682263d52Syt 	 * a COMRESET has not been performed as part of the error recovery
689782263d52Syt 	 * during NCQ command processing.
689882263d52Syt 	 */
689982263d52Syt 	if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
690082263d52Syt 		failed_tags = ahci_get_rdlogext_data(ahci_ctlp,
690182263d52Syt 		    ahci_portp, port);
690282263d52Syt 		goto out;
690382263d52Syt 	}
690482263d52Syt 
690568d33a25Syt 	/*
690668d33a25Syt 	 * Deliver REQUEST SENSE for ATAPI command to gather information about
690768d33a25Syt 	 * the error when a COMRESET has not been performed as part of the
690868d33a25Syt 	 * error recovery.
690968d33a25Syt 	 */
691038547057Sying tian - Beijing China 	if (spkt && ahci_portp->ahciport_device_type == SATA_DTYPE_ATAPI)
691168d33a25Syt 		ahci_get_rqsense_data(ahci_ctlp, ahci_portp, port, spkt);
691268d33a25Syt out:
691382263d52Syt 	AHCIDBG5(AHCIDBG_ERRS, ahci_ctlp,
691438547057Sying tian - Beijing China 	    "ahci_fatal_error_recovery_handler: port %d fatal error "
691582263d52Syt 	    "occurred slot_status = 0x%x, pending_tags = 0x%x, "
691682263d52Syt 	    "pending_ncq_tags = 0x%x failed_tags = 0x%x",
691782263d52Syt 	    port, slot_status, ahci_portp->ahciport_pending_tags,
691882263d52Syt 	    ahci_portp->ahciport_pending_ncq_tags, failed_tags);
691982263d52Syt 
69202fcbc377Syt 	ahci_mop_commands(ahci_ctlp,
69212fcbc377Syt 	    ahci_portp,
692282263d52Syt 	    slot_status,
69232fcbc377Syt 	    failed_tags, /* failed tags */
69242fcbc377Syt 	    0, /* timeout tags */
69252fcbc377Syt 	    0, /* aborted tags */
69262fcbc377Syt 	    0); /* reset tags */
692768d33a25Syt }
692868d33a25Syt 
692968d33a25Syt /*
693068d33a25Syt  * Handle events - fatal error recovery
693168d33a25Syt  */
693268d33a25Syt static void
693368d33a25Syt ahci_events_handler(void *args)
693468d33a25Syt {
693568d33a25Syt 	ahci_event_arg_t *ahci_event_arg;
693668d33a25Syt 	ahci_ctl_t *ahci_ctlp;
693768d33a25Syt 	ahci_port_t *ahci_portp;
693868d33a25Syt 	uint32_t event;
693968d33a25Syt 	uint8_t port;
694068d33a25Syt 
694168d33a25Syt 	ahci_event_arg = (ahci_event_arg_t *)args;
694282263d52Syt 
694368d33a25Syt 	ahci_ctlp = ahci_event_arg->ahciea_ctlp;
694468d33a25Syt 	ahci_portp = ahci_event_arg->ahciea_portp;
694568d33a25Syt 	event = ahci_event_arg->ahciea_event;
694668d33a25Syt 	port = ahci_portp->ahciport_port_num;
694768d33a25Syt 
6948f8a673adSying tian - Beijing China 	AHCIDBG2(AHCIDBG_ENTRY|AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
694982263d52Syt 	    "ahci_events_handler enter: port %d intr_status = 0x%x",
695082263d52Syt 	    port, event);
695168d33a25Syt 
69522fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
69532fcbc377Syt 
695468d33a25Syt 	/*
695568d33a25Syt 	 * ahci_intr_phyrdy_change() may have rendered it to
695668d33a25Syt 	 * SATA_DTYPE_NONE.
695768d33a25Syt 	 */
695868d33a25Syt 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
695968d33a25Syt 		AHCIDBG1(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
696068d33a25Syt 		    "ahci_events_handler: port %d no device attached, "
696168d33a25Syt 		    "and just return without doing anything", port);
696268d33a25Syt 		goto out;
696368d33a25Syt 	}
696468d33a25Syt 
696568d33a25Syt 	if (event & (AHCI_INTR_STATUS_IFS |
696668d33a25Syt 	    AHCI_INTR_STATUS_HBDS |
696768d33a25Syt 	    AHCI_INTR_STATUS_HBFS |
696868d33a25Syt 	    AHCI_INTR_STATUS_TFES))
696968d33a25Syt 		ahci_fatal_error_recovery_handler(ahci_ctlp, ahci_portp,
697082263d52Syt 		    port, event);
697168d33a25Syt 
697268d33a25Syt out:
697368d33a25Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
69742fcbc377Syt }
69752fcbc377Syt 
69762fcbc377Syt /*
697768d33a25Syt  * ahci_watchdog_handler() and ahci_do_sync_start will call us if they
697868d33a25Syt  * detect there are some commands which are timed out.
69792fcbc377Syt  */
69802fcbc377Syt static void
69812fcbc377Syt ahci_timeout_pkts(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
698282263d52Syt     uint8_t port, uint32_t tmp_timeout_tags)
69832fcbc377Syt {
698482263d52Syt 	uint32_t slot_status = 0;
698582263d52Syt 	uint32_t finished_tags = 0;
698682263d52Syt 	uint32_t timeout_tags = 0;
69872fcbc377Syt 
698868d33a25Syt 	AHCIDBG1(AHCIDBG_TIMEOUT|AHCIDBG_ENTRY, ahci_ctlp,
698968d33a25Syt 	    "ahci_timeout_pkts enter: port %d", port);
69902fcbc377Syt 
69912fcbc377Syt 	mutex_enter(&ahci_portp->ahciport_mutex);
69922fcbc377Syt 
699382263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
699482263d52Syt 	    ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
699582263d52Syt 		/* Read PxCI to see which commands are still outstanding */
699682263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
699782263d52Syt 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
6998a9440e8dSyt 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
699982263d52Syt 		/* Read PxSACT to see which commands are still outstanding */
700082263d52Syt 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
700182263d52Syt 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
700282263d52Syt 	}
700368d33a25Syt 
700468d33a25Syt #if AHCI_DEBUG
70052fcbc377Syt 	/*
700682263d52Syt 	 * When AHCI_PORT_FLAG_RQSENSE or AHCI_PORT_FLAG_RDLOGEXT flag is
700782263d52Syt 	 * set, it means a fatal error happened after REQUEST SENSE command
700882263d52Syt 	 * or READ LOG EXT command is delivered to the HBA during the error
700982263d52Syt 	 * recovery process. At this time, the only outstanding command is
701082263d52Syt 	 * supposed to be REQUEST SENSE command or READ LOG EXT command.
70112fcbc377Syt 	 */
701282263d52Syt 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
701382263d52Syt 		AHCIDBG4(AHCIDBG_ERRS|AHCIDBG_TIMEOUT, ahci_ctlp,
701468d33a25Syt 		    "ahci_timeout_pkts called while REQUEST SENSE "
701582263d52Syt 		    "command or READ LOG EXT command for error recovery "
701682263d52Syt 		    "timed out timeout_tags = 0x%x, slot_status = 0x%x, "
701782263d52Syt 		    "pending_tags = 0x%x, pending_ncq_tags = 0x%x",
701882263d52Syt 		    tmp_timeout_tags, slot_status,
701982263d52Syt 		    ahci_portp->ahciport_pending_tags,
702082263d52Syt 		    ahci_portp->ahciport_pending_ncq_tags);
702182263d52Syt 		ASSERT(slot_status == 0x1);
70222fcbc377Syt 	}
702368d33a25Syt #endif
70242fcbc377Syt 
702568d33a25Syt 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
702668d33a25Syt 	ahci_portp->ahciport_mop_in_progress++;
70272fcbc377Syt 
70282fcbc377Syt 	(void) ahci_restart_port_wait_till_ready(ahci_ctlp, ahci_portp,
702968d33a25Syt 	    port, NULL, NULL);
703068d33a25Syt 
70312fcbc377Syt 	/*
70322fcbc377Syt 	 * Re-identify timeout tags because some previously checked commands
70332fcbc377Syt 	 * could already complete.
70342fcbc377Syt 	 */
703582263d52Syt 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
703682263d52Syt 		finished_tags = ahci_portp->ahciport_pending_tags &
703782263d52Syt 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
703882263d52Syt 		timeout_tags = tmp_timeout_tags & ~finished_tags;
703982263d52Syt 
704082263d52Syt 		AHCIDBG5(AHCIDBG_TIMEOUT, ahci_ctlp,
704182263d52Syt 		    "ahci_timeout_pkts: port %d, finished_tags = 0x%x, "
704282263d52Syt 		    "timeout_tags = 0x%x, port_cmd_issue = 0x%x, "
704382263d52Syt 		    "pending_tags = 0x%x ",
704482263d52Syt 		    port, finished_tags, timeout_tags,
704582263d52Syt 		    slot_status, ahci_portp->ahciport_pending_tags);
7046a9440e8dSyt 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
704782263d52Syt 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
704882263d52Syt 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
704982263d52Syt 		timeout_tags = tmp_timeout_tags & ~finished_tags;
705082263d52Syt 
705182263d52Syt 		AHCIDBG5(AHCIDBG_TIMEOUT|AHCIDBG_NCQ, ahci_ctlp,
705282263d52Syt 		    "ahci_timeout_pkts: port %d, finished_tags = 0x%x, "
705382263d52Syt 		    "timeout_tags = 0x%x, port_sactive = 0x%x, "
705482263d52Syt 		    "pending_ncq_tags = 0x%x ",
705582263d52Syt 		    port, finished_tags, timeout_tags,
705682263d52Syt 		    slot_status, ahci_portp->ahciport_pending_ncq_tags);
7057a9440e8dSyt 	} else if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
705882263d52Syt 		timeout_tags = tmp_timeout_tags;
705982263d52Syt 	}
70602fcbc377Syt 
70612fcbc377Syt 	ahci_mop_commands(ahci_ctlp,
70622fcbc377Syt 	    ahci_portp,
706382263d52Syt 	    slot_status,
70642fcbc377Syt 	    0, /* failed tags */
70652fcbc377Syt 	    timeout_tags, /* timeout tags */
70662fcbc377Syt 	    0, /* aborted tags */
70672fcbc377Syt 	    0); /* reset tags */
706868d33a25Syt 
706968d33a25Syt 	mutex_exit(&ahci_portp->ahciport_mutex);
70702fcbc377Syt }
70712fcbc377Syt 
70722fcbc377Syt /*
70732fcbc377Syt  * Watchdog handler kicks in every 5 seconds to timeout any commands pending
70742fcbc377Syt  * for long time.
70752fcbc377Syt  */
70762fcbc377Syt static void
70772fcbc377Syt ahci_watchdog_handler(ahci_ctl_t *ahci_ctlp)
70782fcbc377Syt {
70792fcbc377Syt 	ahci_port_t *ahci_portp;
708068d33a25Syt 	sata_pkt_t *spkt;
708182263d52Syt 	uint32_t pending_tags;
708282263d52Syt 	uint32_t timeout_tags;
708368d33a25Syt 	uint32_t port_cmd_status;
708482263d52Syt 	uint32_t port_sactive;
70852fcbc377Syt 	uint8_t port;
70862fcbc377Syt 	int tmp_slot;
708768d33a25Syt 	int current_slot;
708882263d52Syt 	uint32_t current_tags;
7089a9440e8dSyt 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
70902fcbc377Syt 	/* max number of cycles this packet should survive */
70912fcbc377Syt 	int max_life_cycles;
70922fcbc377Syt 
70932fcbc377Syt 	/* how many cycles this packet survived so far */
70942fcbc377Syt 	int watched_cycles;
70952fcbc377Syt 
70962fcbc377Syt 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
70972fcbc377Syt 
70982fcbc377Syt 	AHCIDBG0(AHCIDBG_TIMEOUT|AHCIDBG_ENTRY, ahci_ctlp,
70992fcbc377Syt 	    "ahci_watchdog_handler entered");
71002fcbc377Syt 
71012fcbc377Syt 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
71022fcbc377Syt 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
71032fcbc377Syt 			continue;
71042fcbc377Syt 		}
71052fcbc377Syt 
71062fcbc377Syt 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
71072fcbc377Syt 
71082fcbc377Syt 		mutex_enter(&ahci_portp->ahciport_mutex);
71092fcbc377Syt 		if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
71102fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
71112fcbc377Syt 			continue;
71122fcbc377Syt 		}
71132fcbc377Syt 
711468d33a25Syt 		/* Skip the check for those ports in error recovery */
711582263d52Syt 		if ((ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) &&
711682263d52Syt 		    !(ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))) {
711768d33a25Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
711868d33a25Syt 			continue;
711968d33a25Syt 		}
712068d33a25Syt 
712182263d52Syt 		pending_tags = 0;
712268d33a25Syt 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
712368d33a25Syt 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
712468d33a25Syt 
712582263d52Syt 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
712682263d52Syt 			current_slot = 0;
712782263d52Syt 			pending_tags = 0x1;
7128a9440e8dSyt 		} else if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
712982263d52Syt 			current_slot =
713082263d52Syt 			    (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
713182263d52Syt 			    AHCI_CMD_STATUS_CCS_SHIFT;
713282263d52Syt 			pending_tags = ahci_portp->ahciport_pending_tags;
7133a9440e8dSyt 		} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
713482263d52Syt 			port_sactive = ddi_get32(
713582263d52Syt 			    ahci_ctlp->ahcictl_ahci_acc_handle,
713682263d52Syt 			    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
713782263d52Syt 			current_tags = port_sactive &
713882263d52Syt 			    ~port_cmd_status &
713982263d52Syt 			    AHCI_NCQ_SLOT_MASK(ahci_portp);
714082263d52Syt 			pending_tags = ahci_portp->ahciport_pending_ncq_tags;
714182263d52Syt 		}
714282263d52Syt 
71432fcbc377Syt 		timeout_tags = 0;
71442fcbc377Syt 		while (pending_tags) {
71452fcbc377Syt 			tmp_slot = ddi_ffs(pending_tags) - 1;
71462fcbc377Syt 			if (tmp_slot == -1) {
71472fcbc377Syt 				break;
71482fcbc377Syt 			}
71492fcbc377Syt 
715082263d52Syt 			if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
715182263d52Syt 				spkt = ahci_portp->ahciport_err_retri_pkt;
715282263d52Syt 			else
715382263d52Syt 				spkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
715482263d52Syt 
715568d33a25Syt 			if ((spkt != NULL) && spkt->satapkt_time &&
715668d33a25Syt 			    !(spkt->satapkt_op_mode & SATA_OPMODE_POLLING)) {
71572fcbc377Syt 				/*
71582fcbc377Syt 				 * We are overloading satapkt_hba_driver_private
71592fcbc377Syt 				 * with watched_cycle count.
71602fcbc377Syt 				 *
71612fcbc377Syt 				 * If a packet has survived for more than it's
71622fcbc377Syt 				 * max life cycles, it is a candidate for time
71632fcbc377Syt 				 * out.
71642fcbc377Syt 				 */
71652fcbc377Syt 				watched_cycles = (int)(intptr_t)
716668d33a25Syt 				    spkt->satapkt_hba_driver_private;
71672fcbc377Syt 				watched_cycles++;
716868d33a25Syt 				max_life_cycles = (spkt->satapkt_time +
71692fcbc377Syt 				    ahci_watchdog_timeout - 1) /
71702fcbc377Syt 				    ahci_watchdog_timeout;
717168d33a25Syt 
717268d33a25Syt 				spkt->satapkt_hba_driver_private =
717368d33a25Syt 				    (void *)(intptr_t)watched_cycles;
717468d33a25Syt 
717568d33a25Syt 				if (watched_cycles <= max_life_cycles)
717668d33a25Syt 					goto next;
717768d33a25Syt 
7178f8a673adSying tian - Beijing China 				AHCIDBG1(AHCIDBG_ERRS|AHCIDBG_TIMEOUT,
7179f8a673adSying tian - Beijing China 				    ahci_ctlp, "the current slot is %d",
7180f8a673adSying tian - Beijing China 				    current_slot);
718168d33a25Syt 				/*
718268d33a25Syt 				 * We need to check whether the HBA has
718368d33a25Syt 				 * begun to execute the command, if not,
718468d33a25Syt 				 * then re-set the timer of the command.
718568d33a25Syt 				 */
718682263d52Syt 				if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) &&
718782263d52Syt 				    (tmp_slot != current_slot) ||
718882263d52Syt 				    NCQ_CMD_IN_PROGRESS(ahci_portp) &&
718982263d52Syt 				    ((0x1 << tmp_slot) & current_tags)) {
719068d33a25Syt 					spkt->satapkt_hba_driver_private =
719168d33a25Syt 					    (void *)(intptr_t)0;
719268d33a25Syt 				} else {
71932fcbc377Syt 					timeout_tags |= (0x1 << tmp_slot);
7194a9440e8dSyt 					cmn_err(CE_WARN, "!ahci%d: watchdog "
71952fcbc377Syt 					    "port %d satapkt 0x%p timed out\n",
7196a9440e8dSyt 					    instance, port, (void *)spkt);
71972fcbc377Syt 				}
71982fcbc377Syt 			}
719968d33a25Syt next:
72002fcbc377Syt 			CLEAR_BIT(pending_tags, tmp_slot);
72012fcbc377Syt 		}
72022fcbc377Syt 
72032fcbc377Syt 		if (timeout_tags) {
72042fcbc377Syt 			mutex_exit(&ahci_portp->ahciport_mutex);
72052fcbc377Syt 			mutex_exit(&ahci_ctlp->ahcictl_mutex);
72062fcbc377Syt 			ahci_timeout_pkts(ahci_ctlp, ahci_portp,
720782263d52Syt 			    port, timeout_tags);
72082fcbc377Syt 			mutex_enter(&ahci_ctlp->ahcictl_mutex);
72092fcbc377Syt 			mutex_enter(&ahci_portp->ahciport_mutex);
72102fcbc377Syt 		}
72112fcbc377Syt 
72122fcbc377Syt 		mutex_exit(&ahci_portp->ahciport_mutex);
72132fcbc377Syt 	}
72142fcbc377Syt 
72152fcbc377Syt 	/* Re-install the watchdog timeout handler */
72162fcbc377Syt 	if (ahci_ctlp->ahcictl_timeout_id != 0) {
72172fcbc377Syt 		ahci_ctlp->ahcictl_timeout_id =
72182fcbc377Syt 		    timeout((void (*)(void *))ahci_watchdog_handler,
72192fcbc377Syt 		    (caddr_t)ahci_ctlp, ahci_watchdog_tick);
72202fcbc377Syt 	}
72212fcbc377Syt 
72222fcbc377Syt 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
72232fcbc377Syt }
72242fcbc377Syt 
72252fcbc377Syt /*
722668d33a25Syt  * Fill the error context into sata_cmd for non-queued command error.
722768d33a25Syt  */
722868d33a25Syt static void
722968d33a25Syt ahci_copy_err_cnxt(sata_cmd_t *scmd, ahci_fis_d2h_register_t *rfisp)
723068d33a25Syt {
723168d33a25Syt 	scmd->satacmd_status_reg = GET_RFIS_STATUS(rfisp);
723268d33a25Syt 	scmd->satacmd_error_reg = GET_RFIS_ERROR(rfisp);
723368d33a25Syt 	scmd->satacmd_sec_count_lsb = GET_RFIS_SECTOR_COUNT(rfisp);
723468d33a25Syt 	scmd->satacmd_lba_low_lsb = GET_RFIS_CYL_LOW(rfisp);
723568d33a25Syt 	scmd->satacmd_lba_mid_lsb = GET_RFIS_CYL_MID(rfisp);
723668d33a25Syt 	scmd->satacmd_lba_high_lsb = GET_RFIS_CYL_HI(rfisp);
723768d33a25Syt 	scmd->satacmd_device_reg = GET_RFIS_DEV_HEAD(rfisp);
723868d33a25Syt 
723968d33a25Syt 	if (scmd->satacmd_addr_type == ATA_ADDR_LBA48) {
724068d33a25Syt 		scmd->satacmd_sec_count_msb = GET_RFIS_SECTOR_COUNT_EXP(rfisp);
724168d33a25Syt 		scmd->satacmd_lba_low_msb = GET_RFIS_CYL_LOW_EXP(rfisp);
724268d33a25Syt 		scmd->satacmd_lba_mid_msb = GET_RFIS_CYL_MID_EXP(rfisp);
724368d33a25Syt 		scmd->satacmd_lba_high_msb = GET_RFIS_CYL_HI_EXP(rfisp);
724468d33a25Syt 	}
724568d33a25Syt }
724668d33a25Syt 
724782263d52Syt /*
724882263d52Syt  * Fill the ncq error page into sata_cmd for queued command error.
724982263d52Syt  */
725082263d52Syt static void
725182263d52Syt ahci_copy_ncq_err_page(sata_cmd_t *scmd,
725282263d52Syt     struct sata_ncq_error_recovery_page *ncq_err_page)
725382263d52Syt {
725482263d52Syt 	scmd->satacmd_sec_count_msb = ncq_err_page->ncq_sector_count_ext;
725582263d52Syt 	scmd->satacmd_sec_count_lsb = ncq_err_page->ncq_sector_count;
725682263d52Syt 	scmd->satacmd_lba_low_msb = ncq_err_page->ncq_sector_number_ext;
725782263d52Syt 	scmd->satacmd_lba_low_lsb = ncq_err_page->ncq_sector_number;
725882263d52Syt 	scmd->satacmd_lba_mid_msb = ncq_err_page->ncq_cyl_low_ext;
725982263d52Syt 	scmd->satacmd_lba_mid_lsb = ncq_err_page->ncq_cyl_low;
726082263d52Syt 	scmd->satacmd_lba_high_msb = ncq_err_page->ncq_cyl_high_ext;
726182263d52Syt 	scmd->satacmd_lba_high_lsb = ncq_err_page->ncq_cyl_high;
726282263d52Syt 	scmd->satacmd_device_reg = ncq_err_page->ncq_dev_head;
726382263d52Syt 	scmd->satacmd_status_reg = ncq_err_page->ncq_status;
726482263d52Syt 	scmd->satacmd_error_reg = ncq_err_page->ncq_error;
726582263d52Syt }
726682263d52Syt 
726768d33a25Syt /*
726868d33a25Syt  * Put the respective register value to sata_cmd_t for satacmd_flags.
72692fcbc377Syt  */
72702fcbc377Syt static void
72712fcbc377Syt ahci_copy_out_regs(sata_cmd_t *scmd, ahci_fis_d2h_register_t *rfisp)
72722fcbc377Syt {
72732fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_sec_count_msb)
72742fcbc377Syt 		scmd->satacmd_sec_count_msb = GET_RFIS_SECTOR_COUNT_EXP(rfisp);
72752fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_lba_low_msb)
72762fcbc377Syt 		scmd->satacmd_lba_low_msb = GET_RFIS_CYL_LOW_EXP(rfisp);
72772fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_lba_mid_msb)
72782fcbc377Syt 		scmd->satacmd_lba_mid_msb = GET_RFIS_CYL_MID_EXP(rfisp);
72792fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_lba_high_msb)
72802fcbc377Syt 		scmd->satacmd_lba_high_msb = GET_RFIS_CYL_HI_EXP(rfisp);
72812fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_sec_count_lsb)
72822fcbc377Syt 		scmd->satacmd_sec_count_lsb = GET_RFIS_SECTOR_COUNT(rfisp);
72832fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_lba_low_lsb)
72842fcbc377Syt 		scmd->satacmd_lba_low_lsb = GET_RFIS_CYL_LOW(rfisp);
72852fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_lba_mid_lsb)
72862fcbc377Syt 		scmd->satacmd_lba_mid_lsb = GET_RFIS_CYL_MID(rfisp);
72872fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_lba_high_lsb)
72882fcbc377Syt 		scmd->satacmd_lba_high_lsb = GET_RFIS_CYL_HI(rfisp);
72892fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_device_reg)
72902fcbc377Syt 		scmd->satacmd_device_reg = GET_RFIS_DEV_HEAD(rfisp);
72912fcbc377Syt 	if (scmd->satacmd_flags.sata_copy_out_error_reg)
72922fcbc377Syt 		scmd->satacmd_error_reg = GET_RFIS_ERROR(rfisp);
72932fcbc377Syt }
72942fcbc377Syt 
729568d33a25Syt static void
729668d33a25Syt ahci_log_fatal_error_message(ahci_ctl_t *ahci_ctlp, uint8_t port,
729768d33a25Syt     uint32_t intr_status)
729868d33a25Syt {
7299a9440e8dSyt 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
730068d33a25Syt 
730168d33a25Syt 	if (intr_status & AHCI_INTR_STATUS_IFS)
73027095af19Sying tian - Beijing China 		cmn_err(CE_WARN, "!ahci%d: ahci port %d has interface fatal "
7303a9440e8dSyt 		    "error", instance, port);
730468d33a25Syt 
730568d33a25Syt 	if (intr_status & AHCI_INTR_STATUS_HBDS)
73067095af19Sying tian - Beijing China 		cmn_err(CE_WARN, "!ahci%d: ahci port %d has bus data error",
7307a9440e8dSyt 		    instance, port);
730868d33a25Syt 
730968d33a25Syt 	if (intr_status & AHCI_INTR_STATUS_HBFS)
73107095af19Sying tian - Beijing China 		cmn_err(CE_WARN, "!ahci%d: ahci port %d has bus fatal error",
7311a9440e8dSyt 		    instance, port);
731268d33a25Syt 
731368d33a25Syt 	if (intr_status & AHCI_INTR_STATUS_TFES)
73147095af19Sying tian - Beijing China 		cmn_err(CE_WARN, "!ahci%d: ahci port %d has task file error",
7315a9440e8dSyt 		    instance, port);
731668d33a25Syt 
73177095af19Sying tian - Beijing China 	cmn_err(CE_WARN, "!ahci%d: ahci port %d is trying to do error "
7318a9440e8dSyt 	    "recovery", instance, port);
731968d33a25Syt }
732068d33a25Syt 
73212fcbc377Syt /*
73222fcbc377Syt  * Dump the serror message to the log.
73232fcbc377Syt  */
73242fcbc377Syt static void
732568d33a25Syt ahci_log_serror_message(ahci_ctl_t *ahci_ctlp, uint8_t port,
7326a9440e8dSyt     uint32_t port_serror, int debug_only)
73272fcbc377Syt {
7328a9440e8dSyt 	static char err_buf[512];
7329a9440e8dSyt 	static char err_msg_header[16];
7330a9440e8dSyt 	char *err_msg = err_buf;
7331a9440e8dSyt 
7332a9440e8dSyt 	*err_buf = '\0';
7333a9440e8dSyt 	*err_msg_header = '\0';
73342fcbc377Syt 
733582263d52Syt 	if (port_serror & SERROR_DATA_ERR_FIXED) {
7336a9440e8dSyt 		err_msg = strcat(err_msg,
7337a9440e8dSyt 		    "\tRecovered Data Integrity Error (I)\n");
73382fcbc377Syt 	}
73392fcbc377Syt 
734082263d52Syt 	if (port_serror & SERROR_COMM_ERR_FIXED) {
7341a9440e8dSyt 		err_msg = strcat(err_msg,
7342a9440e8dSyt 		    "\tRecovered Communication Error (M)\n");
73432fcbc377Syt 	}
73442fcbc377Syt 
734582263d52Syt 	if (port_serror & SERROR_DATA_ERR) {
7346a9440e8dSyt 		err_msg = strcat(err_msg,
7347a9440e8dSyt 		    "\tTransient Data Integrity Error (T)\n");
73482fcbc377Syt 	}
73492fcbc377Syt 
735082263d52Syt 	if (port_serror & SERROR_PERSISTENT_ERR) {
7351a9440e8dSyt 		err_msg = strcat(err_msg,
7352a9440e8dSyt 		    "\tPersistent Communication or Data Integrity Error (C)\n");
73532fcbc377Syt 	}
73542fcbc377Syt 
735582263d52Syt 	if (port_serror & SERROR_PROTOCOL_ERR) {
7356a9440e8dSyt 		err_msg = strcat(err_msg, "\tProtocol Error (P)\n");
73572fcbc377Syt 	}
73582fcbc377Syt 
735982263d52Syt 	if (port_serror & SERROR_INT_ERR) {
7360a9440e8dSyt 		err_msg = strcat(err_msg, "\tInternal Error (E)\n");
73612fcbc377Syt 	}
73622fcbc377Syt 
736382263d52Syt 	if (port_serror & SERROR_PHY_RDY_CHG) {
7364a9440e8dSyt 		err_msg = strcat(err_msg, "\tPhyRdy Change (N)\n");
73652fcbc377Syt 	}
73662fcbc377Syt 
736782263d52Syt 	if (port_serror & SERROR_PHY_INT_ERR) {
7368a9440e8dSyt 		err_msg = strcat(err_msg, "\tPhy Internal Error (I)\n");
73692fcbc377Syt 	}
73702fcbc377Syt 
737182263d52Syt 	if (port_serror & SERROR_COMM_WAKE) {
7372a9440e8dSyt 		err_msg = strcat(err_msg, "\tComm Wake (W)\n");
73732fcbc377Syt 	}
73742fcbc377Syt 
737582263d52Syt 	if (port_serror & SERROR_10B_TO_8B_ERR) {
7376a9440e8dSyt 		err_msg = strcat(err_msg, "\t10B to 8B Decode Error (B)\n");
73772fcbc377Syt 	}
73782fcbc377Syt 
737982263d52Syt 	if (port_serror & SERROR_DISPARITY_ERR) {
7380a9440e8dSyt 		err_msg = strcat(err_msg, "\tDisparity Error (D)\n");
73812fcbc377Syt 	}
73822fcbc377Syt 
738382263d52Syt 	if (port_serror & SERROR_CRC_ERR) {
7384a9440e8dSyt 		err_msg = strcat(err_msg, "\tCRC Error (C)\n");
73852fcbc377Syt 	}
73862fcbc377Syt 
738782263d52Syt 	if (port_serror & SERROR_HANDSHAKE_ERR) {
7388a9440e8dSyt 		err_msg = strcat(err_msg, "\tHandshake Error (H)\n");
73892fcbc377Syt 	}
73902fcbc377Syt 
739182263d52Syt 	if (port_serror & SERROR_LINK_SEQ_ERR) {
7392a9440e8dSyt 		err_msg = strcat(err_msg, "\tLink Sequence Error (S)\n");
73932fcbc377Syt 	}
73942fcbc377Syt 
739582263d52Syt 	if (port_serror & SERROR_TRANS_ERR) {
7396a9440e8dSyt 		err_msg = strcat(err_msg,
7397a9440e8dSyt 		    "\tTransport state transition error (T)\n");
73982fcbc377Syt 	}
73992fcbc377Syt 
740082263d52Syt 	if (port_serror & SERROR_FIS_TYPE) {
7401a9440e8dSyt 		err_msg = strcat(err_msg, "\tUnknown FIS Type (F)\n");
74022fcbc377Syt 	}
74032fcbc377Syt 
740482263d52Syt 	if (port_serror & SERROR_EXCHANGED_ERR) {
7405a9440e8dSyt 		err_msg = strcat(err_msg, "\tExchanged (X)\n");
7406a9440e8dSyt 	}
7407a9440e8dSyt 
7408a9440e8dSyt 	if (err_msg == NULL)
7409a9440e8dSyt 		return;
7410a9440e8dSyt 
7411a9440e8dSyt 	if (debug_only) {
7412a9440e8dSyt 		(void) sprintf(err_msg_header, "port %d", port);
7413a9440e8dSyt 		AHCIDBG0(AHCIDBG_ERRS, ahci_ctlp, err_msg_header);
7414a9440e8dSyt 		AHCIDBG0(AHCIDBG_ERRS, ahci_ctlp, err_msg);
7415a9440e8dSyt 	} else if (ahci_ctlp) {
74167095af19Sying tian - Beijing China 		cmn_err(CE_WARN, "!ahci%d: %s %s",
7417a9440e8dSyt 		    ddi_get_instance(ahci_ctlp->ahcictl_dip),
7418a9440e8dSyt 		    err_msg_header, err_msg);
7419a9440e8dSyt 	} else {
74207095af19Sying tian - Beijing China 		cmn_err(CE_WARN, "!ahci: %s %s", err_msg_header, err_msg);
74212fcbc377Syt 	}
74222fcbc377Syt }
74232fcbc377Syt 
74242fcbc377Syt /*
74252fcbc377Syt  * This routine is to calculate the total number of ports implemented
74262fcbc377Syt  * by the HBA.
74272fcbc377Syt  */
74282fcbc377Syt static int
74292fcbc377Syt ahci_get_num_implemented_ports(uint32_t ports_implemented)
74302fcbc377Syt {
74312fcbc377Syt 	uint8_t i;
74322fcbc377Syt 	int num = 0;
74332fcbc377Syt 
74342fcbc377Syt 	for (i = 0; i < AHCI_MAX_PORTS; i++) {
74352fcbc377Syt 		if (((uint32_t)0x1 << i) & ports_implemented)
74362fcbc377Syt 			num++;
74372fcbc377Syt 	}
74382fcbc377Syt 
74392fcbc377Syt 	return (num);
74402fcbc377Syt }
74412fcbc377Syt 
7442689d74b0Syt #if AHCI_DEBUG
74432fcbc377Syt static void
74442fcbc377Syt ahci_log(ahci_ctl_t *ahci_ctlp, uint_t level, char *fmt, ...)
74452fcbc377Syt {
7446a9440e8dSyt 	static char name[16];
74472fcbc377Syt 	va_list ap;
74482fcbc377Syt 
74492fcbc377Syt 	mutex_enter(&ahci_log_mutex);
74502fcbc377Syt 
74512fcbc377Syt 	va_start(ap, fmt);
74522fcbc377Syt 	if (ahci_ctlp) {
7453a9440e8dSyt 		(void) sprintf(name, "ahci%d: ",
74542fcbc377Syt 		    ddi_get_instance(ahci_ctlp->ahcictl_dip));
74552fcbc377Syt 	} else {
7456a9440e8dSyt 		(void) sprintf(name, "ahci: ");
74572fcbc377Syt 	}
74582fcbc377Syt 
74592fcbc377Syt 	(void) vsprintf(ahci_log_buf, fmt, ap);
74602fcbc377Syt 	va_end(ap);
74612fcbc377Syt 
7462a9440e8dSyt 	cmn_err(level, "%s%s", name, ahci_log_buf);
74632fcbc377Syt 
74642fcbc377Syt 	mutex_exit(&ahci_log_mutex);
74652fcbc377Syt }
7466689d74b0Syt #endif
746719397407SSherry Moore 
746819397407SSherry Moore /*
746919397407SSherry Moore  * quiesce(9E) entry point.
747019397407SSherry Moore  *
747119397407SSherry Moore  * This function is called when the system is single-threaded at high
747219397407SSherry Moore  * PIL with preemption disabled. Therefore, this function must not be
747319397407SSherry Moore  * blocked.
747419397407SSherry Moore  *
747519397407SSherry Moore  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
747619397407SSherry Moore  * DDI_FAILURE indicates an error condition and should almost never happen.
747719397407SSherry Moore  */
747819397407SSherry Moore static int
747919397407SSherry Moore ahci_quiesce(dev_info_t *dip)
748019397407SSherry Moore {
748119397407SSherry Moore 	ahci_ctl_t *ahci_ctlp;
748219397407SSherry Moore 	ahci_port_t *ahci_portp;
748319397407SSherry Moore 	int instance, port;
748419397407SSherry Moore 
748519397407SSherry Moore 	instance = ddi_get_instance(dip);
748619397407SSherry Moore 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
748719397407SSherry Moore 
748819397407SSherry Moore 	if (ahci_ctlp == NULL)
748919397407SSherry Moore 		return (DDI_FAILURE);
749019397407SSherry Moore 
749119397407SSherry Moore #if AHCI_DEBUG
749219397407SSherry Moore 	ahci_debug_flags = 0;
749319397407SSherry Moore #endif
749419397407SSherry Moore 
749519397407SSherry Moore 	/* disable all the interrupts. */
749619397407SSherry Moore 	ahci_disable_all_intrs(ahci_ctlp);
749719397407SSherry Moore 
749819397407SSherry Moore 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
749919397407SSherry Moore 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
750019397407SSherry Moore 			continue;
750119397407SSherry Moore 		}
750219397407SSherry Moore 
750319397407SSherry Moore 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
750419397407SSherry Moore 
750519397407SSherry Moore 		/*
750619397407SSherry Moore 		 * Stop the port by clearing PxCMD.ST
750719397407SSherry Moore 		 *
750819397407SSherry Moore 		 * Here we must disable the port interrupt because
750919397407SSherry Moore 		 * ahci_disable_all_intrs only clear GHC.IE, and IS
751019397407SSherry Moore 		 * register will be still set if PxIE is enabled.
751119397407SSherry Moore 		 * When ahci shares one IRQ with other drivers, the
751219397407SSherry Moore 		 * intr handler may claim the intr mistakenly.
751319397407SSherry Moore 		 */
751419397407SSherry Moore 		ahci_disable_port_intrs(ahci_ctlp, port);
751519397407SSherry Moore 		(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
751619397407SSherry Moore 		    ahci_portp, port);
751719397407SSherry Moore 	}
751819397407SSherry Moore 
751919397407SSherry Moore 	return (DDI_SUCCESS);
752019397407SSherry Moore }
7521