1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
|
/* $OpenBSD: vscsi.c,v 1.28 2014/01/22 01:21:33 dlg Exp $ */
/*
* Copyright (c) 2008 David Gwynne <dlg@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/buf.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/device.h>
#include <sys/proc.h>
#include <sys/conf.h>
#include <sys/queue.h>
#include <sys/rwlock.h>
#include <sys/pool.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/selinfo.h>
#include <scsi/scsi_all.h>
#include <scsi/scsiconf.h>
#include <dev/vscsivar.h>
int vscsi_match(struct device *, void *, void *);
void vscsi_attach(struct device *, struct device *, void *);
void vscsi_shutdown(void *);
struct vscsi_ccb {
TAILQ_ENTRY(vscsi_ccb) ccb_entry;
int ccb_tag;
struct scsi_xfer *ccb_xs;
size_t ccb_datalen;
};
TAILQ_HEAD(vscsi_ccb_list, vscsi_ccb);
enum vscsi_state {
VSCSI_S_CLOSED,
VSCSI_S_CONFIG,
VSCSI_S_RUNNING
};
struct vscsi_softc {
struct device sc_dev;
struct scsi_link sc_link;
struct scsibus_softc *sc_scsibus;
struct mutex sc_state_mtx;
enum vscsi_state sc_state;
u_int sc_ref_count;
struct pool sc_ccb_pool;
struct scsi_iopool sc_iopool;
struct vscsi_ccb_list sc_ccb_i2t;
struct vscsi_ccb_list sc_ccb_t2i;
int sc_ccb_tag;
struct mutex sc_poll_mtx;
struct rwlock sc_ioc_lock;
struct selinfo sc_sel;
struct mutex sc_sel_mtx;
};
#define DEVNAME(_s) ((_s)->sc_dev.dv_xname)
#define DEV2SC(_d) ((struct vscsi_softc *)device_lookup(&vscsi_cd, minor(_d)))
struct cfattach vscsi_ca = {
sizeof(struct vscsi_softc),
vscsi_match,
vscsi_attach
};
struct cfdriver vscsi_cd = {
NULL,
"vscsi",
DV_DULL
};
void vscsi_cmd(struct scsi_xfer *);
int vscsi_probe(struct scsi_link *);
void vscsi_free(struct scsi_link *);
struct scsi_adapter vscsi_switch = {
vscsi_cmd,
scsi_minphys,
vscsi_probe,
vscsi_free
};
int vscsi_i2t(struct vscsi_softc *, struct vscsi_ioc_i2t *);
int vscsi_data(struct vscsi_softc *, struct vscsi_ioc_data *, int);
int vscsi_t2i(struct vscsi_softc *, struct vscsi_ioc_t2i *);
void vscsi_done(struct vscsi_softc *, struct vscsi_ccb *);
void * vscsi_ccb_get(void *);
void vscsi_ccb_put(void *, void *);
void filt_vscsidetach(struct knote *);
int filt_vscsiread(struct knote *, long);
struct filterops vscsi_filtops = {
1,
NULL,
filt_vscsidetach,
filt_vscsiread
};
int
vscsi_match(struct device *parent, void *match, void *aux)
{
return (1);
}
void
vscsi_attach(struct device *parent, struct device *self, void *aux)
{
struct vscsi_softc *sc = (struct vscsi_softc *)self;
struct scsibus_attach_args saa;
printf("\n");
mtx_init(&sc->sc_state_mtx, IPL_BIO);
sc->sc_state = VSCSI_S_CLOSED;
TAILQ_INIT(&sc->sc_ccb_i2t);
TAILQ_INIT(&sc->sc_ccb_t2i);
mtx_init(&sc->sc_poll_mtx, IPL_BIO);
mtx_init(&sc->sc_sel_mtx, IPL_BIO);
rw_init(&sc->sc_ioc_lock, "vscsiioc");
scsi_iopool_init(&sc->sc_iopool, sc, vscsi_ccb_get, vscsi_ccb_put);
sc->sc_link.adapter = &vscsi_switch;
sc->sc_link.adapter_softc = sc;
sc->sc_link.adapter_target = 256;
sc->sc_link.adapter_buswidth = 256;
sc->sc_link.openings = 16;
sc->sc_link.pool = &sc->sc_iopool;
bzero(&saa, sizeof(saa));
saa.saa_sc_link = &sc->sc_link;
sc->sc_scsibus = (struct scsibus_softc *)config_found(&sc->sc_dev,
&saa, scsiprint);
}
void
vscsi_cmd(struct scsi_xfer *xs)
{
struct scsi_link *link = xs->sc_link;
struct vscsi_softc *sc = link->adapter_softc;
struct vscsi_ccb *ccb = xs->io;
int polled = ISSET(xs->flags, SCSI_POLL);
int running = 0;
if (ISSET(xs->flags, SCSI_POLL) && ISSET(xs->flags, SCSI_NOSLEEP)) {
printf("%s: POLL && NOSLEEP for 0x%02x\n", DEVNAME(sc),
xs->cmd->opcode);
xs->error = XS_DRIVER_STUFFUP;
scsi_done(xs);
return;
}
ccb->ccb_xs = xs;
mtx_enter(&sc->sc_state_mtx);
if (sc->sc_state == VSCSI_S_RUNNING) {
running = 1;
TAILQ_INSERT_TAIL(&sc->sc_ccb_i2t, ccb, ccb_entry);
}
mtx_leave(&sc->sc_state_mtx);
if (!running) {
xs->error = XS_DRIVER_STUFFUP;
scsi_done(xs);
return;
}
selwakeup(&sc->sc_sel);
if (polled) {
mtx_enter(&sc->sc_poll_mtx);
while (ccb->ccb_xs != NULL)
msleep(ccb, &sc->sc_poll_mtx, PRIBIO, "vscsipoll", 0);
mtx_leave(&sc->sc_poll_mtx);
scsi_done(xs);
}
}
void
vscsi_done(struct vscsi_softc *sc, struct vscsi_ccb *ccb)
{
struct scsi_xfer *xs = ccb->ccb_xs;
if (ISSET(xs->flags, SCSI_POLL)) {
mtx_enter(&sc->sc_poll_mtx);
ccb->ccb_xs = NULL;
wakeup(ccb);
mtx_leave(&sc->sc_poll_mtx);
} else
scsi_done(xs);
}
int
vscsi_probe(struct scsi_link *link)
{
struct vscsi_softc *sc = link->adapter_softc;
int rv = 0;
mtx_enter(&sc->sc_state_mtx);
if (sc->sc_state == VSCSI_S_RUNNING)
sc->sc_ref_count++;
else
rv = ENXIO;
mtx_leave(&sc->sc_state_mtx);
return (rv);
}
void
vscsi_free(struct scsi_link *link)
{
struct vscsi_softc *sc = link->adapter_softc;
mtx_enter(&sc->sc_state_mtx);
sc->sc_ref_count--;
if (sc->sc_state != VSCSI_S_RUNNING && sc->sc_ref_count == 0)
wakeup(&sc->sc_ref_count);
mtx_leave(&sc->sc_state_mtx);
}
int
vscsiopen(dev_t dev, int flags, int mode, struct proc *p)
{
struct vscsi_softc *sc = DEV2SC(dev);
enum vscsi_state state = VSCSI_S_RUNNING;
int rv = 0;
if (sc == NULL)
return (ENXIO);
mtx_enter(&sc->sc_state_mtx);
if (sc->sc_state != VSCSI_S_CLOSED)
rv = EBUSY;
else
sc->sc_state = VSCSI_S_CONFIG;
mtx_leave(&sc->sc_state_mtx);
if (rv != 0) {
device_unref(&sc->sc_dev);
return (rv);
}
pool_init(&sc->sc_ccb_pool, sizeof(struct vscsi_ccb), 0, 0, 0,
"vscsiccb", NULL);
pool_setipl(&sc->sc_ccb_pool, IPL_BIO);
/* we need to guarantee some ccbs will be available for the iopool */
rv = pool_prime(&sc->sc_ccb_pool, 8);
if (rv != 0) {
pool_destroy(&sc->sc_ccb_pool);
state = VSCSI_S_CLOSED;
}
/* commit changes */
mtx_enter(&sc->sc_state_mtx);
sc->sc_state = state;
mtx_leave(&sc->sc_state_mtx);
device_unref(&sc->sc_dev);
return (rv);
}
int
vscsiioctl(dev_t dev, u_long cmd, caddr_t addr, int flags, struct proc *p)
{
struct vscsi_softc *sc = DEV2SC(dev);
struct vscsi_ioc_devevent *de = (struct vscsi_ioc_devevent *)addr;
int read = 0;
int err = 0;
if (sc == NULL)
return (ENXIO);
rw_enter_write(&sc->sc_ioc_lock);
switch (cmd) {
case VSCSI_I2T:
err = vscsi_i2t(sc, (struct vscsi_ioc_i2t *)addr);
break;
case VSCSI_DATA_READ:
read = 1;
case VSCSI_DATA_WRITE:
err = vscsi_data(sc, (struct vscsi_ioc_data *)addr, read);
break;
case VSCSI_T2I:
err = vscsi_t2i(sc, (struct vscsi_ioc_t2i *)addr);
break;
case VSCSI_REQPROBE:
err = scsi_req_probe(sc->sc_scsibus, de->target, de->lun);
break;
case VSCSI_REQDETACH:
err = scsi_req_detach(sc->sc_scsibus, de->target, de->lun,
DETACH_FORCE);
break;
default:
err = ENOTTY;
break;
}
rw_exit_write(&sc->sc_ioc_lock);
device_unref(&sc->sc_dev);
return (err);
}
int
vscsi_i2t(struct vscsi_softc *sc, struct vscsi_ioc_i2t *i2t)
{
struct vscsi_ccb *ccb;
struct scsi_xfer *xs;
struct scsi_link *link;
mtx_enter(&sc->sc_state_mtx);
ccb = TAILQ_FIRST(&sc->sc_ccb_i2t);
if (ccb != NULL)
TAILQ_REMOVE(&sc->sc_ccb_i2t, ccb, ccb_entry);
mtx_leave(&sc->sc_state_mtx);
if (ccb == NULL)
return (EAGAIN);
xs = ccb->ccb_xs;
link = xs->sc_link;
i2t->tag = ccb->ccb_tag;
i2t->target = link->target;
i2t->lun = link->lun;
bcopy(xs->cmd, &i2t->cmd, xs->cmdlen);
i2t->cmdlen = xs->cmdlen;
i2t->datalen = xs->datalen;
switch (xs->flags & (SCSI_DATA_IN | SCSI_DATA_OUT)) {
case SCSI_DATA_IN:
i2t->direction = VSCSI_DIR_READ;
break;
case SCSI_DATA_OUT:
i2t->direction = VSCSI_DIR_WRITE;
break;
default:
i2t->direction = VSCSI_DIR_NONE;
break;
}
TAILQ_INSERT_TAIL(&sc->sc_ccb_t2i, ccb, ccb_entry);
return (0);
}
int
vscsi_data(struct vscsi_softc *sc, struct vscsi_ioc_data *data, int read)
{
struct vscsi_ccb *ccb;
struct scsi_xfer *xs;
int xsread;
u_int8_t *buf;
int rv = EINVAL;
TAILQ_FOREACH(ccb, &sc->sc_ccb_t2i, ccb_entry) {
if (ccb->ccb_tag == data->tag)
break;
}
if (ccb == NULL)
return (EFAULT);
xs = ccb->ccb_xs;
if (data->datalen > xs->datalen - ccb->ccb_datalen)
return (ENOMEM);
switch (xs->flags & (SCSI_DATA_IN | SCSI_DATA_OUT)) {
case SCSI_DATA_IN:
xsread = 1;
break;
case SCSI_DATA_OUT:
xsread = 0;
break;
default:
return (EINVAL);
}
if (read != xsread)
return (EINVAL);
buf = xs->data;
buf += ccb->ccb_datalen;
if (read)
rv = copyin(data->data, buf, data->datalen);
else
rv = copyout(buf, data->data, data->datalen);
if (rv == 0)
ccb->ccb_datalen += data->datalen;
return (rv);
}
int
vscsi_t2i(struct vscsi_softc *sc, struct vscsi_ioc_t2i *t2i)
{
struct vscsi_ccb *ccb;
struct scsi_xfer *xs;
struct scsi_link *link;
int rv = 0;
TAILQ_FOREACH(ccb, &sc->sc_ccb_t2i, ccb_entry) {
if (ccb->ccb_tag == t2i->tag)
break;
}
if (ccb == NULL)
return (EFAULT);
TAILQ_REMOVE(&sc->sc_ccb_t2i, ccb, ccb_entry);
xs = ccb->ccb_xs;
link = xs->sc_link;
xs->resid = xs->datalen - ccb->ccb_datalen;
xs->status = SCSI_OK;
switch (t2i->status) {
case VSCSI_STAT_DONE:
xs->error = XS_NOERROR;
break;
case VSCSI_STAT_SENSE:
xs->error = XS_SENSE;
bcopy(&t2i->sense, &xs->sense, sizeof(xs->sense));
break;
case VSCSI_STAT_RESET:
xs->error = XS_RESET;
break;
case VSCSI_STAT_ERR:
default:
xs->error = XS_DRIVER_STUFFUP;
break;
}
vscsi_done(sc, ccb);
return (rv);
}
int
vscsipoll(dev_t dev, int events, struct proc *p)
{
struct vscsi_softc *sc = DEV2SC(dev);
int revents = 0;
if (sc == NULL)
return (ENXIO);
if (events & (POLLIN | POLLRDNORM)) {
mtx_enter(&sc->sc_state_mtx);
if (!TAILQ_EMPTY(&sc->sc_ccb_i2t))
revents |= events & (POLLIN | POLLRDNORM);
mtx_leave(&sc->sc_state_mtx);
}
if (revents == 0) {
if (events & (POLLIN | POLLRDNORM))
selrecord(p, &sc->sc_sel);
}
device_unref(&sc->sc_dev);
return (revents);
}
int
vscsikqfilter(dev_t dev, struct knote *kn)
{
struct vscsi_softc *sc = DEV2SC(dev);
struct klist *klist;
if (sc == NULL)
return (ENXIO);
klist = &sc->sc_sel.si_note;
switch (kn->kn_filter) {
case EVFILT_READ:
kn->kn_fop = &vscsi_filtops;
break;
default:
device_unref(&sc->sc_dev);
return (EINVAL);
}
kn->kn_hook = (caddr_t)sc;
mtx_enter(&sc->sc_sel_mtx);
SLIST_INSERT_HEAD(klist, kn, kn_selnext);
mtx_leave(&sc->sc_sel_mtx);
device_unref(&sc->sc_dev);
return (0);
}
void
filt_vscsidetach(struct knote *kn)
{
struct vscsi_softc *sc = (struct vscsi_softc *)kn->kn_hook;
struct klist *klist = &sc->sc_sel.si_note;
mtx_enter(&sc->sc_sel_mtx);
SLIST_REMOVE(klist, kn, knote, kn_selnext);
mtx_leave(&sc->sc_sel_mtx);
}
int
filt_vscsiread(struct knote *kn, long hint)
{
struct vscsi_softc *sc = (struct vscsi_softc *)kn->kn_hook;
int event = 0;
mtx_enter(&sc->sc_state_mtx);
if (!TAILQ_EMPTY(&sc->sc_ccb_i2t))
event = 1;
mtx_leave(&sc->sc_state_mtx);
return (event);
}
int
vscsiclose(dev_t dev, int flags, int mode, struct proc *p)
{
struct vscsi_softc *sc = DEV2SC(dev);
struct vscsi_ccb *ccb;
if (sc == NULL)
return (ENXIO);
mtx_enter(&sc->sc_state_mtx);
KASSERT(sc->sc_state == VSCSI_S_RUNNING);
sc->sc_state = VSCSI_S_CONFIG;
mtx_leave(&sc->sc_state_mtx);
scsi_activate(sc->sc_scsibus, -1, -1, DVACT_DEACTIVATE);
while ((ccb = TAILQ_FIRST(&sc->sc_ccb_t2i)) != NULL) {
TAILQ_REMOVE(&sc->sc_ccb_t2i, ccb, ccb_entry);
ccb->ccb_xs->error = XS_RESET;
vscsi_done(sc, ccb);
}
while ((ccb = TAILQ_FIRST(&sc->sc_ccb_i2t)) != NULL) {
TAILQ_REMOVE(&sc->sc_ccb_i2t, ccb, ccb_entry);
ccb->ccb_xs->error = XS_RESET;
vscsi_done(sc, ccb);
}
scsi_req_detach(sc->sc_scsibus, -1, -1, DETACH_FORCE);
mtx_enter(&sc->sc_state_mtx);
while (sc->sc_ref_count > 0) {
msleep(&sc->sc_ref_count, &sc->sc_state_mtx,
PRIBIO, "vscsiref", 0);
}
mtx_leave(&sc->sc_state_mtx);
pool_destroy(&sc->sc_ccb_pool);
mtx_enter(&sc->sc_state_mtx);
sc->sc_state = VSCSI_S_CLOSED;
mtx_leave(&sc->sc_state_mtx);
device_unref(&sc->sc_dev);
return (0);
}
void *
vscsi_ccb_get(void *cookie)
{
struct vscsi_softc *sc = cookie;
struct vscsi_ccb *ccb = NULL;
ccb = pool_get(&sc->sc_ccb_pool, PR_NOWAIT);
if (ccb != NULL) {
ccb->ccb_tag = sc->sc_ccb_tag++;
ccb->ccb_datalen = 0;
}
return (ccb);
}
void
vscsi_ccb_put(void *cookie, void *io)
{
struct vscsi_softc *sc = cookie;
struct vscsi_ccb *ccb = io;
pool_put(&sc->sc_ccb_pool, ccb);
}
|