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