xref: /illumos-gate/usr/src/uts/common/io/scsi/adapters/pmcs/pmcs_scsa.c (revision 0d166b18feda26f6f45f5be1c0c8c5e539b90e6c)
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 
1029 	/* SMP serialization */
1030 	pmcs_smp_acquire(pptr->iport);
1031 
1032 	pwrk->state = PMCS_WORK_STATE_ONCHIP;
1033 	htag = pwrk->htag;
1034 	INC_IQ_ENTRY(pwp, PMCS_IQ_OTHER);
1035 
1036 	pmcs_unlock_phy(pptr);
1037 	WAIT_FOR(pwrk, smp_pkt->smp_pkt_timeout * 1000, result);
1038 	pmcs_pwork(pwp, pwrk);
1039 	pmcs_lock_phy(pptr);
1040 
1041 	/* SMP serialization */
1042 	pmcs_smp_release(pptr->iport);
1043 
1044 	if (result) {
1045 		pmcs_timed_out(pwp, htag, __func__);
1046 		if (pmcs_abort(pwp, pptr, htag, 0, 0)) {
1047 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, pptr, NULL,
1048 			    "%s: Unable to issue SMP ABORT for htag 0x%08x",
1049 			    __func__, htag);
1050 		} else {
1051 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, pptr, NULL,
1052 			    "%s: Issuing SMP ABORT for htag 0x%08x",
1053 			    __func__, htag);
1054 		}
1055 		pmcs_unlock_phy(pptr);
1056 		pmcs_release_scratch(pwp);
1057 		smp_pkt->smp_pkt_reason = ETIMEDOUT;
1058 		return (DDI_FAILURE);
1059 	}
1060 	status = LE_32(msg[2]);
1061 	if (status == PMCOUT_STATUS_OVERFLOW) {
1062 		status = PMCOUT_STATUS_OK;
1063 		smp_pkt->smp_pkt_reason = EOVERFLOW;
1064 	}
1065 	if (status != PMCOUT_STATUS_OK) {
1066 		const char *emsg = pmcs_status_str(status);
1067 		if (emsg == NULL) {
1068 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL,
1069 			    "SMP operation failed (0x%x)", status);
1070 		} else {
1071 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL,
1072 			    "SMP operation failed (%s)", emsg);
1073 		}
1074 
1075 		if ((status == PMCOUT_STATUS_ERROR_HW_TIMEOUT) ||
1076 		    (status == PMCOUT_STATUS_IO_XFER_OPEN_RETRY_TIMEOUT)) {
1077 			smp_pkt->smp_pkt_reason =
1078 			    will_retry ? EAGAIN : ETIMEDOUT;
1079 			result = DDI_FAILURE;
1080 		} else if (status ==
1081 		    PMCOUT_STATUS_OPEN_CNX_ERROR_IT_NEXUS_LOSS) {
1082 			xp = pptr->target;
1083 			if (xp == NULL) {
1084 				smp_pkt->smp_pkt_reason = EIO;
1085 				result = DDI_FAILURE;
1086 				goto out;
1087 			}
1088 			if (xp->dev_state !=
1089 			    PMCS_DEVICE_STATE_NON_OPERATIONAL) {
1090 				xp->dev_state =
1091 				    PMCS_DEVICE_STATE_NON_OPERATIONAL;
1092 				pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, xp,
1093 				    "%s: Got _IT_NEXUS_LOSS SMP status. "
1094 				    "Tgt(0x%p) dev_state set to "
1095 				    "_NON_OPERATIONAL", __func__,
1096 				    (void *)xp);
1097 			}
1098 			/* ABORT any pending commands related to this device */
1099 			if (pmcs_abort(pwp, pptr, pptr->device_id, 1, 1) != 0) {
1100 				pptr->abort_pending = 1;
1101 				smp_pkt->smp_pkt_reason = EIO;
1102 				result = DDI_FAILURE;
1103 			}
1104 		} else {
1105 			smp_pkt->smp_pkt_reason = will_retry ? EAGAIN : EIO;
1106 			result = DDI_FAILURE;
1107 		}
1108 	} else {
1109 		(void) memcpy(smp_pkt->smp_pkt_rsp,
1110 		    &((uint8_t *)pwp->scratch)[rdoff], rspsz);
1111 		if (smp_pkt->smp_pkt_reason == EOVERFLOW) {
1112 			result = DDI_FAILURE;
1113 		} else {
1114 			result = DDI_SUCCESS;
1115 		}
1116 	}
1117 out:
1118 	pmcs_unlock_phy(pptr);
1119 	pmcs_release_scratch(pwp);
1120 	return (result);
1121 }
1122 
1123 static int
1124 pmcs_smp_init(dev_info_t *self, dev_info_t *child,
1125     smp_hba_tran_t *tran, smp_device_t *smp_sd)
1126 {
1127 	_NOTE(ARGUNUSED(tran, smp_sd));
1128 	pmcs_iport_t *iport;
1129 	pmcs_hw_t *pwp;
1130 	pmcs_xscsi_t *tgt;
1131 	pmcs_phy_t *phy, *pphy;
1132 	uint64_t wwn;
1133 	char *addr, *tgt_port;
1134 	int ua_form = 1;
1135 
1136 	iport = ddi_get_soft_state(pmcs_iport_softstate,
1137 	    ddi_get_instance(self));
1138 	ASSERT(iport);
1139 	if (iport == NULL)
1140 		return (DDI_FAILURE);
1141 	pwp = iport->pwp;
1142 	ASSERT(pwp);
1143 	if (pwp == NULL)
1144 		return (DDI_FAILURE);
1145 	pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL, "%s: %s", __func__,
1146 	    ddi_get_name(child));
1147 
1148 	/* Get "target-port" prop from devinfo node */
1149 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1150 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1151 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_SUCCESS) {
1152 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s: Failed to "
1153 		    "lookup prop ("SCSI_ADDR_PROP_TARGET_PORT")", __func__);
1154 		/* Dont fail _smp_init() because we couldnt get/set a prop */
1155 		return (DDI_SUCCESS);
1156 	}
1157 
1158 	/*
1159 	 * Validate that this tran_tgt_init is for an active iport.
1160 	 */
1161 	if (iport->ua_state == UA_INACTIVE) {
1162 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
1163 		    "%s: Init on inactive iport for '%s'", __func__, tgt_port);
1164 		ddi_prop_free(tgt_port);
1165 		return (DDI_FAILURE);
1166 	}
1167 
1168 	mutex_enter(&pwp->lock);
1169 
1170 	/* Retrieve softstate using unit-address */
1171 	tgt = pmcs_get_target(iport, tgt_port);
1172 	if (tgt == NULL) {
1173 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
1174 		    "%s: tgt softstate not found", __func__);
1175 		ddi_prop_free(tgt_port);
1176 		mutex_exit(&pwp->lock);
1177 		return (DDI_FAILURE);
1178 	}
1179 
1180 	phy = tgt->phy;
1181 	ASSERT(mutex_owned(&phy->phy_lock));
1182 
1183 	if (IS_ROOT_PHY(phy)) {
1184 		/* Expander attached to HBA - don't ref_count it */
1185 		wwn = pwp->sas_wwns[0];
1186 	} else {
1187 		pmcs_inc_phy_ref_count(phy);
1188 
1189 		/*
1190 		 * Parent (in topology) is also an expander
1191 		 * Now that we've increased the ref count on phy, it's OK
1192 		 * to drop the lock so we can acquire the parent's lock.
1193 		 */
1194 
1195 		pphy = phy->parent;
1196 		pmcs_unlock_phy(phy);
1197 		pmcs_lock_phy(pphy);
1198 		wwn = pmcs_barray2wwn(pphy->sas_address);
1199 		pmcs_unlock_phy(pphy);
1200 		pmcs_lock_phy(phy);
1201 	}
1202 
1203 	/*
1204 	 * If this is the 1st smp_init, add this to our list.
1205 	 */
1206 	if (tgt->target_num == PMCS_INVALID_TARGET_NUM) {
1207 		int target;
1208 		for (target = 0; target < pwp->max_dev; target++) {
1209 			if (pwp->targets[target] != NULL) {
1210 				continue;
1211 			}
1212 
1213 			pwp->targets[target] = tgt;
1214 			tgt->target_num = (uint16_t)target;
1215 			tgt->assigned = 1;
1216 			tgt->dev_state = PMCS_DEVICE_STATE_OPERATIONAL;
1217 			break;
1218 		}
1219 
1220 		if (target == pwp->max_dev) {
1221 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL,
1222 			    "Target list full.");
1223 			goto smp_init_fail;
1224 		}
1225 	}
1226 
1227 	if (!pmcs_assign_device(pwp, tgt)) {
1228 		pwp->targets[tgt->target_num] = NULL;
1229 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, tgt,
1230 		    "%s: pmcs_assign_device failed for target 0x%p",
1231 		    __func__, (void *)tgt);
1232 		goto smp_init_fail;
1233 	}
1234 
1235 	pmcs_unlock_phy(phy);
1236 	mutex_exit(&pwp->lock);
1237 
1238 	tgt->ref_count++;
1239 	tgt->dtype = phy->dtype;
1240 
1241 	addr = scsi_wwn_to_wwnstr(wwn, ua_form, NULL);
1242 	/* XXX: Update smp devinfo node using ndi_xxx */
1243 	if (ndi_prop_update_string(DDI_DEV_T_NONE, child,
1244 	    SCSI_ADDR_PROP_ATTACHED_PORT, addr) != DDI_SUCCESS) {
1245 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s: Failed to set "
1246 		    "prop ("SCSI_ADDR_PROP_ATTACHED_PORT")", __func__);
1247 	}
1248 	(void) scsi_free_wwnstr(addr);
1249 	ddi_prop_free(tgt_port);
1250 	return (DDI_SUCCESS);
1251 
1252 smp_init_fail:
1253 	tgt->phy = NULL;
1254 	tgt->target_num = PMCS_INVALID_TARGET_NUM;
1255 	phy->target = NULL;
1256 	if (!IS_ROOT_PHY(phy)) {
1257 		pmcs_dec_phy_ref_count(phy);
1258 	}
1259 	pmcs_unlock_phy(phy);
1260 	mutex_exit(&pwp->lock);
1261 	ddi_soft_state_bystr_free(iport->tgt_sstate, tgt->unit_address);
1262 	ddi_prop_free(tgt_port);
1263 	return (DDI_FAILURE);
1264 }
1265 
1266 static void
1267 pmcs_smp_free(dev_info_t *self, dev_info_t *child,
1268     smp_hba_tran_t *tran, smp_device_t *smp)
1269 {
1270 	_NOTE(ARGUNUSED(tran, smp));
1271 	pmcs_iport_t *iport;
1272 	pmcs_hw_t *pwp;
1273 	pmcs_xscsi_t *tgt;
1274 	char *tgt_port;
1275 
1276 	iport = ddi_get_soft_state(pmcs_iport_softstate,
1277 	    ddi_get_instance(self));
1278 	ASSERT(iport);
1279 	if (iport == NULL)
1280 		return;
1281 
1282 	pwp = iport->pwp;
1283 	if (pwp == NULL)
1284 		return;
1285 	ASSERT(pwp);
1286 	pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, NULL, "%s: %s", __func__,
1287 	    ddi_get_name(child));
1288 
1289 	/* Get "target-port" prop from devinfo node */
1290 	if (ddi_prop_lookup_string(DDI_DEV_T_ANY, child,
1291 	    DDI_PROP_DONTPASS | DDI_PROP_NOTPROM,
1292 	    SCSI_ADDR_PROP_TARGET_PORT, &tgt_port) != DDI_SUCCESS) {
1293 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s: Failed to "
1294 		    "lookup prop ("SCSI_ADDR_PROP_TARGET_PORT")", __func__);
1295 		return;
1296 	}
1297 	/* Retrieve softstate using unit-address */
1298 	tgt = ddi_soft_state_bystr_get(iport->tgt_sstate, tgt_port);
1299 	ddi_prop_free(tgt_port);
1300 
1301 	if (tgt == NULL) {
1302 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
1303 		    "%s: tgt softstate not found", __func__);
1304 		return;
1305 	}
1306 
1307 	mutex_enter(&pwp->lock);
1308 	mutex_enter(&tgt->statlock);
1309 	if (tgt->phy) {
1310 		if (!IS_ROOT_PHY(tgt->phy)) {
1311 			pmcs_dec_phy_ref_count(tgt->phy);
1312 		}
1313 	}
1314 
1315 	if (--tgt->ref_count == 0) {
1316 		/*
1317 		 * Remove this target from our list. The softstate
1318 		 * will remain, and the device will remain registered
1319 		 * with the hardware unless/until we're told that the
1320 		 * device physically went away.
1321 		 */
1322 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, NULL, tgt,
1323 		    "Removing target 0x%p (vtgt %d) from target list",
1324 		    (void *)tgt, tgt->target_num);
1325 		pwp->targets[tgt->target_num] = NULL;
1326 		tgt->target_num = PMCS_INVALID_TARGET_NUM;
1327 		tgt->phy->target = NULL;
1328 		tgt->phy = NULL;
1329 	}
1330 
1331 	mutex_exit(&tgt->statlock);
1332 	mutex_exit(&pwp->lock);
1333 }
1334 
1335 static int
1336 pmcs_scsi_quiesce(dev_info_t *dip)
1337 {
1338 	pmcs_hw_t *pwp;
1339 	int totactive = -1;
1340 	pmcs_xscsi_t *xp;
1341 	uint16_t target;
1342 
1343 	if (ddi_get_soft_state(pmcs_iport_softstate, ddi_get_instance(dip)))
1344 		return (0);		/* iport */
1345 
1346 	pwp  = ddi_get_soft_state(pmcs_softc_state, ddi_get_instance(dip));
1347 	if (pwp == NULL) {
1348 		return (-1);
1349 	}
1350 	mutex_enter(&pwp->lock);
1351 	if (pwp->state != STATE_RUNNING) {
1352 		mutex_exit(&pwp->lock);
1353 		return (-1);
1354 	}
1355 
1356 	pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s called", __func__);
1357 	pwp->blocked = 1;
1358 	while (totactive) {
1359 		totactive = 0;
1360 		for (target = 0; target < pwp->max_dev; target++) {
1361 			xp = pwp->targets[target];
1362 			if (xp == NULL) {
1363 				continue;
1364 			}
1365 			mutex_enter(&xp->statlock);
1366 			if (xp->actv_cnt) {
1367 				totactive += xp->actv_cnt;
1368 				xp->draining = 1;
1369 			}
1370 			mutex_exit(&xp->statlock);
1371 		}
1372 		if (totactive) {
1373 			cv_wait(&pwp->drain_cv, &pwp->lock);
1374 		}
1375 		/*
1376 		 * The pwp->blocked may have been reset. e.g a SCSI bus reset
1377 		 */
1378 		pwp->blocked = 1;
1379 	}
1380 
1381 	for (target = 0; target < pwp->max_dev; target++) {
1382 		xp = pwp->targets[target];
1383 		if (xp == NULL) {
1384 			continue;
1385 		}
1386 		mutex_enter(&xp->statlock);
1387 		xp->draining = 0;
1388 		mutex_exit(&xp->statlock);
1389 	}
1390 
1391 	mutex_exit(&pwp->lock);
1392 	if (totactive == 0) {
1393 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
1394 		    "%s drain complete", __func__);
1395 	}
1396 	return (0);
1397 }
1398 
1399 static int
1400 pmcs_scsi_unquiesce(dev_info_t *dip)
1401 {
1402 	pmcs_hw_t *pwp;
1403 
1404 	if (ddi_get_soft_state(pmcs_iport_softstate, ddi_get_instance(dip)))
1405 		return (0);		/* iport */
1406 
1407 	pwp  = ddi_get_soft_state(pmcs_softc_state, ddi_get_instance(dip));
1408 	if (pwp == NULL) {
1409 		return (-1);
1410 	}
1411 	mutex_enter(&pwp->lock);
1412 	if (pwp->state != STATE_RUNNING) {
1413 		mutex_exit(&pwp->lock);
1414 		return (-1);
1415 	}
1416 	pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL, "%s called", __func__);
1417 	pwp->blocked = 0;
1418 	mutex_exit(&pwp->lock);
1419 
1420 	/*
1421 	 * Run all pending commands.
1422 	 */
1423 	pmcs_scsa_wq_run(pwp);
1424 
1425 	/*
1426 	 * Complete all completed commands.
1427 	 * This also unlocks us.
1428 	 */
1429 	PMCS_CQ_RUN(pwp);
1430 	return (0);
1431 }
1432 
1433 /*
1434  * Start commands for a particular device
1435  * If the actual start of a command fails, return B_FALSE.  Any other result
1436  * is a B_TRUE return.
1437  */
1438 boolean_t
1439 pmcs_scsa_wq_run_one(pmcs_hw_t *pwp, pmcs_xscsi_t *xp)
1440 {
1441 	pmcs_cmd_t *sp;
1442 	pmcs_phy_t *phyp;
1443 	pmcwork_t *pwrk;
1444 	boolean_t run_one, blocked;
1445 	int rval;
1446 
1447 	/*
1448 	 * First, check to see if we're blocked or resource limited
1449 	 */
1450 	mutex_enter(&pwp->lock);
1451 	blocked = pwp->blocked;
1452 	/*
1453 	 * If resource_limited is set, we're resource constrained and
1454 	 * we will run only one work request for this target.
1455 	 */
1456 	run_one = pwp->resource_limited;
1457 	mutex_exit(&pwp->lock);
1458 
1459 	if (blocked) {
1460 		/* Queues will get restarted when we get unblocked */
1461 		return (B_TRUE);
1462 	}
1463 
1464 	/*
1465 	 * Might as well verify the queue is not empty before moving on
1466 	 */
1467 	mutex_enter(&xp->wqlock);
1468 	if (STAILQ_EMPTY(&xp->wq)) {
1469 		mutex_exit(&xp->wqlock);
1470 		return (B_TRUE);
1471 	}
1472 	mutex_exit(&xp->wqlock);
1473 
1474 	/*
1475 	 * If we're draining or resetting, just reschedule work queue and bail.
1476 	 */
1477 	mutex_enter(&xp->statlock);
1478 	if (xp->draining || xp->resetting || xp->special_running ||
1479 	    xp->special_needed) {
1480 		mutex_exit(&xp->statlock);
1481 		SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
1482 		return (B_TRUE);
1483 	}
1484 
1485 	/*
1486 	 * Next, check to see if the target is gone.
1487 	 */
1488 	if (xp->dev_gone) {
1489 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
1490 		    "%s: Flushing wait queue for dead tgt 0x%p", __func__,
1491 		    (void *)xp);
1492 		pmcs_flush_target_queues(pwp, xp, PMCS_TGT_WAIT_QUEUE);
1493 		mutex_exit(&xp->statlock);
1494 		return (B_TRUE);
1495 	}
1496 
1497 	/*
1498 	 * Increment the PHY's ref_count now so we know it won't go away
1499 	 * after we drop the target lock.  Drop it before returning.  If the
1500 	 * PHY dies, the commands we attempt to send will fail, but at least
1501 	 * we know we have a real PHY pointer.
1502 	 */
1503 	phyp = xp->phy;
1504 	pmcs_inc_phy_ref_count(phyp);
1505 	mutex_exit(&xp->statlock);
1506 
1507 	mutex_enter(&xp->wqlock);
1508 	while ((sp = STAILQ_FIRST(&xp->wq)) != NULL) {
1509 		pwrk = pmcs_gwork(pwp, PMCS_TAG_TYPE_CBACK, phyp);
1510 		if (pwrk == NULL) {
1511 			pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, NULL,
1512 			    "%s: out of work structures", __func__);
1513 			SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
1514 			break;
1515 		}
1516 		STAILQ_REMOVE_HEAD(&xp->wq, cmd_next);
1517 		mutex_exit(&xp->wqlock);
1518 
1519 		pwrk->xp = xp;
1520 		pwrk->arg = sp;
1521 		sp->cmd_tag = pwrk->htag;
1522 		pwrk->timer = US2WT(CMD2PKT(sp)->pkt_time * 1000000);
1523 		if (pwrk->timer == 0) {
1524 			pwrk->timer = US2WT(1000000);
1525 		}
1526 
1527 		pwrk->dtype = xp->dtype;
1528 
1529 		if (xp->dtype == SAS) {
1530 			pwrk->ptr = (void *) pmcs_SAS_done;
1531 			if ((rval = pmcs_SAS_run(sp, pwrk)) != 0) {
1532 				sp->cmd_tag = NULL;
1533 				pmcs_dec_phy_ref_count(phyp);
1534 				pmcs_pwork(pwp, pwrk);
1535 				SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
1536 				if (rval == PMCS_WQ_RUN_FAIL_RES) {
1537 					return (B_FALSE);
1538 				} else {
1539 					return (B_TRUE);
1540 				}
1541 			}
1542 		} else {
1543 			ASSERT(xp->dtype == SATA);
1544 			pwrk->ptr = (void *) pmcs_SATA_done;
1545 			if ((rval = pmcs_SATA_run(sp, pwrk)) != 0) {
1546 				sp->cmd_tag = NULL;
1547 				pmcs_dec_phy_ref_count(phyp);
1548 				pmcs_pwork(pwp, pwrk);
1549 				SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
1550 				if (rval == PMCS_WQ_RUN_FAIL_RES) {
1551 					return (B_FALSE);
1552 				} else {
1553 					return (B_TRUE);
1554 				}
1555 			}
1556 		}
1557 
1558 		if (run_one) {
1559 			goto wq_out;
1560 		}
1561 		mutex_enter(&xp->wqlock);
1562 	}
1563 
1564 	mutex_exit(&xp->wqlock);
1565 
1566 wq_out:
1567 	pmcs_dec_phy_ref_count(phyp);
1568 	return (B_TRUE);
1569 }
1570 
1571 /*
1572  * Start commands for all devices.
1573  */
1574 void
1575 pmcs_scsa_wq_run(pmcs_hw_t *pwp)
1576 {
1577 	pmcs_xscsi_t *xp;
1578 	uint16_t target_start, target;
1579 	boolean_t	rval = B_TRUE;
1580 
1581 	mutex_enter(&pwp->lock);
1582 	target_start = pwp->last_wq_dev;
1583 	target = target_start;
1584 
1585 	do {
1586 		xp = pwp->targets[target];
1587 		if (xp == NULL) {
1588 			if (++target == pwp->max_dev) {
1589 				target = 0;
1590 			}
1591 			continue;
1592 		}
1593 
1594 		mutex_exit(&pwp->lock);
1595 		rval = pmcs_scsa_wq_run_one(pwp, xp);
1596 		if (rval == B_FALSE) {
1597 			mutex_enter(&pwp->lock);
1598 			break;
1599 		}
1600 		mutex_enter(&pwp->lock);
1601 		if (++target == pwp->max_dev) {
1602 			target = 0;
1603 		}
1604 	} while (target != target_start);
1605 
1606 	if (rval) {
1607 		pwp->resource_limited = 0; /* Not resource-constrained */
1608 	} else {
1609 		pwp->resource_limited = 1; /* Give others a chance */
1610 	}
1611 
1612 	pwp->last_wq_dev = target;
1613 	mutex_exit(&pwp->lock);
1614 }
1615 
1616 /*
1617  * Pull the completion queue, drop the lock and complete all elements.
1618  */
1619 
1620 void
1621 pmcs_scsa_cq_run(void *arg)
1622 {
1623 	pmcs_cq_thr_info_t *cqti = (pmcs_cq_thr_info_t *)arg;
1624 	pmcs_hw_t *pwp = cqti->cq_pwp;
1625 	pmcs_cmd_t *sp, *nxt;
1626 	struct scsi_pkt *pkt;
1627 	pmcs_iocomp_cb_t *ioccb, *ioccb_next;
1628 	pmcs_cb_t callback;
1629 	uint32_t niodone;
1630 
1631 	DTRACE_PROBE1(pmcs__scsa__cq__run__start, pmcs_cq_thr_info_t *, cqti);
1632 
1633 	mutex_enter(&pwp->cq_lock);
1634 
1635 	while (!pwp->cq_info.cq_stop) {
1636 		/*
1637 		 * First, check the I/O completion callback queue.
1638 		 */
1639 
1640 		ioccb = pwp->iocomp_cb_head;
1641 		pwp->iocomp_cb_head = NULL;
1642 		pwp->iocomp_cb_tail = NULL;
1643 		mutex_exit(&pwp->cq_lock);
1644 
1645 		niodone = 0;
1646 
1647 		while (ioccb) {
1648 			niodone++;
1649 			/*
1650 			 * Grab the lock on the work structure. The callback
1651 			 * routine is responsible for clearing it.
1652 			 */
1653 			mutex_enter(&ioccb->pwrk->lock);
1654 			ioccb_next = ioccb->next;
1655 			callback = (pmcs_cb_t)ioccb->pwrk->ptr;
1656 			(*callback)(pwp, ioccb->pwrk,
1657 			    (uint32_t *)((void *)ioccb->iomb));
1658 			kmem_cache_free(pwp->iocomp_cb_cache, ioccb);
1659 			ioccb = ioccb_next;
1660 		}
1661 
1662 		/*
1663 		 * Next, run the completion queue
1664 		 */
1665 
1666 		mutex_enter(&pwp->cq_lock);
1667 		sp = STAILQ_FIRST(&pwp->cq);
1668 		STAILQ_INIT(&pwp->cq);
1669 		mutex_exit(&pwp->cq_lock);
1670 
1671 		DTRACE_PROBE1(pmcs__scsa__cq__run__start__loop,
1672 		    pmcs_cq_thr_info_t *, cqti);
1673 
1674 		if (sp && pmcs_check_acc_dma_handle(pwp)) {
1675 			ddi_fm_service_impact(pwp->dip, DDI_SERVICE_UNAFFECTED);
1676 		}
1677 
1678 		while (sp) {
1679 			nxt = STAILQ_NEXT(sp, cmd_next);
1680 			pkt = CMD2PKT(sp);
1681 			pmcs_prt(pwp, PMCS_PRT_DEBUG3, NULL, sp->cmd_target,
1682 			    "%s: calling completion on %p for tgt %p", __func__,
1683 			    (void *)sp, (void *)sp->cmd_target);
1684 			scsi_hba_pkt_comp(pkt);
1685 			sp = nxt;
1686 		}
1687 
1688 		DTRACE_PROBE1(pmcs__scsa__cq__run__end__loop,
1689 		    pmcs_cq_thr_info_t *, cqti);
1690 
1691 		mutex_enter(&cqti->cq_thr_lock);
1692 		cv_wait(&cqti->cq_cv, &cqti->cq_thr_lock);
1693 		mutex_exit(&cqti->cq_thr_lock);
1694 
1695 		mutex_enter(&pwp->cq_lock);
1696 	}
1697 
1698 	mutex_exit(&pwp->cq_lock);
1699 	DTRACE_PROBE1(pmcs__scsa__cq__run__stop, pmcs_cq_thr_info_t *, cqti);
1700 	thread_exit();
1701 }
1702 
1703 /*
1704  * Run a SAS command.  Called with pwrk->lock held, returns unlocked.
1705  */
1706 static int
1707 pmcs_SAS_run(pmcs_cmd_t *sp, pmcwork_t *pwrk)
1708 {
1709 	pmcs_hw_t *pwp = CMD2PMC(sp);
1710 	struct scsi_pkt *pkt = CMD2PKT(sp);
1711 	pmcs_xscsi_t *xp = pwrk->xp;
1712 	uint32_t iq, *ptr;
1713 	sas_ssp_cmd_iu_t sc;
1714 
1715 	mutex_enter(&xp->statlock);
1716 	if (!xp->assigned) {
1717 		mutex_exit(&xp->statlock);
1718 		return (PMCS_WQ_RUN_FAIL_OTHER);
1719 	}
1720 	if ((xp->actv_cnt >= xp->qdepth) || xp->recover_wait) {
1721 		mutex_exit(&xp->statlock);
1722 		mutex_enter(&xp->wqlock);
1723 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
1724 		mutex_exit(&xp->wqlock);
1725 		return (PMCS_WQ_RUN_FAIL_OTHER);
1726 	}
1727 	GET_IO_IQ_ENTRY(pwp, ptr, pwrk->phy->device_id, iq);
1728 	if (ptr == NULL) {
1729 		mutex_exit(&xp->statlock);
1730 		/*
1731 		 * This is a temporary failure not likely to unblocked by
1732 		 * commands completing as the test for scheduling the
1733 		 * restart of work is a per-device test.
1734 		 */
1735 		mutex_enter(&xp->wqlock);
1736 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
1737 		mutex_exit(&xp->wqlock);
1738 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
1739 		    "%s: Failed to get IO IQ entry for tgt %d",
1740 		    __func__, xp->target_num);
1741 		return (PMCS_WQ_RUN_FAIL_RES);
1742 
1743 	}
1744 
1745 	ptr[0] =
1746 	    LE_32(PMCS_IOMB_IN_SAS(PMCS_OQ_IODONE, PMCIN_SSP_INI_IO_START));
1747 	ptr[1] = LE_32(pwrk->htag);
1748 	ptr[2] = LE_32(pwrk->phy->device_id);
1749 	ptr[3] = LE_32(pkt->pkt_dma_len);
1750 	if (ptr[3]) {
1751 		ASSERT(pkt->pkt_numcookies);
1752 		if (pkt->pkt_dma_flags & DDI_DMA_READ) {
1753 			ptr[4] = LE_32(PMCIN_DATADIR_2_INI);
1754 		} else {
1755 			ptr[4] = LE_32(PMCIN_DATADIR_2_DEV);
1756 		}
1757 		if (pmcs_dma_load(pwp, sp, ptr)) {
1758 			mutex_exit(&pwp->iqp_lock[iq]);
1759 			mutex_exit(&xp->statlock);
1760 			mutex_enter(&xp->wqlock);
1761 			if (STAILQ_EMPTY(&xp->wq)) {
1762 				STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
1763 				mutex_exit(&xp->wqlock);
1764 			} else {
1765 				mutex_exit(&xp->wqlock);
1766 				CMD2PKT(sp)->pkt_scbp[0] = STATUS_QFULL;
1767 				CMD2PKT(sp)->pkt_reason = CMD_CMPLT;
1768 				CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS |
1769 				    STATE_GOT_TARGET | STATE_SENT_CMD |
1770 				    STATE_GOT_STATUS;
1771 				mutex_enter(&pwp->cq_lock);
1772 				STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
1773 				mutex_exit(&pwp->cq_lock);
1774 				pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
1775 				    "%s: Failed to dma_load for tgt %d (QF)",
1776 				    __func__, xp->target_num);
1777 			}
1778 			return (PMCS_WQ_RUN_FAIL_RES);
1779 		}
1780 	} else {
1781 		ptr[4] = LE_32(PMCIN_DATADIR_NONE);
1782 		CLEAN_MESSAGE(ptr, 12);
1783 	}
1784 	xp->actv_cnt++;
1785 	if (xp->actv_cnt > xp->maxdepth) {
1786 		xp->maxdepth = xp->actv_cnt;
1787 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, pwrk->phy, xp, "%s: max depth "
1788 		    "now %u", pwrk->phy->path, xp->maxdepth);
1789 	}
1790 	mutex_exit(&xp->statlock);
1791 
1792 
1793 #ifdef	DEBUG
1794 	/*
1795 	 * Generate a PMCOUT_STATUS_XFER_CMD_FRAME_ISSUED
1796 	 * event when this goes out on the wire.
1797 	 */
1798 	ptr[4] |= PMCIN_MESSAGE_REPORT;
1799 #endif
1800 	/*
1801 	 * Fill in the SSP IU
1802 	 */
1803 
1804 	bzero(&sc, sizeof (sas_ssp_cmd_iu_t));
1805 	bcopy((uint8_t *)&sp->cmd_lun->scsi_lun, sc.lun, sizeof (scsi_lun_t));
1806 
1807 	switch (pkt->pkt_flags & FLAG_TAGMASK) {
1808 	case FLAG_HTAG:
1809 		sc.task_attribute = SAS_CMD_TASK_ATTR_HEAD;
1810 		break;
1811 	case FLAG_OTAG:
1812 		sc.task_attribute = SAS_CMD_TASK_ATTR_ORDERED;
1813 		break;
1814 	case FLAG_STAG:
1815 	default:
1816 		sc.task_attribute = SAS_CMD_TASK_ATTR_SIMPLE;
1817 		break;
1818 	}
1819 	(void) memcpy(sc.cdb, pkt->pkt_cdbp,
1820 	    min(SCSA_CDBLEN(sp), sizeof (sc.cdb)));
1821 	(void) memcpy(&ptr[5], &sc, sizeof (sas_ssp_cmd_iu_t));
1822 	pwrk->state = PMCS_WORK_STATE_ONCHIP;
1823 	mutex_exit(&pwrk->lock);
1824 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL,
1825 	    "%s: giving pkt %p (tag %x) to the hardware", __func__,
1826 	    (void *)pkt, pwrk->htag);
1827 #ifdef DEBUG
1828 	pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "SAS INI Message", ptr);
1829 #endif
1830 	mutex_enter(&xp->aqlock);
1831 	STAILQ_INSERT_TAIL(&xp->aq, sp, cmd_next);
1832 	mutex_exit(&xp->aqlock);
1833 	INC_IQ_ENTRY(pwp, iq);
1834 
1835 	/*
1836 	 * If we just submitted the last command queued from device state
1837 	 * recovery, clear the wq_recovery_tail pointer.
1838 	 */
1839 	mutex_enter(&xp->wqlock);
1840 	if (xp->wq_recovery_tail == sp) {
1841 		xp->wq_recovery_tail = NULL;
1842 	}
1843 	mutex_exit(&xp->wqlock);
1844 
1845 	return (PMCS_WQ_RUN_SUCCESS);
1846 }
1847 
1848 /*
1849  * Complete a SAS command
1850  *
1851  * Called with pwrk lock held.
1852  * The free of pwrk releases the lock.
1853  */
1854 
1855 static void
1856 pmcs_SAS_done(pmcs_hw_t *pwp, pmcwork_t *pwrk, uint32_t *msg)
1857 {
1858 	pmcs_cmd_t *sp = pwrk->arg;
1859 	pmcs_phy_t *pptr = pwrk->phy;
1860 	pmcs_xscsi_t *xp = pwrk->xp;
1861 	struct scsi_pkt *pkt = CMD2PKT(sp);
1862 	int dead;
1863 	uint32_t sts;
1864 	boolean_t aborted = B_FALSE;
1865 	boolean_t do_ds_recovery = B_FALSE;
1866 
1867 	ASSERT(xp != NULL);
1868 	ASSERT(sp != NULL);
1869 	ASSERT(pptr != NULL);
1870 
1871 	DTRACE_PROBE4(pmcs__io__done, uint64_t, pkt->pkt_dma_len, int,
1872 	    (pkt->pkt_dma_flags & DDI_DMA_READ) != 0, hrtime_t, pwrk->start,
1873 	    hrtime_t, gethrtime());
1874 
1875 	dead = pwrk->dead;
1876 
1877 	if (msg) {
1878 		sts = LE_32(msg[2]);
1879 	} else {
1880 		sts = 0;
1881 	}
1882 
1883 	if (dead != 0) {
1884 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, "%s: dead cmd tag "
1885 		    "0x%x for %s", __func__, pwrk->htag, pptr->path);
1886 		goto out;
1887 	}
1888 
1889 	if (sts == PMCOUT_STATUS_ABORTED) {
1890 		aborted = B_TRUE;
1891 	}
1892 
1893 	if (pwrk->state == PMCS_WORK_STATE_TIMED_OUT) {
1894 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
1895 		    "%s: cmd 0x%p (tag 0x%x) timed out for %s",
1896 		    __func__, (void *)sp, pwrk->htag, pptr->path);
1897 		goto out;
1898 	}
1899 
1900 	/*
1901 	 * If the status isn't okay but not underflow,
1902 	 * step to the side and parse the (possible) error.
1903 	 */
1904 #ifdef DEBUG
1905 	if (msg) {
1906 		pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "Outbound Message", msg);
1907 	}
1908 #endif
1909 	if (!msg) {
1910 		goto out;
1911 	}
1912 
1913 	switch (sts) {
1914 	case PMCOUT_STATUS_OPEN_CNX_ERROR_IT_NEXUS_LOSS:
1915 	case PMCOUT_STATUS_IO_DS_NON_OPERATIONAL:
1916 	case PMCOUT_STATUS_IO_DS_IN_RECOVERY:
1917 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
1918 		    "%s: PHY %s requires device state recovery (status=%d)",
1919 		    __func__, pptr->path, sts);
1920 		do_ds_recovery = B_TRUE;
1921 		break;
1922 	case PMCOUT_STATUS_UNDERFLOW:
1923 		(void) pmcs_set_resid(pkt, pkt->pkt_dma_len, LE_32(msg[3]));
1924 		pmcs_prt(pwp, PMCS_PRT_DEBUG_UNDERFLOW, NULL, NULL,
1925 		    "%s: underflow %u for cdb 0x%x",
1926 		    __func__, LE_32(msg[3]), pkt->pkt_cdbp[0] & 0xff);
1927 		sts = PMCOUT_STATUS_OK;
1928 		msg[3] = 0;
1929 		break;
1930 	case PMCOUT_STATUS_OK:
1931 		pkt->pkt_resid = 0;
1932 		break;
1933 	}
1934 
1935 	if (sts != PMCOUT_STATUS_OK) {
1936 		pmcs_ioerror(pwp, SAS, pwrk, msg);
1937 	} else {
1938 		if (msg[3]) {
1939 			uint8_t local[PMCS_QENTRY_SIZE << 1], *xd;
1940 			sas_ssp_rsp_iu_t *rptr = (void *)local;
1941 			const int lim =
1942 			    (PMCS_QENTRY_SIZE << 1) - SAS_RSP_HDR_SIZE;
1943 			static const uint8_t ssp_rsp_evec[] = {
1944 				0x58, 0x61, 0x56, 0x72, 0x00
1945 			};
1946 
1947 			/*
1948 			 * Transform the the first part of the response
1949 			 * to host canonical form. This gives us enough
1950 			 * information to figure out what to do with the
1951 			 * rest (which remains unchanged in the incoming
1952 			 * message which can be up to two queue entries
1953 			 * in length).
1954 			 */
1955 			pmcs_endian_transform(pwp, local, &msg[5],
1956 			    ssp_rsp_evec);
1957 			xd = (uint8_t *)(&msg[5]);
1958 			xd += SAS_RSP_HDR_SIZE;
1959 
1960 			if (rptr->datapres == SAS_RSP_DATAPRES_RESPONSE_DATA) {
1961 				if (rptr->response_data_length != 4) {
1962 					pmcs_print_entry(pwp, PMCS_PRT_DEBUG,
1963 					    "Bad SAS RESPONSE DATA LENGTH",
1964 					    msg);
1965 					pkt->pkt_reason = CMD_TRAN_ERR;
1966 					goto out;
1967 				}
1968 				(void) memcpy(&sts, xd, sizeof (uint32_t));
1969 				sts = BE_32(sts);
1970 				/*
1971 				 * The only response code we should legally get
1972 				 * here is an INVALID FRAME response code.
1973 				 */
1974 				if (sts == SAS_RSP_INVALID_FRAME) {
1975 					pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
1976 					    "%s: pkt %p tgt %u path %s "
1977 					    "completed: INVALID FRAME response",
1978 					    __func__, (void *)pkt,
1979 					    xp->target_num, pptr->path);
1980 				} else {
1981 					pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
1982 					    "%s: pkt %p tgt %u path %s "
1983 					    "completed: illegal response 0x%x",
1984 					    __func__, (void *)pkt,
1985 					    xp->target_num, pptr->path, sts);
1986 				}
1987 				pkt->pkt_reason = CMD_TRAN_ERR;
1988 				goto out;
1989 			}
1990 			if (rptr->datapres == SAS_RSP_DATAPRES_SENSE_DATA) {
1991 				uint32_t slen;
1992 				slen = rptr->sense_data_length;
1993 				if (slen > lim) {
1994 					slen = lim;
1995 				}
1996 				pmcs_latch_status(pwp, sp, rptr->status, xd,
1997 				    slen, pptr->path);
1998 			} else if (rptr->datapres == SAS_RSP_DATAPRES_NO_DATA) {
1999 				pmcout_ssp_comp_t *sspcp;
2000 				sspcp = (pmcout_ssp_comp_t *)msg;
2001 				uint32_t *residp;
2002 				/*
2003 				 * This is the case for a plain SCSI status.
2004 				 * Note: If RESC_V is set and we're here, there
2005 				 * is a residual.  We need to find it and update
2006 				 * the packet accordingly.
2007 				 */
2008 				pmcs_latch_status(pwp, sp, rptr->status, NULL,
2009 				    0, pptr->path);
2010 
2011 				if (sspcp->resc_v) {
2012 					/*
2013 					 * Point residual to the SSP_RESP_IU
2014 					 */
2015 					residp = (uint32_t *)(sspcp + 1);
2016 					/*
2017 					 * param contains the number of bytes
2018 					 * between where the SSP_RESP_IU may
2019 					 * or may not be and the residual.
2020 					 * Increment residp by the appropriate
2021 					 * number of words: (param+resc_pad)/4).
2022 					 */
2023 					residp += (LE_32(sspcp->param) +
2024 					    sspcp->resc_pad) /
2025 					    sizeof (uint32_t);
2026 					pmcs_prt(pwp, PMCS_PRT_DEBUG_UNDERFLOW,
2027 					    pptr, xp, "%s: tgt 0x%p "
2028 					    "residual %d for pkt 0x%p",
2029 					    __func__, (void *) xp, *residp,
2030 					    (void *) pkt);
2031 					ASSERT(LE_32(*residp) <=
2032 					    pkt->pkt_dma_len);
2033 					(void) pmcs_set_resid(pkt,
2034 					    pkt->pkt_dma_len, LE_32(*residp));
2035 				}
2036 			} else {
2037 				pmcs_print_entry(pwp, PMCS_PRT_DEBUG,
2038 				    "illegal SAS response", msg);
2039 				pkt->pkt_reason = CMD_TRAN_ERR;
2040 				goto out;
2041 			}
2042 		} else {
2043 			pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0,
2044 			    pptr->path);
2045 		}
2046 		if (pkt->pkt_dma_len) {
2047 			pkt->pkt_state |= STATE_XFERRED_DATA;
2048 		}
2049 	}
2050 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp,
2051 	    "%s: pkt %p tgt %u done reason=%x state=%x resid=%ld status=%x",
2052 	    __func__, (void *)pkt, xp->target_num, pkt->pkt_reason,
2053 	    pkt->pkt_state, pkt->pkt_resid, pkt->pkt_scbp[0]);
2054 
2055 	if (pwrk->state == PMCS_WORK_STATE_ABORTED) {
2056 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2057 		    "%s: scsi_pkt 0x%p aborted for PHY %s; work = 0x%p",
2058 		    __func__, (void *)pkt, pptr->path, (void *)pwrk);
2059 		aborted = B_TRUE;
2060 	}
2061 
2062 out:
2063 	pmcs_pwork(pwp, pwrk);
2064 	pmcs_dma_unload(pwp, sp);
2065 
2066 	mutex_enter(&xp->statlock);
2067 	if (xp->dev_gone) {
2068 		mutex_exit(&xp->statlock);
2069 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp,
2070 		    "%s: Completing command for dead target 0x%p", __func__,
2071 		    (void *)xp);
2072 		return;
2073 	}
2074 
2075 	ASSERT(xp->actv_cnt > 0);
2076 	if (--(xp->actv_cnt) == 0) {
2077 		if (xp->draining) {
2078 			pmcs_prt(pwp, PMCS_PRT_DEBUG1, pptr, xp,
2079 			    "%s: waking up drain waiters", __func__);
2080 			cv_signal(&pwp->drain_cv);
2081 		}
2082 	}
2083 	mutex_exit(&xp->statlock);
2084 	if (dead == 0) {
2085 #ifdef	DEBUG
2086 		pmcs_cmd_t *wp;
2087 		mutex_enter(&xp->aqlock);
2088 		STAILQ_FOREACH(wp, &xp->aq, cmd_next) {
2089 			if (wp == sp) {
2090 				break;
2091 			}
2092 		}
2093 		ASSERT(wp != NULL);
2094 #else
2095 		mutex_enter(&xp->aqlock);
2096 #endif
2097 		STAILQ_REMOVE(&xp->aq, sp, pmcs_cmd, cmd_next);
2098 		if (aborted) {
2099 			pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2100 			    "%s: Aborted cmd for tgt 0x%p, signaling waiters",
2101 			    __func__, (void *)xp);
2102 			cv_signal(&xp->abort_cv);
2103 		}
2104 		mutex_exit(&xp->aqlock);
2105 	}
2106 
2107 	/*
2108 	 * If do_ds_recovery is set, we need to initiate device state
2109 	 * recovery.  In this case, we put this I/O back on the head of
2110 	 * the wait queue to run again after recovery is complete
2111 	 */
2112 	if (do_ds_recovery) {
2113 		mutex_enter(&xp->statlock);
2114 		pmcs_start_dev_state_recovery(xp, pptr);
2115 		mutex_exit(&xp->statlock);
2116 		pmcs_prt(pwp, PMCS_PRT_DEBUG1, pptr, xp, "%s: Putting cmd 0x%p "
2117 		    "back on wq during recovery for tgt 0x%p", __func__,
2118 		    (void *)sp, (void *)xp);
2119 		mutex_enter(&xp->wqlock);
2120 		if (xp->wq_recovery_tail == NULL) {
2121 			STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2122 		} else {
2123 			/*
2124 			 * If there are other I/Os waiting at the head due to
2125 			 * device state recovery, add this one in the right spot
2126 			 * to maintain proper order.
2127 			 */
2128 			STAILQ_INSERT_AFTER(&xp->wq, xp->wq_recovery_tail, sp,
2129 			    cmd_next);
2130 		}
2131 		xp->wq_recovery_tail = sp;
2132 		mutex_exit(&xp->wqlock);
2133 	} else {
2134 		/*
2135 		 * If we're not initiating device state recovery and this
2136 		 * command was not "dead", put it on the completion queue
2137 		 */
2138 		if (!dead) {
2139 			mutex_enter(&pwp->cq_lock);
2140 			STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
2141 			mutex_exit(&pwp->cq_lock);
2142 		}
2143 	}
2144 }
2145 
2146 /*
2147  * Run a SATA command (normal reads and writes),
2148  * or block and schedule a SATL interpretation
2149  * of the command.
2150  *
2151  * Called with pwrk lock held, returns unlocked.
2152  */
2153 
2154 static int
2155 pmcs_SATA_run(pmcs_cmd_t *sp, pmcwork_t *pwrk)
2156 {
2157 	pmcs_hw_t *pwp = CMD2PMC(sp);
2158 	struct scsi_pkt *pkt = CMD2PKT(sp);
2159 	pmcs_xscsi_t *xp;
2160 	uint8_t cdb_base, asc, tag;
2161 	uint32_t *ptr, iq, nblk, i, mtype;
2162 	fis_t fis;
2163 	size_t amt;
2164 	uint64_t lba;
2165 
2166 	xp = pwrk->xp;
2167 
2168 	/*
2169 	 * First, see if this is just a plain read/write command.
2170 	 * If not, we have to queue it up for processing, block
2171 	 * any additional commands from coming in, and wake up
2172 	 * the thread that will process this command.
2173 	 */
2174 	cdb_base = pkt->pkt_cdbp[0] & 0x1f;
2175 	if (cdb_base != SCMD_READ && cdb_base != SCMD_WRITE) {
2176 		pmcs_prt(pwp, PMCS_PRT_DEBUG1, NULL, NULL,
2177 		    "%s: special SATA cmd %p", __func__, (void *)sp);
2178 
2179 		ASSERT(xp->phy != NULL);
2180 		pmcs_pwork(pwp, pwrk);
2181 		pmcs_lock_phy(xp->phy);
2182 		mutex_enter(&xp->statlock);
2183 		xp->special_needed = 1; /* Set the special_needed flag */
2184 		STAILQ_INSERT_TAIL(&xp->sq, sp, cmd_next);
2185 		if (pmcs_run_sata_special(pwp, xp)) {
2186 			SCHEDULE_WORK(pwp, PMCS_WORK_SATA_RUN);
2187 		}
2188 		mutex_exit(&xp->statlock);
2189 		pmcs_unlock_phy(xp->phy);
2190 
2191 		return (PMCS_WQ_RUN_SUCCESS);
2192 	}
2193 
2194 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL, "%s: regular cmd", __func__);
2195 
2196 	mutex_enter(&xp->statlock);
2197 	if (!xp->assigned) {
2198 		mutex_exit(&xp->statlock);
2199 		return (PMCS_WQ_RUN_FAIL_OTHER);
2200 	}
2201 	if (xp->special_running || xp->special_needed || xp->recover_wait) {
2202 		mutex_exit(&xp->statlock);
2203 		mutex_enter(&xp->wqlock);
2204 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2205 		mutex_exit(&xp->wqlock);
2206 		/*
2207 		 * By the time we get here the special
2208 		 * commands running or waiting to be run
2209 		 * may have come and gone, so kick our
2210 		 * worker to run the waiting queues
2211 		 * just in case.
2212 		 */
2213 		SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
2214 		return (PMCS_WQ_RUN_FAIL_OTHER);
2215 	}
2216 	lba = xp->capacity;
2217 	mutex_exit(&xp->statlock);
2218 
2219 	/*
2220 	 * Extract data length and lba parameters out of the command. The
2221 	 * function pmcs_SATA_rwparm returns a non-zero ASC value if the CDB
2222 	 * values are considered illegal.
2223 	 */
2224 	asc = pmcs_SATA_rwparm(pkt->pkt_cdbp, &nblk, &lba, lba);
2225 	if (asc) {
2226 		uint8_t sns[18];
2227 		bzero(sns, sizeof (sns));
2228 		sns[0] = 0xf0;
2229 		sns[2] = 0x5;
2230 		sns[12] = asc;
2231 		pmcs_latch_status(pwp, sp, STATUS_CHECK, sns, sizeof (sns),
2232 		    pwrk->phy->path);
2233 		pmcs_pwork(pwp, pwrk);
2234 		mutex_enter(&pwp->cq_lock);
2235 		STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
2236 		PMCS_CQ_RUN_LOCKED(pwp);
2237 		mutex_exit(&pwp->cq_lock);
2238 		return (PMCS_WQ_RUN_SUCCESS);
2239 	}
2240 
2241 	/*
2242 	 * If the command decodes as not moving any data, complete it here.
2243 	 */
2244 	amt = nblk;
2245 	amt <<= 9;
2246 	amt = pmcs_set_resid(pkt, amt, nblk << 9);
2247 	if (amt == 0) {
2248 		pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0,
2249 		    pwrk->phy->path);
2250 		pmcs_pwork(pwp, pwrk);
2251 		mutex_enter(&pwp->cq_lock);
2252 		STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
2253 		PMCS_CQ_RUN_LOCKED(pwp);
2254 		mutex_exit(&pwp->cq_lock);
2255 		return (PMCS_WQ_RUN_SUCCESS);
2256 	}
2257 
2258 	/*
2259 	 * Get an inbound queue entry for this I/O
2260 	 */
2261 	GET_IO_IQ_ENTRY(pwp, ptr, xp->phy->device_id, iq);
2262 	if (ptr == NULL) {
2263 		/*
2264 		 * This is a temporary failure not likely to unblocked by
2265 		 * commands completing as the test for scheduling the
2266 		 * restart of work is a per-device test.
2267 		 */
2268 		mutex_enter(&xp->wqlock);
2269 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2270 		mutex_exit(&xp->wqlock);
2271 		pmcs_dma_unload(pwp, sp);
2272 		SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES);
2273 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
2274 		    "%s: Failed to get IO IQ entry for tgt %d",
2275 		    __func__, xp->target_num);
2276 		return (PMCS_WQ_RUN_FAIL_RES);
2277 	}
2278 
2279 	/*
2280 	 * Get a tag.  At this point, hold statlock until the tagmap is
2281 	 * updated (just prior to sending the cmd to the hardware).
2282 	 */
2283 	mutex_enter(&xp->statlock);
2284 	for (tag = 0; tag < xp->qdepth; tag++) {
2285 		if ((xp->tagmap & (1 << tag)) == 0) {
2286 			break;
2287 		}
2288 	}
2289 
2290 	if (tag == xp->qdepth) {
2291 		mutex_exit(&xp->statlock);
2292 		mutex_exit(&pwp->iqp_lock[iq]);
2293 		mutex_enter(&xp->wqlock);
2294 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2295 		mutex_exit(&xp->wqlock);
2296 		return (PMCS_WQ_RUN_FAIL_OTHER);
2297 	}
2298 
2299 	sp->cmd_satltag = (uint8_t)tag;
2300 
2301 	/*
2302 	 * Set up the command
2303 	 */
2304 	bzero(fis, sizeof (fis));
2305 	ptr[0] =
2306 	    LE_32(PMCS_IOMB_IN_SAS(PMCS_OQ_IODONE, PMCIN_SATA_HOST_IO_START));
2307 	ptr[1] = LE_32(pwrk->htag);
2308 	ptr[2] = LE_32(pwrk->phy->device_id);
2309 	ptr[3] = LE_32(amt);
2310 
2311 	if (xp->ncq) {
2312 		mtype = SATA_PROTOCOL_FPDMA | (tag << 16);
2313 		fis[0] = ((nblk & 0xff) << 24) | (C_BIT << 8) | FIS_REG_H2DEV;
2314 		if (cdb_base == SCMD_READ) {
2315 			fis[0] |= (READ_FPDMA_QUEUED << 16);
2316 		} else {
2317 			fis[0] |= (WRITE_FPDMA_QUEUED << 16);
2318 		}
2319 		fis[1] = (FEATURE_LBA << 24) | (lba & 0xffffff);
2320 		fis[2] = ((nblk & 0xff00) << 16) | ((lba >> 24) & 0xffffff);
2321 		fis[3] = tag << 3;
2322 	} else {
2323 		int op;
2324 		fis[0] = (C_BIT << 8) | FIS_REG_H2DEV;
2325 		if (xp->pio) {
2326 			mtype = SATA_PROTOCOL_PIO;
2327 			if (cdb_base == SCMD_READ) {
2328 				op = READ_SECTORS_EXT;
2329 			} else {
2330 				op = WRITE_SECTORS_EXT;
2331 			}
2332 		} else {
2333 			mtype = SATA_PROTOCOL_DMA;
2334 			if (cdb_base == SCMD_READ) {
2335 				op = READ_DMA_EXT;
2336 			} else {
2337 				op = WRITE_DMA_EXT;
2338 			}
2339 		}
2340 		fis[0] |= (op << 16);
2341 		fis[1] = (FEATURE_LBA << 24) | (lba & 0xffffff);
2342 		fis[2] = (lba >> 24) & 0xffffff;
2343 		fis[3] = nblk;
2344 	}
2345 
2346 	if (cdb_base == SCMD_READ) {
2347 		ptr[4] = LE_32(mtype | PMCIN_DATADIR_2_INI);
2348 	} else {
2349 		ptr[4] = LE_32(mtype | PMCIN_DATADIR_2_DEV);
2350 	}
2351 #ifdef	DEBUG
2352 	/*
2353 	 * Generate a PMCOUT_STATUS_XFER_CMD_FRAME_ISSUED
2354 	 * event when this goes out on the wire.
2355 	 */
2356 	ptr[4] |= PMCIN_MESSAGE_REPORT;
2357 #endif
2358 	for (i = 0; i < (sizeof (fis_t))/(sizeof (uint32_t)); i++) {
2359 		ptr[i+5] = LE_32(fis[i]);
2360 	}
2361 	if (pmcs_dma_load(pwp, sp, ptr)) {
2362 		mutex_exit(&xp->statlock);
2363 		mutex_exit(&pwp->iqp_lock[iq]);
2364 		mutex_enter(&xp->wqlock);
2365 		STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next);
2366 		mutex_exit(&xp->wqlock);
2367 		pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp,
2368 		    "%s: Failed to dma_load for tgt %d",
2369 		    __func__, xp->target_num);
2370 		return (PMCS_WQ_RUN_FAIL_RES);
2371 
2372 	}
2373 
2374 	pwrk->state = PMCS_WORK_STATE_ONCHIP;
2375 	mutex_exit(&pwrk->lock);
2376 	xp->tagmap |= (1 << tag);
2377 	xp->actv_cnt++;
2378 	if (xp->actv_cnt > xp->maxdepth) {
2379 		xp->maxdepth = xp->actv_cnt;
2380 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, pwrk->phy, xp,
2381 		    "%s: max depth now %u", pwrk->phy->path, xp->maxdepth);
2382 	}
2383 	mutex_exit(&xp->statlock);
2384 	mutex_enter(&xp->aqlock);
2385 	STAILQ_INSERT_TAIL(&xp->aq, sp, cmd_next);
2386 	mutex_exit(&xp->aqlock);
2387 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL,
2388 	    "%s: giving pkt %p to hardware", __func__, (void *)pkt);
2389 #ifdef DEBUG
2390 	pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "SATA INI Message", ptr);
2391 #endif
2392 	INC_IQ_ENTRY(pwp, iq);
2393 
2394 	return (PMCS_WQ_RUN_SUCCESS);
2395 }
2396 
2397 /*
2398  * Complete a SATA command.  Called with pwrk lock held.
2399  */
2400 void
2401 pmcs_SATA_done(pmcs_hw_t *pwp, pmcwork_t *pwrk, uint32_t *msg)
2402 {
2403 	pmcs_cmd_t *sp = pwrk->arg;
2404 	struct scsi_pkt *pkt = CMD2PKT(sp);
2405 	pmcs_phy_t *pptr = pwrk->phy;
2406 	int dead;
2407 	uint32_t sts;
2408 	pmcs_xscsi_t *xp;
2409 	boolean_t aborted = B_FALSE;
2410 
2411 	xp = pwrk->xp;
2412 	ASSERT(xp != NULL);
2413 
2414 	DTRACE_PROBE4(pmcs__io__done, uint64_t, pkt->pkt_dma_len, int,
2415 	    (pkt->pkt_dma_flags & DDI_DMA_READ) != 0, hrtime_t, pwrk->start,
2416 	    hrtime_t, gethrtime());
2417 
2418 	dead = pwrk->dead;
2419 
2420 	if (msg) {
2421 		sts = LE_32(msg[2]);
2422 	} else {
2423 		sts = 0;
2424 	}
2425 
2426 	if (dead != 0) {
2427 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, "%s: dead cmd tag "
2428 		    "0x%x for %s", __func__, pwrk->htag, pptr->path);
2429 		goto out;
2430 	}
2431 	if ((pwrk->state == PMCS_WORK_STATE_TIMED_OUT) &&
2432 	    (sts != PMCOUT_STATUS_ABORTED)) {
2433 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2434 		    "%s: cmd 0x%p (tag 0x%x) timed out for %s",
2435 		    __func__, (void *)sp, pwrk->htag, pptr->path);
2436 		CMD2PKT(sp)->pkt_scbp[0] = STATUS_GOOD;
2437 		/* pkt_reason already set to CMD_TIMEOUT */
2438 		ASSERT(CMD2PKT(sp)->pkt_reason == CMD_TIMEOUT);
2439 		CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
2440 		    STATE_SENT_CMD;
2441 		CMD2PKT(sp)->pkt_statistics |= STAT_TIMEOUT;
2442 		goto out;
2443 	}
2444 
2445 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp, "%s: pkt %p tgt %u done",
2446 	    __func__, (void *)pkt, xp->target_num);
2447 
2448 	/*
2449 	 * If the status isn't okay but not underflow,
2450 	 * step to the side and parse the (possible) error.
2451 	 */
2452 #ifdef DEBUG
2453 	if (msg) {
2454 		pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "Outbound Message", msg);
2455 	}
2456 #endif
2457 	if (!msg) {
2458 		goto out;
2459 	}
2460 
2461 	/*
2462 	 * If the status isn't okay or we got a FIS response of some kind,
2463 	 * step to the side and parse the (possible) error.
2464 	 */
2465 	if ((sts != PMCOUT_STATUS_OK) || (LE_32(msg[3]) != 0)) {
2466 		if (sts == PMCOUT_STATUS_IO_DS_NON_OPERATIONAL) {
2467 			mutex_exit(&pwrk->lock);
2468 			pmcs_lock_phy(pptr);
2469 			mutex_enter(&xp->statlock);
2470 			if ((xp->resetting == 0) && (xp->reset_success != 0) &&
2471 			    (xp->reset_wait == 0)) {
2472 				mutex_exit(&xp->statlock);
2473 				if (pmcs_reset_phy(pwp, pptr,
2474 				    PMCS_PHYOP_LINK_RESET) != 0) {
2475 					pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2476 					    "%s: PHY (%s) Local Control/Link "
2477 					    "Reset FAILED as part of error "
2478 					    "recovery", __func__, pptr->path);
2479 				}
2480 				mutex_enter(&xp->statlock);
2481 			}
2482 			mutex_exit(&xp->statlock);
2483 			pmcs_unlock_phy(pptr);
2484 			mutex_enter(&pwrk->lock);
2485 		}
2486 		pmcs_ioerror(pwp, SATA, pwrk, msg);
2487 	} else {
2488 		pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0,
2489 		    pwrk->phy->path);
2490 		pkt->pkt_state |= STATE_XFERRED_DATA;
2491 		pkt->pkt_resid = 0;
2492 	}
2493 
2494 	pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp,
2495 	    "%s: pkt %p tgt %u done reason=%x state=%x resid=%ld status=%x",
2496 	    __func__, (void *)pkt, xp->target_num, pkt->pkt_reason,
2497 	    pkt->pkt_state, pkt->pkt_resid, pkt->pkt_scbp[0]);
2498 
2499 	if (pwrk->state == PMCS_WORK_STATE_ABORTED) {
2500 		pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2501 		    "%s: scsi_pkt 0x%p aborted for PHY %s; work = 0x%p",
2502 		    __func__, (void *)pkt, pptr->path, (void *)pwrk);
2503 		aborted = B_TRUE;
2504 	}
2505 
2506 out:
2507 	pmcs_pwork(pwp, pwrk);
2508 	pmcs_dma_unload(pwp, sp);
2509 
2510 	mutex_enter(&xp->statlock);
2511 	xp->tagmap &= ~(1 << sp->cmd_satltag);
2512 
2513 	if (xp->dev_gone) {
2514 		mutex_exit(&xp->statlock);
2515 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp,
2516 		    "%s: Completing command for dead target 0x%p", __func__,
2517 		    (void *)xp);
2518 		return;
2519 	}
2520 
2521 	ASSERT(xp->actv_cnt > 0);
2522 	if (--(xp->actv_cnt) == 0) {
2523 		if (xp->draining) {
2524 			pmcs_prt(pwp, PMCS_PRT_DEBUG1, pptr, xp,
2525 			    "%s: waking up drain waiters", __func__);
2526 			cv_signal(&pwp->drain_cv);
2527 		} else if (xp->special_needed) {
2528 			SCHEDULE_WORK(pwp, PMCS_WORK_SATA_RUN);
2529 		}
2530 	}
2531 	mutex_exit(&xp->statlock);
2532 
2533 	if (dead == 0) {
2534 #ifdef	DEBUG
2535 		pmcs_cmd_t *wp;
2536 		mutex_enter(&xp->aqlock);
2537 		STAILQ_FOREACH(wp, &xp->aq, cmd_next) {
2538 			if (wp == sp) {
2539 				break;
2540 			}
2541 		}
2542 		ASSERT(wp != NULL);
2543 #else
2544 		mutex_enter(&xp->aqlock);
2545 #endif
2546 		STAILQ_REMOVE(&xp->aq, sp, pmcs_cmd, cmd_next);
2547 		if (aborted) {
2548 			pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp,
2549 			    "%s: Aborted cmd for tgt 0x%p, signaling waiters",
2550 			    __func__, (void *)xp);
2551 			cv_signal(&xp->abort_cv);
2552 		}
2553 		mutex_exit(&xp->aqlock);
2554 		mutex_enter(&pwp->cq_lock);
2555 		STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next);
2556 		mutex_exit(&pwp->cq_lock);
2557 	}
2558 }
2559 
2560 static uint8_t
2561 pmcs_SATA_rwparm(uint8_t *cdb, uint32_t *xfr, uint64_t *lba, uint64_t lbamax)
2562 {
2563 	uint8_t asc = 0;
2564 	switch (cdb[0]) {
2565 	case SCMD_READ_G5:
2566 	case SCMD_WRITE_G5:
2567 		*xfr =
2568 		    (((uint32_t)cdb[10]) <<  24) |
2569 		    (((uint32_t)cdb[11]) <<  16) |
2570 		    (((uint32_t)cdb[12]) <<   8) |
2571 		    ((uint32_t)cdb[13]);
2572 		*lba =
2573 		    (((uint64_t)cdb[2]) << 56) |
2574 		    (((uint64_t)cdb[3]) << 48) |
2575 		    (((uint64_t)cdb[4]) << 40) |
2576 		    (((uint64_t)cdb[5]) << 32) |
2577 		    (((uint64_t)cdb[6]) << 24) |
2578 		    (((uint64_t)cdb[7]) << 16) |
2579 		    (((uint64_t)cdb[8]) <<  8) |
2580 		    ((uint64_t)cdb[9]);
2581 		/* Check for illegal bits */
2582 		if (cdb[15]) {
2583 			asc = 0x24;	/* invalid field in cdb */
2584 		}
2585 		break;
2586 	case SCMD_READ_G4:
2587 	case SCMD_WRITE_G4:
2588 		*xfr =
2589 		    (((uint32_t)cdb[6]) <<  16) |
2590 		    (((uint32_t)cdb[7]) <<   8) |
2591 		    ((uint32_t)cdb[8]);
2592 		*lba =
2593 		    (((uint32_t)cdb[2]) << 24) |
2594 		    (((uint32_t)cdb[3]) << 16) |
2595 		    (((uint32_t)cdb[4]) <<  8) |
2596 		    ((uint32_t)cdb[5]);
2597 		/* Check for illegal bits */
2598 		if (cdb[11]) {
2599 			asc = 0x24;	/* invalid field in cdb */
2600 		}
2601 		break;
2602 	case SCMD_READ_G1:
2603 	case SCMD_WRITE_G1:
2604 		*xfr = (((uint32_t)cdb[7]) <<  8) | ((uint32_t)cdb[8]);
2605 		*lba =
2606 		    (((uint32_t)cdb[2]) << 24) |
2607 		    (((uint32_t)cdb[3]) << 16) |
2608 		    (((uint32_t)cdb[4]) <<  8) |
2609 		    ((uint32_t)cdb[5]);
2610 		/* Check for illegal bits */
2611 		if (cdb[9]) {
2612 			asc = 0x24;	/* invalid field in cdb */
2613 		}
2614 		break;
2615 	case SCMD_READ:
2616 	case SCMD_WRITE:
2617 		*xfr = cdb[4];
2618 		if (*xfr == 0) {
2619 			*xfr = 256;
2620 		}
2621 		*lba =
2622 		    (((uint32_t)cdb[1] & 0x1f) << 16) |
2623 		    (((uint32_t)cdb[2]) << 8) |
2624 		    ((uint32_t)cdb[3]);
2625 		/* Check for illegal bits */
2626 		if (cdb[5]) {
2627 			asc = 0x24;	/* invalid field in cdb */
2628 		}
2629 		break;
2630 	}
2631 
2632 	if (asc == 0) {
2633 		if ((*lba + *xfr) > lbamax) {
2634 			asc = 0x21;	/* logical block out of range */
2635 		}
2636 	}
2637 	return (asc);
2638 }
2639 
2640 /*
2641  * Called with pwrk lock held.
2642  */
2643 static void
2644 pmcs_ioerror(pmcs_hw_t *pwp, pmcs_dtype_t t, pmcwork_t *pwrk, uint32_t *w)
2645 {
2646 	static uint8_t por[] = {
2647 	    0xf0, 0x0, 0x6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x28
2648 	};
2649 	static uint8_t parity[] = {
2650 	    0xf0, 0x0, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x47, 5
2651 	};
2652 	const char *msg;
2653 	char buf[20];
2654 	pmcs_cmd_t *sp = pwrk->arg;
2655 	pmcs_phy_t *phyp = pwrk->phy;
2656 	struct scsi_pkt *pkt = CMD2PKT(sp);
2657 	uint32_t status;
2658 	uint32_t resid;
2659 
2660 	ASSERT(w != NULL);
2661 	status = LE_32(w[2]);
2662 	resid = LE_32(w[3]);
2663 
2664 	msg = pmcs_status_str(status);
2665 	if (msg == NULL) {
2666 		(void) snprintf(buf, sizeof (buf), "Error 0x%x", status);
2667 		msg = buf;
2668 	}
2669 
2670 	if (status != PMCOUT_STATUS_OK) {
2671 		pmcs_prt(pwp, PMCS_PRT_DEBUG2, phyp, NULL,
2672 		    "%s: device %s tag 0x%x status %s @ %llu", __func__,
2673 		    phyp->path, pwrk->htag, msg,
2674 		    (unsigned long long)gethrtime());
2675 	}
2676 
2677 	pkt->pkt_reason = CMD_CMPLT;		/* default reason */
2678 
2679 	switch (status) {
2680 	case PMCOUT_STATUS_OK:
2681 		if (t == SATA) {
2682 			int i;
2683 			fis_t fis;
2684 			for (i = 0; i < sizeof (fis) / sizeof (fis[0]); i++) {
2685 				fis[i] = LE_32(w[4+i]);
2686 			}
2687 			if ((fis[0] & 0xff) != FIS_REG_D2H) {
2688 				pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, NULL,
2689 				    "unexpected fis code 0x%x", fis[0] & 0xff);
2690 			} else {
2691 				pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, NULL,
2692 				    "FIS ERROR");
2693 				pmcs_fis_dump(pwp, fis);
2694 			}
2695 			pkt->pkt_reason = CMD_TRAN_ERR;
2696 			break;
2697 		}
2698 		pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0, phyp->path);
2699 		break;
2700 
2701 	case PMCOUT_STATUS_ABORTED:
2702 		/*
2703 		 * Command successfully aborted.
2704 		 */
2705 		if (phyp->dead) {
2706 			pkt->pkt_reason = CMD_DEV_GONE;
2707 			pkt->pkt_state = STATE_GOT_BUS;
2708 		} else if (pwrk->ssp_event != 0) {
2709 			pkt->pkt_reason = CMD_TRAN_ERR;
2710 			pkt->pkt_state = STATE_GOT_BUS;
2711 		} else if (pwrk->state == PMCS_WORK_STATE_TIMED_OUT) {
2712 			pkt->pkt_reason = CMD_TIMEOUT;
2713 			pkt->pkt_statistics |= STAT_TIMEOUT;
2714 			pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2715 			    STATE_SENT_CMD;
2716 		} else {
2717 			pkt->pkt_reason = CMD_ABORTED;
2718 			pkt->pkt_statistics |= STAT_ABORTED;
2719 			pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET |
2720 			    STATE_SENT_CMD;
2721 		}
2722 
2723 		/*
2724 		 * PMCS_WORK_STATE_TIMED_OUT doesn't need to be preserved past
2725 		 * this point, so go ahead and mark it as aborted.
2726 		 */
2727 		pwrk->state = PMCS_WORK_STATE_ABORTED;
2728 		break;
2729 
2730 	case PMCOUT_STATUS_UNDERFLOW:
2731 		/*
2732 		 * This will only get called for SATA
2733 		 */
2734 		pkt->pkt_resid = resid;
2735 		if (pkt->pkt_dma_len < pkt->pkt_resid) {
2736 			(void) pmcs_set_resid(pkt, pkt->pkt_dma_len, resid);
2737 		}
2738 		pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0, phyp->path);
2739 		break;
2740 
2741 	case PMCOUT_STATUS_NO_DEVICE:
2742 	case PMCOUT_STATUS_XFER_ERROR_SATA_LINK_TIMEOUT:
2743 		pkt->pkt_reason = CMD_DEV_GONE;
2744 		break;
2745 
2746 	case PMCOUT_STATUS_OPEN_CNX_ERROR_WRONG_DESTINATION:
2747 		/*
2748 		 * Need to do rediscovery. We probably have
2749 		 * the wrong device (disk swap), so kill
2750 		 * this one.
2751 		 */
2752 	case PMCOUT_STATUS_OPEN_CNX_PROTOCOL_NOT_SUPPORTED:
2753 	case PMCOUT_STATUS_OPEN_CNX_ERROR_ZONE_VIOLATION:
2754 	case PMCOUT_STATUS_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED:
2755 	case PMCOUT_STATUS_OPEN_CNX_ERROR_UNKNOWN_EROOR:
2756 		/*
2757 		 * Need to do rediscovery.
2758 		 */
2759 		if (!phyp->dead) {
2760 			mutex_exit(&pwrk->lock);
2761 			pmcs_lock_phy(pwrk->phy);
2762 			pmcs_kill_changed(pwp, pwrk->phy, 0);
2763 			pmcs_unlock_phy(pwrk->phy);
2764 			mutex_enter(&pwrk->lock);
2765 			pkt->pkt_reason = CMD_INCOMPLETE;
2766 			pkt->pkt_state = STATE_GOT_BUS;
2767 		} else {
2768 			pkt->pkt_reason = CMD_DEV_GONE;
2769 		}
2770 		break;
2771 
2772 	case PMCOUT_STATUS_OPEN_CNX_ERROR_BREAK:
2773 	case PMCOUT_STATUS_OPEN_CNX_ERROR_IT_NEXUS_LOSS:
2774 	case PMCOUT_STATUS_OPENCNX_ERROR_BAD_DESTINATION:
2775 	case PMCOUT_STATUS_IO_XFER_ERROR_NAK_RECEIVED:
2776 		/* cmd is pending on the target */
2777 	case PMCOUT_STATUS_XFER_ERROR_OFFSET_MISMATCH:
2778 	case PMCOUT_STATUS_XFER_ERROR_REJECTED_NCQ_MODE:
2779 		/* transitory - commands sent while in NCQ failure mode */
2780 	case PMCOUT_STATUS_XFER_ERROR_ABORTED_NCQ_MODE:
2781 		/* NCQ failure */
2782 	case PMCOUT_STATUS_IO_PORT_IN_RESET:
2783 	case PMCOUT_STATUS_XFER_ERR_BREAK:
2784 	case PMCOUT_STATUS_XFER_ERR_PHY_NOT_READY:
2785 		pkt->pkt_reason = CMD_INCOMPLETE;
2786 		pkt->pkt_state = STATE_GOT_BUS;
2787 		break;
2788 
2789 	case PMCOUT_STATUS_IO_XFER_OPEN_RETRY_TIMEOUT:
2790 		pmcs_latch_status(pwp, sp, STATUS_BUSY, NULL, 0, phyp->path);
2791 		break;
2792 
2793 	case PMCOUT_STATUS_OPEN_CNX_ERROR_STP_RESOURCES_BUSY:
2794 		/* synthesize a RESERVATION CONFLICT */
2795 		pmcs_latch_status(pwp, sp, STATUS_RESERVATION_CONFLICT, NULL,
2796 		    0, phyp->path);
2797 		break;
2798 
2799 	case PMCOUT_STATUS_XFER_ERROR_ABORTED_DUE_TO_SRST:
2800 		/* synthesize a power-on/reset */
2801 		pmcs_latch_status(pwp, sp, STATUS_CHECK, por, sizeof (por),
2802 		    phyp->path);
2803 		break;
2804 
2805 	case PMCOUT_STATUS_XFER_ERROR_UNEXPECTED_PHASE:
2806 	case PMCOUT_STATUS_XFER_ERROR_RDY_OVERRUN:
2807 	case PMCOUT_STATUS_XFER_ERROR_RDY_NOT_EXPECTED:
2808 	case PMCOUT_STATUS_XFER_ERROR_CMD_ISSUE_ACK_NAK_TIMEOUT:
2809 	case PMCOUT_STATUS_XFER_ERROR_CMD_ISSUE_BREAK_BEFORE_ACK_NACK:
2810 	case PMCOUT_STATUS_XFER_ERROR_CMD_ISSUE_PHY_DOWN_BEFORE_ACK_NAK:
2811 		/* synthesize a PARITY ERROR */
2812 		pmcs_latch_status(pwp, sp, STATUS_CHECK, parity,
2813 		    sizeof (parity), phyp->path);
2814 		break;
2815 
2816 	case PMCOUT_STATUS_IO_XFER_ERROR_DMA:
2817 	case PMCOUT_STATUS_IO_NOT_VALID:
2818 	case PMCOUT_STATUS_PROG_ERROR:
2819 	case PMCOUT_STATUS_XFER_ERROR_PEER_ABORTED:
2820 	case PMCOUT_STATUS_XFER_ERROR_SATA: /* non-NCQ failure */
2821 	default:
2822 		pkt->pkt_reason = CMD_TRAN_ERR;
2823 		break;
2824 	}
2825 }
2826 
2827 /*
2828  * Latch up SCSI status
2829  */
2830 
2831 void
2832 pmcs_latch_status(pmcs_hw_t *pwp, pmcs_cmd_t *sp, uint8_t status,
2833     uint8_t *snsp, size_t snslen, char *path)
2834 {
2835 	static const char c1[] =
2836 	    "%s: Status Byte 0x%02x for CDB0=0x%02x (%02x %02x %02x) "
2837 	    "HTAG 0x%x @ %llu";
2838 	static const char c2[] =
2839 	    "%s: Status Byte 0x%02x for CDB0=0x%02x HTAG 0x%x @ %llu";
2840 
2841 	CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET |
2842 	    STATE_SENT_CMD | STATE_GOT_STATUS;
2843 	CMD2PKT(sp)->pkt_scbp[0] = status;
2844 
2845 	if (status == STATUS_CHECK && snsp &&
2846 	    (size_t)SCSA_STSLEN(sp) >= sizeof (struct scsi_arq_status)) {
2847 		struct scsi_arq_status *aqp =
2848 		    (void *) CMD2PKT(sp)->pkt_scbp;
2849 		size_t amt = sizeof (struct scsi_extended_sense);
2850 		uint8_t key = scsi_sense_key(snsp);
2851 		uint8_t asc = scsi_sense_asc(snsp);
2852 		uint8_t ascq = scsi_sense_ascq(snsp);
2853 		if (amt > snslen) {
2854 			amt = snslen;
2855 		}
2856 		pmcs_prt(pwp, PMCS_PRT_DEBUG_SCSI_STATUS, NULL, NULL, c1, path,
2857 		    status, CMD2PKT(sp)->pkt_cdbp[0] & 0xff, key, asc, ascq,
2858 		    sp->cmd_tag, (unsigned long long)gethrtime());
2859 		CMD2PKT(sp)->pkt_state |= STATE_ARQ_DONE;
2860 		(*(uint8_t *)&aqp->sts_rqpkt_status) = STATUS_GOOD;
2861 		aqp->sts_rqpkt_statistics = 0;
2862 		aqp->sts_rqpkt_reason = CMD_CMPLT;
2863 		aqp->sts_rqpkt_state = STATE_GOT_BUS |
2864 		    STATE_GOT_TARGET | STATE_SENT_CMD |
2865 		    STATE_XFERRED_DATA | STATE_GOT_STATUS;
2866 		(void) memcpy(&aqp->sts_sensedata, snsp, amt);
2867 		if (aqp->sts_sensedata.es_class != CLASS_EXTENDED_SENSE) {
2868 			aqp->sts_rqpkt_reason = CMD_TRAN_ERR;
2869 			aqp->sts_rqpkt_state = 0;
2870 			aqp->sts_rqpkt_resid =
2871 			    sizeof (struct scsi_extended_sense);
2872 		} else {
2873 			aqp->sts_rqpkt_resid =
2874 			    sizeof (struct scsi_extended_sense) - amt;
2875 		}
2876 	} else if (status) {
2877 		pmcs_prt(pwp, PMCS_PRT_DEBUG_SCSI_STATUS, NULL, NULL, c2,
2878 		    path, status, CMD2PKT(sp)->pkt_cdbp[0] & 0xff,
2879 		    sp->cmd_tag, (unsigned long long)gethrtime());
2880 	}
2881 
2882 	CMD2PKT(sp)->pkt_reason = CMD_CMPLT;
2883 }
2884 
2885 /*
2886  * Calculate and set packet residual and return the amount
2887  * left over after applying various filters.
2888  */
2889 size_t
2890 pmcs_set_resid(struct scsi_pkt *pkt, size_t amt, uint32_t cdbamt)
2891 {
2892 	pkt->pkt_resid = cdbamt;
2893 	if (amt > pkt->pkt_resid) {
2894 		amt = pkt->pkt_resid;
2895 	}
2896 	if (amt > pkt->pkt_dma_len) {
2897 		amt = pkt->pkt_dma_len;
2898 	}
2899 	return (amt);
2900 }
2901 
2902 /*
2903  * Return the existing target softstate if there is one.  If there is,
2904  * the PHY is locked as well and that lock must be freed by the caller
2905  * after the target/PHY linkage is established.
2906  */
2907 pmcs_xscsi_t *
2908 pmcs_get_target(pmcs_iport_t *iport, char *tgt_port)
2909 {
2910 	pmcs_hw_t *pwp = iport->pwp;
2911 	pmcs_phy_t *phyp;
2912 	pmcs_xscsi_t *tgt;
2913 	uint64_t wwn;
2914 	char unit_address[PMCS_MAX_UA_SIZE];
2915 	int ua_form = 1;
2916 
2917 	/*
2918 	 * Find the PHY for this target
2919 	 */
2920 	phyp = pmcs_find_phy_by_sas_address(pwp, iport, NULL, tgt_port);
2921 	if (phyp == NULL) {
2922 		pmcs_prt(pwp, PMCS_PRT_DEBUG3, NULL, NULL,
2923 		    "%s: No PHY for target @ %s", __func__, tgt_port);
2924 		return (NULL);
2925 	}
2926 
2927 	tgt = ddi_soft_state_bystr_get(iport->tgt_sstate, tgt_port);
2928 
2929 	if (tgt) {
2930 		/*
2931 		 * There's already a target.  Check its PHY pointer to see
2932 		 * if we need to clear the old linkages
2933 		 */
2934 		if (tgt->phy && (tgt->phy != phyp)) {
2935 			pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
2936 			    "%s: Target PHY updated from %p to %p", __func__,
2937 			    (void *)tgt->phy, (void *)phyp);
2938 			if (!IS_ROOT_PHY(tgt->phy)) {
2939 				pmcs_dec_phy_ref_count(tgt->phy);
2940 				pmcs_inc_phy_ref_count(phyp);
2941 			}
2942 			tgt->phy->target = NULL;
2943 		}
2944 
2945 		tgt->phy = phyp;
2946 		phyp->target = tgt;
2947 		return (tgt);
2948 	}
2949 
2950 	/*
2951 	 * Make sure the PHY we found is on the correct iport
2952 	 */
2953 	if (phyp->iport != iport) {
2954 		pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, NULL,
2955 		    "%s: No target at %s on this iport", __func__, tgt_port);
2956 		pmcs_unlock_phy(phyp);
2957 		return (NULL);
2958 	}
2959 
2960 	/*
2961 	 * Allocate the new softstate
2962 	 */
2963 	wwn = pmcs_barray2wwn(phyp->sas_address);
2964 	(void) scsi_wwn_to_wwnstr(wwn, ua_form, unit_address);
2965 
2966 	if (ddi_soft_state_bystr_zalloc(iport->tgt_sstate, unit_address) !=
2967 	    DDI_SUCCESS) {
2968 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
2969 		    "%s: Couldn't alloc softstate for device at %s",
2970 		    __func__, unit_address);
2971 		pmcs_unlock_phy(phyp);
2972 		return (NULL);
2973 	}
2974 
2975 	tgt = ddi_soft_state_bystr_get(iport->tgt_sstate, unit_address);
2976 	STAILQ_INIT(&tgt->wq);
2977 	STAILQ_INIT(&tgt->aq);
2978 	STAILQ_INIT(&tgt->sq);
2979 	mutex_init(&tgt->statlock, NULL, MUTEX_DRIVER,
2980 	    DDI_INTR_PRI(pwp->intr_pri));
2981 	mutex_init(&tgt->wqlock, NULL, MUTEX_DRIVER,
2982 	    DDI_INTR_PRI(pwp->intr_pri));
2983 	mutex_init(&tgt->aqlock, NULL, MUTEX_DRIVER,
2984 	    DDI_INTR_PRI(pwp->intr_pri));
2985 	cv_init(&tgt->reset_cv, NULL, CV_DRIVER, NULL);
2986 	cv_init(&tgt->abort_cv, NULL, CV_DRIVER, NULL);
2987 	tgt->qdepth = 1;
2988 	tgt->target_num = PMCS_INVALID_TARGET_NUM;
2989 	bcopy(unit_address, tgt->unit_address, PMCS_MAX_UA_SIZE);
2990 	tgt->pwp = pwp;
2991 	tgt->ua = strdup(iport->ua);
2992 	tgt->phy = phyp;
2993 	ASSERT((phyp->target == NULL) || (phyp->target == tgt));
2994 	if (phyp->target == NULL) {
2995 		phyp->target = tgt;
2996 	}
2997 
2998 	/*
2999 	 * Don't allocate LUN softstate for SMP targets
3000 	 */
3001 	if (phyp->dtype == EXPANDER) {
3002 		return (tgt);
3003 	}
3004 
3005 	if (ddi_soft_state_bystr_init(&tgt->lun_sstate,
3006 	    sizeof (pmcs_lun_t), PMCS_LUN_SSTATE_SZ) != 0) {
3007 		pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt,
3008 		    "%s: LUN soft_state_bystr_init failed", __func__);
3009 		ddi_soft_state_bystr_free(iport->tgt_sstate, tgt_port);
3010 		pmcs_unlock_phy(phyp);
3011 		return (NULL);
3012 	}
3013 
3014 	return (tgt);
3015 }
3016