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