xref: /illumos-gate/usr/src/uts/common/io/scsi/adapters/pmcs/pmcs_scsa.c (revision cf8b971efe8cbaaac8c733c2466206380608c8e4)
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  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * SCSI (SCSA) midlayer interface for PMC drier.
27  */
28 
29 #include <sys/scsi/adapters/pmcs/pmcs.h>
30 
31 extern scsi_lun_t scsi_lun64_to_lun(scsi_lun64_t lun64);
32 
33 static int pmcs_scsa_tran_tgt_init(dev_info_t *, dev_info_t *,
34     scsi_hba_tran_t *, struct scsi_device *);
35 static void pmcs_scsa_tran_tgt_free(dev_info_t *, dev_info_t *,
36     scsi_hba_tran_t *, struct scsi_device *);
37 static int pmcs_scsa_start(struct scsi_address *, struct scsi_pkt *);
38 static int pmcs_scsa_abort(struct scsi_address *, struct scsi_pkt *);
39 static int pmcs_scsa_reset(struct scsi_address *, int);
40 static int pmcs_scsi_reset_notify(struct scsi_address *, int,
41     void (*)(caddr_t), caddr_t);
42 static int pmcs_scsa_getcap(struct scsi_address *, char *, int);
43 static int pmcs_scsa_setcap(struct scsi_address *, char *, int, int);
44 static int pmcs_scsa_setup_pkt(struct scsi_pkt *, int (*)(caddr_t), caddr_t);
45 static void pmcs_scsa_teardown_pkt(struct scsi_pkt *);
46 
47 static int pmcs_smp_init(dev_info_t *, dev_info_t *, smp_hba_tran_t *,
48     smp_device_t *);
49 static void pmcs_smp_free(dev_info_t *, dev_info_t *, smp_hba_tran_t *,
50     smp_device_t *);
51 static int pmcs_smp_start(struct smp_pkt *);
52 
53 static int pmcs_scsi_quiesce(dev_info_t *);
54 static int pmcs_scsi_unquiesce(dev_info_t *);
55 
56 static int pmcs_cap(struct scsi_address *, char *, int, int, int);
57 static pmcs_xscsi_t *
58     pmcs_addr2xp(struct scsi_address *, uint64_t *, pmcs_cmd_t *);
59 static int pmcs_SAS_run(pmcs_cmd_t *, pmcwork_t *);
60 static void pmcs_SAS_done(pmcs_hw_t *, pmcwork_t *, uint32_t *);
61 
62 static int pmcs_SATA_run(pmcs_cmd_t *, pmcwork_t *);
63 static void pmcs_SATA_done(pmcs_hw_t *, pmcwork_t *, uint32_t *);
64 static uint8_t pmcs_SATA_rwparm(uint8_t *, uint32_t *, uint64_t *, uint64_t);
65 
66 static void pmcs_ioerror(pmcs_hw_t *, pmcs_dtype_t pmcs_dtype,
67     pmcwork_t *, uint32_t *);
68 
69 
70 int
71 pmcs_scsa_init(pmcs_hw_t *pwp, const ddi_dma_attr_t *ap)
72 {
73 	scsi_hba_tran_t *tran;
74 	ddi_dma_attr_t pmcs_scsa_dattr;
75 	int flags;
76 
77 	(void) memcpy(&pmcs_scsa_dattr, ap, sizeof (ddi_dma_attr_t));
78 	pmcs_scsa_dattr.dma_attr_sgllen =
79 	    ((PMCS_SGL_NCHUNKS - 1) * (PMCS_MAX_CHUNKS - 1)) + PMCS_SGL_NCHUNKS;
80 	pmcs_scsa_dattr.dma_attr_flags = DDI_DMA_RELAXED_ORDERING;
81 	pmcs_scsa_dattr.dma_attr_flags |= DDI_DMA_FLAGERR;
82 
83 	/*
84 	 * Allocate a transport structure
85 	 */
86 	tran = scsi_hba_tran_alloc(pwp->dip, SCSI_HBA_CANSLEEP);
87 	if (tran == NULL) {
88 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
89 		    "scsi_hba_tran_alloc failed");
90 		return (DDI_FAILURE);
91 	}
92 
93 	tran->tran_hba_private		= pwp;
94 	tran->tran_tgt_init		= pmcs_scsa_tran_tgt_init;
95 	tran->tran_tgt_free		= pmcs_scsa_tran_tgt_free;
96 	tran->tran_start		= pmcs_scsa_start;
97 	tran->tran_abort		= pmcs_scsa_abort;
98 	tran->tran_reset		= pmcs_scsa_reset;
99 	tran->tran_reset_notify		= pmcs_scsi_reset_notify;
100 	tran->tran_getcap		= pmcs_scsa_getcap;
101 	tran->tran_setcap		= pmcs_scsa_setcap;
102 	tran->tran_setup_pkt		= pmcs_scsa_setup_pkt;
103 	tran->tran_teardown_pkt		= pmcs_scsa_teardown_pkt;
104 	tran->tran_quiesce		= pmcs_scsi_quiesce;
105 	tran->tran_unquiesce		= pmcs_scsi_unquiesce;
106 	tran->tran_interconnect_type	= INTERCONNECT_SAS;
107 	tran->tran_hba_len		= sizeof (pmcs_cmd_t);
108 
109 	/*
110 	 * Attach this instance of the hba
111 	 */
112 
113 	flags = SCSI_HBA_TRAN_SCB | SCSI_HBA_TRAN_CDB | SCSI_HBA_ADDR_COMPLEX |
114 	    SCSI_HBA_TRAN_PHCI | SCSI_HBA_HBA;
115 
116 	if (scsi_hba_attach_setup(pwp->dip, &pmcs_scsa_dattr, tran, flags)) {
117 		scsi_hba_tran_free(tran);
118 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
119 		    "scsi_hba_attach failed");
120 		return (DDI_FAILURE);
121 	}
122 	pwp->tran = tran;
123 
124 	/*
125 	 * Attach the SMP part of this hba
126 	 */
127 	pwp->smp_tran = smp_hba_tran_alloc(pwp->dip);
128 	ASSERT(pwp->smp_tran != NULL);
129 	pwp->smp_tran->smp_tran_hba_private = pwp;
130 	pwp->smp_tran->smp_tran_init = pmcs_smp_init;
131 	pwp->smp_tran->smp_tran_free = pmcs_smp_free;
132 	pwp->smp_tran->smp_tran_start = pmcs_smp_start;
133 
134 	if (smp_hba_attach_setup(pwp->dip, pwp->smp_tran) != DDI_SUCCESS) {
135 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
136 		    "smp_hba_attach failed");
137 		smp_hba_tran_free(pwp->smp_tran);
138 		pwp->smp_tran = NULL;
139 		scsi_hba_tran_free(tran);
140 		return (DDI_FAILURE);
141 	}
142 
143 	return (DDI_SUCCESS);
144 }
145 
146 /*
147  * SCSA entry points
148  */
149 
150 static int
151 pmcs_scsa_tran_tgt_init(dev_info_t *hba_dip, dev_info_t *tgt_dip,
152     scsi_hba_tran_t *tran, struct scsi_device *sd)
153 {
154 	pmcs_hw_t	*pwp = NULL;
155 	int		rval;
156 	char		*variant_prop = "sata";
157 	char		*tgt_port = NULL, *ua = NULL;
158 	pmcs_xscsi_t	*tgt = NULL;
159 	pmcs_iport_t	*iport;
160 	pmcs_lun_t	*lun = NULL;
161 	pmcs_phy_t	*phyp = NULL;
162 	uint64_t	lun_num;
163 	boolean_t	got_scratch = B_FALSE;
164 
165 	/*
166 	 * First, make sure we're an iport and get the pointer to the HBA
167 	 * node's softstate
168 	 */
169 	if (scsi_hba_iport_unit_address(hba_dip) == NULL) {
170 		pmcs_prt(TRAN2PMC(tran), PMCS_PRT_DEBUG_CONFIG, NULL, NULL,
171 		    "%s: We don't enumerate devices on the HBA node", __func__);
172 		goto tgt_init_fail;
173 	}
174 
175 	pwp = ITRAN2PMC(tran);
176 	iport = ITRAN2IPORT(tran);
177 
178 	/*
179 	 * Get the target address
180 	 */
181 	rval = scsi_device_prop_lookup_string(sd, SCSI_DEVICE_PROP_PATH,
182 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port);
183 	if (rval != DDI_PROP_SUCCESS) {
184 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL,
185 		    "Couldn't get target UA");
186 		pwp = NULL;
187 		goto tgt_init_fail;
188 	}
189 	pmcs_prt(pwp, PMCS_PRT_DEBUG3, NULL, NULL,
190 	    "got tgt_port '%s'", tgt_port);
191 
192 	/*
193 	 * Validate that this tran_tgt_init is for an active iport.
194 	 */
195 	if (iport->ua_state == UA_INACTIVE) {
196 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
197 		    "%s: Got tran_tgt_init on inactive iport for '%s'",
198 		    __func__, tgt_port);
199 		pwp = NULL;
200 		goto tgt_init_fail;
201 	}
202 
203 	/*
204 	 * Since we're going to wait for scratch, be sure to acquire it while
205 	 * we're not holding any other locks
206 	 */
207 	(void) pmcs_acquire_scratch(pwp, B_TRUE);
208 	got_scratch = B_TRUE;
209 
210 	mutex_enter(&pwp->lock);
211 
212 	/*
213 	 * See if there's already a target softstate.  If not, allocate one.
214 	 */
215 	tgt = pmcs_get_target(iport, tgt_port);
216 
217 	if (tgt == NULL) {
218 		goto tgt_init_fail;
219 	}
220 
221 	phyp = tgt->phy;
222 	if (!IS_ROOT_PHY(phyp)) {
223 		pmcs_inc_phy_ref_count(phyp);
224 	}
225 	ASSERT(mutex_owned(&phyp->phy_lock));
226 
227 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, phyp, tgt, "tgt = 0x%p, dip = 0x%p",
228 	    (void *)tgt, (void *)tgt_dip);
229 
230 	/*
231 	 * Now get the full "w<WWN>,LUN" unit-address (including LU).
232 	 */
233 	ua = scsi_device_unit_address(sd);
234 	if (ua == NULL) {
235 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
236 		    "Couldn't get LU unit address");
237 		goto tgt_init_fail;
238 	}
239 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, tgt, "got lun ua '%s'", ua);
240 
241 	lun_num = scsi_device_prop_get_int64(sd, SCSI_DEVICE_PROP_PATH,
242 	    SCSI_ADDR_PROP_LUN64, SCSI_LUN64_ILLEGAL);
243 	if (lun_num == SCSI_LUN64_ILLEGAL) {
244 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
245 		    "No LUN for tgt %p", (void *)tgt);
246 		goto tgt_init_fail;
247 	}
248 
249 	pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt, "%s: @%s tgt 0x%p phy "
250 	    "0x%p (%s)", __func__, ua, (void *)tgt, (void *)phyp, phyp->path);
251 
252 	mutex_enter(&tgt->statlock);
253 	tgt->dtype = phyp->dtype;
254 	if (tgt->dtype != SAS && tgt->dtype != SATA) {
255 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
256 		    "PHY 0x%p went away?", (void *)phyp);
257 		goto tgt_init_fail;
258 	}
259 
260 	/* We don't support SATA devices at LUN > 0. */
261 	if ((tgt->dtype == SATA) && (lun_num > 0)) {
262 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
263 		    "%s: No support for SATA devices at LUN > 0 "
264 		    "(target = 0x%p)", __func__, (void *)tgt);
265 		goto tgt_init_fail;
266 	}
267 
268 	/*
269 	 * Allocate LU soft state. We use ddi_soft_state_bystr_zalloc instead
270 	 * of kmem_alloc because ddi_soft_state_bystr_zalloc allows us to
271 	 * verify that the framework never tries to initialize two scsi_device
272 	 * structures with the same unit-address at the same time.
273 	 */
274 	if (ddi_soft_state_bystr_zalloc(tgt->lun_sstate, ua) != DDI_SUCCESS) {
275 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, phyp, tgt,
276 		    "Couldn't allocate LU soft state");
277 		goto tgt_init_fail;
278 	}
279 
280 	lun = ddi_soft_state_bystr_get(tgt->lun_sstate, ua);
281 	if (lun == NULL) {
282 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, phyp, tgt,
283 		    "Couldn't get LU soft state");
284 		goto tgt_init_fail;
285 	}
286 	scsi_device_hba_private_set(sd, lun);
287 	lun->lun_num = lun_num;
288 
289 	/* convert the scsi_lun64_t value to SCSI standard form */
290 	lun->scsi_lun = scsi_lun64_to_lun(lun_num);
291 
292 	ASSERT(strlen(ua) < (PMCS_MAX_UA_SIZE - 1));
293 	bcopy(ua, lun->unit_address, strnlen(ua, PMCS_MAX_UA_SIZE - 1));
294 
295 	lun->target = tgt;
296 
297 	/*
298 	 * If this is the first tran_tgt_init, add this target to our list
299 	 */
300 	if (tgt->target_num == PMCS_INVALID_TARGET_NUM) {
301 		int target;
302 		for (target = 0; target < pwp->max_dev; target++) {
303 			if (pwp->targets[target] != NULL) {
304 				continue;
305 			}
306 
307 			pwp->targets[target] = tgt;
308 			tgt->target_num = (uint16_t)target;
309 			break;
310 		}
311 
312 		if (target == pwp->max_dev) {
313 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
314 			    "Target list full.");
315 			goto tgt_init_fail;
316 		}
317 	}
318 
319 	tgt->dip = sd->sd_dev;
320 
321 	if (!pmcs_assign_device(pwp, tgt)) {
322 		pmcs_release_scratch(pwp);
323 		pwp->targets[tgt->target_num] = NULL;
324 		tgt->target_num = PMCS_INVALID_TARGET_NUM;
325 		tgt->phy = NULL;
326 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
327 		    "%s: pmcs_assign_device failed for target 0x%p",
328 		    __func__, (void *)tgt);
329 		goto tgt_init_fail;
330 	}
331 
332 	pmcs_release_scratch(pwp);
333 	tgt->ref_count++;
334 
335 	(void) scsi_device_prop_update_int(sd, SCSI_DEVICE_PROP_PATH,
336 	    SCSI_ADDR_PROP_TARGET, (uint32_t)(tgt->target_num));
337 
338 	/* SM-HBA */
339 	if (tgt->dtype == SATA) {
340 		/* TCR in PSARC/1997/281 opinion */
341 		(void) scsi_device_prop_update_string(sd,
342 		    SCSI_DEVICE_PROP_PATH, "variant", variant_prop);
343 	}
344 
345 	tgt->phy_addressable = PMCS_PHY_ADDRESSABLE(phyp);
346 
347 	if (tgt->phy_addressable) {
348 		(void) scsi_device_prop_update_int(sd, SCSI_DEVICE_PROP_PATH,
349 		    SCSI_ADDR_PROP_SATA_PHY, phyp->phynum);
350 	}
351 
352 	/* SM-HBA */
353 	(void) pmcs_smhba_set_scsi_device_props(pwp, phyp, sd);
354 
355 	mutex_exit(&tgt->statlock);
356 	pmcs_unlock_phy(phyp);
357 	mutex_exit(&pwp->lock);
358 	scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, tgt_port);
359 	return (DDI_SUCCESS);
360 
361 tgt_init_fail:
362 	if (got_scratch) {
363 		pmcs_release_scratch(pwp);
364 	}
365 	if (lun) {
366 		ddi_soft_state_bystr_free(tgt->lun_sstate, ua);
367 	}
368 	if (phyp) {
369 		mutex_exit(&tgt->statlock);
370 		pmcs_unlock_phy(phyp);
371 		/*
372 		 * phyp's ref count was incremented in pmcs_new_tport.
373 		 * We're failing configuration, we now need to decrement it.
374 		 */
375 		if (!IS_ROOT_PHY(phyp)) {
376 			pmcs_dec_phy_ref_count(phyp);
377 		}
378 		phyp->target = NULL;
379 	}
380 	if (tgt && tgt->ref_count == 0) {
381 		ddi_soft_state_bystr_free(iport->tgt_sstate, tgt_port);
382 	}
383 	if (pwp) {
384 		mutex_exit(&pwp->lock);
385 	}
386 	if (tgt_port) {
387 		scsi_device_prop_free(sd, SCSI_DEVICE_PROP_PATH, tgt_port);
388 	}
389 	return (DDI_FAILURE);
390 }
391 
392 static void
393 pmcs_scsa_tran_tgt_free(dev_info_t *hba_dip, dev_info_t *tgt_dip,
394     scsi_hba_tran_t *tran, struct scsi_device *sd)
395 {
396 	_NOTE(ARGUNUSED(hba_dip, tgt_dip));
397 	pmcs_hw_t	*pwp;
398 	pmcs_lun_t	*lun;
399 	pmcs_xscsi_t	*target;
400 	char		*unit_address;
401 	pmcs_phy_t	*phyp;
402 
403 	if (scsi_hba_iport_unit_address(hba_dip) == NULL) {
404 		pwp = TRAN2PMC(tran);
405 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL,
406 		    "%s: We don't enumerate devices on the HBA node", __func__);
407 		return;
408 	}
409 
410 	lun = (pmcs_lun_t *)scsi_device_hba_private_get(sd);
411 
412 	ASSERT((lun != NULL) && (lun->target != NULL));
413 	ASSERT(lun->target->ref_count > 0);
414 
415 	target = lun->target;
416 
417 	unit_address = lun->unit_address;
418 	ddi_soft_state_bystr_free(lun->target->lun_sstate, unit_address);
419 
420 	pwp = ITRAN2PMC(tran);
421 	mutex_enter(&pwp->lock);
422 	mutex_enter(&target->statlock);
423 	ASSERT(target->phy);
424 	phyp = target->phy;
425 
426 	if (target->recover_wait) {
427 		mutex_exit(&target->statlock);
428 		mutex_exit(&pwp->lock);
429 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, target, "%s: "
430 		    "Target 0x%p in device state recovery, fail tran_tgt_free",
431 		    __func__, (void *)target);
432 		return;
433 	}
434 
435 	/*
436 	 * If this target still has a PHY pointer and that PHY's target pointer
437 	 * has been cleared, then that PHY has been reaped. In that case, there
438 	 * would be no need to decrement the reference count
439 	 */
440 	if (phyp && !IS_ROOT_PHY(phyp) && phyp->target) {
441 		pmcs_dec_phy_ref_count(phyp);
442 	}
443 
444 	if (--target->ref_count == 0) {
445 		/*
446 		 * Remove this target from our list.  The target soft
447 		 * state will remain, and the device will remain registered
448 		 * with the hardware unless/until we're told the device
449 		 * physically went away.
450 		 */
451 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, target,
452 		    "%s: Free target 0x%p (vtgt %d)", __func__, (void *)target,
453 		    target->target_num);
454 		pwp->targets[target->target_num] = NULL;
455 		target->target_num = PMCS_INVALID_TARGET_NUM;
456 		/*
457 		 * If the target still has a PHY pointer, break the linkage
458 		 */
459 		if (phyp) {
460 			phyp->target = NULL;
461 		}
462 		target->phy = NULL;
463 		pmcs_destroy_target(target);
464 	} else {
465 		mutex_exit(&target->statlock);
466 	}
467 
468 	mutex_exit(&pwp->lock);
469 }
470 
471 static int
472 pmcs_scsa_start(struct scsi_address *ap, struct scsi_pkt *pkt)
473 {
474 	pmcs_cmd_t *sp = PKT2CMD(pkt);
475 	pmcs_hw_t *pwp = ADDR2PMC(ap);
476 	pmcs_xscsi_t *xp;
477 	boolean_t blocked;
478 	uint32_t hba_state;
479 
480 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL,
481 	    "%s: pkt %p sd %p cdb0=0x%02x dl=%lu", __func__, (void *)pkt,
482 	    (void *)scsi_address_device(&pkt->pkt_address),
483 	    pkt->pkt_cdbp[0] & 0xff, pkt->pkt_dma_len);
484 
485 	if (pkt->pkt_flags & FLAG_NOINTR) {
486 		pmcs_prt(pwp, PMCS_PRT_DEBUG3, NULL, NULL,
487 		    "%s: nointr pkt", __func__);
488 		return (TRAN_BADPKT);
489 	}
490 
491 	sp->cmd_tag = 0;
492 	pkt->pkt_state = pkt->pkt_statistics = 0;
493 	pkt->pkt_reason = CMD_INCOMPLETE;
494 
495 	mutex_enter(&pwp->lock);
496 	hba_state = pwp->state;
497 	blocked = pwp->blocked;
498 	mutex_exit(&pwp->lock);
499 
500 	if (hba_state != STATE_RUNNING) {
501 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
502 		    "%s: hba dead", __func__);
503 		return (TRAN_FATAL_ERROR);
504 	}
505 
506 	xp = pmcs_addr2xp(ap, NULL, sp);
507 	if (xp == NULL) {
508 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL,
509 		    "%s: dropping due to null target", __func__);
510 		goto dead_target;
511 	}
512 	ASSERT(mutex_owned(&xp->statlock));
513 
514 	/*
515 	 * First, check to see if the device is gone.
516 	 */
517 	if (xp->dev_gone) {
518 		mutex_exit(&xp->statlock);
519 		pmcs_prt(pwp, PMCS_PRT_DEBUG3, NULL, xp,
520 		    "%s: dropping due to dead target 0x%p",
521 		    __func__, (void *)xp);
522 		goto dead_target;
523 	}
524 
525 	/*
526 	 * If we're blocked (quiesced) just return.
527 	 */
528 	if (blocked) {
529 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
530 		    "%s: hba blocked", __func__);
531 		mutex_exit(&xp->statlock);
532 		mutex_enter(&xp->wqlock);
533 		STAILQ_INSERT_TAIL(&xp->wq, sp, cmd_next);
534 		mutex_exit(&xp->wqlock);
535 		return (TRAN_ACCEPT);
536 	}
537 
538 	/*
539 	 * If we're draining or resetting, queue and return.
540 	 */
541 	if (xp->draining || xp->resetting || xp->recover_wait) {
542 		mutex_exit(&xp->statlock);
543 		mutex_enter(&xp->wqlock);
544 		STAILQ_INSERT_TAIL(&xp->wq, sp, cmd_next);
545 		mutex_exit(&xp->wqlock);
546 		pmcs_prt(pwp, PMCS_PRT_DEBUG1, NULL, xp,
547 		    "%s: draining/resetting/recovering (cnt %u)",
548 		    __func__, xp->actv_cnt);
549 		/*
550 		 * By the time we get here, draining or
551 		 * resetting may have come and gone, not
552 		 * yet noticing that we had put something
553 		 * on the wait queue, so schedule a worker
554 		 * to look at this later.
555 		 */
556 		SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
557 		return (TRAN_ACCEPT);
558 	}
559 	mutex_exit(&xp->statlock);
560 
561 	/*
562 	 * Queue this command to the tail of the wait queue.
563 	 * This keeps us getting commands out of order.
564 	 */
565 	mutex_enter(&xp->wqlock);
566 	STAILQ_INSERT_TAIL(&xp->wq, sp, cmd_next);
567 	mutex_exit(&xp->wqlock);
568 
569 	/*
570 	 * Now run the queue for this device.
571 	 */
572 	(void) pmcs_scsa_wq_run_one(pwp, xp);
573 
574 	return (TRAN_ACCEPT);
575 
576 dead_target:
577 	pkt->pkt_state = STATE_GOT_BUS;
578 	pkt->pkt_reason = CMD_DEV_GONE;
579 	mutex_enter(&pwp->cq_lock);
580 	STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
581 	PMCS_CQ_RUN_LOCKED(pwp);
582 	mutex_exit(&pwp->cq_lock);
583 	return (TRAN_ACCEPT);
584 }
585 
586 static int
587 pmcs_scsa_abort(struct scsi_address *ap, struct scsi_pkt *pkt)
588 {
589 	pmcs_hw_t *pwp = ADDR2PMC(ap);
590 	pmcs_cmd_t *sp = PKT2CMD(pkt);
591 	pmcs_xscsi_t *xp = sp->cmd_target;
592 	pmcs_phy_t *pptr;
593 	uint32_t tag;
594 	uint64_t lun;
595 	pmcwork_t *pwrk;
596 
597 	mutex_enter(&pwp->lock);
598 	if (pwp->state != STATE_RUNNING) {
599 		mutex_exit(&pwp->lock);
600 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
601 		    "%s: hba dead", __func__);
602 		return (0);
603 	}
604 	mutex_exit(&pwp->lock);
605 
606 	if (sp->cmd_lun) {
607 		lun = sp->cmd_lun->lun_num;
608 	} else {
609 		lun = 0;
610 	}
611 	if (xp == NULL) {
612 		return (0);
613 	}
614 
615 	/*
616 	 * See if we have a real work structure associated with this cmd.
617 	 */
618 	pwrk = pmcs_tag2wp(pwp, sp->cmd_tag);
619 	if (pwrk && pwrk->arg == sp) {
620 		tag = pwrk->htag;
621 		pptr = pwrk->phy;
622 		pwrk->timer = 0;	/* we don't time this here */
623 		ASSERT(pwrk->state == PMCS_WORK_STATE_ONCHIP);
624 		mutex_exit(&pwrk->lock);
625 		pmcs_lock_phy(pptr);
626 		if (pptr->dtype == SAS) {
627 			if (pmcs_ssp_tmf(pwp, pptr, SAS_ABORT_TASK, tag, lun,
628 			    NULL)) {
629 				pptr->abort_pending = 1;
630 				pmcs_unlock_phy(pptr);
631 				SCHEDULE_WORK(pwp, PMCS_WORK_ABORT_HANDLE);
632 				return (0);
633 			}
634 		} else {
635 			/*
636 			 * XXX: Was the command that was active an
637 			 * NCQ I/O command?
638 			 */
639 			pptr->need_rl_ext = 1;
640 			if (pmcs_sata_abort_ncq(pwp, pptr)) {
641 				pptr->abort_pending = 1;
642 				pmcs_unlock_phy(pptr);
643 				SCHEDULE_WORK(pwp, PMCS_WORK_ABORT_HANDLE);
644 				return (0);
645 			}
646 		}
647 		pptr->abort_pending = 1;
648 		pmcs_unlock_phy(pptr);
649 		SCHEDULE_WORK(pwp, PMCS_WORK_ABORT_HANDLE);
650 		return (1);
651 	}
652 	if (pwrk) {
653 		mutex_exit(&pwrk->lock);
654 	}
655 	/*
656 	 * Okay, those weren't the droids we were looking for.
657 	 * See if the command is on any of the wait queues.
658 	 */
659 	mutex_enter(&xp->wqlock);
660 	sp = NULL;
661 	STAILQ_FOREACH(sp, &xp->wq, cmd_next) {
662 		if (sp == PKT2CMD(pkt)) {
663 			STAILQ_REMOVE(&xp->wq, sp, pmcs_cmd, cmd_next);
664 			break;
665 		}
666 	}
667 	mutex_exit(&xp->wqlock);
668 	if (sp) {
669 		pkt->pkt_reason = CMD_ABORTED;
670 		pkt->pkt_statistics |= STAT_ABORTED;
671 		mutex_enter(&pwp->cq_lock);
672 		STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
673 		PMCS_CQ_RUN_LOCKED(pwp);
674 		mutex_exit(&pwp->cq_lock);
675 		return (1);
676 	}
677 	return (0);
678 }
679 
680 /*
681  * SCSA reset functions
682  */
683 static int
684 pmcs_scsa_reset(struct scsi_address *ap, int level)
685 {
686 	pmcs_hw_t *pwp = ADDR2PMC(ap);
687 	pmcs_phy_t *pptr;
688 	pmcs_xscsi_t *xp;
689 	uint64_t lun = (uint64_t)-1, *lp = NULL;
690 	int rval;
691 
692 	mutex_enter(&pwp->lock);
693 	if (pwp->state != STATE_RUNNING) {
694 		mutex_exit(&pwp->lock);
695 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
696 		    "%s: hba dead", __func__);
697 		return (0);
698 	}
699 	mutex_exit(&pwp->lock);
700 
701 	switch (level)  {
702 	case RESET_ALL:
703 		rval = 0;
704 		break;
705 	case RESET_LUN:
706 		/*
707 		 * Point lp at lun so that pmcs_addr2xp
708 		 * will fill out the 64 bit lun number.
709 		 */
710 		lp = &lun;
711 		/* FALLTHROUGH */
712 	case RESET_TARGET:
713 		xp = pmcs_addr2xp(ap, lp, NULL);
714 		if (xp == NULL) {
715 			pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
716 			    "%s: no xp found for this scsi address", __func__);
717 			return (0);
718 		}
719 
720 		if (xp->dev_gone) {
721 			mutex_exit(&xp->statlock);
722 			pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
723 			    "%s: Target 0x%p has gone away", __func__,
724 			    (void *)xp);
725 			return (0);
726 		}
727 
728 		/*
729 		 * If we're already performing this action, or if device
730 		 * state recovery is already running, just return failure.
731 		 */
732 		if (xp->resetting || xp->recover_wait) {
733 			mutex_exit(&xp->statlock);
734 			return (0);
735 		}
736 		xp->reset_wait = 0;
737 		xp->reset_success = 0;
738 		xp->resetting = 1;
739 		pptr = xp->phy;
740 		mutex_exit(&xp->statlock);
741 
742 		if (pmcs_reset_dev(pwp, pptr, lun)) {
743 			rval = 0;
744 		} else {
745 			rval = 1;
746 		}
747 
748 		mutex_enter(&xp->statlock);
749 		if (rval == 1) {
750 			xp->reset_success = 1;
751 		}
752 		if (xp->reset_wait) {
753 			xp->reset_wait = 0;
754 			cv_signal(&xp->reset_cv);
755 		}
756 		xp->resetting = 0;
757 		mutex_exit(&xp->statlock);
758 		SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
759 		break;
760 	default:
761 		rval = 0;
762 		break;
763 	}
764 
765 	return (rval);
766 }
767 
768 static int
769 pmcs_scsi_reset_notify(struct scsi_address *ap, int flag,
770     void (*callback)(caddr_t), caddr_t arg)
771 {
772 	pmcs_hw_t *pwp = ADDR2PMC(ap);
773 	return (scsi_hba_reset_notify_setup(ap, flag, callback, arg,
774 	    &pwp->lock, &pwp->reset_notify_listf));
775 }
776 
777 
778 static int
779 pmcs_cap(struct scsi_address *ap, char *cap, int val, int tonly, int set)
780 {
781 	_NOTE(ARGUNUSED(val, tonly));
782 	int cidx, rval = 0;
783 	pmcs_xscsi_t *xp;
784 
785 	cidx = scsi_hba_lookup_capstr(cap);
786 	if (cidx == -1) {
787 		return (-1);
788 	}
789 
790 	xp = pmcs_addr2xp(ap, NULL, NULL);
791 	if (xp == NULL) {
792 		return (-1);
793 	}
794 
795 	switch (cidx) {
796 	case SCSI_CAP_DMA_MAX:
797 	case SCSI_CAP_INITIATOR_ID:
798 		if (set == 0) {
799 			rval = INT_MAX;	/* argh */
800 		}
801 		break;
802 	case SCSI_CAP_DISCONNECT:
803 	case SCSI_CAP_SYNCHRONOUS:
804 	case SCSI_CAP_WIDE_XFER:
805 	case SCSI_CAP_PARITY:
806 	case SCSI_CAP_ARQ:
807 	case SCSI_CAP_UNTAGGED_QING:
808 		if (set == 0) {
809 			rval = 1;
810 		}
811 		break;
812 
813 	case SCSI_CAP_TAGGED_QING:
814 		rval = 1;
815 		break;
816 
817 	case SCSI_CAP_MSG_OUT:
818 	case SCSI_CAP_RESET_NOTIFICATION:
819 	case SCSI_CAP_QFULL_RETRIES:
820 	case SCSI_CAP_QFULL_RETRY_INTERVAL:
821 		break;
822 	case SCSI_CAP_SCSI_VERSION:
823 		if (set == 0) {
824 			rval = SCSI_VERSION_3;
825 		}
826 		break;
827 	case SCSI_CAP_INTERCONNECT_TYPE:
828 		if (set) {
829 			break;
830 		}
831 		if (xp->phy_addressable) {
832 			rval = INTERCONNECT_SATA;
833 		} else {
834 			rval = INTERCONNECT_SAS;
835 		}
836 		break;
837 	case SCSI_CAP_CDB_LEN:
838 		if (set == 0) {
839 			rval = 16;
840 		}
841 		break;
842 	case SCSI_CAP_LUN_RESET:
843 		if (set) {
844 			break;
845 		}
846 		if (xp->dtype == SATA) {
847 			rval = 0;
848 		} else {
849 			rval = 1;
850 		}
851 		break;
852 	default:
853 		rval = -1;
854 		break;
855 	}
856 	mutex_exit(&xp->statlock);
857 	pmcs_prt(ADDR2PMC(ap), PMCS_PRT_DEBUG3, NULL, NULL,
858 	    "%s: cap %s val %d set %d rval %d",
859 	    __func__, cap, val, set, rval);
860 	return (rval);
861 }
862 
863 /*
864  * Returns with statlock held if the xp is found.
865  * Fills in pmcs_cmd_t with values if pmcs_cmd_t pointer non-NULL.
866  */
867 static pmcs_xscsi_t *
868 pmcs_addr2xp(struct scsi_address *ap, uint64_t *lp, pmcs_cmd_t *sp)
869 {
870 	pmcs_xscsi_t *xp;
871 	pmcs_lun_t *lun = (pmcs_lun_t *)
872 	    scsi_device_hba_private_get(scsi_address_device(ap));
873 
874 	if ((lun == NULL) || (lun->target == NULL)) {
875 		return (NULL);
876 	}
877 	xp = lun->target;
878 	mutex_enter(&xp->statlock);
879 
880 	if (xp->dev_gone || (xp->phy == NULL)) {
881 		mutex_exit(&xp->statlock);
882 		return (NULL);
883 	}
884 
885 	if (sp != NULL) {
886 		sp->cmd_target = xp;
887 		sp->cmd_lun = lun;
888 	}
889 	if (lp) {
890 		*lp = lun->lun_num;
891 	}
892 	return (xp);
893 }
894 
895 static int
896 pmcs_scsa_getcap(struct scsi_address *ap, char *cap, int whom)
897 {
898 	int r;
899 	if (cap == NULL) {
900 		return (-1);
901 	}
902 	r = pmcs_cap(ap, cap, 0, whom, 0);
903 	return (r);
904 }
905 
906 static int
907 pmcs_scsa_setcap(struct scsi_address *ap, char *cap, int value, int whom)
908 {
909 	int r;
910 	if (cap == NULL) {
911 		return (-1);
912 	}
913 	r = pmcs_cap(ap, cap, value, whom, 1);
914 	return (r);
915 }
916 
917 static int
918 pmcs_scsa_setup_pkt(struct scsi_pkt *pkt, int (*callback)(caddr_t),
919     caddr_t cbarg)
920 {
921 	_NOTE(ARGUNUSED(callback, cbarg));
922 	pmcs_cmd_t *sp = pkt->pkt_ha_private;
923 
924 	bzero(sp, sizeof (pmcs_cmd_t));
925 	sp->cmd_pkt = pkt;
926 	return (0);
927 }
928 
929 static void
930 pmcs_scsa_teardown_pkt(struct scsi_pkt *pkt)
931 {
932 	pmcs_cmd_t *sp = pkt->pkt_ha_private;
933 	sp->cmd_target = NULL;
934 	sp->cmd_lun = NULL;
935 }
936 
937 static int
938 pmcs_smp_start(struct smp_pkt *smp_pkt)
939 {
940 	struct pmcwork *pwrk;
941 	const uint_t rdoff = SAS_SMP_MAX_PAYLOAD;
942 	uint32_t msg[PMCS_MSG_SIZE], *ptr, htag, status;
943 	uint64_t wwn;
944 	pmcs_hw_t *pwp;
945 	pmcs_phy_t *pptr;
946 	pmcs_xscsi_t *xp;
947 	uint_t reqsz, rspsz, will_retry;
948 	int result;
949 
950 	pwp = smp_pkt->smp_pkt_address->smp_a_hba_tran->smp_tran_hba_private;
951 	bcopy(smp_pkt->smp_pkt_address->smp_a_wwn, &wwn, SAS_WWN_BYTE_SIZE);
952 
953 	pmcs_prt(pwp, PMCS_PRT_DEBUG1, NULL, NULL,
954 	    "%s: starting for wwn 0x%" PRIx64, __func__, wwn);
955 
956 	will_retry = smp_pkt->smp_pkt_will_retry;
957 
958 	(void) pmcs_acquire_scratch(pwp, B_TRUE);
959 	reqsz = smp_pkt->smp_pkt_reqsize;
960 	if (reqsz > SAS_SMP_MAX_PAYLOAD) {
961 		reqsz = SAS_SMP_MAX_PAYLOAD;
962 	}
963 	(void) memcpy(pwp->scratch, smp_pkt->smp_pkt_req, reqsz);
964 
965 	rspsz = smp_pkt->smp_pkt_rspsize;
966 	if (rspsz > SAS_SMP_MAX_PAYLOAD) {
967 		rspsz = SAS_SMP_MAX_PAYLOAD;
968 	}
969 
970 	/*
971 	 * The request size from the SMP driver always includes 4 bytes
972 	 * for the CRC. The PMCS chip, however, doesn't want to see those
973 	 * counts as part of the transfer size.
974 	 */
975 	reqsz -= 4;
976 
977 	pptr = pmcs_find_phy_by_wwn(pwp, wwn);
978 	/* PHY is now locked */
979 	if (pptr == NULL || pptr->dtype != EXPANDER) {
980 		if (pptr) {
981 			pmcs_unlock_phy(pptr);
982 		}
983 		pmcs_release_scratch(pwp);
984 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
985 		    "%s: could not find phy", __func__);
986 		smp_pkt->smp_pkt_reason = ENXIO;
987 		return (DDI_FAILURE);
988 	}
989 
990 	pwrk = pmcs_gwork(pwp, PMCS_TAG_TYPE_WAIT, pptr);
991 	if (pwrk == NULL) {
992 		pmcs_unlock_phy(pptr);
993 		pmcs_release_scratch(pwp);
994 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, NULL,
995 		    "%s: could not get work structure", __func__);
996 		smp_pkt->smp_pkt_reason = will_retry ? EAGAIN : EBUSY;
997 		return (DDI_FAILURE);
998 	}
999 
1000 	pwrk->arg = msg;
1001 	pwrk->dtype = EXPANDER;
1002 	mutex_enter(&pwp->iqp_lock[PMCS_IQ_OTHER]);
1003 	ptr = GET_IQ_ENTRY(pwp, PMCS_IQ_OTHER);
1004 	if (ptr == NULL) {
1005 		pmcs_pwork(pwp, pwrk);
1006 		mutex_exit(&pwp->iqp_lock[PMCS_IQ_OTHER]);
1007 		pmcs_unlock_phy(pptr);
1008 		pmcs_release_scratch(pwp);
1009 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
1010 		    "%s: could not get IQ entry", __func__);
1011 		smp_pkt->smp_pkt_reason = will_retry ? EAGAIN :EBUSY;
1012 		return (DDI_FAILURE);
1013 	}
1014 	msg[0] = LE_32(PMCS_HIPRI(pwp, PMCS_OQ_GENERAL, PMCIN_SMP_REQUEST));
1015 	msg[1] = LE_32(pwrk->htag);
1016 	msg[2] = LE_32(pptr->device_id);
1017 	msg[3] = LE_32(SMP_INDIRECT_RESPONSE | SMP_INDIRECT_REQUEST);
1018 	msg[8] = LE_32(DWORD0(pwp->scratch_dma));
1019 	msg[9] = LE_32(DWORD1(pwp->scratch_dma));
1020 	msg[10] = LE_32(reqsz);
1021 	msg[11] = 0;
1022 	msg[12] = LE_32(DWORD0(pwp->scratch_dma+rdoff));
1023 	msg[13] = LE_32(DWORD1(pwp->scratch_dma+rdoff));
1024 	msg[14] = LE_32(rspsz);
1025 	msg[15] = 0;
1026 
1027 	COPY_MESSAGE(ptr, msg, PMCS_MSG_SIZE);
1028 	pwrk->state = PMCS_WORK_STATE_ONCHIP;
1029 	htag = pwrk->htag;
1030 	INC_IQ_ENTRY(pwp, PMCS_IQ_OTHER);
1031 
1032 	pmcs_unlock_phy(pptr);
1033 	WAIT_FOR(pwrk, smp_pkt->smp_pkt_timeout * 1000, result);
1034 	pmcs_pwork(pwp, pwrk);
1035 	pmcs_lock_phy(pptr);
1036 
1037 	if (result) {
1038 		pmcs_timed_out(pwp, htag, __func__);
1039 		if (pmcs_abort(pwp, pptr, htag, 0, 0)) {
1040 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, pptr, NULL,
1041 			    "%s: Unable to issue SMP ABORT for htag 0x%08x",
1042 			    __func__, htag);
1043 		} else {
1044 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, pptr, NULL,
1045 			    "%s: Issuing SMP ABORT for htag 0x%08x",
1046 			    __func__, htag);
1047 		}
1048 		pmcs_unlock_phy(pptr);
1049 		pmcs_release_scratch(pwp);
1050 		smp_pkt->smp_pkt_reason = ETIMEDOUT;
1051 		return (DDI_FAILURE);
1052 	}
1053 	status = LE_32(msg[2]);
1054 	if (status == PMCOUT_STATUS_OVERFLOW) {
1055 		status = PMCOUT_STATUS_OK;
1056 		smp_pkt->smp_pkt_reason = EOVERFLOW;
1057 	}
1058 	if (status != PMCOUT_STATUS_OK) {
1059 		const char *emsg = pmcs_status_str(status);
1060 		if (emsg == NULL) {
1061 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL,
1062 			    "SMP operation failed (0x%x)", status);
1063 		} else {
1064 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL,
1065 			    "SMP operation failed (%s)", emsg);
1066 		}
1067 
1068 		if ((status == PMCOUT_STATUS_ERROR_HW_TIMEOUT) ||
1069 		    (status == PMCOUT_STATUS_IO_XFER_OPEN_RETRY_TIMEOUT)) {
1070 			smp_pkt->smp_pkt_reason =
1071 			    will_retry ? EAGAIN : ETIMEDOUT;
1072 			result = DDI_FAILURE;
1073 		} else if (status ==
1074 		    PMCOUT_STATUS_OPEN_CNX_ERROR_IT_NEXUS_LOSS) {
1075 			xp = pptr->target;
1076 			if (xp == NULL) {
1077 				smp_pkt->smp_pkt_reason = EIO;
1078 				result = DDI_FAILURE;
1079 				goto out;
1080 			}
1081 			if (xp->dev_state !=
1082 			    PMCS_DEVICE_STATE_NON_OPERATIONAL) {
1083 				xp->dev_state =
1084 				    PMCS_DEVICE_STATE_NON_OPERATIONAL;
1085 				pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, xp,
1086 				    "%s: Got _IT_NEXUS_LOSS SMP status. "
1087 				    "Tgt(0x%p) dev_state set to "
1088 				    "_NON_OPERATIONAL", __func__,
1089 				    (void *)xp);
1090 			}
1091 			/* ABORT any pending commands related to this device */
1092 			if (pmcs_abort(pwp, pptr, pptr->device_id, 1, 1) != 0) {
1093 				pptr->abort_pending = 1;
1094 				smp_pkt->smp_pkt_reason = EIO;
1095 				result = DDI_FAILURE;
1096 			}
1097 		} else {
1098 			smp_pkt->smp_pkt_reason = will_retry ? EAGAIN : EIO;
1099 			result = DDI_FAILURE;
1100 		}
1101 	} else {
1102 		(void) memcpy(smp_pkt->smp_pkt_rsp,
1103 		    &((uint8_t *)pwp->scratch)[rdoff], rspsz);
1104 		if (smp_pkt->smp_pkt_reason == EOVERFLOW) {
1105 			result = DDI_FAILURE;
1106 		} else {
1107 			result = DDI_SUCCESS;
1108 		}
1109 	}
1110 out:
1111 	pmcs_unlock_phy(pptr);
1112 	pmcs_release_scratch(pwp);
1113 	return (result);
1114 }
1115 
1116 static int
1117 pmcs_smp_init(dev_info_t *self, dev_info_t *child,
1118     smp_hba_tran_t *tran, smp_device_t *smp_sd)
1119 {
1120 	_NOTE(ARGUNUSED(tran, smp_sd));
1121 	pmcs_iport_t *iport;
1122 	pmcs_hw_t *pwp;
1123 	pmcs_xscsi_t *tgt;
1124 	pmcs_phy_t *phy, *pphy;
1125 	uint64_t wwn;
1126 	char *addr, *tgt_port;
1127 	int ua_form = 1;
1128 
1129 	iport = ddi_get_soft_state(pmcs_iport_softstate,
1130 	    ddi_get_instance(self));
1131 	ASSERT(iport);
1132 	if (iport == NULL)
1133 		return (DDI_FAILURE);
1134 	pwp = iport->pwp;
1135 	ASSERT(pwp);
1136 	if (pwp == NULL)
1137 		return (DDI_FAILURE);
1138 	pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL, "%s: %s", __func__,
1139 	    ddi_get_name(child));
1140 
1141 	/* Get "target-port" prop from devinfo node */
1142 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1143 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1144 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_SUCCESS) {
1145 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s: Failed to "
1146 		    "lookup prop ("SCSI_ADDR_PROP_TARGET_PORT")", __func__);
1147 		/* Dont fail _smp_init() because we couldnt get/set a prop */
1148 		return (DDI_SUCCESS);
1149 	}
1150 
1151 	/*
1152 	 * Validate that this tran_tgt_init is for an active iport.
1153 	 */
1154 	if (iport->ua_state == UA_INACTIVE) {
1155 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
1156 		    "%s: Init on inactive iport for '%s'", __func__, tgt_port);
1157 		ddi_prop_free(tgt_port);
1158 		return (DDI_FAILURE);
1159 	}
1160 
1161 	mutex_enter(&pwp->lock);
1162 
1163 	/* Retrieve softstate using unit-address */
1164 	tgt = pmcs_get_target(iport, tgt_port);
1165 	if (tgt == NULL) {
1166 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
1167 		    "%s: tgt softstate not found", __func__);
1168 		ddi_prop_free(tgt_port);
1169 		mutex_exit(&pwp->lock);
1170 		return (DDI_FAILURE);
1171 	}
1172 
1173 	phy = tgt->phy;
1174 	ASSERT(mutex_owned(&phy->phy_lock));
1175 
1176 	if (IS_ROOT_PHY(phy)) {
1177 		/* Expander attached to HBA - don't ref_count it */
1178 		wwn = pwp->sas_wwns[0];
1179 	} else {
1180 		pmcs_inc_phy_ref_count(phy);
1181 
1182 		/*
1183 		 * Parent (in topology) is also an expander
1184 		 * Now that we've increased the ref count on phy, it's OK
1185 		 * to drop the lock so we can acquire the parent's lock.
1186 		 */
1187 
1188 		pphy = phy->parent;
1189 		pmcs_unlock_phy(phy);
1190 		pmcs_lock_phy(pphy);
1191 		wwn = pmcs_barray2wwn(pphy->sas_address);
1192 		pmcs_unlock_phy(pphy);
1193 		pmcs_lock_phy(phy);
1194 	}
1195 
1196 	/*
1197 	 * If this is the 1st smp_init, add this to our list.
1198 	 */
1199 	if (tgt->target_num == PMCS_INVALID_TARGET_NUM) {
1200 		int target;
1201 		for (target = 0; target < pwp->max_dev; target++) {
1202 			if (pwp->targets[target] != NULL) {
1203 				continue;
1204 			}
1205 
1206 			pwp->targets[target] = tgt;
1207 			tgt->target_num = (uint16_t)target;
1208 			tgt->assigned = 1;
1209 			tgt->dev_state = PMCS_DEVICE_STATE_OPERATIONAL;
1210 			break;
1211 		}
1212 
1213 		if (target == pwp->max_dev) {
1214 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL,
1215 			    "Target list full.");
1216 			goto smp_init_fail;
1217 		}
1218 	}
1219 
1220 	if (!pmcs_assign_device(pwp, tgt)) {
1221 		pwp->targets[tgt->target_num] = NULL;
1222 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, tgt,
1223 		    "%s: pmcs_assign_device failed for target 0x%p",
1224 		    __func__, (void *)tgt);
1225 		goto smp_init_fail;
1226 	}
1227 
1228 	pmcs_unlock_phy(phy);
1229 	mutex_exit(&pwp->lock);
1230 
1231 	tgt->ref_count++;
1232 	tgt->dtype = phy->dtype;
1233 
1234 	addr = scsi_wwn_to_wwnstr(wwn, ua_form, NULL);
1235 	/* XXX: Update smp devinfo node using ndi_xxx */
1236 	if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
1237 	    SCSI_ADDR_PROP_ATTACHED_PORT, addr) != DDI_SUCCESS) {
1238 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s: Failed to set "
1239 		    "prop ("SCSI_ADDR_PROP_ATTACHED_PORT")", __func__);
1240 	}
1241 	(void) scsi_free_wwnstr(addr);
1242 	ddi_prop_free(tgt_port);
1243 	return (DDI_SUCCESS);
1244 
1245 smp_init_fail:
1246 	tgt->phy = NULL;
1247 	tgt->target_num = PMCS_INVALID_TARGET_NUM;
1248 	phy->target = NULL;
1249 	if (!IS_ROOT_PHY(phy)) {
1250 		pmcs_dec_phy_ref_count(phy);
1251 	}
1252 	pmcs_unlock_phy(phy);
1253 	mutex_exit(&pwp->lock);
1254 	ddi_soft_state_bystr_free(iport->tgt_sstate, tgt->unit_address);
1255 	ddi_prop_free(tgt_port);
1256 	return (DDI_FAILURE);
1257 }
1258 
1259 static void
1260 pmcs_smp_free(dev_info_t *self, dev_info_t *child,
1261     smp_hba_tran_t *tran, smp_device_t *smp)
1262 {
1263 	_NOTE(ARGUNUSED(tran, smp));
1264 	pmcs_iport_t *iport;
1265 	pmcs_hw_t *pwp;
1266 	pmcs_xscsi_t *tgt;
1267 	char *tgt_port;
1268 
1269 	iport = ddi_get_soft_state(pmcs_iport_softstate,
1270 	    ddi_get_instance(self));
1271 	ASSERT(iport);
1272 	if (iport == NULL)
1273 		return;
1274 
1275 	pwp = iport->pwp;
1276 	if (pwp == NULL)
1277 		return;
1278 	ASSERT(pwp);
1279 	pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL, "%s: %s", __func__,
1280 	    ddi_get_name(child));
1281 
1282 	/* Get "target-port" prop from devinfo node */
1283 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1284 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1285 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_SUCCESS) {
1286 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s: Failed to "
1287 		    "lookup prop ("SCSI_ADDR_PROP_TARGET_PORT")", __func__);
1288 		return;
1289 	}
1290 	/* Retrieve softstate using unit-address */
1291 	tgt = ddi_soft_state_bystr_get(iport->tgt_sstate, tgt_port);
1292 	ddi_prop_free(tgt_port);
1293 
1294 	if (tgt == NULL) {
1295 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
1296 		    "%s: tgt softstate not found", __func__);
1297 		return;
1298 	}
1299 
1300 	mutex_enter(&pwp->lock);
1301 	mutex_enter(&tgt->statlock);
1302 	if (tgt->phy) {
1303 		if (!IS_ROOT_PHY(tgt->phy)) {
1304 			pmcs_dec_phy_ref_count(tgt->phy);
1305 		}
1306 	}
1307 
1308 	if (--tgt->ref_count == 0) {
1309 		/*
1310 		 * Remove this target from our list. The softstate
1311 		 * will remain, and the device will remain registered
1312 		 * with the hardware unless/until we're told that the
1313 		 * device physically went away.
1314 		 */
1315 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, tgt,
1316 		    "Removing target 0x%p (vtgt %d) from target list",
1317 		    (void *)tgt, tgt->target_num);
1318 		pwp->targets[tgt->target_num] = NULL;
1319 		tgt->target_num = PMCS_INVALID_TARGET_NUM;
1320 		tgt->phy->target = NULL;
1321 		tgt->phy = NULL;
1322 	}
1323 
1324 	mutex_exit(&tgt->statlock);
1325 	mutex_exit(&pwp->lock);
1326 }
1327 
1328 static int
1329 pmcs_scsi_quiesce(dev_info_t *dip)
1330 {
1331 	pmcs_hw_t *pwp;
1332 	int totactive = -1;
1333 	pmcs_xscsi_t *xp;
1334 	uint16_t target;
1335 
1336 	if (ddi_get_soft_state(pmcs_iport_softstate, ddi_get_instance(dip)))
1337 		return (0);		/* iport */
1338 
1339 	pwp  = ddi_get_soft_state(pmcs_softc_state, ddi_get_instance(dip));
1340 	if (pwp == NULL) {
1341 		return (-1);
1342 	}
1343 	mutex_enter(&pwp->lock);
1344 	if (pwp->state != STATE_RUNNING) {
1345 		mutex_exit(&pwp->lock);
1346 		return (-1);
1347 	}
1348 
1349 	pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s called", __func__);
1350 	pwp->blocked = 1;
1351 	while (totactive) {
1352 		totactive = 0;
1353 		for (target = 0; target < pwp->max_dev; target++) {
1354 			xp = pwp->targets[target];
1355 			if (xp == NULL) {
1356 				continue;
1357 			}
1358 			mutex_enter(&xp->statlock);
1359 			if (xp->actv_cnt) {
1360 				totactive += xp->actv_cnt;
1361 				xp->draining = 1;
1362 			}
1363 			mutex_exit(&xp->statlock);
1364 		}
1365 		if (totactive) {
1366 			cv_wait(&pwp->drain_cv, &pwp->lock);
1367 		}
1368 		/*
1369 		 * The pwp->blocked may have been reset. e.g a SCSI bus reset
1370 		 */
1371 		pwp->blocked = 1;
1372 	}
1373 
1374 	for (target = 0; target < pwp->max_dev; target++) {
1375 		xp = pwp->targets[target];
1376 		if (xp == NULL) {
1377 			continue;
1378 		}
1379 		mutex_enter(&xp->statlock);
1380 		xp->draining = 0;
1381 		mutex_exit(&xp->statlock);
1382 	}
1383 
1384 	mutex_exit(&pwp->lock);
1385 	if (totactive == 0) {
1386 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
1387 		    "%s drain complete", __func__);
1388 	}
1389 	return (0);
1390 }
1391 
1392 static int
1393 pmcs_scsi_unquiesce(dev_info_t *dip)
1394 {
1395 	pmcs_hw_t *pwp;
1396 
1397 	if (ddi_get_soft_state(pmcs_iport_softstate, ddi_get_instance(dip)))
1398 		return (0);		/* iport */
1399 
1400 	pwp  = ddi_get_soft_state(pmcs_softc_state, ddi_get_instance(dip));
1401 	if (pwp == NULL) {
1402 		return (-1);
1403 	}
1404 	mutex_enter(&pwp->lock);
1405 	if (pwp->state != STATE_RUNNING) {
1406 		mutex_exit(&pwp->lock);
1407 		return (-1);
1408 	}
1409 	pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s called", __func__);
1410 	pwp->blocked = 0;
1411 	mutex_exit(&pwp->lock);
1412 
1413 	/*
1414 	 * Run all pending commands.
1415 	 */
1416 	pmcs_scsa_wq_run(pwp);
1417 
1418 	/*
1419 	 * Complete all completed commands.
1420 	 * This also unlocks us.
1421 	 */
1422 	PMCS_CQ_RUN(pwp);
1423 	return (0);
1424 }
1425 
1426 /*
1427  * Start commands for a particular device
1428  * If the actual start of a command fails, return B_FALSE.  Any other result
1429  * is a B_TRUE return.
1430  */
1431 boolean_t
1432 pmcs_scsa_wq_run_one(pmcs_hw_t *pwp, pmcs_xscsi_t *xp)
1433 {
1434 	pmcs_cmd_t *sp;
1435 	pmcs_phy_t *phyp;
1436 	pmcwork_t *pwrk;
1437 	boolean_t run_one, blocked;
1438 	int rval;
1439 
1440 	/*
1441 	 * First, check to see if we're blocked or resource limited
1442 	 */
1443 	mutex_enter(&pwp->lock);
1444 	blocked = pwp->blocked;
1445 	/*
1446 	 * If resource_limited is set, we're resource constrained and
1447 	 * we will run only one work request for this target.
1448 	 */
1449 	run_one = pwp->resource_limited;
1450 	mutex_exit(&pwp->lock);
1451 
1452 	if (blocked) {
1453 		/* Queues will get restarted when we get unblocked */
1454 		return (B_TRUE);
1455 	}
1456 
1457 	/*
1458 	 * Might as well verify the queue is not empty before moving on
1459 	 */
1460 	mutex_enter(&xp->wqlock);
1461 	if (STAILQ_EMPTY(&xp->wq)) {
1462 		mutex_exit(&xp->wqlock);
1463 		return (B_TRUE);
1464 	}
1465 	mutex_exit(&xp->wqlock);
1466 
1467 	/*
1468 	 * If we're draining or resetting, just reschedule work queue and bail.
1469 	 */
1470 	mutex_enter(&xp->statlock);
1471 	if (xp->draining || xp->resetting || xp->special_running ||
1472 	    xp->special_needed) {
1473 		mutex_exit(&xp->statlock);
1474 		SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
1475 		return (B_TRUE);
1476 	}
1477 
1478 	/*
1479 	 * Next, check to see if the target is gone.
1480 	 */
1481 	if (xp->dev_gone) {
1482 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
1483 		    "%s: Flushing wait queue for dead tgt 0x%p", __func__,
1484 		    (void *)xp);
1485 		pmcs_flush_target_queues(pwp, xp, PMCS_TGT_WAIT_QUEUE);
1486 		mutex_exit(&xp->statlock);
1487 		return (B_TRUE);
1488 	}
1489 
1490 	/*
1491 	 * Increment the PHY's ref_count now so we know it won't go away
1492 	 * after we drop the target lock.  Drop it before returning.  If the
1493 	 * PHY dies, the commands we attempt to send will fail, but at least
1494 	 * we know we have a real PHY pointer.
1495 	 */
1496 	phyp = xp->phy;
1497 	pmcs_inc_phy_ref_count(phyp);
1498 	mutex_exit(&xp->statlock);
1499 
1500 	mutex_enter(&xp->wqlock);
1501 	while ((sp = STAILQ_FIRST(&xp->wq)) != NULL) {
1502 		pwrk = pmcs_gwork(pwp, PMCS_TAG_TYPE_CBACK, phyp);
1503 		if (pwrk == NULL) {
1504 			pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
1505 			    "%s: out of work structures", __func__);
1506 			SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
1507 			break;
1508 		}
1509 		STAILQ_REMOVE_HEAD(&xp->wq, cmd_next);
1510 		mutex_exit(&xp->wqlock);
1511 
1512 		pwrk->xp = xp;
1513 		pwrk->arg = sp;
1514 		sp->cmd_tag = pwrk->htag;
1515 		pwrk->timer = US2WT(CMD2PKT(sp)->pkt_time * 1000000);
1516 		if (pwrk->timer == 0) {
1517 			pwrk->timer = US2WT(1000000);
1518 		}
1519 
1520 		pwrk->dtype = xp->dtype;
1521 
1522 		if (xp->dtype == SAS) {
1523 			pwrk->ptr = (void *) pmcs_SAS_done;
1524 			if ((rval = pmcs_SAS_run(sp, pwrk)) != 0) {
1525 				sp->cmd_tag = NULL;
1526 				pmcs_dec_phy_ref_count(phyp);
1527 				pmcs_pwork(pwp, pwrk);
1528 				SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
1529 				if (rval == PMCS_WQ_RUN_FAIL_RES) {
1530 					return (B_FALSE);
1531 				} else {
1532 					return (B_TRUE);
1533 				}
1534 			}
1535 		} else {
1536 			ASSERT(xp->dtype == SATA);
1537 			pwrk->ptr = (void *) pmcs_SATA_done;
1538 			if ((rval = pmcs_SATA_run(sp, pwrk)) != 0) {
1539 				sp->cmd_tag = NULL;
1540 				pmcs_dec_phy_ref_count(phyp);
1541 				pmcs_pwork(pwp, pwrk);
1542 				SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
1543 				if (rval == PMCS_WQ_RUN_FAIL_RES) {
1544 					return (B_FALSE);
1545 				} else {
1546 					return (B_TRUE);
1547 				}
1548 			}
1549 		}
1550 
1551 		if (run_one) {
1552 			goto wq_out;
1553 		}
1554 		mutex_enter(&xp->wqlock);
1555 	}
1556 
1557 	mutex_exit(&xp->wqlock);
1558 
1559 wq_out:
1560 	pmcs_dec_phy_ref_count(phyp);
1561 	return (B_TRUE);
1562 }
1563 
1564 /*
1565  * Start commands for all devices.
1566  */
1567 void
1568 pmcs_scsa_wq_run(pmcs_hw_t *pwp)
1569 {
1570 	pmcs_xscsi_t *xp;
1571 	uint16_t target_start, target;
1572 	boolean_t	rval = B_TRUE;
1573 
1574 	mutex_enter(&pwp->lock);
1575 	target_start = pwp->last_wq_dev;
1576 	target = target_start;
1577 
1578 	do {
1579 		xp = pwp->targets[target];
1580 		if (xp == NULL) {
1581 			if (++target == pwp->max_dev) {
1582 				target = 0;
1583 			}
1584 			continue;
1585 		}
1586 
1587 		mutex_exit(&pwp->lock);
1588 		rval = pmcs_scsa_wq_run_one(pwp, xp);
1589 		if (rval == B_FALSE) {
1590 			mutex_enter(&pwp->lock);
1591 			break;
1592 		}
1593 		mutex_enter(&pwp->lock);
1594 		if (++target == pwp->max_dev) {
1595 			target = 0;
1596 		}
1597 	} while (target != target_start);
1598 
1599 	if (rval) {
1600 		pwp->resource_limited = 0; /* Not resource-constrained */
1601 	} else {
1602 		pwp->resource_limited = 1; /* Give others a chance */
1603 	}
1604 
1605 	pwp->last_wq_dev = target;
1606 	mutex_exit(&pwp->lock);
1607 }
1608 
1609 /*
1610  * Pull the completion queue, drop the lock and complete all elements.
1611  */
1612 
1613 void
1614 pmcs_scsa_cq_run(void *arg)
1615 {
1616 	pmcs_cq_thr_info_t *cqti = (pmcs_cq_thr_info_t *)arg;
1617 	pmcs_hw_t *pwp = cqti->cq_pwp;
1618 	pmcs_cmd_t *sp, *nxt;
1619 	struct scsi_pkt *pkt;
1620 	pmcs_iocomp_cb_t *ioccb, *ioccb_next;
1621 	pmcs_cb_t callback;
1622 	uint32_t niodone;
1623 
1624 	DTRACE_PROBE1(pmcs__scsa__cq__run__start, pmcs_cq_thr_info_t *, cqti);
1625 
1626 	mutex_enter(&pwp->cq_lock);
1627 
1628 	while (!pwp->cq_info.cq_stop) {
1629 		/*
1630 		 * First, check the I/O completion callback queue.
1631 		 */
1632 
1633 		ioccb = pwp->iocomp_cb_head;
1634 		pwp->iocomp_cb_head = NULL;
1635 		pwp->iocomp_cb_tail = NULL;
1636 		mutex_exit(&pwp->cq_lock);
1637 
1638 		niodone = 0;
1639 
1640 		while (ioccb) {
1641 			niodone++;
1642 			/*
1643 			 * Grab the lock on the work structure. The callback
1644 			 * routine is responsible for clearing it.
1645 			 */
1646 			mutex_enter(&ioccb->pwrk->lock);
1647 			ioccb_next = ioccb->next;
1648 			callback = (pmcs_cb_t)ioccb->pwrk->ptr;
1649 			(*callback)(pwp, ioccb->pwrk,
1650 			    (uint32_t *)((void *)ioccb->iomb));
1651 			kmem_cache_free(pwp->iocomp_cb_cache, ioccb);
1652 			ioccb = ioccb_next;
1653 		}
1654 
1655 		/*
1656 		 * Next, run the completion queue
1657 		 */
1658 
1659 		mutex_enter(&pwp->cq_lock);
1660 		sp = STAILQ_FIRST(&pwp->cq);
1661 		STAILQ_INIT(&pwp->cq);
1662 		mutex_exit(&pwp->cq_lock);
1663 
1664 		DTRACE_PROBE1(pmcs__scsa__cq__run__start__loop,
1665 		    pmcs_cq_thr_info_t *, cqti);
1666 
1667 		if (sp && pmcs_check_acc_dma_handle(pwp)) {
1668 			ddi_fm_service_impact(pwp->dip, DDI_SERVICE_UNAFFECTED);
1669 		}
1670 
1671 		while (sp) {
1672 			nxt = STAILQ_NEXT(sp, cmd_next);
1673 			pkt = CMD2PKT(sp);
1674 			pmcs_prt(pwp, PMCS_PRT_DEBUG3, NULL, sp->cmd_target,
1675 			    "%s: calling completion on %p for tgt %p", __func__,
1676 			    (void *)sp, (void *)sp->cmd_target);
1677 			scsi_hba_pkt_comp(pkt);
1678 			sp = nxt;
1679 		}
1680 
1681 		DTRACE_PROBE1(pmcs__scsa__cq__run__end__loop,
1682 		    pmcs_cq_thr_info_t *, cqti);
1683 
1684 		mutex_enter(&cqti->cq_thr_lock);
1685 		cv_wait(&cqti->cq_cv, &cqti->cq_thr_lock);
1686 		mutex_exit(&cqti->cq_thr_lock);
1687 
1688 		mutex_enter(&pwp->cq_lock);
1689 	}
1690 
1691 	mutex_exit(&pwp->cq_lock);
1692 	DTRACE_PROBE1(pmcs__scsa__cq__run__stop, pmcs_cq_thr_info_t *, cqti);
1693 	thread_exit();
1694 }
1695 
1696 /*
1697  * Run a SAS command.  Called with pwrk->lock held, returns unlocked.
1698  */
1699 static int
1700 pmcs_SAS_run(pmcs_cmd_t *sp, pmcwork_t *pwrk)
1701 {
1702 	pmcs_hw_t *pwp = CMD2PMC(sp);
1703 	struct scsi_pkt *pkt = CMD2PKT(sp);
1704 	pmcs_xscsi_t *xp = pwrk->xp;
1705 	uint32_t iq, *ptr;
1706 	sas_ssp_cmd_iu_t sc;
1707 
1708 	mutex_enter(&xp->statlock);
1709 	if (!xp->assigned) {
1710 		mutex_exit(&xp->statlock);
1711 		return (PMCS_WQ_RUN_FAIL_OTHER);
1712 	}
1713 	if ((xp->actv_cnt >= xp->qdepth) || xp->recover_wait) {
1714 		mutex_exit(&xp->statlock);
1715 		mutex_enter(&xp->wqlock);
1716 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
1717 		mutex_exit(&xp->wqlock);
1718 		return (PMCS_WQ_RUN_FAIL_OTHER);
1719 	}
1720 	GET_IO_IQ_ENTRY(pwp, ptr, pwrk->phy->device_id, iq);
1721 	if (ptr == NULL) {
1722 		mutex_exit(&xp->statlock);
1723 		/*
1724 		 * This is a temporary failure not likely to unblocked by
1725 		 * commands completing as the test for scheduling the
1726 		 * restart of work is a per-device test.
1727 		 */
1728 		mutex_enter(&xp->wqlock);
1729 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
1730 		mutex_exit(&xp->wqlock);
1731 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
1732 		    "%s: Failed to get IO IQ entry for tgt %d",
1733 		    __func__, xp->target_num);
1734 		return (PMCS_WQ_RUN_FAIL_RES);
1735 
1736 	}
1737 
1738 	ptr[0] =
1739 	    LE_32(PMCS_IOMB_IN_SAS(PMCS_OQ_IODONE, PMCIN_SSP_INI_IO_START));
1740 	ptr[1] = LE_32(pwrk->htag);
1741 	ptr[2] = LE_32(pwrk->phy->device_id);
1742 	ptr[3] = LE_32(pkt->pkt_dma_len);
1743 	if (ptr[3]) {
1744 		ASSERT(pkt->pkt_numcookies);
1745 		if (pkt->pkt_dma_flags & DDI_DMA_READ) {
1746 			ptr[4] = LE_32(PMCIN_DATADIR_2_INI);
1747 		} else {
1748 			ptr[4] = LE_32(PMCIN_DATADIR_2_DEV);
1749 		}
1750 		if (pmcs_dma_load(pwp, sp, ptr)) {
1751 			mutex_exit(&pwp->iqp_lock[iq]);
1752 			mutex_exit(&xp->statlock);
1753 			mutex_enter(&xp->wqlock);
1754 			if (STAILQ_EMPTY(&xp->wq)) {
1755 				STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
1756 				mutex_exit(&xp->wqlock);
1757 			} else {
1758 				mutex_exit(&xp->wqlock);
1759 				CMD2PKT(sp)->pkt_scbp[0] = STATUS_QFULL;
1760 				CMD2PKT(sp)->pkt_reason = CMD_CMPLT;
1761 				CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS |
1762 				    STATE_GOT_TARGET | STATE_SENT_CMD |
1763 				    STATE_GOT_STATUS;
1764 				mutex_enter(&pwp->cq_lock);
1765 				STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
1766 				mutex_exit(&pwp->cq_lock);
1767 				pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
1768 				    "%s: Failed to dma_load for tgt %d (QF)",
1769 				    __func__, xp->target_num);
1770 			}
1771 			return (PMCS_WQ_RUN_FAIL_RES);
1772 		}
1773 	} else {
1774 		ptr[4] = LE_32(PMCIN_DATADIR_NONE);
1775 		CLEAN_MESSAGE(ptr, 12);
1776 	}
1777 	xp->actv_cnt++;
1778 	if (xp->actv_cnt > xp->maxdepth) {
1779 		xp->maxdepth = xp->actv_cnt;
1780 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, pwrk->phy, xp, "%s: max depth "
1781 		    "now %u", pwrk->phy->path, xp->maxdepth);
1782 	}
1783 	mutex_exit(&xp->statlock);
1784 
1785 
1786 #ifdef	DEBUG
1787 	/*
1788 	 * Generate a PMCOUT_STATUS_XFER_CMD_FRAME_ISSUED
1789 	 * event when this goes out on the wire.
1790 	 */
1791 	ptr[4] |= PMCIN_MESSAGE_REPORT;
1792 #endif
1793 	/*
1794 	 * Fill in the SSP IU
1795 	 */
1796 
1797 	bzero(&sc, sizeof (sas_ssp_cmd_iu_t));
1798 	bcopy((uint8_t *)&sp->cmd_lun->scsi_lun, sc.lun, sizeof (scsi_lun_t));
1799 
1800 	switch (pkt->pkt_flags & FLAG_TAGMASK) {
1801 	case FLAG_HTAG:
1802 		sc.task_attribute = SAS_CMD_TASK_ATTR_HEAD;
1803 		break;
1804 	case FLAG_OTAG:
1805 		sc.task_attribute = SAS_CMD_TASK_ATTR_ORDERED;
1806 		break;
1807 	case FLAG_STAG:
1808 	default:
1809 		sc.task_attribute = SAS_CMD_TASK_ATTR_SIMPLE;
1810 		break;
1811 	}
1812 	(void) memcpy(sc.cdb, pkt->pkt_cdbp,
1813 	    min(SCSA_CDBLEN(sp), sizeof (sc.cdb)));
1814 	(void) memcpy(&ptr[5], &sc, sizeof (sas_ssp_cmd_iu_t));
1815 	pwrk->state = PMCS_WORK_STATE_ONCHIP;
1816 	mutex_exit(&pwrk->lock);
1817 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL,
1818 	    "%s: giving pkt %p (tag %x) to the hardware", __func__,
1819 	    (void *)pkt, pwrk->htag);
1820 #ifdef DEBUG
1821 	pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "SAS INI Message", ptr);
1822 #endif
1823 	mutex_enter(&xp->aqlock);
1824 	STAILQ_INSERT_TAIL(&xp->aq, sp, cmd_next);
1825 	mutex_exit(&xp->aqlock);
1826 	INC_IQ_ENTRY(pwp, iq);
1827 
1828 	/*
1829 	 * If we just submitted the last command queued from device state
1830 	 * recovery, clear the wq_recovery_tail pointer.
1831 	 */
1832 	mutex_enter(&xp->wqlock);
1833 	if (xp->wq_recovery_tail == sp) {
1834 		xp->wq_recovery_tail = NULL;
1835 	}
1836 	mutex_exit(&xp->wqlock);
1837 
1838 	return (PMCS_WQ_RUN_SUCCESS);
1839 }
1840 
1841 /*
1842  * Complete a SAS command
1843  *
1844  * Called with pwrk lock held.
1845  * The free of pwrk releases the lock.
1846  */
1847 
1848 static void
1849 pmcs_SAS_done(pmcs_hw_t *pwp, pmcwork_t *pwrk, uint32_t *msg)
1850 {
1851 	pmcs_cmd_t *sp = pwrk->arg;
1852 	pmcs_phy_t *pptr = pwrk->phy;
1853 	pmcs_xscsi_t *xp = pwrk->xp;
1854 	struct scsi_pkt *pkt = CMD2PKT(sp);
1855 	int dead;
1856 	uint32_t sts;
1857 	boolean_t aborted = B_FALSE;
1858 	boolean_t do_ds_recovery = B_FALSE;
1859 
1860 	ASSERT(xp != NULL);
1861 	ASSERT(sp != NULL);
1862 	ASSERT(pptr != NULL);
1863 
1864 	DTRACE_PROBE4(pmcs__io__done, uint64_t, pkt->pkt_dma_len, int,
1865 	    (pkt->pkt_dma_flags & DDI_DMA_READ) != 0, hrtime_t, pwrk->start,
1866 	    hrtime_t, gethrtime());
1867 
1868 	dead = pwrk->dead;
1869 
1870 	if (msg) {
1871 		sts = LE_32(msg[2]);
1872 	} else {
1873 		sts = 0;
1874 	}
1875 
1876 	if (dead != 0) {
1877 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, "%s: dead cmd tag "
1878 		    "0x%x for %s", __func__, pwrk->htag, pptr->path);
1879 		goto out;
1880 	}
1881 
1882 	if (sts == PMCOUT_STATUS_ABORTED) {
1883 		aborted = B_TRUE;
1884 	}
1885 
1886 	if (pwrk->state == PMCS_WORK_STATE_TIMED_OUT) {
1887 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
1888 		    "%s: cmd 0x%p (tag 0x%x) timed out for %s",
1889 		    __func__, (void *)sp, pwrk->htag, pptr->path);
1890 		goto out;
1891 	}
1892 
1893 	/*
1894 	 * If the status isn't okay but not underflow,
1895 	 * step to the side and parse the (possible) error.
1896 	 */
1897 #ifdef DEBUG
1898 	if (msg) {
1899 		pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "Outbound Message", msg);
1900 	}
1901 #endif
1902 	if (!msg) {
1903 		goto out;
1904 	}
1905 
1906 	switch (sts) {
1907 	case PMCOUT_STATUS_OPEN_CNX_ERROR_IT_NEXUS_LOSS:
1908 	case PMCOUT_STATUS_IO_DS_NON_OPERATIONAL:
1909 	case PMCOUT_STATUS_IO_DS_IN_RECOVERY:
1910 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
1911 		    "%s: PHY %s requires device state recovery (status=%d)",
1912 		    __func__, pptr->path, sts);
1913 		do_ds_recovery = B_TRUE;
1914 		break;
1915 	case PMCOUT_STATUS_UNDERFLOW:
1916 		(void) pmcs_set_resid(pkt, pkt->pkt_dma_len, LE_32(msg[3]));
1917 		pmcs_prt(pwp, PMCS_PRT_DEBUG_UNDERFLOW, NULL, NULL,
1918 		    "%s: underflow %u for cdb 0x%x",
1919 		    __func__, LE_32(msg[3]), pkt->pkt_cdbp[0] & 0xff);
1920 		sts = PMCOUT_STATUS_OK;
1921 		msg[3] = 0;
1922 		break;
1923 	case PMCOUT_STATUS_OK:
1924 		pkt->pkt_resid = 0;
1925 		break;
1926 	}
1927 
1928 	if (sts != PMCOUT_STATUS_OK) {
1929 		pmcs_ioerror(pwp, SAS, pwrk, msg);
1930 	} else {
1931 		if (msg[3]) {
1932 			uint8_t local[PMCS_QENTRY_SIZE << 1], *xd;
1933 			sas_ssp_rsp_iu_t *rptr = (void *)local;
1934 			const int lim =
1935 			    (PMCS_QENTRY_SIZE << 1) - SAS_RSP_HDR_SIZE;
1936 			static const uint8_t ssp_rsp_evec[] = {
1937 				0x58, 0x61, 0x56, 0x72, 0x00
1938 			};
1939 
1940 			/*
1941 			 * Transform the the first part of the response
1942 			 * to host canonical form. This gives us enough
1943 			 * information to figure out what to do with the
1944 			 * rest (which remains unchanged in the incoming
1945 			 * message which can be up to two queue entries
1946 			 * in length).
1947 			 */
1948 			pmcs_endian_transform(pwp, local, &msg[5],
1949 			    ssp_rsp_evec);
1950 			xd = (uint8_t *)(&msg[5]);
1951 			xd += SAS_RSP_HDR_SIZE;
1952 
1953 			if (rptr->datapres == SAS_RSP_DATAPRES_RESPONSE_DATA) {
1954 				if (rptr->response_data_length != 4) {
1955 					pmcs_print_entry(pwp, PMCS_PRT_DEBUG,
1956 					    "Bad SAS RESPONSE DATA LENGTH",
1957 					    msg);
1958 					pkt->pkt_reason = CMD_TRAN_ERR;
1959 					goto out;
1960 				}
1961 				(void) memcpy(&sts, xd, sizeof (uint32_t));
1962 				sts = BE_32(sts);
1963 				/*
1964 				 * The only response code we should legally get
1965 				 * here is an INVALID FRAME response code.
1966 				 */
1967 				if (sts == SAS_RSP_INVALID_FRAME) {
1968 					pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
1969 					    "%s: pkt %p tgt %u path %s "
1970 					    "completed: INVALID FRAME response",
1971 					    __func__, (void *)pkt,
1972 					    xp->target_num, pptr->path);
1973 				} else {
1974 					pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
1975 					    "%s: pkt %p tgt %u path %s "
1976 					    "completed: illegal response 0x%x",
1977 					    __func__, (void *)pkt,
1978 					    xp->target_num, pptr->path, sts);
1979 				}
1980 				pkt->pkt_reason = CMD_TRAN_ERR;
1981 				goto out;
1982 			}
1983 			if (rptr->datapres == SAS_RSP_DATAPRES_SENSE_DATA) {
1984 				uint32_t slen;
1985 				slen = rptr->sense_data_length;
1986 				if (slen > lim) {
1987 					slen = lim;
1988 				}
1989 				pmcs_latch_status(pwp, sp, rptr->status, xd,
1990 				    slen, pptr->path);
1991 			} else if (rptr->datapres == SAS_RSP_DATAPRES_NO_DATA) {
1992 				pmcout_ssp_comp_t *sspcp;
1993 				sspcp = (pmcout_ssp_comp_t *)msg;
1994 				uint32_t *residp;
1995 				/*
1996 				 * This is the case for a plain SCSI status.
1997 				 * Note: If RESC_V is set and we're here, there
1998 				 * is a residual.  We need to find it and update
1999 				 * the packet accordingly.
2000 				 */
2001 				pmcs_latch_status(pwp, sp, rptr->status, NULL,
2002 				    0, pptr->path);
2003 
2004 				if (sspcp->resc_v) {
2005 					/*
2006 					 * Point residual to the SSP_RESP_IU
2007 					 */
2008 					residp = (uint32_t *)(sspcp + 1);
2009 					/*
2010 					 * param contains the number of bytes
2011 					 * between where the SSP_RESP_IU may
2012 					 * or may not be and the residual.
2013 					 * Increment residp by the appropriate
2014 					 * number of words: (param+resc_pad)/4).
2015 					 */
2016 					residp += (LE_32(sspcp->param) +
2017 					    sspcp->resc_pad) /
2018 					    sizeof (uint32_t);
2019 					pmcs_prt(pwp, PMCS_PRT_DEBUG_UNDERFLOW,
2020 					    pptr, xp, "%s: tgt 0x%p "
2021 					    "residual %d for pkt 0x%p",
2022 					    __func__, (void *) xp, *residp,
2023 					    (void *) pkt);
2024 					ASSERT(LE_32(*residp) <=
2025 					    pkt->pkt_dma_len);
2026 					(void) pmcs_set_resid(pkt,
2027 					    pkt->pkt_dma_len, LE_32(*residp));
2028 				}
2029 			} else {
2030 				pmcs_print_entry(pwp, PMCS_PRT_DEBUG,
2031 				    "illegal SAS response", msg);
2032 				pkt->pkt_reason = CMD_TRAN_ERR;
2033 				goto out;
2034 			}
2035 		} else {
2036 			pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0,
2037 			    pptr->path);
2038 		}
2039 		if (pkt->pkt_dma_len) {
2040 			pkt->pkt_state |= STATE_XFERRED_DATA;
2041 		}
2042 	}
2043 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp,
2044 	    "%s: pkt %p tgt %u done reason=%x state=%x resid=%ld status=%x",
2045 	    __func__, (void *)pkt, xp->target_num, pkt->pkt_reason,
2046 	    pkt->pkt_state, pkt->pkt_resid, pkt->pkt_scbp[0]);
2047 
2048 	if (pwrk->state == PMCS_WORK_STATE_ABORTED) {
2049 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2050 		    "%s: scsi_pkt 0x%p aborted for PHY %s; work = 0x%p",
2051 		    __func__, (void *)pkt, pptr->path, (void *)pwrk);
2052 		aborted = B_TRUE;
2053 	}
2054 
2055 out:
2056 	pmcs_pwork(pwp, pwrk);
2057 	pmcs_dma_unload(pwp, sp);
2058 
2059 	mutex_enter(&xp->statlock);
2060 	if (xp->dev_gone) {
2061 		mutex_exit(&xp->statlock);
2062 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp,
2063 		    "%s: Completing command for dead target 0x%p", __func__,
2064 		    (void *)xp);
2065 		return;
2066 	}
2067 
2068 	ASSERT(xp->actv_cnt > 0);
2069 	if (--(xp->actv_cnt) == 0) {
2070 		if (xp->draining) {
2071 			pmcs_prt(pwp, PMCS_PRT_DEBUG1, pptr, xp,
2072 			    "%s: waking up drain waiters", __func__);
2073 			cv_signal(&pwp->drain_cv);
2074 		}
2075 	}
2076 	mutex_exit(&xp->statlock);
2077 	if (dead == 0) {
2078 #ifdef	DEBUG
2079 		pmcs_cmd_t *wp;
2080 		mutex_enter(&xp->aqlock);
2081 		STAILQ_FOREACH(wp, &xp->aq, cmd_next) {
2082 			if (wp == sp) {
2083 				break;
2084 			}
2085 		}
2086 		ASSERT(wp != NULL);
2087 #else
2088 		mutex_enter(&xp->aqlock);
2089 #endif
2090 		STAILQ_REMOVE(&xp->aq, sp, pmcs_cmd, cmd_next);
2091 		if (aborted) {
2092 			pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2093 			    "%s: Aborted cmd for tgt 0x%p, signaling waiters",
2094 			    __func__, (void *)xp);
2095 			cv_signal(&xp->abort_cv);
2096 		}
2097 		mutex_exit(&xp->aqlock);
2098 	}
2099 
2100 	/*
2101 	 * If do_ds_recovery is set, we need to initiate device state
2102 	 * recovery.  In this case, we put this I/O back on the head of
2103 	 * the wait queue to run again after recovery is complete
2104 	 */
2105 	if (do_ds_recovery) {
2106 		mutex_enter(&xp->statlock);
2107 		pmcs_start_dev_state_recovery(xp, pptr);
2108 		mutex_exit(&xp->statlock);
2109 		pmcs_prt(pwp, PMCS_PRT_DEBUG1, pptr, xp, "%s: Putting cmd 0x%p "
2110 		    "back on wq during recovery for tgt 0x%p", __func__,
2111 		    (void *)sp, (void *)xp);
2112 		mutex_enter(&xp->wqlock);
2113 		if (xp->wq_recovery_tail == NULL) {
2114 			STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2115 		} else {
2116 			/*
2117 			 * If there are other I/Os waiting at the head due to
2118 			 * device state recovery, add this one in the right spot
2119 			 * to maintain proper order.
2120 			 */
2121 			STAILQ_INSERT_AFTER(&xp->wq, xp->wq_recovery_tail, sp,
2122 			    cmd_next);
2123 		}
2124 		xp->wq_recovery_tail = sp;
2125 		mutex_exit(&xp->wqlock);
2126 	} else {
2127 		/*
2128 		 * If we're not initiating device state recovery and this
2129 		 * command was not "dead", put it on the completion queue
2130 		 */
2131 		if (!dead) {
2132 			mutex_enter(&pwp->cq_lock);
2133 			STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
2134 			mutex_exit(&pwp->cq_lock);
2135 		}
2136 	}
2137 }
2138 
2139 /*
2140  * Run a SATA command (normal reads and writes),
2141  * or block and schedule a SATL interpretation
2142  * of the command.
2143  *
2144  * Called with pwrk lock held, returns unlocked.
2145  */
2146 
2147 static int
2148 pmcs_SATA_run(pmcs_cmd_t *sp, pmcwork_t *pwrk)
2149 {
2150 	pmcs_hw_t *pwp = CMD2PMC(sp);
2151 	struct scsi_pkt *pkt = CMD2PKT(sp);
2152 	pmcs_xscsi_t *xp;
2153 	uint8_t cdb_base, asc, tag;
2154 	uint32_t *ptr, iq, nblk, i, mtype;
2155 	fis_t fis;
2156 	size_t amt;
2157 	uint64_t lba;
2158 
2159 	xp = pwrk->xp;
2160 
2161 	/*
2162 	 * First, see if this is just a plain read/write command.
2163 	 * If not, we have to queue it up for processing, block
2164 	 * any additional commands from coming in, and wake up
2165 	 * the thread that will process this command.
2166 	 */
2167 	cdb_base = pkt->pkt_cdbp[0] & 0x1f;
2168 	if (cdb_base != SCMD_READ && cdb_base != SCMD_WRITE) {
2169 		pmcs_prt(pwp, PMCS_PRT_DEBUG1, NULL, NULL,
2170 		    "%s: special SATA cmd %p", __func__, (void *)sp);
2171 
2172 		ASSERT(xp->phy != NULL);
2173 		pmcs_pwork(pwp, pwrk);
2174 		pmcs_lock_phy(xp->phy);
2175 		mutex_enter(&xp->statlock);
2176 		xp->special_needed = 1; /* Set the special_needed flag */
2177 		STAILQ_INSERT_TAIL(&xp->sq, sp, cmd_next);
2178 		if (pmcs_run_sata_special(pwp, xp)) {
2179 			SCHEDULE_WORK(pwp, PMCS_WORK_SATA_RUN);
2180 		}
2181 		mutex_exit(&xp->statlock);
2182 		pmcs_unlock_phy(xp->phy);
2183 
2184 		return (PMCS_WQ_RUN_SUCCESS);
2185 	}
2186 
2187 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL, "%s: regular cmd", __func__);
2188 
2189 	mutex_enter(&xp->statlock);
2190 	if (!xp->assigned) {
2191 		mutex_exit(&xp->statlock);
2192 		return (PMCS_WQ_RUN_FAIL_OTHER);
2193 	}
2194 	if (xp->special_running || xp->special_needed || xp->recover_wait) {
2195 		mutex_exit(&xp->statlock);
2196 		mutex_enter(&xp->wqlock);
2197 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2198 		mutex_exit(&xp->wqlock);
2199 		/*
2200 		 * By the time we get here the special
2201 		 * commands running or waiting to be run
2202 		 * may have come and gone, so kick our
2203 		 * worker to run the waiting queues
2204 		 * just in case.
2205 		 */
2206 		SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
2207 		return (PMCS_WQ_RUN_FAIL_OTHER);
2208 	}
2209 	lba = xp->capacity;
2210 	mutex_exit(&xp->statlock);
2211 
2212 	/*
2213 	 * Extract data length and lba parameters out of the command. The
2214 	 * function pmcs_SATA_rwparm returns a non-zero ASC value if the CDB
2215 	 * values are considered illegal.
2216 	 */
2217 	asc = pmcs_SATA_rwparm(pkt->pkt_cdbp, &nblk, &lba, lba);
2218 	if (asc) {
2219 		uint8_t sns[18];
2220 		bzero(sns, sizeof (sns));
2221 		sns[0] = 0xf0;
2222 		sns[2] = 0x5;
2223 		sns[12] = asc;
2224 		pmcs_latch_status(pwp, sp, STATUS_CHECK, sns, sizeof (sns),
2225 		    pwrk->phy->path);
2226 		pmcs_pwork(pwp, pwrk);
2227 		mutex_enter(&pwp->cq_lock);
2228 		STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
2229 		PMCS_CQ_RUN_LOCKED(pwp);
2230 		mutex_exit(&pwp->cq_lock);
2231 		return (PMCS_WQ_RUN_SUCCESS);
2232 	}
2233 
2234 	/*
2235 	 * If the command decodes as not moving any data, complete it here.
2236 	 */
2237 	amt = nblk;
2238 	amt <<= 9;
2239 	amt = pmcs_set_resid(pkt, amt, nblk << 9);
2240 	if (amt == 0) {
2241 		pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0,
2242 		    pwrk->phy->path);
2243 		pmcs_pwork(pwp, pwrk);
2244 		mutex_enter(&pwp->cq_lock);
2245 		STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
2246 		PMCS_CQ_RUN_LOCKED(pwp);
2247 		mutex_exit(&pwp->cq_lock);
2248 		return (PMCS_WQ_RUN_SUCCESS);
2249 	}
2250 
2251 	/*
2252 	 * Get an inbound queue entry for this I/O
2253 	 */
2254 	GET_IO_IQ_ENTRY(pwp, ptr, xp->phy->device_id, iq);
2255 	if (ptr == NULL) {
2256 		/*
2257 		 * This is a temporary failure not likely to unblocked by
2258 		 * commands completing as the test for scheduling the
2259 		 * restart of work is a per-device test.
2260 		 */
2261 		mutex_enter(&xp->wqlock);
2262 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2263 		mutex_exit(&xp->wqlock);
2264 		pmcs_dma_unload(pwp, sp);
2265 		SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
2266 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
2267 		    "%s: Failed to get IO IQ entry for tgt %d",
2268 		    __func__, xp->target_num);
2269 		return (PMCS_WQ_RUN_FAIL_RES);
2270 	}
2271 
2272 	/*
2273 	 * Get a tag.  At this point, hold statlock until the tagmap is
2274 	 * updated (just prior to sending the cmd to the hardware).
2275 	 */
2276 	mutex_enter(&xp->statlock);
2277 	for (tag = 0; tag < xp->qdepth; tag++) {
2278 		if ((xp->tagmap & (1 << tag)) == 0) {
2279 			break;
2280 		}
2281 	}
2282 
2283 	if (tag == xp->qdepth) {
2284 		mutex_exit(&xp->statlock);
2285 		mutex_exit(&pwp->iqp_lock[iq]);
2286 		mutex_enter(&xp->wqlock);
2287 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2288 		mutex_exit(&xp->wqlock);
2289 		return (PMCS_WQ_RUN_FAIL_OTHER);
2290 	}
2291 
2292 	sp->cmd_satltag = (uint8_t)tag;
2293 
2294 	/*
2295 	 * Set up the command
2296 	 */
2297 	bzero(fis, sizeof (fis));
2298 	ptr[0] =
2299 	    LE_32(PMCS_IOMB_IN_SAS(PMCS_OQ_IODONE, PMCIN_SATA_HOST_IO_START));
2300 	ptr[1] = LE_32(pwrk->htag);
2301 	ptr[2] = LE_32(pwrk->phy->device_id);
2302 	ptr[3] = LE_32(amt);
2303 
2304 	if (xp->ncq) {
2305 		mtype = SATA_PROTOCOL_FPDMA | (tag << 16);
2306 		fis[0] = ((nblk & 0xff) << 24) | (C_BIT << 8) | FIS_REG_H2DEV;
2307 		if (cdb_base == SCMD_READ) {
2308 			fis[0] |= (READ_FPDMA_QUEUED << 16);
2309 		} else {
2310 			fis[0] |= (WRITE_FPDMA_QUEUED << 16);
2311 		}
2312 		fis[1] = (FEATURE_LBA << 24) | (lba & 0xffffff);
2313 		fis[2] = ((nblk & 0xff00) << 16) | ((lba >> 24) & 0xffffff);
2314 		fis[3] = tag << 3;
2315 	} else {
2316 		int op;
2317 		fis[0] = (C_BIT << 8) | FIS_REG_H2DEV;
2318 		if (xp->pio) {
2319 			mtype = SATA_PROTOCOL_PIO;
2320 			if (cdb_base == SCMD_READ) {
2321 				op = READ_SECTORS_EXT;
2322 			} else {
2323 				op = WRITE_SECTORS_EXT;
2324 			}
2325 		} else {
2326 			mtype = SATA_PROTOCOL_DMA;
2327 			if (cdb_base == SCMD_READ) {
2328 				op = READ_DMA_EXT;
2329 			} else {
2330 				op = WRITE_DMA_EXT;
2331 			}
2332 		}
2333 		fis[0] |= (op << 16);
2334 		fis[1] = (FEATURE_LBA << 24) | (lba & 0xffffff);
2335 		fis[2] = (lba >> 24) & 0xffffff;
2336 		fis[3] = nblk;
2337 	}
2338 
2339 	if (cdb_base == SCMD_READ) {
2340 		ptr[4] = LE_32(mtype | PMCIN_DATADIR_2_INI);
2341 	} else {
2342 		ptr[4] = LE_32(mtype | PMCIN_DATADIR_2_DEV);
2343 	}
2344 #ifdef	DEBUG
2345 	/*
2346 	 * Generate a PMCOUT_STATUS_XFER_CMD_FRAME_ISSUED
2347 	 * event when this goes out on the wire.
2348 	 */
2349 	ptr[4] |= PMCIN_MESSAGE_REPORT;
2350 #endif
2351 	for (i = 0; i < (sizeof (fis_t))/(sizeof (uint32_t)); i++) {
2352 		ptr[i+5] = LE_32(fis[i]);
2353 	}
2354 	if (pmcs_dma_load(pwp, sp, ptr)) {
2355 		mutex_exit(&xp->statlock);
2356 		mutex_exit(&pwp->iqp_lock[iq]);
2357 		mutex_enter(&xp->wqlock);
2358 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2359 		mutex_exit(&xp->wqlock);
2360 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
2361 		    "%s: Failed to dma_load for tgt %d",
2362 		    __func__, xp->target_num);
2363 		return (PMCS_WQ_RUN_FAIL_RES);
2364 
2365 	}
2366 
2367 	pwrk->state = PMCS_WORK_STATE_ONCHIP;
2368 	mutex_exit(&pwrk->lock);
2369 	xp->tagmap |= (1 << tag);
2370 	xp->actv_cnt++;
2371 	if (xp->actv_cnt > xp->maxdepth) {
2372 		xp->maxdepth = xp->actv_cnt;
2373 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, pwrk->phy, xp,
2374 		    "%s: max depth now %u", pwrk->phy->path, xp->maxdepth);
2375 	}
2376 	mutex_exit(&xp->statlock);
2377 	mutex_enter(&xp->aqlock);
2378 	STAILQ_INSERT_TAIL(&xp->aq, sp, cmd_next);
2379 	mutex_exit(&xp->aqlock);
2380 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL,
2381 	    "%s: giving pkt %p to hardware", __func__, (void *)pkt);
2382 #ifdef DEBUG
2383 	pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "SATA INI Message", ptr);
2384 #endif
2385 	INC_IQ_ENTRY(pwp, iq);
2386 
2387 	return (PMCS_WQ_RUN_SUCCESS);
2388 }
2389 
2390 /*
2391  * Complete a SATA command.  Called with pwrk lock held.
2392  */
2393 void
2394 pmcs_SATA_done(pmcs_hw_t *pwp, pmcwork_t *pwrk, uint32_t *msg)
2395 {
2396 	pmcs_cmd_t *sp = pwrk->arg;
2397 	struct scsi_pkt *pkt = CMD2PKT(sp);
2398 	pmcs_phy_t *pptr = pwrk->phy;
2399 	int dead;
2400 	uint32_t sts;
2401 	pmcs_xscsi_t *xp;
2402 	boolean_t aborted = B_FALSE;
2403 
2404 	xp = pwrk->xp;
2405 	ASSERT(xp != NULL);
2406 
2407 	DTRACE_PROBE4(pmcs__io__done, uint64_t, pkt->pkt_dma_len, int,
2408 	    (pkt->pkt_dma_flags & DDI_DMA_READ) != 0, hrtime_t, pwrk->start,
2409 	    hrtime_t, gethrtime());
2410 
2411 	dead = pwrk->dead;
2412 
2413 	if (msg) {
2414 		sts = LE_32(msg[2]);
2415 	} else {
2416 		sts = 0;
2417 	}
2418 
2419 	if (dead != 0) {
2420 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, "%s: dead cmd tag "
2421 		    "0x%x for %s", __func__, pwrk->htag, pptr->path);
2422 		goto out;
2423 	}
2424 	if ((pwrk->state == PMCS_WORK_STATE_TIMED_OUT) &&
2425 	    (sts != PMCOUT_STATUS_ABORTED)) {
2426 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2427 		    "%s: cmd 0x%p (tag 0x%x) timed out for %s",
2428 		    __func__, (void *)sp, pwrk->htag, pptr->path);
2429 		CMD2PKT(sp)->pkt_scbp[0] = STATUS_GOOD;
2430 		/* pkt_reason already set to CMD_TIMEOUT */
2431 		ASSERT(CMD2PKT(sp)->pkt_reason == CMD_TIMEOUT);
2432 		CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
2433 		    STATE_SENT_CMD;
2434 		CMD2PKT(sp)->pkt_statistics |= STAT_TIMEOUT;
2435 		goto out;
2436 	}
2437 
2438 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp, "%s: pkt %p tgt %u done",
2439 	    __func__, (void *)pkt, xp->target_num);
2440 
2441 	/*
2442 	 * If the status isn't okay but not underflow,
2443 	 * step to the side and parse the (possible) error.
2444 	 */
2445 #ifdef DEBUG
2446 	if (msg) {
2447 		pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "Outbound Message", msg);
2448 	}
2449 #endif
2450 	if (!msg) {
2451 		goto out;
2452 	}
2453 
2454 	/*
2455 	 * If the status isn't okay or we got a FIS response of some kind,
2456 	 * step to the side and parse the (possible) error.
2457 	 */
2458 	if ((sts != PMCOUT_STATUS_OK) || (LE_32(msg[3]) != 0)) {
2459 		if (sts == PMCOUT_STATUS_IO_DS_NON_OPERATIONAL) {
2460 			mutex_exit(&pwrk->lock);
2461 			pmcs_lock_phy(pptr);
2462 			mutex_enter(&xp->statlock);
2463 			if ((xp->resetting == 0) && (xp->reset_success != 0) &&
2464 			    (xp->reset_wait == 0)) {
2465 				mutex_exit(&xp->statlock);
2466 				if (pmcs_reset_phy(pwp, pptr,
2467 				    PMCS_PHYOP_LINK_RESET) != 0) {
2468 					pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2469 					    "%s: PHY (%s) Local Control/Link "
2470 					    "Reset FAILED as part of error "
2471 					    "recovery", __func__, pptr->path);
2472 				}
2473 				mutex_enter(&xp->statlock);
2474 			}
2475 			mutex_exit(&xp->statlock);
2476 			pmcs_unlock_phy(pptr);
2477 			mutex_enter(&pwrk->lock);
2478 		}
2479 		pmcs_ioerror(pwp, SATA, pwrk, msg);
2480 	} else {
2481 		pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0,
2482 		    pwrk->phy->path);
2483 		pkt->pkt_state |= STATE_XFERRED_DATA;
2484 		pkt->pkt_resid = 0;
2485 	}
2486 
2487 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp,
2488 	    "%s: pkt %p tgt %u done reason=%x state=%x resid=%ld status=%x",
2489 	    __func__, (void *)pkt, xp->target_num, pkt->pkt_reason,
2490 	    pkt->pkt_state, pkt->pkt_resid, pkt->pkt_scbp[0]);
2491 
2492 	if (pwrk->state == PMCS_WORK_STATE_ABORTED) {
2493 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2494 		    "%s: scsi_pkt 0x%p aborted for PHY %s; work = 0x%p",
2495 		    __func__, (void *)pkt, pptr->path, (void *)pwrk);
2496 		aborted = B_TRUE;
2497 	}
2498 
2499 out:
2500 	pmcs_pwork(pwp, pwrk);
2501 	pmcs_dma_unload(pwp, sp);
2502 
2503 	mutex_enter(&xp->statlock);
2504 	xp->tagmap &= ~(1 << sp->cmd_satltag);
2505 
2506 	if (xp->dev_gone) {
2507 		mutex_exit(&xp->statlock);
2508 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp,
2509 		    "%s: Completing command for dead target 0x%p", __func__,
2510 		    (void *)xp);
2511 		return;
2512 	}
2513 
2514 	ASSERT(xp->actv_cnt > 0);
2515 	if (--(xp->actv_cnt) == 0) {
2516 		if (xp->draining) {
2517 			pmcs_prt(pwp, PMCS_PRT_DEBUG1, pptr, xp,
2518 			    "%s: waking up drain waiters", __func__);
2519 			cv_signal(&pwp->drain_cv);
2520 		} else if (xp->special_needed) {
2521 			SCHEDULE_WORK(pwp, PMCS_WORK_SATA_RUN);
2522 		}
2523 	}
2524 	mutex_exit(&xp->statlock);
2525 
2526 	if (dead == 0) {
2527 #ifdef	DEBUG
2528 		pmcs_cmd_t *wp;
2529 		mutex_enter(&xp->aqlock);
2530 		STAILQ_FOREACH(wp, &xp->aq, cmd_next) {
2531 			if (wp == sp) {
2532 				break;
2533 			}
2534 		}
2535 		ASSERT(wp != NULL);
2536 #else
2537 		mutex_enter(&xp->aqlock);
2538 #endif
2539 		STAILQ_REMOVE(&xp->aq, sp, pmcs_cmd, cmd_next);
2540 		if (aborted) {
2541 			pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2542 			    "%s: Aborted cmd for tgt 0x%p, signaling waiters",
2543 			    __func__, (void *)xp);
2544 			cv_signal(&xp->abort_cv);
2545 		}
2546 		mutex_exit(&xp->aqlock);
2547 		mutex_enter(&pwp->cq_lock);
2548 		STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
2549 		mutex_exit(&pwp->cq_lock);
2550 	}
2551 }
2552 
2553 static uint8_t
2554 pmcs_SATA_rwparm(uint8_t *cdb, uint32_t *xfr, uint64_t *lba, uint64_t lbamax)
2555 {
2556 	uint8_t asc = 0;
2557 	switch (cdb[0]) {
2558 	case SCMD_READ_G5:
2559 	case SCMD_WRITE_G5:
2560 		*xfr =
2561 		    (((uint32_t)cdb[10]) <<  24) |
2562 		    (((uint32_t)cdb[11]) <<  16) |
2563 		    (((uint32_t)cdb[12]) <<   8) |
2564 		    ((uint32_t)cdb[13]);
2565 		*lba =
2566 		    (((uint64_t)cdb[2]) << 56) |
2567 		    (((uint64_t)cdb[3]) << 48) |
2568 		    (((uint64_t)cdb[4]) << 40) |
2569 		    (((uint64_t)cdb[5]) << 32) |
2570 		    (((uint64_t)cdb[6]) << 24) |
2571 		    (((uint64_t)cdb[7]) << 16) |
2572 		    (((uint64_t)cdb[8]) <<  8) |
2573 		    ((uint64_t)cdb[9]);
2574 		/* Check for illegal bits */
2575 		if (cdb[15]) {
2576 			asc = 0x24;	/* invalid field in cdb */
2577 		}
2578 		break;
2579 	case SCMD_READ_G4:
2580 	case SCMD_WRITE_G4:
2581 		*xfr =
2582 		    (((uint32_t)cdb[6]) <<  16) |
2583 		    (((uint32_t)cdb[7]) <<   8) |
2584 		    ((uint32_t)cdb[8]);
2585 		*lba =
2586 		    (((uint32_t)cdb[2]) << 24) |
2587 		    (((uint32_t)cdb[3]) << 16) |
2588 		    (((uint32_t)cdb[4]) <<  8) |
2589 		    ((uint32_t)cdb[5]);
2590 		/* Check for illegal bits */
2591 		if (cdb[11]) {
2592 			asc = 0x24;	/* invalid field in cdb */
2593 		}
2594 		break;
2595 	case SCMD_READ_G1:
2596 	case SCMD_WRITE_G1:
2597 		*xfr = (((uint32_t)cdb[7]) <<  8) | ((uint32_t)cdb[8]);
2598 		*lba =
2599 		    (((uint32_t)cdb[2]) << 24) |
2600 		    (((uint32_t)cdb[3]) << 16) |
2601 		    (((uint32_t)cdb[4]) <<  8) |
2602 		    ((uint32_t)cdb[5]);
2603 		/* Check for illegal bits */
2604 		if (cdb[9]) {
2605 			asc = 0x24;	/* invalid field in cdb */
2606 		}
2607 		break;
2608 	case SCMD_READ:
2609 	case SCMD_WRITE:
2610 		*xfr = cdb[4];
2611 		if (*xfr == 0) {
2612 			*xfr = 256;
2613 		}
2614 		*lba =
2615 		    (((uint32_t)cdb[1] & 0x1f) << 16) |
2616 		    (((uint32_t)cdb[2]) << 8) |
2617 		    ((uint32_t)cdb[3]);
2618 		/* Check for illegal bits */
2619 		if (cdb[5]) {
2620 			asc = 0x24;	/* invalid field in cdb */
2621 		}
2622 		break;
2623 	}
2624 
2625 	if (asc == 0) {
2626 		if ((*lba + *xfr) > lbamax) {
2627 			asc = 0x21;	/* logical block out of range */
2628 		}
2629 	}
2630 	return (asc);
2631 }
2632 
2633 /*
2634  * Called with pwrk lock held.
2635  */
2636 static void
2637 pmcs_ioerror(pmcs_hw_t *pwp, pmcs_dtype_t t, pmcwork_t *pwrk, uint32_t *w)
2638 {
2639 	static uint8_t por[] = {
2640 	    0xf0, 0x0, 0x6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x28
2641 	};
2642 	static uint8_t parity[] = {
2643 	    0xf0, 0x0, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x47, 5
2644 	};
2645 	const char *msg;
2646 	char buf[20];
2647 	pmcs_cmd_t *sp = pwrk->arg;
2648 	pmcs_phy_t *phyp = pwrk->phy;
2649 	struct scsi_pkt *pkt = CMD2PKT(sp);
2650 	uint32_t status;
2651 	uint32_t resid;
2652 
2653 	ASSERT(w != NULL);
2654 	status = LE_32(w[2]);
2655 	resid = LE_32(w[3]);
2656 
2657 	msg = pmcs_status_str(status);
2658 	if (msg == NULL) {
2659 		(void) snprintf(buf, sizeof (buf), "Error 0x%x", status);
2660 		msg = buf;
2661 	}
2662 
2663 	if (status != PMCOUT_STATUS_OK) {
2664 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, phyp, NULL,
2665 		    "%s: device %s tag 0x%x status %s @ %llu", __func__,
2666 		    phyp->path, pwrk->htag, msg,
2667 		    (unsigned long long)gethrtime());
2668 	}
2669 
2670 	pkt->pkt_reason = CMD_CMPLT;		/* default reason */
2671 
2672 	switch (status) {
2673 	case PMCOUT_STATUS_OK:
2674 		if (t == SATA) {
2675 			int i;
2676 			fis_t fis;
2677 			for (i = 0; i < sizeof (fis) / sizeof (fis[0]); i++) {
2678 				fis[i] = LE_32(w[4+i]);
2679 			}
2680 			if ((fis[0] & 0xff) != FIS_REG_D2H) {
2681 				pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, NULL,
2682 				    "unexpected fis code 0x%x", fis[0] & 0xff);
2683 			} else {
2684 				pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, NULL,
2685 				    "FIS ERROR");
2686 				pmcs_fis_dump(pwp, fis);
2687 			}
2688 			pkt->pkt_reason = CMD_TRAN_ERR;
2689 			break;
2690 		}
2691 		pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0, phyp->path);
2692 		break;
2693 
2694 	case PMCOUT_STATUS_ABORTED:
2695 		/*
2696 		 * Command successfully aborted.
2697 		 */
2698 		if (phyp->dead) {
2699 			pkt->pkt_reason = CMD_DEV_GONE;
2700 			pkt->pkt_state = STATE_GOT_BUS;
2701 		} else if (pwrk->ssp_event != 0) {
2702 			pkt->pkt_reason = CMD_TRAN_ERR;
2703 			pkt->pkt_state = STATE_GOT_BUS;
2704 		} else if (pwrk->state == PMCS_WORK_STATE_TIMED_OUT) {
2705 			pkt->pkt_reason = CMD_TIMEOUT;
2706 			pkt->pkt_statistics |= STAT_TIMEOUT;
2707 			pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2708 			    STATE_SENT_CMD;
2709 		} else {
2710 			pkt->pkt_reason = CMD_ABORTED;
2711 			pkt->pkt_statistics |= STAT_ABORTED;
2712 			pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2713 			    STATE_SENT_CMD;
2714 		}
2715 
2716 		/*
2717 		 * PMCS_WORK_STATE_TIMED_OUT doesn't need to be preserved past
2718 		 * this point, so go ahead and mark it as aborted.
2719 		 */
2720 		pwrk->state = PMCS_WORK_STATE_ABORTED;
2721 		break;
2722 
2723 	case PMCOUT_STATUS_UNDERFLOW:
2724 		/*
2725 		 * This will only get called for SATA
2726 		 */
2727 		pkt->pkt_resid = resid;
2728 		if (pkt->pkt_dma_len < pkt->pkt_resid) {
2729 			(void) pmcs_set_resid(pkt, pkt->pkt_dma_len, resid);
2730 		}
2731 		pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0, phyp->path);
2732 		break;
2733 
2734 	case PMCOUT_STATUS_NO_DEVICE:
2735 	case PMCOUT_STATUS_XFER_ERROR_SATA_LINK_TIMEOUT:
2736 		pkt->pkt_reason = CMD_DEV_GONE;
2737 		break;
2738 
2739 	case PMCOUT_STATUS_OPEN_CNX_ERROR_WRONG_DESTINATION:
2740 		/*
2741 		 * Need to do rediscovery. We probably have
2742 		 * the wrong device (disk swap), so kill
2743 		 * this one.
2744 		 */
2745 	case PMCOUT_STATUS_OPEN_CNX_PROTOCOL_NOT_SUPPORTED:
2746 	case PMCOUT_STATUS_OPEN_CNX_ERROR_ZONE_VIOLATION:
2747 	case PMCOUT_STATUS_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED:
2748 	case PMCOUT_STATUS_OPEN_CNX_ERROR_UNKNOWN_EROOR:
2749 		/*
2750 		 * Need to do rediscovery.
2751 		 */
2752 		if (!phyp->dead) {
2753 			mutex_exit(&pwrk->lock);
2754 			pmcs_lock_phy(pwrk->phy);
2755 			pmcs_kill_changed(pwp, pwrk->phy, 0);
2756 			pmcs_unlock_phy(pwrk->phy);
2757 			mutex_enter(&pwrk->lock);
2758 			pkt->pkt_reason = CMD_INCOMPLETE;
2759 			pkt->pkt_state = STATE_GOT_BUS;
2760 		} else {
2761 			pkt->pkt_reason = CMD_DEV_GONE;
2762 		}
2763 		break;
2764 
2765 	case PMCOUT_STATUS_OPEN_CNX_ERROR_BREAK:
2766 	case PMCOUT_STATUS_OPEN_CNX_ERROR_IT_NEXUS_LOSS:
2767 	case PMCOUT_STATUS_OPENCNX_ERROR_BAD_DESTINATION:
2768 	case PMCOUT_STATUS_IO_XFER_ERROR_NAK_RECEIVED:
2769 		/* cmd is pending on the target */
2770 	case PMCOUT_STATUS_XFER_ERROR_OFFSET_MISMATCH:
2771 	case PMCOUT_STATUS_XFER_ERROR_REJECTED_NCQ_MODE:
2772 		/* transitory - commands sent while in NCQ failure mode */
2773 	case PMCOUT_STATUS_XFER_ERROR_ABORTED_NCQ_MODE:
2774 		/* NCQ failure */
2775 	case PMCOUT_STATUS_IO_PORT_IN_RESET:
2776 	case PMCOUT_STATUS_XFER_ERR_BREAK:
2777 	case PMCOUT_STATUS_XFER_ERR_PHY_NOT_READY:
2778 		pkt->pkt_reason = CMD_INCOMPLETE;
2779 		pkt->pkt_state = STATE_GOT_BUS;
2780 		break;
2781 
2782 	case PMCOUT_STATUS_IO_XFER_OPEN_RETRY_TIMEOUT:
2783 		pmcs_latch_status(pwp, sp, STATUS_BUSY, NULL, 0, phyp->path);
2784 		break;
2785 
2786 	case PMCOUT_STATUS_OPEN_CNX_ERROR_STP_RESOURCES_BUSY:
2787 		/* synthesize a RESERVATION CONFLICT */
2788 		pmcs_latch_status(pwp, sp, STATUS_RESERVATION_CONFLICT, NULL,
2789 		    0, phyp->path);
2790 		break;
2791 
2792 	case PMCOUT_STATUS_XFER_ERROR_ABORTED_DUE_TO_SRST:
2793 		/* synthesize a power-on/reset */
2794 		pmcs_latch_status(pwp, sp, STATUS_CHECK, por, sizeof (por),
2795 		    phyp->path);
2796 		break;
2797 
2798 	case PMCOUT_STATUS_XFER_ERROR_UNEXPECTED_PHASE:
2799 	case PMCOUT_STATUS_XFER_ERROR_RDY_OVERRUN:
2800 	case PMCOUT_STATUS_XFER_ERROR_RDY_NOT_EXPECTED:
2801 	case PMCOUT_STATUS_XFER_ERROR_CMD_ISSUE_ACK_NAK_TIMEOUT:
2802 	case PMCOUT_STATUS_XFER_ERROR_CMD_ISSUE_BREAK_BEFORE_ACK_NACK:
2803 	case PMCOUT_STATUS_XFER_ERROR_CMD_ISSUE_PHY_DOWN_BEFORE_ACK_NAK:
2804 		/* synthesize a PARITY ERROR */
2805 		pmcs_latch_status(pwp, sp, STATUS_CHECK, parity,
2806 		    sizeof (parity), phyp->path);
2807 		break;
2808 
2809 	case PMCOUT_STATUS_IO_XFER_ERROR_DMA:
2810 	case PMCOUT_STATUS_IO_NOT_VALID:
2811 	case PMCOUT_STATUS_PROG_ERROR:
2812 	case PMCOUT_STATUS_XFER_ERROR_PEER_ABORTED:
2813 	case PMCOUT_STATUS_XFER_ERROR_SATA: /* non-NCQ failure */
2814 	default:
2815 		pkt->pkt_reason = CMD_TRAN_ERR;
2816 		break;
2817 	}
2818 }
2819 
2820 /*
2821  * Latch up SCSI status
2822  */
2823 
2824 void
2825 pmcs_latch_status(pmcs_hw_t *pwp, pmcs_cmd_t *sp, uint8_t status,
2826     uint8_t *snsp, size_t snslen, char *path)
2827 {
2828 	static const char c1[] =
2829 	    "%s: Status Byte 0x%02x for CDB0=0x%02x (%02x %02x %02x) "
2830 	    "HTAG 0x%x @ %llu";
2831 	static const char c2[] =
2832 	    "%s: Status Byte 0x%02x for CDB0=0x%02x HTAG 0x%x @ %llu";
2833 
2834 	CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
2835 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2836 	CMD2PKT(sp)->pkt_scbp[0] = status;
2837 
2838 	if (status == STATUS_CHECK && snsp &&
2839 	    (size_t)SCSA_STSLEN(sp) >= sizeof (struct scsi_arq_status)) {
2840 		struct scsi_arq_status *aqp =
2841 		    (void *) CMD2PKT(sp)->pkt_scbp;
2842 		size_t amt = sizeof (struct scsi_extended_sense);
2843 		uint8_t key = scsi_sense_key(snsp);
2844 		uint8_t asc = scsi_sense_asc(snsp);
2845 		uint8_t ascq = scsi_sense_ascq(snsp);
2846 		if (amt > snslen) {
2847 			amt = snslen;
2848 		}
2849 		pmcs_prt(pwp, PMCS_PRT_DEBUG_SCSI_STATUS, NULL, NULL, c1, path,
2850 		    status, CMD2PKT(sp)->pkt_cdbp[0] & 0xff, key, asc, ascq,
2851 		    sp->cmd_tag, (unsigned long long)gethrtime());
2852 		CMD2PKT(sp)->pkt_state |= STATE_ARQ_DONE;
2853 		(*(uint8_t *)&aqp->sts_rqpkt_status) = STATUS_GOOD;
2854 		aqp->sts_rqpkt_statistics = 0;
2855 		aqp->sts_rqpkt_reason = CMD_CMPLT;
2856 		aqp->sts_rqpkt_state = STATE_GOT_BUS |
2857 		    STATE_GOT_TARGET | STATE_SENT_CMD |
2858 		    STATE_XFERRED_DATA | STATE_GOT_STATUS;
2859 		(void) memcpy(&aqp->sts_sensedata, snsp, amt);
2860 		if (aqp->sts_sensedata.es_class != CLASS_EXTENDED_SENSE) {
2861 			aqp->sts_rqpkt_reason = CMD_TRAN_ERR;
2862 			aqp->sts_rqpkt_state = 0;
2863 			aqp->sts_rqpkt_resid =
2864 			    sizeof (struct scsi_extended_sense);
2865 		} else {
2866 			aqp->sts_rqpkt_resid =
2867 			    sizeof (struct scsi_extended_sense) - amt;
2868 		}
2869 	} else if (status) {
2870 		pmcs_prt(pwp, PMCS_PRT_DEBUG_SCSI_STATUS, NULL, NULL, c2,
2871 		    path, status, CMD2PKT(sp)->pkt_cdbp[0] & 0xff,
2872 		    sp->cmd_tag, (unsigned long long)gethrtime());
2873 	}
2874 
2875 	CMD2PKT(sp)->pkt_reason = CMD_CMPLT;
2876 }
2877 
2878 /*
2879  * Calculate and set packet residual and return the amount
2880  * left over after applying various filters.
2881  */
2882 size_t
2883 pmcs_set_resid(struct scsi_pkt *pkt, size_t amt, uint32_t cdbamt)
2884 {
2885 	pkt->pkt_resid = cdbamt;
2886 	if (amt > pkt->pkt_resid) {
2887 		amt = pkt->pkt_resid;
2888 	}
2889 	if (amt > pkt->pkt_dma_len) {
2890 		amt = pkt->pkt_dma_len;
2891 	}
2892 	return (amt);
2893 }
2894 
2895 /*
2896  * Return the existing target softstate if there is one.  If there is,
2897  * the PHY is locked as well and that lock must be freed by the caller
2898  * after the target/PHY linkage is established.
2899  */
2900 pmcs_xscsi_t *
2901 pmcs_get_target(pmcs_iport_t *iport, char *tgt_port)
2902 {
2903 	pmcs_hw_t *pwp = iport->pwp;
2904 	pmcs_phy_t *phyp;
2905 	pmcs_xscsi_t *tgt;
2906 	uint64_t wwn;
2907 	char unit_address[PMCS_MAX_UA_SIZE];
2908 	int ua_form = 1;
2909 
2910 	/*
2911 	 * Find the PHY for this target
2912 	 */
2913 	phyp = pmcs_find_phy_by_sas_address(pwp, iport, NULL, tgt_port);
2914 	if (phyp == NULL) {
2915 		pmcs_prt(pwp, PMCS_PRT_DEBUG3, NULL, NULL,
2916 		    "%s: No PHY for target @ %s", __func__, tgt_port);
2917 		return (NULL);
2918 	}
2919 
2920 	tgt = ddi_soft_state_bystr_get(iport->tgt_sstate, tgt_port);
2921 
2922 	if (tgt) {
2923 		/*
2924 		 * There's already a target.  Check its PHY pointer to see
2925 		 * if we need to clear the old linkages
2926 		 */
2927 		if (tgt->phy && (tgt->phy != phyp)) {
2928 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
2929 			    "%s: Target PHY updated from %p to %p", __func__,
2930 			    (void *)tgt->phy, (void *)phyp);
2931 			if (!IS_ROOT_PHY(tgt->phy)) {
2932 				pmcs_dec_phy_ref_count(tgt->phy);
2933 				pmcs_inc_phy_ref_count(phyp);
2934 			}
2935 			tgt->phy->target = NULL;
2936 		}
2937 
2938 		tgt->phy = phyp;
2939 		phyp->target = tgt;
2940 		return (tgt);
2941 	}
2942 
2943 	/*
2944 	 * Make sure the PHY we found is on the correct iport
2945 	 */
2946 	if (phyp->iport != iport) {
2947 		pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, NULL,
2948 		    "%s: No target at %s on this iport", __func__, tgt_port);
2949 		pmcs_unlock_phy(phyp);
2950 		return (NULL);
2951 	}
2952 
2953 	/*
2954 	 * Allocate the new softstate
2955 	 */
2956 	wwn = pmcs_barray2wwn(phyp->sas_address);
2957 	(void) scsi_wwn_to_wwnstr(wwn, ua_form, unit_address);
2958 
2959 	if (ddi_soft_state_bystr_zalloc(iport->tgt_sstate, unit_address) !=
2960 	    DDI_SUCCESS) {
2961 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
2962 		    "%s: Couldn't alloc softstate for device at %s",
2963 		    __func__, unit_address);
2964 		pmcs_unlock_phy(phyp);
2965 		return (NULL);
2966 	}
2967 
2968 	tgt = ddi_soft_state_bystr_get(iport->tgt_sstate, unit_address);
2969 	STAILQ_INIT(&tgt->wq);
2970 	STAILQ_INIT(&tgt->aq);
2971 	STAILQ_INIT(&tgt->sq);
2972 	mutex_init(&tgt->statlock, NULL, MUTEX_DRIVER,
2973 	    DDI_INTR_PRI(pwp->intr_pri));
2974 	mutex_init(&tgt->wqlock, NULL, MUTEX_DRIVER,
2975 	    DDI_INTR_PRI(pwp->intr_pri));
2976 	mutex_init(&tgt->aqlock, NULL, MUTEX_DRIVER,
2977 	    DDI_INTR_PRI(pwp->intr_pri));
2978 	cv_init(&tgt->reset_cv, NULL, CV_DRIVER, NULL);
2979 	cv_init(&tgt->abort_cv, NULL, CV_DRIVER, NULL);
2980 	tgt->qdepth = 1;
2981 	tgt->target_num = PMCS_INVALID_TARGET_NUM;
2982 	bcopy(unit_address, tgt->unit_address, PMCS_MAX_UA_SIZE);
2983 	tgt->pwp = pwp;
2984 	tgt->ua = strdup(iport->ua);
2985 	tgt->phy = phyp;
2986 	ASSERT((phyp->target == NULL) || (phyp->target == tgt));
2987 	if (phyp->target == NULL) {
2988 		phyp->target = tgt;
2989 	}
2990 
2991 	/*
2992 	 * Don't allocate LUN softstate for SMP targets
2993 	 */
2994 	if (phyp->dtype == EXPANDER) {
2995 		return (tgt);
2996 	}
2997 
2998 	if (ddi_soft_state_bystr_init(&tgt->lun_sstate,
2999 	    sizeof (pmcs_lun_t), PMCS_LUN_SSTATE_SZ) != 0) {
3000 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
3001 		    "%s: LUN soft_state_bystr_init failed", __func__);
3002 		ddi_soft_state_bystr_free(iport->tgt_sstate, tgt_port);
3003 		pmcs_unlock_phy(phyp);
3004 		return (NULL);
3005 	}
3006 
3007 	return (tgt);
3008 }
3009