scsi_pass.c revision 312357
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: stable/10/sys/cam/scsi/scsi_pass.c 312357 2017-01-17 23:55:10Z ngie $"); 30 31#include <sys/param.h> 32#include <sys/systm.h> 33#include <sys/kernel.h> 34#include <sys/conf.h> 35#include <sys/types.h> 36#include <sys/bio.h> 37#include <sys/bus.h> 38#include <sys/devicestat.h> 39#include <sys/errno.h> 40#include <sys/fcntl.h> 41#include <sys/malloc.h> 42#include <sys/proc.h> 43#include <sys/poll.h> 44#include <sys/selinfo.h> 45#include <sys/sdt.h> 46#include <sys/taskqueue.h> 47#include <vm/uma.h> 48#include <vm/vm.h> 49#include <vm/vm_extern.h> 50 51#include <machine/bus.h> 52 53#include <cam/cam.h> 54#include <cam/cam_ccb.h> 55#include <cam/cam_periph.h> 56#include <cam/cam_queue.h> 57#include <cam/cam_xpt.h> 58#include <cam/cam_xpt_periph.h> 59#include <cam/cam_debug.h> 60#include <cam/cam_compat.h> 61#include <cam/cam_xpt_periph.h> 62 63#include <cam/scsi/scsi_all.h> 64#include <cam/scsi/scsi_pass.h> 65 66typedef enum { 67 PASS_FLAG_OPEN = 0x01, 68 PASS_FLAG_LOCKED = 0x02, 69 PASS_FLAG_INVALID = 0x04, 70 PASS_FLAG_INITIAL_PHYSPATH = 0x08, 71 PASS_FLAG_ZONE_INPROG = 0x10, 72 PASS_FLAG_ZONE_VALID = 0x20, 73 PASS_FLAG_UNMAPPED_CAPABLE = 0x40, 74 PASS_FLAG_ABANDONED_REF_SET = 0x80 75} pass_flags; 76 77typedef enum { 78 PASS_STATE_NORMAL 79} pass_state; 80 81typedef enum { 82 PASS_CCB_BUFFER_IO, 83 PASS_CCB_QUEUED_IO 84} pass_ccb_types; 85 86#define ccb_type ppriv_field0 87#define ccb_ioreq ppriv_ptr1 88 89/* 90 * The maximum number of memory segments we preallocate. 91 */ 92#define PASS_MAX_SEGS 16 93 94typedef enum { 95 PASS_IO_NONE = 0x00, 96 PASS_IO_USER_SEG_MALLOC = 0x01, 97 PASS_IO_KERN_SEG_MALLOC = 0x02, 98 PASS_IO_ABANDONED = 0x04 99} pass_io_flags; 100 101struct pass_io_req { 102 union ccb ccb; 103 union ccb *alloced_ccb; 104 union ccb *user_ccb_ptr; 105 camq_entry user_periph_links; 106 ccb_ppriv_area user_periph_priv; 107 struct cam_periph_map_info mapinfo; 108 pass_io_flags flags; 109 ccb_flags data_flags; 110 int num_user_segs; 111 bus_dma_segment_t user_segs[PASS_MAX_SEGS]; 112 int num_kern_segs; 113 bus_dma_segment_t kern_segs[PASS_MAX_SEGS]; 114 bus_dma_segment_t *user_segptr; 115 bus_dma_segment_t *kern_segptr; 116 int num_bufs; 117 uint32_t dirs[CAM_PERIPH_MAXMAPS]; 118 uint32_t lengths[CAM_PERIPH_MAXMAPS]; 119 uint8_t *user_bufs[CAM_PERIPH_MAXMAPS]; 120 uint8_t *kern_bufs[CAM_PERIPH_MAXMAPS]; 121 struct bintime start_time; 122 TAILQ_ENTRY(pass_io_req) links; 123}; 124 125struct pass_softc { 126 pass_state state; 127 pass_flags flags; 128 u_int8_t pd_type; 129 union ccb saved_ccb; 130 int open_count; 131 u_int maxio; 132 struct devstat *device_stats; 133 struct cdev *dev; 134 struct cdev *alias_dev; 135 struct task add_physpath_task; 136 struct task shutdown_kqueue_task; 137 struct selinfo read_select; 138 TAILQ_HEAD(, pass_io_req) incoming_queue; 139 TAILQ_HEAD(, pass_io_req) active_queue; 140 TAILQ_HEAD(, pass_io_req) abandoned_queue; 141 TAILQ_HEAD(, pass_io_req) done_queue; 142 struct cam_periph *periph; 143 char zone_name[12]; 144 char io_zone_name[12]; 145 uma_zone_t pass_zone; 146 uma_zone_t pass_io_zone; 147 size_t io_zone_size; 148}; 149 150static d_open_t passopen; 151static d_close_t passclose; 152static d_ioctl_t passioctl; 153static d_ioctl_t passdoioctl; 154static d_poll_t passpoll; 155static d_kqfilter_t passkqfilter; 156static void passreadfiltdetach(struct knote *kn); 157static int passreadfilt(struct knote *kn, long hint); 158 159static periph_init_t passinit; 160static periph_ctor_t passregister; 161static periph_oninv_t passoninvalidate; 162static periph_dtor_t passcleanup; 163static periph_start_t passstart; 164static void pass_shutdown_kqueue(void *context, int pending); 165static void pass_add_physpath(void *context, int pending); 166static void passasync(void *callback_arg, u_int32_t code, 167 struct cam_path *path, void *arg); 168static void passdone(struct cam_periph *periph, 169 union ccb *done_ccb); 170static int passcreatezone(struct cam_periph *periph); 171static void passiocleanup(struct pass_softc *softc, 172 struct pass_io_req *io_req); 173static int passcopysglist(struct cam_periph *periph, 174 struct pass_io_req *io_req, 175 ccb_flags direction); 176static int passmemsetup(struct cam_periph *periph, 177 struct pass_io_req *io_req); 178static int passmemdone(struct cam_periph *periph, 179 struct pass_io_req *io_req); 180static int passerror(union ccb *ccb, u_int32_t cam_flags, 181 u_int32_t sense_flags); 182static int passsendccb(struct cam_periph *periph, union ccb *ccb, 183 union ccb *inccb); 184 185static struct periph_driver passdriver = 186{ 187 passinit, "pass", 188 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0 189}; 190 191PERIPHDRIVER_DECLARE(pass, passdriver); 192 193static struct cdevsw pass_cdevsw = { 194 .d_version = D_VERSION, 195 .d_flags = D_TRACKCLOSE, 196 .d_open = passopen, 197 .d_close = passclose, 198 .d_ioctl = passioctl, 199 .d_poll = passpoll, 200 .d_kqfilter = passkqfilter, 201 .d_name = "pass", 202}; 203 204static struct filterops passread_filtops = { 205 .f_isfd = 1, 206 .f_detach = passreadfiltdetach, 207 .f_event = passreadfilt 208}; 209 210static MALLOC_DEFINE(M_SCSIPASS, "scsi_pass", "scsi passthrough buffers"); 211 212static void 213passinit(void) 214{ 215 cam_status status; 216 217 /* 218 * Install a global async callback. This callback will 219 * receive async callbacks like "new device found". 220 */ 221 status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL); 222 223 if (status != CAM_REQ_CMP) { 224 printf("pass: Failed to attach master async callback " 225 "due to status 0x%x!\n", status); 226 } 227 228} 229 230static void 231passrejectios(struct cam_periph *periph) 232{ 233 struct pass_io_req *io_req, *io_req2; 234 struct pass_softc *softc; 235 236 softc = (struct pass_softc *)periph->softc; 237 238 /* 239 * The user can no longer get status for I/O on the done queue, so 240 * clean up all outstanding I/O on the done queue. 241 */ 242 TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) { 243 TAILQ_REMOVE(&softc->done_queue, io_req, links); 244 passiocleanup(softc, io_req); 245 uma_zfree(softc->pass_zone, io_req); 246 } 247 248 /* 249 * The underlying device is gone, so we can't issue these I/Os. 250 * The devfs node has been shut down, so we can't return status to 251 * the user. Free any I/O left on the incoming queue. 252 */ 253 TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, io_req2) { 254 TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 255 passiocleanup(softc, io_req); 256 uma_zfree(softc->pass_zone, io_req); 257 } 258 259 /* 260 * Normally we would put I/Os on the abandoned queue and acquire a 261 * reference when we saw the final close. But, the device went 262 * away and devfs may have moved everything off to deadfs by the 263 * time the I/O done callback is called; as a result, we won't see 264 * any more closes. So, if we have any active I/Os, we need to put 265 * them on the abandoned queue. When the abandoned queue is empty, 266 * we'll release the remaining reference (see below) to the peripheral. 267 */ 268 TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, io_req2) { 269 TAILQ_REMOVE(&softc->active_queue, io_req, links); 270 io_req->flags |= PASS_IO_ABANDONED; 271 TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, links); 272 } 273 274 /* 275 * If we put any I/O on the abandoned queue, acquire a reference. 276 */ 277 if ((!TAILQ_EMPTY(&softc->abandoned_queue)) 278 && ((softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0)) { 279 cam_periph_doacquire(periph); 280 softc->flags |= PASS_FLAG_ABANDONED_REF_SET; 281 } 282} 283 284static void 285passdevgonecb(void *arg) 286{ 287 struct cam_periph *periph; 288 struct mtx *mtx; 289 struct pass_softc *softc; 290 int i; 291 292 periph = (struct cam_periph *)arg; 293 mtx = cam_periph_mtx(periph); 294 mtx_lock(mtx); 295 296 softc = (struct pass_softc *)periph->softc; 297 KASSERT(softc->open_count >= 0, ("Negative open count %d", 298 softc->open_count)); 299 300 /* 301 * When we get this callback, we will get no more close calls from 302 * devfs. So if we have any dangling opens, we need to release the 303 * reference held for that particular context. 304 */ 305 for (i = 0; i < softc->open_count; i++) 306 cam_periph_release_locked(periph); 307 308 softc->open_count = 0; 309 310 /* 311 * Release the reference held for the device node, it is gone now. 312 * Accordingly, inform all queued I/Os of their fate. 313 */ 314 cam_periph_release_locked(periph); 315 passrejectios(periph); 316 317 /* 318 * We reference the SIM lock directly here, instead of using 319 * cam_periph_unlock(). The reason is that the final call to 320 * cam_periph_release_locked() above could result in the periph 321 * getting freed. If that is the case, dereferencing the periph 322 * with a cam_periph_unlock() call would cause a page fault. 323 */ 324 mtx_unlock(mtx); 325 326 /* 327 * We have to remove our kqueue context from a thread because it 328 * may sleep. It would be nice if we could get a callback from 329 * kqueue when it is done cleaning up resources. 330 */ 331 taskqueue_enqueue(taskqueue_thread, &softc->shutdown_kqueue_task); 332} 333 334static void 335passoninvalidate(struct cam_periph *periph) 336{ 337 struct pass_softc *softc; 338 339 softc = (struct pass_softc *)periph->softc; 340 341 /* 342 * De-register any async callbacks. 343 */ 344 xpt_register_async(0, passasync, periph, periph->path); 345 346 softc->flags |= PASS_FLAG_INVALID; 347 348 /* 349 * Tell devfs this device has gone away, and ask for a callback 350 * when it has cleaned up its state. 351 */ 352 destroy_dev_sched_cb(softc->dev, passdevgonecb, periph); 353} 354 355static void 356passcleanup(struct cam_periph *periph) 357{ 358 struct pass_softc *softc; 359 360 softc = (struct pass_softc *)periph->softc; 361 362 cam_periph_assert(periph, MA_OWNED); 363 KASSERT(TAILQ_EMPTY(&softc->active_queue), 364 ("%s called when there are commands on the active queue!\n", 365 __func__)); 366 KASSERT(TAILQ_EMPTY(&softc->abandoned_queue), 367 ("%s called when there are commands on the abandoned queue!\n", 368 __func__)); 369 KASSERT(TAILQ_EMPTY(&softc->incoming_queue), 370 ("%s called when there are commands on the incoming queue!\n", 371 __func__)); 372 KASSERT(TAILQ_EMPTY(&softc->done_queue), 373 ("%s called when there are commands on the done queue!\n", 374 __func__)); 375 376 devstat_remove_entry(softc->device_stats); 377 378 cam_periph_unlock(periph); 379 380 /* 381 * We call taskqueue_drain() for the physpath task to make sure it 382 * is complete. We drop the lock because this can potentially 383 * sleep. XXX KDM that is bad. Need a way to get a callback when 384 * a taskqueue is drained. 385 * 386 * Note that we don't drain the kqueue shutdown task queue. This 387 * is because we hold a reference on the periph for kqueue, and 388 * release that reference from the kqueue shutdown task queue. So 389 * we cannot come into this routine unless we've released that 390 * reference. Also, because that could be the last reference, we 391 * could be called from the cam_periph_release() call in 392 * pass_shutdown_kqueue(). In that case, the taskqueue_drain() 393 * would deadlock. It would be preferable if we had a way to 394 * get a callback when a taskqueue is done. 395 */ 396 taskqueue_drain(taskqueue_thread, &softc->add_physpath_task); 397 398 cam_periph_lock(periph); 399 400 free(softc, M_DEVBUF); 401} 402 403static void 404pass_shutdown_kqueue(void *context, int pending) 405{ 406 struct cam_periph *periph; 407 struct pass_softc *softc; 408 409 periph = context; 410 softc = periph->softc; 411 412 knlist_clear(&softc->read_select.si_note, /*is_locked*/ 0); 413 knlist_destroy(&softc->read_select.si_note); 414 415 /* 416 * Release the reference we held for kqueue. 417 */ 418 cam_periph_release(periph); 419} 420 421static void 422pass_add_physpath(void *context, int pending) 423{ 424 struct cam_periph *periph; 425 struct pass_softc *softc; 426 struct mtx *mtx; 427 char *physpath; 428 429 /* 430 * If we have one, create a devfs alias for our 431 * physical path. 432 */ 433 periph = context; 434 softc = periph->softc; 435 physpath = malloc(MAXPATHLEN, M_DEVBUF, M_WAITOK); 436 mtx = cam_periph_mtx(periph); 437 mtx_lock(mtx); 438 439 if (periph->flags & CAM_PERIPH_INVALID) 440 goto out; 441 442 if (xpt_getattr(physpath, MAXPATHLEN, 443 "GEOM::physpath", periph->path) == 0 444 && strlen(physpath) != 0) { 445 446 mtx_unlock(mtx); 447 make_dev_physpath_alias(MAKEDEV_WAITOK, &softc->alias_dev, 448 softc->dev, softc->alias_dev, physpath); 449 mtx_lock(mtx); 450 } 451 452out: 453 /* 454 * Now that we've made our alias, we no longer have to have a 455 * reference to the device. 456 */ 457 if ((softc->flags & PASS_FLAG_INITIAL_PHYSPATH) == 0) 458 softc->flags |= PASS_FLAG_INITIAL_PHYSPATH; 459 460 /* 461 * We always acquire a reference to the periph before queueing this 462 * task queue function, so it won't go away before we run. 463 */ 464 while (pending-- > 0) 465 cam_periph_release_locked(periph); 466 mtx_unlock(mtx); 467 468 free(physpath, M_DEVBUF); 469} 470 471static void 472passasync(void *callback_arg, u_int32_t code, 473 struct cam_path *path, void *arg) 474{ 475 struct cam_periph *periph; 476 477 periph = (struct cam_periph *)callback_arg; 478 479 switch (code) { 480 case AC_FOUND_DEVICE: 481 { 482 struct ccb_getdev *cgd; 483 cam_status status; 484 485 cgd = (struct ccb_getdev *)arg; 486 if (cgd == NULL) 487 break; 488 489 /* 490 * Allocate a peripheral instance for 491 * this device and start the probe 492 * process. 493 */ 494 status = cam_periph_alloc(passregister, passoninvalidate, 495 passcleanup, passstart, "pass", 496 CAM_PERIPH_BIO, path, 497 passasync, AC_FOUND_DEVICE, cgd); 498 499 if (status != CAM_REQ_CMP 500 && status != CAM_REQ_INPROG) { 501 const struct cam_status_entry *entry; 502 503 entry = cam_fetch_status_entry(status); 504 505 printf("passasync: Unable to attach new device " 506 "due to status %#x: %s\n", status, entry ? 507 entry->status_text : "Unknown"); 508 } 509 510 break; 511 } 512 case AC_ADVINFO_CHANGED: 513 { 514 uintptr_t buftype; 515 516 buftype = (uintptr_t)arg; 517 if (buftype == CDAI_TYPE_PHYS_PATH) { 518 struct pass_softc *softc; 519 cam_status status; 520 521 softc = (struct pass_softc *)periph->softc; 522 /* 523 * Acquire a reference to the periph before we 524 * start the taskqueue, so that we don't run into 525 * a situation where the periph goes away before 526 * the task queue has a chance to run. 527 */ 528 status = cam_periph_acquire(periph); 529 if (status != CAM_REQ_CMP) 530 break; 531 532 taskqueue_enqueue(taskqueue_thread, 533 &softc->add_physpath_task); 534 } 535 break; 536 } 537 default: 538 cam_periph_async(periph, code, path, arg); 539 break; 540 } 541} 542 543static cam_status 544passregister(struct cam_periph *periph, void *arg) 545{ 546 struct pass_softc *softc; 547 struct ccb_getdev *cgd; 548 struct ccb_pathinq cpi; 549 struct make_dev_args args; 550 int error, no_tags; 551 552 cgd = (struct ccb_getdev *)arg; 553 if (cgd == NULL) { 554 printf("%s: no getdev CCB, can't register device\n", __func__); 555 return(CAM_REQ_CMP_ERR); 556 } 557 558 softc = (struct pass_softc *)malloc(sizeof(*softc), 559 M_DEVBUF, M_NOWAIT); 560 561 if (softc == NULL) { 562 printf("%s: Unable to probe new device. " 563 "Unable to allocate softc\n", __func__); 564 return(CAM_REQ_CMP_ERR); 565 } 566 567 bzero(softc, sizeof(*softc)); 568 softc->state = PASS_STATE_NORMAL; 569 if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI) 570 softc->pd_type = SID_TYPE(&cgd->inq_data); 571 else if (cgd->protocol == PROTO_SATAPM) 572 softc->pd_type = T_ENCLOSURE; 573 else 574 softc->pd_type = T_DIRECT; 575 576 periph->softc = softc; 577 softc->periph = periph; 578 TAILQ_INIT(&softc->incoming_queue); 579 TAILQ_INIT(&softc->active_queue); 580 TAILQ_INIT(&softc->abandoned_queue); 581 TAILQ_INIT(&softc->done_queue); 582 snprintf(softc->zone_name, sizeof(softc->zone_name), "%s%d", 583 periph->periph_name, periph->unit_number); 584 snprintf(softc->io_zone_name, sizeof(softc->io_zone_name), "%s%dIO", 585 periph->periph_name, periph->unit_number); 586 softc->io_zone_size = MAXPHYS; 587 knlist_init_mtx(&softc->read_select.si_note, cam_periph_mtx(periph)); 588 589 bzero(&cpi, sizeof(cpi)); 590 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL); 591 cpi.ccb_h.func_code = XPT_PATH_INQ; 592 xpt_action((union ccb *)&cpi); 593 594 if (cpi.maxio == 0) 595 softc->maxio = DFLTPHYS; /* traditional default */ 596 else if (cpi.maxio > MAXPHYS) 597 softc->maxio = MAXPHYS; /* for safety */ 598 else 599 softc->maxio = cpi.maxio; /* real value */ 600 601 if (cpi.hba_misc & PIM_UNMAPPED) 602 softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE; 603 604 /* 605 * We pass in 0 for a blocksize, since we don't 606 * know what the blocksize of this device is, if 607 * it even has a blocksize. 608 */ 609 cam_periph_unlock(periph); 610 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 611 softc->device_stats = devstat_new_entry("pass", 612 periph->unit_number, 0, 613 DEVSTAT_NO_BLOCKSIZE 614 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 615 softc->pd_type | 616 XPORT_DEVSTAT_TYPE(cpi.transport) | 617 DEVSTAT_TYPE_PASS, 618 DEVSTAT_PRIORITY_PASS); 619 620 /* 621 * Initialize the taskqueue handler for shutting down kqueue. 622 */ 623 TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0, 624 pass_shutdown_kqueue, periph); 625 626 /* 627 * Acquire a reference to the periph that we can release once we've 628 * cleaned up the kqueue. 629 */ 630 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 631 xpt_print(periph->path, "%s: lost periph during " 632 "registration!\n", __func__); 633 cam_periph_lock(periph); 634 return (CAM_REQ_CMP_ERR); 635 } 636 637 /* 638 * Acquire a reference to the periph before we create the devfs 639 * instance for it. We'll release this reference once the devfs 640 * instance has been freed. 641 */ 642 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 643 xpt_print(periph->path, "%s: lost periph during " 644 "registration!\n", __func__); 645 cam_periph_lock(periph); 646 return (CAM_REQ_CMP_ERR); 647 } 648 649 /* Register the device */ 650 make_dev_args_init(&args); 651 args.mda_devsw = &pass_cdevsw; 652 args.mda_unit = periph->unit_number; 653 args.mda_uid = UID_ROOT; 654 args.mda_gid = GID_OPERATOR; 655 args.mda_mode = 0600; 656 args.mda_si_drv1 = periph; 657 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, 658 periph->unit_number); 659 if (error != 0) { 660 cam_periph_lock(periph); 661 cam_periph_release_locked(periph); 662 return (CAM_REQ_CMP_ERR); 663 } 664 665 /* 666 * Hold a reference to the periph before we create the physical 667 * path alias so it can't go away. 668 */ 669 if (cam_periph_acquire(periph) != CAM_REQ_CMP) { 670 xpt_print(periph->path, "%s: lost periph during " 671 "registration!\n", __func__); 672 cam_periph_lock(periph); 673 return (CAM_REQ_CMP_ERR); 674 } 675 676 cam_periph_lock(periph); 677 678 TASK_INIT(&softc->add_physpath_task, /*priority*/0, 679 pass_add_physpath, periph); 680 681 /* 682 * See if physical path information is already available. 683 */ 684 taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task); 685 686 /* 687 * Add an async callback so that we get notified if 688 * this device goes away or its physical path 689 * (stored in the advanced info data of the EDT) has 690 * changed. 691 */ 692 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED, 693 passasync, periph, periph->path); 694 695 if (bootverbose) 696 xpt_announce_periph(periph, NULL); 697 698 return(CAM_REQ_CMP); 699} 700 701static int 702passopen(struct cdev *dev, int flags, int fmt, struct thread *td) 703{ 704 struct cam_periph *periph; 705 struct pass_softc *softc; 706 int error; 707 708 periph = (struct cam_periph *)dev->si_drv1; 709 if (cam_periph_acquire(periph) != CAM_REQ_CMP) 710 return (ENXIO); 711 712 cam_periph_lock(periph); 713 714 softc = (struct pass_softc *)periph->softc; 715 716 if (softc->flags & PASS_FLAG_INVALID) { 717 cam_periph_release_locked(periph); 718 cam_periph_unlock(periph); 719 return(ENXIO); 720 } 721 722 /* 723 * Don't allow access when we're running at a high securelevel. 724 */ 725 error = securelevel_gt(td->td_ucred, 1); 726 if (error) { 727 cam_periph_release_locked(periph); 728 cam_periph_unlock(periph); 729 return(error); 730 } 731 732 /* 733 * Only allow read-write access. 734 */ 735 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) { 736 cam_periph_release_locked(periph); 737 cam_periph_unlock(periph); 738 return(EPERM); 739 } 740 741 /* 742 * We don't allow nonblocking access. 743 */ 744 if ((flags & O_NONBLOCK) != 0) { 745 xpt_print(periph->path, "can't do nonblocking access\n"); 746 cam_periph_release_locked(periph); 747 cam_periph_unlock(periph); 748 return(EINVAL); 749 } 750 751 softc->open_count++; 752 753 cam_periph_unlock(periph); 754 755 return (error); 756} 757 758static int 759passclose(struct cdev *dev, int flag, int fmt, struct thread *td) 760{ 761 struct cam_periph *periph; 762 struct pass_softc *softc; 763 struct mtx *mtx; 764 765 periph = (struct cam_periph *)dev->si_drv1; 766 mtx = cam_periph_mtx(periph); 767 mtx_lock(mtx); 768 769 softc = periph->softc; 770 softc->open_count--; 771 772 if (softc->open_count == 0) { 773 struct pass_io_req *io_req, *io_req2; 774 int need_unlock; 775 776 need_unlock = 0; 777 778 TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) { 779 TAILQ_REMOVE(&softc->done_queue, io_req, links); 780 passiocleanup(softc, io_req); 781 uma_zfree(softc->pass_zone, io_req); 782 } 783 784 TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, 785 io_req2) { 786 TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 787 passiocleanup(softc, io_req); 788 uma_zfree(softc->pass_zone, io_req); 789 } 790 791 /* 792 * If there are any active I/Os, we need to forcibly acquire a 793 * reference to the peripheral so that we don't go away 794 * before they complete. We'll release the reference when 795 * the abandoned queue is empty. 796 */ 797 io_req = TAILQ_FIRST(&softc->active_queue); 798 if ((io_req != NULL) 799 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) { 800 cam_periph_doacquire(periph); 801 softc->flags |= PASS_FLAG_ABANDONED_REF_SET; 802 } 803 804 /* 805 * Since the I/O in the active queue is not under our 806 * control, just set a flag so that we can clean it up when 807 * it completes and put it on the abandoned queue. This 808 * will prevent our sending spurious completions in the 809 * event that the device is opened again before these I/Os 810 * complete. 811 */ 812 TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, 813 io_req2) { 814 TAILQ_REMOVE(&softc->active_queue, io_req, links); 815 io_req->flags |= PASS_IO_ABANDONED; 816 TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, 817 links); 818 } 819 } 820 821 cam_periph_release_locked(periph); 822 823 /* 824 * We reference the lock directly here, instead of using 825 * cam_periph_unlock(). The reason is that the call to 826 * cam_periph_release_locked() above could result in the periph 827 * getting freed. If that is the case, dereferencing the periph 828 * with a cam_periph_unlock() call would cause a page fault. 829 * 830 * cam_periph_release() avoids this problem using the same method, 831 * but we're manually acquiring and dropping the lock here to 832 * protect the open count and avoid another lock acquisition and 833 * release. 834 */ 835 mtx_unlock(mtx); 836 837 return (0); 838} 839 840 841static void 842passstart(struct cam_periph *periph, union ccb *start_ccb) 843{ 844 struct pass_softc *softc; 845 846 softc = (struct pass_softc *)periph->softc; 847 848 switch (softc->state) { 849 case PASS_STATE_NORMAL: { 850 struct pass_io_req *io_req; 851 852 /* 853 * Check for any queued I/O requests that require an 854 * allocated slot. 855 */ 856 io_req = TAILQ_FIRST(&softc->incoming_queue); 857 if (io_req == NULL) { 858 xpt_release_ccb(start_ccb); 859 break; 860 } 861 TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 862 TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 863 /* 864 * Merge the user's CCB into the allocated CCB. 865 */ 866 xpt_merge_ccb(start_ccb, &io_req->ccb); 867 start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO; 868 start_ccb->ccb_h.ccb_ioreq = io_req; 869 start_ccb->ccb_h.cbfcnp = passdone; 870 io_req->alloced_ccb = start_ccb; 871 binuptime(&io_req->start_time); 872 devstat_start_transaction(softc->device_stats, 873 &io_req->start_time); 874 875 xpt_action(start_ccb); 876 877 /* 878 * If we have any more I/O waiting, schedule ourselves again. 879 */ 880 if (!TAILQ_EMPTY(&softc->incoming_queue)) 881 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 882 break; 883 } 884 default: 885 break; 886 } 887} 888 889static void 890passdone(struct cam_periph *periph, union ccb *done_ccb) 891{ 892 struct pass_softc *softc; 893 struct ccb_scsiio *csio; 894 895 softc = (struct pass_softc *)periph->softc; 896 897 cam_periph_assert(periph, MA_OWNED); 898 899 csio = &done_ccb->csio; 900 switch (csio->ccb_h.ccb_type) { 901 case PASS_CCB_QUEUED_IO: { 902 struct pass_io_req *io_req; 903 904 io_req = done_ccb->ccb_h.ccb_ioreq; 905#if 0 906 xpt_print(periph->path, "%s: called for user CCB %p\n", 907 __func__, io_req->user_ccb_ptr); 908#endif 909 if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 910 && (done_ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) 911 && ((io_req->flags & PASS_IO_ABANDONED) == 0)) { 912 int error; 913 914 error = passerror(done_ccb, CAM_RETRY_SELTO, 915 SF_RETRY_UA | SF_NO_PRINT); 916 917 if (error == ERESTART) { 918 /* 919 * A retry was scheduled, so 920 * just return. 921 */ 922 return; 923 } 924 } 925 926 /* 927 * Copy the allocated CCB contents back to the malloced CCB 928 * so we can give status back to the user when he requests it. 929 */ 930 bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb)); 931 932 /* 933 * Log data/transaction completion with devstat(9). 934 */ 935 switch (done_ccb->ccb_h.func_code) { 936 case XPT_SCSI_IO: 937 devstat_end_transaction(softc->device_stats, 938 done_ccb->csio.dxfer_len - done_ccb->csio.resid, 939 done_ccb->csio.tag_action & 0x3, 940 ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 941 CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 942 (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 943 DEVSTAT_WRITE : DEVSTAT_READ, NULL, 944 &io_req->start_time); 945 break; 946 case XPT_ATA_IO: 947 devstat_end_transaction(softc->device_stats, 948 done_ccb->ataio.dxfer_len - done_ccb->ataio.resid, 949 done_ccb->ataio.tag_action & 0x3, 950 ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 951 CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 952 (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 953 DEVSTAT_WRITE : DEVSTAT_READ, NULL, 954 &io_req->start_time); 955 break; 956 case XPT_SMP_IO: 957 /* 958 * XXX KDM this isn't quite right, but there isn't 959 * currently an easy way to represent a bidirectional 960 * transfer in devstat. The only way to do it 961 * and have the byte counts come out right would 962 * mean that we would have to record two 963 * transactions, one for the request and one for the 964 * response. For now, so that we report something, 965 * just treat the entire thing as a read. 966 */ 967 devstat_end_transaction(softc->device_stats, 968 done_ccb->smpio.smp_request_len + 969 done_ccb->smpio.smp_response_len, 970 DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL, 971 &io_req->start_time); 972 break; 973 default: 974 devstat_end_transaction(softc->device_stats, 0, 975 DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL, 976 &io_req->start_time); 977 break; 978 } 979 980 /* 981 * In the normal case, take the completed I/O off of the 982 * active queue and put it on the done queue. Notitfy the 983 * user that we have a completed I/O. 984 */ 985 if ((io_req->flags & PASS_IO_ABANDONED) == 0) { 986 TAILQ_REMOVE(&softc->active_queue, io_req, links); 987 TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 988 selwakeuppri(&softc->read_select, PRIBIO); 989 KNOTE_LOCKED(&softc->read_select.si_note, 0); 990 } else { 991 /* 992 * In the case of an abandoned I/O (final close 993 * without fetching the I/O), take it off of the 994 * abandoned queue and free it. 995 */ 996 TAILQ_REMOVE(&softc->abandoned_queue, io_req, links); 997 passiocleanup(softc, io_req); 998 uma_zfree(softc->pass_zone, io_req); 999 1000 /* 1001 * Release the done_ccb here, since we may wind up 1002 * freeing the peripheral when we decrement the 1003 * reference count below. 1004 */ 1005 xpt_release_ccb(done_ccb); 1006 1007 /* 1008 * If the abandoned queue is empty, we can release 1009 * our reference to the periph since we won't have 1010 * any more completions coming. 1011 */ 1012 if ((TAILQ_EMPTY(&softc->abandoned_queue)) 1013 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) { 1014 softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET; 1015 cam_periph_release_locked(periph); 1016 } 1017 1018 /* 1019 * We have already released the CCB, so we can 1020 * return. 1021 */ 1022 return; 1023 } 1024 break; 1025 } 1026 } 1027 xpt_release_ccb(done_ccb); 1028} 1029 1030static int 1031passcreatezone(struct cam_periph *periph) 1032{ 1033 struct pass_softc *softc; 1034 int error; 1035 1036 error = 0; 1037 softc = (struct pass_softc *)periph->softc; 1038 1039 cam_periph_assert(periph, MA_OWNED); 1040 KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0), 1041 ("%s called when the pass(4) zone is valid!\n", __func__)); 1042 KASSERT((softc->pass_zone == NULL), 1043 ("%s called when the pass(4) zone is allocated!\n", __func__)); 1044 1045 if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) { 1046 1047 /* 1048 * We're the first context through, so we need to create 1049 * the pass(4) UMA zone for I/O requests. 1050 */ 1051 softc->flags |= PASS_FLAG_ZONE_INPROG; 1052 1053 /* 1054 * uma_zcreate() does a blocking (M_WAITOK) allocation, 1055 * so we cannot hold a mutex while we call it. 1056 */ 1057 cam_periph_unlock(periph); 1058 1059 softc->pass_zone = uma_zcreate(softc->zone_name, 1060 sizeof(struct pass_io_req), NULL, NULL, NULL, NULL, 1061 /*align*/ 0, /*flags*/ 0); 1062 1063 softc->pass_io_zone = uma_zcreate(softc->io_zone_name, 1064 softc->io_zone_size, NULL, NULL, NULL, NULL, 1065 /*align*/ 0, /*flags*/ 0); 1066 1067 cam_periph_lock(periph); 1068 1069 if ((softc->pass_zone == NULL) 1070 || (softc->pass_io_zone == NULL)) { 1071 if (softc->pass_zone == NULL) 1072 xpt_print(periph->path, "unable to allocate " 1073 "IO Req UMA zone\n"); 1074 else 1075 xpt_print(periph->path, "unable to allocate " 1076 "IO UMA zone\n"); 1077 softc->flags &= ~PASS_FLAG_ZONE_INPROG; 1078 goto bailout; 1079 } 1080 1081 /* 1082 * Set the flags appropriately and notify any other waiters. 1083 */ 1084 softc->flags &= PASS_FLAG_ZONE_INPROG; 1085 softc->flags |= PASS_FLAG_ZONE_VALID; 1086 wakeup(&softc->pass_zone); 1087 } else { 1088 /* 1089 * In this case, the UMA zone has not yet been created, but 1090 * another context is in the process of creating it. We 1091 * need to sleep until the creation is either done or has 1092 * failed. 1093 */ 1094 while ((softc->flags & PASS_FLAG_ZONE_INPROG) 1095 && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) { 1096 error = msleep(&softc->pass_zone, 1097 cam_periph_mtx(periph), PRIBIO, 1098 "paszon", 0); 1099 if (error != 0) 1100 goto bailout; 1101 } 1102 /* 1103 * If the zone creation failed, no luck for the user. 1104 */ 1105 if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){ 1106 error = ENOMEM; 1107 goto bailout; 1108 } 1109 } 1110bailout: 1111 return (error); 1112} 1113 1114static void 1115passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req) 1116{ 1117 union ccb *ccb; 1118 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1119 int i, numbufs; 1120 1121 ccb = &io_req->ccb; 1122 1123 switch (ccb->ccb_h.func_code) { 1124 case XPT_DEV_MATCH: 1125 numbufs = min(io_req->num_bufs, 2); 1126 1127 if (numbufs == 1) { 1128 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1129 } else { 1130 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1131 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1132 } 1133 break; 1134 case XPT_SCSI_IO: 1135 case XPT_CONT_TARGET_IO: 1136 data_ptrs[0] = &ccb->csio.data_ptr; 1137 numbufs = min(io_req->num_bufs, 1); 1138 break; 1139 case XPT_ATA_IO: 1140 data_ptrs[0] = &ccb->ataio.data_ptr; 1141 numbufs = min(io_req->num_bufs, 1); 1142 break; 1143 case XPT_SMP_IO: 1144 numbufs = min(io_req->num_bufs, 2); 1145 data_ptrs[0] = &ccb->smpio.smp_request; 1146 data_ptrs[1] = &ccb->smpio.smp_response; 1147 break; 1148 case XPT_DEV_ADVINFO: 1149 numbufs = min(io_req->num_bufs, 1); 1150 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1151 break; 1152 default: 1153 /* allow ourselves to be swapped once again */ 1154 return; 1155 break; /* NOTREACHED */ 1156 } 1157 1158 if (io_req->flags & PASS_IO_USER_SEG_MALLOC) { 1159 free(io_req->user_segptr, M_SCSIPASS); 1160 io_req->user_segptr = NULL; 1161 } 1162 1163 /* 1164 * We only want to free memory we malloced. 1165 */ 1166 if (io_req->data_flags == CAM_DATA_VADDR) { 1167 for (i = 0; i < io_req->num_bufs; i++) { 1168 if (io_req->kern_bufs[i] == NULL) 1169 continue; 1170 1171 free(io_req->kern_bufs[i], M_SCSIPASS); 1172 io_req->kern_bufs[i] = NULL; 1173 } 1174 } else if (io_req->data_flags == CAM_DATA_SG) { 1175 for (i = 0; i < io_req->num_kern_segs; i++) { 1176 if ((uint8_t *)(uintptr_t) 1177 io_req->kern_segptr[i].ds_addr == NULL) 1178 continue; 1179 1180 uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t) 1181 io_req->kern_segptr[i].ds_addr); 1182 io_req->kern_segptr[i].ds_addr = 0; 1183 } 1184 } 1185 1186 if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) { 1187 free(io_req->kern_segptr, M_SCSIPASS); 1188 io_req->kern_segptr = NULL; 1189 } 1190 1191 if (io_req->data_flags != CAM_DATA_PADDR) { 1192 for (i = 0; i < numbufs; i++) { 1193 /* 1194 * Restore the user's buffer pointers to their 1195 * previous values. 1196 */ 1197 if (io_req->user_bufs[i] != NULL) 1198 *data_ptrs[i] = io_req->user_bufs[i]; 1199 } 1200 } 1201 1202} 1203 1204static int 1205passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req, 1206 ccb_flags direction) 1207{ 1208 bus_size_t kern_watermark, user_watermark, len_copied, len_to_copy; 1209 bus_dma_segment_t *user_sglist, *kern_sglist; 1210 int i, j, error; 1211 1212 error = 0; 1213 kern_watermark = 0; 1214 user_watermark = 0; 1215 len_to_copy = 0; 1216 len_copied = 0; 1217 user_sglist = io_req->user_segptr; 1218 kern_sglist = io_req->kern_segptr; 1219 1220 for (i = 0, j = 0; i < io_req->num_user_segs && 1221 j < io_req->num_kern_segs;) { 1222 uint8_t *user_ptr, *kern_ptr; 1223 1224 len_to_copy = min(user_sglist[i].ds_len -user_watermark, 1225 kern_sglist[j].ds_len - kern_watermark); 1226 1227 user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr; 1228 user_ptr = user_ptr + user_watermark; 1229 kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr; 1230 kern_ptr = kern_ptr + kern_watermark; 1231 1232 user_watermark += len_to_copy; 1233 kern_watermark += len_to_copy; 1234 1235 if (!useracc(user_ptr, len_to_copy, 1236 (direction == CAM_DIR_IN) ? VM_PROT_WRITE : VM_PROT_READ)) { 1237 xpt_print(periph->path, "%s: unable to access user " 1238 "S/G list element %p len %zu\n", __func__, 1239 user_ptr, len_to_copy); 1240 error = EFAULT; 1241 goto bailout; 1242 } 1243 1244 if (direction == CAM_DIR_IN) { 1245 error = copyout(kern_ptr, user_ptr, len_to_copy); 1246 if (error != 0) { 1247 xpt_print(periph->path, "%s: copyout of %u " 1248 "bytes from %p to %p failed with " 1249 "error %d\n", __func__, len_to_copy, 1250 kern_ptr, user_ptr, error); 1251 goto bailout; 1252 } 1253 } else { 1254 error = copyin(user_ptr, kern_ptr, len_to_copy); 1255 if (error != 0) { 1256 xpt_print(periph->path, "%s: copyin of %u " 1257 "bytes from %p to %p failed with " 1258 "error %d\n", __func__, len_to_copy, 1259 user_ptr, kern_ptr, error); 1260 goto bailout; 1261 } 1262 } 1263 1264 len_copied += len_to_copy; 1265 1266 if (user_sglist[i].ds_len == user_watermark) { 1267 i++; 1268 user_watermark = 0; 1269 } 1270 1271 if (kern_sglist[j].ds_len == kern_watermark) { 1272 j++; 1273 kern_watermark = 0; 1274 } 1275 } 1276 1277bailout: 1278 1279 return (error); 1280} 1281 1282static int 1283passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req) 1284{ 1285 union ccb *ccb; 1286 struct pass_softc *softc; 1287 int numbufs, i; 1288 uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1289 uint32_t lengths[CAM_PERIPH_MAXMAPS]; 1290 uint32_t dirs[CAM_PERIPH_MAXMAPS]; 1291 uint32_t num_segs; 1292 uint16_t *seg_cnt_ptr; 1293 size_t maxmap; 1294 int error; 1295 1296 cam_periph_assert(periph, MA_NOTOWNED); 1297 1298 softc = periph->softc; 1299 1300 error = 0; 1301 ccb = &io_req->ccb; 1302 maxmap = 0; 1303 num_segs = 0; 1304 seg_cnt_ptr = NULL; 1305 1306 switch(ccb->ccb_h.func_code) { 1307 case XPT_DEV_MATCH: 1308 if (ccb->cdm.match_buf_len == 0) { 1309 printf("%s: invalid match buffer length 0\n", __func__); 1310 return(EINVAL); 1311 } 1312 if (ccb->cdm.pattern_buf_len > 0) { 1313 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1314 lengths[0] = ccb->cdm.pattern_buf_len; 1315 dirs[0] = CAM_DIR_OUT; 1316 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1317 lengths[1] = ccb->cdm.match_buf_len; 1318 dirs[1] = CAM_DIR_IN; 1319 numbufs = 2; 1320 } else { 1321 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1322 lengths[0] = ccb->cdm.match_buf_len; 1323 dirs[0] = CAM_DIR_IN; 1324 numbufs = 1; 1325 } 1326 io_req->data_flags = CAM_DATA_VADDR; 1327 break; 1328 case XPT_SCSI_IO: 1329 case XPT_CONT_TARGET_IO: 1330 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1331 return(0); 1332 1333 /* 1334 * The user shouldn't be able to supply a bio. 1335 */ 1336 if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO) 1337 return (EINVAL); 1338 1339 io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK; 1340 1341 data_ptrs[0] = &ccb->csio.data_ptr; 1342 lengths[0] = ccb->csio.dxfer_len; 1343 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1344 num_segs = ccb->csio.sglist_cnt; 1345 seg_cnt_ptr = &ccb->csio.sglist_cnt; 1346 numbufs = 1; 1347 maxmap = softc->maxio; 1348 break; 1349 case XPT_ATA_IO: 1350 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1351 return(0); 1352 1353 /* 1354 * We only support a single virtual address for ATA I/O. 1355 */ 1356 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 1357 return (EINVAL); 1358 1359 io_req->data_flags = CAM_DATA_VADDR; 1360 1361 data_ptrs[0] = &ccb->ataio.data_ptr; 1362 lengths[0] = ccb->ataio.dxfer_len; 1363 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1364 numbufs = 1; 1365 maxmap = softc->maxio; 1366 break; 1367 case XPT_SMP_IO: 1368 io_req->data_flags = CAM_DATA_VADDR; 1369 1370 data_ptrs[0] = &ccb->smpio.smp_request; 1371 lengths[0] = ccb->smpio.smp_request_len; 1372 dirs[0] = CAM_DIR_OUT; 1373 data_ptrs[1] = &ccb->smpio.smp_response; 1374 lengths[1] = ccb->smpio.smp_response_len; 1375 dirs[1] = CAM_DIR_IN; 1376 numbufs = 2; 1377 maxmap = softc->maxio; 1378 break; 1379 case XPT_DEV_ADVINFO: 1380 if (ccb->cdai.bufsiz == 0) 1381 return (0); 1382 1383 io_req->data_flags = CAM_DATA_VADDR; 1384 1385 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1386 lengths[0] = ccb->cdai.bufsiz; 1387 dirs[0] = CAM_DIR_IN; 1388 numbufs = 1; 1389 break; 1390 default: 1391 return(EINVAL); 1392 break; /* NOTREACHED */ 1393 } 1394 1395 io_req->num_bufs = numbufs; 1396 1397 /* 1398 * If there is a maximum, check to make sure that the user's 1399 * request fits within the limit. In general, we should only have 1400 * a maximum length for requests that go to hardware. Otherwise it 1401 * is whatever we're able to malloc. 1402 */ 1403 for (i = 0; i < numbufs; i++) { 1404 io_req->user_bufs[i] = *data_ptrs[i]; 1405 io_req->dirs[i] = dirs[i]; 1406 io_req->lengths[i] = lengths[i]; 1407 1408 if (maxmap == 0) 1409 continue; 1410 1411 if (lengths[i] <= maxmap) 1412 continue; 1413 1414 xpt_print(periph->path, "%s: data length %u > max allowed %u " 1415 "bytes\n", __func__, lengths[i], maxmap); 1416 error = EINVAL; 1417 goto bailout; 1418 } 1419 1420 switch (io_req->data_flags) { 1421 case CAM_DATA_VADDR: 1422 /* Map or copy the buffer into kernel address space */ 1423 for (i = 0; i < numbufs; i++) { 1424 uint8_t *tmp_buf; 1425 1426 /* 1427 * If for some reason no length is specified, we 1428 * don't need to allocate anything. 1429 */ 1430 if (io_req->lengths[i] == 0) 1431 continue; 1432 1433 /* 1434 * Make sure that the user's buffer is accessible 1435 * to that process. 1436 */ 1437 if (!useracc(io_req->user_bufs[i], io_req->lengths[i], 1438 (io_req->dirs[i] == CAM_DIR_IN) ? VM_PROT_WRITE : 1439 VM_PROT_READ)) { 1440 xpt_print(periph->path, "%s: user address %p " 1441 "length %u is not accessible\n", __func__, 1442 io_req->user_bufs[i], io_req->lengths[i]); 1443 error = EFAULT; 1444 goto bailout; 1445 } 1446 1447 tmp_buf = malloc(lengths[i], M_SCSIPASS, 1448 M_WAITOK | M_ZERO); 1449 io_req->kern_bufs[i] = tmp_buf; 1450 *data_ptrs[i] = tmp_buf; 1451 1452#if 0 1453 xpt_print(periph->path, "%s: malloced %p len %u, user " 1454 "buffer %p, operation: %s\n", __func__, 1455 tmp_buf, lengths[i], io_req->user_bufs[i], 1456 (dirs[i] == CAM_DIR_IN) ? "read" : "write"); 1457#endif 1458 /* 1459 * We only need to copy in if the user is writing. 1460 */ 1461 if (dirs[i] != CAM_DIR_OUT) 1462 continue; 1463 1464 error = copyin(io_req->user_bufs[i], 1465 io_req->kern_bufs[i], lengths[i]); 1466 if (error != 0) { 1467 xpt_print(periph->path, "%s: copy of user " 1468 "buffer from %p to %p failed with " 1469 "error %d\n", __func__, 1470 io_req->user_bufs[i], 1471 io_req->kern_bufs[i], error); 1472 goto bailout; 1473 } 1474 } 1475 break; 1476 case CAM_DATA_PADDR: 1477 /* Pass down the pointer as-is */ 1478 break; 1479 case CAM_DATA_SG: { 1480 size_t sg_length, size_to_go, alloc_size; 1481 uint32_t num_segs_needed; 1482 1483 /* 1484 * Copy the user S/G list in, and then copy in the 1485 * individual segments. 1486 */ 1487 /* 1488 * We shouldn't see this, but check just in case. 1489 */ 1490 if (numbufs != 1) { 1491 xpt_print(periph->path, "%s: cannot currently handle " 1492 "more than one S/G list per CCB\n", __func__); 1493 error = EINVAL; 1494 goto bailout; 1495 } 1496 1497 /* 1498 * We have to have at least one segment. 1499 */ 1500 if (num_segs == 0) { 1501 xpt_print(periph->path, "%s: CAM_DATA_SG flag set, " 1502 "but sglist_cnt=0!\n", __func__); 1503 error = EINVAL; 1504 goto bailout; 1505 } 1506 1507 /* 1508 * Make sure the user specified the total length and didn't 1509 * just leave it to us to decode the S/G list. 1510 */ 1511 if (lengths[0] == 0) { 1512 xpt_print(periph->path, "%s: no dxfer_len specified, " 1513 "but CAM_DATA_SG flag is set!\n", __func__); 1514 error = EINVAL; 1515 goto bailout; 1516 } 1517 1518 /* 1519 * We allocate buffers in io_zone_size increments for an 1520 * S/G list. This will generally be MAXPHYS. 1521 */ 1522 if (lengths[0] <= softc->io_zone_size) 1523 num_segs_needed = 1; 1524 else { 1525 num_segs_needed = lengths[0] / softc->io_zone_size; 1526 if ((lengths[0] % softc->io_zone_size) != 0) 1527 num_segs_needed++; 1528 } 1529 1530 /* Figure out the size of the S/G list */ 1531 sg_length = num_segs * sizeof(bus_dma_segment_t); 1532 io_req->num_user_segs = num_segs; 1533 io_req->num_kern_segs = num_segs_needed; 1534 1535 /* Save the user's S/G list pointer for later restoration */ 1536 io_req->user_bufs[0] = *data_ptrs[0]; 1537 1538 /* 1539 * If we have enough segments allocated by default to handle 1540 * the length of the user's S/G list, 1541 */ 1542 if (num_segs > PASS_MAX_SEGS) { 1543 io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1544 num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1545 io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1546 } else 1547 io_req->user_segptr = io_req->user_segs; 1548 1549 if (!useracc(*data_ptrs[0], sg_length, VM_PROT_READ)) { 1550 xpt_print(periph->path, "%s: unable to access user " 1551 "S/G list at %p\n", __func__, *data_ptrs[0]); 1552 error = EFAULT; 1553 goto bailout; 1554 } 1555 1556 error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1557 if (error != 0) { 1558 xpt_print(periph->path, "%s: copy of user S/G list " 1559 "from %p to %p failed with error %d\n", 1560 __func__, *data_ptrs[0], io_req->user_segptr, 1561 error); 1562 goto bailout; 1563 } 1564 1565 if (num_segs_needed > PASS_MAX_SEGS) { 1566 io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) * 1567 num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO); 1568 io_req->flags |= PASS_IO_KERN_SEG_MALLOC; 1569 } else { 1570 io_req->kern_segptr = io_req->kern_segs; 1571 } 1572 1573 /* 1574 * Allocate the kernel S/G list. 1575 */ 1576 for (size_to_go = lengths[0], i = 0; 1577 size_to_go > 0 && i < num_segs_needed; 1578 i++, size_to_go -= alloc_size) { 1579 uint8_t *kern_ptr; 1580 1581 alloc_size = min(size_to_go, softc->io_zone_size); 1582 kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK); 1583 io_req->kern_segptr[i].ds_addr = 1584 (bus_addr_t)(uintptr_t)kern_ptr; 1585 io_req->kern_segptr[i].ds_len = alloc_size; 1586 } 1587 if (size_to_go > 0) { 1588 printf("%s: size_to_go = %zu, software error!\n", 1589 __func__, size_to_go); 1590 error = EINVAL; 1591 goto bailout; 1592 } 1593 1594 *data_ptrs[0] = (uint8_t *)io_req->kern_segptr; 1595 *seg_cnt_ptr = io_req->num_kern_segs; 1596 1597 /* 1598 * We only need to copy data here if the user is writing. 1599 */ 1600 if (dirs[0] == CAM_DIR_OUT) 1601 error = passcopysglist(periph, io_req, dirs[0]); 1602 break; 1603 } 1604 case CAM_DATA_SG_PADDR: { 1605 size_t sg_length; 1606 1607 /* 1608 * We shouldn't see this, but check just in case. 1609 */ 1610 if (numbufs != 1) { 1611 printf("%s: cannot currently handle more than one " 1612 "S/G list per CCB\n", __func__); 1613 error = EINVAL; 1614 goto bailout; 1615 } 1616 1617 /* 1618 * We have to have at least one segment. 1619 */ 1620 if (num_segs == 0) { 1621 xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag " 1622 "set, but sglist_cnt=0!\n", __func__); 1623 error = EINVAL; 1624 goto bailout; 1625 } 1626 1627 /* 1628 * Make sure the user specified the total length and didn't 1629 * just leave it to us to decode the S/G list. 1630 */ 1631 if (lengths[0] == 0) { 1632 xpt_print(periph->path, "%s: no dxfer_len specified, " 1633 "but CAM_DATA_SG flag is set!\n", __func__); 1634 error = EINVAL; 1635 goto bailout; 1636 } 1637 1638 /* Figure out the size of the S/G list */ 1639 sg_length = num_segs * sizeof(bus_dma_segment_t); 1640 io_req->num_user_segs = num_segs; 1641 io_req->num_kern_segs = io_req->num_user_segs; 1642 1643 /* Save the user's S/G list pointer for later restoration */ 1644 io_req->user_bufs[0] = *data_ptrs[0]; 1645 1646 if (num_segs > PASS_MAX_SEGS) { 1647 io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1648 num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1649 io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1650 } else 1651 io_req->user_segptr = io_req->user_segs; 1652 1653 io_req->kern_segptr = io_req->user_segptr; 1654 1655 error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1656 if (error != 0) { 1657 xpt_print(periph->path, "%s: copy of user S/G list " 1658 "from %p to %p failed with error %d\n", 1659 __func__, *data_ptrs[0], io_req->user_segptr, 1660 error); 1661 goto bailout; 1662 } 1663 break; 1664 } 1665 default: 1666 case CAM_DATA_BIO: 1667 /* 1668 * A user shouldn't be attaching a bio to the CCB. It 1669 * isn't a user-accessible structure. 1670 */ 1671 error = EINVAL; 1672 break; 1673 } 1674 1675bailout: 1676 if (error != 0) 1677 passiocleanup(softc, io_req); 1678 1679 return (error); 1680} 1681 1682static int 1683passmemdone(struct cam_periph *periph, struct pass_io_req *io_req) 1684{ 1685 struct pass_softc *softc; 1686 union ccb *ccb; 1687 int error; 1688 int i; 1689 1690 error = 0; 1691 softc = (struct pass_softc *)periph->softc; 1692 ccb = &io_req->ccb; 1693 1694 switch (io_req->data_flags) { 1695 case CAM_DATA_VADDR: 1696 /* 1697 * Copy back to the user buffer if this was a read. 1698 */ 1699 for (i = 0; i < io_req->num_bufs; i++) { 1700 if (io_req->dirs[i] != CAM_DIR_IN) 1701 continue; 1702 1703 error = copyout(io_req->kern_bufs[i], 1704 io_req->user_bufs[i], io_req->lengths[i]); 1705 if (error != 0) { 1706 xpt_print(periph->path, "Unable to copy %u " 1707 "bytes from %p to user address %p\n", 1708 io_req->lengths[i], 1709 io_req->kern_bufs[i], 1710 io_req->user_bufs[i]); 1711 goto bailout; 1712 } 1713 1714 } 1715 break; 1716 case CAM_DATA_PADDR: 1717 /* Do nothing. The pointer is a physical address already */ 1718 break; 1719 case CAM_DATA_SG: 1720 /* 1721 * Copy back to the user buffer if this was a read. 1722 * Restore the user's S/G list buffer pointer. 1723 */ 1724 if (io_req->dirs[0] == CAM_DIR_IN) 1725 error = passcopysglist(periph, io_req, io_req->dirs[0]); 1726 break; 1727 case CAM_DATA_SG_PADDR: 1728 /* 1729 * Restore the user's S/G list buffer pointer. No need to 1730 * copy. 1731 */ 1732 break; 1733 default: 1734 case CAM_DATA_BIO: 1735 error = EINVAL; 1736 break; 1737 } 1738 1739bailout: 1740 /* 1741 * Reset the user's pointers to their original values and free 1742 * allocated memory. 1743 */ 1744 passiocleanup(softc, io_req); 1745 1746 return (error); 1747} 1748 1749static int 1750passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1751{ 1752 int error; 1753 1754 if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) { 1755 error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl); 1756 } 1757 return (error); 1758} 1759 1760static int 1761passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1762{ 1763 struct cam_periph *periph; 1764 struct pass_softc *softc; 1765 int error; 1766 uint32_t priority; 1767 1768 periph = (struct cam_periph *)dev->si_drv1; 1769 cam_periph_lock(periph); 1770 softc = (struct pass_softc *)periph->softc; 1771 1772 error = 0; 1773 1774 switch (cmd) { 1775 1776 case CAMIOCOMMAND: 1777 { 1778 union ccb *inccb; 1779 union ccb *ccb; 1780 int ccb_malloced; 1781 1782 inccb = (union ccb *)addr; 1783 1784 /* 1785 * Some CCB types, like scan bus and scan lun can only go 1786 * through the transport layer device. 1787 */ 1788 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1789 xpt_print(periph->path, "CCB function code %#x is " 1790 "restricted to the XPT device\n", 1791 inccb->ccb_h.func_code); 1792 error = ENODEV; 1793 break; 1794 } 1795 1796 /* Compatibility for RL/priority-unaware code. */ 1797 priority = inccb->ccb_h.pinfo.priority; 1798 if (priority <= CAM_PRIORITY_OOB) 1799 priority += CAM_PRIORITY_OOB + 1; 1800 1801 /* 1802 * Non-immediate CCBs need a CCB from the per-device pool 1803 * of CCBs, which is scheduled by the transport layer. 1804 * Immediate CCBs and user-supplied CCBs should just be 1805 * malloced. 1806 */ 1807 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 1808 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 1809 ccb = cam_periph_getccb(periph, priority); 1810 ccb_malloced = 0; 1811 } else { 1812 ccb = xpt_alloc_ccb_nowait(); 1813 1814 if (ccb != NULL) 1815 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1816 priority); 1817 ccb_malloced = 1; 1818 } 1819 1820 if (ccb == NULL) { 1821 xpt_print(periph->path, "unable to allocate CCB\n"); 1822 error = ENOMEM; 1823 break; 1824 } 1825 1826 error = passsendccb(periph, ccb, inccb); 1827 1828 if (ccb_malloced) 1829 xpt_free_ccb(ccb); 1830 else 1831 xpt_release_ccb(ccb); 1832 1833 break; 1834 } 1835 case CAMIOQUEUE: 1836 { 1837 struct pass_io_req *io_req; 1838 union ccb **user_ccb, *ccb; 1839 xpt_opcode fc; 1840 1841 if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) { 1842 error = passcreatezone(periph); 1843 if (error != 0) 1844 goto bailout; 1845 } 1846 1847 /* 1848 * We're going to do a blocking allocation for this I/O 1849 * request, so we have to drop the lock. 1850 */ 1851 cam_periph_unlock(periph); 1852 1853 io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO); 1854 ccb = &io_req->ccb; 1855 user_ccb = (union ccb **)addr; 1856 1857 /* 1858 * Unlike the CAMIOCOMMAND ioctl above, we only have a 1859 * pointer to the user's CCB, so we have to copy the whole 1860 * thing in to a buffer we have allocated (above) instead 1861 * of allowing the ioctl code to malloc a buffer and copy 1862 * it in. 1863 * 1864 * This is an advantage for this asynchronous interface, 1865 * since we don't want the memory to get freed while the 1866 * CCB is outstanding. 1867 */ 1868#if 0 1869 xpt_print(periph->path, "Copying user CCB %p to " 1870 "kernel address %p\n", *user_ccb, ccb); 1871#endif 1872 error = copyin(*user_ccb, ccb, sizeof(*ccb)); 1873 if (error != 0) { 1874 xpt_print(periph->path, "Copy of user CCB %p to " 1875 "kernel address %p failed with error %d\n", 1876 *user_ccb, ccb, error); 1877 uma_zfree(softc->pass_zone, io_req); 1878 cam_periph_lock(periph); 1879 break; 1880 } 1881 1882 if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 1883 if (ccb->csio.cdb_len > IOCDBLEN) { 1884 error = EINVAL; 1885 break; 1886 } 1887 error = copyin(ccb->csio.cdb_io.cdb_ptr, 1888 ccb->csio.cdb_io.cdb_bytes, ccb->csio.cdb_len); 1889 if (error) 1890 break; 1891 ccb->ccb_h.flags &= ~CAM_CDB_POINTER; 1892 } 1893 1894 /* 1895 * Some CCB types, like scan bus and scan lun can only go 1896 * through the transport layer device. 1897 */ 1898 if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1899 xpt_print(periph->path, "CCB function code %#x is " 1900 "restricted to the XPT device\n", 1901 ccb->ccb_h.func_code); 1902 uma_zfree(softc->pass_zone, io_req); 1903 cam_periph_lock(periph); 1904 error = ENODEV; 1905 break; 1906 } 1907 1908 /* 1909 * Save the user's CCB pointer as well as his linked list 1910 * pointers and peripheral private area so that we can 1911 * restore these later. 1912 */ 1913 io_req->user_ccb_ptr = *user_ccb; 1914 io_req->user_periph_links = ccb->ccb_h.periph_links; 1915 io_req->user_periph_priv = ccb->ccb_h.periph_priv; 1916 1917 /* 1918 * Now that we've saved the user's values, we can set our 1919 * own peripheral private entry. 1920 */ 1921 ccb->ccb_h.ccb_ioreq = io_req; 1922 1923 /* Compatibility for RL/priority-unaware code. */ 1924 priority = ccb->ccb_h.pinfo.priority; 1925 if (priority <= CAM_PRIORITY_OOB) 1926 priority += CAM_PRIORITY_OOB + 1; 1927 1928 /* 1929 * Setup fields in the CCB like the path and the priority. 1930 * The path in particular cannot be done in userland, since 1931 * it is a pointer to a kernel data structure. 1932 */ 1933 xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority, 1934 ccb->ccb_h.flags); 1935 1936 /* 1937 * Setup our done routine. There is no way for the user to 1938 * have a valid pointer here. 1939 */ 1940 ccb->ccb_h.cbfcnp = passdone; 1941 1942 fc = ccb->ccb_h.func_code; 1943 /* 1944 * If this function code has memory that can be mapped in 1945 * or out, we need to call passmemsetup(). 1946 */ 1947 if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) 1948 || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH) 1949 || (fc == XPT_DEV_ADVINFO)) { 1950 error = passmemsetup(periph, io_req); 1951 if (error != 0) { 1952 uma_zfree(softc->pass_zone, io_req); 1953 cam_periph_lock(periph); 1954 break; 1955 } 1956 } else 1957 io_req->mapinfo.num_bufs_used = 0; 1958 1959 cam_periph_lock(periph); 1960 1961 /* 1962 * Everything goes on the incoming queue initially. 1963 */ 1964 TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links); 1965 1966 /* 1967 * If the CCB is queued, and is not a user CCB, then 1968 * we need to allocate a slot for it. Call xpt_schedule() 1969 * so that our start routine will get called when a CCB is 1970 * available. 1971 */ 1972 if ((fc & XPT_FC_QUEUED) 1973 && ((fc & XPT_FC_USER_CCB) == 0)) { 1974 xpt_schedule(periph, priority); 1975 break; 1976 } 1977 1978 /* 1979 * At this point, the CCB in question is either an 1980 * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB 1981 * and therefore should be malloced, not allocated via a slot. 1982 * Remove the CCB from the incoming queue and add it to the 1983 * active queue. 1984 */ 1985 TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 1986 TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 1987 1988 xpt_action(ccb); 1989 1990 /* 1991 * If this is not a queued CCB (i.e. it is an immediate CCB), 1992 * then it is already done. We need to put it on the done 1993 * queue for the user to fetch. 1994 */ 1995 if ((fc & XPT_FC_QUEUED) == 0) { 1996 TAILQ_REMOVE(&softc->active_queue, io_req, links); 1997 TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 1998 } 1999 break; 2000 } 2001 case CAMIOGET: 2002 { 2003 union ccb **user_ccb; 2004 struct pass_io_req *io_req; 2005 int old_error; 2006 2007 user_ccb = (union ccb **)addr; 2008 old_error = 0; 2009 2010 io_req = TAILQ_FIRST(&softc->done_queue); 2011 if (io_req == NULL) { 2012 error = ENOENT; 2013 break; 2014 } 2015 2016 /* 2017 * Remove the I/O from the done queue. 2018 */ 2019 TAILQ_REMOVE(&softc->done_queue, io_req, links); 2020 2021 /* 2022 * We have to drop the lock during the copyout because the 2023 * copyout can result in VM faults that require sleeping. 2024 */ 2025 cam_periph_unlock(periph); 2026 2027 /* 2028 * Do any needed copies (e.g. for reads) and revert the 2029 * pointers in the CCB back to the user's pointers. 2030 */ 2031 error = passmemdone(periph, io_req); 2032 2033 old_error = error; 2034 2035 io_req->ccb.ccb_h.periph_links = io_req->user_periph_links; 2036 io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv; 2037 2038#if 0 2039 xpt_print(periph->path, "Copying to user CCB %p from " 2040 "kernel address %p\n", *user_ccb, &io_req->ccb); 2041#endif 2042 2043 error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb)); 2044 if (error != 0) { 2045 xpt_print(periph->path, "Copy to user CCB %p from " 2046 "kernel address %p failed with error %d\n", 2047 *user_ccb, &io_req->ccb, error); 2048 } 2049 2050 /* 2051 * Prefer the first error we got back, and make sure we 2052 * don't overwrite bad status with good. 2053 */ 2054 if (old_error != 0) 2055 error = old_error; 2056 2057 cam_periph_lock(periph); 2058 2059 /* 2060 * At this point, if there was an error, we could potentially 2061 * re-queue the I/O and try again. But why? The error 2062 * would almost certainly happen again. We might as well 2063 * not leak memory. 2064 */ 2065 uma_zfree(softc->pass_zone, io_req); 2066 break; 2067 } 2068 default: 2069 error = cam_periph_ioctl(periph, cmd, addr, passerror); 2070 break; 2071 } 2072 2073bailout: 2074 cam_periph_unlock(periph); 2075 2076 return(error); 2077} 2078 2079static int 2080passpoll(struct cdev *dev, int poll_events, struct thread *td) 2081{ 2082 struct cam_periph *periph; 2083 struct pass_softc *softc; 2084 int revents; 2085 2086 periph = (struct cam_periph *)dev->si_drv1; 2087 softc = (struct pass_softc *)periph->softc; 2088 2089 revents = poll_events & (POLLOUT | POLLWRNORM); 2090 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) { 2091 cam_periph_lock(periph); 2092 2093 if (!TAILQ_EMPTY(&softc->done_queue)) { 2094 revents |= poll_events & (POLLIN | POLLRDNORM); 2095 } 2096 cam_periph_unlock(periph); 2097 if (revents == 0) 2098 selrecord(td, &softc->read_select); 2099 } 2100 2101 return (revents); 2102} 2103 2104static int 2105passkqfilter(struct cdev *dev, struct knote *kn) 2106{ 2107 struct cam_periph *periph; 2108 struct pass_softc *softc; 2109 2110 periph = (struct cam_periph *)dev->si_drv1; 2111 softc = (struct pass_softc *)periph->softc; 2112 2113 kn->kn_hook = (caddr_t)periph; 2114 kn->kn_fop = &passread_filtops; 2115 knlist_add(&softc->read_select.si_note, kn, 0); 2116 2117 return (0); 2118} 2119 2120static void 2121passreadfiltdetach(struct knote *kn) 2122{ 2123 struct cam_periph *periph; 2124 struct pass_softc *softc; 2125 2126 periph = (struct cam_periph *)kn->kn_hook; 2127 softc = (struct pass_softc *)periph->softc; 2128 2129 knlist_remove(&softc->read_select.si_note, kn, 0); 2130} 2131 2132static int 2133passreadfilt(struct knote *kn, long hint) 2134{ 2135 struct cam_periph *periph; 2136 struct pass_softc *softc; 2137 int retval; 2138 2139 periph = (struct cam_periph *)kn->kn_hook; 2140 softc = (struct pass_softc *)periph->softc; 2141 2142 cam_periph_assert(periph, MA_OWNED); 2143 2144 if (TAILQ_EMPTY(&softc->done_queue)) 2145 retval = 0; 2146 else 2147 retval = 1; 2148 2149 return (retval); 2150} 2151 2152/* 2153 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 2154 * should be the CCB that is copied in from the user. 2155 */ 2156static int 2157passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 2158{ 2159 struct pass_softc *softc; 2160 struct cam_periph_map_info mapinfo; 2161 uint8_t *cmd; 2162 xpt_opcode fc; 2163 int error; 2164 2165 softc = (struct pass_softc *)periph->softc; 2166 2167 /* 2168 * There are some fields in the CCB header that need to be 2169 * preserved, the rest we get from the user. 2170 */ 2171 xpt_merge_ccb(ccb, inccb); 2172 2173 if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 2174 cmd = __builtin_alloca(ccb->csio.cdb_len); 2175 error = copyin(ccb->csio.cdb_io.cdb_ptr, cmd, ccb->csio.cdb_len); 2176 if (error) 2177 return (error); 2178 ccb->csio.cdb_io.cdb_ptr = cmd; 2179 } 2180 2181 /* 2182 */ 2183 ccb->ccb_h.cbfcnp = passdone; 2184 2185 /* 2186 * Let cam_periph_mapmem do a sanity check on the data pointer format. 2187 * Even if no data transfer is needed, it's a cheap check and it 2188 * simplifies the code. 2189 */ 2190 fc = ccb->ccb_h.func_code; 2191 if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO) 2192 || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO)) { 2193 bzero(&mapinfo, sizeof(mapinfo)); 2194 2195 /* 2196 * cam_periph_mapmem calls into proc and vm functions that can 2197 * sleep as well as trigger I/O, so we can't hold the lock. 2198 * Dropping it here is reasonably safe. 2199 */ 2200 cam_periph_unlock(periph); 2201 error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio); 2202 cam_periph_lock(periph); 2203 2204 /* 2205 * cam_periph_mapmem returned an error, we can't continue. 2206 * Return the error to the user. 2207 */ 2208 if (error) 2209 return(error); 2210 } else 2211 /* Ensure that the unmap call later on is a no-op. */ 2212 mapinfo.num_bufs_used = 0; 2213 2214 /* 2215 * If the user wants us to perform any error recovery, then honor 2216 * that request. Otherwise, it's up to the user to perform any 2217 * error recovery. 2218 */ 2219 cam_periph_runccb(ccb, passerror, /* cam_flags */ CAM_RETRY_SELTO, 2220 /* sense_flags */ ((ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 2221 SF_RETRY_UA : SF_NO_RECOVERY) | SF_NO_PRINT, 2222 softc->device_stats); 2223 2224 cam_periph_unmapmem(ccb, &mapinfo); 2225 2226 ccb->ccb_h.cbfcnp = NULL; 2227 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 2228 bcopy(ccb, inccb, sizeof(union ccb)); 2229 2230 return(0); 2231} 2232 2233static int 2234passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 2235{ 2236 struct cam_periph *periph; 2237 struct pass_softc *softc; 2238 2239 periph = xpt_path_periph(ccb->ccb_h.path); 2240 softc = (struct pass_softc *)periph->softc; 2241 2242 return(cam_periph_error(ccb, cam_flags, sense_flags, 2243 &softc->saved_ccb)); 2244} 2245