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