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