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