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_kva(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_data = bp->b_kvabase; 715 bp->b_bcount = lengths[i]; 716 vm_hold_load_pages(bp, (vm_offset_t)bp->b_data, 717 (vm_offset_t)bp->b_data + bp->b_bcount); 718 if (mapinfo->dirs[i] & CAM_DIR_OUT) { 719 error = copyin(*data_ptrs[i], bp->b_data, bp->b_bcount); 720 if (error) { 721 vm_hold_free_pages(bp, 722 (vm_offset_t)bp->b_data, 723 (vm_offset_t)bp->b_data + 724 bp->b_bcount); 725 } 726 } else { 727 error = 0; 728 } 729 if (error) { 730 relpbuf(bp, NULL); 731 cam_periph_unmapbufs(mapinfo, data_ptrs, i); 732 mapinfo->num_bufs_used -= i; 733 return(error); 734 } 735 736 /* set our pointer to the new mapped area */ 737 *data_ptrs[i] = bp->b_data; 738 739 mapinfo->bp[i] = bp; 740 mapinfo->num_bufs_used++; 741 } 742 743 return(0); 744 } 745 746 /* 747 * Unmap memory segments mapped into kernel virtual address space by 748 * cam_periph_mapmem(). 749 */ 750 void 751 cam_periph_unmapmem(union ccb *ccb, struct cam_periph_map_info *mapinfo) 752 { 753 int numbufs; 754 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 755 756 if (mapinfo->num_bufs_used <= 0) { 757 /* allow ourselves to be swapped once again */ 758 return; 759 } 760 761 switch (ccb->ccb_h.func_code) { 762 case XPT_DEV_MATCH: 763 numbufs = min(mapinfo->num_bufs_used, 2); 764 765 if (numbufs == 1) { 766 data_ptrs[0] = (void *)&ccb->cdm.matches; 767 } else { 768 data_ptrs[0] = (void *)&ccb->cdm.patterns; 769 data_ptrs[1] = (void *)&ccb->cdm.matches; 770 } 771 break; 772 case XPT_SCSI_IO: 773 case XPT_CONT_TARGET_IO: 774 data_ptrs[0] = &ccb->csio.data_ptr; 775 numbufs = min(mapinfo->num_bufs_used, 1); 776 break; 777 default: 778 /* allow ourselves to be swapped once again */ 779 return; 780 break; /* NOTREACHED */ 781 } 782 cam_periph_unmapbufs(mapinfo, data_ptrs, numbufs); 783 } 784 785 static void 786 cam_periph_unmapbufs(struct cam_periph_map_info *mapinfo, 787 u_int8_t ***data_ptrs, int numbufs) 788 { 789 struct buf *bp; 790 int i; 791 792 for (i = 0; i < numbufs; i++) { 793 bp = mapinfo->bp[i]; 794 795 /* Set the user's pointer back to the original value */ 796 *data_ptrs[i] = mapinfo->saved_ptrs[i]; 797 798 if (mapinfo->dirs[i] & CAM_DIR_IN) { 799 /* XXX return error */ 800 copyout(bp->b_data, *data_ptrs[i], bp->b_bcount); 801 } 802 vm_hold_free_pages(bp, (vm_offset_t)bp->b_data, 803 (vm_offset_t)bp->b_data + bp->b_bcount); 804 relpbuf(bp, NULL); 805 mapinfo->bp[i] = NULL; 806 } 807 } 808 809 union ccb * 810 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority) 811 { 812 struct ccb_hdr *ccb_h; 813 814 sim_lock_assert_owned(periph->sim->lock); 815 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering cdgetccb\n")); 816 817 while (SLIST_FIRST(&periph->ccb_list) == NULL) { 818 if (periph->immediate_priority > priority) 819 periph->immediate_priority = priority; 820 xpt_schedule(periph, priority); 821 if ((SLIST_FIRST(&periph->ccb_list) != NULL) 822 && (SLIST_FIRST(&periph->ccb_list)->pinfo.priority == priority)) 823 break; 824 sim_lock_sleep(&periph->ccb_list, 0, "cgticb", 0, 825 periph->sim->lock); 826 } 827 828 ccb_h = SLIST_FIRST(&periph->ccb_list); 829 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle); 830 return ((union ccb *)ccb_h); 831 } 832 833 void 834 cam_periph_ccbwait(union ccb *ccb) 835 { 836 struct cam_sim *sim; 837 838 sim = xpt_path_sim(ccb->ccb_h.path); 839 while ((ccb->ccb_h.pinfo.index != CAM_UNQUEUED_INDEX) 840 || ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG)) { 841 sim_lock_sleep(&ccb->ccb_h.cbfcnp, 0, "cbwait", 0, sim->lock); 842 } 843 } 844 845 int 846 cam_periph_ioctl(struct cam_periph *periph, u_long cmd, caddr_t addr, 847 int (*error_routine)(union ccb *ccb, 848 cam_flags camflags, 849 u_int32_t sense_flags)) 850 { 851 union ccb *ccb; 852 int error; 853 int found; 854 855 error = found = 0; 856 857 switch(cmd){ 858 case CAMGETPASSTHRU: 859 ccb = cam_periph_getccb(periph, /* priority */ 1); 860 xpt_setup_ccb(&ccb->ccb_h, 861 ccb->ccb_h.path, 862 /*priority*/1); 863 ccb->ccb_h.func_code = XPT_GDEVLIST; 864 865 /* 866 * Basically, the point of this is that we go through 867 * getting the list of devices, until we find a passthrough 868 * device. In the current version of the CAM code, the 869 * only way to determine what type of device we're dealing 870 * with is by its name. 871 */ 872 while (found == 0) { 873 ccb->cgdl.index = 0; 874 ccb->cgdl.status = CAM_GDEVLIST_MORE_DEVS; 875 while (ccb->cgdl.status == CAM_GDEVLIST_MORE_DEVS) { 876 877 /* we want the next device in the list */ 878 xpt_action(ccb); 879 if (strncmp(ccb->cgdl.periph_name, 880 "pass", 4) == 0){ 881 found = 1; 882 break; 883 } 884 } 885 if ((ccb->cgdl.status == CAM_GDEVLIST_LAST_DEVICE) && 886 (found == 0)) { 887 ccb->cgdl.periph_name[0] = '\0'; 888 ccb->cgdl.unit_number = 0; 889 break; 890 } 891 } 892 893 /* copy the result back out */ 894 bcopy(ccb, addr, sizeof(union ccb)); 895 896 /* and release the ccb */ 897 xpt_release_ccb(ccb); 898 899 break; 900 default: 901 error = ENOTTY; 902 break; 903 } 904 return(error); 905 } 906 907 int 908 cam_periph_runccb(union ccb *ccb, 909 int (*error_routine)(union ccb *ccb, 910 cam_flags camflags, 911 u_int32_t sense_flags), 912 cam_flags camflags, u_int32_t sense_flags, 913 struct devstat *ds) 914 { 915 struct cam_sim *sim; 916 int error; 917 918 error = 0; 919 sim = xpt_path_sim(ccb->ccb_h.path); 920 sim_lock_assert_owned(sim->lock); 921 922 /* 923 * If the user has supplied a stats structure, and if we understand 924 * this particular type of ccb, record the transaction start. 925 */ 926 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO)) 927 devstat_start_transaction(ds); 928 929 xpt_action(ccb); 930 931 do { 932 cam_periph_ccbwait(ccb); 933 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) 934 error = 0; 935 else if (error_routine != NULL) 936 error = (*error_routine)(ccb, camflags, sense_flags); 937 else 938 error = 0; 939 940 } while (error == ERESTART); 941 942 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) 943 cam_release_devq(ccb->ccb_h.path, 944 /* relsim_flags */0, 945 /* openings */0, 946 /* timeout */0, 947 /* getcount_only */ FALSE); 948 949 if ((ds != NULL) && (ccb->ccb_h.func_code == XPT_SCSI_IO)) 950 devstat_end_transaction(ds, 951 ccb->csio.dxfer_len, 952 ccb->csio.tag_action & 0xf, 953 ((ccb->ccb_h.flags & CAM_DIR_MASK) == 954 CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 955 (ccb->ccb_h.flags & CAM_DIR_OUT) ? 956 DEVSTAT_WRITE : 957 DEVSTAT_READ); 958 959 return(error); 960 } 961 962 void 963 cam_freeze_devq(struct cam_path *path) 964 { 965 struct ccb_hdr ccb_h; 966 967 xpt_setup_ccb(&ccb_h, path, /*priority*/1); 968 ccb_h.func_code = XPT_NOOP; 969 ccb_h.flags = CAM_DEV_QFREEZE; 970 xpt_action((union ccb *)&ccb_h); 971 } 972 973 u_int32_t 974 cam_release_devq(struct cam_path *path, u_int32_t relsim_flags, 975 u_int32_t openings, u_int32_t timeout, 976 int getcount_only) 977 { 978 struct ccb_relsim crs; 979 980 xpt_setup_ccb(&crs.ccb_h, path, 981 /*priority*/1); 982 crs.ccb_h.func_code = XPT_REL_SIMQ; 983 crs.ccb_h.flags = getcount_only ? CAM_DEV_QFREEZE : 0; 984 crs.release_flags = relsim_flags; 985 crs.openings = openings; 986 crs.release_timeout = timeout; 987 xpt_action((union ccb *)&crs); 988 return (crs.qfrozen_cnt); 989 } 990 991 #define saved_ccb_ptr ppriv_ptr0 992 static void 993 camperiphdone(struct cam_periph *periph, union ccb *done_ccb) 994 { 995 union ccb *saved_ccb; 996 cam_status status; 997 int frozen; 998 int sense; 999 struct scsi_start_stop_unit *scsi_cmd; 1000 u_int32_t relsim_flags, timeout; 1001 u_int32_t qfrozen_cnt; 1002 int xpt_done_ccb; 1003 1004 xpt_done_ccb = FALSE; 1005 status = done_ccb->ccb_h.status; 1006 frozen = (status & CAM_DEV_QFRZN) != 0; 1007 sense = (status & CAM_AUTOSNS_VALID) != 0; 1008 status &= CAM_STATUS_MASK; 1009 1010 timeout = 0; 1011 relsim_flags = 0; 1012 saved_ccb = (union ccb *)done_ccb->ccb_h.saved_ccb_ptr; 1013 1014 /* 1015 * Unfreeze the queue once if it is already frozen.. 1016 */ 1017 if (frozen != 0) { 1018 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path, 1019 /*relsim_flags*/0, 1020 /*openings*/0, 1021 /*timeout*/0, 1022 /*getcount_only*/0); 1023 } 1024 1025 switch (status) { 1026 case CAM_REQ_CMP: 1027 { 1028 /* 1029 * If we have successfully taken a device from the not 1030 * ready to ready state, re-scan the device and re-get 1031 * the inquiry information. Many devices (mostly disks) 1032 * don't properly report their inquiry information unless 1033 * they are spun up. 1034 * 1035 * If we manually retrieved sense into a CCB and got 1036 * something other than "NO SENSE" send the updated CCB 1037 * back to the client via xpt_done() to be processed via 1038 * the error recovery code again. 1039 */ 1040 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO) { 1041 scsi_cmd = (struct scsi_start_stop_unit *) 1042 &done_ccb->csio.cdb_io.cdb_bytes; 1043 1044 if (scsi_cmd->opcode == START_STOP_UNIT) 1045 xpt_async(AC_INQ_CHANGED, 1046 done_ccb->ccb_h.path, NULL); 1047 if (scsi_cmd->opcode == REQUEST_SENSE) { 1048 u_int sense_key; 1049 1050 sense_key = saved_ccb->csio.sense_data.flags; 1051 sense_key &= SSD_KEY; 1052 if (sense_key != SSD_KEY_NO_SENSE) { 1053 saved_ccb->ccb_h.status |= 1054 CAM_AUTOSNS_VALID; 1055 #if 0 1056 xpt_print(saved_ccb->ccb_h.path, 1057 "Recovered Sense\n"); 1058 scsi_sense_print(&saved_ccb->csio); 1059 cam_error_print(saved_ccb, CAM_ESF_ALL, 1060 CAM_EPF_ALL); 1061 #endif 1062 xpt_done_ccb = TRUE; 1063 } 1064 } 1065 } 1066 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb, 1067 sizeof(union ccb)); 1068 1069 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG; 1070 1071 if (xpt_done_ccb == FALSE) 1072 xpt_action(done_ccb); 1073 1074 break; 1075 } 1076 case CAM_SCSI_STATUS_ERROR: 1077 scsi_cmd = (struct scsi_start_stop_unit *) 1078 &done_ccb->csio.cdb_io.cdb_bytes; 1079 if (sense != 0) { 1080 struct ccb_getdev cgd; 1081 struct scsi_sense_data *sense; 1082 int error_code, sense_key, asc, ascq; 1083 scsi_sense_action err_action; 1084 1085 sense = &done_ccb->csio.sense_data; 1086 scsi_extract_sense(sense, &error_code, 1087 &sense_key, &asc, &ascq); 1088 1089 /* 1090 * Grab the inquiry data for this device. 1091 */ 1092 xpt_setup_ccb(&cgd.ccb_h, done_ccb->ccb_h.path, 1093 /*priority*/ 1); 1094 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1095 xpt_action((union ccb *)&cgd); 1096 err_action = scsi_error_action(&done_ccb->csio, 1097 &cgd.inq_data, 0); 1098 1099 /* 1100 * If the error is "invalid field in CDB", 1101 * and the load/eject flag is set, turn the 1102 * flag off and try again. This is just in 1103 * case the drive in question barfs on the 1104 * load eject flag. The CAM code should set 1105 * the load/eject flag by default for 1106 * removable media. 1107 */ 1108 1109 /* XXX KDM 1110 * Should we check to see what the specific 1111 * scsi status is?? Or does it not matter 1112 * since we already know that there was an 1113 * error, and we know what the specific 1114 * error code was, and we know what the 1115 * opcode is.. 1116 */ 1117 if ((scsi_cmd->opcode == START_STOP_UNIT) && 1118 ((scsi_cmd->how & SSS_LOEJ) != 0) && 1119 (asc == 0x24) && (ascq == 0x00) && 1120 (done_ccb->ccb_h.retry_count > 0)) { 1121 1122 scsi_cmd->how &= ~SSS_LOEJ; 1123 1124 xpt_action(done_ccb); 1125 1126 } else if ((done_ccb->ccb_h.retry_count > 1) 1127 && ((err_action & SS_MASK) != SS_FAIL)) { 1128 1129 /* 1130 * In this case, the error recovery 1131 * command failed, but we've got 1132 * some retries left on it. Give 1133 * it another try unless this is an 1134 * unretryable error. 1135 */ 1136 1137 /* set the timeout to .5 sec */ 1138 relsim_flags = 1139 RELSIM_RELEASE_AFTER_TIMEOUT; 1140 timeout = 500; 1141 1142 xpt_action(done_ccb); 1143 1144 break; 1145 1146 } else { 1147 /* 1148 * Perform the final retry with the original 1149 * CCB so that final error processing is 1150 * performed by the owner of the CCB. 1151 */ 1152 bcopy(done_ccb->ccb_h.saved_ccb_ptr, 1153 done_ccb, sizeof(union ccb)); 1154 1155 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG; 1156 1157 xpt_action(done_ccb); 1158 } 1159 } else { 1160 /* 1161 * Eh?? The command failed, but we don't 1162 * have any sense. What's up with that? 1163 * Fire the CCB again to return it to the 1164 * caller. 1165 */ 1166 bcopy(done_ccb->ccb_h.saved_ccb_ptr, 1167 done_ccb, sizeof(union ccb)); 1168 1169 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG; 1170 1171 xpt_action(done_ccb); 1172 1173 } 1174 break; 1175 default: 1176 bcopy(done_ccb->ccb_h.saved_ccb_ptr, done_ccb, 1177 sizeof(union ccb)); 1178 1179 periph->flags &= ~CAM_PERIPH_RECOVERY_INPROG; 1180 1181 xpt_action(done_ccb); 1182 1183 break; 1184 } 1185 1186 /* decrement the retry count */ 1187 /* 1188 * XXX This isn't appropriate in all cases. Restructure, 1189 * so that the retry count is only decremented on an 1190 * actual retry. Remeber that the orignal ccb had its 1191 * retry count dropped before entering recovery, so 1192 * doing it again is a bug. 1193 */ 1194 if (done_ccb->ccb_h.retry_count > 0) 1195 done_ccb->ccb_h.retry_count--; 1196 1197 qfrozen_cnt = cam_release_devq(done_ccb->ccb_h.path, 1198 /*relsim_flags*/relsim_flags, 1199 /*openings*/0, 1200 /*timeout*/timeout, 1201 /*getcount_only*/0); 1202 if (xpt_done_ccb == TRUE) 1203 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb); 1204 } 1205 1206 /* 1207 * Generic Async Event handler. Peripheral drivers usually 1208 * filter out the events that require personal attention, 1209 * and leave the rest to this function. 1210 */ 1211 void 1212 cam_periph_async(struct cam_periph *periph, u_int32_t code, 1213 struct cam_path *path, void *arg) 1214 { 1215 switch (code) { 1216 case AC_LOST_DEVICE: 1217 cam_periph_invalidate(periph); 1218 break; 1219 case AC_SENT_BDR: 1220 case AC_BUS_RESET: 1221 { 1222 cam_periph_bus_settle(periph, scsi_delay); 1223 break; 1224 } 1225 default: 1226 break; 1227 } 1228 } 1229 1230 void 1231 cam_periph_bus_settle(struct cam_periph *periph, u_int bus_settle) 1232 { 1233 struct ccb_getdevstats cgds; 1234 1235 xpt_setup_ccb(&cgds.ccb_h, periph->path, /*priority*/1); 1236 cgds.ccb_h.func_code = XPT_GDEV_STATS; 1237 xpt_action((union ccb *)&cgds); 1238 cam_periph_freeze_after_event(periph, &cgds.last_reset, bus_settle); 1239 } 1240 1241 void 1242 cam_periph_freeze_after_event(struct cam_periph *periph, 1243 struct timeval* event_time, u_int duration_ms) 1244 { 1245 struct timeval delta; 1246 struct timeval duration_tv; 1247 1248 microuptime(&delta); 1249 timevalsub(&delta, event_time); 1250 duration_tv.tv_sec = duration_ms / 1000; 1251 duration_tv.tv_usec = (duration_ms % 1000) * 1000; 1252 if (timevalcmp(&delta, &duration_tv, <)) { 1253 timevalsub(&duration_tv, &delta); 1254 1255 duration_ms = duration_tv.tv_sec * 1000; 1256 duration_ms += duration_tv.tv_usec / 1000; 1257 cam_freeze_devq(periph->path); 1258 cam_release_devq(periph->path, 1259 RELSIM_RELEASE_AFTER_TIMEOUT, 1260 /*reduction*/0, 1261 /*timeout*/duration_ms, 1262 /*getcount_only*/0); 1263 } 1264 1265 } 1266 1267 static int 1268 camperiphscsistatuserror(union ccb *ccb, cam_flags camflags, 1269 u_int32_t sense_flags, union ccb *save_ccb, 1270 int *openings, u_int32_t *relsim_flags, 1271 u_int32_t *timeout) 1272 { 1273 int error; 1274 1275 switch (ccb->csio.scsi_status) { 1276 case SCSI_STATUS_OK: 1277 case SCSI_STATUS_COND_MET: 1278 case SCSI_STATUS_INTERMED: 1279 case SCSI_STATUS_INTERMED_COND_MET: 1280 error = 0; 1281 break; 1282 case SCSI_STATUS_CMD_TERMINATED: 1283 case SCSI_STATUS_CHECK_COND: 1284 error = camperiphscsisenseerror(ccb, 1285 camflags, 1286 sense_flags, 1287 save_ccb, 1288 openings, 1289 relsim_flags, 1290 timeout); 1291 break; 1292 case SCSI_STATUS_QUEUE_FULL: 1293 { 1294 /* no decrement */ 1295 struct ccb_getdevstats cgds; 1296 1297 /* 1298 * First off, find out what the current 1299 * transaction counts are. 1300 */ 1301 xpt_setup_ccb(&cgds.ccb_h, 1302 ccb->ccb_h.path, 1303 /*priority*/1); 1304 cgds.ccb_h.func_code = XPT_GDEV_STATS; 1305 xpt_action((union ccb *)&cgds); 1306 1307 /* 1308 * If we were the only transaction active, treat 1309 * the QUEUE FULL as if it were a BUSY condition. 1310 */ 1311 if (cgds.dev_active != 0) { 1312 int total_openings; 1313 1314 /* 1315 * Reduce the number of openings to 1316 * be 1 less than the amount it took 1317 * to get a queue full bounded by the 1318 * minimum allowed tag count for this 1319 * device. 1320 */ 1321 total_openings = cgds.dev_active + cgds.dev_openings; 1322 *openings = cgds.dev_active; 1323 if (*openings < cgds.mintags) 1324 *openings = cgds.mintags; 1325 if (*openings < total_openings) 1326 *relsim_flags = RELSIM_ADJUST_OPENINGS; 1327 else { 1328 /* 1329 * Some devices report queue full for 1330 * temporary resource shortages. For 1331 * this reason, we allow a minimum 1332 * tag count to be entered via a 1333 * quirk entry to prevent the queue 1334 * count on these devices from falling 1335 * to a pessimisticly low value. We 1336 * still wait for the next successful 1337 * completion, however, before queueing 1338 * more transactions to the device. 1339 */ 1340 *relsim_flags = RELSIM_RELEASE_AFTER_CMDCMPLT; 1341 } 1342 *timeout = 0; 1343 error = ERESTART; 1344 if (bootverbose) { 1345 xpt_print(ccb->ccb_h.path, "Queue Full\n"); 1346 } 1347 break; 1348 } 1349 /* FALLTHROUGH */ 1350 } 1351 case SCSI_STATUS_BUSY: 1352 /* 1353 * Restart the queue after either another 1354 * command completes or a 1 second timeout. 1355 */ 1356 if (bootverbose) { 1357 xpt_print(ccb->ccb_h.path, "Device Busy\n"); 1358 } 1359 if (ccb->ccb_h.retry_count > 0) { 1360 ccb->ccb_h.retry_count--; 1361 error = ERESTART; 1362 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT 1363 | RELSIM_RELEASE_AFTER_CMDCMPLT; 1364 *timeout = 1000; 1365 } else { 1366 error = EIO; 1367 } 1368 break; 1369 case SCSI_STATUS_RESERV_CONFLICT: 1370 xpt_print(ccb->ccb_h.path, "Reservation Conflict\n"); 1371 error = EIO; 1372 break; 1373 default: 1374 xpt_print(ccb->ccb_h.path, "SCSI Status 0x%x\n", 1375 ccb->csio.scsi_status); 1376 error = EIO; 1377 break; 1378 } 1379 return (error); 1380 } 1381 1382 static int 1383 camperiphscsisenseerror(union ccb *ccb, cam_flags camflags, 1384 u_int32_t sense_flags, union ccb *save_ccb, 1385 int *openings, u_int32_t *relsim_flags, 1386 u_int32_t *timeout) 1387 { 1388 struct cam_periph *periph; 1389 int error; 1390 1391 periph = xpt_path_periph(ccb->ccb_h.path); 1392 if (periph->flags & CAM_PERIPH_RECOVERY_INPROG) { 1393 1394 /* 1395 * If error recovery is already in progress, don't attempt 1396 * to process this error, but requeue it unconditionally 1397 * and attempt to process it once error recovery has 1398 * completed. This failed command is probably related to 1399 * the error that caused the currently active error recovery 1400 * action so our current recovery efforts should also 1401 * address this command. Be aware that the error recovery 1402 * code assumes that only one recovery action is in progress 1403 * on a particular peripheral instance at any given time 1404 * (e.g. only one saved CCB for error recovery) so it is 1405 * imperitive that we don't violate this assumption. 1406 */ 1407 error = ERESTART; 1408 } else { 1409 scsi_sense_action err_action; 1410 struct ccb_getdev cgd; 1411 const char *action_string; 1412 union ccb* print_ccb; 1413 1414 /* A description of the error recovery action performed */ 1415 action_string = NULL; 1416 1417 /* 1418 * The location of the orignal ccb 1419 * for sense printing purposes. 1420 */ 1421 print_ccb = ccb; 1422 1423 /* 1424 * Grab the inquiry data for this device. 1425 */ 1426 xpt_setup_ccb(&cgd.ccb_h, ccb->ccb_h.path, /*priority*/ 1); 1427 cgd.ccb_h.func_code = XPT_GDEV_TYPE; 1428 xpt_action((union ccb *)&cgd); 1429 1430 if ((ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) 1431 err_action = scsi_error_action(&ccb->csio, 1432 &cgd.inq_data, 1433 sense_flags); 1434 else if ((ccb->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) 1435 err_action = SS_REQSENSE; 1436 else 1437 err_action = SS_RETRY|SSQ_DECREMENT_COUNT|EIO; 1438 1439 error = err_action & SS_ERRMASK; 1440 1441 /* 1442 * If the recovery action will consume a retry, 1443 * make sure we actually have retries available. 1444 */ 1445 if ((err_action & SSQ_DECREMENT_COUNT) != 0) { 1446 if (ccb->ccb_h.retry_count > 0) 1447 ccb->ccb_h.retry_count--; 1448 else { 1449 action_string = "Retries Exhausted"; 1450 goto sense_error_done; 1451 } 1452 } 1453 1454 if ((err_action & SS_MASK) >= SS_START) { 1455 /* 1456 * Do common portions of commands that 1457 * use recovery CCBs. 1458 */ 1459 if (save_ccb == NULL) { 1460 action_string = "No recovery CCB supplied"; 1461 goto sense_error_done; 1462 } 1463 bcopy(ccb, save_ccb, sizeof(*save_ccb)); 1464 print_ccb = save_ccb; 1465 periph->flags |= CAM_PERIPH_RECOVERY_INPROG; 1466 } 1467 1468 switch (err_action & SS_MASK) { 1469 case SS_NOP: 1470 action_string = "No Recovery Action Needed"; 1471 error = 0; 1472 break; 1473 case SS_RETRY: 1474 action_string = "Retrying Command (per Sense Data)"; 1475 error = ERESTART; 1476 break; 1477 case SS_FAIL: 1478 action_string = "Unretryable error"; 1479 break; 1480 case SS_START: 1481 { 1482 int le; 1483 1484 /* 1485 * Send a start unit command to the device, and 1486 * then retry the command. 1487 */ 1488 action_string = "Attempting to Start Unit"; 1489 1490 /* 1491 * Check for removable media and set 1492 * load/eject flag appropriately. 1493 */ 1494 if (SID_IS_REMOVABLE(&cgd.inq_data)) 1495 le = TRUE; 1496 else 1497 le = FALSE; 1498 1499 scsi_start_stop(&ccb->csio, 1500 /*retries*/1, 1501 camperiphdone, 1502 MSG_SIMPLE_Q_TAG, 1503 /*start*/TRUE, 1504 /*load/eject*/le, 1505 /*immediate*/FALSE, 1506 SSD_FULL_SIZE, 1507 /*timeout*/50000); 1508 break; 1509 } 1510 case SS_TUR: 1511 { 1512 /* 1513 * Send a Test Unit Ready to the device. 1514 * If the 'many' flag is set, we send 120 1515 * test unit ready commands, one every half 1516 * second. Otherwise, we just send one TUR. 1517 * We only want to do this if the retry 1518 * count has not been exhausted. 1519 */ 1520 int retries; 1521 1522 if ((err_action & SSQ_MANY) != 0) { 1523 action_string = "Polling device for readiness"; 1524 retries = 120; 1525 } else { 1526 action_string = "Testing device for readiness"; 1527 retries = 1; 1528 } 1529 scsi_test_unit_ready(&ccb->csio, 1530 retries, 1531 camperiphdone, 1532 MSG_SIMPLE_Q_TAG, 1533 SSD_FULL_SIZE, 1534 /*timeout*/5000); 1535 1536 /* 1537 * Accomplish our 500ms delay by deferring 1538 * the release of our device queue appropriately. 1539 */ 1540 *relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 1541 *timeout = 500; 1542 break; 1543 } 1544 case SS_REQSENSE: 1545 { 1546 /* 1547 * Send a Request Sense to the device. We 1548 * assume that we are in a contingent allegiance 1549 * condition so we do not tag this request. 1550 */ 1551 scsi_request_sense(&ccb->csio, /*retries*/1, 1552 camperiphdone, 1553 &save_ccb->csio.sense_data, 1554 sizeof(save_ccb->csio.sense_data), 1555 CAM_TAG_ACTION_NONE, 1556 /*sense_len*/SSD_FULL_SIZE, 1557 /*timeout*/5000); 1558 break; 1559 } 1560 default: 1561 panic("Unhandled error action %x", err_action); 1562 } 1563 1564 if ((err_action & SS_MASK) >= SS_START) { 1565 /* 1566 * Drop the priority to 0 so that the recovery 1567 * CCB is the first to execute. Freeze the queue 1568 * after this command is sent so that we can 1569 * restore the old csio and have it queued in 1570 * the proper order before we release normal 1571 * transactions to the device. 1572 */ 1573 ccb->ccb_h.pinfo.priority = 0; 1574 ccb->ccb_h.flags |= CAM_DEV_QFREEZE; 1575 ccb->ccb_h.saved_ccb_ptr = save_ccb; 1576 error = ERESTART; 1577 } 1578 1579 sense_error_done: 1580 if ((err_action & SSQ_PRINT_SENSE) != 0 1581 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID) != 0) { 1582 cam_error_print(print_ccb, CAM_ESF_ALL, CAM_EPF_ALL); 1583 xpt_print_path(ccb->ccb_h.path); 1584 if (bootverbose) 1585 scsi_sense_print(&print_ccb->csio); 1586 kprintf("%s\n", action_string); 1587 } 1588 } 1589 return (error); 1590 } 1591 1592 /* 1593 * Generic error handler. Peripheral drivers usually filter 1594 * out the errors that they handle in a unique mannor, then 1595 * call this function. 1596 */ 1597 int 1598 cam_periph_error(union ccb *ccb, cam_flags camflags, 1599 u_int32_t sense_flags, union ccb *save_ccb) 1600 { 1601 const char *action_string; 1602 cam_status status; 1603 int frozen; 1604 int error, printed = 0; 1605 int openings; 1606 u_int32_t relsim_flags; 1607 u_int32_t timeout = 0; 1608 1609 action_string = NULL; 1610 status = ccb->ccb_h.status; 1611 frozen = (status & CAM_DEV_QFRZN) != 0; 1612 status &= CAM_STATUS_MASK; 1613 openings = relsim_flags = 0; 1614 1615 switch (status) { 1616 case CAM_REQ_CMP: 1617 error = 0; 1618 break; 1619 case CAM_SCSI_STATUS_ERROR: 1620 error = camperiphscsistatuserror(ccb, 1621 camflags, 1622 sense_flags, 1623 save_ccb, 1624 &openings, 1625 &relsim_flags, 1626 &timeout); 1627 break; 1628 case CAM_AUTOSENSE_FAIL: 1629 xpt_print(ccb->ccb_h.path, "AutoSense Failed\n"); 1630 error = EIO; /* we have to kill the command */ 1631 break; 1632 case CAM_REQ_CMP_ERR: 1633 if (bootverbose && printed == 0) { 1634 xpt_print(ccb->ccb_h.path, 1635 "Request completed with CAM_REQ_CMP_ERR\n"); 1636 printed++; 1637 } 1638 /* FALLTHROUGH */ 1639 case CAM_CMD_TIMEOUT: 1640 if (bootverbose && printed == 0) { 1641 xpt_print(ccb->ccb_h.path, "Command timed out\n"); 1642 printed++; 1643 } 1644 /* FALLTHROUGH */ 1645 case CAM_UNEXP_BUSFREE: 1646 if (bootverbose && printed == 0) { 1647 xpt_print(ccb->ccb_h.path, "Unexpected Bus Free\n"); 1648 printed++; 1649 } 1650 /* FALLTHROUGH */ 1651 case CAM_UNCOR_PARITY: 1652 if (bootverbose && printed == 0) { 1653 xpt_print(ccb->ccb_h.path, 1654 "Uncorrected Parity Error\n"); 1655 printed++; 1656 } 1657 /* FALLTHROUGH */ 1658 case CAM_DATA_RUN_ERR: 1659 if (bootverbose && printed == 0) { 1660 xpt_print(ccb->ccb_h.path, "Data Overrun\n"); 1661 printed++; 1662 } 1663 error = EIO; /* we have to kill the command */ 1664 /* decrement the number of retries */ 1665 if (ccb->ccb_h.retry_count > 0) { 1666 ccb->ccb_h.retry_count--; 1667 error = ERESTART; 1668 } else { 1669 action_string = "Retries Exhausted"; 1670 error = EIO; 1671 } 1672 break; 1673 case CAM_UA_ABORT: 1674 case CAM_UA_TERMIO: 1675 case CAM_MSG_REJECT_REC: 1676 /* XXX Don't know that these are correct */ 1677 error = EIO; 1678 break; 1679 case CAM_SEL_TIMEOUT: 1680 { 1681 struct cam_path *newpath; 1682 1683 if ((camflags & CAM_RETRY_SELTO) != 0) { 1684 if (ccb->ccb_h.retry_count > 0) { 1685 1686 ccb->ccb_h.retry_count--; 1687 error = ERESTART; 1688 if (bootverbose && printed == 0) { 1689 xpt_print(ccb->ccb_h.path, 1690 "Selection Timeout\n"); 1691 printed++; 1692 } 1693 1694 /* 1695 * Wait a bit to give the device 1696 * time to recover before we try again. 1697 */ 1698 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 1699 timeout = periph_selto_delay; 1700 break; 1701 } 1702 } 1703 error = ENXIO; 1704 /* Should we do more if we can't create the path?? */ 1705 if (xpt_create_path(&newpath, xpt_path_periph(ccb->ccb_h.path), 1706 xpt_path_path_id(ccb->ccb_h.path), 1707 xpt_path_target_id(ccb->ccb_h.path), 1708 CAM_LUN_WILDCARD) != CAM_REQ_CMP) 1709 break; 1710 1711 /* 1712 * Let peripheral drivers know that this device has gone 1713 * away. 1714 */ 1715 xpt_async(AC_LOST_DEVICE, newpath, NULL); 1716 xpt_free_path(newpath); 1717 break; 1718 } 1719 case CAM_REQ_INVALID: 1720 case CAM_PATH_INVALID: 1721 case CAM_DEV_NOT_THERE: 1722 case CAM_NO_HBA: 1723 case CAM_PROVIDE_FAIL: 1724 case CAM_REQ_TOO_BIG: 1725 case CAM_LUN_INVALID: 1726 case CAM_TID_INVALID: 1727 error = EINVAL; 1728 break; 1729 case CAM_SCSI_BUS_RESET: 1730 case CAM_BDR_SENT: 1731 /* 1732 * Commands that repeatedly timeout and cause these 1733 * kinds of error recovery actions, should return 1734 * CAM_CMD_TIMEOUT, which allows us to safely assume 1735 * that this command was an innocent bystander to 1736 * these events and should be unconditionally 1737 * retried. 1738 */ 1739 if (bootverbose && printed == 0) { 1740 xpt_print_path(ccb->ccb_h.path); 1741 if (status == CAM_BDR_SENT) 1742 kprintf("Bus Device Reset sent\n"); 1743 else 1744 kprintf("Bus Reset issued\n"); 1745 printed++; 1746 } 1747 /* FALLTHROUGH */ 1748 case CAM_REQUEUE_REQ: 1749 /* Unconditional requeue */ 1750 error = ERESTART; 1751 if (bootverbose && printed == 0) { 1752 xpt_print(ccb->ccb_h.path, "Request Requeued\n"); 1753 printed++; 1754 } 1755 break; 1756 case CAM_RESRC_UNAVAIL: 1757 /* Wait a bit for the resource shortage to abate. */ 1758 timeout = periph_noresrc_delay; 1759 /* FALLTHROUGH */ 1760 case CAM_BUSY: 1761 if (timeout == 0) { 1762 /* Wait a bit for the busy condition to abate. */ 1763 timeout = periph_busy_delay; 1764 } 1765 relsim_flags = RELSIM_RELEASE_AFTER_TIMEOUT; 1766 /* FALLTHROUGH */ 1767 default: 1768 /* decrement the number of retries */ 1769 if (ccb->ccb_h.retry_count > 0) { 1770 ccb->ccb_h.retry_count--; 1771 error = ERESTART; 1772 if (bootverbose && printed == 0) { 1773 xpt_print(ccb->ccb_h.path, "CAM Status 0x%x\n", 1774 status); 1775 printed++; 1776 } 1777 } else { 1778 error = EIO; 1779 action_string = "Retries Exhausted"; 1780 } 1781 break; 1782 } 1783 1784 /* Attempt a retry */ 1785 if (error == ERESTART || error == 0) { 1786 if (frozen != 0) 1787 ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 1788 1789 if (error == ERESTART) { 1790 action_string = "Retrying Command"; 1791 xpt_action(ccb); 1792 } 1793 1794 if (frozen != 0) 1795 cam_release_devq(ccb->ccb_h.path, 1796 relsim_flags, 1797 openings, 1798 timeout, 1799 /*getcount_only*/0); 1800 } 1801 1802 /* 1803 * If we have an error and are booting verbosely, whine 1804 * *unless* this was a non-retryable selection timeout. 1805 */ 1806 if (error != 0 && bootverbose && (sense_flags & SF_NO_PRINT) == 0 && 1807 !(status == CAM_SEL_TIMEOUT && (camflags & CAM_RETRY_SELTO) == 0)) { 1808 1809 1810 if (action_string == NULL) 1811 action_string = "Unretryable Error"; 1812 if (error != ERESTART) { 1813 xpt_print(ccb->ccb_h.path, "error %d\n", error); 1814 } 1815 xpt_print(ccb->ccb_h.path, "%s\n", action_string); 1816 } 1817 1818 return (error); 1819 } 1820