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