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