xref: /illumos-gate/usr/src/uts/common/io/sata/adapters/si3124/si3124.c (revision 002c70ff32f5df6f93c15f88d351ce26443e6ee7)
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 
30 
31 /*
32  * SiliconImage 3124/3132 sata controller driver
33  */
34 
35 /*
36  *
37  *
38  * 			Few Design notes
39  *
40  *
41  * I. General notes
42  *
43  * Even though the driver is named as si3124, it is actually meant to
44  * work with both 3124 and 3132 controllers.
45  *
46  * The current file si3124.c is the main driver code. The si3124reg.h
47  * holds the register definitions from SiI 3124/3132 data sheets. The
48  * si3124var.h holds the driver specific definitions which are not
49  * directly derived from data sheets.
50  *
51  *
52  * II. Data structures
53  *
54  * si_ctl_state_t: This holds the driver private information for each
55  * 	controller instance. Each of the sata ports within a single
56  *	controller are represented by si_port_state_t. The
57  *	sictl_global_acc_handle and sictl_global_address map the
58  *	controller-wide global register space and are derived from pci
59  *	BAR 0. The sictl_port_acc_handle and sictl_port_addr map the
60  *	per-port register space and are derived from pci BAR 1.
61  *
62  * si_port_state_t: This holds the per port information. The siport_mutex
63  *	holds the per port mutex. The siport_pending_tags is the bit mask of
64  * 	commands posted to controller. The siport_slot_pkts[] holds the
65  * 	pending sata packets. The siport_port_type holds the device type
66  *	connected directly to the port while the siport_portmult_state
67  * 	holds the similar information for the devices behind a port
68  *	multiplier.
69  *
70  * si_prb_t: This contains the PRB being posted to the controller.
71  *	The two SGE entries contained within si_prb_t itself are not
72  *	really used to hold any scatter gather entries. The scatter gather
73  *	list is maintained external to PRB and is linked from one
74  * 	of the contained SGEs inside the PRB. For atapi devices, the
75  *	first contained SGE holds the PACKET and second contained
76  *	SGE holds the link to an external SGT. For non-atapi devices,
77  *	the first contained SGE works as link to external SGT while
78  *	second SGE is blank.
79  *
80  * external SGT tables: The external SGT tables pointed to from
81  *	within si_prb_t are actually abstracted as si_sgblock_t. Each
82  *	si_sgblock_t contains SI_MAX_SGT_TABLES_PER_PRB number of
83  *	SGT tables linked in a chain. Currently this max value of
84  *	SGT tables per block is hard coded as 10 which translates
85  *	to a maximum of 31 dma cookies per single dma transfer.
86  *
87  *
88  * III. Driver operation
89  *
90  * Command Issuing: We use the "indirect method of command issuance". The
91  *	PRB contains the command [and atapi PACKET] and a link to the
92  *	external SGT chain. We write the physical address of the PRB into
93  *	command activation register. There are 31 command slots for
94  *	each port. After posting a command, we remember the posted slot &
95  *	the sata packet in siport_pending_tags & siport_slot_pkts[]
96  *	respectively.
97  *
98  * Command completion: On a successful completion, intr_command_complete()
99  * 	receives the control. The slot_status register holds the outstanding
100  *	commands. Any reading of slot_status register automatically clears
101  *	the interrupt. By comparing the slot_status register contents with
102  *	per port siport_pending_tags, we determine which of the previously
103  *	posted commands have finished.
104  *
105  * Timeout handling: Every 5 seconds, the watchdog handler scans thru the
106  * 	pending packets. The satapkt->satapkt_hba_driver_private field is
107  * 	overloaded with the count of watchdog cycles a packet has survived.
108  *	If a packet has not completed within satapkt->satapkt_time, it is
109  *	failed with error code of SATA_PKT_TIMEOUT. There is one watchdog
110  *	handler running for each instance of controller.
111  *
112  * Error handling: For 3124, whenever any single command has encountered
113  *	an error, the whole port execution completely stalls; there is no
114  *	way of canceling or aborting the particular failed command. If
115  * 	the port is connected to a port multiplier, we can however RESUME
116  *	other non-error devices connected to the port multiplier.
117  *	The only way to recover the failed commands is to either initialize
118  *	the port or reset the port/device. Both port initialize and reset
119  *	operations result in discarding any of pending commands on the port.
120  *	All such discarded commands are sent up to framework with PKT_RESET
121  *	satapkt_reason. The assumption is that framework [and sd] would
122  *	retry these commands again. The failed command itself however is
123  *	sent up with PKT_DEV_ERROR.
124  *
125  *	Here is the implementation strategy based on SiliconImage email
126  *	regarding how they handle the errors for their Windows driver:
127  *
128  *	  a) for DEVICEERROR:
129  *		If the port is connected to port multiplier, then
130  *		 1) Resume the port
131  *		 2) Wait for all the non-failed commands to complete
132  *		 3) Perform a Port Initialize
133  *
134  *		If the port is not connected to port multiplier, issue
135  *		a Port Initialize.
136  *
137  *	  b) for SDBERROR: [SDBERROR means failed command is an NCQ command]
138  * 		Handle exactly like DEVICEERROR handling.
139  *		After the Port Initialize done, do a Read Log Extended.
140  *
141  *	  c) for SENDFISERROR:
142  *		If the port is connected to port multiplier, then
143  *		 1) Resume the port
144  *		 2) Wait for all the non-failed commands to complete
145  *		 3) Perform a Port Initialize
146  *
147  *		If the port is not connected to port multiplier, issue
148  * 		a Device Reset.
149  *
150  *	  d) for DATAFISERROR:
151  *		If the port was executing an NCQ command, issue a Device
152  *		Reset.
153  *
154  *		Otherwise, follow the same error recovery as DEVICEERROR.
155  *
156  *	  e) for any other error, simply issue a Device Reset.
157  *
158  * 	To synchronize the interactions between various control flows (e.g.
159  *	error recovery, timeout handling, si_poll_timeout, incoming flow
160  *	from framework etc.), the following precautions are taken care of:
161  *		a) During mopping_in_progress, no more commands are
162  *		accepted from the framework.
163  *
164  *		b) While draining the port multiplier commands, we should
165  *		handle the possibility of any of the other waited commands
166  *		failing (possibly with a different error code)
167  *
168  * Atapi handling: For atapi devices, we use the first SGE within the PRB
169  * 	to fill the scsi cdb while the second SGE points to external SGT.
170  *
171  * Queuing: Queue management is achieved external to the driver inside sd.
172  *	Based on sata_hba_tran->qdepth and IDENTIFY data, the framework
173  *	enables or disables the queuing. The qdepth for si3124 is 31
174  *	commands.
175  *
176  * Port Multiplier: Enumeration of port multiplier is handled during the
177  *	controller initialization and also during the a hotplug operation.
178  *	Current logic takes care of situation where a port multiplier
179  *	is hotplugged into a port which had a cdisk connected previously
180  *	and vice versa.
181  *
182  * Register poll timeouts: Currently most of poll timeouts on register
183  *	reads is set to 0.5 seconds except for a value of 10 seconds
184  *	while reading the device signature. [Such a big timeout values
185  *	for device signature were found needed during cold reboots
186  *	for devices behind port multiplier].
187  *
188  *
189  * IV. Known Issues
190  *
191  * 1) Currently the atapi packet length is hard coded to 12 bytes
192  *	This is wrong. The framework should determine it just like they
193  * 	determine ad_cdb_len in legacy atapi.c. It should even reject
194  *	init_pkt() for greater CDB lengths. See atapi.c. Revisit this
195  *	in 2nd phase of framework project.
196  *
197  * 2) Do real REQUEST SENSE command instead of faking for ATAPI case.
198  *
199  */
200 
201 
202 #include <sys/note.h>
203 #include <sys/scsi/scsi.h>
204 #include <sys/pci.h>
205 #include <sys/sata/sata_hba.h>
206 #include <sys/sata/adapters/si3124/si3124reg.h>
207 #include <sys/sata/adapters/si3124/si3124var.h>
208 
209 /*
210  * Function prototypes for driver entry points
211  */
212 static	int si_attach(dev_info_t *, ddi_attach_cmd_t);
213 static	int si_detach(dev_info_t *, ddi_detach_cmd_t);
214 static	int si_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **);
215 static int si_power(dev_info_t *, int, int);
216 
217 /*
218  * Function prototypes for SATA Framework interfaces
219  */
220 static	int si_register_sata_hba_tran(si_ctl_state_t *);
221 static	int si_unregister_sata_hba_tran(si_ctl_state_t *);
222 
223 static	int si_tran_probe_port(dev_info_t *, sata_device_t *);
224 static	int si_tran_start(dev_info_t *, sata_pkt_t *spkt);
225 static	int si_tran_abort(dev_info_t *, sata_pkt_t *, int);
226 static	int si_tran_reset_dport(dev_info_t *, sata_device_t *);
227 static	int si_tran_hotplug_port_activate(dev_info_t *, sata_device_t *);
228 static	int si_tran_hotplug_port_deactivate(dev_info_t *, sata_device_t *);
229 
230 /*
231  * Local function prototypes
232  */
233 
234 static	int si_alloc_port_state(si_ctl_state_t *, int);
235 static	void si_dealloc_port_state(si_ctl_state_t *, int);
236 static	int si_alloc_sgbpool(si_ctl_state_t *, int);
237 static	void si_dealloc_sgbpool(si_ctl_state_t *, int);
238 static	int si_alloc_prbpool(si_ctl_state_t *, int);
239 static	void si_dealloc_prbpool(si_ctl_state_t *, int);
240 
241 static void si_find_dev_signature(si_ctl_state_t *, si_port_state_t *,
242 						int, int);
243 static void si_poll_cmd(si_ctl_state_t *, si_port_state_t *, int, int,
244 						sata_pkt_t *);
245 static	int si_claim_free_slot(si_ctl_state_t *, si_port_state_t *, int);
246 static	int si_deliver_satapkt(si_ctl_state_t *, si_port_state_t *, int,
247 						sata_pkt_t *);
248 
249 static	int si_initialize_controller(si_ctl_state_t *);
250 static	void si_deinititalize_controller(si_ctl_state_t *);
251 static void si_init_port(si_ctl_state_t *, int);
252 static	int si_enumerate_port_multiplier(si_ctl_state_t *,
253 						si_port_state_t *, int);
254 static int si_read_portmult_reg(si_ctl_state_t *, si_port_state_t *,
255 						int, int, int, uint32_t *);
256 static int si_write_portmult_reg(si_ctl_state_t *, si_port_state_t *,
257 						int, int, int, uint32_t);
258 static void si_set_sense_data(sata_pkt_t *, int);
259 
260 static uint_t si_intr(caddr_t, caddr_t);
261 static int si_intr_command_complete(si_ctl_state_t *,
262 					si_port_state_t *, int);
263 static int si_intr_command_error(si_ctl_state_t *,
264 					si_port_state_t *, int);
265 static void si_error_recovery_DEVICEERROR(si_ctl_state_t *,
266 					si_port_state_t *, int);
267 static void si_error_recovery_SDBERROR(si_ctl_state_t *,
268 					si_port_state_t *, int);
269 static void si_error_recovery_DATAFISERROR(si_ctl_state_t *,
270 					si_port_state_t *, int);
271 static void si_error_recovery_SENDFISERROR(si_ctl_state_t *,
272 					si_port_state_t *, int);
273 static void si_error_recovery_default(si_ctl_state_t *,
274 					si_port_state_t *, int);
275 static uint8_t si_read_log_ext(si_ctl_state_t *,
276 					si_port_state_t *si_portp, int);
277 static void si_log_error_message(si_ctl_state_t *, int, uint32_t);
278 static int si_intr_port_ready(si_ctl_state_t *, si_port_state_t *, int);
279 static int si_intr_pwr_change(si_ctl_state_t *, si_port_state_t *, int);
280 static int si_intr_phy_ready_change(si_ctl_state_t *, si_port_state_t *, int);
281 static int si_intr_comwake_rcvd(si_ctl_state_t *, si_port_state_t *, int);
282 static int si_intr_unrecognised_fis(si_ctl_state_t *, si_port_state_t *, int);
283 static int si_intr_dev_xchanged(si_ctl_state_t *, si_port_state_t *, int);
284 static int si_intr_decode_err_threshold(si_ctl_state_t *,
285 					si_port_state_t *, int);
286 static int si_intr_crc_err_threshold(si_ctl_state_t *, si_port_state_t *, int);
287 static int si_intr_handshake_err_threshold(si_ctl_state_t *,
288 					si_port_state_t *, int);
289 static int si_intr_set_devbits_notify(si_ctl_state_t *, si_port_state_t *, int);
290 static	void si_handle_attention_raised(si_ctl_state_t *,
291 					si_port_state_t *, int);
292 
293 static	void si_enable_port_interrupts(si_ctl_state_t *, int);
294 static	void si_enable_all_interrupts(si_ctl_state_t *);
295 static	void si_disable_port_interrupts(si_ctl_state_t *, int);
296 static	void si_disable_all_interrupts(si_ctl_state_t *);
297 static 	void fill_dev_sregisters(si_ctl_state_t *, int, sata_device_t *);
298 static 	int si_add_legacy_intrs(si_ctl_state_t *);
299 static 	int si_add_msi_intrs(si_ctl_state_t *);
300 static 	void si_rem_intrs(si_ctl_state_t *);
301 
302 static	int si_reset_dport_wait_till_ready(si_ctl_state_t *,
303 				si_port_state_t *, int, int);
304 static	int si_initialize_port_wait_till_ready(si_ctl_state_t *, int);
305 
306 static void si_timeout_pkts(si_ctl_state_t *, si_port_state_t *, int, uint32_t);
307 static	void si_watchdog_handler(si_ctl_state_t *);
308 
309 static	void si_log(si_ctl_state_t *, uint_t, char *, ...);
310 
311 static	void si_copy_out_regs(sata_cmd_t *, fis_reg_h2d_t *);
312 
313 /*
314  * DMA attributes for the data buffer
315  */
316 
317 static ddi_dma_attr_t buffer_dma_attr = {
318 	DMA_ATTR_V0,		/* dma_attr_version */
319 	0,			/* dma_attr_addr_lo: lowest bus address */
320 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
321 	0xffffffffull,		/* dma_attr_count_max i.e. for one cookie */
322 	1,			/* dma_attr_align: single byte aligned */
323 	1,			/* dma_attr_burstsizes */
324 	1,			/* dma_attr_minxfer */
325 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
326 	0xffffffffull,		/* dma_attr_seg */
327 	SI_MAX_SGL_LENGTH,	/* dma_attr_sgllen */
328 	512,			/* dma_attr_granular */
329 	0,			/* dma_attr_flags */
330 };
331 
332 /*
333  * DMA attributes for incore RPB and SGT pool
334  */
335 static ddi_dma_attr_t prb_sgt_dma_attr = {
336 	DMA_ATTR_V0,		/* dma_attr_version */
337 	0,			/* dma_attr_addr_lo: lowest bus address */
338 	0xffffffffffffffffull,	/* dma_attr_addr_hi: highest bus address */
339 	0xffffffffull,		/* dma_attr_count_max i.e. for one cookie */
340 	8,			/* dma_attr_align: quad word aligned */
341 	1,			/* dma_attr_burstsizes */
342 	1,			/* dma_attr_minxfer */
343 	0xffffffffull,		/* dma_attr_maxxfer i.e. includes all cookies */
344 	0xffffffffull,		/* dma_attr_seg */
345 	1,			/* dma_attr_sgllen */
346 	1,			/* dma_attr_granular */
347 	0,			/* dma_attr_flags */
348 };
349 
350 /* Device access attributes */
351 static ddi_device_acc_attr_t accattr = {
352     DDI_DEVICE_ATTR_V0,
353     DDI_STRUCTURE_LE_ACC,
354     DDI_STRICTORDER_ACC
355 };
356 
357 
358 static struct dev_ops sictl_dev_ops = {
359 	DEVO_REV,		/* devo_rev */
360 	0,			/* refcnt  */
361 	si_getinfo,		/* info */
362 	nulldev,		/* identify */
363 	nulldev,		/* probe */
364 	si_attach,		/* attach */
365 	si_detach,		/* detach */
366 	nodev,			/* no reset */
367 	(struct cb_ops *)0,	/* driver operations */
368 	NULL,			/* bus operations */
369 	si_power		/* power */
370 };
371 
372 static sata_tran_hotplug_ops_t si_tran_hotplug_ops = {
373 	SATA_TRAN_HOTPLUG_OPS_REV_1,
374 	si_tran_hotplug_port_activate,
375 	si_tran_hotplug_port_deactivate
376 };
377 
378 
379 static int si_watchdog_timeout = 5; /* 5 seconds */
380 static int si_watchdog_tick;
381 
382 extern struct mod_ops mod_driverops;
383 
384 static  struct modldrv modldrv = {
385 	&mod_driverops,	/* driverops */
386 	"si3124 driver v%I%",
387 	&sictl_dev_ops,	/* driver ops */
388 };
389 
390 static  struct modlinkage modlinkage = {
391 	MODREV_1,
392 	&modldrv,
393 	NULL
394 };
395 
396 
397 /* The following are needed for si_log() */
398 static kmutex_t si_log_mutex;
399 static char si_log_buf[512];
400 uint32_t si_debug_flags = 0x0;
401 static int is_msi_supported = 0;
402 
403 /* Opaque state pointer to be initialized by ddi_soft_state_init() */
404 static void *si_statep	= NULL;
405 
406 /*
407  *  si3124 module initialization.
408  *
409  */
410 int
411 _init(void)
412 {
413 	int	error;
414 
415 	error = ddi_soft_state_init(&si_statep, sizeof (si_ctl_state_t), 0);
416 	if (error != 0) {
417 		return (error);
418 	}
419 
420 	mutex_init(&si_log_mutex, NULL, MUTEX_DRIVER, NULL);
421 
422 	if ((error = sata_hba_init(&modlinkage)) != 0) {
423 		mutex_destroy(&si_log_mutex);
424 		ddi_soft_state_fini(&si_statep);
425 		return (error);
426 	}
427 
428 	error = mod_install(&modlinkage);
429 	if (error != 0) {
430 		sata_hba_fini(&modlinkage);
431 		mutex_destroy(&si_log_mutex);
432 		ddi_soft_state_fini(&si_statep);
433 		return (error);
434 	}
435 
436 	si_watchdog_tick = drv_usectohz((clock_t)si_watchdog_timeout * 1000000);
437 
438 	return (error);
439 }
440 
441 /*
442  * si3124 module uninitialize.
443  *
444  */
445 int
446 _fini(void)
447 {
448 	int	error;
449 
450 	error = mod_remove(&modlinkage);
451 	if (error != 0) {
452 		return (error);
453 	}
454 
455 	/* Remove the resources allocated in _init(). */
456 	sata_hba_fini(&modlinkage);
457 	mutex_destroy(&si_log_mutex);
458 	ddi_soft_state_fini(&si_statep);
459 
460 	return (error);
461 }
462 
463 /*
464  * _info entry point
465  *
466  */
467 int
468 _info(struct modinfo *modinfop)
469 {
470 	return (mod_info(&modlinkage, modinfop));
471 }
472 
473 
474 /*
475  * The attach entry point for dev_ops.
476  *
477  * We initialize the controller, initialize the soft state, register
478  * the interrupt handlers and then register ourselves with sata framework.
479  */
480 static int
481 si_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
482 {
483 	si_ctl_state_t *si_ctlp;
484 	int instance;
485 	int status;
486 	int attach_state;
487 	int intr_types;
488 	sata_device_t sdevice;
489 
490 	SIDBG0(SIDBG_INIT|SIDBG_ENTRY, NULL, "si_attach enter");
491 	instance = ddi_get_instance(dip);
492 	attach_state = ATTACH_PROGRESS_NONE;
493 
494 	switch (cmd) {
495 
496 	case DDI_ATTACH:
497 
498 		/* Allocate si_softc. */
499 		status = ddi_soft_state_zalloc(si_statep, instance);
500 		if (status != DDI_SUCCESS) {
501 			goto err_out;
502 		}
503 
504 		si_ctlp = ddi_get_soft_state(si_statep, instance);
505 		si_ctlp->sictl_devinfop = dip;
506 
507 		attach_state |= ATTACH_PROGRESS_STATEP_ALLOC;
508 
509 		/* Configure pci config space handle. */
510 		status = pci_config_setup(dip, &si_ctlp->sictl_pci_conf_handle);
511 		if (status != DDI_SUCCESS) {
512 			goto err_out;
513 		}
514 
515 		si_ctlp->sictl_devid =
516 			pci_config_get16(si_ctlp->sictl_pci_conf_handle,
517 							PCI_CONF_DEVID);
518 		if (si_ctlp->sictl_devid == SI3132_DEV_ID) {
519 			si_ctlp->sictl_num_ports = SI3132_MAX_PORTS;
520 		} else {
521 			si_ctlp->sictl_num_ports = SI3124_MAX_PORTS;
522 		}
523 
524 		attach_state |= ATTACH_PROGRESS_CONF_HANDLE;
525 
526 		/* Now map the bar0; the bar0 contains the global registers. */
527 		status = ddi_regs_map_setup(dip,
528 					PCI_BAR0,
529 					(caddr_t *)&si_ctlp->sictl_global_addr,
530 					0,
531 					0,
532 					&accattr,
533 					&si_ctlp->sictl_global_acc_handle);
534 		if (status != DDI_SUCCESS) {
535 			goto err_out;
536 		}
537 
538 		attach_state |= ATTACH_PROGRESS_BAR0_MAP;
539 
540 		/* Now map bar1; the bar1 contains the port registers. */
541 		status = ddi_regs_map_setup(dip,
542 					PCI_BAR1,
543 					(caddr_t *)&si_ctlp->sictl_port_addr,
544 					0,
545 					0,
546 					&accattr,
547 					&si_ctlp->sictl_port_acc_handle);
548 		if (status != DDI_SUCCESS) {
549 			goto err_out;
550 		}
551 
552 		attach_state |= ATTACH_PROGRESS_BAR1_MAP;
553 
554 		/*
555 		 * Disable all the interrupts before adding interrupt
556 		 * handler(s). The interrupts shall be re-enabled selectively
557 		 * out of si_init_port().
558 		 */
559 		si_disable_all_interrupts(si_ctlp);
560 
561 		/* Get supported interrupt types. */
562 		if (ddi_intr_get_supported_types(dip, &intr_types)
563 					!= DDI_SUCCESS) {
564 			SIDBG0(SIDBG_INIT, NULL,
565 				"ddi_intr_get_supported_types failed");
566 			goto err_out;
567 		}
568 
569 		SIDBG1(SIDBG_INIT, NULL,
570 			"ddi_intr_get_supported_types() returned: 0x%x",
571 			intr_types);
572 
573 		if (is_msi_supported && (intr_types & DDI_INTR_TYPE_MSI)) {
574 			SIDBG0(SIDBG_INIT, NULL, "Using MSI interrupt type");
575 
576 			/*
577 			 * Try MSI first, but fall back to legacy if MSI
578 			 * attach fails.
579 			 */
580 			if (si_add_msi_intrs(si_ctlp) == DDI_SUCCESS) {
581 				si_ctlp->sictl_intr_type = DDI_INTR_TYPE_MSI;
582 				attach_state |= ATTACH_PROGRESS_INTR_ADDED;
583 				SIDBG0(SIDBG_INIT, NULL,
584 					"MSI interrupt setup done");
585 			} else {
586 				SIDBG0(SIDBG_INIT, NULL,
587 					"MSI registration failed "
588 					"will try Legacy interrupts");
589 			}
590 		}
591 
592 		if (!(attach_state & ATTACH_PROGRESS_INTR_ADDED) &&
593 			(intr_types & DDI_INTR_TYPE_FIXED)) {
594 			/*
595 			 * Either the MSI interrupt setup has failed or only
596 			 * fixed interrupts are available on the system.
597 			 */
598 			SIDBG0(SIDBG_INIT, NULL, "Using Legacy interrupt type");
599 
600 			if (si_add_legacy_intrs(si_ctlp) == DDI_SUCCESS) {
601 				si_ctlp->sictl_intr_type = DDI_INTR_TYPE_FIXED;
602 				attach_state |= ATTACH_PROGRESS_INTR_ADDED;
603 				SIDBG0(SIDBG_INIT, NULL,
604 					"Legacy interrupt setup done");
605 			} else {
606 				SIDBG0(SIDBG_INIT, NULL,
607 					"legacy interrupt setup failed");
608 				goto err_out;
609 			}
610 		}
611 
612 		if (!(attach_state & ATTACH_PROGRESS_INTR_ADDED)) {
613 			SIDBG0(SIDBG_INIT, NULL,
614 				"si3124: No interrupts registered");
615 			goto err_out;
616 		}
617 
618 
619 		/* Initialize the mutex. */
620 		mutex_init(&si_ctlp->sictl_mutex, NULL, MUTEX_DRIVER,
621 				(void *)(uintptr_t)si_ctlp->sictl_intr_pri);
622 
623 		attach_state |= ATTACH_PROGRESS_MUTEX_INIT;
624 
625 		/*
626 		 * Initialize the controller and driver core.
627 		 */
628 		si_ctlp->sictl_flags |= SI_ATTACH;
629 		status = si_initialize_controller(si_ctlp);
630 		si_ctlp->sictl_flags &= ~SI_ATTACH;
631 		if (status) {
632 			goto err_out;
633 		}
634 
635 		attach_state |= ATTACH_PROGRESS_HW_INIT;
636 
637 		if (si_register_sata_hba_tran(si_ctlp)) {
638 			SIDBG0(SIDBG_INIT, NULL,
639 				"si3124: setting sata hba tran failed");
640 			goto err_out;
641 		}
642 
643 		si_ctlp->sictl_timeout_id = timeout(
644 					(void (*)(void *))si_watchdog_handler,
645 					(caddr_t)si_ctlp, si_watchdog_tick);
646 
647 		si_ctlp->sictl_power_level = PM_LEVEL_D0;
648 
649 		return (DDI_SUCCESS);
650 
651 	case DDI_RESUME:
652 		si_ctlp = ddi_get_soft_state(si_statep, instance);
653 
654 		status = si_initialize_controller(si_ctlp);
655 		if (status) {
656 			return (DDI_FAILURE);
657 		}
658 
659 		si_ctlp->sictl_timeout_id = timeout(
660 					(void (*)(void *))si_watchdog_handler,
661 					(caddr_t)si_ctlp, si_watchdog_tick);
662 
663 		(void) pm_power_has_changed(dip, 0, PM_LEVEL_D0);
664 
665 		/* Notify SATA framework about RESUME. */
666 		if (sata_hba_attach(si_ctlp->sictl_devinfop,
667 				si_ctlp->sictl_sata_hba_tran,
668 				DDI_RESUME) != DDI_SUCCESS) {
669 			return (DDI_FAILURE);
670 		}
671 
672 		/*
673 		 * Notify the "framework" that it should reprobe ports to see
674 		 * if any device got changed while suspended.
675 		 */
676 		bzero((void *)&sdevice, sizeof (sata_device_t));
677 		sata_hba_event_notify(dip, &sdevice,
678 					SATA_EVNT_PWR_LEVEL_CHANGED);
679 		SIDBG0(SIDBG_INIT|SIDBG_EVENT, si_ctlp,
680 			"sending event up: SATA_EVNT_PWR_LEVEL_CHANGED");
681 
682 		(void) pm_idle_component(si_ctlp->sictl_devinfop, 0);
683 
684 		si_ctlp->sictl_power_level = PM_LEVEL_D0;
685 
686 		return (DDI_SUCCESS);
687 
688 	default:
689 		return (DDI_FAILURE);
690 
691 	}
692 
693 err_out:
694 	if (attach_state & ATTACH_PROGRESS_HW_INIT) {
695 		si_ctlp->sictl_flags |= SI_DETACH;
696 		/* We want to set SI_DETACH to deallocate all memory */
697 		si_deinititalize_controller(si_ctlp);
698 		si_ctlp->sictl_flags &= ~SI_DETACH;
699 	}
700 
701 	if (attach_state & ATTACH_PROGRESS_MUTEX_INIT) {
702 		mutex_destroy(&si_ctlp->sictl_mutex);
703 	}
704 
705 	if (attach_state & ATTACH_PROGRESS_INTR_ADDED) {
706 		si_rem_intrs(si_ctlp);
707 	}
708 
709 	if (attach_state & ATTACH_PROGRESS_BAR1_MAP) {
710 		ddi_regs_map_free(&si_ctlp->sictl_port_acc_handle);
711 	}
712 
713 	if (attach_state & ATTACH_PROGRESS_BAR0_MAP) {
714 		ddi_regs_map_free(&si_ctlp->sictl_global_acc_handle);
715 	}
716 
717 	if (attach_state & ATTACH_PROGRESS_CONF_HANDLE) {
718 		pci_config_teardown(&si_ctlp->sictl_pci_conf_handle);
719 	}
720 
721 	if (attach_state & ATTACH_PROGRESS_STATEP_ALLOC) {
722 		ddi_soft_state_free(si_statep, instance);
723 	}
724 
725 	return (DDI_FAILURE);
726 }
727 
728 
729 /*
730  * The detach entry point for dev_ops.
731  *
732  * We undo the things we did in si_attach().
733  */
734 static int
735 si_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
736 {
737 	si_ctl_state_t *si_ctlp;
738 	int instance;
739 
740 	SIDBG0(SIDBG_INIT|SIDBG_ENTRY, NULL, "si_detach enter");
741 	instance = ddi_get_instance(dip);
742 	si_ctlp = ddi_get_soft_state(si_statep, instance);
743 
744 	switch (cmd) {
745 
746 	case DDI_DETACH:
747 
748 		mutex_enter(&si_ctlp->sictl_mutex);
749 
750 		/* disable the interrupts for an uninterrupted detach */
751 		si_disable_all_interrupts(si_ctlp);
752 
753 		mutex_exit(&si_ctlp->sictl_mutex);
754 		/* unregister from the sata framework. */
755 		if (si_unregister_sata_hba_tran(si_ctlp) != SI_SUCCESS) {
756 			si_enable_all_interrupts(si_ctlp);
757 			return (DDI_FAILURE);
758 		}
759 		mutex_enter(&si_ctlp->sictl_mutex);
760 
761 		/* now cancel the timeout handler. */
762 		si_ctlp->sictl_flags |= SI_NO_TIMEOUTS;
763 		(void) untimeout(si_ctlp->sictl_timeout_id);
764 		si_ctlp->sictl_flags &= ~SI_NO_TIMEOUTS;
765 
766 		/* deinitialize the controller. */
767 		si_ctlp->sictl_flags |= SI_DETACH;
768 		si_deinititalize_controller(si_ctlp);
769 		si_ctlp->sictl_flags &= ~SI_DETACH;
770 
771 		/* destroy any mutexes */
772 		mutex_exit(&si_ctlp->sictl_mutex);
773 		mutex_destroy(&si_ctlp->sictl_mutex);
774 
775 		/* remove the interrupts */
776 		si_rem_intrs(si_ctlp);
777 
778 		/* remove the reg maps. */
779 		ddi_regs_map_free(&si_ctlp->sictl_port_acc_handle);
780 		ddi_regs_map_free(&si_ctlp->sictl_global_acc_handle);
781 		pci_config_teardown(&si_ctlp->sictl_pci_conf_handle);
782 
783 		/* free the soft state. */
784 		ddi_soft_state_free(si_statep, instance);
785 
786 		return (DDI_SUCCESS);
787 
788 	case DDI_SUSPEND:
789 		/* Inform SATA framework */
790 		if (sata_hba_detach(dip, cmd) != DDI_SUCCESS) {
791 			return (DDI_FAILURE);
792 		}
793 
794 		mutex_enter(&si_ctlp->sictl_mutex);
795 
796 		/*
797 		 * Device needs to be at full power in case it is needed to
798 		 * handle dump(9e) to save CPR state after DDI_SUSPEND
799 		 * completes.  This is OK since presumably power will be
800 		 * removed anyways.  No outstanding transactions should be
801 		 * on the controller since the children are already quiesed.
802 		 *
803 		 * If any ioctls/cfgadm support is added that touches
804 		 * hardware, those entry points will need to check for
805 		 * suspend and then block or return errors until resume.
806 		 *
807 		 */
808 		if (pm_busy_component(si_ctlp->sictl_devinfop, 0) ==
809 								DDI_SUCCESS) {
810 			mutex_exit(&si_ctlp->sictl_mutex);
811 			(void) pm_raise_power(si_ctlp->sictl_devinfop, 0,
812 							PM_LEVEL_D0);
813 			mutex_enter(&si_ctlp->sictl_mutex);
814 		}
815 
816 		si_deinititalize_controller(si_ctlp);
817 
818 		si_ctlp->sictl_flags |= SI_NO_TIMEOUTS;
819 		(void) untimeout(si_ctlp->sictl_timeout_id);
820 		si_ctlp->sictl_flags &= ~SI_NO_TIMEOUTS;
821 
822 		SIDBG1(SIDBG_POWER, NULL, "si3124%d: DDI_SUSPEND", instance);
823 
824 		mutex_exit(&si_ctlp->sictl_mutex);
825 
826 		return (DDI_SUCCESS);
827 
828 	default:
829 		return (DDI_FAILURE);
830 
831 	}
832 
833 }
834 
835 static int
836 si_power(dev_info_t *dip, int component, int level)
837 {
838 #ifndef __lock_lint
839 	_NOTE(ARGUNUSED(component))
840 #endif /* __lock_lint */
841 
842 	si_ctl_state_t *si_ctlp;
843 	int instance = ddi_get_instance(dip);
844 	int rval = DDI_SUCCESS;
845 	int old_level;
846 	sata_device_t sdevice;
847 
848 	si_ctlp = ddi_get_soft_state(si_statep, instance);
849 
850 	if (si_ctlp == NULL) {
851 		return (DDI_FAILURE);
852 	}
853 
854 	SIDBG0(SIDBG_ENTRY, NULL, "si_power enter");
855 
856 	mutex_enter(&si_ctlp->sictl_mutex);
857 	old_level = si_ctlp->sictl_power_level;
858 
859 	switch (level) {
860 	case PM_LEVEL_D0: /* fully on */
861 		pci_config_put16(si_ctlp->sictl_pci_conf_handle,
862 			PM_CSR(si_ctlp->sictl_devid), PCI_PMCSR_D0);
863 #ifndef __lock_lint
864 		delay(drv_usectohz(10000));
865 #endif /* __lock_lint */
866 		si_ctlp->sictl_power_level = PM_LEVEL_D0;
867 		(void) pci_restore_config_regs(si_ctlp->sictl_devinfop);
868 
869 		SIDBG2(SIDBG_POWER, si_ctlp,
870 			"si3124%d: turning power ON. old level %d",
871 			instance, old_level);
872 		/*
873 		 * If called from attach, just raise device power,
874 		 * restore config registers (if they were saved
875 		 * from a previous detach that lowered power),
876 		 * and exit.
877 		 */
878 		if (si_ctlp->sictl_flags & SI_ATTACH)
879 			break;
880 
881 		mutex_exit(&si_ctlp->sictl_mutex);
882 		(void) si_initialize_controller(si_ctlp);
883 		mutex_enter(&si_ctlp->sictl_mutex);
884 
885 		si_ctlp->sictl_timeout_id = timeout(
886 					(void (*)(void *))si_watchdog_handler,
887 					(caddr_t)si_ctlp, si_watchdog_tick);
888 
889 		bzero((void *)&sdevice, sizeof (sata_device_t));
890 		sata_hba_event_notify(
891 			si_ctlp->sictl_sata_hba_tran->sata_tran_hba_dip,
892 			&sdevice, SATA_EVNT_PWR_LEVEL_CHANGED);
893 		SIDBG0(SIDBG_EVENT|SIDBG_POWER, si_ctlp,
894 				"sending event up: PWR_LEVEL_CHANGED");
895 
896 		break;
897 
898 	case PM_LEVEL_D3: /* fully off */
899 		if (!(si_ctlp->sictl_flags & SI_DETACH)) {
900 			si_ctlp->sictl_flags |= SI_NO_TIMEOUTS;
901 			(void) untimeout(si_ctlp->sictl_timeout_id);
902 			si_ctlp->sictl_flags &= ~SI_NO_TIMEOUTS;
903 
904 			si_deinititalize_controller(si_ctlp);
905 
906 			si_ctlp->sictl_power_level = PM_LEVEL_D3;
907 		}
908 
909 		(void) pci_save_config_regs(si_ctlp->sictl_devinfop);
910 
911 		pci_config_put16(si_ctlp->sictl_pci_conf_handle,
912 			PM_CSR(si_ctlp->sictl_devid), PCI_PMCSR_D3HOT);
913 
914 		SIDBG2(SIDBG_POWER, NULL, "si3124%d: turning power OFF. "
915 		    "old level %d", instance, old_level);
916 
917 		break;
918 
919 	default:
920 		SIDBG2(SIDBG_POWER, NULL, "si3124%d: turning power OFF. "
921 		    "old level %d", instance, old_level);
922 		rval = DDI_FAILURE;
923 		break;
924 	}
925 
926 	mutex_exit(&si_ctlp->sictl_mutex);
927 
928 	return (rval);
929 }
930 
931 
932 /*
933  * The info entry point for dev_ops.
934  *
935  */
936 static int
937 si_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd,
938 		void *arg,
939 		void **result)
940 {
941 #ifndef __lock_lint
942 	_NOTE(ARGUNUSED(dip))
943 #endif /* __lock_lint */
944 	si_ctl_state_t *si_ctlp;
945 	int instance;
946 	dev_t dev;
947 
948 	dev = (dev_t)arg;
949 	instance = getminor(dev);
950 
951 	switch (infocmd) {
952 		case DDI_INFO_DEVT2DEVINFO:
953 			si_ctlp = ddi_get_soft_state(si_statep,  instance);
954 			if (si_ctlp != NULL) {
955 				*result = si_ctlp->sictl_devinfop;
956 				return (DDI_SUCCESS);
957 			} else {
958 				*result = NULL;
959 				return (DDI_FAILURE);
960 			}
961 		case DDI_INFO_DEVT2INSTANCE:
962 			*(int *)result = instance;
963 			break;
964 		default:
965 			break;
966 	}
967 	return (DDI_SUCCESS);
968 }
969 
970 
971 
972 /*
973  * Registers the si3124 with sata framework.
974  */
975 static int
976 si_register_sata_hba_tran(si_ctl_state_t *si_ctlp)
977 {
978 	struct 	sata_hba_tran	*sata_hba_tran;
979 
980 	SIDBG0(SIDBG_INIT|SIDBG_ENTRY, si_ctlp,
981 			"si_register_sata_hba_tran entry");
982 
983 	mutex_enter(&si_ctlp->sictl_mutex);
984 
985 	/* Allocate memory for the sata_hba_tran  */
986 	sata_hba_tran = kmem_zalloc(sizeof (sata_hba_tran_t), KM_SLEEP);
987 
988 	sata_hba_tran->sata_tran_hba_rev = SATA_TRAN_HBA_REV;
989 	sata_hba_tran->sata_tran_hba_dip = si_ctlp->sictl_devinfop;
990 	sata_hba_tran->sata_tran_hba_dma_attr = &buffer_dma_attr;
991 
992 	sata_hba_tran->sata_tran_hba_num_cports = si_ctlp->sictl_num_ports;
993 	sata_hba_tran->sata_tran_hba_features_support = 0;
994 	sata_hba_tran->sata_tran_hba_qdepth = SI_NUM_SLOTS;
995 
996 	sata_hba_tran->sata_tran_probe_port = si_tran_probe_port;
997 	sata_hba_tran->sata_tran_start = si_tran_start;
998 	sata_hba_tran->sata_tran_abort = si_tran_abort;
999 	sata_hba_tran->sata_tran_reset_dport = si_tran_reset_dport;
1000 	sata_hba_tran->sata_tran_selftest = NULL;
1001 	sata_hba_tran->sata_tran_hotplug_ops = &si_tran_hotplug_ops;
1002 	sata_hba_tran->sata_tran_pwrmgt_ops = NULL;
1003 	sata_hba_tran->sata_tran_ioctl = NULL;
1004 	mutex_exit(&si_ctlp->sictl_mutex);
1005 
1006 	/* Attach it to SATA framework */
1007 	if (sata_hba_attach(si_ctlp->sictl_devinfop, sata_hba_tran, DDI_ATTACH)
1008 							!= DDI_SUCCESS) {
1009 		kmem_free((void *)sata_hba_tran, sizeof (sata_hba_tran_t));
1010 		return (SI_FAILURE);
1011 	}
1012 
1013 	mutex_enter(&si_ctlp->sictl_mutex);
1014 	si_ctlp->sictl_sata_hba_tran = sata_hba_tran;
1015 	mutex_exit(&si_ctlp->sictl_mutex);
1016 
1017 	return (SI_SUCCESS);
1018 }
1019 
1020 
1021 /*
1022  * Unregisters the si3124 with sata framework.
1023  */
1024 static int
1025 si_unregister_sata_hba_tran(si_ctl_state_t *si_ctlp)
1026 {
1027 
1028 	/* Detach from the SATA framework. */
1029 	if (sata_hba_detach(si_ctlp->sictl_devinfop, DDI_DETACH) !=
1030 							DDI_SUCCESS) {
1031 		return (SI_FAILURE);
1032 	}
1033 
1034 	/* Deallocate sata_hba_tran. */
1035 	kmem_free((void *)si_ctlp->sictl_sata_hba_tran,
1036 				sizeof (sata_hba_tran_t));
1037 
1038 	si_ctlp->sictl_sata_hba_tran = NULL;
1039 
1040 	return (SI_SUCCESS);
1041 }
1042 
1043 /*
1044  * Called by sata framework to probe a port. We return the
1045  * cached information from a previous hardware probe.
1046  *
1047  * The actual hardware probing itself was done either from within
1048  * si_initialize_controller() during the driver attach or
1049  * from a phy ready change interrupt handler.
1050  */
1051 static int
1052 si_tran_probe_port(dev_info_t *dip, sata_device_t *sd)
1053 {
1054 
1055 	si_ctl_state_t	*si_ctlp;
1056 	uint8_t cport = sd->satadev_addr.cport;
1057 	uint8_t pmport = sd->satadev_addr.pmport;
1058 	uint8_t qual = sd->satadev_addr.qual;
1059 	uint8_t port_type;
1060 	si_port_state_t *si_portp;
1061 	si_portmult_state_t *si_portmultp;
1062 
1063 	si_ctlp = ddi_get_soft_state(si_statep, ddi_get_instance(dip));
1064 
1065 	SIDBG3(SIDBG_ENTRY, si_ctlp,
1066 		"si_tran_probe_port: cport: 0x%x, pmport: 0x%x, qual: 0x%x",
1067 		cport, pmport, qual);
1068 
1069 	if (cport >= SI_MAX_PORTS) {
1070 		sd->satadev_type = SATA_DTYPE_NONE;
1071 		sd->satadev_state = SATA_STATE_PROBED;
1072 		return (SATA_FAILURE);
1073 	}
1074 
1075 	mutex_enter(&si_ctlp->sictl_mutex);
1076 	si_portp = si_ctlp->sictl_ports[cport];
1077 	mutex_exit(&si_ctlp->sictl_mutex);
1078 	if (si_portp == NULL) {
1079 		sd->satadev_type = SATA_DTYPE_NONE;
1080 		sd->satadev_state = SATA_STATE_PROBED;
1081 		return (SATA_FAILURE);
1082 	}
1083 
1084 	mutex_enter(&si_portp->siport_mutex);
1085 
1086 	if (qual == SATA_ADDR_PMPORT) {
1087 		if (pmport >= si_portp->siport_portmult_state.sipm_num_ports) {
1088 			sd->satadev_type = SATA_DTYPE_NONE;
1089 			sd->satadev_state = SATA_STATE_PROBED;
1090 			mutex_exit(&si_portp->siport_mutex);
1091 			return (SATA_FAILURE);
1092 		} else {
1093 			si_portmultp = 	&si_portp->siport_portmult_state;
1094 			port_type = si_portmultp->sipm_port_type[pmport];
1095 		}
1096 	} else {
1097 		port_type = si_portp->siport_port_type;
1098 	}
1099 
1100 	switch (port_type) {
1101 
1102 	case PORT_TYPE_DISK:
1103 		sd->satadev_type = SATA_DTYPE_ATADISK;
1104 		sd->satadev_state = SATA_STATE_PROBED;
1105 		break;
1106 
1107 	case PORT_TYPE_ATAPI:
1108 		sd->satadev_type = SATA_DTYPE_ATAPICD;
1109 		sd->satadev_state = SATA_STATE_PROBED;
1110 		break;
1111 
1112 	case PORT_TYPE_MULTIPLIER:
1113 		sd->satadev_type = SATA_DTYPE_PMULT;
1114 		sd->satadev_add_info =
1115 			si_portp->siport_portmult_state.sipm_num_ports;
1116 		sd->satadev_state = SATA_STATE_PROBED;
1117 		break;
1118 
1119 	case PORT_TYPE_UNKNOWN:
1120 		sd->satadev_type = SATA_DTYPE_UNKNOWN;
1121 		sd->satadev_state = SATA_STATE_PROBED;
1122 
1123 	default:
1124 		/* we don't support any other device types. */
1125 		sd->satadev_type = SATA_DTYPE_NONE;
1126 		sd->satadev_state = SATA_STATE_PROBED;
1127 		break;
1128 	}
1129 
1130 	if (qual == SATA_ADDR_PMPORT) {
1131 		(void) si_read_portmult_reg(si_ctlp, si_portp, cport,
1132 				pmport, PSCR_REG0, &sd->satadev_scr.sstatus);
1133 		(void) si_read_portmult_reg(si_ctlp, si_portp, cport,
1134 				pmport, PSCR_REG1, &sd->satadev_scr.serror);
1135 		(void) si_read_portmult_reg(si_ctlp, si_portp, cport,
1136 				pmport, PSCR_REG2, &sd->satadev_scr.scontrol);
1137 		(void) si_read_portmult_reg(si_ctlp, si_portp, cport,
1138 				pmport, PSCR_REG3, &sd->satadev_scr.sactive);
1139 	} else {
1140 		fill_dev_sregisters(si_ctlp, cport, sd);
1141 		if (!(si_portp->siport_active)) {
1142 			/*
1143 			 * Since we are implementing the port deactivation
1144 			 * in software only, we need to fake a valid value
1145 			 * for sstatus when the device is in deactivated state.
1146 			 */
1147 			SSTATUS_SET_DET(sd->satadev_scr.sstatus,
1148 						SSTATUS_DET_PHYOFFLINE);
1149 			SSTATUS_SET_IPM(sd->satadev_scr.sstatus,
1150 						SSTATUS_IPM_NODEV_NOPHY);
1151 		}
1152 	}
1153 
1154 	mutex_exit(&si_portp->siport_mutex);
1155 	return (SATA_SUCCESS);
1156 }
1157 
1158 /*
1159  * Called by sata framework to transport a sata packet down stream.
1160  *
1161  * The actual work of building the FIS & transporting it to the hardware
1162  * is done out of the subroutine si_deliver_satapkt().
1163  */
1164 static int
1165 si_tran_start(dev_info_t *dip, sata_pkt_t *spkt)
1166 {
1167 	si_ctl_state_t *si_ctlp;
1168 	uint8_t	cport;
1169 	si_port_state_t *si_portp;
1170 	int slot;
1171 
1172 	cport = spkt->satapkt_device.satadev_addr.cport;
1173 	si_ctlp = ddi_get_soft_state(si_statep, ddi_get_instance(dip));
1174 	mutex_enter(&si_ctlp->sictl_mutex);
1175 	si_portp = si_ctlp->sictl_ports[cport];
1176 	mutex_exit(&si_ctlp->sictl_mutex);
1177 
1178 	SIDBG1(SIDBG_ENTRY, si_ctlp,
1179 		"si_tran_start entry: port: 0x%x", cport);
1180 
1181 	mutex_enter(&si_portp->siport_mutex);
1182 
1183 	if ((si_portp->siport_port_type == PORT_TYPE_NODEV) ||
1184 			!si_portp->siport_active) {
1185 		/*
1186 		 * si_intr_phy_ready_change() may have rendered it to
1187 		 * PORT_TYPE_NODEV. cfgadm operation may have rendered
1188 		 * it inactive.
1189 		 */
1190 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
1191 		fill_dev_sregisters(si_ctlp, cport, &spkt->satapkt_device);
1192 		mutex_exit(&si_portp->siport_mutex);
1193 		return (SATA_TRAN_PORT_ERROR);
1194 	}
1195 
1196 	if (spkt->satapkt_cmd.satacmd_flags.sata_clear_dev_reset) {
1197 		si_portp->siport_reset_in_progress = 0;
1198 		SIDBG1(SIDBG_ENTRY, si_ctlp,
1199 			"si_tran_start clearing the "
1200 			"reset_in_progress for port: 0x%x", cport);
1201 	}
1202 
1203 	if (si_portp->siport_reset_in_progress &&
1204 		! spkt->satapkt_cmd.satacmd_flags.sata_ignore_dev_reset &&
1205 		! ddi_in_panic()) {
1206 
1207 		spkt->satapkt_reason = SATA_PKT_BUSY;
1208 		SIDBG1(SIDBG_ERRS, si_ctlp,
1209 			"si_tran_start returning BUSY while "
1210 			"reset in progress: port: 0x%x", cport);
1211 		mutex_exit(&si_portp->siport_mutex);
1212 		return (SATA_TRAN_BUSY);
1213 	}
1214 
1215 	if (si_portp->mopping_in_progress) {
1216 		spkt->satapkt_reason = SATA_PKT_BUSY;
1217 		SIDBG1(SIDBG_ERRS, si_ctlp,
1218 			"si_tran_start returning BUSY while "
1219 			"mopping in progress: port: 0x%x", cport);
1220 		mutex_exit(&si_portp->siport_mutex);
1221 		return (SATA_TRAN_BUSY);
1222 	}
1223 
1224 	if ((slot = si_deliver_satapkt(si_ctlp, si_portp, cport, spkt))
1225 							== SI_FAILURE) {
1226 		spkt->satapkt_reason = SATA_PKT_QUEUE_FULL;
1227 		SIDBG1(SIDBG_ERRS, si_ctlp,
1228 			"si_tran_start returning QUEUE_FULL: port: 0x%x",
1229 			cport);
1230 		mutex_exit(&si_portp->siport_mutex);
1231 		return (SATA_TRAN_QUEUE_FULL);
1232 	}
1233 
1234 	if (spkt->satapkt_op_mode & (SATA_OPMODE_POLLING|SATA_OPMODE_SYNCH)) {
1235 		/* we need to poll now */
1236 		mutex_exit(&si_portp->siport_mutex);
1237 		si_poll_cmd(si_ctlp, si_portp, cport, slot, spkt);
1238 		mutex_enter(&si_portp->siport_mutex);
1239 	}
1240 
1241 	mutex_exit(&si_portp->siport_mutex);
1242 	return (SATA_TRAN_ACCEPTED);
1243 }
1244 
1245 #define	SENDUP_PACKET(si_portp, satapkt, reason)			\
1246 	if ((satapkt->satapkt_cmd.satacmd_cmd_reg ==			\
1247 					SATAC_WRITE_FPDMA_QUEUED) ||	\
1248 	    (satapkt->satapkt_cmd.satacmd_cmd_reg ==			\
1249 					SATAC_READ_FPDMA_QUEUED)) {	\
1250 		si_portp->siport_pending_ncq_count--;			\
1251 	}								\
1252 	if (satapkt) {							\
1253 		satapkt->satapkt_reason = reason;			\
1254 		/*							\
1255 		 * We set the satapkt_reason in both synch and		\
1256 		 * non-synch cases.					\
1257 		 */							\
1258 	}								\
1259 	if (satapkt &&							\
1260 		!(satapkt->satapkt_op_mode & SATA_OPMODE_SYNCH) &&	\
1261 		satapkt->satapkt_comp) {				\
1262 		mutex_exit(&si_portp->siport_mutex);			\
1263 		(*satapkt->satapkt_comp)(satapkt);			\
1264 		mutex_enter(&si_portp->siport_mutex);			\
1265 	}
1266 
1267 /*
1268  * Mopping is necessitated because of the si3124 hardware limitation.
1269  * The only way to recover from errors or to abort a command is to
1270  * reset the port/device but such a reset also results in throwing
1271  * away all the unfinished pending commands.
1272  *
1273  * A port or device is reset in four scenarios:
1274  *	a) some commands failed with errors
1275  *	b) or we need to timeout some commands
1276  *	c) or we need to abort some commands
1277  *	d) or we need reset the port at the request of sata framework
1278  *
1279  * In all these scenarios, we need to send any pending unfinished
1280  * commands up to sata framework.
1281  *
1282  * Only one mopping process at a time is allowed; this is achieved
1283  * by using siport_mop_mutex.
1284  */
1285 static void
1286 si_mop_commands(si_ctl_state_t *si_ctlp,
1287 		si_port_state_t *si_portp,
1288 		uint8_t	port,
1289 
1290 		uint32_t slot_status,
1291 		uint32_t failed_tags,
1292 		uint32_t timedout_tags,
1293 		uint32_t aborting_tags,
1294 		uint32_t reset_tags)
1295 {
1296 	uint32_t finished_tags, unfinished_tags;
1297 	int tmpslot;
1298 	sata_pkt_t *satapkt;
1299 	si_prb_t *prb;
1300 	uint32_t *prb_word_ptr;
1301 	int i;
1302 
1303 	SIDBG1(SIDBG_ERRS|SIDBG_ENTRY, si_ctlp,
1304 		"si_mop_commands entered: slot_status: 0x%x",
1305 		slot_status);
1306 
1307 	SIDBG4(SIDBG_ERRS|SIDBG_ENTRY, si_ctlp,
1308 		"si_mop_commands: failed_tags: 0x%x, timedout_tags: 0x%x"
1309 		"aborting_tags: 0x%x, reset_tags: 0x%x",
1310 		failed_tags,
1311 		timedout_tags,
1312 		aborting_tags,
1313 		reset_tags);
1314 	/*
1315 	 * We could be here for four reasons: abort, reset,
1316 	 * timeout or error handling. Only one such mopping
1317 	 * is allowed at a time.
1318 	 *
1319 	 * Note that we are already holding the main per port
1320 	 * mutex; all we need now is siport_mop_mutex.
1321 	 */
1322 	mutex_enter(&si_portp->siport_mop_mutex);
1323 	mutex_enter(&si_portp->siport_mutex);
1324 
1325 	si_portp->mopping_in_progress = 1;
1326 
1327 	finished_tags =  si_portp->siport_pending_tags &
1328 					~slot_status & SI_SLOT_MASK;
1329 
1330 	unfinished_tags = slot_status & SI_SLOT_MASK &
1331 			~failed_tags &
1332 			~aborting_tags &
1333 			~reset_tags &
1334 			~timedout_tags;
1335 
1336 	/* Send up the finished_tags with SATA_PKT_COMPLETED. */
1337 	while (finished_tags) {
1338 		tmpslot = ddi_ffs(finished_tags) - 1;
1339 		if (tmpslot == -1) {
1340 			break;
1341 		}
1342 
1343 		satapkt = si_portp->siport_slot_pkts[tmpslot];
1344 		ASSERT(satapkt != NULL);
1345 		prb =  &si_portp->siport_prbpool[tmpslot];
1346 		ASSERT(prb != NULL);
1347 		satapkt->satapkt_cmd.satacmd_status_reg =
1348 						GET_FIS_COMMAND(prb->prb_fis);
1349 		if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs)
1350 			si_copy_out_regs(&satapkt->satapkt_cmd, &prb->prb_fis);
1351 
1352 		SIDBG1(SIDBG_ERRS, si_ctlp,
1353 			"si_mop_commands sending up completed satapkt: %x",
1354 			satapkt);
1355 		SENDUP_PACKET(si_portp, satapkt, SATA_PKT_COMPLETED);
1356 
1357 		CLEAR_BIT(si_portp->siport_pending_tags, tmpslot);
1358 		CLEAR_BIT(finished_tags, tmpslot);
1359 	}
1360 
1361 	ASSERT(finished_tags == 0);
1362 
1363 	/* Send up failed_tags with SATA_PKT_DEV_ERROR. */
1364 	while (failed_tags) {
1365 		tmpslot = ddi_ffs(failed_tags) - 1;
1366 		if (tmpslot == -1) {
1367 			break;
1368 		}
1369 		SIDBG1(SIDBG_ERRS, si_ctlp, "si3124: si_mop_commands: "
1370 			"handling failed slot: 0x%x", tmpslot);
1371 
1372 		satapkt = si_portp->siport_slot_pkts[tmpslot];
1373 		ASSERT(satapkt != NULL);
1374 		if (satapkt->satapkt_device.satadev_type ==
1375 							SATA_DTYPE_ATAPICD) {
1376 			si_set_sense_data(satapkt, SATA_PKT_DEV_ERROR);
1377 		}
1378 
1379 		/*
1380 		 * The LRAM contains the the modified FIS.
1381 		 * Read the modified FIS to obtain the Error & Status.
1382 		 */
1383 		prb =  &(si_portp->siport_prbpool[tmpslot]);
1384 		prb_word_ptr = (uint32_t *)prb;
1385 		for (i = 0; i < (sizeof (si_prb_t)/4); i++) {
1386 			prb_word_ptr[i] = ddi_get32(
1387 					si_ctlp->sictl_port_acc_handle,
1388 					(uint32_t *)(PORT_LRAM(si_ctlp, port,
1389 					tmpslot)+i*4));
1390 		}
1391 
1392 		satapkt->satapkt_cmd.satacmd_status_reg =
1393 						GET_FIS_COMMAND(prb->prb_fis);
1394 		satapkt->satapkt_cmd.satacmd_error_reg =
1395 						GET_FIS_FEATURES(prb->prb_fis);
1396 		satapkt->satapkt_cmd.satacmd_sec_count_lsb =
1397 					GET_FIS_SECTOR_COUNT(prb->prb_fis);
1398 		satapkt->satapkt_cmd.satacmd_lba_low_lsb =
1399 					GET_FIS_SECTOR(prb->prb_fis);
1400 		satapkt->satapkt_cmd.satacmd_lba_mid_lsb =
1401 					GET_FIS_CYL_LOW(prb->prb_fis);
1402 		satapkt->satapkt_cmd.satacmd_lba_high_lsb =
1403 					GET_FIS_CYL_HI(prb->prb_fis);
1404 		satapkt->satapkt_cmd.satacmd_device_reg =
1405 					GET_FIS_DEV_HEAD(prb->prb_fis);
1406 
1407 		if (satapkt->satapkt_cmd.satacmd_addr_type == ATA_ADDR_LBA48) {
1408 			satapkt->satapkt_cmd.satacmd_sec_count_msb =
1409 					GET_FIS_SECTOR_COUNT_EXP(prb->prb_fis);
1410 			satapkt->satapkt_cmd.satacmd_lba_low_msb =
1411 					GET_FIS_SECTOR_EXP(prb->prb_fis);
1412 			satapkt->satapkt_cmd.satacmd_lba_mid_msb =
1413 					GET_FIS_CYL_LOW_EXP(prb->prb_fis);
1414 			satapkt->satapkt_cmd.satacmd_lba_high_msb =
1415 					GET_FIS_CYL_HI_EXP(prb->prb_fis);
1416 		}
1417 
1418 		if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs)
1419 			si_copy_out_regs(&satapkt->satapkt_cmd, &prb->prb_fis);
1420 
1421 		/*
1422 		 * In the case of NCQ command failures, the error is
1423 		 * overwritten by the one obtained from issuing of a
1424 		 * READ LOG EXTENDED command.
1425 		 */
1426 		if (si_portp->siport_err_tags_SDBERROR & (1 << tmpslot)) {
1427 			satapkt->satapkt_cmd.satacmd_error_reg =
1428 				si_read_log_ext(si_ctlp, si_portp, port);
1429 		}
1430 
1431 		SENDUP_PACKET(si_portp, satapkt, SATA_PKT_DEV_ERROR);
1432 
1433 		CLEAR_BIT(failed_tags, tmpslot);
1434 		CLEAR_BIT(si_portp->siport_pending_tags, tmpslot);
1435 	}
1436 
1437 	ASSERT(failed_tags == 0);
1438 
1439 	/* Send up timedout_tags with SATA_PKT_TIMEOUT. */
1440 	while (timedout_tags) {
1441 		tmpslot = ddi_ffs(timedout_tags) - 1;
1442 		if (tmpslot == -1) {
1443 			break;
1444 		}
1445 
1446 		satapkt = si_portp->siport_slot_pkts[tmpslot];
1447 		ASSERT(satapkt != NULL);
1448 		SIDBG1(SIDBG_ERRS, si_ctlp,
1449 			"si_mop_commands sending "
1450 			"spkt up with PKT_TIMEOUT: %x",
1451 			satapkt);
1452 
1453 		SENDUP_PACKET(si_portp, satapkt, SATA_PKT_TIMEOUT);
1454 
1455 		CLEAR_BIT(si_portp->siport_pending_tags, tmpslot);
1456 		CLEAR_BIT(timedout_tags, tmpslot);
1457 	}
1458 
1459 	ASSERT(timedout_tags == 0);
1460 
1461 	/* Send up aborting packets with SATA_PKT_ABORTED. */
1462 	while (aborting_tags) {
1463 		tmpslot = ddi_ffs(unfinished_tags) - 1;
1464 		if (tmpslot == -1) {
1465 			break;
1466 		}
1467 
1468 		satapkt = si_portp->siport_slot_pkts[tmpslot];
1469 		ASSERT(satapkt != NULL);
1470 		SIDBG1(SIDBG_ERRS, si_ctlp,
1471 			"si_mop_commands aborting spkt: %x",
1472 			satapkt);
1473 		if (satapkt->satapkt_device.satadev_type ==
1474 							SATA_DTYPE_ATAPICD) {
1475 			si_set_sense_data(satapkt, SATA_PKT_ABORTED);
1476 		}
1477 		SENDUP_PACKET(si_portp, satapkt, SATA_PKT_ABORTED);
1478 
1479 		CLEAR_BIT(si_portp->siport_pending_tags, tmpslot);
1480 		CLEAR_BIT(aborting_tags, tmpslot);
1481 
1482 	}
1483 
1484 	ASSERT(aborting_tags == 0);
1485 
1486 	/* Reset tags are sent up to framework with SATA_PKT_RESET. */
1487 	while (reset_tags) {
1488 		tmpslot = ddi_ffs(reset_tags) - 1;
1489 		if (tmpslot == -1) {
1490 			break;
1491 		}
1492 		satapkt = si_portp->siport_slot_pkts[tmpslot];
1493 		ASSERT(satapkt != NULL);
1494 		SIDBG1(SIDBG_ERRS, si_ctlp,
1495 			"si_mop_commands sending PKT_RESET for "
1496 			"reset spkt: %x",
1497 			satapkt);
1498 		SENDUP_PACKET(si_portp, satapkt, SATA_PKT_RESET);
1499 
1500 		CLEAR_BIT(reset_tags, tmpslot);
1501 		CLEAR_BIT(si_portp->siport_pending_tags, tmpslot);
1502 	}
1503 
1504 	ASSERT(reset_tags == 0);
1505 
1506 	/* Send up the unfinished_tags with SATA_PKT_BUSY. */
1507 	while (unfinished_tags) {
1508 		tmpslot = ddi_ffs(unfinished_tags) - 1;
1509 		if (tmpslot == -1) {
1510 			break;
1511 		}
1512 		satapkt = si_portp->siport_slot_pkts[tmpslot];
1513 		ASSERT(satapkt != NULL);
1514 		SIDBG1(SIDBG_ERRS, si_ctlp,
1515 			"si_mop_commands sending PKT_BUSY for "
1516 			"retry spkt: %x",
1517 			satapkt);
1518 		SENDUP_PACKET(si_portp, satapkt, SATA_PKT_BUSY);
1519 
1520 		CLEAR_BIT(unfinished_tags, tmpslot);
1521 		CLEAR_BIT(si_portp->siport_pending_tags, tmpslot);
1522 	}
1523 
1524 	ASSERT(unfinished_tags == 0);
1525 
1526 	si_portp->mopping_in_progress = 0;
1527 
1528 	mutex_exit(&si_portp->siport_mutex);
1529 	mutex_exit(&si_portp->siport_mop_mutex);
1530 
1531 }
1532 
1533 /*
1534  * Called by the sata framework to abort the previously sent packet(s).
1535  *
1536  * We reset the device and mop the commands on the port.
1537  */
1538 static int
1539 si_tran_abort(dev_info_t *dip, sata_pkt_t *spkt, int flag)
1540 {
1541 	uint32_t slot_status;
1542 	uint8_t	port;
1543 	int tmpslot;
1544 	uint32_t aborting_tags;
1545 	uint32_t finished_tags;
1546 	si_port_state_t *si_portp;
1547 	si_ctl_state_t *si_ctlp;
1548 
1549 	port = spkt->satapkt_device.satadev_addr.cport;
1550 	si_ctlp = ddi_get_soft_state(si_statep, ddi_get_instance(dip));
1551 	mutex_enter(&si_ctlp->sictl_mutex);
1552 	si_portp = si_ctlp->sictl_ports[port];
1553 	mutex_exit(&si_ctlp->sictl_mutex);
1554 
1555 	SIDBG1(SIDBG_ENTRY, si_ctlp, "si_tran_abort on port: %x", port);
1556 
1557 	mutex_enter(&si_portp->siport_mutex);
1558 
1559 	if ((si_portp->siport_port_type == PORT_TYPE_NODEV) ||
1560 			!si_portp->siport_active) {
1561 		/*
1562 		 * si_intr_phy_ready_change() may have rendered it to
1563 		 * PORT_TYPE_NODEV. cfgadm operation may have rendered
1564 		 * it inactive.
1565 		 */
1566 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
1567 		fill_dev_sregisters(si_ctlp, port, &spkt->satapkt_device);
1568 		mutex_exit(&si_portp->siport_mutex);
1569 		return (SATA_FAILURE);
1570 	}
1571 
1572 	if (flag == SATA_ABORT_ALL_PACKETS) {
1573 		aborting_tags = si_portp->siport_pending_tags;
1574 	} else {
1575 		/*
1576 		 * Need to abort a single packet.
1577 		 * Search our siport_slot_pkts[] list for matching spkt.
1578 		 */
1579 		aborting_tags = 0xffffffff; /* 0xffffffff is impossible tag */
1580 		for (tmpslot = 0; tmpslot < SI_NUM_SLOTS; tmpslot++) {
1581 			if (si_portp->siport_slot_pkts[tmpslot] == spkt) {
1582 				aborting_tags = (0x1 << tmpslot);
1583 				break;
1584 			}
1585 		}
1586 
1587 		if (aborting_tags == 0xffffffff) {
1588 			/* requested packet is not on pending list. */
1589 			fill_dev_sregisters(si_ctlp, port,
1590 							&spkt->satapkt_device);
1591 			mutex_exit(&si_portp->siport_mutex);
1592 			return (SATA_FAILURE);
1593 		}
1594 	}
1595 
1596 
1597 	slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
1598 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
1599 	(void) si_reset_dport_wait_till_ready(si_ctlp, si_portp,
1600 				port, SI_DEVICE_RESET);
1601 
1602 	/*
1603 	 * Compute which have finished and which need to be retried.
1604 	 *
1605 	 * The finished tags are siport_pending_tags minus the slot_status.
1606 	 * The aborting_tags have to be reduced by finished_tags since we
1607 	 * can't possibly abort a tag which had finished already.
1608 	 */
1609 	finished_tags =  si_portp->siport_pending_tags &
1610 					~slot_status & SI_SLOT_MASK;
1611 	aborting_tags &= ~finished_tags;
1612 
1613 	mutex_exit(&si_portp->siport_mutex);
1614 	si_mop_commands(si_ctlp,
1615 			si_portp,
1616 			port,
1617 			slot_status,
1618 			0, /* failed_tags */
1619 			0, /* timedout_tags */
1620 			aborting_tags,
1621 			0); /* reset_tags */
1622 	mutex_enter(&si_portp->siport_mutex);
1623 
1624 	fill_dev_sregisters(si_ctlp, port, &spkt->satapkt_device);
1625 	mutex_exit(&si_portp->siport_mutex);
1626 	return (SATA_SUCCESS);
1627 }
1628 
1629 
1630 /*
1631  * Used to reject all the pending packets on a port during a reset
1632  * operation.
1633  *
1634  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
1635  * before calling us.
1636  */
1637 static void
1638 si_reject_all_reset_pkts(
1639 	si_ctl_state_t *si_ctlp,
1640 	si_port_state_t *si_portp,
1641 	int port)
1642 {
1643 	uint32_t slot_status;
1644 	uint32_t reset_tags;
1645 
1646 	_NOTE(ASSUMING_PROTECTED(si_portp))
1647 
1648 	SIDBG1(SIDBG_ENTRY, si_ctlp,
1649 			"si_reject_all_reset_pkts on port: %x",
1650 			port);
1651 
1652 	slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
1653 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
1654 
1655 	/* Compute which tags need to be sent up. */
1656 	reset_tags = slot_status & SI_SLOT_MASK;
1657 
1658 	mutex_exit(&si_portp->siport_mutex);
1659 	si_mop_commands(si_ctlp,
1660 			si_portp,
1661 			port,
1662 			slot_status,
1663 			0, /* failed_tags */
1664 			0, /* timedout_tags */
1665 			0, /* aborting_tags */
1666 			reset_tags);
1667 	mutex_enter(&si_portp->siport_mutex);
1668 
1669 }
1670 
1671 
1672 /*
1673  * Called by sata framework to reset a port(s) or device.
1674  *
1675  */
1676 static int
1677 si_tran_reset_dport(dev_info_t *dip, sata_device_t *sd)
1678 {
1679 	si_ctl_state_t	*si_ctlp;
1680 	uint8_t port = sd->satadev_addr.cport;
1681 	int i;
1682 	si_port_state_t *si_portp;
1683 	int retval = SI_SUCCESS;
1684 
1685 	si_ctlp = ddi_get_soft_state(si_statep, ddi_get_instance(dip));
1686 	SIDBG1(SIDBG_ENTRY, si_ctlp,
1687 		"si_tran_reset_port entry: port: 0x%x",
1688 		port);
1689 
1690 	switch (sd->satadev_addr.qual) {
1691 	case SATA_ADDR_CPORT:
1692 		mutex_enter(&si_ctlp->sictl_mutex);
1693 		si_portp = si_ctlp->sictl_ports[port];
1694 		mutex_exit(&si_ctlp->sictl_mutex);
1695 
1696 		mutex_enter(&si_portp->siport_mutex);
1697 		retval = si_reset_dport_wait_till_ready(si_ctlp, si_portp, port,
1698 							SI_PORT_RESET);
1699 		si_reject_all_reset_pkts(si_ctlp,  si_portp, port);
1700 		mutex_exit(&si_portp->siport_mutex);
1701 
1702 		break;
1703 
1704 	case SATA_ADDR_DCPORT:
1705 		mutex_enter(&si_ctlp->sictl_mutex);
1706 		si_portp = si_ctlp->sictl_ports[port];
1707 		mutex_exit(&si_ctlp->sictl_mutex);
1708 
1709 		mutex_enter(&si_portp->siport_mutex);
1710 
1711 		if ((si_portp->siport_port_type == PORT_TYPE_NODEV) ||
1712 				!si_portp->siport_active) {
1713 			mutex_exit(&si_portp->siport_mutex);
1714 			retval = SI_FAILURE;
1715 			break;
1716 		}
1717 
1718 		retval = si_reset_dport_wait_till_ready(si_ctlp, si_portp, port,
1719 							SI_DEVICE_RESET);
1720 		si_reject_all_reset_pkts(si_ctlp,  si_portp, port);
1721 		mutex_exit(&si_portp->siport_mutex);
1722 
1723 		break;
1724 
1725 	case SATA_ADDR_CNTRL:
1726 		for (i = 0; i < si_ctlp->sictl_num_ports; i++) {
1727 			mutex_enter(&si_ctlp->sictl_mutex);
1728 			si_portp = si_ctlp->sictl_ports[port];
1729 			mutex_exit(&si_ctlp->sictl_mutex);
1730 
1731 			mutex_enter(&si_portp->siport_mutex);
1732 			retval = si_reset_dport_wait_till_ready(si_ctlp,
1733 						si_portp, i, SI_PORT_RESET);
1734 			if (retval) {
1735 				mutex_exit(&si_portp->siport_mutex);
1736 				break;
1737 			}
1738 			si_reject_all_reset_pkts(si_ctlp,  si_portp, port);
1739 			mutex_exit(&si_portp->siport_mutex);
1740 		}
1741 		break;
1742 
1743 	case SATA_ADDR_PMPORT:
1744 	case SATA_ADDR_DPMPORT:
1745 		SIDBG0(SIDBG_VERBOSE, si_ctlp,
1746 			"port mult reset not implemented yet");
1747 		/* FALLSTHROUGH */
1748 
1749 	default:
1750 		retval = SI_FAILURE;
1751 
1752 	}
1753 
1754 	return (retval);
1755 }
1756 
1757 
1758 /*
1759  * Called by sata framework to activate a port as part of hotplug.
1760  *
1761  * Note: Not port-mult aware.
1762  */
1763 static int
1764 si_tran_hotplug_port_activate(dev_info_t *dip, sata_device_t *satadev)
1765 {
1766 	si_ctl_state_t *si_ctlp;
1767 	si_port_state_t *si_portp;
1768 	uint8_t	port;
1769 
1770 	si_ctlp = ddi_get_soft_state(si_statep, ddi_get_instance(dip));
1771 	port = satadev->satadev_addr.cport;
1772 	mutex_enter(&si_ctlp->sictl_mutex);
1773 	si_portp = si_ctlp->sictl_ports[port];
1774 	mutex_exit(&si_ctlp->sictl_mutex);
1775 
1776 	SIDBG0(SIDBG_ENTRY, si_ctlp, "si_tran_hotplug_port_activate entry");
1777 
1778 	mutex_enter(&si_portp->siport_mutex);
1779 	si_enable_port_interrupts(si_ctlp, port);
1780 
1781 	/*
1782 	 * Reset the device so that a si_find_dev_signature() would trigger.
1783 	 * But this reset is an internal operation; the sata framework does
1784 	 * not need to know about it.
1785 	 */
1786 	(void) si_reset_dport_wait_till_ready(si_ctlp, si_portp, port,
1787 					SI_DEVICE_RESET|SI_RESET_NO_EVENTS_UP);
1788 
1789 	satadev->satadev_state = SATA_STATE_READY;
1790 
1791 	si_portp->siport_active = PORT_ACTIVE;
1792 
1793 	fill_dev_sregisters(si_ctlp, port, satadev);
1794 
1795 	mutex_exit(&si_portp->siport_mutex);
1796 	return (SATA_SUCCESS);
1797 }
1798 
1799 /*
1800  * Called by sata framework to deactivate a port as part of hotplug.
1801  *
1802  * Note: Not port-mult aware.
1803  */
1804 static int
1805 si_tran_hotplug_port_deactivate(dev_info_t *dip, sata_device_t *satadev)
1806 {
1807 	si_ctl_state_t *si_ctlp;
1808 	si_port_state_t *si_portp;
1809 	uint8_t	port;
1810 
1811 	si_ctlp = ddi_get_soft_state(si_statep, ddi_get_instance(dip));
1812 	port = satadev->satadev_addr.cport;
1813 	mutex_enter(&si_ctlp->sictl_mutex);
1814 	si_portp = si_ctlp->sictl_ports[port];
1815 	mutex_exit(&si_ctlp->sictl_mutex);
1816 
1817 	SIDBG0(SIDBG_ENTRY, NULL, "si_tran_hotplug_port_deactivate entry");
1818 
1819 	mutex_enter(&si_portp->siport_mutex);
1820 	if (si_portp->siport_pending_tags & SI_SLOT_MASK) {
1821 		/*
1822 		 * There are pending commands on this port.
1823 		 * Fail the deactivate request.
1824 		 */
1825 		satadev->satadev_state = SATA_STATE_READY;
1826 		mutex_exit(&si_portp->siport_mutex);
1827 		return (SATA_FAILURE);
1828 	}
1829 
1830 	/* mark the device as not accessible any more. */
1831 	si_portp->siport_active = PORT_INACTIVE;
1832 
1833 	/* disable the interrupts on the port. */
1834 	si_disable_port_interrupts(si_ctlp, port);
1835 
1836 	satadev->satadev_state = SATA_PSTATE_SHUTDOWN;
1837 
1838 	fill_dev_sregisters(si_ctlp, port, satadev);
1839 	/*
1840 	 * Since we are implementing the port deactivation in software only,
1841 	 * we need to fake a valid value for sstatus.
1842 	 */
1843 	SSTATUS_SET_DET(satadev->satadev_scr.sstatus, SSTATUS_DET_PHYOFFLINE);
1844 	SSTATUS_SET_IPM(satadev->satadev_scr.sstatus, SSTATUS_IPM_NODEV_NOPHY);
1845 
1846 	mutex_exit(&si_portp->siport_mutex);
1847 	return (SATA_SUCCESS);
1848 }
1849 
1850 
1851 /*
1852  * Allocates the si_port_state_t.
1853  */
1854 static int
1855 si_alloc_port_state(si_ctl_state_t *si_ctlp, int port)
1856 {
1857 	si_port_state_t *si_portp;
1858 
1859 	si_ctlp->sictl_ports[port] = (si_port_state_t *)kmem_zalloc(
1860 					sizeof (si_port_state_t), KM_SLEEP);
1861 
1862 	si_portp = si_ctlp->sictl_ports[port];
1863 	mutex_init(&si_portp->siport_mutex, NULL, MUTEX_DRIVER,
1864 				(void *)(uintptr_t)si_ctlp->sictl_intr_pri);
1865 	mutex_init(&si_portp->siport_mop_mutex, NULL, MUTEX_DRIVER,
1866 				(void *)(uintptr_t)si_ctlp->sictl_intr_pri);
1867 	mutex_enter(&si_portp->siport_mutex);
1868 
1869 	/* allocate prb & sgt pkts for this port. */
1870 	if (si_alloc_prbpool(si_ctlp, port)) {
1871 		mutex_exit(&si_portp->siport_mutex);
1872 		kmem_free(si_ctlp->sictl_ports[port], sizeof (si_port_state_t));
1873 		return (SI_FAILURE);
1874 	}
1875 	if (si_alloc_sgbpool(si_ctlp, port)) {
1876 		si_dealloc_prbpool(si_ctlp, port);
1877 		mutex_exit(&si_portp->siport_mutex);
1878 		kmem_free(si_ctlp->sictl_ports[port], sizeof (si_port_state_t));
1879 		return (SI_FAILURE);
1880 	}
1881 
1882 	si_portp->siport_active = PORT_ACTIVE;
1883 	mutex_exit(&si_portp->siport_mutex);
1884 
1885 	return (SI_SUCCESS);
1886 
1887 }
1888 
1889 /*
1890  * Deallocates the si_port_state_t.
1891  */
1892 static void
1893 si_dealloc_port_state(si_ctl_state_t *si_ctlp, int port)
1894 {
1895 	si_port_state_t *si_portp;
1896 	si_portp = si_ctlp->sictl_ports[port];
1897 
1898 	mutex_enter(&si_portp->siport_mutex);
1899 	si_dealloc_sgbpool(si_ctlp, port);
1900 	si_dealloc_prbpool(si_ctlp, port);
1901 	mutex_exit(&si_portp->siport_mutex);
1902 
1903 	mutex_destroy(&si_portp->siport_mutex);
1904 	mutex_destroy(&si_portp->siport_mop_mutex);
1905 
1906 	kmem_free(si_ctlp->sictl_ports[port], sizeof (si_port_state_t));
1907 
1908 }
1909 
1910 /*
1911  * Allocates the SGB (Scatter Gather Block) incore buffer.
1912  */
1913 static int
1914 si_alloc_sgbpool(si_ctl_state_t *si_ctlp, int port)
1915 {
1916 	si_port_state_t *si_portp;
1917 	uint_t cookie_count;
1918 	size_t incore_sgbpool_size = SI_NUM_SLOTS * sizeof (si_sgblock_t);
1919 	size_t ret_len;
1920 	ddi_dma_cookie_t sgbpool_dma_cookie;
1921 
1922 	si_portp = si_ctlp->sictl_ports[port];
1923 
1924 	/* allocate sgbpool dma handle. */
1925 	if (ddi_dma_alloc_handle(si_ctlp->sictl_devinfop,
1926 				&prb_sgt_dma_attr,
1927 				DDI_DMA_SLEEP,
1928 				NULL,
1929 				&si_portp->siport_sgbpool_dma_handle) !=
1930 								DDI_SUCCESS) {
1931 
1932 		return (SI_FAILURE);
1933 	}
1934 
1935 	/* allocate the memory for sgbpool. */
1936 	if (ddi_dma_mem_alloc(si_portp->siport_sgbpool_dma_handle,
1937 				incore_sgbpool_size,
1938 				&accattr,
1939 				DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
1940 				DDI_DMA_SLEEP,
1941 				NULL,
1942 				(caddr_t *)&si_portp->siport_sgbpool,
1943 				&ret_len,
1944 				&si_portp->siport_sgbpool_acc_handle) != NULL) {
1945 
1946 		/*  error.. free the dma handle. */
1947 		ddi_dma_free_handle(&si_portp->siport_sgbpool_dma_handle);
1948 		return (SI_FAILURE);
1949 	}
1950 
1951 	/* now bind it */
1952 	if (ddi_dma_addr_bind_handle(si_portp->siport_sgbpool_dma_handle,
1953 				NULL,
1954 				(caddr_t)si_portp->siport_sgbpool,
1955 				incore_sgbpool_size,
1956 				DDI_DMA_CONSISTENT,
1957 				DDI_DMA_SLEEP,
1958 				NULL,
1959 				&sgbpool_dma_cookie,
1960 				&cookie_count) !=  DDI_DMA_MAPPED) {
1961 		/*  error.. free the dma handle & free the memory. */
1962 		ddi_dma_mem_free(&si_portp->siport_sgbpool_acc_handle);
1963 		ddi_dma_free_handle(&si_portp->siport_sgbpool_dma_handle);
1964 		return (SI_FAILURE);
1965 	}
1966 
1967 	si_portp->siport_sgbpool_physaddr = sgbpool_dma_cookie.dmac_laddress;
1968 	return (SI_SUCCESS);
1969 }
1970 
1971 /*
1972  * Deallocates the SGB (Scatter Gather Block) incore buffer.
1973  */
1974 static void
1975 si_dealloc_sgbpool(si_ctl_state_t *si_ctlp, int port)
1976 {
1977 	si_port_state_t *si_portp = si_ctlp->sictl_ports[port];
1978 
1979 	/* Unbind the dma handle first. */
1980 	(void) ddi_dma_unbind_handle(si_portp->siport_sgbpool_dma_handle);
1981 
1982 	/* Then free the underlying memory. */
1983 	ddi_dma_mem_free(&si_portp->siport_sgbpool_acc_handle);
1984 
1985 	/* Now free the handle itself. */
1986 	ddi_dma_free_handle(&si_portp->siport_sgbpool_dma_handle);
1987 
1988 }
1989 
1990 /*
1991  * Allocates the PRB (Port Request Block) incore packets.
1992  */
1993 static int
1994 si_alloc_prbpool(si_ctl_state_t *si_ctlp, int port)
1995 {
1996 	si_port_state_t *si_portp;
1997 	uint_t cookie_count;
1998 	size_t incore_pkt_size = SI_NUM_SLOTS * sizeof (si_prb_t);
1999 	size_t ret_len;
2000 	ddi_dma_cookie_t prbpool_dma_cookie;
2001 
2002 	si_portp = si_ctlp->sictl_ports[port];
2003 
2004 	/* allocate prb pkts. */
2005 	if (ddi_dma_alloc_handle(si_ctlp->sictl_devinfop,
2006 				&prb_sgt_dma_attr,
2007 				DDI_DMA_SLEEP,
2008 				NULL,
2009 				&si_portp->siport_prbpool_dma_handle) !=
2010 								DDI_SUCCESS) {
2011 
2012 		return (SI_FAILURE);
2013 	}
2014 
2015 	if (ddi_dma_mem_alloc(si_portp->siport_prbpool_dma_handle,
2016 				incore_pkt_size,
2017 				&accattr,
2018 				DDI_DMA_RDWR | DDI_DMA_CONSISTENT,
2019 				DDI_DMA_SLEEP,
2020 				NULL,
2021 				(caddr_t *)&si_portp->siport_prbpool,
2022 				&ret_len,
2023 				&si_portp->siport_prbpool_acc_handle) != NULL) {
2024 
2025 		/* error.. free the dma handle. */
2026 		ddi_dma_free_handle(&si_portp->siport_prbpool_dma_handle);
2027 		return (SI_FAILURE);
2028 	}
2029 
2030 	if (ddi_dma_addr_bind_handle(si_portp->siport_prbpool_dma_handle,
2031 				NULL,
2032 				(caddr_t)si_portp->siport_prbpool,
2033 				incore_pkt_size,
2034 				DDI_DMA_CONSISTENT,
2035 				DDI_DMA_SLEEP,
2036 				NULL,
2037 				&prbpool_dma_cookie,
2038 				&cookie_count) !=  DDI_DMA_MAPPED) {
2039 		/*  error.. free the dma handle & free the memory. */
2040 		ddi_dma_mem_free(&si_portp->siport_prbpool_acc_handle);
2041 		ddi_dma_free_handle(&si_portp->siport_prbpool_dma_handle);
2042 		return (SI_FAILURE);
2043 	}
2044 
2045 	si_portp->siport_prbpool_physaddr =
2046 			prbpool_dma_cookie.dmac_laddress;
2047 	return (SI_SUCCESS);
2048 }
2049 
2050 /*
2051  * Deallocates the PRB (Port Request Block) incore packets.
2052  */
2053 static void
2054 si_dealloc_prbpool(si_ctl_state_t *si_ctlp, int port)
2055 {
2056 	si_port_state_t *si_portp = si_ctlp->sictl_ports[port];
2057 
2058 	/* Unbind the prb dma handle first. */
2059 	(void) ddi_dma_unbind_handle(si_portp->siport_prbpool_dma_handle);
2060 
2061 	/* Then free the underlying memory. */
2062 	ddi_dma_mem_free(&si_portp->siport_prbpool_acc_handle);
2063 
2064 	/* Now free the handle itself. */
2065 	ddi_dma_free_handle(&si_portp->siport_prbpool_dma_handle);
2066 
2067 }
2068 
2069 
2070 
2071 /*
2072  * Soft-reset the port to find the signature of the device connected to
2073  * the port.
2074  */
2075 static void
2076 si_find_dev_signature(
2077 	si_ctl_state_t *si_ctlp,
2078 	si_port_state_t *si_portp,
2079 	int port,
2080 	int pmp)
2081 {
2082 	si_prb_t *prb;
2083 	uint32_t slot_status, signature;
2084 	int slot, loop_count;
2085 
2086 	SIDBG2(SIDBG_ENTRY|SIDBG_INIT, si_ctlp,
2087 		"si_find_dev_signature enter: port: %x, pmp: %x",
2088 		port, pmp);
2089 
2090 	/* Build a Soft Reset PRB in host memory. */
2091 	mutex_enter(&si_portp->siport_mutex);
2092 
2093 	slot = si_claim_free_slot(si_ctlp, si_portp, port);
2094 	if (slot == -1) {
2095 		/* Empty slot could not be found. */
2096 		if (pmp != PORTMULT_CONTROL_PORT) {
2097 			/* We are behind port multiplier. */
2098 			si_portp->siport_portmult_state.sipm_port_type[pmp] =
2099 					PORT_TYPE_NODEV;
2100 		} else {
2101 			si_portp->siport_port_type = PORT_TYPE_NODEV;
2102 		}
2103 
2104 		mutex_exit(&si_portp->siport_mutex);
2105 		return;
2106 	}
2107 	prb = &si_portp->siport_prbpool[slot];
2108 	bzero((void *)prb, sizeof (si_prb_t));
2109 
2110 	SET_FIS_PMP(prb->prb_fis, pmp);
2111 	SET_PRB_CONTROL_SOFT_RESET(prb);
2112 
2113 #if SI_DEBUG
2114 	if (si_debug_flags & SIDBG_DUMP_PRB) {
2115 		char *ptr;
2116 		int j;
2117 
2118 		ptr = (char *)prb;
2119 		cmn_err(CE_WARN, "si_find_dev_signature, prb: ");
2120 		for (j = 0; j < (sizeof (si_prb_t)); j++) {
2121 			if (j%4 == 0) {
2122 				cmn_err(CE_WARN, "----");
2123 			}
2124 			cmn_err(CE_WARN, "%x ", ptr[j]);
2125 		}
2126 
2127 	}
2128 #endif /* SI_DEBUG */
2129 
2130 	/* deliver soft reset prb to empty slot. */
2131 	POST_PRB_ADDR(si_ctlp, si_portp, port, slot);
2132 
2133 	loop_count = 0;
2134 	/* Loop till the soft reset is finished. */
2135 	do {
2136 		slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
2137 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
2138 
2139 		if (loop_count++ > SI_POLLRATE_SOFT_RESET) {
2140 			/* We are effectively timing out after 10 sec. */
2141 			break;
2142 		}
2143 
2144 		/* Wait for 10 millisec */
2145 #ifndef __lock_lint
2146 		delay(SI_10MS_TICKS);
2147 #endif /* __lock_lint */
2148 
2149 	} while (slot_status & SI_SLOT_MASK & (0x1 << slot));
2150 
2151 	SIDBG2(SIDBG_POLL_LOOP, si_ctlp,
2152 		"si_find_dev_signature: loop count: %d, slot_status: 0x%x",
2153 		loop_count, slot_status);
2154 
2155 	CLEAR_BIT(si_portp->siport_pending_tags, slot);
2156 
2157 	/* Read device signature from command slot. */
2158 	signature = ddi_get32(si_ctlp->sictl_port_acc_handle,
2159 			(uint32_t *)(PORT_SIGNATURE_MSB(si_ctlp, port, slot)));
2160 	signature <<= 8;
2161 	signature |= (0xff & ddi_get32(si_ctlp->sictl_port_acc_handle,
2162 				(uint32_t *)(PORT_SIGNATURE_LSB(si_ctlp,
2163 								port, slot))));
2164 
2165 	SIDBG1(SIDBG_INIT, si_ctlp, "Device signature: 0x%x", signature);
2166 
2167 	if (signature == SI_SIGNATURE_PORT_MULTIPLIER) {
2168 
2169 		SIDBG2(SIDBG_INIT, si_ctlp,
2170 			"Found multiplier at cport: 0x%d, pmport: 0x%x",
2171 			port, pmp);
2172 
2173 		if (pmp != PORTMULT_CONTROL_PORT) {
2174 			/*
2175 			 * It is wrong to chain a port multiplier behind
2176 			 * another port multiplier.
2177 			 */
2178 			si_portp->siport_portmult_state.sipm_port_type[pmp] =
2179 							PORT_TYPE_NODEV;
2180 		} else {
2181 			si_portp->siport_port_type = PORT_TYPE_MULTIPLIER;
2182 			mutex_exit(&si_portp->siport_mutex);
2183 			(void) si_enumerate_port_multiplier(si_ctlp,
2184 							si_portp, port);
2185 			mutex_enter(&si_portp->siport_mutex);
2186 		}
2187 		si_init_port(si_ctlp, port);
2188 
2189 	} else if (signature == SI_SIGNATURE_ATAPI) {
2190 		if (pmp != PORTMULT_CONTROL_PORT) {
2191 			/* We are behind port multiplier. */
2192 			si_portp->siport_portmult_state.sipm_port_type[pmp] =
2193 						PORT_TYPE_ATAPI;
2194 		} else {
2195 			si_portp->siport_port_type = PORT_TYPE_ATAPI;
2196 			si_init_port(si_ctlp, port);
2197 		}
2198 		SIDBG2(SIDBG_INIT, si_ctlp,
2199 			"Found atapi at : cport: %x, pmport: %x",
2200 			port, pmp);
2201 
2202 	} else if (signature == SI_SIGNATURE_DISK) {
2203 
2204 		if (pmp != PORTMULT_CONTROL_PORT) {
2205 			/* We are behind port multiplier. */
2206 			si_portp->siport_portmult_state.sipm_port_type[pmp] =
2207 							PORT_TYPE_DISK;
2208 		} else {
2209 			si_portp->siport_port_type = PORT_TYPE_DISK;
2210 			si_init_port(si_ctlp, port);
2211 		}
2212 		SIDBG2(SIDBG_INIT, si_ctlp,
2213 			"found disk at : cport: %x, pmport: %x",
2214 			port, pmp);
2215 
2216 	} else {
2217 		if (pmp != PORTMULT_CONTROL_PORT) {
2218 			/* We are behind port multiplier. */
2219 			si_portp->siport_portmult_state.sipm_port_type[pmp] =
2220 							PORT_TYPE_UNKNOWN;
2221 		} else {
2222 			si_portp->siport_port_type = PORT_TYPE_UNKNOWN;
2223 		}
2224 		SIDBG3(SIDBG_INIT, si_ctlp,
2225 			"Found unknown signature 0x%x at: port: %x, pmp: %x",
2226 			signature, port, pmp);
2227 	}
2228 
2229 	mutex_exit(&si_portp->siport_mutex);
2230 }
2231 
2232 
2233 /*
2234  * Polls for the completion of the command. This is safe with both
2235  * interrupts enabled or disabled.
2236  */
2237 static void
2238 si_poll_cmd(
2239 	si_ctl_state_t *si_ctlp,
2240 	si_port_state_t *si_portp,
2241 	int port,
2242 	int slot,
2243 	sata_pkt_t *satapkt)
2244 {
2245 	uint32_t slot_status;
2246 	int pkt_timeout_ticks;
2247 	uint32_t port_intr_status;
2248 	int in_panic = ddi_in_panic();
2249 
2250 	SIDBG1(SIDBG_ENTRY, si_ctlp, "si_poll_cmd entered: port: 0x%x", port);
2251 
2252 	pkt_timeout_ticks = drv_usectohz((clock_t)satapkt->satapkt_time *
2253 								1000000);
2254 
2255 	mutex_enter(&si_portp->siport_mutex);
2256 
2257 	/* we start out with SATA_PKT_COMPLETED as the satapkt_reason */
2258 	satapkt->satapkt_reason = SATA_PKT_COMPLETED;
2259 
2260 	do {
2261 		slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
2262 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
2263 
2264 		if (slot_status & SI_SLOT_MASK & (0x1 << slot)) {
2265 			if (in_panic) {
2266 				/*
2267 				 * If we are in panic, we can't rely on
2268 				 * timers; so, busy wait instead of delay().
2269 				 */
2270 				mutex_exit(&si_portp->siport_mutex);
2271 				drv_usecwait(SI_1MS_USECS);
2272 				mutex_enter(&si_portp->siport_mutex);
2273 			} else {
2274 				mutex_exit(&si_portp->siport_mutex);
2275 #ifndef __lock_lint
2276 				delay(SI_1MS_TICKS);
2277 #endif /* __lock_lint */
2278 				mutex_enter(&si_portp->siport_mutex);
2279 			}
2280 		} else {
2281 			break;
2282 		}
2283 
2284 		pkt_timeout_ticks -= SI_1MS_TICKS;
2285 
2286 	} while (pkt_timeout_ticks > 0);
2287 
2288 	if (satapkt->satapkt_reason != SATA_PKT_COMPLETED) {
2289 		/* The si_mop_command() got to our packet before us */
2290 		goto poll_done;
2291 	}
2292 
2293 	/*
2294 	 * Interrupts and timers may not be working properly in a crash dump
2295 	 * situation; we may need to handle all the three conditions here:
2296 	 * successful completion, packet failure and packet timeout.
2297 	 */
2298 	if (IS_ATTENTION_RAISED(slot_status)) { /* error seen on port */
2299 
2300 		port_intr_status = ddi_get32(si_ctlp->sictl_global_acc_handle,
2301 			(uint32_t *)PORT_INTERRUPT_STATUS(si_ctlp, port));
2302 
2303 		SIDBG2(SIDBG_VERBOSE, si_ctlp,
2304 			"si_poll_cmd: port_intr_status: 0x%x, port: %x",
2305 			port_intr_status, port);
2306 
2307 		if (port_intr_status & INTR_COMMAND_ERROR) {
2308 			mutex_exit(&si_portp->siport_mutex);
2309 			(void) si_intr_command_error(si_ctlp, si_portp, port);
2310 			mutex_enter(&si_portp->siport_mutex);
2311 
2312 			goto poll_done;
2313 
2314 			/*
2315 			 * Why do we need to call si_intr_command_error() ?
2316 			 *
2317 			 * Answer: Even if the current packet is not the
2318 			 * offending command, we need to restart the stalled
2319 			 * port; (may be, the interrupts are not working well
2320 			 * in panic condition). The call to routine
2321 			 * si_intr_command_error() will achieve that.
2322 			 *
2323 			 * What if the interrupts are working fine and the
2324 			 * si_intr_command_error() gets called once more from
2325 			 * interrupt context ?
2326 			 *
2327 			 * Answer: The second instance of routine
2328 			 * si_intr_command_error() will not mop anything
2329 			 * since the first error handler has already blown
2330 			 * away the hardware pending queues through reset.
2331 			 *
2332 			 * Will the si_intr_command_error() hurt current
2333 			 * packet ?
2334 			 *
2335 			 * Answer: No.
2336 			 */
2337 		} else {
2338 			/* Ignore any non-error interrupts at this stage */
2339 			ddi_put32(si_ctlp->sictl_port_acc_handle,
2340 				(uint32_t *)(PORT_INTERRUPT_STATUS(si_ctlp,
2341 								port)),
2342 				port_intr_status & INTR_MASK);
2343 		}
2344 
2345 
2346 	} else if (slot_status & SI_SLOT_MASK & (0x1 << slot)) {
2347 		satapkt->satapkt_reason = SATA_PKT_TIMEOUT;
2348 	} /* else: the command completed successfully */
2349 
2350 	if ((satapkt->satapkt_cmd.satacmd_cmd_reg ==
2351 					SATAC_WRITE_FPDMA_QUEUED) ||
2352 	    (satapkt->satapkt_cmd.satacmd_cmd_reg ==
2353 					SATAC_READ_FPDMA_QUEUED)) {
2354 		si_portp->siport_pending_ncq_count--;
2355 	}
2356 
2357 	CLEAR_BIT(si_portp->siport_pending_tags, slot);
2358 
2359 poll_done:
2360 	mutex_exit(&si_portp->siport_mutex);
2361 
2362 	/*
2363 	 * tidbit: What is the interaction of abort with polling ?
2364 	 * What happens if the current polled pkt is aborted in parallel ?
2365 	 *
2366 	 * Answer: Assuming that the si_mop_commands() completes ahead
2367 	 * of polling, all it does is to set the satapkt_reason to
2368 	 * SPKT_PKT_ABORTED. That would be fine with us.
2369 	 *
2370 	 * The same logic applies to reset interacting with polling.
2371 	 */
2372 }
2373 
2374 
2375 /*
2376  * Searches for and claims a free slot.
2377  *
2378  * Returns: 	SI_FAILURE if no slots found
2379  *		claimed slot number if successful
2380  *
2381  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
2382  * before calling us.
2383  */
2384 /*ARGSUSED*/
2385 static int
2386 si_claim_free_slot(si_ctl_state_t *si_ctlp, si_port_state_t *si_portp, int port)
2387 {
2388 	uint32_t free_slots;
2389 	int slot;
2390 
2391 	_NOTE(ASSUMING_PROTECTED(si_portp))
2392 
2393 	SIDBG1(SIDBG_ENTRY, si_ctlp,
2394 		"si_claim_free_slot entry: siport_pending_tags: %x",
2395 		si_portp->siport_pending_tags);
2396 
2397 	free_slots = (~si_portp->siport_pending_tags) & SI_SLOT_MASK;
2398 	slot = ddi_ffs(free_slots) - 1;
2399 	if (slot == -1) {
2400 		SIDBG0(SIDBG_VERBOSE, si_ctlp,
2401 			"si_claim_free_slot: no empty slots");
2402 		return (SI_FAILURE);
2403 	}
2404 
2405 	si_portp->siport_pending_tags |= (0x1 << slot);
2406 	SIDBG1(SIDBG_VERBOSE, si_ctlp, "si_claim_free_slot: found slot: 0x%x",
2407 		slot);
2408 	return (slot);
2409 }
2410 
2411 /*
2412  * Builds the PRB for the sata packet and delivers it to controller.
2413  *
2414  * Returns:
2415  *	slot number if we can obtain a slot successfully
2416  *	otherwise, return SI_FAILURE
2417  *
2418  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
2419  * before calling us.
2420  */
2421 static int
2422 si_deliver_satapkt(
2423 	si_ctl_state_t *si_ctlp,
2424 	si_port_state_t *si_portp,
2425 	int port,
2426 	sata_pkt_t *spkt)
2427 {
2428 	int slot;
2429 	si_prb_t *prb;
2430 	sata_cmd_t *cmd;
2431 	si_sge_t *sgep; /* scatter gather entry pointer */
2432 	si_sgt_t *sgtp; /* scatter gather table pointer */
2433 	si_sgblock_t *sgbp; /* scatter gather block pointer */
2434 	int i, j, cookie_index;
2435 	int ncookies;
2436 	int is_atapi = 0;
2437 	ddi_dma_cookie_t cookie;
2438 
2439 	_NOTE(ASSUMING_PROTECTED(si_portp))
2440 
2441 	slot = si_claim_free_slot(si_ctlp, si_portp, port);
2442 	if (slot == -1) {
2443 		return (SI_FAILURE);
2444 	}
2445 
2446 	if (spkt->satapkt_device.satadev_type == SATA_DTYPE_ATAPICD) {
2447 		is_atapi = 1;
2448 	}
2449 
2450 	if ((si_portp->siport_port_type == PORT_TYPE_NODEV) ||
2451 			!si_portp->siport_active) {
2452 		/*
2453 		 * si_intr_phy_ready_change() may have rendered it to
2454 		 * PORT_TYPE_NODEV. cfgadm operation may have rendered
2455 		 * it inactive.
2456 		 */
2457 		spkt->satapkt_reason = SATA_PKT_PORT_ERROR;
2458 		fill_dev_sregisters(si_ctlp, port, &spkt->satapkt_device);
2459 
2460 		return (SI_FAILURE);
2461 	}
2462 
2463 
2464 	prb =  &(si_portp->siport_prbpool[slot]);
2465 	bzero((void *)prb, sizeof (si_prb_t));
2466 
2467 	cmd = &spkt->satapkt_cmd;
2468 
2469 	SIDBG4(SIDBG_ENTRY, si_ctlp,
2470 		"si_deliver_satpkt entry: cmd_reg: 0x%x, slot: 0x%x, \
2471 		port: %x, satapkt: %x",
2472 		cmd->satacmd_cmd_reg, slot, port, (uint32_t)(intptr_t)spkt);
2473 
2474 	/* Now fill the prb. */
2475 	if (is_atapi) {
2476 		if (spkt->satapkt_cmd.satacmd_flags.sata_data_direction ==
2477 		    SATA_DIR_READ) {
2478 			SET_PRB_CONTROL_PKT_READ(prb);
2479 		} else if (spkt->satapkt_cmd.satacmd_flags.sata_data_direction
2480 		    == SATA_DIR_WRITE) {
2481 			SET_PRB_CONTROL_PKT_WRITE(prb);
2482 		}
2483 	}
2484 
2485 	SET_FIS_TYPE(prb->prb_fis, REGISTER_FIS_H2D);
2486 	if ((spkt->satapkt_device.satadev_addr.qual == SATA_ADDR_PMPORT) ||
2487 	    (spkt->satapkt_device.satadev_addr.qual == SATA_ADDR_DPMPORT)) {
2488 		SET_FIS_PMP(prb->prb_fis,
2489 				spkt->satapkt_device.satadev_addr.pmport);
2490 	}
2491 	SET_FIS_CDMDEVCTL(prb->prb_fis, 1);
2492 	SET_FIS_COMMAND(prb->prb_fis, cmd->satacmd_cmd_reg);
2493 	SET_FIS_FEATURES(prb->prb_fis, cmd->satacmd_features_reg);
2494 	SET_FIS_SECTOR_COUNT(prb->prb_fis, cmd->satacmd_sec_count_lsb);
2495 
2496 	switch (cmd->satacmd_addr_type) {
2497 
2498 	case ATA_ADDR_LBA:
2499 		/* fallthru */
2500 
2501 	case ATA_ADDR_LBA28:
2502 		/* LBA[7:0] */
2503 		SET_FIS_SECTOR(prb->prb_fis, cmd->satacmd_lba_low_lsb);
2504 
2505 		/* LBA[15:8] */
2506 		SET_FIS_CYL_LOW(prb->prb_fis, cmd->satacmd_lba_mid_lsb);
2507 
2508 		/* LBA[23:16] */
2509 		SET_FIS_CYL_HI(prb->prb_fis, cmd->satacmd_lba_high_lsb);
2510 
2511 		/* LBA [27:24] (also called dev_head) */
2512 		SET_FIS_DEV_HEAD(prb->prb_fis, cmd->satacmd_device_reg);
2513 
2514 		break;
2515 
2516 	case ATA_ADDR_LBA48:
2517 		/* LBA[7:0] */
2518 		SET_FIS_SECTOR(prb->prb_fis, cmd->satacmd_lba_low_lsb);
2519 
2520 		/* LBA[15:8] */
2521 		SET_FIS_CYL_LOW(prb->prb_fis, cmd->satacmd_lba_mid_lsb);
2522 
2523 		/* LBA[23:16] */
2524 		SET_FIS_CYL_HI(prb->prb_fis, cmd->satacmd_lba_high_lsb);
2525 
2526 		/* LBA [31:24] */
2527 		SET_FIS_SECTOR_EXP(prb->prb_fis, cmd->satacmd_lba_low_msb);
2528 
2529 		/* LBA [39:32] */
2530 		SET_FIS_CYL_LOW_EXP(prb->prb_fis, cmd->satacmd_lba_mid_msb);
2531 
2532 		/* LBA [47:40] */
2533 		SET_FIS_CYL_HI_EXP(prb->prb_fis, cmd->satacmd_lba_high_msb);
2534 
2535 		/* Set dev_head */
2536 		SET_FIS_DEV_HEAD(prb->prb_fis, cmd->satacmd_device_reg);
2537 
2538 		/* Set the extended sector count and features */
2539 		SET_FIS_SECTOR_COUNT_EXP(prb->prb_fis,
2540 					cmd->satacmd_sec_count_msb);
2541 		SET_FIS_FEATURES_EXP(prb->prb_fis,
2542 					cmd->satacmd_features_reg_ext);
2543 
2544 		break;
2545 
2546 	}
2547 
2548 	if (cmd->satacmd_flags.sata_queued) {
2549 		/*
2550 		 * For queued commands, the TAG for the sector count lsb is
2551 		 * generated from current slot number.
2552 		 */
2553 		SET_FIS_SECTOR_COUNT(prb->prb_fis, slot << 3);
2554 	}
2555 
2556 	if ((cmd->satacmd_cmd_reg == SATAC_WRITE_FPDMA_QUEUED) ||
2557 	    (cmd->satacmd_cmd_reg == SATAC_READ_FPDMA_QUEUED)) {
2558 		si_portp->siport_pending_ncq_count++;
2559 	}
2560 
2561 	/* *** now fill the scatter gather list ******* */
2562 
2563 	if (is_atapi) { /* It is an ATAPI drive */
2564 		/* atapi command goes into sge0 */
2565 		bcopy(cmd->satacmd_acdb, &prb->prb_sge0, sizeof (si_sge_t));
2566 
2567 		/* Now fill sge1 with pointer to external SGT. */
2568 		if (spkt->satapkt_cmd.satacmd_num_dma_cookies) {
2569 			prb->prb_sge1.sge_addr =
2570 				si_portp->siport_sgbpool_physaddr +
2571 				slot*sizeof (si_sgblock_t);
2572 			SET_SGE_LNK(prb->prb_sge1);
2573 		} else {
2574 			SET_SGE_TRM(prb->prb_sge1);
2575 		}
2576 	} else {
2577 		/* Fill the sge0 */
2578 		if (spkt->satapkt_cmd.satacmd_num_dma_cookies) {
2579 			prb->prb_sge0.sge_addr =
2580 				si_portp->siport_sgbpool_physaddr +
2581 				slot*sizeof (si_sgblock_t);
2582 			SET_SGE_LNK(prb->prb_sge0);
2583 
2584 		} else {
2585 			SET_SGE_TRM(prb->prb_sge0);
2586 		}
2587 
2588 		/* sge1 is left empty in non-ATAPI case */
2589 	}
2590 
2591 	bzero(&si_portp->siport_sgbpool[slot], sizeof (si_sgblock_t));
2592 
2593 	ncookies = spkt->satapkt_cmd.satacmd_num_dma_cookies;
2594 	ASSERT(ncookies <= SI_MAX_SGL_LENGTH);
2595 
2596 	SIDBG1(SIDBG_COOKIES, si_ctlp, "total ncookies: %d", ncookies);
2597 	if (ncookies == 0) {
2598 		sgbp = &si_portp->siport_sgbpool[slot];
2599 		sgtp = &sgbp->sgb_sgt[0];
2600 		sgep = &sgtp->sgt_sge[0];
2601 
2602 		/* No cookies. Terminate the chain. */
2603 		SIDBG0(SIDBG_COOKIES, si_ctlp, "empty cookies: terminating.");
2604 
2605 		sgep->sge_addr_low = 0;
2606 		sgep->sge_addr_high = 0;
2607 		sgep->sge_data_count = 0;
2608 		SET_SGE_TRM((*sgep));
2609 
2610 		goto sgl_fill_done;
2611 	}
2612 
2613 	for (i = 0, cookie_index = 0, sgbp = &si_portp->siport_sgbpool[slot];
2614 					i < SI_MAX_SGT_TABLES_PER_PRB; i++) {
2615 
2616 		sgtp = &sgbp->sgb_sgt[i];
2617 
2618 		/* Now fill the first 3 entries of SGT in the loop below. */
2619 		for (j = 0, sgep = &sgtp->sgt_sge[0];
2620 				((j < 3) && (cookie_index < ncookies-1));
2621 				j++, cookie_index++, sgep++)  {
2622 			ASSERT(cookie_index < ncookies);
2623 			SIDBG2(SIDBG_COOKIES, si_ctlp,
2624 				"inner loop: cookie_index: %d, ncookies: %d",
2625 				cookie_index,
2626 				ncookies);
2627 			cookie = spkt->satapkt_cmd.
2628 					satacmd_dma_cookie_list[cookie_index];
2629 
2630 			sgep->sge_addr_low = cookie._dmu._dmac_la[0];
2631 			sgep->sge_addr_high = cookie._dmu._dmac_la[1];
2632 			sgep->sge_data_count = cookie.dmac_size;
2633 		}
2634 
2635 		/*
2636 		 * If this happens to be the last cookie, we terminate it here.
2637 		 * Otherwise, we link to next SGT.
2638 		 */
2639 
2640 		if (cookie_index == ncookies-1) {
2641 			/* This is the last cookie. Terminate the chain. */
2642 			SIDBG2(SIDBG_COOKIES, si_ctlp,
2643 				"filling the last: cookie_index: %d, "
2644 				"ncookies: %d",
2645 				cookie_index,
2646 				ncookies);
2647 			cookie = spkt->satapkt_cmd.
2648 					satacmd_dma_cookie_list[cookie_index];
2649 
2650 			sgep->sge_addr_low = cookie._dmu._dmac_la[0];
2651 			sgep->sge_addr_high = cookie._dmu._dmac_la[1];
2652 			sgep->sge_data_count = cookie.dmac_size;
2653 			SET_SGE_TRM((*sgep));
2654 
2655 			break; /* we break the loop */
2656 
2657 		} else {
2658 			/* This is not the last one. So link it. */
2659 			SIDBG2(SIDBG_COOKIES, si_ctlp,
2660 				"linking SGT: cookie_index: %d, ncookies: %d",
2661 				cookie_index,
2662 				ncookies);
2663 			sgep->sge_addr = si_portp->siport_sgbpool_physaddr +
2664 						slot * sizeof (si_sgblock_t) +
2665 						(i+1) * sizeof (si_sgt_t);
2666 
2667 			SET_SGE_LNK((*sgep));
2668 		}
2669 
2670 	}
2671 
2672 	/* *** finished filling the scatter gather list ******* */
2673 
2674 sgl_fill_done:
2675 	/* Now remember the sata packet in siport_slot_pkts[]. */
2676 	si_portp->siport_slot_pkts[slot] = spkt;
2677 
2678 	/*
2679 	 * We are overloading satapkt_hba_driver_private with
2680 	 * watched_cycle count.
2681 	 */
2682 	spkt->satapkt_hba_driver_private = (void *)(intptr_t)0;
2683 
2684 	if (is_atapi) {
2685 		/* program the packet_lenth if it is atapi device. */
2686 
2687 
2688 #ifdef ATAPI_2nd_PHASE
2689 		/*
2690 		 * Framework needs to calculate the acdb_len based on
2691 		 * identify packet data. This needs to be accomplished
2692 		 * in second phase of the project.
2693 		 */
2694 		ASSERT((cmd->satacmd_acdb_len == 12) ||
2695 				(cmd->satacmd_acdb_len == 16));
2696 		SIDBG1(SIDBG_VERBOSE, si_ctlp, "deliver: acdb_len: %d",
2697 			cmd->satacmd_acdb_len);
2698 
2699 		if (cmd->satacmd_acdb_len == 16) {
2700 			ddi_put32(si_ctlp->sictl_port_acc_handle,
2701 				(uint32_t *)PORT_CONTROL_SET(si_ctlp, port),
2702 				PORT_CONTROL_SET_BITS_PACKET_LEN);
2703 		} else {
2704 			ddi_put32(si_ctlp->sictl_port_acc_handle,
2705 				(uint32_t *)PORT_CONTROL_CLEAR(si_ctlp, port),
2706 				PORT_CONTROL_CLEAR_BITS_PACKET_LEN);
2707 		}
2708 
2709 #else /* ATAPI_2nd_PHASE */
2710 		/* hard coding for now to 12 bytes */
2711 		ddi_put32(si_ctlp->sictl_port_acc_handle,
2712 			(uint32_t *)PORT_CONTROL_CLEAR(si_ctlp, port),
2713 			PORT_CONTROL_CLEAR_BITS_PACKET_LEN);
2714 #endif /* ATAPI_2nd_PHASE */
2715 	}
2716 
2717 
2718 #if SI_DEBUG
2719 	if (si_debug_flags & SIDBG_DUMP_PRB) {
2720 		if (!(is_atapi && (prb->prb_sge0.sge_addr_low == 0))) {
2721 			/*
2722 			 * Do not dump the atapi Test-Unit-Ready commands.
2723 			 * The sd_media_watch spews too many of these.
2724 			 */
2725 			int *ptr;
2726 			si_sge_t *tmpsgep;
2727 			int j;
2728 
2729 			ptr = (int *)prb;
2730 			cmn_err(CE_WARN, "si_deliver_satpkt prb: ");
2731 			for (j = 0; j < (sizeof (si_prb_t)/4); j++) {
2732 				cmn_err(CE_WARN, "%x ", ptr[j]);
2733 			}
2734 
2735 			cmn_err(CE_WARN,
2736 				"si_deliver_satpkt sgt: low, high, count link");
2737 			for (j = 0,
2738 				tmpsgep = (si_sge_t *)
2739 					&si_portp->siport_sgbpool[slot];
2740 				j < (sizeof (si_sgblock_t)/ sizeof (si_sge_t));
2741 				j++, tmpsgep++) {
2742 				ptr = (int *)tmpsgep;
2743 				cmn_err(CE_WARN, "%x %x %x %x",
2744 					ptr[0],
2745 					ptr[1],
2746 					ptr[2],
2747 					ptr[3]);
2748 				if (IS_SGE_TRM_SET((*tmpsgep))) {
2749 					break;
2750 				}
2751 
2752 			}
2753 		}
2754 
2755 	}
2756 #endif  /* SI_DEBUG */
2757 
2758 	/* Deliver PRB */
2759 	POST_PRB_ADDR(si_ctlp, si_portp, port, slot);
2760 
2761 	return (slot);
2762 }
2763 
2764 /*
2765  * Initialize the controller and set up driver data structures.
2766  *
2767  * This routine can be called from three separate cases: DDI_ATTACH, PM_LEVEL_D0
2768  * and DDI_RESUME. The DDI_ATTACH case is different from other two cases; the
2769  * memory allocation & device signature probing are attempted only during
2770  * DDI_ATTACH case. In the case of PM_LEVEL_D0 & DDI_RESUME, we are starting
2771  * from a previously initialized state; so there is no need to allocate memory
2772  * or to attempt probing the device signatures.
2773  */
2774 static int
2775 si_initialize_controller(si_ctl_state_t *si_ctlp)
2776 {
2777 	uint32_t port_status;
2778 	uint32_t SStatus;
2779 	uint32_t SControl;
2780 	int port;
2781 	int loop_count = 0;
2782 	si_port_state_t *si_portp;
2783 
2784 	SIDBG0(SIDBG_INIT|SIDBG_ENTRY, si_ctlp,
2785 		"si3124: si_initialize_controller entered");
2786 
2787 	mutex_enter(&si_ctlp->sictl_mutex);
2788 
2789 	/* Remove the Global Reset. */
2790 	ddi_put32(si_ctlp->sictl_global_acc_handle,
2791 			(uint32_t *)GLOBAL_CONTROL_REG(si_ctlp),
2792 			GLOBAL_CONTROL_REG_BITS_CLEAR);
2793 
2794 	for (port = 0; port < si_ctlp->sictl_num_ports; port++) {
2795 
2796 		if (si_ctlp->sictl_flags & SI_ATTACH) {
2797 			/*
2798 			 * We allocate the port state only during attach
2799 			 * sequence. We don't want to do it during
2800 			 * suspend/resume sequence.
2801 			 */
2802 			if (si_alloc_port_state(si_ctlp, port)) {
2803 				mutex_exit(&si_ctlp->sictl_mutex);
2804 				return (SI_FAILURE);
2805 			}
2806 		}
2807 
2808 		si_portp = si_ctlp->sictl_ports[port];
2809 		mutex_enter(&si_portp->siport_mutex);
2810 
2811 		/* Clear Port Reset. */
2812 		ddi_put32(si_ctlp->sictl_port_acc_handle,
2813 			(uint32_t *)PORT_CONTROL_SET(si_ctlp, port),
2814 			PORT_CONTROL_SET_BITS_PORT_RESET);
2815 		ddi_put32(si_ctlp->sictl_port_acc_handle,
2816 			(uint32_t *)PORT_CONTROL_CLEAR(si_ctlp, port),
2817 			PORT_CONTROL_CLEAR_BITS_PORT_RESET);
2818 
2819 		/*
2820 		 * Arm the interrupts for: Cmd completion, Cmd error,
2821 		 * Port Ready, PM Change, PhyRdyChange, Commwake,
2822 		 * UnrecFIS, Devxchanged, SDBNotify.
2823 		 */
2824 		ddi_put32(si_ctlp->sictl_port_acc_handle,
2825 			(uint32_t *)PORT_INTERRUPT_ENABLE_SET(si_ctlp, port),
2826 			(INTR_COMMAND_COMPLETE |
2827 			INTR_COMMAND_ERROR |
2828 			INTR_PORT_READY |
2829 			INTR_POWER_CHANGE |
2830 			INTR_PHYRDY_CHANGE |
2831 			INTR_COMWAKE_RECEIVED |
2832 			INTR_UNRECOG_FIS |
2833 			INTR_DEV_XCHANGED |
2834 			INTR_SETDEVBITS_NOTIFY));
2835 
2836 		/* Now enable the interrupts. */
2837 		si_enable_port_interrupts(si_ctlp, port);
2838 
2839 		/*
2840 		 * The following PHY initialization is redundant in
2841 		 * in x86 since the BIOS anyway does this as part of
2842 		 * device enumeration during the power up. But this
2843 		 * is a required step in sparc since there is no BIOS.
2844 		 *
2845 		 * The way to initialize the PHY is to write a 1 and then
2846 		 * a 0 to DET field of SControl register.
2847 		 */
2848 
2849 		/*
2850 		 * Fetch the current SControl before writing the
2851 		 * DET part with 1
2852 		 */
2853 		SControl = ddi_get32(si_ctlp->sictl_port_acc_handle,
2854 				(uint32_t *)PORT_SCONTROL(si_ctlp, port));
2855 		SCONTROL_SET_DET(SControl, SCONTROL_DET_COMRESET);
2856 		ddi_put32(si_ctlp->sictl_port_acc_handle,
2857 			(uint32_t *)(PORT_SCONTROL(si_ctlp, port)),
2858 			SControl);
2859 #ifndef __lock_lint
2860 		delay(SI_10MS_TICKS); /* give time for COMRESET to percolate */
2861 #endif /* __lock_lint */
2862 
2863 		/*
2864 		 * Now fetch the SControl again and rewrite the
2865 		 * DET part with 0
2866 		 */
2867 		SControl = ddi_get32(si_ctlp->sictl_port_acc_handle,
2868 				(uint32_t *)PORT_SCONTROL(si_ctlp, port));
2869 		SCONTROL_SET_DET(SControl, SCONTROL_DET_NOACTION);
2870 		ddi_put32(si_ctlp->sictl_port_acc_handle,
2871 			(uint32_t *)(PORT_SCONTROL(si_ctlp, port)),
2872 			SControl);
2873 
2874 		/*
2875 		 * PHY may be initialized by now. Check the DET field of
2876 		 * SStatus to determine if there is a device present.
2877 		 *
2878 		 * The DET field is valid only if IPM field indicates that
2879 		 * the interface is in active state.
2880 		 */
2881 
2882 		loop_count = 0;
2883 		do {
2884 			SStatus = ddi_get32(si_ctlp->sictl_port_acc_handle,
2885 				(uint32_t *)PORT_SSTATUS(si_ctlp, port));
2886 
2887 			if (SSTATUS_GET_IPM(SStatus) !=
2888 						SSTATUS_IPM_INTERFACE_ACTIVE) {
2889 				/*
2890 				 * If the interface is not active, the DET field
2891 				 * is considered not accurate. So we want to
2892 				 * continue looping.
2893 				 */
2894 				SSTATUS_SET_DET(SStatus,
2895 						SSTATUS_DET_NODEV_NOPHY);
2896 			}
2897 
2898 			if (loop_count++ > SI_POLLRATE_SSTATUS) {
2899 				/*
2900 				 * We are effectively timing out after 0.1 sec.
2901 				 */
2902 				break;
2903 			}
2904 
2905 			/* Wait for 10 millisec */
2906 #ifndef __lock_lint
2907 			delay(SI_10MS_TICKS);
2908 #endif /* __lock_lint */
2909 
2910 		} while (SSTATUS_GET_DET(SStatus) !=
2911 					SSTATUS_DET_DEVPRESENT_PHYONLINE);
2912 
2913 		SIDBG2(SIDBG_POLL_LOOP|SIDBG_INIT, si_ctlp,
2914 			"si_initialize_controller: 1st loop count: %d, "
2915 			"SStatus: 0x%x",
2916 			loop_count,
2917 			SStatus);
2918 
2919 		if ((SSTATUS_GET_IPM(SStatus) !=
2920 					SSTATUS_IPM_INTERFACE_ACTIVE) ||
2921 			(SSTATUS_GET_DET(SStatus) !=
2922 					SSTATUS_DET_DEVPRESENT_PHYONLINE)) {
2923 			/*
2924 			 * Either the port is not active or there
2925 			 * is no device present.
2926 			 */
2927 			si_ctlp->sictl_ports[port]->siport_port_type =
2928 							PORT_TYPE_NODEV;
2929 			mutex_exit(&si_portp->siport_mutex);
2930 			continue;
2931 		}
2932 
2933 		/* Wait until Port Ready */
2934 		loop_count = 0;
2935 		do {
2936 			port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
2937 				(uint32_t *)PORT_STATUS(si_ctlp, port));
2938 
2939 			if (loop_count++ > SI_POLLRATE_PORTREADY) {
2940 				/*
2941 				 * We are effectively timing out after 0.5 sec.
2942 				 */
2943 				break;
2944 			}
2945 
2946 			/* Wait for 10 millisec */
2947 #ifndef __lock_lint
2948 			delay(SI_10MS_TICKS);
2949 #endif /* __lock_lint */
2950 
2951 		} while (!(port_status & PORT_STATUS_BITS_PORT_READY));
2952 
2953 		SIDBG1(SIDBG_POLL_LOOP|SIDBG_INIT, si_ctlp,
2954 			"si_initialize_controller: 2nd loop count: %d",
2955 			loop_count);
2956 
2957 		if (si_ctlp->sictl_flags & SI_ATTACH) {
2958 			/*
2959 			 * We want to probe for dev signature only during attach
2960 			 * case. Don't do it during suspend/resume sequence.
2961 			 */
2962 			if (port_status & PORT_STATUS_BITS_PORT_READY) {
2963 				mutex_exit(&si_portp->siport_mutex);
2964 				si_find_dev_signature(si_ctlp, si_portp, port,
2965 						PORTMULT_CONTROL_PORT);
2966 				mutex_enter(&si_portp->siport_mutex);
2967 			} else {
2968 				si_ctlp->sictl_ports[port]->siport_port_type =
2969 					PORT_TYPE_NODEV;
2970 			}
2971 		}
2972 
2973 		mutex_exit(&si_portp->siport_mutex);
2974 	}
2975 
2976 	mutex_exit(&si_ctlp->sictl_mutex);
2977 	return (SI_SUCCESS);
2978 }
2979 
2980 /*
2981  * Reverse of si_initialize_controller().
2982  *
2983  * WARNING, WARNING: The caller is expected to obtain the sictl_mutex
2984  * before calling us.
2985  */
2986 static void
2987 si_deinititalize_controller(si_ctl_state_t *si_ctlp)
2988 {
2989 	int port;
2990 
2991 	_NOTE(ASSUMING_PROTECTED(si_ctlp))
2992 
2993 	SIDBG0(SIDBG_INIT|SIDBG_ENTRY, si_ctlp,
2994 		"si3124: si_deinititalize_controller entered");
2995 
2996 	/* disable all the interrupts. */
2997 	si_disable_all_interrupts(si_ctlp);
2998 
2999 	if (si_ctlp->sictl_flags & SI_DETACH) {
3000 		/*
3001 		 * We want to dealloc all the memory in detach case.
3002 		 */
3003 		for (port = 0; port < si_ctlp->sictl_num_ports; port++) {
3004 			si_dealloc_port_state(si_ctlp, port);
3005 		}
3006 	}
3007 
3008 }
3009 
3010 /*
3011  * Prepare the port ready for usage.
3012  *
3013  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
3014  * before calling us.
3015  */
3016 static void
3017 si_init_port(si_ctl_state_t *si_ctlp, int port)
3018 {
3019 
3020 	SIDBG1(SIDBG_ENTRY|SIDBG_INIT, si_ctlp,
3021 			"si_init_port entered: port: 0x%x",
3022 			port);
3023 
3024 	/* Initialize the port. */
3025 	ddi_put32(si_ctlp->sictl_port_acc_handle,
3026 			(uint32_t *)PORT_CONTROL_SET(si_ctlp, port),
3027 			PORT_CONTROL_SET_BITS_PORT_INITIALIZE);
3028 
3029 	/*
3030 	 * Clear the InterruptNCOR (Interupt No Clear on Read).
3031 	 * This step ensures that a mere reading of slot_status will clear
3032 	 * the interrupt; no explicit clearing of interrupt condition
3033 	 * will be needed for successful completion of commands.
3034 	 */
3035 	ddi_put32(si_ctlp->sictl_port_acc_handle,
3036 		(uint32_t *)PORT_CONTROL_CLEAR(si_ctlp, port),
3037 		PORT_CONTROL_CLEAR_BITS_INTR_NCoR);
3038 
3039 	/* clear any pending interrupts at this point */
3040 	ddi_put32(si_ctlp->sictl_port_acc_handle,
3041 		(uint32_t *)(PORT_INTERRUPT_STATUS(si_ctlp, port)),
3042 		INTR_MASK);
3043 
3044 }
3045 
3046 
3047 /*
3048  * Enumerate the devices connected to the port multiplier.
3049  * Once a device is detected, we call si_find_dev_signature()
3050  * to find the type of device connected. Even though we are
3051  * called from within si_find_dev_signature(), there is no
3052  * recursion possible.
3053  */
3054 static int
3055 si_enumerate_port_multiplier(
3056 	si_ctl_state_t *si_ctlp,
3057 	si_port_state_t *si_portp,
3058 	int port)
3059 {
3060 	uint32_t num_dev_ports = 0;
3061 	int pmport;
3062 	uint32_t SControl = 0;
3063 	uint32_t SStatus = 0;
3064 	uint32_t SError = 0;
3065 	int loop_count = 0;
3066 
3067 	SIDBG1(SIDBG_ENTRY|SIDBG_INIT, si_ctlp,
3068 		"si_enumerate_port_multiplier entered: port: %d",
3069 		port);
3070 
3071 	mutex_enter(&si_portp->siport_mutex);
3072 
3073 	/* Enable Port Multiplier context switching. */
3074 	ddi_put32(si_ctlp->sictl_port_acc_handle,
3075 		(uint32_t *)PORT_CONTROL_SET(si_ctlp, port),
3076 		PORT_CONTROL_SET_BITS_PM_ENABLE);
3077 
3078 	/*
3079 	 * Read the num dev ports connected.
3080 	 * GSCR[2] contains the number of device ports.
3081 	 */
3082 	if (si_read_portmult_reg(si_ctlp, si_portp, port, PORTMULT_CONTROL_PORT,
3083 						PSCR_REG2, &num_dev_ports)) {
3084 		mutex_exit(&si_portp->siport_mutex);
3085 		return (SI_FAILURE);
3086 	}
3087 	si_portp->siport_portmult_state.sipm_num_ports = num_dev_ports;
3088 
3089 	SIDBG1(SIDBG_INIT, si_ctlp,
3090 		"si_enumerate_port_multiplier: ports found: %d",
3091 		num_dev_ports);
3092 
3093 	for (pmport = 0; pmport < num_dev_ports-1; pmport++) {
3094 		/*
3095 		 * Enable PHY by writing a 1, then a 0 to SControl
3096 		 * (i.e. PSCR[2]) DET field.
3097 		 */
3098 		if (si_read_portmult_reg(si_ctlp, si_portp, port, pmport,
3099 						PSCR_REG2, &SControl)) {
3100 			continue;
3101 		}
3102 
3103 		/* First write a 1 to DET field of SControl. */
3104 		SCONTROL_SET_DET(SControl, SCONTROL_DET_COMRESET);
3105 		if (si_write_portmult_reg(si_ctlp, si_portp, port, pmport,
3106 						PSCR_REG2, SControl)) {
3107 			continue;
3108 		}
3109 #ifndef __lock_lint
3110 		delay(SI_10MS_TICKS); /* give time for COMRESET to percolate */
3111 #endif /* __lock_lint */
3112 
3113 		/* Then write a 0 to the DET field of SControl. */
3114 		SCONTROL_SET_DET(SControl, SCONTROL_DET_NOACTION);
3115 		if (si_write_portmult_reg(si_ctlp, si_portp, port, pmport,
3116 						PSCR_REG2, SControl)) {
3117 			continue;
3118 		}
3119 
3120 		/* Wait for PHYRDY by polling SStatus (i.e. PSCR[0]). */
3121 		loop_count = 0;
3122 		do {
3123 			if (si_read_portmult_reg(si_ctlp, si_portp, port,
3124 					pmport, PSCR_REG0, &SStatus)) {
3125 				break;
3126 			}
3127 			SIDBG1(SIDBG_POLL_LOOP, si_ctlp,
3128 				"looping for PHYRDY: SStatus: %x",
3129 				SStatus);
3130 
3131 			if (SSTATUS_GET_IPM(SStatus) !=
3132 						SSTATUS_IPM_INTERFACE_ACTIVE) {
3133 				/*
3134 				 * If the interface is not active, the DET field
3135 				 * is considered not accurate. So we want to
3136 				 * continue looping.
3137 				 */
3138 				SSTATUS_SET_DET(SStatus,
3139 						SSTATUS_DET_NODEV_NOPHY);
3140 			}
3141 
3142 			if (loop_count++ > SI_POLLRATE_SSTATUS) {
3143 				/*
3144 				 * We are effectively timing out after 0.1 sec.
3145 				 */
3146 				break;
3147 			}
3148 
3149 			/* Wait for 10 millisec */
3150 #ifndef __lock_lint
3151 			delay(SI_10MS_TICKS);
3152 #endif /* __lock_lint */
3153 
3154 		} while (SSTATUS_GET_DET(SStatus) !=
3155 					SSTATUS_DET_DEVPRESENT_PHYONLINE);
3156 
3157 		SIDBG2(SIDBG_POLL_LOOP, si_ctlp,
3158 			"si_enumerate_port_multiplier: "
3159 			"loop count: %d, SStatus: 0x%x",
3160 			loop_count,
3161 			SStatus);
3162 
3163 		if ((SSTATUS_GET_IPM(SStatus) ==
3164 					SSTATUS_IPM_INTERFACE_ACTIVE) &&
3165 			(SSTATUS_GET_DET(SStatus) ==
3166 					SSTATUS_DET_DEVPRESENT_PHYONLINE)) {
3167 			/* The interface is active and the device is present */
3168 			SIDBG1(SIDBG_INIT, si_ctlp,
3169 				"Status: %x, device exists",
3170 				SStatus);
3171 			/*
3172 			 * Clear error bits in SError register (i.e. PSCR[1]
3173 			 * by writing back error bits.
3174 			 */
3175 			if (si_read_portmult_reg(si_ctlp, si_portp, port,
3176 						pmport, PSCR_REG1, &SError)) {
3177 				continue;
3178 			}
3179 			SIDBG1(SIDBG_INIT, si_ctlp,
3180 				"SError bits are: %x", SError);
3181 			if (si_write_portmult_reg(si_ctlp, si_portp, port,
3182 						pmport, PSCR_REG1, SError)) {
3183 				continue;
3184 			}
3185 
3186 			/* There exists a device. */
3187 			mutex_exit(&si_portp->siport_mutex);
3188 			si_find_dev_signature(si_ctlp, si_portp, port, pmport);
3189 			mutex_enter(&si_portp->siport_mutex);
3190 		}
3191 	}
3192 
3193 	mutex_exit(&si_portp->siport_mutex);
3194 
3195 	return (SI_SUCCESS);
3196 }
3197 
3198 
3199 /*
3200  * Read a port multiplier register.
3201  *
3202  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
3203  * before calling us.
3204  */
3205 static int
3206 si_read_portmult_reg(
3207 	si_ctl_state_t *si_ctlp,
3208 	si_port_state_t *si_portp,
3209 	int port,
3210 	int pmport,
3211 	int regnum,
3212 	uint32_t *regval)
3213 {
3214 	int slot;
3215 	si_prb_t *prb;
3216 	uint32_t *prb_word_ptr;
3217 	int i;
3218 	uint32_t slot_status;
3219 	int loop_count = 0;
3220 
3221 	_NOTE(ASSUMING_PROTECTED(si_portp))
3222 
3223 	SIDBG3(SIDBG_ENTRY, si_ctlp, "si_read_portmult_reg: port: %x,"
3224 			"pmport: %x, regnum: %x",
3225 			port, pmport, regnum);
3226 
3227 	slot = si_claim_free_slot(si_ctlp, si_portp, port);
3228 	if (slot == -1) {
3229 		return (SI_FAILURE);
3230 	}
3231 
3232 	prb =  &(si_portp->siport_prbpool[slot]);
3233 	bzero((void *)prb, sizeof (si_prb_t));
3234 
3235 	/* Now fill the prb. */
3236 	SET_FIS_TYPE(prb->prb_fis, REGISTER_FIS_H2D);
3237 	SET_FIS_PMP(prb->prb_fis, PORTMULT_CONTROL_PORT);
3238 	SET_FIS_CDMDEVCTL(prb->prb_fis, 1);
3239 	SET_FIS_COMMAND(prb->prb_fis, SATAC_READ_PM_REG);
3240 
3241 	SET_FIS_DEV_HEAD(prb->prb_fis, pmport);
3242 	SET_FIS_FEATURES(prb->prb_fis, regnum);
3243 
3244 	/* no real data transfer is involved. */
3245 	SET_SGE_TRM(prb->prb_sge0);
3246 
3247 #if SI_DEBUG
3248 	if (si_debug_flags & SIDBG_DUMP_PRB) {
3249 		int *ptr;
3250 		int j;
3251 
3252 		ptr = (int *)prb;
3253 		cmn_err(CE_WARN, "read_port_mult_reg, prb: ");
3254 		for (j = 0; j < (sizeof (si_prb_t)/4); j++) {
3255 			cmn_err(CE_WARN, "%x ", ptr[j]);
3256 		}
3257 
3258 	}
3259 #endif /* SI_DEBUG */
3260 
3261 	/* Deliver PRB */
3262 	POST_PRB_ADDR(si_ctlp, si_portp, port, slot);
3263 
3264 	/* Loop till the command is finished. */
3265 	do {
3266 		slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3267 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
3268 
3269 		SIDBG1(SIDBG_POLL_LOOP, si_ctlp,
3270 			"looping read_pm slot_status: 0x%x",
3271 			slot_status);
3272 
3273 		if (loop_count++ > SI_POLLRATE_SLOTSTATUS) {
3274 			/* We are effectively timing out after 0.5 sec. */
3275 			break;
3276 		}
3277 
3278 		/* Wait for 10 millisec */
3279 #ifndef __lock_lint
3280 		delay(SI_10MS_TICKS);
3281 #endif /* __lock_lint */
3282 
3283 	} while (slot_status & SI_SLOT_MASK & (0x1 << slot));
3284 
3285 	SIDBG1(SIDBG_POLL_LOOP, si_ctlp,
3286 		"read_portmult_reg: loop count: %d",
3287 		loop_count);
3288 
3289 	CLEAR_BIT(si_portp->siport_pending_tags, slot);
3290 
3291 	/* Now inspect the port LRAM for the modified FIS. */
3292 	prb_word_ptr = (uint32_t *)prb;
3293 	for (i = 0; i < (sizeof (si_prb_t)/4); i++) {
3294 		prb_word_ptr[i] = ddi_get32(si_ctlp->sictl_port_acc_handle,
3295 			(uint32_t *)(PORT_LRAM(si_ctlp, port, slot)+i*4));
3296 	}
3297 
3298 	if (((GET_FIS_COMMAND(prb->prb_fis) & 0x1) != 0) ||
3299 	    (GET_FIS_FEATURES(prb->prb_fis) != 0)) {
3300 		/* command failed. */
3301 		return (SI_FAILURE);
3302 	}
3303 
3304 	/* command succeeded. */
3305 	*regval = (GET_FIS_SECTOR_COUNT(prb->prb_fis) & 0xff) |
3306 			((GET_FIS_SECTOR(prb->prb_fis) << 8)  & 0xff00) |
3307 			((GET_FIS_CYL_LOW(prb->prb_fis) << 16)  & 0xff0000) |
3308 			((GET_FIS_CYL_HI(prb->prb_fis) << 24)  & 0xff000000);
3309 
3310 	return (SI_SUCCESS);
3311 }
3312 
3313 /*
3314  * Write a port multiplier register.
3315  *
3316  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
3317  * before calling us.
3318  */
3319 static int
3320 si_write_portmult_reg(
3321 	si_ctl_state_t *si_ctlp,
3322 	si_port_state_t *si_portp,
3323 	int port,
3324 	int pmport,
3325 	int regnum,
3326 	uint32_t regval)
3327 {
3328 	int slot;
3329 	si_prb_t *prb;
3330 	uint32_t *prb_word_ptr;
3331 	uint32_t slot_status;
3332 	int i;
3333 	int loop_count = 0;
3334 
3335 	_NOTE(ASSUMING_PROTECTED(si_portp))
3336 
3337 	SIDBG4(SIDBG_ENTRY, si_ctlp,
3338 		"si_write_portmult_reg: port: %x, pmport: %x,"
3339 		"regnum: %x, regval: %x",
3340 		port, pmport, regnum, regval);
3341 
3342 	slot = si_claim_free_slot(si_ctlp, si_portp, port);
3343 	if (slot == -1) {
3344 		return (SI_FAILURE);
3345 	}
3346 
3347 	prb =  &(si_portp->siport_prbpool[slot]);
3348 	bzero((void *)prb, sizeof (si_prb_t));
3349 
3350 	/* Now fill the prb. */
3351 	SET_FIS_TYPE(prb->prb_fis, REGISTER_FIS_H2D);
3352 	SET_FIS_PMP(prb->prb_fis, PORTMULT_CONTROL_PORT);
3353 	SET_FIS_CDMDEVCTL(prb->prb_fis, 1);
3354 
3355 	SET_FIS_COMMAND(prb->prb_fis, SATAC_WRITE_PM_REG);
3356 	SET_FIS_DEV_HEAD(prb->prb_fis, pmport);
3357 	SET_FIS_FEATURES(prb->prb_fis, regnum);
3358 
3359 	SET_FIS_SECTOR_COUNT(prb->prb_fis, regval & 0xff);
3360 	SET_FIS_SECTOR(prb->prb_fis, (regval >> 8) & 0xff);
3361 	SET_FIS_CYL_LOW(prb->prb_fis, (regval >> 16) & 0xff);
3362 	SET_FIS_CYL_HI(prb->prb_fis, (regval >> 24)  & 0xff);
3363 
3364 	/* no real data transfer is involved. */
3365 	SET_SGE_TRM(prb->prb_sge0);
3366 
3367 #if SI_DEBUG
3368 	if (si_debug_flags & SIDBG_DUMP_PRB) {
3369 		int *ptr;
3370 		int j;
3371 
3372 		ptr = (int *)prb;
3373 		cmn_err(CE_WARN, "read_port_mult_reg, prb: ");
3374 		for (j = 0; j < (sizeof (si_prb_t)/4); j++) {
3375 			cmn_err(CE_WARN, "%x ", ptr[j]);
3376 		}
3377 
3378 	}
3379 #endif /* SI_DEBUG */
3380 
3381 	/* Deliver PRB */
3382 	POST_PRB_ADDR(si_ctlp, si_portp, port, slot);
3383 
3384 	/* Loop till the command is finished. */
3385 	do {
3386 		slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3387 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
3388 
3389 		SIDBG1(SIDBG_POLL_LOOP, si_ctlp,
3390 			"looping write_pmp slot_status: 0x%x",
3391 			slot_status);
3392 
3393 		if (loop_count++ > SI_POLLRATE_SLOTSTATUS) {
3394 			/* We are effectively timing out after 0.5 sec. */
3395 			break;
3396 		}
3397 
3398 		/* Wait for 10 millisec */
3399 #ifndef __lock_lint
3400 		delay(SI_10MS_TICKS);
3401 #endif /* __lock_lint */
3402 
3403 	} while (slot_status & SI_SLOT_MASK & (0x1 << slot));
3404 
3405 	SIDBG1(SIDBG_POLL_LOOP, si_ctlp,
3406 		"write_portmult_reg: loop count: %d",
3407 		loop_count);
3408 
3409 	CLEAR_BIT(si_portp->siport_pending_tags, slot);
3410 
3411 	/* Now inspect the port LRAM for the modified FIS. */
3412 	prb_word_ptr = (uint32_t *)prb;
3413 	for (i = 0; i < (sizeof (si_prb_t)/4); i++) {
3414 		prb_word_ptr[i] = ddi_get32(si_ctlp->sictl_port_acc_handle,
3415 			(uint32_t *)(PORT_LRAM(si_ctlp, port, slot)+i*4));
3416 	}
3417 
3418 	if (((GET_FIS_COMMAND(prb->prb_fis) & 0x1) != 0) ||
3419 	    (GET_FIS_FEATURES(prb->prb_fis) != 0)) {
3420 		/* command failed */
3421 		return (SI_FAILURE);
3422 	}
3423 
3424 	/* command succeeded */
3425 	return (SI_SUCCESS);
3426 }
3427 
3428 
3429 /*
3430  * Set the auto sense data for ATAPI devices.
3431  *
3432  * Note: Currently the sense data is simulated; this code will be enhanced
3433  * in second phase to fetch the real sense data from the atapi device.
3434  */
3435 static void
3436 si_set_sense_data(sata_pkt_t *satapkt, int reason)
3437 {
3438 	struct scsi_extended_sense *sense;
3439 
3440 	sense = (struct scsi_extended_sense *)
3441 			satapkt->satapkt_cmd.satacmd_rqsense;
3442 	bzero(sense, sizeof (struct scsi_extended_sense));
3443 	sense->es_valid = 1;		/* Valid sense */
3444 	sense->es_class = 7;		/* Response code 0x70 - current err */
3445 	sense->es_key = 0;
3446 	sense->es_info_1 = 0;
3447 	sense->es_info_2 = 0;
3448 	sense->es_info_3 = 0;
3449 	sense->es_info_4 = 0;
3450 	sense->es_add_len = 6;		/* Additional length */
3451 	sense->es_cmd_info[0] = 0;
3452 	sense->es_cmd_info[1] = 0;
3453 	sense->es_cmd_info[2] = 0;
3454 	sense->es_cmd_info[3] = 0;
3455 	sense->es_add_code = 0;
3456 	sense->es_qual_code = 0;
3457 
3458 	if ((reason == SATA_PKT_DEV_ERROR) || (reason == SATA_PKT_TIMEOUT)) {
3459 		sense->es_key = KEY_HARDWARE_ERROR;
3460 	}
3461 }
3462 
3463 
3464 /*
3465  * Interrupt service handler. We loop through each of the ports to find
3466  * if the interrupt belongs to any of them.
3467  *
3468  * Bulk of the interrupt handling is actually done out of subroutines
3469  * like si_intr_command_complete() etc.
3470  */
3471 /*ARGSUSED*/
3472 static uint_t
3473 si_intr(caddr_t arg1, caddr_t arg2)
3474 {
3475 
3476 	si_ctl_state_t *si_ctlp = (si_ctl_state_t *)arg1;
3477 	si_port_state_t *si_portp;
3478 	uint32_t global_intr_status;
3479 	uint32_t mask, port_intr_status;
3480 	int port;
3481 
3482 	global_intr_status = ddi_get32(si_ctlp->sictl_global_acc_handle,
3483 				(uint32_t *)GLOBAL_INTERRUPT_STATUS(si_ctlp));
3484 
3485 	SIDBG1(SIDBG_INTR|SIDBG_ENTRY, si_ctlp,
3486 		"si_intr: global_int_status: 0x%x",
3487 		global_intr_status);
3488 
3489 	if (!(global_intr_status & SI31xx_INTR_PORT_MASK)) {
3490 		/* Sorry, the interrupt is not ours. */
3491 		return (DDI_INTR_UNCLAIMED);
3492 	}
3493 
3494 	/* Loop for all the ports. */
3495 	for (port = 0; port < si_ctlp->sictl_num_ports; port++) {
3496 
3497 		mask = 0x1 << port;
3498 		if (!(global_intr_status & mask)) {
3499 			continue;
3500 		}
3501 
3502 		mutex_enter(&si_ctlp->sictl_mutex);
3503 		si_portp = si_ctlp->sictl_ports[port];
3504 		mutex_exit(&si_ctlp->sictl_mutex);
3505 
3506 		port_intr_status = ddi_get32(si_ctlp->sictl_global_acc_handle,
3507 			(uint32_t *)PORT_INTERRUPT_STATUS(si_ctlp, port));
3508 
3509 		SIDBG2(SIDBG_VERBOSE, si_ctlp,
3510 			"s_intr: port_intr_status: 0x%x, port: %x",
3511 			port_intr_status,
3512 			port);
3513 
3514 		if (port_intr_status & INTR_COMMAND_COMPLETE) {
3515 			(void) si_intr_command_complete(si_ctlp, si_portp,
3516 							port);
3517 		} else {
3518 			/* Clear the interrupts */
3519 			ddi_put32(si_ctlp->sictl_port_acc_handle,
3520 			    (uint32_t *)(PORT_INTERRUPT_STATUS(si_ctlp, port)),
3521 			    port_intr_status & INTR_MASK);
3522 		}
3523 
3524 		/*
3525 		 * Note that we did not clear the interrupt for command
3526 		 * completion interrupt. Reading of slot_status takes care
3527 		 * of clearing the interrupt for command completion case.
3528 		 */
3529 
3530 		if (port_intr_status & INTR_COMMAND_ERROR) {
3531 			(void) si_intr_command_error(si_ctlp, si_portp, port);
3532 		}
3533 
3534 		if (port_intr_status & INTR_PORT_READY) {
3535 			(void) si_intr_port_ready(si_ctlp, si_portp, port);
3536 		}
3537 
3538 		if (port_intr_status & INTR_POWER_CHANGE) {
3539 			(void) si_intr_pwr_change(si_ctlp, si_portp, port);
3540 		}
3541 
3542 		if (port_intr_status & INTR_PHYRDY_CHANGE) {
3543 			(void) si_intr_phy_ready_change(si_ctlp, si_portp,
3544 								port);
3545 		}
3546 
3547 		if (port_intr_status & INTR_COMWAKE_RECEIVED) {
3548 			(void) si_intr_comwake_rcvd(si_ctlp, si_portp,
3549 								port);
3550 		}
3551 
3552 		if (port_intr_status & INTR_UNRECOG_FIS) {
3553 			(void) si_intr_unrecognised_fis(si_ctlp, si_portp,
3554 								port);
3555 		}
3556 
3557 		if (port_intr_status & INTR_DEV_XCHANGED) {
3558 			(void) si_intr_dev_xchanged(si_ctlp, si_portp, port);
3559 		}
3560 
3561 		if (port_intr_status & INTR_8B10B_DECODE_ERROR) {
3562 			(void) si_intr_decode_err_threshold(si_ctlp, si_portp,
3563 								port);
3564 		}
3565 
3566 		if (port_intr_status & INTR_CRC_ERROR) {
3567 			(void) si_intr_crc_err_threshold(si_ctlp, si_portp,
3568 								port);
3569 		}
3570 
3571 		if (port_intr_status & INTR_HANDSHAKE_ERROR) {
3572 			(void) si_intr_handshake_err_threshold(si_ctlp,
3573 							si_portp, port);
3574 		}
3575 
3576 		if (port_intr_status & INTR_SETDEVBITS_NOTIFY) {
3577 			(void) si_intr_set_devbits_notify(si_ctlp, si_portp,
3578 								port);
3579 		}
3580 	}
3581 
3582 	return (DDI_INTR_CLAIMED);
3583 }
3584 
3585 /*
3586  * Interrupt which indicates that one or more commands have successfully
3587  * completed.
3588  *
3589  * Since we disabled W1C (write-one-to-clear) previously, mere reading
3590  * of slot_status register clears the interrupt. There is no need to
3591  * explicitly clear the interrupt.
3592  */
3593 static int
3594 si_intr_command_complete(
3595 	si_ctl_state_t *si_ctlp,
3596 	si_port_state_t *si_portp,
3597 	int port)
3598 {
3599 
3600 	uint32_t slot_status;
3601 	uint32_t finished_tags;
3602 	int finished_slot;
3603 	sata_pkt_t *satapkt;
3604 
3605 	SIDBG0(SIDBG_ENTRY|SIDBG_INTR, si_ctlp,
3606 			"si_intr_command_complete enter");
3607 
3608 	mutex_enter(&si_portp->siport_mutex);
3609 
3610 	slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3611 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
3612 
3613 	if (!si_portp->siport_pending_tags) {
3614 		/*
3615 		 * Spurious interrupt. Nothing to be done.
3616 		 * The interrupt was cleared when slot_status was read.
3617 		 */
3618 		mutex_exit(&si_portp->siport_mutex);
3619 		return (SI_SUCCESS);
3620 	}
3621 
3622 	SIDBG2(SIDBG_VERBOSE, si_ctlp, "si3124: si_intr_command_complete: "
3623 			"pending_tags: %x, slot_status: %x",
3624 			si_portp->siport_pending_tags,
3625 			slot_status);
3626 
3627 	finished_tags =  si_portp->siport_pending_tags &
3628 					~slot_status & SI_SLOT_MASK;
3629 	while (finished_tags) {
3630 		si_prb_t *prb;
3631 
3632 		finished_slot = ddi_ffs(finished_tags) - 1;
3633 		if (finished_slot == -1) {
3634 			break;
3635 		}
3636 		prb =  &si_portp->siport_prbpool[finished_slot];
3637 
3638 		satapkt = si_portp->siport_slot_pkts[finished_slot];
3639 		satapkt->satapkt_cmd.satacmd_status_reg =
3640 						GET_FIS_COMMAND(prb->prb_fis);
3641 
3642 		if (satapkt->satapkt_cmd.satacmd_flags.sata_special_regs)
3643 			si_copy_out_regs(&satapkt->satapkt_cmd, &prb->prb_fis);
3644 
3645 		SENDUP_PACKET(si_portp, satapkt, SATA_PKT_COMPLETED);
3646 
3647 		CLEAR_BIT(si_portp->siport_pending_tags, finished_slot);
3648 		CLEAR_BIT(finished_tags, finished_slot);
3649 	}
3650 
3651 	SIDBG2(SIDBG_PKTCOMP, si_ctlp,
3652 		"command_complete done: pend_tags: 0x%x, slot_status: 0x%x",
3653 		si_portp->siport_pending_tags,
3654 		slot_status);
3655 
3656 	/*
3657 	 * tidbit: no need to clear the interrupt since reading of
3658 	 * slot_status automatically clears the interrupt in the case
3659 	 * of a successful command completion.
3660 	 */
3661 
3662 	mutex_exit(&si_portp->siport_mutex);
3663 
3664 	return (SI_SUCCESS);
3665 }
3666 
3667 /*
3668  * Interrupt which indicates that a command did not complete successfully.
3669  *
3670  * The port halts whenever a command error interrupt is received.
3671  * The only way to restart it is to reset or reinitialize the port
3672  * but such an operation throws away all the pending commands on
3673  * the port.
3674  *
3675  * We reset the device and mop the commands on the port.
3676  */
3677 static int
3678 si_intr_command_error(
3679 	si_ctl_state_t *si_ctlp,
3680 	si_port_state_t *si_portp,
3681 	int port)
3682 {
3683 	uint32_t command_error, slot_status;
3684 	uint32_t failed_tags;
3685 
3686 	command_error = ddi_get32(si_ctlp->sictl_port_acc_handle,
3687 			(uint32_t *)(PORT_COMMAND_ERROR(si_ctlp, port)));
3688 
3689 	SIDBG1(SIDBG_INTR|SIDBG_ENTRY, si_ctlp,
3690 		"si_intr_command_error: command_error: 0x%x",
3691 		command_error);
3692 
3693 	mutex_enter(&si_portp->siport_mutex);
3694 
3695 	/*
3696 	 * Remember the slot_status since any of the recovery handler
3697 	 * can blow it away with reset operation.
3698 	 */
3699 	slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3700 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
3701 
3702 	si_log_error_message(si_ctlp, port, command_error);
3703 
3704 	switch (command_error) {
3705 
3706 	case CMD_ERR_DEVICEERRROR:
3707 		si_error_recovery_DEVICEERROR(si_ctlp, si_portp, port);
3708 		break;
3709 
3710 	case CMD_ERR_SDBERROR:
3711 		si_error_recovery_SDBERROR(si_ctlp, si_portp, port);
3712 		break;
3713 
3714 	case CMD_ERR_DATAFISERROR:
3715 		si_error_recovery_DATAFISERROR(si_ctlp, si_portp, port);
3716 		break;
3717 
3718 	case CMD_ERR_SENDFISERROR:
3719 		si_error_recovery_SENDFISERROR(si_ctlp, si_portp, port);
3720 		break;
3721 
3722 	default:
3723 		si_error_recovery_default(si_ctlp, si_portp, port);
3724 		break;
3725 
3726 	}
3727 
3728 	/*
3729 	 * Compute the failed_tags by adding up the error tags.
3730 	 *
3731 	 * The siport_err_tags_SDBERROR and siport_err_tags_nonSDBERROR
3732 	 * were filled in by the si_error_recovery_* routines.
3733 	 */
3734 	failed_tags = si_portp->siport_pending_tags &
3735 			(si_portp->siport_err_tags_SDBERROR |
3736 			si_portp->siport_err_tags_nonSDBERROR);
3737 
3738 	SIDBG3(SIDBG_ERRS|SIDBG_INTR, si_ctlp, "si_intr_command_error: "
3739 			"err_tags_SDBERROR: 0x%x, "
3740 			"err_tags_nonSDBERRROR: 0x%x, "
3741 			"failed_tags: 0x%x",
3742 			si_portp->siport_err_tags_SDBERROR,
3743 			si_portp->siport_err_tags_nonSDBERROR,
3744 			failed_tags);
3745 
3746 	SIDBG2(SIDBG_ERRS|SIDBG_INTR, si_ctlp, "si3124: si_intr_command_error: "
3747 			"slot_status:0x%x, pending_tags: 0x%x",
3748 			slot_status,
3749 			si_portp->siport_pending_tags);
3750 
3751 	mutex_exit(&si_portp->siport_mutex);
3752 	si_mop_commands(si_ctlp,
3753 			si_portp,
3754 			port,
3755 			slot_status,
3756 			failed_tags,
3757 			0, 	/* timedout_tags */
3758 			0, 	/* aborting_tags */
3759 			0); 	/* reset_tags */
3760 	mutex_enter(&si_portp->siport_mutex);
3761 
3762 	ASSERT(si_portp->siport_pending_tags == 0);
3763 
3764 	si_portp->siport_err_tags_SDBERROR = 0;
3765 	si_portp->siport_err_tags_nonSDBERROR = 0;
3766 
3767 	mutex_exit(&si_portp->siport_mutex);
3768 
3769 	return (SI_SUCCESS);
3770 }
3771 
3772 /*
3773  * There is a subtle difference between errors on a normal port and
3774  * a port-mult port. When an error happens on a normal port, the port
3775  * is halted effectively until the port is reset or initialized.
3776  * However, in port-mult port errors, port does not get halted since
3777  * other non-error devices behind the port multiplier can still
3778  * continue to operate. So we wait till all the commands are drained
3779  * instead of resetting it right away.
3780  *
3781  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
3782  * before calling us.
3783  */
3784 static void
3785 si_recover_portmult_errors(
3786 	si_ctl_state_t *si_ctlp,
3787 	si_port_state_t *si_portp,
3788 	int port)
3789 {
3790 	uint32_t command_error, slot_status, port_status;
3791 	int failed_slot;
3792 	int loop_count = 0;
3793 
3794 	_NOTE(ASSUMING_PROTECTED(si_portp))
3795 
3796 	SIDBG1(SIDBG_ERRS|SIDBG_ENTRY, si_ctlp,
3797 		"si_recover_portmult_errors: port: 0x%x",
3798 		port);
3799 
3800 	/* Resume the port */
3801 	ddi_put32(si_ctlp->sictl_port_acc_handle,
3802 				(uint32_t *)PORT_CONTROL_SET(si_ctlp, port),
3803 				PORT_CONTROL_SET_BITS_RESUME);
3804 
3805 	port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3806 				(uint32_t *)PORT_STATUS(si_ctlp, port));
3807 
3808 	failed_slot = (port_status >> 16) & SI_NUM_SLOTS;
3809 	command_error = ddi_get32(si_ctlp->sictl_port_acc_handle,
3810 			(uint32_t *)(PORT_COMMAND_ERROR(si_ctlp, port)));
3811 
3812 	if (command_error ==  CMD_ERR_SDBERROR) {
3813 		si_portp->siport_err_tags_SDBERROR |= (0x1 << failed_slot);
3814 	} else {
3815 		si_portp->siport_err_tags_nonSDBERROR |= (0x1 << failed_slot);
3816 	}
3817 
3818 	/* Now we drain the pending commands. */
3819 	do {
3820 		slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3821 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
3822 
3823 		/*
3824 		 * Since we have not yet returned DDI_INTR_CLAIMED,
3825 		 * our interrupt handler is guaranteed not to be called again.
3826 		 * So we need to check IS_ATTENTION_RAISED() for further
3827 		 * decisions.
3828 		 *
3829 		 * This is a too big a delay for an interrupt context.
3830 		 * But this is supposed to be a rare condition.
3831 		 */
3832 
3833 		if (IS_ATTENTION_RAISED(slot_status)) {
3834 			/* Resume again */
3835 			ddi_put32(si_ctlp->sictl_port_acc_handle,
3836 				(uint32_t *)PORT_CONTROL_SET(si_ctlp, port),
3837 				PORT_CONTROL_SET_BITS_RESUME);
3838 
3839 			port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3840 				(uint32_t *)PORT_STATUS(si_ctlp, port));
3841 			failed_slot = (port_status >> 16) & SI_NUM_SLOTS;
3842 			command_error = ddi_get32(
3843 				si_ctlp->sictl_port_acc_handle,
3844 				(uint32_t *)(PORT_COMMAND_ERROR(si_ctlp,
3845 							port)));
3846 			if (command_error ==  CMD_ERR_SDBERROR) {
3847 				si_portp->siport_err_tags_SDBERROR |=
3848 							(0x1 << failed_slot);
3849 			} else {
3850 				si_portp->siport_err_tags_nonSDBERROR |=
3851 							(0x1 << failed_slot);
3852 			}
3853 		}
3854 
3855 		if (loop_count++ > SI_POLLRATE_RECOVERPORTMULT) {
3856 			/* We are effectively timing out after 10 sec. */
3857 			break;
3858 		}
3859 
3860 		/* Wait for 10 millisec */
3861 #ifndef __lock_lint
3862 		delay(SI_10MS_TICKS);
3863 #endif /* __lock_lint */
3864 
3865 	} while (slot_status & SI_SLOT_MASK);
3866 
3867 	/*
3868 	 * The above loop can be improved for 3132 since we could obtain the
3869 	 * Port Multiplier Context of the device in error. Then we could
3870 	 * do a better job in filtering out commands for the device in error.
3871 	 * The loop could finish much earlier with such a logic.
3872 	 */
3873 
3874 	/* Clear the RESUME bit. */
3875 	ddi_put32(si_ctlp->sictl_port_acc_handle,
3876 				(uint32_t *)PORT_CONTROL_CLEAR(si_ctlp, port),
3877 				PORT_CONTROL_CLEAR_BITS_RESUME);
3878 
3879 }
3880 
3881 /*
3882  * If we are connected to port multiplier, drain the non-failed devices.
3883  * Otherwise, we initialize the port (which effectively fails all the
3884  * pending commands in the hope that sd would retry them later).
3885  *
3886  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
3887  * before calling us.
3888  */
3889 static void
3890 si_error_recovery_DEVICEERROR(
3891 	si_ctl_state_t *si_ctlp,
3892 	si_port_state_t *si_portp,
3893 	int port)
3894 {
3895 	uint32_t port_status;
3896 	int failed_slot;
3897 
3898 	_NOTE(ASSUMING_PROTECTED(si_portp))
3899 
3900 	SIDBG1(SIDBG_ERRS|SIDBG_ENTRY, si_ctlp,
3901 		"si_error_recovery_DEVICEERROR: port: 0x%x",
3902 		port);
3903 
3904 	if (si_portp->siport_port_type == PORT_TYPE_MULTIPLIER) {
3905 		si_recover_portmult_errors(si_ctlp, si_portp, port);
3906 	} else {
3907 		port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3908 				(uint32_t *)PORT_STATUS(si_ctlp, port));
3909 		failed_slot = (port_status >> 16) & SI_NUM_SLOTS;
3910 		si_portp->siport_err_tags_nonSDBERROR |= (0x1 << failed_slot);
3911 	}
3912 
3913 	/* In either case (port-mult or not), we reinitialize the port. */
3914 	(void) si_initialize_port_wait_till_ready(si_ctlp, port);
3915 }
3916 
3917 /*
3918  * Handle exactly like DEVICEERROR. Remember the tags with SDBERROR
3919  * to perform read_log_ext on them later. SDBERROR means that the
3920  * error was for an NCQ command.
3921  *
3922  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
3923  * before calling us.
3924  */
3925 static void
3926 si_error_recovery_SDBERROR(
3927 	si_ctl_state_t *si_ctlp,
3928 	si_port_state_t *si_portp,
3929 	int port)
3930 {
3931 	uint32_t port_status;
3932 	int failed_slot;
3933 
3934 	_NOTE(ASSUMING_PROTECTED(si_portp))
3935 
3936 	SIDBG1(SIDBG_ERRS|SIDBG_ENTRY, si_ctlp,
3937 		"si3124: si_error_recovery_SDBERROR: port: 0x%x",
3938 		port);
3939 
3940 	if (si_portp->siport_port_type == PORT_TYPE_MULTIPLIER) {
3941 		si_recover_portmult_errors(si_ctlp, si_portp, port);
3942 	} else {
3943 		port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3944 				(uint32_t *)PORT_STATUS(si_ctlp, port));
3945 		failed_slot = (port_status >> 16) & SI_NUM_SLOTS;
3946 		si_portp->siport_err_tags_SDBERROR |= (0x1 << failed_slot);
3947 	}
3948 
3949 	/* In either case (port-mult or not), we reinitialize the port. */
3950 	(void) si_initialize_port_wait_till_ready(si_ctlp, port);
3951 }
3952 
3953 /*
3954  * Handle exactly like DEVICEERROR except resetting the port if there was
3955  * an NCQ command on the port.
3956  *
3957  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
3958  * before calling us.
3959  */
3960 static void
3961 si_error_recovery_DATAFISERROR(
3962 	si_ctl_state_t *si_ctlp,
3963 	si_port_state_t *si_portp,
3964 	int port)
3965 {
3966 	uint32_t port_status;
3967 	int failed_slot;
3968 
3969 	_NOTE(ASSUMING_PROTECTED(si_portp))
3970 
3971 	SIDBG1(SIDBG_ERRS|SIDBG_ENTRY, si_ctlp,
3972 		"si3124: si_error_recovery_DATAFISERROR: port: 0x%x",
3973 		port);
3974 
3975 	/* reset device if we were waiting for any ncq commands. */
3976 	if (si_portp->siport_pending_ncq_count) {
3977 		port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
3978 				(uint32_t *)PORT_STATUS(si_ctlp, port));
3979 		failed_slot = (port_status >> 16) & SI_NUM_SLOTS;
3980 		si_portp->siport_err_tags_nonSDBERROR |= (0x1 << failed_slot);
3981 		(void) si_reset_dport_wait_till_ready(si_ctlp, si_portp, port,
3982 					SI_DEVICE_RESET);
3983 		return;
3984 	}
3985 
3986 	/*
3987 	 * If we don't have any ncq commands pending, the rest of
3988 	 * the process is similar to the one for DEVICEERROR.
3989 	 */
3990 	si_error_recovery_DEVICEERROR(si_ctlp, si_portp, port);
3991 }
3992 
3993 /*
3994  * We handle just like DEVICERROR except that we reset the device instead
3995  * of initializing the port.
3996  *
3997  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
3998  * before calling us.
3999  */
4000 static void
4001 si_error_recovery_SENDFISERROR(
4002 	si_ctl_state_t *si_ctlp,
4003 	si_port_state_t *si_portp,
4004 	int port)
4005 {
4006 	uint32_t port_status;
4007 	int failed_slot;
4008 
4009 	_NOTE(ASSUMING_PROTECTED(si_portp))
4010 
4011 	SIDBG1(SIDBG_ERRS|SIDBG_ENTRY, si_ctlp,
4012 		"si3124: si_error_recovery_SENDFISERROR: port: 0x%x",
4013 		port);
4014 
4015 	if (si_portp->siport_port_type == PORT_TYPE_MULTIPLIER) {
4016 		si_recover_portmult_errors(si_ctlp, si_portp, port);
4017 	} else {
4018 		port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
4019 				(uint32_t *)PORT_STATUS(si_ctlp, port));
4020 		failed_slot = (port_status >> 16) & SI_NUM_SLOTS;
4021 		si_portp->siport_err_tags_nonSDBERROR |= (0x1 << failed_slot);
4022 		(void) si_reset_dport_wait_till_ready(si_ctlp, si_portp, port,
4023 					SI_DEVICE_RESET);
4024 	}
4025 }
4026 
4027 /*
4028  * The default behavior for all other errors is to reset the device.
4029  *
4030  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
4031  * before calling us.
4032  */
4033 static void
4034 si_error_recovery_default(
4035 	si_ctl_state_t *si_ctlp,
4036 	si_port_state_t *si_portp,
4037 	int port)
4038 {
4039 	uint32_t port_status;
4040 	int failed_slot;
4041 
4042 	_NOTE(ASSUMING_PROTECTED(si_portp))
4043 
4044 	SIDBG1(SIDBG_ERRS|SIDBG_ENTRY, si_ctlp,
4045 		"si3124: si_error_recovery_default: port: 0x%x",
4046 		port);
4047 
4048 	port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
4049 				(uint32_t *)PORT_STATUS(si_ctlp, port));
4050 	failed_slot = (port_status >> 16) & SI_NUM_SLOTS;
4051 	si_portp->siport_err_tags_nonSDBERROR |= (0x1 << failed_slot);
4052 
4053 	(void) si_reset_dport_wait_till_ready(si_ctlp, si_portp, port,
4054 					SI_DEVICE_RESET);
4055 }
4056 
4057 /*
4058  * Read Log Ext with PAGE 10 to retrieve the error for an NCQ command.
4059  *
4060  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
4061  * before calling us.
4062  */
4063 static uint8_t
4064 si_read_log_ext(si_ctl_state_t *si_ctlp, si_port_state_t *si_portp, int port)
4065 {
4066 	int slot;
4067 	si_prb_t *prb;
4068 	int i;
4069 	uint32_t slot_status;
4070 	int loop_count = 0;
4071 	uint32_t *prb_word_ptr;
4072 	uint8_t error;
4073 
4074 	_NOTE(ASSUMING_PROTECTED(si_portp))
4075 
4076 	SIDBG1(SIDBG_ENTRY|SIDBG_ERRS, si_ctlp,
4077 			"si_read_log_ext: port: %x", port);
4078 
4079 	slot = si_claim_free_slot(si_ctlp, si_portp, port);
4080 	if (slot == -1) {
4081 		return (0);
4082 	}
4083 
4084 	prb =  &(si_portp->siport_prbpool[slot]);
4085 	bzero((void *)prb, sizeof (si_prb_t));
4086 
4087 	/* Now fill the prb */
4088 	SET_FIS_TYPE(prb->prb_fis, REGISTER_FIS_H2D);
4089 	SET_FIS_PMP(prb->prb_fis, PORTMULT_CONTROL_PORT);
4090 	SET_FIS_CDMDEVCTL(prb->prb_fis, 1);
4091 	SET_FIS_COMMAND(prb->prb_fis, SATAC_READ_LOG_EXT);
4092 	SET_FIS_SECTOR(prb->prb_fis, SATA_LOG_PAGE_10);
4093 
4094 	/* no real data transfer is involved */
4095 	SET_SGE_TRM(prb->prb_sge0);
4096 
4097 #if SI_DEBUG
4098 	if (si_debug_flags & SIDBG_DUMP_PRB) {
4099 		int *ptr;
4100 		int j;
4101 
4102 		ptr = (int *)prb;
4103 		cmn_err(CE_WARN, "read_port_mult_reg, prb: ");
4104 		for (j = 0; j < (sizeof (si_prb_t)/4); j++) {
4105 			cmn_err(CE_WARN, "%x ", ptr[j]);
4106 		}
4107 
4108 	}
4109 #endif /* SI_DEBUG */
4110 
4111 	/* Deliver PRB */
4112 	POST_PRB_ADDR(si_ctlp, si_portp, port, slot);
4113 
4114 	/* Loop till the command is finished. */
4115 	do {
4116 		slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
4117 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
4118 
4119 		SIDBG1(SIDBG_POLL_LOOP, si_ctlp,
4120 			"looping read_log_ext slot_status: 0x%x",
4121 			slot_status);
4122 
4123 		if (loop_count++ > SI_POLLRATE_SLOTSTATUS) {
4124 			/* We are effectively timing out after 0.5 sec. */
4125 			break;
4126 		}
4127 
4128 		/* Wait for 10 millisec */
4129 #ifndef __lock_lint
4130 		delay(SI_10MS_TICKS);
4131 #endif /* __lock_lint */
4132 
4133 	} while (slot_status & SI_SLOT_MASK & (0x1 << slot));
4134 
4135 	if (slot_status & SI_SLOT_MASK & (0x1 << slot)) {
4136 		/*
4137 		 * If we fail with the READ LOG EXT command, we need to
4138 		 * initialize the port to clear the slot_status register.
4139 		 * We don't need to worry about any other valid commands
4140 		 * being thrown away because we are already in recovery
4141 		 * mode and READ LOG EXT is the only pending command.
4142 		 */
4143 		(void) si_initialize_port_wait_till_ready(si_ctlp, port);
4144 	}
4145 
4146 	SIDBG1(SIDBG_POLL_LOOP, si_ctlp,
4147 		"read_portmult_reg: loop count: %d",
4148 		loop_count);
4149 
4150 	/*
4151 	 * The LRAM contains the the modified FIS.
4152 	 * Read the modified FIS to obtain the Error.
4153 	 */
4154 	prb_word_ptr = (uint32_t *)prb;
4155 	for (i = 0; i < (sizeof (si_prb_t)/4); i++) {
4156 		prb_word_ptr[i] = ddi_get32(si_ctlp->sictl_port_acc_handle,
4157 			(uint32_t *)(PORT_LRAM(si_ctlp, port, slot)+i*4));
4158 	}
4159 	error = GET_FIS_FEATURES(prb->prb_fis);
4160 
4161 	CLEAR_BIT(si_portp->siport_pending_tags, slot);
4162 
4163 	return (error);
4164 
4165 }
4166 
4167 /*
4168  * Dump the error message to the log.
4169  */
4170 static void
4171 si_log_error_message(si_ctl_state_t *si_ctlp, int port, uint32_t command_error)
4172 {
4173 	char *errstr;
4174 
4175 	switch (command_error) {
4176 
4177 	case CMD_ERR_DEVICEERRROR:
4178 		errstr = "Standard Error: Error bit set in register - device"
4179 			" to host FIS";
4180 		break;
4181 
4182 	case CMD_ERR_SDBERROR:
4183 		errstr = "NCQ Error: Error bit set in register - device"
4184 			" to host FIS";
4185 		break;
4186 
4187 	case CMD_ERR_DATAFISERROR:
4188 		errstr = "Error in data FIS not detected by device";
4189 		break;
4190 
4191 	case CMD_ERR_SENDFISERROR:
4192 		errstr = "Initial command FIS transmission failed";
4193 		break;
4194 
4195 	case CMD_ERR_INCONSISTENTSTATE:
4196 		errstr = "Inconsistency in protocol";
4197 		break;
4198 
4199 	case CMD_ERR_DIRECTIONERROR:
4200 		errstr = "DMA direction flag does not match the command";
4201 		break;
4202 
4203 	case CMD_ERR_UNDERRUNERROR:
4204 		errstr = "Run out of scatter gather entries while writing data";
4205 		break;
4206 
4207 	case CMD_ERR_OVERRUNERROR:
4208 		errstr = "Run out of scatter gather entries while reading data";
4209 		break;
4210 
4211 	case CMD_ERR_PACKETPROTOCOLERROR:
4212 		errstr = "Packet protocol error";
4213 		break;
4214 
4215 	case CMD_ERR_PLDSGTERRORBOUNDARY:
4216 		errstr = "Scatter/gather table not on quadword boundary";
4217 		break;
4218 
4219 	case CMD_ERR_PLDSGTERRORTARETABORT:
4220 		errstr = "PCI(X) Target abort while fetching scatter/gather"
4221 			" table";
4222 		break;
4223 
4224 	case CMD_ERR_PLDSGTERRORMASTERABORT:
4225 		errstr = "PCI(X) Master abort while fetching scatter/gather"
4226 			" table";
4227 		break;
4228 
4229 	case CMD_ERR_PLDSGTERRORPCIERR:
4230 		errstr = "PCI(X) parity error while fetching scatter/gather"
4231 			" table";
4232 		break;
4233 
4234 	case CMD_ERR_PLDCMDERRORBOUNDARY:
4235 		errstr = "PRB not on quadword boundary";
4236 		break;
4237 
4238 	case CMD_ERR_PLDCMDERRORTARGETABORT:
4239 		errstr = "PCI(X) Target abort while fetching PRB";
4240 		break;
4241 
4242 	case CMD_ERR_PLDCMDERRORMASTERABORT:
4243 		errstr = "PCI(X) Master abort while fetching PRB";
4244 		break;
4245 
4246 	case CMD_ERR_PLDCMDERORPCIERR:
4247 		errstr = "PCI(X) parity error while fetching PRB";
4248 		break;
4249 
4250 	case CMD_ERR_PSDERRORTARGETABORT:
4251 		errstr = "PCI(X) Target abort during data transfer";
4252 		break;
4253 
4254 	case CMD_ERR_PSDERRORMASTERABORT:
4255 		errstr = "PCI(X) Master abort during data transfer";
4256 		break;
4257 
4258 	case CMD_ERR_PSDERRORPCIERR:
4259 		errstr = "PCI(X) parity error during data transfer";
4260 		break;
4261 
4262 	case CMD_ERR_SENDSERVICEERROR:
4263 		errstr = "FIS received while sending service FIS in"
4264 			" legacy queuing operation";
4265 		break;
4266 
4267 	default:
4268 		errstr = "Unknown Error";
4269 		break;
4270 
4271 	}
4272 
4273 	SIDBG2(SIDBG_ERRS, si_ctlp,
4274 			"command error: port: 0x%x, error: %s",
4275 			port,
4276 			errstr);
4277 
4278 }
4279 
4280 
4281 /*
4282  * Interrupt which indicates that the Port Ready state has changed
4283  * from zero to one.
4284  *
4285  * We are not interested in this interrupt; we just log a debug message.
4286  */
4287 /*ARGSUSED*/
4288 static int
4289 si_intr_port_ready(
4290 	si_ctl_state_t *si_ctlp,
4291 	si_port_state_t *si_portp,
4292 	int port)
4293 {
4294 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp, "si_intr_ready");
4295 	return (SI_SUCCESS);
4296 }
4297 
4298 /*
4299  * Interrupt which indicates that the port power management state
4300  * has been modified.
4301  *
4302  * We are not interested in this interrupt; we just log a debug message.
4303  */
4304 /*ARGSUSED*/
4305 static int
4306 si_intr_pwr_change(
4307 	si_ctl_state_t *si_ctlp,
4308 	si_port_state_t *si_portp,
4309 	int port)
4310 {
4311 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp, "si_intr_pwr_change");
4312 	return (SI_SUCCESS);
4313 }
4314 
4315 /*
4316  * Interrupt which indicates that the PHY sate has changed either from
4317  * Not-Ready to Ready or from Ready to Not-Ready.
4318  */
4319 static int
4320 si_intr_phy_ready_change(
4321 	si_ctl_state_t *si_ctlp,
4322 	si_port_state_t *si_portp,
4323 	int port)
4324 {
4325 	sata_device_t sdevice;
4326 	uint32_t SStatus = 0; /* No dev present & PHY not established. */
4327 	int dev_exists_now = 0;
4328 	int dev_existed_previously = 0;
4329 
4330 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp, "si_intr_phy_rdy_change");
4331 
4332 	mutex_enter(&si_ctlp->sictl_mutex);
4333 	if ((si_ctlp->sictl_sata_hba_tran == NULL) || (si_portp == NULL)) {
4334 		/* the whole controller setup is not yet done. */
4335 		mutex_exit(&si_ctlp->sictl_mutex);
4336 		return (SI_SUCCESS);
4337 	}
4338 
4339 	mutex_exit(&si_ctlp->sictl_mutex);
4340 
4341 	mutex_enter(&si_portp->siport_mutex);
4342 
4343 	/* SStatus tells the presence of device. */
4344 	SStatus = ddi_get32(si_ctlp->sictl_port_acc_handle,
4345 				(uint32_t *)PORT_SSTATUS(si_ctlp, port));
4346 	dev_exists_now =
4347 		(SSTATUS_GET_DET(SStatus) == SSTATUS_DET_DEVPRESENT_PHYONLINE);
4348 
4349 	if (si_portp->siport_port_type != PORT_TYPE_NODEV) {
4350 		dev_existed_previously = 1;
4351 	}
4352 
4353 	bzero((void *)&sdevice, sizeof (sata_device_t));
4354 	sdevice.satadev_addr.cport = port;
4355 	sdevice.satadev_addr.pmport = PORTMULT_CONTROL_PORT;
4356 
4357 	/* we don't have a way of determining the exact port-mult port. */
4358 	if (si_portp->siport_port_type == PORT_TYPE_MULTIPLIER) {
4359 		sdevice.satadev_addr.qual = SATA_ADDR_PMPORT;
4360 	} else {
4361 		sdevice.satadev_addr.qual = SATA_ADDR_CPORT;
4362 	}
4363 
4364 	sdevice.satadev_state = SATA_PSTATE_PWRON;
4365 
4366 	if (dev_exists_now) {
4367 		if (dev_existed_previously) {
4368 
4369 			/* Things are fine now. The loss was temporary. */
4370 			SIDBG0(SIDBG_INTR, NULL,
4371 				"phyrdy: doing BOTH EVENTS TOGETHER");
4372 			if (si_portp->siport_active) {
4373 				SIDBG0(SIDBG_EVENT, si_ctlp,
4374 					"sending event: LINK_LOST & "
4375 					"LINK_ESTABLISHED");
4376 
4377 				sata_hba_event_notify(
4378 					si_ctlp->sictl_sata_hba_tran->\
4379 						sata_tran_hba_dip,
4380 					&sdevice,
4381 					SATA_EVNT_LINK_LOST|
4382 					SATA_EVNT_LINK_ESTABLISHED);
4383 			}
4384 
4385 		} else {
4386 
4387 			/* A new device has been detected. */
4388 			mutex_exit(&si_portp->siport_mutex);
4389 			si_find_dev_signature(si_ctlp, si_portp, port,
4390 							PORTMULT_CONTROL_PORT);
4391 			mutex_enter(&si_portp->siport_mutex);
4392 			SIDBG0(SIDBG_INTR, NULL, "phyrdy: doing ATTACH event");
4393 			if (si_portp->siport_active) {
4394 				SIDBG0(SIDBG_EVENT, si_ctlp,
4395 					"sending event up: LINK_ESTABLISHED");
4396 
4397 				sata_hba_event_notify(
4398 					si_ctlp->sictl_sata_hba_tran->\
4399 						sata_tran_hba_dip,
4400 					&sdevice,
4401 					SATA_EVNT_LINK_ESTABLISHED);
4402 			}
4403 
4404 		}
4405 	} else { /* No device exists now */
4406 
4407 		if (dev_existed_previously) {
4408 
4409 			/* An existing device is lost. */
4410 			if (si_portp->siport_active) {
4411 				SIDBG0(SIDBG_EVENT, si_ctlp,
4412 					"sending event up: LINK_LOST");
4413 
4414 				sata_hba_event_notify(
4415 					si_ctlp->sictl_sata_hba_tran->
4416 						sata_tran_hba_dip,
4417 					&sdevice,
4418 					SATA_EVNT_LINK_LOST);
4419 			}
4420 			si_portp->siport_port_type = PORT_TYPE_NODEV;
4421 
4422 		} else {
4423 
4424 			/* spurious interrupt */
4425 			SIDBG0(SIDBG_INTR, NULL,
4426 				"spurious phy ready interrupt");
4427 		}
4428 	}
4429 
4430 	mutex_exit(&si_portp->siport_mutex);
4431 	return (SI_SUCCESS);
4432 }
4433 
4434 
4435 /*
4436  * Interrupt which indicates that a COMWAKE OOB signal has been decoded
4437  * on the receiver.
4438  *
4439  * We are not interested in this interrupt; we just log a debug message.
4440  */
4441 /*ARGSUSED*/
4442 static int
4443 si_intr_comwake_rcvd(
4444 	si_ctl_state_t *si_ctlp,
4445 	si_port_state_t *si_portp,
4446 	int port)
4447 {
4448 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp, "si_intr_commwake_rcvd");
4449 	return (SI_SUCCESS);
4450 }
4451 
4452 /*
4453  * Interrupt which indicates that the F-bit has been set in SError
4454  * Diag field.
4455  *
4456  * We are not interested in this interrupt; we just log a debug message.
4457  */
4458 /*ARGSUSED*/
4459 static int
4460 si_intr_unrecognised_fis(
4461 	si_ctl_state_t *si_ctlp,
4462 	si_port_state_t *si_portp,
4463 	int port)
4464 {
4465 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp, "si_intr_unrecognised_fis");
4466 	return (SI_SUCCESS);
4467 }
4468 
4469 /*
4470  * Interrupt which indicates that the X-bit has been set in SError
4471  * Diag field.
4472  *
4473  * We are not interested in this interrupt; we just log a debug message.
4474  */
4475 /*ARGSUSED*/
4476 static int
4477 si_intr_dev_xchanged(
4478 	si_ctl_state_t *si_ctlp,
4479 	si_port_state_t *si_portp,
4480 	int port)
4481 {
4482 
4483 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp, "si_intr_dev_xchanged");
4484 	return (SI_SUCCESS);
4485 }
4486 
4487 /*
4488  * Interrupt which indicates that the 8b/10 Decode Error counter has
4489  * exceeded the programmed non-zero threshold value.
4490  *
4491  * We are not interested in this interrupt; we just log a debug message.
4492  */
4493 /*ARGSUSED*/
4494 static int
4495 si_intr_decode_err_threshold(
4496 	si_ctl_state_t *si_ctlp,
4497 	si_port_state_t *si_portp,
4498 	int port)
4499 {
4500 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp, "si_intr_err_threshold");
4501 	return (SI_SUCCESS);
4502 }
4503 
4504 /*
4505  * Interrupt which indicates that the CRC Error counter has exceeded the
4506  * programmed non-zero threshold value.
4507  *
4508  * We are not interested in this interrupt; we just log a debug message.
4509  */
4510 /*ARGSUSED*/
4511 static int
4512 si_intr_crc_err_threshold(
4513 	si_ctl_state_t *si_ctlp,
4514 	si_port_state_t *si_portp,
4515 	int port)
4516 {
4517 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp, "si_intr_crc_threshold");
4518 	return (SI_SUCCESS);
4519 }
4520 
4521 /*
4522  * Interrupt which indicates that the Handshake Error counter has
4523  * exceeded the programmed non-zero threshold value.
4524  *
4525  * We are not interested in this interrupt; we just log a debug message.
4526  */
4527 /*ARGSUSED*/
4528 static int
4529 si_intr_handshake_err_threshold(
4530 	si_ctl_state_t *si_ctlp,
4531 	si_port_state_t *si_portp,
4532 	int port)
4533 {
4534 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp,
4535 		"si_intr_handshake_err_threshold");
4536 	return (SI_SUCCESS);
4537 }
4538 
4539 /*
4540  * Interrupt which indicates that a "Set Device Bits" FIS has been
4541  * received with N-bit set in the control field.
4542  *
4543  * We are not interested in this interrupt; we just log a debug message.
4544  */
4545 /*ARGSUSED*/
4546 static int
4547 si_intr_set_devbits_notify(
4548 	si_ctl_state_t *si_ctlp,
4549 	si_port_state_t *si_portp,
4550 	int port)
4551 {
4552 	SIDBG0(SIDBG_INTR|SIDBG_ENTRY, si_ctlp, "si_intr_set_devbits_notify");
4553 	return (SI_SUCCESS);
4554 }
4555 
4556 
4557 /*
4558  * Enable the interrupts for a particular port.
4559  *
4560  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
4561  * before calling us.
4562  */
4563 static void
4564 si_enable_port_interrupts(si_ctl_state_t *si_ctlp, int port)
4565 {
4566 	uint32_t mask;
4567 
4568 	/* get the current settings first. */
4569 	mask = ddi_get32(si_ctlp->sictl_global_acc_handle,
4570 			(uint32_t *)GLOBAL_CONTROL_REG(si_ctlp));
4571 
4572 	SIDBG1(SIDBG_INIT|SIDBG_ENTRY, si_ctlp,
4573 		"si_enable_port_interrupts: current mask: 0x%x",
4574 		mask);
4575 
4576 	/* enable the bit for current port. */
4577 	SET_BIT(mask, port);
4578 
4579 	/* now use this mask to enable the interrupt. */
4580 	ddi_put32(si_ctlp->sictl_global_acc_handle,
4581 			(uint32_t *)GLOBAL_CONTROL_REG(si_ctlp),
4582 			mask);
4583 }
4584 
4585 /*
4586  * Enable interrupts for all the ports.
4587  */
4588 static void
4589 si_enable_all_interrupts(si_ctl_state_t *si_ctlp)
4590 {
4591 	int port;
4592 
4593 	for (port = 0; port < si_ctlp->sictl_num_ports; port++) {
4594 		si_enable_port_interrupts(si_ctlp, port);
4595 	}
4596 }
4597 
4598 /*
4599  * Disable interrupts for a particular port.
4600  *
4601  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
4602  * before calling us.
4603  */
4604 static void
4605 si_disable_port_interrupts(si_ctl_state_t *si_ctlp, int port)
4606 {
4607 	uint32_t mask;
4608 
4609 	/* get the current settings first. */
4610 	mask = ddi_get32(si_ctlp->sictl_global_acc_handle,
4611 			(uint32_t *)GLOBAL_CONTROL_REG(si_ctlp));
4612 
4613 	/* clear the bit for current port. */
4614 	CLEAR_BIT(mask, port);
4615 
4616 	/* now use this mask to disable the interrupt. */
4617 	ddi_put32(si_ctlp->sictl_global_acc_handle,
4618 			(uint32_t *)GLOBAL_CONTROL_REG(si_ctlp),
4619 			mask);
4620 
4621 }
4622 
4623 /*
4624  * Disable interrupts for all the ports.
4625  */
4626 static void
4627 si_disable_all_interrupts(si_ctl_state_t *si_ctlp)
4628 {
4629 	int port;
4630 
4631 	for (port = 0; port < si_ctlp->sictl_num_ports; port++) {
4632 		si_disable_port_interrupts(si_ctlp, port);
4633 	}
4634 }
4635 
4636 /*
4637  * Fetches the latest sstatus, scontrol, serror, sactive registers
4638  * and stuffs them into sata_device_t structure.
4639  */
4640 static void
4641 fill_dev_sregisters(si_ctl_state_t *si_ctlp, int port, sata_device_t *satadev)
4642 {
4643 	satadev->satadev_scr.sstatus = ddi_get32(si_ctlp->sictl_port_acc_handle,
4644 				(uint32_t *)(PORT_SSTATUS(si_ctlp, port)));
4645 	satadev->satadev_scr.serror = ddi_get32(si_ctlp->sictl_port_acc_handle,
4646 				(uint32_t *)(PORT_SERROR(si_ctlp, port)));
4647 	satadev->satadev_scr.sactive = ddi_get32(si_ctlp->sictl_port_acc_handle,
4648 				(uint32_t *)(PORT_SACTIVE(si_ctlp, port)));
4649 	satadev->satadev_scr.scontrol =
4650 			ddi_get32(si_ctlp->sictl_port_acc_handle,
4651 				(uint32_t *)(PORT_SCONTROL(si_ctlp, port)));
4652 
4653 }
4654 
4655 /*
4656  * si_add_legacy_intrs() handles INTx and legacy interrupts.
4657  */
4658 static int
4659 si_add_legacy_intrs(si_ctl_state_t *si_ctlp)
4660 {
4661 	dev_info_t	*devinfo = si_ctlp->sictl_devinfop;
4662 	int		actual, count = 0;
4663 	int		x, y, rc, inum = 0;
4664 
4665 	SIDBG0(SIDBG_ENTRY, si_ctlp, "si_add_legacy_intrs");
4666 
4667 	/* get number of interrupts. */
4668 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_FIXED, &count);
4669 	if ((rc != DDI_SUCCESS) || (count == 0)) {
4670 		SIDBG2(SIDBG_INTR|SIDBG_INIT, si_ctlp,
4671 			"ddi_intr_get_nintrs() failed, "
4672 			"rc %d count %d\n", rc, count);
4673 		return (DDI_FAILURE);
4674 	}
4675 
4676 	/* Allocate an array of interrupt handles. */
4677 	si_ctlp->sictl_intr_size = count * sizeof (ddi_intr_handle_t);
4678 	si_ctlp->sictl_htable = kmem_zalloc(si_ctlp->sictl_intr_size, KM_SLEEP);
4679 
4680 	/* call ddi_intr_alloc(). */
4681 	rc = ddi_intr_alloc(devinfo, si_ctlp->sictl_htable, DDI_INTR_TYPE_FIXED,
4682 		inum, count, &actual, DDI_INTR_ALLOC_STRICT);
4683 
4684 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
4685 		SIDBG1(SIDBG_INTR|SIDBG_INIT, si_ctlp,
4686 			"ddi_intr_alloc() failed, rc %d\n", rc);
4687 		kmem_free(si_ctlp->sictl_htable, si_ctlp->sictl_intr_size);
4688 		return (DDI_FAILURE);
4689 	}
4690 
4691 	if (actual < count) {
4692 		SIDBG2(SIDBG_INTR|SIDBG_INIT, si_ctlp,
4693 			"Requested: %d, Received: %d", count, actual);
4694 
4695 		for (x = 0; x < actual; x++) {
4696 			(void) ddi_intr_free(si_ctlp->sictl_htable[x]);
4697 		}
4698 
4699 		kmem_free(si_ctlp->sictl_htable, si_ctlp->sictl_intr_size);
4700 		return (DDI_FAILURE);
4701 	}
4702 
4703 	si_ctlp->sictl_intr_cnt = actual;
4704 
4705 	/* Get intr priority. */
4706 	if (ddi_intr_get_pri(si_ctlp->sictl_htable[0],
4707 				&si_ctlp->sictl_intr_pri) != DDI_SUCCESS) {
4708 		SIDBG0(SIDBG_INTR|SIDBG_INIT, si_ctlp,
4709 				"ddi_intr_get_pri() failed");
4710 
4711 		for (x = 0; x < actual; x++) {
4712 			(void) ddi_intr_free(si_ctlp->sictl_htable[x]);
4713 		}
4714 
4715 		kmem_free(si_ctlp->sictl_htable, si_ctlp->sictl_intr_size);
4716 		return (DDI_FAILURE);
4717 	}
4718 
4719 	/* Test for high level mutex. */
4720 	if (si_ctlp->sictl_intr_pri >= ddi_intr_get_hilevel_pri()) {
4721 		SIDBG0(SIDBG_INTR|SIDBG_INIT, si_ctlp,
4722 			"si_add_legacy_intrs: Hi level intr not supported");
4723 
4724 		for (x = 0; x < actual; x++) {
4725 			(void) ddi_intr_free(si_ctlp->sictl_htable[x]);
4726 		}
4727 
4728 		kmem_free(si_ctlp->sictl_htable, sizeof (ddi_intr_handle_t));
4729 
4730 		return (DDI_FAILURE);
4731 	}
4732 
4733 	/* Call ddi_intr_add_handler(). */
4734 	for (x = 0; x < actual; x++) {
4735 		if (ddi_intr_add_handler(si_ctlp->sictl_htable[x], si_intr,
4736 		    (caddr_t)si_ctlp, NULL) != DDI_SUCCESS) {
4737 			SIDBG0(SIDBG_INTR|SIDBG_INIT, si_ctlp,
4738 				"ddi_intr_add_handler() failed");
4739 
4740 			for (y = 0; y < actual; y++) {
4741 				(void) ddi_intr_free(si_ctlp->sictl_htable[y]);
4742 			}
4743 
4744 			kmem_free(si_ctlp->sictl_htable,
4745 					si_ctlp->sictl_intr_size);
4746 			return (DDI_FAILURE);
4747 		}
4748 	}
4749 
4750 	/* Call ddi_intr_enable() for legacy interrupts. */
4751 	for (x = 0; x < si_ctlp->sictl_intr_cnt; x++) {
4752 		(void) ddi_intr_enable(si_ctlp->sictl_htable[x]);
4753 	}
4754 
4755 	return (DDI_SUCCESS);
4756 }
4757 
4758 /*
4759  * si_add_msictl_intrs() handles MSI interrupts.
4760  */
4761 static int
4762 si_add_msi_intrs(si_ctl_state_t *si_ctlp)
4763 {
4764 	dev_info_t	*devinfo = si_ctlp->sictl_devinfop;
4765 	int		count, avail, actual;
4766 	int		x, y, rc, inum = 0;
4767 
4768 	SIDBG0(SIDBG_ENTRY|SIDBG_INIT, si_ctlp, "si_add_msi_intrs");
4769 
4770 	/* get number of interrupts. */
4771 	rc = ddi_intr_get_nintrs(devinfo, DDI_INTR_TYPE_MSI, &count);
4772 	if ((rc != DDI_SUCCESS) || (count == 0)) {
4773 		SIDBG2(SIDBG_INIT, si_ctlp,
4774 			"ddi_intr_get_nintrs() failed, "
4775 			"rc %d count %d\n", rc, count);
4776 		return (DDI_FAILURE);
4777 	}
4778 
4779 	/* get number of available interrupts. */
4780 	rc = ddi_intr_get_navail(devinfo, DDI_INTR_TYPE_MSI, &avail);
4781 	if ((rc != DDI_SUCCESS) || (avail == 0)) {
4782 		SIDBG2(SIDBG_INIT, si_ctlp,
4783 			"ddi_intr_get_navail() failed, "
4784 			"rc %d avail %d\n", rc, avail);
4785 		return (DDI_FAILURE);
4786 	}
4787 
4788 	if (avail < count) {
4789 		SIDBG2(SIDBG_INIT, si_ctlp,
4790 			"ddi_intr_get_nvail returned %d, navail() returned %d",
4791 			count, avail);
4792 	}
4793 
4794 	/* Allocate an array of interrupt handles. */
4795 	si_ctlp->sictl_intr_size = count * sizeof (ddi_intr_handle_t);
4796 	si_ctlp->sictl_htable = kmem_alloc(si_ctlp->sictl_intr_size, KM_SLEEP);
4797 
4798 	/* call ddi_intr_alloc(). */
4799 	rc = ddi_intr_alloc(devinfo, si_ctlp->sictl_htable, DDI_INTR_TYPE_MSI,
4800 		inum, count, &actual, DDI_INTR_ALLOC_NORMAL);
4801 
4802 	if ((rc != DDI_SUCCESS) || (actual == 0)) {
4803 		SIDBG1(SIDBG_INIT, si_ctlp,
4804 			"ddi_intr_alloc() failed, rc %d\n", rc);
4805 		kmem_free(si_ctlp->sictl_htable, si_ctlp->sictl_intr_size);
4806 		return (DDI_FAILURE);
4807 	}
4808 
4809 	/* use interrupt count returned */
4810 	if (actual < count) {
4811 		SIDBG2(SIDBG_INIT, si_ctlp,
4812 			"Requested: %d, Received: %d", count, actual);
4813 	}
4814 
4815 	si_ctlp->sictl_intr_cnt = actual;
4816 
4817 	/*
4818 	 * Get priority for first msi, assume remaining are all the same.
4819 	 */
4820 	if (ddi_intr_get_pri(si_ctlp->sictl_htable[0],
4821 				&si_ctlp->sictl_intr_pri) != DDI_SUCCESS) {
4822 		SIDBG0(SIDBG_INIT, si_ctlp, "ddi_intr_get_pri() failed");
4823 
4824 		/* Free already allocated intr. */
4825 		for (y = 0; y < actual; y++) {
4826 			(void) ddi_intr_free(si_ctlp->sictl_htable[y]);
4827 		}
4828 
4829 		kmem_free(si_ctlp->sictl_htable, si_ctlp->sictl_intr_size);
4830 		return (DDI_FAILURE);
4831 	}
4832 
4833 	/* Test for high level mutex. */
4834 	if (si_ctlp->sictl_intr_pri >= ddi_intr_get_hilevel_pri()) {
4835 		SIDBG0(SIDBG_INIT, si_ctlp,
4836 			"si_add_msi_intrs: Hi level intr not supported");
4837 
4838 		/* Free already allocated intr. */
4839 		for (y = 0; y < actual; y++) {
4840 			(void) ddi_intr_free(si_ctlp->sictl_htable[y]);
4841 		}
4842 
4843 		kmem_free(si_ctlp->sictl_htable, sizeof (ddi_intr_handle_t));
4844 
4845 		return (DDI_FAILURE);
4846 	}
4847 
4848 	/* Call ddi_intr_add_handler(). */
4849 	for (x = 0; x < actual; x++) {
4850 		if (ddi_intr_add_handler(si_ctlp->sictl_htable[x], si_intr,
4851 		    (caddr_t)si_ctlp, NULL) != DDI_SUCCESS) {
4852 			SIDBG0(SIDBG_INIT, si_ctlp,
4853 				"ddi_intr_add_handler() failed");
4854 
4855 			/* Free already allocated intr. */
4856 			for (y = 0; y < actual; y++) {
4857 				(void) ddi_intr_free(si_ctlp->sictl_htable[y]);
4858 			}
4859 
4860 			kmem_free(si_ctlp->sictl_htable,
4861 					si_ctlp->sictl_intr_size);
4862 			return (DDI_FAILURE);
4863 		}
4864 	}
4865 
4866 	(void) ddi_intr_get_cap(si_ctlp->sictl_htable[0],
4867 					&si_ctlp->sictl_intr_cap);
4868 
4869 	if (si_ctlp->sictl_intr_cap & DDI_INTR_FLAG_BLOCK) {
4870 		/* Call ddi_intr_block_enable() for MSI. */
4871 		(void) ddi_intr_block_enable(si_ctlp->sictl_htable,
4872 						si_ctlp->sictl_intr_cnt);
4873 	} else {
4874 		/* Call ddi_intr_enable() for MSI non block enable. */
4875 		for (x = 0; x < si_ctlp->sictl_intr_cnt; x++) {
4876 			(void) ddi_intr_enable(si_ctlp->sictl_htable[x]);
4877 		}
4878 	}
4879 
4880 	return (DDI_SUCCESS);
4881 }
4882 
4883 /*
4884  * Removes the registered interrupts irrespective of whether they
4885  * were legacy or MSI.
4886  */
4887 static void
4888 si_rem_intrs(si_ctl_state_t *si_ctlp)
4889 {
4890 	int x;
4891 
4892 	SIDBG0(SIDBG_ENTRY, si_ctlp, "si_rem_intrs entered");
4893 
4894 	/* Disable all interrupts. */
4895 	if ((si_ctlp->sictl_intr_type == DDI_INTR_TYPE_MSI) &&
4896 		(si_ctlp->sictl_intr_cap & DDI_INTR_FLAG_BLOCK)) {
4897 		/* Call ddi_intr_block_disable(). */
4898 		(void) ddi_intr_block_disable(si_ctlp->sictl_htable,
4899 						si_ctlp->sictl_intr_cnt);
4900 	} else {
4901 		for (x = 0; x < si_ctlp->sictl_intr_cnt; x++) {
4902 			(void) ddi_intr_disable(si_ctlp->sictl_htable[x]);
4903 		}
4904 	}
4905 
4906 	/* Call ddi_intr_remove_handler(). */
4907 	for (x = 0; x < si_ctlp->sictl_intr_cnt; x++) {
4908 		(void) ddi_intr_remove_handler(si_ctlp->sictl_htable[x]);
4909 		(void) ddi_intr_free(si_ctlp->sictl_htable[x]);
4910 	}
4911 
4912 	kmem_free(si_ctlp->sictl_htable, si_ctlp->sictl_intr_size);
4913 }
4914 
4915 /*
4916  * Resets either the port or the device connected to the port based on
4917  * the flag variable.
4918  *
4919  * The reset effectively throws away all the pending commands. So, the caller
4920  * has to make provision to handle the pending commands.
4921  *
4922  * After the reset, we wait till the port is ready again.
4923  *
4924  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
4925  * before calling us.
4926  *
4927  * Note: Not port-mult aware.
4928  */
4929 static int
4930 si_reset_dport_wait_till_ready(
4931 	si_ctl_state_t *si_ctlp,
4932 	si_port_state_t *si_portp,
4933 	int port,
4934 	int flag)
4935 {
4936 	uint32_t port_status;
4937 	int loop_count = 0;
4938 	sata_device_t sdevice;
4939 	uint32_t SStatus;
4940 	uint32_t SControl;
4941 
4942 	_NOTE(ASSUMING_PROTECTED(si_portp))
4943 
4944 	if (flag == SI_PORT_RESET) {
4945 		ddi_put32(si_ctlp->sictl_port_acc_handle,
4946 			(uint32_t *)PORT_CONTROL_SET(si_ctlp, port),
4947 			PORT_CONTROL_SET_BITS_PORT_RESET);
4948 
4949 		/* Port reset is not self clearing. So clear it now. */
4950 		ddi_put32(si_ctlp->sictl_port_acc_handle,
4951 			(uint32_t *)PORT_CONTROL_CLEAR(si_ctlp, port),
4952 			PORT_CONTROL_CLEAR_BITS_PORT_RESET);
4953 	} else {
4954 		/* Reset the device. */
4955 		ddi_put32(si_ctlp->sictl_port_acc_handle,
4956 			(uint32_t *)PORT_CONTROL_SET(si_ctlp, port),
4957 			PORT_CONTROL_SET_BITS_DEV_RESET);
4958 
4959 		/*
4960 		 * tidbit: this bit is self clearing; so there is no need
4961 		 * for manual clear as we did for port reset.
4962 		 */
4963 	}
4964 
4965 	/* Set the reset in progress flag */
4966 	if (!(flag & SI_RESET_NO_EVENTS_UP)) {
4967 		si_portp->siport_reset_in_progress = 1;
4968 	}
4969 
4970 	/*
4971 	 * For some reason, we are losing the interrupt enablement after
4972 	 * any reset condition. So restore them back now.
4973 	 */
4974 	SIDBG1(SIDBG_INIT, si_ctlp,
4975 		"current interrupt enable set: 0x%x",
4976 		ddi_get32(si_ctlp->sictl_port_acc_handle,
4977 			(uint32_t *)PORT_INTERRUPT_ENABLE_SET(si_ctlp, port)));
4978 
4979 	ddi_put32(si_ctlp->sictl_port_acc_handle,
4980 			(uint32_t *)PORT_INTERRUPT_ENABLE_SET(si_ctlp, port),
4981 			(INTR_COMMAND_COMPLETE |
4982 			INTR_COMMAND_ERROR |
4983 			INTR_PORT_READY |
4984 			INTR_POWER_CHANGE |
4985 			INTR_PHYRDY_CHANGE |
4986 			INTR_COMWAKE_RECEIVED |
4987 			INTR_UNRECOG_FIS |
4988 			INTR_DEV_XCHANGED |
4989 			INTR_SETDEVBITS_NOTIFY));
4990 
4991 	si_enable_port_interrupts(si_ctlp, port);
4992 
4993 	/*
4994 	 * Every reset needs a PHY initialization.
4995 	 *
4996 	 * The way to initialize the PHY is to write a 1 and then
4997 	 * a 0 to DET field of SControl register.
4998 	 */
4999 
5000 	/* Fetch the current SControl before writing the DET part with 1. */
5001 	SControl = ddi_get32(si_ctlp->sictl_port_acc_handle,
5002 				(uint32_t *)PORT_SCONTROL(si_ctlp, port));
5003 	SCONTROL_SET_DET(SControl, SCONTROL_DET_COMRESET);
5004 	ddi_put32(si_ctlp->sictl_port_acc_handle,
5005 			(uint32_t *)(PORT_SCONTROL(si_ctlp, port)),
5006 			SControl);
5007 #ifndef __lock_lint
5008 	delay(SI_10MS_TICKS); /* give time for COMRESET to percolate */
5009 #endif /* __lock_lint */
5010 
5011 	/* Now fetch the SControl again and rewrite the DET part with 0 */
5012 	SControl = ddi_get32(si_ctlp->sictl_port_acc_handle,
5013 				(uint32_t *)PORT_SCONTROL(si_ctlp, port));
5014 	SCONTROL_SET_DET(SControl, SCONTROL_DET_NOACTION);
5015 	ddi_put32(si_ctlp->sictl_port_acc_handle,
5016 			(uint32_t *)(PORT_SCONTROL(si_ctlp, port)),
5017 			SControl);
5018 
5019 	/*
5020 	 * PHY may be initialized by now. Check the DET field of SStatus
5021 	 * to determine if there is a device present.
5022 	 *
5023 	 * The DET field is valid only if IPM field indicates that
5024 	 * the interface is in active state.
5025 	 */
5026 
5027 	loop_count = 0;
5028 	do {
5029 		SStatus = ddi_get32(si_ctlp->sictl_port_acc_handle,
5030 				(uint32_t *)PORT_SSTATUS(si_ctlp, port));
5031 
5032 		if (SSTATUS_GET_IPM(SStatus) !=
5033 					SSTATUS_IPM_INTERFACE_ACTIVE) {
5034 			/*
5035 			 * If the interface is not active, the DET field
5036 			 * is considered not accurate. So we want to
5037 			 * continue looping.
5038 			 */
5039 			SSTATUS_SET_DET(SStatus, SSTATUS_DET_NODEV_NOPHY);
5040 		}
5041 
5042 		if (loop_count++ > SI_POLLRATE_SSTATUS) {
5043 			/* We are effectively timing out after 0.1 sec. */
5044 			break;
5045 		}
5046 
5047 		/* Wait for 10 millisec */
5048 #ifndef __lock_lint
5049 		delay(SI_10MS_TICKS);
5050 #endif /* __lock_lint */
5051 
5052 	} while (SSTATUS_GET_DET(SStatus) != SSTATUS_DET_DEVPRESENT_PHYONLINE);
5053 
5054 	SIDBG2(SIDBG_POLL_LOOP, si_ctlp,
5055 		"si_reset_dport_wait_till_ready: loop count: %d, \
5056 		SStatus: 0x%x",
5057 		loop_count,
5058 		SStatus);
5059 
5060 	/* Now check for port readiness. */
5061 	loop_count = 0;
5062 	do {
5063 		port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
5064 				(uint32_t *)PORT_STATUS(si_ctlp, port));
5065 
5066 		if (loop_count++ > SI_POLLRATE_PORTREADY) {
5067 			/* We are effectively timing out after 0.5 sec. */
5068 			break;
5069 		}
5070 
5071 		/* Wait for 10 millisec */
5072 #ifndef __lock_lint
5073 		delay(SI_10MS_TICKS);
5074 #endif /* __lock_lint */
5075 
5076 	} while (!(port_status & PORT_STATUS_BITS_PORT_READY));
5077 
5078 	SIDBG3(SIDBG_POLL_LOOP, si_ctlp,
5079 		"si_reset_dport_wait_till_ready: loop count: %d, \
5080 		port_status: 0x%x, SStatus: 0x%x",
5081 		loop_count,
5082 		port_status,
5083 		SStatus);
5084 
5085 	/* Indicate to the framework that a reset has happened. */
5086 	if (!(flag & SI_RESET_NO_EVENTS_UP)) {
5087 
5088 		bzero((void *)&sdevice, sizeof (sata_device_t));
5089 		sdevice.satadev_addr.cport = port;
5090 		sdevice.satadev_addr.pmport = PORTMULT_CONTROL_PORT;
5091 
5092 		if (si_portp->siport_port_type == PORT_TYPE_MULTIPLIER) {
5093 			sdevice.satadev_addr.qual = SATA_ADDR_DPMPORT;
5094 		} else {
5095 			sdevice.satadev_addr.qual = SATA_ADDR_DCPORT;
5096 		}
5097 		sdevice.satadev_state = SATA_DSTATE_RESET |
5098 					SATA_DSTATE_PWR_ACTIVE;
5099 		if (si_ctlp->sictl_sata_hba_tran) {
5100 			sata_hba_event_notify(
5101 			si_ctlp->sictl_sata_hba_tran->sata_tran_hba_dip,
5102 			&sdevice,
5103 			SATA_EVNT_DEVICE_RESET);
5104 		}
5105 
5106 		SIDBG0(SIDBG_EVENT, si_ctlp,
5107 			"sending event up: SATA_EVNT_RESET");
5108 	}
5109 
5110 	if ((SSTATUS_GET_IPM(SStatus) == SSTATUS_IPM_INTERFACE_ACTIVE) &&
5111 		(SSTATUS_GET_DET(SStatus) ==
5112 					SSTATUS_DET_DEVPRESENT_PHYONLINE)) {
5113 		/* The interface is active and the device is present */
5114 		if (!(port_status & PORT_STATUS_BITS_PORT_READY)) {
5115 			/* But the port is is not ready for some reason */
5116 			SIDBG0(SIDBG_POLL_LOOP, si_ctlp,
5117 				"si_reset_dport_wait_till_ready failed");
5118 			return (SI_FAILURE);
5119 		}
5120 	}
5121 
5122 	SIDBG0(SIDBG_POLL_LOOP, si_ctlp,
5123 		"si_reset_dport_wait_till_ready returning success");
5124 
5125 	return (SI_SUCCESS);
5126 }
5127 
5128 /*
5129  * Initializes the port.
5130  *
5131  * Initialization effectively throws away all the pending commands on
5132  * the port. So, the caller  has to make provision to handle the pending
5133  * commands.
5134  *
5135  * After the port initialization, we wait till the port is ready again.
5136  *
5137  * WARNING, WARNING: The caller is expected to obtain the siport_mutex
5138  * before calling us.
5139  */
5140 static int
5141 si_initialize_port_wait_till_ready(si_ctl_state_t *si_ctlp, int port)
5142 {
5143 	uint32_t port_status;
5144 	int loop_count = 0;
5145 	uint32_t SStatus;
5146 
5147 	/* Initialize the port. */
5148 	ddi_put32(si_ctlp->sictl_port_acc_handle,
5149 			(uint32_t *)PORT_CONTROL_SET(si_ctlp, port),
5150 			PORT_CONTROL_SET_BITS_PORT_INITIALIZE);
5151 
5152 	/* Wait until Port Ready */
5153 	loop_count = 0;
5154 	do {
5155 		port_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
5156 				(uint32_t *)PORT_STATUS(si_ctlp, port));
5157 
5158 		if (loop_count++ > SI_POLLRATE_PORTREADY) {
5159 			SIDBG1(SIDBG_INTR, si_ctlp,
5160 				"si_initialize_port_wait is timing out: "
5161 				"port_status: %x",
5162 				port_status);
5163 			/* We are effectively timing out after 0.5 sec. */
5164 			break;
5165 		}
5166 
5167 		/* Wait for 10 millisec */
5168 #ifndef __lock_lint
5169 		delay(SI_10MS_TICKS);
5170 #endif /* __lock_lint */
5171 
5172 	} while (!(port_status & PORT_STATUS_BITS_PORT_READY));
5173 
5174 	SIDBG1(SIDBG_POLL_LOOP, si_ctlp,
5175 		"si_initialize_port_wait_till_ready: loop count: %d",
5176 		loop_count);
5177 
5178 	SStatus = ddi_get32(si_ctlp->sictl_port_acc_handle,
5179 				(uint32_t *)PORT_SSTATUS(si_ctlp, port));
5180 
5181 	if ((SSTATUS_GET_IPM(SStatus) == SSTATUS_IPM_INTERFACE_ACTIVE) &&
5182 		(SSTATUS_GET_DET(SStatus) ==
5183 					SSTATUS_DET_DEVPRESENT_PHYONLINE)) {
5184 		/* The interface is active and the device is present */
5185 		if (!(port_status & PORT_STATUS_BITS_PORT_READY)) {
5186 			/* But the port is is not ready for some reason */
5187 			return (SI_FAILURE);
5188 		}
5189 	}
5190 
5191 	return (SI_SUCCESS);
5192 }
5193 
5194 
5195 /*
5196  * si_watchdog_handler() calls us if it detects that there are some
5197  * commands which timed out. We recalculate the timed out commands once
5198  * again since some of them may have finished recently.
5199  */
5200 static void
5201 si_timeout_pkts(
5202 	si_ctl_state_t *si_ctlp,
5203 	si_port_state_t *si_portp,
5204 	int port,
5205 	uint32_t timedout_tags)
5206 {
5207 	uint32_t slot_status;
5208 	uint32_t finished_tags;
5209 
5210 	SIDBG0(SIDBG_TIMEOUT|SIDBG_ENTRY, si_ctlp, "si_timeout_pkts entry");
5211 
5212 	mutex_enter(&si_portp->siport_mutex);
5213 	slot_status = ddi_get32(si_ctlp->sictl_port_acc_handle,
5214 				(uint32_t *)(PORT_SLOT_STATUS(si_ctlp, port)));
5215 
5216 	/*
5217 	 * Initialize the controller. The only way to timeout the commands
5218 	 * is to reset or initialize the controller. We mop commands after
5219 	 * the initialization.
5220 	 */
5221 	(void) si_initialize_port_wait_till_ready(si_ctlp, port);
5222 
5223 	/*
5224 	 * Recompute the timedout tags since some of them may have finished
5225 	 * meanwhile.
5226 	 */
5227 	finished_tags =  si_portp->siport_pending_tags &
5228 				~slot_status & SI_SLOT_MASK;
5229 	timedout_tags &= ~finished_tags;
5230 
5231 	SIDBG2(SIDBG_TIMEOUT, si_ctlp,
5232 		"si_timeout_pkts: finished: %x, timeout: %x",
5233 		finished_tags,
5234 		timedout_tags);
5235 
5236 	mutex_exit(&si_portp->siport_mutex);
5237 	si_mop_commands(si_ctlp,
5238 			si_portp,
5239 			port,
5240 			slot_status,
5241 			0, /* failed_tags */
5242 			timedout_tags,
5243 			0, /* aborting_tags */
5244 			0);  /* reset_tags */
5245 
5246 }
5247 
5248 
5249 
5250 /*
5251  * Watchdog handler kicks in every 5 seconds to timeout any commands pending
5252  * for long time.
5253  */
5254 static void
5255 si_watchdog_handler(si_ctl_state_t *si_ctlp)
5256 {
5257 	uint32_t pending_tags = 0;
5258 	uint32_t timedout_tags = 0;
5259 	si_port_state_t *si_portp;
5260 	int port;
5261 	int tmpslot;
5262 	sata_pkt_t *satapkt;
5263 
5264 	/* max number of cycles this packet should survive */
5265 	int max_life_cycles;
5266 
5267 	/* how many cycles this packet survived so far */
5268 	int watched_cycles;
5269 
5270 	mutex_enter(&si_ctlp->sictl_mutex);
5271 	SIDBG0(SIDBG_TIMEOUT|SIDBG_ENTRY, si_ctlp,
5272 			"si_watchdog_handler entered");
5273 
5274 	for (port = 0; port < si_ctlp->sictl_num_ports; port++) {
5275 
5276 		si_portp = si_ctlp->sictl_ports[port];
5277 		if (si_portp == NULL) {
5278 			continue;
5279 		}
5280 
5281 		mutex_enter(&si_portp->siport_mutex);
5282 
5283 		if (si_portp->siport_port_type == PORT_TYPE_NODEV) {
5284 			mutex_exit(&si_portp->siport_mutex);
5285 			continue;
5286 		}
5287 
5288 		pending_tags =  si_portp->siport_pending_tags;
5289 		timedout_tags = 0;
5290 		while (pending_tags) {
5291 			tmpslot = ddi_ffs(pending_tags) - 1;
5292 			if (tmpslot == -1) {
5293 				break;
5294 			}
5295 			satapkt = si_portp->siport_slot_pkts[tmpslot];
5296 
5297 			if ((satapkt != NULL) && satapkt->satapkt_time) {
5298 
5299 				/*
5300 				 * We are overloading satapkt_hba_driver_private
5301 				 * with watched_cycle count.
5302 				 *
5303 				 * If a packet has survived for more than it's
5304 				 * max life cycles, it is a candidate for time
5305 				 * out.
5306 				 */
5307 				watched_cycles = (int)(intptr_t)
5308 					satapkt->satapkt_hba_driver_private;
5309 				watched_cycles++;
5310 				max_life_cycles = (satapkt->satapkt_time +
5311 						si_watchdog_timeout - 1) /
5312 						si_watchdog_timeout;
5313 				if (watched_cycles > max_life_cycles) {
5314 					timedout_tags |= (0x1 << tmpslot);
5315 					SIDBG1(SIDBG_TIMEOUT|SIDBG_VERBOSE,
5316 						si_ctlp,
5317 						"watchdog: timedout_tags: 0x%x",
5318 						timedout_tags);
5319 				}
5320 				satapkt->satapkt_hba_driver_private =
5321 					(void *)(intptr_t)watched_cycles;
5322 			}
5323 
5324 			CLEAR_BIT(pending_tags, tmpslot);
5325 		}
5326 
5327 		if (timedout_tags) {
5328 			mutex_exit(&si_portp->siport_mutex);
5329 			mutex_exit(&si_ctlp->sictl_mutex);
5330 			si_timeout_pkts(si_ctlp, si_portp, port, timedout_tags);
5331 			mutex_enter(&si_ctlp->sictl_mutex);
5332 			mutex_enter(&si_portp->siport_mutex);
5333 		}
5334 
5335 		mutex_exit(&si_portp->siport_mutex);
5336 	}
5337 
5338 	/* Reinstall the watchdog timeout handler. */
5339 	if (!(si_ctlp->sictl_flags & SI_NO_TIMEOUTS)) {
5340 		si_ctlp->sictl_timeout_id =
5341 			timeout((void (*)(void *))si_watchdog_handler,
5342 				(caddr_t)si_ctlp, si_watchdog_tick);
5343 	}
5344 	mutex_exit(&si_ctlp->sictl_mutex);
5345 }
5346 
5347 
5348 /*
5349  * Logs the message.
5350  */
5351 static void
5352 si_log(si_ctl_state_t *si_ctlp, uint_t level, char *fmt, ...)
5353 {
5354 	va_list ap;
5355 
5356 	mutex_enter(&si_log_mutex);
5357 
5358 	va_start(ap, fmt);
5359 	if (si_ctlp) {
5360 		(void) sprintf(si_log_buf, "%s-[%d]:",
5361 				ddi_get_name(si_ctlp->sictl_devinfop),
5362 				ddi_get_instance(si_ctlp->sictl_devinfop));
5363 	} else {
5364 		(void) sprintf(si_log_buf, "si3124:");
5365 	}
5366 	(void) vsprintf(si_log_buf, fmt, ap);
5367 	va_end(ap);
5368 
5369 	cmn_err(level, "%s", si_log_buf);
5370 
5371 	mutex_exit(&si_log_mutex);
5372 
5373 }
5374 
5375 static void
5376 si_copy_out_regs(sata_cmd_t *scmd, fis_reg_h2d_t *fisp)
5377 {
5378 	fis_reg_h2d_t	fis = *fisp;
5379 
5380 	if (scmd->satacmd_flags.sata_copy_out_sec_count_msb)
5381 		scmd->satacmd_sec_count_msb = GET_FIS_SECTOR_COUNT_EXP(fis);
5382 	if (scmd->satacmd_flags.sata_copy_out_lba_low_msb)
5383 		scmd->satacmd_lba_low_msb = GET_FIS_SECTOR_EXP(fis);
5384 	if (scmd->satacmd_flags.sata_copy_out_lba_mid_msb)
5385 		scmd->satacmd_lba_mid_msb = GET_FIS_CYL_LOW_EXP(fis);
5386 	if (scmd->satacmd_flags.sata_copy_out_lba_high_msb)
5387 		scmd->satacmd_lba_high_msb = GET_FIS_CYL_HI_EXP(fis);
5388 	if (scmd->satacmd_flags.sata_copy_out_sec_count_lsb)
5389 		scmd->satacmd_sec_count_lsb = GET_FIS_SECTOR_COUNT(fis);
5390 	if (scmd->satacmd_flags.sata_copy_out_lba_low_lsb)
5391 		scmd->satacmd_lba_low_lsb = GET_FIS_SECTOR(fis);
5392 	if (scmd->satacmd_flags.sata_copy_out_lba_mid_lsb)
5393 		scmd->satacmd_lba_mid_lsb = GET_FIS_CYL_LOW(fis);
5394 	if (scmd->satacmd_flags.sata_copy_out_lba_high_lsb)
5395 		scmd->satacmd_lba_high_lsb = GET_FIS_CYL_HI(fis);
5396 	if (scmd->satacmd_flags.sata_copy_out_device_reg)
5397 		scmd->satacmd_device_reg = GET_FIS_DEV_HEAD(fis);
5398 	if (scmd->satacmd_flags.sata_copy_out_error_reg)
5399 		scmd->satacmd_error_reg = GET_FIS_FEATURES(fis);
5400 }
5401