2 * Copyright (c) 1997, 1998, 2000 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
35 #include <sys/types.h>
38 #include <sys/devicestat.h>
39 #include <sys/errno.h>
40 #include <sys/fcntl.h>
41 #include <sys/malloc.h>
44 #include <sys/selinfo.h>
46 #include <sys/taskqueue.h>
49 #include <vm/vm_extern.h>
51 #include <machine/bus.h>
54 #include <cam/cam_ccb.h>
55 #include <cam/cam_periph.h>
56 #include <cam/cam_queue.h>
57 #include <cam/cam_xpt.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_debug.h>
60 #include <cam/cam_compat.h>
61 #include <cam/cam_xpt_periph.h>
63 #include <cam/scsi/scsi_all.h>
64 #include <cam/scsi/scsi_pass.h>
67 PASS_FLAG_OPEN = 0x01,
68 PASS_FLAG_LOCKED = 0x02,
69 PASS_FLAG_INVALID = 0x04,
70 PASS_FLAG_INITIAL_PHYSPATH = 0x08,
71 PASS_FLAG_ZONE_INPROG = 0x10,
72 PASS_FLAG_ZONE_VALID = 0x20,
73 PASS_FLAG_UNMAPPED_CAPABLE = 0x40,
74 PASS_FLAG_ABANDONED_REF_SET = 0x80
86 #define ccb_type ppriv_field0
87 #define ccb_ioreq ppriv_ptr1
90 * The maximum number of memory segments we preallocate.
92 #define PASS_MAX_SEGS 16
96 PASS_IO_USER_SEG_MALLOC = 0x01,
97 PASS_IO_KERN_SEG_MALLOC = 0x02,
98 PASS_IO_ABANDONED = 0x04
103 union ccb *alloced_ccb;
104 union ccb *user_ccb_ptr;
105 camq_entry user_periph_links;
106 ccb_ppriv_area user_periph_priv;
107 struct cam_periph_map_info mapinfo;
109 ccb_flags data_flags;
111 bus_dma_segment_t user_segs[PASS_MAX_SEGS];
113 bus_dma_segment_t kern_segs[PASS_MAX_SEGS];
114 bus_dma_segment_t *user_segptr;
115 bus_dma_segment_t *kern_segptr;
117 uint32_t dirs[CAM_PERIPH_MAXMAPS];
118 uint32_t lengths[CAM_PERIPH_MAXMAPS];
119 uint8_t *user_bufs[CAM_PERIPH_MAXMAPS];
120 uint8_t *kern_bufs[CAM_PERIPH_MAXMAPS];
121 struct bintime start_time;
122 TAILQ_ENTRY(pass_io_req) links;
132 struct devstat *device_stats;
134 struct cdev *alias_dev;
135 struct task add_physpath_task;
136 struct task shutdown_kqueue_task;
137 struct selinfo read_select;
138 TAILQ_HEAD(, pass_io_req) incoming_queue;
139 TAILQ_HEAD(, pass_io_req) active_queue;
140 TAILQ_HEAD(, pass_io_req) abandoned_queue;
141 TAILQ_HEAD(, pass_io_req) done_queue;
142 struct cam_periph *periph;
144 char io_zone_name[12];
145 uma_zone_t pass_zone;
146 uma_zone_t pass_io_zone;
150 static d_open_t passopen;
151 static d_close_t passclose;
152 static d_ioctl_t passioctl;
153 static d_ioctl_t passdoioctl;
154 static d_poll_t passpoll;
155 static d_kqfilter_t passkqfilter;
156 static void passreadfiltdetach(struct knote *kn);
157 static int passreadfilt(struct knote *kn, long hint);
159 static periph_init_t passinit;
160 static periph_ctor_t passregister;
161 static periph_oninv_t passoninvalidate;
162 static periph_dtor_t passcleanup;
163 static periph_start_t passstart;
164 static void pass_shutdown_kqueue(void *context, int pending);
165 static void pass_add_physpath(void *context, int pending);
166 static void passasync(void *callback_arg, u_int32_t code,
167 struct cam_path *path, void *arg);
168 static void passdone(struct cam_periph *periph,
169 union ccb *done_ccb);
170 static int passcreatezone(struct cam_periph *periph);
171 static void passiocleanup(struct pass_softc *softc,
172 struct pass_io_req *io_req);
173 static int passcopysglist(struct cam_periph *periph,
174 struct pass_io_req *io_req,
175 ccb_flags direction);
176 static int passmemsetup(struct cam_periph *periph,
177 struct pass_io_req *io_req);
178 static int passmemdone(struct cam_periph *periph,
179 struct pass_io_req *io_req);
180 static int passerror(union ccb *ccb, u_int32_t cam_flags,
181 u_int32_t sense_flags);
182 static int passsendccb(struct cam_periph *periph, union ccb *ccb,
185 static struct periph_driver passdriver =
188 TAILQ_HEAD_INITIALIZER(passdriver.units), /* generation */ 0
191 PERIPHDRIVER_DECLARE(pass, passdriver);
193 static struct cdevsw pass_cdevsw = {
194 .d_version = D_VERSION,
195 .d_flags = D_TRACKCLOSE,
197 .d_close = passclose,
198 .d_ioctl = passioctl,
200 .d_kqfilter = passkqfilter,
204 static struct filterops passread_filtops = {
206 .f_detach = passreadfiltdetach,
207 .f_event = passreadfilt
210 static MALLOC_DEFINE(M_SCSIPASS, "scsi_pass", "scsi passthrough buffers");
218 * Install a global async callback. This callback will
219 * receive async callbacks like "new device found".
221 status = xpt_register_async(AC_FOUND_DEVICE, passasync, NULL, NULL);
223 if (status != CAM_REQ_CMP) {
224 printf("pass: Failed to attach master async callback "
225 "due to status 0x%x!\n", status);
231 passrejectios(struct cam_periph *periph)
233 struct pass_io_req *io_req, *io_req2;
234 struct pass_softc *softc;
236 softc = (struct pass_softc *)periph->softc;
239 * The user can no longer get status for I/O on the done queue, so
240 * clean up all outstanding I/O on the done queue.
242 TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
243 TAILQ_REMOVE(&softc->done_queue, io_req, links);
244 passiocleanup(softc, io_req);
245 uma_zfree(softc->pass_zone, io_req);
249 * The underlying device is gone, so we can't issue these I/Os.
250 * The devfs node has been shut down, so we can't return status to
251 * the user. Free any I/O left on the incoming queue.
253 TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links, io_req2) {
254 TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
255 passiocleanup(softc, io_req);
256 uma_zfree(softc->pass_zone, io_req);
260 * Normally we would put I/Os on the abandoned queue and acquire a
261 * reference when we saw the final close. But, the device went
262 * away and devfs may have moved everything off to deadfs by the
263 * time the I/O done callback is called; as a result, we won't see
264 * any more closes. So, if we have any active I/Os, we need to put
265 * them on the abandoned queue. When the abandoned queue is empty,
266 * we'll release the remaining reference (see below) to the peripheral.
268 TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links, io_req2) {
269 TAILQ_REMOVE(&softc->active_queue, io_req, links);
270 io_req->flags |= PASS_IO_ABANDONED;
271 TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req, links);
275 * If we put any I/O on the abandoned queue, acquire a reference.
277 if ((!TAILQ_EMPTY(&softc->abandoned_queue))
278 && ((softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0)) {
279 cam_periph_doacquire(periph);
280 softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
285 passdevgonecb(void *arg)
287 struct cam_periph *periph;
289 struct pass_softc *softc;
292 periph = (struct cam_periph *)arg;
293 mtx = cam_periph_mtx(periph);
296 softc = (struct pass_softc *)periph->softc;
297 KASSERT(softc->open_count >= 0, ("Negative open count %d",
301 * When we get this callback, we will get no more close calls from
302 * devfs. So if we have any dangling opens, we need to release the
303 * reference held for that particular context.
305 for (i = 0; i < softc->open_count; i++)
306 cam_periph_release_locked(periph);
308 softc->open_count = 0;
311 * Release the reference held for the device node, it is gone now.
312 * Accordingly, inform all queued I/Os of their fate.
314 cam_periph_release_locked(periph);
315 passrejectios(periph);
318 * We reference the SIM lock directly here, instead of using
319 * cam_periph_unlock(). The reason is that the final call to
320 * cam_periph_release_locked() above could result in the periph
321 * getting freed. If that is the case, dereferencing the periph
322 * with a cam_periph_unlock() call would cause a page fault.
327 * We have to remove our kqueue context from a thread because it
328 * may sleep. It would be nice if we could get a callback from
329 * kqueue when it is done cleaning up resources.
331 taskqueue_enqueue(taskqueue_thread, &softc->shutdown_kqueue_task);
335 passoninvalidate(struct cam_periph *periph)
337 struct pass_softc *softc;
339 softc = (struct pass_softc *)periph->softc;
342 * De-register any async callbacks.
344 xpt_register_async(0, passasync, periph, periph->path);
346 softc->flags |= PASS_FLAG_INVALID;
349 * Tell devfs this device has gone away, and ask for a callback
350 * when it has cleaned up its state.
352 destroy_dev_sched_cb(softc->dev, passdevgonecb, periph);
356 passcleanup(struct cam_periph *periph)
358 struct pass_softc *softc;
360 softc = (struct pass_softc *)periph->softc;
362 cam_periph_assert(periph, MA_OWNED);
363 KASSERT(TAILQ_EMPTY(&softc->active_queue),
364 ("%s called when there are commands on the active queue!\n",
366 KASSERT(TAILQ_EMPTY(&softc->abandoned_queue),
367 ("%s called when there are commands on the abandoned queue!\n",
369 KASSERT(TAILQ_EMPTY(&softc->incoming_queue),
370 ("%s called when there are commands on the incoming queue!\n",
372 KASSERT(TAILQ_EMPTY(&softc->done_queue),
373 ("%s called when there are commands on the done queue!\n",
376 devstat_remove_entry(softc->device_stats);
378 cam_periph_unlock(periph);
381 * We call taskqueue_drain() for the physpath task to make sure it
382 * is complete. We drop the lock because this can potentially
383 * sleep. XXX KDM that is bad. Need a way to get a callback when
384 * a taskqueue is drained.
386 * Note that we don't drain the kqueue shutdown task queue. This
387 * is because we hold a reference on the periph for kqueue, and
388 * release that reference from the kqueue shutdown task queue. So
389 * we cannot come into this routine unless we've released that
390 * reference. Also, because that could be the last reference, we
391 * could be called from the cam_periph_release() call in
392 * pass_shutdown_kqueue(). In that case, the taskqueue_drain()
393 * would deadlock. It would be preferable if we had a way to
394 * get a callback when a taskqueue is done.
396 taskqueue_drain(taskqueue_thread, &softc->add_physpath_task);
398 cam_periph_lock(periph);
400 free(softc, M_DEVBUF);
404 pass_shutdown_kqueue(void *context, int pending)
406 struct cam_periph *periph;
407 struct pass_softc *softc;
410 softc = periph->softc;
412 knlist_clear(&softc->read_select.si_note, /*is_locked*/ 0);
413 knlist_destroy(&softc->read_select.si_note);
416 * Release the reference we held for kqueue.
418 cam_periph_release(periph);
422 pass_add_physpath(void *context, int pending)
424 struct cam_periph *periph;
425 struct pass_softc *softc;
430 * If we have one, create a devfs alias for our
434 softc = periph->softc;
435 physpath = malloc(MAXPATHLEN, M_DEVBUF, M_WAITOK);
436 mtx = cam_periph_mtx(periph);
439 if (periph->flags & CAM_PERIPH_INVALID)
442 if (xpt_getattr(physpath, MAXPATHLEN,
443 "GEOM::physpath", periph->path) == 0
444 && strlen(physpath) != 0) {
447 make_dev_physpath_alias(MAKEDEV_WAITOK, &softc->alias_dev,
448 softc->dev, softc->alias_dev, physpath);
454 * Now that we've made our alias, we no longer have to have a
455 * reference to the device.
457 if ((softc->flags & PASS_FLAG_INITIAL_PHYSPATH) == 0)
458 softc->flags |= PASS_FLAG_INITIAL_PHYSPATH;
461 * We always acquire a reference to the periph before queueing this
462 * task queue function, so it won't go away before we run.
464 while (pending-- > 0)
465 cam_periph_release_locked(periph);
468 free(physpath, M_DEVBUF);
472 passasync(void *callback_arg, u_int32_t code,
473 struct cam_path *path, void *arg)
475 struct cam_periph *periph;
477 periph = (struct cam_periph *)callback_arg;
480 case AC_FOUND_DEVICE:
482 struct ccb_getdev *cgd;
485 cgd = (struct ccb_getdev *)arg;
490 * Allocate a peripheral instance for
491 * this device and start the probe
494 status = cam_periph_alloc(passregister, passoninvalidate,
495 passcleanup, passstart, "pass",
496 CAM_PERIPH_BIO, path,
497 passasync, AC_FOUND_DEVICE, cgd);
499 if (status != CAM_REQ_CMP
500 && status != CAM_REQ_INPROG) {
501 const struct cam_status_entry *entry;
503 entry = cam_fetch_status_entry(status);
505 printf("passasync: Unable to attach new device "
506 "due to status %#x: %s\n", status, entry ?
507 entry->status_text : "Unknown");
512 case AC_ADVINFO_CHANGED:
516 buftype = (uintptr_t)arg;
517 if (buftype == CDAI_TYPE_PHYS_PATH) {
518 struct pass_softc *softc;
521 softc = (struct pass_softc *)periph->softc;
523 * Acquire a reference to the periph before we
524 * start the taskqueue, so that we don't run into
525 * a situation where the periph goes away before
526 * the task queue has a chance to run.
528 status = cam_periph_acquire(periph);
529 if (status != CAM_REQ_CMP)
532 taskqueue_enqueue(taskqueue_thread,
533 &softc->add_physpath_task);
538 cam_periph_async(periph, code, path, arg);
544 passregister(struct cam_periph *periph, void *arg)
546 struct pass_softc *softc;
547 struct ccb_getdev *cgd;
548 struct ccb_pathinq cpi;
549 struct make_dev_args args;
552 cgd = (struct ccb_getdev *)arg;
554 printf("%s: no getdev CCB, can't register device\n", __func__);
555 return(CAM_REQ_CMP_ERR);
558 softc = (struct pass_softc *)malloc(sizeof(*softc),
562 printf("%s: Unable to probe new device. "
563 "Unable to allocate softc\n", __func__);
564 return(CAM_REQ_CMP_ERR);
567 bzero(softc, sizeof(*softc));
568 softc->state = PASS_STATE_NORMAL;
569 if (cgd->protocol == PROTO_SCSI || cgd->protocol == PROTO_ATAPI)
570 softc->pd_type = SID_TYPE(&cgd->inq_data);
571 else if (cgd->protocol == PROTO_SATAPM)
572 softc->pd_type = T_ENCLOSURE;
574 softc->pd_type = T_DIRECT;
576 periph->softc = softc;
577 softc->periph = periph;
578 TAILQ_INIT(&softc->incoming_queue);
579 TAILQ_INIT(&softc->active_queue);
580 TAILQ_INIT(&softc->abandoned_queue);
581 TAILQ_INIT(&softc->done_queue);
582 snprintf(softc->zone_name, sizeof(softc->zone_name), "%s%d",
583 periph->periph_name, periph->unit_number);
584 snprintf(softc->io_zone_name, sizeof(softc->io_zone_name), "%s%dIO",
585 periph->periph_name, periph->unit_number);
586 softc->io_zone_size = MAXPHYS;
587 knlist_init_mtx(&softc->read_select.si_note, cam_periph_mtx(periph));
589 bzero(&cpi, sizeof(cpi));
590 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
591 cpi.ccb_h.func_code = XPT_PATH_INQ;
592 xpt_action((union ccb *)&cpi);
595 softc->maxio = DFLTPHYS; /* traditional default */
596 else if (cpi.maxio > MAXPHYS)
597 softc->maxio = MAXPHYS; /* for safety */
599 softc->maxio = cpi.maxio; /* real value */
601 if (cpi.hba_misc & PIM_UNMAPPED)
602 softc->flags |= PASS_FLAG_UNMAPPED_CAPABLE;
605 * We pass in 0 for a blocksize, since we don't
606 * know what the blocksize of this device is, if
607 * it even has a blocksize.
609 cam_periph_unlock(periph);
610 no_tags = (cgd->inq_data.flags & SID_CmdQue) == 0;
611 softc->device_stats = devstat_new_entry("pass",
612 periph->unit_number, 0,
614 | (no_tags ? DEVSTAT_NO_ORDERED_TAGS : 0),
616 XPORT_DEVSTAT_TYPE(cpi.transport) |
618 DEVSTAT_PRIORITY_PASS);
621 * Initialize the taskqueue handler for shutting down kqueue.
623 TASK_INIT(&softc->shutdown_kqueue_task, /*priority*/ 0,
624 pass_shutdown_kqueue, periph);
627 * Acquire a reference to the periph that we can release once we've
628 * cleaned up the kqueue.
630 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
631 xpt_print(periph->path, "%s: lost periph during "
632 "registration!\n", __func__);
633 cam_periph_lock(periph);
634 return (CAM_REQ_CMP_ERR);
638 * Acquire a reference to the periph before we create the devfs
639 * instance for it. We'll release this reference once the devfs
640 * instance has been freed.
642 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
643 xpt_print(periph->path, "%s: lost periph during "
644 "registration!\n", __func__);
645 cam_periph_lock(periph);
646 return (CAM_REQ_CMP_ERR);
649 /* Register the device */
650 make_dev_args_init(&args);
651 args.mda_devsw = &pass_cdevsw;
652 args.mda_unit = periph->unit_number;
653 args.mda_uid = UID_ROOT;
654 args.mda_gid = GID_OPERATOR;
655 args.mda_mode = 0600;
656 args.mda_si_drv1 = periph;
657 error = make_dev_s(&args, &softc->dev, "%s%d", periph->periph_name,
658 periph->unit_number);
660 cam_periph_lock(periph);
661 cam_periph_release_locked(periph);
662 return (CAM_REQ_CMP_ERR);
666 * Hold a reference to the periph before we create the physical
667 * path alias so it can't go away.
669 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
670 xpt_print(periph->path, "%s: lost periph during "
671 "registration!\n", __func__);
672 cam_periph_lock(periph);
673 return (CAM_REQ_CMP_ERR);
676 cam_periph_lock(periph);
678 TASK_INIT(&softc->add_physpath_task, /*priority*/0,
679 pass_add_physpath, periph);
682 * See if physical path information is already available.
684 taskqueue_enqueue(taskqueue_thread, &softc->add_physpath_task);
687 * Add an async callback so that we get notified if
688 * this device goes away or its physical path
689 * (stored in the advanced info data of the EDT) has
692 xpt_register_async(AC_LOST_DEVICE | AC_ADVINFO_CHANGED,
693 passasync, periph, periph->path);
696 xpt_announce_periph(periph, NULL);
702 passopen(struct cdev *dev, int flags, int fmt, struct thread *td)
704 struct cam_periph *periph;
705 struct pass_softc *softc;
708 periph = (struct cam_periph *)dev->si_drv1;
709 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
712 cam_periph_lock(periph);
714 softc = (struct pass_softc *)periph->softc;
716 if (softc->flags & PASS_FLAG_INVALID) {
717 cam_periph_release_locked(periph);
718 cam_periph_unlock(periph);
723 * Don't allow access when we're running at a high securelevel.
725 error = securelevel_gt(td->td_ucred, 1);
727 cam_periph_release_locked(periph);
728 cam_periph_unlock(periph);
733 * Only allow read-write access.
735 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0)) {
736 cam_periph_release_locked(periph);
737 cam_periph_unlock(periph);
742 * We don't allow nonblocking access.
744 if ((flags & O_NONBLOCK) != 0) {
745 xpt_print(periph->path, "can't do nonblocking access\n");
746 cam_periph_release_locked(periph);
747 cam_periph_unlock(periph);
753 cam_periph_unlock(periph);
759 passclose(struct cdev *dev, int flag, int fmt, struct thread *td)
761 struct cam_periph *periph;
762 struct pass_softc *softc;
765 periph = (struct cam_periph *)dev->si_drv1;
766 mtx = cam_periph_mtx(periph);
769 softc = periph->softc;
772 if (softc->open_count == 0) {
773 struct pass_io_req *io_req, *io_req2;
775 TAILQ_FOREACH_SAFE(io_req, &softc->done_queue, links, io_req2) {
776 TAILQ_REMOVE(&softc->done_queue, io_req, links);
777 passiocleanup(softc, io_req);
778 uma_zfree(softc->pass_zone, io_req);
781 TAILQ_FOREACH_SAFE(io_req, &softc->incoming_queue, links,
783 TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
784 passiocleanup(softc, io_req);
785 uma_zfree(softc->pass_zone, io_req);
789 * If there are any active I/Os, we need to forcibly acquire a
790 * reference to the peripheral so that we don't go away
791 * before they complete. We'll release the reference when
792 * the abandoned queue is empty.
794 io_req = TAILQ_FIRST(&softc->active_queue);
796 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET) == 0) {
797 cam_periph_doacquire(periph);
798 softc->flags |= PASS_FLAG_ABANDONED_REF_SET;
802 * Since the I/O in the active queue is not under our
803 * control, just set a flag so that we can clean it up when
804 * it completes and put it on the abandoned queue. This
805 * will prevent our sending spurious completions in the
806 * event that the device is opened again before these I/Os
809 TAILQ_FOREACH_SAFE(io_req, &softc->active_queue, links,
811 TAILQ_REMOVE(&softc->active_queue, io_req, links);
812 io_req->flags |= PASS_IO_ABANDONED;
813 TAILQ_INSERT_TAIL(&softc->abandoned_queue, io_req,
818 cam_periph_release_locked(periph);
821 * We reference the lock directly here, instead of using
822 * cam_periph_unlock(). The reason is that the call to
823 * cam_periph_release_locked() above could result in the periph
824 * getting freed. If that is the case, dereferencing the periph
825 * with a cam_periph_unlock() call would cause a page fault.
827 * cam_periph_release() avoids this problem using the same method,
828 * but we're manually acquiring and dropping the lock here to
829 * protect the open count and avoid another lock acquisition and
839 passstart(struct cam_periph *periph, union ccb *start_ccb)
841 struct pass_softc *softc;
843 softc = (struct pass_softc *)periph->softc;
845 switch (softc->state) {
846 case PASS_STATE_NORMAL: {
847 struct pass_io_req *io_req;
850 * Check for any queued I/O requests that require an
853 io_req = TAILQ_FIRST(&softc->incoming_queue);
854 if (io_req == NULL) {
855 xpt_release_ccb(start_ccb);
858 TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
859 TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
861 * Merge the user's CCB into the allocated CCB.
863 xpt_merge_ccb(start_ccb, &io_req->ccb);
864 start_ccb->ccb_h.ccb_type = PASS_CCB_QUEUED_IO;
865 start_ccb->ccb_h.ccb_ioreq = io_req;
866 start_ccb->ccb_h.cbfcnp = passdone;
867 io_req->alloced_ccb = start_ccb;
868 binuptime(&io_req->start_time);
869 devstat_start_transaction(softc->device_stats,
870 &io_req->start_time);
872 xpt_action(start_ccb);
875 * If we have any more I/O waiting, schedule ourselves again.
877 if (!TAILQ_EMPTY(&softc->incoming_queue))
878 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
887 passdone(struct cam_periph *periph, union ccb *done_ccb)
889 struct pass_softc *softc;
890 struct ccb_scsiio *csio;
892 softc = (struct pass_softc *)periph->softc;
894 cam_periph_assert(periph, MA_OWNED);
896 csio = &done_ccb->csio;
897 switch (csio->ccb_h.ccb_type) {
898 case PASS_CCB_QUEUED_IO: {
899 struct pass_io_req *io_req;
901 io_req = done_ccb->ccb_h.ccb_ioreq;
903 xpt_print(periph->path, "%s: called for user CCB %p\n",
904 __func__, io_req->user_ccb_ptr);
906 if (((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
907 && (done_ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER)
908 && ((io_req->flags & PASS_IO_ABANDONED) == 0)) {
911 error = passerror(done_ccb, CAM_RETRY_SELTO,
912 SF_RETRY_UA | SF_NO_PRINT);
914 if (error == ERESTART) {
916 * A retry was scheduled, so
924 * Copy the allocated CCB contents back to the malloced CCB
925 * so we can give status back to the user when he requests it.
927 bcopy(done_ccb, &io_req->ccb, sizeof(*done_ccb));
930 * Log data/transaction completion with devstat(9).
932 switch (done_ccb->ccb_h.func_code) {
934 devstat_end_transaction(softc->device_stats,
935 done_ccb->csio.dxfer_len - done_ccb->csio.resid,
936 done_ccb->csio.tag_action & 0x3,
937 ((done_ccb->ccb_h.flags & CAM_DIR_MASK) ==
938 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
939 (done_ccb->ccb_h.flags & CAM_DIR_OUT) ?
940 DEVSTAT_WRITE : DEVSTAT_READ, NULL,
941 &io_req->start_time);
944 devstat_end_transaction(softc->device_stats,
945 done_ccb->ataio.dxfer_len - done_ccb->ataio.resid,
946 0, /* Not used in ATA */
947 ((done_ccb->ccb_h.flags & CAM_DIR_MASK) ==
948 CAM_DIR_NONE) ? DEVSTAT_NO_DATA :
949 (done_ccb->ccb_h.flags & CAM_DIR_OUT) ?
950 DEVSTAT_WRITE : DEVSTAT_READ, NULL,
951 &io_req->start_time);
955 * XXX KDM this isn't quite right, but there isn't
956 * currently an easy way to represent a bidirectional
957 * transfer in devstat. The only way to do it
958 * and have the byte counts come out right would
959 * mean that we would have to record two
960 * transactions, one for the request and one for the
961 * response. For now, so that we report something,
962 * just treat the entire thing as a read.
964 devstat_end_transaction(softc->device_stats,
965 done_ccb->smpio.smp_request_len +
966 done_ccb->smpio.smp_response_len,
967 DEVSTAT_TAG_SIMPLE, DEVSTAT_READ, NULL,
968 &io_req->start_time);
971 devstat_end_transaction(softc->device_stats, 0,
972 DEVSTAT_TAG_NONE, DEVSTAT_NO_DATA, NULL,
973 &io_req->start_time);
978 * In the normal case, take the completed I/O off of the
979 * active queue and put it on the done queue. Notitfy the
980 * user that we have a completed I/O.
982 if ((io_req->flags & PASS_IO_ABANDONED) == 0) {
983 TAILQ_REMOVE(&softc->active_queue, io_req, links);
984 TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
985 selwakeuppri(&softc->read_select, PRIBIO);
986 KNOTE_LOCKED(&softc->read_select.si_note, 0);
989 * In the case of an abandoned I/O (final close
990 * without fetching the I/O), take it off of the
991 * abandoned queue and free it.
993 TAILQ_REMOVE(&softc->abandoned_queue, io_req, links);
994 passiocleanup(softc, io_req);
995 uma_zfree(softc->pass_zone, io_req);
998 * Release the done_ccb here, since we may wind up
999 * freeing the peripheral when we decrement the
1000 * reference count below.
1002 xpt_release_ccb(done_ccb);
1005 * If the abandoned queue is empty, we can release
1006 * our reference to the periph since we won't have
1007 * any more completions coming.
1009 if ((TAILQ_EMPTY(&softc->abandoned_queue))
1010 && (softc->flags & PASS_FLAG_ABANDONED_REF_SET)) {
1011 softc->flags &= ~PASS_FLAG_ABANDONED_REF_SET;
1012 cam_periph_release_locked(periph);
1016 * We have already released the CCB, so we can
1024 xpt_release_ccb(done_ccb);
1028 passcreatezone(struct cam_periph *periph)
1030 struct pass_softc *softc;
1034 softc = (struct pass_softc *)periph->softc;
1036 cam_periph_assert(periph, MA_OWNED);
1037 KASSERT(((softc->flags & PASS_FLAG_ZONE_VALID) == 0),
1038 ("%s called when the pass(4) zone is valid!\n", __func__));
1039 KASSERT((softc->pass_zone == NULL),
1040 ("%s called when the pass(4) zone is allocated!\n", __func__));
1042 if ((softc->flags & PASS_FLAG_ZONE_INPROG) == 0) {
1045 * We're the first context through, so we need to create
1046 * the pass(4) UMA zone for I/O requests.
1048 softc->flags |= PASS_FLAG_ZONE_INPROG;
1051 * uma_zcreate() does a blocking (M_WAITOK) allocation,
1052 * so we cannot hold a mutex while we call it.
1054 cam_periph_unlock(periph);
1056 softc->pass_zone = uma_zcreate(softc->zone_name,
1057 sizeof(struct pass_io_req), NULL, NULL, NULL, NULL,
1058 /*align*/ 0, /*flags*/ 0);
1060 softc->pass_io_zone = uma_zcreate(softc->io_zone_name,
1061 softc->io_zone_size, NULL, NULL, NULL, NULL,
1062 /*align*/ 0, /*flags*/ 0);
1064 cam_periph_lock(periph);
1066 if ((softc->pass_zone == NULL)
1067 || (softc->pass_io_zone == NULL)) {
1068 if (softc->pass_zone == NULL)
1069 xpt_print(periph->path, "unable to allocate "
1070 "IO Req UMA zone\n");
1072 xpt_print(periph->path, "unable to allocate "
1074 softc->flags &= ~PASS_FLAG_ZONE_INPROG;
1079 * Set the flags appropriately and notify any other waiters.
1081 softc->flags &= PASS_FLAG_ZONE_INPROG;
1082 softc->flags |= PASS_FLAG_ZONE_VALID;
1083 wakeup(&softc->pass_zone);
1086 * In this case, the UMA zone has not yet been created, but
1087 * another context is in the process of creating it. We
1088 * need to sleep until the creation is either done or has
1091 while ((softc->flags & PASS_FLAG_ZONE_INPROG)
1092 && ((softc->flags & PASS_FLAG_ZONE_VALID) == 0)) {
1093 error = msleep(&softc->pass_zone,
1094 cam_periph_mtx(periph), PRIBIO,
1100 * If the zone creation failed, no luck for the user.
1102 if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0){
1112 passiocleanup(struct pass_softc *softc, struct pass_io_req *io_req)
1115 u_int8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1120 switch (ccb->ccb_h.func_code) {
1122 numbufs = min(io_req->num_bufs, 2);
1125 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1127 data_ptrs[0] = (u_int8_t **)&ccb->cdm.patterns;
1128 data_ptrs[1] = (u_int8_t **)&ccb->cdm.matches;
1132 case XPT_CONT_TARGET_IO:
1133 data_ptrs[0] = &ccb->csio.data_ptr;
1134 numbufs = min(io_req->num_bufs, 1);
1137 data_ptrs[0] = &ccb->ataio.data_ptr;
1138 numbufs = min(io_req->num_bufs, 1);
1141 numbufs = min(io_req->num_bufs, 2);
1142 data_ptrs[0] = &ccb->smpio.smp_request;
1143 data_ptrs[1] = &ccb->smpio.smp_response;
1145 case XPT_DEV_ADVINFO:
1146 numbufs = min(io_req->num_bufs, 1);
1147 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1150 /* allow ourselves to be swapped once again */
1152 break; /* NOTREACHED */
1155 if (io_req->flags & PASS_IO_USER_SEG_MALLOC) {
1156 free(io_req->user_segptr, M_SCSIPASS);
1157 io_req->user_segptr = NULL;
1161 * We only want to free memory we malloced.
1163 if (io_req->data_flags == CAM_DATA_VADDR) {
1164 for (i = 0; i < io_req->num_bufs; i++) {
1165 if (io_req->kern_bufs[i] == NULL)
1168 free(io_req->kern_bufs[i], M_SCSIPASS);
1169 io_req->kern_bufs[i] = NULL;
1171 } else if (io_req->data_flags == CAM_DATA_SG) {
1172 for (i = 0; i < io_req->num_kern_segs; i++) {
1173 if ((uint8_t *)(uintptr_t)
1174 io_req->kern_segptr[i].ds_addr == NULL)
1177 uma_zfree(softc->pass_io_zone, (uint8_t *)(uintptr_t)
1178 io_req->kern_segptr[i].ds_addr);
1179 io_req->kern_segptr[i].ds_addr = 0;
1183 if (io_req->flags & PASS_IO_KERN_SEG_MALLOC) {
1184 free(io_req->kern_segptr, M_SCSIPASS);
1185 io_req->kern_segptr = NULL;
1188 if (io_req->data_flags != CAM_DATA_PADDR) {
1189 for (i = 0; i < numbufs; i++) {
1191 * Restore the user's buffer pointers to their
1194 if (io_req->user_bufs[i] != NULL)
1195 *data_ptrs[i] = io_req->user_bufs[i];
1202 passcopysglist(struct cam_periph *periph, struct pass_io_req *io_req,
1203 ccb_flags direction)
1205 bus_size_t kern_watermark, user_watermark, len_copied, len_to_copy;
1206 bus_dma_segment_t *user_sglist, *kern_sglist;
1214 user_sglist = io_req->user_segptr;
1215 kern_sglist = io_req->kern_segptr;
1217 for (i = 0, j = 0; i < io_req->num_user_segs &&
1218 j < io_req->num_kern_segs;) {
1219 uint8_t *user_ptr, *kern_ptr;
1221 len_to_copy = min(user_sglist[i].ds_len -user_watermark,
1222 kern_sglist[j].ds_len - kern_watermark);
1224 user_ptr = (uint8_t *)(uintptr_t)user_sglist[i].ds_addr;
1225 user_ptr = user_ptr + user_watermark;
1226 kern_ptr = (uint8_t *)(uintptr_t)kern_sglist[j].ds_addr;
1227 kern_ptr = kern_ptr + kern_watermark;
1229 user_watermark += len_to_copy;
1230 kern_watermark += len_to_copy;
1232 if (!useracc(user_ptr, len_to_copy,
1233 (direction == CAM_DIR_IN) ? VM_PROT_WRITE : VM_PROT_READ)) {
1234 xpt_print(periph->path, "%s: unable to access user "
1235 "S/G list element %p len %zu\n", __func__,
1236 user_ptr, len_to_copy);
1241 if (direction == CAM_DIR_IN) {
1242 error = copyout(kern_ptr, user_ptr, len_to_copy);
1244 xpt_print(periph->path, "%s: copyout of %u "
1245 "bytes from %p to %p failed with "
1246 "error %d\n", __func__, len_to_copy,
1247 kern_ptr, user_ptr, error);
1251 error = copyin(user_ptr, kern_ptr, len_to_copy);
1253 xpt_print(periph->path, "%s: copyin of %u "
1254 "bytes from %p to %p failed with "
1255 "error %d\n", __func__, len_to_copy,
1256 user_ptr, kern_ptr, error);
1261 len_copied += len_to_copy;
1263 if (user_sglist[i].ds_len == user_watermark) {
1268 if (kern_sglist[j].ds_len == kern_watermark) {
1280 passmemsetup(struct cam_periph *periph, struct pass_io_req *io_req)
1283 struct pass_softc *softc;
1285 uint8_t **data_ptrs[CAM_PERIPH_MAXMAPS];
1286 uint32_t lengths[CAM_PERIPH_MAXMAPS];
1287 uint32_t dirs[CAM_PERIPH_MAXMAPS];
1289 uint16_t *seg_cnt_ptr;
1293 cam_periph_assert(periph, MA_NOTOWNED);
1295 softc = periph->softc;
1303 switch(ccb->ccb_h.func_code) {
1305 if (ccb->cdm.match_buf_len == 0) {
1306 printf("%s: invalid match buffer length 0\n", __func__);
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;
1318 data_ptrs[0] = (u_int8_t **)&ccb->cdm.matches;
1319 lengths[0] = ccb->cdm.match_buf_len;
1320 dirs[0] = CAM_DIR_IN;
1323 io_req->data_flags = CAM_DATA_VADDR;
1326 case XPT_CONT_TARGET_IO:
1327 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1331 * The user shouldn't be able to supply a bio.
1333 if ((ccb->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_BIO)
1336 io_req->data_flags = ccb->ccb_h.flags & CAM_DATA_MASK;
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;
1344 maxmap = softc->maxio;
1347 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_NONE)
1351 * We only support a single virtual address for ATA I/O.
1353 if ((ccb->ccb_h.flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
1356 io_req->data_flags = CAM_DATA_VADDR;
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;
1362 maxmap = softc->maxio;
1365 io_req->data_flags = CAM_DATA_VADDR;
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;
1374 maxmap = softc->maxio;
1376 case XPT_DEV_ADVINFO:
1377 if (ccb->cdai.bufsiz == 0)
1380 io_req->data_flags = CAM_DATA_VADDR;
1382 data_ptrs[0] = (uint8_t **)&ccb->cdai.buf;
1383 lengths[0] = ccb->cdai.bufsiz;
1384 dirs[0] = CAM_DIR_IN;
1389 break; /* NOTREACHED */
1392 io_req->num_bufs = numbufs;
1395 * If there is a maximum, check to make sure that the user's
1396 * request fits within the limit. In general, we should only have
1397 * a maximum length for requests that go to hardware. Otherwise it
1398 * is whatever we're able to malloc.
1400 for (i = 0; i < numbufs; i++) {
1401 io_req->user_bufs[i] = *data_ptrs[i];
1402 io_req->dirs[i] = dirs[i];
1403 io_req->lengths[i] = lengths[i];
1408 if (lengths[i] <= maxmap)
1411 xpt_print(periph->path, "%s: data length %u > max allowed %u "
1412 "bytes\n", __func__, lengths[i], maxmap);
1417 switch (io_req->data_flags) {
1418 case CAM_DATA_VADDR:
1419 /* Map or copy the buffer into kernel address space */
1420 for (i = 0; i < numbufs; i++) {
1424 * If for some reason no length is specified, we
1425 * don't need to allocate anything.
1427 if (io_req->lengths[i] == 0)
1431 * Make sure that the user's buffer is accessible
1434 if (!useracc(io_req->user_bufs[i], io_req->lengths[i],
1435 (io_req->dirs[i] == CAM_DIR_IN) ? VM_PROT_WRITE :
1437 xpt_print(periph->path, "%s: user address %p "
1438 "length %u is not accessible\n", __func__,
1439 io_req->user_bufs[i], io_req->lengths[i]);
1444 tmp_buf = malloc(lengths[i], M_SCSIPASS,
1446 io_req->kern_bufs[i] = tmp_buf;
1447 *data_ptrs[i] = tmp_buf;
1450 xpt_print(periph->path, "%s: malloced %p len %u, user "
1451 "buffer %p, operation: %s\n", __func__,
1452 tmp_buf, lengths[i], io_req->user_bufs[i],
1453 (dirs[i] == CAM_DIR_IN) ? "read" : "write");
1456 * We only need to copy in if the user is writing.
1458 if (dirs[i] != CAM_DIR_OUT)
1461 error = copyin(io_req->user_bufs[i],
1462 io_req->kern_bufs[i], lengths[i]);
1464 xpt_print(periph->path, "%s: copy of user "
1465 "buffer from %p to %p failed with "
1466 "error %d\n", __func__,
1467 io_req->user_bufs[i],
1468 io_req->kern_bufs[i], error);
1473 case CAM_DATA_PADDR:
1474 /* Pass down the pointer as-is */
1477 size_t sg_length, size_to_go, alloc_size;
1478 uint32_t num_segs_needed;
1481 * Copy the user S/G list in, and then copy in the
1482 * individual segments.
1485 * We shouldn't see this, but check just in case.
1488 xpt_print(periph->path, "%s: cannot currently handle "
1489 "more than one S/G list per CCB\n", __func__);
1495 * We have to have at least one segment.
1497 if (num_segs == 0) {
1498 xpt_print(periph->path, "%s: CAM_DATA_SG flag set, "
1499 "but sglist_cnt=0!\n", __func__);
1505 * Make sure the user specified the total length and didn't
1506 * just leave it to us to decode the S/G list.
1508 if (lengths[0] == 0) {
1509 xpt_print(periph->path, "%s: no dxfer_len specified, "
1510 "but CAM_DATA_SG flag is set!\n", __func__);
1516 * We allocate buffers in io_zone_size increments for an
1517 * S/G list. This will generally be MAXPHYS.
1519 if (lengths[0] <= softc->io_zone_size)
1520 num_segs_needed = 1;
1522 num_segs_needed = lengths[0] / softc->io_zone_size;
1523 if ((lengths[0] % softc->io_zone_size) != 0)
1527 /* Figure out the size of the S/G list */
1528 sg_length = num_segs * sizeof(bus_dma_segment_t);
1529 io_req->num_user_segs = num_segs;
1530 io_req->num_kern_segs = num_segs_needed;
1532 /* Save the user's S/G list pointer for later restoration */
1533 io_req->user_bufs[0] = *data_ptrs[0];
1536 * If we have enough segments allocated by default to handle
1537 * the length of the user's S/G list,
1539 if (num_segs > PASS_MAX_SEGS) {
1540 io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
1541 num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
1542 io_req->flags |= PASS_IO_USER_SEG_MALLOC;
1544 io_req->user_segptr = io_req->user_segs;
1546 if (!useracc(*data_ptrs[0], sg_length, VM_PROT_READ)) {
1547 xpt_print(periph->path, "%s: unable to access user "
1548 "S/G list at %p\n", __func__, *data_ptrs[0]);
1553 error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
1555 xpt_print(periph->path, "%s: copy of user S/G list "
1556 "from %p to %p failed with error %d\n",
1557 __func__, *data_ptrs[0], io_req->user_segptr,
1562 if (num_segs_needed > PASS_MAX_SEGS) {
1563 io_req->kern_segptr = malloc(sizeof(bus_dma_segment_t) *
1564 num_segs_needed, M_SCSIPASS, M_WAITOK | M_ZERO);
1565 io_req->flags |= PASS_IO_KERN_SEG_MALLOC;
1567 io_req->kern_segptr = io_req->kern_segs;
1571 * Allocate the kernel S/G list.
1573 for (size_to_go = lengths[0], i = 0;
1574 size_to_go > 0 && i < num_segs_needed;
1575 i++, size_to_go -= alloc_size) {
1578 alloc_size = min(size_to_go, softc->io_zone_size);
1579 kern_ptr = uma_zalloc(softc->pass_io_zone, M_WAITOK);
1580 io_req->kern_segptr[i].ds_addr =
1581 (bus_addr_t)(uintptr_t)kern_ptr;
1582 io_req->kern_segptr[i].ds_len = alloc_size;
1584 if (size_to_go > 0) {
1585 printf("%s: size_to_go = %zu, software error!\n",
1586 __func__, size_to_go);
1591 *data_ptrs[0] = (uint8_t *)io_req->kern_segptr;
1592 *seg_cnt_ptr = io_req->num_kern_segs;
1595 * We only need to copy data here if the user is writing.
1597 if (dirs[0] == CAM_DIR_OUT)
1598 error = passcopysglist(periph, io_req, dirs[0]);
1601 case CAM_DATA_SG_PADDR: {
1605 * We shouldn't see this, but check just in case.
1608 printf("%s: cannot currently handle more than one "
1609 "S/G list per CCB\n", __func__);
1615 * We have to have at least one segment.
1617 if (num_segs == 0) {
1618 xpt_print(periph->path, "%s: CAM_DATA_SG_PADDR flag "
1619 "set, but sglist_cnt=0!\n", __func__);
1625 * Make sure the user specified the total length and didn't
1626 * just leave it to us to decode the S/G list.
1628 if (lengths[0] == 0) {
1629 xpt_print(periph->path, "%s: no dxfer_len specified, "
1630 "but CAM_DATA_SG flag is set!\n", __func__);
1635 /* Figure out the size of the S/G list */
1636 sg_length = num_segs * sizeof(bus_dma_segment_t);
1637 io_req->num_user_segs = num_segs;
1638 io_req->num_kern_segs = io_req->num_user_segs;
1640 /* Save the user's S/G list pointer for later restoration */
1641 io_req->user_bufs[0] = *data_ptrs[0];
1643 if (num_segs > PASS_MAX_SEGS) {
1644 io_req->user_segptr = malloc(sizeof(bus_dma_segment_t) *
1645 num_segs, M_SCSIPASS, M_WAITOK | M_ZERO);
1646 io_req->flags |= PASS_IO_USER_SEG_MALLOC;
1648 io_req->user_segptr = io_req->user_segs;
1650 io_req->kern_segptr = io_req->user_segptr;
1652 error = copyin(*data_ptrs[0], io_req->user_segptr, sg_length);
1654 xpt_print(periph->path, "%s: copy of user S/G list "
1655 "from %p to %p failed with error %d\n",
1656 __func__, *data_ptrs[0], io_req->user_segptr,
1665 * A user shouldn't be attaching a bio to the CCB. It
1666 * isn't a user-accessible structure.
1674 passiocleanup(softc, io_req);
1680 passmemdone(struct cam_periph *periph, struct pass_io_req *io_req)
1682 struct pass_softc *softc;
1688 softc = (struct pass_softc *)periph->softc;
1691 switch (io_req->data_flags) {
1692 case CAM_DATA_VADDR:
1694 * Copy back to the user buffer if this was a read.
1696 for (i = 0; i < io_req->num_bufs; i++) {
1697 if (io_req->dirs[i] != CAM_DIR_IN)
1700 error = copyout(io_req->kern_bufs[i],
1701 io_req->user_bufs[i], io_req->lengths[i]);
1703 xpt_print(periph->path, "Unable to copy %u "
1704 "bytes from %p to user address %p\n",
1706 io_req->kern_bufs[i],
1707 io_req->user_bufs[i]);
1713 case CAM_DATA_PADDR:
1714 /* Do nothing. The pointer is a physical address already */
1718 * Copy back to the user buffer if this was a read.
1719 * Restore the user's S/G list buffer pointer.
1721 if (io_req->dirs[0] == CAM_DIR_IN)
1722 error = passcopysglist(periph, io_req, io_req->dirs[0]);
1724 case CAM_DATA_SG_PADDR:
1726 * Restore the user's S/G list buffer pointer. No need to
1738 * Reset the user's pointers to their original values and free
1741 passiocleanup(softc, io_req);
1747 passioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1751 if ((error = passdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
1752 error = cam_compat_ioctl(dev, cmd, addr, flag, td, passdoioctl);
1758 passdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1760 struct cam_periph *periph;
1761 struct pass_softc *softc;
1765 periph = (struct cam_periph *)dev->si_drv1;
1766 cam_periph_lock(periph);
1767 softc = (struct pass_softc *)periph->softc;
1779 inccb = (union ccb *)addr;
1782 * Some CCB types, like scan bus and scan lun can only go
1783 * through the transport layer device.
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);
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;
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
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);
1809 ccb = xpt_alloc_ccb_nowait();
1812 xpt_setup_ccb(&ccb->ccb_h, periph->path,
1818 xpt_print(periph->path, "unable to allocate CCB\n");
1823 error = passsendccb(periph, ccb, inccb);
1828 xpt_release_ccb(ccb);
1834 struct pass_io_req *io_req;
1835 union ccb **user_ccb, *ccb;
1838 if ((softc->flags & PASS_FLAG_ZONE_VALID) == 0) {
1839 error = passcreatezone(periph);
1845 * We're going to do a blocking allocation for this I/O
1846 * request, so we have to drop the lock.
1848 cam_periph_unlock(periph);
1850 io_req = uma_zalloc(softc->pass_zone, M_WAITOK | M_ZERO);
1852 user_ccb = (union ccb **)addr;
1855 * Unlike the CAMIOCOMMAND ioctl above, we only have a
1856 * pointer to the user's CCB, so we have to copy the whole
1857 * thing in to a buffer we have allocated (above) instead
1858 * of allowing the ioctl code to malloc a buffer and copy
1861 * This is an advantage for this asynchronous interface,
1862 * since we don't want the memory to get freed while the
1863 * CCB is outstanding.
1866 xpt_print(periph->path, "Copying user CCB %p to "
1867 "kernel address %p\n", *user_ccb, ccb);
1869 error = copyin(*user_ccb, ccb, sizeof(*ccb));
1871 xpt_print(periph->path, "Copy of user CCB %p to "
1872 "kernel address %p failed with error %d\n",
1873 *user_ccb, ccb, error);
1874 uma_zfree(softc->pass_zone, io_req);
1875 cam_periph_lock(periph);
1880 * Some CCB types, like scan bus and scan lun can only go
1881 * through the transport layer device.
1883 if (ccb->ccb_h.func_code & XPT_FC_XPT_ONLY) {
1884 xpt_print(periph->path, "CCB function code %#x is "
1885 "restricted to the XPT device\n",
1886 ccb->ccb_h.func_code);
1887 uma_zfree(softc->pass_zone, io_req);
1888 cam_periph_lock(periph);
1894 * Save the user's CCB pointer as well as his linked list
1895 * pointers and peripheral private area so that we can
1896 * restore these later.
1898 io_req->user_ccb_ptr = *user_ccb;
1899 io_req->user_periph_links = ccb->ccb_h.periph_links;
1900 io_req->user_periph_priv = ccb->ccb_h.periph_priv;
1903 * Now that we've saved the user's values, we can set our
1904 * own peripheral private entry.
1906 ccb->ccb_h.ccb_ioreq = io_req;
1908 /* Compatibility for RL/priority-unaware code. */
1909 priority = ccb->ccb_h.pinfo.priority;
1910 if (priority <= CAM_PRIORITY_OOB)
1911 priority += CAM_PRIORITY_OOB + 1;
1914 * Setup fields in the CCB like the path and the priority.
1915 * The path in particular cannot be done in userland, since
1916 * it is a pointer to a kernel data structure.
1918 xpt_setup_ccb_flags(&ccb->ccb_h, periph->path, priority,
1922 * Setup our done routine. There is no way for the user to
1923 * have a valid pointer here.
1925 ccb->ccb_h.cbfcnp = passdone;
1927 fc = ccb->ccb_h.func_code;
1929 * If this function code has memory that can be mapped in
1930 * or out, we need to call passmemsetup().
1932 if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO)
1933 || (fc == XPT_SMP_IO) || (fc == XPT_DEV_MATCH)
1934 || (fc == XPT_DEV_ADVINFO)) {
1935 error = passmemsetup(periph, io_req);
1937 uma_zfree(softc->pass_zone, io_req);
1938 cam_periph_lock(periph);
1942 io_req->mapinfo.num_bufs_used = 0;
1944 cam_periph_lock(periph);
1947 * Everything goes on the incoming queue initially.
1949 TAILQ_INSERT_TAIL(&softc->incoming_queue, io_req, links);
1952 * If the CCB is queued, and is not a user CCB, then
1953 * we need to allocate a slot for it. Call xpt_schedule()
1954 * so that our start routine will get called when a CCB is
1957 if ((fc & XPT_FC_QUEUED)
1958 && ((fc & XPT_FC_USER_CCB) == 0)) {
1959 xpt_schedule(periph, priority);
1964 * At this point, the CCB in question is either an
1965 * immediate CCB (like XPT_DEV_ADVINFO) or it is a user CCB
1966 * and therefore should be malloced, not allocated via a slot.
1967 * Remove the CCB from the incoming queue and add it to the
1970 TAILQ_REMOVE(&softc->incoming_queue, io_req, links);
1971 TAILQ_INSERT_TAIL(&softc->active_queue, io_req, links);
1976 * If this is not a queued CCB (i.e. it is an immediate CCB),
1977 * then it is already done. We need to put it on the done
1978 * queue for the user to fetch.
1980 if ((fc & XPT_FC_QUEUED) == 0) {
1981 TAILQ_REMOVE(&softc->active_queue, io_req, links);
1982 TAILQ_INSERT_TAIL(&softc->done_queue, io_req, links);
1988 union ccb **user_ccb;
1989 struct pass_io_req *io_req;
1992 user_ccb = (union ccb **)addr;
1995 io_req = TAILQ_FIRST(&softc->done_queue);
1996 if (io_req == NULL) {
2002 * Remove the I/O from the done queue.
2004 TAILQ_REMOVE(&softc->done_queue, io_req, links);
2007 * We have to drop the lock during the copyout because the
2008 * copyout can result in VM faults that require sleeping.
2010 cam_periph_unlock(periph);
2013 * Do any needed copies (e.g. for reads) and revert the
2014 * pointers in the CCB back to the user's pointers.
2016 error = passmemdone(periph, io_req);
2020 io_req->ccb.ccb_h.periph_links = io_req->user_periph_links;
2021 io_req->ccb.ccb_h.periph_priv = io_req->user_periph_priv;
2024 xpt_print(periph->path, "Copying to user CCB %p from "
2025 "kernel address %p\n", *user_ccb, &io_req->ccb);
2028 error = copyout(&io_req->ccb, *user_ccb, sizeof(union ccb));
2030 xpt_print(periph->path, "Copy to user CCB %p from "
2031 "kernel address %p failed with error %d\n",
2032 *user_ccb, &io_req->ccb, error);
2036 * Prefer the first error we got back, and make sure we
2037 * don't overwrite bad status with good.
2042 cam_periph_lock(periph);
2045 * At this point, if there was an error, we could potentially
2046 * re-queue the I/O and try again. But why? The error
2047 * would almost certainly happen again. We might as well
2050 uma_zfree(softc->pass_zone, io_req);
2054 error = cam_periph_ioctl(periph, cmd, addr, passerror);
2059 cam_periph_unlock(periph);
2065 passpoll(struct cdev *dev, int poll_events, struct thread *td)
2067 struct cam_periph *periph;
2068 struct pass_softc *softc;
2071 periph = (struct cam_periph *)dev->si_drv1;
2072 softc = (struct pass_softc *)periph->softc;
2074 revents = poll_events & (POLLOUT | POLLWRNORM);
2075 if ((poll_events & (POLLIN | POLLRDNORM)) != 0) {
2076 cam_periph_lock(periph);
2078 if (!TAILQ_EMPTY(&softc->done_queue)) {
2079 revents |= poll_events & (POLLIN | POLLRDNORM);
2081 cam_periph_unlock(periph);
2083 selrecord(td, &softc->read_select);
2090 passkqfilter(struct cdev *dev, struct knote *kn)
2092 struct cam_periph *periph;
2093 struct pass_softc *softc;
2095 periph = (struct cam_periph *)dev->si_drv1;
2096 softc = (struct pass_softc *)periph->softc;
2098 kn->kn_hook = (caddr_t)periph;
2099 kn->kn_fop = &passread_filtops;
2100 knlist_add(&softc->read_select.si_note, kn, 0);
2106 passreadfiltdetach(struct knote *kn)
2108 struct cam_periph *periph;
2109 struct pass_softc *softc;
2111 periph = (struct cam_periph *)kn->kn_hook;
2112 softc = (struct pass_softc *)periph->softc;
2114 knlist_remove(&softc->read_select.si_note, kn, 0);
2118 passreadfilt(struct knote *kn, long hint)
2120 struct cam_periph *periph;
2121 struct pass_softc *softc;
2124 periph = (struct cam_periph *)kn->kn_hook;
2125 softc = (struct pass_softc *)periph->softc;
2127 cam_periph_assert(periph, MA_OWNED);
2129 if (TAILQ_EMPTY(&softc->done_queue))
2138 * Generally, "ccb" should be the CCB supplied by the kernel. "inccb"
2139 * should be the CCB that is copied in from the user.
2142 passsendccb(struct cam_periph *periph, union ccb *ccb, union ccb *inccb)
2144 struct pass_softc *softc;
2145 struct cam_periph_map_info mapinfo;
2149 softc = (struct pass_softc *)periph->softc;
2152 * There are some fields in the CCB header that need to be
2153 * preserved, the rest we get from the user.
2155 xpt_merge_ccb(ccb, inccb);
2159 ccb->ccb_h.cbfcnp = passdone;
2162 * Let cam_periph_mapmem do a sanity check on the data pointer format.
2163 * Even if no data transfer is needed, it's a cheap check and it
2164 * simplifies the code.
2166 fc = ccb->ccb_h.func_code;
2167 if ((fc == XPT_SCSI_IO) || (fc == XPT_ATA_IO) || (fc == XPT_SMP_IO)
2168 || (fc == XPT_DEV_MATCH) || (fc == XPT_DEV_ADVINFO)) {
2169 bzero(&mapinfo, sizeof(mapinfo));
2172 * cam_periph_mapmem calls into proc and vm functions that can
2173 * sleep as well as trigger I/O, so we can't hold the lock.
2174 * Dropping it here is reasonably safe.
2176 cam_periph_unlock(periph);
2177 error = cam_periph_mapmem(ccb, &mapinfo, softc->maxio);
2178 cam_periph_lock(periph);
2181 * cam_periph_mapmem returned an error, we can't continue.
2182 * Return the error to the user.
2187 /* Ensure that the unmap call later on is a no-op. */
2188 mapinfo.num_bufs_used = 0;
2191 * If the user wants us to perform any error recovery, then honor
2192 * that request. Otherwise, it's up to the user to perform any
2195 cam_periph_runccb(ccb, passerror, /* cam_flags */ CAM_RETRY_SELTO,
2196 /* sense_flags */ ((ccb->ccb_h.flags & CAM_PASS_ERR_RECOVER) ?
2197 SF_RETRY_UA : SF_NO_RECOVERY) | SF_NO_PRINT,
2198 softc->device_stats);
2200 cam_periph_unmapmem(ccb, &mapinfo);
2202 ccb->ccb_h.cbfcnp = NULL;
2203 ccb->ccb_h.periph_priv = inccb->ccb_h.periph_priv;
2204 bcopy(ccb, inccb, sizeof(union ccb));
2210 passerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2212 struct cam_periph *periph;
2213 struct pass_softc *softc;
2215 periph = xpt_path_periph(ccb->ccb_h.path);
2216 softc = (struct pass_softc *)periph->softc;
2218 return(cam_periph_error(ccb, cam_flags, sense_flags,
2219 &softc->saved_ccb));