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