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 241 /* 242 * Allocate a peripheral instance for 243 * this device and start the probe 244 * process. 245 */ 246 status = cam_periph_alloc(passregister, passoninvalidate, 247 passcleanup, passstart, "pass", 248 CAM_PERIPH_BIO, cgd->ccb_h.path, 249 passasync, AC_FOUND_DEVICE, cgd); 250 251 if (status != CAM_REQ_CMP 252 && status != CAM_REQ_INPROG) { 253 const struct cam_status_entry *entry; 254 255 entry = cam_fetch_status_entry(status); 256 257 printf("passasync: Unable to attach new device " 258 "due to status %#x: %s\n", status, entry ? 259 entry->status_text : "Unknown"); 260 } 261 262 break; 263 } 264 default: 265 cam_periph_async(periph, code, path, arg); 266 break; 267 } 268 } 269 270 static cam_status 271 passregister(struct cam_periph *periph, void *arg) 272 { 273 struct pass_softc *softc; 274 struct ccb_setasync csa; 275 struct ccb_getdev *cgd; 276 int no_tags; 277 278 cgd = (struct ccb_getdev *)arg; 279 if (periph == NULL) { 280 printf("passregister: periph was NULL!!\n"); 281 return(CAM_REQ_CMP_ERR); 282 } 283 284 if (cgd == NULL) { 285 printf("passregister: no getdev CCB, can't register device\n"); 286 return(CAM_REQ_CMP_ERR); 287 } 288 289 softc = (struct pass_softc *)malloc(sizeof(*softc), 290 M_DEVBUF, M_NOWAIT); 291 292 if (softc == NULL) { 293 printf("passregister: Unable to probe new device. " 294 "Unable to allocate softc\n"); 295 return(CAM_REQ_CMP_ERR); 296 } 297 298 bzero(softc, sizeof(*softc)); 299 softc->state = PASS_STATE_NORMAL; 300 softc->pd_type = SID_TYPE(&cgd->inq_data); 301 302 periph->softc = softc; 303 cam_extend_set(passperiphs, periph->unit_number, periph); 304 305 /* 306 * We pass in 0 for a blocksize, since we don't 307 * know what the blocksize of this device is, if 308 * it even has a blocksize. 309 */ 310 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 311 devstat_add_entry(&softc->device_stats, "pass", periph->unit_number, 0, 312 DEVSTAT_NO_BLOCKSIZE 313 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 314 softc->pd_type | 315 DEVSTAT_TYPE_IF_SCSI | 316 DEVSTAT_TYPE_PASS, 317 DEVSTAT_PRIORITY_PASS); 318 319 /* Register the device */ 320 softc->dev = make_dev(&pass_cdevsw, periph->unit_number, UID_ROOT, 321 GID_OPERATOR, 0600, "%s%d", periph->periph_name, 322 periph->unit_number); 323 324 /* 325 * Add an async callback so that we get 326 * notified if this device goes away. 327 */ 328 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5); 329 csa.ccb_h.func_code = XPT_SASYNC_CB; 330 csa.event_enable = AC_LOST_DEVICE; 331 csa.callback = passasync; 332 csa.callback_arg = periph; 333 xpt_action((union ccb *)&csa); 334 335 if (bootverbose) 336 xpt_announce_periph(periph, NULL); 337 338 return(CAM_REQ_CMP); 339 } 340 341 static int 342 passopen(dev_t dev, int flags, int fmt, struct proc *p) 343 { 344 struct cam_periph *periph; 345 struct pass_softc *softc; 346 int unit, error; 347 int s; 348 349 error = 0; /* default to no error */ 350 351 /* unit = dkunit(dev); */ 352 /* XXX KDM fix this */ 353 unit = minor(dev) & 0xff; 354 355 periph = cam_extend_get(passperiphs, unit); 356 357 if (periph == NULL) 358 return (ENXIO); 359 360 softc = (struct pass_softc *)periph->softc; 361 362 s = splsoftcam(); 363 if (softc->flags & PASS_FLAG_INVALID) { 364 splx(s); 365 return(ENXIO); 366 } 367 368 /* 369 * Don't allow access when we're running at a high securelvel. 370 */ 371 if (securelevel > 1) { 372 splx(s); 373 return(EPERM); 374 } 375 376 /* 377 * Only allow read-write access. 378 */ 379 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) { 380 splx(s); 381 return(EPERM); 382 } 383 384 /* 385 * We don't allow nonblocking access. 386 */ 387 if ((flags & O_NONBLOCK) != 0) { 388 xpt_print_path(periph->path); 389 printf("can't do nonblocking accesss\n"); 390 splx(s); 391 return(EINVAL); 392 } 393 394 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) { 395 splx(s); 396 return (error); 397 } 398 399 splx(s); 400 401 if ((softc->flags & PASS_FLAG_OPEN) == 0) { 402 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 403 return(ENXIO); 404 softc->flags |= PASS_FLAG_OPEN; 405 } 406 407 cam_periph_unlock(periph); 408 409 return (error); 410 } 411 412 static int 413 passclose(dev_t dev, int flag, int fmt, struct proc *p) 414 { 415 struct cam_periph *periph; 416 struct pass_softc *softc; 417 int unit, error; 418 419 /* unit = dkunit(dev); */ 420 /* XXX KDM fix this */ 421 unit = minor(dev) & 0xff; 422 423 periph = cam_extend_get(passperiphs, unit); 424 if (periph == NULL) 425 return (ENXIO); 426 427 softc = (struct pass_softc *)periph->softc; 428 429 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) 430 return (error); 431 432 softc->flags &= ~PASS_FLAG_OPEN; 433 434 cam_periph_unlock(periph); 435 cam_periph_release(periph); 436 437 return (0); 438 } 439 440 static void 441 passstart(struct cam_periph *periph, union ccb *start_ccb) 442 { 443 struct pass_softc *softc; 444 int s; 445 446 softc = (struct pass_softc *)periph->softc; 447 448 switch (softc->state) { 449 case PASS_STATE_NORMAL: 450 s = splbio(); 451 start_ccb->ccb_h.ccb_type = PASS_CCB_WAITING; 452 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 453 periph_links.sle); 454 periph->immediate_priority = CAM_PRIORITY_NONE; 455 splx(s); 456 wakeup(&periph->ccb_list); 457 break; 458 } 459 } 460 461 static void 462 passdone(struct cam_periph *periph, union ccb *done_ccb) 463 { 464 struct pass_softc *softc; 465 struct ccb_scsiio *csio; 466 467 softc = (struct pass_softc *)periph->softc; 468 csio = &done_ccb->csio; 469 switch (csio->ccb_h.ccb_type) { 470 case PASS_CCB_WAITING: 471 /* Caller will release the CCB */ 472 wakeup(&done_ccb->ccb_h.cbfcnp); 473 return; 474 } 475 xpt_release_ccb(done_ccb); 476 } 477 478 static int 479 passioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct proc *p) 480 { 481 struct cam_periph *periph; 482 struct pass_softc *softc; 483 u_int8_t unit; 484 int error; 485 486 487 /* unit = dkunit(dev); */ 488 /* XXX KDM fix this */ 489 unit = minor(dev) & 0xff; 490 491 periph = cam_extend_get(passperiphs, unit); 492 493 if (periph == NULL) 494 return(ENXIO); 495 496 softc = (struct pass_softc *)periph->softc; 497 498 error = 0; 499 500 switch (cmd) { 501 502 case CAMIOCOMMAND: 503 { 504 union ccb *inccb; 505 union ccb *ccb; 506 int ccb_malloced; 507 508 inccb = (union ccb *)addr; 509 510 /* 511 * Some CCB types, like scan bus and scan lun can only go 512 * through the transport layer device. 513 */ 514 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 515 xpt_print_path(periph->path); 516 printf("CCB function code %#x is restricted to the " 517 "XPT device\n", inccb->ccb_h.func_code); 518 error = ENODEV; 519 break; 520 } 521 522 /* 523 * Non-immediate CCBs need a CCB from the per-device pool 524 * of CCBs, which is scheduled by the transport layer. 525 * Immediate CCBs and user-supplied CCBs should just be 526 * malloced. 527 */ 528 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 529 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 530 ccb = cam_periph_getccb(periph, 531 inccb->ccb_h.pinfo.priority); 532 ccb_malloced = 0; 533 } else { 534 ccb = xpt_alloc_ccb(); 535 536 if (ccb != NULL) 537 xpt_setup_ccb(&ccb->ccb_h, periph->path, 538 inccb->ccb_h.pinfo.priority); 539 ccb_malloced = 1; 540 } 541 542 if (ccb == NULL) { 543 xpt_print_path(periph->path); 544 printf("unable to allocate CCB\n"); 545 error = ENOMEM; 546 break; 547 } 548 549 error = passsendccb(periph, ccb, inccb); 550 551 if (ccb_malloced) 552 xpt_free_ccb(ccb); 553 else 554 xpt_release_ccb(ccb); 555 556 break; 557 } 558 default: 559 error = cam_periph_ioctl(periph, cmd, addr, passerror); 560 break; 561 } 562 563 return(error); 564 } 565 566 /* 567 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 568 * should be the CCB that is copied in from the user. 569 */ 570 static int 571 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 572 { 573 struct pass_softc *softc; 574 struct cam_periph_map_info mapinfo; 575 int error, need_unmap; 576 577 softc = (struct pass_softc *)periph->softc; 578 579 need_unmap = 0; 580 581 /* 582 * There are some fields in the CCB header that need to be 583 * preserved, the rest we get from the user. 584 */ 585 xpt_merge_ccb(ccb, inccb); 586 587 /* 588 * There's no way for the user to have a completion 589 * function, so we put our own completion function in here. 590 */ 591 ccb->ccb_h.cbfcnp = passdone; 592 593 /* 594 * We only attempt to map the user memory into kernel space 595 * if they haven't passed in a physical memory pointer, 596 * and if there is actually an I/O operation to perform. 597 * Right now cam_periph_mapmem() only supports SCSI and device 598 * match CCBs. For the SCSI CCBs, we only pass the CCB in if 599 * there's actually data to map. cam_periph_mapmem() will do the 600 * right thing, even if there isn't data to map, but since CCBs 601 * without data are a reasonably common occurance (e.g. test unit 602 * ready), it will save a few cycles if we check for it here. 603 */ 604 if (((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) 605 && (((ccb->ccb_h.func_code == XPT_SCSI_IO) 606 && ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)) 607 || (ccb->ccb_h.func_code == XPT_DEV_MATCH))) { 608 609 bzero(&mapinfo, sizeof(mapinfo)); 610 611 error = cam_periph_mapmem(ccb, &mapinfo); 612 613 /* 614 * cam_periph_mapmem returned an error, we can't continue. 615 * Return the error to the user. 616 */ 617 if (error) 618 return(error); 619 620 /* 621 * We successfully mapped the memory in, so we need to 622 * unmap it when the transaction is done. 623 */ 624 need_unmap = 1; 625 } 626 627 /* 628 * If the user wants us to perform any error recovery, then honor 629 * that request. Otherwise, it's up to the user to perform any 630 * error recovery. 631 */ 632 error = cam_periph_runccb(ccb, 633 (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 634 passerror : NULL, 635 /* cam_flags */ CAM_RETRY_SELTO, 636 /* sense_flags */SF_RETRY_UA, 637 &softc->device_stats); 638 639 if (need_unmap != 0) 640 cam_periph_unmapmem(ccb, &mapinfo); 641 642 ccb->ccb_h.cbfcnp = NULL; 643 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 644 bcopy(ccb, inccb, sizeof(union ccb)); 645 646 return(error); 647 } 648 649 static int 650 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 651 { 652 struct cam_periph *periph; 653 struct pass_softc *softc; 654 655 periph = xpt_path_periph(ccb->ccb_h.path); 656 softc = (struct pass_softc *)periph->softc; 657 658 return(cam_periph_error(ccb, cam_flags, sense_flags, 659 &softc->saved_ccb)); 660 } 661