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