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