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