1 /*- 2 * Common functions for CAM "type" (peripheral) drivers. 3 * 4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 5 * 6 * Copyright (c) 1997, 1998 Justin T. Gibbs. 7 * Copyright (c) 1997, 1998, 1999, 2000 Kenneth D. Merry. 8 * All rights reserved. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions, and the following disclaimer, 15 * without modification, immediately at the beginning of the file. 16 * 2. The name of the author may not be used to endorse or promote products 17 * derived from this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/types.h> 38 #include <sys/malloc.h> 39 #include <sys/kernel.h> 40 #include <sys/bio.h> 41 #include <sys/lock.h> 42 #include <sys/mutex.h> 43 #include <sys/buf.h> 44 #include <sys/proc.h> 45 #include <sys/devicestat.h> 46 #include <sys/bus.h> 47 #include <sys/sbuf.h> 48 #include <vm/vm.h> 49 #include <vm/vm_extern.h> 50 51 #include <cam/cam.h> 52 #include <cam/cam_ccb.h> 53 #include <cam/cam_queue.h> 54 #include <cam/cam_xpt_periph.h> 55 #include <cam/cam_periph.h> 56 #include <cam/cam_debug.h> 57 #include <cam/cam_sim.h> 58 59 #include <cam/scsi/scsi_all.h> 60 #include <cam/scsi/scsi_message.h> 61 #include <cam/scsi/scsi_pass.h> 62 63 static u_int camperiphnextunit(struct periph_driver *p_drv, 64 u_int newunit, int wired, 65 path_id_t pathid, target_id_t target, 66 lun_id_t lun); 67 static u_int camperiphunit(struct periph_driver *p_drv, 68 path_id_t pathid, target_id_t target, 69 lun_id_t lun); 70 static void camperiphdone(struct cam_periph *periph, 71 union ccb *done_ccb); 72 static void camperiphfree(struct cam_periph *periph); 73 static int camperiphscsistatuserror(union ccb *ccb, 74 union ccb **orig_ccb, 75 cam_flags camflags, 76 u_int32_t sense_flags, 77 int *openings, 78 u_int32_t *relsim_flags, 79 u_int32_t *timeout, 80 u_int32_t *action, 81 const char **action_string); 82 static int camperiphscsisenseerror(union ccb *ccb, 83 union ccb **orig_ccb, 84 cam_flags camflags, 85 u_int32_t sense_flags, 86 int *openings, 87 u_int32_t *relsim_flags, 88 u_int32_t *timeout, 89 u_int32_t *action, 90 const char **action_string); 91 static void cam_periph_devctl_notify(union ccb *ccb); 92 93 static int nperiph_drivers; 94 static int initialized = 0; 95 struct periph_driver **periph_drivers; 96 97 static MALLOC_DEFINE(M_CAMPERIPH, "CAM periph", "CAM peripheral buffers"); 98 99 static int periph_selto_delay = 1000; 100 TUNABLE_INT("kern.cam.periph_selto_delay", &periph_selto_delay); 101 static int periph_noresrc_delay = 500; 102 TUNABLE_INT("kern.cam.periph_noresrc_delay", &periph_noresrc_delay); 103 static int periph_busy_delay = 500; 104 TUNABLE_INT("kern.cam.periph_busy_delay", &periph_busy_delay); 105 106 107 void 108 periphdriver_register(void *data) 109 { 110 struct periph_driver *drv = (struct periph_driver *)data; 111 struct periph_driver **newdrivers, **old; 112 int ndrivers; 113 114 again: 115 ndrivers = nperiph_drivers + 2; 116 newdrivers = malloc(sizeof(*newdrivers) * ndrivers, M_CAMPERIPH, 117 M_WAITOK); 118 xpt_lock_buses(); 119 if (ndrivers != nperiph_drivers + 2) { 120 /* 121 * Lost race against itself; go around. 122 */ 123 xpt_unlock_buses(); 124 free(newdrivers, M_CAMPERIPH); 125 goto again; 126 } 127 if (periph_drivers) 128 bcopy(periph_drivers, newdrivers, 129 sizeof(*newdrivers) * nperiph_drivers); 130 newdrivers[nperiph_drivers] = drv; 131 newdrivers[nperiph_drivers + 1] = NULL; 132 old = periph_drivers; 133 periph_drivers = newdrivers; 134 nperiph_drivers++; 135 xpt_unlock_buses(); 136 if (old) 137 free(old, M_CAMPERIPH); 138 /* If driver marked as early or it is late now, initialize it. */ 139 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) || 140 initialized > 1) 141 (*drv->init)(); 142 } 143 144 int 145 periphdriver_unregister(void *data) 146 { 147 struct periph_driver *drv = (struct periph_driver *)data; 148 int error, n; 149 150 /* If driver marked as early or it is late now, deinitialize it. */ 151 if (((drv->flags & CAM_PERIPH_DRV_EARLY) != 0 && initialized > 0) || 152 initialized > 1) { 153 if (drv->deinit == NULL) { 154 printf("CAM periph driver '%s' doesn't have deinit.\n", 155 drv->driver_name); 156 return (EOPNOTSUPP); 157 } 158 error = drv->deinit(); 159 if (error != 0) 160 return (error); 161 } 162 163 xpt_lock_buses(); 164 for (n = 0; n < nperiph_drivers && periph_drivers[n] != drv; n++) 165 ; 166 KASSERT(n < nperiph_drivers, 167 ("Periph driver '%s' was not registered", drv->driver_name)); 168 for (; n + 1 < nperiph_drivers; n++) 169 periph_drivers[n] = periph_drivers[n + 1]; 170 periph_drivers[n + 1] = NULL; 171 nperiph_drivers--; 172 xpt_unlock_buses(); 173 return (0); 174 } 175 176 void 177 periphdriver_init(int level) 178 { 179 int i, early; 180 181 initialized = max(initialized, level); 182 for (i = 0; periph_drivers[i] != NULL; i++) { 183 early = (periph_drivers[i]->flags & CAM_PERIPH_DRV_EARLY) ? 1 : 2; 184 if (early == initialized) 185 (*periph_drivers[i]->init)(); 186 } 187 } 188 189 cam_status 190 cam_periph_alloc(periph_ctor_t *periph_ctor, 191 periph_oninv_t *periph_oninvalidate, 192 periph_dtor_t *periph_dtor, periph_start_t *periph_start, 193 char *name, cam_periph_type type, struct cam_path *path, 194 ac_callback_t *ac_callback, ac_code code, void *arg) 195 { 196 struct periph_driver **p_drv; 197 struct cam_sim *sim; 198 struct cam_periph *periph; 199 struct cam_periph *cur_periph; 200 path_id_t path_id; 201 target_id_t target_id; 202 lun_id_t lun_id; 203 cam_status status; 204 u_int init_level; 205 206 init_level = 0; 207 /* 208 * Handle Hot-Plug scenarios. If there is already a peripheral 209 * of our type assigned to this path, we are likely waiting for 210 * final close on an old, invalidated, peripheral. If this is 211 * the case, queue up a deferred call to the peripheral's async 212 * handler. If it looks like a mistaken re-allocation, complain. 213 */ 214 if ((periph = cam_periph_find(path, name)) != NULL) { 215 216 if ((periph->flags & CAM_PERIPH_INVALID) != 0 217 && (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) == 0) { 218 periph->flags |= CAM_PERIPH_NEW_DEV_FOUND; 219 periph->deferred_callback = ac_callback; 220 periph->deferred_ac = code; 221 return (CAM_REQ_INPROG); 222 } else { 223 printf("cam_periph_alloc: attempt to re-allocate " 224 "valid device %s%d rejected flags %#x " 225 "refcount %d\n", periph->periph_name, 226 periph->unit_number, periph->flags, 227 periph->refcount); 228 } 229 return (CAM_REQ_INVALID); 230 } 231 232 periph = (struct cam_periph *)malloc(sizeof(*periph), M_CAMPERIPH, 233 M_NOWAIT|M_ZERO); 234 235 if (periph == NULL) 236 return (CAM_RESRC_UNAVAIL); 237 238 init_level++; 239 240 241 sim = xpt_path_sim(path); 242 path_id = xpt_path_path_id(path); 243 target_id = xpt_path_target_id(path); 244 lun_id = xpt_path_lun_id(path); 245 periph->periph_start = periph_start; 246 periph->periph_dtor = periph_dtor; 247 periph->periph_oninval = periph_oninvalidate; 248 periph->type = type; 249 periph->periph_name = name; 250 periph->scheduled_priority = CAM_PRIORITY_NONE; 251 periph->immediate_priority = CAM_PRIORITY_NONE; 252 periph->refcount = 1; /* Dropped by invalidation. */ 253 periph->sim = sim; 254 SLIST_INIT(&periph->ccb_list); 255 status = xpt_create_path(&path, periph, path_id, target_id, lun_id); 256 if (status != CAM_REQ_CMP) 257 goto failure; 258 periph->path = path; 259 260 xpt_lock_buses(); 261 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) { 262 if (strcmp((*p_drv)->driver_name, name) == 0) 263 break; 264 } 265 if (*p_drv == NULL) { 266 printf("cam_periph_alloc: invalid periph name '%s'\n", name); 267 xpt_unlock_buses(); 268 xpt_free_path(periph->path); 269 free(periph, M_CAMPERIPH); 270 return (CAM_REQ_INVALID); 271 } 272 periph->unit_number = camperiphunit(*p_drv, path_id, target_id, lun_id); 273 cur_periph = TAILQ_FIRST(&(*p_drv)->units); 274 while (cur_periph != NULL 275 && cur_periph->unit_number < periph->unit_number) 276 cur_periph = TAILQ_NEXT(cur_periph, unit_links); 277 if (cur_periph != NULL) { 278 KASSERT(cur_periph->unit_number != periph->unit_number, ("duplicate units on periph list")); 279 TAILQ_INSERT_BEFORE(cur_periph, periph, unit_links); 280 } else { 281 TAILQ_INSERT_TAIL(&(*p_drv)->units, periph, unit_links); 282 (*p_drv)->generation++; 283 } 284 xpt_unlock_buses(); 285 286 init_level++; 287 288 status = xpt_add_periph(periph); 289 if (status != CAM_REQ_CMP) 290 goto failure; 291 292 init_level++; 293 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph created\n")); 294 295 status = periph_ctor(periph, arg); 296 297 if (status == CAM_REQ_CMP) 298 init_level++; 299 300 failure: 301 switch (init_level) { 302 case 4: 303 /* Initialized successfully */ 304 break; 305 case 3: 306 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n")); 307 xpt_remove_periph(periph); 308 /* FALLTHROUGH */ 309 case 2: 310 xpt_lock_buses(); 311 TAILQ_REMOVE(&(*p_drv)->units, periph, unit_links); 312 xpt_unlock_buses(); 313 xpt_free_path(periph->path); 314 /* FALLTHROUGH */ 315 case 1: 316 free(periph, M_CAMPERIPH); 317 /* FALLTHROUGH */ 318 case 0: 319 /* No cleanup to perform. */ 320 break; 321 default: 322 panic("%s: Unknown init level", __func__); 323 } 324 return(status); 325 } 326 327 /* 328 * Find a peripheral structure with the specified path, target, lun, 329 * and (optionally) type. If the name is NULL, this function will return 330 * the first peripheral driver that matches the specified path. 331 */ 332 struct cam_periph * 333 cam_periph_find(struct cam_path *path, char *name) 334 { 335 struct periph_driver **p_drv; 336 struct cam_periph *periph; 337 338 xpt_lock_buses(); 339 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) { 340 341 if (name != NULL && (strcmp((*p_drv)->driver_name, name) != 0)) 342 continue; 343 344 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) { 345 if (xpt_path_comp(periph->path, path) == 0) { 346 xpt_unlock_buses(); 347 cam_periph_assert(periph, MA_OWNED); 348 return(periph); 349 } 350 } 351 if (name != NULL) { 352 xpt_unlock_buses(); 353 return(NULL); 354 } 355 } 356 xpt_unlock_buses(); 357 return(NULL); 358 } 359 360 /* 361 * Find peripheral driver instances attached to the specified path. 362 */ 363 int 364 cam_periph_list(struct cam_path *path, struct sbuf *sb) 365 { 366 struct sbuf local_sb; 367 struct periph_driver **p_drv; 368 struct cam_periph *periph; 369 int count; 370 int sbuf_alloc_len; 371 372 sbuf_alloc_len = 16; 373 retry: 374 sbuf_new(&local_sb, NULL, sbuf_alloc_len, SBUF_FIXEDLEN); 375 count = 0; 376 xpt_lock_buses(); 377 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) { 378 379 TAILQ_FOREACH(periph, &(*p_drv)->units, unit_links) { 380 if (xpt_path_comp(periph->path, path) != 0) 381 continue; 382 383 if (sbuf_len(&local_sb) != 0) 384 sbuf_cat(&local_sb, ","); 385 386 sbuf_printf(&local_sb, "%s%d", periph->periph_name, 387 periph->unit_number); 388 389 if (sbuf_error(&local_sb) == ENOMEM) { 390 sbuf_alloc_len *= 2; 391 xpt_unlock_buses(); 392 sbuf_delete(&local_sb); 393 goto retry; 394 } 395 count++; 396 } 397 } 398 xpt_unlock_buses(); 399 sbuf_finish(&local_sb); 400 sbuf_cpy(sb, sbuf_data(&local_sb)); 401 sbuf_delete(&local_sb); 402 return (count); 403 } 404 405 cam_status 406 cam_periph_acquire(struct cam_periph *periph) 407 { 408 cam_status status; 409 410 status = CAM_REQ_CMP_ERR; 411 if (periph == NULL) 412 return (status); 413 414 xpt_lock_buses(); 415 if ((periph->flags & CAM_PERIPH_INVALID) == 0) { 416 periph->refcount++; 417 status = CAM_REQ_CMP; 418 } 419 xpt_unlock_buses(); 420 421 return (status); 422 } 423 424 void 425 cam_periph_doacquire(struct cam_periph *periph) 426 { 427 428 xpt_lock_buses(); 429 KASSERT(periph->refcount >= 1, 430 ("cam_periph_doacquire() with refcount == %d", periph->refcount)); 431 periph->refcount++; 432 xpt_unlock_buses(); 433 } 434 435 void 436 cam_periph_release_locked_buses(struct cam_periph *periph) 437 { 438 439 cam_periph_assert(periph, MA_OWNED); 440 KASSERT(periph->refcount >= 1, ("periph->refcount >= 1")); 441 if (--periph->refcount == 0) 442 camperiphfree(periph); 443 } 444 445 void 446 cam_periph_release_locked(struct cam_periph *periph) 447 { 448 449 if (periph == NULL) 450 return; 451 452 xpt_lock_buses(); 453 cam_periph_release_locked_buses(periph); 454 xpt_unlock_buses(); 455 } 456 457 void 458 cam_periph_release(struct cam_periph *periph) 459 { 460 struct mtx *mtx; 461 462 if (periph == NULL) 463 return; 464 465 cam_periph_assert(periph, MA_NOTOWNED); 466 mtx = cam_periph_mtx(periph); 467 mtx_lock(mtx); 468 cam_periph_release_locked(periph); 469 mtx_unlock(mtx); 470 } 471 472 int 473 cam_periph_hold(struct cam_periph *periph, int priority) 474 { 475 int error; 476 477 /* 478 * Increment the reference count on the peripheral 479 * while we wait for our lock attempt to succeed 480 * to ensure the peripheral doesn't disappear out 481 * from user us while we sleep. 482 */ 483 484 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 485 return (ENXIO); 486 487 cam_periph_assert(periph, MA_OWNED); 488 while ((periph->flags & CAM_PERIPH_LOCKED) != 0) { 489 periph->flags |= CAM_PERIPH_LOCK_WANTED; 490 if ((error = cam_periph_sleep(periph, periph, priority, 491 "caplck", 0)) != 0) { 492 cam_periph_release_locked(periph); 493 return (error); 494 } 495 if (periph->flags & CAM_PERIPH_INVALID) { 496 cam_periph_release_locked(periph); 497 return (ENXIO); 498 } 499 } 500 501 periph->flags |= CAM_PERIPH_LOCKED; 502 return (0); 503 } 504 505 void 506 cam_periph_unhold(struct cam_periph *periph) 507 { 508 509 cam_periph_assert(periph, MA_OWNED); 510 511 periph->flags &= ~CAM_PERIPH_LOCKED; 512 if ((periph->flags & CAM_PERIPH_LOCK_WANTED) != 0) { 513 periph->flags &= ~CAM_PERIPH_LOCK_WANTED; 514 wakeup(periph); 515 } 516 517 cam_periph_release_locked(periph); 518 } 519 520 /* 521 * Look for the next unit number that is not currently in use for this 522 * peripheral type starting at "newunit". Also exclude unit numbers that 523 * are reserved by for future "hardwiring" unless we already know that this 524 * is a potential wired device. Only assume that the device is "wired" the 525 * first time through the loop since after that we'll be looking at unit 526 * numbers that did not match a wiring entry. 527 */ 528 static u_int 529 camperiphnextunit(struct periph_driver *p_drv, u_int newunit, int wired, 530 path_id_t pathid, target_id_t target, lun_id_t lun) 531 { 532 struct cam_periph *periph; 533 char *periph_name; 534 int i, val, dunit, r; 535 const char *dname, *strval; 536 537 periph_name = p_drv->driver_name; 538 for (;;newunit++) { 539 540 for (periph = TAILQ_FIRST(&p_drv->units); 541 periph != NULL && periph->unit_number != newunit; 542 periph = TAILQ_NEXT(periph, unit_links)) 543 ; 544 545 if (periph != NULL && periph->unit_number == newunit) { 546 if (wired != 0) { 547 xpt_print(periph->path, "Duplicate Wired " 548 "Device entry!\n"); 549 xpt_print(periph->path, "Second device (%s " 550 "device at scbus%d target %d lun %d) will " 551 "not be wired\n", periph_name, pathid, 552 target, lun); 553 wired = 0; 554 } 555 continue; 556 } 557 if (wired) 558 break; 559 560 /* 561 * Don't match entries like "da 4" as a wired down 562 * device, but do match entries like "da 4 target 5" 563 * or even "da 4 scbus 1". 564 */ 565 i = 0; 566 dname = periph_name; 567 for (;;) { 568 r = resource_find_dev(&i, dname, &dunit, NULL, NULL); 569 if (r != 0) 570 break; 571 /* if no "target" and no specific scbus, skip */ 572 if (resource_int_value(dname, dunit, "target", &val) && 573 (resource_string_value(dname, dunit, "at",&strval)|| 574 strcmp(strval, "scbus") == 0)) 575 continue; 576 if (newunit == dunit) 577 break; 578 } 579 if (r != 0) 580 break; 581 } 582 return (newunit); 583 } 584 585 static u_int 586 camperiphunit(struct periph_driver *p_drv, path_id_t pathid, 587 target_id_t target, lun_id_t lun) 588 { 589 u_int unit; 590 int wired, i, val, dunit; 591 const char *dname, *strval; 592 char pathbuf[32], *periph_name; 593 594 periph_name = p_drv->driver_name; 595 snprintf(pathbuf, sizeof(pathbuf), "scbus%d", pathid); 596 unit = 0; 597 i = 0; 598 dname = periph_name; 599 for (wired = 0; resource_find_dev(&i, dname, &dunit, NULL, NULL) == 0; 600 wired = 0) { 601 if (resource_string_value(dname, dunit, "at", &strval) == 0) { 602 if (strcmp(strval, pathbuf) != 0) 603 continue; 604 wired++; 605 } 606 if (resource_int_value(dname, dunit, "target", &val) == 0) { 607 if (val != target) 608 continue; 609 wired++; 610 } 611 if (resource_int_value(dname, dunit, "lun", &val) == 0) { 612 if (val != lun) 613 continue; 614 wired++; 615 } 616 if (wired != 0) { 617 unit = dunit; 618 break; 619 } 620 } 621 622 /* 623 * Either start from 0 looking for the next unit or from 624 * the unit number given in the resource config. This way, 625 * if we have wildcard matches, we don't return the same 626 * unit number twice. 627 */ 628 unit = camperiphnextunit(p_drv, unit, wired, pathid, target, lun); 629 630 return (unit); 631 } 632 633 void 634 cam_periph_invalidate(struct cam_periph *periph) 635 { 636 637 cam_periph_assert(periph, MA_OWNED); 638 /* 639 * We only call this routine the first time a peripheral is 640 * invalidated. 641 */ 642 if ((periph->flags & CAM_PERIPH_INVALID) != 0) 643 return; 644 645 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph invalidated\n")); 646 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) { 647 struct sbuf sb; 648 char buffer[160]; 649 650 sbuf_new(&sb, buffer, 160, SBUF_FIXEDLEN); 651 xpt_denounce_periph_sbuf(periph, &sb); 652 sbuf_finish(&sb); 653 sbuf_putbuf(&sb); 654 } 655 periph->flags |= CAM_PERIPH_INVALID; 656 periph->flags &= ~CAM_PERIPH_NEW_DEV_FOUND; 657 if (periph->periph_oninval != NULL) 658 periph->periph_oninval(periph); 659 cam_periph_release_locked(periph); 660 } 661 662 static void 663 camperiphfree(struct cam_periph *periph) 664 { 665 struct periph_driver **p_drv; 666 struct periph_driver *drv; 667 668 cam_periph_assert(periph, MA_OWNED); 669 KASSERT(periph->periph_allocating == 0, ("%s%d: freed while allocating", 670 periph->periph_name, periph->unit_number)); 671 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++) { 672 if (strcmp((*p_drv)->driver_name, periph->periph_name) == 0) 673 break; 674 } 675 if (*p_drv == NULL) { 676 printf("camperiphfree: attempt to free non-existant periph\n"); 677 return; 678 } 679 /* 680 * Cache a pointer to the periph_driver structure. If a 681 * periph_driver is added or removed from the array (see 682 * periphdriver_register()) while we drop the toplogy lock 683 * below, p_drv may change. This doesn't protect against this 684 * particular periph_driver going away. That will require full 685 * reference counting in the periph_driver infrastructure. 686 */ 687 drv = *p_drv; 688 689 /* 690 * We need to set this flag before dropping the topology lock, to 691 * let anyone who is traversing the list that this peripheral is 692 * about to be freed, and there will be no more reference count 693 * checks. 694 */ 695 periph->flags |= CAM_PERIPH_FREE; 696 697 /* 698 * The peripheral destructor semantics dictate calling with only the 699 * SIM mutex held. Since it might sleep, it should not be called 700 * with the topology lock held. 701 */ 702 xpt_unlock_buses(); 703 704 /* 705 * We need to call the peripheral destructor prior to removing the 706 * peripheral from the list. Otherwise, we risk running into a 707 * scenario where the peripheral unit number may get reused 708 * (because it has been removed from the list), but some resources 709 * used by the peripheral are still hanging around. In particular, 710 * the devfs nodes used by some peripherals like the pass(4) driver 711 * aren't fully cleaned up until the destructor is run. If the 712 * unit number is reused before the devfs instance is fully gone, 713 * devfs will panic. 714 */ 715 if (periph->periph_dtor != NULL) 716 periph->periph_dtor(periph); 717 718 /* 719 * The peripheral list is protected by the topology lock. 720 */ 721 xpt_lock_buses(); 722 723 TAILQ_REMOVE(&drv->units, periph, unit_links); 724 drv->generation++; 725 726 xpt_remove_periph(periph); 727 728 xpt_unlock_buses(); 729 if ((periph->flags & CAM_PERIPH_ANNOUNCED) && !rebooting) 730 xpt_print(periph->path, "Periph destroyed\n"); 731 else 732 CAM_DEBUG(periph->path, CAM_DEBUG_INFO, ("Periph destroyed\n")); 733 734 if (periph->flags & CAM_PERIPH_NEW_DEV_FOUND) { 735 union ccb ccb; 736 void *arg; 737 738 switch (periph->deferred_ac) { 739 case AC_FOUND_DEVICE: 740 ccb.ccb_h.func_code = XPT_GDEV_TYPE; 741 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 742 xpt_action(&ccb); 743 arg = &ccb; 744 break; 745 case AC_PATH_REGISTERED: 746 xpt_path_inq(&ccb.cpi, periph->path); 747 arg = &ccb; 748 break; 749 default: 750 arg = NULL; 751 break; 752 } 753 periph->deferred_callback(NULL, periph->deferred_ac, 754 periph->path, arg); 755 } 756 xpt_free_path(periph->path); 757 free(periph, M_CAMPERIPH); 758 xpt_lock_buses(); 759 } 760 761 /* 762 * Map user virtual pointers into kernel virtual address space, so we can 763 * access the memory. This is now a generic function that centralizes most 764 * of the sanity checks on the data flags, if any. 765 * This also only works for up to MAXPHYS memory. Since we use 766 * buffers to map stuff in and out, we're limited to the buffer size. 767 */ 768 int 769 cam_periph_mapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo, 770 u_int maxmap) 771 { 772 int numbufs, i, j; 773 int flags[CAM_PERIPH_MAXMAPS]; 774 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 775 u_int32_t lengths[CAM_PERIPH_MAXMAPS]; 776 u_int32_t dirs[CAM_PERIPH_MAXMAPS]; 777 778 if (maxmap == 0) 779 maxmap = DFLTPHYS; /* traditional default */ 780 else if (maxmap > MAXPHYS) 781 maxmap = MAXPHYS; /* for safety */ 782 switch(ccb->ccb_h.func_code) { 783 case XPT_DEV_MATCH: 784 if (ccb->cdm.match_buf_len == 0) { 785 printf("cam_periph_mapmem: invalid match buffer " 786 "length 0\n"); 787 return(EINVAL); 788 } 789 if (ccb->cdm.pattern_buf_len > 0) { 790 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 791 lengths[0] = ccb->cdm.pattern_buf_len; 792 dirs[0] = CAM_DIR_OUT; 793 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 794 lengths[1] = ccb->cdm.match_buf_len; 795 dirs[1] = CAM_DIR_IN; 796 numbufs = 2; 797 } else { 798 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 799 lengths[0] = ccb->cdm.match_buf_len; 800 dirs[0] = CAM_DIR_IN; 801 numbufs = 1; 802 } 803 /* 804 * This request will not go to the hardware, no reason 805 * to be so strict. vmapbuf() is able to map up to MAXPHYS. 806 */ 807 maxmap = MAXPHYS; 808 break; 809 case XPT_SCSI_IO: 810 case XPT_CONT_TARGET_IO: 811 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 812 return(0); 813 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 814 return (EINVAL); 815 data_ptrs[0] = &ccb->csio.data_ptr; 816 lengths[0] = ccb->csio.dxfer_len; 817 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 818 numbufs = 1; 819 break; 820 case XPT_ATA_IO: 821 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 822 return(0); 823 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 824 return (EINVAL); 825 data_ptrs[0] = &ccb->ataio.data_ptr; 826 lengths[0] = ccb->ataio.dxfer_len; 827 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 828 numbufs = 1; 829 break; 830 case XPT_MMC_IO: 831 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 832 return(0); 833 /* Two mappings: one for cmd->data and one for cmd->data->data */ 834 data_ptrs[0] = (unsigned char **)&ccb->mmcio.cmd.data; 835 lengths[0] = sizeof(struct mmc_data *); 836 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 837 data_ptrs[1] = (unsigned char **)&ccb->mmcio.cmd.data->data; 838 lengths[1] = ccb->mmcio.cmd.data->len; 839 dirs[1] = ccb->ccb_h.flags & CAM_DIR_MASK; 840 numbufs = 2; 841 break; 842 case XPT_SMP_IO: 843 data_ptrs[0] = &ccb->smpio.smp_request; 844 lengths[0] = ccb->smpio.smp_request_len; 845 dirs[0] = CAM_DIR_OUT; 846 data_ptrs[1] = &ccb->smpio.smp_response; 847 lengths[1] = ccb->smpio.smp_response_len; 848 dirs[1] = CAM_DIR_IN; 849 numbufs = 2; 850 break; 851 case XPT_NVME_IO: 852 case XPT_NVME_ADMIN: 853 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 854 return (0); 855 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 856 return (EINVAL); 857 data_ptrs[0] = &ccb->nvmeio.data_ptr; 858 lengths[0] = ccb->nvmeio.dxfer_len; 859 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 860 numbufs = 1; 861 break; 862 case XPT_DEV_ADVINFO: 863 if (ccb->cdai.bufsiz == 0) 864 return (0); 865 866 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 867 lengths[0] = ccb->cdai.bufsiz; 868 dirs[0] = CAM_DIR_IN; 869 numbufs = 1; 870 871 /* 872 * This request will not go to the hardware, no reason 873 * to be so strict. vmapbuf() is able to map up to MAXPHYS. 874 */ 875 maxmap = MAXPHYS; 876 break; 877 default: 878 return(EINVAL); 879 break; /* NOTREACHED */ 880 } 881 882 /* 883 * Check the transfer length and permissions first, so we don't 884 * have to unmap any previously mapped buffers. 885 */ 886 for (i = 0; i < numbufs; i++) { 887 888 flags[i] = 0; 889 890 /* 891 * The userland data pointer passed in may not be page 892 * aligned. vmapbuf() truncates the address to a page 893 * boundary, so if the address isn't page aligned, we'll 894 * need enough space for the given transfer length, plus 895 * whatever extra space is necessary to make it to the page 896 * boundary. 897 */ 898 if ((lengths[i] + 899 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)) > maxmap){ 900 printf("cam_periph_mapmem: attempt to map %lu bytes, " 901 "which is greater than %lu\n", 902 (long)(lengths[i] + 903 (((vm_offset_t)(*data_ptrs[i])) & PAGE_MASK)), 904 (u_long)maxmap); 905 return(E2BIG); 906 } 907 908 if (dirs[i] & CAM_DIR_OUT) { 909 flags[i] = BIO_WRITE; 910 } 911 912 if (dirs[i] & CAM_DIR_IN) { 913 flags[i] = BIO_READ; 914 } 915 916 } 917 918 /* 919 * This keeps the kernel stack of current thread from getting 920 * swapped. In low-memory situations where the kernel stack might 921 * otherwise get swapped out, this holds it and allows the thread 922 * to make progress and release the kernel mapped pages sooner. 923 * 924 * XXX KDM should I use P_NOSWAP instead? 925 */ 926 PHOLD(curproc); 927 928 for (i = 0; i < numbufs; i++) { 929 /* 930 * Get the buffer. 931 */ 932 mapinfo->bp[i] = getpbuf(NULL); 933 934 /* put our pointer in the data slot */ 935 mapinfo->bp[i]->b_data = *data_ptrs[i]; 936 937 /* save the user's data address */ 938 mapinfo->bp[i]->b_caller1 = *data_ptrs[i]; 939 940 /* set the transfer length, we know it's < MAXPHYS */ 941 mapinfo->bp[i]->b_bufsize = lengths[i]; 942 943 /* set the direction */ 944 mapinfo->bp[i]->b_iocmd = flags[i]; 945 946 /* 947 * Map the buffer into kernel memory. 948 * 949 * Note that useracc() alone is not a sufficient test. 950 * vmapbuf() can still fail due to a smaller file mapped 951 * into a larger area of VM, or if userland races against 952 * vmapbuf() after the useracc() check. 953 */ 954 if (vmapbuf(mapinfo->bp[i], 1) < 0) { 955 for (j = 0; j < i; ++j) { 956 *data_ptrs[j] = mapinfo->bp[j]->b_caller1; 957 vunmapbuf(mapinfo->bp[j]); 958 relpbuf(mapinfo->bp[j], NULL); 959 } 960 relpbuf(mapinfo->bp[i], NULL); 961 PRELE(curproc); 962 return(EACCES); 963 } 964 965 /* set our pointer to the new mapped area */ 966 *data_ptrs[i] = mapinfo->bp[i]->b_data; 967 968 mapinfo->num_bufs_used++; 969 } 970 971 /* 972 * Now that we've gotten this far, change ownership to the kernel 973 * of the buffers so that we don't run afoul of returning to user 974 * space with locks (on the buffer) held. 975 */ 976 for (i = 0; i < numbufs; i++) { 977 BUF_KERNPROC(mapinfo->bp[i]); 978 } 979 980 981 return(0); 982 } 983 984 /* 985 * Unmap memory segments mapped into kernel virtual address space by 986 * cam_periph_mapmem(). 987 */ 988 void 989 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo) 990 { 991 int numbufs, i; 992 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 993 994 if (mapinfo->num_bufs_used <= 0) { 995 /* nothing to free and the process wasn't held. */ 996 return; 997 } 998 999 switch (ccb->ccb_h.func_code) { 1000 case XPT_DEV_MATCH: 1001 numbufs = min(mapinfo->num_bufs_used, 2); 1002 1003 if (numbufs == 1) { 1004 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1005 } else { 1006 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1007 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1008 } 1009 break; 1010 case XPT_SCSI_IO: 1011 case XPT_CONT_TARGET_IO: 1012 data_ptrs[0] = &ccb->csio.data_ptr; 1013 numbufs = min(mapinfo->num_bufs_used, 1); 1014 break; 1015 case XPT_ATA_IO: 1016 data_ptrs[0] = &ccb->ataio.data_ptr; 1017 numbufs = min(mapinfo->num_bufs_used, 1); 1018 break; 1019 case XPT_SMP_IO: 1020 numbufs = min(mapinfo->num_bufs_used, 2); 1021 data_ptrs[0] = &ccb->smpio.smp_request; 1022 data_ptrs[1] = &ccb->smpio.smp_response; 1023 break; 1024 case XPT_DEV_ADVINFO: 1025 numbufs = min(mapinfo->num_bufs_used, 1); 1026 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1027 break; 1028 case XPT_NVME_IO: 1029 case XPT_NVME_ADMIN: 1030 data_ptrs[0] = &ccb->nvmeio.data_ptr; 1031 numbufs = min(mapinfo->num_bufs_used, 1); 1032 break; 1033 default: 1034 /* allow ourselves to be swapped once again */ 1035 PRELE(curproc); 1036 return; 1037 break; /* NOTREACHED */ 1038 } 1039 1040 for (i = 0; i < numbufs; i++) { 1041 /* Set the user's pointer back to the original value */ 1042 *data_ptrs[i] = mapinfo->bp[i]->b_caller1; 1043 1044 /* unmap the buffer */ 1045 vunmapbuf(mapinfo->bp[i]); 1046 1047 /* release the buffer */ 1048 relpbuf(mapinfo->bp[i], NULL); 1049 } 1050 1051 /* allow ourselves to be swapped once again */ 1052 PRELE(curproc); 1053 } 1054 1055 int 1056 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr, 1057 int (*error_routine)(union ccb *ccb, 1058 cam_flags camflags, 1059 u_int32_t sense_flags)) 1060 { 1061 union ccb *ccb; 1062 int error; 1063 int found; 1064 1065 error = found = 0; 1066 1067 switch(cmd){ 1068 case CAMGETPASSTHRU: 1069 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL); 1070 xpt_setup_ccb(&ccb->ccb_h, 1071 ccb->ccb_h.path, 1072 CAM_PRIORITY_NORMAL); 1073 ccb->ccb_h.func_code = XPT_GDEVLIST; 1074 1075 /* 1076 * Basically, the point of this is that we go through 1077 * getting the list of devices, until we find a passthrough 1078 * device. In the current version of the CAM code, the 1079 * only way to determine what type of device we're dealing 1080 * with is by its name. 1081 */ 1082 while (found == 0) { 1083 ccb->cgdl.index = 0; 1084 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS; 1085 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) { 1086 1087 /* we want the next device in the list */ 1088 xpt_action(ccb); 1089 if (strncmp(ccb->cgdl.periph_name, 1090 "pass", 4) == 0){ 1091 found = 1; 1092 break; 1093 } 1094 } 1095 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) && 1096 (found == 0)) { 1097 ccb->cgdl.periph_name[0] = '\0'; 1098 ccb->cgdl.unit_number = 0; 1099 break; 1100 } 1101 } 1102 1103 /* copy the result back out */ 1104 bcopy(ccb, addr, sizeof(union ccb)); 1105 1106 /* and release the ccb */ 1107 xpt_release_ccb(ccb); 1108 1109 break; 1110 default: 1111 error = ENOTTY; 1112 break; 1113 } 1114 return(error); 1115 } 1116 1117 static void 1118 cam_periph_done_panic(struct cam_periph *periph, union ccb *done_ccb) 1119 { 1120 1121 panic("%s: already done with ccb %p", __func__, done_ccb); 1122 } 1123 1124 static void 1125 cam_periph_done(struct cam_periph *periph, union ccb *done_ccb) 1126 { 1127 1128 /* Caller will release the CCB */ 1129 xpt_path_assert(done_ccb->ccb_h.path, MA_OWNED); 1130 done_ccb->ccb_h.cbfcnp = cam_periph_done_panic; 1131 wakeup(&done_ccb->ccb_h.cbfcnp); 1132 } 1133 1134 static void 1135 cam_periph_ccbwait(union ccb *ccb) 1136 { 1137 1138 if ((ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) { 1139 while (ccb->ccb_h.cbfcnp != cam_periph_done_panic) 1140 xpt_path_sleep(ccb->ccb_h.path, &ccb->ccb_h.cbfcnp, 1141 PRIBIO, "cbwait", 0); 1142 } 1143 KASSERT(ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX && 1144 (ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG, 1145 ("%s: proceeding with incomplete ccb: ccb=%p, func_code=%#x, " 1146 "status=%#x, index=%d", __func__, ccb, ccb->ccb_h.func_code, 1147 ccb->ccb_h.status, ccb->ccb_h.pinfo.index)); 1148 } 1149 1150 int 1151 cam_periph_runccb(union ccb *ccb, 1152 int (*error_routine)(union ccb *ccb, 1153 cam_flags camflags, 1154 u_int32_t sense_flags), 1155 cam_flags camflags, u_int32_t sense_flags, 1156 struct devstat *ds) 1157 { 1158 struct bintime *starttime; 1159 struct bintime ltime; 1160 int error; 1161 bool sched_stopped; 1162 struct mtx *periph_mtx; 1163 struct cam_periph *periph; 1164 uint32_t timeout = 1; 1165 1166 starttime = NULL; 1167 xpt_path_assert(ccb->ccb_h.path, MA_OWNED); 1168 KASSERT((ccb->ccb_h.flags & CAM_UNLOCKED) == 0, 1169 ("%s: ccb=%p, func_code=%#x, flags=%#x", __func__, ccb, 1170 ccb->ccb_h.func_code, ccb->ccb_h.flags)); 1171 1172 /* 1173 * If the user has supplied a stats structure, and if we understand 1174 * this particular type of ccb, record the transaction start. 1175 */ 1176 if (ds != NULL && 1177 (ccb->ccb_h.func_code == XPT_SCSI_IO || 1178 ccb->ccb_h.func_code == XPT_ATA_IO || 1179 ccb->ccb_h.func_code == XPT_NVME_IO)) { 1180 starttime = <ime; 1181 binuptime(starttime); 1182 devstat_start_transaction(ds, starttime); 1183 } 1184 1185 sched_stopped = SCHEDULER_STOPPED(); 1186 ccb->ccb_h.cbfcnp = cam_periph_done; 1187 periph = xpt_path_periph(ccb->ccb_h.path); 1188 periph_mtx = cam_periph_mtx(periph); 1189 1190 /* 1191 * If we're polling, then we need to ensure that we have ample resources 1192 * in the periph. We also need to drop the periph lock while we're polling. 1193 * cam_periph_error can reschedule the ccb by calling xpt_action and returning 1194 * ERESTART, so we have to effect the polling in the do loop below. 1195 */ 1196 if (sched_stopped) { 1197 mtx_unlock(periph_mtx); 1198 timeout = xpt_poll_setup(ccb); 1199 } 1200 1201 if (timeout == 0) { 1202 ccb->ccb_h.status = CAM_RESRC_UNAVAIL; 1203 error = EBUSY; 1204 } else { 1205 xpt_action(ccb); 1206 do { 1207 if (!sched_stopped) 1208 cam_periph_ccbwait(ccb); 1209 else { 1210 xpt_pollwait(ccb, timeout); 1211 timeout = ccb->ccb_h.timeout * 10; 1212 } 1213 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 1214 error = 0; 1215 else if (error_routine != NULL) { 1216 ccb->ccb_h.cbfcnp = cam_periph_done; 1217 error = (*error_routine)(ccb, camflags, sense_flags); 1218 } else 1219 error = 0; 1220 } while (error == ERESTART); 1221 } 1222 1223 if (sched_stopped) 1224 mtx_lock(periph_mtx); 1225 1226 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 1227 cam_release_devq(ccb->ccb_h.path, 1228 /* relsim_flags */0, 1229 /* openings */0, 1230 /* timeout */0, 1231 /* getcount_only */ FALSE); 1232 ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 1233 } 1234 1235 if (ds != NULL) { 1236 uint32_t bytes; 1237 devstat_tag_type tag; 1238 bool valid = true; 1239 1240 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 1241 bytes = ccb->csio.dxfer_len - ccb->csio.resid; 1242 tag = (devstat_tag_type)(ccb->csio.tag_action & 0x3); 1243 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) { 1244 bytes = ccb->ataio.dxfer_len - ccb->ataio.resid; 1245 tag = (devstat_tag_type)0; 1246 } else if (ccb->ccb_h.func_code == XPT_NVME_IO) { 1247 bytes = ccb->nvmeio.dxfer_len; /* NB: resid no possible */ 1248 tag = (devstat_tag_type)0; 1249 } else { 1250 valid = false; 1251 } 1252 if (valid) 1253 devstat_end_transaction(ds, bytes, tag, 1254 ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) ? 1255 DEVSTAT_NO_DATA : (ccb->ccb_h.flags & CAM_DIR_OUT) ? 1256 DEVSTAT_WRITE : DEVSTAT_READ, NULL, starttime); 1257 } 1258 1259 return(error); 1260 } 1261 1262 void 1263 cam_freeze_devq(struct cam_path *path) 1264 { 1265 struct ccb_hdr ccb_h; 1266 1267 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_freeze_devq\n")); 1268 xpt_setup_ccb(&ccb_h, path, /*priority*/1); 1269 ccb_h.func_code = XPT_NOOP; 1270 ccb_h.flags = CAM_DEV_QFREEZE; 1271 xpt_action((union ccb *)&ccb_h); 1272 } 1273 1274 u_int32_t 1275 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags, 1276 u_int32_t openings, u_int32_t arg, 1277 int getcount_only) 1278 { 1279 struct ccb_relsim crs; 1280 1281 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("cam_release_devq(%u, %u, %u, %d)\n", 1282 relsim_flags, openings, arg, getcount_only)); 1283 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL); 1284 crs.ccb_h.func_code = XPT_REL_SIMQ; 1285 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0; 1286 crs.release_flags = relsim_flags; 1287 crs.openings = openings; 1288 crs.release_timeout = arg; 1289 xpt_action((union ccb *)&crs); 1290 return (crs.qfrozen_cnt); 1291 } 1292 1293 #define saved_ccb_ptr ppriv_ptr0 1294 static void 1295 camperiphdone(struct cam_periph *periph, union ccb *done_ccb) 1296 { 1297 union ccb *saved_ccb; 1298 cam_status status; 1299 struct scsi_start_stop_unit *scsi_cmd; 1300 int error_code, sense_key, asc, ascq; 1301 1302 scsi_cmd = (struct scsi_start_stop_unit *) 1303 &done_ccb->csio.cdb_io.cdb_bytes; 1304 status = done_ccb->ccb_h.status; 1305 1306 if ((status & CAM_STATUS_MASK) != CAM_REQ_CMP) { 1307 if (scsi_extract_sense_ccb(done_ccb, 1308 &error_code, &sense_key, &asc, &ascq)) { 1309 /* 1310 * If the error is "invalid field in CDB", 1311 * and the load/eject flag is set, turn the 1312 * flag off and try again. This is just in 1313 * case the drive in question barfs on the 1314 * load eject flag. The CAM code should set 1315 * the load/eject flag by default for 1316 * removable media. 1317 */ 1318 if ((scsi_cmd->opcode == START_STOP_UNIT) && 1319 ((scsi_cmd->how & SSS_LOEJ) != 0) && 1320 (asc == 0x24) && (ascq == 0x00)) { 1321 scsi_cmd->how &= ~SSS_LOEJ; 1322 if (status & CAM_DEV_QFRZN) { 1323 cam_release_devq(done_ccb->ccb_h.path, 1324 0, 0, 0, 0); 1325 done_ccb->ccb_h.status &= 1326 ~CAM_DEV_QFRZN; 1327 } 1328 xpt_action(done_ccb); 1329 goto out; 1330 } 1331 } 1332 if (cam_periph_error(done_ccb, 1333 0, SF_RETRY_UA | SF_NO_PRINT) == ERESTART) 1334 goto out; 1335 if (done_ccb->ccb_h.status & CAM_DEV_QFRZN) { 1336 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0); 1337 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 1338 } 1339 } else { 1340 /* 1341 * If we have successfully taken a device from the not 1342 * ready to ready state, re-scan the device and re-get 1343 * the inquiry information. Many devices (mostly disks) 1344 * don't properly report their inquiry information unless 1345 * they are spun up. 1346 */ 1347 if (scsi_cmd->opcode == START_STOP_UNIT) 1348 xpt_async(AC_INQ_CHANGED, done_ccb->ccb_h.path, NULL); 1349 } 1350 1351 /* 1352 * Perform the final retry with the original CCB so that final 1353 * error processing is performed by the owner of the CCB. 1354 */ 1355 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr; 1356 bcopy(saved_ccb, done_ccb, sizeof(*done_ccb)); 1357 xpt_free_ccb(saved_ccb); 1358 if (done_ccb->ccb_h.cbfcnp != camperiphdone) 1359 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG; 1360 xpt_action(done_ccb); 1361 1362 out: 1363 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */ 1364 cam_release_devq(done_ccb->ccb_h.path, 0, 0, 0, 0); 1365 } 1366 1367 /* 1368 * Generic Async Event handler. Peripheral drivers usually 1369 * filter out the events that require personal attention, 1370 * and leave the rest to this function. 1371 */ 1372 void 1373 cam_periph_async(struct cam_periph *periph, u_int32_t code, 1374 struct cam_path *path, void *arg) 1375 { 1376 switch (code) { 1377 case AC_LOST_DEVICE: 1378 cam_periph_invalidate(periph); 1379 break; 1380 default: 1381 break; 1382 } 1383 } 1384 1385 void 1386 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle) 1387 { 1388 struct ccb_getdevstats cgds; 1389 1390 xpt_setup_ccb(&cgds.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 1391 cgds.ccb_h.func_code = XPT_GDEV_STATS; 1392 xpt_action((union ccb *)&cgds); 1393 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle); 1394 } 1395 1396 void 1397 cam_periph_freeze_after_event(struct cam_periph *periph, 1398 struct timeval* event_time, u_int duration_ms) 1399 { 1400 struct timeval delta; 1401 struct timeval duration_tv; 1402 1403 if (!timevalisset(event_time)) 1404 return; 1405 1406 microtime(&delta); 1407 timevalsub(&delta, event_time); 1408 duration_tv.tv_sec = duration_ms / 1000; 1409 duration_tv.tv_usec = (duration_ms % 1000) * 1000; 1410 if (timevalcmp(&delta, &duration_tv, <)) { 1411 timevalsub(&duration_tv, &delta); 1412 1413 duration_ms = duration_tv.tv_sec * 1000; 1414 duration_ms += duration_tv.tv_usec / 1000; 1415 cam_freeze_devq(periph->path); 1416 cam_release_devq(periph->path, 1417 RELSIM_RELEASE_AFTER_TIMEOUT, 1418 /*reduction*/0, 1419 /*timeout*/duration_ms, 1420 /*getcount_only*/0); 1421 } 1422 1423 } 1424 1425 static int 1426 camperiphscsistatuserror(union ccb *ccb, union ccb **orig_ccb, 1427 cam_flags camflags, u_int32_t sense_flags, 1428 int *openings, u_int32_t *relsim_flags, 1429 u_int32_t *timeout, u_int32_t *action, const char **action_string) 1430 { 1431 int error; 1432 1433 switch (ccb->csio.scsi_status) { 1434 case SCSI_STATUS_OK: 1435 case SCSI_STATUS_COND_MET: 1436 case SCSI_STATUS_INTERMED: 1437 case SCSI_STATUS_INTERMED_COND_MET: 1438 error = 0; 1439 break; 1440 case SCSI_STATUS_CMD_TERMINATED: 1441 case SCSI_STATUS_CHECK_COND: 1442 error = camperiphscsisenseerror(ccb, orig_ccb, 1443 camflags, 1444 sense_flags, 1445 openings, 1446 relsim_flags, 1447 timeout, 1448 action, 1449 action_string); 1450 break; 1451 case SCSI_STATUS_QUEUE_FULL: 1452 { 1453 /* no decrement */ 1454 struct ccb_getdevstats cgds; 1455 1456 /* 1457 * First off, find out what the current 1458 * transaction counts are. 1459 */ 1460 xpt_setup_ccb(&cgds.ccb_h, 1461 ccb->ccb_h.path, 1462 CAM_PRIORITY_NORMAL); 1463 cgds.ccb_h.func_code = XPT_GDEV_STATS; 1464 xpt_action((union ccb *)&cgds); 1465 1466 /* 1467 * If we were the only transaction active, treat 1468 * the QUEUE FULL as if it were a BUSY condition. 1469 */ 1470 if (cgds.dev_active != 0) { 1471 int total_openings; 1472 1473 /* 1474 * Reduce the number of openings to 1475 * be 1 less than the amount it took 1476 * to get a queue full bounded by the 1477 * minimum allowed tag count for this 1478 * device. 1479 */ 1480 total_openings = cgds.dev_active + cgds.dev_openings; 1481 *openings = cgds.dev_active; 1482 if (*openings < cgds.mintags) 1483 *openings = cgds.mintags; 1484 if (*openings < total_openings) 1485 *relsim_flags = RELSIM_ADJUST_OPENINGS; 1486 else { 1487 /* 1488 * Some devices report queue full for 1489 * temporary resource shortages. For 1490 * this reason, we allow a minimum 1491 * tag count to be entered via a 1492 * quirk entry to prevent the queue 1493 * count on these devices from falling 1494 * to a pessimisticly low value. We 1495 * still wait for the next successful 1496 * completion, however, before queueing 1497 * more transactions to the device. 1498 */ 1499 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT; 1500 } 1501 *timeout = 0; 1502 error = ERESTART; 1503 *action &= ~SSQ_PRINT_SENSE; 1504 break; 1505 } 1506 /* FALLTHROUGH */ 1507 } 1508 case SCSI_STATUS_BUSY: 1509 /* 1510 * Restart the queue after either another 1511 * command completes or a 1 second timeout. 1512 */ 1513 if ((sense_flags & SF_RETRY_BUSY) != 0 || 1514 (ccb->ccb_h.retry_count--) > 0) { 1515 error = ERESTART; 1516 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT 1517 | RELSIM_RELEASE_AFTER_CMDCMPLT; 1518 *timeout = 1000; 1519 } else { 1520 error = EIO; 1521 } 1522 break; 1523 case SCSI_STATUS_RESERV_CONFLICT: 1524 default: 1525 error = EIO; 1526 break; 1527 } 1528 return (error); 1529 } 1530 1531 static int 1532 camperiphscsisenseerror(union ccb *ccb, union ccb **orig, 1533 cam_flags camflags, u_int32_t sense_flags, 1534 int *openings, u_int32_t *relsim_flags, 1535 u_int32_t *timeout, u_int32_t *action, const char **action_string) 1536 { 1537 struct cam_periph *periph; 1538 union ccb *orig_ccb = ccb; 1539 int error, recoveryccb; 1540 1541 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 1542 if (ccb->ccb_h.func_code == XPT_SCSI_IO && ccb->csio.bio != NULL) 1543 biotrack(ccb->csio.bio, __func__); 1544 #endif 1545 1546 periph = xpt_path_periph(ccb->ccb_h.path); 1547 recoveryccb = (ccb->ccb_h.cbfcnp == camperiphdone); 1548 if ((periph->flags & CAM_PERIPH_RECOVERY_INPROG) && !recoveryccb) { 1549 /* 1550 * If error recovery is already in progress, don't attempt 1551 * to process this error, but requeue it unconditionally 1552 * and attempt to process it once error recovery has 1553 * completed. This failed command is probably related to 1554 * the error that caused the currently active error recovery 1555 * action so our current recovery efforts should also 1556 * address this command. Be aware that the error recovery 1557 * code assumes that only one recovery action is in progress 1558 * on a particular peripheral instance at any given time 1559 * (e.g. only one saved CCB for error recovery) so it is 1560 * imperitive that we don't violate this assumption. 1561 */ 1562 error = ERESTART; 1563 *action &= ~SSQ_PRINT_SENSE; 1564 } else { 1565 scsi_sense_action err_action; 1566 struct ccb_getdev cgd; 1567 1568 /* 1569 * Grab the inquiry data for this device. 1570 */ 1571 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, CAM_PRIORITY_NORMAL); 1572 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1573 xpt_action((union ccb *)&cgd); 1574 1575 err_action = scsi_error_action(&ccb->csio, &cgd.inq_data, 1576 sense_flags); 1577 error = err_action & SS_ERRMASK; 1578 1579 /* 1580 * Do not autostart sequential access devices 1581 * to avoid unexpected tape loading. 1582 */ 1583 if ((err_action & SS_MASK) == SS_START && 1584 SID_TYPE(&cgd.inq_data) == T_SEQUENTIAL) { 1585 *action_string = "Will not autostart a " 1586 "sequential access device"; 1587 goto sense_error_done; 1588 } 1589 1590 /* 1591 * Avoid recovery recursion if recovery action is the same. 1592 */ 1593 if ((err_action & SS_MASK) >= SS_START && recoveryccb) { 1594 if (((err_action & SS_MASK) == SS_START && 1595 ccb->csio.cdb_io.cdb_bytes[0] == START_STOP_UNIT) || 1596 ((err_action & SS_MASK) == SS_TUR && 1597 (ccb->csio.cdb_io.cdb_bytes[0] == TEST_UNIT_READY))) { 1598 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO; 1599 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 1600 *timeout = 500; 1601 } 1602 } 1603 1604 /* 1605 * If the recovery action will consume a retry, 1606 * make sure we actually have retries available. 1607 */ 1608 if ((err_action & SSQ_DECREMENT_COUNT) != 0) { 1609 if (ccb->ccb_h.retry_count > 0 && 1610 (periph->flags & CAM_PERIPH_INVALID) == 0) 1611 ccb->ccb_h.retry_count--; 1612 else { 1613 *action_string = "Retries exhausted"; 1614 goto sense_error_done; 1615 } 1616 } 1617 1618 if ((err_action & SS_MASK) >= SS_START) { 1619 /* 1620 * Do common portions of commands that 1621 * use recovery CCBs. 1622 */ 1623 orig_ccb = xpt_alloc_ccb_nowait(); 1624 if (orig_ccb == NULL) { 1625 *action_string = "Can't allocate recovery CCB"; 1626 goto sense_error_done; 1627 } 1628 /* 1629 * Clear freeze flag for original request here, as 1630 * this freeze will be dropped as part of ERESTART. 1631 */ 1632 ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 1633 bcopy(ccb, orig_ccb, sizeof(*orig_ccb)); 1634 } 1635 1636 switch (err_action & SS_MASK) { 1637 case SS_NOP: 1638 *action_string = "No recovery action needed"; 1639 error = 0; 1640 break; 1641 case SS_RETRY: 1642 *action_string = "Retrying command (per sense data)"; 1643 error = ERESTART; 1644 break; 1645 case SS_FAIL: 1646 *action_string = "Unretryable error"; 1647 break; 1648 case SS_START: 1649 { 1650 int le; 1651 1652 /* 1653 * Send a start unit command to the device, and 1654 * then retry the command. 1655 */ 1656 *action_string = "Attempting to start unit"; 1657 periph->flags |= CAM_PERIPH_RECOVERY_INPROG; 1658 1659 /* 1660 * Check for removable media and set 1661 * load/eject flag appropriately. 1662 */ 1663 if (SID_IS_REMOVABLE(&cgd.inq_data)) 1664 le = TRUE; 1665 else 1666 le = FALSE; 1667 1668 scsi_start_stop(&ccb->csio, 1669 /*retries*/1, 1670 camperiphdone, 1671 MSG_SIMPLE_Q_TAG, 1672 /*start*/TRUE, 1673 /*load/eject*/le, 1674 /*immediate*/FALSE, 1675 SSD_FULL_SIZE, 1676 /*timeout*/50000); 1677 break; 1678 } 1679 case SS_TUR: 1680 { 1681 /* 1682 * Send a Test Unit Ready to the device. 1683 * If the 'many' flag is set, we send 120 1684 * test unit ready commands, one every half 1685 * second. Otherwise, we just send one TUR. 1686 * We only want to do this if the retry 1687 * count has not been exhausted. 1688 */ 1689 int retries; 1690 1691 if ((err_action & SSQ_MANY) != 0) { 1692 *action_string = "Polling device for readiness"; 1693 retries = 120; 1694 } else { 1695 *action_string = "Testing device for readiness"; 1696 retries = 1; 1697 } 1698 periph->flags |= CAM_PERIPH_RECOVERY_INPROG; 1699 scsi_test_unit_ready(&ccb->csio, 1700 retries, 1701 camperiphdone, 1702 MSG_SIMPLE_Q_TAG, 1703 SSD_FULL_SIZE, 1704 /*timeout*/5000); 1705 1706 /* 1707 * Accomplish our 500ms delay by deferring 1708 * the release of our device queue appropriately. 1709 */ 1710 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 1711 *timeout = 500; 1712 break; 1713 } 1714 default: 1715 panic("Unhandled error action %x", err_action); 1716 } 1717 1718 if ((err_action & SS_MASK) >= SS_START) { 1719 /* 1720 * Drop the priority, so that the recovery 1721 * CCB is the first to execute. Freeze the queue 1722 * after this command is sent so that we can 1723 * restore the old csio and have it queued in 1724 * the proper order before we release normal 1725 * transactions to the device. 1726 */ 1727 ccb->ccb_h.pinfo.priority--; 1728 ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1729 ccb->ccb_h.saved_ccb_ptr = orig_ccb; 1730 error = ERESTART; 1731 *orig = orig_ccb; 1732 } 1733 1734 sense_error_done: 1735 *action = err_action; 1736 } 1737 return (error); 1738 } 1739 1740 /* 1741 * Generic error handler. Peripheral drivers usually filter 1742 * out the errors that they handle in a unique manner, then 1743 * call this function. 1744 */ 1745 int 1746 cam_periph_error(union ccb *ccb, cam_flags camflags, 1747 u_int32_t sense_flags) 1748 { 1749 struct cam_path *newpath; 1750 union ccb *orig_ccb, *scan_ccb; 1751 struct cam_periph *periph; 1752 const char *action_string; 1753 cam_status status; 1754 int frozen, error, openings, devctl_err; 1755 u_int32_t action, relsim_flags, timeout; 1756 1757 action = SSQ_PRINT_SENSE; 1758 periph = xpt_path_periph(ccb->ccb_h.path); 1759 action_string = NULL; 1760 status = ccb->ccb_h.status; 1761 frozen = (status & CAM_DEV_QFRZN) != 0; 1762 status &= CAM_STATUS_MASK; 1763 devctl_err = openings = relsim_flags = timeout = 0; 1764 orig_ccb = ccb; 1765 1766 /* Filter the errors that should be reported via devctl */ 1767 switch (ccb->ccb_h.status & CAM_STATUS_MASK) { 1768 case CAM_CMD_TIMEOUT: 1769 case CAM_REQ_ABORTED: 1770 case CAM_REQ_CMP_ERR: 1771 case CAM_REQ_TERMIO: 1772 case CAM_UNREC_HBA_ERROR: 1773 case CAM_DATA_RUN_ERR: 1774 case CAM_SCSI_STATUS_ERROR: 1775 case CAM_ATA_STATUS_ERROR: 1776 case CAM_SMP_STATUS_ERROR: 1777 devctl_err++; 1778 break; 1779 default: 1780 break; 1781 } 1782 1783 switch (status) { 1784 case CAM_REQ_CMP: 1785 error = 0; 1786 action &= ~SSQ_PRINT_SENSE; 1787 break; 1788 case CAM_SCSI_STATUS_ERROR: 1789 error = camperiphscsistatuserror(ccb, &orig_ccb, 1790 camflags, sense_flags, &openings, &relsim_flags, 1791 &timeout, &action, &action_string); 1792 break; 1793 case CAM_AUTOSENSE_FAIL: 1794 error = EIO; /* we have to kill the command */ 1795 break; 1796 case CAM_UA_ABORT: 1797 case CAM_UA_TERMIO: 1798 case CAM_MSG_REJECT_REC: 1799 /* XXX Don't know that these are correct */ 1800 error = EIO; 1801 break; 1802 case CAM_SEL_TIMEOUT: 1803 if ((camflags & CAM_RETRY_SELTO) != 0) { 1804 if (ccb->ccb_h.retry_count > 0 && 1805 (periph->flags & CAM_PERIPH_INVALID) == 0) { 1806 ccb->ccb_h.retry_count--; 1807 error = ERESTART; 1808 1809 /* 1810 * Wait a bit to give the device 1811 * time to recover before we try again. 1812 */ 1813 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 1814 timeout = periph_selto_delay; 1815 break; 1816 } 1817 action_string = "Retries exhausted"; 1818 } 1819 /* FALLTHROUGH */ 1820 case CAM_DEV_NOT_THERE: 1821 error = ENXIO; 1822 action = SSQ_LOST; 1823 break; 1824 case CAM_REQ_INVALID: 1825 case CAM_PATH_INVALID: 1826 case CAM_NO_HBA: 1827 case CAM_PROVIDE_FAIL: 1828 case CAM_REQ_TOO_BIG: 1829 case CAM_LUN_INVALID: 1830 case CAM_TID_INVALID: 1831 case CAM_FUNC_NOTAVAIL: 1832 error = EINVAL; 1833 break; 1834 case CAM_SCSI_BUS_RESET: 1835 case CAM_BDR_SENT: 1836 /* 1837 * Commands that repeatedly timeout and cause these 1838 * kinds of error recovery actions, should return 1839 * CAM_CMD_TIMEOUT, which allows us to safely assume 1840 * that this command was an innocent bystander to 1841 * these events and should be unconditionally 1842 * retried. 1843 */ 1844 case CAM_REQUEUE_REQ: 1845 /* Unconditional requeue if device is still there */ 1846 if (periph->flags & CAM_PERIPH_INVALID) { 1847 action_string = "Periph was invalidated"; 1848 error = EIO; 1849 } else if (sense_flags & SF_NO_RETRY) { 1850 error = EIO; 1851 action_string = "Retry was blocked"; 1852 } else { 1853 error = ERESTART; 1854 action &= ~SSQ_PRINT_SENSE; 1855 } 1856 break; 1857 case CAM_RESRC_UNAVAIL: 1858 /* Wait a bit for the resource shortage to abate. */ 1859 timeout = periph_noresrc_delay; 1860 /* FALLTHROUGH */ 1861 case CAM_BUSY: 1862 if (timeout == 0) { 1863 /* Wait a bit for the busy condition to abate. */ 1864 timeout = periph_busy_delay; 1865 } 1866 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 1867 /* FALLTHROUGH */ 1868 case CAM_ATA_STATUS_ERROR: 1869 case CAM_REQ_CMP_ERR: 1870 case CAM_CMD_TIMEOUT: 1871 case CAM_UNEXP_BUSFREE: 1872 case CAM_UNCOR_PARITY: 1873 case CAM_DATA_RUN_ERR: 1874 default: 1875 if (periph->flags & CAM_PERIPH_INVALID) { 1876 error = EIO; 1877 action_string = "Periph was invalidated"; 1878 } else if (ccb->ccb_h.retry_count == 0) { 1879 error = EIO; 1880 action_string = "Retries exhausted"; 1881 } else if (sense_flags & SF_NO_RETRY) { 1882 error = EIO; 1883 action_string = "Retry was blocked"; 1884 } else { 1885 ccb->ccb_h.retry_count--; 1886 error = ERESTART; 1887 } 1888 break; 1889 } 1890 1891 if ((sense_flags & SF_PRINT_ALWAYS) || 1892 CAM_DEBUGGED(ccb->ccb_h.path, CAM_DEBUG_INFO)) 1893 action |= SSQ_PRINT_SENSE; 1894 else if (sense_flags & SF_NO_PRINT) 1895 action &= ~SSQ_PRINT_SENSE; 1896 if ((action & SSQ_PRINT_SENSE) != 0) 1897 cam_error_print(orig_ccb, CAM_ESF_ALL, CAM_EPF_ALL); 1898 if (error != 0 && (action & SSQ_PRINT_SENSE) != 0) { 1899 if (error != ERESTART) { 1900 if (action_string == NULL) 1901 action_string = "Unretryable error"; 1902 xpt_print(ccb->ccb_h.path, "Error %d, %s\n", 1903 error, action_string); 1904 } else if (action_string != NULL) 1905 xpt_print(ccb->ccb_h.path, "%s\n", action_string); 1906 else 1907 xpt_print(ccb->ccb_h.path, "Retrying command\n"); 1908 } 1909 1910 if (devctl_err && (error != 0 || (action & SSQ_PRINT_SENSE) != 0)) 1911 cam_periph_devctl_notify(orig_ccb); 1912 1913 if ((action & SSQ_LOST) != 0) { 1914 lun_id_t lun_id; 1915 1916 /* 1917 * For a selection timeout, we consider all of the LUNs on 1918 * the target to be gone. If the status is CAM_DEV_NOT_THERE, 1919 * then we only get rid of the device(s) specified by the 1920 * path in the original CCB. 1921 */ 1922 if (status == CAM_SEL_TIMEOUT) 1923 lun_id = CAM_LUN_WILDCARD; 1924 else 1925 lun_id = xpt_path_lun_id(ccb->ccb_h.path); 1926 1927 /* Should we do more if we can't create the path?? */ 1928 if (xpt_create_path(&newpath, periph, 1929 xpt_path_path_id(ccb->ccb_h.path), 1930 xpt_path_target_id(ccb->ccb_h.path), 1931 lun_id) == CAM_REQ_CMP) { 1932 1933 /* 1934 * Let peripheral drivers know that this 1935 * device has gone away. 1936 */ 1937 xpt_async(AC_LOST_DEVICE, newpath, NULL); 1938 xpt_free_path(newpath); 1939 } 1940 } 1941 1942 /* Broadcast UNIT ATTENTIONs to all periphs. */ 1943 if ((action & SSQ_UA) != 0) 1944 xpt_async(AC_UNIT_ATTENTION, orig_ccb->ccb_h.path, orig_ccb); 1945 1946 /* Rescan target on "Reported LUNs data has changed" */ 1947 if ((action & SSQ_RESCAN) != 0) { 1948 if (xpt_create_path(&newpath, NULL, 1949 xpt_path_path_id(ccb->ccb_h.path), 1950 xpt_path_target_id(ccb->ccb_h.path), 1951 CAM_LUN_WILDCARD) == CAM_REQ_CMP) { 1952 1953 scan_ccb = xpt_alloc_ccb_nowait(); 1954 if (scan_ccb != NULL) { 1955 scan_ccb->ccb_h.path = newpath; 1956 scan_ccb->ccb_h.func_code = XPT_SCAN_TGT; 1957 scan_ccb->crcn.flags = 0; 1958 xpt_rescan(scan_ccb); 1959 } else { 1960 xpt_print(newpath, 1961 "Can't allocate CCB to rescan target\n"); 1962 xpt_free_path(newpath); 1963 } 1964 } 1965 } 1966 1967 /* Attempt a retry */ 1968 if (error == ERESTART || error == 0) { 1969 if (frozen != 0) 1970 ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 1971 if (error == ERESTART) 1972 xpt_action(ccb); 1973 if (frozen != 0) 1974 cam_release_devq(ccb->ccb_h.path, 1975 relsim_flags, 1976 openings, 1977 timeout, 1978 /*getcount_only*/0); 1979 } 1980 1981 return (error); 1982 } 1983 1984 #define CAM_PERIPH_DEVD_MSG_SIZE 256 1985 1986 static void 1987 cam_periph_devctl_notify(union ccb *ccb) 1988 { 1989 struct cam_periph *periph; 1990 struct ccb_getdev *cgd; 1991 struct sbuf sb; 1992 int serr, sk, asc, ascq; 1993 char *sbmsg, *type; 1994 1995 sbmsg = malloc(CAM_PERIPH_DEVD_MSG_SIZE, M_CAMPERIPH, M_NOWAIT); 1996 if (sbmsg == NULL) 1997 return; 1998 1999 sbuf_new(&sb, sbmsg, CAM_PERIPH_DEVD_MSG_SIZE, SBUF_FIXEDLEN); 2000 2001 periph = xpt_path_periph(ccb->ccb_h.path); 2002 sbuf_printf(&sb, "device=%s%d ", periph->periph_name, 2003 periph->unit_number); 2004 2005 sbuf_printf(&sb, "serial=\""); 2006 if ((cgd = (struct ccb_getdev *)xpt_alloc_ccb_nowait()) != NULL) { 2007 xpt_setup_ccb(&cgd->ccb_h, ccb->ccb_h.path, 2008 CAM_PRIORITY_NORMAL); 2009 cgd->ccb_h.func_code = XPT_GDEV_TYPE; 2010 xpt_action((union ccb *)cgd); 2011 2012 if (cgd->ccb_h.status == CAM_REQ_CMP) 2013 sbuf_bcat(&sb, cgd->serial_num, cgd->serial_num_len); 2014 xpt_free_ccb((union ccb *)cgd); 2015 } 2016 sbuf_printf(&sb, "\" "); 2017 sbuf_printf(&sb, "cam_status=\"0x%x\" ", ccb->ccb_h.status); 2018 2019 switch (ccb->ccb_h.status & CAM_STATUS_MASK) { 2020 case CAM_CMD_TIMEOUT: 2021 sbuf_printf(&sb, "timeout=%d ", ccb->ccb_h.timeout); 2022 type = "timeout"; 2023 break; 2024 case CAM_SCSI_STATUS_ERROR: 2025 sbuf_printf(&sb, "scsi_status=%d ", ccb->csio.scsi_status); 2026 if (scsi_extract_sense_ccb(ccb, &serr, &sk, &asc, &ascq)) 2027 sbuf_printf(&sb, "scsi_sense=\"%02x %02x %02x %02x\" ", 2028 serr, sk, asc, ascq); 2029 type = "error"; 2030 break; 2031 case CAM_ATA_STATUS_ERROR: 2032 sbuf_printf(&sb, "RES=\""); 2033 ata_res_sbuf(&ccb->ataio.res, &sb); 2034 sbuf_printf(&sb, "\" "); 2035 type = "error"; 2036 break; 2037 default: 2038 type = "error"; 2039 break; 2040 } 2041 2042 if (ccb->ccb_h.func_code == XPT_SCSI_IO) { 2043 sbuf_printf(&sb, "CDB=\""); 2044 scsi_cdb_sbuf(scsiio_cdb_ptr(&ccb->csio), &sb); 2045 sbuf_printf(&sb, "\" "); 2046 } else if (ccb->ccb_h.func_code == XPT_ATA_IO) { 2047 sbuf_printf(&sb, "ACB=\""); 2048 ata_cmd_sbuf(&ccb->ataio.cmd, &sb); 2049 sbuf_printf(&sb, "\" "); 2050 } 2051 2052 if (sbuf_finish(&sb) == 0) 2053 devctl_notify("CAM", "periph", type, sbuf_data(&sb)); 2054 sbuf_delete(&sb); 2055 free(sbmsg, M_CAMPERIPH); 2056 } 2057 2058