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