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 if (rval != PMCS_WQ_RUN_FAIL_RES_CMP) { 1648 sp->cmd_tag = NULL; 1649 } 1650 pmcs_dec_phy_ref_count(phyp); 1651 pmcs_pwork(pwp, pwrk); 1652 SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES); 1653 if (rval == PMCS_WQ_RUN_FAIL_RES) { 1654 return (B_FALSE); 1655 } else { 1656 return (B_TRUE); 1657 } 1658 } 1659 } else { 1660 ASSERT(xp->dtype == SATA); 1661 pwrk->ptr = (void *) pmcs_SATA_done; 1662 if ((rval = pmcs_SATA_run(sp, pwrk)) != 0) { 1663 sp->cmd_tag = NULL; 1664 pmcs_dec_phy_ref_count(phyp); 1665 pmcs_pwork(pwp, pwrk); 1666 SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES); 1667 if (rval == PMCS_WQ_RUN_FAIL_RES) { 1668 return (B_FALSE); 1669 } else { 1670 return (B_TRUE); 1671 } 1672 } 1673 } 1674 1675 if (run_one) { 1676 goto wq_out; 1677 } 1678 mutex_enter(&xp->wqlock); 1679 } 1680 1681 mutex_exit(&xp->wqlock); 1682 1683 wq_out: 1684 pmcs_dec_phy_ref_count(phyp); 1685 return (B_TRUE); 1686 } 1687 1688 /* 1689 * Start commands for all devices. 1690 */ 1691 void 1692 pmcs_scsa_wq_run(pmcs_hw_t *pwp) 1693 { 1694 pmcs_xscsi_t *xp; 1695 uint16_t target_start, target; 1696 boolean_t rval = B_TRUE; 1697 1698 mutex_enter(&pwp->lock); 1699 target_start = pwp->last_wq_dev; 1700 target = target_start; 1701 1702 do { 1703 xp = pwp->targets[target]; 1704 if ((xp == NULL) || (STAILQ_EMPTY(&xp->wq))) { 1705 if (++target == pwp->max_dev) { 1706 target = 0; 1707 } 1708 continue; 1709 } 1710 1711 mutex_exit(&pwp->lock); 1712 rval = pmcs_scsa_wq_run_one(pwp, xp); 1713 mutex_enter(&pwp->lock); 1714 1715 if (rval == B_FALSE) { 1716 break; 1717 } 1718 1719 if (++target == pwp->max_dev) { 1720 target = 0; 1721 } 1722 } while (target != target_start); 1723 1724 if (rval) { 1725 /* 1726 * If we were resource limited, but apparently are not now, 1727 * reschedule the work queues anyway. 1728 */ 1729 if (pwp->resource_limited) { 1730 SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES); 1731 } 1732 pwp->resource_limited = 0; /* Not resource-constrained */ 1733 } else { 1734 /* 1735 * Give everybody a chance, and reschedule to run the queues 1736 * again as long as we're limited. 1737 */ 1738 pwp->resource_limited = 1; 1739 SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES); 1740 } 1741 1742 pwp->last_wq_dev = target; 1743 mutex_exit(&pwp->lock); 1744 } 1745 1746 /* 1747 * Pull the completion queue, drop the lock and complete all elements. 1748 */ 1749 1750 void 1751 pmcs_scsa_cq_run(void *arg) 1752 { 1753 pmcs_cq_thr_info_t *cqti = (pmcs_cq_thr_info_t *)arg; 1754 pmcs_hw_t *pwp = cqti->cq_pwp; 1755 pmcs_cmd_t *sp, *nxt; 1756 struct scsi_pkt *pkt; 1757 pmcs_xscsi_t *tgt; 1758 pmcs_iocomp_cb_t *ioccb, *ioccb_next; 1759 pmcs_cb_t callback; 1760 1761 DTRACE_PROBE1(pmcs__scsa__cq__run__start, pmcs_cq_thr_info_t *, cqti); 1762 1763 mutex_enter(&pwp->cq_lock); 1764 1765 while (!pwp->cq_info.cq_stop) { 1766 /* 1767 * First, check the I/O completion callback queue. 1768 */ 1769 ioccb = pwp->iocomp_cb_head; 1770 pwp->iocomp_cb_head = NULL; 1771 pwp->iocomp_cb_tail = NULL; 1772 mutex_exit(&pwp->cq_lock); 1773 1774 while (ioccb) { 1775 /* 1776 * Grab the lock on the work structure. The callback 1777 * routine is responsible for clearing it. 1778 */ 1779 mutex_enter(&ioccb->pwrk->lock); 1780 ioccb_next = ioccb->next; 1781 callback = (pmcs_cb_t)ioccb->pwrk->ptr; 1782 (*callback)(pwp, ioccb->pwrk, 1783 (uint32_t *)((void *)ioccb->iomb)); 1784 kmem_cache_free(pwp->iocomp_cb_cache, ioccb); 1785 ioccb = ioccb_next; 1786 } 1787 1788 /* 1789 * Next, run the completion queue 1790 */ 1791 mutex_enter(&pwp->cq_lock); 1792 sp = STAILQ_FIRST(&pwp->cq); 1793 STAILQ_INIT(&pwp->cq); 1794 mutex_exit(&pwp->cq_lock); 1795 1796 DTRACE_PROBE1(pmcs__scsa__cq__run__start__loop, 1797 pmcs_cq_thr_info_t *, cqti); 1798 1799 if (sp && pmcs_check_acc_dma_handle(pwp)) { 1800 ddi_fm_service_impact(pwp->dip, DDI_SERVICE_UNAFFECTED); 1801 } 1802 1803 while (sp) { 1804 nxt = STAILQ_NEXT(sp, cmd_next); 1805 pkt = CMD2PKT(sp); 1806 tgt = sp->cmd_target; 1807 pmcs_prt(pwp, PMCS_PRT_DEBUG3, NULL, tgt, 1808 "%s: calling completion on %p for tgt %p", __func__, 1809 (void *)sp, (void *)tgt); 1810 if (tgt) { 1811 mutex_enter(&tgt->statlock); 1812 ASSERT(tgt->actv_pkts != 0); 1813 tgt->actv_pkts--; 1814 mutex_exit(&tgt->statlock); 1815 } 1816 scsi_hba_pkt_comp(pkt); 1817 sp = nxt; 1818 } 1819 1820 DTRACE_PROBE1(pmcs__scsa__cq__run__end__loop, 1821 pmcs_cq_thr_info_t *, cqti); 1822 1823 /* 1824 * Check if there are more completions to do. If so, and we've 1825 * not been told to stop, skip the wait and cycle through again. 1826 */ 1827 mutex_enter(&pwp->cq_lock); 1828 if ((pwp->iocomp_cb_head == NULL) && STAILQ_EMPTY(&pwp->cq) && 1829 !pwp->cq_info.cq_stop) { 1830 mutex_exit(&pwp->cq_lock); 1831 mutex_enter(&cqti->cq_thr_lock); 1832 cv_wait(&cqti->cq_cv, &cqti->cq_thr_lock); 1833 mutex_exit(&cqti->cq_thr_lock); 1834 mutex_enter(&pwp->cq_lock); 1835 } 1836 } 1837 1838 mutex_exit(&pwp->cq_lock); 1839 DTRACE_PROBE1(pmcs__scsa__cq__run__stop, pmcs_cq_thr_info_t *, cqti); 1840 thread_exit(); 1841 } 1842 1843 /* 1844 * Run a SAS command. Called with pwrk->lock held, returns unlocked. 1845 */ 1846 static int 1847 pmcs_SAS_run(pmcs_cmd_t *sp, pmcwork_t *pwrk) 1848 { 1849 pmcs_hw_t *pwp = CMD2PMC(sp); 1850 struct scsi_pkt *pkt = CMD2PKT(sp); 1851 pmcs_xscsi_t *xp = pwrk->xp; 1852 uint32_t iq, *ptr; 1853 sas_ssp_cmd_iu_t sc; 1854 1855 ASSERT(xp != NULL); 1856 mutex_enter(&xp->statlock); 1857 if (!xp->assigned) { 1858 mutex_exit(&xp->statlock); 1859 return (PMCS_WQ_RUN_FAIL_OTHER); 1860 } 1861 if ((xp->actv_cnt >= xp->qdepth) || xp->recover_wait) { 1862 mutex_exit(&xp->statlock); 1863 mutex_enter(&xp->wqlock); 1864 STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next); 1865 mutex_exit(&xp->wqlock); 1866 return (PMCS_WQ_RUN_FAIL_OTHER); 1867 } 1868 GET_IO_IQ_ENTRY(pwp, ptr, pwrk->phy->device_id, iq); 1869 if (ptr == NULL) { 1870 mutex_exit(&xp->statlock); 1871 /* 1872 * This is a temporary failure not likely to unblocked by 1873 * commands completing as the test for scheduling the 1874 * restart of work is a per-device test. 1875 */ 1876 mutex_enter(&xp->wqlock); 1877 STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next); 1878 mutex_exit(&xp->wqlock); 1879 pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp, 1880 "%s: Failed to get IO IQ entry for tgt %d", 1881 __func__, xp->target_num); 1882 return (PMCS_WQ_RUN_FAIL_RES); 1883 1884 } 1885 1886 ptr[0] = 1887 LE_32(PMCS_IOMB_IN_SAS(PMCS_OQ_IODONE, PMCIN_SSP_INI_IO_START)); 1888 ptr[1] = LE_32(pwrk->htag); 1889 ptr[2] = LE_32(pwrk->phy->device_id); 1890 ptr[3] = LE_32(pkt->pkt_dma_len); 1891 if (ptr[3]) { 1892 ASSERT(pkt->pkt_numcookies); 1893 if (pkt->pkt_dma_flags & DDI_DMA_READ) { 1894 ptr[4] = LE_32(PMCIN_DATADIR_2_INI); 1895 } else { 1896 ptr[4] = LE_32(PMCIN_DATADIR_2_DEV); 1897 } 1898 if (pmcs_dma_load(pwp, sp, ptr)) { 1899 mutex_exit(&pwp->iqp_lock[iq]); 1900 mutex_exit(&xp->statlock); 1901 mutex_enter(&xp->wqlock); 1902 if (STAILQ_EMPTY(&xp->wq)) { 1903 STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next); 1904 mutex_exit(&xp->wqlock); 1905 return (PMCS_WQ_RUN_FAIL_RES); 1906 } else { 1907 mutex_exit(&xp->wqlock); 1908 CMD2PKT(sp)->pkt_scbp[0] = STATUS_QFULL; 1909 CMD2PKT(sp)->pkt_reason = CMD_CMPLT; 1910 CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS | 1911 STATE_GOT_TARGET | STATE_SENT_CMD | 1912 STATE_GOT_STATUS; 1913 sp->cmd_tag = NULL; 1914 mutex_enter(&pwp->cq_lock); 1915 STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next); 1916 PMCS_CQ_RUN_LOCKED(pwp); 1917 mutex_exit(&pwp->cq_lock); 1918 pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp, 1919 "%s: Failed to dma_load for tgt %d (QF)", 1920 __func__, xp->target_num); 1921 return (PMCS_WQ_RUN_FAIL_RES_CMP); 1922 } 1923 } 1924 } else { 1925 ptr[4] = LE_32(PMCIN_DATADIR_NONE); 1926 CLEAN_MESSAGE(ptr, 12); 1927 } 1928 xp->actv_cnt++; 1929 if (xp->actv_cnt > xp->maxdepth) { 1930 xp->maxdepth = xp->actv_cnt; 1931 pmcs_prt(pwp, PMCS_PRT_DEBUG2, pwrk->phy, xp, "%s: max depth " 1932 "now %u", pwrk->phy->path, xp->maxdepth); 1933 } 1934 mutex_exit(&xp->statlock); 1935 1936 1937 #ifdef DEBUG 1938 /* 1939 * Generate a PMCOUT_STATUS_XFER_CMD_FRAME_ISSUED 1940 * event when this goes out on the wire. 1941 */ 1942 ptr[4] |= PMCIN_MESSAGE_REPORT; 1943 #endif 1944 /* 1945 * Fill in the SSP IU 1946 */ 1947 1948 bzero(&sc, sizeof (sas_ssp_cmd_iu_t)); 1949 bcopy((uint8_t *)&sp->cmd_lun->scsi_lun, sc.lun, sizeof (scsi_lun_t)); 1950 1951 switch (pkt->pkt_flags & FLAG_TAGMASK) { 1952 case FLAG_HTAG: 1953 sc.task_attribute = SAS_CMD_TASK_ATTR_HEAD; 1954 break; 1955 case FLAG_OTAG: 1956 sc.task_attribute = SAS_CMD_TASK_ATTR_ORDERED; 1957 break; 1958 case FLAG_STAG: 1959 default: 1960 sc.task_attribute = SAS_CMD_TASK_ATTR_SIMPLE; 1961 break; 1962 } 1963 (void) memcpy(sc.cdb, pkt->pkt_cdbp, 1964 min(SCSA_CDBLEN(sp), sizeof (sc.cdb))); 1965 (void) memcpy(&ptr[5], &sc, sizeof (sas_ssp_cmd_iu_t)); 1966 pwrk->state = PMCS_WORK_STATE_ONCHIP; 1967 mutex_exit(&pwrk->lock); 1968 pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL, 1969 "%s: giving pkt %p (tag %x) to the hardware", __func__, 1970 (void *)pkt, pwrk->htag); 1971 #ifdef DEBUG 1972 pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "SAS INI Message", ptr); 1973 #endif 1974 mutex_enter(&xp->aqlock); 1975 STAILQ_INSERT_TAIL(&xp->aq, sp, cmd_next); 1976 mutex_exit(&xp->aqlock); 1977 INC_IQ_ENTRY(pwp, iq); 1978 1979 /* 1980 * If we just submitted the last command queued from device state 1981 * recovery, clear the wq_recovery_tail pointer. 1982 */ 1983 mutex_enter(&xp->wqlock); 1984 if (xp->wq_recovery_tail == sp) { 1985 xp->wq_recovery_tail = NULL; 1986 } 1987 mutex_exit(&xp->wqlock); 1988 1989 return (PMCS_WQ_RUN_SUCCESS); 1990 } 1991 1992 /* 1993 * Complete a SAS command 1994 * 1995 * Called with pwrk lock held. 1996 * The free of pwrk releases the lock. 1997 */ 1998 1999 static void 2000 pmcs_SAS_done(pmcs_hw_t *pwp, pmcwork_t *pwrk, uint32_t *msg) 2001 { 2002 pmcs_cmd_t *sp = pwrk->arg; 2003 pmcs_phy_t *pptr = pwrk->phy; 2004 pmcs_xscsi_t *xp = pwrk->xp; 2005 struct scsi_pkt *pkt = CMD2PKT(sp); 2006 int dead; 2007 uint32_t sts; 2008 boolean_t aborted = B_FALSE; 2009 boolean_t do_ds_recovery = B_FALSE; 2010 2011 ASSERT(xp != NULL); 2012 ASSERT(sp != NULL); 2013 ASSERT(pptr != NULL); 2014 2015 DTRACE_PROBE4(pmcs__io__done, uint64_t, pkt->pkt_dma_len, int, 2016 (pkt->pkt_dma_flags & DDI_DMA_READ) != 0, hrtime_t, pwrk->start, 2017 hrtime_t, gethrtime()); 2018 2019 dead = pwrk->dead; 2020 2021 if (msg) { 2022 sts = LE_32(msg[2]); 2023 } else { 2024 sts = 0; 2025 } 2026 2027 if (dead != 0) { 2028 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, "%s: dead cmd tag " 2029 "0x%x for %s", __func__, pwrk->htag, pptr->path); 2030 goto out; 2031 } 2032 2033 if (sts == PMCOUT_STATUS_ABORTED) { 2034 aborted = B_TRUE; 2035 } 2036 2037 if (pwrk->state == PMCS_WORK_STATE_TIMED_OUT) { 2038 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2039 "%s: cmd 0x%p (tag 0x%x) timed out for %s", 2040 __func__, (void *)sp, pwrk->htag, pptr->path); 2041 CMD2PKT(sp)->pkt_scbp[0] = STATUS_GOOD; 2042 CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET | 2043 STATE_SENT_CMD; 2044 CMD2PKT(sp)->pkt_statistics |= STAT_TIMEOUT; 2045 goto out; 2046 } 2047 2048 /* 2049 * If the status isn't okay but not underflow, 2050 * step to the side and parse the (possible) error. 2051 */ 2052 #ifdef DEBUG 2053 if (msg) { 2054 pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "Outbound Message", msg); 2055 } 2056 #endif 2057 if (!msg) { 2058 goto out; 2059 } 2060 2061 switch (sts) { 2062 case PMCOUT_STATUS_OPEN_CNX_ERROR_IT_NEXUS_LOSS: 2063 case PMCOUT_STATUS_IO_DS_NON_OPERATIONAL: 2064 case PMCOUT_STATUS_IO_DS_IN_RECOVERY: 2065 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2066 "%s: PHY %s requires DS recovery (status=%d)", 2067 __func__, pptr->path, sts); 2068 do_ds_recovery = B_TRUE; 2069 break; 2070 case PMCOUT_STATUS_UNDERFLOW: 2071 (void) pmcs_set_resid(pkt, pkt->pkt_dma_len, LE_32(msg[3])); 2072 pmcs_prt(pwp, PMCS_PRT_DEBUG_UNDERFLOW, NULL, NULL, 2073 "%s: underflow %u for cdb 0x%x", 2074 __func__, LE_32(msg[3]), pkt->pkt_cdbp[0] & 0xff); 2075 sts = PMCOUT_STATUS_OK; 2076 msg[3] = 0; 2077 break; 2078 case PMCOUT_STATUS_OK: 2079 pkt->pkt_resid = 0; 2080 break; 2081 } 2082 2083 if (sts != PMCOUT_STATUS_OK) { 2084 pmcs_ioerror(pwp, SAS, pwrk, msg, sts); 2085 } else { 2086 if (msg[3]) { 2087 uint8_t local[PMCS_QENTRY_SIZE << 1], *xd; 2088 sas_ssp_rsp_iu_t *rptr = (void *)local; 2089 const int lim = 2090 (PMCS_QENTRY_SIZE << 1) - SAS_RSP_HDR_SIZE; 2091 static const uint8_t ssp_rsp_evec[] = { 2092 0x58, 0x61, 0x56, 0x72, 0x00 2093 }; 2094 2095 /* 2096 * Transform the the first part of the response 2097 * to host canonical form. This gives us enough 2098 * information to figure out what to do with the 2099 * rest (which remains unchanged in the incoming 2100 * message which can be up to two queue entries 2101 * in length). 2102 */ 2103 pmcs_endian_transform(pwp, local, &msg[5], 2104 ssp_rsp_evec); 2105 xd = (uint8_t *)(&msg[5]); 2106 xd += SAS_RSP_HDR_SIZE; 2107 2108 if (rptr->datapres == SAS_RSP_DATAPRES_RESPONSE_DATA) { 2109 if (rptr->response_data_length != 4) { 2110 pmcs_print_entry(pwp, PMCS_PRT_DEBUG, 2111 "Bad SAS RESPONSE DATA LENGTH", 2112 msg); 2113 pkt->pkt_reason = CMD_TRAN_ERR; 2114 goto out; 2115 } 2116 (void) memcpy(&sts, xd, sizeof (uint32_t)); 2117 sts = BE_32(sts); 2118 /* 2119 * The only response code we should legally get 2120 * here is an INVALID FRAME response code. 2121 */ 2122 if (sts == SAS_RSP_INVALID_FRAME) { 2123 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2124 "%s: pkt %p tgt %u path %s " 2125 "completed: INVALID FRAME response", 2126 __func__, (void *)pkt, 2127 xp->target_num, pptr->path); 2128 } else { 2129 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2130 "%s: pkt %p tgt %u path %s " 2131 "completed: illegal response 0x%x", 2132 __func__, (void *)pkt, 2133 xp->target_num, pptr->path, sts); 2134 } 2135 pkt->pkt_reason = CMD_TRAN_ERR; 2136 goto out; 2137 } 2138 if (rptr->datapres == SAS_RSP_DATAPRES_SENSE_DATA) { 2139 uint32_t slen; 2140 slen = rptr->sense_data_length; 2141 if (slen > lim) { 2142 slen = lim; 2143 } 2144 pmcs_latch_status(pwp, sp, rptr->status, xd, 2145 slen, pptr->path); 2146 } else if (rptr->datapres == SAS_RSP_DATAPRES_NO_DATA) { 2147 pmcout_ssp_comp_t *sspcp; 2148 sspcp = (pmcout_ssp_comp_t *)msg; 2149 uint32_t *residp; 2150 /* 2151 * This is the case for a plain SCSI status. 2152 * Note: If RESC_V is set and we're here, there 2153 * is a residual. We need to find it and update 2154 * the packet accordingly. 2155 */ 2156 pmcs_latch_status(pwp, sp, rptr->status, NULL, 2157 0, pptr->path); 2158 2159 if (sspcp->resc_v) { 2160 /* 2161 * Point residual to the SSP_RESP_IU 2162 */ 2163 residp = (uint32_t *)(sspcp + 1); 2164 /* 2165 * param contains the number of bytes 2166 * between where the SSP_RESP_IU may 2167 * or may not be and the residual. 2168 * Increment residp by the appropriate 2169 * number of words: (param+resc_pad)/4). 2170 */ 2171 residp += (LE_32(sspcp->param) + 2172 sspcp->resc_pad) / 2173 sizeof (uint32_t); 2174 pmcs_prt(pwp, PMCS_PRT_DEBUG_UNDERFLOW, 2175 pptr, xp, "%s: tgt 0x%p " 2176 "residual %d for pkt 0x%p", 2177 __func__, (void *) xp, *residp, 2178 (void *) pkt); 2179 ASSERT(LE_32(*residp) <= 2180 pkt->pkt_dma_len); 2181 (void) pmcs_set_resid(pkt, 2182 pkt->pkt_dma_len, LE_32(*residp)); 2183 } 2184 } else { 2185 pmcs_print_entry(pwp, PMCS_PRT_DEBUG, 2186 "illegal SAS response", msg); 2187 pkt->pkt_reason = CMD_TRAN_ERR; 2188 goto out; 2189 } 2190 } else { 2191 pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0, 2192 pptr->path); 2193 } 2194 if (pkt->pkt_dma_len) { 2195 pkt->pkt_state |= STATE_XFERRED_DATA; 2196 } 2197 } 2198 pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp, 2199 "%s: pkt %p tgt %u done reason=%x state=%x resid=%ld status=%x", 2200 __func__, (void *)pkt, xp->target_num, pkt->pkt_reason, 2201 pkt->pkt_state, pkt->pkt_resid, pkt->pkt_scbp[0]); 2202 2203 if (pwrk->state == PMCS_WORK_STATE_ABORTED) { 2204 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2205 "%s: scsi_pkt 0x%p aborted for PHY %s; work = 0x%p", 2206 __func__, (void *)pkt, pptr->path, (void *)pwrk); 2207 aborted = B_TRUE; 2208 } 2209 2210 out: 2211 pmcs_dma_unload(pwp, sp); 2212 mutex_enter(&xp->statlock); 2213 2214 /* 2215 * If the device no longer has a PHY pointer, clear the PHY pointer 2216 * from the work structure before we free it. Otherwise, pmcs_pwork 2217 * may decrement the ref_count on a PHY that's been freed. 2218 */ 2219 if (xp->phy == NULL) { 2220 pwrk->phy = NULL; 2221 } 2222 2223 /* 2224 * We may arrive here due to a command timing out, which in turn 2225 * could be addressed in a different context. So, free the work 2226 * back, but only after confirming it's not already been freed 2227 * elsewhere. 2228 */ 2229 if (pwrk->htag != PMCS_TAG_FREE) { 2230 pmcs_pwork(pwp, pwrk); 2231 } 2232 2233 /* 2234 * If the device is gone, we only put this command on the completion 2235 * queue if the work structure is not marked dead. If it's marked 2236 * dead, it will already have been put there. 2237 */ 2238 if (xp->dev_gone) { 2239 mutex_exit(&xp->statlock); 2240 if (!dead) { 2241 mutex_enter(&xp->aqlock); 2242 STAILQ_REMOVE(&xp->aq, sp, pmcs_cmd, cmd_next); 2243 mutex_exit(&xp->aqlock); 2244 pmcs_prt(pwp, PMCS_PRT_DEBUG3, pptr, xp, 2245 "%s: Removing cmd 0x%p (htag 0x%x) from aq", 2246 __func__, (void *)sp, sp->cmd_tag); 2247 mutex_enter(&pwp->cq_lock); 2248 STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next); 2249 PMCS_CQ_RUN_LOCKED(pwp); 2250 mutex_exit(&pwp->cq_lock); 2251 pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp, 2252 "%s: Completing command for dead target 0x%p", 2253 __func__, (void *)xp); 2254 } 2255 return; 2256 } 2257 2258 ASSERT(xp->actv_cnt > 0); 2259 if (--(xp->actv_cnt) == 0) { 2260 if (xp->draining) { 2261 pmcs_prt(pwp, PMCS_PRT_DEBUG1, pptr, xp, 2262 "%s: waking up drain waiters", __func__); 2263 cv_signal(&pwp->drain_cv); 2264 } 2265 } 2266 mutex_exit(&xp->statlock); 2267 2268 /* 2269 * If the status is other than OK, determine if it's something that 2270 * is worth re-attempting enumeration. If so, mark the PHY. 2271 */ 2272 if (sts != PMCOUT_STATUS_OK) { 2273 pmcs_status_disposition(pptr, sts); 2274 } 2275 2276 if (dead == 0) { 2277 #ifdef DEBUG 2278 pmcs_cmd_t *wp; 2279 mutex_enter(&xp->aqlock); 2280 STAILQ_FOREACH(wp, &xp->aq, cmd_next) { 2281 if (wp == sp) { 2282 break; 2283 } 2284 } 2285 ASSERT(wp != NULL); 2286 #else 2287 mutex_enter(&xp->aqlock); 2288 #endif 2289 pmcs_prt(pwp, PMCS_PRT_DEBUG3, pptr, xp, 2290 "%s: Removing cmd 0x%p (htag 0x%x) from aq", __func__, 2291 (void *)sp, sp->cmd_tag); 2292 STAILQ_REMOVE(&xp->aq, sp, pmcs_cmd, cmd_next); 2293 if (aborted) { 2294 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2295 "%s: Aborted cmd for tgt 0x%p, signaling waiters", 2296 __func__, (void *)xp); 2297 cv_signal(&xp->abort_cv); 2298 } 2299 mutex_exit(&xp->aqlock); 2300 } 2301 2302 /* 2303 * If do_ds_recovery is set, we need to initiate device state 2304 * recovery. In this case, we put this I/O back on the head of 2305 * the wait queue to run again after recovery is complete 2306 */ 2307 if (do_ds_recovery) { 2308 mutex_enter(&xp->statlock); 2309 pmcs_start_dev_state_recovery(xp, pptr); 2310 mutex_exit(&xp->statlock); 2311 pmcs_prt(pwp, PMCS_PRT_DEBUG1, pptr, xp, "%s: Putting cmd 0x%p " 2312 "back on wq during recovery for tgt 0x%p", __func__, 2313 (void *)sp, (void *)xp); 2314 mutex_enter(&xp->wqlock); 2315 if (xp->wq_recovery_tail == NULL) { 2316 STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next); 2317 } else { 2318 /* 2319 * If there are other I/Os waiting at the head due to 2320 * device state recovery, add this one in the right spot 2321 * to maintain proper order. 2322 */ 2323 STAILQ_INSERT_AFTER(&xp->wq, xp->wq_recovery_tail, sp, 2324 cmd_next); 2325 } 2326 xp->wq_recovery_tail = sp; 2327 mutex_exit(&xp->wqlock); 2328 } else { 2329 /* 2330 * If we're not initiating device state recovery and this 2331 * command was not "dead", put it on the completion queue 2332 */ 2333 if (!dead) { 2334 mutex_enter(&pwp->cq_lock); 2335 STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next); 2336 PMCS_CQ_RUN_LOCKED(pwp); 2337 mutex_exit(&pwp->cq_lock); 2338 } 2339 } 2340 } 2341 2342 /* 2343 * Run a SATA command (normal reads and writes), 2344 * or block and schedule a SATL interpretation 2345 * of the command. 2346 * 2347 * Called with pwrk lock held, returns unlocked. 2348 */ 2349 2350 static int 2351 pmcs_SATA_run(pmcs_cmd_t *sp, pmcwork_t *pwrk) 2352 { 2353 pmcs_hw_t *pwp = CMD2PMC(sp); 2354 struct scsi_pkt *pkt = CMD2PKT(sp); 2355 pmcs_xscsi_t *xp; 2356 uint8_t cdb_base, asc, tag; 2357 uint32_t *ptr, iq, nblk, i, mtype; 2358 fis_t fis; 2359 size_t amt; 2360 uint64_t lba; 2361 2362 xp = pwrk->xp; 2363 ASSERT(xp != NULL); 2364 2365 /* 2366 * First, see if this is just a plain read/write command. 2367 * If not, we have to queue it up for processing, block 2368 * any additional commands from coming in, and wake up 2369 * the thread that will process this command. 2370 */ 2371 cdb_base = pkt->pkt_cdbp[0] & 0x1f; 2372 if (cdb_base != SCMD_READ && cdb_base != SCMD_WRITE) { 2373 pmcs_prt(pwp, PMCS_PRT_DEBUG1, NULL, NULL, 2374 "%s: special SATA cmd %p", __func__, (void *)sp); 2375 2376 ASSERT(xp->phy != NULL); 2377 pmcs_pwork(pwp, pwrk); 2378 pmcs_lock_phy(xp->phy); 2379 mutex_enter(&xp->statlock); 2380 xp->special_needed = 1; /* Set the special_needed flag */ 2381 STAILQ_INSERT_TAIL(&xp->sq, sp, cmd_next); 2382 if (pmcs_run_sata_special(pwp, xp)) { 2383 SCHEDULE_WORK(pwp, PMCS_WORK_SATA_RUN); 2384 } 2385 mutex_exit(&xp->statlock); 2386 pmcs_unlock_phy(xp->phy); 2387 2388 return (PMCS_WQ_RUN_SUCCESS); 2389 } 2390 2391 pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL, "%s: regular cmd", __func__); 2392 2393 mutex_enter(&xp->statlock); 2394 if (!xp->assigned) { 2395 mutex_exit(&xp->statlock); 2396 return (PMCS_WQ_RUN_FAIL_OTHER); 2397 } 2398 if (xp->special_running || xp->special_needed || xp->recover_wait) { 2399 mutex_exit(&xp->statlock); 2400 mutex_enter(&xp->wqlock); 2401 STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next); 2402 mutex_exit(&xp->wqlock); 2403 /* 2404 * By the time we get here the special 2405 * commands running or waiting to be run 2406 * may have come and gone, so kick our 2407 * worker to run the waiting queues 2408 * just in case. 2409 */ 2410 SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES); 2411 return (PMCS_WQ_RUN_FAIL_OTHER); 2412 } 2413 lba = xp->capacity; 2414 mutex_exit(&xp->statlock); 2415 2416 /* 2417 * Extract data length and lba parameters out of the command. The 2418 * function pmcs_SATA_rwparm returns a non-zero ASC value if the CDB 2419 * values are considered illegal. 2420 */ 2421 asc = pmcs_SATA_rwparm(pkt->pkt_cdbp, &nblk, &lba, lba); 2422 if (asc) { 2423 uint8_t sns[18]; 2424 bzero(sns, sizeof (sns)); 2425 sns[0] = 0xf0; 2426 sns[2] = 0x5; 2427 sns[12] = asc; 2428 pmcs_latch_status(pwp, sp, STATUS_CHECK, sns, sizeof (sns), 2429 pwrk->phy->path); 2430 pmcs_pwork(pwp, pwrk); 2431 mutex_enter(&pwp->cq_lock); 2432 STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next); 2433 PMCS_CQ_RUN_LOCKED(pwp); 2434 mutex_exit(&pwp->cq_lock); 2435 return (PMCS_WQ_RUN_SUCCESS); 2436 } 2437 2438 /* 2439 * If the command decodes as not moving any data, complete it here. 2440 */ 2441 amt = nblk; 2442 amt <<= 9; 2443 amt = pmcs_set_resid(pkt, amt, nblk << 9); 2444 if (amt == 0) { 2445 pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0, 2446 pwrk->phy->path); 2447 pmcs_pwork(pwp, pwrk); 2448 mutex_enter(&pwp->cq_lock); 2449 STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next); 2450 PMCS_CQ_RUN_LOCKED(pwp); 2451 mutex_exit(&pwp->cq_lock); 2452 return (PMCS_WQ_RUN_SUCCESS); 2453 } 2454 2455 /* 2456 * Get an inbound queue entry for this I/O 2457 */ 2458 GET_IO_IQ_ENTRY(pwp, ptr, xp->phy->device_id, iq); 2459 if (ptr == NULL) { 2460 /* 2461 * This is a temporary failure not likely to unblocked by 2462 * commands completing as the test for scheduling the 2463 * restart of work is a per-device test. 2464 */ 2465 mutex_enter(&xp->wqlock); 2466 STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next); 2467 mutex_exit(&xp->wqlock); 2468 pmcs_dma_unload(pwp, sp); 2469 SCHEDULE_WORK(pwp, PMCS_WORK_RUN_QUEUES); 2470 pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp, 2471 "%s: Failed to get IO IQ entry for tgt %d", 2472 __func__, xp->target_num); 2473 return (PMCS_WQ_RUN_FAIL_RES); 2474 } 2475 2476 /* 2477 * Get a tag. At this point, hold statlock until the tagmap is 2478 * updated (just prior to sending the cmd to the hardware). 2479 */ 2480 mutex_enter(&xp->statlock); 2481 for (tag = 0; tag < xp->qdepth; tag++) { 2482 if ((xp->tagmap & (1 << tag)) == 0) { 2483 break; 2484 } 2485 } 2486 2487 if (tag == xp->qdepth) { 2488 mutex_exit(&xp->statlock); 2489 mutex_exit(&pwp->iqp_lock[iq]); 2490 mutex_enter(&xp->wqlock); 2491 STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next); 2492 mutex_exit(&xp->wqlock); 2493 return (PMCS_WQ_RUN_FAIL_OTHER); 2494 } 2495 2496 sp->cmd_satltag = (uint8_t)tag; 2497 2498 /* 2499 * Set up the command 2500 */ 2501 bzero(fis, sizeof (fis)); 2502 ptr[0] = 2503 LE_32(PMCS_IOMB_IN_SAS(PMCS_OQ_IODONE, PMCIN_SATA_HOST_IO_START)); 2504 ptr[1] = LE_32(pwrk->htag); 2505 ptr[2] = LE_32(pwrk->phy->device_id); 2506 ptr[3] = LE_32(amt); 2507 2508 if (xp->ncq) { 2509 mtype = SATA_PROTOCOL_FPDMA | (tag << 16); 2510 fis[0] = ((nblk & 0xff) << 24) | (C_BIT << 8) | FIS_REG_H2DEV; 2511 if (cdb_base == SCMD_READ) { 2512 fis[0] |= (READ_FPDMA_QUEUED << 16); 2513 } else { 2514 fis[0] |= (WRITE_FPDMA_QUEUED << 16); 2515 } 2516 fis[1] = (FEATURE_LBA << 24) | (lba & 0xffffff); 2517 fis[2] = ((nblk & 0xff00) << 16) | ((lba >> 24) & 0xffffff); 2518 fis[3] = tag << 3; 2519 } else { 2520 int op; 2521 fis[0] = (C_BIT << 8) | FIS_REG_H2DEV; 2522 if (xp->pio) { 2523 mtype = SATA_PROTOCOL_PIO; 2524 if (cdb_base == SCMD_READ) { 2525 op = READ_SECTORS_EXT; 2526 } else { 2527 op = WRITE_SECTORS_EXT; 2528 } 2529 } else { 2530 mtype = SATA_PROTOCOL_DMA; 2531 if (cdb_base == SCMD_READ) { 2532 op = READ_DMA_EXT; 2533 } else { 2534 op = WRITE_DMA_EXT; 2535 } 2536 } 2537 fis[0] |= (op << 16); 2538 fis[1] = (FEATURE_LBA << 24) | (lba & 0xffffff); 2539 fis[2] = (lba >> 24) & 0xffffff; 2540 fis[3] = nblk; 2541 } 2542 2543 if (cdb_base == SCMD_READ) { 2544 ptr[4] = LE_32(mtype | PMCIN_DATADIR_2_INI); 2545 } else { 2546 ptr[4] = LE_32(mtype | PMCIN_DATADIR_2_DEV); 2547 } 2548 #ifdef DEBUG 2549 /* 2550 * Generate a PMCOUT_STATUS_XFER_CMD_FRAME_ISSUED 2551 * event when this goes out on the wire. 2552 */ 2553 ptr[4] |= PMCIN_MESSAGE_REPORT; 2554 #endif 2555 for (i = 0; i < (sizeof (fis_t))/(sizeof (uint32_t)); i++) { 2556 ptr[i+5] = LE_32(fis[i]); 2557 } 2558 if (pmcs_dma_load(pwp, sp, ptr)) { 2559 mutex_exit(&xp->statlock); 2560 mutex_exit(&pwp->iqp_lock[iq]); 2561 mutex_enter(&xp->wqlock); 2562 STAILQ_INSERT_HEAD(&xp->wq, sp, cmd_next); 2563 mutex_exit(&xp->wqlock); 2564 pmcs_prt(pwp, PMCS_PRT_DEBUG, NULL, xp, 2565 "%s: Failed to dma_load for tgt %d", 2566 __func__, xp->target_num); 2567 return (PMCS_WQ_RUN_FAIL_RES); 2568 2569 } 2570 2571 pwrk->state = PMCS_WORK_STATE_ONCHIP; 2572 mutex_exit(&pwrk->lock); 2573 xp->tagmap |= (1 << tag); 2574 xp->actv_cnt++; 2575 if (xp->actv_cnt > xp->maxdepth) { 2576 xp->maxdepth = xp->actv_cnt; 2577 pmcs_prt(pwp, PMCS_PRT_DEBUG2, pwrk->phy, xp, 2578 "%s: max depth now %u", pwrk->phy->path, xp->maxdepth); 2579 } 2580 mutex_exit(&xp->statlock); 2581 mutex_enter(&xp->aqlock); 2582 STAILQ_INSERT_TAIL(&xp->aq, sp, cmd_next); 2583 mutex_exit(&xp->aqlock); 2584 pmcs_prt(pwp, PMCS_PRT_DEBUG2, NULL, NULL, 2585 "%s: giving pkt %p to hardware", __func__, (void *)pkt); 2586 #ifdef DEBUG 2587 pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "SATA INI Message", ptr); 2588 #endif 2589 INC_IQ_ENTRY(pwp, iq); 2590 2591 return (PMCS_WQ_RUN_SUCCESS); 2592 } 2593 2594 /* 2595 * Complete a SATA command. Called with pwrk lock held. 2596 */ 2597 void 2598 pmcs_SATA_done(pmcs_hw_t *pwp, pmcwork_t *pwrk, uint32_t *msg) 2599 { 2600 pmcs_cmd_t *sp = pwrk->arg; 2601 struct scsi_pkt *pkt = CMD2PKT(sp); 2602 pmcs_phy_t *pptr = pwrk->phy; 2603 int dead; 2604 uint32_t sts; 2605 pmcs_xscsi_t *xp; 2606 boolean_t aborted = B_FALSE; 2607 2608 xp = pwrk->xp; 2609 ASSERT(xp != NULL); 2610 2611 DTRACE_PROBE4(pmcs__io__done, uint64_t, pkt->pkt_dma_len, int, 2612 (pkt->pkt_dma_flags & DDI_DMA_READ) != 0, hrtime_t, pwrk->start, 2613 hrtime_t, gethrtime()); 2614 2615 dead = pwrk->dead; 2616 2617 if (msg) { 2618 sts = LE_32(msg[2]); 2619 } else { 2620 sts = 0; 2621 } 2622 2623 if (dead != 0) { 2624 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, "%s: dead cmd tag " 2625 "0x%x for %s", __func__, pwrk->htag, pptr->path); 2626 goto out; 2627 } 2628 if ((pwrk->state == PMCS_WORK_STATE_TIMED_OUT) && 2629 (sts != PMCOUT_STATUS_ABORTED)) { 2630 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2631 "%s: cmd 0x%p (tag 0x%x) timed out for %s", 2632 __func__, (void *)sp, pwrk->htag, pptr->path); 2633 CMD2PKT(sp)->pkt_scbp[0] = STATUS_GOOD; 2634 /* pkt_reason already set to CMD_TIMEOUT */ 2635 ASSERT(CMD2PKT(sp)->pkt_reason == CMD_TIMEOUT); 2636 CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET | 2637 STATE_SENT_CMD; 2638 CMD2PKT(sp)->pkt_statistics |= STAT_TIMEOUT; 2639 goto out; 2640 } 2641 2642 pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp, "%s: pkt %p tgt %u done", 2643 __func__, (void *)pkt, xp->target_num); 2644 2645 /* 2646 * If the status isn't okay but not underflow, 2647 * step to the side and parse the (possible) error. 2648 */ 2649 #ifdef DEBUG 2650 if (msg) { 2651 pmcs_print_entry(pwp, PMCS_PRT_DEBUG3, "Outbound Message", msg); 2652 } 2653 #endif 2654 if (!msg) { 2655 goto out; 2656 } 2657 2658 /* 2659 * If the status isn't okay or we got a FIS response of some kind, 2660 * step to the side and parse the (possible) error. 2661 */ 2662 if ((sts != PMCOUT_STATUS_OK) || (LE_32(msg[3]) != 0)) { 2663 if (sts == PMCOUT_STATUS_IO_DS_NON_OPERATIONAL) { 2664 mutex_exit(&pwrk->lock); 2665 pmcs_lock_phy(pptr); 2666 mutex_enter(&xp->statlock); 2667 if ((xp->resetting == 0) && (xp->reset_success != 0) && 2668 (xp->reset_wait == 0)) { 2669 mutex_exit(&xp->statlock); 2670 if (pmcs_reset_phy(pwp, pptr, 2671 PMCS_PHYOP_LINK_RESET) != 0) { 2672 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2673 "%s: PHY (%s) Local Control/Link " 2674 "Reset FAILED as part of error " 2675 "recovery", __func__, pptr->path); 2676 } 2677 mutex_enter(&xp->statlock); 2678 } 2679 mutex_exit(&xp->statlock); 2680 pmcs_unlock_phy(pptr); 2681 mutex_enter(&pwrk->lock); 2682 } 2683 pmcs_ioerror(pwp, SATA, pwrk, msg, sts); 2684 } else { 2685 pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0, 2686 pwrk->phy->path); 2687 pkt->pkt_state |= STATE_XFERRED_DATA; 2688 pkt->pkt_resid = 0; 2689 } 2690 2691 pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp, 2692 "%s: pkt %p tgt %u done reason=%x state=%x resid=%ld status=%x", 2693 __func__, (void *)pkt, xp->target_num, pkt->pkt_reason, 2694 pkt->pkt_state, pkt->pkt_resid, pkt->pkt_scbp[0]); 2695 2696 if (pwrk->state == PMCS_WORK_STATE_ABORTED) { 2697 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2698 "%s: scsi_pkt 0x%p aborted for PHY %s; work = 0x%p", 2699 __func__, (void *)pkt, pptr->path, (void *)pwrk); 2700 aborted = B_TRUE; 2701 } 2702 2703 out: 2704 pmcs_dma_unload(pwp, sp); 2705 mutex_enter(&xp->statlock); 2706 xp->tagmap &= ~(1 << sp->cmd_satltag); 2707 2708 /* 2709 * If the device no longer has a PHY pointer, clear the PHY pointer 2710 * from the work structure before we free it. Otherwise, pmcs_pwork 2711 * may decrement the ref_count on a PHY that's been freed. 2712 */ 2713 if (xp->phy == NULL) { 2714 pwrk->phy = NULL; 2715 } 2716 2717 /* 2718 * We may arrive here due to a command timing out, which in turn 2719 * could be addressed in a different context. So, free the work 2720 * back, but only after confirming it's not already been freed 2721 * elsewhere. 2722 */ 2723 if (pwrk->htag != PMCS_TAG_FREE) { 2724 pmcs_pwork(pwp, pwrk); 2725 } 2726 2727 if (xp->dev_gone) { 2728 mutex_exit(&xp->statlock); 2729 if (!dead) { 2730 mutex_enter(&xp->aqlock); 2731 STAILQ_REMOVE(&xp->aq, sp, pmcs_cmd, cmd_next); 2732 mutex_exit(&xp->aqlock); 2733 pmcs_prt(pwp, PMCS_PRT_DEBUG3, pptr, xp, 2734 "%s: Removing cmd 0x%p (htag 0x%x) from aq", 2735 __func__, (void *)sp, sp->cmd_tag); 2736 mutex_enter(&pwp->cq_lock); 2737 STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next); 2738 PMCS_CQ_RUN_LOCKED(pwp); 2739 mutex_exit(&pwp->cq_lock); 2740 pmcs_prt(pwp, PMCS_PRT_DEBUG2, pptr, xp, 2741 "%s: Completing command for dead target 0x%p", 2742 __func__, (void *)xp); 2743 } 2744 return; 2745 } 2746 2747 ASSERT(xp->actv_cnt > 0); 2748 if (--(xp->actv_cnt) == 0) { 2749 if (xp->draining) { 2750 pmcs_prt(pwp, PMCS_PRT_DEBUG1, pptr, xp, 2751 "%s: waking up drain waiters", __func__); 2752 cv_signal(&pwp->drain_cv); 2753 } else if (xp->special_needed) { 2754 SCHEDULE_WORK(pwp, PMCS_WORK_SATA_RUN); 2755 } 2756 } 2757 mutex_exit(&xp->statlock); 2758 2759 /* 2760 * If the status is other than OK, determine if it's something that 2761 * is worth re-attempting enumeration. If so, mark the PHY. 2762 */ 2763 if (sts != PMCOUT_STATUS_OK) { 2764 pmcs_status_disposition(pptr, sts); 2765 } 2766 2767 if (dead == 0) { 2768 #ifdef DEBUG 2769 pmcs_cmd_t *wp; 2770 mutex_enter(&xp->aqlock); 2771 STAILQ_FOREACH(wp, &xp->aq, cmd_next) { 2772 if (wp == sp) { 2773 break; 2774 } 2775 } 2776 ASSERT(wp != NULL); 2777 #else 2778 mutex_enter(&xp->aqlock); 2779 #endif 2780 STAILQ_REMOVE(&xp->aq, sp, pmcs_cmd, cmd_next); 2781 if (aborted) { 2782 pmcs_prt(pwp, PMCS_PRT_DEBUG, pptr, xp, 2783 "%s: Aborted cmd for tgt 0x%p, signaling waiters", 2784 __func__, (void *)xp); 2785 cv_signal(&xp->abort_cv); 2786 } 2787 mutex_exit(&xp->aqlock); 2788 mutex_enter(&pwp->cq_lock); 2789 STAILQ_INSERT_TAIL(&pwp->cq, sp, cmd_next); 2790 PMCS_CQ_RUN_LOCKED(pwp); 2791 mutex_exit(&pwp->cq_lock); 2792 } 2793 } 2794 2795 static uint8_t 2796 pmcs_SATA_rwparm(uint8_t *cdb, uint32_t *xfr, uint64_t *lba, uint64_t lbamax) 2797 { 2798 uint8_t asc = 0; 2799 switch (cdb[0]) { 2800 case SCMD_READ_G5: 2801 case SCMD_WRITE_G5: 2802 *xfr = 2803 (((uint32_t)cdb[10]) << 24) | 2804 (((uint32_t)cdb[11]) << 16) | 2805 (((uint32_t)cdb[12]) << 8) | 2806 ((uint32_t)cdb[13]); 2807 *lba = 2808 (((uint64_t)cdb[2]) << 56) | 2809 (((uint64_t)cdb[3]) << 48) | 2810 (((uint64_t)cdb[4]) << 40) | 2811 (((uint64_t)cdb[5]) << 32) | 2812 (((uint64_t)cdb[6]) << 24) | 2813 (((uint64_t)cdb[7]) << 16) | 2814 (((uint64_t)cdb[8]) << 8) | 2815 ((uint64_t)cdb[9]); 2816 /* Check for illegal bits */ 2817 if (cdb[15]) { 2818 asc = 0x24; /* invalid field in cdb */ 2819 } 2820 break; 2821 case SCMD_READ_G4: 2822 case SCMD_WRITE_G4: 2823 *xfr = 2824 (((uint32_t)cdb[6]) << 16) | 2825 (((uint32_t)cdb[7]) << 8) | 2826 ((uint32_t)cdb[8]); 2827 *lba = 2828 (((uint32_t)cdb[2]) << 24) | 2829 (((uint32_t)cdb[3]) << 16) | 2830 (((uint32_t)cdb[4]) << 8) | 2831 ((uint32_t)cdb[5]); 2832 /* Check for illegal bits */ 2833 if (cdb[11]) { 2834 asc = 0x24; /* invalid field in cdb */ 2835 } 2836 break; 2837 case SCMD_READ_G1: 2838 case SCMD_WRITE_G1: 2839 *xfr = (((uint32_t)cdb[7]) << 8) | ((uint32_t)cdb[8]); 2840 *lba = 2841 (((uint32_t)cdb[2]) << 24) | 2842 (((uint32_t)cdb[3]) << 16) | 2843 (((uint32_t)cdb[4]) << 8) | 2844 ((uint32_t)cdb[5]); 2845 /* Check for illegal bits */ 2846 if (cdb[9]) { 2847 asc = 0x24; /* invalid field in cdb */ 2848 } 2849 break; 2850 case SCMD_READ: 2851 case SCMD_WRITE: 2852 *xfr = cdb[4]; 2853 if (*xfr == 0) { 2854 *xfr = 256; 2855 } 2856 *lba = 2857 (((uint32_t)cdb[1] & 0x1f) << 16) | 2858 (((uint32_t)cdb[2]) << 8) | 2859 ((uint32_t)cdb[3]); 2860 /* Check for illegal bits */ 2861 if (cdb[5]) { 2862 asc = 0x24; /* invalid field in cdb */ 2863 } 2864 break; 2865 } 2866 2867 if (asc == 0) { 2868 if ((*lba + *xfr) > lbamax) { 2869 asc = 0x21; /* logical block out of range */ 2870 } 2871 } 2872 return (asc); 2873 } 2874 2875 /* 2876 * Called with pwrk lock held. 2877 */ 2878 static void 2879 pmcs_ioerror(pmcs_hw_t *pwp, pmcs_dtype_t t, pmcwork_t *pwrk, uint32_t *w, 2880 uint32_t status) 2881 { 2882 static uint8_t por[] = { 2883 0xf0, 0x0, 0x6, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x28 2884 }; 2885 static uint8_t parity[] = { 2886 0xf0, 0x0, 0xb, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x47, 5 2887 }; 2888 const char *msg; 2889 char buf[20]; 2890 pmcs_cmd_t *sp = pwrk->arg; 2891 pmcs_phy_t *phyp = pwrk->phy; 2892 struct scsi_pkt *pkt = CMD2PKT(sp); 2893 uint32_t resid; 2894 2895 ASSERT(w != NULL); 2896 resid = LE_32(w[3]); 2897 2898 msg = pmcs_status_str(status); 2899 if (msg == NULL) { 2900 (void) snprintf(buf, sizeof (buf), "Error 0x%x", status); 2901 msg = buf; 2902 } 2903 2904 if (status != PMCOUT_STATUS_OK) { 2905 pmcs_prt(pwp, PMCS_PRT_DEBUG1, phyp, NULL, 2906 "%s: device %s tag 0x%x status %s @ %llu", __func__, 2907 phyp->path, pwrk->htag, msg, 2908 (unsigned long long)gethrtime()); 2909 } 2910 2911 pkt->pkt_reason = CMD_CMPLT; /* default reason */ 2912 2913 switch (status) { 2914 case PMCOUT_STATUS_OK: 2915 if (t == SATA) { 2916 int i; 2917 fis_t fis; 2918 for (i = 0; i < sizeof (fis) / sizeof (fis[0]); i++) { 2919 fis[i] = LE_32(w[4+i]); 2920 } 2921 if ((fis[0] & 0xff) != FIS_REG_D2H) { 2922 pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, NULL, 2923 "unexpected fis code 0x%x", fis[0] & 0xff); 2924 } else { 2925 pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, NULL, 2926 "FIS ERROR"); 2927 pmcs_fis_dump(pwp, fis); 2928 } 2929 pkt->pkt_reason = CMD_TRAN_ERR; 2930 break; 2931 } 2932 pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0, phyp->path); 2933 break; 2934 2935 case PMCOUT_STATUS_ABORTED: 2936 /* 2937 * Command successfully aborted. 2938 */ 2939 if (phyp->dead) { 2940 pkt->pkt_reason = CMD_DEV_GONE; 2941 pkt->pkt_state = STATE_GOT_BUS; 2942 } else if (pwrk->ssp_event != 0) { 2943 pkt->pkt_reason = CMD_TRAN_ERR; 2944 pkt->pkt_state = STATE_GOT_BUS; 2945 } else if (pwrk->state == PMCS_WORK_STATE_TIMED_OUT) { 2946 pkt->pkt_reason = CMD_TIMEOUT; 2947 pkt->pkt_statistics |= STAT_TIMEOUT; 2948 pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 2949 STATE_SENT_CMD; 2950 } else { 2951 pkt->pkt_reason = CMD_ABORTED; 2952 pkt->pkt_statistics |= STAT_ABORTED; 2953 pkt->pkt_state = STATE_GOT_BUS | STATE_GOT_TARGET | 2954 STATE_SENT_CMD; 2955 } 2956 2957 /* 2958 * PMCS_WORK_STATE_TIMED_OUT doesn't need to be preserved past 2959 * this point, so go ahead and mark it as aborted. 2960 */ 2961 pwrk->state = PMCS_WORK_STATE_ABORTED; 2962 break; 2963 2964 case PMCOUT_STATUS_UNDERFLOW: 2965 /* 2966 * This will only get called for SATA 2967 */ 2968 pkt->pkt_resid = resid; 2969 if (pkt->pkt_dma_len < pkt->pkt_resid) { 2970 (void) pmcs_set_resid(pkt, pkt->pkt_dma_len, resid); 2971 } 2972 pmcs_latch_status(pwp, sp, STATUS_GOOD, NULL, 0, phyp->path); 2973 break; 2974 2975 case PMCOUT_STATUS_NO_DEVICE: 2976 case PMCOUT_STATUS_XFER_ERROR_SATA_LINK_TIMEOUT: 2977 pkt->pkt_reason = CMD_DEV_GONE; 2978 break; 2979 2980 case PMCOUT_STATUS_OPEN_CNX_ERROR_WRONG_DESTINATION: 2981 /* 2982 * Need to do rediscovery. We probably have 2983 * the wrong device (disk swap), so kill 2984 * this one. 2985 */ 2986 case PMCOUT_STATUS_OPEN_CNX_PROTOCOL_NOT_SUPPORTED: 2987 case PMCOUT_STATUS_OPEN_CNX_ERROR_ZONE_VIOLATION: 2988 case PMCOUT_STATUS_OPEN_CNX_ERROR_CONNECTION_RATE_NOT_SUPPORTED: 2989 case PMCOUT_STATUS_OPEN_CNX_ERROR_UNKNOWN_ERROR: 2990 /* 2991 * Need to do rediscovery. 2992 */ 2993 if (!phyp->dead) { 2994 mutex_exit(&pwrk->lock); 2995 pmcs_lock_phy(pwrk->phy); 2996 pmcs_kill_changed(pwp, pwrk->phy, 0); 2997 pmcs_unlock_phy(pwrk->phy); 2998 mutex_enter(&pwrk->lock); 2999 pkt->pkt_reason = CMD_INCOMPLETE; 3000 pkt->pkt_state = STATE_GOT_BUS; 3001 } else { 3002 pkt->pkt_reason = CMD_DEV_GONE; 3003 } 3004 break; 3005 3006 case PMCOUT_STATUS_OPEN_CNX_ERROR_BREAK: 3007 case PMCOUT_STATUS_OPEN_CNX_ERROR_IT_NEXUS_LOSS: 3008 case PMCOUT_STATUS_OPENCNX_ERROR_BAD_DESTINATION: 3009 case PMCOUT_STATUS_IO_XFER_ERROR_NAK_RECEIVED: 3010 /* cmd is pending on the target */ 3011 case PMCOUT_STATUS_XFER_ERROR_OFFSET_MISMATCH: 3012 case PMCOUT_STATUS_XFER_ERROR_REJECTED_NCQ_MODE: 3013 /* transitory - commands sent while in NCQ failure mode */ 3014 case PMCOUT_STATUS_XFER_ERROR_ABORTED_NCQ_MODE: 3015 /* NCQ failure */ 3016 case PMCOUT_STATUS_IO_PORT_IN_RESET: 3017 case PMCOUT_STATUS_XFER_ERR_BREAK: 3018 case PMCOUT_STATUS_XFER_ERR_PHY_NOT_READY: 3019 pkt->pkt_reason = CMD_INCOMPLETE; 3020 pkt->pkt_state = STATE_GOT_BUS; 3021 break; 3022 3023 case PMCOUT_STATUS_IO_XFER_OPEN_RETRY_TIMEOUT: 3024 pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, phyp->target, 3025 "STATUS_BUSY for htag 0x%08x", sp->cmd_tag); 3026 pmcs_latch_status(pwp, sp, STATUS_BUSY, NULL, 0, phyp->path); 3027 break; 3028 3029 case PMCOUT_STATUS_OPEN_CNX_ERROR_STP_RESOURCES_BUSY: 3030 /* synthesize a RESERVATION CONFLICT */ 3031 pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, phyp->target, 3032 "%s: Potential affiliation active on 0x%" PRIx64, __func__, 3033 pmcs_barray2wwn(phyp->sas_address)); 3034 pmcs_latch_status(pwp, sp, STATUS_RESERVATION_CONFLICT, NULL, 3035 0, phyp->path); 3036 break; 3037 3038 case PMCOUT_STATUS_XFER_ERROR_ABORTED_DUE_TO_SRST: 3039 /* synthesize a power-on/reset */ 3040 pmcs_latch_status(pwp, sp, STATUS_CHECK, por, sizeof (por), 3041 phyp->path); 3042 break; 3043 3044 case PMCOUT_STATUS_XFER_ERROR_UNEXPECTED_PHASE: 3045 case PMCOUT_STATUS_XFER_ERROR_RDY_OVERRUN: 3046 case PMCOUT_STATUS_XFER_ERROR_RDY_NOT_EXPECTED: 3047 case PMCOUT_STATUS_XFER_ERROR_CMD_ISSUE_ACK_NAK_TIMEOUT: 3048 case PMCOUT_STATUS_XFER_ERROR_CMD_ISSUE_BREAK_BEFORE_ACK_NACK: 3049 case PMCOUT_STATUS_XFER_ERROR_CMD_ISSUE_PHY_DOWN_BEFORE_ACK_NAK: 3050 /* synthesize a PARITY ERROR */ 3051 pmcs_latch_status(pwp, sp, STATUS_CHECK, parity, 3052 sizeof (parity), phyp->path); 3053 break; 3054 3055 case PMCOUT_STATUS_IO_XFER_ERROR_DMA: 3056 case PMCOUT_STATUS_IO_NOT_VALID: 3057 case PMCOUT_STATUS_PROG_ERROR: 3058 case PMCOUT_STATUS_XFER_ERROR_PEER_ABORTED: 3059 case PMCOUT_STATUS_XFER_ERROR_SATA: /* non-NCQ failure */ 3060 default: 3061 pkt->pkt_reason = CMD_TRAN_ERR; 3062 break; 3063 } 3064 } 3065 3066 /* 3067 * Latch up SCSI status 3068 */ 3069 3070 void 3071 pmcs_latch_status(pmcs_hw_t *pwp, pmcs_cmd_t *sp, uint8_t status, 3072 uint8_t *snsp, size_t snslen, char *path) 3073 { 3074 static const char c1[] = 3075 "%s: Status Byte 0x%02x for CDB0=0x%02x (%02x %02x %02x) " 3076 "HTAG 0x%x @ %llu"; 3077 static const char c2[] = 3078 "%s: Status Byte 0x%02x for CDB0=0x%02x HTAG 0x%x @ %llu"; 3079 3080 CMD2PKT(sp)->pkt_state |= STATE_GOT_BUS | STATE_GOT_TARGET | 3081 STATE_SENT_CMD | STATE_GOT_STATUS; 3082 CMD2PKT(sp)->pkt_scbp[0] = status; 3083 3084 if (status == STATUS_CHECK && snsp && 3085 (size_t)SCSA_STSLEN(sp) >= sizeof (struct scsi_arq_status)) { 3086 struct scsi_arq_status *aqp = 3087 (void *) CMD2PKT(sp)->pkt_scbp; 3088 size_t amt = sizeof (struct scsi_extended_sense); 3089 uint8_t key = scsi_sense_key(snsp); 3090 uint8_t asc = scsi_sense_asc(snsp); 3091 uint8_t ascq = scsi_sense_ascq(snsp); 3092 if (amt > snslen) { 3093 amt = snslen; 3094 } 3095 pmcs_prt(pwp, PMCS_PRT_DEBUG_SCSI_STATUS, NULL, NULL, c1, path, 3096 status, CMD2PKT(sp)->pkt_cdbp[0] & 0xff, key, asc, ascq, 3097 sp->cmd_tag, (unsigned long long)gethrtime()); 3098 CMD2PKT(sp)->pkt_state |= STATE_ARQ_DONE; 3099 (*(uint8_t *)&aqp->sts_rqpkt_status) = STATUS_GOOD; 3100 aqp->sts_rqpkt_statistics = 0; 3101 aqp->sts_rqpkt_reason = CMD_CMPLT; 3102 aqp->sts_rqpkt_state = STATE_GOT_BUS | 3103 STATE_GOT_TARGET | STATE_SENT_CMD | 3104 STATE_XFERRED_DATA | STATE_GOT_STATUS; 3105 (void) memcpy(&aqp->sts_sensedata, snsp, amt); 3106 if (aqp->sts_sensedata.es_class != CLASS_EXTENDED_SENSE) { 3107 aqp->sts_rqpkt_reason = CMD_TRAN_ERR; 3108 aqp->sts_rqpkt_state = 0; 3109 aqp->sts_rqpkt_resid = 3110 sizeof (struct scsi_extended_sense); 3111 } else { 3112 aqp->sts_rqpkt_resid = 3113 sizeof (struct scsi_extended_sense) - amt; 3114 } 3115 } else if (status) { 3116 pmcs_prt(pwp, PMCS_PRT_DEBUG_SCSI_STATUS, NULL, NULL, c2, 3117 path, status, CMD2PKT(sp)->pkt_cdbp[0] & 0xff, 3118 sp->cmd_tag, (unsigned long long)gethrtime()); 3119 } 3120 3121 CMD2PKT(sp)->pkt_reason = CMD_CMPLT; 3122 } 3123 3124 /* 3125 * Calculate and set packet residual and return the amount 3126 * left over after applying various filters. 3127 */ 3128 size_t 3129 pmcs_set_resid(struct scsi_pkt *pkt, size_t amt, uint32_t cdbamt) 3130 { 3131 pkt->pkt_resid = cdbamt; 3132 if (amt > pkt->pkt_resid) { 3133 amt = pkt->pkt_resid; 3134 } 3135 if (amt > pkt->pkt_dma_len) { 3136 amt = pkt->pkt_dma_len; 3137 } 3138 return (amt); 3139 } 3140 3141 /* 3142 * Return the existing target softstate (unlocked) if there is one. If so, 3143 * the PHY is locked and that lock must be freed by the caller after the 3144 * target/PHY linkage is established. If there isn't one, and alloc_tgt is 3145 * TRUE, then allocate one. 3146 */ 3147 pmcs_xscsi_t * 3148 pmcs_get_target(pmcs_iport_t *iport, char *tgt_port, boolean_t alloc_tgt) 3149 { 3150 pmcs_hw_t *pwp = iport->pwp; 3151 pmcs_phy_t *phyp; 3152 pmcs_xscsi_t *tgt; 3153 uint64_t wwn; 3154 char unit_address[PMCS_MAX_UA_SIZE]; 3155 int ua_form = 1; 3156 3157 /* 3158 * Find the PHY for this target 3159 */ 3160 phyp = pmcs_find_phy_by_sas_address(pwp, iport, NULL, tgt_port); 3161 if (phyp == NULL) { 3162 pmcs_prt(pwp, PMCS_PRT_DEBUG3, NULL, NULL, 3163 "%s: No PHY for target @ %s", __func__, tgt_port); 3164 return (NULL); 3165 } 3166 3167 tgt = ddi_soft_state_bystr_get(iport->tgt_sstate, tgt_port); 3168 3169 if (tgt) { 3170 mutex_enter(&tgt->statlock); 3171 /* 3172 * There's already a target. Check its PHY pointer to see 3173 * if we need to clear the old linkages 3174 */ 3175 if (tgt->phy && (tgt->phy != phyp)) { 3176 pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt, 3177 "%s: Target PHY updated from %p to %p", __func__, 3178 (void *)tgt->phy, (void *)phyp); 3179 if (!IS_ROOT_PHY(tgt->phy)) { 3180 pmcs_dec_phy_ref_count(tgt->phy); 3181 pmcs_inc_phy_ref_count(phyp); 3182 } 3183 tgt->phy->target = NULL; 3184 } 3185 3186 /* 3187 * If this target has no PHY pointer and alloc_tgt is FALSE, 3188 * that implies we expect the target to already exist. This 3189 * implies that there has already been a tran_tgt_init on at 3190 * least one LU. 3191 */ 3192 if ((tgt->phy == NULL) && !alloc_tgt) { 3193 pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, tgt, 3194 "%s: Establish linkage from new PHY to old target @" 3195 "%s", __func__, tgt->unit_address); 3196 for (int idx = 0; idx < tgt->ref_count; idx++) { 3197 pmcs_inc_phy_ref_count(phyp); 3198 } 3199 } 3200 3201 /* 3202 * Set this target pointer back up, since it's been 3203 * through pmcs_clear_xp(). 3204 */ 3205 tgt->dev_gone = 0; 3206 tgt->assigned = 1; 3207 tgt->dtype = phyp->dtype; 3208 tgt->dev_state = PMCS_DEVICE_STATE_OPERATIONAL; 3209 tgt->phy = phyp; 3210 phyp->target = tgt; 3211 3212 mutex_exit(&tgt->statlock); 3213 return (tgt); 3214 } 3215 3216 /* 3217 * Make sure the PHY we found is on the correct iport 3218 */ 3219 if (phyp->iport != iport) { 3220 pmcs_prt(pwp, PMCS_PRT_DEBUG, phyp, NULL, 3221 "%s: No target at %s on this iport", __func__, tgt_port); 3222 pmcs_unlock_phy(phyp); 3223 return (NULL); 3224 } 3225 3226 /* 3227 * If this was just a lookup (i.e. alloc_tgt is false), return now. 3228 */ 3229 if (alloc_tgt == B_FALSE) { 3230 pmcs_unlock_phy(phyp); 3231 return (NULL); 3232 } 3233 3234 /* 3235 * Allocate the new softstate 3236 */ 3237 wwn = pmcs_barray2wwn(phyp->sas_address); 3238 (void) scsi_wwn_to_wwnstr(wwn, ua_form, unit_address); 3239 3240 if (ddi_soft_state_bystr_zalloc(iport->tgt_sstate, unit_address) != 3241 DDI_SUCCESS) { 3242 pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt, 3243 "%s: Couldn't alloc softstate for device at %s", 3244 __func__, unit_address); 3245 pmcs_unlock_phy(phyp); 3246 return (NULL); 3247 } 3248 3249 tgt = ddi_soft_state_bystr_get(iport->tgt_sstate, unit_address); 3250 ASSERT(tgt != NULL); 3251 STAILQ_INIT(&tgt->wq); 3252 STAILQ_INIT(&tgt->aq); 3253 STAILQ_INIT(&tgt->sq); 3254 mutex_init(&tgt->statlock, NULL, MUTEX_DRIVER, 3255 DDI_INTR_PRI(pwp->intr_pri)); 3256 mutex_init(&tgt->wqlock, NULL, MUTEX_DRIVER, 3257 DDI_INTR_PRI(pwp->intr_pri)); 3258 mutex_init(&tgt->aqlock, NULL, MUTEX_DRIVER, 3259 DDI_INTR_PRI(pwp->intr_pri)); 3260 cv_init(&tgt->reset_cv, NULL, CV_DRIVER, NULL); 3261 cv_init(&tgt->abort_cv, NULL, CV_DRIVER, NULL); 3262 list_create(&tgt->lun_list, sizeof (pmcs_lun_t), 3263 offsetof(pmcs_lun_t, lun_list_next)); 3264 tgt->qdepth = 1; 3265 tgt->target_num = PMCS_INVALID_TARGET_NUM; 3266 bcopy(unit_address, tgt->unit_address, PMCS_MAX_UA_SIZE); 3267 tgt->pwp = pwp; 3268 tgt->ua = strdup(iport->ua); 3269 tgt->phy = phyp; 3270 ASSERT((phyp->target == NULL) || (phyp->target == tgt)); 3271 if (phyp->target == NULL) { 3272 phyp->target = tgt; 3273 } 3274 3275 /* 3276 * Don't allocate LUN softstate for SMP targets 3277 */ 3278 if (phyp->dtype == EXPANDER) { 3279 return (tgt); 3280 } 3281 3282 if (ddi_soft_state_bystr_init(&tgt->lun_sstate, 3283 sizeof (pmcs_lun_t), PMCS_LUN_SSTATE_SZ) != 0) { 3284 pmcs_prt(pwp, PMCS_PRT_DEBUG_CONFIG, phyp, tgt, 3285 "%s: LUN soft_state_bystr_init failed", __func__); 3286 ddi_soft_state_bystr_free(iport->tgt_sstate, tgt_port); 3287 pmcs_unlock_phy(phyp); 3288 return (NULL); 3289 } 3290 3291 return (tgt); 3292 } 3293