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