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
|
/* $NetBSD: vatapi.c,v 1.4 2021/08/07 16:19:07 thorpej Exp $ */
/*-
* Copyright (c) 2018 Reinoud Zandijk <reinoud@NetBSD.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: vatapi.c,v 1.4 2021/08/07 16:19:07 thorpej Exp $");
#include <sys/param.h>
#include <sys/proc.h>
#include <sys/systm.h>
#include <sys/device.h>
#include <sys/buf.h>
#include <sys/disk.h>
#include <sys/kmem.h>
#include <sys/malloc.h>
#include <sys/scsiio.h>
#include <machine/mainbus.h>
#include <machine/thunk.h>
#include <machine/intr.h>
#include <dev/scsipi/scsi_all.h> /* for SCSI status */
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsipiconf.h>
#include <dev/scsipi/atapiconf.h>
#include "opt_scsi.h"
/* parameter? */
#define VDEV_ATAPI_DRIVE 0
#define MAX_SIZE ((1<<16))
/* forwards */
struct vatapi_softc;
static int vatapi_match(device_t, cfdata_t, void *);
static void vatapi_attach(device_t, device_t, void *);
static void vatapi_callback(device_t self);
static void vatapi_minphys(struct buf *bp);
static void vatapi_kill_pending(struct scsipi_periph *periph);
static void vatapi_scsipi_request(struct scsipi_channel *chan,
scsipi_adapter_req_t req, void *arg);
static void vatapi_probe_device(struct atapibus_softc *, int);
static void vatapi_complete(void *arg);
/* for debugging */
#ifdef SCSIVERBOSE
void scsipi_print_sense_data_real(struct scsi_sense_data *sense, int verbosity);
#endif
/* Note its one vdev, one adapter, one channel for now */
struct vatapi_softc {
device_t sc_dev;
device_t sc_pdev;
int sc_flags;
#define VATAPI_FLAG_POLLING 1
#define VATAPI_FLAG_INTR 2
/* backing `device' with its active command */
int sc_fd;
void *sc_ih;
struct scsipi_xfer *sc_xs;
/* atapibus */
device_t sc_vatapibus;
struct atapi_adapter sc_atapi_adapter;
#define sc_adapter sc_atapi_adapter._generic
struct scsipi_channel sc_channel;
};
CFATTACH_DECL_NEW(vatapi_thunkbus, sizeof(struct vatapi_softc),
vatapi_match, vatapi_attach, NULL, NULL);
static const struct scsipi_bustype vatapi_bustype = {
SCSIPI_BUSTYPE_ATAPI,
atapi_scsipi_cmd,
atapi_interpret_sense,
atapi_print_addr,
vatapi_kill_pending,
NULL
};
int
vatapi_match(device_t parent, cfdata_t match, void *opaque)
{
struct thunkbus_attach_args *taa = opaque;
if (taa->taa_type != THUNKBUS_TYPE_VATAPI)
return 0;
return 1;
}
static void
vatapi_attach(device_t parent, device_t self, void *opaque)
{
struct vatapi_softc *sc = device_private(self);
struct thunkbus_attach_args *taa = opaque;
sc->sc_dev = self;
sc->sc_pdev = parent;
/* open device */
sc->sc_fd = thunk_open(taa->u.vdev.path, O_RDWR, 0);
if (sc->sc_fd < 0) {
aprint_error(": error %d opening path\n", thunk_geterrno());
return;
}
aprint_naive("\n");
aprint_normal("\n");
sc->sc_ih = softint_establish(SOFTINT_BIO,
vatapi_complete, sc);
/* rest of the configuration is deferred */
config_interrupts(self, vatapi_callback);
}
static void
vatapi_callback(device_t self)
{
struct vatapi_softc *sc = device_private(self);
struct scsipi_adapter *adapt = &sc->sc_adapter;
struct scsipi_channel *chan = &sc->sc_channel;
/* set up the scsipi adapter and channel */
memset(adapt, 0, sizeof(*adapt));
adapt->adapt_dev = sc->sc_dev;
adapt->adapt_nchannels = 1;
adapt->adapt_request = vatapi_scsipi_request;
adapt->adapt_minphys = vatapi_minphys;
adapt->adapt_flags = 0; //SCSIPI_ADAPT_POLL_ONLY;
sc->sc_atapi_adapter.atapi_probe_device = vatapi_probe_device;
memset(chan, 0, sizeof(*chan));
chan->chan_adapter = adapt;
chan->chan_bustype = &vatapi_bustype;
chan->chan_channel = 0; /* location */
chan->chan_flags = SCSIPI_CHAN_OPENINGS;
chan->chan_openings = 1;
chan->chan_max_periph = 1;
chan->chan_ntargets = 1;
chan->chan_nluns = 1;
/* set polling */
//sc->sc_flags = VATAPI_FLAG_POLLING;
/* we `discovered' an atapi adapter */
sc->sc_vatapibus =
config_found(sc->sc_dev, chan, atapiprint, CFARGS_NONE);
}
/* XXX why is it be called minphys, when it enforces maxphys? */
static void
vatapi_minphys(struct buf *bp)
{
if (bp->b_bcount > MAX_SIZE)
bp->b_bcount = MAX_SIZE;
minphys(bp);
}
/*
* ATAPI device probing
*/
static void
vatapi_probe_device(struct atapibus_softc *atapi, int target)
{
struct scsipi_channel *chan = atapi->sc_channel;
struct scsipi_periph *periph;
struct scsipibus_attach_args sa;
char vendor[33], product[65], revision[17];
struct scsipi_inquiry_data inqbuf;
if (target != VDEV_ATAPI_DRIVE) /* only probe drive 0 */
return;
/* skip if already attached */
if (scsipi_lookup_periph(chan, target, 0) != NULL)
return;
/* allocate and set up periph structure */
periph = scsipi_alloc_periph(M_NOWAIT);
if (periph == NULL) {
aprint_error_dev(atapi->sc_dev,
"can't allocate link for drive %d\n", target);
return;
}
periph->periph_channel = chan;
periph->periph_switch = &atapi_probe_periphsw;
periph->periph_target = target;
periph->periph_quirks = chan->chan_defquirks;
/* inquiry */
aprint_verbose("%s: inquiry devices\n", __func__);
memset(&inqbuf, 0, sizeof(inqbuf));
if (scsipi_inquire(periph, &inqbuf, XS_CTL_DISCOVERY) != 0) {
aprint_error_dev(atapi->sc_dev, ": scsipi_inquire failed\n");
free(periph, M_DEVBUF);
return;
}
#define scsipi_strvis(a, al, b, bl) strlcpy(a, b, al)
scsipi_strvis(vendor, 33, inqbuf.vendor, 8);
scsipi_strvis(product, 65, inqbuf.product, 16);
scsipi_strvis(revision, 17, inqbuf.revision, 4);
#undef scsipi_strvis
sa.sa_periph = periph;
sa.sa_inqbuf.type = inqbuf.device;
sa.sa_inqbuf.removable = inqbuf.dev_qual2 & SID_REMOVABLE ?
T_REMOV : T_FIXED;
if (sa.sa_inqbuf.removable)
periph->periph_flags |= PERIPH_REMOVABLE;
sa.sa_inqbuf.vendor = vendor;
sa.sa_inqbuf.product = product;
sa.sa_inqbuf.revision = revision;
sa.sa_inqptr = NULL;
/* ATAPI demands only READ10 and higher IIRC */
periph->periph_quirks |= PQUIRK_ONLYBIG;
aprint_verbose(": probedev on vendor '%s' product '%s' revision '%s'\n",
vendor, product, revision);
atapi_probe_device(atapi, target, periph, &sa);
/* atapi_probe_device() frees the periph when there is no device.*/
}
/*
* Issue a request for a periph.
*/
static void
vatapi_scsipi_request(struct scsipi_channel *chan,
scsipi_adapter_req_t req, void *arg)
{
device_t sc_dev = chan->chan_adapter->adapt_dev;
struct vatapi_softc *sc = device_private(sc_dev);
struct scsipi_xfer *xs = arg;
switch (req) {
case ADAPTER_REQ_GROW_RESOURCES:
case ADAPTER_REQ_SET_XFER_MODE:
return;
case ADAPTER_REQ_RUN_XFER :
KASSERT(sc->sc_xs == NULL);
/* queue the command */
KASSERT(sc->sc_xs == NULL);
sc->sc_xs = xs;
softint_schedule(sc->sc_ih);
}
}
static void
vatapi_report_problem(scsireq_t *kreq)
{
#ifdef SCSIVERBOSE
printf("vatapi cmd failed: ");
for (int i = 0; i < kreq->cmdlen; i++) {
printf("%02x ", kreq->cmd[i]);
}
printf("\n");
scsipi_print_sense_data_real(
(struct scsi_sense_data *) kreq->sense, 1);
#endif
}
static void
vatapi_complete(void *arg)
{
struct vatapi_softc *sc = arg;
struct scsipi_xfer *xs = sc->sc_xs;
scsireq_t kreq;
memset(&kreq, 0, sizeof(kreq));
memcpy(kreq.cmd, xs->cmd, xs->cmdlen);
kreq.cmdlen = xs->cmdlen;
kreq.databuf = xs->data; /* in virt? */
kreq.datalen = xs->datalen;
kreq.timeout = xs->timeout;
kreq.flags = (xs->xs_control & XS_CTL_DATA_IN) ?
SCCMD_READ : SCCMD_WRITE;
kreq.senselen = sizeof(struct scsi_sense_data);
xs->error = XS_SHORTSENSE;
/* this is silly, but better make sure */
thunk_assert_presence((vaddr_t) kreq.databuf,
(size_t) kreq.datalen);
if (thunk_ioctl(sc->sc_fd, SCIOCCOMMAND, &kreq) != -1) {
switch (kreq.retsts) {
case SCCMD_OK :
xs->resid = 0;
xs->error = 0;
break;
case SCCMD_TIMEOUT :
break;
case SCCMD_BUSY :
break;
case SCCMD_SENSE :
xs->error = XS_SHORTSENSE; /* ATAPI */
memcpy(&xs->sense.scsi_sense, kreq.sense,
sizeof(struct scsi_sense_data));
vatapi_report_problem(&kreq);
break;
default:
thunk_printf("unhandled/unknown retstst %d\n", kreq.retsts);
break;
}
} else {
printf("thunk_ioctl == -1, errno %d\n", thunk_geterrno());
}
sc->sc_xs = NULL;
scsipi_done(xs);
}
/*
* Kill off all pending xfers for a periph.
*
* Must be called at splbio().
*/
static void
vatapi_kill_pending(struct scsipi_periph *periph)
{
/* do we need to do anything ? (yet?) */
printf("%s: target %d\n", __func__, periph->periph_target);
}
|