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 522 softc = (struct pass_softc *)periph->softc; 523 /* 524 * Acquire a reference to the periph before we 525 * start the taskqueue, so that we don't run into 526 * a situation where the periph goes away before 527 * the task queue has a chance to run. 528 */ 529 if (cam_periph_acquire(periph) != 0) 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 543 static cam_status 544 passregister(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 xpt_path_inq(&cpi, periph->path); 590 591 if (cpi.maxio == 0) 592 softc->maxio = DFLTPHYS; /* traditional default */ 593 else if (cpi.maxio > MAXPHYS) 594 softc->maxio = MAXPHYS; /* for safety */ 595 else 596 softc->maxio = cpi.maxio; /* real value */ 597 598 if (cpi.hba_misc & PIM_UNMAPPED) 599 softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE; 600 601 /* 602 * We pass in 0 for a blocksize, since we don't 603 * know what the blocksize of this device is, if 604 * it even has a blocksize. 605 */ 606 cam_periph_unlock(periph); 607 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0; 608 softc->device_stats = devstat_new_entry("pass", 609 periph->unit_number, 0, 610 DEVSTAT_NO_BLOCKSIZE 611 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0), 612 softc->pd_type | 613 XPORT_DEVSTAT_TYPE(cpi.transport) | 614 DEVSTAT_TYPE_PASS, 615 DEVSTAT_PRIORITY_PASS); 616 617 /* 618 * Initialize the taskqueue handler for shutting down kqueue. 619 */ 620 TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0, 621 pass_shutdown_kqueue, periph); 622 623 /* 624 * Acquire a reference to the periph that we can release once we've 625 * cleaned up the kqueue. 626 */ 627 if (cam_periph_acquire(periph) != 0) { 628 xpt_print(periph->path, "%s: lost periph during " 629 "registration!\n", __func__); 630 cam_periph_lock(periph); 631 return (CAM_REQ_CMP_ERR); 632 } 633 634 /* 635 * Acquire a reference to the periph before we create the devfs 636 * instance for it. We'll release this reference once the devfs 637 * instance has been freed. 638 */ 639 if (cam_periph_acquire(periph) != 0) { 640 xpt_print(periph->path, "%s: lost periph during " 641 "registration!\n", __func__); 642 cam_periph_lock(periph); 643 return (CAM_REQ_CMP_ERR); 644 } 645 646 /* Register the device */ 647 make_dev_args_init(&args); 648 args.mda_devsw = &pass_cdevsw; 649 args.mda_unit = periph->unit_number; 650 args.mda_uid = UID_ROOT; 651 args.mda_gid = GID_OPERATOR; 652 args.mda_mode = 0600; 653 args.mda_si_drv1 = periph; 654 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name, 655 periph->unit_number); 656 if (error != 0) { 657 cam_periph_lock(periph); 658 cam_periph_release_locked(periph); 659 return (CAM_REQ_CMP_ERR); 660 } 661 662 /* 663 * Hold a reference to the periph before we create the physical 664 * path alias so it can't go away. 665 */ 666 if (cam_periph_acquire(periph) != 0) { 667 xpt_print(periph->path, "%s: lost periph during " 668 "registration!\n", __func__); 669 cam_periph_lock(periph); 670 return (CAM_REQ_CMP_ERR); 671 } 672 673 cam_periph_lock(periph); 674 675 TASK_INIT(&softc->add_physpath_task, /*priority*/0, 676 pass_add_physpath, periph); 677 678 /* 679 * See if physical path information is already available. 680 */ 681 taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task); 682 683 /* 684 * Add an async callback so that we get notified if 685 * this device goes away or its physical path 686 * (stored in the advanced info data of the EDT) has 687 * changed. 688 */ 689 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED, 690 passasync, periph, periph->path); 691 692 if (bootverbose) 693 xpt_announce_periph(periph, NULL); 694 695 return(CAM_REQ_CMP); 696 } 697 698 static int 699 passopen(struct cdev *dev, int flags, int fmt, struct thread *td) 700 { 701 struct cam_periph *periph; 702 struct pass_softc *softc; 703 int error; 704 705 periph = (struct cam_periph *)dev->si_drv1; 706 if (cam_periph_acquire(periph) != 0) 707 return (ENXIO); 708 709 cam_periph_lock(periph); 710 711 softc = (struct pass_softc *)periph->softc; 712 713 if (softc->flags & PASS_FLAG_INVALID) { 714 cam_periph_release_locked(periph); 715 cam_periph_unlock(periph); 716 return(ENXIO); 717 } 718 719 /* 720 * Don't allow access when we're running at a high securelevel. 721 */ 722 error = securelevel_gt(td->td_ucred, 1); 723 if (error) { 724 cam_periph_release_locked(periph); 725 cam_periph_unlock(periph); 726 return(error); 727 } 728 729 /* 730 * Only allow read-write access. 731 */ 732 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) { 733 cam_periph_release_locked(periph); 734 cam_periph_unlock(periph); 735 return(EPERM); 736 } 737 738 /* 739 * We don't allow nonblocking access. 740 */ 741 if ((flags & O_NONBLOCK) != 0) { 742 xpt_print(periph->path, "can't do nonblocking access\n"); 743 cam_periph_release_locked(periph); 744 cam_periph_unlock(periph); 745 return(EINVAL); 746 } 747 748 softc->open_count++; 749 750 cam_periph_unlock(periph); 751 752 return (error); 753 } 754 755 static int 756 passclose(struct cdev *dev, int flag, int fmt, struct thread *td) 757 { 758 struct cam_periph *periph; 759 struct pass_softc *softc; 760 struct mtx *mtx; 761 762 periph = (struct cam_periph *)dev->si_drv1; 763 mtx = cam_periph_mtx(periph); 764 mtx_lock(mtx); 765 766 softc = periph->softc; 767 softc->open_count--; 768 769 if (softc->open_count == 0) { 770 struct pass_io_req *io_req, *io_req2; 771 772 TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) { 773 TAILQ_REMOVE(&softc->done_queue, io_req, links); 774 passiocleanup(softc, io_req); 775 uma_zfree(softc->pass_zone, io_req); 776 } 777 778 TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, 779 io_req2) { 780 TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 781 passiocleanup(softc, io_req); 782 uma_zfree(softc->pass_zone, io_req); 783 } 784 785 /* 786 * If there are any active I/Os, we need to forcibly acquire a 787 * reference to the peripheral so that we don't go away 788 * before they complete. We'll release the reference when 789 * the abandoned queue is empty. 790 */ 791 io_req = TAILQ_FIRST(&softc->active_queue); 792 if ((io_req != NULL) 793 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) { 794 cam_periph_doacquire(periph); 795 softc->flags |= PASS_FLAG_ABANDONED_REF_SET; 796 } 797 798 /* 799 * Since the I/O in the active queue is not under our 800 * control, just set a flag so that we can clean it up when 801 * it completes and put it on the abandoned queue. This 802 * will prevent our sending spurious completions in the 803 * event that the device is opened again before these I/Os 804 * complete. 805 */ 806 TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, 807 io_req2) { 808 TAILQ_REMOVE(&softc->active_queue, io_req, links); 809 io_req->flags |= PASS_IO_ABANDONED; 810 TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, 811 links); 812 } 813 } 814 815 cam_periph_release_locked(periph); 816 817 /* 818 * We reference the lock directly here, instead of using 819 * cam_periph_unlock(). The reason is that the call to 820 * cam_periph_release_locked() above could result in the periph 821 * getting freed. If that is the case, dereferencing the periph 822 * with a cam_periph_unlock() call would cause a page fault. 823 * 824 * cam_periph_release() avoids this problem using the same method, 825 * but we're manually acquiring and dropping the lock here to 826 * protect the open count and avoid another lock acquisition and 827 * release. 828 */ 829 mtx_unlock(mtx); 830 831 return (0); 832 } 833 834 835 static void 836 passstart(struct cam_periph *periph, union ccb *start_ccb) 837 { 838 struct pass_softc *softc; 839 840 softc = (struct pass_softc *)periph->softc; 841 842 switch (softc->state) { 843 case PASS_STATE_NORMAL: { 844 struct pass_io_req *io_req; 845 846 /* 847 * Check for any queued I/O requests that require an 848 * allocated slot. 849 */ 850 io_req = TAILQ_FIRST(&softc->incoming_queue); 851 if (io_req == NULL) { 852 xpt_release_ccb(start_ccb); 853 break; 854 } 855 TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 856 TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 857 /* 858 * Merge the user's CCB into the allocated CCB. 859 */ 860 xpt_merge_ccb(start_ccb, &io_req->ccb); 861 start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO; 862 start_ccb->ccb_h.ccb_ioreq = io_req; 863 start_ccb->ccb_h.cbfcnp = passdone; 864 io_req->alloced_ccb = start_ccb; 865 binuptime(&io_req->start_time); 866 devstat_start_transaction(softc->device_stats, 867 &io_req->start_time); 868 869 xpt_action(start_ccb); 870 871 /* 872 * If we have any more I/O waiting, schedule ourselves again. 873 */ 874 if (!TAILQ_EMPTY(&softc->incoming_queue)) 875 xpt_schedule(periph, CAM_PRIORITY_NORMAL); 876 break; 877 } 878 default: 879 break; 880 } 881 } 882 883 static void 884 passdone(struct cam_periph *periph, union ccb *done_ccb) 885 { 886 struct pass_softc *softc; 887 struct ccb_scsiio *csio; 888 889 softc = (struct pass_softc *)periph->softc; 890 891 cam_periph_assert(periph, MA_OWNED); 892 893 csio = &done_ccb->csio; 894 switch (csio->ccb_h.ccb_type) { 895 case PASS_CCB_QUEUED_IO: { 896 struct pass_io_req *io_req; 897 898 io_req = done_ccb->ccb_h.ccb_ioreq; 899 #if 0 900 xpt_print(periph->path, "%s: called for user CCB %p\n", 901 __func__, io_req->user_ccb_ptr); 902 #endif 903 if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) 904 && (done_ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) 905 && ((io_req->flags & PASS_IO_ABANDONED) == 0)) { 906 int error; 907 908 error = passerror(done_ccb, CAM_RETRY_SELTO, 909 SF_RETRY_UA | SF_NO_PRINT); 910 911 if (error == ERESTART) { 912 /* 913 * A retry was scheduled, so 914 * just return. 915 */ 916 return; 917 } 918 } 919 920 /* 921 * Copy the allocated CCB contents back to the malloced CCB 922 * so we can give status back to the user when he requests it. 923 */ 924 bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb)); 925 926 /* 927 * Log data/transaction completion with devstat(9). 928 */ 929 switch (done_ccb->ccb_h.func_code) { 930 case XPT_SCSI_IO: 931 devstat_end_transaction(softc->device_stats, 932 done_ccb->csio.dxfer_len - done_ccb->csio.resid, 933 done_ccb->csio.tag_action & 0x3, 934 ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 935 CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 936 (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 937 DEVSTAT_WRITE : DEVSTAT_READ, NULL, 938 &io_req->start_time); 939 break; 940 case XPT_ATA_IO: 941 devstat_end_transaction(softc->device_stats, 942 done_ccb->ataio.dxfer_len - done_ccb->ataio.resid, 943 0, /* Not used in ATA */ 944 ((done_ccb->ccb_h.flags & CAM_DIR_MASK) == 945 CAM_DIR_NONE) ? DEVSTAT_NO_DATA : 946 (done_ccb->ccb_h.flags & CAM_DIR_OUT) ? 947 DEVSTAT_WRITE : DEVSTAT_READ, NULL, 948 &io_req->start_time); 949 break; 950 case XPT_SMP_IO: 951 /* 952 * XXX KDM this isn't quite right, but there isn't 953 * currently an easy way to represent a bidirectional 954 * transfer in devstat. The only way to do it 955 * and have the byte counts come out right would 956 * mean that we would have to record two 957 * transactions, one for the request and one for the 958 * response. For now, so that we report something, 959 * just treat the entire thing as a read. 960 */ 961 devstat_end_transaction(softc->device_stats, 962 done_ccb->smpio.smp_request_len + 963 done_ccb->smpio.smp_response_len, 964 DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL, 965 &io_req->start_time); 966 break; 967 default: 968 devstat_end_transaction(softc->device_stats, 0, 969 DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL, 970 &io_req->start_time); 971 break; 972 } 973 974 /* 975 * In the normal case, take the completed I/O off of the 976 * active queue and put it on the done queue. Notitfy the 977 * user that we have a completed I/O. 978 */ 979 if ((io_req->flags & PASS_IO_ABANDONED) == 0) { 980 TAILQ_REMOVE(&softc->active_queue, io_req, links); 981 TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 982 selwakeuppri(&softc->read_select, PRIBIO); 983 KNOTE_LOCKED(&softc->read_select.si_note, 0); 984 } else { 985 /* 986 * In the case of an abandoned I/O (final close 987 * without fetching the I/O), take it off of the 988 * abandoned queue and free it. 989 */ 990 TAILQ_REMOVE(&softc->abandoned_queue, io_req, links); 991 passiocleanup(softc, io_req); 992 uma_zfree(softc->pass_zone, io_req); 993 994 /* 995 * Release the done_ccb here, since we may wind up 996 * freeing the peripheral when we decrement the 997 * reference count below. 998 */ 999 xpt_release_ccb(done_ccb); 1000 1001 /* 1002 * If the abandoned queue is empty, we can release 1003 * our reference to the periph since we won't have 1004 * any more completions coming. 1005 */ 1006 if ((TAILQ_EMPTY(&softc->abandoned_queue)) 1007 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) { 1008 softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET; 1009 cam_periph_release_locked(periph); 1010 } 1011 1012 /* 1013 * We have already released the CCB, so we can 1014 * return. 1015 */ 1016 return; 1017 } 1018 break; 1019 } 1020 } 1021 xpt_release_ccb(done_ccb); 1022 } 1023 1024 static int 1025 passcreatezone(struct cam_periph *periph) 1026 { 1027 struct pass_softc *softc; 1028 int error; 1029 1030 error = 0; 1031 softc = (struct pass_softc *)periph->softc; 1032 1033 cam_periph_assert(periph, MA_OWNED); 1034 KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0), 1035 ("%s called when the pass(4) zone is valid!\n", __func__)); 1036 KASSERT((softc->pass_zone == NULL), 1037 ("%s called when the pass(4) zone is allocated!\n", __func__)); 1038 1039 if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) { 1040 1041 /* 1042 * We're the first context through, so we need to create 1043 * the pass(4) UMA zone for I/O requests. 1044 */ 1045 softc->flags |= PASS_FLAG_ZONE_INPROG; 1046 1047 /* 1048 * uma_zcreate() does a blocking (M_WAITOK) allocation, 1049 * so we cannot hold a mutex while we call it. 1050 */ 1051 cam_periph_unlock(periph); 1052 1053 softc->pass_zone = uma_zcreate(softc->zone_name, 1054 sizeof(struct pass_io_req), NULL, NULL, NULL, NULL, 1055 /*align*/ 0, /*flags*/ 0); 1056 1057 softc->pass_io_zone = uma_zcreate(softc->io_zone_name, 1058 softc->io_zone_size, NULL, NULL, NULL, NULL, 1059 /*align*/ 0, /*flags*/ 0); 1060 1061 cam_periph_lock(periph); 1062 1063 if ((softc->pass_zone == NULL) 1064 || (softc->pass_io_zone == NULL)) { 1065 if (softc->pass_zone == NULL) 1066 xpt_print(periph->path, "unable to allocate " 1067 "IO Req UMA zone\n"); 1068 else 1069 xpt_print(periph->path, "unable to allocate " 1070 "IO UMA zone\n"); 1071 softc->flags &= ~PASS_FLAG_ZONE_INPROG; 1072 goto bailout; 1073 } 1074 1075 /* 1076 * Set the flags appropriately and notify any other waiters. 1077 */ 1078 softc->flags &= PASS_FLAG_ZONE_INPROG; 1079 softc->flags |= PASS_FLAG_ZONE_VALID; 1080 wakeup(&softc->pass_zone); 1081 } else { 1082 /* 1083 * In this case, the UMA zone has not yet been created, but 1084 * another context is in the process of creating it. We 1085 * need to sleep until the creation is either done or has 1086 * failed. 1087 */ 1088 while ((softc->flags & PASS_FLAG_ZONE_INPROG) 1089 && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) { 1090 error = msleep(&softc->pass_zone, 1091 cam_periph_mtx(periph), PRIBIO, 1092 "paszon", 0); 1093 if (error != 0) 1094 goto bailout; 1095 } 1096 /* 1097 * If the zone creation failed, no luck for the user. 1098 */ 1099 if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){ 1100 error = ENOMEM; 1101 goto bailout; 1102 } 1103 } 1104 bailout: 1105 return (error); 1106 } 1107 1108 static void 1109 passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req) 1110 { 1111 union ccb *ccb; 1112 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1113 int i, numbufs; 1114 1115 ccb = &io_req->ccb; 1116 1117 switch (ccb->ccb_h.func_code) { 1118 case XPT_DEV_MATCH: 1119 numbufs = min(io_req->num_bufs, 2); 1120 1121 if (numbufs == 1) { 1122 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1123 } else { 1124 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1125 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1126 } 1127 break; 1128 case XPT_SCSI_IO: 1129 case XPT_CONT_TARGET_IO: 1130 data_ptrs[0] = &ccb->csio.data_ptr; 1131 numbufs = min(io_req->num_bufs, 1); 1132 break; 1133 case XPT_ATA_IO: 1134 data_ptrs[0] = &ccb->ataio.data_ptr; 1135 numbufs = min(io_req->num_bufs, 1); 1136 break; 1137 case XPT_SMP_IO: 1138 numbufs = min(io_req->num_bufs, 2); 1139 data_ptrs[0] = &ccb->smpio.smp_request; 1140 data_ptrs[1] = &ccb->smpio.smp_response; 1141 break; 1142 case XPT_DEV_ADVINFO: 1143 numbufs = min(io_req->num_bufs, 1); 1144 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1145 break; 1146 case XPT_NVME_IO: 1147 case XPT_NVME_ADMIN: 1148 data_ptrs[0] = &ccb->nvmeio.data_ptr; 1149 numbufs = min(io_req->num_bufs, 1); 1150 break; 1151 default: 1152 /* allow ourselves to be swapped once again */ 1153 return; 1154 break; /* NOTREACHED */ 1155 } 1156 1157 if (io_req->flags & PASS_IO_USER_SEG_MALLOC) { 1158 free(io_req->user_segptr, M_SCSIPASS); 1159 io_req->user_segptr = NULL; 1160 } 1161 1162 /* 1163 * We only want to free memory we malloced. 1164 */ 1165 if (io_req->data_flags == CAM_DATA_VADDR) { 1166 for (i = 0; i < io_req->num_bufs; i++) { 1167 if (io_req->kern_bufs[i] == NULL) 1168 continue; 1169 1170 free(io_req->kern_bufs[i], M_SCSIPASS); 1171 io_req->kern_bufs[i] = NULL; 1172 } 1173 } else if (io_req->data_flags == CAM_DATA_SG) { 1174 for (i = 0; i < io_req->num_kern_segs; i++) { 1175 if ((uint8_t *)(uintptr_t) 1176 io_req->kern_segptr[i].ds_addr == NULL) 1177 continue; 1178 1179 uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t) 1180 io_req->kern_segptr[i].ds_addr); 1181 io_req->kern_segptr[i].ds_addr = 0; 1182 } 1183 } 1184 1185 if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) { 1186 free(io_req->kern_segptr, M_SCSIPASS); 1187 io_req->kern_segptr = NULL; 1188 } 1189 1190 if (io_req->data_flags != CAM_DATA_PADDR) { 1191 for (i = 0; i < numbufs; i++) { 1192 /* 1193 * Restore the user's buffer pointers to their 1194 * previous values. 1195 */ 1196 if (io_req->user_bufs[i] != NULL) 1197 *data_ptrs[i] = io_req->user_bufs[i]; 1198 } 1199 } 1200 1201 } 1202 1203 static int 1204 passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req, 1205 ccb_flags direction) 1206 { 1207 bus_size_t kern_watermark, user_watermark, len_copied, len_to_copy; 1208 bus_dma_segment_t *user_sglist, *kern_sglist; 1209 int i, j, error; 1210 1211 error = 0; 1212 kern_watermark = 0; 1213 user_watermark = 0; 1214 len_to_copy = 0; 1215 len_copied = 0; 1216 user_sglist = io_req->user_segptr; 1217 kern_sglist = io_req->kern_segptr; 1218 1219 for (i = 0, j = 0; i < io_req->num_user_segs && 1220 j < io_req->num_kern_segs;) { 1221 uint8_t *user_ptr, *kern_ptr; 1222 1223 len_to_copy = min(user_sglist[i].ds_len -user_watermark, 1224 kern_sglist[j].ds_len - kern_watermark); 1225 1226 user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr; 1227 user_ptr = user_ptr + user_watermark; 1228 kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr; 1229 kern_ptr = kern_ptr + kern_watermark; 1230 1231 user_watermark += len_to_copy; 1232 kern_watermark += len_to_copy; 1233 1234 if (!useracc(user_ptr, len_to_copy, 1235 (direction == CAM_DIR_IN) ? VM_PROT_WRITE : VM_PROT_READ)) { 1236 xpt_print(periph->path, "%s: unable to access user " 1237 "S/G list element %p len %zu\n", __func__, 1238 user_ptr, len_to_copy); 1239 error = EFAULT; 1240 goto bailout; 1241 } 1242 1243 if (direction == CAM_DIR_IN) { 1244 error = copyout(kern_ptr, user_ptr, len_to_copy); 1245 if (error != 0) { 1246 xpt_print(periph->path, "%s: copyout of %u " 1247 "bytes from %p to %p failed with " 1248 "error %d\n", __func__, len_to_copy, 1249 kern_ptr, user_ptr, error); 1250 goto bailout; 1251 } 1252 } else { 1253 error = copyin(user_ptr, kern_ptr, len_to_copy); 1254 if (error != 0) { 1255 xpt_print(periph->path, "%s: copyin of %u " 1256 "bytes from %p to %p failed with " 1257 "error %d\n", __func__, len_to_copy, 1258 user_ptr, kern_ptr, error); 1259 goto bailout; 1260 } 1261 } 1262 1263 len_copied += len_to_copy; 1264 1265 if (user_sglist[i].ds_len == user_watermark) { 1266 i++; 1267 user_watermark = 0; 1268 } 1269 1270 if (kern_sglist[j].ds_len == kern_watermark) { 1271 j++; 1272 kern_watermark = 0; 1273 } 1274 } 1275 1276 bailout: 1277 1278 return (error); 1279 } 1280 1281 static int 1282 passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req) 1283 { 1284 union ccb *ccb; 1285 struct pass_softc *softc; 1286 int numbufs, i; 1287 uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS]; 1288 uint32_t lengths[CAM_PERIPH_MAXMAPS]; 1289 uint32_t dirs[CAM_PERIPH_MAXMAPS]; 1290 uint32_t num_segs; 1291 uint16_t *seg_cnt_ptr; 1292 size_t maxmap; 1293 int error; 1294 1295 cam_periph_assert(periph, MA_NOTOWNED); 1296 1297 softc = periph->softc; 1298 1299 error = 0; 1300 ccb = &io_req->ccb; 1301 maxmap = 0; 1302 num_segs = 0; 1303 seg_cnt_ptr = NULL; 1304 1305 switch(ccb->ccb_h.func_code) { 1306 case XPT_DEV_MATCH: 1307 if (ccb->cdm.match_buf_len == 0) { 1308 printf("%s: invalid match buffer length 0\n", __func__); 1309 return(EINVAL); 1310 } 1311 if (ccb->cdm.pattern_buf_len > 0) { 1312 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns; 1313 lengths[0] = ccb->cdm.pattern_buf_len; 1314 dirs[0] = CAM_DIR_OUT; 1315 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches; 1316 lengths[1] = ccb->cdm.match_buf_len; 1317 dirs[1] = CAM_DIR_IN; 1318 numbufs = 2; 1319 } else { 1320 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches; 1321 lengths[0] = ccb->cdm.match_buf_len; 1322 dirs[0] = CAM_DIR_IN; 1323 numbufs = 1; 1324 } 1325 io_req->data_flags = CAM_DATA_VADDR; 1326 break; 1327 case XPT_SCSI_IO: 1328 case XPT_CONT_TARGET_IO: 1329 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1330 return(0); 1331 1332 /* 1333 * The user shouldn't be able to supply a bio. 1334 */ 1335 if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO) 1336 return (EINVAL); 1337 1338 io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK; 1339 1340 data_ptrs[0] = &ccb->csio.data_ptr; 1341 lengths[0] = ccb->csio.dxfer_len; 1342 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1343 num_segs = ccb->csio.sglist_cnt; 1344 seg_cnt_ptr = &ccb->csio.sglist_cnt; 1345 numbufs = 1; 1346 maxmap = softc->maxio; 1347 break; 1348 case XPT_ATA_IO: 1349 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1350 return(0); 1351 1352 /* 1353 * We only support a single virtual address for ATA I/O. 1354 */ 1355 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR) 1356 return (EINVAL); 1357 1358 io_req->data_flags = CAM_DATA_VADDR; 1359 1360 data_ptrs[0] = &ccb->ataio.data_ptr; 1361 lengths[0] = ccb->ataio.dxfer_len; 1362 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1363 numbufs = 1; 1364 maxmap = softc->maxio; 1365 break; 1366 case XPT_SMP_IO: 1367 io_req->data_flags = CAM_DATA_VADDR; 1368 1369 data_ptrs[0] = &ccb->smpio.smp_request; 1370 lengths[0] = ccb->smpio.smp_request_len; 1371 dirs[0] = CAM_DIR_OUT; 1372 data_ptrs[1] = &ccb->smpio.smp_response; 1373 lengths[1] = ccb->smpio.smp_response_len; 1374 dirs[1] = CAM_DIR_IN; 1375 numbufs = 2; 1376 maxmap = softc->maxio; 1377 break; 1378 case XPT_DEV_ADVINFO: 1379 if (ccb->cdai.bufsiz == 0) 1380 return (0); 1381 1382 io_req->data_flags = CAM_DATA_VADDR; 1383 1384 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf; 1385 lengths[0] = ccb->cdai.bufsiz; 1386 dirs[0] = CAM_DIR_IN; 1387 numbufs = 1; 1388 break; 1389 case XPT_NVME_ADMIN: 1390 case XPT_NVME_IO: 1391 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE) 1392 return (0); 1393 1394 io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK; 1395 1396 data_ptrs[0] = &ccb->nvmeio.data_ptr; 1397 lengths[0] = ccb->nvmeio.dxfer_len; 1398 dirs[0] = ccb->ccb_h.flags & CAM_DIR_MASK; 1399 num_segs = ccb->nvmeio.sglist_cnt; 1400 seg_cnt_ptr = &ccb->nvmeio.sglist_cnt; 1401 numbufs = 1; 1402 maxmap = softc->maxio; 1403 break; 1404 default: 1405 return(EINVAL); 1406 break; /* NOTREACHED */ 1407 } 1408 1409 io_req->num_bufs = numbufs; 1410 1411 /* 1412 * If there is a maximum, check to make sure that the user's 1413 * request fits within the limit. In general, we should only have 1414 * a maximum length for requests that go to hardware. Otherwise it 1415 * is whatever we're able to malloc. 1416 */ 1417 for (i = 0; i < numbufs; i++) { 1418 io_req->user_bufs[i] = *data_ptrs[i]; 1419 io_req->dirs[i] = dirs[i]; 1420 io_req->lengths[i] = lengths[i]; 1421 1422 if (maxmap == 0) 1423 continue; 1424 1425 if (lengths[i] <= maxmap) 1426 continue; 1427 1428 xpt_print(periph->path, "%s: data length %u > max allowed %u " 1429 "bytes\n", __func__, lengths[i], maxmap); 1430 error = EINVAL; 1431 goto bailout; 1432 } 1433 1434 switch (io_req->data_flags) { 1435 case CAM_DATA_VADDR: 1436 /* Map or copy the buffer into kernel address space */ 1437 for (i = 0; i < numbufs; i++) { 1438 uint8_t *tmp_buf; 1439 1440 /* 1441 * If for some reason no length is specified, we 1442 * don't need to allocate anything. 1443 */ 1444 if (io_req->lengths[i] == 0) 1445 continue; 1446 1447 /* 1448 * Make sure that the user's buffer is accessible 1449 * to that process. 1450 */ 1451 if (!useracc(io_req->user_bufs[i], io_req->lengths[i], 1452 (io_req->dirs[i] == CAM_DIR_IN) ? VM_PROT_WRITE : 1453 VM_PROT_READ)) { 1454 xpt_print(periph->path, "%s: user address %p " 1455 "length %u is not accessible\n", __func__, 1456 io_req->user_bufs[i], io_req->lengths[i]); 1457 error = EFAULT; 1458 goto bailout; 1459 } 1460 1461 tmp_buf = malloc(lengths[i], M_SCSIPASS, 1462 M_WAITOK | M_ZERO); 1463 io_req->kern_bufs[i] = tmp_buf; 1464 *data_ptrs[i] = tmp_buf; 1465 1466 #if 0 1467 xpt_print(periph->path, "%s: malloced %p len %u, user " 1468 "buffer %p, operation: %s\n", __func__, 1469 tmp_buf, lengths[i], io_req->user_bufs[i], 1470 (dirs[i] == CAM_DIR_IN) ? "read" : "write"); 1471 #endif 1472 /* 1473 * We only need to copy in if the user is writing. 1474 */ 1475 if (dirs[i] != CAM_DIR_OUT) 1476 continue; 1477 1478 error = copyin(io_req->user_bufs[i], 1479 io_req->kern_bufs[i], lengths[i]); 1480 if (error != 0) { 1481 xpt_print(periph->path, "%s: copy of user " 1482 "buffer from %p to %p failed with " 1483 "error %d\n", __func__, 1484 io_req->user_bufs[i], 1485 io_req->kern_bufs[i], error); 1486 goto bailout; 1487 } 1488 } 1489 break; 1490 case CAM_DATA_PADDR: 1491 /* Pass down the pointer as-is */ 1492 break; 1493 case CAM_DATA_SG: { 1494 size_t sg_length, size_to_go, alloc_size; 1495 uint32_t num_segs_needed; 1496 1497 /* 1498 * Copy the user S/G list in, and then copy in the 1499 * individual segments. 1500 */ 1501 /* 1502 * We shouldn't see this, but check just in case. 1503 */ 1504 if (numbufs != 1) { 1505 xpt_print(periph->path, "%s: cannot currently handle " 1506 "more than one S/G list per CCB\n", __func__); 1507 error = EINVAL; 1508 goto bailout; 1509 } 1510 1511 /* 1512 * We have to have at least one segment. 1513 */ 1514 if (num_segs == 0) { 1515 xpt_print(periph->path, "%s: CAM_DATA_SG flag set, " 1516 "but sglist_cnt=0!\n", __func__); 1517 error = EINVAL; 1518 goto bailout; 1519 } 1520 1521 /* 1522 * Make sure the user specified the total length and didn't 1523 * just leave it to us to decode the S/G list. 1524 */ 1525 if (lengths[0] == 0) { 1526 xpt_print(periph->path, "%s: no dxfer_len specified, " 1527 "but CAM_DATA_SG flag is set!\n", __func__); 1528 error = EINVAL; 1529 goto bailout; 1530 } 1531 1532 /* 1533 * We allocate buffers in io_zone_size increments for an 1534 * S/G list. This will generally be MAXPHYS. 1535 */ 1536 if (lengths[0] <= softc->io_zone_size) 1537 num_segs_needed = 1; 1538 else { 1539 num_segs_needed = lengths[0] / softc->io_zone_size; 1540 if ((lengths[0] % softc->io_zone_size) != 0) 1541 num_segs_needed++; 1542 } 1543 1544 /* Figure out the size of the S/G list */ 1545 sg_length = num_segs * sizeof(bus_dma_segment_t); 1546 io_req->num_user_segs = num_segs; 1547 io_req->num_kern_segs = num_segs_needed; 1548 1549 /* Save the user's S/G list pointer for later restoration */ 1550 io_req->user_bufs[0] = *data_ptrs[0]; 1551 1552 /* 1553 * If we have enough segments allocated by default to handle 1554 * the length of the user's S/G list, 1555 */ 1556 if (num_segs > PASS_MAX_SEGS) { 1557 io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1558 num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1559 io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1560 } else 1561 io_req->user_segptr = io_req->user_segs; 1562 1563 if (!useracc(*data_ptrs[0], sg_length, VM_PROT_READ)) { 1564 xpt_print(periph->path, "%s: unable to access user " 1565 "S/G list at %p\n", __func__, *data_ptrs[0]); 1566 error = EFAULT; 1567 goto bailout; 1568 } 1569 1570 error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1571 if (error != 0) { 1572 xpt_print(periph->path, "%s: copy of user S/G list " 1573 "from %p to %p failed with error %d\n", 1574 __func__, *data_ptrs[0], io_req->user_segptr, 1575 error); 1576 goto bailout; 1577 } 1578 1579 if (num_segs_needed > PASS_MAX_SEGS) { 1580 io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) * 1581 num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO); 1582 io_req->flags |= PASS_IO_KERN_SEG_MALLOC; 1583 } else { 1584 io_req->kern_segptr = io_req->kern_segs; 1585 } 1586 1587 /* 1588 * Allocate the kernel S/G list. 1589 */ 1590 for (size_to_go = lengths[0], i = 0; 1591 size_to_go > 0 && i < num_segs_needed; 1592 i++, size_to_go -= alloc_size) { 1593 uint8_t *kern_ptr; 1594 1595 alloc_size = min(size_to_go, softc->io_zone_size); 1596 kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK); 1597 io_req->kern_segptr[i].ds_addr = 1598 (bus_addr_t)(uintptr_t)kern_ptr; 1599 io_req->kern_segptr[i].ds_len = alloc_size; 1600 } 1601 if (size_to_go > 0) { 1602 printf("%s: size_to_go = %zu, software error!\n", 1603 __func__, size_to_go); 1604 error = EINVAL; 1605 goto bailout; 1606 } 1607 1608 *data_ptrs[0] = (uint8_t *)io_req->kern_segptr; 1609 *seg_cnt_ptr = io_req->num_kern_segs; 1610 1611 /* 1612 * We only need to copy data here if the user is writing. 1613 */ 1614 if (dirs[0] == CAM_DIR_OUT) 1615 error = passcopysglist(periph, io_req, dirs[0]); 1616 break; 1617 } 1618 case CAM_DATA_SG_PADDR: { 1619 size_t sg_length; 1620 1621 /* 1622 * We shouldn't see this, but check just in case. 1623 */ 1624 if (numbufs != 1) { 1625 printf("%s: cannot currently handle more than one " 1626 "S/G list per CCB\n", __func__); 1627 error = EINVAL; 1628 goto bailout; 1629 } 1630 1631 /* 1632 * We have to have at least one segment. 1633 */ 1634 if (num_segs == 0) { 1635 xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag " 1636 "set, but sglist_cnt=0!\n", __func__); 1637 error = EINVAL; 1638 goto bailout; 1639 } 1640 1641 /* 1642 * Make sure the user specified the total length and didn't 1643 * just leave it to us to decode the S/G list. 1644 */ 1645 if (lengths[0] == 0) { 1646 xpt_print(periph->path, "%s: no dxfer_len specified, " 1647 "but CAM_DATA_SG flag is set!\n", __func__); 1648 error = EINVAL; 1649 goto bailout; 1650 } 1651 1652 /* Figure out the size of the S/G list */ 1653 sg_length = num_segs * sizeof(bus_dma_segment_t); 1654 io_req->num_user_segs = num_segs; 1655 io_req->num_kern_segs = io_req->num_user_segs; 1656 1657 /* Save the user's S/G list pointer for later restoration */ 1658 io_req->user_bufs[0] = *data_ptrs[0]; 1659 1660 if (num_segs > PASS_MAX_SEGS) { 1661 io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) * 1662 num_segs, M_SCSIPASS, M_WAITOK | M_ZERO); 1663 io_req->flags |= PASS_IO_USER_SEG_MALLOC; 1664 } else 1665 io_req->user_segptr = io_req->user_segs; 1666 1667 io_req->kern_segptr = io_req->user_segptr; 1668 1669 error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length); 1670 if (error != 0) { 1671 xpt_print(periph->path, "%s: copy of user S/G list " 1672 "from %p to %p failed with error %d\n", 1673 __func__, *data_ptrs[0], io_req->user_segptr, 1674 error); 1675 goto bailout; 1676 } 1677 break; 1678 } 1679 default: 1680 case CAM_DATA_BIO: 1681 /* 1682 * A user shouldn't be attaching a bio to the CCB. It 1683 * isn't a user-accessible structure. 1684 */ 1685 error = EINVAL; 1686 break; 1687 } 1688 1689 bailout: 1690 if (error != 0) 1691 passiocleanup(softc, io_req); 1692 1693 return (error); 1694 } 1695 1696 static int 1697 passmemdone(struct cam_periph *periph, struct pass_io_req *io_req) 1698 { 1699 struct pass_softc *softc; 1700 int error; 1701 int i; 1702 1703 error = 0; 1704 softc = (struct pass_softc *)periph->softc; 1705 1706 switch (io_req->data_flags) { 1707 case CAM_DATA_VADDR: 1708 /* 1709 * Copy back to the user buffer if this was a read. 1710 */ 1711 for (i = 0; i < io_req->num_bufs; i++) { 1712 if (io_req->dirs[i] != CAM_DIR_IN) 1713 continue; 1714 1715 error = copyout(io_req->kern_bufs[i], 1716 io_req->user_bufs[i], io_req->lengths[i]); 1717 if (error != 0) { 1718 xpt_print(periph->path, "Unable to copy %u " 1719 "bytes from %p to user address %p\n", 1720 io_req->lengths[i], 1721 io_req->kern_bufs[i], 1722 io_req->user_bufs[i]); 1723 goto bailout; 1724 } 1725 1726 } 1727 break; 1728 case CAM_DATA_PADDR: 1729 /* Do nothing. The pointer is a physical address already */ 1730 break; 1731 case CAM_DATA_SG: 1732 /* 1733 * Copy back to the user buffer if this was a read. 1734 * Restore the user's S/G list buffer pointer. 1735 */ 1736 if (io_req->dirs[0] == CAM_DIR_IN) 1737 error = passcopysglist(periph, io_req, io_req->dirs[0]); 1738 break; 1739 case CAM_DATA_SG_PADDR: 1740 /* 1741 * Restore the user's S/G list buffer pointer. No need to 1742 * copy. 1743 */ 1744 break; 1745 default: 1746 case CAM_DATA_BIO: 1747 error = EINVAL; 1748 break; 1749 } 1750 1751 bailout: 1752 /* 1753 * Reset the user's pointers to their original values and free 1754 * allocated memory. 1755 */ 1756 passiocleanup(softc, io_req); 1757 1758 return (error); 1759 } 1760 1761 static int 1762 passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1763 { 1764 int error; 1765 1766 if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) { 1767 error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl); 1768 } 1769 return (error); 1770 } 1771 1772 static int 1773 passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 1774 { 1775 struct cam_periph *periph; 1776 struct pass_softc *softc; 1777 int error; 1778 uint32_t priority; 1779 1780 periph = (struct cam_periph *)dev->si_drv1; 1781 cam_periph_lock(periph); 1782 softc = (struct pass_softc *)periph->softc; 1783 1784 error = 0; 1785 1786 switch (cmd) { 1787 1788 case CAMIOCOMMAND: 1789 { 1790 union ccb *inccb; 1791 union ccb *ccb; 1792 int ccb_malloced; 1793 1794 inccb = (union ccb *)addr; 1795 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 1796 if (inccb->ccb_h.func_code == XPT_SCSI_IO) 1797 inccb->csio.bio = NULL; 1798 #endif 1799 1800 if (inccb->ccb_h.flags & CAM_UNLOCKED) { 1801 error = EINVAL; 1802 break; 1803 } 1804 1805 /* 1806 * Some CCB types, like scan bus and scan lun can only go 1807 * through the transport layer device. 1808 */ 1809 if (inccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1810 xpt_print(periph->path, "CCB function code %#x is " 1811 "restricted to the XPT device\n", 1812 inccb->ccb_h.func_code); 1813 error = ENODEV; 1814 break; 1815 } 1816 1817 /* Compatibility for RL/priority-unaware code. */ 1818 priority = inccb->ccb_h.pinfo.priority; 1819 if (priority <= CAM_PRIORITY_OOB) 1820 priority += CAM_PRIORITY_OOB + 1; 1821 1822 /* 1823 * Non-immediate CCBs need a CCB from the per-device pool 1824 * of CCBs, which is scheduled by the transport layer. 1825 * Immediate CCBs and user-supplied CCBs should just be 1826 * malloced. 1827 */ 1828 if ((inccb->ccb_h.func_code & XPT_FC_QUEUED) 1829 && ((inccb->ccb_h.func_code & XPT_FC_USER_CCB) == 0)) { 1830 ccb = cam_periph_getccb(periph, priority); 1831 ccb_malloced = 0; 1832 } else { 1833 ccb = xpt_alloc_ccb_nowait(); 1834 1835 if (ccb != NULL) 1836 xpt_setup_ccb(&ccb->ccb_h, periph->path, 1837 priority); 1838 ccb_malloced = 1; 1839 } 1840 1841 if (ccb == NULL) { 1842 xpt_print(periph->path, "unable to allocate CCB\n"); 1843 error = ENOMEM; 1844 break; 1845 } 1846 1847 error = passsendccb(periph, ccb, inccb); 1848 1849 if (ccb_malloced) 1850 xpt_free_ccb(ccb); 1851 else 1852 xpt_release_ccb(ccb); 1853 1854 break; 1855 } 1856 case CAMIOQUEUE: 1857 { 1858 struct pass_io_req *io_req; 1859 union ccb **user_ccb, *ccb; 1860 xpt_opcode fc; 1861 1862 if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) { 1863 error = passcreatezone(periph); 1864 if (error != 0) 1865 goto bailout; 1866 } 1867 1868 /* 1869 * We're going to do a blocking allocation for this I/O 1870 * request, so we have to drop the lock. 1871 */ 1872 cam_periph_unlock(periph); 1873 1874 io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO); 1875 ccb = &io_req->ccb; 1876 user_ccb = (union ccb **)addr; 1877 1878 /* 1879 * Unlike the CAMIOCOMMAND ioctl above, we only have a 1880 * pointer to the user's CCB, so we have to copy the whole 1881 * thing in to a buffer we have allocated (above) instead 1882 * of allowing the ioctl code to malloc a buffer and copy 1883 * it in. 1884 * 1885 * This is an advantage for this asynchronous interface, 1886 * since we don't want the memory to get freed while the 1887 * CCB is outstanding. 1888 */ 1889 #if 0 1890 xpt_print(periph->path, "Copying user CCB %p to " 1891 "kernel address %p\n", *user_ccb, ccb); 1892 #endif 1893 error = copyin(*user_ccb, ccb, sizeof(*ccb)); 1894 if (error != 0) { 1895 xpt_print(periph->path, "Copy of user CCB %p to " 1896 "kernel address %p failed with error %d\n", 1897 *user_ccb, ccb, error); 1898 goto camioqueue_error; 1899 } 1900 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING) 1901 if (ccb->ccb_h.func_code == XPT_SCSI_IO) 1902 ccb->csio.bio = NULL; 1903 #endif 1904 1905 if (ccb->ccb_h.flags & CAM_UNLOCKED) { 1906 error = EINVAL; 1907 goto camioqueue_error; 1908 } 1909 1910 if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 1911 if (ccb->csio.cdb_len > IOCDBLEN) { 1912 error = EINVAL; 1913 goto camioqueue_error; 1914 } 1915 error = copyin(ccb->csio.cdb_io.cdb_ptr, 1916 ccb->csio.cdb_io.cdb_bytes, ccb->csio.cdb_len); 1917 if (error != 0) 1918 goto camioqueue_error; 1919 ccb->ccb_h.flags &= ~CAM_CDB_POINTER; 1920 } 1921 1922 /* 1923 * Some CCB types, like scan bus and scan lun can only go 1924 * through the transport layer device. 1925 */ 1926 if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) { 1927 xpt_print(periph->path, "CCB function code %#x is " 1928 "restricted to the XPT device\n", 1929 ccb->ccb_h.func_code); 1930 error = ENODEV; 1931 goto camioqueue_error; 1932 } 1933 1934 /* 1935 * Save the user's CCB pointer as well as his linked list 1936 * pointers and peripheral private area so that we can 1937 * restore these later. 1938 */ 1939 io_req->user_ccb_ptr = *user_ccb; 1940 io_req->user_periph_links = ccb->ccb_h.periph_links; 1941 io_req->user_periph_priv = ccb->ccb_h.periph_priv; 1942 1943 /* 1944 * Now that we've saved the user's values, we can set our 1945 * own peripheral private entry. 1946 */ 1947 ccb->ccb_h.ccb_ioreq = io_req; 1948 1949 /* Compatibility for RL/priority-unaware code. */ 1950 priority = ccb->ccb_h.pinfo.priority; 1951 if (priority <= CAM_PRIORITY_OOB) 1952 priority += CAM_PRIORITY_OOB + 1; 1953 1954 /* 1955 * Setup fields in the CCB like the path and the priority. 1956 * The path in particular cannot be done in userland, since 1957 * it is a pointer to a kernel data structure. 1958 */ 1959 xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority, 1960 ccb->ccb_h.flags); 1961 1962 /* 1963 * Setup our done routine. There is no way for the user to 1964 * have a valid pointer here. 1965 */ 1966 ccb->ccb_h.cbfcnp = passdone; 1967 1968 fc = ccb->ccb_h.func_code; 1969 /* 1970 * If this function code has memory that can be mapped in 1971 * or out, we need to call passmemsetup(). 1972 */ 1973 if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) 1974 || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH) 1975 || (fc == XPT_DEV_ADVINFO) 1976 || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) { 1977 error = passmemsetup(periph, io_req); 1978 if (error != 0) 1979 goto camioqueue_error; 1980 } else 1981 io_req->mapinfo.num_bufs_used = 0; 1982 1983 cam_periph_lock(periph); 1984 1985 /* 1986 * Everything goes on the incoming queue initially. 1987 */ 1988 TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links); 1989 1990 /* 1991 * If the CCB is queued, and is not a user CCB, then 1992 * we need to allocate a slot for it. Call xpt_schedule() 1993 * so that our start routine will get called when a CCB is 1994 * available. 1995 */ 1996 if ((fc & XPT_FC_QUEUED) 1997 && ((fc & XPT_FC_USER_CCB) == 0)) { 1998 xpt_schedule(periph, priority); 1999 break; 2000 } 2001 2002 /* 2003 * At this point, the CCB in question is either an 2004 * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB 2005 * and therefore should be malloced, not allocated via a slot. 2006 * Remove the CCB from the incoming queue and add it to the 2007 * active queue. 2008 */ 2009 TAILQ_REMOVE(&softc->incoming_queue, io_req, links); 2010 TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links); 2011 2012 xpt_action(ccb); 2013 2014 /* 2015 * If this is not a queued CCB (i.e. it is an immediate CCB), 2016 * then it is already done. We need to put it on the done 2017 * queue for the user to fetch. 2018 */ 2019 if ((fc & XPT_FC_QUEUED) == 0) { 2020 TAILQ_REMOVE(&softc->active_queue, io_req, links); 2021 TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links); 2022 } 2023 break; 2024 2025 camioqueue_error: 2026 uma_zfree(softc->pass_zone, io_req); 2027 cam_periph_lock(periph); 2028 break; 2029 } 2030 case CAMIOGET: 2031 { 2032 union ccb **user_ccb; 2033 struct pass_io_req *io_req; 2034 int old_error; 2035 2036 user_ccb = (union ccb **)addr; 2037 old_error = 0; 2038 2039 io_req = TAILQ_FIRST(&softc->done_queue); 2040 if (io_req == NULL) { 2041 error = ENOENT; 2042 break; 2043 } 2044 2045 /* 2046 * Remove the I/O from the done queue. 2047 */ 2048 TAILQ_REMOVE(&softc->done_queue, io_req, links); 2049 2050 /* 2051 * We have to drop the lock during the copyout because the 2052 * copyout can result in VM faults that require sleeping. 2053 */ 2054 cam_periph_unlock(periph); 2055 2056 /* 2057 * Do any needed copies (e.g. for reads) and revert the 2058 * pointers in the CCB back to the user's pointers. 2059 */ 2060 error = passmemdone(periph, io_req); 2061 2062 old_error = error; 2063 2064 io_req->ccb.ccb_h.periph_links = io_req->user_periph_links; 2065 io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv; 2066 2067 #if 0 2068 xpt_print(periph->path, "Copying to user CCB %p from " 2069 "kernel address %p\n", *user_ccb, &io_req->ccb); 2070 #endif 2071 2072 error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb)); 2073 if (error != 0) { 2074 xpt_print(periph->path, "Copy to user CCB %p from " 2075 "kernel address %p failed with error %d\n", 2076 *user_ccb, &io_req->ccb, error); 2077 } 2078 2079 /* 2080 * Prefer the first error we got back, and make sure we 2081 * don't overwrite bad status with good. 2082 */ 2083 if (old_error != 0) 2084 error = old_error; 2085 2086 cam_periph_lock(periph); 2087 2088 /* 2089 * At this point, if there was an error, we could potentially 2090 * re-queue the I/O and try again. But why? The error 2091 * would almost certainly happen again. We might as well 2092 * not leak memory. 2093 */ 2094 uma_zfree(softc->pass_zone, io_req); 2095 break; 2096 } 2097 default: 2098 error = cam_periph_ioctl(periph, cmd, addr, passerror); 2099 break; 2100 } 2101 2102 bailout: 2103 cam_periph_unlock(periph); 2104 2105 return(error); 2106 } 2107 2108 static int 2109 passpoll(struct cdev *dev, int poll_events, struct thread *td) 2110 { 2111 struct cam_periph *periph; 2112 struct pass_softc *softc; 2113 int revents; 2114 2115 periph = (struct cam_periph *)dev->si_drv1; 2116 softc = (struct pass_softc *)periph->softc; 2117 2118 revents = poll_events & (POLLOUT | POLLWRNORM); 2119 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) { 2120 cam_periph_lock(periph); 2121 2122 if (!TAILQ_EMPTY(&softc->done_queue)) { 2123 revents |= poll_events & (POLLIN | POLLRDNORM); 2124 } 2125 cam_periph_unlock(periph); 2126 if (revents == 0) 2127 selrecord(td, &softc->read_select); 2128 } 2129 2130 return (revents); 2131 } 2132 2133 static int 2134 passkqfilter(struct cdev *dev, struct knote *kn) 2135 { 2136 struct cam_periph *periph; 2137 struct pass_softc *softc; 2138 2139 periph = (struct cam_periph *)dev->si_drv1; 2140 softc = (struct pass_softc *)periph->softc; 2141 2142 kn->kn_hook = (caddr_t)periph; 2143 kn->kn_fop = &passread_filtops; 2144 knlist_add(&softc->read_select.si_note, kn, 0); 2145 2146 return (0); 2147 } 2148 2149 static void 2150 passreadfiltdetach(struct knote *kn) 2151 { 2152 struct cam_periph *periph; 2153 struct pass_softc *softc; 2154 2155 periph = (struct cam_periph *)kn->kn_hook; 2156 softc = (struct pass_softc *)periph->softc; 2157 2158 knlist_remove(&softc->read_select.si_note, kn, 0); 2159 } 2160 2161 static int 2162 passreadfilt(struct knote *kn, long hint) 2163 { 2164 struct cam_periph *periph; 2165 struct pass_softc *softc; 2166 int retval; 2167 2168 periph = (struct cam_periph *)kn->kn_hook; 2169 softc = (struct pass_softc *)periph->softc; 2170 2171 cam_periph_assert(periph, MA_OWNED); 2172 2173 if (TAILQ_EMPTY(&softc->done_queue)) 2174 retval = 0; 2175 else 2176 retval = 1; 2177 2178 return (retval); 2179 } 2180 2181 /* 2182 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb" 2183 * should be the CCB that is copied in from the user. 2184 */ 2185 static int 2186 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb) 2187 { 2188 struct pass_softc *softc; 2189 struct cam_periph_map_info mapinfo; 2190 uint8_t *cmd; 2191 xpt_opcode fc; 2192 int error; 2193 2194 softc = (struct pass_softc *)periph->softc; 2195 2196 /* 2197 * There are some fields in the CCB header that need to be 2198 * preserved, the rest we get from the user. 2199 */ 2200 xpt_merge_ccb(ccb, inccb); 2201 2202 if (ccb->ccb_h.flags & CAM_CDB_POINTER) { 2203 cmd = __builtin_alloca(ccb->csio.cdb_len); 2204 error = copyin(ccb->csio.cdb_io.cdb_ptr, cmd, ccb->csio.cdb_len); 2205 if (error) 2206 return (error); 2207 ccb->csio.cdb_io.cdb_ptr = cmd; 2208 } 2209 2210 /* 2211 */ 2212 ccb->ccb_h.cbfcnp = passdone; 2213 2214 /* 2215 * Let cam_periph_mapmem do a sanity check on the data pointer format. 2216 * Even if no data transfer is needed, it's a cheap check and it 2217 * simplifies the code. 2218 */ 2219 fc = ccb->ccb_h.func_code; 2220 if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO) 2221 || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO) || (fc == XPT_MMC_IO) 2222 || (fc == XPT_NVME_ADMIN) || (fc == XPT_NVME_IO)) { 2223 2224 bzero(&mapinfo, sizeof(mapinfo)); 2225 2226 /* 2227 * cam_periph_mapmem calls into proc and vm functions that can 2228 * sleep as well as trigger I/O, so we can't hold the lock. 2229 * Dropping it here is reasonably safe. 2230 */ 2231 cam_periph_unlock(periph); 2232 error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio); 2233 cam_periph_lock(periph); 2234 2235 /* 2236 * cam_periph_mapmem returned an error, we can't continue. 2237 * Return the error to the user. 2238 */ 2239 if (error) 2240 return(error); 2241 } else 2242 /* Ensure that the unmap call later on is a no-op. */ 2243 mapinfo.num_bufs_used = 0; 2244 2245 /* 2246 * If the user wants us to perform any error recovery, then honor 2247 * that request. Otherwise, it's up to the user to perform any 2248 * error recovery. 2249 */ 2250 cam_periph_runccb(ccb, (ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ? 2251 passerror : NULL, /* cam_flags */ CAM_RETRY_SELTO, 2252 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT, 2253 softc->device_stats); 2254 2255 cam_periph_unmapmem(ccb, &mapinfo); 2256 2257 ccb->ccb_h.cbfcnp = NULL; 2258 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv; 2259 bcopy(ccb, inccb, sizeof(union ccb)); 2260 2261 return(0); 2262 } 2263 2264 static int 2265 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 2266 { 2267 struct cam_periph *periph; 2268 struct pass_softc *softc; 2269 2270 periph = xpt_path_periph(ccb->ccb_h.path); 2271 softc = (struct pass_softc *)periph->softc; 2272 2273 return(cam_periph_error(ccb, cam_flags, sense_flags)); 2274 } 2275