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