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