xref: /illumos-gate/usr/src/uts/common/io/sata/adapters/ahci/ahci.c (revision e5803b76927480e8f9b67b22201c484ccf4c2bcf)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*
27  * AHCI (Advanced Host Controller Interface) SATA HBA Driver
28  *
29  * Power Management Support
30  * ------------------------
31  *
32  * At the moment, the ahci driver only implements suspend/resume to
33  * support Suspend to RAM on X86 feature. Device power management isn't
34  * implemented, link power management is disabled, and hot plug isn't
35  * allowed during the period from suspend to resume.
36  *
37  * For s/r support, the ahci driver only need to implement DDI_SUSPEND
38  * and DDI_RESUME entries, and don't need to take care of new requests
39  * sent down after suspend because the target driver (sd) has already
40  * handled these conditions, and blocked these requests. For the detailed
41  * information, please check with sdopen, sdclose and sdioctl routines.
42  *
43  */
44 
45 #include <sys/note.h>
46 #include <sys/scsi/scsi.h>
47 #include <sys/pci.h>
48 #include <sys/disp.h>
49 #include <sys/sata/sata_hba.h>
50 #include <sys/sata/adapters/ahci/ahcireg.h>
51 #include <sys/sata/adapters/ahci/ahcivar.h>
52 
53 /*
54  * FMA header files
55  */
56 #include <sys/ddifm.h>
57 #include <sys/fm/protocol.h>
58 #include <sys/fm/util.h>
59 #include <sys/fm/io/ddi.h>
60 
61 /*
62  * This is the string displayed by modinfo, etc.
63  * Make sure you keep the version ID up to date!
64  */
65 static char ahci_ident[] = "ahci driver";
66 
67 /*
68  * Function prototypes for driver entry points
69  */
70 static	int ahci_attach(dev_info_t *, ddi_attach_cmd_t);
71 static	int ahci_detach(dev_info_t *, ddi_detach_cmd_t);
72 static	int ahci_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
73 static	int ahci_quiesce(dev_info_t *);
74 
75 /*
76  * Function prototypes for SATA Framework interfaces
77  */
78 static	int ahci_register_sata_hba_tran(ahci_ctl_t *, uint32_t);
79 static	int ahci_unregister_sata_hba_tran(ahci_ctl_t *);
80 
81 static	int ahci_tran_probe_port(dev_info_t *, sata_device_t *);
82 static	int ahci_tran_start(dev_info_t *, sata_pkt_t *spkt);
83 static	int ahci_tran_abort(dev_info_t *, sata_pkt_t *, int);
84 static	int ahci_tran_reset_dport(dev_info_t *, sata_device_t *);
85 static	int ahci_tran_hotplug_port_activate(dev_info_t *, sata_device_t *);
86 static	int ahci_tran_hotplug_port_deactivate(dev_info_t *, sata_device_t *);
87 #if defined(__lock_lint)
88 static	int ahci_selftest(dev_info_t *, sata_device_t *);
89 #endif
90 
91 /*
92  * FMA Prototypes
93  */
94 static	void ahci_fm_init(ahci_ctl_t *);
95 static	void ahci_fm_fini(ahci_ctl_t *);
96 static	int ahci_fm_error_cb(dev_info_t *, ddi_fm_error_t *, const void*);
97 int	ahci_check_acc_handle(ddi_acc_handle_t);
98 int	ahci_check_dma_handle(ddi_dma_handle_t);
99 void	ahci_fm_ereport(ahci_ctl_t *, char *);
100 static	int ahci_check_all_handle(ahci_ctl_t *);
101 static	int ahci_check_ctl_handle(ahci_ctl_t *);
102 static	int ahci_check_port_handle(ahci_ctl_t *, int);
103 static	int ahci_check_slot_handle(ahci_port_t *, int);
104 
105 /*
106  * Local function prototypes
107  */
108 static	int ahci_alloc_ports_state(ahci_ctl_t *);
109 static	void ahci_dealloc_ports_state(ahci_ctl_t *);
110 static	int ahci_alloc_port_state(ahci_ctl_t *, uint8_t);
111 static	void ahci_dealloc_port_state(ahci_ctl_t *, uint8_t);
112 static	int ahci_alloc_rcvd_fis(ahci_ctl_t *, ahci_port_t *, uint8_t);
113 static	void ahci_dealloc_rcvd_fis(ahci_port_t *);
114 static	int ahci_alloc_cmd_list(ahci_ctl_t *, ahci_port_t *, uint8_t);
115 static	void ahci_dealloc_cmd_list(ahci_ctl_t *, ahci_port_t *);
116 static  int ahci_alloc_cmd_tables(ahci_ctl_t *, ahci_port_t *);
117 static  void ahci_dealloc_cmd_tables(ahci_ctl_t *, ahci_port_t *);
118 static	void ahci_alloc_pmult(ahci_ctl_t *, ahci_port_t *);
119 static	void ahci_dealloc_pmult(ahci_ctl_t *, ahci_port_t *);
120 
121 static	int ahci_initialize_controller(ahci_ctl_t *);
122 static	void ahci_uninitialize_controller(ahci_ctl_t *);
123 static	int ahci_initialize_port(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
124 static	int ahci_config_space_init(ahci_ctl_t *);
125 
126 static	void ahci_drain_ports_taskq(ahci_ctl_t *);
127 static	int ahci_rdwr_pmult(ahci_ctl_t *, ahci_addr_t *, uint8_t, uint32_t *,
128     uint8_t);
129 static	int ahci_read_pmult(ahci_ctl_t *, ahci_addr_t *, uint8_t, uint32_t *);
130 static	int ahci_write_pmult(ahci_ctl_t *, ahci_addr_t *, uint8_t, uint32_t);
131 static	int ahci_update_pmult_pscr(ahci_ctl_t *, ahci_addr_t *,
132     sata_device_t *);
133 static	int ahci_update_pmult_gscr(ahci_ctl_t *, ahci_addr_t *,
134     sata_pmult_gscr_t *);
135 static	int ahci_initialize_pmult(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *,
136     sata_device_t *);
137 static	int ahci_initialize_pmport(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
138 static	int ahci_probe_pmult(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
139 static	int ahci_probe_pmport(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *,
140     sata_device_t *);
141 
142 static	void ahci_disable_interface_pm(ahci_ctl_t *, uint8_t);
143 static	int ahci_start_port(ahci_ctl_t *, ahci_port_t *, uint8_t);
144 static	void ahci_find_dev_signature(ahci_ctl_t *, ahci_port_t *,
145     ahci_addr_t *);
146 static	void ahci_update_sata_registers(ahci_ctl_t *, uint8_t, sata_device_t *);
147 static	int ahci_deliver_satapkt(ahci_ctl_t *, ahci_port_t *,
148     ahci_addr_t *, sata_pkt_t *);
149 static	int ahci_do_sync_start(ahci_ctl_t *, ahci_port_t *,
150     ahci_addr_t *, sata_pkt_t *);
151 static	int ahci_claim_free_slot(ahci_ctl_t *, ahci_port_t *,
152     ahci_addr_t *, int);
153 static  void ahci_copy_err_cnxt(sata_cmd_t *, ahci_fis_d2h_register_t *);
154 static	void ahci_copy_ncq_err_page(sata_cmd_t *,
155     struct sata_ncq_error_recovery_page *);
156 static	void ahci_copy_out_regs(sata_cmd_t *, ahci_fis_d2h_register_t *);
157 static	void ahci_add_doneq(ahci_port_t *, sata_pkt_t *, int);
158 static	void ahci_flush_doneq(ahci_port_t *);
159 
160 static	int ahci_software_reset(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
161 static	int ahci_hba_reset(ahci_ctl_t *);
162 static	int ahci_port_reset(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
163 static	int ahci_pmport_reset(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
164 static	void ahci_reject_all_abort_pkts(ahci_ctl_t *, ahci_port_t *, uint8_t);
165 static	int ahci_reset_device_reject_pkts(ahci_ctl_t *, ahci_port_t *,
166     ahci_addr_t *);
167 static	int ahci_reset_pmdevice_reject_pkts(ahci_ctl_t *, ahci_port_t *,
168     ahci_addr_t *);
169 static	int ahci_reset_port_reject_pkts(ahci_ctl_t *, ahci_port_t *,
170     ahci_addr_t *);
171 static	int ahci_reset_hba_reject_pkts(ahci_ctl_t *);
172 static	int ahci_put_port_into_notrunning_state(ahci_ctl_t *, ahci_port_t *,
173     uint8_t);
174 static	int ahci_restart_port_wait_till_ready(ahci_ctl_t *, ahci_port_t *,
175     uint8_t, int, int *);
176 static	void ahci_mop_commands(ahci_ctl_t *, ahci_port_t *, uint32_t,
177     uint32_t, uint32_t, uint32_t, uint32_t);
178 static	uint32_t ahci_get_rdlogext_data(ahci_ctl_t *, ahci_port_t *, uint8_t);
179 static void ahci_get_rqsense_data(ahci_ctl_t *, ahci_port_t *,
180     uint8_t, sata_pkt_t *);
181 static	void ahci_fatal_error_recovery_handler(ahci_ctl_t *, ahci_port_t *,
182     ahci_addr_t *, uint32_t);
183 static	void ahci_pmult_error_recovery_handler(ahci_ctl_t *, ahci_port_t *,
184     uint8_t, uint32_t);
185 static	void ahci_timeout_pkts(ahci_ctl_t *, ahci_port_t *,
186     uint8_t, uint32_t);
187 static	void ahci_events_handler(void *);
188 static	void ahci_watchdog_handler(ahci_ctl_t *);
189 
190 static	uint_t ahci_intr(caddr_t, caddr_t);
191 static	void ahci_port_intr(ahci_ctl_t *, ahci_port_t *, uint8_t);
192 static	int ahci_add_intrs(ahci_ctl_t *, int);
193 static	void ahci_rem_intrs(ahci_ctl_t *);
194 static	void ahci_enable_all_intrs(ahci_ctl_t *);
195 static	void ahci_disable_all_intrs(ahci_ctl_t *);
196 static	void ahci_enable_port_intrs(ahci_ctl_t *, uint8_t);
197 static	void ahci_disable_port_intrs(ahci_ctl_t *, uint8_t);
198 
199 static  int ahci_intr_cmd_cmplt(ahci_ctl_t *, ahci_port_t *, uint8_t);
200 static	int ahci_intr_set_device_bits(ahci_ctl_t *, ahci_port_t *, uint8_t);
201 static	int ahci_intr_ncq_events(ahci_ctl_t *, ahci_port_t *, ahci_addr_t *);
202 static	int ahci_intr_pmult_sntf_events(ahci_ctl_t *, ahci_port_t *, uint8_t);
203 static	int ahci_intr_port_connect_change(ahci_ctl_t *, ahci_port_t *, uint8_t);
204 static	int ahci_intr_device_mechanical_presence_status(ahci_ctl_t *,
205     ahci_port_t *, uint8_t);
206 static	int ahci_intr_phyrdy_change(ahci_ctl_t *, ahci_port_t *, uint8_t);
207 static	int ahci_intr_non_fatal_error(ahci_ctl_t *, ahci_port_t *,
208     uint8_t, uint32_t);
209 static  int ahci_intr_fatal_error(ahci_ctl_t *, ahci_port_t *,
210     uint8_t, uint32_t);
211 static	int ahci_intr_cold_port_detect(ahci_ctl_t *, ahci_port_t *, uint8_t);
212 
213 static	void ahci_get_ahci_addr(ahci_ctl_t *, sata_device_t *, ahci_addr_t *);
214 static	int ahci_get_num_implemented_ports(uint32_t);
215 static  void ahci_log_fatal_error_message(ahci_ctl_t *, uint8_t, uint32_t);
216 static	void ahci_dump_commands(ahci_ctl_t *, uint8_t, uint32_t);
217 static	void ahci_log_serror_message(ahci_ctl_t *, uint8_t, uint32_t, int);
218 #if AHCI_DEBUG
219 static	void ahci_log(ahci_ctl_t *, uint_t, char *, ...);
220 #endif
221 
222 
223 /*
224  * DMA attributes for the data buffer
225  *
226  * dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
227  * does not support 64-bit addressing
228  */
229 static ddi_dma_attr_t buffer_dma_attr = {
230 	DMA_ATTR_V0,		/* dma_attr_version */
231 	0x0ull,			/* dma_attr_addr_lo: lowest bus address */
232 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
233 	0x3fffffull,		/* dma_attr_count_max i.e. for one cookie */
234 	0x2ull,			/* dma_attr_align: word aligned */
235 	1,			/* dma_attr_burstsizes */
236 	1,			/* dma_attr_minxfer */
237 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
238 	0xffffffffull,		/* dma_attr_seg */
239 	AHCI_PRDT_NUMBER,	/* dma_attr_sgllen */
240 	512,			/* dma_attr_granular */
241 	0,			/* dma_attr_flags */
242 };
243 
244 /*
245  * DMA attributes for the rcvd FIS
246  *
247  * dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
248  * does not support 64-bit addressing
249  */
250 static ddi_dma_attr_t rcvd_fis_dma_attr = {
251 	DMA_ATTR_V0,		/* dma_attr_version */
252 	0x0ull,			/* dma_attr_addr_lo: lowest bus address */
253 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
254 	0xffffffffull,		/* dma_attr_count_max i.e. for one cookie */
255 	0x100ull,		/* dma_attr_align: 256-byte aligned */
256 	1,			/* dma_attr_burstsizes */
257 	1,			/* dma_attr_minxfer */
258 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
259 	0xffffffffull,		/* dma_attr_seg */
260 	1,			/* dma_attr_sgllen */
261 	1,			/* dma_attr_granular */
262 	0,			/* dma_attr_flags */
263 };
264 
265 /*
266  * DMA attributes for the command list
267  *
268  * dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
269  * does not support 64-bit addressing
270  */
271 static ddi_dma_attr_t cmd_list_dma_attr = {
272 	DMA_ATTR_V0,		/* dma_attr_version */
273 	0x0ull,			/* dma_attr_addr_lo: lowest bus address */
274 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
275 	0xffffffffull,		/* dma_attr_count_max i.e. for one cookie */
276 	0x400ull,		/* dma_attr_align: 1K-byte aligned */
277 	1,			/* dma_attr_burstsizes */
278 	1,			/* dma_attr_minxfer */
279 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
280 	0xffffffffull,		/* dma_attr_seg */
281 	1,			/* dma_attr_sgllen */
282 	1,			/* dma_attr_granular */
283 	0,			/* dma_attr_flags */
284 };
285 
286 /*
287  * DMA attributes for cmd tables
288  *
289  * dma_attr_addr_hi will be changed to 0xffffffffull if the HBA
290  * does not support 64-bit addressing
291  */
292 static ddi_dma_attr_t cmd_table_dma_attr = {
293 	DMA_ATTR_V0,		/* dma_attr_version */
294 	0x0ull,			/* dma_attr_addr_lo: lowest bus address */
295 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
296 	0xffffffffull,		/* dma_attr_count_max i.e. for one cookie */
297 	0x80ull,		/* dma_attr_align: 128-byte aligned */
298 	1,			/* dma_attr_burstsizes */
299 	1,			/* dma_attr_minxfer */
300 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
301 	0xffffffffull,		/* dma_attr_seg */
302 	1,			/* dma_attr_sgllen */
303 	1,			/* dma_attr_granular */
304 	0,			/* dma_attr_flags */
305 };
306 
307 
308 /* Device access attributes */
309 static ddi_device_acc_attr_t accattr = {
310 	DDI_DEVICE_ATTR_V1,
311 	DDI_STRUCTURE_LE_ACC,
312 	DDI_STRICTORDER_ACC,
313 	DDI_DEFAULT_ACC
314 };
315 
316 
317 static struct dev_ops ahcictl_dev_ops = {
318 	DEVO_REV,		/* devo_rev */
319 	0,			/* refcnt  */
320 	ahci_getinfo,		/* info */
321 	nulldev,		/* identify */
322 	nulldev,		/* probe */
323 	ahci_attach,		/* attach */
324 	ahci_detach,		/* detach */
325 	nodev,			/* no reset */
326 	(struct cb_ops *)0,	/* driver operations */
327 	NULL,			/* bus operations */
328 	NULL,			/* power */
329 	ahci_quiesce,		/* quiesce */
330 };
331 
332 static sata_tran_hotplug_ops_t ahci_tran_hotplug_ops = {
333 	SATA_TRAN_HOTPLUG_OPS_REV_1,
334 	ahci_tran_hotplug_port_activate,
335 	ahci_tran_hotplug_port_deactivate
336 };
337 
338 extern struct mod_ops mod_driverops;
339 
340 static  struct modldrv modldrv = {
341 	&mod_driverops,		/* driverops */
342 	ahci_ident,		/* short description */
343 	&ahcictl_dev_ops,	/* driver ops */
344 };
345 
346 static  struct modlinkage modlinkage = {
347 	MODREV_1,
348 	&modldrv,
349 	NULL
350 };
351 
352 /* The following variables are watchdog handler related */
353 static clock_t ahci_watchdog_timeout = 5; /* 5 seconds */
354 static clock_t ahci_watchdog_tick;
355 
356 /*
357  * This static variable indicates the size of command table,
358  * and it's changeable with prdt number, which ahci_dma_prdt_number
359  * indicates.
360  */
361 static size_t ahci_cmd_table_size;
362 
363 /*
364  * The below global variables are tunable via /etc/system
365  *
366  *	ahci_dma_prdt_number
367  *	ahci_msi_enabled
368  *	ahci_buf_64bit_dma
369  *	ahci_commu_64bit_dma
370  */
371 
372 /* The number of Physical Region Descriptor Table(PRDT) in Command Table */
373 int ahci_dma_prdt_number = AHCI_PRDT_NUMBER;
374 
375 /* AHCI MSI is tunable */
376 boolean_t ahci_msi_enabled = B_TRUE;
377 
378 /*
379  * 64-bit dma addressing for data buffer is tunable
380  *
381  * The variable controls only the below value:
382  *	DBAU (upper 32-bits physical address of data block)
383  */
384 boolean_t ahci_buf_64bit_dma = B_TRUE;
385 
386 /*
387  * 64-bit dma addressing for communication system descriptors is tunable
388  *
389  * The variable controls the below three values:
390  *
391  *	PxCLBU (upper 32-bits for the command list base physical address)
392  *	PxFBU (upper 32-bits for the received FIS base physical address)
393  *	CTBAU (upper 32-bits of command table base)
394  */
395 boolean_t ahci_commu_64bit_dma = B_TRUE;
396 
397 /*
398  * By default, 64-bit dma for data buffer will be disabled for AMD/ATI SB600
399  * chipset. If the users want to have a try with 64-bit dma, please change
400  * the below variable value to enable it.
401  */
402 boolean_t sb600_buf_64bit_dma_disable = B_TRUE;
403 
404 /*
405  * By default, 64-bit dma for command buffer will be disabled for AMD/ATI
406  * SB600/700/710/750/800. If the users want to have a try with 64-bit dma,
407  * please change the below value to enable it.
408  */
409 boolean_t sbxxx_commu_64bit_dma_disable = B_TRUE;
410 
411 
412 /*
413  * End of global tunable variable definition
414  */
415 
416 #if AHCI_DEBUG
417 uint32_t ahci_debug_flags = 0;
418 #else
419 uint32_t ahci_debug_flags = (AHCIDBG_ERRS|AHCIDBG_TIMEOUT);
420 #endif
421 
422 
423 #if AHCI_DEBUG
424 /* The following is needed for ahci_log() */
425 static kmutex_t ahci_log_mutex;
426 static char ahci_log_buf[512];
427 #endif
428 
429 /* Opaque state pointer initialized by ddi_soft_state_init() */
430 static void *ahci_statep = NULL;
431 
432 /*
433  *  ahci module initialization.
434  */
435 int
436 _init(void)
437 {
438 	int	ret;
439 
440 	ret = ddi_soft_state_init(&ahci_statep, sizeof (ahci_ctl_t), 0);
441 	if (ret != 0) {
442 		goto err_out;
443 	}
444 
445 #if AHCI_DEBUG
446 	mutex_init(&ahci_log_mutex, NULL, MUTEX_DRIVER, NULL);
447 #endif
448 
449 	if ((ret = sata_hba_init(&modlinkage)) != 0) {
450 #if AHCI_DEBUG
451 		mutex_destroy(&ahci_log_mutex);
452 #endif
453 		ddi_soft_state_fini(&ahci_statep);
454 		goto err_out;
455 	}
456 
457 	ret = mod_install(&modlinkage);
458 	if (ret != 0) {
459 		sata_hba_fini(&modlinkage);
460 #if AHCI_DEBUG
461 		mutex_destroy(&ahci_log_mutex);
462 #endif
463 		ddi_soft_state_fini(&ahci_statep);
464 		goto err_out;
465 	}
466 
467 	/* watchdog tick */
468 	ahci_watchdog_tick = drv_usectohz(
469 	    (clock_t)ahci_watchdog_timeout * 1000000);
470 	return (ret);
471 
472 err_out:
473 	cmn_err(CE_WARN, "!ahci: Module init failed");
474 	return (ret);
475 }
476 
477 /*
478  * ahci module uninitialize.
479  */
480 int
481 _fini(void)
482 {
483 	int	ret;
484 
485 	ret = mod_remove(&modlinkage);
486 	if (ret != 0) {
487 		return (ret);
488 	}
489 
490 	/* Remove the resources allocated in _init(). */
491 	sata_hba_fini(&modlinkage);
492 #if AHCI_DEBUG
493 	mutex_destroy(&ahci_log_mutex);
494 #endif
495 	ddi_soft_state_fini(&ahci_statep);
496 
497 	return (ret);
498 }
499 
500 /*
501  * _info entry point
502  */
503 int
504 _info(struct modinfo *modinfop)
505 {
506 	return (mod_info(&modlinkage, modinfop));
507 }
508 
509 /*
510  * The attach entry point for dev_ops.
511  */
512 static int
513 ahci_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
514 {
515 	ahci_ctl_t *ahci_ctlp = NULL;
516 	int instance = ddi_get_instance(dip);
517 	int status;
518 	int attach_state;
519 	uint32_t cap_status, ahci_version;
520 	int intr_types;
521 	int i;
522 	pci_regspec_t *regs;
523 	int regs_length;
524 	int rnumber;
525 #if AHCI_DEBUG
526 	int speed;
527 #endif
528 
529 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp, "ahci_attach enter",
530 	    NULL);
531 
532 	switch (cmd) {
533 	case DDI_ATTACH:
534 		break;
535 
536 	case DDI_RESUME:
537 
538 		/*
539 		 * During DDI_RESUME, the hardware state of the device
540 		 * (power may have been removed from the device) must be
541 		 * restored, allow pending requests to continue, and
542 		 * service new requests.
543 		 */
544 		ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
545 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
546 
547 		/* Restart watch thread */
548 		if (ahci_ctlp->ahcictl_timeout_id == 0)
549 			ahci_ctlp->ahcictl_timeout_id = timeout(
550 			    (void (*)(void *))ahci_watchdog_handler,
551 			    (caddr_t)ahci_ctlp, ahci_watchdog_tick);
552 
553 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
554 
555 		/*
556 		 * Re-initialize the controller and enable the interrupts and
557 		 * restart all the ports.
558 		 *
559 		 * Note that so far we don't support hot-plug during
560 		 * suspend/resume.
561 		 */
562 		if (ahci_initialize_controller(ahci_ctlp) != AHCI_SUCCESS) {
563 			AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PM, ahci_ctlp,
564 			    "Failed to initialize the controller "
565 			    "during DDI_RESUME", NULL);
566 			return (DDI_FAILURE);
567 		}
568 
569 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
570 		ahci_ctlp->ahcictl_flags &= ~ AHCI_SUSPEND;
571 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
572 
573 		return (DDI_SUCCESS);
574 
575 	default:
576 		return (DDI_FAILURE);
577 	}
578 
579 	attach_state = AHCI_ATTACH_STATE_NONE;
580 
581 	/* Allocate soft state */
582 	status = ddi_soft_state_zalloc(ahci_statep, instance);
583 	if (status != DDI_SUCCESS) {
584 		cmn_err(CE_WARN, "!ahci%d: Cannot allocate soft state",
585 		    instance);
586 		goto err_out;
587 	}
588 
589 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
590 	ahci_ctlp->ahcictl_flags |= AHCI_ATTACH;
591 	ahci_ctlp->ahcictl_dip = dip;
592 
593 	/* Initialize the cport/port mapping */
594 	for (i = 0; i < AHCI_MAX_PORTS; i++) {
595 		ahci_ctlp->ahcictl_port_to_cport[i] = 0xff;
596 		ahci_ctlp->ahcictl_cport_to_port[i] = 0xff;
597 	}
598 
599 	attach_state |= AHCI_ATTACH_STATE_STATEP_ALLOC;
600 
601 	/* Initialize FMA properties */
602 	ahci_fm_init(ahci_ctlp);
603 
604 	attach_state |= AHCI_ATTACH_STATE_FMA;
605 
606 	/*
607 	 * Now map the AHCI base address; which includes global
608 	 * registers and port control registers
609 	 *
610 	 * According to the spec, the AHCI Base Address is BAR5,
611 	 * but BAR0-BAR4 are optional, so we need to check which
612 	 * rnumber is used for BAR5.
613 	 */
614 
615 	/*
616 	 * search through DDI "reg" property for the AHCI register set
617 	 */
618 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
619 	    DDI_PROP_DONTPASS, "reg", (int **)&regs,
620 	    (uint_t *)&regs_length) != DDI_PROP_SUCCESS) {
621 		cmn_err(CE_WARN, "!ahci%d: Cannot lookup reg property",
622 		    instance);
623 		goto err_out;
624 	}
625 
626 	/* AHCI Base Address is located at 0x24 offset */
627 	for (rnumber = 0; rnumber < regs_length; ++rnumber) {
628 		if ((regs[rnumber].pci_phys_hi & PCI_REG_REG_M)
629 		    == AHCI_PCI_RNUM)
630 			break;
631 	}
632 
633 	ddi_prop_free(regs);
634 
635 	if (rnumber == regs_length) {
636 		cmn_err(CE_WARN, "!ahci%d: Cannot find AHCI register set",
637 		    instance);
638 		goto err_out;
639 	}
640 
641 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "rnumber = %d", rnumber);
642 
643 	status = ddi_regs_map_setup(dip,
644 	    rnumber,
645 	    (caddr_t *)&ahci_ctlp->ahcictl_ahci_addr,
646 	    0,
647 	    0,
648 	    &accattr,
649 	    &ahci_ctlp->ahcictl_ahci_acc_handle);
650 	if (status != DDI_SUCCESS) {
651 		cmn_err(CE_WARN, "!ahci%d: Cannot map register space",
652 		    instance);
653 		goto err_out;
654 	}
655 
656 	attach_state |= AHCI_ATTACH_STATE_REG_MAP;
657 
658 	/* Get the AHCI version information */
659 	ahci_version = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
660 	    (uint32_t *)AHCI_GLOBAL_VS(ahci_ctlp));
661 
662 	cmn_err(CE_NOTE, "!ahci%d: hba AHCI version = %x.%x", instance,
663 	    (ahci_version & 0xffff0000) >> 16,
664 	    ((ahci_version & 0x0000ff00) >> 4 |
665 	    (ahci_version & 0x000000ff)));
666 
667 	/* We don't support controllers whose versions are lower than 1.0 */
668 	if (!(ahci_version & 0xffff0000)) {
669 		cmn_err(CE_WARN, "ahci%d: Don't support AHCI HBA with lower "
670 		    "than version 1.0", instance);
671 		goto err_out;
672 	}
673 
674 	/* Get the HBA capabilities information */
675 	cap_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
676 	    (uint32_t *)AHCI_GLOBAL_CAP(ahci_ctlp));
677 
678 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "hba capabilities = 0x%x",
679 	    cap_status);
680 
681 #if AHCI_DEBUG
682 	/* Get the interface speed supported by the HBA */
683 	speed = (cap_status & AHCI_HBA_CAP_ISS) >> AHCI_HBA_CAP_ISS_SHIFT;
684 	if (speed == 0x01) {
685 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
686 		    "hba interface speed support: Gen 1 (1.5Gbps)", NULL);
687 	} else if (speed == 0x10) {
688 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
689 		    "hba interface speed support: Gen 2 (3 Gbps)", NULL);
690 	} else if (speed == 0x11) {
691 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
692 		    "hba interface speed support: Gen 3 (6 Gbps)", NULL);
693 	}
694 #endif
695 
696 	/* Get the number of command slots supported by the HBA */
697 	ahci_ctlp->ahcictl_num_cmd_slots =
698 	    ((cap_status & AHCI_HBA_CAP_NCS) >>
699 	    AHCI_HBA_CAP_NCS_SHIFT) + 1;
700 
701 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "hba number of cmd slots: %d",
702 	    ahci_ctlp->ahcictl_num_cmd_slots);
703 
704 	/* Get the bit map which indicates ports implemented by the HBA */
705 	ahci_ctlp->ahcictl_ports_implemented =
706 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
707 	    (uint32_t *)AHCI_GLOBAL_PI(ahci_ctlp));
708 
709 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "hba implementation of ports: 0x%x",
710 	    ahci_ctlp->ahcictl_ports_implemented);
711 
712 	/*
713 	 * According to the AHCI spec, CAP.NP should indicate the maximum
714 	 * number of ports supported by the HBA silicon, but we found
715 	 * this value of ICH8 chipset only indicates the number of ports
716 	 * implemented (exposed) by it. Therefore, the driver should calculate
717 	 * the potential maximum value by checking PI register, and use
718 	 * the maximum of this value and CAP.NP.
719 	 */
720 	ahci_ctlp->ahcictl_num_ports = max(
721 	    (cap_status & AHCI_HBA_CAP_NP) + 1,
722 	    ddi_fls(ahci_ctlp->ahcictl_ports_implemented));
723 
724 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "hba number of ports: %d",
725 	    ahci_ctlp->ahcictl_num_ports);
726 
727 	/* Get the number of implemented ports by the HBA */
728 	ahci_ctlp->ahcictl_num_implemented_ports =
729 	    ahci_get_num_implemented_ports(
730 	    ahci_ctlp->ahcictl_ports_implemented);
731 
732 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
733 	    "hba number of implemented ports: %d",
734 	    ahci_ctlp->ahcictl_num_implemented_ports);
735 
736 	/* Check whether HBA supports 64bit DMA addressing */
737 	if (!(cap_status & AHCI_HBA_CAP_S64A)) {
738 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_BUF_32BIT_DMA;
739 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_COMMU_32BIT_DMA;
740 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
741 		    "hba does not support 64-bit addressing", NULL);
742 	}
743 
744 	/* Checking for the support of Port Multiplier */
745 	if (cap_status & AHCI_HBA_CAP_SPM) {
746 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_PMULT_CBSS;
747 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
748 		    "hba supports port multiplier (CBSS)", NULL);
749 
750 		/* Support FIS-based switching ? */
751 		if (cap_status & AHCI_HBA_CAP_FBSS) {
752 			ahci_ctlp->ahcictl_cap |= AHCI_CAP_PMULT_FBSS;
753 			AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
754 			    "hba supports FIS-based switching (FBSS)", NULL);
755 		}
756 	}
757 
758 	/* Checking for Support Command List Override */
759 	if (cap_status & AHCI_HBA_CAP_SCLO) {
760 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_SCLO;
761 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
762 		    "hba supports command list override.", NULL);
763 	}
764 
765 	/* Checking for Asynchronous Notification */
766 	if (cap_status & AHCI_HBA_CAP_SSNTF) {
767 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_SNTF;
768 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
769 		    "hba supports asynchronous notification.", NULL);
770 	}
771 
772 	if (pci_config_setup(dip, &ahci_ctlp->ahcictl_pci_conf_handle)
773 	    != DDI_SUCCESS) {
774 		cmn_err(CE_WARN, "!ahci%d: Cannot set up pci configure space",
775 		    instance);
776 		goto err_out;
777 	}
778 
779 	attach_state |= AHCI_ATTACH_STATE_PCICFG_SETUP;
780 
781 	/*
782 	 * Check the pci configuration space, and set caps. We also
783 	 * handle the hardware defect in this function.
784 	 *
785 	 * For example, force ATI SB600 to use 32-bit dma addressing
786 	 * since it doesn't support 64-bit dma though its CAP register
787 	 * declares it support.
788 	 */
789 	if (ahci_config_space_init(ahci_ctlp) == AHCI_FAILURE) {
790 		cmn_err(CE_WARN, "!ahci%d: ahci_config_space_init failed",
791 		    instance);
792 		goto err_out;
793 	}
794 
795 	/*
796 	 * Disable the whole controller interrupts before adding
797 	 * interrupt handlers(s).
798 	 */
799 	ahci_disable_all_intrs(ahci_ctlp);
800 
801 	/* Get supported interrupt types */
802 	if (ddi_intr_get_supported_types(dip, &intr_types) != DDI_SUCCESS) {
803 		cmn_err(CE_WARN, "!ahci%d: ddi_intr_get_supported_types failed",
804 		    instance);
805 		goto err_out;
806 	}
807 
808 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
809 	    "ddi_intr_get_supported_types() returned: 0x%x",
810 	    intr_types);
811 
812 	if (ahci_msi_enabled && (intr_types & DDI_INTR_TYPE_MSI)) {
813 		/*
814 		 * Try MSI first, but fall back to FIXED if failed
815 		 */
816 		if (ahci_add_intrs(ahci_ctlp, DDI_INTR_TYPE_MSI) ==
817 		    DDI_SUCCESS) {
818 			ahci_ctlp->ahcictl_intr_type = DDI_INTR_TYPE_MSI;
819 			AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
820 			    "Using MSI interrupt type", NULL);
821 			goto intr_done;
822 		}
823 
824 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
825 		    "MSI registration failed, "
826 		    "trying FIXED interrupts", NULL);
827 	}
828 
829 	if (intr_types & DDI_INTR_TYPE_FIXED) {
830 		if (ahci_add_intrs(ahci_ctlp, DDI_INTR_TYPE_FIXED) ==
831 		    DDI_SUCCESS) {
832 			ahci_ctlp->ahcictl_intr_type = DDI_INTR_TYPE_FIXED;
833 			AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
834 			    "Using FIXED interrupt type", NULL);
835 			goto intr_done;
836 		}
837 
838 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
839 		    "FIXED interrupt registration failed", NULL);
840 	}
841 
842 	cmn_err(CE_WARN, "!ahci%d: Interrupt registration failed", instance);
843 
844 	goto err_out;
845 
846 intr_done:
847 
848 	attach_state |= AHCI_ATTACH_STATE_INTR_ADDED;
849 
850 	/* Initialize the controller mutex */
851 	mutex_init(&ahci_ctlp->ahcictl_mutex, NULL, MUTEX_DRIVER,
852 	    (void *)(uintptr_t)ahci_ctlp->ahcictl_intr_pri);
853 
854 	attach_state |= AHCI_ATTACH_STATE_MUTEX_INIT;
855 
856 	if (ahci_dma_prdt_number < AHCI_MIN_PRDT_NUMBER) {
857 		ahci_dma_prdt_number = AHCI_MIN_PRDT_NUMBER;
858 	} else if (ahci_dma_prdt_number > AHCI_MAX_PRDT_NUMBER) {
859 		ahci_dma_prdt_number = AHCI_MAX_PRDT_NUMBER;
860 	}
861 
862 	ahci_cmd_table_size = (sizeof (ahci_cmd_table_t) +
863 	    (ahci_dma_prdt_number - AHCI_PRDT_NUMBER) *
864 	    sizeof (ahci_prdt_item_t));
865 
866 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
867 	    "ahci_attach: ahci_dma_prdt_number set by user is 0x%x,"
868 	    " ahci_cmd_table_size is 0x%x",
869 	    ahci_dma_prdt_number, ahci_cmd_table_size);
870 
871 	if (ahci_dma_prdt_number != AHCI_PRDT_NUMBER)
872 		ahci_ctlp->ahcictl_buffer_dma_attr.dma_attr_sgllen =
873 		    ahci_dma_prdt_number;
874 
875 	ahci_ctlp->ahcictl_buffer_dma_attr = buffer_dma_attr;
876 	ahci_ctlp->ahcictl_rcvd_fis_dma_attr = rcvd_fis_dma_attr;
877 	ahci_ctlp->ahcictl_cmd_list_dma_attr = cmd_list_dma_attr;
878 	ahci_ctlp->ahcictl_cmd_table_dma_attr = cmd_table_dma_attr;
879 
880 	/*
881 	 * enable 64bit dma for data buffer for SB600 if
882 	 * sb600_buf_64bit_dma_disable is B_FALSE
883 	 */
884 	if ((ahci_buf_64bit_dma == B_FALSE) ||
885 	    ((ahci_ctlp->ahcictl_cap & AHCI_CAP_BUF_32BIT_DMA) &&
886 	    !(sb600_buf_64bit_dma_disable == B_FALSE &&
887 	    ahci_ctlp->ahcictl_venid == 0x1002 &&
888 	    ahci_ctlp->ahcictl_devid == 0x4380))) {
889 		ahci_ctlp->ahcictl_buffer_dma_attr.dma_attr_addr_hi =
890 		    0xffffffffull;
891 	}
892 
893 	/*
894 	 * enable 64bit dma for command buffer for SB600/700/710/800
895 	 * if sbxxx_commu_64bit_dma_disable is B_FALSE
896 	 */
897 	if ((ahci_commu_64bit_dma == B_FALSE) ||
898 	    ((ahci_ctlp->ahcictl_cap & AHCI_CAP_COMMU_32BIT_DMA) &&
899 	    !(sbxxx_commu_64bit_dma_disable == B_FALSE &&
900 	    ahci_ctlp->ahcictl_venid == 0x1002 &&
901 	    (ahci_ctlp->ahcictl_devid == 0x4380 ||
902 	    ahci_ctlp->ahcictl_devid == 0x4391)))) {
903 		ahci_ctlp->ahcictl_rcvd_fis_dma_attr.dma_attr_addr_hi =
904 		    0xffffffffull;
905 		ahci_ctlp->ahcictl_cmd_list_dma_attr.dma_attr_addr_hi =
906 		    0xffffffffull;
907 		ahci_ctlp->ahcictl_cmd_table_dma_attr.dma_attr_addr_hi =
908 		    0xffffffffull;
909 	}
910 
911 	/* Allocate the ports structure */
912 	status = ahci_alloc_ports_state(ahci_ctlp);
913 	if (status != AHCI_SUCCESS) {
914 		cmn_err(CE_WARN, "!ahci%d: Cannot allocate ports structure",
915 		    instance);
916 		goto err_out;
917 	}
918 
919 	attach_state |= AHCI_ATTACH_STATE_PORT_ALLOC;
920 
921 	/*
922 	 * Initialize the controller and ports.
923 	 */
924 	status = ahci_initialize_controller(ahci_ctlp);
925 	if (status != AHCI_SUCCESS) {
926 		cmn_err(CE_WARN, "!ahci%d: HBA initialization failed",
927 		    instance);
928 		goto err_out;
929 	}
930 
931 	attach_state |= AHCI_ATTACH_STATE_HW_INIT;
932 
933 	/* Start one thread to check packet timeouts */
934 	ahci_ctlp->ahcictl_timeout_id = timeout(
935 	    (void (*)(void *))ahci_watchdog_handler,
936 	    (caddr_t)ahci_ctlp, ahci_watchdog_tick);
937 
938 	attach_state |= AHCI_ATTACH_STATE_TIMEOUT_ENABLED;
939 
940 	if (ahci_register_sata_hba_tran(ahci_ctlp, cap_status)) {
941 		cmn_err(CE_WARN, "!ahci%d: sata hba tran registration failed",
942 		    instance);
943 		goto err_out;
944 	}
945 
946 	/* Check all handles at the end of the attach operation. */
947 	if (ahci_check_all_handle(ahci_ctlp) != DDI_SUCCESS) {
948 		cmn_err(CE_WARN, "!ahci%d: invalid dma/acc handles",
949 		    instance);
950 		goto err_out;
951 	}
952 
953 	ahci_ctlp->ahcictl_flags &= ~AHCI_ATTACH;
954 
955 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "ahci_attach success!", NULL);
956 
957 	return (DDI_SUCCESS);
958 
959 err_out:
960 	/* FMA message */
961 	ahci_fm_ereport(ahci_ctlp, DDI_FM_DEVICE_NO_RESPONSE);
962 	ddi_fm_service_impact(ahci_ctlp->ahcictl_dip, DDI_SERVICE_LOST);
963 
964 	if (attach_state & AHCI_ATTACH_STATE_TIMEOUT_ENABLED) {
965 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
966 		(void) untimeout(ahci_ctlp->ahcictl_timeout_id);
967 		ahci_ctlp->ahcictl_timeout_id = 0;
968 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
969 	}
970 
971 	if (attach_state & AHCI_ATTACH_STATE_HW_INIT) {
972 		ahci_uninitialize_controller(ahci_ctlp);
973 	}
974 
975 	if (attach_state & AHCI_ATTACH_STATE_PORT_ALLOC) {
976 		ahci_dealloc_ports_state(ahci_ctlp);
977 	}
978 
979 	if (attach_state & AHCI_ATTACH_STATE_MUTEX_INIT) {
980 		mutex_destroy(&ahci_ctlp->ahcictl_mutex);
981 	}
982 
983 	if (attach_state & AHCI_ATTACH_STATE_INTR_ADDED) {
984 		ahci_rem_intrs(ahci_ctlp);
985 	}
986 
987 	if (attach_state & AHCI_ATTACH_STATE_PCICFG_SETUP) {
988 		pci_config_teardown(&ahci_ctlp->ahcictl_pci_conf_handle);
989 	}
990 
991 	if (attach_state & AHCI_ATTACH_STATE_REG_MAP) {
992 		ddi_regs_map_free(&ahci_ctlp->ahcictl_ahci_acc_handle);
993 	}
994 
995 	if (attach_state & AHCI_ATTACH_STATE_FMA) {
996 		ahci_fm_fini(ahci_ctlp);
997 	}
998 
999 	if (attach_state & AHCI_ATTACH_STATE_STATEP_ALLOC) {
1000 		ddi_soft_state_free(ahci_statep, instance);
1001 	}
1002 
1003 	return (DDI_FAILURE);
1004 }
1005 
1006 /*
1007  * The detach entry point for dev_ops.
1008  */
1009 static int
1010 ahci_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
1011 {
1012 	ahci_ctl_t *ahci_ctlp;
1013 	int instance;
1014 	int ret;
1015 
1016 	instance = ddi_get_instance(dip);
1017 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
1018 
1019 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_detach enter", NULL);
1020 
1021 	switch (cmd) {
1022 	case DDI_DETACH:
1023 
1024 		/* disable the interrupts for an uninterrupted detach */
1025 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
1026 		ahci_disable_all_intrs(ahci_ctlp);
1027 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
1028 
1029 		/* unregister from the sata framework. */
1030 		ret = ahci_unregister_sata_hba_tran(ahci_ctlp);
1031 		if (ret != AHCI_SUCCESS) {
1032 			mutex_enter(&ahci_ctlp->ahcictl_mutex);
1033 			ahci_enable_all_intrs(ahci_ctlp);
1034 			mutex_exit(&ahci_ctlp->ahcictl_mutex);
1035 			return (DDI_FAILURE);
1036 		}
1037 
1038 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
1039 
1040 		/* stop the watchdog handler */
1041 		(void) untimeout(ahci_ctlp->ahcictl_timeout_id);
1042 		ahci_ctlp->ahcictl_timeout_id = 0;
1043 
1044 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
1045 
1046 		/* uninitialize the controller */
1047 		ahci_uninitialize_controller(ahci_ctlp);
1048 
1049 		/* remove the interrupts */
1050 		ahci_rem_intrs(ahci_ctlp);
1051 
1052 		/* deallocate the ports structures */
1053 		ahci_dealloc_ports_state(ahci_ctlp);
1054 
1055 		/* destroy mutex */
1056 		mutex_destroy(&ahci_ctlp->ahcictl_mutex);
1057 
1058 		/* teardown the pci config */
1059 		pci_config_teardown(&ahci_ctlp->ahcictl_pci_conf_handle);
1060 
1061 		/* remove the reg maps. */
1062 		ddi_regs_map_free(&ahci_ctlp->ahcictl_ahci_acc_handle);
1063 
1064 		/* release fma resource */
1065 		ahci_fm_fini(ahci_ctlp);
1066 
1067 		/* free the soft state. */
1068 		ddi_soft_state_free(ahci_statep, instance);
1069 
1070 		return (DDI_SUCCESS);
1071 
1072 	case DDI_SUSPEND:
1073 
1074 		/*
1075 		 * The steps associated with suspension must include putting
1076 		 * the underlying device into a quiescent state so that it
1077 		 * will not generate interrupts or modify or access memory.
1078 		 */
1079 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
1080 		if (ahci_ctlp->ahcictl_flags & AHCI_SUSPEND) {
1081 			mutex_exit(&ahci_ctlp->ahcictl_mutex);
1082 			return (DDI_SUCCESS);
1083 		}
1084 
1085 		ahci_ctlp->ahcictl_flags |= AHCI_SUSPEND;
1086 
1087 		/* stop the watchdog handler */
1088 		if (ahci_ctlp->ahcictl_timeout_id) {
1089 			(void) untimeout(ahci_ctlp->ahcictl_timeout_id);
1090 			ahci_ctlp->ahcictl_timeout_id = 0;
1091 		}
1092 
1093 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
1094 
1095 		/*
1096 		 * drain the taskq
1097 		 */
1098 		ahci_drain_ports_taskq(ahci_ctlp);
1099 
1100 		/*
1101 		 * Disable the interrupts and stop all the ports.
1102 		 */
1103 		ahci_uninitialize_controller(ahci_ctlp);
1104 
1105 		return (DDI_SUCCESS);
1106 
1107 	default:
1108 		return (DDI_FAILURE);
1109 	}
1110 }
1111 
1112 /*
1113  * The info entry point for dev_ops.
1114  *
1115  */
1116 static int
1117 ahci_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
1118 		    void *arg, void **result)
1119 {
1120 #ifndef __lock_lint
1121 	_NOTE(ARGUNUSED(dip))
1122 #endif /* __lock_lint */
1123 
1124 	ahci_ctl_t *ahci_ctlp;
1125 	int instance;
1126 	dev_t dev;
1127 
1128 	dev = (dev_t)arg;
1129 	instance = getminor(dev);
1130 
1131 	switch (infocmd) {
1132 		case DDI_INFO_DEVT2DEVINFO:
1133 			ahci_ctlp = ddi_get_soft_state(ahci_statep,  instance);
1134 			if (ahci_ctlp != NULL) {
1135 				*result = ahci_ctlp->ahcictl_dip;
1136 				return (DDI_SUCCESS);
1137 			} else {
1138 				*result = NULL;
1139 				return (DDI_FAILURE);
1140 			}
1141 		case DDI_INFO_DEVT2INSTANCE:
1142 			*(int *)result = instance;
1143 			break;
1144 		default:
1145 			break;
1146 	}
1147 
1148 	return (DDI_SUCCESS);
1149 }
1150 
1151 /*
1152  * Registers the ahci with sata framework.
1153  */
1154 static int
1155 ahci_register_sata_hba_tran(ahci_ctl_t *ahci_ctlp, uint32_t cap_status)
1156 {
1157 	struct 	sata_hba_tran	*sata_hba_tran;
1158 
1159 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
1160 	    "ahci_register_sata_hba_tran enter", NULL);
1161 
1162 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
1163 
1164 	/* Allocate memory for the sata_hba_tran  */
1165 	sata_hba_tran = kmem_zalloc(sizeof (sata_hba_tran_t), KM_SLEEP);
1166 
1167 	sata_hba_tran->sata_tran_hba_rev = SATA_TRAN_HBA_REV;
1168 	sata_hba_tran->sata_tran_hba_dip = ahci_ctlp->ahcictl_dip;
1169 	sata_hba_tran->sata_tran_hba_dma_attr =
1170 	    &ahci_ctlp->ahcictl_buffer_dma_attr;
1171 
1172 	/* Report the number of implemented ports */
1173 	sata_hba_tran->sata_tran_hba_num_cports =
1174 	    ahci_ctlp->ahcictl_num_implemented_ports;
1175 
1176 	/* Support ATAPI device */
1177 	sata_hba_tran->sata_tran_hba_features_support = SATA_CTLF_ATAPI;
1178 
1179 	/* Get the data transfer capability for PIO command by the HBA */
1180 	if (cap_status & AHCI_HBA_CAP_PMD) {
1181 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_PIO_MDRQ;
1182 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "HBA supports multiple "
1183 		    "DRQ block data transfer for PIO command protocol", NULL);
1184 	}
1185 
1186 	/*
1187 	 * According to the AHCI spec, the ATA/ATAPI-7 queued feature set
1188 	 * is not supported by AHCI (including the READ QUEUED (EXT), WRITE
1189 	 * QUEUED (EXT), and SERVICE commands). Queued operations are
1190 	 * supported in AHCI using the READ FPDMA QUEUED and WRITE FPDMA
1191 	 * QUEUED commands when the HBA and device support native command
1192 	 * queuing(NCQ).
1193 	 *
1194 	 * SATA_CTLF_NCQ will be set to sata_tran_hba_features_support if the
1195 	 * CAP register of the HBA indicates NCQ is supported.
1196 	 *
1197 	 * SATA_CTLF_NCQ cannot be set if AHCI_CAP_NO_MCMDLIST_NONQUEUE is
1198 	 * set because the previous register content of PxCI can be re-written
1199 	 * in the register write.
1200 	 */
1201 	if ((cap_status & AHCI_HBA_CAP_SNCQ) &&
1202 	    !(ahci_ctlp->ahcictl_cap & AHCI_CAP_NO_MCMDLIST_NONQUEUE)) {
1203 		sata_hba_tran->sata_tran_hba_features_support |= SATA_CTLF_NCQ;
1204 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_NCQ;
1205 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "HBA supports Native "
1206 		    "Command Queuing", NULL);
1207 	}
1208 
1209 	/* Support port multiplier? */
1210 	if (cap_status & AHCI_HBA_CAP_SPM) {
1211 		sata_hba_tran->sata_tran_hba_features_support |=
1212 		    SATA_CTLF_PORT_MULTIPLIER;
1213 
1214 		/* Support FIS-based switching for port multiplier? */
1215 		if (cap_status & AHCI_HBA_CAP_FBSS) {
1216 			sata_hba_tran->sata_tran_hba_features_support |=
1217 			    SATA_CTLF_PMULT_FBS;
1218 		}
1219 	}
1220 
1221 	/* Report the number of command slots */
1222 	sata_hba_tran->sata_tran_hba_qdepth = ahci_ctlp->ahcictl_num_cmd_slots;
1223 
1224 	sata_hba_tran->sata_tran_probe_port = ahci_tran_probe_port;
1225 	sata_hba_tran->sata_tran_start = ahci_tran_start;
1226 	sata_hba_tran->sata_tran_abort = ahci_tran_abort;
1227 	sata_hba_tran->sata_tran_reset_dport = ahci_tran_reset_dport;
1228 	sata_hba_tran->sata_tran_hotplug_ops = &ahci_tran_hotplug_ops;
1229 #ifdef __lock_lint
1230 	sata_hba_tran->sata_tran_selftest = ahci_selftest;
1231 #endif
1232 	/*
1233 	 * When SATA framework adds support for pwrmgt the
1234 	 * pwrmgt_ops needs to be updated
1235 	 */
1236 	sata_hba_tran->sata_tran_pwrmgt_ops = NULL;
1237 	sata_hba_tran->sata_tran_ioctl = NULL;
1238 
1239 	ahci_ctlp->ahcictl_sata_hba_tran = sata_hba_tran;
1240 
1241 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
1242 
1243 	/* Attach it to SATA framework */
1244 	if (sata_hba_attach(ahci_ctlp->ahcictl_dip, sata_hba_tran, DDI_ATTACH)
1245 	    != DDI_SUCCESS) {
1246 		kmem_free((void *)sata_hba_tran, sizeof (sata_hba_tran_t));
1247 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
1248 		ahci_ctlp->ahcictl_sata_hba_tran = NULL;
1249 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
1250 		return (AHCI_FAILURE);
1251 	}
1252 
1253 	return (AHCI_SUCCESS);
1254 }
1255 
1256 /*
1257  * Unregisters the ahci with sata framework.
1258  */
1259 static int
1260 ahci_unregister_sata_hba_tran(ahci_ctl_t *ahci_ctlp)
1261 {
1262 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
1263 	    "ahci_unregister_sata_hba_tran enter", NULL);
1264 
1265 	/* Detach from the SATA framework. */
1266 	if (sata_hba_detach(ahci_ctlp->ahcictl_dip, DDI_DETACH) !=
1267 	    DDI_SUCCESS) {
1268 		return (AHCI_FAILURE);
1269 	}
1270 
1271 	/* Deallocate sata_hba_tran. */
1272 	kmem_free((void *)ahci_ctlp->ahcictl_sata_hba_tran,
1273 	    sizeof (sata_hba_tran_t));
1274 
1275 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
1276 	ahci_ctlp->ahcictl_sata_hba_tran = NULL;
1277 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
1278 
1279 	return (AHCI_SUCCESS);
1280 }
1281 
1282 #define	SET_PORTSTR(str, addrp)						\
1283 	if (AHCI_ADDR_IS_PORT(addrp))					\
1284 		(void) sprintf((str), "%d", (addrp)->aa_port);		\
1285 	else if (AHCI_ADDR_IS_PMULT(addrp))				\
1286 		(void) sprintf((str), "%d (pmult)", (addrp)->aa_port);	\
1287 	else								\
1288 		(void) sprintf((str), "%d:%d", (addrp)->aa_port,	\
1289 		    (addrp)->aa_pmport);
1290 
1291 /*
1292  * ahci_tran_probe_port is called by SATA framework. It returns port state,
1293  * port status registers and an attached device type via sata_device
1294  * structure.
1295  *
1296  * We return the cached information from a previous hardware probe. The
1297  * actual hardware probing itself was done either from within
1298  * ahci_initialize_controller() during the driver attach or from a phy
1299  * ready change interrupt handler.
1300  */
1301 static int
1302 ahci_tran_probe_port(dev_info_t *dip, sata_device_t *sd)
1303 {
1304 	ahci_ctl_t *ahci_ctlp;
1305 	ahci_port_t *ahci_portp;
1306 	ahci_addr_t addr, pmult_addr;
1307 	uint8_t cport = sd->satadev_addr.cport;
1308 	char portstr[10];
1309 	uint8_t device_type;
1310 	uint32_t port_state;
1311 	uint8_t port;
1312 	int rval = SATA_SUCCESS, rval_init;
1313 
1314 	ahci_ctlp = ddi_get_soft_state(ahci_statep, ddi_get_instance(dip));
1315 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
1316 
1317 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
1318 
1319 	mutex_enter(&ahci_portp->ahciport_mutex);
1320 
1321 	ahci_get_ahci_addr(ahci_ctlp, sd, &addr);
1322 	ASSERT(AHCI_ADDR_IS_VALID(&addr));
1323 	SET_PORTSTR(portstr, &addr);
1324 
1325 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
1326 	    "ahci_tran_probe_port enter: port %s", portstr);
1327 
1328 	if ((AHCI_ADDR_IS_PMULT(&addr) || AHCI_ADDR_IS_PMPORT(&addr)) &&
1329 	    (ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT ||
1330 	    ahci_portp->ahciport_pmult_info == NULL)) {
1331 		/* port mutliplier is removed. */
1332 		AHCIDBG(AHCIDBG_PMULT, ahci_ctlp,
1333 		    "ahci_tran_probe_port: "
1334 		    "pmult is removed from port %s", portstr);
1335 		mutex_exit(&ahci_portp->ahciport_mutex);
1336 		return (SATA_FAILURE);
1337 	}
1338 
1339 	/*
1340 	 * The sata_device may refer to
1341 	 * 1. A controller port.
1342 	 *    A controller port should be ready here.
1343 	 * 2. A port multiplier.
1344 	 *    SATA_ADDR_PMULT_SPEC - if it is not initialized yet, initialize
1345 	 *    it and register the port multiplier to the framework.
1346 	 *    SATA_ADDR_PMULT - check the status of all its device ports.
1347 	 * 3. A port multiplier port.
1348 	 *    If it has not been initialized, initialized it.
1349 	 *
1350 	 * A port multiplier or a port multiplier port may require some
1351 	 * initialization because we cannot do these time-consuming jobs in an
1352 	 * interrupt context.
1353 	 */
1354 	if (sd->satadev_addr.qual & SATA_ADDR_PMULT_SPEC) {
1355 		AHCI_ADDR_SET_PMULT(&pmult_addr, port);
1356 		/* Initialize registers on a port multiplier */
1357 		rval_init = ahci_initialize_pmult(ahci_ctlp,
1358 		    ahci_portp, &pmult_addr, sd);
1359 		if (rval_init != AHCI_SUCCESS) {
1360 			AHCIDBG(AHCIDBG_PMULT, ahci_ctlp,
1361 			    "ahci_tran_probe_port: "
1362 			    "pmult initialization failed.", NULL);
1363 			mutex_exit(&ahci_portp->ahciport_mutex);
1364 			return (SATA_FAILURE);
1365 		}
1366 	} else if (sd->satadev_addr.qual & SATA_ADDR_PMULT) {
1367 		/* Check pmports hotplug events */
1368 		(void) ahci_probe_pmult(ahci_ctlp, ahci_portp, &addr);
1369 	} else if (sd->satadev_addr.qual & (SATA_ADDR_PMPORT |
1370 	    SATA_ADDR_DPMPORT)) {
1371 		if (ahci_probe_pmport(ahci_ctlp, ahci_portp,
1372 		    &addr, sd) != AHCI_SUCCESS) {
1373 			rval = SATA_FAILURE;
1374 			goto out;
1375 		}
1376 	}
1377 
1378 	/* Update port state and device type */
1379 	port_state = AHCIPORT_GET_STATE(ahci_portp, &addr);
1380 
1381 	switch (port_state) {
1382 
1383 	case SATA_PSTATE_FAILED:
1384 		sd->satadev_state = SATA_PSTATE_FAILED;
1385 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1386 		    "ahci_tran_probe_port: port %s PORT FAILED", portstr);
1387 		goto out;
1388 
1389 	case SATA_PSTATE_SHUTDOWN:
1390 		sd->satadev_state = SATA_PSTATE_SHUTDOWN;
1391 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1392 		    "ahci_tran_probe_port: port %s PORT SHUTDOWN", portstr);
1393 		goto out;
1394 
1395 	case SATA_PSTATE_PWROFF:
1396 		sd->satadev_state = SATA_PSTATE_PWROFF;
1397 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1398 		    "ahci_tran_probe_port: port %s PORT PWROFF", portstr);
1399 		goto out;
1400 
1401 	case SATA_PSTATE_PWRON:
1402 		sd->satadev_state = SATA_PSTATE_PWRON;
1403 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1404 		    "ahci_tran_probe_port: port %s PORT PWRON", portstr);
1405 		break;
1406 
1407 	default:
1408 		sd->satadev_state = port_state;
1409 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1410 		    "ahci_tran_probe_port: port %s PORT NORMAL %x",
1411 		    portstr, port_state);
1412 		break;
1413 	}
1414 
1415 	device_type = AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr);
1416 
1417 	switch (device_type) {
1418 
1419 	case SATA_DTYPE_ATADISK:
1420 		sd->satadev_type = SATA_DTYPE_ATADISK;
1421 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1422 		    "ahci_tran_probe_port: port %s DISK found", portstr);
1423 		break;
1424 
1425 	case SATA_DTYPE_ATAPI:
1426 		/*
1427 		 * HBA driver only knows it's an ATAPI device, and don't know
1428 		 * it's CD/DVD, tape or ATAPI disk because the ATAPI device
1429 		 * type need to be determined by checking IDENTIFY PACKET
1430 		 * DEVICE data
1431 		 */
1432 		sd->satadev_type = SATA_DTYPE_ATAPI;
1433 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1434 		    "ahci_tran_probe_port: port %s ATAPI found", portstr);
1435 		break;
1436 
1437 	case SATA_DTYPE_PMULT:
1438 		ASSERT(AHCI_ADDR_IS_PORT(&addr)||AHCI_ADDR_IS_PMULT(&addr));
1439 		sd->satadev_type = SATA_DTYPE_PMULT;
1440 
1441 		/* Update the number of pmports. */
1442 		ASSERT(ahci_portp->ahciport_pmult_info != NULL);
1443 		sd->satadev_add_info = ahci_portp->
1444 		    ahciport_pmult_info->ahcipmi_num_dev_ports;
1445 
1446 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1447 		    "ahci_tran_probe_port: port %s Port Multiplier found",
1448 		    portstr);
1449 		break;
1450 
1451 	case SATA_DTYPE_UNKNOWN:
1452 		sd->satadev_type = SATA_DTYPE_UNKNOWN;
1453 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1454 		    "ahci_tran_probe_port: port %s Unknown device found",
1455 		    portstr);
1456 		break;
1457 
1458 	default:
1459 		/* we don't support any other device types */
1460 		sd->satadev_type = SATA_DTYPE_NONE;
1461 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1462 		    "ahci_tran_probe_port: port %s No device found", portstr);
1463 		break;
1464 	}
1465 
1466 out:
1467 	/* Register update only fails while probing a pmult/pmport */
1468 	if (AHCI_ADDR_IS_PORT(&addr) || AHCI_ADDR_IS_PMULT(&addr)) {
1469 		ahci_update_sata_registers(ahci_ctlp, port, sd);
1470 	} else if (AHCI_ADDR_IS_PMPORT(&addr)) {
1471 		if (port_state & SATA_STATE_READY)
1472 			if (ahci_update_pmult_pscr(ahci_ctlp,
1473 			    &addr, sd) != AHCI_SUCCESS)
1474 				rval = SATA_FAILURE;
1475 	}
1476 
1477 	/* Check handles for the sata registers access */
1478 	if ((ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) ||
1479 	    (ahci_check_port_handle(ahci_ctlp, port) != DDI_SUCCESS)) {
1480 		ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
1481 		    DDI_SERVICE_UNAFFECTED);
1482 		rval = SATA_FAILURE;
1483 	}
1484 
1485 	mutex_exit(&ahci_portp->ahciport_mutex);
1486 	return (rval);
1487 }
1488 
1489 /*
1490  * There are four operation modes in sata framework:
1491  * SATA_OPMODE_INTERRUPTS
1492  * SATA_OPMODE_POLLING
1493  * SATA_OPMODE_ASYNCH
1494  * SATA_OPMODE_SYNCH
1495  *
1496  * Their combined meanings as following:
1497  *
1498  * SATA_OPMODE_SYNCH
1499  * The command has to be completed before sata_tran_start functions returns.
1500  * Either interrupts or polling could be used - it's up to the driver.
1501  * Mode used currently for internal, sata-module initiated operations.
1502  *
1503  * SATA_OPMODE_SYNCH | SATA_OPMODE_INTERRUPTS
1504  * It is the same as the one above.
1505  *
1506  * SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING
1507  * The command has to be completed before sata_tran_start function returns.
1508  * No interrupt used, polling only. This should be the mode used for scsi
1509  * packets with FLAG_NOINTR.
1510  *
1511  * SATA_OPMODE_ASYNCH | SATA_OPMODE_INTERRUPTS
1512  * The command may be queued (callback function specified). Interrupts could
1513  * be used. It's normal operation mode.
1514  */
1515 /*
1516  * Called by sata framework to transport a sata packet down stream.
1517  */
1518 static int
1519 ahci_tran_start(dev_info_t *dip, sata_pkt_t *spkt)
1520 {
1521 	ahci_ctl_t *ahci_ctlp;
1522 	ahci_port_t *ahci_portp;
1523 	ahci_addr_t addr;
1524 	uint8_t	cport = spkt->satapkt_device.satadev_addr.cport;
1525 	uint8_t port;
1526 	char portstr[10];
1527 
1528 	ahci_ctlp = ddi_get_soft_state(ahci_statep, ddi_get_instance(dip));
1529 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
1530 
1531 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
1532 	    "ahci_tran_start enter: cport %d satapkt 0x%p",
1533 	    cport, (void *)spkt);
1534 
1535 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
1536 
1537 	mutex_enter(&ahci_portp->ahciport_mutex);
1538 	ahci_get_ahci_addr(ahci_ctlp, &spkt->satapkt_device, &addr);
1539 	SET_PORTSTR(portstr, &addr);
1540 
1541 	/* Sanity check */
1542 	if (AHCI_ADDR_IS_PMPORT(&addr)) {
1543 		if (ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT ||
1544 		    ahci_portp->ahciport_pmult_info == NULL) {
1545 
1546 			spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
1547 			spkt->satapkt_device.satadev_type = SATA_DTYPE_NONE;
1548 			spkt->satapkt_device.satadev_state = SATA_STATE_UNKNOWN;
1549 			ahci_update_sata_registers(ahci_ctlp, port,
1550 			    &spkt->satapkt_device);
1551 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1552 			    "ahci_tran_start returning PORT_ERROR while "
1553 			    "pmult removed: port: %s", portstr);
1554 			mutex_exit(&ahci_portp->ahciport_mutex);
1555 			return (SATA_TRAN_PORT_ERROR);
1556 		}
1557 
1558 		if (!(AHCIPORT_GET_STATE(ahci_portp, &addr) &
1559 		    SATA_STATE_READY)) {
1560 			if (!ddi_in_panic() ||
1561 			    ahci_initialize_pmport(ahci_ctlp,
1562 			    ahci_portp, &addr) != AHCI_SUCCESS) {
1563 				spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
1564 				spkt->satapkt_device.satadev_type =
1565 				    AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr);
1566 				spkt->satapkt_device.satadev_state =
1567 				    AHCIPORT_GET_STATE(ahci_portp, &addr);
1568 				ahci_update_sata_registers(ahci_ctlp, port,
1569 				    &spkt->satapkt_device);
1570 				AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1571 				    "ahci_tran_start returning PORT_ERROR "
1572 				    "while sub-link is not initialized "
1573 				    "at port: %s", portstr);
1574 				mutex_exit(&ahci_portp->ahciport_mutex);
1575 				return (SATA_TRAN_PORT_ERROR);
1576 			}
1577 		}
1578 	}
1579 
1580 	if (AHCIPORT_GET_STATE(ahci_portp, &addr) & SATA_PSTATE_FAILED ||
1581 	    AHCIPORT_GET_STATE(ahci_portp, &addr) & SATA_PSTATE_SHUTDOWN||
1582 	    AHCIPORT_GET_STATE(ahci_portp, &addr) & SATA_PSTATE_PWROFF) {
1583 		/*
1584 		 * In case the target driver would send the packet before
1585 		 * sata framework can have the opportunity to process those
1586 		 * event reports.
1587 		 */
1588 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
1589 		spkt->satapkt_device.satadev_state =
1590 		    ahci_portp->ahciport_port_state;
1591 		ahci_update_sata_registers(ahci_ctlp, port,
1592 		    &spkt->satapkt_device);
1593 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1594 		    "ahci_tran_start returning PORT_ERROR while "
1595 		    "port in FAILED/SHUTDOWN/PWROFF state: "
1596 		    "port: %s", portstr);
1597 		mutex_exit(&ahci_portp->ahciport_mutex);
1598 		return (SATA_TRAN_PORT_ERROR);
1599 	}
1600 
1601 	if (AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr) == SATA_DTYPE_NONE) {
1602 		/*
1603 		 * ahci_intr_phyrdy_change() may have rendered it to
1604 		 * SATA_DTYPE_NONE.
1605 		 */
1606 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
1607 		spkt->satapkt_device.satadev_type = SATA_DTYPE_NONE;
1608 		spkt->satapkt_device.satadev_state =
1609 		    ahci_portp->ahciport_port_state;
1610 		ahci_update_sata_registers(ahci_ctlp, port,
1611 		    &spkt->satapkt_device);
1612 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1613 		    "ahci_tran_start returning PORT_ERROR while "
1614 		    "no device attached: port: %s", portstr);
1615 		mutex_exit(&ahci_portp->ahciport_mutex);
1616 		return (SATA_TRAN_PORT_ERROR);
1617 	}
1618 
1619 	/* R/W PMULT command will occupy the whole HBA port */
1620 	if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
1621 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1622 		    "ahci_tran_start returning BUSY while "
1623 		    "executing READ/WRITE PORT-MULT command: "
1624 		    "port: %s", portstr);
1625 		spkt->satapkt_reason = SATA_PKT_BUSY;
1626 		mutex_exit(&ahci_portp->ahciport_mutex);
1627 		return (SATA_TRAN_BUSY);
1628 	}
1629 
1630 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_HOTPLUG) {
1631 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1632 		    "ahci_tran_start returning BUSY while "
1633 		    "hot-plug in progress: port: %s", portstr);
1634 		spkt->satapkt_reason = SATA_PKT_BUSY;
1635 		mutex_exit(&ahci_portp->ahciport_mutex);
1636 		return (SATA_TRAN_BUSY);
1637 	}
1638 
1639 	/*
1640 	 * SATA HBA driver should remember that a device was reset and it
1641 	 * is supposed to reject any packets which do not specify either
1642 	 * SATA_IGNORE_DEV_RESET_STATE or SATA_CLEAR_DEV_RESET_STATE.
1643 	 *
1644 	 * This is to prevent a race condition when a device was arbitrarily
1645 	 * reset by the HBA driver (and lost it's setting) and a target
1646 	 * driver sending some commands to a device before the sata framework
1647 	 * has a chance to restore the device setting (such as cache enable/
1648 	 * disable or other resettable stuff).
1649 	 */
1650 	/*
1651 	 * It is unnecessary to use specific flags to indicate
1652 	 * reset_in_progress for a pmport. While mopping, all command will be
1653 	 * mopped so that the entire HBA port is being dealt as a single
1654 	 * object.
1655 	 */
1656 	if (spkt->satapkt_cmd.satacmd_flags.sata_clear_dev_reset) {
1657 		ahci_portp->ahciport_reset_in_progress = 0;
1658 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1659 		    "ahci_tran_start [CLEAR] the "
1660 		    "reset_in_progress for port: %d", port);
1661 	}
1662 
1663 	if (ahci_portp->ahciport_reset_in_progress &&
1664 	    ! spkt->satapkt_cmd.satacmd_flags.sata_ignore_dev_reset &&
1665 	    ! ddi_in_panic()) {
1666 		spkt->satapkt_reason = SATA_PKT_BUSY;
1667 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1668 		    "ahci_tran_start returning BUSY while "
1669 		    "reset in progress: port: %d", port);
1670 		mutex_exit(&ahci_portp->ahciport_mutex);
1671 		return (SATA_TRAN_BUSY);
1672 	}
1673 
1674 #ifdef AHCI_DEBUG
1675 	if (spkt->satapkt_cmd.satacmd_flags.sata_ignore_dev_reset) {
1676 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1677 		    "ahci_tran_start: packet 0x%p [PASSTHRU] at port %d",
1678 		    spkt, port);
1679 	}
1680 #endif
1681 
1682 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
1683 		spkt->satapkt_reason = SATA_PKT_BUSY;
1684 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1685 		    "ahci_tran_start returning BUSY while "
1686 		    "mopping in progress: port: %d", port);
1687 		mutex_exit(&ahci_portp->ahciport_mutex);
1688 		return (SATA_TRAN_BUSY);
1689 	}
1690 
1691 	if (ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) {
1692 		ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
1693 		    DDI_SERVICE_UNAFFECTED);
1694 		mutex_exit(&ahci_portp->ahciport_mutex);
1695 		return (SATA_TRAN_BUSY);
1696 	}
1697 
1698 	if (spkt->satapkt_op_mode &
1699 	    (SATA_OPMODE_SYNCH | SATA_OPMODE_POLLING)) {
1700 		/*
1701 		 * If a SYNC command to be executed in interrupt context,
1702 		 * bounce it back to sata module.
1703 		 */
1704 		if (!(spkt->satapkt_op_mode & SATA_OPMODE_POLLING) &&
1705 		    servicing_interrupt()) {
1706 			spkt->satapkt_reason = SATA_PKT_BUSY;
1707 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1708 			    "ahci_tran_start returning BUSY while "
1709 			    "sending SYNC mode under interrupt context: "
1710 			    "port : %d", port);
1711 			mutex_exit(&ahci_portp->ahciport_mutex);
1712 			return (SATA_TRAN_BUSY);
1713 		}
1714 
1715 		/* We need to do the sync start now */
1716 		if (ahci_do_sync_start(ahci_ctlp, ahci_portp, &addr,
1717 		    spkt) == AHCI_FAILURE) {
1718 			goto fail_out;
1719 		}
1720 	} else {
1721 		/* Async start, using interrupt */
1722 		if (ahci_deliver_satapkt(ahci_ctlp, ahci_portp, &addr, spkt)
1723 		    == AHCI_FAILURE) {
1724 			spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
1725 			goto fail_out;
1726 		}
1727 	}
1728 
1729 	AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "ahci_tran_start "
1730 	    "sata tran accepted: port %s", portstr);
1731 
1732 	mutex_exit(&ahci_portp->ahciport_mutex);
1733 	return (SATA_TRAN_ACCEPTED);
1734 
1735 fail_out:
1736 	/*
1737 	 * Failed to deliver packet to the controller.
1738 	 * Check if it's caused by invalid handles.
1739 	 */
1740 	if (ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS ||
1741 	    ahci_check_port_handle(ahci_ctlp, port) != DDI_SUCCESS) {
1742 		spkt->satapkt_device.satadev_type =
1743 		    AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr);
1744 		spkt->satapkt_device.satadev_state =
1745 		    AHCIPORT_GET_STATE(ahci_portp, &addr);
1746 		spkt->satapkt_reason = SATA_PKT_DEV_ERROR;
1747 		mutex_exit(&ahci_portp->ahciport_mutex);
1748 		return (SATA_TRAN_PORT_ERROR);
1749 	}
1750 
1751 	AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_tran_start "
1752 	    "return QUEUE_FULL: port %d", port);
1753 	mutex_exit(&ahci_portp->ahciport_mutex);
1754 	return (SATA_TRAN_QUEUE_FULL);
1755 }
1756 
1757 /*
1758  * SATA_OPMODE_SYNCH flag is set
1759  *
1760  * If SATA_OPMODE_POLLING flag is set, then we must poll the command
1761  * without interrupt, otherwise we can still use the interrupt.
1762  *
1763  * WARNING!!! ahciport_mutex should be acquired before the function
1764  * is called.
1765  */
1766 static int
1767 ahci_do_sync_start(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
1768     ahci_addr_t *addrp, sata_pkt_t *spkt)
1769 {
1770 	int pkt_timeout_ticks;
1771 	uint32_t timeout_tags;
1772 	int rval;
1773 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
1774 	uint8_t port = addrp->aa_port;
1775 
1776 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_do_sync_start enter: "
1777 	    "port %d:%d spkt 0x%p", port, addrp->aa_pmport, spkt);
1778 
1779 	if (spkt->satapkt_op_mode & SATA_OPMODE_POLLING) {
1780 		ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_POLLING;
1781 		if ((rval = ahci_deliver_satapkt(ahci_ctlp, ahci_portp,
1782 		    addrp, spkt)) == AHCI_FAILURE) {
1783 			ahci_portp->ahciport_flags &= ~ AHCI_PORT_FLAG_POLLING;
1784 			return (rval);
1785 		}
1786 
1787 		pkt_timeout_ticks =
1788 		    drv_usectohz((clock_t)spkt->satapkt_time * 1000000);
1789 
1790 		while (spkt->satapkt_reason == SATA_PKT_BUSY) {
1791 			mutex_exit(&ahci_portp->ahciport_mutex);
1792 
1793 			/* Simulate the interrupt */
1794 			ahci_port_intr(ahci_ctlp, ahci_portp, port);
1795 
1796 			drv_usecwait(AHCI_10MS_USECS);
1797 
1798 			mutex_enter(&ahci_portp->ahciport_mutex);
1799 			pkt_timeout_ticks -= AHCI_10MS_TICKS;
1800 			if (pkt_timeout_ticks < 0) {
1801 				cmn_err(CE_WARN, "!ahci%d: ahci_do_sync_start "
1802 				    "port %d satapkt 0x%p timed out\n",
1803 				    instance, port, (void *)spkt);
1804 				timeout_tags = (0x1 << rval);
1805 				mutex_exit(&ahci_portp->ahciport_mutex);
1806 				ahci_timeout_pkts(ahci_ctlp, ahci_portp,
1807 				    port, timeout_tags);
1808 				mutex_enter(&ahci_portp->ahciport_mutex);
1809 			}
1810 		}
1811 		ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_POLLING;
1812 		return (AHCI_SUCCESS);
1813 
1814 	} else {
1815 		if ((rval = ahci_deliver_satapkt(ahci_ctlp, ahci_portp,
1816 		    addrp, spkt)) == AHCI_FAILURE)
1817 			return (rval);
1818 
1819 #if AHCI_DEBUG
1820 		/*
1821 		 * Note that the driver always uses the slot 0 to deliver
1822 		 * REQUEST SENSE or READ LOG EXT command
1823 		 */
1824 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
1825 			ASSERT(rval == 0);
1826 #endif
1827 
1828 		while (spkt->satapkt_reason == SATA_PKT_BUSY)
1829 			cv_wait(&ahci_portp->ahciport_cv,
1830 			    &ahci_portp->ahciport_mutex);
1831 
1832 		return (AHCI_SUCCESS);
1833 	}
1834 }
1835 
1836 /*
1837  * Searches for and claims a free command slot.
1838  *
1839  * Returns value:
1840  *
1841  * AHCI_FAILURE returned only if
1842  *	1. No empty slot left
1843  *	2. Non-queued command requested while queued command(s) is outstanding
1844  *	3. Queued command requested while non-queued command(s) is outstanding
1845  *	4. HBA doesn't support multiple-use of command list while already a
1846  *	   non-queued command is oustanding
1847  *	5. Queued command requested while some queued command(s) has been
1848  *	   outstanding on a different port multiplier port. (AHCI spec 1.2,
1849  *	   9.1.2)
1850  *
1851  * claimed slot number returned if succeeded
1852  *
1853  * NOTE: it will always return slot 0 for following commands to simplify the
1854  * algorithm.
1855  * 	1. REQUEST SENSE or READ LOG EXT command during error recovery process
1856  * 	2. READ/WRITE PORTMULT command
1857  *
1858  * WARNING!!! ahciport_mutex should be acquired before the function
1859  * is called.
1860  */
1861 static int
1862 ahci_claim_free_slot(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
1863     ahci_addr_t *addrp, int command_type)
1864 {
1865 	uint32_t port_cmd_issue;
1866 	uint32_t free_slots;
1867 	int slot;
1868 
1869 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_claim_free_slot enter "
1870 	    "ahciport_pending_tags = 0x%x "
1871 	    "ahciport_pending_ncq_tags = 0x%x",
1872 	    ahci_portp->ahciport_pending_tags,
1873 	    ahci_portp->ahciport_pending_ncq_tags);
1874 
1875 	/*
1876 	 * According to the AHCI spec, system software is responsible to
1877 	 * ensure that queued and non-queued commands are not mixed in
1878 	 * the command list.
1879 	 */
1880 	if (command_type == AHCI_NON_NCQ_CMD) {
1881 		/* Non-NCQ command request */
1882 		if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
1883 			AHCIDBG(AHCIDBG_INFO|AHCIDBG_NCQ, ahci_ctlp,
1884 			    "ahci_claim_free_slot: there is still pending "
1885 			    "queued command(s) in the command list, "
1886 			    "so no available slot for the non-queued "
1887 			    "command", NULL);
1888 			return (AHCI_FAILURE);
1889 		}
1890 		if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
1891 			AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
1892 			    "ahci_claim_free_slot: there is still pending "
1893 			    "read/write port-mult command(s) in command list, "
1894 			    "so no available slot for the non-queued command",
1895 			    NULL);
1896 			return (AHCI_FAILURE);
1897 		}
1898 		if ((ahci_ctlp->ahcictl_cap & AHCI_CAP_NO_MCMDLIST_NONQUEUE) &&
1899 		    NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
1900 			AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1901 			    "ahci_claim_free_slot: HBA cannot support multiple-"
1902 			    "use of the command list for non-queued commands",
1903 			    NULL);
1904 			return (AHCI_FAILURE);
1905 		}
1906 		free_slots = (~ahci_portp->ahciport_pending_tags) &
1907 		    AHCI_SLOT_MASK(ahci_ctlp);
1908 	} else if (command_type == AHCI_NCQ_CMD) {
1909 		/* NCQ command request */
1910 		if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
1911 			AHCIDBG(AHCIDBG_INFO|AHCIDBG_NCQ, ahci_ctlp,
1912 			    "ahci_claim_free_slot: there is still pending "
1913 			    "non-queued command(s) in the command list, "
1914 			    "so no available slot for the queued command",
1915 			    NULL);
1916 			return (AHCI_FAILURE);
1917 		}
1918 
1919 		/*
1920 		 * NCQ commands cannot be sent to different port multiplier
1921 		 * ports in Command-Based Switching mode
1922 		 */
1923 		/*
1924 		 * NOTE: In Command-Based Switching mode, AHCI controller
1925 		 * usually reports a 'Handshake Error' when multiple NCQ
1926 		 * commands are outstanding simultaneously.
1927 		 */
1928 		if (AHCIPORT_DEV_TYPE(ahci_portp, addrp) == SATA_DTYPE_PMULT) {
1929 			ASSERT(ahci_portp->ahciport_pmult_info != NULL);
1930 			if (!(ahci_ctlp->ahcictl_cap & AHCI_CAP_PMULT_FBSS) &&
1931 			    NCQ_CMD_IN_PROGRESS(ahci_portp) &&
1932 			    AHCIPORT_NCQ_PMPORT(ahci_portp) !=
1933 			    addrp->aa_pmport) {
1934 				AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1935 				    "ahci_claim_free_slot: there is still "
1936 				    "pending queued command(s) in the "
1937 				    "command list for another Port Multiplier "
1938 				    "port, so no available slot.", NULL);
1939 				return (AHCI_FAILURE);
1940 			}
1941 		}
1942 
1943 		free_slots = (~ahci_portp->ahciport_pending_ncq_tags) &
1944 		    AHCI_NCQ_SLOT_MASK(ahci_portp);
1945 	} else if (command_type == AHCI_ERR_RETRI_CMD) {
1946 		/* Error retrieval command request */
1947 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
1948 		    "ahci_claim_free_slot: slot 0 is allocated for REQUEST "
1949 		    "SENSE or READ LOG EXT command", NULL);
1950 		slot = 0;
1951 		goto out;
1952 	} else if (command_type == AHCI_RDWR_PMULT_CMD) {
1953 		/*
1954 		 * An extra check on PxCI. Sometimes PxCI bits may not be
1955 		 * cleared during hot-plug or error recovery process.
1956 		 */
1957 		port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
1958 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, addrp->aa_port));
1959 
1960 		if (port_cmd_issue != 0) {
1961 			AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
1962 			    "ahci_claim_free_slot: there is still pending "
1963 			    "command(s) in command list (0x%x/0x%x, PxCI %x),"
1964 			    "so no available slot for R/W PMULT command.",
1965 			    NON_NCQ_CMD_IN_PROGRESS(ahci_portp),
1966 			    NCQ_CMD_IN_PROGRESS(ahci_portp),
1967 			    port_cmd_issue);
1968 			return (AHCI_FAILURE);
1969 		}
1970 
1971 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
1972 		    "ahci_claim_free_slot: slot 0 is allocated for "
1973 		    "READ/WRITE PORTMULT command", NULL);
1974 		slot = 0;
1975 		goto out;
1976 	}
1977 
1978 	slot = ddi_ffs(free_slots) - 1;
1979 	if (slot == -1) {
1980 		AHCIDBG(AHCIDBG_VERBOSE, ahci_ctlp,
1981 		    "ahci_claim_free_slot: no empty slots", NULL);
1982 		return (AHCI_FAILURE);
1983 	}
1984 
1985 	/*
1986 	 * According to the AHCI spec, to allow a simple mechanism for the
1987 	 * HBA to map command list slots to queue entries, software must
1988 	 * match the tag number it uses to the slot it is placing the command
1989 	 * in. For example, if a queued command is placed in slot 5, the tag
1990 	 * for that command must be 5.
1991 	 */
1992 	if (command_type == AHCI_NCQ_CMD) {
1993 		ahci_portp->ahciport_pending_ncq_tags |= (0x1 << slot);
1994 		if (AHCI_ADDR_IS_PMPORT(addrp)) {
1995 			ASSERT(ahci_portp->ahciport_pmult_info != NULL);
1996 			AHCIPORT_NCQ_PMPORT(ahci_portp) = addrp->aa_pmport;
1997 		}
1998 	}
1999 
2000 	ahci_portp->ahciport_pending_tags |= (0x1 << slot);
2001 
2002 out:
2003 	AHCIDBG(AHCIDBG_VERBOSE, ahci_ctlp,
2004 	    "ahci_claim_free_slot: found slot: 0x%x", slot);
2005 
2006 	return (slot);
2007 }
2008 
2009 /*
2010  * Builds the Command Table for the sata packet and delivers it to controller.
2011  *
2012  * Returns:
2013  * 	slot number if we can obtain a slot successfully
2014  *	otherwise, return AHCI_FAILURE
2015  *
2016  * WARNING!!! ahciport_mutex should be acquired before the function is called.
2017  */
2018 static int
2019 ahci_deliver_satapkt(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
2020     ahci_addr_t *addrp, sata_pkt_t *spkt)
2021 {
2022 	int cmd_slot;
2023 	sata_cmd_t *scmd;
2024 	ahci_fis_h2d_register_t *h2d_register_fisp;
2025 	ahci_cmd_table_t *cmd_table;
2026 	ahci_cmd_header_t *cmd_header;
2027 	int ncookies;
2028 	int i;
2029 	int command_type = AHCI_NON_NCQ_CMD;
2030 	int ncq_qdepth;
2031 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
2032 	uint8_t port, pmport;
2033 #if AHCI_DEBUG
2034 	uint32_t *ptr;
2035 	uint8_t *ptr2;
2036 #endif
2037 
2038 	port = addrp->aa_port;
2039 	pmport = addrp->aa_pmport;
2040 
2041 	spkt->satapkt_reason = SATA_PKT_BUSY;
2042 
2043 	scmd = &spkt->satapkt_cmd;
2044 
2045 	/* Check if the command is a NCQ command */
2046 	if (scmd->satacmd_cmd_reg == SATAC_READ_FPDMA_QUEUED ||
2047 	    scmd->satacmd_cmd_reg == SATAC_WRITE_FPDMA_QUEUED) {
2048 		command_type = AHCI_NCQ_CMD;
2049 
2050 		/*
2051 		 * When NCQ is support, system software must determine the
2052 		 * maximum tag allowed by the device and the HBA, and it
2053 		 * must use a value not beyond of the lower bound of the two.
2054 		 *
2055 		 * Sata module is going to calculate the qdepth and send
2056 		 * down to HBA driver via sata_cmd.
2057 		 */
2058 		ncq_qdepth = scmd->satacmd_flags.sata_max_queue_depth + 1;
2059 
2060 		/*
2061 		 * At the moment, the driver doesn't support the dynamic
2062 		 * setting of the maximum ncq depth, and the value can be
2063 		 * set either during the attach or after hot-plug insertion.
2064 		 */
2065 		if (ahci_portp->ahciport_max_ncq_tags == 0) {
2066 			ahci_portp->ahciport_max_ncq_tags = ncq_qdepth;
2067 			AHCIDBG(AHCIDBG_NCQ, ahci_ctlp,
2068 			    "ahci_deliver_satapkt: port %d the max tags for "
2069 			    "NCQ command is %d", port, ncq_qdepth);
2070 		} else {
2071 			if (ncq_qdepth != ahci_portp->ahciport_max_ncq_tags) {
2072 				cmn_err(CE_WARN, "!ahci%d: ahci_deliver_satapkt"
2073 				    " port %d the max tag for NCQ command is "
2074 				    "requested to change from %d to %d, at the"
2075 				    " moment the driver doesn't support the "
2076 				    "dynamic change so it's going to "
2077 				    "still use the previous tag value",
2078 				    instance, port,
2079 				    ahci_portp->ahciport_max_ncq_tags,
2080 				    ncq_qdepth);
2081 			}
2082 		}
2083 	}
2084 
2085 	/* Check if the command is an error retrieval command */
2086 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
2087 		command_type = AHCI_ERR_RETRI_CMD;
2088 
2089 	/* Check if the command is an read/write pmult command */
2090 	if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp))
2091 		command_type = AHCI_RDWR_PMULT_CMD;
2092 
2093 	/* Check if there is an empty command slot */
2094 	cmd_slot = ahci_claim_free_slot(ahci_ctlp, ahci_portp,
2095 	    addrp, command_type);
2096 	if (cmd_slot == AHCI_FAILURE) {
2097 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "no free command slot", NULL);
2098 		return (AHCI_FAILURE);
2099 	}
2100 
2101 	AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INFO, ahci_ctlp,
2102 	    "ahci_deliver_satapkt enter: cmd_reg: 0x%x, cmd_slot: 0x%x, "
2103 	    "port: %d, satapkt: 0x%p", scmd->satacmd_cmd_reg,
2104 	    cmd_slot, port, (void *)spkt);
2105 
2106 	cmd_table = ahci_portp->ahciport_cmd_tables[cmd_slot];
2107 	bzero((void *)cmd_table, ahci_cmd_table_size);
2108 
2109 	/* For data transfer operations, it is the H2D Register FIS */
2110 	h2d_register_fisp =
2111 	    &(cmd_table->ahcict_command_fis.ahcifc_fis.ahcifc_h2d_register);
2112 
2113 	SET_FIS_TYPE(h2d_register_fisp, AHCI_H2D_REGISTER_FIS_TYPE);
2114 
2115 	/*
2116 	 * PMP field only make sense when target is a port multiplier or a
2117 	 * device behind a port multiplier. Otherwise should set it to 0.
2118 	 */
2119 	if (AHCI_ADDR_IS_PMULT(addrp) || AHCI_ADDR_IS_PMPORT(addrp))
2120 		SET_FIS_PMP(h2d_register_fisp, pmport);
2121 
2122 	SET_FIS_CDMDEVCTL(h2d_register_fisp, 1);
2123 	SET_FIS_COMMAND(h2d_register_fisp, scmd->satacmd_cmd_reg);
2124 	SET_FIS_FEATURES(h2d_register_fisp, scmd->satacmd_features_reg);
2125 	SET_FIS_SECTOR_COUNT(h2d_register_fisp, scmd->satacmd_sec_count_lsb);
2126 
2127 	switch (scmd->satacmd_addr_type) {
2128 
2129 	case 0:
2130 		/*
2131 		 * satacmd_addr_type will be 0 for the commands below:
2132 		 * 	ATAPI command
2133 		 * 	SATAC_IDLE_IM
2134 		 * 	SATAC_STANDBY_IM
2135 		 * 	SATAC_DOWNLOAD_MICROCODE
2136 		 * 	SATAC_FLUSH_CACHE
2137 		 * 	SATAC_SET_FEATURES
2138 		 * 	SATAC_SMART
2139 		 * 	SATAC_ID_PACKET_DEVICE
2140 		 * 	SATAC_ID_DEVICE
2141 		 * 	SATAC_READ_PORTMULT
2142 		 * 	SATAC_WRITE_PORTMULT
2143 		 */
2144 		/* FALLTHRU */
2145 
2146 	case ATA_ADDR_LBA:
2147 		/* FALLTHRU */
2148 
2149 	case ATA_ADDR_LBA28:
2150 		/* LBA[7:0] */
2151 		SET_FIS_SECTOR(h2d_register_fisp, scmd->satacmd_lba_low_lsb);
2152 
2153 		/* LBA[15:8] */
2154 		SET_FIS_CYL_LOW(h2d_register_fisp, scmd->satacmd_lba_mid_lsb);
2155 
2156 		/* LBA[23:16] */
2157 		SET_FIS_CYL_HI(h2d_register_fisp, scmd->satacmd_lba_high_lsb);
2158 
2159 		/* LBA [27:24] (also called dev_head) */
2160 		SET_FIS_DEV_HEAD(h2d_register_fisp, scmd->satacmd_device_reg);
2161 
2162 		break;
2163 
2164 	case ATA_ADDR_LBA48:
2165 		/* LBA[7:0] */
2166 		SET_FIS_SECTOR(h2d_register_fisp, scmd->satacmd_lba_low_lsb);
2167 
2168 		/* LBA[15:8] */
2169 		SET_FIS_CYL_LOW(h2d_register_fisp, scmd->satacmd_lba_mid_lsb);
2170 
2171 		/* LBA[23:16] */
2172 		SET_FIS_CYL_HI(h2d_register_fisp, scmd->satacmd_lba_high_lsb);
2173 
2174 		/* LBA [31:24] */
2175 		SET_FIS_SECTOR_EXP(h2d_register_fisp,
2176 		    scmd->satacmd_lba_low_msb);
2177 
2178 		/* LBA [39:32] */
2179 		SET_FIS_CYL_LOW_EXP(h2d_register_fisp,
2180 		    scmd->satacmd_lba_mid_msb);
2181 
2182 		/* LBA [47:40] */
2183 		SET_FIS_CYL_HI_EXP(h2d_register_fisp,
2184 		    scmd->satacmd_lba_high_msb);
2185 
2186 		/* Set dev_head */
2187 		SET_FIS_DEV_HEAD(h2d_register_fisp,
2188 		    scmd->satacmd_device_reg);
2189 
2190 		/* Set the extended sector count and features */
2191 		SET_FIS_SECTOR_COUNT_EXP(h2d_register_fisp,
2192 		    scmd->satacmd_sec_count_msb);
2193 		SET_FIS_FEATURES_EXP(h2d_register_fisp,
2194 		    scmd->satacmd_features_reg_ext);
2195 		break;
2196 	}
2197 
2198 	/*
2199 	 * For NCQ command (READ/WRITE FPDMA QUEUED), sector count 7:0 is
2200 	 * filled into features field, and sector count 8:15 is filled into
2201 	 * features (exp) field. The hba driver doesn't need to anything
2202 	 * special with regard to this, since sata framework has already
2203 	 * done so.
2204 	 *
2205 	 * However the driver needs to make sure TAG is filled into sector
2206 	 * field.
2207 	 */
2208 	if (command_type == AHCI_NCQ_CMD) {
2209 		SET_FIS_SECTOR_COUNT(h2d_register_fisp,
2210 		    (cmd_slot << SATA_TAG_QUEUING_SHIFT));
2211 	}
2212 
2213 	ncookies = scmd->satacmd_num_dma_cookies;
2214 	AHCIDBG(AHCIDBG_PRDT, ahci_ctlp,
2215 	    "ncookies = 0x%x, ahci_dma_prdt_number = 0x%x",
2216 	    ncookies, ahci_dma_prdt_number);
2217 
2218 	ASSERT(ncookies <= ahci_dma_prdt_number);
2219 	ahci_portp->ahciport_prd_bytecounts[cmd_slot] = 0;
2220 
2221 	/* *** now fill the scatter gather list ******* */
2222 	for (i = 0; i < ncookies; i++) {
2223 		cmd_table->ahcict_prdt[i].ahcipi_data_base_addr =
2224 		    scmd->satacmd_dma_cookie_list[i]._dmu._dmac_la[0];
2225 		cmd_table->ahcict_prdt[i].ahcipi_data_base_addr_upper =
2226 		    scmd->satacmd_dma_cookie_list[i]._dmu._dmac_la[1];
2227 		cmd_table->ahcict_prdt[i].ahcipi_descr_info =
2228 		    scmd->satacmd_dma_cookie_list[i].dmac_size - 1;
2229 		ahci_portp->ahciport_prd_bytecounts[cmd_slot] +=
2230 		    scmd->satacmd_dma_cookie_list[i].dmac_size;
2231 	}
2232 
2233 	AHCIDBG(AHCIDBG_PRDT, ahci_ctlp,
2234 	    "ahciport_prd_bytecounts 0x%x for cmd_slot 0x%x",
2235 	    ahci_portp->ahciport_prd_bytecounts[cmd_slot], cmd_slot);
2236 
2237 	/* The ACMD field is filled in for ATAPI command */
2238 	if (scmd->satacmd_cmd_reg == SATAC_PACKET) {
2239 		bcopy(scmd->satacmd_acdb, cmd_table->ahcict_atapi_cmd,
2240 		    SATA_ATAPI_MAX_CDB_LEN);
2241 	}
2242 
2243 	/* Set Command Header in Command List */
2244 	cmd_header = &ahci_portp->ahciport_cmd_list[cmd_slot];
2245 	BZERO_DESCR_INFO(cmd_header);
2246 	BZERO_PRD_BYTE_COUNT(cmd_header);
2247 
2248 	/* Set the number of entries in the PRD table */
2249 	SET_PRD_TABLE_LENGTH(cmd_header, ncookies);
2250 
2251 	/* Set the length of the command in the CFIS area */
2252 	SET_COMMAND_FIS_LENGTH(cmd_header, AHCI_H2D_REGISTER_FIS_LENGTH);
2253 
2254 	/*
2255 	 * PMP field only make sense when target is a port multiplier or a
2256 	 * device behind a port multiplier. Otherwise should set it to 0.
2257 	 */
2258 	if (AHCI_ADDR_IS_PMULT(addrp) || AHCI_ADDR_IS_PMPORT(addrp))
2259 		SET_PORT_MULTI_PORT(cmd_header, pmport);
2260 
2261 	AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "command data direction is "
2262 	    "sata_data_direction = 0x%x",
2263 	    scmd->satacmd_flags.sata_data_direction);
2264 
2265 	/* Set A bit if it is an ATAPI command */
2266 	if (scmd->satacmd_cmd_reg == SATAC_PACKET)
2267 		SET_ATAPI(cmd_header, AHCI_CMDHEAD_ATAPI);
2268 
2269 	/* Set W bit if data is going to the device */
2270 	if (scmd->satacmd_flags.sata_data_direction == SATA_DIR_WRITE)
2271 		SET_WRITE(cmd_header, AHCI_CMDHEAD_DATA_WRITE);
2272 
2273 	/*
2274 	 * Set the prefetchable bit - this bit is only valid if the PRDTL
2275 	 * field is non-zero or the ATAPI 'A' bit is set in the command
2276 	 * header. This bit cannot be set when using native command
2277 	 * queuing commands or when using FIS-based switching with a Port
2278 	 * multiplier.
2279 	 */
2280 	if (command_type != AHCI_NCQ_CMD)
2281 		SET_PREFETCHABLE(cmd_header, AHCI_CMDHEAD_PREFETCHABLE);
2282 
2283 	/*
2284 	 * Now remember the sata packet in ahciport_slot_pkts[].
2285 	 * Error retrieval command and r/w port multiplier command will
2286 	 * be stored specifically for each port.
2287 	 */
2288 	if (!ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
2289 	    !RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp))
2290 		ahci_portp->ahciport_slot_pkts[cmd_slot] = spkt;
2291 
2292 	/*
2293 	 * Keep the timeout value
2294 	 */
2295 	ahci_portp->ahciport_slot_timeout[cmd_slot] = spkt->satapkt_time;
2296 
2297 	/*
2298 	 * If the intial timout is less than 1 tick, then make it longer by
2299 	 * 1 tick to avoid immediate timeout
2300 	 */
2301 	if (ahci_portp->ahciport_slot_timeout[cmd_slot] <=
2302 	    ahci_watchdog_timeout)
2303 		ahci_portp->ahciport_slot_timeout[cmd_slot] +=
2304 		    ahci_watchdog_timeout;
2305 
2306 #if AHCI_DEBUG
2307 	if (ahci_debug_flags & AHCIDBG_ATACMD &&
2308 	    scmd->satacmd_cmd_reg != SATAC_PACKET ||
2309 	    ahci_debug_flags & AHCIDBG_ATAPICMD &&
2310 	    scmd->satacmd_cmd_reg == SATAC_PACKET) {
2311 
2312 		/* Dump the command header and table */
2313 		ahci_log(ahci_ctlp, CE_WARN, "\n");
2314 		ahci_log(ahci_ctlp, CE_WARN, "Command header&table for spkt "
2315 		    "0x%p cmd_reg 0x%x port %d", spkt,
2316 		    scmd->satacmd_cmd_reg, port);
2317 		ptr = (uint32_t *)cmd_header;
2318 		ahci_log(ahci_ctlp, CE_WARN,
2319 		    "  Command Header:%8x %8x %8x %8x",
2320 		    ptr[0], ptr[1], ptr[2], ptr[3]);
2321 
2322 		/* Dump the H2D register FIS */
2323 		ptr = (uint32_t *)h2d_register_fisp;
2324 		ahci_log(ahci_ctlp, CE_WARN,
2325 		    "  Command FIS:   %8x %8x %8x %8x",
2326 		    ptr[0], ptr[1], ptr[2], ptr[3]);
2327 
2328 		/* Dump the ACMD register FIS */
2329 		ptr2 = (uint8_t *)&(cmd_table->ahcict_atapi_cmd);
2330 		for (i = 0; i < SATA_ATAPI_MAX_CDB_LEN/8; i++)
2331 			if (ahci_debug_flags & AHCIDBG_ATAPICMD)
2332 				ahci_log(ahci_ctlp, CE_WARN,
2333 				    "  ATAPI command: %2x %2x %2x %2x "
2334 				    "%2x %2x %2x %2x",
2335 				    ptr2[8 * i], ptr2[8 * i + 1],
2336 				    ptr2[8 * i + 2], ptr2[8 * i + 3],
2337 				    ptr2[8 * i + 4], ptr2[8 * i + 5],
2338 				    ptr2[8 * i + 6], ptr2[8 * i + 7]);
2339 
2340 		/* Dump the PRDT */
2341 		for (i = 0; i < ncookies; i++) {
2342 			ptr = (uint32_t *)&(cmd_table->ahcict_prdt[i]);
2343 			ahci_log(ahci_ctlp, CE_WARN,
2344 			    "  Cookie %d:      %8x %8x %8x %8x",
2345 			    i, ptr[0], ptr[1], ptr[2], ptr[3]);
2346 		}
2347 	}
2348 #endif
2349 
2350 	(void) ddi_dma_sync(
2351 	    ahci_portp->ahciport_cmd_tables_dma_handle[cmd_slot],
2352 	    0,
2353 	    ahci_cmd_table_size,
2354 	    DDI_DMA_SYNC_FORDEV);
2355 
2356 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_list_dma_handle,
2357 	    cmd_slot * sizeof (ahci_cmd_header_t),
2358 	    sizeof (ahci_cmd_header_t),
2359 	    DDI_DMA_SYNC_FORDEV);
2360 
2361 	if ((ahci_check_dma_handle(ahci_portp->
2362 	    ahciport_cmd_tables_dma_handle[cmd_slot]) != DDI_FM_OK) ||
2363 	    ahci_check_dma_handle(ahci_portp->
2364 	    ahciport_cmd_list_dma_handle) != DDI_FM_OK) {
2365 		ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
2366 		    DDI_SERVICE_UNAFFECTED);
2367 		return (AHCI_FAILURE);
2368 	}
2369 
2370 	/* Set the corresponding bit in the PxSACT.DS for queued command */
2371 	if (command_type == AHCI_NCQ_CMD) {
2372 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
2373 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port),
2374 		    (0x1 << cmd_slot));
2375 	}
2376 
2377 	/* Indicate to the HBA that a command is active. */
2378 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
2379 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port),
2380 	    (0x1 << cmd_slot));
2381 
2382 	AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "ahci_deliver_satapkt "
2383 	    "exit: port %d", port);
2384 
2385 	/* Make sure the command is started by the PxSACT/PxCI */
2386 	if (ahci_check_acc_handle(ahci_ctlp->
2387 	    ahcictl_ahci_acc_handle) != DDI_FM_OK) {
2388 		ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
2389 		    DDI_SERVICE_UNAFFECTED);
2390 		return (AHCI_FAILURE);
2391 	}
2392 
2393 	return (cmd_slot);
2394 }
2395 
2396 /*
2397  * Called by the sata framework to abort the previously sent packet(s).
2398  *
2399  * Reset device to abort commands.
2400  */
2401 static int
2402 ahci_tran_abort(dev_info_t *dip, sata_pkt_t *spkt, int flag)
2403 {
2404 	ahci_ctl_t *ahci_ctlp;
2405 	ahci_port_t *ahci_portp;
2406 	uint32_t slot_status = 0;
2407 	uint32_t aborted_tags = 0;
2408 	uint32_t finished_tags = 0;
2409 	uint8_t cport = spkt->satapkt_device.satadev_addr.cport;
2410 	uint8_t port;
2411 	int tmp_slot;
2412 	int instance = ddi_get_instance(dip);
2413 
2414 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
2415 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
2416 
2417 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
2418 	    "ahci_tran_abort enter: port %d", port);
2419 
2420 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
2421 	mutex_enter(&ahci_portp->ahciport_mutex);
2422 
2423 	/*
2424 	 * If AHCI_PORT_FLAG_MOPPING flag is set, it means all the pending
2425 	 * commands are being mopped, therefore there is nothing else to do
2426 	 */
2427 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
2428 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
2429 		    "ahci_tran_abort: port %d is in "
2430 		    "mopping process, so just return directly ", port);
2431 		mutex_exit(&ahci_portp->ahciport_mutex);
2432 		return (SATA_SUCCESS);
2433 	}
2434 
2435 	/*
2436 	 * If AHCI_PORT_FLAG_RDWR_PMULT flag is set, it means a R/W PMULT
2437 	 * command is being executed so no other commands is outstanding,
2438 	 * nothing to do.
2439 	 */
2440 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_RDWR_PMULT) {
2441 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
2442 		    "ahci_tran_abort: port %d is reading/writing "
2443 		    "port multiplier, so just return directly ", port);
2444 		mutex_exit(&ahci_portp->ahciport_mutex);
2445 		return (SATA_SUCCESS);
2446 	}
2447 
2448 	if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED |
2449 	    ahci_portp->ahciport_port_state & SATA_PSTATE_SHUTDOWN |
2450 	    ahci_portp->ahciport_port_state & SATA_PSTATE_PWROFF) {
2451 		/*
2452 		 * In case the targer driver would send the request before
2453 		 * sata framework can have the opportunity to process those
2454 		 * event reports.
2455 		 */
2456 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
2457 		spkt->satapkt_device.satadev_state =
2458 		    ahci_portp->ahciport_port_state;
2459 		ahci_update_sata_registers(ahci_ctlp, port,
2460 		    &spkt->satapkt_device);
2461 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
2462 		    "ahci_tran_abort returning SATA_FAILURE while "
2463 		    "port in FAILED/SHUTDOWN/PWROFF state: "
2464 		    "port: %d", port);
2465 		mutex_exit(&ahci_portp->ahciport_mutex);
2466 		return (SATA_FAILURE);
2467 	}
2468 
2469 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
2470 		/*
2471 		 * ahci_intr_phyrdy_change() may have rendered it to
2472 		 * AHCI_PORT_TYPE_NODEV.
2473 		 */
2474 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
2475 		spkt->satapkt_device.satadev_type = SATA_DTYPE_NONE;
2476 		spkt->satapkt_device.satadev_state =
2477 		    ahci_portp->ahciport_port_state;
2478 		ahci_update_sata_registers(ahci_ctlp, port,
2479 		    &spkt->satapkt_device);
2480 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
2481 		    "ahci_tran_abort returning SATA_FAILURE while "
2482 		    "no device attached: port: %d", port);
2483 		mutex_exit(&ahci_portp->ahciport_mutex);
2484 		return (SATA_FAILURE);
2485 	}
2486 
2487 	if (flag == SATA_ABORT_ALL_PACKETS) {
2488 		if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
2489 			aborted_tags = ahci_portp->ahciport_pending_tags;
2490 		else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
2491 			aborted_tags = ahci_portp->ahciport_pending_ncq_tags;
2492 
2493 		cmn_err(CE_NOTE, "!ahci%d: ahci port %d abort all packets",
2494 		    instance, port);
2495 	} else {
2496 		aborted_tags = 0xffffffff;
2497 		/*
2498 		 * Aborting one specific packet, first search the
2499 		 * ahciport_slot_pkts[] list for matching spkt.
2500 		 */
2501 		for (tmp_slot = 0;
2502 		    tmp_slot < ahci_ctlp->ahcictl_num_cmd_slots; tmp_slot++) {
2503 			if (ahci_portp->ahciport_slot_pkts[tmp_slot] == spkt) {
2504 				aborted_tags = (0x1 << tmp_slot);
2505 				break;
2506 			}
2507 		}
2508 
2509 		if (aborted_tags == 0xffffffff) {
2510 			/* request packet is not on the pending list */
2511 			AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
2512 			    "Cannot find the aborting pkt 0x%p on the "
2513 			    "pending list", (void *)spkt);
2514 			ahci_update_sata_registers(ahci_ctlp, port,
2515 			    &spkt->satapkt_device);
2516 			mutex_exit(&ahci_portp->ahciport_mutex);
2517 			return (SATA_FAILURE);
2518 		}
2519 		cmn_err(CE_NOTE, "!ahci%d: ahci port %d abort satapkt 0x%p",
2520 		    instance, port, (void *)spkt);
2521 	}
2522 
2523 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
2524 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2525 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
2526 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
2527 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2528 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
2529 
2530 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
2531 	ahci_portp->ahciport_mop_in_progress++;
2532 
2533 	/*
2534 	 * To abort the packet(s), first we are trying to clear PxCMD.ST
2535 	 * to stop the port, and if the port can be stopped
2536 	 * successfully with PxTFD.STS.BSY and PxTFD.STS.DRQ cleared to '0',
2537 	 * then we just send back the aborted packet(s) with ABORTED flag
2538 	 * and then restart the port by setting PxCMD.ST and PxCMD.FRE.
2539 	 * If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to '1', then we
2540 	 * perform a COMRESET.
2541 	 */
2542 	(void) ahci_restart_port_wait_till_ready(ahci_ctlp,
2543 	    ahci_portp, port, NULL, NULL);
2544 
2545 	/*
2546 	 * Compute which have finished and which need to be retried.
2547 	 *
2548 	 * The finished tags are ahciport_pending_tags/ahciport_pending_ncq_tags
2549 	 * minus the slot_status. The aborted_tags has to be deducted by
2550 	 * finished_tags since we can't possibly abort a tag which had finished
2551 	 * already.
2552 	 */
2553 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
2554 		finished_tags = ahci_portp->ahciport_pending_tags &
2555 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2556 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
2557 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
2558 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
2559 
2560 	aborted_tags &= ~finished_tags;
2561 
2562 	ahci_mop_commands(ahci_ctlp,
2563 	    ahci_portp,
2564 	    slot_status,
2565 	    0, /* failed tags */
2566 	    0, /* timeout tags */
2567 	    aborted_tags,
2568 	    0); /* reset tags */
2569 
2570 	ahci_update_sata_registers(ahci_ctlp, port, &spkt->satapkt_device);
2571 	mutex_exit(&ahci_portp->ahciport_mutex);
2572 
2573 	return (SATA_SUCCESS);
2574 }
2575 
2576 /*
2577  * Used to do device reset and reject all the pending packets on a device
2578  * during the reset operation.
2579  *
2580  * NOTE: ONLY called by ahci_tran_reset_dport
2581  * WARNING!!! ahciport_mutex should be acquired before the function is called.
2582  */
2583 static int
2584 ahci_reset_device_reject_pkts(ahci_ctl_t *ahci_ctlp,
2585     ahci_port_t *ahci_portp, ahci_addr_t *addrp)
2586 {
2587 	uint32_t slot_status = 0;
2588 	uint32_t reset_tags = 0;
2589 	uint32_t finished_tags = 0;
2590 	uint8_t port = addrp->aa_port;
2591 	sata_device_t sdevice;
2592 	int ret;
2593 
2594 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
2595 	    "ahci_reset_device_reject_pkts on port: %d", port);
2596 
2597 	/*
2598 	 * If AHCI_PORT_FLAG_MOPPING flag is set, it means all the pending
2599 	 * commands are being mopped, therefore there is nothing else to do
2600 	 */
2601 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
2602 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
2603 		    "ahci_reset_device_reject_pkts: port %d is in "
2604 		    "mopping process, so return directly ", port);
2605 		return (SATA_SUCCESS);
2606 	}
2607 
2608 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
2609 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2610 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
2611 		reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2612 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
2613 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2614 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
2615 		reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
2616 	}
2617 
2618 	if (ahci_software_reset(ahci_ctlp, ahci_portp, addrp)
2619 	    != AHCI_SUCCESS) {
2620 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
2621 		    "Try to do a port reset after software "
2622 		    "reset failed", port);
2623 		ret = ahci_port_reset(ahci_ctlp, ahci_portp, addrp);
2624 		if (ret != AHCI_SUCCESS) {
2625 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
2626 			    "ahci_reset_device_reject_pkts: port %d "
2627 			    "failed", port);
2628 			return (SATA_FAILURE);
2629 		}
2630 	}
2631 	/* Set the reset in progress flag */
2632 	ahci_portp->ahciport_reset_in_progress = 1;
2633 
2634 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
2635 	ahci_portp->ahciport_mop_in_progress++;
2636 
2637 	/* Indicate to the framework that a reset has happened */
2638 	bzero((void *)&sdevice, sizeof (sata_device_t));
2639 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
2640 	sdevice.satadev_addr.pmport = 0;
2641 	sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
2642 	sdevice.satadev_state = SATA_DSTATE_RESET |
2643 	    SATA_DSTATE_PWR_ACTIVE;
2644 	mutex_exit(&ahci_portp->ahciport_mutex);
2645 	sata_hba_event_notify(
2646 	    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
2647 	    &sdevice,
2648 	    SATA_EVNT_DEVICE_RESET);
2649 	mutex_enter(&ahci_portp->ahciport_mutex);
2650 
2651 	AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
2652 	    "port %d sending event up: SATA_EVNT_DEVICE_RESET", port);
2653 
2654 	/* Next try to mop the pending commands */
2655 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
2656 		finished_tags = ahci_portp->ahciport_pending_tags &
2657 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2658 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
2659 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
2660 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
2661 
2662 	reset_tags &= ~finished_tags;
2663 
2664 	ahci_mop_commands(ahci_ctlp,
2665 	    ahci_portp,
2666 	    slot_status,
2667 	    0, /* failed tags */
2668 	    0, /* timeout tags */
2669 	    0, /* aborted tags */
2670 	    reset_tags); /* reset tags */
2671 
2672 	return (SATA_SUCCESS);
2673 }
2674 
2675 /*
2676  * Used to do device reset and reject all the pending packets on a device
2677  * during the reset operation.
2678  *
2679  * NOTE: ONLY called by ahci_tran_reset_dport
2680  * WARNING!!! ahciport_mutex should be acquired before the function is called.
2681  */
2682 static int
2683 ahci_reset_pmdevice_reject_pkts(ahci_ctl_t *ahci_ctlp,
2684     ahci_port_t *ahci_portp, ahci_addr_t *addrp)
2685 {
2686 	uint32_t finished_tags = 0, reset_tags = 0, slot_status = 0;
2687 	uint8_t port = addrp->aa_port;
2688 	uint8_t pmport = addrp->aa_pmport;
2689 	sata_device_t sdevice;
2690 
2691 	AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_PMULT, ahci_ctlp,
2692 	    "ahci_reset_pmdevice_reject_pkts at port %d:%d", port, pmport);
2693 
2694 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
2695 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
2696 		    "ahci_reset_pmdevice_reject_pkts: port %d is in "
2697 		    "mopping process, so return directly ", port);
2698 		return (SATA_SUCCESS);
2699 	}
2700 
2701 	/* Checking for outstanding commands */
2702 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
2703 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2704 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
2705 		reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2706 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
2707 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2708 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
2709 		reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
2710 	}
2711 
2712 	/* Issue SOFTWARE reset command. */
2713 	if (ahci_software_reset(ahci_ctlp, ahci_portp, addrp)
2714 	    != AHCI_SUCCESS) {
2715 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
2716 		    "Try to do a port reset after software "
2717 		    "reset failed", port);
2718 		return (SATA_FAILURE);
2719 	}
2720 
2721 	/* Set the reset in progress flag */
2722 	ahci_portp->ahciport_reset_in_progress = 1;
2723 
2724 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
2725 	ahci_portp->ahciport_mop_in_progress++;
2726 
2727 	/* Indicate to the framework that a reset has happened */
2728 	bzero((void *)&sdevice, sizeof (sata_device_t));
2729 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
2730 	sdevice.satadev_addr.pmport = pmport;
2731 	if (AHCI_ADDR_IS_PMULT(addrp))
2732 		sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
2733 	else
2734 		sdevice.satadev_addr.qual = SATA_ADDR_DPMPORT;
2735 	sdevice.satadev_state = SATA_DSTATE_RESET |
2736 	    SATA_DSTATE_PWR_ACTIVE;
2737 	mutex_exit(&ahci_portp->ahciport_mutex);
2738 	sata_hba_event_notify(
2739 	    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
2740 	    &sdevice,
2741 	    SATA_EVNT_DEVICE_RESET);
2742 	mutex_enter(&ahci_portp->ahciport_mutex);
2743 
2744 	AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
2745 	    "port %d:%d sending event up: SATA_EVNT_DEVICE_RESET",
2746 	    port, pmport);
2747 
2748 	/* Next try to mop the pending commands */
2749 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
2750 		finished_tags = ahci_portp->ahciport_pending_tags &
2751 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2752 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
2753 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
2754 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
2755 	reset_tags &= ~finished_tags;
2756 
2757 	AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
2758 	    "reset_tags = %x, finished_tags = %x, slot_status = %x",
2759 	    reset_tags, finished_tags, slot_status);
2760 
2761 	/*
2762 	 * NOTE: Because PxCI be only erased by unset PxCMD.ST bit, so even we
2763 	 * try to reset a single device behind a port multiplier will
2764 	 * terminate all the commands on that HBA port. We need mop these
2765 	 * commands as well.
2766 	 */
2767 	ahci_mop_commands(ahci_ctlp,
2768 	    ahci_portp,
2769 	    slot_status,
2770 	    0, /* failed tags */
2771 	    0, /* timeout tags */
2772 	    0, /* aborted tags */
2773 	    reset_tags); /* reset tags */
2774 
2775 	return (SATA_SUCCESS);
2776 }
2777 
2778 /*
2779  * Used to do port reset and reject all the pending packets on a port during
2780  * the reset operation.
2781  *
2782  * WARNING!!! ahciport_mutex should be acquired before the function is called.
2783  */
2784 static int
2785 ahci_reset_port_reject_pkts(ahci_ctl_t *ahci_ctlp,
2786     ahci_port_t *ahci_portp, ahci_addr_t *addrp)
2787 {
2788 	uint32_t slot_status = 0;
2789 	uint32_t reset_tags = 0;
2790 	uint32_t finished_tags = 0;
2791 	uint8_t port = addrp->aa_port;
2792 
2793 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
2794 	    "ahci_reset_port_reject_pkts at port: %d", port);
2795 
2796 	/*
2797 	 * If AHCI_PORT_FLAG_MOPPING flag is set, it means all the pending
2798 	 * commands are being mopped, therefore there is nothing else to do
2799 	 */
2800 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
2801 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
2802 		    "ahci_reset_port_reject_pkts: port %d is in "
2803 		    "mopping process, so return directly ", port);
2804 		return (SATA_SUCCESS);
2805 	}
2806 
2807 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
2808 	ahci_portp->ahciport_mop_in_progress++;
2809 
2810 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
2811 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2812 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
2813 		reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2814 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
2815 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
2816 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
2817 		reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
2818 	}
2819 
2820 	if (ahci_restart_port_wait_till_ready(ahci_ctlp,
2821 	    ahci_portp, port, AHCI_PORT_RESET|AHCI_RESET_NO_EVENTS_UP,
2822 	    NULL) != AHCI_SUCCESS) {
2823 
2824 		/* Clear mop flag */
2825 		ahci_portp->ahciport_mop_in_progress--;
2826 		if (ahci_portp->ahciport_mop_in_progress == 0)
2827 			ahci_portp->ahciport_flags &=
2828 			    ~AHCI_PORT_FLAG_MOPPING;
2829 		return (SATA_FAILURE);
2830 	}
2831 
2832 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
2833 		finished_tags = ahci_portp->ahciport_pending_tags &
2834 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
2835 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
2836 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
2837 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
2838 
2839 	reset_tags &= ~finished_tags;
2840 
2841 	ahci_mop_commands(ahci_ctlp,
2842 	    ahci_portp,
2843 	    slot_status,
2844 	    0, /* failed tags */
2845 	    0, /* timeout tags */
2846 	    0, /* aborted tags */
2847 	    reset_tags); /* reset tags */
2848 
2849 	return (SATA_SUCCESS);
2850 }
2851 
2852 /*
2853  * Used to do hba reset and reject all the pending packets on all ports
2854  * during the reset operation.
2855  */
2856 static int
2857 ahci_reset_hba_reject_pkts(ahci_ctl_t *ahci_ctlp)
2858 {
2859 	ahci_port_t *ahci_portp;
2860 	uint32_t slot_status[AHCI_MAX_PORTS];
2861 	uint32_t reset_tags[AHCI_MAX_PORTS];
2862 	uint32_t finished_tags[AHCI_MAX_PORTS];
2863 	int port;
2864 	int ret = SATA_SUCCESS;
2865 
2866 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
2867 	    "ahci_reset_hba_reject_pkts enter", NULL);
2868 
2869 	bzero(slot_status, sizeof (slot_status));
2870 	bzero(reset_tags, sizeof (reset_tags));
2871 	bzero(finished_tags, sizeof (finished_tags));
2872 
2873 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
2874 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
2875 			continue;
2876 		}
2877 
2878 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
2879 
2880 		mutex_enter(&ahci_portp->ahciport_mutex);
2881 		ahci_portp->ahciport_reset_in_progress = 1;
2882 		if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
2883 			slot_status[port] = ddi_get32(
2884 			    ahci_ctlp->ahcictl_ahci_acc_handle,
2885 			    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
2886 			reset_tags[port] = slot_status[port] &
2887 			    AHCI_SLOT_MASK(ahci_ctlp);
2888 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
2889 			    "port %d: reset_tags = 0x%x pending_tags = 0x%x",
2890 			    port, reset_tags[port],
2891 			    ahci_portp->ahciport_pending_tags);
2892 		} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
2893 			slot_status[port] = ddi_get32(
2894 			    ahci_ctlp->ahcictl_ahci_acc_handle,
2895 			    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
2896 			reset_tags[port] = slot_status[port] &
2897 			    AHCI_NCQ_SLOT_MASK(ahci_portp);
2898 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
2899 			    "port %d: reset_tags = 0x%x pending_tags = 0x%x",
2900 			    port, reset_tags[port],
2901 			    ahci_portp->ahciport_pending_tags);
2902 		}
2903 		mutex_exit(&ahci_portp->ahciport_mutex);
2904 	}
2905 
2906 	if (ahci_hba_reset(ahci_ctlp) != AHCI_SUCCESS) {
2907 		ret = SATA_FAILURE;
2908 	}
2909 
2910 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
2911 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
2912 			continue;
2913 		}
2914 
2915 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
2916 
2917 		mutex_enter(&ahci_portp->ahciport_mutex);
2918 		/*
2919 		 * To prevent recursive enter to ahci_mop_commands, we need
2920 		 * check AHCI_PORT_FLAG_MOPPING flag.
2921 		 */
2922 		if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
2923 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
2924 			    "ahci_reset_hba_reject_pkts: port %d is in "
2925 			    "mopping process, so return directly ", port);
2926 			mutex_exit(&ahci_portp->ahciport_mutex);
2927 			continue;
2928 		}
2929 
2930 		ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
2931 		ahci_portp->ahciport_mop_in_progress++;
2932 
2933 		if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
2934 			finished_tags[port]  =
2935 			    ahci_portp->ahciport_pending_tags &
2936 			    ~slot_status[port] & AHCI_SLOT_MASK(ahci_ctlp);
2937 		else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
2938 			finished_tags[port] =
2939 			    ahci_portp->ahciport_pending_ncq_tags &
2940 			    ~slot_status[port] & AHCI_NCQ_SLOT_MASK(ahci_portp);
2941 
2942 		reset_tags[port] &= ~finished_tags[port];
2943 
2944 		ahci_mop_commands(ahci_ctlp,
2945 		    ahci_portp,
2946 		    slot_status[port],
2947 		    0, /* failed tags */
2948 		    0, /* timeout tags */
2949 		    0, /* aborted tags */
2950 		    reset_tags[port]); /* reset tags */
2951 		mutex_exit(&ahci_portp->ahciport_mutex);
2952 	}
2953 out:
2954 	return (ret);
2955 }
2956 
2957 /*
2958  * Called by sata framework to reset a port(s) or device.
2959  */
2960 static int
2961 ahci_tran_reset_dport(dev_info_t *dip, sata_device_t *sd)
2962 {
2963 	ahci_ctl_t *ahci_ctlp;
2964 	ahci_port_t *ahci_portp;
2965 	ahci_addr_t addr;
2966 	uint8_t cport = sd->satadev_addr.cport;
2967 	uint8_t pmport = sd->satadev_addr.pmport;
2968 	uint8_t port;
2969 	int ret = SATA_SUCCESS;
2970 	int instance = ddi_get_instance(dip);
2971 
2972 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
2973 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
2974 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
2975 
2976 	ahci_get_ahci_addr(ahci_ctlp, sd, &addr);
2977 
2978 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
2979 	    "ahci_tran_reset_dport enter: cport %d", cport);
2980 
2981 	switch (sd->satadev_addr.qual) {
2982 	case SATA_ADDR_PMPORT:
2983 		/*
2984 		 * If we want to issue a COMRESET on a pmport, we need to
2985 		 * reject the outstanding commands on that pmport. According
2986 		 * to AHCI spec, PxCI register could only be cleared by
2987 		 * clearing PxCMD.ST, which will halt the controller port - as
2988 		 * well as other pmports.
2989 		 *
2990 		 * Therefore we directly reset the controller port for
2991 		 * simplicity. ahci_tran_probe_port() will handle reset stuff
2992 		 * like initializing the given pmport.
2993 		 */
2994 		/* FALLTHRU */
2995 	case SATA_ADDR_CPORT:
2996 		/* Port reset */
2997 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
2998 		cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport "
2999 		    "port %d reset port", instance, port);
3000 
3001 		mutex_enter(&ahci_portp->ahciport_mutex);
3002 		ret = ahci_reset_port_reject_pkts(ahci_ctlp, ahci_portp, &addr);
3003 		mutex_exit(&ahci_portp->ahciport_mutex);
3004 
3005 		break;
3006 
3007 	case SATA_ADDR_DPMPORT:
3008 		cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport "
3009 		    "port %d:%d reset device", instance, port, pmport);
3010 		/* FALLTHRU */
3011 	case SATA_ADDR_DCPORT:
3012 		/* Device reset */
3013 		if (sd->satadev_addr.qual == SATA_ADDR_DCPORT)
3014 			cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport "
3015 			    "port %d reset device", instance, port);
3016 
3017 		mutex_enter(&ahci_portp->ahciport_mutex);
3018 		/*
3019 		 * software reset request must be sent to SATA_PMULT_HOSTPORT
3020 		 * if target is a port multiplier:
3021 		 */
3022 		if (sd->satadev_addr.qual == SATA_ADDR_DCPORT &&
3023 		    ahci_portp->ahciport_device_type == SATA_DTYPE_PMULT)
3024 			AHCI_ADDR_SET_PMULT(&addr, port);
3025 
3026 		if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED |
3027 		    ahci_portp->ahciport_port_state & SATA_PSTATE_SHUTDOWN |
3028 		    ahci_portp->ahciport_port_state & SATA_PSTATE_PWROFF) {
3029 			/*
3030 			 * In case the targer driver would send the request
3031 			 * before sata framework can have the opportunity to
3032 			 * process those event reports.
3033 			 */
3034 			sd->satadev_state = ahci_portp->ahciport_port_state;
3035 			ahci_update_sata_registers(ahci_ctlp, port, sd);
3036 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
3037 			    "ahci_tran_reset_dport returning SATA_FAILURE "
3038 			    "while port in FAILED/SHUTDOWN/PWROFF state: "
3039 			    "port: %d", port);
3040 			mutex_exit(&ahci_portp->ahciport_mutex);
3041 			ret = SATA_FAILURE;
3042 			break;
3043 		}
3044 
3045 		if (AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr) ==
3046 		    SATA_DTYPE_NONE) {
3047 			/*
3048 			 * ahci_intr_phyrdy_change() may have rendered it to
3049 			 * AHCI_PORT_TYPE_NODEV.
3050 			 */
3051 			sd->satadev_type = SATA_DTYPE_NONE;
3052 			sd->satadev_state = AHCIPORT_GET_STATE(ahci_portp,
3053 			    &addr);
3054 			ahci_update_sata_registers(ahci_ctlp, port, sd);
3055 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
3056 			    "ahci_tran_reset_dport returning SATA_FAILURE "
3057 			    "while no device attached: port: %d", port);
3058 			mutex_exit(&ahci_portp->ahciport_mutex);
3059 			ret = SATA_FAILURE;
3060 			break;
3061 		}
3062 
3063 		if (AHCI_ADDR_IS_PORT(&addr)) {
3064 			ret = ahci_reset_device_reject_pkts(ahci_ctlp,
3065 			    ahci_portp, &addr);
3066 		} else {
3067 			ret = ahci_reset_pmdevice_reject_pkts(ahci_ctlp,
3068 			    ahci_portp, &addr);
3069 		}
3070 
3071 		mutex_exit(&ahci_portp->ahciport_mutex);
3072 		break;
3073 
3074 	case SATA_ADDR_CNTRL:
3075 		/* Reset the whole controller */
3076 		cmn_err(CE_NOTE, "!ahci%d: ahci_tran_reset_dport "
3077 		    "reset the whole hba", instance);
3078 		ret = ahci_reset_hba_reject_pkts(ahci_ctlp);
3079 		break;
3080 
3081 	default:
3082 		ret = SATA_FAILURE;
3083 	}
3084 
3085 	return (ret);
3086 }
3087 
3088 /*
3089  * Called by sata framework to activate a port as part of hotplug.
3090  * (cfgadm -c connect satax/y)
3091  * Support port multiplier.
3092  */
3093 static int
3094 ahci_tran_hotplug_port_activate(dev_info_t *dip, sata_device_t *satadev)
3095 {
3096 	ahci_ctl_t *ahci_ctlp;
3097 	ahci_port_t *ahci_portp;
3098 	ahci_addr_t addr;
3099 	uint8_t	cport = satadev->satadev_addr.cport;
3100 	uint8_t	pmport = satadev->satadev_addr.pmport;
3101 	uint8_t port;
3102 	int instance = ddi_get_instance(dip);
3103 
3104 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
3105 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
3106 
3107 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
3108 	    "ahci_tran_hotplug_port_activate enter: cport %d", cport);
3109 
3110 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
3111 
3112 	mutex_enter(&ahci_portp->ahciport_mutex);
3113 	ahci_get_ahci_addr(ahci_ctlp, satadev, &addr);
3114 	ASSERT(AHCI_ADDR_IS_PORT(&addr) || AHCI_ADDR_IS_PMPORT(&addr));
3115 
3116 	if (AHCI_ADDR_IS_PORT(&addr)) {
3117 		cmn_err(CE_NOTE, "!ahci%d: ahci port %d is activated",
3118 		    instance, port);
3119 
3120 		/* Enable the interrupts on the port */
3121 		ahci_enable_port_intrs(ahci_ctlp, port);
3122 
3123 		/*
3124 		 * Reset the port so that the PHY communication would be
3125 		 * re-established.  But this reset is an internal operation
3126 		 * and the sata module doesn't need to know about it.
3127 		 * Moreover, the port with a device attached will be started
3128 		 * too.
3129 		 */
3130 		(void) ahci_restart_port_wait_till_ready(ahci_ctlp,
3131 		    ahci_portp, port,
3132 		    AHCI_PORT_RESET|AHCI_RESET_NO_EVENTS_UP,
3133 		    NULL);
3134 
3135 		/*
3136 		 * Need to check the link status and device status of the port
3137 		 * and consider raising power if the port was in D3 state
3138 		 */
3139 		ahci_portp->ahciport_port_state |= SATA_PSTATE_PWRON;
3140 		ahci_portp->ahciport_port_state &= ~SATA_PSTATE_PWROFF;
3141 		ahci_portp->ahciport_port_state &= ~SATA_PSTATE_SHUTDOWN;
3142 	} else if (AHCI_ADDR_IS_PMPORT(&addr)) {
3143 		cmn_err(CE_NOTE, "!ahci%d: ahci port %d:%d is activated",
3144 		    instance, port, pmport);
3145 		/* AHCI_ADDR_PMPORT */
3146 		AHCIPORT_PMSTATE(ahci_portp, &addr) |= SATA_PSTATE_PWRON;
3147 		AHCIPORT_PMSTATE(ahci_portp, &addr) &=
3148 		    ~(SATA_PSTATE_PWROFF|SATA_PSTATE_SHUTDOWN);
3149 	}
3150 
3151 	satadev->satadev_state = ahci_portp->ahciport_port_state;
3152 
3153 	ahci_update_sata_registers(ahci_ctlp, port, satadev);
3154 
3155 	mutex_exit(&ahci_portp->ahciport_mutex);
3156 	return (SATA_SUCCESS);
3157 }
3158 
3159 /*
3160  * Called by sata framework to deactivate a port as part of hotplug.
3161  * (cfgadm -c disconnect satax/y)
3162  * Support port multiplier.
3163  */
3164 static int
3165 ahci_tran_hotplug_port_deactivate(dev_info_t *dip, sata_device_t *satadev)
3166 {
3167 	ahci_ctl_t *ahci_ctlp;
3168 	ahci_port_t *ahci_portp;
3169 	ahci_addr_t addr;
3170 	uint8_t	cport = satadev->satadev_addr.cport;
3171 	uint8_t	pmport = satadev->satadev_addr.pmport;
3172 	uint8_t port;
3173 	uint32_t port_scontrol;
3174 	int instance = ddi_get_instance(dip);
3175 
3176 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
3177 	port = ahci_ctlp->ahcictl_cport_to_port[cport];
3178 
3179 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
3180 	    "ahci_tran_hotplug_port_deactivate enter: cport %d", cport);
3181 
3182 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
3183 	mutex_enter(&ahci_portp->ahciport_mutex);
3184 	ahci_get_ahci_addr(ahci_ctlp, satadev, &addr);
3185 	ASSERT(AHCI_ADDR_IS_PORT(&addr) || AHCI_ADDR_IS_PMPORT(&addr));
3186 
3187 	if (AHCI_ADDR_IS_PORT(&addr)) {
3188 		cmn_err(CE_NOTE, "!ahci%d: ahci port %d is deactivated",
3189 		    instance, port);
3190 
3191 		/* Disable the interrupts on the port */
3192 		ahci_disable_port_intrs(ahci_ctlp, port);
3193 
3194 		if (ahci_portp->ahciport_device_type != SATA_DTYPE_NONE) {
3195 
3196 			/* First to abort all the pending commands */
3197 			ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
3198 
3199 			/* Then stop the port */
3200 			(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
3201 			    ahci_portp, port);
3202 		}
3203 
3204 		/* Next put the PHY offline */
3205 		port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3206 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
3207 		SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_DISABLE);
3208 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle, (uint32_t *)
3209 		    AHCI_PORT_PxSCTL(ahci_ctlp, port), port_scontrol);
3210 	} else if (AHCI_ADDR_IS_PMPORT(&addr)) {
3211 		cmn_err(CE_NOTE, "!ahci%d: ahci port %d:%d is deactivated",
3212 		    instance, port, pmport);
3213 
3214 		ahci_disable_port_intrs(ahci_ctlp, port);
3215 		if (AHCIPORT_GET_DEV_TYPE(ahci_portp, &addr)
3216 		    != SATA_DTYPE_NONE)
3217 			ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
3218 
3219 		/* Re-enable the interrupts for the other pmports */
3220 		ahci_enable_port_intrs(ahci_ctlp, port);
3221 	}
3222 
3223 	/* Update port state */
3224 	AHCIPORT_SET_STATE(ahci_portp, &addr, SATA_PSTATE_SHUTDOWN);
3225 	satadev->satadev_state = SATA_PSTATE_SHUTDOWN;
3226 
3227 	ahci_update_sata_registers(ahci_ctlp, port, satadev);
3228 
3229 	mutex_exit(&ahci_portp->ahciport_mutex);
3230 	return (SATA_SUCCESS);
3231 }
3232 
3233 /*
3234  * To be used to mark all the outstanding pkts with SATA_PKT_ABORTED
3235  * when a device is unplugged or a port is deactivated.
3236  *
3237  * WARNING!!! ahciport_mutex should be acquired before the function is called.
3238  */
3239 static void
3240 ahci_reject_all_abort_pkts(ahci_ctl_t *ahci_ctlp,
3241     ahci_port_t *ahci_portp, uint8_t port)
3242 {
3243 	uint32_t slot_status = 0;
3244 	uint32_t abort_tags = 0;
3245 
3246 	AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
3247 	    "ahci_reject_all_abort_pkts at port: %d", port);
3248 
3249 	/* Read/write port multiplier command takes highest priority */
3250 	if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
3251 		slot_status = 0x1;
3252 		abort_tags = 0x1;
3253 		goto out;
3254 	}
3255 
3256 	/*
3257 	 * When AHCI_PORT_FLAG_MOPPING is set, we need to check whether a
3258 	 * REQUEST SENSE command or READ LOG EXT command is delivered to HBA
3259 	 * to get the error data, if yes when the device is removed, the
3260 	 * command needs to be aborted too.
3261 	 */
3262 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) {
3263 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
3264 			slot_status = 0x1;
3265 			abort_tags = 0x1;
3266 			goto out;
3267 		} else {
3268 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
3269 			    "ahci_reject_all_abort_pkts return directly "
3270 			    "port %d no needs to reject any outstanding "
3271 			    "commands", port);
3272 			return;
3273 		}
3274 	}
3275 
3276 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
3277 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3278 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
3279 		abort_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
3280 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
3281 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3282 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
3283 		abort_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
3284 	}
3285 
3286 out:
3287 	/* No need to do mop when there is no outstanding commands */
3288 	if (slot_status != 0) {
3289 		ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
3290 		ahci_portp->ahciport_mop_in_progress++;
3291 
3292 		ahci_mop_commands(ahci_ctlp,
3293 		    ahci_portp,
3294 		    slot_status,
3295 		    0, /* failed tags */
3296 		    0, /* timeout tags */
3297 		    abort_tags, /* aborting tags */
3298 		    0); /* reset tags */
3299 	}
3300 }
3301 
3302 #if defined(__lock_lint)
3303 static int
3304 ahci_selftest(dev_info_t *dip, sata_device_t *device)
3305 {
3306 	return (SATA_SUCCESS);
3307 }
3308 #endif
3309 
3310 /*
3311  * Initialize fma capabilities and register with IO fault services.
3312  */
3313 static void
3314 ahci_fm_init(ahci_ctl_t *ahci_ctlp)
3315 {
3316 	/*
3317 	 * Need to change iblock to priority for new MSI intr
3318 	 */
3319 	ddi_iblock_cookie_t fm_ibc;
3320 
3321 	ahci_ctlp->ahcictl_fm_cap = ddi_getprop(DDI_DEV_T_ANY,
3322 	    ahci_ctlp->ahcictl_dip,
3323 	    DDI_PROP_CANSLEEP | DDI_PROP_DONTPASS, "fm-capable",
3324 	    DDI_FM_EREPORT_CAPABLE | DDI_FM_ACCCHK_CAPABLE |
3325 	    DDI_FM_DMACHK_CAPABLE | DDI_FM_ERRCB_CAPABLE);
3326 
3327 	/* Only register with IO Fault Services if we have some capability */
3328 	if (ahci_ctlp->ahcictl_fm_cap) {
3329 		/* Adjust access and dma attributes for FMA */
3330 		accattr.devacc_attr_access = DDI_FLAGERR_ACC;
3331 		buffer_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
3332 		rcvd_fis_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
3333 		cmd_list_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
3334 		cmd_table_dma_attr.dma_attr_flags |= DDI_DMA_FLAGERR;
3335 
3336 		/*
3337 		 * Register capabilities with IO Fault Services.
3338 		 * ahcictl_fm_cap will be updated to indicate
3339 		 * capabilities actually supported (not requested.)
3340 		 */
3341 		ddi_fm_init(ahci_ctlp->ahcictl_dip,
3342 		    &ahci_ctlp->ahcictl_fm_cap, &fm_ibc);
3343 
3344 		if (ahci_ctlp->ahcictl_fm_cap == DDI_FM_NOT_CAPABLE) {
3345 			cmn_err(CE_WARN, "!ahci%d: fma init failed.",
3346 			    ddi_get_instance(ahci_ctlp->ahcictl_dip));
3347 			return;
3348 		}
3349 		/*
3350 		 * Initialize pci ereport capabilities if ereport
3351 		 * capable (should always be.)
3352 		 */
3353 		if (DDI_FM_EREPORT_CAP(ahci_ctlp->ahcictl_fm_cap) ||
3354 		    DDI_FM_ERRCB_CAP(ahci_ctlp->ahcictl_fm_cap)) {
3355 			pci_ereport_setup(ahci_ctlp->ahcictl_dip);
3356 		}
3357 
3358 		/*
3359 		 * Register error callback if error callback capable.
3360 		 */
3361 		if (DDI_FM_ERRCB_CAP(ahci_ctlp->ahcictl_fm_cap)) {
3362 			ddi_fm_handler_register(ahci_ctlp->ahcictl_dip,
3363 			    ahci_fm_error_cb, (void *) ahci_ctlp);
3364 		}
3365 
3366 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
3367 		    "ahci_fm_fini: fma enabled.", NULL);
3368 	}
3369 }
3370 
3371 /*
3372  * Releases fma capabilities and un-registers with IO fault services.
3373  */
3374 static void
3375 ahci_fm_fini(ahci_ctl_t *ahci_ctlp)
3376 {
3377 	/* Only unregister FMA capabilities if registered */
3378 	if (ahci_ctlp->ahcictl_fm_cap) {
3379 		/*
3380 		 * Un-register error callback if error callback capable.
3381 		 */
3382 		if (DDI_FM_ERRCB_CAP(ahci_ctlp->ahcictl_fm_cap)) {
3383 			ddi_fm_handler_unregister(ahci_ctlp->ahcictl_dip);
3384 		}
3385 
3386 		/*
3387 		 * Release any resources allocated by pci_ereport_setup()
3388 		 */
3389 		if (DDI_FM_EREPORT_CAP(ahci_ctlp->ahcictl_fm_cap) ||
3390 		    DDI_FM_ERRCB_CAP(ahci_ctlp->ahcictl_fm_cap)) {
3391 			pci_ereport_teardown(ahci_ctlp->ahcictl_dip);
3392 		}
3393 
3394 		/* Unregister from IO Fault Services */
3395 		ddi_fm_fini(ahci_ctlp->ahcictl_dip);
3396 
3397 		/* Adjust access and dma attributes for FMA */
3398 		accattr.devacc_attr_access = DDI_DEFAULT_ACC;
3399 		buffer_dma_attr.dma_attr_flags &=  ~DDI_DMA_FLAGERR;
3400 		rcvd_fis_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
3401 		cmd_list_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
3402 		cmd_table_dma_attr.dma_attr_flags &= ~DDI_DMA_FLAGERR;
3403 
3404 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
3405 		    "ahci_fm_fini: fma disabled.", NULL);
3406 	}
3407 }
3408 
3409 /*ARGSUSED*/
3410 static int
3411 ahci_fm_error_cb(dev_info_t *dip, ddi_fm_error_t *err, const void *impl_data)
3412 {
3413 	/*
3414 	 * as the driver can always deal with an error in any dma or
3415 	 * access handle, we can just return the fme_status value.
3416 	 */
3417 	pci_ereport_post(dip, err, NULL);
3418 	return (err->fme_status);
3419 }
3420 
3421 int
3422 ahci_check_acc_handle(ddi_acc_handle_t handle)
3423 {
3424 	ddi_fm_error_t de;
3425 
3426 	ddi_fm_acc_err_get(handle, &de, DDI_FME_VERSION);
3427 	return (de.fme_status);
3428 }
3429 
3430 int
3431 ahci_check_dma_handle(ddi_dma_handle_t handle)
3432 {
3433 	ddi_fm_error_t de;
3434 
3435 	ddi_fm_dma_err_get(handle, &de, DDI_FME_VERSION);
3436 	return (de.fme_status);
3437 }
3438 
3439 /*
3440  * Generate an ereport
3441  */
3442 void
3443 ahci_fm_ereport(ahci_ctl_t *ahci_ctlp, char *detail)
3444 {
3445 	uint64_t ena;
3446 	char buf[FM_MAX_CLASS];
3447 
3448 	(void) snprintf(buf, FM_MAX_CLASS, "%s.%s", DDI_FM_DEVICE, detail);
3449 	ena = fm_ena_generate(0, FM_ENA_FMT1);
3450 	if (DDI_FM_EREPORT_CAP(ahci_ctlp->ahcictl_fm_cap)) {
3451 		ddi_fm_ereport_post(ahci_ctlp->ahcictl_dip, buf, ena,
3452 		    DDI_NOSLEEP, FM_VERSION, DATA_TYPE_UINT8,
3453 		    FM_EREPORT_VERSION, NULL);
3454 	}
3455 }
3456 
3457 /*
3458  * Check if all handles are correctly allocated. This function is only called
3459  * by ahci_attach(), so we do not need hold any mutex here.
3460  */
3461 static int
3462 ahci_check_all_handle(ahci_ctl_t *ahci_ctlp)
3463 {
3464 	int port;
3465 	if (ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) {
3466 		return (DDI_FAILURE);
3467 	}
3468 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
3469 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port))
3470 			continue;
3471 		if (ahci_check_port_handle(ahci_ctlp, port) != DDI_SUCCESS) {
3472 			return (DDI_FAILURE);
3473 		}
3474 	}
3475 	return (DDI_SUCCESS);
3476 }
3477 
3478 /*
3479  * Check the access handles for the controller. Note that
3480  * ahcictl_pci_conf_handle is only used in attach process.
3481  */
3482 static int
3483 ahci_check_ctl_handle(ahci_ctl_t *ahci_ctlp)
3484 {
3485 	if ((ahci_check_acc_handle(ahci_ctlp->
3486 	    ahcictl_pci_conf_handle) != DDI_FM_OK) ||
3487 	    (ahci_check_acc_handle(ahci_ctlp->
3488 	    ahcictl_ahci_acc_handle) != DDI_FM_OK)) {
3489 		return (DDI_FAILURE);
3490 	}
3491 	return (DDI_SUCCESS);
3492 }
3493 
3494 /*
3495  * Check the DMA handles and the access handles of a controller port.
3496  *
3497  * WARNING!!! ahciport_mutex should be acquired before the function is called.
3498  */
3499 static int
3500 ahci_check_port_handle(ahci_ctl_t *ahci_ctlp, int port)
3501 {
3502 	ahci_port_t *ahci_portp = ahci_ctlp->ahcictl_ports[port];
3503 	int slot;
3504 	if ((ahci_check_dma_handle(ahci_portp->
3505 	    ahciport_rcvd_fis_dma_handle) != DDI_FM_OK) ||
3506 	    (ahci_check_dma_handle(ahci_portp->
3507 	    ahciport_cmd_list_dma_handle) != DDI_FM_OK) ||
3508 	    (ahci_check_acc_handle(ahci_portp->
3509 	    ahciport_rcvd_fis_acc_handle) != DDI_FM_OK) ||
3510 	    (ahci_check_acc_handle(ahci_portp->
3511 	    ahciport_cmd_list_acc_handle) != DDI_FM_OK)) {
3512 		return (DDI_FAILURE);
3513 	}
3514 	for (slot = 0; slot < ahci_ctlp->ahcictl_num_cmd_slots;
3515 	    slot ++) {
3516 		if (ahci_check_slot_handle(ahci_portp, slot)
3517 		    != DDI_SUCCESS) {
3518 			return (DDI_FAILURE);
3519 		}
3520 	}
3521 	return (DDI_SUCCESS);
3522 }
3523 
3524 /*
3525  * Check the DMA handles and the access handles of a cmd table slot.
3526  *
3527  * WARNING!!! ahciport_mutex should be acquired before the function is called.
3528  */
3529 static int
3530 ahci_check_slot_handle(ahci_port_t *ahci_portp, int slot)
3531 {
3532 	if ((ahci_check_acc_handle(ahci_portp->
3533 	    ahciport_cmd_tables_acc_handle[slot]) != DDI_FM_OK) ||
3534 	    (ahci_check_dma_handle(ahci_portp->
3535 	    ahciport_cmd_tables_dma_handle[slot]) != DDI_FM_OK)) {
3536 		return (DDI_FAILURE);
3537 	}
3538 	return (DDI_SUCCESS);
3539 }
3540 /*
3541  * Allocate the ports structure, only called by ahci_attach
3542  */
3543 static int
3544 ahci_alloc_ports_state(ahci_ctl_t *ahci_ctlp)
3545 {
3546 	int port, cport = 0;
3547 
3548 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
3549 	    "ahci_alloc_ports_state enter", NULL);
3550 
3551 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
3552 
3553 	/* Allocate structures only for the implemented ports */
3554 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
3555 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
3556 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
3557 			    "hba port %d not implemented", port);
3558 			continue;
3559 		}
3560 
3561 		ahci_ctlp->ahcictl_cport_to_port[cport] = (uint8_t)port;
3562 		ahci_ctlp->ahcictl_port_to_cport[port] =
3563 		    (uint8_t)cport++;
3564 
3565 		if (ahci_alloc_port_state(ahci_ctlp, port) != AHCI_SUCCESS) {
3566 			goto err_out;
3567 		}
3568 	}
3569 
3570 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
3571 	return (AHCI_SUCCESS);
3572 
3573 err_out:
3574 	for (port--; port >= 0; port--) {
3575 		if (AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
3576 			ahci_dealloc_port_state(ahci_ctlp, port);
3577 		}
3578 	}
3579 
3580 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
3581 	return (AHCI_FAILURE);
3582 }
3583 
3584 /*
3585  * Reverse of ahci_alloc_ports_state(), only called by ahci_detach
3586  */
3587 static void
3588 ahci_dealloc_ports_state(ahci_ctl_t *ahci_ctlp)
3589 {
3590 	int port;
3591 
3592 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
3593 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
3594 		/* if this port is implemented by the HBA */
3595 		if (AHCI_PORT_IMPLEMENTED(ahci_ctlp, port))
3596 			ahci_dealloc_port_state(ahci_ctlp, port);
3597 	}
3598 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
3599 }
3600 
3601 /*
3602  * Drain the taskq.
3603  */
3604 static void
3605 ahci_drain_ports_taskq(ahci_ctl_t *ahci_ctlp)
3606 {
3607 	ahci_port_t *ahci_portp;
3608 	int port;
3609 
3610 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
3611 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
3612 			continue;
3613 		}
3614 
3615 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
3616 
3617 		mutex_enter(&ahci_portp->ahciport_mutex);
3618 		ddi_taskq_wait(ahci_portp->ahciport_event_taskq);
3619 		mutex_exit(&ahci_portp->ahciport_mutex);
3620 	}
3621 }
3622 
3623 /*
3624  * Initialize the controller and all ports. And then try to start the ports
3625  * if there are devices attached.
3626  *
3627  * This routine can be called from three seperate cases: DDI_ATTACH,
3628  * PM_LEVEL_D0 and DDI_RESUME. The DDI_ATTACH case is different from
3629  * other two cases; device signature probing are attempted only during
3630  * DDI_ATTACH case.
3631  *
3632  * WARNING!!! Disable the whole controller's interrupts before calling and
3633  * the interrupts will be enabled upon successfully return.
3634  */
3635 static int
3636 ahci_initialize_controller(ahci_ctl_t *ahci_ctlp)
3637 {
3638 	ahci_port_t *ahci_portp;
3639 	ahci_addr_t addr;
3640 	uint32_t ghc_control;
3641 	int port;
3642 
3643 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
3644 	    "ahci_initialize_controller enter", NULL);
3645 
3646 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
3647 
3648 	/*
3649 	 * Indicate that system software is AHCI aware by setting
3650 	 * GHC.AE to 1
3651 	 */
3652 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3653 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
3654 
3655 	ghc_control |= AHCI_HBA_GHC_AE;
3656 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
3657 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp),
3658 	    ghc_control);
3659 
3660 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
3661 
3662 	/* Initialize the implemented ports and structures */
3663 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
3664 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
3665 			continue;
3666 		}
3667 
3668 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
3669 		mutex_enter(&ahci_portp->ahciport_mutex);
3670 
3671 		/*
3672 		 * Ensure that the controller is not in the running state
3673 		 * by checking every implemented port's PxCMD register
3674 		 */
3675 		AHCI_ADDR_SET_PORT(&addr, (uint8_t)port);
3676 
3677 		if (ahci_initialize_port(ahci_ctlp, ahci_portp, &addr)
3678 		    != AHCI_SUCCESS) {
3679 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
3680 			    "ahci_initialize_controller: failed to "
3681 			    "initialize port %d", port);
3682 			/*
3683 			 * Set the port state to SATA_PSTATE_FAILED if
3684 			 * failed to initialize it.
3685 			 */
3686 			ahci_portp->ahciport_port_state = SATA_PSTATE_FAILED;
3687 		}
3688 
3689 		mutex_exit(&ahci_portp->ahciport_mutex);
3690 	}
3691 
3692 	/* Enable the whole controller interrupts */
3693 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
3694 	ahci_enable_all_intrs(ahci_ctlp);
3695 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
3696 
3697 	return (AHCI_SUCCESS);
3698 }
3699 
3700 /*
3701  * Reverse of ahci_initialize_controller()
3702  *
3703  * We only need to stop the ports and disable the interrupt.
3704  */
3705 static void
3706 ahci_uninitialize_controller(ahci_ctl_t *ahci_ctlp)
3707 {
3708 	ahci_port_t *ahci_portp;
3709 	int port;
3710 
3711 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
3712 	    "ahci_uninitialize_controller enter", NULL);
3713 
3714 	/* disable all the interrupts. */
3715 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
3716 	ahci_disable_all_intrs(ahci_ctlp);
3717 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
3718 
3719 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
3720 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
3721 			continue;
3722 		}
3723 
3724 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
3725 
3726 		/* Stop the port by clearing PxCMD.ST */
3727 		mutex_enter(&ahci_portp->ahciport_mutex);
3728 
3729 		/*
3730 		 * Here we must disable the port interrupt because
3731 		 * ahci_disable_all_intrs only clear GHC.IE, and IS
3732 		 * register will be still set if PxIE is enabled.
3733 		 * When ahci shares one IRQ with other drivers, the
3734 		 * intr handler may claim the intr mistakenly.
3735 		 */
3736 		ahci_disable_port_intrs(ahci_ctlp, port);
3737 		(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
3738 		    ahci_portp, port);
3739 		mutex_exit(&ahci_portp->ahciport_mutex);
3740 	}
3741 }
3742 
3743 /*
3744  * ahci_alloc_pmult()
3745  * 1. Setting HBA port registers which are necessary for a port multiplier.
3746  *    (Set PxCMD.PMA while PxCMD.ST is '0')
3747  * 2. Allocate ahci_pmult_info structure.
3748  *
3749  * NOTE: Must stop port before the function is called.
3750  * WARNING!!! ahciport_mutex should be acquired before the function is
3751  * called.
3752  */
3753 static void
3754 ahci_alloc_pmult(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
3755 {
3756 	uint32_t port_cmd_status;
3757 	uint8_t port = ahci_portp->ahciport_port_num;
3758 
3759 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3760 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
3761 
3762 	/* The port must have been stopped before. */
3763 	ASSERT(!(port_cmd_status & AHCI_CMD_STATUS_ST));
3764 
3765 	if (!(port_cmd_status & AHCI_CMD_STATUS_PMA)) {
3766 		/* set PMA bit */
3767 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
3768 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
3769 		    port_cmd_status|AHCI_CMD_STATUS_PMA);
3770 
3771 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
3772 		    "ahci_alloc_pmult: "
3773 		    "PxCMD.PMA bit set at port %d.", port);
3774 	}
3775 
3776 	/* Allocate port multiplier information structure */
3777 	if (ahci_portp->ahciport_pmult_info == NULL) {
3778 		ahci_portp->ahciport_pmult_info = (ahci_pmult_info_t *)
3779 		    kmem_zalloc(sizeof (ahci_pmult_info_t), KM_SLEEP);
3780 	}
3781 
3782 	ASSERT(ahci_portp->ahciport_pmult_info != NULL);
3783 }
3784 
3785 /*
3786  * ahci_dealloc_pmult()
3787  * 1. Clearing related registers when a port multiplier is detached.
3788  *    (Clear PxCMD.PMA while PxCMD.ST is '0')
3789  * 2. Deallocate ahci_pmult_info structure.
3790  *
3791  * NOTE: Must stop port before the function is called.
3792  * WARNING!!! ahciport_mutex should be acquired before the function is
3793  * called.
3794  */
3795 static void
3796 ahci_dealloc_pmult(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
3797 {
3798 	uint32_t port_cmd_status;
3799 	uint8_t port = ahci_portp->ahciport_port_num;
3800 
3801 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3802 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
3803 
3804 	if (port_cmd_status & AHCI_CMD_STATUS_PMA) {
3805 		/* Clear PMA bit */
3806 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
3807 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
3808 		    (port_cmd_status & (~AHCI_CMD_STATUS_PMA)));
3809 
3810 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_PMULT, ahci_ctlp,
3811 		    "ahci_dealloc_pmult: "
3812 		    "PxCMD.PMA bit cleared at port %d.", port);
3813 	}
3814 
3815 	/* Release port multiplier information structure */
3816 	if (ahci_portp->ahciport_pmult_info != NULL) {
3817 		kmem_free(ahci_portp->ahciport_pmult_info,
3818 		    sizeof (ahci_pmult_info_t));
3819 		ahci_portp->ahciport_pmult_info = NULL;
3820 	}
3821 }
3822 
3823 /*
3824  * The routine is to initialize a port. First put the port in NOTRunning
3825  * state, then enable port interrupt and clear Serror register. And under
3826  * AHCI_ATTACH case, find device signature and then try to start the port.
3827  *
3828  * Called by
3829  *    1. ahci_initialize_controller
3830  *    2. ahci_intr_phyrdy_change (hotplug)
3831  *
3832  * WARNING!!! ahciport_mutex should be acquired before the function is called.
3833  */
3834 static int
3835 ahci_initialize_port(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
3836     ahci_addr_t *addrp)
3837 {
3838 	uint32_t port_sstatus, port_task_file, port_cmd_status;
3839 	uint8_t port = addrp->aa_port;
3840 	boolean_t resuming = B_TRUE;	/*  processing DDI_RESUME */
3841 	int ret;
3842 
3843 	ASSERT(mutex_owned(&ahci_portp->ahciport_mutex));
3844 
3845 	/* AHCI_ADDR_PORT: We've no idea of the attached device here.  */
3846 	ASSERT(AHCI_ADDR_IS_PORT(addrp));
3847 
3848 	/*
3849 	 * At the time being, only probe ports/devices and get the types of
3850 	 * attached devices during DDI_ATTACH. In fact, the device can be
3851 	 * changed during power state changes, but at the time being, we
3852 	 * don't support the situation.
3853 	 */
3854 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_HOTPLUG) {
3855 		resuming = B_FALSE;
3856 	} else {
3857 		/* check for DDI_RESUME case */
3858 		mutex_exit(&ahci_portp->ahciport_mutex);
3859 		mutex_enter(&ahci_ctlp->ahcictl_mutex);
3860 		if (ahci_ctlp->ahcictl_flags & AHCI_ATTACH)
3861 			resuming = B_FALSE;
3862 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
3863 		mutex_enter(&ahci_portp->ahciport_mutex);
3864 	}
3865 
3866 	if (resuming) {
3867 		/*
3868 		 * During the resume, we need to set the PxCLB, PxCLBU, PxFB
3869 		 * and PxFBU registers in case these registers were cleared
3870 		 * during the suspend.
3871 		 */
3872 		AHCIDBG(AHCIDBG_PM, ahci_ctlp,
3873 		    "ahci_initialize_port: port %d "
3874 		    "set PxCLB, PxCLBU, PxFB and PxFBU "
3875 		    "during resume", port);
3876 
3877 		/* Config Port Received FIS Base Address */
3878 		ddi_put64(ahci_ctlp->ahcictl_ahci_acc_handle,
3879 		    (uint64_t *)AHCI_PORT_PxFB(ahci_ctlp, port),
3880 		    ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_laddress);
3881 
3882 		/* Config Port Command List Base Address */
3883 		ddi_put64(ahci_ctlp->ahcictl_ahci_acc_handle,
3884 		    (uint64_t *)AHCI_PORT_PxCLB(ahci_ctlp, port),
3885 		    ahci_portp->ahciport_cmd_list_dma_cookie.dmac_laddress);
3886 	}
3887 
3888 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3889 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
3890 
3891 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
3892 	    "ahci_initialize_port: port %d ", port);
3893 
3894 	/*
3895 	 * Check whether the port is in NotRunning state, if not,
3896 	 * put the port in NotRunning state
3897 	 */
3898 	if (port_cmd_status &
3899 	    (AHCI_CMD_STATUS_ST |
3900 	    AHCI_CMD_STATUS_CR |
3901 	    AHCI_CMD_STATUS_FRE |
3902 	    AHCI_CMD_STATUS_FR)) {
3903 		(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
3904 		    ahci_portp, port);
3905 	}
3906 
3907 	/* Disable interrupt */
3908 	ahci_disable_port_intrs(ahci_ctlp, port);
3909 
3910 	/* Device is unknown at first */
3911 	AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_UNKNOWN);
3912 
3913 	/* Disable the interface power management */
3914 	ahci_disable_interface_pm(ahci_ctlp, port);
3915 
3916 	port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3917 	    (uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
3918 	port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
3919 	    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
3920 
3921 	/* Check physcial link status */
3922 	if (SSTATUS_GET_IPM(port_sstatus) == SSTATUS_IPM_NODEV_NOPHYCOM ||
3923 	    SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_NOPHYCOM ||
3924 
3925 	    /* Check interface status */
3926 	    port_task_file & AHCI_TFD_STS_BSY ||
3927 	    port_task_file & AHCI_TFD_STS_DRQ ||
3928 
3929 	    /* Check whether port reset must be executed */
3930 	    ahci_ctlp->ahcictl_cap & AHCI_CAP_INIT_PORT_RESET ||
3931 
3932 	    /* Always reset port on RESUME */
3933 	    resuming != B_FALSE) {
3934 
3935 		/* Something went wrong, we need do some reset things */
3936 		ret = ahci_port_reset(ahci_ctlp, ahci_portp, addrp);
3937 
3938 		/* Does port reset succeed on HBA port? */
3939 		if (ret != AHCI_SUCCESS) {
3940 			AHCIDBG(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
3941 			    "ahci_initialize_port:"
3942 			    "port reset faild at port %d", port);
3943 			return (AHCI_FAILURE);
3944 		}
3945 
3946 		/* Is port failed? */
3947 		if (AHCIPORT_GET_STATE(ahci_portp, addrp) &
3948 		    SATA_PSTATE_FAILED) {
3949 			AHCIDBG(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
3950 			    "ahci_initialize_port: port %d state 0x%x",
3951 			    port, ahci_portp->ahciport_port_state);
3952 			return (AHCI_FAILURE);
3953 		}
3954 	}
3955 	AHCIPORT_SET_STATE(ahci_portp, addrp, SATA_STATE_READY);
3956 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "port %d is ready now.", port);
3957 
3958 	/*
3959 	 * Try to get the device signature if the port is not empty.
3960 	 */
3961 	if (!resuming && ahci_portp->ahciport_device_type != SATA_DTYPE_NONE)
3962 		ahci_find_dev_signature(ahci_ctlp, ahci_portp, addrp);
3963 
3964 	/* Return directly if no device connected */
3965 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
3966 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
3967 		    "No device connected to port %d", port);
3968 		goto out;
3969 	}
3970 
3971 	/* If this is a port multiplier, we need do some initialization */
3972 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_PMULT) {
3973 		AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
3974 		    "Port multiplier found at port %d", port);
3975 		ahci_alloc_pmult(ahci_ctlp, ahci_portp);
3976 	}
3977 
3978 	/* Try to start the port */
3979 	if (ahci_start_port(ahci_ctlp, ahci_portp, port)
3980 	    != AHCI_SUCCESS) {
3981 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
3982 		    "failed to start port %d", port);
3983 		return (AHCI_FAILURE);
3984 	}
3985 out:
3986 	/* Enable port interrupts */
3987 	ahci_enable_port_intrs(ahci_ctlp, port);
3988 
3989 	return (AHCI_SUCCESS);
3990 }
3991 
3992 /*
3993  *  Handle hardware defect, and check the capabilities. For example,
3994  *  power management capabilty and MSI capability.
3995  */
3996 static int
3997 ahci_config_space_init(ahci_ctl_t *ahci_ctlp)
3998 {
3999 	ushort_t caps_ptr, cap_count, cap;
4000 #if AHCI_DEBUG
4001 	ushort_t pmcap, pmcsr;
4002 	ushort_t msimc;
4003 #endif
4004 	uint8_t revision;
4005 
4006 	ahci_ctlp->ahcictl_venid =
4007 	    pci_config_get16(ahci_ctlp->ahcictl_pci_conf_handle,
4008 	    PCI_CONF_VENID);
4009 
4010 	ahci_ctlp->ahcictl_devid =
4011 	    pci_config_get16(ahci_ctlp->ahcictl_pci_conf_handle,
4012 	    PCI_CONF_DEVID);
4013 
4014 	/*
4015 	 * Modify dma_attr_align of ahcictl_buffer_dma_attr. For VT8251, those
4016 	 * controllers with 0x00 revision id work on 4-byte aligned buffer,
4017 	 * which is a bug and was fixed after 0x00 revision id controllers.
4018 	 *
4019 	 * Moreover, VT8251 cannot use multiple command slots in the command
4020 	 * list for non-queued commands because the previous register content
4021 	 * of PxCI can be re-written in the register write, so a flag will be
4022 	 * set to record this defect - AHCI_CAP_NO_MCMDLIST_NONQUEUE.
4023 	 *
4024 	 * For VT8251, software reset also has the same defect as the below
4025 	 * AMD/ATI chipset. That is, software reset will get failed if 0xf
4026 	 * is filled in pmport field. Therefore, another software reset need
4027 	 * to be done with 0 filled in pmport field.
4028 	 */
4029 	if (ahci_ctlp->ahcictl_venid == VIA_VENID) {
4030 		revision = pci_config_get8(ahci_ctlp->ahcictl_pci_conf_handle,
4031 		    PCI_CONF_REVID);
4032 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4033 		    "revision id = 0x%x", revision);
4034 		if (revision == 0x00) {
4035 			ahci_ctlp->ahcictl_buffer_dma_attr.dma_attr_align = 0x4;
4036 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4037 			    "change ddi_attr_align to 0x4", NULL);
4038 		}
4039 
4040 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_NO_MCMDLIST_NONQUEUE;
4041 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4042 		    "VT8251 cannot use multiple command lists for "
4043 		    "non-queued commands", NULL);
4044 
4045 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_SRST_NO_HOSTPORT;
4046 	}
4047 
4048 	/*
4049 	 * AMD/ATI SB600 (0x1002,0x4380) AHCI chipset doesn't support 64-bit
4050 	 * DMA addressing for communication memory descriptors though S64A bit
4051 	 * of CAP register declares it supports. Even though 64-bit DMA for
4052 	 * data buffer works on ASUS M2A-VM with newer BIOS, three other
4053 	 * motherboards are known not, so both AHCI_CAP_BUF_32BIT_DMA and
4054 	 * AHCI_CAP_COMMU_32BIT_DMA are set for this controller.
4055 	 *
4056 	 * Due to certain hardware issue, the chipset must do port reset during
4057 	 * initialization, otherwise, when retrieving device signature,
4058 	 * software reset will get time out. So AHCI_CAP_INIT_PORT_RESET flag
4059 	 * need to set.
4060 	 *
4061 	 * For this chipset software reset will get failure if the pmport of
4062 	 * Register FIS was set with SATA_PMULT_HOSTPORT (0xf) and no port
4063 	 * multiplier is connected to the port. In order to fix the issue,
4064 	 * AHCI_CAP_SRST_NO_HOSTPORT flag need to be set, and once software
4065 	 * reset got failure, the driver will try to do another software reset
4066 	 * with pmport 0.
4067 	 */
4068 	if (ahci_ctlp->ahcictl_venid == 0x1002 &&
4069 	    ahci_ctlp->ahcictl_devid == 0x4380) {
4070 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_BUF_32BIT_DMA;
4071 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_COMMU_32BIT_DMA;
4072 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_INIT_PORT_RESET;
4073 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_SRST_NO_HOSTPORT;
4074 
4075 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4076 		    "ATI SB600 cannot do 64-bit DMA for both data buffer and "
4077 		    "communication memory descriptors though CAP indicates "
4078 		    "support, so force it to use 32-bit DMA", NULL);
4079 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4080 		    "ATI SB600 need to do a port reset during initialization",
4081 		    NULL);
4082 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4083 		    "ATI SB600 will get software reset failure if pmport "
4084 		    "is set 0xf and no port multiplier is attached", NULL);
4085 	}
4086 
4087 	/*
4088 	 * AMD/ATI SB700/710/750/800 and SP5100 AHCI chipset share the same
4089 	 * vendor ID and device ID (0x1002,0x4391).
4090 	 *
4091 	 * SB700/750 AHCI chipset on some boards doesn't support 64-bit
4092 	 * DMA addressing for communication memory descriptors though S64A bit
4093 	 * of CAP register declares the support. However, it does support
4094 	 * 64-bit DMA for data buffer. So only AHCI_CAP_COMMU_32BIT_DMA is
4095 	 * set for this controller.
4096 	 *
4097 	 * SB710 has the same initialization issue as SB600, so it also need
4098 	 * a port reset. That is AHCI_CAP_INIT_PORT_RESET need to set for it.
4099 	 *
4100 	 * SB700 also has the same issue about software reset, and thus
4101 	 * AHCI_CAP_SRST_NO_HOSTPORT flag also is needed.
4102 	 */
4103 	if (ahci_ctlp->ahcictl_venid == 0x1002 &&
4104 	    ahci_ctlp->ahcictl_devid == 0x4391) {
4105 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_COMMU_32BIT_DMA;
4106 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_INIT_PORT_RESET;
4107 		ahci_ctlp->ahcictl_cap |= AHCI_CAP_SRST_NO_HOSTPORT;
4108 
4109 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4110 		    "ATI SB700/750 cannot do 64-bit DMA for communication "
4111 		    "memory descriptors though CAP indicates support, "
4112 		    "so force it to use 32-bit DMA", NULL);
4113 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4114 		    "ATI SB710 need to do a port reset during initialization",
4115 		    NULL);
4116 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4117 		    "ATI SB700 will get software reset failure if pmport "
4118 		    "is set 0xf and no port multiplier is attached", NULL);
4119 	}
4120 
4121 	/*
4122 	 * Check if capabilities list is supported and if so,
4123 	 * get initial capabilities pointer and clear bits 0,1.
4124 	 */
4125 	if (pci_config_get16(ahci_ctlp->ahcictl_pci_conf_handle,
4126 	    PCI_CONF_STAT) & PCI_STAT_CAP) {
4127 		caps_ptr = P2ALIGN(pci_config_get8(
4128 		    ahci_ctlp->ahcictl_pci_conf_handle,
4129 		    PCI_CONF_CAP_PTR), 4);
4130 	} else {
4131 		caps_ptr = PCI_CAP_NEXT_PTR_NULL;
4132 	}
4133 
4134 	/*
4135 	 * Walk capabilities if supported.
4136 	 */
4137 	for (cap_count = 0; caps_ptr != PCI_CAP_NEXT_PTR_NULL; ) {
4138 
4139 		/*
4140 		 * Check that we haven't exceeded the maximum number of
4141 		 * capabilities and that the pointer is in a valid range.
4142 		 */
4143 		if (++cap_count > PCI_CAP_MAX_PTR) {
4144 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
4145 			    "too many device capabilities", NULL);
4146 			return (AHCI_FAILURE);
4147 		}
4148 		if (caps_ptr < PCI_CAP_PTR_OFF) {
4149 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
4150 			    "capabilities pointer 0x%x out of range",
4151 			    caps_ptr);
4152 			return (AHCI_FAILURE);
4153 		}
4154 
4155 		/*
4156 		 * Get next capability and check that it is valid.
4157 		 * For now, we only support power management.
4158 		 */
4159 		cap = pci_config_get8(ahci_ctlp->ahcictl_pci_conf_handle,
4160 		    caps_ptr);
4161 		switch (cap) {
4162 		case PCI_CAP_ID_PM:
4163 
4164 			/* power management supported */
4165 			ahci_ctlp->ahcictl_cap |= AHCI_CAP_PM;
4166 
4167 			/* Save PMCSR offset */
4168 			ahci_ctlp->ahcictl_pmcsr_offset = caps_ptr + PCI_PMCSR;
4169 
4170 #if AHCI_DEBUG
4171 			pmcap = pci_config_get16(
4172 			    ahci_ctlp->ahcictl_pci_conf_handle,
4173 			    caps_ptr + PCI_PMCAP);
4174 			pmcsr = pci_config_get16(
4175 			    ahci_ctlp->ahcictl_pci_conf_handle,
4176 			    ahci_ctlp->ahcictl_pmcsr_offset);
4177 			AHCIDBG(AHCIDBG_PM, ahci_ctlp,
4178 			    "Power Management capability found PCI_PMCAP "
4179 			    "= 0x%x PCI_PMCSR = 0x%x", pmcap, pmcsr);
4180 			if ((pmcap & 0x3) == 0x3)
4181 				AHCIDBG(AHCIDBG_PM, ahci_ctlp,
4182 				    "PCI Power Management Interface "
4183 				    "spec 1.2 compliant", NULL);
4184 #endif
4185 			break;
4186 
4187 		case PCI_CAP_ID_MSI:
4188 #if AHCI_DEBUG
4189 			msimc = pci_config_get16(
4190 			    ahci_ctlp->ahcictl_pci_conf_handle,
4191 			    caps_ptr + PCI_MSI_CTRL);
4192 			AHCIDBG(AHCIDBG_MSI, ahci_ctlp,
4193 			    "Message Signaled Interrupt capability found "
4194 			    "MSICAP_MC.MMC = 0x%x", (msimc & 0xe) >> 1);
4195 #endif
4196 			AHCIDBG(AHCIDBG_MSI, ahci_ctlp,
4197 			    "MSI capability found", NULL);
4198 			break;
4199 
4200 		case PCI_CAP_ID_PCIX:
4201 			AHCIDBG(AHCIDBG_PM, ahci_ctlp,
4202 			    "PCI-X capability found", NULL);
4203 			break;
4204 
4205 		case PCI_CAP_ID_PCI_E:
4206 			AHCIDBG(AHCIDBG_PM, ahci_ctlp,
4207 			    "PCI Express capability found", NULL);
4208 			break;
4209 
4210 		case PCI_CAP_ID_MSI_X:
4211 			AHCIDBG(AHCIDBG_PM, ahci_ctlp,
4212 			    "MSI-X capability found", NULL);
4213 			break;
4214 
4215 		case PCI_CAP_ID_SATA:
4216 			AHCIDBG(AHCIDBG_PM, ahci_ctlp,
4217 			    "SATA capability found", NULL);
4218 			break;
4219 
4220 		case PCI_CAP_ID_VS:
4221 			AHCIDBG(AHCIDBG_PM, ahci_ctlp,
4222 			    "Vendor Specific capability found", NULL);
4223 			break;
4224 
4225 		default:
4226 			AHCIDBG(AHCIDBG_PM, ahci_ctlp,
4227 			    "unrecognized capability 0x%x", cap);
4228 			break;
4229 		}
4230 
4231 		/*
4232 		 * Get next capabilities pointer and clear bits 0,1.
4233 		 */
4234 		caps_ptr = P2ALIGN(pci_config_get8(
4235 		    ahci_ctlp->ahcictl_pci_conf_handle,
4236 		    (caps_ptr + PCI_CAP_NEXT_PTR)), 4);
4237 	}
4238 
4239 	return (AHCI_SUCCESS);
4240 }
4241 
4242 /*
4243  * Read/Write a register at port multiplier by SATA READ PORTMULT / SATA WRITE
4244  * PORTMULT command. SYNC & POLLING mode is used.
4245  *
4246  * WARNING!!! ahciport_mutex should be acquired before the function
4247  * is called.
4248  */
4249 static int
4250 ahci_rdwr_pmult(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
4251     uint8_t regn, uint32_t *pregv, uint8_t type)
4252 {
4253 	ahci_port_t *ahci_portp;
4254 	ahci_addr_t pmult_addr;
4255 	sata_pkt_t *spkt;
4256 	sata_cmd_t *scmd;
4257 	sata_device_t sata_device;
4258 	uint8_t port = addrp->aa_port;
4259 	uint8_t pmport = addrp->aa_pmport;
4260 	uint8_t cport;
4261 	uint32_t intr_mask;
4262 	int rval;
4263 	char portstr[10];
4264 
4265 	SET_PORTSTR(portstr, addrp);
4266 	cport = ahci_ctlp->ahcictl_port_to_cport[port];
4267 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
4268 
4269 	ASSERT(AHCI_ADDR_IS_PMPORT(addrp) || AHCI_ADDR_IS_PMULT(addrp));
4270 	ASSERT(mutex_owned(&ahci_portp->ahciport_mutex));
4271 
4272 	/* Check the existence of the port multiplier */
4273 	if (ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT)
4274 		return (AHCI_FAILURE);
4275 
4276 	/* Request a READ/WRITE PORTMULT sata packet. */
4277 	bzero(&sata_device, sizeof (sata_device_t));
4278 	sata_device.satadev_addr.cport = cport;
4279 	sata_device.satadev_addr.pmport = pmport;
4280 	sata_device.satadev_addr.qual = SATA_ADDR_PMULT;
4281 	sata_device.satadev_rev = SATA_DEVICE_REV;
4282 
4283 	/*
4284 	 * Make sure no command is outstanding here. All R/W PMULT requests
4285 	 * come from
4286 	 *
4287 	 * 1. ahci_attach()
4288 	 *    The port should be empty.
4289 	 *
4290 	 * 2. ahci_tran_probe_port()
4291 	 *    Any request from SATA framework (via ahci_tran_start) should be
4292 	 *    rejected if R/W PMULT command is outstanding.
4293 	 *
4294 	 *    If we are doing mopping, do not check those flags because no
4295 	 *    command will be actually outstanding.
4296 	 *
4297 	 *    If the port has been occupied by any other commands, the probe
4298 	 *    function will return a SATA_RETRY. SATA framework will retry
4299 	 *    later.
4300 	 */
4301 	if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
4302 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
4303 		    "R/W PMULT failed: R/W PMULT in progress at port %d.",
4304 		    port, ahci_portp->ahciport_flags);
4305 		return (AHCI_FAILURE);
4306 	}
4307 
4308 	if (!(ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) && (
4309 	    ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) ||
4310 	    NCQ_CMD_IN_PROGRESS(ahci_portp) ||
4311 	    NON_NCQ_CMD_IN_PROGRESS(ahci_portp))) {
4312 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
4313 		    "R/W PMULT failed: port %d is occupied (flags 0x%x).",
4314 		    port, ahci_portp->ahciport_flags);
4315 		return (AHCI_FAILURE);
4316 	}
4317 
4318 	/*
4319 	 * The port multiplier is gone. This may happen when
4320 	 * 1. Cutting off the power of an enclosure. The device lose the power
4321 	 *    before port multiplier.
4322 	 * 2. Disconnecting the port multiplier during hot-plugging a sub-drive.
4323 	 *
4324 	 * The issued command should be aborted and the following command
4325 	 * should not be continued.
4326 	 */
4327 	if (!(ahci_portp->ahciport_port_state & SATA_STATE_READY)) {
4328 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
4329 		    "READ/WRITE PMULT failed: "
4330 		    "port-mult is removed from port %d", port);
4331 		return (AHCI_FAILURE);
4332 	}
4333 
4334 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_RDWR_PMULT;
4335 
4336 	spkt = sata_get_rdwr_pmult_pkt(ahci_ctlp->ahcictl_dip,
4337 	    &sata_device, regn, *pregv, type);
4338 
4339 	/*
4340 	 * READ/WRITE PORTMULT command is intended to sent to the control port
4341 	 * of the port multiplier.
4342 	 */
4343 	AHCI_ADDR_SET_PMULT(&pmult_addr, addrp->aa_port);
4344 
4345 	ahci_portp->ahciport_rdwr_pmult_pkt = spkt;
4346 
4347 	/* No interrupt here. Store the interrupt enable mask. */
4348 	intr_mask = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
4349 	    (uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port));
4350 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
4351 	    (uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port), 0);
4352 
4353 	rval = ahci_do_sync_start(ahci_ctlp, ahci_portp, &pmult_addr, spkt);
4354 
4355 	if (rval == AHCI_SUCCESS &&
4356 	    spkt->satapkt_reason == SATA_PKT_COMPLETED) {
4357 		if (type == SATA_RDWR_PMULT_PKT_TYPE_READ) {
4358 			scmd = &spkt->satapkt_cmd;
4359 			*pregv = scmd->satacmd_lba_high_lsb << 24 |
4360 			    scmd->satacmd_lba_mid_lsb << 16 |
4361 			    scmd->satacmd_lba_low_lsb << 8 |
4362 			    scmd->satacmd_sec_count_lsb;
4363 		}
4364 	} else {
4365 		/* Failed or not completed. */
4366 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
4367 		    "ahci_rdwr_pmult: cannot [%s] %s[%d] at port %s",
4368 		    type == SATA_RDWR_PMULT_PKT_TYPE_READ?"Read":"Write",
4369 		    AHCI_ADDR_IS_PMULT(addrp)?"gscr":"pscr", regn, portstr);
4370 		rval = AHCI_FAILURE;
4371 	}
4372 out:
4373 	/* Restore the interrupt mask */
4374 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
4375 	    (uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port), intr_mask);
4376 
4377 	ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_RDWR_PMULT;
4378 	ahci_portp->ahciport_rdwr_pmult_pkt = NULL;
4379 	sata_free_rdwr_pmult_pkt(spkt);
4380 	return (rval);
4381 }
4382 
4383 static int
4384 ahci_read_pmult(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
4385     uint8_t regn, uint32_t *pregv)
4386 {
4387 	return ahci_rdwr_pmult(ahci_ctlp, addrp, regn, pregv,
4388 	    SATA_RDWR_PMULT_PKT_TYPE_READ);
4389 }
4390 
4391 static int
4392 ahci_write_pmult(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
4393     uint8_t regn, uint32_t regv)
4394 {
4395 	return ahci_rdwr_pmult(ahci_ctlp, addrp, regn, &regv,
4396 	    SATA_RDWR_PMULT_PKT_TYPE_WRITE);
4397 }
4398 
4399 #define	READ_PMULT(addrp, r, pv, out)					\
4400 	if (ahci_read_pmult(ahci_ctlp, addrp, r, pv) != AHCI_SUCCESS)	\
4401 		goto out;
4402 
4403 #define	WRITE_PMULT(addrp, r, v, out)					\
4404 	if (ahci_write_pmult(ahci_ctlp, addrp, r, v) != AHCI_SUCCESS)	\
4405 		goto out;
4406 
4407 /*
4408  * Update sata registers on port multiplier, including GSCR/PSCR registers.
4409  * ahci_update_pmult_gscr()
4410  * ahci_update_pmult_pscr()
4411  *
4412  * WARNING!!! ahciport_mutex should be acquired before those functions
4413  * get called.
4414  */
4415 static int
4416 ahci_update_pmult_gscr(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
4417     sata_pmult_gscr_t *sg)
4418 {
4419 	READ_PMULT(addrp, SATA_PMULT_GSCR0, &sg->gscr0, err);
4420 	READ_PMULT(addrp, SATA_PMULT_GSCR1, &sg->gscr1, err);
4421 	READ_PMULT(addrp, SATA_PMULT_GSCR2, &sg->gscr2, err);
4422 	READ_PMULT(addrp, SATA_PMULT_GSCR64, &sg->gscr64, err);
4423 
4424 	return (AHCI_SUCCESS);
4425 
4426 err:	/* R/W PMULT error */
4427 	return (AHCI_FAILURE);
4428 }
4429 
4430 static int
4431 ahci_update_pmult_pscr(ahci_ctl_t *ahci_ctlp, ahci_addr_t *addrp,
4432     sata_device_t *sd)
4433 {
4434 	ASSERT(AHCI_ADDR_IS_PMPORT(addrp));
4435 
4436 	READ_PMULT(addrp, SATA_PMULT_REG_SSTS, &sd->satadev_scr.sstatus, err);
4437 	READ_PMULT(addrp, SATA_PMULT_REG_SERR, &sd->satadev_scr.serror, err);
4438 	READ_PMULT(addrp, SATA_PMULT_REG_SCTL, &sd->satadev_scr.scontrol, err);
4439 	READ_PMULT(addrp, SATA_PMULT_REG_SACT, &sd->satadev_scr.sactive, err);
4440 
4441 	return (AHCI_SUCCESS);
4442 
4443 err:	/* R/W PMULT error */
4444 	return (AHCI_FAILURE);
4445 }
4446 
4447 /*
4448  * ahci_initialize_pmult()
4449  *
4450  * Initialize a port multiplier, including
4451  * 1. Enable FEATURES register at port multiplier. (SATA Chp.16)
4452  * 2. Redefine MASK register. (SATA Chap 16.?)
4453  *
4454  * WARNING!!! ahciport_mutex should be acquired before the function
4455  * is called.
4456  */
4457 static int
4458 ahci_initialize_pmult(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
4459     ahci_addr_t *addrp, sata_device_t *sd)
4460 {
4461 	sata_pmult_gscr_t sg;
4462 	uint32_t gscr64;
4463 	uint8_t port = addrp->aa_port;
4464 
4465 	ASSERT(mutex_owned(&ahci_portp->ahciport_mutex));
4466 	AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
4467 	    "[Initialize] Port-multiplier at port %d.", port);
4468 
4469 	/*
4470 	 * Enable features of port multiplier. Currently only
4471 	 * Asynchronous Notification is enabled.
4472 	 */
4473 	/* Check gscr64 for supported features. */
4474 	READ_PMULT(addrp, SATA_PMULT_GSCR64, &gscr64, err);
4475 
4476 	if (gscr64 & SATA_PMULT_CAP_SNOTIF) {
4477 		AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
4478 		    "port %d: Port Multiplier supports "
4479 		    "Asynchronous Notification.", port);
4480 
4481 		/* Write to gscr96 to enabled features */
4482 		WRITE_PMULT(addrp, SATA_PMULT_GSCR96,
4483 		    SATA_PMULT_CAP_SNOTIF, err);
4484 
4485 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
4486 		    (uint32_t *)AHCI_PORT_PxSNTF(ahci_ctlp, port),
4487 		    AHCI_SNOTIF_CLEAR_ALL);
4488 		AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
4489 		    "port %d: PMult PxSNTF cleared.", port);
4490 
4491 	}
4492 
4493 	/*
4494 	 * Now we need to update gscr33 register to enable hot-plug interrupt
4495 	 * for sub devices behind port multiplier.
4496 	 */
4497 	WRITE_PMULT(addrp, SATA_PMULT_GSCR33, (0x1ffff), err);
4498 	AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
4499 	    "port %d: gscr33 mask set to %x.", port, (0x1ffff));
4500 
4501 	/*
4502 	 * Fetch the number of device ports of the port multiplier
4503 	 */
4504 	if (ahci_update_pmult_gscr(ahci_ctlp, addrp, &sg) != AHCI_SUCCESS)
4505 		return (AHCI_FAILURE);
4506 
4507 	/* Register the port multiplier to SATA Framework. */
4508 	mutex_exit(&ahci_portp->ahciport_mutex);
4509 	sata_register_pmult(ahci_ctlp->ahcictl_dip, sd, &sg);
4510 	mutex_enter(&ahci_portp->ahciport_mutex);
4511 
4512 	ahci_portp->ahciport_pmult_info->ahcipmi_num_dev_ports =
4513 	    sd->satadev_add_info & SATA_PMULT_PORTNUM_MASK;
4514 
4515 	AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
4516 	    "port %d: pmult sub-port number updated to %x.", port,
4517 	    ahci_portp->ahciport_pmult_info->ahcipmi_num_dev_ports);
4518 
4519 	/* Till now port-mult is successfully initialized */
4520 	ahci_portp->ahciport_port_state |= SATA_DSTATE_PMULT_INIT;
4521 	return (AHCI_SUCCESS);
4522 
4523 err:	/* R/W PMULT error */
4524 	return (AHCI_FAILURE);
4525 }
4526 
4527 /*
4528  * Initialize a port multiplier port. According to spec, firstly we need
4529  * issue a COMRESET, then a software reset to get its signature.
4530  *
4531  * NOTE: This function should only be called in ahci_probe_pmport()
4532  * WARNING!!! ahciport_mutex should be acquired before the function.
4533  */
4534 static int
4535 ahci_initialize_pmport(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
4536     ahci_addr_t *addrp)
4537 {
4538 	uint32_t finished_tags = 0, reset_tags = 0, slot_status = 0;
4539 	uint8_t port = addrp->aa_port;
4540 	uint8_t pmport = addrp->aa_pmport;
4541 	int ret = AHCI_FAILURE;
4542 
4543 	ASSERT(AHCI_ADDR_IS_PMPORT(addrp));
4544 
4545 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
4546 	    "ahci_initialize_pmport: port %d:%d", port, pmport);
4547 
4548 	/* Check HBA port state */
4549 	if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED) {
4550 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
4551 		    "ahci_initialize_pmport:"
4552 		    "port %d:%d Port Multiplier is failed.",
4553 		    port, pmport);
4554 		return (AHCI_FAILURE);
4555 	}
4556 
4557 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_HOTPLUG) {
4558 		return (AHCI_FAILURE);
4559 	}
4560 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_HOTPLUG;
4561 
4562 	/* Checking for outstanding commands */
4563 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
4564 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
4565 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
4566 		reset_tags = slot_status & AHCI_SLOT_MASK(ahci_ctlp);
4567 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
4568 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
4569 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
4570 		reset_tags = slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
4571 	}
4572 
4573 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
4574 	ahci_portp->ahciport_mop_in_progress++;
4575 
4576 	/* Clear status */
4577 	AHCIPORT_SET_STATE(ahci_portp, addrp, SATA_STATE_UNKNOWN);
4578 
4579 	/* Firstly assume an unknown device */
4580 	AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_UNKNOWN);
4581 
4582 	ahci_disable_port_intrs(ahci_ctlp, port);
4583 
4584 	/* port reset is necessary for port multiplier port */
4585 	if (ahci_pmport_reset(ahci_ctlp, ahci_portp, addrp) != AHCI_SUCCESS) {
4586 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_ERRS, ahci_ctlp,
4587 		    "ahci_initialize_pmport:"
4588 		    "port reset failed at port %d:%d",
4589 		    port, pmport);
4590 		goto out;
4591 	}
4592 
4593 	/* Is port failed? */
4594 	if (AHCIPORT_GET_STATE(ahci_portp, addrp) &
4595 	    SATA_PSTATE_FAILED) {
4596 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
4597 		    "ahci_initialize_pmport: port %d:%d failed. "
4598 		    "state = 0x%x", port, pmport,
4599 		    ahci_portp->ahciport_port_state);
4600 		goto out;
4601 	}
4602 
4603 	/* Is there any device attached? */
4604 	if (AHCIPORT_GET_DEV_TYPE(ahci_portp, addrp)
4605 	    == SATA_DTYPE_NONE) {
4606 		/* Do not waste time on an empty port */
4607 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
4608 		    "ahci_initialize_pmport: No device is found "
4609 		    "at port %d:%d", port, pmport);
4610 		ret = AHCI_SUCCESS;
4611 		goto out;
4612 	}
4613 
4614 	AHCIPORT_SET_STATE(ahci_portp, addrp, SATA_STATE_READY);
4615 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
4616 	    "port %d:%d is ready now.", port, pmport);
4617 
4618 	/*
4619 	 * Till now we can assure a device attached to that HBA port and work
4620 	 * correctly. Now try to get the device signature. This is an optional
4621 	 * step. If failed, unknown device is assumed, then SATA module will
4622 	 * continue to use IDENTIFY DEVICE to get the information of the
4623 	 * device.
4624 	 */
4625 	ahci_find_dev_signature(ahci_ctlp, ahci_portp, addrp);
4626 
4627 	ret = AHCI_SUCCESS;
4628 
4629 out:
4630 	/* Next try to mop the pending commands */
4631 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp))
4632 		finished_tags = ahci_portp->ahciport_pending_tags &
4633 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
4634 	else if (NCQ_CMD_IN_PROGRESS(ahci_portp))
4635 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
4636 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
4637 	reset_tags &= ~finished_tags;
4638 
4639 	ahci_mop_commands(ahci_ctlp,
4640 	    ahci_portp,
4641 	    slot_status,
4642 	    0, /* failed tags */
4643 	    0, /* timeout tags */
4644 	    0, /* aborted tags */
4645 	    reset_tags); /* reset tags */
4646 
4647 	/* Clear PxSNTF register if supported. */
4648 	if (ahci_ctlp->ahcictl_cap & AHCI_CAP_SNTF) {
4649 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
4650 		    (uint32_t *)AHCI_PORT_PxSNTF(ahci_ctlp, port),
4651 		    AHCI_SNOTIF_CLEAR_ALL);
4652 	}
4653 
4654 	ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_HOTPLUG;
4655 	ahci_enable_port_intrs(ahci_ctlp, port);
4656 	return (ret);
4657 }
4658 
4659 /*
4660  * ahci_probe_pmult()
4661  *
4662  * This function will be called to probe a port multiplier, which will
4663  * handle hotplug events on port multiplier ports.
4664  *
4665  * NOTE: Only called from ahci_tran_probe_port()
4666  * WARNING!!! ahciport_mutex should be acquired before the function is called.
4667  */
4668 static int
4669 ahci_probe_pmult(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
4670     ahci_addr_t *addrp)
4671 {
4672 	sata_device_t sdevice;
4673 	ahci_addr_t pmport_addr;
4674 	uint32_t gscr32, port_hotplug_tags;
4675 	uint32_t pmport_sstatus;
4676 	int dev_exists_now = 0, dev_existed_previously = 0;
4677 	uint8_t port = addrp->aa_port;
4678 	int npmport;
4679 
4680 	/* The bits in GSCR32 refers to the pmport that has a hot-plug event. */
4681 	READ_PMULT(addrp, SATA_PMULT_GSCR32, &gscr32, err);
4682 	port_hotplug_tags = gscr32 & AHCI_PMPORT_MASK(ahci_portp);
4683 
4684 	do {
4685 		npmport = ddi_ffs(port_hotplug_tags) - 1;
4686 		if (npmport == -1)
4687 			/* no pending hot plug events. */
4688 			return (AHCI_SUCCESS);
4689 
4690 		AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
4691 		    "hot-plug event at port %d:%d", port, npmport);
4692 
4693 		AHCI_ADDR_SET_PMPORT(&pmport_addr, port, (uint8_t)npmport);
4694 
4695 		/* Check previous device at that port */
4696 		if (AHCIPORT_GET_DEV_TYPE(ahci_portp, &pmport_addr)
4697 		    != SATA_DTYPE_NONE)
4698 			dev_existed_previously = 1;
4699 
4700 		/* PxSStatus tells the presence of device. */
4701 		READ_PMULT(&pmport_addr, SATA_PMULT_REG_SSTS,
4702 		    &pmport_sstatus, err);
4703 
4704 		if (SSTATUS_GET_DET(pmport_sstatus) ==
4705 		    SSTATUS_DET_DEVPRE_PHYCOM)
4706 			dev_exists_now = 1;
4707 
4708 		/*
4709 		 * Clear PxSERR is critical. The transition from 0 to 1 will
4710 		 * emit a FIS which generates an asynchronous notification
4711 		 * event at controller. If we fail to clear the PxSERR, the
4712 		 * Async Notif events will no longer be activated on this
4713 		 * pmport.
4714 		 */
4715 		WRITE_PMULT(&pmport_addr, SATA_PMULT_REG_SERR,
4716 		    AHCI_SERROR_CLEAR_ALL, err);
4717 
4718 		bzero((void *)&sdevice, sizeof (sata_device_t));
4719 		sdevice.satadev_addr.cport = ahci_ctlp->
4720 		    ahcictl_port_to_cport[port];
4721 		sdevice.satadev_addr.qual = SATA_ADDR_PMPORT;
4722 		sdevice.satadev_addr.pmport = (uint8_t)npmport;
4723 		sdevice.satadev_state = SATA_PSTATE_PWRON;
4724 
4725 		AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
4726 		    "[Existence] %d -> %d", dev_existed_previously,
4727 		    dev_exists_now);
4728 
4729 		if (dev_exists_now) {
4730 			if (dev_existed_previously) {
4731 				/* Link (may) not change: Exist -> Exist * */
4732 				AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
4733 				    "ahci_probe_pmult: port %d:%d "
4734 				    "device link lost/established",
4735 				    port, npmport);
4736 
4737 				mutex_exit(&ahci_portp->ahciport_mutex);
4738 				sata_hba_event_notify(
4739 				    ahci_ctlp->ahcictl_sata_hba_tran->
4740 				    sata_tran_hba_dip,
4741 				    &sdevice,
4742 				    SATA_EVNT_LINK_LOST|
4743 				    SATA_EVNT_LINK_ESTABLISHED);
4744 				mutex_enter(&ahci_portp->ahciport_mutex);
4745 			} else {
4746 				/* Link change: None -> Exist */
4747 				AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
4748 				    "ahci_probe_pmult: port %d:%d "
4749 				    "device link established", port, npmport);
4750 
4751 				/* Clear port state */
4752 				AHCIPORT_SET_STATE(ahci_portp, &pmport_addr,
4753 				    SATA_STATE_UNKNOWN);
4754 				AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
4755 				    "ahci_probe_pmult: port %d "
4756 				    "ahciport_port_state [Cleared].", port);
4757 
4758 				mutex_exit(&ahci_portp->ahciport_mutex);
4759 				sata_hba_event_notify(
4760 				    ahci_ctlp->ahcictl_sata_hba_tran->
4761 				    sata_tran_hba_dip,
4762 				    &sdevice,
4763 				    SATA_EVNT_LINK_ESTABLISHED);
4764 				mutex_enter(&ahci_portp->ahciport_mutex);
4765 			}
4766 		} else { /* No device exists now */
4767 			if (dev_existed_previously) {
4768 
4769 				/* Link change: Exist -> None */
4770 				AHCIDBG(AHCIDBG_EVENT|AHCIDBG_PMULT, ahci_ctlp,
4771 				    "ahci_probe_pmult: port %d:%d "
4772 				    "device link lost", port, npmport);
4773 
4774 				/* An existing device is lost. */
4775 				AHCIPORT_SET_STATE(ahci_portp, &pmport_addr,
4776 				    SATA_STATE_UNKNOWN);
4777 				AHCIPORT_SET_DEV_TYPE(ahci_portp, &pmport_addr,
4778 				    SATA_DTYPE_NONE);
4779 
4780 				mutex_exit(&ahci_portp->ahciport_mutex);
4781 				sata_hba_event_notify(
4782 				    ahci_ctlp->ahcictl_sata_hba_tran->
4783 				    sata_tran_hba_dip,
4784 				    &sdevice,
4785 				    SATA_EVNT_LINK_LOST);
4786 				mutex_enter(&ahci_portp->ahciport_mutex);
4787 			}
4788 		}
4789 
4790 		CLEAR_BIT(port_hotplug_tags, npmport);
4791 	} while (port_hotplug_tags != 0);
4792 
4793 	return (AHCI_SUCCESS);
4794 
4795 err:	/* R/W PMULT error */
4796 	return (AHCI_FAILURE);
4797 }
4798 
4799 /*
4800  * Probe and initialize a port multiplier port.
4801  * A port multiplier port could only be initilaizer here.
4802  *
4803  * WARNING!!! ahcictl_mutex should be acquired before the function
4804  * is called.
4805  */
4806 static int
4807 ahci_probe_pmport(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
4808     ahci_addr_t *addrp, sata_device_t *sd)
4809 {
4810 	uint32_t port_state;
4811 	uint8_t port = addrp->aa_port;
4812 	ahci_addr_t addr_pmult;
4813 
4814 	/*
4815 	 * Check the parent - port multiplier first.
4816 	 */
4817 
4818 	/*
4819 	 * Parent port multiplier might have been removed. This event will be
4820 	 * ignored and failure.
4821 	 */
4822 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE ||
4823 	    ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) {
4824 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
4825 		    "ahci_tran_probe_port: "
4826 		    "parent device removed, ignore event.", NULL);
4827 
4828 		return (AHCI_FAILURE);
4829 	}
4830 
4831 	/* The port is ready? */
4832 	port_state = ahci_portp->ahciport_port_state;
4833 	if (!(port_state & SATA_STATE_READY)) {
4834 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
4835 		    "ahci_tran_probe_port: "
4836 		    "parent port-mult is NOT ready.", NULL);
4837 
4838 		if (ahci_restart_port_wait_till_ready(ahci_ctlp,
4839 		    ahci_portp, port, AHCI_PORT_RESET, NULL) !=
4840 		    AHCI_SUCCESS) {
4841 			AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
4842 			    "ahci_tran_probe_port: "
4843 			    "restart port-mult failed.", NULL);
4844 			return (AHCI_FAILURE);
4845 		}
4846 	}
4847 
4848 	/*
4849 	 * If port-mult is restarted due to some reason, we need
4850 	 * re-initialized the PMult.
4851 	 */
4852 	if (!(port_state & SATA_DSTATE_PMULT_INIT)) {
4853 		/* Initialize registers on a port multiplier */
4854 		AHCI_ADDR_SET_PMULT(&addr_pmult, addrp->aa_port);
4855 		if (ahci_initialize_pmult(ahci_ctlp, ahci_portp,
4856 		    &addr_pmult, sd) != AHCI_SUCCESS)
4857 			return (AHCI_FAILURE);
4858 	}
4859 
4860 	/*
4861 	 * Then we check the port-mult port
4862 	 */
4863 	/* Is this pmport initialized? */
4864 	port_state = AHCIPORT_GET_STATE(ahci_portp, addrp);
4865 	if (!(port_state & SATA_STATE_READY)) {
4866 
4867 		/* ahci_initialize_pmport() will set READY state */
4868 		if (ahci_initialize_pmport(ahci_ctlp,
4869 		    ahci_portp, addrp) != AHCI_SUCCESS)
4870 			return (AHCI_FAILURE);
4871 	}
4872 
4873 	return (AHCI_SUCCESS);
4874 }
4875 
4876 /*
4877  * AHCI device reset ...; a single device on one of the ports is reset,
4878  * but the HBA and physical communication remain intact. This is the
4879  * least intrusive.
4880  *
4881  * When issuing a software reset sequence, there should not be other
4882  * commands in the command list, so we will first clear and then re-set
4883  * PxCMD.ST to clear PxCI. And before issuing the software reset,
4884  * the port must be idle and PxTFD.STS.BSY and PxTFD.STS.DRQ must be
4885  * cleared unless command list override (PxCMD.CLO) is supported.
4886  *
4887  * WARNING!!! ahciport_mutex should be acquired and PxCMD.FRE should be
4888  * set before the function is called.
4889  */
4890 static int
4891 ahci_software_reset(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
4892     ahci_addr_t *addrp)
4893 {
4894 	ahci_fis_h2d_register_t *h2d_register_fisp;
4895 	ahci_cmd_table_t *cmd_table;
4896 	ahci_cmd_header_t *cmd_header;
4897 	uint32_t port_cmd_status, port_cmd_issue, port_task_file;
4898 	int slot, loop_count;
4899 	uint8_t port = addrp->aa_port;
4900 	uint8_t pmport = addrp->aa_pmport;
4901 	int rval = AHCI_FAILURE;
4902 
4903 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
4904 	    "port %d:%d device software resetting (FIS)", port, pmport);
4905 
4906 	/* First clear PxCMD.ST (AHCI v1.2 10.4.1) */
4907 	if (ahci_put_port_into_notrunning_state(ahci_ctlp, ahci_portp,
4908 	    port) != AHCI_SUCCESS) {
4909 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
4910 		    "ahci_software_reset: cannot stop HBA port %d.", port);
4911 		goto out;
4912 	}
4913 
4914 	/* Check PxTFD.STS.BSY and PxTFD.STS.DRQ */
4915 	port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
4916 	    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
4917 
4918 	if (port_task_file & AHCI_TFD_STS_BSY ||
4919 	    port_task_file & AHCI_TFD_STS_DRQ) {
4920 		if (!(ahci_ctlp->ahcictl_cap & AHCI_CAP_SCLO)) {
4921 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
4922 			    "PxTFD.STS.BSY/DRQ is set (PxTFD=0x%x), "
4923 			    "cannot issue a software reset.", port_task_file);
4924 			goto out;
4925 		}
4926 
4927 		/*
4928 		 * If HBA Support CLO, as Command List Override (CAP.SCLO is
4929 		 * set), PxCMD.CLO bit should be set before set PxCMD.ST, in
4930 		 * order to clear PxTFD.STS.BSY and PxTFD.STS.DRQ.
4931 		 */
4932 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
4933 		    "PxTFD.STS.BSY/DRQ is set, try SCLO.", NULL)
4934 
4935 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
4936 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
4937 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
4938 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
4939 		    port_cmd_status|AHCI_CMD_STATUS_CLO);
4940 
4941 		/* Waiting till PxCMD.SCLO bit is cleared */
4942 		loop_count = 0;
4943 		do {
4944 			/* Wait for 10 millisec */
4945 			drv_usecwait(AHCI_10MS_USECS);
4946 
4947 			/* We are effectively timing out after 1 sec. */
4948 			if (loop_count++ > 100) {
4949 				AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
4950 				    "SCLO time out. port %d is busy.", port);
4951 				goto out;
4952 			}
4953 
4954 			port_cmd_status =
4955 			    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
4956 			    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
4957 		} while (port_cmd_status & AHCI_CMD_STATUS_CLO);
4958 
4959 		/* Re-check */
4960 		port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
4961 		    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
4962 		if (port_task_file & AHCI_TFD_STS_BSY ||
4963 		    port_task_file & AHCI_TFD_STS_DRQ) {
4964 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
4965 			    "SCLO cannot clear PxTFD.STS.BSY/DRQ (PxTFD=0x%x)",
4966 			    port_task_file);
4967 			goto out;
4968 		}
4969 	}
4970 
4971 	/* Then start port */
4972 	if (ahci_start_port(ahci_ctlp, ahci_portp, port)
4973 	    != AHCI_SUCCESS) {
4974 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
4975 		    "ahci_software_reset: cannot start AHCI port %d.", port);
4976 		goto out;
4977 	}
4978 
4979 	/*
4980 	 * When ahci_port.ahciport_mop_in_progress is set, A non-zero
4981 	 * ahci_port.ahciport_pending_ncq_tags may fail
4982 	 * ahci_claim_free_slot(). Actually according to spec, by clearing
4983 	 * PxCMD.ST there is no command outstanding while executing software
4984 	 * reseting. Hence we directly use slot 0 instead of
4985 	 * ahci_claim_free_slot().
4986 	 */
4987 	slot = 0;
4988 
4989 	/* Now send the first H2D Register FIS with SRST set to 1 */
4990 	cmd_table = ahci_portp->ahciport_cmd_tables[slot];
4991 	bzero((void *)cmd_table, ahci_cmd_table_size);
4992 
4993 	h2d_register_fisp =
4994 	    &(cmd_table->ahcict_command_fis.ahcifc_fis.ahcifc_h2d_register);
4995 
4996 	SET_FIS_TYPE(h2d_register_fisp, AHCI_H2D_REGISTER_FIS_TYPE);
4997 	SET_FIS_PMP(h2d_register_fisp, pmport);
4998 	SET_FIS_DEVCTL(h2d_register_fisp, SATA_DEVCTL_SRST);
4999 
5000 	/* Set Command Header in Command List */
5001 	cmd_header = &ahci_portp->ahciport_cmd_list[slot];
5002 	BZERO_DESCR_INFO(cmd_header);
5003 	BZERO_PRD_BYTE_COUNT(cmd_header);
5004 	SET_COMMAND_FIS_LENGTH(cmd_header, 5);
5005 	SET_PORT_MULTI_PORT(cmd_header, pmport);
5006 
5007 	SET_CLEAR_BUSY_UPON_R_OK(cmd_header, 1);
5008 	SET_RESET(cmd_header, 1);
5009 	SET_WRITE(cmd_header, 1);
5010 
5011 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_tables_dma_handle[slot],
5012 	    0,
5013 	    ahci_cmd_table_size,
5014 	    DDI_DMA_SYNC_FORDEV);
5015 
5016 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_list_dma_handle,
5017 	    slot * sizeof (ahci_cmd_header_t),
5018 	    sizeof (ahci_cmd_header_t),
5019 	    DDI_DMA_SYNC_FORDEV);
5020 
5021 	/* Indicate to the HBA that a command is active. */
5022 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5023 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port),
5024 	    (0x1 << slot));
5025 
5026 	loop_count = 0;
5027 
5028 	/* Loop till the first command is finished */
5029 	do {
5030 		port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5031 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
5032 
5033 		/* We are effectively timing out after 1 sec. */
5034 		if (loop_count++ > AHCI_POLLRATE_PORT_SOFTRESET) {
5035 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
5036 			    "the first SRST FIS is timed out, "
5037 			    "loop_count = %d", loop_count);
5038 			goto out;
5039 		}
5040 		/* Wait for 10 millisec */
5041 		drv_usecwait(AHCI_10MS_USECS);
5042 	} while (port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp) & (0x1 << slot));
5043 
5044 	AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
5045 	    "ahci_software_reset: 1st loop count: %d, "
5046 	    "port_cmd_issue = 0x%x, slot = 0x%x",
5047 	    loop_count, port_cmd_issue, slot);
5048 
5049 	/* According to ATA spec, we need wait at least 5 microsecs here. */
5050 	drv_usecwait(AHCI_1MS_USECS);
5051 
5052 	/* Now send the second H2D Register FIS with SRST cleard to zero */
5053 	cmd_table = ahci_portp->ahciport_cmd_tables[slot];
5054 	bzero((void *)cmd_table, ahci_cmd_table_size);
5055 
5056 	h2d_register_fisp =
5057 	    &(cmd_table->ahcict_command_fis.ahcifc_fis.ahcifc_h2d_register);
5058 
5059 	SET_FIS_TYPE(h2d_register_fisp, AHCI_H2D_REGISTER_FIS_TYPE);
5060 	SET_FIS_PMP(h2d_register_fisp, pmport);
5061 
5062 	/* Set Command Header in Command List */
5063 	cmd_header = &ahci_portp->ahciport_cmd_list[slot];
5064 	BZERO_DESCR_INFO(cmd_header);
5065 	BZERO_PRD_BYTE_COUNT(cmd_header);
5066 	SET_COMMAND_FIS_LENGTH(cmd_header, 5);
5067 	SET_PORT_MULTI_PORT(cmd_header, pmport);
5068 
5069 	SET_WRITE(cmd_header, 1);
5070 
5071 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_tables_dma_handle[slot],
5072 	    0,
5073 	    ahci_cmd_table_size,
5074 	    DDI_DMA_SYNC_FORDEV);
5075 
5076 	(void) ddi_dma_sync(ahci_portp->ahciport_cmd_list_dma_handle,
5077 	    slot * sizeof (ahci_cmd_header_t),
5078 	    sizeof (ahci_cmd_header_t),
5079 	    DDI_DMA_SYNC_FORDEV);
5080 
5081 	/* Indicate to the HBA that a command is active. */
5082 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5083 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port),
5084 	    (0x1 << slot));
5085 
5086 	loop_count = 0;
5087 
5088 	/* Loop till the second command is finished */
5089 	do {
5090 		port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5091 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
5092 
5093 		/* We are effectively timing out after 1 sec. */
5094 		if (loop_count++ > AHCI_POLLRATE_PORT_SOFTRESET) {
5095 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
5096 			    "the second SRST FIS is timed out, "
5097 			    "loop_count = %d", loop_count);
5098 			goto out;
5099 		}
5100 
5101 		/* Wait for 10 millisec */
5102 		drv_usecwait(AHCI_10MS_USECS);
5103 	} while (port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp) & (0x1 << slot));
5104 
5105 	AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
5106 	    "ahci_software_reset: 2nd loop count: %d, "
5107 	    "port_cmd_issue = 0x%x, slot = 0x%x",
5108 	    loop_count, port_cmd_issue, slot);
5109 
5110 	if ((ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) ||
5111 	    (ahci_check_port_handle(ahci_ctlp, port) != DDI_SUCCESS)) {
5112 		ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
5113 		    DDI_SERVICE_UNAFFECTED);
5114 		goto out;
5115 	}
5116 
5117 	rval = AHCI_SUCCESS;
5118 out:
5119 	AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
5120 	    "ahci_software_reset: %s at port %d:%d",
5121 	    rval == AHCI_SUCCESS ? "succeed" : "failed",
5122 	    port, pmport);
5123 
5124 	return (rval);
5125 }
5126 
5127 /*
5128  * AHCI port reset ...; the physical communication between the HBA and device
5129  * on a port are disabled. This is more intrusive.
5130  *
5131  * When an HBA or port reset occurs, Phy communication is going to
5132  * be re-established with the device through a COMRESET followed by the
5133  * normal out-of-band communication sequence defined in Serial ATA. AT
5134  * the end of reset, the device, if working properly, will send a D2H
5135  * Register FIS, which contains the device signature. When the HBA receives
5136  * this FIS, it updates PxTFD.STS and PxTFD.ERR register fields, and updates
5137  * the PxSIG register with the signature.
5138  *
5139  * Staggered spin-up is an optional feature in SATA II, and it enables an HBA
5140  * to individually spin-up attached devices. Please refer to chapter 10.9 of
5141  * AHCI 1.0 spec.
5142  */
5143 /*
5144  * WARNING!!! ahciport_mutex should be acquired, and PxCMD.ST should be also
5145  * cleared before the function is called.
5146  */
5147 static int
5148 ahci_port_reset(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
5149     ahci_addr_t *addrp)
5150 {
5151 	ahci_addr_t pmult_addr;
5152 	uint32_t cap_status, port_cmd_status;
5153 	uint32_t port_scontrol, port_sstatus, port_serror;
5154 	uint32_t port_intr_status, port_task_file;
5155 	uint32_t port_state;
5156 	uint8_t port = addrp->aa_port;
5157 
5158 	int loop_count;
5159 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
5160 
5161 	/* Target is a port multiplier port? */
5162 	if (AHCI_ADDR_IS_PMPORT(addrp))
5163 		return (ahci_pmport_reset(ahci_ctlp, ahci_portp, addrp));
5164 
5165 	/* Otherwise it must be an HBA port. */
5166 	ASSERT(AHCI_ADDR_IS_PORT(addrp));
5167 
5168 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
5169 	    "Port %d port resetting...", port);
5170 	ahci_portp->ahciport_port_state = 0;
5171 
5172 	cap_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5173 	    (uint32_t *)AHCI_GLOBAL_CAP(ahci_ctlp));
5174 
5175 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5176 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
5177 
5178 	if (cap_status & AHCI_HBA_CAP_SSS) {
5179 		/*
5180 		 * HBA support staggered spin-up, if the port has
5181 		 * not spin up yet, then force it to do spin-up
5182 		 */
5183 		if (!(port_cmd_status & AHCI_CMD_STATUS_SUD)) {
5184 			if (!(ahci_portp->ahciport_flags
5185 			    & AHCI_PORT_FLAG_SPINUP)) {
5186 				AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
5187 				    "Port %d PxCMD.SUD is zero, force "
5188 				    "it to do spin-up", port);
5189 				ahci_portp->ahciport_flags |=
5190 				    AHCI_PORT_FLAG_SPINUP;
5191 			}
5192 		}
5193 	} else {
5194 		/*
5195 		 * HBA doesn't support stagger spin-up, force it
5196 		 * to do normal COMRESET
5197 		 */
5198 		if (ahci_portp->ahciport_flags &
5199 		    AHCI_PORT_FLAG_SPINUP) {
5200 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
5201 			    "HBA does not support staggered spin-up "
5202 			    "force it to do normal COMRESET", NULL);
5203 			ahci_portp->ahciport_flags &=
5204 			    ~AHCI_PORT_FLAG_SPINUP;
5205 		}
5206 	}
5207 
5208 	if (!(ahci_portp->ahciport_flags & AHCI_PORT_FLAG_SPINUP)) {
5209 		/* Do normal COMRESET */
5210 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
5211 		    "ahci_port_reset: do normal COMRESET", port);
5212 
5213 		/*
5214 		 * According to the spec, SUD bit should be set here,
5215 		 * but JMicron JMB363 doesn't follow it, so remove
5216 		 * the assertion, and just print a debug message.
5217 		 */
5218 #if AHCI_DEBUG
5219 		if (!(port_cmd_status & AHCI_CMD_STATUS_SUD))
5220 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
5221 			    "port %d SUD bit not set", port)
5222 #endif
5223 
5224 		port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5225 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
5226 		SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_COMRESET);
5227 
5228 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5229 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port),
5230 		    port_scontrol);
5231 
5232 		/* Enable PxCMD.FRE to read device */
5233 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5234 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
5235 		    port_cmd_status|AHCI_CMD_STATUS_FRE);
5236 
5237 		/*
5238 		 * Give time for COMRESET to percolate, according to the AHCI
5239 		 * spec, software shall wait at least 1 millisecond before
5240 		 * clearing PxSCTL.DET
5241 		 */
5242 		drv_usecwait(AHCI_1MS_USECS*2);
5243 
5244 		/* Fetch the SCONTROL again and rewrite the DET part with 0 */
5245 		port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5246 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
5247 		SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_NOACTION);
5248 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5249 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port),
5250 		    port_scontrol);
5251 	} else {
5252 		/* Do staggered spin-up */
5253 		port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5254 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
5255 		SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_NOACTION);
5256 
5257 		/* PxSCTL.DET must be 0 */
5258 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5259 		    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port),
5260 		    port_scontrol);
5261 
5262 		port_cmd_status &= ~AHCI_CMD_STATUS_SUD;
5263 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5264 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
5265 		    port_cmd_status);
5266 
5267 		/* 0 -> 1 edge */
5268 		drv_usecwait(AHCI_1MS_USECS*2);
5269 
5270 		/* Set PxCMD.SUD to 1 */
5271 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5272 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
5273 		port_cmd_status |= AHCI_CMD_STATUS_SUD;
5274 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5275 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
5276 		    port_cmd_status);
5277 
5278 		/* Enable PxCMD.FRE to read device */
5279 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5280 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
5281 		    port_cmd_status|AHCI_CMD_STATUS_FRE);
5282 	}
5283 
5284 	/*
5285 	 * The port enters P:StartComm state, and HBA tells link layer to
5286 	 * start communication, which involves sending COMRESET to device.
5287 	 * And the HBA resets PxTFD.STS to 7Fh.
5288 	 *
5289 	 * When a COMINIT is received from the device, then the port enters
5290 	 * P:ComInit state. And HBA sets PxTFD.STS to FFh or 80h. HBA sets
5291 	 * PxSSTS.DET to 1h to indicate a device is detected but communication
5292 	 * is not yet established. HBA sets PxSERR.DIAG.X to '1' to indicate
5293 	 * a COMINIT has been received.
5294 	 */
5295 	/*
5296 	 * The DET field is valid only if IPM field indicates
5297 	 * that the interface is in active state.
5298 	 */
5299 	loop_count = 0;
5300 	do {
5301 		port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5302 		    (uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
5303 
5304 		if (SSTATUS_GET_IPM(port_sstatus) != SSTATUS_IPM_ACTIVE) {
5305 			/*
5306 			 * If the interface is not active, the DET field
5307 			 * is considered not accurate. So we want to
5308 			 * continue looping.
5309 			 */
5310 			SSTATUS_SET_DET(port_sstatus, SSTATUS_DET_NODEV);
5311 		}
5312 
5313 		if (loop_count++ > AHCI_POLLRATE_PORT_SSTATUS) {
5314 			/*
5315 			 * We are effectively timing out after 0.1 sec.
5316 			 */
5317 			break;
5318 		}
5319 
5320 		/* Wait for 10 millisec */
5321 		drv_usecwait(AHCI_10MS_USECS);
5322 	} while (SSTATUS_GET_DET(port_sstatus) != SSTATUS_DET_DEVPRE_PHYCOM);
5323 
5324 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
5325 	    "ahci_port_reset: 1st loop count: %d, "
5326 	    "port_sstatus = 0x%x port %d",
5327 	    loop_count, port_sstatus, port);
5328 
5329 	if ((SSTATUS_GET_IPM(port_sstatus) != SSTATUS_IPM_ACTIVE) ||
5330 	    (SSTATUS_GET_DET(port_sstatus) != SSTATUS_DET_DEVPRE_PHYCOM)) {
5331 		/*
5332 		 * Either the port is not active or there
5333 		 * is no device present.
5334 		 */
5335 		AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_NONE);
5336 		return (AHCI_SUCCESS);
5337 	}
5338 
5339 	/* Now we can make sure there is a device connected to the port */
5340 	port_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5341 	    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port));
5342 	port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5343 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
5344 
5345 	/*
5346 	 * A COMINIT signal is supposed to be received
5347 	 * PxSERR.DIAG.X or PxIS.PCS should be set
5348 	 */
5349 	if (!(port_intr_status & AHCI_INTR_STATUS_PCS) &&
5350 	    !(port_serror & SERROR_EXCHANGED_ERR)) {
5351 		cmn_err(CE_WARN, "!ahci%d: ahci_port_reset port %d "
5352 		    "COMINIT signal from the device not received",
5353 		    instance, port);
5354 		AHCIPORT_SET_STATE(ahci_portp, addrp, SATA_PSTATE_FAILED);
5355 		return (AHCI_FAILURE);
5356 	}
5357 
5358 	/*
5359 	 * According to the spec, when PxSCTL.DET is set to 0h, upon
5360 	 * receiving a COMINIT from the attached device, PxTFD.STS.BSY
5361 	 * shall be set to '1' by the HBA.
5362 	 *
5363 	 * However, we found JMicron JMB363 doesn't follow this, so
5364 	 * remove this check, and just print a debug message.
5365 	 */
5366 #if AHCI_DEBUG
5367 	port_task_file = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5368 	    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
5369 	if (!(port_task_file & AHCI_TFD_STS_BSY)) {
5370 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_port_reset: "
5371 		    "port %d BSY bit is not set after COMINIT signal "
5372 		    "is received", port);
5373 	}
5374 #endif
5375 
5376 	/*
5377 	 * PxSERR.DIAG.X has to be cleared in order to update PxTFD with
5378 	 * the D2H FIS received by HBA.
5379 	 */
5380 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5381 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
5382 	    SERROR_EXCHANGED_ERR);
5383 
5384 	/*
5385 	 * Devices should return a FIS contains its signature to HBA after
5386 	 * COMINIT signal. Check whether a D2H FIS is received by polling
5387 	 * PxTFD.STS.ERR bit.
5388 	 */
5389 	loop_count = 0;
5390 	do {
5391 		/* Wait for 10 millisec */
5392 		drv_usecwait(AHCI_10MS_USECS);
5393 
5394 		if (loop_count++ > AHCI_POLLRATE_PORT_TFD_ERROR) {
5395 			/*
5396 			 * We are effectively timing out after 11 sec.
5397 			 */
5398 			cmn_err(CE_WARN, "!ahci%d: ahci_port_reset port %d "
5399 			    "the device hardware has been initialized and "
5400 			    "the power-up diagnostics failed",
5401 			    instance, port);
5402 
5403 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_port_reset: "
5404 			    "port %d PxTFD.STS.ERR is not set, we need another "
5405 			    "software reset.", port);
5406 
5407 			/* Clear port serror register for the port */
5408 			ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5409 			    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
5410 			    AHCI_SERROR_CLEAR_ALL);
5411 
5412 			AHCI_ADDR_SET_PMULT(&pmult_addr, port);
5413 
5414 			/* Try another software reset. */
5415 			if (ahci_software_reset(ahci_ctlp, ahci_portp,
5416 			    &pmult_addr) != AHCI_SUCCESS) {
5417 				AHCIPORT_SET_STATE(ahci_portp, addrp,
5418 				    SATA_PSTATE_FAILED);
5419 				return (AHCI_FAILURE);
5420 			}
5421 			break;
5422 		}
5423 
5424 		/* Wait for 10 millisec */
5425 		drv_usecwait(AHCI_10MS_USECS);
5426 
5427 		/*
5428 		 * The Error bit '1' means COMRESET is finished successfully
5429 		 * The device hardware has been initialized and the power-up
5430 		 * diagnostics successfully completed. The device requests
5431 		 * that the Transport layer transmit a Register - D2H FIS to
5432 		 * the host. (SATA spec 11.5, v2.6)
5433 		 */
5434 		port_task_file =
5435 		    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5436 		    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
5437 	} while (((port_task_file & AHCI_TFD_ERR_MASK)
5438 	    >> AHCI_TFD_ERR_SHIFT) != AHCI_TFD_ERR_SGS);
5439 
5440 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
5441 	    "ahci_port_reset: 2nd loop count: %d, "
5442 	    "port_task_file = 0x%x port %d",
5443 	    loop_count, port_task_file, port);
5444 
5445 	/* Clear port serror register for the port */
5446 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5447 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
5448 	    AHCI_SERROR_CLEAR_ALL);
5449 
5450 	/* Set port as ready */
5451 	port_state = AHCIPORT_GET_STATE(ahci_portp, addrp);
5452 	AHCIPORT_SET_STATE(ahci_portp, addrp, port_state|SATA_STATE_READY);
5453 
5454 	AHCIDBG(AHCIDBG_INFO|AHCIDBG_ERRS, ahci_ctlp,
5455 	    "ahci_port_reset: succeed at port %d.", port);
5456 	return (AHCI_SUCCESS);
5457 }
5458 
5459 /*
5460  * COMRESET on a port multiplier port.
5461  *
5462  * NOTE: Only called in ahci_port_reset()
5463  */
5464 static int
5465 ahci_pmport_reset(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
5466     ahci_addr_t *addrp)
5467 {
5468 	uint32_t port_scontrol, port_sstatus, port_serror;
5469 	uint32_t port_cmd_status, port_intr_status;
5470 	uint32_t port_state;
5471 	uint8_t port = addrp->aa_port;
5472 	uint8_t pmport = addrp->aa_pmport;
5473 	int loop_count;
5474 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
5475 
5476 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
5477 	    "port %d:%d: pmport resetting", port, pmport);
5478 
5479 	/* Initialize pmport state */
5480 	AHCIPORT_SET_STATE(ahci_portp, addrp, 0);
5481 
5482 	READ_PMULT(addrp, SATA_PMULT_REG_SCTL, &port_scontrol, err);
5483 	SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_COMRESET);
5484 	WRITE_PMULT(addrp, SATA_PMULT_REG_SCTL, port_scontrol, err);
5485 
5486 	/* PxCMD.FRE should be set before. */
5487 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5488 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
5489 	ASSERT(port_cmd_status & AHCI_CMD_STATUS_FRE);
5490 	if (!(port_cmd_status & AHCI_CMD_STATUS_FRE))
5491 		return (AHCI_FAILURE);
5492 
5493 	/*
5494 	 * Give time for COMRESET to percolate, according to the AHCI
5495 	 * spec, software shall wait at least 1 millisecond before
5496 	 * clearing PxSCTL.DET
5497 	 */
5498 	drv_usecwait(AHCI_1MS_USECS*2);
5499 
5500 	/*
5501 	 * Fetch the SCONTROL again and rewrite the DET part with 0
5502 	 * This will generate an Asychronous Notification events.
5503 	 */
5504 	READ_PMULT(addrp, SATA_PMULT_REG_SCTL, &port_scontrol, err);
5505 	SCONTROL_SET_DET(port_scontrol, SCONTROL_DET_NOACTION);
5506 	WRITE_PMULT(addrp, SATA_PMULT_REG_SCTL, port_scontrol, err);
5507 
5508 	/*
5509 	 * The port enters P:StartComm state, and HBA tells link layer to
5510 	 * start communication, which involves sending COMRESET to device.
5511 	 * And the HBA resets PxTFD.STS to 7Fh.
5512 	 *
5513 	 * When a COMINIT is received from the device, then the port enters
5514 	 * P:ComInit state. And HBA sets PxTFD.STS to FFh or 80h. HBA sets
5515 	 * PxSSTS.DET to 1h to indicate a device is detected but communication
5516 	 * is not yet established. HBA sets PxSERR.DIAG.X to '1' to indicate
5517 	 * a COMINIT has been received.
5518 	 */
5519 	/*
5520 	 * The DET field is valid only if IPM field indicates
5521 	 * that the interface is in active state.
5522 	 */
5523 	loop_count = 0;
5524 	do {
5525 		READ_PMULT(addrp, SATA_PMULT_REG_SSTS, &port_sstatus, err);
5526 
5527 		if (SSTATUS_GET_IPM(port_sstatus) != SSTATUS_IPM_ACTIVE) {
5528 			/*
5529 			 * If the interface is not active, the DET field
5530 			 * is considered not accurate. So we want to
5531 			 * continue looping.
5532 			 */
5533 			SSTATUS_SET_DET(port_sstatus, SSTATUS_DET_NODEV);
5534 		}
5535 
5536 		if (loop_count++ > AHCI_POLLRATE_PORT_SSTATUS) {
5537 			/*
5538 			 * We are effectively timing out after 0.1 sec.
5539 			 */
5540 			break;
5541 		}
5542 
5543 		/* Wait for 10 millisec */
5544 		drv_usecwait(AHCI_10MS_USECS);
5545 
5546 	} while (SSTATUS_GET_DET(port_sstatus) != SSTATUS_DET_DEVPRE_PHYCOM);
5547 
5548 	AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
5549 	    "ahci_pmport_reset: 1st loop count: %d, "
5550 	    "port_sstatus = 0x%x port %d:%d",
5551 	    loop_count, port_sstatus, port, pmport);
5552 
5553 	if ((SSTATUS_GET_IPM(port_sstatus) != SSTATUS_IPM_ACTIVE) ||
5554 	    (SSTATUS_GET_DET(port_sstatus) != SSTATUS_DET_DEVPRE_PHYCOM)) {
5555 		/*
5556 		 * Either the port is not active or there
5557 		 * is no device present.
5558 		 */
5559 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_INFO, ahci_ctlp,
5560 		    "ahci_pmport_reset: "
5561 		    "no device attached to port %d:%d",
5562 		    port, pmport);
5563 		AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_NONE);
5564 		return (AHCI_SUCCESS);
5565 	}
5566 
5567 	/* Now we can make sure there is a device connected to the port */
5568 	/* COMINIT signal is supposed to be received (PxSERR.DIAG.X = '1') */
5569 	READ_PMULT(addrp, SATA_PMULT_REG_SERR, &port_serror, err);
5570 
5571 	if (!(port_serror & (1 << 26))) {
5572 		cmn_err(CE_WARN, "!ahci%d: ahci_pmport_reset: "
5573 		    "COMINIT signal from the device not received port %d:%d",
5574 		    instance, port, pmport);
5575 
5576 		AHCIPORT_SET_STATE(ahci_portp, addrp, SATA_PSTATE_FAILED);
5577 		return (AHCI_FAILURE);
5578 	}
5579 
5580 	/*
5581 	 * After clear PxSERR register, we will receive a D2H FIS.
5582 	 * Normally this FIS will cause a IPMS error according to AHCI spec
5583 	 * v1.2 because there is no command outstanding for it. So we need
5584 	 * to ignore this error.
5585 	 */
5586 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_IGNORE_IPMS;
5587 	WRITE_PMULT(addrp, SATA_PMULT_REG_SERR, AHCI_SERROR_CLEAR_ALL, err);
5588 
5589 	/* Now we need to check the D2H FIS by checking IPMS error. */
5590 	loop_count = 0;
5591 	do {
5592 		port_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5593 		    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port));
5594 
5595 		if (loop_count++ > AHCI_POLLRATE_PORT_TFD_ERROR) {
5596 			/*
5597 			 * No D2H FIS received. This is possible according
5598 			 * to SATA 2.6 spec.
5599 			 */
5600 			cmn_err(CE_WARN, "ahci_port_reset: port %d:%d "
5601 			    "PxIS.IPMS is not set, we need another "
5602 			    "software reset.", port, pmport);
5603 
5604 			break;
5605 		}
5606 
5607 		/* Wait for 10 millisec */
5608 		mutex_exit(&ahci_portp->ahciport_mutex);
5609 		delay(AHCI_10MS_TICKS);
5610 		mutex_enter(&ahci_portp->ahciport_mutex);
5611 
5612 	} while (!(port_intr_status & AHCI_INTR_STATUS_IPMS));
5613 
5614 	AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
5615 	    "ahci_pmport_reset: 2st loop count: %d, "
5616 	    "port_sstatus = 0x%x port %d:%d",
5617 	    loop_count, port_sstatus, port, pmport);
5618 
5619 	/* Clear IPMS */
5620 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5621 	    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port),
5622 	    AHCI_INTR_STATUS_IPMS);
5623 	ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_IGNORE_IPMS;
5624 
5625 	/* This pmport is now ready for ahci_tran_start() */
5626 	port_state = AHCIPORT_GET_STATE(ahci_portp, addrp);
5627 	AHCIPORT_SET_STATE(ahci_portp, addrp, port_state|SATA_STATE_READY);
5628 
5629 	AHCIDBG(AHCIDBG_INFO|AHCIDBG_ERRS, ahci_ctlp,
5630 	    "ahci_pmport_reset: succeed at port %d:%d", port, pmport);
5631 	return (AHCI_SUCCESS);
5632 
5633 err:	/* R/W PMULT error */
5634 	/* IPMS flags might be set before. */
5635 	ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_IGNORE_IPMS;
5636 	AHCIDBG(AHCIDBG_INFO|AHCIDBG_ERRS, ahci_ctlp,
5637 	    "ahci_pmport_reset: failed at port %d:%d", port, pmport);
5638 
5639 	return (AHCI_FAILURE);
5640 }
5641 
5642 /*
5643  * AHCI HBA reset ...; the entire HBA is reset, and all ports are disabled.
5644  * This is the most intrusive.
5645  *
5646  * When an HBA reset occurs, Phy communication will be re-established with
5647  * the device through a COMRESET followed by the normal out-of-band
5648  * communication sequence defined in Serial ATA. AT the end of reset, the
5649  * device, if working properly, will send a D2H Register FIS, which contains
5650  * the device signature. When the HBA receives this FIS, it updates PxTFD.STS
5651  * and PxTFD.ERR register fields, and updates the PxSIG register with the
5652  * signature.
5653  *
5654  * Remember to set GHC.AE to 1 before calling ahci_hba_reset.
5655  */
5656 static int
5657 ahci_hba_reset(ahci_ctl_t *ahci_ctlp)
5658 {
5659 	ahci_port_t *ahci_portp;
5660 	ahci_addr_t addr;
5661 	uint32_t ghc_control;
5662 	uint8_t port;
5663 	int loop_count;
5664 	int rval = AHCI_SUCCESS;
5665 
5666 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp, "HBA resetting",
5667 	    NULL);
5668 
5669 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
5670 
5671 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5672 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
5673 
5674 	/* Setting GHC.HR to 1, remember GHC.AE is already set to 1 before */
5675 	ghc_control |= AHCI_HBA_GHC_HR;
5676 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5677 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
5678 
5679 	/*
5680 	 * Wait until HBA Reset complete or timeout
5681 	 */
5682 	loop_count = 0;
5683 	do {
5684 		ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5685 		    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
5686 
5687 		if (loop_count++ > AHCI_POLLRATE_HBA_RESET) {
5688 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
5689 			    "ahci hba reset is timing out, "
5690 			    "ghc_control = 0x%x", ghc_control);
5691 			/* We are effectively timing out after 1 sec. */
5692 			break;
5693 		}
5694 
5695 		/* Wait for 10 millisec */
5696 		drv_usecwait(AHCI_10MS_USECS);
5697 	} while (ghc_control & AHCI_HBA_GHC_HR);
5698 
5699 	AHCIDBG(AHCIDBG_POLL_LOOP, ahci_ctlp,
5700 	    "ahci_hba_reset: 1st loop count: %d, "
5701 	    "ghc_control = 0x%x", loop_count, ghc_control);
5702 
5703 	if (ghc_control & AHCI_HBA_GHC_HR) {
5704 		/* The hba is not reset for some reasons */
5705 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
5706 		    "hba reset failed: HBA in a hung or locked state", NULL);
5707 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
5708 		return (AHCI_FAILURE);
5709 	}
5710 
5711 	/*
5712 	 * HBA reset will clear (AHCI Spec v1.2 10.4.3) GHC.IE / GHC.AE
5713 	 */
5714 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5715 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
5716 	ghc_control |= (AHCI_HBA_GHC_AE | AHCI_HBA_GHC_IE);
5717 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5718 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
5719 
5720 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
5721 
5722 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
5723 		/* Only check implemented ports */
5724 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
5725 			continue;
5726 		}
5727 
5728 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
5729 		mutex_enter(&ahci_portp->ahciport_mutex);
5730 
5731 		AHCI_ADDR_SET_PORT(&addr, port);
5732 
5733 		if (ahci_restart_port_wait_till_ready(ahci_ctlp, ahci_portp,
5734 		    port, AHCI_PORT_RESET|AHCI_RESET_NO_EVENTS_UP, NULL) !=
5735 		    AHCI_SUCCESS) {
5736 			rval = AHCI_FAILURE;
5737 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
5738 			    "ahci_hba_reset: port %d failed", port);
5739 			/*
5740 			 * Set the port state to SATA_PSTATE_FAILED if
5741 			 * failed to initialize it.
5742 			 */
5743 			ahci_portp->ahciport_port_state = SATA_PSTATE_FAILED;
5744 		}
5745 
5746 		mutex_exit(&ahci_portp->ahciport_mutex);
5747 	}
5748 
5749 	return (rval);
5750 }
5751 
5752 /*
5753  * This routine is only called from AHCI_ATTACH or phyrdy change
5754  * case. It first calls software reset, then stop the port and try to
5755  * read PxSIG register to find the type of device attached to the port.
5756  *
5757  * The caller should make sure a valid device exists on specified port and
5758  * physical communication has been established so that the signature could
5759  * be retrieved by software reset.
5760  *
5761  * WARNING!!! ahciport_mutex should be acquired before the function
5762  * is called. And the port interrupt is disabled.
5763  */
5764 static void
5765 ahci_find_dev_signature(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
5766     ahci_addr_t *addrp)
5767 {
5768 	ahci_addr_t dev_addr;
5769 	uint32_t signature;
5770 	uint8_t port = addrp->aa_port;
5771 	uint8_t pmport = addrp->aa_pmport;
5772 	int rval;
5773 
5774 	ASSERT(AHCI_ADDR_IS_VALID(addrp));
5775 
5776 	/*
5777 	 * If the HBA doesn't support port multiplier, then the driver
5778 	 * doesn't need to bother to check port multiplier device.
5779 	 *
5780 	 * The second port of ICH7 on ASUS P5W DH deluxe motherboard is
5781 	 * connected to Silicon Image 4723, to which the two sata drives
5782 	 * attached can be set with RAID1, RAID0 or Spanning mode.
5783 	 *
5784 	 * We found software reset will get failure if port multiplier address
5785 	 * 0xf is used by software reset, so just ignore the check since
5786 	 * ICH7 doesn't support port multiplier device at all.
5787 	 */
5788 	if (AHCI_ADDR_IS_PORT(addrp) &&
5789 	    (ahci_ctlp->ahcictl_cap & AHCI_CAP_PMULT_CBSS)) {
5790 		AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
5791 		    "ahci_find_dev_signature enter: port %d", port);
5792 
5793 		/*
5794 		 * NOTE: when the ahci address is a HBA port, we do not know
5795 		 * it is a device or a port multiplier that attached. we need
5796 		 * try a software reset at port multiplier address (0xf
5797 		 * pmport)
5798 		 */
5799 		AHCI_ADDR_SET_PMULT(&dev_addr, addrp->aa_port);
5800 	} else {
5801 		AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
5802 		    "ahci_find_dev_signature enter: port %d:%d",
5803 		    port, pmport);
5804 		dev_addr = *addrp;
5805 	}
5806 
5807 	/* Assume it is unknown. */
5808 	AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_UNKNOWN);
5809 
5810 	/* Issue a software reset to get the signature */
5811 	rval = ahci_software_reset(ahci_ctlp, ahci_portp, &dev_addr);
5812 	if (rval != AHCI_SUCCESS) {
5813 
5814 		/*
5815 		 * Try to do software reset again with pmport set with 0 if
5816 		 * the controller is set with AHCI_CAP_SRST_NO_HOSTPORT and
5817 		 * the original pmport is set with SATA_PMULT_HOSTPORT (0xf)
5818 		 */
5819 		if ((ahci_ctlp->ahcictl_cap & AHCI_CAP_SRST_NO_HOSTPORT) &&
5820 		    (dev_addr.aa_pmport == SATA_PMULT_HOSTPORT)) {
5821 			dev_addr.aa_pmport = 0;
5822 			rval = ahci_software_reset(ahci_ctlp, ahci_portp,
5823 			    &dev_addr);
5824 		}
5825 
5826 		if (rval != AHCI_SUCCESS) {
5827 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
5828 			    "ahci_find_dev_signature: software reset failed "
5829 			    "at port %d:%d, cannot get signature.",
5830 			    port, pmport);
5831 
5832 			AHCIPORT_SET_STATE(ahci_portp, addrp,
5833 			    SATA_PSTATE_FAILED);
5834 			return;
5835 		}
5836 	}
5837 
5838 	/*
5839 	 * ahci_software_reset has started the port, so we need manually stop
5840 	 * the port again.
5841 	 */
5842 	if (AHCI_ADDR_IS_PORT(addrp)) {
5843 		if (ahci_put_port_into_notrunning_state(ahci_ctlp,
5844 		    ahci_portp, port) != AHCI_SUCCESS) {
5845 			AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
5846 			    "ahci_find_dev_signature: cannot stop port %d.",
5847 			    port);
5848 			ahci_portp->ahciport_port_state = SATA_PSTATE_FAILED;
5849 			return;
5850 		}
5851 	}
5852 
5853 	/* Now we can make sure that a valid signature is received. */
5854 	signature = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5855 	    (uint32_t *)AHCI_PORT_PxSIG(ahci_ctlp, port));
5856 
5857 #ifdef AHCI_DEBUG
5858 	if (AHCI_ADDR_IS_PMPORT(addrp)) {
5859 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
5860 		    "ahci_find_dev_signature: signature = 0x%x at port %d:%d",
5861 		    signature, port, pmport);
5862 	} else {
5863 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_INFO, ahci_ctlp,
5864 		    "ahci_find_dev_signature: signature = 0x%x at port %d",
5865 		    signature, port);
5866 	}
5867 #endif
5868 
5869 	/* NOTE: Only support ATAPI device at controller port. */
5870 	if (signature == AHCI_SIGNATURE_ATAPI && !AHCI_ADDR_IS_PORT(addrp))
5871 		signature = SATA_DTYPE_UNKNOWN;
5872 
5873 	switch (signature) {
5874 
5875 	case AHCI_SIGNATURE_DISK:
5876 		AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_ATADISK);
5877 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
5878 		    "Disk is found at port: %d", port);
5879 		break;
5880 
5881 	case AHCI_SIGNATURE_ATAPI:
5882 		AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_ATAPI);
5883 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
5884 		    "ATAPI device is found at port: %d", port);
5885 		break;
5886 
5887 	case AHCI_SIGNATURE_PORT_MULTIPLIER:
5888 		/* Port Multiplier cannot recursively attached. */
5889 		ASSERT(AHCI_ADDR_IS_PORT(addrp));
5890 		AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_PMULT);
5891 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
5892 		    "Port Multiplier is found at port: %d", port);
5893 		break;
5894 
5895 	default:
5896 		AHCIPORT_SET_DEV_TYPE(ahci_portp, addrp, SATA_DTYPE_UNKNOWN);
5897 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
5898 		    "Unknown device is found at port: %d", port);
5899 	}
5900 }
5901 
5902 /*
5903  * According to the spec, to reliably detect hot plug removals, software
5904  * must disable interface power management. Software should perform the
5905  * following initialization on a port after a device is attached:
5906  *   Set PxSCTL.IPM to 3h to disable interface state transitions
5907  *   Set PxCMD.ALPE to '0' to disable aggressive power management
5908  *   Disable device initiated interface power management by SET FEATURE
5909  *
5910  * We can ignore the last item because by default the feature is disabled
5911  */
5912 static void
5913 ahci_disable_interface_pm(ahci_ctl_t *ahci_ctlp, uint8_t port)
5914 {
5915 	uint32_t port_scontrol, port_cmd_status;
5916 
5917 	port_scontrol = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5918 	    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port));
5919 	SCONTROL_SET_IPM(port_scontrol, SCONTROL_IPM_DISABLE_BOTH);
5920 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5921 	    (uint32_t *)AHCI_PORT_PxSCTL(ahci_ctlp, port), port_scontrol);
5922 
5923 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5924 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
5925 	port_cmd_status &= ~AHCI_CMD_STATUS_ALPE;
5926 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5927 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port), port_cmd_status);
5928 }
5929 
5930 /*
5931  * Start the port - set PxCMD.ST to 1, if PxCMD.FRE is not set
5932  * to 1, then set it firstly.
5933  *
5934  * Each port contains two major DMA engines. One DMA engine walks through
5935  * the command list, and is controlled by PxCMD.ST. The second DMA engine
5936  * copies received FISes into system memory, and is controlled by PxCMD.FRE.
5937  *
5938  * Software shall not set PxCMD.ST to '1' until it verifies that PxCMD.CR
5939  * is '0' and has set PxCMD.FRE is '1'. And software shall not clear
5940  * PxCMD.FRE while PxCMD.ST or PxCMD.CR is set '1'.
5941  *
5942  * Software shall not set PxCMD.ST to '1' unless a functional device is
5943  * present on the port(as determined by PxTFD.STS.BSY = '0',
5944  * PxTFD.STS.DRQ = '0', and PxSSTS.DET = 3h).
5945  *
5946  * WARNING!!! ahciport_mutex should be acquired before the function
5947  * is called.
5948  */
5949 static int
5950 ahci_start_port(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp, uint8_t port)
5951 {
5952 	uint32_t port_cmd_status;
5953 
5954 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_start_port: %d enter", port);
5955 
5956 	if (ahci_portp->ahciport_port_state & SATA_PSTATE_FAILED) {
5957 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_start_port failed "
5958 		    "the state for port %d is 0x%x",
5959 		    port, ahci_portp->ahciport_port_state);
5960 		return (AHCI_FAILURE);
5961 	}
5962 
5963 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
5964 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_start_port failed "
5965 		    "no device is attached at port %d", port);
5966 		return (AHCI_FAILURE);
5967 	}
5968 
5969 	/* First to set PxCMD.FRE before setting PxCMD.ST. */
5970 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5971 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
5972 
5973 	if (!(port_cmd_status & AHCI_CMD_STATUS_FRE)) {
5974 		port_cmd_status |= AHCI_CMD_STATUS_FRE;
5975 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5976 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
5977 		    port_cmd_status);
5978 	}
5979 
5980 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
5981 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
5982 
5983 	port_cmd_status |= AHCI_CMD_STATUS_ST;
5984 
5985 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
5986 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port),
5987 	    port_cmd_status);
5988 
5989 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_STARTED;
5990 
5991 	AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_start_port: "
5992 	    "PxCMD.ST set to '1' at port %d", port);
5993 
5994 	return (AHCI_SUCCESS);
5995 }
5996 
5997 /*
5998  * Allocate the ahci_port_t including Received FIS and Command List.
5999  * The argument - port is the physical port number, and not logical
6000  * port number seen by the SATA framework.
6001  *
6002  * WARNING!!! ahcictl_mutex should be acquired before the function
6003  * is called.
6004  */
6005 static int
6006 ahci_alloc_port_state(ahci_ctl_t *ahci_ctlp, uint8_t port)
6007 {
6008 	dev_info_t *dip = ahci_ctlp->ahcictl_dip;
6009 	ahci_port_t *ahci_portp;
6010 	char taskq_name[64] = "event_handle_taskq";
6011 
6012 	ahci_portp =
6013 	    (ahci_port_t *)kmem_zalloc(sizeof (ahci_port_t), KM_SLEEP);
6014 
6015 	ahci_ctlp->ahcictl_ports[port] = ahci_portp;
6016 	ahci_portp->ahciport_port_num = port;
6017 
6018 	/* Initialize the port condition variable */
6019 	cv_init(&ahci_portp->ahciport_cv, NULL, CV_DRIVER, NULL);
6020 
6021 	/* Initialize the port mutex */
6022 	mutex_init(&ahci_portp->ahciport_mutex, NULL, MUTEX_DRIVER,
6023 	    (void *)(uintptr_t)ahci_ctlp->ahcictl_intr_pri);
6024 
6025 	mutex_enter(&ahci_portp->ahciport_mutex);
6026 
6027 	/*
6028 	 * Allocate memory for received FIS structure and
6029 	 * command list for this port
6030 	 */
6031 	if (ahci_alloc_rcvd_fis(ahci_ctlp, ahci_portp, port) != AHCI_SUCCESS) {
6032 		goto err_case1;
6033 	}
6034 
6035 	if (ahci_alloc_cmd_list(ahci_ctlp, ahci_portp, port) != AHCI_SUCCESS) {
6036 		goto err_case2;
6037 	}
6038 
6039 	(void) snprintf(taskq_name + strlen(taskq_name),
6040 	    sizeof (taskq_name) - strlen(taskq_name),
6041 	    "_port%d", port);
6042 
6043 	/* Create the taskq for the port */
6044 	if ((ahci_portp->ahciport_event_taskq = ddi_taskq_create(dip,
6045 	    taskq_name, 2, TASKQ_DEFAULTPRI, 0)) == NULL) {
6046 		cmn_err(CE_WARN, "!ahci%d: ddi_taskq_create failed for event "
6047 		    "handle", ddi_get_instance(ahci_ctlp->ahcictl_dip));
6048 		goto err_case3;
6049 	}
6050 
6051 	/* Allocate the argument for the taskq */
6052 	ahci_portp->ahciport_event_args =
6053 	    kmem_zalloc(sizeof (ahci_event_arg_t), KM_SLEEP);
6054 
6055 	ahci_portp->ahciport_event_args->ahciea_addrp =
6056 	    kmem_zalloc(sizeof (ahci_addr_t), KM_SLEEP);
6057 
6058 	if (ahci_portp->ahciport_event_args == NULL)
6059 		goto err_case4;
6060 
6061 	/* Initialize the done queue */
6062 	ahci_portp->ahciport_doneq = NULL;
6063 	ahci_portp->ahciport_doneqtail = &ahci_portp->ahciport_doneq;
6064 	ahci_portp->ahciport_doneq_len = 0;
6065 
6066 	mutex_exit(&ahci_portp->ahciport_mutex);
6067 
6068 	return (AHCI_SUCCESS);
6069 
6070 err_case4:
6071 	ddi_taskq_destroy(ahci_portp->ahciport_event_taskq);
6072 
6073 err_case3:
6074 	ahci_dealloc_cmd_list(ahci_ctlp, ahci_portp);
6075 
6076 err_case2:
6077 	ahci_dealloc_rcvd_fis(ahci_portp);
6078 
6079 err_case1:
6080 	mutex_exit(&ahci_portp->ahciport_mutex);
6081 	mutex_destroy(&ahci_portp->ahciport_mutex);
6082 	cv_destroy(&ahci_portp->ahciport_cv);
6083 
6084 	kmem_free(ahci_portp, sizeof (ahci_port_t));
6085 
6086 	return (AHCI_FAILURE);
6087 }
6088 
6089 /*
6090  * Reverse of ahci_dealloc_port_state().
6091  *
6092  * WARNING!!! ahcictl_mutex should be acquired before the function
6093  * is called.
6094  */
6095 static void
6096 ahci_dealloc_port_state(ahci_ctl_t *ahci_ctlp, uint8_t port)
6097 {
6098 	ahci_port_t *ahci_portp = ahci_ctlp->ahcictl_ports[port];
6099 
6100 	ASSERT(ahci_portp != NULL);
6101 
6102 	mutex_enter(&ahci_portp->ahciport_mutex);
6103 	kmem_free(ahci_portp->ahciport_event_args->ahciea_addrp,
6104 	    sizeof (ahci_addr_t));
6105 	ahci_portp->ahciport_event_args->ahciea_addrp = NULL;
6106 	kmem_free(ahci_portp->ahciport_event_args, sizeof (ahci_event_arg_t));
6107 	ahci_portp->ahciport_event_args = NULL;
6108 	ddi_taskq_destroy(ahci_portp->ahciport_event_taskq);
6109 	ahci_dealloc_cmd_list(ahci_ctlp, ahci_portp);
6110 	ahci_dealloc_rcvd_fis(ahci_portp);
6111 	ahci_dealloc_pmult(ahci_ctlp, ahci_portp);
6112 	mutex_exit(&ahci_portp->ahciport_mutex);
6113 
6114 	mutex_destroy(&ahci_portp->ahciport_mutex);
6115 	cv_destroy(&ahci_portp->ahciport_cv);
6116 
6117 	kmem_free(ahci_portp, sizeof (ahci_port_t));
6118 
6119 	ahci_ctlp->ahcictl_ports[port] = NULL;
6120 }
6121 
6122 /*
6123  * Allocates memory for the Received FIS Structure
6124  *
6125  * WARNING!!! ahciport_mutex should be acquired before the function
6126  * is called.
6127  */
6128 static int
6129 ahci_alloc_rcvd_fis(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
6130     uint8_t port)
6131 {
6132 	size_t rcvd_fis_size;
6133 	size_t ret_len;
6134 	uint_t cookie_count;
6135 
6136 	rcvd_fis_size = sizeof (ahci_rcvd_fis_t);
6137 
6138 	/* allocate rcvd FIS dma handle. */
6139 	if (ddi_dma_alloc_handle(ahci_ctlp->ahcictl_dip,
6140 	    &ahci_ctlp->ahcictl_rcvd_fis_dma_attr,
6141 	    DDI_DMA_SLEEP,
6142 	    NULL,
6143 	    &ahci_portp->ahciport_rcvd_fis_dma_handle) !=
6144 	    DDI_SUCCESS) {
6145 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
6146 		    "rcvd FIS dma handle alloc failed", NULL);
6147 
6148 		return (AHCI_FAILURE);
6149 	}
6150 
6151 	if (ddi_dma_mem_alloc(ahci_portp->ahciport_rcvd_fis_dma_handle,
6152 	    rcvd_fis_size,
6153 	    &accattr,
6154 	    DDI_DMA_CONSISTENT,
6155 	    DDI_DMA_SLEEP,
6156 	    NULL,
6157 	    (caddr_t *)&ahci_portp->ahciport_rcvd_fis,
6158 	    &ret_len,
6159 	    &ahci_portp->ahciport_rcvd_fis_acc_handle) != NULL) {
6160 
6161 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
6162 		    "rcvd FIS dma mem alloc fail", NULL);
6163 		/* error.. free the dma handle. */
6164 		ddi_dma_free_handle(&ahci_portp->ahciport_rcvd_fis_dma_handle);
6165 		return (AHCI_FAILURE);
6166 	}
6167 
6168 	if (ddi_dma_addr_bind_handle(ahci_portp->ahciport_rcvd_fis_dma_handle,
6169 	    NULL,
6170 	    (caddr_t)ahci_portp->ahciport_rcvd_fis,
6171 	    rcvd_fis_size,
6172 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
6173 	    DDI_DMA_SLEEP,
6174 	    NULL,
6175 	    &ahci_portp->ahciport_rcvd_fis_dma_cookie,
6176 	    &cookie_count) !=  DDI_DMA_MAPPED) {
6177 
6178 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
6179 		    "rcvd FIS dma handle bind fail", NULL);
6180 		/*  error.. free the dma handle & free the memory. */
6181 		ddi_dma_mem_free(&ahci_portp->ahciport_rcvd_fis_acc_handle);
6182 		ddi_dma_free_handle(&ahci_portp->ahciport_rcvd_fis_dma_handle);
6183 		return (AHCI_FAILURE);
6184 	}
6185 
6186 	bzero((void *)ahci_portp->ahciport_rcvd_fis, rcvd_fis_size);
6187 
6188 	/* Config Port Received FIS Base Address */
6189 	ddi_put64(ahci_ctlp->ahcictl_ahci_acc_handle,
6190 	    (uint64_t *)AHCI_PORT_PxFB(ahci_ctlp, port),
6191 	    ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_laddress);
6192 
6193 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "64-bit, dma address: 0x%llx",
6194 	    ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_laddress);
6195 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "32-bit, dma address: 0x%x",
6196 	    ahci_portp->ahciport_rcvd_fis_dma_cookie.dmac_address);
6197 
6198 	return (AHCI_SUCCESS);
6199 }
6200 
6201 /*
6202  * Deallocates the Received FIS Structure
6203  *
6204  * WARNING!!! ahciport_mutex should be acquired before the function
6205  * is called.
6206  */
6207 static void
6208 ahci_dealloc_rcvd_fis(ahci_port_t *ahci_portp)
6209 {
6210 	/* Unbind the cmd list dma handle first. */
6211 	(void) ddi_dma_unbind_handle(ahci_portp->ahciport_rcvd_fis_dma_handle);
6212 
6213 	/* Then free the underlying memory. */
6214 	ddi_dma_mem_free(&ahci_portp->ahciport_rcvd_fis_acc_handle);
6215 
6216 	/* Now free the handle itself. */
6217 	ddi_dma_free_handle(&ahci_portp->ahciport_rcvd_fis_dma_handle);
6218 }
6219 
6220 /*
6221  * Allocates memory for the Command List, which contains up to 32 entries.
6222  * Each entry contains a command header, which is a 32-byte structure that
6223  * includes the pointer to the command table.
6224  *
6225  * WARNING!!! ahciport_mutex should be acquired before the function
6226  * is called.
6227  */
6228 static int
6229 ahci_alloc_cmd_list(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
6230     uint8_t port)
6231 {
6232 	size_t cmd_list_size;
6233 	size_t ret_len;
6234 	uint_t cookie_count;
6235 
6236 	cmd_list_size =
6237 	    ahci_ctlp->ahcictl_num_cmd_slots * sizeof (ahci_cmd_header_t);
6238 
6239 	/* allocate cmd list dma handle. */
6240 	if (ddi_dma_alloc_handle(ahci_ctlp->ahcictl_dip,
6241 	    &ahci_ctlp->ahcictl_cmd_list_dma_attr,
6242 	    DDI_DMA_SLEEP,
6243 	    NULL,
6244 	    &ahci_portp->ahciport_cmd_list_dma_handle) != DDI_SUCCESS) {
6245 
6246 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
6247 		    "cmd list dma handle alloc failed", NULL);
6248 		return (AHCI_FAILURE);
6249 	}
6250 
6251 	if (ddi_dma_mem_alloc(ahci_portp->ahciport_cmd_list_dma_handle,
6252 	    cmd_list_size,
6253 	    &accattr,
6254 	    DDI_DMA_CONSISTENT,
6255 	    DDI_DMA_SLEEP,
6256 	    NULL,
6257 	    (caddr_t *)&ahci_portp->ahciport_cmd_list,
6258 	    &ret_len,
6259 	    &ahci_portp->ahciport_cmd_list_acc_handle) != NULL) {
6260 
6261 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
6262 		    "cmd list dma mem alloc fail", NULL);
6263 		/* error.. free the dma handle. */
6264 		ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
6265 		return (AHCI_FAILURE);
6266 	}
6267 
6268 	if (ddi_dma_addr_bind_handle(ahci_portp->ahciport_cmd_list_dma_handle,
6269 	    NULL,
6270 	    (caddr_t)ahci_portp->ahciport_cmd_list,
6271 	    cmd_list_size,
6272 	    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
6273 	    DDI_DMA_SLEEP,
6274 	    NULL,
6275 	    &ahci_portp->ahciport_cmd_list_dma_cookie,
6276 	    &cookie_count) !=  DDI_DMA_MAPPED) {
6277 
6278 		AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
6279 		    "cmd list dma handle bind fail", NULL);
6280 		/*  error.. free the dma handle & free the memory. */
6281 		ddi_dma_mem_free(&ahci_portp->ahciport_cmd_list_acc_handle);
6282 		ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
6283 		return (AHCI_FAILURE);
6284 	}
6285 
6286 	bzero((void *)ahci_portp->ahciport_cmd_list, cmd_list_size);
6287 
6288 	/* Config Port Command List Base Address */
6289 	ddi_put64(ahci_ctlp->ahcictl_ahci_acc_handle,
6290 	    (uint64_t *)AHCI_PORT_PxCLB(ahci_ctlp, port),
6291 	    ahci_portp->ahciport_cmd_list_dma_cookie.dmac_laddress);
6292 
6293 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "64-bit, dma address: 0x%llx",
6294 	    ahci_portp->ahciport_cmd_list_dma_cookie.dmac_laddress);
6295 
6296 	AHCIDBG(AHCIDBG_INIT, ahci_ctlp, "32-bit, dma address: 0x%x",
6297 	    ahci_portp->ahciport_cmd_list_dma_cookie.dmac_address);
6298 
6299 	if (ahci_alloc_cmd_tables(ahci_ctlp, ahci_portp) != AHCI_SUCCESS) {
6300 		goto err_out;
6301 	}
6302 
6303 	return (AHCI_SUCCESS);
6304 
6305 err_out:
6306 	/* Unbind the cmd list dma handle first. */
6307 	(void) ddi_dma_unbind_handle(ahci_portp->ahciport_cmd_list_dma_handle);
6308 
6309 	/* Then free the underlying memory. */
6310 	ddi_dma_mem_free(&ahci_portp->ahciport_cmd_list_acc_handle);
6311 
6312 	/* Now free the handle itself. */
6313 	ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
6314 
6315 	return (AHCI_FAILURE);
6316 }
6317 
6318 /*
6319  * Deallocates the Command List
6320  *
6321  * WARNING!!! ahciport_mutex should be acquired before the function
6322  * is called.
6323  */
6324 static void
6325 ahci_dealloc_cmd_list(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
6326 {
6327 	/* First dealloc command table */
6328 	ahci_dealloc_cmd_tables(ahci_ctlp, ahci_portp);
6329 
6330 	/* Unbind the cmd list dma handle first. */
6331 	(void) ddi_dma_unbind_handle(ahci_portp->ahciport_cmd_list_dma_handle);
6332 
6333 	/* Then free the underlying memory. */
6334 	ddi_dma_mem_free(&ahci_portp->ahciport_cmd_list_acc_handle);
6335 
6336 	/* Now free the handle itself. */
6337 	ddi_dma_free_handle(&ahci_portp->ahciport_cmd_list_dma_handle);
6338 }
6339 
6340 /*
6341  * Allocates memory for all Command Tables, which contains Command FIS,
6342  * ATAPI Command and Physical Region Descriptor Table.
6343  *
6344  * WARNING!!! ahciport_mutex should be acquired before the function
6345  * is called.
6346  */
6347 static int
6348 ahci_alloc_cmd_tables(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
6349 {
6350 	size_t ret_len;
6351 	ddi_dma_cookie_t cmd_table_dma_cookie;
6352 	uint_t cookie_count;
6353 	int slot;
6354 
6355 	AHCIDBG(AHCIDBG_INIT|AHCIDBG_ENTRY, ahci_ctlp,
6356 	    "ahci_alloc_cmd_tables: port %d enter",
6357 	    ahci_portp->ahciport_port_num);
6358 
6359 	for (slot = 0; slot < ahci_ctlp->ahcictl_num_cmd_slots; slot++) {
6360 		/* Allocate cmd table dma handle. */
6361 		if (ddi_dma_alloc_handle(ahci_ctlp->ahcictl_dip,
6362 		    &ahci_ctlp->ahcictl_cmd_table_dma_attr,
6363 		    DDI_DMA_SLEEP,
6364 		    NULL,
6365 		    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]) !=
6366 		    DDI_SUCCESS) {
6367 
6368 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
6369 			    "cmd table dma handle alloc failed", NULL);
6370 
6371 			goto err_out;
6372 		}
6373 
6374 		if (ddi_dma_mem_alloc(
6375 		    ahci_portp->ahciport_cmd_tables_dma_handle[slot],
6376 		    ahci_cmd_table_size,
6377 		    &accattr,
6378 		    DDI_DMA_CONSISTENT,
6379 		    DDI_DMA_SLEEP,
6380 		    NULL,
6381 		    (caddr_t *)&ahci_portp->ahciport_cmd_tables[slot],
6382 		    &ret_len,
6383 		    &ahci_portp->ahciport_cmd_tables_acc_handle[slot]) !=
6384 		    NULL) {
6385 
6386 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
6387 			    "cmd table dma mem alloc fail", NULL);
6388 
6389 			/* error.. free the dma handle. */
6390 			ddi_dma_free_handle(
6391 			    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
6392 			goto err_out;
6393 		}
6394 
6395 		if (ddi_dma_addr_bind_handle(
6396 		    ahci_portp->ahciport_cmd_tables_dma_handle[slot],
6397 		    NULL,
6398 		    (caddr_t)ahci_portp->ahciport_cmd_tables[slot],
6399 		    ahci_cmd_table_size,
6400 		    DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
6401 		    DDI_DMA_SLEEP,
6402 		    NULL,
6403 		    &cmd_table_dma_cookie,
6404 		    &cookie_count) !=  DDI_DMA_MAPPED) {
6405 
6406 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
6407 			    "cmd table dma handle bind fail", NULL);
6408 			/*  error.. free the dma handle & free the memory. */
6409 			ddi_dma_mem_free(
6410 			    &ahci_portp->ahciport_cmd_tables_acc_handle[slot]);
6411 			ddi_dma_free_handle(
6412 			    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
6413 			goto err_out;
6414 		}
6415 
6416 		bzero((void *)ahci_portp->ahciport_cmd_tables[slot],
6417 		    ahci_cmd_table_size);
6418 
6419 		/* Config Port Command Table Base Address */
6420 		SET_COMMAND_TABLE_BASE_ADDR(
6421 		    (&ahci_portp->ahciport_cmd_list[slot]),
6422 		    cmd_table_dma_cookie.dmac_laddress & 0xffffffffull);
6423 
6424 #ifndef __lock_lint
6425 		SET_COMMAND_TABLE_BASE_ADDR_UPPER(
6426 		    (&ahci_portp->ahciport_cmd_list[slot]),
6427 		    cmd_table_dma_cookie.dmac_laddress >> 32);
6428 #endif
6429 	}
6430 
6431 	return (AHCI_SUCCESS);
6432 err_out:
6433 
6434 	for (slot--; slot >= 0; slot--) {
6435 		/* Unbind the cmd table dma handle first */
6436 		(void) ddi_dma_unbind_handle(
6437 		    ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
6438 
6439 		/* Then free the underlying memory */
6440 		ddi_dma_mem_free(
6441 		    &ahci_portp->ahciport_cmd_tables_acc_handle[slot]);
6442 
6443 		/* Now free the handle itself */
6444 		ddi_dma_free_handle(
6445 		    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
6446 	}
6447 
6448 	return (AHCI_FAILURE);
6449 }
6450 
6451 /*
6452  * Deallocates memory for all Command Tables.
6453  *
6454  * WARNING!!! ahciport_mutex should be acquired before the function
6455  * is called.
6456  */
6457 static void
6458 ahci_dealloc_cmd_tables(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp)
6459 {
6460 	int slot;
6461 
6462 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
6463 	    "ahci_dealloc_cmd_tables: %d enter",
6464 	    ahci_portp->ahciport_port_num);
6465 
6466 	for (slot = 0; slot < ahci_ctlp->ahcictl_num_cmd_slots; slot++) {
6467 		/* Unbind the cmd table dma handle first. */
6468 		(void) ddi_dma_unbind_handle(
6469 		    ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
6470 
6471 		/* Then free the underlying memory. */
6472 		ddi_dma_mem_free(
6473 		    &ahci_portp->ahciport_cmd_tables_acc_handle[slot]);
6474 
6475 		/* Now free the handle itself. */
6476 		ddi_dma_free_handle(
6477 		    &ahci_portp->ahciport_cmd_tables_dma_handle[slot]);
6478 	}
6479 }
6480 
6481 /*
6482  * Update SATA registers at controller ports
6483  *
6484  * WARNING!!! ahciport_mutex should be acquired before those functions
6485  * get called.
6486  */
6487 static void
6488 ahci_update_sata_registers(ahci_ctl_t *ahci_ctlp, uint8_t port,
6489     sata_device_t *sd)
6490 {
6491 	sd->satadev_scr.sstatus =
6492 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
6493 	    (uint32_t *)(AHCI_PORT_PxSSTS(ahci_ctlp, port)));
6494 	sd->satadev_scr.serror =
6495 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
6496 	    (uint32_t *)(AHCI_PORT_PxSERR(ahci_ctlp, port)));
6497 	sd->satadev_scr.scontrol =
6498 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
6499 	    (uint32_t *)(AHCI_PORT_PxSCTL(ahci_ctlp, port)));
6500 	sd->satadev_scr.sactive =
6501 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
6502 	    (uint32_t *)(AHCI_PORT_PxSACT(ahci_ctlp, port)));
6503 }
6504 
6505 /*
6506  * For poll mode, ahci_port_intr will be called to emulate the interrupt
6507  */
6508 static void
6509 ahci_port_intr(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp, uint8_t port)
6510 {
6511 	uint32_t port_intr_status;
6512 	uint32_t port_intr_enable;
6513 
6514 	AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
6515 	    "ahci_port_intr enter: port %d", port);
6516 
6517 	mutex_enter(&ahci_portp->ahciport_mutex);
6518 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_POLLING) {
6519 		/* For SATA_OPMODE_POLLING commands */
6520 		port_intr_enable =
6521 		    (AHCI_INTR_STATUS_DHRS |
6522 		    AHCI_INTR_STATUS_PSS |
6523 		    AHCI_INTR_STATUS_SDBS |
6524 		    AHCI_INTR_STATUS_UFS |
6525 		    AHCI_INTR_STATUS_PCS |
6526 		    AHCI_INTR_STATUS_PRCS |
6527 		    AHCI_INTR_STATUS_OFS |
6528 		    AHCI_INTR_STATUS_INFS |
6529 		    AHCI_INTR_STATUS_IFS |
6530 		    AHCI_INTR_STATUS_HBDS |
6531 		    AHCI_INTR_STATUS_HBFS |
6532 		    AHCI_INTR_STATUS_TFES);
6533 	} else {
6534 		/*
6535 		 * port_intr_enable indicates that the corresponding interrrupt
6536 		 * reporting is enabled.
6537 		 */
6538 		port_intr_enable = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
6539 		    (uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port));
6540 	}
6541 
6542 	/* IPMS error in port reset should be ignored according AHCI spec. */
6543 	if (!(ahci_portp->ahciport_flags & AHCI_PORT_FLAG_IGNORE_IPMS))
6544 		port_intr_enable |= AHCI_INTR_STATUS_IPMS;
6545 	mutex_exit(&ahci_portp->ahciport_mutex);
6546 
6547 	/*
6548 	 * port_intr_stats indicates that the corresponding interrupt
6549 	 * condition is active.
6550 	 */
6551 	port_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
6552 	    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port));
6553 
6554 	AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
6555 	    "ahci_port_intr: port %d, port_intr_status = 0x%x, "
6556 	    "port_intr_enable = 0x%x",
6557 	    port, port_intr_status, port_intr_enable);
6558 
6559 	port_intr_status &= port_intr_enable;
6560 
6561 	/*
6562 	 * Pending interrupt events are indicated by the PxIS register.
6563 	 * Make sure we don't miss any event.
6564 	 */
6565 	if (ahci_check_ctl_handle(ahci_ctlp) != DDI_SUCCESS) {
6566 		ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
6567 		    DDI_SERVICE_UNAFFECTED);
6568 		ddi_fm_acc_err_clear(ahci_ctlp->ahcictl_ahci_acc_handle,
6569 		    DDI_FME_VERSION);
6570 		return;
6571 	}
6572 
6573 	/* First clear the port interrupts status */
6574 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
6575 	    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port),
6576 	    port_intr_status);
6577 
6578 	/* Check the completed non-queued commands */
6579 	if (port_intr_status & (AHCI_INTR_STATUS_DHRS |
6580 	    AHCI_INTR_STATUS_PSS)) {
6581 		(void) ahci_intr_cmd_cmplt(ahci_ctlp,
6582 		    ahci_portp, port);
6583 	}
6584 
6585 	/* Check the completed queued commands */
6586 	if (port_intr_status & AHCI_INTR_STATUS_SDBS) {
6587 		(void) ahci_intr_set_device_bits(ahci_ctlp,
6588 		    ahci_portp, port);
6589 	}
6590 
6591 	/* Check the port connect change status interrupt bit */
6592 	if (port_intr_status & AHCI_INTR_STATUS_PCS) {
6593 		(void) ahci_intr_port_connect_change(ahci_ctlp,
6594 		    ahci_portp, port);
6595 	}
6596 
6597 	/* Check the device mechanical presence status interrupt bit */
6598 	if (port_intr_status & AHCI_INTR_STATUS_DMPS) {
6599 		(void) ahci_intr_device_mechanical_presence_status(
6600 		    ahci_ctlp, ahci_portp, port);
6601 	}
6602 
6603 	/* Check the PhyRdy change status interrupt bit */
6604 	if (port_intr_status & AHCI_INTR_STATUS_PRCS) {
6605 		(void) ahci_intr_phyrdy_change(ahci_ctlp, ahci_portp,
6606 		    port);
6607 	}
6608 
6609 	/*
6610 	 * Check the non-fatal error interrupt bits, there are four
6611 	 * kinds of non-fatal errors at the time being:
6612 	 *
6613 	 *    PxIS.UFS - Unknown FIS Error
6614 	 *    PxIS.OFS - Overflow Error
6615 	 *    PxIS.INFS - Interface Non-Fatal Error
6616 	 *    PxIS.IPMS - Incorrect Port Multiplier Status Error
6617 	 *
6618 	 * For these non-fatal errors, the HBA can continue to operate,
6619 	 * so the driver just log the error messages.
6620 	 */
6621 	if (port_intr_status & (AHCI_INTR_STATUS_UFS |
6622 	    AHCI_INTR_STATUS_OFS |
6623 	    AHCI_INTR_STATUS_IPMS |
6624 	    AHCI_INTR_STATUS_INFS)) {
6625 		(void) ahci_intr_non_fatal_error(ahci_ctlp, ahci_portp,
6626 		    port, port_intr_status);
6627 	}
6628 
6629 	/*
6630 	 * Check the fatal error interrupt bits, there are four kinds
6631 	 * of fatal errors for AHCI controllers:
6632 	 *
6633 	 *    PxIS.HBFS - Host Bus Fatal Error
6634 	 *    PxIS.HBDS - Host Bus Data Error
6635 	 *    PxIS.IFS - Interface Fatal Error
6636 	 *    PxIS.TFES - Task File Error
6637 	 *
6638 	 * The fatal error means the HBA can not recover from it by
6639 	 * itself, and it will try to abort the transfer, and the software
6640 	 * must intervene to restart the port.
6641 	 */
6642 	if (port_intr_status & (AHCI_INTR_STATUS_IFS |
6643 	    AHCI_INTR_STATUS_HBDS |
6644 	    AHCI_INTR_STATUS_HBFS |
6645 	    AHCI_INTR_STATUS_TFES))
6646 		(void) ahci_intr_fatal_error(ahci_ctlp, ahci_portp,
6647 		    port, port_intr_status);
6648 
6649 	/* Check the cold port detect interrupt bit */
6650 	if (port_intr_status & AHCI_INTR_STATUS_CPDS) {
6651 		(void) ahci_intr_cold_port_detect(ahci_ctlp, ahci_portp, port);
6652 	}
6653 
6654 	/* Second clear the corresponding bit in IS.IPS */
6655 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
6656 	    (uint32_t *)AHCI_GLOBAL_IS(ahci_ctlp), (0x1 << port));
6657 
6658 	/* Try to recover at the end of the interrupt handler. */
6659 	if (ahci_check_acc_handle(ahci_ctlp->ahcictl_ahci_acc_handle) !=
6660 	    DDI_FM_OK) {
6661 		ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
6662 		    DDI_SERVICE_UNAFFECTED);
6663 		ddi_fm_acc_err_clear(ahci_ctlp->ahcictl_ahci_acc_handle,
6664 		    DDI_FME_VERSION);
6665 	}
6666 }
6667 
6668 /*
6669  * Interrupt service handler
6670  */
6671 static uint_t
6672 ahci_intr(caddr_t arg1, caddr_t arg2)
6673 {
6674 #ifndef __lock_lint
6675 	_NOTE(ARGUNUSED(arg2))
6676 #endif
6677 	/* LINTED */
6678 	ahci_ctl_t *ahci_ctlp = (ahci_ctl_t *)arg1;
6679 	ahci_port_t *ahci_portp;
6680 	int32_t global_intr_status;
6681 	uint8_t port;
6682 
6683 	/*
6684 	 * global_intr_status indicates that the corresponding port has
6685 	 * an interrupt pending.
6686 	 */
6687 	global_intr_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
6688 	    (uint32_t *)AHCI_GLOBAL_IS(ahci_ctlp));
6689 
6690 	if (!(global_intr_status & ahci_ctlp->ahcictl_ports_implemented)) {
6691 		/* The interrupt is not ours */
6692 		return (DDI_INTR_UNCLAIMED);
6693 	}
6694 
6695 	/*
6696 	 * Check the handle after reading global_intr_status - we don't want
6697 	 * to miss any port with pending interrupts.
6698 	 */
6699 	if (ahci_check_acc_handle(ahci_ctlp->ahcictl_ahci_acc_handle) !=
6700 	    DDI_FM_OK) {
6701 		ddi_fm_service_impact(ahci_ctlp->ahcictl_dip,
6702 		    DDI_SERVICE_UNAFFECTED);
6703 		ddi_fm_acc_err_clear(ahci_ctlp->ahcictl_ahci_acc_handle,
6704 		    DDI_FME_VERSION);
6705 		return (DDI_INTR_UNCLAIMED);
6706 	}
6707 
6708 	/* Loop for all the ports */
6709 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
6710 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
6711 			continue;
6712 		}
6713 		if (!((0x1 << port) & global_intr_status)) {
6714 			continue;
6715 		}
6716 
6717 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
6718 
6719 		/* Call ahci_port_intr */
6720 		ahci_port_intr(ahci_ctlp, ahci_portp, port);
6721 	}
6722 
6723 	return (DDI_INTR_CLAIMED);
6724 }
6725 
6726 /*
6727  * For non-queued commands, when the corresponding bit in the PxCI register
6728  * is cleared, it means the command is completed successfully. And according
6729  * to the HBA state machine, there are three conditions which possibly will
6730  * try to clear the PxCI register bit.
6731  *	1. Receive one D2H Register FIS which is with 'I' bit set
6732  *	2. Update PIO Setup FIS
6733  *	3. Transmit a command and receive R_OK if CTBA.C is set (software reset)
6734  *
6735  * Process completed non-queued commands when the interrupt status bit -
6736  * AHCI_INTR_STATUS_DHRS or AHCI_INTR_STATUS_PSS is set.
6737  *
6738  * AHCI_INTR_STATUS_DHRS means a D2H Register FIS has been received
6739  * with the 'I' bit set. And the following commands will send thus
6740  * FIS with 'I' bit set upon the successful completion:
6741  * 	1. Non-data commands
6742  * 	2. DMA data-in command
6743  * 	3. DMA data-out command
6744  * 	4. PIO data-out command
6745  *	5. PACKET non-data commands
6746  *	6. PACKET PIO data-in command
6747  *	7. PACKET PIO data-out command
6748  *	8. PACKET DMA data-in command
6749  *	9. PACKET DMA data-out command
6750  *
6751  * AHCI_INTR_STATUS_PSS means a PIO Setup FIS has been received
6752  * with the 'I' bit set. And the following commands will send this
6753  * FIS upon the successful completion:
6754  * 	1. PIO data-in command
6755  */
6756 static int
6757 ahci_intr_cmd_cmplt(ahci_ctl_t *ahci_ctlp,
6758     ahci_port_t *ahci_portp, uint8_t port)
6759 {
6760 	uint32_t port_cmd_issue = 0;
6761 	uint32_t finished_tags;
6762 	int finished_slot;
6763 	sata_pkt_t *satapkt;
6764 	ahci_fis_d2h_register_t *rcvd_fisp;
6765 #if AHCI_DEBUG
6766 	ahci_cmd_header_t *cmd_header;
6767 	uint32_t cmd_dmacount;
6768 #endif
6769 
6770 	mutex_enter(&ahci_portp->ahciport_mutex);
6771 
6772 	if (!ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
6773 	    !RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp) &&
6774 	    !NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
6775 		/*
6776 		 * Spurious interrupt. Nothing to be done.
6777 		 */
6778 		mutex_exit(&ahci_portp->ahciport_mutex);
6779 		return (AHCI_SUCCESS);
6780 	}
6781 
6782 	port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
6783 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
6784 
6785 	/* If the PxCI corrupts, don't complete the commmands. */
6786 	if (ahci_check_acc_handle(ahci_ctlp->ahcictl_ahci_acc_handle)
6787 	    != DDI_FM_OK) {
6788 		mutex_exit(&ahci_portp->ahciport_mutex);
6789 		return (AHCI_FAILURE);
6790 	}
6791 
6792 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
6793 		/* Slot 0 is always used during error recovery */
6794 		finished_tags = 0x1 & ~port_cmd_issue;
6795 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
6796 		    "ahci_intr_cmd_cmplt: port %d the sata pkt for error "
6797 		    "retrieval is finished, and finished_tags = 0x%x",
6798 		    port, finished_tags);
6799 	} else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
6800 		finished_tags = 0x1 & ~port_cmd_issue;
6801 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp,
6802 		    "ahci_intr_cmd_cmplt: port %d the sata pkt for r/w "
6803 		    "port multiplier is finished, and finished_tags = 0x%x",
6804 		    port, finished_tags);
6805 
6806 	} else {
6807 
6808 		finished_tags = ahci_portp->ahciport_pending_tags &
6809 		    ~port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp);
6810 	}
6811 
6812 	AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
6813 	    "ahci_intr_cmd_cmplt: pending_tags = 0x%x, "
6814 	    "port_cmd_issue = 0x%x finished_tags = 0x%x",
6815 	    ahci_portp->ahciport_pending_tags, port_cmd_issue,
6816 	    finished_tags);
6817 
6818 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) &&
6819 	    (finished_tags == 0x1)) {
6820 		satapkt = ahci_portp->ahciport_err_retri_pkt;
6821 		ASSERT(satapkt != NULL);
6822 
6823 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
6824 		    "ahci_intr_cmd_cmplt: sending up pkt 0x%p "
6825 		    "with SATA_PKT_COMPLETED", (void *)satapkt);
6826 
6827 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
6828 		goto out;
6829 	}
6830 
6831 	if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp) &&
6832 	    (finished_tags == 0x1)) {
6833 		satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
6834 		ASSERT(satapkt != NULL);
6835 
6836 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
6837 		    "ahci_intr_cmd_cmplt: sending up pkt 0x%p "
6838 		    "with SATA_PKT_COMPLETED", (void *)satapkt);
6839 
6840 		/* READ PORTMULT need copy out FIS content. */
6841 		if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs) {
6842 			rcvd_fisp = &(ahci_portp->ahciport_rcvd_fis->
6843 			    ahcirf_d2h_register_fis);
6844 			satapkt->satapkt_cmd.satacmd_status_reg =
6845 			    GET_RFIS_STATUS(rcvd_fisp);
6846 			ahci_copy_out_regs(&satapkt->satapkt_cmd, rcvd_fisp);
6847 		}
6848 
6849 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
6850 		goto out;
6851 	}
6852 
6853 	while (finished_tags) {
6854 		finished_slot = ddi_ffs(finished_tags) - 1;
6855 		if (finished_slot == -1) {
6856 			goto out;
6857 		}
6858 
6859 		satapkt = ahci_portp->ahciport_slot_pkts[finished_slot];
6860 		ASSERT(satapkt != NULL);
6861 #if AHCI_DEBUG
6862 		/*
6863 		 * For non-native queued commands, the PRD byte count field
6864 		 * shall contain an accurate count of the number of bytes
6865 		 * transferred for the command before the PxCI bit is cleared
6866 		 * to '0' for the command.
6867 		 *
6868 		 * The purpose of this field is to let software know how many
6869 		 * bytes transferred for a given operation in order to
6870 		 * determine if underflow occurred. When issuing native command
6871 		 * queuing commands, this field should not be used and is not
6872 		 * required to be valid since in this case underflow is always
6873 		 * illegal.
6874 		 *
6875 		 * For data reads, the HBA will update its PRD byte count with
6876 		 * the total number of bytes received from the last FIS, and
6877 		 * may be able to continue normally. For data writes, the
6878 		 * device will detect an error, and HBA most likely will get
6879 		 * a fatal error.
6880 		 *
6881 		 * Therefore, here just put code to debug part. And please
6882 		 * refer to the comment above ahci_intr_fatal_error for the
6883 		 * definition of underflow error.
6884 		 */
6885 		cmd_dmacount =
6886 		    ahci_portp->ahciport_prd_bytecounts[finished_slot];
6887 		if (cmd_dmacount) {
6888 			cmd_header =
6889 			    &ahci_portp->ahciport_cmd_list[finished_slot];
6890 			AHCIDBG(AHCIDBG_INTR|AHCIDBG_PRDT, ahci_ctlp,
6891 			    "ahci_intr_cmd_cmplt: port %d, "
6892 			    "PRD Byte Count = 0x%x, "
6893 			    "ahciport_prd_bytecounts = 0x%x", port,
6894 			    cmd_header->ahcich_prd_byte_count,
6895 			    cmd_dmacount);
6896 
6897 			if (cmd_header->ahcich_prd_byte_count != cmd_dmacount) {
6898 				AHCIDBG(AHCIDBG_UNDERFLOW, ahci_ctlp,
6899 				    "ahci_intr_cmd_cmplt: port %d, "
6900 				    "an underflow occurred", port);
6901 			}
6902 		}
6903 #endif
6904 
6905 		/*
6906 		 * For SATAC_SMART command with SATA_SMART_RETURN_STATUS
6907 		 * feature, sata_special_regs flag will be set, and the
6908 		 * driver should copy the status and the other corresponding
6909 		 * register values in the D2H Register FIS received (It's
6910 		 * working on Non-data protocol) from the device back to
6911 		 * the sata_cmd.
6912 		 *
6913 		 * For every AHCI port, there is only one Received FIS
6914 		 * structure, which contains the FISes received from the
6915 		 * device, So we're trying to copy the content of D2H
6916 		 * Register FIS in the Received FIS structure back to
6917 		 * the sata_cmd.
6918 		 */
6919 		if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs) {
6920 			rcvd_fisp = &(ahci_portp->ahciport_rcvd_fis->
6921 			    ahcirf_d2h_register_fis);
6922 			satapkt->satapkt_cmd.satacmd_status_reg =
6923 			    GET_RFIS_STATUS(rcvd_fisp);
6924 			ahci_copy_out_regs(&satapkt->satapkt_cmd, rcvd_fisp);
6925 		}
6926 
6927 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
6928 		    "ahci_intr_cmd_cmplt: sending up pkt 0x%p "
6929 		    "with SATA_PKT_COMPLETED", (void *)satapkt);
6930 
6931 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, finished_slot);
6932 		CLEAR_BIT(finished_tags, finished_slot);
6933 		ahci_portp->ahciport_slot_pkts[finished_slot] = NULL;
6934 
6935 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
6936 	}
6937 out:
6938 	AHCIDBG(AHCIDBG_PKTCOMP, ahci_ctlp,
6939 	    "ahci_intr_cmd_cmplt: pending_tags = 0x%x",
6940 	    ahci_portp->ahciport_pending_tags);
6941 
6942 	ahci_flush_doneq(ahci_portp);
6943 
6944 	mutex_exit(&ahci_portp->ahciport_mutex);
6945 
6946 	return (AHCI_SUCCESS);
6947 }
6948 
6949 /*
6950  * AHCI_INTR_STATUS_SDBS means a Set Device Bits FIS has been received
6951  * with the 'I' bit set and has been copied into system memory. It will
6952  * be sent under the following situations:
6953  *
6954  * 1. NCQ command is completed
6955  *
6956  * The completion of NCQ commands (READ/WRITE FPDMA QUEUED) is performed
6957  * via the Set Device Bits FIS. When such event is generated, the software
6958  * needs to read PxSACT register and compares the current value to the
6959  * list of commands previously issue by software. ahciport_pending_ncq_tags
6960  * keeps the tags of previously issued commands.
6961  *
6962  * 2. Asynchronous Notification
6963  *
6964  * Asynchronous Notification is a feature in SATA spec 2.6.
6965  *
6966  * 1) ATAPI device will send a signal to the host when media is inserted or
6967  * removed and avoids polling the device for media changes. The signal
6968  * sent to the host is a Set Device Bits FIS with the 'I' and 'N' bits
6969  * set to '1'. At the moment, it's not supported yet.
6970  *
6971  * 2) Port multiplier will send a signal to the host when a hot plug event
6972  * has occured on a port multiplier port. It is used when command based
6973  * switching is employed. This is handled by ahci_intr_pmult_sntf_events()
6974  */
6975 static int
6976 ahci_intr_set_device_bits(ahci_ctl_t *ahci_ctlp,
6977     ahci_port_t *ahci_portp, uint8_t port)
6978 {
6979 	ahci_addr_t addr;
6980 
6981 	AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
6982 	    "ahci_intr_set_device_bits enter: port %d", port);
6983 
6984 	/* Initialize HBA port address */
6985 	AHCI_ADDR_SET_PORT(&addr, port);
6986 
6987 	/* NCQ plug handler */
6988 	(void) ahci_intr_ncq_events(ahci_ctlp, ahci_portp, &addr);
6989 
6990 	/* Check port multiplier's asynchronous notification events */
6991 	if (ahci_ctlp->ahcictl_cap & AHCI_CAP_SNTF) {
6992 		(void) ahci_intr_pmult_sntf_events(ahci_ctlp,
6993 		    ahci_portp, port);
6994 	}
6995 
6996 	/* ATAPI events is not supported yet */
6997 
6998 	return (AHCI_SUCCESS);
6999 }
7000 /*
7001  * NCQ interrupt handler. Called upon a NCQ command is completed.
7002  * Only be called from ahci_intr_set_device_bits().
7003  *
7004  * WARNING!!! ahciport_mutex should be acquired before the function is
7005  * called.
7006  */
7007 static int
7008 ahci_intr_ncq_events(ahci_ctl_t *ahci_ctlp,
7009     ahci_port_t *ahci_portp, ahci_addr_t *addrp)
7010 {
7011 	uint32_t port_sactive;
7012 	uint32_t port_cmd_issue;
7013 	uint32_t issued_tags;
7014 	int issued_slot;
7015 	uint32_t finished_tags;
7016 	int finished_slot;
7017 	uint8_t port = addrp->aa_port;
7018 	sata_pkt_t *satapkt;
7019 
7020 	AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
7021 	    "ahci_intr_set_device_bits enter: port %d", port);
7022 
7023 	mutex_enter(&ahci_portp->ahciport_mutex);
7024 	if (!NCQ_CMD_IN_PROGRESS(ahci_portp)) {
7025 		mutex_exit(&ahci_portp->ahciport_mutex);
7026 		return (AHCI_SUCCESS);
7027 	}
7028 
7029 	/*
7030 	 * First the handler got which commands are finished by checking
7031 	 * PxSACT register
7032 	 */
7033 	port_sactive = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7034 	    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
7035 
7036 	finished_tags = ahci_portp->ahciport_pending_ncq_tags &
7037 	    ~port_sactive & AHCI_NCQ_SLOT_MASK(ahci_portp);
7038 
7039 	AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
7040 	    "ahci_intr_set_device_bits: port %d pending_ncq_tags = 0x%x "
7041 	    "port_sactive = 0x%x", port,
7042 	    ahci_portp->ahciport_pending_ncq_tags, port_sactive);
7043 
7044 	AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
7045 	    "ahci_intr_set_device_bits: finished_tags = 0x%x", finished_tags);
7046 
7047 	/*
7048 	 * For NCQ commands, the software can determine which command has
7049 	 * already been transmitted to the device by checking PxCI register.
7050 	 */
7051 	port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7052 	    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
7053 
7054 	issued_tags = ahci_portp->ahciport_pending_tags &
7055 	    ~port_cmd_issue & AHCI_SLOT_MASK(ahci_ctlp);
7056 
7057 	/* If the PxSACT/PxCI corrupts, don't complete the NCQ commmands. */
7058 	if (ahci_check_acc_handle(ahci_ctlp->ahcictl_ahci_acc_handle)
7059 	    != DDI_FM_OK) {
7060 		mutex_exit(&ahci_portp->ahciport_mutex);
7061 		return (AHCI_FAILURE);
7062 	}
7063 
7064 	AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
7065 	    "ahci_intr_set_device_bits: port %d pending_tags = 0x%x "
7066 	    "port_cmd_issue = 0x%x", port,
7067 	    ahci_portp->ahciport_pending_tags, port_cmd_issue);
7068 
7069 	AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
7070 	    "ahci_intr_set_device_bits: issued_tags = 0x%x", issued_tags);
7071 
7072 	/*
7073 	 * Clear ahciport_pending_tags bit when the corresponding command
7074 	 * is already sent down to the device.
7075 	 */
7076 	while (issued_tags) {
7077 		issued_slot = ddi_ffs(issued_tags) - 1;
7078 		if (issued_slot == -1) {
7079 			goto next;
7080 		}
7081 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, issued_slot);
7082 		CLEAR_BIT(issued_tags, issued_slot);
7083 	}
7084 
7085 next:
7086 	while (finished_tags) {
7087 		finished_slot = ddi_ffs(finished_tags) - 1;
7088 		if (finished_slot == -1) {
7089 			goto out;
7090 		}
7091 
7092 		/* The command is certainly transmitted to the device */
7093 		ASSERT(!(ahci_portp->ahciport_pending_tags &
7094 		    (0x1 << finished_slot)));
7095 
7096 		satapkt = ahci_portp->ahciport_slot_pkts[finished_slot];
7097 		ASSERT(satapkt != NULL);
7098 
7099 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ, ahci_ctlp,
7100 		    "ahci_intr_set_device_bits: sending up pkt 0x%p "
7101 		    "with SATA_PKT_COMPLETED", (void *)satapkt);
7102 
7103 		CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags, finished_slot);
7104 		CLEAR_BIT(finished_tags, finished_slot);
7105 		ahci_portp->ahciport_slot_pkts[finished_slot] = NULL;
7106 
7107 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
7108 	}
7109 out:
7110 	AHCIDBG(AHCIDBG_PKTCOMP|AHCIDBG_NCQ, ahci_ctlp,
7111 	    "ahci_intr_set_device_bits: port %d "
7112 	    "pending_ncq_tags = 0x%x pending_tags = 0x%x",
7113 	    port, ahci_portp->ahciport_pending_ncq_tags,
7114 	    ahci_portp->ahciport_pending_tags);
7115 
7116 	ahci_flush_doneq(ahci_portp);
7117 
7118 	mutex_exit(&ahci_portp->ahciport_mutex);
7119 
7120 	return (AHCI_SUCCESS);
7121 }
7122 
7123 /*
7124  * Port multiplier asynchronous notification event handler. Called upon a
7125  * device is hot plugged/pulled.
7126  *
7127  * The async-notification event will only be recorded by ahcipmi_snotif_tags
7128  * here and will be handled by ahci_probe_pmult().
7129  *
7130  * NOTE: called only from ahci_port_intr().
7131  */
7132 static int
7133 ahci_intr_pmult_sntf_events(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
7134     uint8_t port)
7135 {
7136 	sata_device_t sdevice;
7137 
7138 	AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
7139 	    "ahci_intr_pmult_sntf_events enter: port %d ", port);
7140 
7141 	/* no hot-plug while attaching process */
7142 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
7143 	if (ahci_ctlp->ahcictl_flags & AHCI_ATTACH) {
7144 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
7145 		return (AHCI_SUCCESS);
7146 	}
7147 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
7148 
7149 	mutex_enter(&ahci_portp->ahciport_mutex);
7150 	if (ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) {
7151 		mutex_exit(&ahci_portp->ahciport_mutex);
7152 		return (AHCI_SUCCESS);
7153 	}
7154 
7155 	ASSERT(ahci_portp->ahciport_pmult_info != NULL);
7156 
7157 	ahci_portp->ahciport_pmult_info->ahcipmi_snotif_tags =
7158 	    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7159 	    (uint32_t *)AHCI_PORT_PxSNTF(ahci_ctlp, port));
7160 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
7161 	    (uint32_t *)AHCI_PORT_PxSNTF(ahci_ctlp, port),
7162 	    AHCI_SNOTIF_CLEAR_ALL);
7163 
7164 	if (ahci_portp->ahciport_pmult_info->ahcipmi_snotif_tags == 0) {
7165 		mutex_exit(&ahci_portp->ahciport_mutex);
7166 		return (AHCI_SUCCESS);
7167 	}
7168 
7169 	/* Port Multiplier sub-device hot-plug handler */
7170 	if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
7171 		mutex_exit(&ahci_portp->ahciport_mutex);
7172 		return (AHCI_SUCCESS);
7173 	}
7174 
7175 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_PMULT_SNTF) {
7176 		/* Not allowed to re-enter. */
7177 		mutex_exit(&ahci_portp->ahciport_mutex);
7178 		return (AHCI_SUCCESS);
7179 	}
7180 
7181 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_PMULT_SNTF;
7182 
7183 	/*
7184 	 * NOTE:
7185 	 * Even if Asynchronous Notification is supported (and enabled) by
7186 	 * both controller and the port multiplier, the content of PxSNTF
7187 	 * register is always set to 0x8000 by async notification event. We
7188 	 * need to check GSCR[32] on the port multiplier to find out the
7189 	 * owner of this event.
7190 	 * This is not accord with SATA spec 2.6 and needs further
7191 	 * clarification.
7192 	 */
7193 	/* hot-plug will not reported while reseting. */
7194 	if (ahci_portp->ahciport_reset_in_progress == 1) {
7195 		AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
7196 		    "port %d snotif event ignored", port);
7197 		ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_PMULT_SNTF;
7198 		mutex_exit(&ahci_portp->ahciport_mutex);
7199 		return (AHCI_SUCCESS);
7200 	}
7201 
7202 	AHCIDBG(AHCIDBG_INFO|AHCIDBG_PMULT, ahci_ctlp,
7203 	    "PxSNTF is set to 0x%x by port multiplier",
7204 	    ahci_portp->ahciport_pmult_info->ahcipmi_snotif_tags);
7205 
7206 	/*
7207 	 * Now we need do some necessary operation and inform SATA framework
7208 	 * that link/device events has happened.
7209 	 */
7210 	bzero((void *)&sdevice, sizeof (sata_device_t));
7211 	sdevice.satadev_addr.cport = ahci_ctlp->
7212 	    ahcictl_port_to_cport[port];
7213 	sdevice.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
7214 	sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
7215 	sdevice.satadev_state = SATA_PSTATE_PWRON;
7216 
7217 	/* Just reject packets, do not stop that port. */
7218 	ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
7219 
7220 	mutex_exit(&ahci_portp->ahciport_mutex);
7221 	sata_hba_event_notify(
7222 	    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
7223 	    &sdevice,
7224 	    SATA_EVNT_PMULT_LINK_CHANGED);
7225 	mutex_enter(&ahci_portp->ahciport_mutex);
7226 
7227 	ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_PMULT_SNTF;
7228 	mutex_exit(&ahci_portp->ahciport_mutex);
7229 
7230 	return (AHCI_SUCCESS);
7231 }
7232 
7233 /*
7234  * 1=Change in Current Connect Status. 0=No change in Current Connect Status.
7235  * This bit reflects the state of PxSERR.DIAG.X. This bit is only cleared
7236  * when PxSERR.DIAG.X is cleared. When PxSERR.DIAG.X is set to one, it
7237  * indicates a COMINIT signal was received.
7238  *
7239  * Hot plug insertion is detected by reception of a COMINIT signal from the
7240  * device. On reception of unsolicited COMINIT, the HBA shall generate a
7241  * COMRESET. If the COMINIT is in responce to a COMRESET, then the HBA shall
7242  * begin the normal communication negotiation sequence as outlined in the
7243  * Serial ATA 1.0a specification. When a COMRESET is sent to the device the
7244  * PxSSTS.DET field shall be cleared to 0h. When a COMINIT is received, the
7245  * PxSSTS.DET field shall be set to 1h. When the communication negotiation
7246  * sequence is complete and PhyRdy is true the PxSSTS.DET field	shall be set
7247  * to 3h. Therefore, at the moment the ahci driver is going to check PhyRdy
7248  * to handle hot plug insertion. In this interrupt handler, just do nothing
7249  * but print some log message and clear the bit.
7250  */
7251 static int
7252 ahci_intr_port_connect_change(ahci_ctl_t *ahci_ctlp,
7253     ahci_port_t *ahci_portp, uint8_t port)
7254 {
7255 #if AHCI_DEBUG
7256 	uint32_t port_serror;
7257 #endif
7258 
7259 	mutex_enter(&ahci_portp->ahciport_mutex);
7260 
7261 #if AHCI_DEBUG
7262 	port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7263 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
7264 
7265 	AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
7266 	    "ahci_intr_port_connect_change: port %d, "
7267 	    "port_serror = 0x%x", port, port_serror);
7268 #endif
7269 
7270 	/* Clear PxSERR.DIAG.X to clear the interrupt bit */
7271 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
7272 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
7273 	    SERROR_EXCHANGED_ERR);
7274 
7275 	mutex_exit(&ahci_portp->ahciport_mutex);
7276 
7277 	return (AHCI_SUCCESS);
7278 }
7279 
7280 /*
7281  * Hot Plug Operation for platforms that support Mechanical Presence
7282  * Switches.
7283  *
7284  * When set, it indicates that a mechanical presence switch attached to this
7285  * port has been opened or closed, which may lead to a change in the connection
7286  * state of the device. This bit is only valid if both CAP.SMPS and PxCMD.MPSP
7287  * are set to '1'.
7288  *
7289  * At the moment, this interrupt is not needed and disabled and we just log
7290  * the debug message.
7291  */
7292 static int
7293 ahci_intr_device_mechanical_presence_status(ahci_ctl_t *ahci_ctlp,
7294     ahci_port_t *ahci_portp, uint8_t port)
7295 {
7296 	uint32_t cap_status, port_cmd_status;
7297 
7298 	AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
7299 	    "ahci_intr_device_mechanical_presence_status enter, "
7300 	    "port %d", port);
7301 
7302 	cap_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7303 	    (uint32_t *)AHCI_GLOBAL_CAP(ahci_ctlp));
7304 
7305 	mutex_enter(&ahci_portp->ahciport_mutex);
7306 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7307 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
7308 
7309 	if (!(cap_status & AHCI_HBA_CAP_SMPS) ||
7310 	    !(port_cmd_status & AHCI_CMD_STATUS_MPSP)) {
7311 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
7312 		    "CAP.SMPS or PxCMD.MPSP is not set, so just ignore "
7313 		    "the interrupt: cap_status = 0x%x, "
7314 		    "port_cmd_status = 0x%x", cap_status, port_cmd_status);
7315 		mutex_exit(&ahci_portp->ahciport_mutex);
7316 
7317 		return (AHCI_SUCCESS);
7318 	}
7319 
7320 #if AHCI_DEBUG
7321 	if (port_cmd_status & AHCI_CMD_STATUS_MPSS) {
7322 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
7323 		    "The mechanical presence switch is open: "
7324 		    "port %d, port_cmd_status = 0x%x",
7325 		    port, port_cmd_status);
7326 	} else {
7327 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
7328 		    "The mechanical presence switch is close: "
7329 		    "port %d, port_cmd_status = 0x%x",
7330 		    port, port_cmd_status);
7331 	}
7332 #endif
7333 
7334 	mutex_exit(&ahci_portp->ahciport_mutex);
7335 
7336 	return (AHCI_SUCCESS);
7337 }
7338 
7339 /*
7340  * Native Hot Plug Support.
7341  *
7342  * When set, it indicates that the internal PHYRDY signal changed state.
7343  * This bit reflects the state of PxSERR.DIAG.N.
7344  *
7345  * There are three kinds of conditions to generate this interrupt event:
7346  * 1. a device is inserted
7347  * 2. a device is disconnected
7348  * 3. when the link enters/exits a Partial or Slumber interface power
7349  *    management state
7350  *
7351  * If inteface power management is enabled for a port, the PxSERR.DIAG.N
7352  * bit may be set due to the link entering the Partial or Slumber power
7353  * management state, rather than due to a hot plug insertion or removal
7354  * event. So far, the interface power management is disabled, so the
7355  * driver can reliably get removal detection notification via the
7356  * PxSERR.DIAG.N bit.
7357  */
7358 static int
7359 ahci_intr_phyrdy_change(ahci_ctl_t *ahci_ctlp,
7360     ahci_port_t *ahci_portp, uint8_t port)
7361 {
7362 	uint32_t port_sstatus = 0; /* No dev present & PHY not established. */
7363 	sata_device_t sdevice;
7364 	int dev_exists_now = 0;
7365 	int dev_existed_previously = 0;
7366 	ahci_addr_t port_addr;
7367 
7368 	AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY, ahci_ctlp,
7369 	    "ahci_intr_phyrdy_change enter, port %d", port);
7370 
7371 	/* Clear PxSERR.DIAG.N to clear the interrupt bit */
7372 	mutex_enter(&ahci_portp->ahciport_mutex);
7373 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
7374 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
7375 	    SERROR_PHY_RDY_CHG);
7376 	mutex_exit(&ahci_portp->ahciport_mutex);
7377 
7378 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
7379 	if ((ahci_ctlp->ahcictl_sata_hba_tran == NULL) ||
7380 	    (ahci_portp == NULL)) {
7381 		/* The whole controller setup is not yet done. */
7382 		mutex_exit(&ahci_ctlp->ahcictl_mutex);
7383 		return (AHCI_SUCCESS);
7384 	}
7385 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
7386 
7387 	mutex_enter(&ahci_portp->ahciport_mutex);
7388 
7389 	/* SStatus tells the presence of device. */
7390 	port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7391 	    (uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
7392 
7393 	if (SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_PHYCOM) {
7394 		dev_exists_now = 1;
7395 	}
7396 
7397 	if (ahci_portp->ahciport_device_type != SATA_DTYPE_NONE) {
7398 		dev_existed_previously = 1;
7399 	}
7400 
7401 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_NODEV) {
7402 		ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_NODEV;
7403 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
7404 		    "ahci_intr_phyrdy_change: port %d "
7405 		    "AHCI_PORT_FLAG_NODEV is cleared", port);
7406 		if (dev_exists_now == 0)
7407 			dev_existed_previously = 1;
7408 	}
7409 
7410 	bzero((void *)&sdevice, sizeof (sata_device_t));
7411 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
7412 	sdevice.satadev_addr.qual = SATA_ADDR_CPORT;
7413 	sdevice.satadev_addr.pmport = 0;
7414 	sdevice.satadev_state = SATA_PSTATE_PWRON;
7415 	ahci_portp->ahciport_port_state = SATA_PSTATE_PWRON;
7416 
7417 	AHCI_ADDR_SET_PORT(&port_addr, port);
7418 
7419 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_HOTPLUG;
7420 	if (dev_exists_now) {
7421 		if (dev_existed_previously) { /* 1 -> 1 */
7422 			/* Things are fine now. The loss was temporary. */
7423 			AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
7424 			    "ahci_intr_phyrdy_change  port %d "
7425 			    "device link lost/established", port);
7426 
7427 			mutex_exit(&ahci_portp->ahciport_mutex);
7428 			sata_hba_event_notify(
7429 			    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
7430 			    &sdevice,
7431 			    SATA_EVNT_LINK_LOST|SATA_EVNT_LINK_ESTABLISHED);
7432 			mutex_enter(&ahci_portp->ahciport_mutex);
7433 
7434 		} else { /* 0 -> 1 */
7435 			AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
7436 			    "ahci_intr_phyrdy_change: port %d "
7437 			    "device link established", port);
7438 
7439 			/*
7440 			 * A new device has been detected. The new device
7441 			 * might be a port multiplier instead of a drive, so
7442 			 * we cannot update the signature directly.
7443 			 */
7444 			(void) ahci_initialize_port(ahci_ctlp,
7445 			    ahci_portp, &port_addr);
7446 
7447 			/* Try to start the port */
7448 			if (ahci_start_port(ahci_ctlp, ahci_portp, port)
7449 			    != AHCI_SUCCESS) {
7450 				sdevice.satadev_state |= SATA_PSTATE_FAILED;
7451 				AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
7452 				    "ahci_intr_phyrdy_change: port %d failed "
7453 				    "at start port", port);
7454 			}
7455 
7456 			/* Clear the max queue depth for inserted device */
7457 			ahci_portp->ahciport_max_ncq_tags = 0;
7458 
7459 			mutex_exit(&ahci_portp->ahciport_mutex);
7460 			sata_hba_event_notify(
7461 			    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
7462 			    &sdevice,
7463 			    SATA_EVNT_LINK_ESTABLISHED);
7464 			mutex_enter(&ahci_portp->ahciport_mutex);
7465 
7466 		}
7467 	} else { /* No device exists now */
7468 
7469 		if (dev_existed_previously) { /* 1 -> 0 */
7470 			AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
7471 			    "ahci_intr_phyrdy_change: port %d "
7472 			    "device link lost", port);
7473 
7474 			ahci_reject_all_abort_pkts(ahci_ctlp, ahci_portp, port);
7475 			(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
7476 			    ahci_portp, port);
7477 
7478 			if (ahci_portp->ahciport_device_type ==
7479 			    SATA_DTYPE_PMULT) {
7480 				ahci_dealloc_pmult(ahci_ctlp, ahci_portp);
7481 			}
7482 
7483 			/* An existing device is lost. */
7484 			ahci_portp->ahciport_device_type = SATA_DTYPE_NONE;
7485 			ahci_portp->ahciport_port_state = SATA_STATE_UNKNOWN;
7486 
7487 			mutex_exit(&ahci_portp->ahciport_mutex);
7488 			sata_hba_event_notify(
7489 			    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
7490 			    &sdevice,
7491 			    SATA_EVNT_LINK_LOST);
7492 			mutex_enter(&ahci_portp->ahciport_mutex);
7493 		}
7494 	}
7495 	ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_HOTPLUG;
7496 
7497 	mutex_exit(&ahci_portp->ahciport_mutex);
7498 
7499 	return (AHCI_SUCCESS);
7500 }
7501 
7502 /*
7503  * PxIS.UFS - Unknown FIS Error
7504  *
7505  * This interrupt event means an unknown FIS was received and has been
7506  * copied into system memory. An unknown FIS is not considered an illegal
7507  * FIS, unless the length received is more than 64 bytes. If an unknown
7508  * FIS arrives with length <= 64 bytes, it is posted and the HBA continues
7509  * normal operation. If the unknown FIS is more than 64 bytes, then it
7510  * won't be posted to memory and PxSERR.ERR.P will be set, which is then
7511  * a fatal error.
7512  *
7513  * PxIS.IPMS - Incorrect Port Multiplier Status
7514  *
7515  * IPMS Indicates that the HBA received a FIS from a device that did not
7516  * have a command outstanding. The IPMS bit may be set during enumeration
7517  * of devices on a Port Multiplier due to the normal Port Multiplier
7518  * enumeration process. It is recommended that IPMS only be used after
7519  * enumeration is complete on the Port Multiplier (copied from spec).
7520  *
7521  * PxIS.OFS - Overflow Error
7522  *
7523  * Command list overflow is defined as software building a command table
7524  * that has fewer total bytes than the transaction given to the device.
7525  * On device writes, the HBA will run out of data, and on reads, there
7526  * will be no room to put the data.
7527  *
7528  * For an overflow on data read, either PIO or DMA, the HBA will set
7529  * PxIS.OFS, and the HBA will do a best effort to continue, and it's a
7530  * non-fatal error when the HBA can continues. Sometimes, it will cause
7531  * a fatal error and need the software to do something.
7532  *
7533  * For an overflow on data write, setting PxIS.OFS is optional for both
7534  * DMA and PIO, and it's a fatal error, and a COMRESET is required by
7535  * software to clean up from this serious error.
7536  *
7537  * PxIS.INFS - Interface Non-Fatal Error
7538  *
7539  * This interrupt event indicates that the HBA encountered an error on
7540  * the Serial ATA interface but was able to continue operation. The kind
7541  * of error usually occurred during a non-Data FIS, and under this condition
7542  * the FIS will be re-transmitted by HBA automatically.
7543  *
7544  * When the FMA is implemented, there should be a stat structure to
7545  * record how many every kind of error happens.
7546  */
7547 static int
7548 ahci_intr_non_fatal_error(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
7549     uint8_t port, uint32_t intr_status)
7550 {
7551 	uint32_t port_serror;
7552 #if AHCI_DEBUG
7553 	uint32_t port_cmd_status;
7554 	uint32_t port_cmd_issue;
7555 	uint32_t port_sactive;
7556 	int current_slot;
7557 	uint32_t current_tags;
7558 	sata_pkt_t *satapkt;
7559 	ahci_cmd_header_t *cmd_header;
7560 	uint32_t cmd_dmacount;
7561 #endif
7562 
7563 	mutex_enter(&ahci_portp->ahciport_mutex);
7564 
7565 	port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7566 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
7567 
7568 	AHCIDBG(AHCIDBG_INTR|AHCIDBG_ENTRY|AHCIDBG_ERRS, ahci_ctlp,
7569 	    "ahci_intr_non_fatal_error: port %d, "
7570 	    "PxSERR = 0x%x, PxIS = 0x%x ", port, port_serror, intr_status);
7571 
7572 	ahci_log_serror_message(ahci_ctlp, port, port_serror, 1);
7573 
7574 	if (intr_status & AHCI_INTR_STATUS_UFS) {
7575 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
7576 		    "ahci port %d has unknown FIS error", port);
7577 
7578 		/* Clear the interrupt bit by clearing PxSERR.DIAG.F */
7579 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
7580 		    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
7581 		    SERROR_FIS_TYPE);
7582 	}
7583 
7584 #if AHCI_DEBUG
7585 	if (intr_status & AHCI_INTR_STATUS_IPMS) {
7586 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci port %d "
7587 		    "has Incorrect Port Multiplier Status error", port);
7588 	}
7589 
7590 	if (intr_status & AHCI_INTR_STATUS_OFS) {
7591 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
7592 		    "ahci port %d has overflow error", port);
7593 	}
7594 
7595 	if (intr_status & AHCI_INTR_STATUS_INFS) {
7596 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
7597 		    "ahci port %d has interface non fatal error", port);
7598 	}
7599 
7600 	/*
7601 	 * Record the error occurred command's slot.
7602 	 */
7603 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
7604 	    ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
7605 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7606 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
7607 
7608 		current_slot = (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
7609 		    AHCI_CMD_STATUS_CCS_SHIFT;
7610 
7611 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
7612 			satapkt = ahci_portp->ahciport_err_retri_pkt;
7613 			ASSERT(satapkt != NULL);
7614 			ASSERT(current_slot == 0);
7615 		} else {
7616 			satapkt = ahci_portp->ahciport_slot_pkts[current_slot];
7617 		}
7618 
7619 		if (satapkt != NULL) {
7620 			AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
7621 			    "ahci_intr_non_fatal_error: pending_tags = 0x%x "
7622 			    "cmd 0x%x", ahci_portp->ahciport_pending_tags,
7623 			    satapkt->satapkt_cmd.satacmd_cmd_reg);
7624 
7625 			AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
7626 			    "ahci_intr_non_fatal_error: port %d, "
7627 			    "satapkt 0x%p is being processed when error occurs",
7628 			    port, (void *)satapkt);
7629 
7630 			/*
7631 			 * PRD Byte Count field of command header is not
7632 			 * required to reflect the total number of bytes
7633 			 * transferred when an overflow occurs, so here
7634 			 * just log the value.
7635 			 */
7636 			cmd_dmacount =
7637 			    ahci_portp->ahciport_prd_bytecounts[current_slot];
7638 			if (cmd_dmacount) {
7639 				cmd_header = &ahci_portp->
7640 				    ahciport_cmd_list[current_slot];
7641 				AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
7642 				    "ahci_intr_non_fatal_error: port %d, "
7643 				    "PRD Byte Count = 0x%x, "
7644 				    "ahciport_prd_bytecounts = 0x%x", port,
7645 				    cmd_header->ahcich_prd_byte_count,
7646 				    cmd_dmacount);
7647 			}
7648 		}
7649 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
7650 		/*
7651 		 * For queued command, list those command which have already
7652 		 * been transmitted to the device and still not completed.
7653 		 */
7654 		port_sactive = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7655 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
7656 
7657 		port_cmd_issue = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7658 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
7659 
7660 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ|AHCIDBG_ERRS, ahci_ctlp,
7661 		    "ahci_intr_non_fatal_error: pending_ncq_tags = 0x%x "
7662 		    "port_sactive = 0x%x port_cmd_issue = 0x%x",
7663 		    ahci_portp->ahciport_pending_ncq_tags,
7664 		    port_sactive, port_cmd_issue);
7665 
7666 		current_tags = ahci_portp->ahciport_pending_ncq_tags &
7667 		    port_sactive & ~port_cmd_issue &
7668 		    AHCI_NCQ_SLOT_MASK(ahci_portp);
7669 
7670 		while (current_tags) {
7671 			current_slot = ddi_ffs(current_tags) - 1;
7672 			if (current_slot == -1) {
7673 				goto out;
7674 			}
7675 
7676 			satapkt = ahci_portp->ahciport_slot_pkts[current_slot];
7677 			AHCIDBG(AHCIDBG_INTR|AHCIDBG_NCQ|AHCIDBG_ERRS,
7678 			    ahci_ctlp, "ahci_intr_non_fatal_error: "
7679 			    "port %d, satapkt 0x%p is outstanding when "
7680 			    "error occurs", port, (void *)satapkt);
7681 
7682 			CLEAR_BIT(current_tags, current_slot);
7683 		}
7684 	}
7685 out:
7686 #endif
7687 	mutex_exit(&ahci_portp->ahciport_mutex);
7688 
7689 	return (AHCI_SUCCESS);
7690 }
7691 
7692 /*
7693  * According to the AHCI spec, the error types include system memory
7694  * errors, interface errors, port multiplier errors, device errors,
7695  * command list overflow, command list underflow, native command
7696  * queuing tag errors and pio data transfer errors.
7697  *
7698  * System memory errors such as target abort, master abort, and parity
7699  * may cause the host to stop, and they are serious errors and needed
7700  * to be recovered with software intervention. When system software
7701  * has given a pointer to the HBA that doesn't exist in physical memory,
7702  * a master/target abort error occurs, and PxIS.HBFS will be set. A
7703  * data error such as CRC or parity occurs, the HBA aborts the transfer
7704  * (if necessary) and PxIS.HBDS will be set.
7705  *
7706  * Interface errors are errors that occur due to electrical issues on
7707  * the interface, or protocol miscommunication between the device and
7708  * HBA, and the respective PxSERR register bit will be set. And PxIS.IFS
7709  * (fatal) or PxIS.INFS (non-fatal) will be set. The conditions that
7710  * causes PxIS.IFS/PxIS.INFS to be set are
7711  * 	1. in PxSERR.ERR, P bit is set to '1'
7712  *	2. in PxSERR.DIAG, C or H bit is set to '1'
7713  *	3. PhyRdy drop unexpectly, N bit is set to '1'
7714  * If the error occurred during a non-data FIS, the FIS must be
7715  * retransmitted, and the error is non-fatal and PxIS.INFS is set. If
7716  * the error occurred during a data FIS, the transfer will stop, so
7717  * the error is fatal and PxIS.IFS is set.
7718  *
7719  * When a FIS arrives that updates the taskfile, the HBA checks to see
7720  * if PxTFD.STS.ERR is set. If yes, PxIS.TFES will be set and the HBA
7721  * stops processing any more commands.
7722  *
7723  * Command list overflow is defined as software building a command table
7724  * that has fewer total bytes than the transaction given to the device.
7725  * On device writes, the HBA will run out of data, and on reads, there
7726  * will be no room to put the data. For an overflow on data read, either
7727  * PIO or DMA, the HBA will set PxIS.OFS, and it's a non-fatal error.
7728  * For an overflow on data write, setting PxIS.OFS is optional for both
7729  * DMA and PIO, and a COMRESET is required by software to clean up from
7730  * this serious error.
7731  *
7732  * Command list underflow is defined as software building a command
7733  * table that has more total bytes than the transaction given to the
7734  * device. For data writes, both PIO and DMA, the device will detect
7735  * an error and end the transfer. And these errors are most likely going
7736  * to be fatal errors that will cause the port to be restarted. For
7737  * data reads, the HBA updates its PRD byte count, and may be
7738  * able to continue normally, but is not required to. And The HBA is
7739  * not required to detect underflow conditions for native command
7740  * queuing command.
7741  *
7742  * The HBA does not actively check incoming DMA Setup FISes to ensure
7743  * that the PxSACT register bit for that slot is set. Existing error
7744  * mechanisms, such as host bus failure, or bad protocol, are used to
7745  * recover from this case.
7746  *
7747  * In accordance with Serial ATA 1.0a, DATA FISes prior to the final
7748  * DATA FIS must be an integral number of Dwords. If the HBA receives
7749  * a request which is not an integral number of Dwords, the HBA
7750  * set PxSERR.ERR.P to '1', set PxIS.IFS to '1' and stop running until
7751  * software restarts the port. And the HBA ensures that the size
7752  * of the DATA FIS received during a PIO command matches the size in
7753  * the Transfer Cound field of the preceding PIO Setup FIS, if not, the
7754  * HBA sets PxSERR.ERR.P to '1', set PxIS.IFS to '1', and then
7755  * stop running until software restarts the port.
7756  */
7757 /*
7758  * the fatal errors include PxIS.IFS, PxIS.HBDS, PxIS.HBFS and PxIS.TFES.
7759  *
7760  * PxIS.IFS indicates that the hba encountered an error on the serial ata
7761  * interface which caused the transfer to stop.
7762  *
7763  * PxIS.HBDS indicates that the hba encountered a data error
7764  * (uncorrectable ecc/parity) when reading from or writing to system memory.
7765  *
7766  * PxIS.HBFS indicates that the hba encountered a host bus error that it
7767  * cannot recover from, such as a bad software pointer.
7768  *
7769  * PxIS.TFES is set whenever the status register is updated by the device
7770  * and the error bit (bit 0) is set.
7771  */
7772 static int
7773 ahci_intr_fatal_error(ahci_ctl_t *ahci_ctlp,
7774     ahci_port_t *ahci_portp, uint8_t port, uint32_t intr_status)
7775 {
7776 	uint32_t port_cmd_status;
7777 	uint32_t port_serror;
7778 	uint32_t task_file_status;
7779 	int failed_slot;
7780 	sata_pkt_t *spkt = NULL;
7781 	uint8_t err_byte;
7782 	ahci_event_arg_t *args;
7783 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
7784 	uint32_t failed_tags = 0;
7785 	int task_fail_flag = 0, task_abort_flag = 0;
7786 	uint32_t slot_status;
7787 
7788 	mutex_enter(&ahci_portp->ahciport_mutex);
7789 
7790 	/*
7791 	 * ahci_intr_phyrdy_change() may have rendered it to
7792 	 * SATA_DTYPE_NONE.
7793 	 */
7794 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
7795 		AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
7796 		    "ahci_intr_fatal_error: port %d no device attached, "
7797 		    "and just return without doing anything", port);
7798 		goto out0;
7799 	}
7800 
7801 	if (intr_status & AHCI_INTR_STATUS_TFES) {
7802 		task_file_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7803 		    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
7804 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
7805 		    "ahci_intr_fatal_error: port %d "
7806 		    "task_file_status = 0x%x", port, task_file_status);
7807 		task_fail_flag = 1;
7808 
7809 		err_byte = (task_file_status & AHCI_TFD_ERR_MASK)
7810 		    >> AHCI_TFD_ERR_SHIFT;
7811 		if (err_byte == SATA_ERROR_ABORT)
7812 			task_abort_flag = 1;
7813 	}
7814 
7815 	/*
7816 	 * Here we just log the fatal error info in interrupt context.
7817 	 * Misc recovery processing will be handled in task queue.
7818 	 */
7819 	if (task_fail_flag  == 1) {
7820 		if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
7821 			/*
7822 			 * Read PxCMD.CCS to determine the slot that the HBA
7823 			 * was processing when the error occurred.
7824 			 */
7825 			port_cmd_status = ddi_get32(
7826 			    ahci_ctlp->ahcictl_ahci_acc_handle,
7827 			    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
7828 			failed_slot = (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
7829 			    AHCI_CMD_STATUS_CCS_SHIFT;
7830 			failed_tags = 0x1 << failed_slot;
7831 
7832 			spkt = ahci_portp->ahciport_slot_pkts[failed_slot];
7833 			AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
7834 			    "ahci_intr_fatal_error: spkt 0x%p is being "
7835 			    "processed when fatal error occurred for port %d",
7836 			    spkt, port);
7837 
7838 			/*
7839 			 * Won't emit the error message if it is an IDENTIFY
7840 			 * DEVICE command sent to an ATAPI device.
7841 			 */
7842 			if ((spkt != NULL) &&
7843 			    (spkt->satapkt_cmd.satacmd_cmd_reg ==
7844 			    SATAC_ID_DEVICE) &&
7845 			    (task_abort_flag == 1))
7846 			goto out1;
7847 
7848 			/*
7849 			 * Won't emit the error message if it is an ATAPI PACKET
7850 			 * command
7851 			 */
7852 			if ((spkt != NULL) &&
7853 			    (spkt->satapkt_cmd.satacmd_cmd_reg == SATAC_PACKET))
7854 				goto out1;
7855 
7856 		} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
7857 			slot_status = ddi_get32(
7858 			    ahci_ctlp->ahcictl_ahci_acc_handle,
7859 			    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
7860 			failed_tags = slot_status &
7861 			    AHCI_NCQ_SLOT_MASK(ahci_portp);
7862 		}
7863 	}
7864 
7865 	/* print the fatal error type */
7866 	ahci_log_fatal_error_message(ahci_ctlp, port, intr_status);
7867 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_ERRPRINT;
7868 
7869 	port_serror = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7870 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port));
7871 
7872 	/* print PxSERR related error message */
7873 	ahci_log_serror_message(ahci_ctlp, port, port_serror, 0);
7874 
7875 	/* print task file register value */
7876 	if (task_fail_flag == 1) {
7877 		cmn_err(CE_WARN, "!ahci%d: ahci port %d task_file_status "
7878 		    "= 0x%x", instance, port, task_file_status);
7879 		if (task_abort_flag == 1) {
7880 			cmn_err(CE_WARN, "!ahci%d: the below command (s) on "
7881 			    "port %d are aborted", instance, port);
7882 			ahci_dump_commands(ahci_ctlp, port, failed_tags);
7883 		}
7884 	}
7885 
7886 out1:
7887 	/* Prepare the argument for the taskq */
7888 	args = ahci_portp->ahciport_event_args;
7889 	args->ahciea_ctlp = (void *)ahci_ctlp;
7890 	args->ahciea_portp = (void *)ahci_portp;
7891 	args->ahciea_event = intr_status;
7892 	AHCI_ADDR_SET_PORT((ahci_addr_t *)args->ahciea_addrp, port);
7893 
7894 	/* Start the taskq to handle error recovery */
7895 	if ((ddi_taskq_dispatch(ahci_portp->ahciport_event_taskq,
7896 	    ahci_events_handler,
7897 	    (void *)args, DDI_NOSLEEP)) != DDI_SUCCESS) {
7898 		ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_ERRPRINT;
7899 		cmn_err(CE_WARN, "!ahci%d: start taskq for error recovery "
7900 		    "port %d failed", instance, port);
7901 	}
7902 out0:
7903 	mutex_exit(&ahci_portp->ahciport_mutex);
7904 
7905 	return (AHCI_SUCCESS);
7906 }
7907 
7908 /*
7909  * Hot Plug Operation for platforms that support Cold Presence Detect.
7910  *
7911  * When set, a device status has changed as detected by the cold presence
7912  * detect logic. This bit can either be set due to a non-connected port
7913  * receiving a device, or a connected port having its device removed.
7914  * This bit is only valid if the port supports cold presence detect as
7915  * indicated by PxCMD.CPD set to '1'.
7916  *
7917  * At the moment, this interrupt is not needed and disabled and we just
7918  * log the debug message.
7919  */
7920 static int
7921 ahci_intr_cold_port_detect(ahci_ctl_t *ahci_ctlp,
7922     ahci_port_t *ahci_portp, uint8_t port)
7923 {
7924 	uint32_t port_cmd_status;
7925 	sata_device_t sdevice;
7926 
7927 	AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
7928 	    "ahci_intr_cold_port_detect enter, port %d", port);
7929 
7930 	mutex_enter(&ahci_portp->ahciport_mutex);
7931 
7932 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
7933 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
7934 	if (!(port_cmd_status & AHCI_CMD_STATUS_CPD)) {
7935 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
7936 		    "port %d does not support cold presence detect, so "
7937 		    "we just ignore this interrupt", port);
7938 		mutex_exit(&ahci_portp->ahciport_mutex);
7939 		return (AHCI_SUCCESS);
7940 	}
7941 
7942 	AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
7943 	    "port %d device status has changed", port);
7944 
7945 	bzero((void *)&sdevice, sizeof (sata_device_t));
7946 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
7947 	sdevice.satadev_addr.qual = SATA_ADDR_CPORT;
7948 	sdevice.satadev_addr.pmport = 0;
7949 	sdevice.satadev_state = SATA_PSTATE_PWRON;
7950 
7951 	if (port_cmd_status & AHCI_CMD_STATUS_CPS) {
7952 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
7953 		    "port %d: a device is hot plugged", port);
7954 		mutex_exit(&ahci_portp->ahciport_mutex);
7955 		sata_hba_event_notify(
7956 		    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
7957 		    &sdevice,
7958 		    SATA_EVNT_DEVICE_ATTACHED);
7959 		mutex_enter(&ahci_portp->ahciport_mutex);
7960 
7961 	} else {
7962 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
7963 		    "port %d: a device is hot unplugged", port);
7964 		mutex_exit(&ahci_portp->ahciport_mutex);
7965 		sata_hba_event_notify(
7966 		    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
7967 		    &sdevice,
7968 		    SATA_EVNT_DEVICE_DETACHED);
7969 		mutex_enter(&ahci_portp->ahciport_mutex);
7970 	}
7971 
7972 	mutex_exit(&ahci_portp->ahciport_mutex);
7973 
7974 	return (AHCI_SUCCESS);
7975 }
7976 
7977 /*
7978  * Enable the interrupts for a particular port.
7979  *
7980  * WARNING!!! ahciport_mutex should be acquired before the function
7981  * is called.
7982  */
7983 static void
7984 ahci_enable_port_intrs(ahci_ctl_t *ahci_ctlp, uint8_t port)
7985 {
7986 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
7987 	    "ahci_enable_port_intrs enter, port %d", port);
7988 
7989 	/*
7990 	 * Clear port interrupt status before enabling interrupt
7991 	 */
7992 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
7993 	    (uint32_t *)AHCI_PORT_PxIS(ahci_ctlp, port),
7994 	    AHCI_PORT_INTR_MASK);
7995 
7996 	/*
7997 	 * Clear the pending bit from IS.IPS
7998 	 */
7999 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
8000 	    (uint32_t *)AHCI_GLOBAL_IS(ahci_ctlp), (1 << port));
8001 
8002 	/*
8003 	 * Enable the following interrupts:
8004 	 *	Device to Host Register FIS Interrupt (DHRS)
8005 	 *	PIO Setup FIS Interrupt (PSS)
8006 	 *	Set Device Bits Interrupt (SDBS)
8007 	 *	Unknown FIS Interrupt (UFS)
8008 	 *	Port Connect Change Status (PCS)
8009 	 *	PhyRdy Change Status (PRCS)
8010 	 *	Overflow Status (OFS)
8011 	 *	Interface Non-fatal Error Status (INFS)
8012 	 *	Interface Fatal Error Status (IFS)
8013 	 *	Host Bus Data Error Status (HBDS)
8014 	 *	Host Bus Fatal Error Status (HBFS)
8015 	 *	Task File Error Status (TFES)
8016 	 */
8017 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
8018 	    (uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port),
8019 	    (AHCI_INTR_STATUS_DHRS |
8020 	    AHCI_INTR_STATUS_PSS |
8021 	    AHCI_INTR_STATUS_SDBS |
8022 	    AHCI_INTR_STATUS_UFS |
8023 	    AHCI_INTR_STATUS_DPS |
8024 	    AHCI_INTR_STATUS_PCS |
8025 	    AHCI_INTR_STATUS_PRCS |
8026 	    AHCI_INTR_STATUS_OFS |
8027 	    AHCI_INTR_STATUS_INFS |
8028 	    AHCI_INTR_STATUS_IFS |
8029 	    AHCI_INTR_STATUS_HBDS |
8030 	    AHCI_INTR_STATUS_HBFS |
8031 	    AHCI_INTR_STATUS_TFES));
8032 }
8033 
8034 /*
8035  * Enable interrupts for all the ports.
8036  *
8037  * WARNING!!! ahcictl_mutex should be acquired before the function
8038  * is called.
8039  */
8040 static void
8041 ahci_enable_all_intrs(ahci_ctl_t *ahci_ctlp)
8042 {
8043 	uint32_t ghc_control;
8044 
8045 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_enable_all_intrs enter", NULL);
8046 
8047 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
8048 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
8049 
8050 	ghc_control |= AHCI_HBA_GHC_IE;
8051 
8052 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
8053 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
8054 }
8055 
8056 /*
8057  * Disable interrupts for a particular port.
8058  *
8059  * WARNING!!! ahciport_mutex should be acquired before the function
8060  * is called.
8061  */
8062 static void
8063 ahci_disable_port_intrs(ahci_ctl_t *ahci_ctlp, uint8_t port)
8064 {
8065 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
8066 	    "ahci_disable_port_intrs enter, port %d", port);
8067 
8068 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
8069 	    (uint32_t *)AHCI_PORT_PxIE(ahci_ctlp, port), 0);
8070 }
8071 
8072 /*
8073  * Disable interrupts for the whole HBA.
8074  *
8075  * The global bit is cleared, then all interrupt sources from all
8076  * ports are disabled.
8077  *
8078  * WARNING!!! ahcictl_mutex should be acquired before the function
8079  * is called.
8080  */
8081 static void
8082 ahci_disable_all_intrs(ahci_ctl_t *ahci_ctlp)
8083 {
8084 	uint32_t ghc_control;
8085 
8086 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_disable_all_intrs enter",
8087 	    NULL);
8088 
8089 	ghc_control = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
8090 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp));
8091 
8092 	ghc_control &= ~ AHCI_HBA_GHC_IE;
8093 
8094 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
8095 	    (uint32_t *)AHCI_GLOBAL_GHC(ahci_ctlp), ghc_control);
8096 }
8097 
8098 /*
8099  * Handle FIXED or MSI interrupts.
8100  */
8101 /*
8102  * According to AHCI spec, the HBA may support several interrupt modes:
8103  *	* pin based interrupts (FIXED)
8104  *	* single MSI message interrupts
8105  *	* multiple MSI based message interrupts
8106  *
8107  * For pin based interrupts, the software interrupt handler need to check IS
8108  * register to find out which port has pending interrupts. And then check
8109  * PxIS register to find out which interrupt events happened on that port.
8110  *
8111  * For single MSI message interrupts, MSICAP.MC.MSIE is set with '1', and
8112  * MSICAP.MC.MME is set with '0'. This mode is similar to pin based interrupts
8113  * in that software interrupt handler need to check IS register to determine
8114  * which port triggered the interrupts since it uses a single message for all
8115  * port interrupts.
8116  *
8117  * HBA may optionally support multiple MSI message for better performance. In
8118  * this mode, each port may have its own interrupt message, and thus generation
8119  * of interrupts is no longer controlled through the IS register. MSICAP.MC.MMC
8120  * represents a power-of-2 wrapper on the number of implemented ports, and
8121  * the mapping of ports to interrupts is done in a 1-1 relationship, up to the
8122  * maximum number of assigned interrupts. When the number of MSI messages
8123  * allocated is less than the number requested, then hardware may have two
8124  * implementation behaviors:
8125  *	* assign each ports its own interrupt and then force all additional
8126  *	  ports to share the last interrupt message, and this condition is
8127  *	  indicated by clearing GHC.MRSM to '0'
8128  *	* revert to single MSI mode, indicated by setting GHC.MRSM to '1'
8129  * When multiple-message MSI is enabled, hardware will still set IS register
8130  * as single message case. And this IS register may be used by software when
8131  * fewer than the requested number of messages is granted in order to determine
8132  * which port had the interrupt.
8133  *
8134  * Note: The current ahci driver only supports the first two interrupt modes:
8135  * pin based interrupts and single MSI message interrupts, and the reason
8136  * is indicated in below code.
8137  */
8138 static int
8139 ahci_add_intrs(ahci_ctl_t *ahci_ctlp, int intr_type)
8140 {
8141 	dev_info_t *dip = ahci_ctlp->ahcictl_dip;
8142 	int		count, avail, actual;
8143 	int		i, rc;
8144 
8145 	AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INIT|AHCIDBG_INTR, ahci_ctlp,
8146 	    "ahci_add_intrs enter interrupt type 0x%x", intr_type);
8147 
8148 	/* get number of interrupts. */
8149 	rc = ddi_intr_get_nintrs(dip, intr_type, &count);
8150 	if ((rc != DDI_SUCCESS) || (count == 0)) {
8151 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
8152 		    "ddi_intr_get_nintrs() failed, "
8153 		    "rc %d count %d\n", rc, count);
8154 		return (DDI_FAILURE);
8155 	}
8156 
8157 	/* get number of available interrupts. */
8158 	rc = ddi_intr_get_navail(dip, intr_type, &avail);
8159 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
8160 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
8161 		    "ddi_intr_get_navail() failed, "
8162 		    "rc %d avail %d\n", rc, avail);
8163 		return (DDI_FAILURE);
8164 	}
8165 
8166 #if AHCI_DEBUG
8167 	if (avail < count) {
8168 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
8169 		    "ddi_intr_get_nintrs returned %d, navail() returned %d",
8170 		    count, avail);
8171 	}
8172 #endif
8173 
8174 	/*
8175 	 * Note: So far Solaris restricts the maximum number of messages for
8176 	 * x86 to 2, that is avail is 2, so here we set the count with 1 to
8177 	 * force the driver to use single MSI message interrupt. In future if
8178 	 * Solaris remove the restriction, then we need to delete the below
8179 	 * code and try to use multiple interrupt routine to gain better
8180 	 * performance.
8181 	 */
8182 	if ((intr_type == DDI_INTR_TYPE_MSI) && (count > 1)) {
8183 		AHCIDBG(AHCIDBG_INTR, ahci_ctlp,
8184 		    "force to use one interrupt routine though the "
8185 		    "HBA supports %d interrupt", count);
8186 		count = 1;
8187 	}
8188 
8189 	/* Allocate an array of interrupt handles. */
8190 	ahci_ctlp->ahcictl_intr_size = count * sizeof (ddi_intr_handle_t);
8191 	ahci_ctlp->ahcictl_intr_htable =
8192 	    kmem_alloc(ahci_ctlp->ahcictl_intr_size, KM_SLEEP);
8193 
8194 	/* call ddi_intr_alloc(). */
8195 	rc = ddi_intr_alloc(dip, ahci_ctlp->ahcictl_intr_htable,
8196 	    intr_type, 0, count, &actual, DDI_INTR_ALLOC_NORMAL);
8197 
8198 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
8199 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
8200 		    "ddi_intr_alloc() failed, rc %d count %d actual %d "
8201 		    "avail %d\n", rc, count, actual, avail);
8202 		kmem_free(ahci_ctlp->ahcictl_intr_htable,
8203 		    ahci_ctlp->ahcictl_intr_size);
8204 		return (DDI_FAILURE);
8205 	}
8206 
8207 	/* use interrupt count returned */
8208 #if AHCI_DEBUG
8209 	if (actual < count) {
8210 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
8211 		    "Requested: %d, Received: %d", count, actual);
8212 	}
8213 #endif
8214 
8215 	ahci_ctlp->ahcictl_intr_cnt = actual;
8216 
8217 	/*
8218 	 * Get priority for first, assume remaining are all the same.
8219 	 */
8220 	if (ddi_intr_get_pri(ahci_ctlp->ahcictl_intr_htable[0],
8221 	    &ahci_ctlp->ahcictl_intr_pri) != DDI_SUCCESS) {
8222 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
8223 		    "ddi_intr_get_pri() failed", NULL);
8224 
8225 		/* Free already allocated intr. */
8226 		for (i = 0; i < actual; i++) {
8227 			(void) ddi_intr_free(ahci_ctlp->ahcictl_intr_htable[i]);
8228 		}
8229 
8230 		kmem_free(ahci_ctlp->ahcictl_intr_htable,
8231 		    ahci_ctlp->ahcictl_intr_size);
8232 		return (DDI_FAILURE);
8233 	}
8234 
8235 	/* Test for high level interrupt. */
8236 	if (ahci_ctlp->ahcictl_intr_pri >= ddi_intr_get_hilevel_pri()) {
8237 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
8238 		    "ahci_add_intrs: Hi level intr not supported", NULL);
8239 
8240 		/* Free already allocated intr. */
8241 		for (i = 0; i < actual; i++) {
8242 			(void) ddi_intr_free(ahci_ctlp->ahcictl_intr_htable[i]);
8243 		}
8244 
8245 		kmem_free(ahci_ctlp->ahcictl_intr_htable,
8246 		    sizeof (ddi_intr_handle_t));
8247 
8248 		return (DDI_FAILURE);
8249 	}
8250 
8251 	/* Call ddi_intr_add_handler(). */
8252 	for (i = 0; i < actual; i++) {
8253 		if (ddi_intr_add_handler(ahci_ctlp->ahcictl_intr_htable[i],
8254 		    ahci_intr, (caddr_t)ahci_ctlp, NULL) != DDI_SUCCESS) {
8255 			AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
8256 			    "ddi_intr_add_handler() failed", NULL);
8257 
8258 			/* Free already allocated intr. */
8259 			for (i = 0; i < actual; i++) {
8260 				(void) ddi_intr_free(
8261 				    ahci_ctlp->ahcictl_intr_htable[i]);
8262 			}
8263 
8264 			kmem_free(ahci_ctlp->ahcictl_intr_htable,
8265 			    ahci_ctlp->ahcictl_intr_size);
8266 			return (DDI_FAILURE);
8267 		}
8268 	}
8269 
8270 	if (ddi_intr_get_cap(ahci_ctlp->ahcictl_intr_htable[0],
8271 	    &ahci_ctlp->ahcictl_intr_cap) != DDI_SUCCESS) {
8272 		AHCIDBG(AHCIDBG_INTR|AHCIDBG_INIT, ahci_ctlp,
8273 		    "ddi_intr_get_cap() failed", NULL);
8274 
8275 		/* Free already allocated intr. */
8276 		for (i = 0; i < actual; i++) {
8277 			(void) ddi_intr_free(
8278 			    ahci_ctlp->ahcictl_intr_htable[i]);
8279 		}
8280 
8281 		kmem_free(ahci_ctlp->ahcictl_intr_htable,
8282 		    ahci_ctlp->ahcictl_intr_size);
8283 		return (DDI_FAILURE);
8284 	}
8285 
8286 	if (ahci_ctlp->ahcictl_intr_cap & DDI_INTR_FLAG_BLOCK) {
8287 		/* Call ddi_intr_block_enable() for MSI. */
8288 		(void) ddi_intr_block_enable(ahci_ctlp->ahcictl_intr_htable,
8289 		    ahci_ctlp->ahcictl_intr_cnt);
8290 	} else {
8291 		/* Call ddi_intr_enable() for FIXED or MSI non block enable. */
8292 		for (i = 0; i < ahci_ctlp->ahcictl_intr_cnt; i++) {
8293 			(void) ddi_intr_enable(
8294 			    ahci_ctlp->ahcictl_intr_htable[i]);
8295 		}
8296 	}
8297 
8298 	return (DDI_SUCCESS);
8299 }
8300 
8301 /*
8302  * Removes the registered interrupts irrespective of whether they
8303  * were legacy or MSI.
8304  *
8305  * WARNING!!! The controller interrupts must be disabled before calling
8306  * this routine.
8307  */
8308 static void
8309 ahci_rem_intrs(ahci_ctl_t *ahci_ctlp)
8310 {
8311 	int x;
8312 
8313 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp, "ahci_rem_intrs entered", NULL);
8314 
8315 	/* Disable all interrupts. */
8316 	if ((ahci_ctlp->ahcictl_intr_type == DDI_INTR_TYPE_MSI) &&
8317 	    (ahci_ctlp->ahcictl_intr_cap & DDI_INTR_FLAG_BLOCK)) {
8318 		/* Call ddi_intr_block_disable(). */
8319 		(void) ddi_intr_block_disable(ahci_ctlp->ahcictl_intr_htable,
8320 		    ahci_ctlp->ahcictl_intr_cnt);
8321 	} else {
8322 		for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
8323 			(void) ddi_intr_disable(
8324 			    ahci_ctlp->ahcictl_intr_htable[x]);
8325 		}
8326 	}
8327 
8328 	/* Call ddi_intr_remove_handler(). */
8329 	for (x = 0; x < ahci_ctlp->ahcictl_intr_cnt; x++) {
8330 		(void) ddi_intr_remove_handler(
8331 		    ahci_ctlp->ahcictl_intr_htable[x]);
8332 		(void) ddi_intr_free(ahci_ctlp->ahcictl_intr_htable[x]);
8333 	}
8334 
8335 	kmem_free(ahci_ctlp->ahcictl_intr_htable, ahci_ctlp->ahcictl_intr_size);
8336 }
8337 
8338 /*
8339  * This routine tries to put port into P:NotRunning state by clearing
8340  * PxCMD.ST. HBA will clear PxCI to 0h, PxSACT to 0h, PxCMD.CCS to 0h
8341  * and PxCMD.CR to '0'.
8342  *
8343  * WARNING!!! ahciport_mutex should be acquired before the function
8344  * is called.
8345  */
8346 static int
8347 ahci_put_port_into_notrunning_state(ahci_ctl_t *ahci_ctlp,
8348     ahci_port_t *ahci_portp, uint8_t port)
8349 {
8350 	uint32_t port_cmd_status;
8351 	int loop_count;
8352 
8353 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
8354 	    "ahci_put_port_into_notrunning_state enter: port %d", port);
8355 
8356 	port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
8357 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
8358 
8359 	port_cmd_status &= ~AHCI_CMD_STATUS_ST;
8360 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
8361 	    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port), port_cmd_status);
8362 
8363 	/* Wait until PxCMD.CR is cleared */
8364 	loop_count = 0;
8365 	do {
8366 		port_cmd_status =
8367 		    ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
8368 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
8369 
8370 		if (loop_count++ > AHCI_POLLRATE_PORT_IDLE) {
8371 			AHCIDBG(AHCIDBG_INIT, ahci_ctlp,
8372 			    "clearing port %d CMD.CR timeout, "
8373 			    "port_cmd_status = 0x%x", port,
8374 			    port_cmd_status);
8375 			/*
8376 			 * We are effectively timing out after 0.5 sec.
8377 			 * This value is specified in AHCI spec.
8378 			 */
8379 			break;
8380 		}
8381 
8382 		/* Wait for 10 millisec */
8383 		drv_usecwait(AHCI_10MS_USECS);
8384 	} while (port_cmd_status & AHCI_CMD_STATUS_CR);
8385 
8386 	ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_STARTED;
8387 
8388 	if (port_cmd_status & AHCI_CMD_STATUS_CR) {
8389 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
8390 		    "ahci_put_port_into_notrunning_state: failed to clear "
8391 		    "PxCMD.CR to '0' after loop count: %d, and "
8392 		    "port_cmd_status = 0x%x", loop_count, port_cmd_status);
8393 		return (AHCI_FAILURE);
8394 	} else {
8395 		AHCIDBG(AHCIDBG_INIT|AHCIDBG_POLL_LOOP, ahci_ctlp,
8396 		    "ahci_put_port_into_notrunning_state: succeeded to clear "
8397 		    "PxCMD.CR to '0' after loop count: %d, and "
8398 		    "port_cmd_status = 0x%x", loop_count, port_cmd_status);
8399 		return (AHCI_SUCCESS);
8400 	}
8401 }
8402 
8403 /*
8404  * First clear PxCMD.ST, and then check PxTFD. If both PxTFD.STS.BSY
8405  * and PxTFD.STS.DRQ cleared to '0', it means the device is in a
8406  * stable state, then set PxCMD.ST to '1' to start the port directly.
8407  * If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to '1', then issue a
8408  * COMRESET to the device to put it in an idle state.
8409  *
8410  * The fifth argument returns whether the port reset is involved during
8411  * the process.
8412  *
8413  * The routine will be called under following scenarios:
8414  * 	+ To reset the HBA
8415  *	+ To abort the packet(s)
8416  *	+ To reset the port
8417  *	+ To activate the port
8418  *	+ Fatal error recovery
8419  *	+ To abort the timeout packet(s)
8420  *
8421  * WARNING!!! ahciport_mutex should be acquired before the function
8422  * is called. And ahciport_mutex will be released before the reset
8423  * event is reported to sata module by calling sata_hba_event_notify,
8424  * and then be acquired again later.
8425  *
8426  * NOTES!!! During this procedure, PxSERR register will be cleared, and
8427  * according to the spec, the clearance of three bits will also clear
8428  * three interrupt status bits.
8429  *	1. PxSERR.DIAG.F will clear PxIS.UFS
8430  *	2. PxSERR.DIAG.X will clear PxIS.PCS
8431  *	3. PxSERR.DIAG.N will clear PxIS.PRCS
8432  *
8433  * Among these three interrupt events, the driver needs to take care of
8434  * PxIS.PRCS, which is the hot plug event. When the driver found out
8435  * a device was unplugged, it will call the interrupt handler.
8436  */
8437 static int
8438 ahci_restart_port_wait_till_ready(ahci_ctl_t *ahci_ctlp,
8439     ahci_port_t *ahci_portp, uint8_t port, int flag, int *reset_flag)
8440 {
8441 	uint32_t port_sstatus;
8442 	uint32_t task_file_status;
8443 	sata_device_t sdevice;
8444 	int rval;
8445 	ahci_addr_t addr_port;
8446 	ahci_pmult_info_t *pminfo = NULL;
8447 	int dev_exists_begin = 0;
8448 	int dev_exists_end = 0;
8449 	uint32_t previous_dev_type = ahci_portp->ahciport_device_type;
8450 	int npmport = 0;
8451 	uint8_t cport = ahci_ctlp->ahcictl_port_to_cport[port];
8452 
8453 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
8454 	    "ahci_restart_port_wait_till_ready: port %d enter", port);
8455 
8456 	AHCI_ADDR_SET_PORT(&addr_port, port);
8457 
8458 	if (ahci_portp->ahciport_device_type != SATA_DTYPE_NONE)
8459 		dev_exists_begin = 1;
8460 
8461 	/* First clear PxCMD.ST */
8462 	rval = ahci_put_port_into_notrunning_state(ahci_ctlp, ahci_portp,
8463 	    port);
8464 	if (rval != AHCI_SUCCESS)
8465 		/*
8466 		 * If PxCMD.CR does not clear within a reasonable time, it
8467 		 * may assume the interface is in a hung condition and may
8468 		 * continue with issuing the port reset.
8469 		 */
8470 		goto reset;
8471 
8472 	/* Then clear PxSERR */
8473 	ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
8474 	    (uint32_t *)AHCI_PORT_PxSERR(ahci_ctlp, port),
8475 	    AHCI_SERROR_CLEAR_ALL);
8476 
8477 	/* Then get PxTFD */
8478 	task_file_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
8479 	    (uint32_t *)AHCI_PORT_PxTFD(ahci_ctlp, port));
8480 
8481 	/*
8482 	 * Check whether the device is in a stable status, if yes,
8483 	 * then start the port directly. However for ahci_tran_reset_dport,
8484 	 * we may have to perform a port reset.
8485 	 */
8486 	if (!(task_file_status & (AHCI_TFD_STS_BSY | AHCI_TFD_STS_DRQ)) &&
8487 	    !(flag & AHCI_PORT_RESET))
8488 		goto out;
8489 
8490 reset:
8491 	/*
8492 	 * If PxTFD.STS.BSY or PxTFD.STS.DRQ is set to '1', then issue
8493 	 * a COMRESET to the device
8494 	 */
8495 	ahci_disable_port_intrs(ahci_ctlp, port);
8496 	rval = ahci_port_reset(ahci_ctlp, ahci_portp, &addr_port);
8497 	ahci_enable_port_intrs(ahci_ctlp, port);
8498 
8499 #ifdef AHCI_DEBUG
8500 	if (rval != AHCI_SUCCESS)
8501 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
8502 		    "ahci_restart_port_wait_till_ready: port %d failed",
8503 		    port);
8504 #endif
8505 
8506 	if (reset_flag != NULL)
8507 		*reset_flag = 1;
8508 
8509 	/* Indicate to the framework that a reset has happened. */
8510 	if ((ahci_portp->ahciport_device_type != SATA_DTYPE_NONE) &&
8511 	    (ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) &&
8512 	    !(flag & AHCI_RESET_NO_EVENTS_UP)) {
8513 		/* Set the reset in progress flag */
8514 		ahci_portp->ahciport_reset_in_progress = 1;
8515 
8516 		bzero((void *)&sdevice, sizeof (sata_device_t));
8517 		sdevice.satadev_addr.cport =
8518 		    ahci_ctlp->ahcictl_port_to_cport[port];
8519 		sdevice.satadev_addr.pmport = 0;
8520 		sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
8521 
8522 		sdevice.satadev_state = SATA_DSTATE_RESET |
8523 		    SATA_DSTATE_PWR_ACTIVE;
8524 		if (ahci_ctlp->ahcictl_sata_hba_tran) {
8525 			mutex_exit(&ahci_portp->ahciport_mutex);
8526 			sata_hba_event_notify(
8527 			    ahci_ctlp->ahcictl_sata_hba_tran->sata_tran_hba_dip,
8528 			    &sdevice,
8529 			    SATA_EVNT_DEVICE_RESET);
8530 			mutex_enter(&ahci_portp->ahciport_mutex);
8531 		}
8532 
8533 		AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
8534 		    "port %d sending event up: SATA_EVNT_DEVICE_RESET", port);
8535 	} else {
8536 		ahci_portp->ahciport_reset_in_progress = 0;
8537 	}
8538 
8539 out:
8540 	(void) ahci_start_port(ahci_ctlp, ahci_portp, port);
8541 
8542 	/* SStatus tells the presence of device. */
8543 	port_sstatus = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
8544 	    (uint32_t *)AHCI_PORT_PxSSTS(ahci_ctlp, port));
8545 
8546 	if (SSTATUS_GET_DET(port_sstatus) == SSTATUS_DET_DEVPRE_PHYCOM) {
8547 		dev_exists_end = 1;
8548 	}
8549 
8550 	if (dev_exists_begin == 0 && dev_exists_end == 0) /* 0 -> 0 */
8551 		return (rval);
8552 
8553 	/* Check whether a hot plug event happened */
8554 	if (dev_exists_begin == 1 && dev_exists_end == 0) { /* 1 -> 0 */
8555 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
8556 		    "ahci_restart_port_wait_till_ready: port %d "
8557 		    "device is removed", port);
8558 		ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_NODEV;
8559 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
8560 		    "ahci_restart_port_wait_till_ready: port %d "
8561 		    "AHCI_PORT_FLAG_NODEV flag is set", port);
8562 		mutex_exit(&ahci_portp->ahciport_mutex);
8563 		(void) ahci_intr_phyrdy_change(ahci_ctlp, ahci_portp, port);
8564 		mutex_enter(&ahci_portp->ahciport_mutex);
8565 
8566 		return (rval);
8567 	}
8568 
8569 
8570 	/* 0/1 -> 1 : device may change */
8571 	/*
8572 	 * May be called by ahci_fatal_error_recovery_handler, so
8573 	 * don't issue software if the previous device is ATAPI.
8574 	 */
8575 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_ATAPI)
8576 		return (rval);
8577 
8578 	/*
8579 	 * The COMRESET will make port multiplier enter legacy mode.
8580 	 * Issue a software reset to make it work again.
8581 	 */
8582 	ahci_find_dev_signature(ahci_ctlp, ahci_portp, &addr_port);
8583 
8584 	/*
8585 	 * Following codes are specific for the port multiplier
8586 	 */
8587 	if (previous_dev_type != SATA_DTYPE_PMULT &&
8588 	    ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) {
8589 		/* in case previous_dev_type is corrupt */
8590 		ahci_dealloc_pmult(ahci_ctlp, ahci_portp);
8591 		(void) ahci_start_port(ahci_ctlp, ahci_portp, port);
8592 		return (rval);
8593 	}
8594 
8595 	/* Device change: PMult -> Non-PMult */
8596 	if (previous_dev_type == SATA_DTYPE_PMULT &&
8597 	    ahci_portp->ahciport_device_type != SATA_DTYPE_PMULT) {
8598 		/*
8599 		 * This might happen because
8600 		 * 1. Software reset failed. Port multiplier is not correctly
8601 		 *    enumerated.
8602 		 * 2. Another non-port-multiplier device is attached. Perhaps
8603 		 *    the port multiplier was replaced by another device by
8604 		 *    whatever reason, but AHCI driver missed hot-plug event.
8605 		 *
8606 		 * Now that the port has been initialized, we just need to
8607 		 * update the port structure according new device, then report
8608 		 * and wait SATA framework to probe new device.
8609 		 */
8610 
8611 		/* Force to release pmult resource */
8612 		ahci_dealloc_pmult(ahci_ctlp, ahci_portp);
8613 		(void) ahci_start_port(ahci_ctlp, ahci_portp, port);
8614 
8615 		bzero((void *)&sdevice, sizeof (sata_device_t));
8616 		sdevice.satadev_addr.cport =
8617 		    ahci_ctlp->ahcictl_port_to_cport[port];
8618 		sdevice.satadev_addr.pmport = 0;
8619 		sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
8620 
8621 		sdevice.satadev_state = SATA_DSTATE_RESET |
8622 		    SATA_DSTATE_PWR_ACTIVE;
8623 
8624 		mutex_exit(&ahci_portp->ahciport_mutex);
8625 		sata_hba_event_notify(
8626 		    ahci_ctlp->ahcictl_dip,
8627 		    &sdevice,
8628 		    SATA_EVNT_DEVICE_RESET);
8629 		mutex_enter(&ahci_portp->ahciport_mutex);
8630 
8631 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
8632 		    "Port multiplier is [Gone] at port %d ", port);
8633 		AHCIDBG(AHCIDBG_EVENT, ahci_ctlp,
8634 		    "port %d sending event up: SATA_EVNT_DEVICE_RESET", port);
8635 
8636 		return (AHCI_SUCCESS);
8637 	}
8638 
8639 	/* Device change: Non-PMult -> PMult */
8640 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_PMULT) {
8641 
8642 		/* NOTE: The PxCMD.PMA may be cleared by HBA reset. */
8643 		ahci_alloc_pmult(ahci_ctlp, ahci_portp);
8644 
8645 		(void) ahci_start_port(ahci_ctlp, ahci_portp, port);
8646 	}
8647 	pminfo = ahci_portp->ahciport_pmult_info;
8648 	ASSERT(pminfo != NULL);
8649 
8650 	/* Device (may) change: PMult -> PMult */
8651 	/*
8652 	 * First initialize port multiplier. Set state to READY and wait for
8653 	 * probe entry point to initialize it
8654 	 */
8655 	ahci_portp->ahciport_port_state = SATA_STATE_READY;
8656 
8657 	/*
8658 	 * It's a little complicated while target is a port multiplier. we
8659 	 * need to COMRESET all pmports behind that PMult otherwise those
8660 	 * sub-links between the PMult and the sub-devices will be in an
8661 	 * inactive state (indicated by PSCR0/PxSSTS) and the following access
8662 	 * to those sub-devices will be rejected by Link-Fatal-Error.
8663 	 */
8664 	/*
8665 	 * The PxSNTF will be set soon after the pmult is plugged. While the
8666 	 * pmult itself is attaching, sata_hba_event_notfiy will fail. so we
8667 	 * simply mark every sub-port as 'unknown', then ahci_probe_pmport
8668 	 * will initialized it.
8669 	 */
8670 	for (npmport = 0; npmport < pminfo->ahcipmi_num_dev_ports; npmport++)
8671 		pminfo->ahcipmi_port_state[npmport] = SATA_STATE_UNKNOWN;
8672 
8673 	/* Report reset event. */
8674 	ahci_portp->ahciport_reset_in_progress = 1;
8675 
8676 	bzero((void *)&sdevice, sizeof (sata_device_t));
8677 	sdevice.satadev_addr.cport = cport;
8678 	sdevice.satadev_addr.pmport = SATA_PMULT_HOSTPORT;
8679 	sdevice.satadev_addr.qual = SATA_ADDR_PMULT;
8680 	sdevice.satadev_state = SATA_DSTATE_RESET | SATA_DSTATE_PWR_ACTIVE;
8681 	sata_hba_event_notify(ahci_ctlp->ahcictl_dip, &sdevice,
8682 	    SATA_EVNT_DEVICE_RESET);
8683 
8684 	return (rval);
8685 }
8686 
8687 /*
8688  * This routine may be called under four scenarios:
8689  *	a) do the recovery from fatal error
8690  *	b) or we need to timeout some commands
8691  *	c) or we need to abort some commands
8692  *	d) or we need reset device/port/controller
8693  *
8694  * In all these scenarios, we need to send any pending unfinished
8695  * commands up to sata framework.
8696  *
8697  * WARNING!!! ahciport_mutex should be acquired before the function is called.
8698  */
8699 static void
8700 ahci_mop_commands(ahci_ctl_t *ahci_ctlp,
8701     ahci_port_t *ahci_portp,
8702     uint32_t slot_status,
8703     uint32_t failed_tags,
8704     uint32_t timeout_tags,
8705     uint32_t aborted_tags,
8706     uint32_t reset_tags)
8707 {
8708 	uint32_t finished_tags = 0;
8709 	uint32_t unfinished_tags = 0;
8710 	int tmp_slot;
8711 	sata_pkt_t *satapkt;
8712 	int ncq_cmd_in_progress = 0;
8713 	int err_retri_cmd_in_progress = 0;
8714 	int rdwr_pmult_cmd_in_progress = 0;
8715 
8716 	AHCIDBG(AHCIDBG_ERRS|AHCIDBG_ENTRY, ahci_ctlp,
8717 	    "ahci_mop_commands entered: port: %d slot_status: 0x%x",
8718 	    ahci_portp->ahciport_port_num, slot_status);
8719 
8720 	AHCIDBG(AHCIDBG_ERRS|AHCIDBG_ENTRY, ahci_ctlp,
8721 	    "ahci_mop_commands: failed_tags: 0x%x, "
8722 	    "timeout_tags: 0x%x aborted_tags: 0x%x, "
8723 	    "reset_tags: 0x%x", failed_tags,
8724 	    timeout_tags, aborted_tags, reset_tags);
8725 
8726 #ifdef AHCI_DEBUG
8727 	if (ahci_debug_flags & AHCIDBG_ERRS) {
8728 		int i;
8729 		char msg_buf[200] = {0, };
8730 		for (i = 0x1f; i >= 0; i--) {
8731 			if (ahci_portp->ahciport_slot_pkts[i] != NULL)
8732 				msg_buf[i] = 'X';
8733 			else
8734 				msg_buf[i] = '.';
8735 		}
8736 		msg_buf[0x20] = '\0';
8737 		cmn_err(CE_NOTE, "port[%d] slots: %s",
8738 		    ahci_portp->ahciport_port_num, msg_buf);
8739 		cmn_err(CE_NOTE, "[ERR-RT] %p [RW-PM] %p ",
8740 		    (void *)ahci_portp->ahciport_err_retri_pkt,
8741 		    (void *)ahci_portp->ahciport_rdwr_pmult_pkt);
8742 	}
8743 #endif
8744 
8745 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
8746 		finished_tags = ahci_portp->ahciport_pending_tags &
8747 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
8748 
8749 		unfinished_tags = slot_status &
8750 		    AHCI_SLOT_MASK(ahci_ctlp) &
8751 		    ~failed_tags &
8752 		    ~aborted_tags &
8753 		    ~reset_tags &
8754 		    ~timeout_tags;
8755 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
8756 		ncq_cmd_in_progress = 1;
8757 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
8758 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
8759 
8760 		unfinished_tags = slot_status &
8761 		    AHCI_NCQ_SLOT_MASK(ahci_portp) &
8762 		    ~failed_tags &
8763 		    ~aborted_tags &
8764 		    ~reset_tags &
8765 		    ~timeout_tags;
8766 	} else if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
8767 
8768 		/*
8769 		 * When AHCI_PORT_FLAG_RQSENSE or AHCI_PORT_FLAG_RDLOGEXT is
8770 		 * set, it means REQUEST SENSE or READ LOG EXT command doesn't
8771 		 * complete successfully due to one of the following three
8772 		 * conditions:
8773 		 *
8774 		 *	1. Fatal error - failed_tags includes its slot
8775 		 *	2. Timed out - timeout_tags includes its slot
8776 		 *	3. Aborted when hot unplug - aborted_tags includes its
8777 		 *	   slot
8778 		 *
8779 		 * Please note that the command is always sent down in Slot 0
8780 		 */
8781 		err_retri_cmd_in_progress = 1;
8782 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_NCQ, ahci_ctlp,
8783 		    "ahci_mop_commands is called for port %d while "
8784 		    "REQUEST SENSE or READ LOG EXT for error retrieval "
8785 		    "is being executed slot_status = 0x%x",
8786 		    ahci_portp->ahciport_port_num, slot_status);
8787 		ASSERT(ahci_portp->ahciport_mop_in_progress > 1);
8788 		ASSERT(slot_status == 0x1);
8789 	} else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
8790 		rdwr_pmult_cmd_in_progress = 1;
8791 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_PMULT, ahci_ctlp,
8792 		    "ahci_mop_commands is called for port %d while "
8793 		    "READ/WRITE PORTMULT command is being executed",
8794 		    ahci_portp->ahciport_port_num);
8795 
8796 		ASSERT(slot_status == 0x1);
8797 	}
8798 
8799 #ifdef AHCI_DEBUG
8800 	AHCIDBG(AHCIDBG_ERRS|AHCIDBG_ENTRY, ahci_ctlp,
8801 	    "ahci_mop_commands: finished_tags: 0x%x, "
8802 	    "unfinished_tags 0x%x", finished_tags, unfinished_tags);
8803 #endif
8804 
8805 	/* Send up finished packets with SATA_PKT_COMPLETED */
8806 	while (finished_tags) {
8807 		tmp_slot = ddi_ffs(finished_tags) - 1;
8808 		if (tmp_slot == -1) {
8809 			break;
8810 		}
8811 
8812 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
8813 		ASSERT(satapkt != NULL);
8814 
8815 		AHCIDBG(AHCIDBG_INFO, ahci_ctlp, "ahci_mop_commands: "
8816 		    "sending up pkt 0x%p with SATA_PKT_COMPLETED",
8817 		    (void *)satapkt);
8818 
8819 		/*
8820 		 * Cannot fetch the return register content since the port
8821 		 * was restarted, so the corresponding tag will be set to
8822 		 * aborted tags.
8823 		 */
8824 		if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs) {
8825 			CLEAR_BIT(finished_tags, tmp_slot);
8826 			aborted_tags |= tmp_slot;
8827 			continue;
8828 		}
8829 
8830 		if (ncq_cmd_in_progress)
8831 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
8832 			    tmp_slot);
8833 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
8834 		CLEAR_BIT(finished_tags, tmp_slot);
8835 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
8836 
8837 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_COMPLETED);
8838 	}
8839 
8840 	/* Send up failed packets with SATA_PKT_DEV_ERROR. */
8841 	while (failed_tags) {
8842 		if (err_retri_cmd_in_progress) {
8843 			satapkt = ahci_portp->ahciport_err_retri_pkt;
8844 			ASSERT(satapkt != NULL);
8845 			ASSERT(failed_tags == 0x1);
8846 
8847 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
8848 			    "sending up pkt 0x%p with SATA_PKT_DEV_ERROR",
8849 			    (void *)satapkt);
8850 			ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_DEV_ERROR);
8851 			break;
8852 		}
8853 		if (rdwr_pmult_cmd_in_progress) {
8854 			satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
8855 			ASSERT(satapkt != NULL);
8856 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
8857 			    "ahci_mop_commands: sending up "
8858 			    "rdwr pmult pkt 0x%p with SATA_PKT_DEV_ERROR",
8859 			    (void *)satapkt);
8860 			ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_DEV_ERROR);
8861 			break;
8862 		}
8863 
8864 		tmp_slot = ddi_ffs(failed_tags) - 1;
8865 		if (tmp_slot == -1) {
8866 			break;
8867 		}
8868 
8869 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
8870 		ASSERT(satapkt != NULL);
8871 
8872 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
8873 		    "sending up pkt 0x%p with SATA_PKT_DEV_ERROR",
8874 		    (void *)satapkt);
8875 
8876 		if (ncq_cmd_in_progress)
8877 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
8878 			    tmp_slot);
8879 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
8880 		CLEAR_BIT(failed_tags, tmp_slot);
8881 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
8882 
8883 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_DEV_ERROR);
8884 	}
8885 
8886 	/* Send up timeout packets with SATA_PKT_TIMEOUT. */
8887 	while (timeout_tags) {
8888 		if (err_retri_cmd_in_progress) {
8889 			satapkt = ahci_portp->ahciport_err_retri_pkt;
8890 			ASSERT(satapkt != NULL);
8891 			ASSERT(timeout_tags == 0x1);
8892 
8893 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
8894 			    "sending up pkt 0x%p with SATA_PKT_TIMEOUT",
8895 			    (void *)satapkt);
8896 			ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_TIMEOUT);
8897 			break;
8898 		}
8899 		if (rdwr_pmult_cmd_in_progress) {
8900 			satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
8901 			ASSERT(satapkt != NULL);
8902 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
8903 			    "ahci_mop_commands: sending up "
8904 			    "rdwr pmult pkt 0x%p with SATA_PKT_TIMEOUT",
8905 			    (void *)satapkt);
8906 			ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_TIMEOUT);
8907 			break;
8908 		}
8909 
8910 		tmp_slot = ddi_ffs(timeout_tags) - 1;
8911 		if (tmp_slot == -1) {
8912 			break;
8913 		}
8914 
8915 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
8916 		ASSERT(satapkt != NULL);
8917 
8918 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
8919 		    "sending up pkt 0x%p with SATA_PKT_TIMEOUT",
8920 		    (void *)satapkt);
8921 
8922 		if (ncq_cmd_in_progress)
8923 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
8924 			    tmp_slot);
8925 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
8926 		CLEAR_BIT(timeout_tags, tmp_slot);
8927 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
8928 
8929 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_TIMEOUT);
8930 	}
8931 
8932 	/* Send up aborted packets with SATA_PKT_ABORTED */
8933 	while (aborted_tags) {
8934 		if (err_retri_cmd_in_progress) {
8935 			satapkt = ahci_portp->ahciport_err_retri_pkt;
8936 			ASSERT(satapkt != NULL);
8937 			ASSERT(aborted_tags == 0x1);
8938 
8939 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
8940 			    "sending up pkt 0x%p with SATA_PKT_ABORTED",
8941 			    (void *)satapkt);
8942 			ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_ABORTED);
8943 			break;
8944 		}
8945 		if (rdwr_pmult_cmd_in_progress) {
8946 			satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
8947 			ASSERT(satapkt != NULL);
8948 			ASSERT(aborted_tags == 0x1);
8949 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
8950 			    "ahci_mop_commands: sending up "
8951 			    "rdwr pmult pkt 0x%p with SATA_PKT_ABORTED",
8952 			    (void *)satapkt);
8953 			ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_ABORTED);
8954 			break;
8955 		}
8956 
8957 		tmp_slot = ddi_ffs(aborted_tags) - 1;
8958 		if (tmp_slot == -1) {
8959 			break;
8960 		}
8961 
8962 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
8963 		ASSERT(satapkt != NULL);
8964 
8965 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
8966 		    "sending up pkt 0x%p with SATA_PKT_ABORTED",
8967 		    (void *)satapkt);
8968 
8969 		if (ncq_cmd_in_progress)
8970 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
8971 			    tmp_slot);
8972 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
8973 		CLEAR_BIT(aborted_tags, tmp_slot);
8974 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
8975 
8976 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_ABORTED);
8977 	}
8978 
8979 	/* Send up reset packets with SATA_PKT_RESET. */
8980 	while (reset_tags) {
8981 		if (rdwr_pmult_cmd_in_progress) {
8982 			satapkt = ahci_portp->ahciport_rdwr_pmult_pkt;
8983 			ASSERT(satapkt != NULL);
8984 			ASSERT(aborted_tags == 0x1);
8985 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
8986 			    "ahci_mop_commands: sending up "
8987 			    "rdwr pmult pkt 0x%p with SATA_PKT_RESET",
8988 			    (void *)satapkt);
8989 			ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_RESET);
8990 			break;
8991 		}
8992 
8993 		tmp_slot = ddi_ffs(reset_tags) - 1;
8994 		if (tmp_slot == -1) {
8995 			break;
8996 		}
8997 
8998 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
8999 		ASSERT(satapkt != NULL);
9000 
9001 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
9002 		    "sending up pkt 0x%p with SATA_PKT_RESET",
9003 		    (void *)satapkt);
9004 
9005 		if (ncq_cmd_in_progress)
9006 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
9007 			    tmp_slot);
9008 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
9009 		CLEAR_BIT(reset_tags, tmp_slot);
9010 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
9011 
9012 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_RESET);
9013 	}
9014 
9015 	/* Send up unfinished packets with SATA_PKT_RESET */
9016 	while (unfinished_tags) {
9017 		tmp_slot = ddi_ffs(unfinished_tags) - 1;
9018 		if (tmp_slot == -1) {
9019 			break;
9020 		}
9021 
9022 		satapkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
9023 		ASSERT(satapkt != NULL);
9024 
9025 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, "ahci_mop_commands: "
9026 		    "sending up pkt 0x%p with SATA_PKT_RESET",
9027 		    (void *)satapkt);
9028 
9029 		if (ncq_cmd_in_progress)
9030 			CLEAR_BIT(ahci_portp->ahciport_pending_ncq_tags,
9031 			    tmp_slot);
9032 		CLEAR_BIT(ahci_portp->ahciport_pending_tags, tmp_slot);
9033 		CLEAR_BIT(unfinished_tags, tmp_slot);
9034 		ahci_portp->ahciport_slot_pkts[tmp_slot] = NULL;
9035 
9036 		ahci_add_doneq(ahci_portp, satapkt, SATA_PKT_RESET);
9037 	}
9038 
9039 	ahci_portp->ahciport_mop_in_progress--;
9040 	ASSERT(ahci_portp->ahciport_mop_in_progress >= 0);
9041 
9042 	if (ahci_portp->ahciport_mop_in_progress == 0)
9043 		ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_MOPPING;
9044 
9045 	ahci_flush_doneq(ahci_portp);
9046 }
9047 
9048 /*
9049  * This routine is going to first request a READ LOG EXT sata pkt from sata
9050  * module, and then deliver it to the HBA to get the ncq failure context.
9051  * The return value is the exactly failed tags.
9052  */
9053 static uint32_t
9054 ahci_get_rdlogext_data(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
9055     uint8_t port)
9056 {
9057 	sata_device_t	sdevice;
9058 	sata_pkt_t	*rdlog_spkt, *spkt;
9059 	ddi_dma_handle_t buf_dma_handle;
9060 	ahci_addr_t	addr;
9061 	int		loop_count;
9062 	int		rval;
9063 	int		failed_slot;
9064 	uint32_t	failed_tags = 0;
9065 	struct sata_ncq_error_recovery_page *ncq_err_page;
9066 
9067 	AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_NCQ, ahci_ctlp,
9068 	    "ahci_get_rdlogext_data enter: port %d", port);
9069 
9070 	/* Prepare the sdevice data */
9071 	bzero((void *)&sdevice, sizeof (sata_device_t));
9072 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
9073 
9074 	sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
9075 	sdevice.satadev_addr.pmport = 0;
9076 
9077 	/* Translate sata_device.satadev_addr -> ahci_addr */
9078 	ahci_get_ahci_addr(ahci_ctlp, &sdevice, &addr);
9079 
9080 	/*
9081 	 * Call the sata hba interface to get a rdlog spkt
9082 	 */
9083 	loop_count = 0;
9084 loop:
9085 	rdlog_spkt = sata_get_error_retrieval_pkt(ahci_ctlp->ahcictl_dip,
9086 	    &sdevice, SATA_ERR_RETR_PKT_TYPE_NCQ);
9087 	if (rdlog_spkt == NULL) {
9088 		if (loop_count++ < AHCI_POLLRATE_GET_SPKT) {
9089 			/* Sleep for a while */
9090 			drv_usecwait(AHCI_10MS_USECS);
9091 			goto loop;
9092 		}
9093 		/* Timed out after 1s */
9094 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
9095 		    "failed to get rdlog spkt for port %d", port);
9096 		return (failed_tags);
9097 	}
9098 
9099 	ASSERT(rdlog_spkt->satapkt_op_mode & SATA_OPMODE_SYNCH);
9100 
9101 	/*
9102 	 * This flag is used to handle the specific error recovery when the
9103 	 * READ LOG EXT command gets a failure (fatal error or time-out).
9104 	 */
9105 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_RDLOGEXT;
9106 
9107 	/*
9108 	 * This start is not supposed to fail because after port is restarted,
9109 	 * the whole command list is empty.
9110 	 */
9111 	ahci_portp->ahciport_err_retri_pkt = rdlog_spkt;
9112 	(void) ahci_do_sync_start(ahci_ctlp, ahci_portp, &addr, rdlog_spkt);
9113 	ahci_portp->ahciport_err_retri_pkt = NULL;
9114 
9115 	/* Remove the flag after READ LOG EXT command is completed */
9116 	ahci_portp->ahciport_flags &= ~ AHCI_PORT_FLAG_RDLOGEXT;
9117 
9118 	if (rdlog_spkt->satapkt_reason == SATA_PKT_COMPLETED) {
9119 		/* Update the request log data */
9120 		buf_dma_handle = *(ddi_dma_handle_t *)
9121 		    (rdlog_spkt->satapkt_cmd.satacmd_err_ret_buf_handle);
9122 		rval = ddi_dma_sync(buf_dma_handle, 0, 0,
9123 		    DDI_DMA_SYNC_FORKERNEL);
9124 		if (rval == DDI_SUCCESS) {
9125 			ncq_err_page =
9126 			    (struct sata_ncq_error_recovery_page *)rdlog_spkt->
9127 			    satapkt_cmd.satacmd_bp->b_un.b_addr;
9128 
9129 			/* Get the failed tag */
9130 			failed_slot = ncq_err_page->ncq_tag;
9131 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
9132 			    "ahci_get_rdlogext_data: port %d "
9133 			    "failed slot %d", port, failed_slot);
9134 			if (failed_slot & NQ) {
9135 				AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
9136 				    "the failed slot is not a valid tag", NULL);
9137 				goto out;
9138 			}
9139 
9140 			failed_slot &= NCQ_TAG_MASK;
9141 			spkt = ahci_portp->ahciport_slot_pkts[failed_slot];
9142 			AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
9143 			    "ahci_get_rdlogext_data: failed spkt 0x%p",
9144 			    (void *)spkt);
9145 			if (spkt == NULL) {
9146 				AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
9147 				    "the failed slot spkt is NULL", NULL);
9148 				goto out;
9149 			}
9150 
9151 			failed_tags = 0x1 << failed_slot;
9152 
9153 			/* Fill out the error context */
9154 			ahci_copy_ncq_err_page(&spkt->satapkt_cmd,
9155 			    ncq_err_page);
9156 			ahci_update_sata_registers(ahci_ctlp, port,
9157 			    &spkt->satapkt_device);
9158 		}
9159 	}
9160 out:
9161 	sata_free_error_retrieval_pkt(rdlog_spkt);
9162 
9163 	return (failed_tags);
9164 }
9165 
9166 /*
9167  * This routine is going to first request a REQUEST SENSE sata pkt from sata
9168  * module, and then deliver it to the HBA to get the sense data and copy
9169  * the sense data back to the orignal failed sata pkt, and free the REQUEST
9170  * SENSE sata pkt later.
9171  */
9172 static void
9173 ahci_get_rqsense_data(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
9174     uint8_t port, sata_pkt_t *spkt)
9175 {
9176 	sata_device_t	sdevice;
9177 	sata_pkt_t	*rs_spkt;
9178 	sata_cmd_t	*sata_cmd;
9179 	ddi_dma_handle_t buf_dma_handle;
9180 	ahci_addr_t	addr;
9181 	int		loop_count;
9182 #if AHCI_DEBUG
9183 	struct scsi_extended_sense *rqsense;
9184 #endif
9185 
9186 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
9187 	    "ahci_get_rqsense_data enter: port %d", port);
9188 
9189 	/* Prepare the sdevice data */
9190 	bzero((void *)&sdevice, sizeof (sata_device_t));
9191 	sdevice.satadev_addr.cport = ahci_ctlp->ahcictl_port_to_cport[port];
9192 
9193 	sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
9194 	sdevice.satadev_addr.pmport = 0;
9195 
9196 	/* Translate sata_device.satadev_addr -> ahci_addr */
9197 	ahci_get_ahci_addr(ahci_ctlp, &sdevice, &addr);
9198 
9199 	sata_cmd = &spkt->satapkt_cmd;
9200 
9201 	/*
9202 	 * Call the sata hba interface to get a rs spkt
9203 	 */
9204 	loop_count = 0;
9205 loop:
9206 	rs_spkt = sata_get_error_retrieval_pkt(ahci_ctlp->ahcictl_dip,
9207 	    &sdevice, SATA_ERR_RETR_PKT_TYPE_ATAPI);
9208 	if (rs_spkt == NULL) {
9209 		if (loop_count++ < AHCI_POLLRATE_GET_SPKT) {
9210 			/* Sleep for a while */
9211 			drv_usecwait(AHCI_10MS_USECS);
9212 			goto loop;
9213 
9214 		}
9215 		/* Timed out after 1s */
9216 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
9217 		    "failed to get rs spkt for port %d", port);
9218 		return;
9219 	}
9220 
9221 	ASSERT(rs_spkt->satapkt_op_mode & SATA_OPMODE_SYNCH);
9222 
9223 	/*
9224 	 * This flag is used to handle the specific error recovery when the
9225 	 * REQUEST SENSE command gets a faiure (fatal error or time-out).
9226 	 */
9227 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_RQSENSE;
9228 
9229 	/*
9230 	 * This start is not supposed to fail because after port is restarted,
9231 	 * the whole command list is empty.
9232 	 */
9233 	ahci_portp->ahciport_err_retri_pkt = rs_spkt;
9234 	(void) ahci_do_sync_start(ahci_ctlp, ahci_portp, &addr, rs_spkt);
9235 	ahci_portp->ahciport_err_retri_pkt = NULL;
9236 
9237 	/* Remove the flag after REQUEST SENSE command is completed */
9238 	ahci_portp->ahciport_flags &= ~ AHCI_PORT_FLAG_RQSENSE;
9239 
9240 	if (rs_spkt->satapkt_reason == SATA_PKT_COMPLETED) {
9241 		/* Update the request sense data */
9242 		buf_dma_handle = *(ddi_dma_handle_t *)
9243 		    (rs_spkt->satapkt_cmd.satacmd_err_ret_buf_handle);
9244 		(void) ddi_dma_sync(buf_dma_handle, 0, 0,
9245 		    DDI_DMA_SYNC_FORKERNEL);
9246 		/* Copy the request sense data */
9247 		bcopy(rs_spkt->
9248 		    satapkt_cmd.satacmd_bp->b_un.b_addr,
9249 		    &sata_cmd->satacmd_rqsense,
9250 		    SATA_ATAPI_MIN_RQSENSE_LEN);
9251 #if AHCI_DEBUG
9252 		rqsense = (struct scsi_extended_sense *)
9253 		    sata_cmd->satacmd_rqsense;
9254 
9255 		/* Dump the sense data */
9256 		AHCIDBG(AHCIDBG_SENSEDATA, ahci_ctlp, "\n", NULL);
9257 		AHCIDBG(AHCIDBG_SENSEDATA, ahci_ctlp,
9258 		    "Sense data for satapkt %p ATAPI cmd 0x%x",
9259 		    spkt, sata_cmd->satacmd_acdb[0]);
9260 		AHCIDBG(AHCIDBG_SENSEDATA, ahci_ctlp,
9261 		    "  es_code 0x%x es_class 0x%x "
9262 		    "es_key 0x%x es_add_code 0x%x "
9263 		    "es_qual_code 0x%x",
9264 		    rqsense->es_code, rqsense->es_class,
9265 		    rqsense->es_key, rqsense->es_add_code,
9266 		    rqsense->es_qual_code);
9267 #endif
9268 	}
9269 
9270 	sata_free_error_retrieval_pkt(rs_spkt);
9271 }
9272 
9273 /*
9274  * Fatal errors will cause the HBA to enter the ERR: Fatal state. To recover,
9275  * the port must be restarted. When the HBA detects thus error, it may try
9276  * to abort a transfer. And if the transfer was aborted, the device is
9277  * expected to send a D2H Register FIS with PxTFD.STS.ERR set to '1' and both
9278  * PxTFD.STS.BSY and PxTFD.STS.DRQ cleared to '0'. Then system software knows
9279  * that the device is in a stable status and transfers may be restarted without
9280  * issuing a COMRESET to the device. If PxTFD.STS.BSY or PxTFD.STS.DRQ is set,
9281  * then the software will send the COMRESET to do the port reset.
9282  *
9283  * Software should perform the appropriate error recovery actions based on
9284  * whether non-queued commands were being issued or natived command queuing
9285  * commands were being issued.
9286  *
9287  * And software will complete the command that had the error with error mark
9288  * to higher level software.
9289  *
9290  * Fatal errors include the following:
9291  *	PxIS.IFS - Interface Fatal Error Status
9292  *	PxIS.HBDS - Host Bus Data Error Status
9293  *	PxIS.HBFS - Host Bus Fatal Error Status
9294  *	PxIS.TFES - Task File Error Status
9295  *
9296  * WARNING!!! ahciport_mutex should be acquired before the function is called.
9297  */
9298 static void
9299 ahci_fatal_error_recovery_handler(ahci_ctl_t *ahci_ctlp,
9300     ahci_port_t *ahci_portp, ahci_addr_t *addrp, uint32_t intr_status)
9301 {
9302 	uint32_t	port_cmd_status;
9303 	uint32_t	slot_status = 0;
9304 	uint32_t	failed_tags = 0;
9305 	int		failed_slot;
9306 	int		reset_flag = 0, flag = 0;
9307 	ahci_fis_d2h_register_t	*ahci_rcvd_fisp;
9308 	sata_cmd_t	*sata_cmd = NULL;
9309 	sata_pkt_t	*spkt = NULL;
9310 #if AHCI_DEBUG
9311 	ahci_cmd_header_t *cmd_header;
9312 #endif
9313 	uint8_t		port = addrp->aa_port;
9314 	int		instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
9315 	int		rval;
9316 
9317 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
9318 	    "ahci_fatal_error_recovery_handler enter: port %d", port);
9319 
9320 	/* Port multiplier error */
9321 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_PMULT) {
9322 		/* FBS code is neither completed nor tested. */
9323 		ahci_pmult_error_recovery_handler(ahci_ctlp, ahci_portp,
9324 		    port, intr_status);
9325 
9326 		/* Force a port reset */
9327 		flag = AHCI_PORT_RESET;
9328 	}
9329 
9330 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
9331 	    ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
9332 
9333 		/* Read PxCI to see which commands are still outstanding */
9334 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
9335 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
9336 
9337 		/*
9338 		 * Read PxCMD.CCS to determine the slot that the HBA
9339 		 * was processing when the error occurred.
9340 		 */
9341 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
9342 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
9343 		failed_slot = (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
9344 		    AHCI_CMD_STATUS_CCS_SHIFT;
9345 
9346 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
9347 			spkt = ahci_portp->ahciport_err_retri_pkt;
9348 			ASSERT(spkt != NULL);
9349 		} else {
9350 			spkt = ahci_portp->ahciport_slot_pkts[failed_slot];
9351 			if (spkt == NULL) {
9352 				/* May happen when interface errors occur? */
9353 				goto next;
9354 			}
9355 		}
9356 
9357 #if AHCI_DEBUG
9358 		/*
9359 		 * Debugging purpose...
9360 		 */
9361 		if (ahci_portp->ahciport_prd_bytecounts[failed_slot]) {
9362 			cmd_header =
9363 			    &ahci_portp->ahciport_cmd_list[failed_slot];
9364 			AHCIDBG(AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
9365 			    "ahci_fatal_error_recovery_handler: port %d, "
9366 			    "PRD Byte Count = 0x%x, "
9367 			    "ahciport_prd_bytecounts = 0x%x", port,
9368 			    cmd_header->ahcich_prd_byte_count,
9369 			    ahci_portp->ahciport_prd_bytecounts[failed_slot]);
9370 		}
9371 #endif
9372 
9373 		sata_cmd = &spkt->satapkt_cmd;
9374 
9375 		/* Fill out the status and error registers for PxIS.TFES */
9376 		if (intr_status & AHCI_INTR_STATUS_TFES) {
9377 			ahci_rcvd_fisp = &(ahci_portp->ahciport_rcvd_fis->
9378 			    ahcirf_d2h_register_fis);
9379 
9380 			/* Copy the error context back to the sata_cmd */
9381 			ahci_copy_err_cnxt(sata_cmd, ahci_rcvd_fisp);
9382 		}
9383 
9384 		/* The failed command must be one of the outstanding commands */
9385 		failed_tags = 0x1 << failed_slot;
9386 		ASSERT(failed_tags & slot_status);
9387 
9388 		/* Update the sata registers, especially PxSERR register */
9389 		ahci_update_sata_registers(ahci_ctlp, port,
9390 		    &spkt->satapkt_device);
9391 
9392 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
9393 		/* Read PxSACT to see which commands are still outstanding */
9394 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
9395 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
9396 	}
9397 next:
9398 
9399 #if AHCI_DEBUG
9400 	/*
9401 	 * When AHCI_PORT_FLAG_RQSENSE or AHCI_PORT_FLAG_RDLOGEXT flag is
9402 	 * set, it means a fatal error happened after REQUEST SENSE command
9403 	 * or READ LOG EXT command is delivered to the HBA during the error
9404 	 * recovery process. At this time, the only outstanding command is
9405 	 * supposed to be REQUEST SENSE command or READ LOG EXT command.
9406 	 */
9407 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
9408 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
9409 		    "ahci_fatal_error_recovery_handler: port %d REQUEST SENSE "
9410 		    "command or READ LOG EXT command for error data retrieval "
9411 		    "failed", port);
9412 		ASSERT(slot_status == 0x1);
9413 		ASSERT(failed_slot == 0);
9414 		ASSERT(spkt->satapkt_cmd.satacmd_acdb[0] ==
9415 		    SCMD_REQUEST_SENSE ||
9416 		    spkt->satapkt_cmd.satacmd_cmd_reg ==
9417 		    SATAC_READ_LOG_EXT);
9418 	}
9419 #endif
9420 
9421 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
9422 	ahci_portp->ahciport_mop_in_progress++;
9423 
9424 	rval = ahci_restart_port_wait_till_ready(ahci_ctlp, ahci_portp,
9425 	    port, flag, &reset_flag);
9426 
9427 	if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_ERRPRINT) {
9428 		ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_ERRPRINT;
9429 		if (rval == AHCI_SUCCESS)
9430 			cmn_err(CE_WARN, "!ahci%d: error recovery for port %d "
9431 			    "succeed", instance, port);
9432 		else
9433 			cmn_err(CE_WARN, "!ahci%d: error recovery for port %d "
9434 			    "failed", instance, port);
9435 	}
9436 
9437 	/*
9438 	 * Won't retrieve error information:
9439 	 * 1. Port reset was involved to recover
9440 	 * 2. Device is gone
9441 	 * 3. IDENTIFY DEVICE command sent to ATAPI device
9442 	 * 4. REQUEST SENSE or READ LOG EXT command during error recovery
9443 	 */
9444 	if (reset_flag ||
9445 	    ahci_portp->ahciport_device_type == SATA_DTYPE_NONE ||
9446 	    spkt && spkt->satapkt_cmd.satacmd_cmd_reg == SATAC_ID_DEVICE ||
9447 	    ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
9448 		goto out;
9449 
9450 	/*
9451 	 * Deliver READ LOG EXT to gather information about the error when
9452 	 * a COMRESET has not been performed as part of the error recovery
9453 	 * during NCQ command processing.
9454 	 */
9455 	if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
9456 		failed_tags = ahci_get_rdlogext_data(ahci_ctlp,
9457 		    ahci_portp, port);
9458 		goto out;
9459 	}
9460 
9461 	/*
9462 	 * Deliver REQUEST SENSE for ATAPI command to gather information about
9463 	 * the error when a COMRESET has not been performed as part of the
9464 	 * error recovery.
9465 	 */
9466 	if (spkt && ahci_portp->ahciport_device_type == SATA_DTYPE_ATAPI)
9467 		ahci_get_rqsense_data(ahci_ctlp, ahci_portp, port, spkt);
9468 out:
9469 	AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
9470 	    "ahci_fatal_error_recovery_handler: port %d fatal error "
9471 	    "occurred slot_status = 0x%x, pending_tags = 0x%x, "
9472 	    "pending_ncq_tags = 0x%x failed_tags = 0x%x",
9473 	    port, slot_status, ahci_portp->ahciport_pending_tags,
9474 	    ahci_portp->ahciport_pending_ncq_tags, failed_tags);
9475 
9476 	ahci_mop_commands(ahci_ctlp,
9477 	    ahci_portp,
9478 	    slot_status,
9479 	    failed_tags, /* failed tags */
9480 	    0, /* timeout tags */
9481 	    0, /* aborted tags */
9482 	    0); /* reset tags */
9483 }
9484 
9485 /*
9486  * Used to recovery a PMULT pmport fatal error under FIS-based switching.
9487  * 	1. device specific.PxFBS.SDE=1
9488  * 	2. Non-Deivce specific.
9489  * Nothing will be done when Command-based switching is employed.
9490  *
9491  * Currently code is neither completed nor tested.
9492  *
9493  * WARNING!!! ahciport_mutex should be acquired before the function
9494  * is called.
9495  */
9496 static void
9497 ahci_pmult_error_recovery_handler(ahci_ctl_t *ahci_ctlp,
9498     ahci_port_t *ahci_portp, uint8_t port, uint32_t intr_status)
9499 {
9500 #ifndef __lock_lint
9501 	_NOTE(ARGUNUSED(intr_status))
9502 #endif
9503 	uint32_t	port_fbs_ctrl;
9504 	int loop_count = 0;
9505 	ahci_addr_t	addr;
9506 
9507 	/* Nothing will be done under Command-based switching. */
9508 	if (!(ahci_ctlp->ahcictl_cap & AHCI_CAP_PMULT_FBSS))
9509 		return;
9510 
9511 	port_fbs_ctrl = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
9512 	    (uint32_t *)AHCI_PORT_PxFBS(ahci_ctlp, port));
9513 
9514 	if (!(port_fbs_ctrl & AHCI_FBS_EN))
9515 		/* FBS is not enabled. */
9516 		return;
9517 
9518 	/* Problem's getting complicated now. */
9519 	/*
9520 	 * If FIS-based switching is used, we need to check
9521 	 * the PxFBS to see the error type.
9522 	 */
9523 	port_fbs_ctrl = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
9524 	    (uint32_t *)AHCI_PORT_PxFBS(ahci_ctlp, port));
9525 
9526 	/* Refer to spec(v1.2) 9.3.6.1 */
9527 	if (port_fbs_ctrl & AHCI_FBS_SDE) {
9528 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp,
9529 		    "A Device Sepcific Error: port %d", port);
9530 		/*
9531 		 * Controller has paused commands for all other
9532 		 * sub-devices until PxFBS.DEC is set.
9533 		 */
9534 		ahci_reject_all_abort_pkts(ahci_ctlp,
9535 		    ahci_portp, 0);
9536 
9537 		ddi_put32(ahci_ctlp->ahcictl_ahci_acc_handle,
9538 		    (uint32_t *)AHCI_PORT_PxFBS(ahci_ctlp, port),
9539 		    port_fbs_ctrl | AHCI_FBS_DEC);
9540 
9541 		/*
9542 		 * Wait controller clear PxFBS.DEC,
9543 		 * then we can continue.
9544 		 */
9545 		loop_count = 0;
9546 		do {
9547 			port_fbs_ctrl = ddi_get32(ahci_ctlp->
9548 			    ahcictl_ahci_acc_handle, (uint32_t *)
9549 			    AHCI_PORT_PxFBS(ahci_ctlp, port));
9550 
9551 			if (loop_count++ > 1000)
9552 				/*
9553 				 * Esclate the error. Follow
9554 				 * non-device specific error
9555 				 * procedure.
9556 				 */
9557 				return;
9558 
9559 			drv_usecwait(AHCI_100US_USECS);
9560 		} while (port_fbs_ctrl & AHCI_FBS_DEC);
9561 
9562 		/*
9563 		 * Issue a software reset to ensure drive is in
9564 		 * a known state.
9565 		 */
9566 		(void) ahci_software_reset(ahci_ctlp,
9567 		    ahci_portp, &addr);
9568 
9569 	} else {
9570 
9571 		/* Process Non-Device Specific Error. */
9572 		/* This will be handled later on. */
9573 		cmn_err(CE_NOTE, "!FBS is not supported now.");
9574 	}
9575 }
9576 /*
9577  * Handle events - fatal error recovery
9578  */
9579 static void
9580 ahci_events_handler(void *args)
9581 {
9582 	ahci_event_arg_t *ahci_event_arg;
9583 	ahci_ctl_t *ahci_ctlp;
9584 	ahci_port_t *ahci_portp;
9585 	ahci_addr_t *addrp;
9586 	uint32_t event;
9587 	int instance;
9588 
9589 	ahci_event_arg = (ahci_event_arg_t *)args;
9590 
9591 	ahci_ctlp = ahci_event_arg->ahciea_ctlp;
9592 	ahci_portp = ahci_event_arg->ahciea_portp;
9593 	addrp = ahci_event_arg->ahciea_addrp;
9594 	event = ahci_event_arg->ahciea_event;
9595 	instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
9596 
9597 	AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR|AHCIDBG_ERRS, ahci_ctlp,
9598 	    "ahci_events_handler enter: port %d intr_status = 0x%x",
9599 	    ahci_portp->ahciport_port_num, event);
9600 
9601 	mutex_enter(&ahci_portp->ahciport_mutex);
9602 
9603 	/*
9604 	 * ahci_intr_phyrdy_change() may have rendered it to
9605 	 * SATA_DTYPE_NONE.
9606 	 */
9607 	if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
9608 		AHCIDBG(AHCIDBG_ENTRY|AHCIDBG_INTR, ahci_ctlp,
9609 		    "ahci_events_handler: port %d no device attached, "
9610 		    "and just return without doing anything",
9611 		    ahci_portp->ahciport_port_num);
9612 
9613 		if (ahci_portp->ahciport_flags & AHCI_PORT_FLAG_ERRPRINT) {
9614 			ahci_portp->ahciport_flags &= ~AHCI_PORT_FLAG_ERRPRINT;
9615 			cmn_err(CE_WARN, "!ahci%d: error recovery for port %d "
9616 			    "succeed", instance, ahci_portp->ahciport_port_num);
9617 		}
9618 
9619 		goto out;
9620 	}
9621 
9622 	if (event & (AHCI_INTR_STATUS_IFS |
9623 	    AHCI_INTR_STATUS_HBDS |
9624 	    AHCI_INTR_STATUS_HBFS |
9625 	    AHCI_INTR_STATUS_TFES))
9626 		ahci_fatal_error_recovery_handler(ahci_ctlp, ahci_portp,
9627 		    addrp, event);
9628 
9629 out:
9630 	mutex_exit(&ahci_portp->ahciport_mutex);
9631 }
9632 
9633 /*
9634  * ahci_watchdog_handler() and ahci_do_sync_start will call us if they
9635  * detect there are some commands which are timed out.
9636  */
9637 static void
9638 ahci_timeout_pkts(ahci_ctl_t *ahci_ctlp, ahci_port_t *ahci_portp,
9639     uint8_t port, uint32_t tmp_timeout_tags)
9640 {
9641 	uint32_t slot_status = 0;
9642 	uint32_t finished_tags = 0;
9643 	uint32_t timeout_tags = 0;
9644 
9645 	AHCIDBG(AHCIDBG_TIMEOUT|AHCIDBG_ENTRY, ahci_ctlp,
9646 	    "ahci_timeout_pkts enter: port %d", port);
9647 
9648 	mutex_enter(&ahci_portp->ahciport_mutex);
9649 
9650 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) ||
9651 	    RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp) ||
9652 	    ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
9653 		/* Read PxCI to see which commands are still outstanding */
9654 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
9655 		    (uint32_t *)AHCI_PORT_PxCI(ahci_ctlp, port));
9656 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
9657 		/* Read PxSACT to see which commands are still outstanding */
9658 		slot_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
9659 		    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
9660 	}
9661 
9662 #if AHCI_DEBUG
9663 	/*
9664 	 * When AHCI_PORT_FLAG_RQSENSE or AHCI_PORT_FLAG_RDLOGEXT flag is
9665 	 * set, it means a fatal error happened after REQUEST SENSE command
9666 	 * or READ LOG EXT command is delivered to the HBA during the error
9667 	 * recovery process. At this time, the only outstanding command is
9668 	 * supposed to be REQUEST SENSE command or READ LOG EXT command.
9669 	 */
9670 	if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp)) {
9671 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_TIMEOUT, ahci_ctlp,
9672 		    "ahci_timeout_pkts called while REQUEST SENSE "
9673 		    "command or READ LOG EXT command for error recovery "
9674 		    "timed out timeout_tags = 0x%x, slot_status = 0x%x, "
9675 		    "pending_tags = 0x%x, pending_ncq_tags = 0x%x",
9676 		    tmp_timeout_tags, slot_status,
9677 		    ahci_portp->ahciport_pending_tags,
9678 		    ahci_portp->ahciport_pending_ncq_tags);
9679 		ASSERT(slot_status == 0x1);
9680 	} else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
9681 		AHCIDBG(AHCIDBG_ERRS|AHCIDBG_TIMEOUT, ahci_ctlp,
9682 		    "ahci_timeout_pkts called while executing R/W PMULT "
9683 		    "command timeout_tags = 0x%x, slot_status = 0x%x",
9684 		    tmp_timeout_tags, slot_status);
9685 		ASSERT(slot_status == 0x1);
9686 	}
9687 #endif
9688 
9689 	ahci_portp->ahciport_flags |= AHCI_PORT_FLAG_MOPPING;
9690 	ahci_portp->ahciport_mop_in_progress++;
9691 
9692 	(void) ahci_restart_port_wait_till_ready(ahci_ctlp, ahci_portp,
9693 	    port, AHCI_PORT_RESET, NULL);
9694 
9695 	/*
9696 	 * Re-identify timeout tags because some previously checked commands
9697 	 * could already complete.
9698 	 */
9699 	if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
9700 		finished_tags = ahci_portp->ahciport_pending_tags &
9701 		    ~slot_status & AHCI_SLOT_MASK(ahci_ctlp);
9702 		timeout_tags = tmp_timeout_tags & ~finished_tags;
9703 
9704 		AHCIDBG(AHCIDBG_TIMEOUT, ahci_ctlp,
9705 		    "ahci_timeout_pkts: port %d, finished_tags = 0x%x, "
9706 		    "timeout_tags = 0x%x, port_cmd_issue = 0x%x, "
9707 		    "pending_tags = 0x%x ",
9708 		    port, finished_tags, timeout_tags,
9709 		    slot_status, ahci_portp->ahciport_pending_tags);
9710 	} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
9711 		finished_tags = ahci_portp->ahciport_pending_ncq_tags &
9712 		    ~slot_status & AHCI_NCQ_SLOT_MASK(ahci_portp);
9713 		timeout_tags = tmp_timeout_tags & ~finished_tags;
9714 
9715 		AHCIDBG(AHCIDBG_TIMEOUT|AHCIDBG_NCQ, ahci_ctlp,
9716 		    "ahci_timeout_pkts: port %d, finished_tags = 0x%x, "
9717 		    "timeout_tags = 0x%x, port_sactive = 0x%x, "
9718 		    "pending_ncq_tags = 0x%x ",
9719 		    port, finished_tags, timeout_tags,
9720 		    slot_status, ahci_portp->ahciport_pending_ncq_tags);
9721 	} else if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) ||
9722 	    RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
9723 		timeout_tags = tmp_timeout_tags;
9724 	}
9725 
9726 	ahci_mop_commands(ahci_ctlp,
9727 	    ahci_portp,
9728 	    slot_status,
9729 	    0,			/* failed tags */
9730 	    timeout_tags,	/* timeout tags */
9731 	    0,			/* aborted tags */
9732 	    0);			/* reset tags */
9733 
9734 	mutex_exit(&ahci_portp->ahciport_mutex);
9735 }
9736 
9737 /*
9738  * Watchdog handler kicks in every 5 seconds to timeout any commands pending
9739  * for long time.
9740  */
9741 static void
9742 ahci_watchdog_handler(ahci_ctl_t *ahci_ctlp)
9743 {
9744 	ahci_port_t *ahci_portp;
9745 	sata_pkt_t *spkt;
9746 	uint32_t pending_tags;
9747 	uint32_t timeout_tags;
9748 	uint32_t port_cmd_status;
9749 	uint32_t port_sactive;
9750 	uint8_t port;
9751 	int tmp_slot;
9752 	int current_slot;
9753 	uint32_t current_tags;
9754 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
9755 
9756 	mutex_enter(&ahci_ctlp->ahcictl_mutex);
9757 
9758 	AHCIDBG(AHCIDBG_ENTRY, ahci_ctlp,
9759 	    "ahci_watchdog_handler entered", NULL);
9760 
9761 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
9762 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
9763 			continue;
9764 		}
9765 
9766 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
9767 
9768 		mutex_enter(&ahci_portp->ahciport_mutex);
9769 		if (ahci_portp->ahciport_device_type == SATA_DTYPE_NONE) {
9770 			mutex_exit(&ahci_portp->ahciport_mutex);
9771 			continue;
9772 		}
9773 
9774 		/* Skip the check for those ports in error recovery */
9775 		if ((ahci_portp->ahciport_flags & AHCI_PORT_FLAG_MOPPING) &&
9776 		    !(ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))) {
9777 			mutex_exit(&ahci_portp->ahciport_mutex);
9778 			continue;
9779 		}
9780 
9781 		pending_tags = 0;
9782 		port_cmd_status = ddi_get32(ahci_ctlp->ahcictl_ahci_acc_handle,
9783 		    (uint32_t *)AHCI_PORT_PxCMD(ahci_ctlp, port));
9784 
9785 		if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp) ||
9786 		    RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp)) {
9787 			current_slot = 0;
9788 			pending_tags = 0x1;
9789 		} else if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp)) {
9790 			current_slot =
9791 			    (port_cmd_status & AHCI_CMD_STATUS_CCS) >>
9792 			    AHCI_CMD_STATUS_CCS_SHIFT;
9793 			pending_tags = ahci_portp->ahciport_pending_tags;
9794 		} else if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
9795 			port_sactive = ddi_get32(
9796 			    ahci_ctlp->ahcictl_ahci_acc_handle,
9797 			    (uint32_t *)AHCI_PORT_PxSACT(ahci_ctlp, port));
9798 			current_tags = port_sactive &
9799 			    ~port_cmd_status &
9800 			    AHCI_NCQ_SLOT_MASK(ahci_portp);
9801 			pending_tags = ahci_portp->ahciport_pending_ncq_tags;
9802 		}
9803 
9804 		timeout_tags = 0;
9805 		while (pending_tags) {
9806 			tmp_slot = ddi_ffs(pending_tags) - 1;
9807 			if (tmp_slot == -1) {
9808 				break;
9809 			}
9810 
9811 			if (ERR_RETRI_CMD_IN_PROGRESS(ahci_portp))
9812 				spkt = ahci_portp->ahciport_err_retri_pkt;
9813 			else if (RDWR_PMULT_CMD_IN_PROGRESS(ahci_portp))
9814 				spkt = ahci_portp->ahciport_rdwr_pmult_pkt;
9815 			else
9816 				spkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
9817 
9818 			if ((spkt != NULL) && spkt->satapkt_time &&
9819 			    !(spkt->satapkt_op_mode & SATA_OPMODE_POLLING)) {
9820 				/*
9821 				 * If a packet has survived for more than it's
9822 				 * max life cycles, it is a candidate for time
9823 				 * out.
9824 				 */
9825 				ahci_portp->ahciport_slot_timeout[tmp_slot] -=
9826 				    ahci_watchdog_timeout;
9827 
9828 				if (ahci_portp->ahciport_slot_timeout[tmp_slot]
9829 				    > 0)
9830 					goto next;
9831 
9832 #if AHCI_DEBUG
9833 				if (NCQ_CMD_IN_PROGRESS(ahci_portp)) {
9834 					AHCIDBG(AHCIDBG_ERRS|AHCIDBG_TIMEOUT,
9835 					    ahci_ctlp, "watchdog: the current "
9836 					    "tags is 0x%x", current_tags);
9837 				} else {
9838 					AHCIDBG(AHCIDBG_ERRS|AHCIDBG_TIMEOUT,
9839 					    ahci_ctlp, "watchdog: the current "
9840 					    "slot is %d", current_slot);
9841 				}
9842 #endif
9843 
9844 				/*
9845 				 * We need to check whether the HBA has
9846 				 * begun to execute the command, if not,
9847 				 * then re-set the timer of the command.
9848 				 */
9849 				if (NON_NCQ_CMD_IN_PROGRESS(ahci_portp) &&
9850 				    (tmp_slot != current_slot) ||
9851 				    NCQ_CMD_IN_PROGRESS(ahci_portp) &&
9852 				    ((0x1 << tmp_slot) & current_tags)) {
9853 					ahci_portp->ahciport_slot_timeout \
9854 					    [tmp_slot] = spkt->satapkt_time;
9855 				} else {
9856 					timeout_tags |= (0x1 << tmp_slot);
9857 					cmn_err(CE_WARN, "!ahci%d: watchdog "
9858 					    "port %d satapkt 0x%p timed out\n",
9859 					    instance, port, (void *)spkt);
9860 				}
9861 			}
9862 next:
9863 			CLEAR_BIT(pending_tags, tmp_slot);
9864 		}
9865 
9866 		if (timeout_tags) {
9867 			mutex_exit(&ahci_portp->ahciport_mutex);
9868 			mutex_exit(&ahci_ctlp->ahcictl_mutex);
9869 			ahci_timeout_pkts(ahci_ctlp, ahci_portp,
9870 			    port, timeout_tags);
9871 			mutex_enter(&ahci_ctlp->ahcictl_mutex);
9872 			mutex_enter(&ahci_portp->ahciport_mutex);
9873 		}
9874 
9875 		mutex_exit(&ahci_portp->ahciport_mutex);
9876 	}
9877 
9878 	/* Re-install the watchdog timeout handler */
9879 	if (ahci_ctlp->ahcictl_timeout_id != 0) {
9880 		ahci_ctlp->ahcictl_timeout_id =
9881 		    timeout((void (*)(void *))ahci_watchdog_handler,
9882 		    (caddr_t)ahci_ctlp, ahci_watchdog_tick);
9883 	}
9884 
9885 	mutex_exit(&ahci_ctlp->ahcictl_mutex);
9886 }
9887 
9888 /*
9889  * Fill the error context into sata_cmd for non-queued command error.
9890  */
9891 static void
9892 ahci_copy_err_cnxt(sata_cmd_t *scmd, ahci_fis_d2h_register_t *rfisp)
9893 {
9894 	scmd->satacmd_status_reg = GET_RFIS_STATUS(rfisp);
9895 	scmd->satacmd_error_reg = GET_RFIS_ERROR(rfisp);
9896 	scmd->satacmd_sec_count_lsb = GET_RFIS_SECTOR_COUNT(rfisp);
9897 	scmd->satacmd_lba_low_lsb = GET_RFIS_CYL_LOW(rfisp);
9898 	scmd->satacmd_lba_mid_lsb = GET_RFIS_CYL_MID(rfisp);
9899 	scmd->satacmd_lba_high_lsb = GET_RFIS_CYL_HI(rfisp);
9900 	scmd->satacmd_device_reg = GET_RFIS_DEV_HEAD(rfisp);
9901 
9902 	if (scmd->satacmd_addr_type == ATA_ADDR_LBA48) {
9903 		scmd->satacmd_sec_count_msb = GET_RFIS_SECTOR_COUNT_EXP(rfisp);
9904 		scmd->satacmd_lba_low_msb = GET_RFIS_CYL_LOW_EXP(rfisp);
9905 		scmd->satacmd_lba_mid_msb = GET_RFIS_CYL_MID_EXP(rfisp);
9906 		scmd->satacmd_lba_high_msb = GET_RFIS_CYL_HI_EXP(rfisp);
9907 	}
9908 }
9909 
9910 /*
9911  * Fill the ncq error page into sata_cmd for queued command error.
9912  */
9913 static void
9914 ahci_copy_ncq_err_page(sata_cmd_t *scmd,
9915     struct sata_ncq_error_recovery_page *ncq_err_page)
9916 {
9917 	scmd->satacmd_sec_count_msb = ncq_err_page->ncq_sector_count_ext;
9918 	scmd->satacmd_sec_count_lsb = ncq_err_page->ncq_sector_count;
9919 	scmd->satacmd_lba_low_msb = ncq_err_page->ncq_sector_number_ext;
9920 	scmd->satacmd_lba_low_lsb = ncq_err_page->ncq_sector_number;
9921 	scmd->satacmd_lba_mid_msb = ncq_err_page->ncq_cyl_low_ext;
9922 	scmd->satacmd_lba_mid_lsb = ncq_err_page->ncq_cyl_low;
9923 	scmd->satacmd_lba_high_msb = ncq_err_page->ncq_cyl_high_ext;
9924 	scmd->satacmd_lba_high_lsb = ncq_err_page->ncq_cyl_high;
9925 	scmd->satacmd_device_reg = ncq_err_page->ncq_dev_head;
9926 	scmd->satacmd_status_reg = ncq_err_page->ncq_status;
9927 	scmd->satacmd_error_reg = ncq_err_page->ncq_error;
9928 }
9929 
9930 /*
9931  * Put the respective register value to sata_cmd_t for satacmd_flags.
9932  */
9933 static void
9934 ahci_copy_out_regs(sata_cmd_t *scmd, ahci_fis_d2h_register_t *rfisp)
9935 {
9936 	if (scmd->satacmd_flags.sata_copy_out_sec_count_msb)
9937 		scmd->satacmd_sec_count_msb = GET_RFIS_SECTOR_COUNT_EXP(rfisp);
9938 	if (scmd->satacmd_flags.sata_copy_out_lba_low_msb)
9939 		scmd->satacmd_lba_low_msb = GET_RFIS_CYL_LOW_EXP(rfisp);
9940 	if (scmd->satacmd_flags.sata_copy_out_lba_mid_msb)
9941 		scmd->satacmd_lba_mid_msb = GET_RFIS_CYL_MID_EXP(rfisp);
9942 	if (scmd->satacmd_flags.sata_copy_out_lba_high_msb)
9943 		scmd->satacmd_lba_high_msb = GET_RFIS_CYL_HI_EXP(rfisp);
9944 	if (scmd->satacmd_flags.sata_copy_out_sec_count_lsb)
9945 		scmd->satacmd_sec_count_lsb = GET_RFIS_SECTOR_COUNT(rfisp);
9946 	if (scmd->satacmd_flags.sata_copy_out_lba_low_lsb)
9947 		scmd->satacmd_lba_low_lsb = GET_RFIS_CYL_LOW(rfisp);
9948 	if (scmd->satacmd_flags.sata_copy_out_lba_mid_lsb)
9949 		scmd->satacmd_lba_mid_lsb = GET_RFIS_CYL_MID(rfisp);
9950 	if (scmd->satacmd_flags.sata_copy_out_lba_high_lsb)
9951 		scmd->satacmd_lba_high_lsb = GET_RFIS_CYL_HI(rfisp);
9952 	if (scmd->satacmd_flags.sata_copy_out_device_reg)
9953 		scmd->satacmd_device_reg = GET_RFIS_DEV_HEAD(rfisp);
9954 	if (scmd->satacmd_flags.sata_copy_out_error_reg)
9955 		scmd->satacmd_error_reg = GET_RFIS_ERROR(rfisp);
9956 }
9957 
9958 static void
9959 ahci_log_fatal_error_message(ahci_ctl_t *ahci_ctlp, uint8_t port,
9960     uint32_t intr_status)
9961 {
9962 	int instance = ddi_get_instance(ahci_ctlp->ahcictl_dip);
9963 
9964 	if (intr_status & AHCI_INTR_STATUS_IFS)
9965 		cmn_err(CE_WARN, "!ahci%d: ahci port %d has interface fatal "
9966 		    "error", instance, port);
9967 
9968 	if (intr_status & AHCI_INTR_STATUS_HBDS)
9969 		cmn_err(CE_WARN, "!ahci%d: ahci port %d has bus data error",
9970 		    instance, port);
9971 
9972 	if (intr_status & AHCI_INTR_STATUS_HBFS)
9973 		cmn_err(CE_WARN, "!ahci%d: ahci port %d has bus fatal error",
9974 		    instance, port);
9975 
9976 	if (intr_status & AHCI_INTR_STATUS_TFES)
9977 		cmn_err(CE_WARN, "!ahci%d: ahci port %d has task file error",
9978 		    instance, port);
9979 
9980 	cmn_err(CE_WARN, "!ahci%d: ahci port %d is trying to do error "
9981 	    "recovery", instance, port);
9982 }
9983 
9984 static void
9985 ahci_dump_commands(ahci_ctl_t *ahci_ctlp, uint8_t port,
9986     uint32_t slot_tags)
9987 {
9988 	ahci_port_t *ahci_portp;
9989 	int tmp_slot;
9990 	sata_pkt_t *spkt;
9991 	sata_cmd_t cmd;
9992 
9993 	ahci_portp = ahci_ctlp->ahcictl_ports[port];
9994 	ASSERT(ahci_portp != NULL);
9995 
9996 	while (slot_tags) {
9997 		tmp_slot = ddi_ffs(slot_tags) - 1;
9998 		if (tmp_slot == -1) {
9999 			break;
10000 		}
10001 
10002 		spkt = ahci_portp->ahciport_slot_pkts[tmp_slot];
10003 		ASSERT(spkt != NULL);
10004 		cmd = spkt->satapkt_cmd;
10005 
10006 		cmn_err(CE_WARN, "!satapkt 0x%p: cmd_reg = 0x%x "
10007 		    "features_reg = 0x%x sec_count_msb = 0x%x "
10008 		    "lba_low_msb = 0x%x lba_mid_msb = 0x%x "
10009 		    "lba_high_msb = 0x%x sec_count_lsb = 0x%x "
10010 		    "lba_low_lsb = 0x%x lba_mid_lsb = 0x%x "
10011 		    "lba_high_lsb = 0x%x device_reg = 0x%x "
10012 		    "addr_type = 0x%x cmd_flags = 0x%x", (void *)spkt,
10013 		    cmd.satacmd_cmd_reg, cmd.satacmd_features_reg,
10014 		    cmd.satacmd_sec_count_msb, cmd.satacmd_lba_low_msb,
10015 		    cmd.satacmd_lba_mid_msb, cmd.satacmd_lba_high_msb,
10016 		    cmd.satacmd_sec_count_lsb, cmd.satacmd_lba_low_lsb,
10017 		    cmd.satacmd_lba_mid_lsb, cmd.satacmd_lba_high_lsb,
10018 		    cmd.satacmd_device_reg, cmd.satacmd_addr_type,
10019 		    *((uint32_t *)&(cmd.satacmd_flags)));
10020 
10021 		CLEAR_BIT(slot_tags, tmp_slot);
10022 	}
10023 }
10024 
10025 /*
10026  * Dump the serror message to the log.
10027  */
10028 static void
10029 ahci_log_serror_message(ahci_ctl_t *ahci_ctlp, uint8_t port,
10030     uint32_t port_serror, int debug_only)
10031 {
10032 	static char err_buf[512];
10033 	static char err_msg_header[16];
10034 	char *err_msg = err_buf;
10035 
10036 	*err_buf = '\0';
10037 	*err_msg_header = '\0';
10038 
10039 	if (port_serror & SERROR_DATA_ERR_FIXED) {
10040 		err_msg = strcat(err_msg,
10041 		    "\tRecovered Data Integrity Error (I)\n");
10042 	}
10043 
10044 	if (port_serror & SERROR_COMM_ERR_FIXED) {
10045 		err_msg = strcat(err_msg,
10046 		    "\tRecovered Communication Error (M)\n");
10047 	}
10048 
10049 	if (port_serror & SERROR_DATA_ERR) {
10050 		err_msg = strcat(err_msg,
10051 		    "\tTransient Data Integrity Error (T)\n");
10052 	}
10053 
10054 	if (port_serror & SERROR_PERSISTENT_ERR) {
10055 		err_msg = strcat(err_msg,
10056 		    "\tPersistent Communication or Data Integrity Error (C)\n");
10057 	}
10058 
10059 	if (port_serror & SERROR_PROTOCOL_ERR) {
10060 		err_msg = strcat(err_msg, "\tProtocol Error (P)\n");
10061 	}
10062 
10063 	if (port_serror & SERROR_INT_ERR) {
10064 		err_msg = strcat(err_msg, "\tInternal Error (E)\n");
10065 	}
10066 
10067 	if (port_serror & SERROR_PHY_RDY_CHG) {
10068 		err_msg = strcat(err_msg, "\tPhyRdy Change (N)\n");
10069 	}
10070 
10071 	if (port_serror & SERROR_PHY_INT_ERR) {
10072 		err_msg = strcat(err_msg, "\tPhy Internal Error (I)\n");
10073 	}
10074 
10075 	if (port_serror & SERROR_COMM_WAKE) {
10076 		err_msg = strcat(err_msg, "\tComm Wake (W)\n");
10077 	}
10078 
10079 	if (port_serror & SERROR_10B_TO_8B_ERR) {
10080 		err_msg = strcat(err_msg, "\t10B to 8B Decode Error (B)\n");
10081 	}
10082 
10083 	if (port_serror & SERROR_DISPARITY_ERR) {
10084 		err_msg = strcat(err_msg, "\tDisparity Error (D)\n");
10085 	}
10086 
10087 	if (port_serror & SERROR_CRC_ERR) {
10088 		err_msg = strcat(err_msg, "\tCRC Error (C)\n");
10089 	}
10090 
10091 	if (port_serror & SERROR_HANDSHAKE_ERR) {
10092 		err_msg = strcat(err_msg, "\tHandshake Error (H)\n");
10093 	}
10094 
10095 	if (port_serror & SERROR_LINK_SEQ_ERR) {
10096 		err_msg = strcat(err_msg, "\tLink Sequence Error (S)\n");
10097 	}
10098 
10099 	if (port_serror & SERROR_TRANS_ERR) {
10100 		err_msg = strcat(err_msg,
10101 		    "\tTransport state transition error (T)\n");
10102 	}
10103 
10104 	if (port_serror & SERROR_FIS_TYPE) {
10105 		err_msg = strcat(err_msg, "\tUnknown FIS Type (F)\n");
10106 	}
10107 
10108 	if (port_serror & SERROR_EXCHANGED_ERR) {
10109 		err_msg = strcat(err_msg, "\tExchanged (X)\n");
10110 	}
10111 
10112 	if (*err_msg == '\0')
10113 		return;
10114 
10115 	if (debug_only) {
10116 		(void) sprintf(err_msg_header, "port %d", port);
10117 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, err_msg_header, NULL);
10118 		AHCIDBG(AHCIDBG_ERRS, ahci_ctlp, err_msg, NULL);
10119 	} else if (ahci_ctlp) {
10120 		cmn_err(CE_WARN, "!ahci%d: %s %s",
10121 		    ddi_get_instance(ahci_ctlp->ahcictl_dip),
10122 		    err_msg_header, err_msg);
10123 
10124 		/* sata trace debug */
10125 		sata_trace_debug(ahci_ctlp->ahcictl_dip,
10126 		    "ahci%d: %s %s", ddi_get_instance(ahci_ctlp->ahcictl_dip),
10127 		    err_msg_header, err_msg);
10128 	} else {
10129 		cmn_err(CE_WARN, "!ahci: %s %s", err_msg_header, err_msg);
10130 
10131 		/* sata trace debug */
10132 		sata_trace_debug(NULL, "ahci: %s %s", err_msg_header, err_msg);
10133 	}
10134 }
10135 
10136 /*
10137  * Translate the sata_address_t type into the ahci_addr_t type.
10138  * sata_device.satadev_addr structure is used as source.
10139  */
10140 static void
10141 ahci_get_ahci_addr(ahci_ctl_t *ahci_ctlp, sata_device_t *sd,
10142     ahci_addr_t *ahci_addrp)
10143 {
10144 	sata_address_t *sata_addrp = &sd->satadev_addr;
10145 	ahci_addrp->aa_port =
10146 	    ahci_ctlp->ahcictl_cport_to_port[sata_addrp->cport];
10147 	ahci_addrp->aa_pmport = sata_addrp->pmport;
10148 
10149 	switch (sata_addrp->qual) {
10150 	case SATA_ADDR_DCPORT:
10151 	case SATA_ADDR_CPORT:
10152 		ahci_addrp->aa_qual = AHCI_ADDR_PORT;
10153 		break;
10154 	case SATA_ADDR_PMULT:
10155 	case SATA_ADDR_PMULT_SPEC:
10156 		ahci_addrp->aa_qual = AHCI_ADDR_PMULT;
10157 		break;
10158 	case SATA_ADDR_DPMPORT:
10159 	case SATA_ADDR_PMPORT:
10160 		ahci_addrp->aa_qual = AHCI_ADDR_PMPORT;
10161 		break;
10162 	case SATA_ADDR_NULL:
10163 	default:
10164 		/* something went wrong */
10165 		ahci_addrp->aa_qual = AHCI_ADDR_NULL;
10166 		break;
10167 	}
10168 }
10169 
10170 /*
10171  * This routine is to calculate the total number of ports implemented
10172  * by the HBA.
10173  */
10174 static int
10175 ahci_get_num_implemented_ports(uint32_t ports_implemented)
10176 {
10177 	uint8_t i;
10178 	int num = 0;
10179 
10180 	for (i = 0; i < AHCI_MAX_PORTS; i++) {
10181 		if (((uint32_t)0x1 << i) & ports_implemented)
10182 			num++;
10183 	}
10184 
10185 	return (num);
10186 }
10187 
10188 #if AHCI_DEBUG
10189 static void
10190 ahci_log(ahci_ctl_t *ahci_ctlp, uint_t level, char *fmt, ...)
10191 {
10192 	static char name[16];
10193 	va_list ap;
10194 
10195 	mutex_enter(&ahci_log_mutex);
10196 
10197 	va_start(ap, fmt);
10198 	if (ahci_ctlp) {
10199 		(void) sprintf(name, "ahci%d: ",
10200 		    ddi_get_instance(ahci_ctlp->ahcictl_dip));
10201 	} else {
10202 		(void) sprintf(name, "ahci: ");
10203 	}
10204 
10205 	(void) vsprintf(ahci_log_buf, fmt, ap);
10206 	va_end(ap);
10207 
10208 	cmn_err(level, "%s%s", name, ahci_log_buf);
10209 
10210 	mutex_exit(&ahci_log_mutex);
10211 }
10212 #endif
10213 
10214 /*
10215  * quiesce(9E) entry point.
10216  *
10217  * This function is called when the system is single-threaded at high
10218  * PIL with preemption disabled. Therefore, this function must not be
10219  * blocked.
10220  *
10221  * This function returns DDI_SUCCESS on success, or DDI_FAILURE on failure.
10222  * DDI_FAILURE indicates an error condition and should almost never happen.
10223  */
10224 static int
10225 ahci_quiesce(dev_info_t *dip)
10226 {
10227 	ahci_ctl_t *ahci_ctlp;
10228 	ahci_port_t *ahci_portp;
10229 	int instance, port;
10230 
10231 	instance = ddi_get_instance(dip);
10232 	ahci_ctlp = ddi_get_soft_state(ahci_statep, instance);
10233 
10234 	if (ahci_ctlp == NULL)
10235 		return (DDI_FAILURE);
10236 
10237 #if AHCI_DEBUG
10238 	ahci_debug_flags = 0;
10239 #endif
10240 
10241 	/* disable all the interrupts. */
10242 	ahci_disable_all_intrs(ahci_ctlp);
10243 
10244 	for (port = 0; port < ahci_ctlp->ahcictl_num_ports; port++) {
10245 		if (!AHCI_PORT_IMPLEMENTED(ahci_ctlp, port)) {
10246 			continue;
10247 		}
10248 
10249 		ahci_portp = ahci_ctlp->ahcictl_ports[port];
10250 
10251 		/*
10252 		 * Stop the port by clearing PxCMD.ST
10253 		 *
10254 		 * Here we must disable the port interrupt because
10255 		 * ahci_disable_all_intrs only clear GHC.IE, and IS
10256 		 * register will be still set if PxIE is enabled.
10257 		 * When ahci shares one IRQ with other drivers, the
10258 		 * intr handler may claim the intr mistakenly.
10259 		 */
10260 		ahci_disable_port_intrs(ahci_ctlp, port);
10261 		(void) ahci_put_port_into_notrunning_state(ahci_ctlp,
10262 		    ahci_portp, port);
10263 	}
10264 
10265 	return (DDI_SUCCESS);
10266 }
10267 
10268 /*
10269  * The function will add a sata packet to the done queue.
10270  *
10271  * WARNING!!! ahciport_mutex should be acquired before the function
10272  * is called.
10273  */
10274 static void
10275 ahci_add_doneq(ahci_port_t *ahci_portp, sata_pkt_t *satapkt, int reason)
10276 {
10277 	ASSERT(satapkt != NULL);
10278 
10279 	/* set the reason for all packets */
10280 	satapkt->satapkt_reason = reason;
10281 	satapkt->satapkt_hba_driver_private = NULL;
10282 
10283 	if (! (satapkt->satapkt_op_mode & SATA_OPMODE_SYNCH) &&
10284 	    satapkt->satapkt_comp) {
10285 		/*
10286 		 * only add to queue when mode is not synch and there is
10287 		 * completion callback
10288 		 */
10289 		*ahci_portp->ahciport_doneqtail = satapkt;
10290 		ahci_portp->ahciport_doneqtail =
10291 		    (sata_pkt_t **)&(satapkt->satapkt_hba_driver_private);
10292 		ahci_portp->ahciport_doneq_len ++;
10293 
10294 	} else if ((satapkt->satapkt_op_mode & SATA_OPMODE_SYNCH) &&
10295 	    ! (satapkt->satapkt_op_mode & SATA_OPMODE_POLLING))
10296 		/*
10297 		 * for sync/non-poll mode, just call cv_broadcast
10298 		 */
10299 		cv_broadcast(&ahci_portp->ahciport_cv);
10300 }
10301 
10302 /*
10303  * The function will call completion callback of sata packet on the
10304  * completed queue
10305  *
10306  * WARNING!!! ahciport_mutex should be acquired before the function
10307  * is called.
10308  */
10309 static void
10310 ahci_flush_doneq(ahci_port_t *ahci_portp)
10311 {
10312 	sata_pkt_t *satapkt, *next;
10313 
10314 	if (ahci_portp->ahciport_doneq) {
10315 		satapkt = ahci_portp->ahciport_doneq;
10316 
10317 		ahci_portp->ahciport_doneq = NULL;
10318 		ahci_portp->ahciport_doneqtail = &ahci_portp->ahciport_doneq;
10319 		ahci_portp->ahciport_doneq_len = 0;
10320 
10321 		mutex_exit(&ahci_portp->ahciport_mutex);
10322 
10323 		while (satapkt != NULL) {
10324 			next = satapkt->satapkt_hba_driver_private;
10325 			satapkt->satapkt_hba_driver_private = NULL;
10326 
10327 			/* Call the callback */
10328 			(*satapkt->satapkt_comp)(satapkt);
10329 
10330 			satapkt = next;
10331 		}
10332 
10333 		mutex_enter(&ahci_portp->ahciport_mutex);
10334 	}
10335 }
10336