vioscsi.c revision 1.11 1 1.11 jdolecek /* $NetBSD: vioscsi.c,v 1.11 2017/03/13 21:06:50 jdolecek Exp $ */
2 1.1 christos /* $OpenBSD: vioscsi.c,v 1.3 2015/03/14 03:38:49 jsg Exp $ */
3 1.1 christos
4 1.1 christos /*
5 1.1 christos * Copyright (c) 2013 Google Inc.
6 1.1 christos *
7 1.1 christos * Permission to use, copy, modify, and distribute this software for any
8 1.1 christos * purpose with or without fee is hereby granted, provided that the above
9 1.1 christos * copyright notice and this permission notice appear in all copies.
10 1.1 christos *
11 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 1.1 christos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 1.1 christos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #include <sys/cdefs.h>
21 1.11 jdolecek __KERNEL_RCSID(0, "$NetBSD: vioscsi.c,v 1.11 2017/03/13 21:06:50 jdolecek Exp $");
22 1.1 christos
23 1.1 christos #include <sys/param.h>
24 1.1 christos #include <sys/systm.h>
25 1.1 christos #include <sys/device.h>
26 1.1 christos #include <sys/bus.h>
27 1.1 christos #include <sys/buf.h>
28 1.1 christos
29 1.1 christos #include <dev/pci/pcidevs.h>
30 1.1 christos #include <dev/pci/pcireg.h>
31 1.1 christos #include <dev/pci/pcivar.h>
32 1.1 christos
33 1.1 christos #include <dev/pci/vioscsireg.h>
34 1.1 christos #include <dev/pci/virtiovar.h>
35 1.1 christos
36 1.1 christos #include <dev/scsipi/scsi_all.h>
37 1.1 christos #include <dev/scsipi/scsiconf.h>
38 1.1 christos
39 1.1 christos #ifdef VIOSCSI_DEBUG
40 1.1 christos static int vioscsi_debug = 1;
41 1.1 christos #define DPRINTF(f) do { if (vioscsi_debug) printf f; } while (/*CONSTCOND*/0)
42 1.1 christos #else
43 1.1 christos #define DPRINTF(f) ((void)0)
44 1.1 christos #endif
45 1.1 christos
46 1.1 christos struct vioscsi_req {
47 1.1 christos struct virtio_scsi_req_hdr vr_req;
48 1.1 christos struct virtio_scsi_res_hdr vr_res;
49 1.1 christos struct scsipi_xfer *vr_xs;
50 1.1 christos bus_dmamap_t vr_control;
51 1.1 christos bus_dmamap_t vr_data;
52 1.1 christos };
53 1.1 christos
54 1.1 christos struct vioscsi_softc {
55 1.1 christos device_t sc_dev;
56 1.1 christos struct scsipi_adapter sc_adapter;
57 1.1 christos struct scsipi_channel sc_channel;
58 1.1 christos
59 1.1 christos struct virtqueue sc_vqs[3];
60 1.10 jdolecek #define VIOSCSI_VQ_CONTROL 0
61 1.10 jdolecek #define VIOSCSI_VQ_EVENT 1
62 1.10 jdolecek #define VIOSCSI_VQ_REQUEST 2
63 1.10 jdolecek
64 1.1 christos struct vioscsi_req *sc_reqs;
65 1.9 jdolecek int sc_nreqs;
66 1.1 christos bus_dma_segment_t sc_reqs_segs[1];
67 1.1 christos
68 1.1 christos u_int32_t sc_seg_max;
69 1.1 christos };
70 1.1 christos
71 1.2 christos /*
72 1.2 christos * Each block request uses at least two segments - one for the header
73 1.2 christos * and one for the status.
74 1.2 christos */
75 1.2 christos #define VIRTIO_SCSI_MIN_SEGMENTS 2
76 1.2 christos
77 1.1 christos static int vioscsi_match(device_t, cfdata_t, void *);
78 1.1 christos static void vioscsi_attach(device_t, device_t, void *);
79 1.1 christos
80 1.1 christos static int vioscsi_alloc_reqs(struct vioscsi_softc *,
81 1.1 christos struct virtio_softc *, int, uint32_t);
82 1.1 christos static void vioscsi_scsipi_request(struct scsipi_channel *,
83 1.1 christos scsipi_adapter_req_t, void *);
84 1.1 christos static int vioscsi_vq_done(struct virtqueue *);
85 1.1 christos static void vioscsi_req_done(struct vioscsi_softc *, struct virtio_softc *,
86 1.1 christos struct vioscsi_req *);
87 1.1 christos static struct vioscsi_req *vioscsi_req_get(struct vioscsi_softc *);
88 1.1 christos
89 1.1 christos static const char *const vioscsi_vq_names[] = {
90 1.1 christos "control",
91 1.1 christos "event",
92 1.1 christos "request",
93 1.1 christos };
94 1.1 christos
95 1.1 christos CFATTACH_DECL_NEW(vioscsi, sizeof(struct vioscsi_softc),
96 1.1 christos vioscsi_match, vioscsi_attach, NULL, NULL);
97 1.1 christos
98 1.1 christos static int
99 1.1 christos vioscsi_match(device_t parent, cfdata_t match, void *aux)
100 1.1 christos {
101 1.1 christos struct virtio_softc *va = aux;
102 1.1 christos
103 1.1 christos if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_SCSI)
104 1.1 christos return 1;
105 1.1 christos return 0;
106 1.1 christos }
107 1.1 christos
108 1.1 christos static void
109 1.1 christos vioscsi_attach(device_t parent, device_t self, void *aux)
110 1.1 christos {
111 1.1 christos struct vioscsi_softc *sc = device_private(self);
112 1.1 christos struct virtio_softc *vsc = device_private(parent);
113 1.1 christos struct scsipi_adapter *adapt = &sc->sc_adapter;
114 1.1 christos struct scsipi_channel *chan = &sc->sc_channel;
115 1.1 christos uint32_t features;
116 1.1 christos char buf[256];
117 1.1 christos int rv;
118 1.1 christos
119 1.1 christos if (vsc->sc_child != NULL) {
120 1.1 christos aprint_error(": parent %s already has a child\n",
121 1.1 christos device_xname(parent));
122 1.1 christos return;
123 1.1 christos }
124 1.1 christos
125 1.1 christos sc->sc_dev = self;
126 1.1 christos
127 1.1 christos vsc->sc_child = self;
128 1.1 christos vsc->sc_ipl = IPL_BIO;
129 1.1 christos vsc->sc_vqs = sc->sc_vqs;
130 1.1 christos vsc->sc_nvqs = __arraycount(sc->sc_vqs);
131 1.1 christos vsc->sc_config_change = NULL;
132 1.1 christos vsc->sc_intrhand = virtio_vq_intr;
133 1.1 christos vsc->sc_flags = 0;
134 1.1 christos
135 1.11 jdolecek vsc->sc_flags |= VIRTIO_F_PCI_INTR_MSIX;
136 1.11 jdolecek
137 1.1 christos features = virtio_negotiate_features(vsc, 0);
138 1.1 christos snprintb(buf, sizeof(buf), VIRTIO_COMMON_FLAG_BITS, features);
139 1.1 christos aprint_normal(": Features: %s\n", buf);
140 1.1 christos aprint_naive("\n");
141 1.1 christos
142 1.1 christos uint32_t cmd_per_lun = virtio_read_device_config_4(vsc,
143 1.1 christos VIRTIO_SCSI_CONFIG_CMD_PER_LUN);
144 1.1 christos
145 1.1 christos uint32_t seg_max = virtio_read_device_config_4(vsc,
146 1.1 christos VIRTIO_SCSI_CONFIG_SEG_MAX);
147 1.1 christos
148 1.1 christos uint16_t max_target = virtio_read_device_config_2(vsc,
149 1.1 christos VIRTIO_SCSI_CONFIG_MAX_TARGET);
150 1.1 christos
151 1.1 christos uint16_t max_channel = virtio_read_device_config_2(vsc,
152 1.1 christos VIRTIO_SCSI_CONFIG_MAX_CHANNEL);
153 1.1 christos
154 1.1 christos uint32_t max_lun = virtio_read_device_config_4(vsc,
155 1.1 christos VIRTIO_SCSI_CONFIG_MAX_LUN);
156 1.1 christos
157 1.1 christos sc->sc_seg_max = seg_max;
158 1.1 christos
159 1.1 christos for (size_t i = 0; i < __arraycount(sc->sc_vqs); i++) {
160 1.1 christos rv = virtio_alloc_vq(vsc, &sc->sc_vqs[i], i, MAXPHYS,
161 1.1 christos 1 + howmany(MAXPHYS, NBPG), vioscsi_vq_names[i]);
162 1.1 christos if (rv) {
163 1.1 christos aprint_error_dev(sc->sc_dev,
164 1.1 christos "failed to allocate virtqueue %zu\n", i);
165 1.1 christos return;
166 1.1 christos }
167 1.10 jdolecek
168 1.10 jdolecek if (i == VIOSCSI_VQ_REQUEST)
169 1.10 jdolecek sc->sc_vqs[i].vq_done = vioscsi_vq_done;
170 1.1 christos }
171 1.1 christos
172 1.10 jdolecek int qsize = sc->sc_vqs[VIOSCSI_VQ_REQUEST].vq_num;
173 1.1 christos aprint_normal_dev(sc->sc_dev, "qsize %d\n", qsize);
174 1.1 christos if (vioscsi_alloc_reqs(sc, vsc, qsize, seg_max))
175 1.1 christos return;
176 1.1 christos
177 1.10 jdolecek virtio_start_vq_intr(vsc, &sc->sc_vqs[VIOSCSI_VQ_REQUEST]);
178 1.10 jdolecek
179 1.1 christos /*
180 1.1 christos * Fill in the scsipi_adapter.
181 1.1 christos */
182 1.1 christos memset(adapt, 0, sizeof(*adapt));
183 1.1 christos adapt->adapt_dev = sc->sc_dev;
184 1.1 christos adapt->adapt_nchannels = max_channel;
185 1.1 christos adapt->adapt_openings = cmd_per_lun;
186 1.1 christos adapt->adapt_max_periph = adapt->adapt_openings;
187 1.1 christos adapt->adapt_request = vioscsi_scsipi_request;
188 1.1 christos adapt->adapt_minphys = minphys;
189 1.1 christos
190 1.1 christos /*
191 1.1 christos * Fill in the scsipi_channel.
192 1.1 christos */
193 1.1 christos memset(chan, 0, sizeof(*chan));
194 1.1 christos chan->chan_adapter = adapt;
195 1.1 christos chan->chan_bustype = &scsi_bustype;
196 1.1 christos chan->chan_channel = 0;
197 1.11 jdolecek chan->chan_ntargets = max_target + 1;
198 1.11 jdolecek chan->chan_nluns = max_lun + 1;
199 1.3 christos chan->chan_id = 0;
200 1.6 pooka chan->chan_flags = SCSIPI_CHAN_NOSETTLE;
201 1.1 christos
202 1.1 christos config_found(sc->sc_dev, &sc->sc_channel, scsiprint);
203 1.1 christos }
204 1.1 christos
205 1.1 christos #define XS2DMA(xs) \
206 1.1 christos ((((xs)->xs_control & XS_CTL_DATA_IN) ? BUS_DMA_READ : BUS_DMA_WRITE) | \
207 1.1 christos (((xs)->xs_control & XS_CTL_NOSLEEP) ? BUS_DMA_NOWAIT : BUS_DMA_WAITOK) | \
208 1.1 christos BUS_DMA_STREAMING)
209 1.1 christos
210 1.1 christos #define XS2DMAPRE(xs) (((xs)->xs_control & XS_CTL_DATA_IN) ? \
211 1.1 christos BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE)
212 1.1 christos
213 1.1 christos #define XS2DMAPOST(xs) (((xs)->xs_control & XS_CTL_DATA_IN) ? \
214 1.1 christos BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE)
215 1.1 christos
216 1.1 christos static void
217 1.1 christos vioscsi_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t
218 1.1 christos request, void *arg)
219 1.1 christos {
220 1.1 christos struct vioscsi_softc *sc =
221 1.1 christos device_private(chan->chan_adapter->adapt_dev);
222 1.1 christos struct virtio_softc *vsc = device_private(device_parent(sc->sc_dev));
223 1.1 christos struct scsipi_xfer *xs;
224 1.1 christos struct scsipi_periph *periph;
225 1.1 christos struct vioscsi_req *vr;
226 1.1 christos struct virtio_scsi_req_hdr *req;
227 1.11 jdolecek struct virtqueue *vq = &sc->sc_vqs[VIOSCSI_VQ_REQUEST];
228 1.1 christos int slot, error;
229 1.1 christos
230 1.1 christos DPRINTF(("%s: enter\n", __func__));
231 1.1 christos
232 1.8 jdolecek switch (request) {
233 1.8 jdolecek case ADAPTER_REQ_RUN_XFER:
234 1.8 jdolecek break;
235 1.8 jdolecek case ADAPTER_REQ_SET_XFER_MODE:
236 1.8 jdolecek {
237 1.8 jdolecek struct scsipi_xfer_mode *xm = arg;
238 1.8 jdolecek xm->xm_mode = PERIPH_CAP_TQING;
239 1.8 jdolecek xm->xm_period = 0;
240 1.8 jdolecek xm->xm_offset = 0;
241 1.8 jdolecek scsipi_async_event(chan, ASYNC_EVENT_XFER_MODE, xm);
242 1.8 jdolecek return;
243 1.8 jdolecek }
244 1.8 jdolecek default:
245 1.1 christos DPRINTF(("%s: unhandled %d\n", __func__, request));
246 1.1 christos return;
247 1.1 christos }
248 1.1 christos
249 1.1 christos xs = arg;
250 1.1 christos periph = xs->xs_periph;
251 1.1 christos
252 1.1 christos /*
253 1.7 jdolecek * This can happen when we run out of queue slots.
254 1.1 christos */
255 1.10 jdolecek vr = vioscsi_req_get(sc);
256 1.1 christos if (vr == NULL) {
257 1.7 jdolecek xs->error = XS_RESOURCE_SHORTAGE;
258 1.7 jdolecek scsipi_done(xs);
259 1.7 jdolecek return;
260 1.1 christos }
261 1.7 jdolecek
262 1.1 christos req = &vr->vr_req;
263 1.1 christos slot = vr - sc->sc_reqs;
264 1.1 christos
265 1.1 christos vr->vr_xs = xs;
266 1.1 christos
267 1.1 christos /*
268 1.1 christos * "The only supported format for the LUN field is: first byte set to
269 1.1 christos * 1, second byte set to target, third and fourth byte representing a
270 1.1 christos * single level LUN structure, followed by four zero bytes."
271 1.1 christos */
272 1.1 christos if (periph->periph_target >= 256 || periph->periph_lun >= 16384) {
273 1.1 christos DPRINTF(("%s: bad target %u or lun %u\n", __func__,
274 1.1 christos periph->periph_target, periph->periph_lun));
275 1.1 christos goto stuffup;
276 1.1 christos }
277 1.1 christos req->lun[0] = 1;
278 1.2 christos req->lun[1] = periph->periph_target - 1;
279 1.10 jdolecek req->lun[2] = 0x40 | ((periph->periph_lun >> 8) & 0x3F);
280 1.10 jdolecek req->lun[3] = periph->periph_lun & 0xFF;
281 1.1 christos memset(req->lun + 4, 0, 4);
282 1.2 christos DPRINTF(("%s: command for %u:%u at slot %d\n", __func__,
283 1.2 christos periph->periph_target - 1, periph->periph_lun, slot));
284 1.1 christos
285 1.8 jdolecek /* tag */
286 1.8 jdolecek switch (XS_CTL_TAGTYPE(xs)) {
287 1.8 jdolecek case XS_CTL_HEAD_TAG:
288 1.8 jdolecek req->task_attr = VIRTIO_SCSI_S_HEAD;
289 1.8 jdolecek break;
290 1.8 jdolecek
291 1.8 jdolecek #if 0 /* XXX */
292 1.8 jdolecek case XS_CTL_ACA_TAG:
293 1.8 jdolecek req->task_attr = VIRTIO_SCSI_S_ACA;
294 1.8 jdolecek break;
295 1.8 jdolecek #endif
296 1.8 jdolecek
297 1.8 jdolecek case XS_CTL_ORDERED_TAG:
298 1.8 jdolecek req->task_attr = VIRTIO_SCSI_S_ORDERED;
299 1.8 jdolecek break;
300 1.8 jdolecek
301 1.8 jdolecek case XS_CTL_SIMPLE_TAG:
302 1.8 jdolecek default:
303 1.8 jdolecek req->task_attr = VIRTIO_SCSI_S_SIMPLE;
304 1.8 jdolecek break;
305 1.8 jdolecek }
306 1.10 jdolecek req->id = slot;
307 1.8 jdolecek
308 1.2 christos if ((size_t)xs->cmdlen > sizeof(req->cdb)) {
309 1.2 christos DPRINTF(("%s: bad cmdlen %zu > %zu\n", __func__,
310 1.2 christos (size_t)xs->cmdlen, sizeof(req->cdb)));
311 1.1 christos goto stuffup;
312 1.2 christos }
313 1.2 christos
314 1.1 christos memset(req->cdb, 0, sizeof(req->cdb));
315 1.1 christos memcpy(req->cdb, xs->cmd, xs->cmdlen);
316 1.1 christos
317 1.1 christos error = bus_dmamap_load(vsc->sc_dmat, vr->vr_data,
318 1.1 christos xs->data, xs->datalen, NULL, XS2DMA(xs));
319 1.1 christos switch (error) {
320 1.1 christos case 0:
321 1.1 christos break;
322 1.1 christos case ENOMEM:
323 1.1 christos case EAGAIN:
324 1.1 christos xs->error = XS_RESOURCE_SHORTAGE;
325 1.1 christos goto nomore;
326 1.1 christos default:
327 1.1 christos aprint_error_dev(sc->sc_dev, "error %d loading DMA map\n",
328 1.1 christos error);
329 1.11 jdolecek stuffup:
330 1.1 christos xs->error = XS_DRIVER_STUFFUP;
331 1.1 christos nomore:
332 1.11 jdolecek /* nothing else to free */
333 1.1 christos scsipi_done(xs);
334 1.1 christos return;
335 1.1 christos }
336 1.1 christos
337 1.2 christos int nsegs = VIRTIO_SCSI_MIN_SEGMENTS;
338 1.2 christos if ((xs->xs_control & (XS_CTL_DATA_IN|XS_CTL_DATA_OUT)) != 0)
339 1.2 christos nsegs += vr->vr_data->dm_nsegs;
340 1.2 christos
341 1.2 christos error = virtio_enqueue_reserve(vsc, vq, slot, nsegs);
342 1.1 christos if (error) {
343 1.1 christos DPRINTF(("%s: error reserving %d\n", __func__, error));
344 1.11 jdolecek bus_dmamap_unload(vsc->sc_dmat, vr->vr_data);
345 1.11 jdolecek xs->error = XS_RESOURCE_SHORTAGE;
346 1.11 jdolecek goto nomore;
347 1.1 christos }
348 1.1 christos
349 1.1 christos bus_dmamap_sync(vsc->sc_dmat, vr->vr_control,
350 1.1 christos offsetof(struct vioscsi_req, vr_req),
351 1.1 christos sizeof(struct virtio_scsi_req_hdr),
352 1.1 christos BUS_DMASYNC_PREWRITE);
353 1.1 christos bus_dmamap_sync(vsc->sc_dmat, vr->vr_control,
354 1.1 christos offsetof(struct vioscsi_req, vr_res),
355 1.1 christos sizeof(struct virtio_scsi_res_hdr),
356 1.1 christos BUS_DMASYNC_PREREAD);
357 1.2 christos if ((xs->xs_control & (XS_CTL_DATA_IN|XS_CTL_DATA_OUT)) != 0)
358 1.2 christos bus_dmamap_sync(vsc->sc_dmat, vr->vr_data, 0, xs->datalen,
359 1.2 christos XS2DMAPRE(xs));
360 1.1 christos
361 1.1 christos virtio_enqueue_p(vsc, vq, slot, vr->vr_control,
362 1.1 christos offsetof(struct vioscsi_req, vr_req),
363 1.1 christos sizeof(struct virtio_scsi_req_hdr), 1);
364 1.1 christos if (xs->xs_control & XS_CTL_DATA_OUT)
365 1.1 christos virtio_enqueue(vsc, vq, slot, vr->vr_data, 1);
366 1.1 christos virtio_enqueue_p(vsc, vq, slot, vr->vr_control,
367 1.1 christos offsetof(struct vioscsi_req, vr_res),
368 1.1 christos sizeof(struct virtio_scsi_res_hdr), 0);
369 1.1 christos if (xs->xs_control & XS_CTL_DATA_IN)
370 1.1 christos virtio_enqueue(vsc, vq, slot, vr->vr_data, 0);
371 1.1 christos virtio_enqueue_commit(vsc, vq, slot, 1);
372 1.1 christos
373 1.1 christos if ((xs->xs_control & XS_CTL_POLL) == 0)
374 1.1 christos return;
375 1.1 christos
376 1.1 christos DPRINTF(("%s: polling...\n", __func__));
377 1.1 christos // XXX: do this better.
378 1.1 christos int timeout = 1000;
379 1.1 christos do {
380 1.1 christos (*vsc->sc_intrhand)(vsc);
381 1.1 christos if (vr->vr_xs != xs)
382 1.1 christos break;
383 1.1 christos delay(1000);
384 1.1 christos } while (--timeout > 0);
385 1.1 christos
386 1.1 christos if (vr->vr_xs == xs) {
387 1.1 christos // XXX: Abort!
388 1.1 christos xs->error = XS_TIMEOUT;
389 1.1 christos xs->resid = xs->datalen;
390 1.1 christos DPRINTF(("%s: polling timeout\n", __func__));
391 1.1 christos scsipi_done(xs);
392 1.1 christos }
393 1.1 christos DPRINTF(("%s: done (timeout=%d)\n", __func__, timeout));
394 1.1 christos }
395 1.1 christos
396 1.1 christos static void
397 1.1 christos vioscsi_req_done(struct vioscsi_softc *sc, struct virtio_softc *vsc,
398 1.1 christos struct vioscsi_req *vr)
399 1.1 christos {
400 1.1 christos struct scsipi_xfer *xs = vr->vr_xs;
401 1.4 christos struct scsi_sense_data *sense = &xs->sense.scsi_sense;
402 1.4 christos size_t sense_len;
403 1.1 christos
404 1.1 christos DPRINTF(("%s: enter\n", __func__));
405 1.1 christos
406 1.1 christos bus_dmamap_sync(vsc->sc_dmat, vr->vr_control,
407 1.1 christos offsetof(struct vioscsi_req, vr_req),
408 1.1 christos sizeof(struct virtio_scsi_req_hdr),
409 1.1 christos BUS_DMASYNC_POSTWRITE);
410 1.1 christos bus_dmamap_sync(vsc->sc_dmat, vr->vr_control,
411 1.1 christos offsetof(struct vioscsi_req, vr_res),
412 1.1 christos sizeof(struct virtio_scsi_res_hdr),
413 1.1 christos BUS_DMASYNC_POSTREAD);
414 1.1 christos bus_dmamap_sync(vsc->sc_dmat, vr->vr_data, 0, xs->datalen,
415 1.1 christos XS2DMAPOST(xs));
416 1.1 christos
417 1.11 jdolecek xs->status = vr->vr_res.status;
418 1.11 jdolecek xs->resid = vr->vr_res.residual;
419 1.11 jdolecek
420 1.4 christos switch (vr->vr_res.response) {
421 1.4 christos case VIRTIO_SCSI_S_OK:
422 1.4 christos sense_len = MIN(sizeof(xs->sense), vr->vr_res.sense_len);
423 1.4 christos memcpy(&xs->sense, vr->vr_res.sense, sense_len);
424 1.4 christos xs->error = (sense_len == 0) ? XS_NOERROR : XS_SENSE;
425 1.4 christos break;
426 1.4 christos case VIRTIO_SCSI_S_BAD_TARGET:
427 1.4 christos DPRINTF(("%s: bad target\n", __func__));
428 1.4 christos memset(sense, 0, sizeof(*sense));
429 1.4 christos sense->response_code = 0x70;
430 1.4 christos sense->flags = SKEY_ILLEGAL_REQUEST;
431 1.4 christos xs->error = XS_SENSE;
432 1.4 christos xs->status = 0;
433 1.4 christos xs->resid = 0;
434 1.4 christos break;
435 1.4 christos default:
436 1.4 christos DPRINTF(("%s: stuffup: %d\n", __func__, vr->vr_res.response));
437 1.1 christos xs->error = XS_DRIVER_STUFFUP;
438 1.1 christos xs->resid = xs->datalen;
439 1.4 christos break;
440 1.1 christos }
441 1.1 christos
442 1.1 christos DPRINTF(("%s: done %d, %d, %d\n", __func__,
443 1.1 christos xs->error, xs->status, xs->resid));
444 1.1 christos
445 1.11 jdolecek bus_dmamap_unload(vsc->sc_dmat, vr->vr_data);
446 1.1 christos vr->vr_xs = NULL;
447 1.11 jdolecek
448 1.1 christos scsipi_done(xs);
449 1.1 christos }
450 1.1 christos
451 1.1 christos static int
452 1.1 christos vioscsi_vq_done(struct virtqueue *vq)
453 1.1 christos {
454 1.1 christos struct virtio_softc *vsc = vq->vq_owner;
455 1.1 christos struct vioscsi_softc *sc = device_private(vsc->sc_child);
456 1.1 christos int ret = 0;
457 1.1 christos
458 1.1 christos DPRINTF(("%s: enter\n", __func__));
459 1.1 christos
460 1.1 christos for (;;) {
461 1.1 christos int r, slot;
462 1.1 christos r = virtio_dequeue(vsc, vq, &slot, NULL);
463 1.1 christos if (r != 0)
464 1.1 christos break;
465 1.1 christos
466 1.1 christos DPRINTF(("%s: slot=%d\n", __func__, slot));
467 1.11 jdolecek
468 1.1 christos vioscsi_req_done(sc, vsc, &sc->sc_reqs[slot]);
469 1.11 jdolecek
470 1.11 jdolecek virtio_dequeue_commit(vsc, vq, slot);
471 1.11 jdolecek
472 1.1 christos ret = 1;
473 1.1 christos }
474 1.1 christos
475 1.1 christos DPRINTF(("%s: exit %d\n", __func__, ret));
476 1.1 christos
477 1.1 christos return ret;
478 1.1 christos }
479 1.1 christos
480 1.1 christos static struct vioscsi_req *
481 1.1 christos vioscsi_req_get(struct vioscsi_softc *sc)
482 1.1 christos {
483 1.1 christos struct virtio_softc *vsc = device_private(device_parent(sc->sc_dev));
484 1.10 jdolecek struct virtqueue *vq = &sc->sc_vqs[VIOSCSI_VQ_REQUEST];
485 1.1 christos struct vioscsi_req *vr;
486 1.1 christos int r, slot;
487 1.1 christos
488 1.1 christos if ((r = virtio_enqueue_prep(vsc, vq, &slot)) != 0) {
489 1.1 christos DPRINTF(("%s: virtio_enqueue_get error %d\n", __func__, r));
490 1.9 jdolecek return NULL;
491 1.1 christos }
492 1.9 jdolecek KASSERT(slot < sc->sc_nreqs);
493 1.1 christos vr = &sc->sc_reqs[slot];
494 1.1 christos
495 1.1 christos DPRINTF(("%s: %p, %d\n", __func__, vr, slot));
496 1.1 christos
497 1.1 christos return vr;
498 1.1 christos }
499 1.1 christos
500 1.1 christos int
501 1.1 christos vioscsi_alloc_reqs(struct vioscsi_softc *sc, struct virtio_softc *vsc,
502 1.1 christos int qsize, uint32_t seg_max)
503 1.1 christos {
504 1.1 christos size_t allocsize;
505 1.9 jdolecek int r, rsegs, slot;
506 1.1 christos void *vaddr;
507 1.9 jdolecek struct vioscsi_req *vr;
508 1.1 christos
509 1.1 christos allocsize = qsize * sizeof(struct vioscsi_req);
510 1.1 christos r = bus_dmamem_alloc(vsc->sc_dmat, allocsize, 0, 0,
511 1.1 christos &sc->sc_reqs_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
512 1.1 christos if (r != 0) {
513 1.1 christos aprint_error_dev(sc->sc_dev,
514 1.1 christos "%s: bus_dmamem_alloc, size %zu, error %d\n", __func__,
515 1.1 christos allocsize, r);
516 1.9 jdolecek return r;
517 1.1 christos }
518 1.1 christos r = bus_dmamem_map(vsc->sc_dmat, &sc->sc_reqs_segs[0], 1,
519 1.1 christos allocsize, &vaddr, BUS_DMA_NOWAIT);
520 1.1 christos if (r != 0) {
521 1.1 christos aprint_error_dev(sc->sc_dev,
522 1.1 christos "%s: bus_dmamem_map failed, error %d\n", __func__, r);
523 1.1 christos bus_dmamem_free(vsc->sc_dmat, &sc->sc_reqs_segs[0], 1);
524 1.9 jdolecek return r;
525 1.1 christos }
526 1.9 jdolecek memset(vaddr, 0, allocsize);
527 1.9 jdolecek
528 1.1 christos sc->sc_reqs = vaddr;
529 1.9 jdolecek sc->sc_nreqs = qsize;
530 1.9 jdolecek
531 1.9 jdolecek /* Prepare maps for the requests */
532 1.9 jdolecek for (slot=0; slot < qsize; slot++) {
533 1.9 jdolecek vr = &sc->sc_reqs[slot];
534 1.9 jdolecek
535 1.9 jdolecek r = bus_dmamap_create(vsc->sc_dmat,
536 1.9 jdolecek offsetof(struct vioscsi_req, vr_xs), 1,
537 1.9 jdolecek offsetof(struct vioscsi_req, vr_xs), 0,
538 1.9 jdolecek BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &vr->vr_control);
539 1.9 jdolecek if (r != 0) {
540 1.9 jdolecek aprint_error_dev(sc->sc_dev,
541 1.9 jdolecek "%s: bus_dmamem_create failed, error %d\n",
542 1.9 jdolecek __func__, r);
543 1.9 jdolecek goto cleanup;
544 1.9 jdolecek }
545 1.9 jdolecek
546 1.9 jdolecek r = bus_dmamap_create(vsc->sc_dmat, MAXPHYS, sc->sc_seg_max,
547 1.9 jdolecek MAXPHYS, 0, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW, &vr->vr_data);
548 1.9 jdolecek if (r != 0) {
549 1.9 jdolecek aprint_error_dev(sc->sc_dev,
550 1.9 jdolecek "%s: bus_dmamem_map failed, error %d\n",
551 1.9 jdolecek __func__, r);
552 1.9 jdolecek goto cleanup;
553 1.9 jdolecek }
554 1.9 jdolecek
555 1.9 jdolecek r = bus_dmamap_load(vsc->sc_dmat, vr->vr_control,
556 1.9 jdolecek vr, offsetof(struct vioscsi_req, vr_xs), NULL,
557 1.9 jdolecek BUS_DMA_NOWAIT);
558 1.9 jdolecek if (r != 0) {
559 1.9 jdolecek aprint_error_dev(sc->sc_dev,
560 1.9 jdolecek "%s: bus_dmamap_create ctrl error %d\n",
561 1.9 jdolecek __func__, r);
562 1.9 jdolecek goto cleanup;
563 1.9 jdolecek }
564 1.9 jdolecek }
565 1.9 jdolecek
566 1.1 christos return 0;
567 1.9 jdolecek
568 1.9 jdolecek cleanup:
569 1.9 jdolecek for (; slot > 0; slot--) {
570 1.9 jdolecek vr = &sc->sc_reqs[slot];
571 1.9 jdolecek
572 1.9 jdolecek if (vr->vr_control) {
573 1.9 jdolecek /* this will also unload the mapping if loaded */
574 1.9 jdolecek bus_dmamap_destroy(vsc->sc_dmat, vr->vr_control);
575 1.9 jdolecek vr->vr_control = NULL;
576 1.9 jdolecek }
577 1.9 jdolecek
578 1.9 jdolecek if (vr->vr_data) {
579 1.9 jdolecek bus_dmamap_destroy(vsc->sc_dmat, vr->vr_data);
580 1.9 jdolecek vr->vr_data = NULL;
581 1.9 jdolecek }
582 1.9 jdolecek }
583 1.9 jdolecek
584 1.9 jdolecek bus_dmamem_unmap(vsc->sc_dmat, vaddr, allocsize);
585 1.9 jdolecek bus_dmamem_free(vsc->sc_dmat, &sc->sc_reqs_segs[0], 1);
586 1.9 jdolecek
587 1.9 jdolecek return r;
588 1.1 christos }
589