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