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