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