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