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