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