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