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