virtio.c revision 1.68 1 /* $NetBSD: virtio.c,v 1.68 2023/03/24 13:32:19 yamaguchi Exp $ */
2
3 /*
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * Copyright (c) 2012 Stefan Fritsch, Alexander Fiveg.
6 * Copyright (c) 2010 Minoura Makoto.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __KERNEL_RCSID(0, "$NetBSD: virtio.c,v 1.68 2023/03/24 13:32:19 yamaguchi Exp $");
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/atomic.h>
37 #include <sys/bus.h>
38 #include <sys/device.h>
39 #include <sys/kmem.h>
40 #include <sys/module.h>
41
42 #define VIRTIO_PRIVATE
43
44 #include <dev/pci/virtioreg.h> /* XXX: move to non-pci */
45 #include <dev/pci/virtiovar.h> /* XXX: move to non-pci */
46
47 #define MINSEG_INDIRECT 2 /* use indirect if nsegs >= this value */
48
49 /* incomplete list */
50 static const char *virtio_device_name[] = {
51 "unknown (0)", /* 0 */
52 "network", /* 1 */
53 "block", /* 2 */
54 "console", /* 3 */
55 "entropy", /* 4 */
56 "memory balloon", /* 5 */
57 "I/O memory", /* 6 */
58 "remote processor messaging", /* 7 */
59 "SCSI", /* 8 */
60 "9P transport", /* 9 */
61 };
62 #define NDEVNAMES __arraycount(virtio_device_name)
63
64 static void virtio_reset_vq(struct virtio_softc *,
65 struct virtqueue *);
66
67 void
68 virtio_set_status(struct virtio_softc *sc, int status)
69 {
70 sc->sc_ops->set_status(sc, status);
71 }
72
73 /*
74 * Reset the device.
75 */
76 /*
77 * To reset the device to a known state, do following:
78 * virtio_reset(sc); // this will stop the device activity
79 * <dequeue finished requests>; // virtio_dequeue() still can be called
80 * <revoke pending requests in the vqs if any>;
81 * virtio_reinit_start(sc); // dequeue prohibitted
82 * newfeatures = virtio_negotiate_features(sc, requestedfeatures);
83 * <some other initialization>;
84 * virtio_reinit_end(sc); // device activated; enqueue allowed
85 * Once attached, feature negotiation can only be allowed after virtio_reset.
86 */
87 void
88 virtio_reset(struct virtio_softc *sc)
89 {
90 virtio_device_reset(sc);
91 }
92
93 int
94 virtio_reinit_start(struct virtio_softc *sc)
95 {
96 int i, r;
97
98 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_ACK);
99 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER);
100 for (i = 0; i < sc->sc_nvqs; i++) {
101 int n;
102 struct virtqueue *vq = &sc->sc_vqs[i];
103 n = sc->sc_ops->read_queue_size(sc, vq->vq_index);
104 if (n == 0) /* vq disappeared */
105 continue;
106 if (n != vq->vq_num) {
107 panic("%s: virtqueue size changed, vq index %d\n",
108 device_xname(sc->sc_dev),
109 vq->vq_index);
110 }
111 virtio_reset_vq(sc, vq);
112 sc->sc_ops->setup_queue(sc, vq->vq_index,
113 vq->vq_dmamap->dm_segs[0].ds_addr);
114 }
115
116 r = sc->sc_ops->setup_interrupts(sc, 1);
117 if (r != 0)
118 goto fail;
119
120 return 0;
121
122 fail:
123 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
124
125 return 1;
126 }
127
128 void
129 virtio_reinit_end(struct virtio_softc *sc)
130 {
131 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
132 }
133
134 /*
135 * Feature negotiation.
136 */
137 void
138 virtio_negotiate_features(struct virtio_softc *sc, uint64_t guest_features)
139 {
140 if (!(device_cfdata(sc->sc_dev)->cf_flags & 1) &&
141 !(device_cfdata(sc->sc_child)->cf_flags & 1)) /* XXX */
142 guest_features |= VIRTIO_F_RING_INDIRECT_DESC;
143 sc->sc_ops->neg_features(sc, guest_features);
144 if (sc->sc_active_features & VIRTIO_F_RING_INDIRECT_DESC)
145 sc->sc_indirect = true;
146 else
147 sc->sc_indirect = false;
148 }
149
150
151 /*
152 * Device configuration registers readers/writers
153 */
154 #if 0
155 #define DPRINTFR(n, fmt, val, index, num) \
156 printf("\n%s (", n); \
157 for (int i = 0; i < num; i++) \
158 printf("%02x ", bus_space_read_1(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index+i)); \
159 printf(") -> "); printf(fmt, val); printf("\n");
160 #define DPRINTFR2(n, fmt, val_s, val_n) \
161 printf("%s ", n); \
162 printf("\n stream "); printf(fmt, val_s); printf(" norm "); printf(fmt, val_n); printf("\n");
163 #else
164 #define DPRINTFR(n, fmt, val, index, num)
165 #define DPRINTFR2(n, fmt, val_s, val_n)
166 #endif
167
168
169 uint8_t
170 virtio_read_device_config_1(struct virtio_softc *sc, int index)
171 {
172 bus_space_tag_t iot = sc->sc_devcfg_iot;
173 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
174 uint8_t val;
175
176 val = bus_space_read_1(iot, ioh, index);
177
178 DPRINTFR("read_1", "%02x", val, index, 1);
179 return val;
180 }
181
182 uint16_t
183 virtio_read_device_config_2(struct virtio_softc *sc, int index)
184 {
185 bus_space_tag_t iot = sc->sc_devcfg_iot;
186 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
187 uint16_t val;
188
189 val = bus_space_read_2(iot, ioh, index);
190 if (BYTE_ORDER != sc->sc_bus_endian)
191 val = bswap16(val);
192
193 DPRINTFR("read_2", "%04x", val, index, 2);
194 DPRINTFR2("read_2", "%04x",
195 bus_space_read_stream_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
196 index),
197 bus_space_read_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
198 return val;
199 }
200
201 uint32_t
202 virtio_read_device_config_4(struct virtio_softc *sc, int index)
203 {
204 bus_space_tag_t iot = sc->sc_devcfg_iot;
205 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
206 uint32_t val;
207
208 val = bus_space_read_4(iot, ioh, index);
209 if (BYTE_ORDER != sc->sc_bus_endian)
210 val = bswap32(val);
211
212 DPRINTFR("read_4", "%08x", val, index, 4);
213 DPRINTFR2("read_4", "%08x",
214 bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
215 index),
216 bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
217 return val;
218 }
219
220 /*
221 * The Virtio spec explicitly tells that reading and writing 8 bytes are not
222 * considered atomic and no triggers may be connected to reading or writing
223 * it. We access it using two 32 reads. See virtio spec 4.1.3.1.
224 */
225 uint64_t
226 virtio_read_device_config_8(struct virtio_softc *sc, int index)
227 {
228 bus_space_tag_t iot = sc->sc_devcfg_iot;
229 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
230 union {
231 uint64_t u64;
232 uint32_t l[2];
233 } v;
234 uint64_t val;
235
236 v.l[0] = bus_space_read_4(iot, ioh, index);
237 v.l[1] = bus_space_read_4(iot, ioh, index + 4);
238 if (sc->sc_bus_endian != sc->sc_struct_endian) {
239 v.l[0] = bswap32(v.l[0]);
240 v.l[1] = bswap32(v.l[1]);
241 }
242 val = v.u64;
243
244 if (BYTE_ORDER != sc->sc_struct_endian)
245 val = bswap64(val);
246
247 DPRINTFR("read_8", "%08"PRIx64, val, index, 8);
248 DPRINTFR2("read_8 low ", "%08x",
249 bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
250 index),
251 bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index));
252 DPRINTFR2("read_8 high ", "%08x",
253 bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh,
254 index + 4),
255 bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, index + 4));
256 return val;
257 }
258
259 /*
260 * In the older virtio spec, device config registers are host endian. On newer
261 * they are little endian. Some newer devices however explicitly specify their
262 * register to always be little endian. These functions cater for these.
263 */
264 uint16_t
265 virtio_read_device_config_le_2(struct virtio_softc *sc, int index)
266 {
267 bus_space_tag_t iot = sc->sc_devcfg_iot;
268 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
269 uint16_t val;
270
271 val = bus_space_read_2(iot, ioh, index);
272 if (sc->sc_bus_endian != LITTLE_ENDIAN)
273 val = bswap16(val);
274
275 DPRINTFR("read_le_2", "%04x", val, index, 2);
276 DPRINTFR2("read_le_2", "%04x",
277 bus_space_read_stream_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0),
278 bus_space_read_2(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0));
279 return val;
280 }
281
282 uint32_t
283 virtio_read_device_config_le_4(struct virtio_softc *sc, int index)
284 {
285 bus_space_tag_t iot = sc->sc_devcfg_iot;
286 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
287 uint32_t val;
288
289 val = bus_space_read_4(iot, ioh, index);
290 if (sc->sc_bus_endian != LITTLE_ENDIAN)
291 val = bswap32(val);
292
293 DPRINTFR("read_le_4", "%08x", val, index, 4);
294 DPRINTFR2("read_le_4", "%08x",
295 bus_space_read_stream_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0),
296 bus_space_read_4(sc->sc_devcfg_iot, sc->sc_devcfg_ioh, 0));
297 return val;
298 }
299
300 void
301 virtio_write_device_config_1(struct virtio_softc *sc, int index, uint8_t value)
302 {
303 bus_space_tag_t iot = sc->sc_devcfg_iot;
304 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
305
306 bus_space_write_1(iot, ioh, index, value);
307 }
308
309 void
310 virtio_write_device_config_2(struct virtio_softc *sc, int index,
311 uint16_t value)
312 {
313 bus_space_tag_t iot = sc->sc_devcfg_iot;
314 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
315
316 if (BYTE_ORDER != sc->sc_bus_endian)
317 value = bswap16(value);
318 bus_space_write_2(iot, ioh, index, value);
319 }
320
321 void
322 virtio_write_device_config_4(struct virtio_softc *sc, int index,
323 uint32_t value)
324 {
325 bus_space_tag_t iot = sc->sc_devcfg_iot;
326 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
327
328 if (BYTE_ORDER != sc->sc_bus_endian)
329 value = bswap32(value);
330 bus_space_write_4(iot, ioh, index, value);
331 }
332
333 /*
334 * The Virtio spec explicitly tells that reading and writing 8 bytes are not
335 * considered atomic and no triggers may be connected to reading or writing
336 * it. We access it using two 32 bit writes. For good measure it is stated to
337 * always write lsb first just in case of a hypervisor bug. See See virtio
338 * spec 4.1.3.1.
339 */
340 void
341 virtio_write_device_config_8(struct virtio_softc *sc, int index,
342 uint64_t value)
343 {
344 bus_space_tag_t iot = sc->sc_devcfg_iot;
345 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
346 union {
347 uint64_t u64;
348 uint32_t l[2];
349 } v;
350
351 if (BYTE_ORDER != sc->sc_struct_endian)
352 value = bswap64(value);
353
354 v.u64 = value;
355 if (sc->sc_bus_endian != sc->sc_struct_endian) {
356 v.l[0] = bswap32(v.l[0]);
357 v.l[1] = bswap32(v.l[1]);
358 }
359
360 if (sc->sc_struct_endian == LITTLE_ENDIAN) {
361 bus_space_write_4(iot, ioh, index, v.l[0]);
362 bus_space_write_4(iot, ioh, index + 4, v.l[1]);
363 } else {
364 bus_space_write_4(iot, ioh, index + 4, v.l[1]);
365 bus_space_write_4(iot, ioh, index, v.l[0]);
366 }
367 }
368
369 /*
370 * In the older virtio spec, device config registers are host endian. On newer
371 * they are little endian. Some newer devices however explicitly specify their
372 * register to always be little endian. These functions cater for these.
373 */
374 void
375 virtio_write_device_config_le_2(struct virtio_softc *sc, int index,
376 uint16_t value)
377 {
378 bus_space_tag_t iot = sc->sc_devcfg_iot;
379 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
380
381 if (sc->sc_bus_endian != LITTLE_ENDIAN)
382 value = bswap16(value);
383 bus_space_write_2(iot, ioh, index, value);
384 }
385
386 void
387 virtio_write_device_config_le_4(struct virtio_softc *sc, int index,
388 uint32_t value)
389 {
390 bus_space_tag_t iot = sc->sc_devcfg_iot;
391 bus_space_handle_t ioh = sc->sc_devcfg_ioh;
392
393 if (sc->sc_bus_endian != LITTLE_ENDIAN)
394 value = bswap32(value);
395 bus_space_write_4(iot, ioh, index, value);
396 }
397
398
399 /*
400 * data structures endian helpers
401 */
402 uint16_t
403 virtio_rw16(struct virtio_softc *sc, uint16_t val)
404 {
405 KASSERT(sc);
406 return BYTE_ORDER != sc->sc_struct_endian ? bswap16(val) : val;
407 }
408
409 uint32_t
410 virtio_rw32(struct virtio_softc *sc, uint32_t val)
411 {
412 KASSERT(sc);
413 return BYTE_ORDER != sc->sc_struct_endian ? bswap32(val) : val;
414 }
415
416 uint64_t
417 virtio_rw64(struct virtio_softc *sc, uint64_t val)
418 {
419 KASSERT(sc);
420 return BYTE_ORDER != sc->sc_struct_endian ? bswap64(val) : val;
421 }
422
423
424 /*
425 * Interrupt handler.
426 */
427 static void
428 virtio_soft_intr(void *arg)
429 {
430 struct virtio_softc *sc = arg;
431
432 KASSERT(sc->sc_intrhand != NULL);
433
434 (*sc->sc_intrhand)(sc);
435 }
436
437 /* set to vq->vq_intrhand in virtio_init_vq_vqdone() */
438 static int
439 virtio_vq_done(void *xvq)
440 {
441 struct virtqueue *vq = xvq;
442
443 return vq->vq_done(vq);
444 }
445
446 static int
447 virtio_vq_intr(struct virtio_softc *sc)
448 {
449 struct virtqueue *vq;
450 int i, r = 0;
451
452 for (i = 0; i < sc->sc_nvqs; i++) {
453 vq = &sc->sc_vqs[i];
454 if (virtio_vq_is_enqueued(sc, vq) == 1) {
455 r |= (*vq->vq_intrhand)(vq->vq_intrhand_arg);
456 }
457 }
458
459 return r;
460 }
461
462 /*
463 * dmamap sync operations for a virtqueue.
464 */
465 static inline void
466 vq_sync_descs(struct virtio_softc *sc, struct virtqueue *vq, int ops)
467 {
468
469 /* availoffset == sizeof(vring_desc) * vq_num */
470 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap, 0, vq->vq_availoffset,
471 ops);
472 }
473
474 static inline void
475 vq_sync_aring_all(struct virtio_softc *sc, struct virtqueue *vq, int ops)
476 {
477 uint16_t hdrlen = offsetof(struct vring_avail, ring);
478 size_t payloadlen = vq->vq_num * sizeof(uint16_t);
479 size_t usedlen = 0;
480
481 if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX)
482 usedlen = sizeof(uint16_t);
483 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
484 vq->vq_availoffset, hdrlen + payloadlen + usedlen, ops);
485 }
486
487 static inline void
488 vq_sync_aring_header(struct virtio_softc *sc, struct virtqueue *vq, int ops)
489 {
490 uint16_t hdrlen = offsetof(struct vring_avail, ring);
491
492 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
493 vq->vq_availoffset, hdrlen, ops);
494 }
495
496 static inline void
497 vq_sync_aring_payload(struct virtio_softc *sc, struct virtqueue *vq, int ops)
498 {
499 uint16_t hdrlen = offsetof(struct vring_avail, ring);
500 size_t payloadlen = vq->vq_num * sizeof(uint16_t);
501
502 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
503 vq->vq_availoffset + hdrlen, payloadlen, ops);
504 }
505
506 static inline void
507 vq_sync_aring_used(struct virtio_softc *sc, struct virtqueue *vq, int ops)
508 {
509 uint16_t hdrlen = offsetof(struct vring_avail, ring);
510 size_t payloadlen = vq->vq_num * sizeof(uint16_t);
511 size_t usedlen = sizeof(uint16_t);
512
513 if ((sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) == 0)
514 return;
515 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
516 vq->vq_availoffset + hdrlen + payloadlen, usedlen, ops);
517 }
518
519 static inline void
520 vq_sync_uring_all(struct virtio_softc *sc, struct virtqueue *vq, int ops)
521 {
522 uint16_t hdrlen = offsetof(struct vring_used, ring);
523 size_t payloadlen = vq->vq_num * sizeof(struct vring_used_elem);
524 size_t availlen = 0;
525
526 if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX)
527 availlen = sizeof(uint16_t);
528 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
529 vq->vq_usedoffset, hdrlen + payloadlen + availlen, ops);
530 }
531
532 static inline void
533 vq_sync_uring_header(struct virtio_softc *sc, struct virtqueue *vq, int ops)
534 {
535 uint16_t hdrlen = offsetof(struct vring_used, ring);
536
537 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
538 vq->vq_usedoffset, hdrlen, ops);
539 }
540
541 static inline void
542 vq_sync_uring_payload(struct virtio_softc *sc, struct virtqueue *vq, int ops)
543 {
544 uint16_t hdrlen = offsetof(struct vring_used, ring);
545 size_t payloadlen = vq->vq_num * sizeof(struct vring_used_elem);
546
547 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
548 vq->vq_usedoffset + hdrlen, payloadlen, ops);
549 }
550
551 static inline void
552 vq_sync_uring_avail(struct virtio_softc *sc, struct virtqueue *vq, int ops)
553 {
554 uint16_t hdrlen = offsetof(struct vring_used, ring);
555 size_t payloadlen = vq->vq_num * sizeof(struct vring_used_elem);
556 size_t availlen = sizeof(uint16_t);
557
558 if ((sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) == 0)
559 return;
560 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
561 vq->vq_usedoffset + hdrlen + payloadlen, availlen, ops);
562 }
563
564 static inline void
565 vq_sync_indirect(struct virtio_softc *sc, struct virtqueue *vq, int slot,
566 int ops)
567 {
568 int offset = vq->vq_indirectoffset +
569 sizeof(struct vring_desc) * vq->vq_maxnsegs * slot;
570
571 bus_dmamap_sync(sc->sc_dmat, vq->vq_dmamap,
572 offset, sizeof(struct vring_desc) * vq->vq_maxnsegs, ops);
573 }
574
575 bool
576 virtio_vq_is_enqueued(struct virtio_softc *sc, struct virtqueue *vq)
577 {
578
579 if (vq->vq_queued) {
580 vq->vq_queued = 0;
581 vq_sync_aring_all(sc, vq, BUS_DMASYNC_POSTWRITE);
582 }
583
584 vq_sync_uring_header(sc, vq, BUS_DMASYNC_POSTREAD);
585 if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
586 return 0;
587 vq_sync_uring_payload(sc, vq, BUS_DMASYNC_POSTREAD);
588 return 1;
589 }
590
591 /*
592 * Increase the event index in order to delay interrupts.
593 */
594 int
595 virtio_postpone_intr(struct virtio_softc *sc, struct virtqueue *vq,
596 uint16_t nslots)
597 {
598 uint16_t idx, nused;
599
600 idx = vq->vq_used_idx + nslots;
601
602 /* set the new event index: avail_ring->used_event = idx */
603 *vq->vq_used_event = virtio_rw16(sc, idx);
604 vq_sync_aring_used(vq->vq_owner, vq, BUS_DMASYNC_PREWRITE);
605 vq->vq_queued++;
606
607 nused = (uint16_t)
608 (virtio_rw16(sc, vq->vq_used->idx) - vq->vq_used_idx);
609 KASSERT(nused <= vq->vq_num);
610
611 return nslots < nused;
612 }
613
614 /*
615 * Postpone interrupt until 3/4 of the available descriptors have been
616 * consumed.
617 */
618 int
619 virtio_postpone_intr_smart(struct virtio_softc *sc, struct virtqueue *vq)
620 {
621 uint16_t nslots;
622
623 nslots = (uint16_t)
624 (virtio_rw16(sc, vq->vq_avail->idx) - vq->vq_used_idx) * 3 / 4;
625
626 return virtio_postpone_intr(sc, vq, nslots);
627 }
628
629 /*
630 * Postpone interrupt until all of the available descriptors have been
631 * consumed.
632 */
633 int
634 virtio_postpone_intr_far(struct virtio_softc *sc, struct virtqueue *vq)
635 {
636 uint16_t nslots;
637
638 nslots = (uint16_t)
639 (virtio_rw16(sc, vq->vq_avail->idx) - vq->vq_used_idx);
640
641 return virtio_postpone_intr(sc, vq, nslots);
642 }
643
644 /*
645 * Start/stop vq interrupt. No guarantee.
646 */
647 void
648 virtio_stop_vq_intr(struct virtio_softc *sc, struct virtqueue *vq)
649 {
650
651 if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
652 /*
653 * No way to disable the interrupt completely with
654 * RingEventIdx. Instead advance used_event by half the
655 * possible value. This won't happen soon and is far enough in
656 * the past to not trigger a spurios interrupt.
657 */
658 *vq->vq_used_event = virtio_rw16(sc, vq->vq_used_idx + 0x8000);
659 vq_sync_aring_used(sc, vq, BUS_DMASYNC_PREWRITE);
660 } else {
661 vq->vq_avail->flags |=
662 virtio_rw16(sc, VRING_AVAIL_F_NO_INTERRUPT);
663 vq_sync_aring_header(sc, vq, BUS_DMASYNC_PREWRITE);
664 }
665 vq->vq_queued++;
666 }
667
668 int
669 virtio_start_vq_intr(struct virtio_softc *sc, struct virtqueue *vq)
670 {
671
672 if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
673 /*
674 * If event index feature is negotiated, enabling interrupts
675 * is done through setting the latest consumed index in the
676 * used_event field
677 */
678 *vq->vq_used_event = virtio_rw16(sc, vq->vq_used_idx);
679 vq_sync_aring_used(sc, vq, BUS_DMASYNC_PREWRITE);
680 } else {
681 vq->vq_avail->flags &=
682 ~virtio_rw16(sc, VRING_AVAIL_F_NO_INTERRUPT);
683 vq_sync_aring_header(sc, vq, BUS_DMASYNC_PREWRITE);
684 }
685 vq->vq_queued++;
686
687 vq_sync_uring_header(sc, vq, BUS_DMASYNC_POSTREAD);
688 if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
689 return 0;
690 vq_sync_uring_payload(sc, vq, BUS_DMASYNC_POSTREAD);
691 return 1;
692 }
693
694 /*
695 * Initialize vq structure.
696 */
697 /*
698 * Reset virtqueue parameters
699 */
700 static void
701 virtio_reset_vq(struct virtio_softc *sc, struct virtqueue *vq)
702 {
703 int i, j;
704 int vq_size = vq->vq_num;
705
706 memset(vq->vq_vaddr, 0, vq->vq_bytesize);
707
708 /* build the indirect descriptor chain */
709 if (vq->vq_indirect != NULL) {
710 struct vring_desc *vd;
711
712 for (i = 0; i < vq_size; i++) {
713 vd = vq->vq_indirect;
714 vd += vq->vq_maxnsegs * i;
715 for (j = 0; j < vq->vq_maxnsegs - 1; j++) {
716 vd[j].next = virtio_rw16(sc, j + 1);
717 }
718 }
719 }
720
721 /* free slot management */
722 SIMPLEQ_INIT(&vq->vq_freelist);
723 for (i = 0; i < vq_size; i++) {
724 SIMPLEQ_INSERT_TAIL(&vq->vq_freelist, &vq->vq_entries[i],
725 qe_list);
726 vq->vq_entries[i].qe_index = i;
727 }
728
729 /* enqueue/dequeue status */
730 vq->vq_avail_idx = 0;
731 vq->vq_used_idx = 0;
732 vq->vq_queued = 0;
733 vq_sync_uring_all(sc, vq, BUS_DMASYNC_PREREAD);
734 vq->vq_queued++;
735 }
736
737 /* Initialize vq */
738 void
739 virtio_init_vq_vqdone(struct virtio_softc *sc, struct virtqueue *vq,
740 int index, int (*vq_done)(struct virtqueue *))
741 {
742
743 virtio_init_vq(sc, vq, index, virtio_vq_done, vq);
744 vq->vq_done = vq_done;
745 }
746
747 void
748 virtio_init_vq(struct virtio_softc *sc, struct virtqueue *vq, int index,
749 int (*intrhand)(void *), void *arg)
750 {
751
752 memset(vq, 0, sizeof(*vq));
753
754 vq->vq_owner = sc;
755 vq->vq_num = sc->sc_ops->read_queue_size(sc, index);
756 vq->vq_index = index;
757 vq->vq_intrhand = intrhand;
758 vq->vq_intrhand_arg = arg;
759 }
760
761 /*
762 * Allocate/free a vq.
763 */
764 int
765 virtio_alloc_vq(struct virtio_softc *sc, struct virtqueue *vq,
766 int maxsegsize, int maxnsegs, const char *name)
767 {
768 bus_size_t size_desc, size_avail, size_used, size_indirect;
769 bus_size_t allocsize = 0, size_desc_avail;
770 int rsegs, r, hdrlen;
771 unsigned int vq_num;
772 #define VIRTQUEUE_ALIGN(n) roundup(n, VIRTIO_PAGE_SIZE)
773
774 vq_num = vq->vq_num;
775
776 if (vq_num == 0) {
777 aprint_error_dev(sc->sc_dev,
778 "virtqueue not exist, index %d for %s\n",
779 vq->vq_index, name);
780 goto err;
781 }
782
783 hdrlen = sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX ? 3 : 2;
784
785 size_desc = sizeof(vq->vq_desc[0]) * vq_num;
786 size_avail = sizeof(uint16_t) * hdrlen
787 + sizeof(vq->vq_avail[0].ring) * vq_num;
788 size_used = sizeof(uint16_t) *hdrlen
789 + sizeof(vq->vq_used[0].ring) * vq_num;
790 size_indirect = (sc->sc_indirect && maxnsegs >= MINSEG_INDIRECT) ?
791 sizeof(struct vring_desc) * maxnsegs * vq_num : 0;
792
793 size_desc_avail = VIRTQUEUE_ALIGN(size_desc + size_avail);
794 size_used = VIRTQUEUE_ALIGN(size_used);
795
796 allocsize = size_desc_avail + size_used + size_indirect;
797
798 /* alloc and map the memory */
799 r = bus_dmamem_alloc(sc->sc_dmat, allocsize, VIRTIO_PAGE_SIZE, 0,
800 &vq->vq_segs[0], 1, &rsegs, BUS_DMA_WAITOK);
801 if (r != 0) {
802 aprint_error_dev(sc->sc_dev,
803 "virtqueue %d for %s allocation failed, "
804 "error code %d\n", vq->vq_index, name, r);
805 goto err;
806 }
807
808 r = bus_dmamem_map(sc->sc_dmat, &vq->vq_segs[0], rsegs, allocsize,
809 &vq->vq_vaddr, BUS_DMA_WAITOK);
810 if (r != 0) {
811 aprint_error_dev(sc->sc_dev,
812 "virtqueue %d for %s map failed, "
813 "error code %d\n", vq->vq_index, name, r);
814 goto err;
815 }
816
817 r = bus_dmamap_create(sc->sc_dmat, allocsize, 1, allocsize, 0,
818 BUS_DMA_WAITOK, &vq->vq_dmamap);
819 if (r != 0) {
820 aprint_error_dev(sc->sc_dev,
821 "virtqueue %d for %s dmamap creation failed, "
822 "error code %d\n", vq->vq_index, name, r);
823 goto err;
824 }
825
826 r = bus_dmamap_load(sc->sc_dmat, vq->vq_dmamap,
827 vq->vq_vaddr, allocsize, NULL, BUS_DMA_WAITOK);
828 if (r != 0) {
829 aprint_error_dev(sc->sc_dev,
830 "virtqueue %d for %s dmamap load failed, "
831 "error code %d\n", vq->vq_index, name, r);
832 goto err;
833 }
834
835 vq->vq_bytesize = allocsize;
836 vq->vq_maxsegsize = maxsegsize;
837 vq->vq_maxnsegs = maxnsegs;
838
839 #define VIRTIO_PTR(base, offset) (void *)((intptr_t)(base) + (offset))
840 /* initialize vring pointers */
841 vq->vq_desc = VIRTIO_PTR(vq->vq_vaddr, 0);
842 vq->vq_availoffset = size_desc;
843 vq->vq_avail = VIRTIO_PTR(vq->vq_vaddr, vq->vq_availoffset);
844 vq->vq_used_event = VIRTIO_PTR(vq->vq_avail,
845 offsetof(struct vring_avail, ring[vq_num]));
846 vq->vq_usedoffset = size_desc_avail;
847 vq->vq_used = VIRTIO_PTR(vq->vq_vaddr, vq->vq_usedoffset);
848 vq->vq_avail_event = VIRTIO_PTR(vq->vq_used,
849 offsetof(struct vring_used, ring[vq_num]));
850
851 if (size_indirect > 0) {
852 vq->vq_indirectoffset = size_desc_avail + size_used;
853 vq->vq_indirect = VIRTIO_PTR(vq->vq_vaddr,
854 vq->vq_indirectoffset);
855 }
856 #undef VIRTIO_PTR
857
858 /* free slot management */
859 vq->vq_entries = kmem_zalloc(sizeof(struct vq_entry) * vq_num,
860 KM_SLEEP);
861
862 mutex_init(&vq->vq_freelist_lock, MUTEX_SPIN, sc->sc_ipl);
863 mutex_init(&vq->vq_aring_lock, MUTEX_SPIN, sc->sc_ipl);
864 mutex_init(&vq->vq_uring_lock, MUTEX_SPIN, sc->sc_ipl);
865
866 virtio_reset_vq(sc, vq);
867
868 /* set the vq address */
869 sc->sc_ops->setup_queue(sc, vq->vq_index,
870 vq->vq_dmamap->dm_segs[0].ds_addr);
871
872 aprint_verbose_dev(sc->sc_dev,
873 "allocated %zu byte for virtqueue %d for %s, size %d\n",
874 allocsize, vq->vq_index, name, vq_num);
875 if (size_indirect > 0)
876 aprint_verbose_dev(sc->sc_dev,
877 "using %zu byte (%d entries) indirect descriptors\n",
878 size_indirect, maxnsegs * vq_num);
879
880 return 0;
881
882 err:
883 sc->sc_ops->setup_queue(sc, vq->vq_index, 0);
884 if (vq->vq_dmamap)
885 bus_dmamap_destroy(sc->sc_dmat, vq->vq_dmamap);
886 if (vq->vq_vaddr)
887 bus_dmamem_unmap(sc->sc_dmat, vq->vq_vaddr, allocsize);
888 if (vq->vq_segs[0].ds_addr)
889 bus_dmamem_free(sc->sc_dmat, &vq->vq_segs[0], 1);
890 memset(vq, 0, sizeof(*vq));
891
892 return -1;
893 }
894
895 int
896 virtio_free_vq(struct virtio_softc *sc, struct virtqueue *vq)
897 {
898 struct vq_entry *qe;
899 int i = 0;
900
901 if (vq->vq_vaddr == NULL)
902 return 0;
903
904 /* device must be already deactivated */
905 /* confirm the vq is empty */
906 SIMPLEQ_FOREACH(qe, &vq->vq_freelist, qe_list) {
907 i++;
908 }
909 if (i != vq->vq_num) {
910 printf("%s: freeing non-empty vq, index %d\n",
911 device_xname(sc->sc_dev), vq->vq_index);
912 return EBUSY;
913 }
914
915 /* tell device that there's no virtqueue any longer */
916 sc->sc_ops->setup_queue(sc, vq->vq_index, 0);
917
918 vq_sync_aring_all(sc, vq, BUS_DMASYNC_POSTWRITE);
919
920 kmem_free(vq->vq_entries, sizeof(*vq->vq_entries) * vq->vq_num);
921 bus_dmamap_unload(sc->sc_dmat, vq->vq_dmamap);
922 bus_dmamap_destroy(sc->sc_dmat, vq->vq_dmamap);
923 bus_dmamem_unmap(sc->sc_dmat, vq->vq_vaddr, vq->vq_bytesize);
924 bus_dmamem_free(sc->sc_dmat, &vq->vq_segs[0], 1);
925 mutex_destroy(&vq->vq_freelist_lock);
926 mutex_destroy(&vq->vq_uring_lock);
927 mutex_destroy(&vq->vq_aring_lock);
928 memset(vq, 0, sizeof(*vq));
929
930 return 0;
931 }
932
933 /*
934 * Free descriptor management.
935 */
936 static struct vq_entry *
937 vq_alloc_entry(struct virtqueue *vq)
938 {
939 struct vq_entry *qe;
940
941 mutex_enter(&vq->vq_freelist_lock);
942 if (SIMPLEQ_EMPTY(&vq->vq_freelist)) {
943 mutex_exit(&vq->vq_freelist_lock);
944 return NULL;
945 }
946 qe = SIMPLEQ_FIRST(&vq->vq_freelist);
947 SIMPLEQ_REMOVE_HEAD(&vq->vq_freelist, qe_list);
948 mutex_exit(&vq->vq_freelist_lock);
949
950 return qe;
951 }
952
953 static void
954 vq_free_entry(struct virtqueue *vq, struct vq_entry *qe)
955 {
956 mutex_enter(&vq->vq_freelist_lock);
957 SIMPLEQ_INSERT_TAIL(&vq->vq_freelist, qe, qe_list);
958 mutex_exit(&vq->vq_freelist_lock);
959
960 return;
961 }
962
963 /*
964 * Enqueue several dmamaps as a single request.
965 */
966 /*
967 * Typical usage:
968 * <queue size> number of followings are stored in arrays
969 * - command blocks (in dmamem) should be pre-allocated and mapped
970 * - dmamaps for command blocks should be pre-allocated and loaded
971 * - dmamaps for payload should be pre-allocated
972 * r = virtio_enqueue_prep(sc, vq, &slot); // allocate a slot
973 * if (r) // currently 0 or EAGAIN
974 * return r;
975 * r = bus_dmamap_load(dmat, dmamap_payload[slot], data, count, ..);
976 * if (r) {
977 * virtio_enqueue_abort(sc, vq, slot);
978 * return r;
979 * }
980 * r = virtio_enqueue_reserve(sc, vq, slot,
981 * dmamap_payload[slot]->dm_nsegs + 1);
982 * // ^ +1 for command
983 * if (r) { // currently 0 or EAGAIN
984 * bus_dmamap_unload(dmat, dmamap_payload[slot]);
985 * return r; // do not call abort()
986 * }
987 * <setup and prepare commands>
988 * bus_dmamap_sync(dmat, dmamap_cmd[slot],... BUS_DMASYNC_PREWRITE);
989 * bus_dmamap_sync(dmat, dmamap_payload[slot],...);
990 * virtio_enqueue(sc, vq, slot, dmamap_cmd[slot], false);
991 * virtio_enqueue(sc, vq, slot, dmamap_payload[slot], iswrite);
992 * virtio_enqueue_commit(sc, vq, slot, true);
993 */
994
995 /*
996 * enqueue_prep: allocate a slot number
997 */
998 int
999 virtio_enqueue_prep(struct virtio_softc *sc, struct virtqueue *vq, int *slotp)
1000 {
1001 struct vq_entry *qe1;
1002
1003 KASSERT(slotp != NULL);
1004
1005 qe1 = vq_alloc_entry(vq);
1006 if (qe1 == NULL)
1007 return EAGAIN;
1008 /* next slot is not allocated yet */
1009 qe1->qe_next = -1;
1010 *slotp = qe1->qe_index;
1011
1012 return 0;
1013 }
1014
1015 /*
1016 * enqueue_reserve: allocate remaining slots and build the descriptor chain.
1017 */
1018 int
1019 virtio_enqueue_reserve(struct virtio_softc *sc, struct virtqueue *vq,
1020 int slot, int nsegs)
1021 {
1022 int indirect;
1023 struct vq_entry *qe1 = &vq->vq_entries[slot];
1024
1025 KASSERT(qe1->qe_next == -1);
1026 KASSERT(1 <= nsegs && nsegs <= vq->vq_num);
1027
1028 if ((vq->vq_indirect != NULL) &&
1029 (nsegs >= MINSEG_INDIRECT) &&
1030 (nsegs <= vq->vq_maxnsegs))
1031 indirect = 1;
1032 else
1033 indirect = 0;
1034 qe1->qe_indirect = indirect;
1035
1036 if (indirect) {
1037 struct vring_desc *vd;
1038 uint64_t addr;
1039 int i;
1040
1041 vd = &vq->vq_desc[qe1->qe_index];
1042 addr = vq->vq_dmamap->dm_segs[0].ds_addr
1043 + vq->vq_indirectoffset;
1044 addr += sizeof(struct vring_desc)
1045 * vq->vq_maxnsegs * qe1->qe_index;
1046 vd->addr = virtio_rw64(sc, addr);
1047 vd->len = virtio_rw32(sc, sizeof(struct vring_desc) * nsegs);
1048 vd->flags = virtio_rw16(sc, VRING_DESC_F_INDIRECT);
1049
1050 vd = vq->vq_indirect;
1051 vd += vq->vq_maxnsegs * qe1->qe_index;
1052 qe1->qe_desc_base = vd;
1053
1054 for (i = 0; i < nsegs - 1; i++) {
1055 vd[i].flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
1056 }
1057 vd[i].flags = virtio_rw16(sc, 0);
1058 qe1->qe_next = 0;
1059
1060 return 0;
1061 } else {
1062 struct vring_desc *vd;
1063 struct vq_entry *qe;
1064 int i, s;
1065
1066 vd = &vq->vq_desc[0];
1067 qe1->qe_desc_base = vd;
1068 qe1->qe_next = qe1->qe_index;
1069 s = slot;
1070 for (i = 0; i < nsegs - 1; i++) {
1071 qe = vq_alloc_entry(vq);
1072 if (qe == NULL) {
1073 vd[s].flags = virtio_rw16(sc, 0);
1074 virtio_enqueue_abort(sc, vq, slot);
1075 return EAGAIN;
1076 }
1077 vd[s].flags = virtio_rw16(sc, VRING_DESC_F_NEXT);
1078 vd[s].next = virtio_rw16(sc, qe->qe_index);
1079 s = qe->qe_index;
1080 }
1081 vd[s].flags = virtio_rw16(sc, 0);
1082
1083 return 0;
1084 }
1085 }
1086
1087 /*
1088 * enqueue: enqueue a single dmamap.
1089 */
1090 int
1091 virtio_enqueue(struct virtio_softc *sc, struct virtqueue *vq, int slot,
1092 bus_dmamap_t dmamap, bool write)
1093 {
1094 struct vq_entry *qe1 = &vq->vq_entries[slot];
1095 struct vring_desc *vd = qe1->qe_desc_base;
1096 int i;
1097 int s = qe1->qe_next;
1098
1099 KASSERT(s >= 0);
1100 KASSERT(dmamap->dm_nsegs > 0);
1101
1102 for (i = 0; i < dmamap->dm_nsegs; i++) {
1103 vd[s].addr = virtio_rw64(sc, dmamap->dm_segs[i].ds_addr);
1104 vd[s].len = virtio_rw32(sc, dmamap->dm_segs[i].ds_len);
1105 if (!write)
1106 vd[s].flags |= virtio_rw16(sc, VRING_DESC_F_WRITE);
1107 s = virtio_rw16(sc, vd[s].next);
1108 }
1109 qe1->qe_next = s;
1110
1111 return 0;
1112 }
1113
1114 int
1115 virtio_enqueue_p(struct virtio_softc *sc, struct virtqueue *vq, int slot,
1116 bus_dmamap_t dmamap, bus_addr_t start, bus_size_t len,
1117 bool write)
1118 {
1119 struct vq_entry *qe1 = &vq->vq_entries[slot];
1120 struct vring_desc *vd = qe1->qe_desc_base;
1121 int s = qe1->qe_next;
1122
1123 KASSERT(s >= 0);
1124 KASSERT(dmamap->dm_nsegs == 1); /* XXX */
1125 KASSERT(dmamap->dm_segs[0].ds_len > start);
1126 KASSERT(dmamap->dm_segs[0].ds_len >= start + len);
1127
1128 vd[s].addr = virtio_rw64(sc, dmamap->dm_segs[0].ds_addr + start);
1129 vd[s].len = virtio_rw32(sc, len);
1130 if (!write)
1131 vd[s].flags |= virtio_rw16(sc, VRING_DESC_F_WRITE);
1132 qe1->qe_next = virtio_rw16(sc, vd[s].next);
1133
1134 return 0;
1135 }
1136
1137 /*
1138 * enqueue_commit: add it to the aring.
1139 */
1140 int
1141 virtio_enqueue_commit(struct virtio_softc *sc, struct virtqueue *vq, int slot,
1142 bool notifynow)
1143 {
1144 struct vq_entry *qe1;
1145
1146 if (slot < 0) {
1147 mutex_enter(&vq->vq_aring_lock);
1148 goto notify;
1149 }
1150 vq_sync_descs(sc, vq, BUS_DMASYNC_PREWRITE);
1151 qe1 = &vq->vq_entries[slot];
1152 if (qe1->qe_indirect)
1153 vq_sync_indirect(sc, vq, slot, BUS_DMASYNC_PREWRITE);
1154 mutex_enter(&vq->vq_aring_lock);
1155 vq->vq_avail->ring[(vq->vq_avail_idx++) % vq->vq_num] =
1156 virtio_rw16(sc, slot);
1157
1158 notify:
1159 if (notifynow) {
1160 uint16_t o, n, t;
1161 uint16_t flags;
1162
1163 o = virtio_rw16(sc, vq->vq_avail->idx) - 1;
1164 n = vq->vq_avail_idx;
1165
1166 /*
1167 * Prepare for `device->CPU' (host->guest) transfer
1168 * into the buffer. This must happen before we commit
1169 * the vq->vq_avail->idx update to ensure we're not
1170 * still using the buffer in case program-prior loads
1171 * or stores in it get delayed past the store to
1172 * vq->vq_avail->idx.
1173 */
1174 vq_sync_uring_all(sc, vq, BUS_DMASYNC_PREREAD);
1175
1176 /* ensure payload is published, then avail idx */
1177 vq_sync_aring_payload(sc, vq, BUS_DMASYNC_PREWRITE);
1178 vq->vq_avail->idx = virtio_rw16(sc, vq->vq_avail_idx);
1179 vq_sync_aring_header(sc, vq, BUS_DMASYNC_PREWRITE);
1180 vq->vq_queued++;
1181
1182 if (sc->sc_active_features & VIRTIO_F_RING_EVENT_IDX) {
1183 vq_sync_uring_avail(sc, vq, BUS_DMASYNC_POSTREAD);
1184 t = virtio_rw16(sc, *vq->vq_avail_event) + 1;
1185 if ((uint16_t) (n - t) < (uint16_t) (n - o))
1186 sc->sc_ops->kick(sc, vq->vq_index);
1187 } else {
1188 vq_sync_uring_header(sc, vq, BUS_DMASYNC_POSTREAD);
1189 flags = virtio_rw16(sc, vq->vq_used->flags);
1190 if (!(flags & VRING_USED_F_NO_NOTIFY))
1191 sc->sc_ops->kick(sc, vq->vq_index);
1192 }
1193 }
1194 mutex_exit(&vq->vq_aring_lock);
1195
1196 return 0;
1197 }
1198
1199 /*
1200 * enqueue_abort: rollback.
1201 */
1202 int
1203 virtio_enqueue_abort(struct virtio_softc *sc, struct virtqueue *vq, int slot)
1204 {
1205 struct vq_entry *qe = &vq->vq_entries[slot];
1206 struct vring_desc *vd;
1207 int s;
1208
1209 if (qe->qe_next < 0) {
1210 vq_free_entry(vq, qe);
1211 return 0;
1212 }
1213
1214 s = slot;
1215 vd = &vq->vq_desc[0];
1216 while (virtio_rw16(sc, vd[s].flags) & VRING_DESC_F_NEXT) {
1217 s = virtio_rw16(sc, vd[s].next);
1218 vq_free_entry(vq, qe);
1219 qe = &vq->vq_entries[s];
1220 }
1221 vq_free_entry(vq, qe);
1222 return 0;
1223 }
1224
1225 /*
1226 * Dequeue a request.
1227 */
1228 /*
1229 * dequeue: dequeue a request from uring; dmamap_sync for uring is
1230 * already done in the interrupt handler.
1231 */
1232 int
1233 virtio_dequeue(struct virtio_softc *sc, struct virtqueue *vq,
1234 int *slotp, int *lenp)
1235 {
1236 uint16_t slot, usedidx;
1237 struct vq_entry *qe;
1238
1239 if (vq->vq_used_idx == virtio_rw16(sc, vq->vq_used->idx))
1240 return ENOENT;
1241 mutex_enter(&vq->vq_uring_lock);
1242 usedidx = vq->vq_used_idx++;
1243 mutex_exit(&vq->vq_uring_lock);
1244 usedidx %= vq->vq_num;
1245 slot = virtio_rw32(sc, vq->vq_used->ring[usedidx].id);
1246 qe = &vq->vq_entries[slot];
1247
1248 if (qe->qe_indirect)
1249 vq_sync_indirect(sc, vq, slot, BUS_DMASYNC_POSTWRITE);
1250
1251 if (slotp)
1252 *slotp = slot;
1253 if (lenp)
1254 *lenp = virtio_rw32(sc, vq->vq_used->ring[usedidx].len);
1255
1256 return 0;
1257 }
1258
1259 /*
1260 * dequeue_commit: complete dequeue; the slot is recycled for future use.
1261 * if you forget to call this the slot will be leaked.
1262 */
1263 int
1264 virtio_dequeue_commit(struct virtio_softc *sc, struct virtqueue *vq, int slot)
1265 {
1266 struct vq_entry *qe = &vq->vq_entries[slot];
1267 struct vring_desc *vd = &vq->vq_desc[0];
1268 int s = slot;
1269
1270 while (virtio_rw16(sc, vd[s].flags) & VRING_DESC_F_NEXT) {
1271 s = virtio_rw16(sc, vd[s].next);
1272 vq_free_entry(vq, qe);
1273 qe = &vq->vq_entries[s];
1274 }
1275 vq_free_entry(vq, qe);
1276
1277 return 0;
1278 }
1279
1280 /*
1281 * Attach a child, fill all the members.
1282 */
1283 void
1284 virtio_child_attach_start(struct virtio_softc *sc, device_t child, int ipl,
1285 uint64_t req_features, const char *feat_bits)
1286 {
1287 char buf[1024];
1288
1289 sc->sc_child = child;
1290 sc->sc_ipl = ipl;
1291
1292 virtio_negotiate_features(sc, req_features);
1293 snprintb(buf, sizeof(buf), feat_bits, sc->sc_active_features);
1294 aprint_normal(": features: %s\n", buf);
1295 aprint_naive("\n");
1296 }
1297
1298 int
1299 virtio_child_attach_finish(struct virtio_softc *sc,
1300 struct virtqueue *vqs, size_t nvqs,
1301 virtio_callback config_change,
1302 int req_flags)
1303 {
1304 int r;
1305
1306 #ifdef DIAGNOSTIC
1307 KASSERT(nvqs > 0);
1308 #define VIRTIO_ASSERT_FLAGS (VIRTIO_F_INTR_SOFTINT | VIRTIO_F_INTR_PERVQ)
1309 KASSERT((req_flags & VIRTIO_ASSERT_FLAGS) != VIRTIO_ASSERT_FLAGS);
1310 #undef VIRTIO_ASSERT_FLAGS
1311
1312 for (size_t _i = 0; _i < nvqs; _i++){
1313 KASSERT(vqs[_i].vq_index == _i);
1314 KASSERT(vqs[_i].vq_intrhand != NULL);
1315 KASSERT(vqs[_i].vq_done == NULL ||
1316 vqs[_i].vq_intrhand == virtio_vq_done);
1317 }
1318 #endif
1319
1320 sc->sc_finished_called = true;
1321
1322 sc->sc_vqs = vqs;
1323 sc->sc_nvqs = nvqs;
1324 sc->sc_config_change = config_change;
1325 sc->sc_intrhand = virtio_vq_intr;
1326 sc->sc_flags = req_flags;
1327
1328 r = sc->sc_ops->alloc_interrupts(sc);
1329 if (r != 0) {
1330 aprint_error_dev(sc->sc_dev,
1331 "failed to allocate interrupts\n");
1332 goto fail;
1333 }
1334
1335 r = sc->sc_ops->setup_interrupts(sc, 0);
1336 if (r != 0) {
1337 aprint_error_dev(sc->sc_dev, "failed to setup interrupts\n");
1338 goto fail;
1339 }
1340
1341 KASSERT(sc->sc_soft_ih == NULL);
1342 if (sc->sc_flags & VIRTIO_F_INTR_SOFTINT) {
1343 u_int flags = SOFTINT_NET;
1344 if (sc->sc_flags & VIRTIO_F_INTR_MPSAFE)
1345 flags |= SOFTINT_MPSAFE;
1346
1347 sc->sc_soft_ih = softint_establish(flags, virtio_soft_intr,
1348 sc);
1349 if (sc->sc_soft_ih == NULL) {
1350 sc->sc_ops->free_interrupts(sc);
1351 aprint_error_dev(sc->sc_dev,
1352 "failed to establish soft interrupt\n");
1353 goto fail;
1354 }
1355 }
1356
1357 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_DRIVER_OK);
1358 return 0;
1359
1360 fail:
1361 if (sc->sc_soft_ih) {
1362 softint_disestablish(sc->sc_soft_ih);
1363 sc->sc_soft_ih = NULL;
1364 }
1365
1366 sc->sc_ops->free_interrupts(sc);
1367
1368 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
1369 return 1;
1370 }
1371
1372 void
1373 virtio_child_detach(struct virtio_softc *sc)
1374 {
1375 sc->sc_child = NULL;
1376 sc->sc_vqs = NULL;
1377
1378 virtio_device_reset(sc);
1379
1380 sc->sc_ops->free_interrupts(sc);
1381
1382 if (sc->sc_soft_ih) {
1383 softint_disestablish(sc->sc_soft_ih);
1384 sc->sc_soft_ih = NULL;
1385 }
1386 }
1387
1388 void
1389 virtio_child_attach_failed(struct virtio_softc *sc)
1390 {
1391 virtio_child_detach(sc);
1392
1393 virtio_set_status(sc, VIRTIO_CONFIG_DEVICE_STATUS_FAILED);
1394
1395 sc->sc_child = VIRTIO_CHILD_FAILED;
1396 }
1397
1398 bus_dma_tag_t
1399 virtio_dmat(struct virtio_softc *sc)
1400 {
1401 return sc->sc_dmat;
1402 }
1403
1404 device_t
1405 virtio_child(struct virtio_softc *sc)
1406 {
1407 return sc->sc_child;
1408 }
1409
1410 int
1411 virtio_intrhand(struct virtio_softc *sc)
1412 {
1413 return (*sc->sc_intrhand)(sc);
1414 }
1415
1416 uint64_t
1417 virtio_features(struct virtio_softc *sc)
1418 {
1419 return sc->sc_active_features;
1420 }
1421
1422 int
1423 virtio_attach_failed(struct virtio_softc *sc)
1424 {
1425 device_t self = sc->sc_dev;
1426
1427 /* no error if its not connected, but its failed */
1428 if (sc->sc_childdevid == 0)
1429 return 1;
1430
1431 if (sc->sc_child == NULL) {
1432 aprint_error_dev(self,
1433 "no matching child driver; not configured\n");
1434 return 1;
1435 }
1436
1437 if (sc->sc_child == VIRTIO_CHILD_FAILED) {
1438 aprint_error_dev(self, "virtio configuration failed\n");
1439 return 1;
1440 }
1441
1442 /* sanity check */
1443 if (!sc->sc_finished_called) {
1444 aprint_error_dev(self, "virtio internal error, child driver "
1445 "signaled OK but didn't initialize interrupts\n");
1446 return 1;
1447 }
1448
1449 return 0;
1450 }
1451
1452 void
1453 virtio_print_device_type(device_t self, int id, int revision)
1454 {
1455 aprint_normal_dev(self, "%s device (id %d, rev. 0x%02x)\n",
1456 (id < NDEVNAMES ? virtio_device_name[id] : "Unknown"),
1457 id,
1458 revision);
1459 }
1460
1461
1462 MODULE(MODULE_CLASS_DRIVER, virtio, NULL);
1463
1464 #ifdef _MODULE
1465 #include "ioconf.c"
1466 #endif
1467
1468 static int
1469 virtio_modcmd(modcmd_t cmd, void *opaque)
1470 {
1471 int error = 0;
1472
1473 #ifdef _MODULE
1474 switch (cmd) {
1475 case MODULE_CMD_INIT:
1476 error = config_init_component(cfdriver_ioconf_virtio,
1477 cfattach_ioconf_virtio, cfdata_ioconf_virtio);
1478 break;
1479 case MODULE_CMD_FINI:
1480 error = config_fini_component(cfdriver_ioconf_virtio,
1481 cfattach_ioconf_virtio, cfdata_ioconf_virtio);
1482 break;
1483 default:
1484 error = ENOTTY;
1485 break;
1486 }
1487 #endif
1488
1489 return error;
1490 }
1491