scsi_pass.c revision 126076
1/* 2 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs. 3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer, 11 * without modification, immediately at the beginning of the file. 12 * 2. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28#include <sys/cdefs.h> 29__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_pass.c 126076 2004-02-21 19:42:58Z phk $"); 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/types.h> 35#include <sys/bio.h> 36#include <sys/malloc.h> 37#include <sys/fcntl.h> 38#include <sys/conf.h> 39#include <sys/errno.h> 40#include <sys/devicestat.h> 41#include <sys/proc.h> 42 43#include <cam/cam.h> 44#include <cam/cam_ccb.h> 45#include <cam/cam_periph.h> 46#include <cam/cam_queue.h> 47#include <cam/cam_xpt_periph.h> 48#include <cam/cam_debug.h> 49 50#include <cam/scsi/scsi_all.h> 51#include <cam/scsi/scsi_pass.h> 52 53typedef enum { 54 PASS_FLAG_OPEN = 0x01, 55 PASS_FLAG_LOCKED = 0x02, 56 PASS_FLAG_INVALID = 0x04 57} pass_flags; 58 59typedef enum { 60 PASS_STATE_NORMAL 61} pass_state; 62 63typedef enum { 64 PASS_CCB_BUFFER_IO, 65 PASS_CCB_WAITING 66} pass_ccb_types; 67 68#define ccb_type ppriv_field0 69#define ccb_bp ppriv_ptr1 70 71struct pass_softc { 72 pass_state state; 73 pass_flags flags; 74 u_int8_t pd_type; 75 union ccb saved_ccb; 76 struct devstat *device_stats; 77 dev_t dev; 78}; 79 80 81static d_open_t passopen; 82static d_close_t passclose; 83static d_ioctl_t passioctl; 84 85static periph_init_t passinit; 86static periph_ctor_t passregister; 87static periph_oninv_t passoninvalidate; 88static periph_dtor_t passcleanup; 89static periph_start_t passstart; 90static void passasync(void *callback_arg, u_int32_t code, 91 struct cam_path *path, void *arg); 92static void passdone(struct cam_periph *periph, 93 union ccb *done_ccb); 94static int passerror(union ccb *ccb, u_int32_t cam_flags, 95 u_int32_t sense_flags); 96static int passsendccb(struct cam_periph *periph, union ccb *ccb, 97 union ccb *inccb); 98 99static struct periph_driver passdriver = 100{ 101 passinit, "pass", 102 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0 103}; 104 105PERIPHDRIVER_DECLARE(pass, passdriver); 106 107static struct cdevsw pass_cdevsw = { 108 .d_open = passopen, 109 .d_close = passclose, 110 .d_ioctl = passioctl, 111 .d_name = "pass", 112}; 113 114static void 115passinit(void) 116{ 117 cam_status status; 118 struct cam_path *path; 119 120 /* 121 * Install a global async callback. This callback will 122 * receive async callbacks like "new device found". 123 */ 124 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 125 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 126 127 if (status == CAM_REQ_CMP) { 128 struct ccb_setasync csa; 129 130 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 131 csa.ccb_h.func_code = XPT_SASYNC_CB; 132 csa.event_enable = AC_FOUND_DEVICE; 133 csa.callback = passasync; 134 csa.callback_arg = NULL; 135 xpt_action((union ccb *)&csa); 136 status = csa.ccb_h.status; 137 xpt_free_path(path); 138 } 139 140 if (status != CAM_REQ_CMP) { 141 printf("pass: Failed to attach master async callback " 142 "due to status 0x%x!\n", status); 143 } 144 145} 146 147static void 148passoninvalidate(struct cam_periph *periph) 149{ 150 struct pass_softc *softc; 151 struct ccb_setasync csa; 152 153 softc = (struct pass_softc *)periph->softc; 154 155 /* 156 * De-register any async callbacks. 157 */ 158 xpt_setup_ccb(&csa.ccb_h, periph->path, 159 /* priority */ 5); 160 csa.ccb_h.func_code = XPT_SASYNC_CB; 161 csa.event_enable = 0; 162 csa.callback = passasync; 163 csa.callback_arg = periph; 164 xpt_action((union ccb *)&csa); 165 166 softc->flags |= PASS_FLAG_INVALID; 167 168 /* 169 * XXX Return all queued I/O with ENXIO. 170 * XXX Handle any transactions queued to the card 171 * with XPT_ABORT_CCB. 172 */ 173 174 if (bootverbose) { 175 xpt_print_path(periph->path); 176 printf("lost device\n"); 177 } 178 179} 180 181static void 182passcleanup(struct cam_periph *periph) 183{ 184 struct pass_softc *softc; 185 186 softc = (struct pass_softc *)periph->softc; 187 188 devstat_remove_entry(softc->device_stats); 189 190 destroy_dev(softc->dev); 191 192 if (bootverbose) { 193 xpt_print_path(periph->path); 194 printf("removing device entry\n"); 195 } 196 free(softc, M_DEVBUF); 197} 198 199static void 200passasync(void *callback_arg, u_int32_t code, 201 struct cam_path *path, void *arg) 202{ 203 struct cam_periph *periph; 204 205 periph = (struct cam_periph *)callback_arg; 206 207 switch (code) { 208 case AC_FOUND_DEVICE: 209 { 210 struct ccb_getdev *cgd; 211 cam_status status; 212 213 cgd = (struct ccb_getdev *)arg; 214 if (cgd == NULL) 215 break; 216 217 /* 218 * Allocate a peripheral instance for 219 * this device and start the probe 220 * process. 221 */ 222 status = cam_periph_alloc(passregister, passoninvalidate, 223 passcleanup, passstart, "pass", 224 CAM_PERIPH_BIO, cgd->ccb_h.path, 225 passasync, AC_FOUND_DEVICE, cgd); 226 227 if (status != CAM_REQ_CMP 228 && status != CAM_REQ_INPROG) { 229 const struct cam_status_entry *entry; 230 231 entry = cam_fetch_status_entry(status); 232 233 printf("passasync: Unable to attach new device " 234 "due to status %#x: %s\n", status, entry ? 235 entry->status_text : "Unknown"); 236 } 237 238 break; 239 } 240 default: 241 cam_periph_async(periph, code, path, arg); 242 break; 243 } 244} 245 246static cam_status 247passregister(struct cam_periph *periph, void *arg) 248{ 249 struct pass_softc *softc; 250 struct ccb_setasync csa; 251 struct ccb_getdev *cgd; 252 int no_tags; 253 254 cgd = (struct ccb_getdev *)arg; 255 if (periph == NULL) { 256 printf("passregister: periph was NULL!!\n"); 257 return(CAM_REQ_CMP_ERR); 258 } 259 260 if (cgd == NULL) { 261 printf("passregister: no getdev CCB, can't register device\n"); 262 return(CAM_REQ_CMP_ERR); 263 } 264 265 softc = (struct pass_softc *)malloc(sizeof(*softc), 266 M_DEVBUF, M_NOWAIT); 267 268 if (softc == NULL) { 269 printf("passregister: Unable to probe new device. " 270 "Unable to allocate softc\n"); 271 return(CAM_REQ_CMP_ERR); 272 } 273 274 bzero(softc, sizeof(*softc)); 275 softc->state = PASS_STATE_NORMAL; 276 softc->pd_type = SID_TYPE(&cgd->inq_data); 277 278 periph->softc = softc; 279 280 /* 281 * We pass in 0 for a blocksize, since we don't 282 * know what the blocksize of this device is, if 283 * it even has a blocksize. 284 */ 285 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 286 softc->device_stats = devstat_new_entry("pass", periph->unit_number, 0, 287 DEVSTAT_NO_BLOCKSIZE 288 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 289 softc->pd_type | 290 DEVSTAT_TYPE_IF_SCSI | 291 DEVSTAT_TYPE_PASS, 292 DEVSTAT_PRIORITY_PASS); 293 294 /* Register the device */ 295 softc->dev = make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT, 296 GID_OPERATOR, 0600, "%s%d", periph->periph_name, 297 periph->unit_number); 298 softc->dev->si_drv1 = periph; 299 300 /* 301 * Add an async callback so that we get 302 * notified if this device goes away. 303 */ 304 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5); 305 csa.ccb_h.func_code = XPT_SASYNC_CB; 306 csa.event_enable = AC_LOST_DEVICE; 307 csa.callback = passasync; 308 csa.callback_arg = periph; 309 xpt_action((union ccb *)&csa); 310 311 if (bootverbose) 312 xpt_announce_periph(periph, NULL); 313 314 return(CAM_REQ_CMP); 315} 316 317static int 318passopen(dev_t dev, int flags, int fmt, struct thread *td) 319{ 320 struct cam_periph *periph; 321 struct pass_softc *softc; 322 int error; 323 int s; 324 325 error = 0; /* default to no error */ 326 327 periph = (struct cam_periph *)dev->si_drv1; 328 if (periph == NULL) 329 return (ENXIO); 330 331 softc = (struct pass_softc *)periph->softc; 332 333 s = splsoftcam(); 334 if (softc->flags & PASS_FLAG_INVALID) { 335 splx(s); 336 return(ENXIO); 337 } 338 339 /* 340 * Don't allow access when we're running at a high securelevel. 341 */ 342 error = securelevel_gt(td->td_ucred, 1); 343 if (error) { 344 splx(s); 345 return(error); 346 } 347 348 /* 349 * Only allow read-write access. 350 */ 351 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) { 352 splx(s); 353 return(EPERM); 354 } 355 356 /* 357 * We don't allow nonblocking access. 358 */ 359 if ((flags & O_NONBLOCK) != 0) { 360 xpt_print_path(periph->path); 361 printf("can't do nonblocking accesss\n"); 362 splx(s); 363 return(EINVAL); 364 } 365 366 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) { 367 splx(s); 368 return (error); 369 } 370 371 splx(s); 372 373 if ((softc->flags & PASS_FLAG_OPEN) == 0) { 374 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 375 return(ENXIO); 376 softc->flags |= PASS_FLAG_OPEN; 377 } 378 379 cam_periph_unlock(periph); 380 381 return (error); 382} 383 384static int 385passclose(dev_t dev, int flag, int fmt, struct thread *td) 386{ 387 struct cam_periph *periph; 388 struct pass_softc *softc; 389 int error; 390 391 periph = (struct cam_periph *)dev->si_drv1; 392 if (periph == NULL) 393 return (ENXIO); 394 395 softc = (struct pass_softc *)periph->softc; 396 397 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) 398 return (error); 399 400 softc->flags &= ~PASS_FLAG_OPEN; 401 402 cam_periph_unlock(periph); 403 cam_periph_release(periph); 404 405 return (0); 406} 407 408static void 409passstart(struct cam_periph *periph, union ccb *start_ccb) 410{ 411 struct pass_softc *softc; 412 int s; 413 414 softc = (struct pass_softc *)periph->softc; 415 416 switch (softc->state) { 417 case PASS_STATE_NORMAL: 418 s = splbio(); 419 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING; 420 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 421 periph_links.sle); 422 periph->immediate_priority = CAM_PRIORITY_NONE; 423 splx(s); 424 wakeup(&periph->ccb_list); 425 break; 426 } 427} 428 429static void 430passdone(struct cam_periph *periph, union ccb *done_ccb) 431{ 432 struct pass_softc *softc; 433 struct ccb_scsiio *csio; 434 435 softc = (struct pass_softc *)periph->softc; 436 csio = &done_ccb->csio; 437 switch (csio->ccb_h.ccb_type) { 438 case PASS_CCB_WAITING: 439 /* Caller will release the CCB */ 440 wakeup(&done_ccb->ccb_h.cbfcnp); 441 return; 442 } 443 xpt_release_ccb(done_ccb); 444} 445 446static int 447passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 448{ 449 struct cam_periph *periph; 450 struct pass_softc *softc; 451 int error; 452 453 periph = (struct cam_periph *)dev->si_drv1; 454 if (periph == NULL) 455 return(ENXIO); 456 457 softc = (struct pass_softc *)periph->softc; 458 459 error = 0; 460 461 switch (cmd) { 462 463 case CAMIOCOMMAND: 464 { 465 union ccb *inccb; 466 union ccb *ccb; 467 int ccb_malloced; 468 469 inccb = (union ccb *)addr; 470 471 /* 472 * Some CCB types, like scan bus and scan lun can only go 473 * through the transport layer device. 474 */ 475 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 476 xpt_print_path(periph->path); 477 printf("CCB function code %#x is restricted to the " 478 "XPT device\n", inccb->ccb_h.func_code); 479 error = ENODEV; 480 break; 481 } 482 483 /* 484 * Non-immediate CCBs need a CCB from the per-device pool 485 * of CCBs, which is scheduled by the transport layer. 486 * Immediate CCBs and user-supplied CCBs should just be 487 * malloced. 488 */ 489 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 490 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 491 ccb = cam_periph_getccb(periph, 492 inccb->ccb_h.pinfo.priority); 493 ccb_malloced = 0; 494 } else { 495 ccb = xpt_alloc_ccb(); 496 497 if (ccb != NULL) 498 xpt_setup_ccb(&ccb->ccb_h, periph->path, 499 inccb->ccb_h.pinfo.priority); 500 ccb_malloced = 1; 501 } 502 503 if (ccb == NULL) { 504 xpt_print_path(periph->path); 505 printf("unable to allocate CCB\n"); 506 error = ENOMEM; 507 break; 508 } 509 510 error = passsendccb(periph, ccb, inccb); 511 512 if (ccb_malloced) 513 xpt_free_ccb(ccb); 514 else 515 xpt_release_ccb(ccb); 516 517 break; 518 } 519 default: 520 error = cam_periph_ioctl(periph, cmd, addr, passerror); 521 break; 522 } 523 524 return(error); 525} 526 527/* 528 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 529 * should be the CCB that is copied in from the user. 530 */ 531static int 532passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 533{ 534 struct pass_softc *softc; 535 struct cam_periph_map_info mapinfo; 536 int error, need_unmap; 537 538 softc = (struct pass_softc *)periph->softc; 539 540 need_unmap = 0; 541 542 /* 543 * There are some fields in the CCB header that need to be 544 * preserved, the rest we get from the user. 545 */ 546 xpt_merge_ccb(ccb, inccb); 547 548 /* 549 * There's no way for the user to have a completion 550 * function, so we put our own completion function in here. 551 */ 552 ccb->ccb_h.cbfcnp = passdone; 553 554 /* 555 * We only attempt to map the user memory into kernel space 556 * if they haven't passed in a physical memory pointer, 557 * and if there is actually an I/O operation to perform. 558 * Right now cam_periph_mapmem() only supports SCSI and device 559 * match CCBs. For the SCSI CCBs, we only pass the CCB in if 560 * there's actually data to map. cam_periph_mapmem() will do the 561 * right thing, even if there isn't data to map, but since CCBs 562 * without data are a reasonably common occurance (e.g. test unit 563 * ready), it will save a few cycles if we check for it here. 564 */ 565 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) 566 && (((ccb->ccb_h.func_code == XPT_SCSI_IO) 567 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)) 568 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) { 569 570 bzero(&mapinfo, sizeof(mapinfo)); 571 572 error = cam_periph_mapmem(ccb, &mapinfo); 573 574 /* 575 * cam_periph_mapmem returned an error, we can't continue. 576 * Return the error to the user. 577 */ 578 if (error) 579 return(error); 580 581 /* 582 * We successfully mapped the memory in, so we need to 583 * unmap it when the transaction is done. 584 */ 585 need_unmap = 1; 586 } 587 588 /* 589 * If the user wants us to perform any error recovery, then honor 590 * that request. Otherwise, it's up to the user to perform any 591 * error recovery. 592 */ 593 error = cam_periph_runccb(ccb, 594 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 595 passerror : NULL, 596 /* cam_flags */ CAM_RETRY_SELTO, 597 /* sense_flags */SF_RETRY_UA, 598 softc->device_stats); 599 600 if (need_unmap != 0) 601 cam_periph_unmapmem(ccb, &mapinfo); 602 603 ccb->ccb_h.cbfcnp = NULL; 604 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 605 bcopy(ccb, inccb, sizeof(union ccb)); 606 607 return(error); 608} 609 610static int 611passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 612{ 613 struct cam_periph *periph; 614 struct pass_softc *softc; 615 616 periph = xpt_path_periph(ccb->ccb_h.path); 617 softc = (struct pass_softc *)periph->softc; 618 619 return(cam_periph_error(ccb, cam_flags, sense_flags, 620 &softc->saved_ccb)); 621} 622