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