if_vioif.c revision 1.50 1 /* $NetBSD: if_vioif.c,v 1.50 2019/09/14 21:09:00 christos Exp $ */
2
3 /*
4 * Copyright (c) 2010 Minoura Makoto.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.50 2019/09/14 21:09:00 christos Exp $");
30
31 #ifdef _KERNEL_OPT
32 #include "opt_net_mpsafe.h"
33 #endif
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/bus.h>
39 #include <sys/condvar.h>
40 #include <sys/device.h>
41 #include <sys/intr.h>
42 #include <sys/kmem.h>
43 #include <sys/mbuf.h>
44 #include <sys/mutex.h>
45 #include <sys/sockio.h>
46 #include <sys/cpu.h>
47 #include <sys/module.h>
48 #include <sys/pcq.h>
49
50 #include <dev/pci/virtioreg.h>
51 #include <dev/pci/virtiovar.h>
52
53 #include <net/if.h>
54 #include <net/if_media.h>
55 #include <net/if_ether.h>
56
57 #include <net/bpf.h>
58
59 #include "ioconf.h"
60
61 #ifdef NET_MPSAFE
62 #define VIOIF_MPSAFE 1
63 #define VIOIF_MULTIQ 1
64 #endif
65
66 #ifdef SOFTINT_INTR
67 #define VIOIF_SOFTINT_INTR 1
68 #endif
69
70 /*
71 * if_vioifreg.h:
72 */
73 /* Configuration registers */
74 #define VIRTIO_NET_CONFIG_MAC 0 /* 8bit x 6byte */
75 #define VIRTIO_NET_CONFIG_STATUS 6 /* 16bit */
76 #define VIRTIO_NET_CONFIG_MAX_VQ_PAIRS 8 /* 16bit */
77
78 /* Feature bits */
79 #define VIRTIO_NET_F_CSUM __BIT(0)
80 #define VIRTIO_NET_F_GUEST_CSUM __BIT(1)
81 #define VIRTIO_NET_F_MAC __BIT(5)
82 #define VIRTIO_NET_F_GSO __BIT(6)
83 #define VIRTIO_NET_F_GUEST_TSO4 __BIT(7)
84 #define VIRTIO_NET_F_GUEST_TSO6 __BIT(8)
85 #define VIRTIO_NET_F_GUEST_ECN __BIT(9)
86 #define VIRTIO_NET_F_GUEST_UFO __BIT(10)
87 #define VIRTIO_NET_F_HOST_TSO4 __BIT(11)
88 #define VIRTIO_NET_F_HOST_TSO6 __BIT(12)
89 #define VIRTIO_NET_F_HOST_ECN __BIT(13)
90 #define VIRTIO_NET_F_HOST_UFO __BIT(14)
91 #define VIRTIO_NET_F_MRG_RXBUF __BIT(15)
92 #define VIRTIO_NET_F_STATUS __BIT(16)
93 #define VIRTIO_NET_F_CTRL_VQ __BIT(17)
94 #define VIRTIO_NET_F_CTRL_RX __BIT(18)
95 #define VIRTIO_NET_F_CTRL_VLAN __BIT(19)
96 #define VIRTIO_NET_F_CTRL_RX_EXTRA __BIT(20)
97 #define VIRTIO_NET_F_GUEST_ANNOUNCE __BIT(21)
98 #define VIRTIO_NET_F_MQ __BIT(22)
99
100 #define VIRTIO_NET_FLAG_BITS \
101 VIRTIO_COMMON_FLAG_BITS \
102 "\x17""MQ" \
103 "\x16""GUEST_ANNOUNCE" \
104 "\x15""CTRL_RX_EXTRA" \
105 "\x14""CTRL_VLAN" \
106 "\x13""CTRL_RX" \
107 "\x12""CTRL_VQ" \
108 "\x11""STATUS" \
109 "\x10""MRG_RXBUF" \
110 "\x0f""HOST_UFO" \
111 "\x0e""HOST_ECN" \
112 "\x0d""HOST_TSO6" \
113 "\x0c""HOST_TSO4" \
114 "\x0b""GUEST_UFO" \
115 "\x0a""GUEST_ECN" \
116 "\x09""GUEST_TSO6" \
117 "\x08""GUEST_TSO4" \
118 "\x07""GSO" \
119 "\x06""MAC" \
120 "\x02""GUEST_CSUM" \
121 "\x01""CSUM"
122
123 /* Status */
124 #define VIRTIO_NET_S_LINK_UP 1
125
126 /* Packet header structure */
127 struct virtio_net_hdr {
128 uint8_t flags;
129 uint8_t gso_type;
130 uint16_t hdr_len;
131 uint16_t gso_size;
132 uint16_t csum_start;
133 uint16_t csum_offset;
134 #if 0
135 uint16_t num_buffers; /* if VIRTIO_NET_F_MRG_RXBUF enabled */
136 #endif
137 } __packed;
138
139 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /* flags */
140 #define VIRTIO_NET_HDR_GSO_NONE 0 /* gso_type */
141 #define VIRTIO_NET_HDR_GSO_TCPV4 1 /* gso_type */
142 #define VIRTIO_NET_HDR_GSO_UDP 3 /* gso_type */
143 #define VIRTIO_NET_HDR_GSO_TCPV6 4 /* gso_type */
144 #define VIRTIO_NET_HDR_GSO_ECN 0x80 /* gso_type, |'ed */
145
146 #define VIRTIO_NET_MAX_GSO_LEN (65536+ETHER_HDR_LEN)
147
148 /* Control virtqueue */
149 struct virtio_net_ctrl_cmd {
150 uint8_t class;
151 uint8_t command;
152 } __packed;
153 #define VIRTIO_NET_CTRL_RX 0
154 # define VIRTIO_NET_CTRL_RX_PROMISC 0
155 # define VIRTIO_NET_CTRL_RX_ALLMULTI 1
156
157 #define VIRTIO_NET_CTRL_MAC 1
158 # define VIRTIO_NET_CTRL_MAC_TABLE_SET 0
159
160 #define VIRTIO_NET_CTRL_VLAN 2
161 # define VIRTIO_NET_CTRL_VLAN_ADD 0
162 # define VIRTIO_NET_CTRL_VLAN_DEL 1
163
164 #define VIRTIO_NET_CTRL_MQ 4
165 # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET 0
166 # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN 1
167 # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX 0x8000
168
169 struct virtio_net_ctrl_status {
170 uint8_t ack;
171 } __packed;
172 #define VIRTIO_NET_OK 0
173 #define VIRTIO_NET_ERR 1
174
175 struct virtio_net_ctrl_rx {
176 uint8_t onoff;
177 } __packed;
178
179 struct virtio_net_ctrl_mac_tbl {
180 uint32_t nentries;
181 uint8_t macs[][ETHER_ADDR_LEN];
182 } __packed;
183
184 struct virtio_net_ctrl_vlan {
185 uint16_t id;
186 } __packed;
187
188 struct virtio_net_ctrl_mq {
189 uint16_t virtqueue_pairs;
190 } __packed;
191
192 struct vioif_ctrl_cmdspec {
193 bus_dmamap_t dmamap;
194 void *buf;
195 bus_size_t bufsize;
196 };
197
198 /*
199 * if_vioifvar.h:
200 */
201
202 /*
203 * Locking notes:
204 * + a field in vioif_txqueue is protected by txq_lock (a spin mutex), and
205 * a filds in vioif_rxqueue is protected by rxq_lock (a spin mutex).
206 * - more than one lock cannot be held at onece
207 * + ctrlq_inuse is protected by ctrlq_wait_lock.
208 * - other fields in vioif_ctrlqueue are protected by ctrlq_inuse
209 * - txq_lock or rxq_lock cannot be held along with ctrlq_wait_lock
210 */
211
212 struct vioif_txqueue {
213 kmutex_t *txq_lock; /* lock for tx operations */
214
215 struct virtqueue *txq_vq;
216 bool txq_stopping;
217 bool txq_link_active;
218 pcq_t *txq_intrq;
219
220 struct virtio_net_hdr *txq_hdrs;
221 bus_dmamap_t *txq_hdr_dmamaps;
222
223 struct mbuf **txq_mbufs;
224 bus_dmamap_t *txq_dmamaps;
225
226 void *txq_deferred_transmit;
227 };
228
229 struct vioif_rxqueue {
230 kmutex_t *rxq_lock; /* lock for rx operations */
231
232 struct virtqueue *rxq_vq;
233 bool rxq_stopping;
234
235 struct virtio_net_hdr *rxq_hdrs;
236 bus_dmamap_t *rxq_hdr_dmamaps;
237
238 struct mbuf **rxq_mbufs;
239 bus_dmamap_t *rxq_dmamaps;
240
241 void *rxq_softint;
242 };
243
244 struct vioif_ctrlqueue {
245 struct virtqueue *ctrlq_vq;
246 enum {
247 FREE, INUSE, DONE
248 } ctrlq_inuse;
249 kcondvar_t ctrlq_wait;
250 kmutex_t ctrlq_wait_lock;
251 struct lwp *ctrlq_owner;
252
253 struct virtio_net_ctrl_cmd *ctrlq_cmd;
254 struct virtio_net_ctrl_status *ctrlq_status;
255 struct virtio_net_ctrl_rx *ctrlq_rx;
256 struct virtio_net_ctrl_mac_tbl *ctrlq_mac_tbl_uc;
257 struct virtio_net_ctrl_mac_tbl *ctrlq_mac_tbl_mc;
258 struct virtio_net_ctrl_mq *ctrlq_mq;
259
260 bus_dmamap_t ctrlq_cmd_dmamap;
261 bus_dmamap_t ctrlq_status_dmamap;
262 bus_dmamap_t ctrlq_rx_dmamap;
263 bus_dmamap_t ctrlq_tbl_uc_dmamap;
264 bus_dmamap_t ctrlq_tbl_mc_dmamap;
265 bus_dmamap_t ctrlq_mq_dmamap;
266 };
267
268 struct vioif_softc {
269 device_t sc_dev;
270
271 struct virtio_softc *sc_virtio;
272 struct virtqueue *sc_vqs;
273
274 int sc_max_nvq_pairs;
275 int sc_req_nvq_pairs;
276 int sc_act_nvq_pairs;
277
278 uint8_t sc_mac[ETHER_ADDR_LEN];
279 struct ethercom sc_ethercom;
280 short sc_deferred_init_done;
281 bool sc_link_active;
282
283 struct vioif_txqueue *sc_txq;
284 struct vioif_rxqueue *sc_rxq;
285
286 bool sc_has_ctrl;
287 struct vioif_ctrlqueue sc_ctrlq;
288
289 bus_dma_segment_t sc_hdr_segs[1];
290 void *sc_dmamem;
291 void *sc_kmem;
292
293 void *sc_ctl_softint;
294 };
295 #define VIRTIO_NET_TX_MAXNSEGS (16) /* XXX */
296 #define VIRTIO_NET_CTRL_MAC_MAXENTRIES (64) /* XXX */
297
298 /* cfattach interface functions */
299 static int vioif_match(device_t, cfdata_t, void *);
300 static void vioif_attach(device_t, device_t, void *);
301 static void vioif_deferred_init(device_t);
302
303 /* ifnet interface functions */
304 static int vioif_init(struct ifnet *);
305 static void vioif_stop(struct ifnet *, int);
306 static void vioif_start(struct ifnet *);
307 static void vioif_start_locked(struct ifnet *, struct vioif_txqueue *);
308 static int vioif_transmit(struct ifnet *, struct mbuf *);
309 static void vioif_transmit_locked(struct ifnet *, struct vioif_txqueue *);
310 static int vioif_ioctl(struct ifnet *, u_long, void *);
311 static void vioif_watchdog(struct ifnet *);
312
313 /* rx */
314 static int vioif_add_rx_mbuf(struct vioif_rxqueue *, int);
315 static void vioif_free_rx_mbuf(struct vioif_rxqueue *, int);
316 static void vioif_populate_rx_mbufs(struct vioif_rxqueue *);
317 static void vioif_populate_rx_mbufs_locked(struct vioif_rxqueue *);
318 static int vioif_rx_deq(struct vioif_rxqueue *);
319 static int vioif_rx_deq_locked(struct vioif_rxqueue *);
320 static int vioif_rx_vq_done(struct virtqueue *);
321 static void vioif_rx_softint(void *);
322 static void vioif_rx_drain(struct vioif_rxqueue *);
323
324 /* tx */
325 static int vioif_tx_vq_done(struct virtqueue *);
326 static int vioif_tx_vq_done_locked(struct virtqueue *);
327 static void vioif_tx_drain(struct vioif_txqueue *);
328 static void vioif_deferred_transmit(void *);
329
330 /* other control */
331 static bool vioif_is_link_up(struct vioif_softc *);
332 static void vioif_update_link_status(struct vioif_softc *);
333 static int vioif_ctrl_rx(struct vioif_softc *, int, bool);
334 static int vioif_set_promisc(struct vioif_softc *, bool);
335 static int vioif_set_allmulti(struct vioif_softc *, bool);
336 static int vioif_set_rx_filter(struct vioif_softc *);
337 static int vioif_rx_filter(struct vioif_softc *);
338 static int vioif_ctrl_vq_done(struct virtqueue *);
339 static int vioif_config_change(struct virtio_softc *);
340 static void vioif_ctl_softint(void *);
341 static int vioif_ctrl_mq_vq_pairs_set(struct vioif_softc *, int);
342 static void vioif_enable_interrupt_vqpairs(struct vioif_softc *);
343 static void vioif_disable_interrupt_vqpairs(struct vioif_softc *);
344
345 CFATTACH_DECL_NEW(vioif, sizeof(struct vioif_softc),
346 vioif_match, vioif_attach, NULL, NULL);
347
348 static int
349 vioif_match(device_t parent, cfdata_t match, void *aux)
350 {
351 struct virtio_attach_args *va = aux;
352
353 if (va->sc_childdevid == PCI_PRODUCT_VIRTIO_NETWORK)
354 return 1;
355
356 return 0;
357 }
358
359 static int
360 vioif_alloc_queues(struct vioif_softc *sc)
361 {
362 int nvq_pairs = sc->sc_max_nvq_pairs;
363 int nvqs = nvq_pairs * 2;
364 int i;
365
366 KASSERT(nvq_pairs <= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX);
367
368 sc->sc_rxq = kmem_zalloc(sizeof(sc->sc_rxq[0]) * nvq_pairs,
369 KM_NOSLEEP);
370 if (sc->sc_rxq == NULL)
371 return -1;
372
373 sc->sc_txq = kmem_zalloc(sizeof(sc->sc_txq[0]) * nvq_pairs,
374 KM_NOSLEEP);
375 if (sc->sc_txq == NULL)
376 return -1;
377
378 if (sc->sc_has_ctrl)
379 nvqs++;
380
381 sc->sc_vqs = kmem_zalloc(sizeof(sc->sc_vqs[0]) * nvqs, KM_NOSLEEP);
382 if (sc->sc_vqs == NULL)
383 return -1;
384
385 nvqs = 0;
386 for (i = 0; i < nvq_pairs; i++) {
387 sc->sc_rxq[i].rxq_vq = &sc->sc_vqs[nvqs++];
388 sc->sc_txq[i].txq_vq = &sc->sc_vqs[nvqs++];
389 }
390
391 if (sc->sc_has_ctrl)
392 sc->sc_ctrlq.ctrlq_vq = &sc->sc_vqs[nvqs++];
393
394 return 0;
395 }
396
397 static void
398 vioif_free_queues(struct vioif_softc *sc)
399 {
400 int nvq_pairs = sc->sc_max_nvq_pairs;
401 int nvqs = nvq_pairs * 2;
402
403 if (sc->sc_ctrlq.ctrlq_vq)
404 nvqs++;
405
406 if (sc->sc_txq) {
407 kmem_free(sc->sc_txq, sizeof(sc->sc_txq[0]) * nvq_pairs);
408 sc->sc_txq = NULL;
409 }
410
411 if (sc->sc_rxq) {
412 kmem_free(sc->sc_rxq, sizeof(sc->sc_rxq[0]) * nvq_pairs);
413 sc->sc_rxq = NULL;
414 }
415
416 if (sc->sc_vqs) {
417 kmem_free(sc->sc_vqs, sizeof(sc->sc_vqs[0]) * nvqs);
418 sc->sc_vqs = NULL;
419 }
420 }
421
422 /* allocate memory */
423 /*
424 * dma memory is used for:
425 * rxq_hdrs[slot]: metadata array for received frames (READ)
426 * txq_hdrs[slot]: metadata array for frames to be sent (WRITE)
427 * ctrlq_cmd: command to be sent via ctrl vq (WRITE)
428 * ctrlq_status: return value for a command via ctrl vq (READ)
429 * ctrlq_rx: parameter for a VIRTIO_NET_CTRL_RX class command
430 * (WRITE)
431 * ctrlq_mac_tbl_uc: unicast MAC address filter for a VIRTIO_NET_CTRL_MAC
432 * class command (WRITE)
433 * ctrlq_mac_tbl_mc: multicast MAC address filter for a VIRTIO_NET_CTRL_MAC
434 * class command (WRITE)
435 * ctrlq_* structures are allocated only one each; they are protected by
436 * ctrlq_inuse variable and ctrlq_wait condvar.
437 */
438 /*
439 * dynamically allocated memory is used for:
440 * rxq_hdr_dmamaps[slot]: bus_dmamap_t array for sc_rx_hdrs[slot]
441 * txq_hdr_dmamaps[slot]: bus_dmamap_t array for sc_tx_hdrs[slot]
442 * rxq_dmamaps[slot]: bus_dmamap_t array for received payload
443 * txq_dmamaps[slot]: bus_dmamap_t array for sent payload
444 * rxq_mbufs[slot]: mbuf pointer array for received frames
445 * txq_mbufs[slot]: mbuf pointer array for sent frames
446 */
447 static int
448 vioif_alloc_mems(struct vioif_softc *sc)
449 {
450 struct virtio_softc *vsc = sc->sc_virtio;
451 struct vioif_txqueue *txq;
452 struct vioif_rxqueue *rxq;
453 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
454 int allocsize, allocsize2, r, rsegs, i, qid;
455 void *vaddr;
456 intptr_t p;
457
458 allocsize = 0;
459 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
460 rxq = &sc->sc_rxq[qid];
461 txq = &sc->sc_txq[qid];
462
463 allocsize +=
464 sizeof(struct virtio_net_hdr) * rxq->rxq_vq->vq_num;
465 allocsize +=
466 sizeof(struct virtio_net_hdr) * txq->txq_vq->vq_num;
467 }
468 if (sc->sc_has_ctrl) {
469 allocsize += sizeof(struct virtio_net_ctrl_cmd) * 1;
470 allocsize += sizeof(struct virtio_net_ctrl_status) * 1;
471 allocsize += sizeof(struct virtio_net_ctrl_rx) * 1;
472 allocsize += sizeof(struct virtio_net_ctrl_mac_tbl)
473 + sizeof(struct virtio_net_ctrl_mac_tbl)
474 + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES;
475 allocsize += sizeof(struct virtio_net_ctrl_mq) * 1;
476 }
477 r = bus_dmamem_alloc(virtio_dmat(vsc), allocsize, 0, 0,
478 &sc->sc_hdr_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
479 if (r != 0) {
480 aprint_error_dev(sc->sc_dev,
481 "DMA memory allocation failed, size %d, "
482 "error code %d\n", allocsize, r);
483 goto err_none;
484 }
485 r = bus_dmamem_map(virtio_dmat(vsc),
486 &sc->sc_hdr_segs[0], 1, allocsize, &vaddr, BUS_DMA_NOWAIT);
487 if (r != 0) {
488 aprint_error_dev(sc->sc_dev,
489 "DMA memory map failed, error code %d\n", r);
490 goto err_dmamem_alloc;
491 }
492
493 #define P(p, p0, p0size) do { p0 = (void *) p; \
494 p += p0size; } while (0)
495 memset(vaddr, 0, allocsize);
496 sc->sc_dmamem = vaddr;
497 p = (intptr_t) vaddr;
498
499 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
500 rxq = &sc->sc_rxq[qid];
501 txq = &sc->sc_txq[qid];
502
503 P(p, rxq->rxq_hdrs,
504 sizeof(rxq->rxq_hdrs[0]) * rxq->rxq_vq->vq_num);
505 P(p, txq->txq_hdrs,
506 sizeof(txq->txq_hdrs[0]) * txq->txq_vq->vq_num);
507 }
508 if (sc->sc_has_ctrl) {
509 P(p, ctrlq->ctrlq_cmd, sizeof(*ctrlq->ctrlq_cmd));
510 P(p, ctrlq->ctrlq_status, sizeof(*ctrlq->ctrlq_status));
511 P(p, ctrlq->ctrlq_rx, sizeof(*ctrlq->ctrlq_rx));
512 P(p, ctrlq->ctrlq_mac_tbl_uc, sizeof(*ctrlq->ctrlq_mac_tbl_uc));
513 P(p, ctrlq->ctrlq_mac_tbl_mc, sizeof(*ctrlq->ctrlq_mac_tbl_mc)
514 + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES);
515 P(p, ctrlq->ctrlq_mq, sizeof(*ctrlq->ctrlq_mq));
516 }
517
518 allocsize2 = 0;
519 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
520 int rxqsize, txqsize;
521
522 rxq = &sc->sc_rxq[qid];
523 txq = &sc->sc_txq[qid];
524 rxqsize = rxq->rxq_vq->vq_num;
525 txqsize = txq->txq_vq->vq_num;
526
527 allocsize2 += sizeof(rxq->rxq_dmamaps[0]) * rxqsize;
528 allocsize2 += sizeof(rxq->rxq_hdr_dmamaps[0]) * rxqsize;
529 allocsize2 += sizeof(rxq->rxq_mbufs[0]) * rxqsize;
530
531 allocsize2 += sizeof(txq->txq_dmamaps[0]) * txqsize;
532 allocsize2 += sizeof(txq->txq_hdr_dmamaps[0]) * txqsize;
533 allocsize2 += sizeof(txq->txq_mbufs[0]) * txqsize;
534 }
535 vaddr = kmem_zalloc(allocsize2, KM_SLEEP);
536 sc->sc_kmem = vaddr;
537 p = (intptr_t) vaddr;
538
539 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
540 int rxqsize, txqsize;
541 rxq = &sc->sc_rxq[qid];
542 txq = &sc->sc_txq[qid];
543 rxqsize = rxq->rxq_vq->vq_num;
544 txqsize = txq->txq_vq->vq_num;
545
546 P(p, rxq->rxq_hdr_dmamaps,
547 sizeof(rxq->rxq_hdr_dmamaps[0]) * rxqsize);
548 P(p, txq->txq_hdr_dmamaps,
549 sizeof(txq->txq_hdr_dmamaps[0]) * txqsize);
550 P(p, rxq->rxq_dmamaps, sizeof(rxq->rxq_dmamaps[0]) * rxqsize);
551 P(p, txq->txq_dmamaps, sizeof(txq->txq_dmamaps[0]) * txqsize);
552 P(p, rxq->rxq_mbufs, sizeof(rxq->rxq_mbufs[0]) * rxqsize);
553 P(p, txq->txq_mbufs, sizeof(txq->txq_mbufs[0]) * txqsize);
554 }
555 #undef P
556
557 #define C(map, size, nsegs, usage) \
558 do { \
559 r = bus_dmamap_create(virtio_dmat(vsc), size, nsegs, size, 0, \
560 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, \
561 &map); \
562 if (r != 0) { \
563 aprint_error_dev(sc->sc_dev, \
564 usage " dmamap creation failed, " \
565 "error code %d\n", r); \
566 goto err_reqs; \
567 } \
568 } while (0)
569 #define C_L(map, buf, size, nsegs, rw, usage) \
570 C(map, size, nsegs, usage); \
571 do { \
572 r = bus_dmamap_load(virtio_dmat(vsc), map, \
573 buf, size, NULL, \
574 rw | BUS_DMA_NOWAIT); \
575 if (r != 0) { \
576 aprint_error_dev(sc->sc_dev, \
577 usage " dmamap load failed, " \
578 "error code %d\n", r); \
579 goto err_reqs; \
580 } \
581 } while (0)
582
583 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
584 rxq = &sc->sc_rxq[qid];
585 txq = &sc->sc_txq[qid];
586
587 for (i = 0; i < rxq->rxq_vq->vq_num; i++) {
588 C_L(rxq->rxq_hdr_dmamaps[i], &rxq->rxq_hdrs[i],
589 sizeof(rxq->rxq_hdrs[0]), 1,
590 BUS_DMA_READ, "rx header");
591 C(rxq->rxq_dmamaps[i], MCLBYTES, 1, "rx payload");
592 }
593
594 for (i = 0; i < txq->txq_vq->vq_num; i++) {
595 C_L(txq->txq_hdr_dmamaps[i], &txq->txq_hdrs[i],
596 sizeof(txq->txq_hdrs[0]), 1,
597 BUS_DMA_READ, "tx header");
598 C(txq->txq_dmamaps[i], ETHER_MAX_LEN,
599 VIRTIO_NET_TX_MAXNSEGS, "tx payload");
600 }
601 }
602
603 if (sc->sc_has_ctrl) {
604 /* control vq class & command */
605 C_L(ctrlq->ctrlq_cmd_dmamap,
606 ctrlq->ctrlq_cmd, sizeof(*ctrlq->ctrlq_cmd), 1,
607 BUS_DMA_WRITE, "control command");
608 C_L(ctrlq->ctrlq_status_dmamap,
609 ctrlq->ctrlq_status, sizeof(*ctrlq->ctrlq_status), 1,
610 BUS_DMA_READ, "control status");
611
612 /* control vq rx mode command parameter */
613 C_L(ctrlq->ctrlq_rx_dmamap,
614 ctrlq->ctrlq_rx, sizeof(*ctrlq->ctrlq_rx), 1,
615 BUS_DMA_WRITE, "rx mode control command");
616
617 /* multiqueue set command */
618 C_L(ctrlq->ctrlq_mq_dmamap,
619 ctrlq->ctrlq_mq, sizeof(*ctrlq->ctrlq_mq), 1,
620 BUS_DMA_WRITE, "multiqueue set command");
621
622 /* control vq MAC filter table for unicast */
623 /* do not load now since its length is variable */
624 C(ctrlq->ctrlq_tbl_uc_dmamap,
625 sizeof(*ctrlq->ctrlq_mac_tbl_uc) + 0, 1,
626 "unicast MAC address filter command");
627
628 /* control vq MAC filter table for multicast */
629 C(ctrlq->ctrlq_tbl_mc_dmamap,
630 sizeof(*ctrlq->ctrlq_mac_tbl_mc)
631 + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES, 1,
632 "multicast MAC address filter command");
633 }
634 #undef C_L
635 #undef C
636
637 return 0;
638
639 err_reqs:
640 #define D(map) \
641 do { \
642 if (map) { \
643 bus_dmamap_destroy(virtio_dmat(vsc), map); \
644 map = NULL; \
645 } \
646 } while (0)
647 D(ctrlq->ctrlq_tbl_mc_dmamap);
648 D(ctrlq->ctrlq_tbl_uc_dmamap);
649 D(ctrlq->ctrlq_rx_dmamap);
650 D(ctrlq->ctrlq_status_dmamap);
651 D(ctrlq->ctrlq_cmd_dmamap);
652 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
653 rxq = &sc->sc_rxq[qid];
654 txq = &sc->sc_txq[qid];
655
656 for (i = 0; i < txq->txq_vq->vq_num; i++) {
657 D(txq->txq_dmamaps[i]);
658 D(txq->txq_hdr_dmamaps[i]);
659 }
660 for (i = 0; i < rxq->rxq_vq->vq_num; i++) {
661 D(rxq->rxq_dmamaps[i]);
662 D(rxq->rxq_hdr_dmamaps[i]);
663 }
664 }
665 #undef D
666 if (sc->sc_kmem) {
667 kmem_free(sc->sc_kmem, allocsize2);
668 sc->sc_kmem = NULL;
669 }
670 bus_dmamem_unmap(virtio_dmat(vsc), sc->sc_dmamem, allocsize);
671 err_dmamem_alloc:
672 bus_dmamem_free(virtio_dmat(vsc), &sc->sc_hdr_segs[0], 1);
673 err_none:
674 return -1;
675 }
676
677 static void
678 vioif_attach(device_t parent, device_t self, void *aux)
679 {
680 struct vioif_softc *sc = device_private(self);
681 struct virtio_softc *vsc = device_private(parent);
682 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
683 struct vioif_txqueue *txq;
684 struct vioif_rxqueue *rxq;
685 uint32_t features, req_features;
686 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
687 u_int softint_flags;
688 int r, i, nvqs=0, req_flags;
689
690 if (virtio_child(vsc) != NULL) {
691 aprint_normal(": child already attached for %s; "
692 "something wrong...\n", device_xname(parent));
693 return;
694 }
695
696 sc->sc_dev = self;
697 sc->sc_virtio = vsc;
698 sc->sc_link_active = false;
699
700 sc->sc_max_nvq_pairs = 1;
701 sc->sc_req_nvq_pairs = 1;
702 sc->sc_act_nvq_pairs = 1;
703
704 req_flags = 0;
705
706 #ifdef VIOIF_MPSAFE
707 req_flags |= VIRTIO_F_PCI_INTR_MPSAFE;
708 #endif
709 #ifdef VIOIF_SOFTINT_INTR
710 req_flags |= VIRTIO_F_PCI_INTR_SOFTINT;
711 #endif
712 req_flags |= VIRTIO_F_PCI_INTR_MSIX;
713
714 req_features =
715 VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_NET_F_CTRL_VQ |
716 VIRTIO_NET_F_CTRL_RX | VIRTIO_F_NOTIFY_ON_EMPTY;
717 #ifdef VIOIF_MULTIQ
718 req_features |= VIRTIO_NET_F_MQ;
719 #endif
720 virtio_child_attach_start(vsc, self, IPL_NET, NULL,
721 vioif_config_change, virtio_vq_intr, req_flags,
722 req_features, VIRTIO_NET_FLAG_BITS);
723
724 features = virtio_features(vsc);
725
726 if (features & VIRTIO_NET_F_MAC) {
727 for (i = 0; i < __arraycount(sc->sc_mac); i++) {
728 sc->sc_mac[i] = virtio_read_device_config_1(vsc,
729 VIRTIO_NET_CONFIG_MAC + i);
730 }
731 } else {
732 /* code stolen from sys/net/if_tap.c */
733 struct timeval tv;
734 uint32_t ui;
735 getmicrouptime(&tv);
736 ui = (tv.tv_sec ^ tv.tv_usec) & 0xffffff;
737 memcpy(sc->sc_mac+3, (uint8_t *)&ui, 3);
738 for (i = 0; i < __arraycount(sc->sc_mac); i++) {
739 virtio_write_device_config_1(vsc,
740 VIRTIO_NET_CONFIG_MAC + i, sc->sc_mac[i]);
741 }
742 }
743
744 aprint_normal_dev(self, "Ethernet address %s\n",
745 ether_sprintf(sc->sc_mac));
746
747 if ((features & VIRTIO_NET_F_CTRL_VQ) &&
748 (features & VIRTIO_NET_F_CTRL_RX)) {
749 sc->sc_has_ctrl = true;
750
751 cv_init(&ctrlq->ctrlq_wait, "ctrl_vq");
752 mutex_init(&ctrlq->ctrlq_wait_lock, MUTEX_DEFAULT, IPL_NET);
753 ctrlq->ctrlq_inuse = FREE;
754 } else {
755 sc->sc_has_ctrl = false;
756 }
757
758 if (sc->sc_has_ctrl && (features & VIRTIO_NET_F_MQ)) {
759 sc->sc_max_nvq_pairs = virtio_read_device_config_2(vsc,
760 VIRTIO_NET_CONFIG_MAX_VQ_PAIRS);
761
762 if (sc->sc_max_nvq_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX)
763 goto err;
764
765 /* Limit the number of queue pairs to use */
766 sc->sc_req_nvq_pairs = MIN(sc->sc_max_nvq_pairs, ncpu);
767 }
768
769 r = vioif_alloc_queues(sc);
770 if (r != 0)
771 goto err;
772
773 virtio_child_attach_set_vqs(vsc, sc->sc_vqs, sc->sc_req_nvq_pairs);
774
775 #ifdef VIOIF_MPSAFE
776 softint_flags = SOFTINT_NET | SOFTINT_MPSAFE;
777 #else
778 softint_flags = SOFTINT_NET;
779 #endif
780
781 /*
782 * Allocating a virtqueues
783 */
784 for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
785 rxq = &sc->sc_rxq[i];
786 txq = &sc->sc_txq[i];
787 char qname[32];
788
789 rxq->rxq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
790
791 rxq->rxq_softint = softint_establish(softint_flags,
792 vioif_rx_softint, rxq);
793 if (rxq->rxq_softint == NULL) {
794 aprint_error_dev(self, "cannot establish rx softint\n");
795 goto err;
796 }
797 snprintf(qname, sizeof(qname), "rx%d", i);
798 r = virtio_alloc_vq(vsc, rxq->rxq_vq, nvqs,
799 MCLBYTES+sizeof(struct virtio_net_hdr), nvqs, qname);
800 if (r != 0)
801 goto err;
802 nvqs++;
803 rxq->rxq_vq->vq_done = vioif_rx_vq_done;
804 rxq->rxq_vq->vq_done_ctx = (void *)rxq;
805 rxq->rxq_stopping = true;
806
807 txq->txq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
808 txq->txq_deferred_transmit = softint_establish(softint_flags,
809 vioif_deferred_transmit, txq);
810 if (txq->txq_deferred_transmit == NULL) {
811 aprint_error_dev(self, "cannot establish tx softint\n");
812 goto err;
813 }
814 snprintf(qname, sizeof(qname), "tx%d", i);
815 r = virtio_alloc_vq(vsc, txq->txq_vq, nvqs,
816 sizeof(struct virtio_net_hdr)
817 + (ETHER_MAX_LEN - ETHER_HDR_LEN),
818 VIRTIO_NET_TX_MAXNSEGS + 1, qname);
819 if (r != 0)
820 goto err;
821 nvqs++;
822 txq->txq_vq->vq_done = vioif_tx_vq_done;
823 txq->txq_vq->vq_done_ctx = (void *)txq;
824 txq->txq_link_active = sc->sc_link_active;
825 txq->txq_stopping = false;
826 txq->txq_intrq = pcq_create(txq->txq_vq->vq_num, KM_NOSLEEP);
827 if (txq->txq_intrq == NULL)
828 goto err;
829 }
830
831 if (sc->sc_has_ctrl) {
832 /*
833 * Allocating a virtqueue for control channel
834 */
835 r = virtio_alloc_vq(vsc, ctrlq->ctrlq_vq, nvqs,
836 NBPG, 1, "control");
837 if (r != 0) {
838 aprint_error_dev(self, "failed to allocate "
839 "a virtqueue for control channel, error code %d\n",
840 r);
841
842 sc->sc_has_ctrl = false;
843 cv_destroy(&ctrlq->ctrlq_wait);
844 mutex_destroy(&ctrlq->ctrlq_wait_lock);
845 } else {
846 nvqs++;
847 ctrlq->ctrlq_vq->vq_done = vioif_ctrl_vq_done;
848 ctrlq->ctrlq_vq->vq_done_ctx = (void *) ctrlq;
849 }
850 }
851
852 sc->sc_ctl_softint = softint_establish(softint_flags,
853 vioif_ctl_softint, sc);
854 if (sc->sc_ctl_softint == NULL) {
855 aprint_error_dev(self, "cannot establish ctl softint\n");
856 goto err;
857 }
858
859 if (vioif_alloc_mems(sc) < 0)
860 goto err;
861
862 if (virtio_child_attach_finish(vsc) != 0)
863 goto err;
864
865 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
866 ifp->if_softc = sc;
867 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
868 #ifdef VIOIF_MPSAFE
869 ifp->if_extflags = IFEF_MPSAFE;
870 #endif
871 ifp->if_start = vioif_start;
872 if (sc->sc_req_nvq_pairs > 1)
873 ifp->if_transmit = vioif_transmit;
874 ifp->if_ioctl = vioif_ioctl;
875 ifp->if_init = vioif_init;
876 ifp->if_stop = vioif_stop;
877 ifp->if_capabilities = 0;
878 ifp->if_watchdog = vioif_watchdog;
879 txq = &sc->sc_txq[0];
880 IFQ_SET_MAXLEN(&ifp->if_snd, MAX(txq->txq_vq->vq_num, IFQ_MAXLEN));
881 IFQ_SET_READY(&ifp->if_snd);
882
883 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
884
885 if_attach(ifp);
886 if_deferred_start_init(ifp, NULL);
887 ether_ifattach(ifp, sc->sc_mac);
888
889 return;
890
891 err:
892 for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
893 rxq = &sc->sc_rxq[i];
894 txq = &sc->sc_txq[i];
895
896 if (rxq->rxq_lock) {
897 mutex_obj_free(rxq->rxq_lock);
898 rxq->rxq_lock = NULL;
899 }
900
901 if (rxq->rxq_softint) {
902 softint_disestablish(rxq->rxq_softint);
903 rxq->rxq_softint = NULL;
904 }
905
906 if (txq->txq_lock) {
907 mutex_obj_free(txq->txq_lock);
908 txq->txq_lock = NULL;
909 }
910
911 if (txq->txq_deferred_transmit) {
912 softint_disestablish(txq->txq_deferred_transmit);
913 txq->txq_deferred_transmit = NULL;
914 }
915
916 if (txq->txq_intrq) {
917 pcq_destroy(txq->txq_intrq);
918 txq->txq_intrq = NULL;
919 }
920 }
921
922 if (sc->sc_has_ctrl) {
923 cv_destroy(&ctrlq->ctrlq_wait);
924 mutex_destroy(&ctrlq->ctrlq_wait_lock);
925 }
926
927 while (nvqs > 0)
928 virtio_free_vq(vsc, &sc->sc_vqs[--nvqs]);
929
930 vioif_free_queues(sc);
931
932 virtio_child_attach_failed(vsc);
933 return;
934 }
935
936 /* we need interrupts to make promiscuous mode off */
937 static void
938 vioif_deferred_init(device_t self)
939 {
940 struct vioif_softc *sc = device_private(self);
941 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
942 int r;
943
944 if (ifp->if_flags & IFF_PROMISC)
945 return;
946
947 r = vioif_set_promisc(sc, false);
948 if (r != 0)
949 aprint_error_dev(self, "resetting promisc mode failed, "
950 "error code %d\n", r);
951 }
952
953 static void
954 vioif_enable_interrupt_vqpairs(struct vioif_softc *sc)
955 {
956 struct virtio_softc *vsc = sc->sc_virtio;
957 struct vioif_txqueue *txq;
958 struct vioif_rxqueue *rxq;
959 int i;
960
961 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
962 txq = &sc->sc_txq[i];
963 rxq = &sc->sc_rxq[i];
964
965 virtio_start_vq_intr(vsc, txq->txq_vq);
966 virtio_start_vq_intr(vsc, rxq->rxq_vq);
967 }
968 }
969
970 static void
971 vioif_disable_interrupt_vqpairs(struct vioif_softc *sc)
972 {
973 struct virtio_softc *vsc = sc->sc_virtio;
974 struct vioif_txqueue *txq;
975 struct vioif_rxqueue *rxq;
976 int i;
977
978 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
979 txq = &sc->sc_txq[i];
980 rxq = &sc->sc_rxq[i];
981
982 virtio_stop_vq_intr(vsc, txq->txq_vq);
983 virtio_stop_vq_intr(vsc, rxq->rxq_vq);
984 }
985 }
986
987 /*
988 * Interface functions for ifnet
989 */
990 static int
991 vioif_init(struct ifnet *ifp)
992 {
993 struct vioif_softc *sc = ifp->if_softc;
994 struct virtio_softc *vsc = sc->sc_virtio;
995 struct vioif_rxqueue *rxq;
996 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
997 int r, i;
998
999 vioif_stop(ifp, 0);
1000
1001 virtio_reinit_start(vsc);
1002 virtio_negotiate_features(vsc, virtio_features(vsc));
1003
1004 for (i = 0; i < sc->sc_req_nvq_pairs; i++) {
1005 rxq = &sc->sc_rxq[i];
1006
1007 /* Have to set false before vioif_populate_rx_mbufs */
1008 rxq->rxq_stopping = false;
1009 vioif_populate_rx_mbufs(rxq);
1010 }
1011
1012 virtio_reinit_end(vsc);
1013
1014 if (sc->sc_has_ctrl)
1015 virtio_start_vq_intr(vsc, ctrlq->ctrlq_vq);
1016
1017 r = vioif_ctrl_mq_vq_pairs_set(sc, sc->sc_req_nvq_pairs);
1018 if (r == 0)
1019 sc->sc_act_nvq_pairs = sc->sc_req_nvq_pairs;
1020 else
1021 sc->sc_act_nvq_pairs = 1;
1022
1023 for (i = 0; i < sc->sc_act_nvq_pairs; i++)
1024 sc->sc_txq[i].txq_stopping = false;
1025
1026 vioif_enable_interrupt_vqpairs(sc);
1027
1028 if (!sc->sc_deferred_init_done) {
1029 sc->sc_deferred_init_done = 1;
1030 if (sc->sc_has_ctrl)
1031 vioif_deferred_init(sc->sc_dev);
1032 }
1033
1034 vioif_update_link_status(sc);
1035 ifp->if_flags |= IFF_RUNNING;
1036 ifp->if_flags &= ~IFF_OACTIVE;
1037 vioif_rx_filter(sc);
1038
1039 return 0;
1040 }
1041
1042 static void
1043 vioif_stop(struct ifnet *ifp, int disable)
1044 {
1045 struct vioif_softc *sc = ifp->if_softc;
1046 struct virtio_softc *vsc = sc->sc_virtio;
1047 struct vioif_txqueue *txq;
1048 struct vioif_rxqueue *rxq;
1049 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1050 int i;
1051
1052 /* Take the locks to ensure that ongoing TX/RX finish */
1053 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1054 txq = &sc->sc_txq[i];
1055 rxq = &sc->sc_rxq[i];
1056
1057 mutex_enter(txq->txq_lock);
1058 txq->txq_stopping = true;
1059 mutex_exit(txq->txq_lock);
1060
1061 mutex_enter(rxq->rxq_lock);
1062 rxq->rxq_stopping = true;
1063 mutex_exit(rxq->rxq_lock);
1064 }
1065
1066 /* disable interrupts */
1067 vioif_disable_interrupt_vqpairs(sc);
1068
1069 if (sc->sc_has_ctrl)
1070 virtio_stop_vq_intr(vsc, ctrlq->ctrlq_vq);
1071
1072 /* only way to stop I/O and DMA is resetting... */
1073 virtio_reset(vsc);
1074 for (i = 0; i < sc->sc_act_nvq_pairs; i++)
1075 vioif_rx_deq(&sc->sc_rxq[i]);
1076
1077 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1078 sc->sc_link_active = false;
1079
1080 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1081 txq = &sc->sc_txq[i];
1082 rxq = &sc->sc_rxq[i];
1083
1084 txq->txq_link_active = false;
1085
1086 if (disable)
1087 vioif_rx_drain(rxq);
1088
1089 vioif_tx_drain(txq);
1090 }
1091 }
1092
1093 static void
1094 vioif_send_common_locked(struct ifnet *ifp, struct vioif_txqueue *txq,
1095 bool is_transmit)
1096 {
1097 struct vioif_softc *sc = ifp->if_softc;
1098 struct virtio_softc *vsc = sc->sc_virtio;
1099 struct virtqueue *vq = txq->txq_vq;
1100 struct mbuf *m;
1101 int queued = 0;
1102
1103 KASSERT(mutex_owned(txq->txq_lock));
1104
1105 if ((ifp->if_flags & IFF_RUNNING) == 0)
1106 return;
1107
1108 if (!txq->txq_link_active || txq->txq_stopping)
1109 return;
1110
1111 if ((ifp->if_flags & IFF_OACTIVE) != 0 && !is_transmit)
1112 return;
1113
1114 for (;;) {
1115 int slot, r;
1116
1117 if (is_transmit)
1118 m = pcq_get(txq->txq_intrq);
1119 else
1120 IFQ_DEQUEUE(&ifp->if_snd, m);
1121
1122 if (m == NULL)
1123 break;
1124
1125 r = virtio_enqueue_prep(vsc, vq, &slot);
1126 if (r == EAGAIN) {
1127 ifp->if_flags |= IFF_OACTIVE;
1128 m_freem(m);
1129 break;
1130 }
1131 if (r != 0)
1132 panic("enqueue_prep for a tx buffer");
1133
1134 r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
1135 txq->txq_dmamaps[slot], m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1136 if (r != 0) {
1137 /* maybe just too fragmented */
1138 struct mbuf *newm;
1139
1140 newm = m_defrag(m, M_NOWAIT);
1141 if (newm == NULL) {
1142 aprint_error_dev(sc->sc_dev,
1143 "m_defrag() failed\n");
1144 goto skip;
1145 }
1146
1147 m = newm;
1148 r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
1149 txq->txq_dmamaps[slot], m,
1150 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1151 if (r != 0) {
1152 aprint_error_dev(sc->sc_dev,
1153 "tx dmamap load failed, error code %d\n",
1154 r);
1155 skip:
1156 m_freem(m);
1157 virtio_enqueue_abort(vsc, vq, slot);
1158 continue;
1159 }
1160 }
1161
1162 /* This should actually never fail */
1163 r = virtio_enqueue_reserve(vsc, vq, slot,
1164 txq->txq_dmamaps[slot]->dm_nsegs + 1);
1165 if (r != 0) {
1166 aprint_error_dev(sc->sc_dev,
1167 "virtio_enqueue_reserve failed, error code %d\n",
1168 r);
1169 bus_dmamap_unload(virtio_dmat(vsc),
1170 txq->txq_dmamaps[slot]);
1171 /* slot already freed by virtio_enqueue_reserve */
1172 m_freem(m);
1173 continue;
1174 }
1175
1176 txq->txq_mbufs[slot] = m;
1177
1178 memset(&txq->txq_hdrs[slot], 0, sizeof(struct virtio_net_hdr));
1179 bus_dmamap_sync(virtio_dmat(vsc), txq->txq_dmamaps[slot],
1180 0, txq->txq_dmamaps[slot]->dm_mapsize,
1181 BUS_DMASYNC_PREWRITE);
1182 bus_dmamap_sync(virtio_dmat(vsc), txq->txq_hdr_dmamaps[slot],
1183 0, txq->txq_hdr_dmamaps[slot]->dm_mapsize,
1184 BUS_DMASYNC_PREWRITE);
1185 virtio_enqueue(vsc, vq, slot, txq->txq_hdr_dmamaps[slot], true);
1186 virtio_enqueue(vsc, vq, slot, txq->txq_dmamaps[slot], true);
1187 virtio_enqueue_commit(vsc, vq, slot, false);
1188
1189 queued++;
1190 bpf_mtap(ifp, m, BPF_D_OUT);
1191 }
1192
1193 if (queued > 0) {
1194 virtio_enqueue_commit(vsc, vq, -1, true);
1195 ifp->if_timer = 5;
1196 }
1197 }
1198
1199 static void
1200 vioif_start_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
1201 {
1202
1203 /*
1204 * ifp->if_obytes and ifp->if_omcasts are added in if_transmit()@if.c.
1205 */
1206 vioif_send_common_locked(ifp, txq, false);
1207
1208 }
1209
1210 static void
1211 vioif_start(struct ifnet *ifp)
1212 {
1213 struct vioif_softc *sc = ifp->if_softc;
1214 struct vioif_txqueue *txq = &sc->sc_txq[0];
1215
1216 #ifdef VIOIF_MPSAFE
1217 KASSERT(if_is_mpsafe(ifp));
1218 #endif
1219
1220 mutex_enter(txq->txq_lock);
1221 if (!txq->txq_stopping)
1222 vioif_start_locked(ifp, txq);
1223 mutex_exit(txq->txq_lock);
1224 }
1225
1226 static inline int
1227 vioif_select_txqueue(struct ifnet *ifp, struct mbuf *m)
1228 {
1229 struct vioif_softc *sc = ifp->if_softc;
1230 u_int cpuid = cpu_index(curcpu());
1231
1232 return cpuid % sc->sc_act_nvq_pairs;
1233 }
1234
1235 static void
1236 vioif_transmit_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
1237 {
1238
1239 vioif_send_common_locked(ifp, txq, true);
1240 }
1241
1242 static int
1243 vioif_transmit(struct ifnet *ifp, struct mbuf *m)
1244 {
1245 struct vioif_softc *sc = ifp->if_softc;
1246 struct vioif_txqueue *txq;
1247 int qid;
1248
1249 qid = vioif_select_txqueue(ifp, m);
1250 txq = &sc->sc_txq[qid];
1251
1252 if (__predict_false(!pcq_put(txq->txq_intrq, m))) {
1253 m_freem(m);
1254 return ENOBUFS;
1255 }
1256
1257 ifp->if_obytes += m->m_pkthdr.len;
1258 if (m->m_flags & M_MCAST)
1259 ifp->if_omcasts++;
1260
1261 if (mutex_tryenter(txq->txq_lock)) {
1262 if (!txq->txq_stopping)
1263 vioif_transmit_locked(ifp, txq);
1264 mutex_exit(txq->txq_lock);
1265 }
1266
1267 return 0;
1268 }
1269
1270 static void
1271 vioif_deferred_transmit(void *arg)
1272 {
1273 struct vioif_txqueue *txq = arg;
1274 struct virtio_softc *vsc = txq->txq_vq->vq_owner;
1275 struct vioif_softc *sc = device_private(virtio_child(vsc));
1276 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1277
1278 if (mutex_tryenter(txq->txq_lock)) {
1279 vioif_send_common_locked(ifp, txq, true);
1280 mutex_exit(txq->txq_lock);
1281 }
1282 }
1283
1284 static int
1285 vioif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1286 {
1287 int s, r;
1288
1289 s = splnet();
1290
1291 r = ether_ioctl(ifp, cmd, data);
1292 if ((r == 0 && cmd == SIOCSIFFLAGS) ||
1293 (r == ENETRESET && (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI))) {
1294 if (ifp->if_flags & IFF_RUNNING)
1295 r = vioif_rx_filter(ifp->if_softc);
1296 else
1297 r = 0;
1298 }
1299
1300 splx(s);
1301
1302 return r;
1303 }
1304
1305 void
1306 vioif_watchdog(struct ifnet *ifp)
1307 {
1308 struct vioif_softc *sc = ifp->if_softc;
1309 int i;
1310
1311 if (ifp->if_flags & IFF_RUNNING) {
1312 for (i = 0; i < sc->sc_act_nvq_pairs; i++)
1313 vioif_tx_vq_done(sc->sc_txq[i].txq_vq);
1314 }
1315 }
1316
1317
1318 /*
1319 * Receive implementation
1320 */
1321 /* allocate and initialize a mbuf for receive */
1322 static int
1323 vioif_add_rx_mbuf(struct vioif_rxqueue *rxq, int i)
1324 {
1325 struct virtio_softc *vsc = rxq->rxq_vq->vq_owner;
1326 struct mbuf *m;
1327 int r;
1328
1329 MGETHDR(m, M_DONTWAIT, MT_DATA);
1330 if (m == NULL)
1331 return ENOBUFS;
1332 MCLGET(m, M_DONTWAIT);
1333 if ((m->m_flags & M_EXT) == 0) {
1334 m_freem(m);
1335 return ENOBUFS;
1336 }
1337 rxq->rxq_mbufs[i] = m;
1338 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
1339 r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
1340 rxq->rxq_dmamaps[i], m, BUS_DMA_READ | BUS_DMA_NOWAIT);
1341 if (r) {
1342 m_freem(m);
1343 rxq->rxq_mbufs[i] = NULL;
1344 return r;
1345 }
1346
1347 return 0;
1348 }
1349
1350 /* free a mbuf for receive */
1351 static void
1352 vioif_free_rx_mbuf(struct vioif_rxqueue *rxq, int i)
1353 {
1354 struct virtio_softc *vsc = rxq->rxq_vq->vq_owner;
1355
1356 bus_dmamap_unload(virtio_dmat(vsc), rxq->rxq_dmamaps[i]);
1357 m_freem(rxq->rxq_mbufs[i]);
1358 rxq->rxq_mbufs[i] = NULL;
1359 }
1360
1361 /* add mbufs for all the empty receive slots */
1362 static void
1363 vioif_populate_rx_mbufs(struct vioif_rxqueue *rxq)
1364 {
1365
1366 mutex_enter(rxq->rxq_lock);
1367 vioif_populate_rx_mbufs_locked(rxq);
1368 mutex_exit(rxq->rxq_lock);
1369 }
1370
1371 static void
1372 vioif_populate_rx_mbufs_locked(struct vioif_rxqueue *rxq)
1373 {
1374 struct virtqueue *vq = rxq->rxq_vq;
1375 struct virtio_softc *vsc = vq->vq_owner;
1376 struct vioif_softc *sc = device_private(virtio_child(vsc));
1377 int i, r, ndone = 0;
1378
1379 KASSERT(mutex_owned(rxq->rxq_lock));
1380
1381 if (rxq->rxq_stopping)
1382 return;
1383
1384 for (i = 0; i < vq->vq_num; i++) {
1385 int slot;
1386 r = virtio_enqueue_prep(vsc, vq, &slot);
1387 if (r == EAGAIN)
1388 break;
1389 if (r != 0)
1390 panic("enqueue_prep for rx buffers");
1391 if (rxq->rxq_mbufs[slot] == NULL) {
1392 r = vioif_add_rx_mbuf(rxq, slot);
1393 if (r != 0) {
1394 aprint_error_dev(sc->sc_dev,
1395 "rx mbuf allocation failed, "
1396 "error code %d\n", r);
1397 break;
1398 }
1399 }
1400 r = virtio_enqueue_reserve(vsc, vq, slot,
1401 rxq->rxq_dmamaps[slot]->dm_nsegs + 1);
1402 if (r != 0) {
1403 vioif_free_rx_mbuf(rxq, slot);
1404 break;
1405 }
1406 bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_hdr_dmamaps[slot],
1407 0, sizeof(struct virtio_net_hdr), BUS_DMASYNC_PREREAD);
1408 bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_dmamaps[slot],
1409 0, MCLBYTES, BUS_DMASYNC_PREREAD);
1410 virtio_enqueue(vsc, vq, slot, rxq->rxq_hdr_dmamaps[slot],
1411 false);
1412 virtio_enqueue(vsc, vq, slot, rxq->rxq_dmamaps[slot], false);
1413 virtio_enqueue_commit(vsc, vq, slot, false);
1414 ndone++;
1415 }
1416 if (ndone > 0)
1417 virtio_enqueue_commit(vsc, vq, -1, true);
1418 }
1419
1420 /* dequeue received packets */
1421 static int
1422 vioif_rx_deq(struct vioif_rxqueue *rxq)
1423 {
1424 int r;
1425
1426 KASSERT(rxq->rxq_stopping);
1427
1428 mutex_enter(rxq->rxq_lock);
1429 r = vioif_rx_deq_locked(rxq);
1430 mutex_exit(rxq->rxq_lock);
1431
1432 return r;
1433 }
1434
1435 /* dequeue received packets */
1436 static int
1437 vioif_rx_deq_locked(struct vioif_rxqueue *rxq)
1438 {
1439 struct virtqueue *vq = rxq->rxq_vq;
1440 struct virtio_softc *vsc = vq->vq_owner;
1441 struct vioif_softc *sc = device_private(virtio_child(vsc));
1442 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1443 struct mbuf *m;
1444 int r = 0;
1445 int slot, len;
1446
1447 KASSERT(mutex_owned(rxq->rxq_lock));
1448
1449 while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
1450 len -= sizeof(struct virtio_net_hdr);
1451 r = 1;
1452 bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_hdr_dmamaps[slot],
1453 0, sizeof(struct virtio_net_hdr), BUS_DMASYNC_POSTREAD);
1454 bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_dmamaps[slot],
1455 0, MCLBYTES, BUS_DMASYNC_POSTREAD);
1456 m = rxq->rxq_mbufs[slot];
1457 KASSERT(m != NULL);
1458 bus_dmamap_unload(virtio_dmat(vsc), rxq->rxq_dmamaps[slot]);
1459 rxq->rxq_mbufs[slot] = NULL;
1460 virtio_dequeue_commit(vsc, vq, slot);
1461 m_set_rcvif(m, ifp);
1462 m->m_len = m->m_pkthdr.len = len;
1463
1464 mutex_exit(rxq->rxq_lock);
1465 if_percpuq_enqueue(ifp->if_percpuq, m);
1466 mutex_enter(rxq->rxq_lock);
1467
1468 if (rxq->rxq_stopping)
1469 break;
1470 }
1471
1472 return r;
1473 }
1474
1475 /* rx interrupt; call _dequeue above and schedule a softint */
1476 static int
1477 vioif_rx_vq_done(struct virtqueue *vq)
1478 {
1479 struct vioif_rxqueue *rxq = vq->vq_done_ctx;
1480 int r = 0;
1481
1482 #ifdef VIOIF_SOFTINT_INTR
1483 KASSERT(!cpu_intr_p());
1484 #endif
1485
1486 mutex_enter(rxq->rxq_lock);
1487
1488 if (rxq->rxq_stopping)
1489 goto out;
1490
1491 r = vioif_rx_deq_locked(rxq);
1492 if (r)
1493 #ifdef VIOIF_SOFTINT_INTR
1494 vioif_populate_rx_mbufs_locked(rxq);
1495 #else
1496 softint_schedule(rxq->rxq_softint);
1497 #endif
1498
1499 out:
1500 mutex_exit(rxq->rxq_lock);
1501 return r;
1502 }
1503
1504 /* softint: enqueue receive requests for new incoming packets */
1505 static void
1506 vioif_rx_softint(void *arg)
1507 {
1508 struct vioif_rxqueue *rxq = arg;
1509
1510 vioif_populate_rx_mbufs(rxq);
1511 }
1512
1513 /* free all the mbufs; called from if_stop(disable) */
1514 static void
1515 vioif_rx_drain(struct vioif_rxqueue *rxq)
1516 {
1517 struct virtqueue *vq = rxq->rxq_vq;
1518 int i;
1519
1520 for (i = 0; i < vq->vq_num; i++) {
1521 if (rxq->rxq_mbufs[i] == NULL)
1522 continue;
1523 vioif_free_rx_mbuf(rxq, i);
1524 }
1525 }
1526
1527
1528 /*
1529 * Transmition implementation
1530 */
1531 /* actual transmission is done in if_start */
1532 /* tx interrupt; dequeue and free mbufs */
1533 /*
1534 * tx interrupt is actually disabled; this should be called upon
1535 * tx vq full and watchdog
1536 */
1537 static int
1538 vioif_tx_vq_done(struct virtqueue *vq)
1539 {
1540 struct virtio_softc *vsc = vq->vq_owner;
1541 struct vioif_softc *sc = device_private(virtio_child(vsc));
1542 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1543 struct vioif_txqueue *txq = vq->vq_done_ctx;
1544 int r = 0;
1545
1546 mutex_enter(txq->txq_lock);
1547
1548 if (txq->txq_stopping)
1549 goto out;
1550
1551 r = vioif_tx_vq_done_locked(vq);
1552
1553 out:
1554 mutex_exit(txq->txq_lock);
1555 if (r) {
1556 if_schedule_deferred_start(ifp);
1557
1558 KASSERT(txq->txq_deferred_transmit != NULL);
1559 softint_schedule(txq->txq_deferred_transmit);
1560 }
1561 return r;
1562 }
1563
1564 static int
1565 vioif_tx_vq_done_locked(struct virtqueue *vq)
1566 {
1567 struct virtio_softc *vsc = vq->vq_owner;
1568 struct vioif_softc *sc = device_private(virtio_child(vsc));
1569 struct vioif_txqueue *txq = vq->vq_done_ctx;
1570 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1571 struct mbuf *m;
1572 int r = 0;
1573 int slot, len;
1574
1575 KASSERT(mutex_owned(txq->txq_lock));
1576
1577 while (virtio_dequeue(vsc, vq, &slot, &len) == 0) {
1578 r++;
1579 bus_dmamap_sync(virtio_dmat(vsc), txq->txq_hdr_dmamaps[slot],
1580 0, sizeof(struct virtio_net_hdr), BUS_DMASYNC_POSTWRITE);
1581 bus_dmamap_sync(virtio_dmat(vsc), txq->txq_dmamaps[slot],
1582 0, txq->txq_dmamaps[slot]->dm_mapsize,
1583 BUS_DMASYNC_POSTWRITE);
1584 m = txq->txq_mbufs[slot];
1585 bus_dmamap_unload(virtio_dmat(vsc), txq->txq_dmamaps[slot]);
1586 txq->txq_mbufs[slot] = NULL;
1587 virtio_dequeue_commit(vsc, vq, slot);
1588 ifp->if_opackets++;
1589 m_freem(m);
1590 }
1591
1592 if (r)
1593 ifp->if_flags &= ~IFF_OACTIVE;
1594 return r;
1595 }
1596
1597 /* free all the mbufs already put on vq; called from if_stop(disable) */
1598 static void
1599 vioif_tx_drain(struct vioif_txqueue *txq)
1600 {
1601 struct virtqueue *vq = txq->txq_vq;
1602 struct virtio_softc *vsc = vq->vq_owner;
1603 int i;
1604
1605 KASSERT(txq->txq_stopping);
1606
1607 for (i = 0; i < vq->vq_num; i++) {
1608 if (txq->txq_mbufs[i] == NULL)
1609 continue;
1610 bus_dmamap_unload(virtio_dmat(vsc), txq->txq_dmamaps[i]);
1611 m_freem(txq->txq_mbufs[i]);
1612 txq->txq_mbufs[i] = NULL;
1613 }
1614 }
1615
1616 /*
1617 * Control vq
1618 */
1619 /* issue a VIRTIO_NET_CTRL_RX class command and wait for completion */
1620 static void
1621 vioif_ctrl_acquire(struct vioif_softc *sc)
1622 {
1623 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1624
1625 mutex_enter(&ctrlq->ctrlq_wait_lock);
1626 while (ctrlq->ctrlq_inuse != FREE)
1627 cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
1628 ctrlq->ctrlq_inuse = INUSE;
1629 ctrlq->ctrlq_owner = curlwp;
1630 mutex_exit(&ctrlq->ctrlq_wait_lock);
1631 }
1632
1633 static void
1634 vioif_ctrl_release(struct vioif_softc *sc)
1635 {
1636 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1637
1638 KASSERT(ctrlq->ctrlq_inuse != FREE);
1639 KASSERT(ctrlq->ctrlq_owner == curlwp);
1640
1641 mutex_enter(&ctrlq->ctrlq_wait_lock);
1642 ctrlq->ctrlq_inuse = FREE;
1643 ctrlq->ctrlq_owner = NULL;
1644 cv_signal(&ctrlq->ctrlq_wait);
1645 mutex_exit(&ctrlq->ctrlq_wait_lock);
1646 }
1647
1648 static int
1649 vioif_ctrl_load_cmdspec(struct vioif_softc *sc,
1650 struct vioif_ctrl_cmdspec *specs, int nspecs)
1651 {
1652 struct virtio_softc *vsc = sc->sc_virtio;
1653 int i, r, loaded;
1654
1655 loaded = 0;
1656 for (i = 0; i < nspecs; i++) {
1657 r = bus_dmamap_load(virtio_dmat(vsc),
1658 specs[i].dmamap, specs[i].buf, specs[i].bufsize,
1659 NULL, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1660 if (r) {
1661 aprint_error_dev(sc->sc_dev, "control command dmamap"
1662 " load failed, error code %d\n", r);
1663 goto err;
1664 }
1665 loaded++;
1666
1667 }
1668
1669 return r;
1670
1671 err:
1672 for (i = 0; i < loaded; i++) {
1673 bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
1674 }
1675
1676 return r;
1677 }
1678
1679 static void
1680 vioif_ctrl_unload_cmdspec(struct vioif_softc *sc,
1681 struct vioif_ctrl_cmdspec *specs, int nspecs)
1682 {
1683 struct virtio_softc *vsc = sc->sc_virtio;
1684 int i;
1685
1686 for (i = 0; i < nspecs; i++) {
1687 bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
1688 }
1689 }
1690
1691 static int
1692 vioif_ctrl_send_command(struct vioif_softc *sc, uint8_t class, uint8_t cmd,
1693 struct vioif_ctrl_cmdspec *specs, int nspecs)
1694 {
1695 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1696 struct virtqueue *vq = ctrlq->ctrlq_vq;
1697 struct virtio_softc *vsc = sc->sc_virtio;
1698 int i, r, slot;
1699
1700 ctrlq->ctrlq_cmd->class = class;
1701 ctrlq->ctrlq_cmd->command = cmd;
1702
1703 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap,
1704 0, sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_PREWRITE);
1705 for (i = 0; i < nspecs; i++) {
1706 bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap,
1707 0, specs[i].bufsize, BUS_DMASYNC_PREWRITE);
1708 }
1709 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap,
1710 0, sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_PREREAD);
1711
1712 r = virtio_enqueue_prep(vsc, vq, &slot);
1713 if (r != 0)
1714 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1715 r = virtio_enqueue_reserve(vsc, vq, slot, nspecs + 2);
1716 if (r != 0)
1717 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
1718 virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_cmd_dmamap, true);
1719 for (i = 0; i < nspecs; i++) {
1720 virtio_enqueue(vsc, vq, slot, specs[i].dmamap, true);
1721 }
1722 virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_status_dmamap, false);
1723 virtio_enqueue_commit(vsc, vq, slot, true);
1724
1725 /* wait for done */
1726 mutex_enter(&ctrlq->ctrlq_wait_lock);
1727 while (ctrlq->ctrlq_inuse != DONE)
1728 cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
1729 mutex_exit(&ctrlq->ctrlq_wait_lock);
1730 /* already dequeueued */
1731
1732 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap, 0,
1733 sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_POSTWRITE);
1734 for (i = 0; i < nspecs; i++) {
1735 bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap, 0,
1736 specs[i].bufsize, BUS_DMASYNC_POSTWRITE);
1737 }
1738 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap, 0,
1739 sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_POSTREAD);
1740
1741 if (ctrlq->ctrlq_status->ack == VIRTIO_NET_OK)
1742 r = 0;
1743 else {
1744 aprint_error_dev(sc->sc_dev, "failed setting rx mode\n");
1745 r = EIO;
1746 }
1747
1748 return r;
1749 }
1750
1751 static int
1752 vioif_ctrl_rx(struct vioif_softc *sc, int cmd, bool onoff)
1753 {
1754 struct virtio_net_ctrl_rx *rx = sc->sc_ctrlq.ctrlq_rx;
1755 struct vioif_ctrl_cmdspec specs[1];
1756 int r;
1757
1758 if (!sc->sc_has_ctrl)
1759 return ENOTSUP;
1760
1761 vioif_ctrl_acquire(sc);
1762
1763 rx->onoff = onoff;
1764 specs[0].dmamap = sc->sc_ctrlq.ctrlq_rx_dmamap;
1765 specs[0].buf = rx;
1766 specs[0].bufsize = sizeof(*rx);
1767
1768 r = vioif_ctrl_send_command(sc, VIRTIO_NET_CTRL_RX, cmd,
1769 specs, __arraycount(specs));
1770
1771 vioif_ctrl_release(sc);
1772 return r;
1773 }
1774
1775 static int
1776 vioif_set_promisc(struct vioif_softc *sc, bool onoff)
1777 {
1778 return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_PROMISC, onoff);
1779 }
1780
1781 static int
1782 vioif_set_allmulti(struct vioif_softc *sc, bool onoff)
1783 {
1784 return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, onoff);
1785 }
1786
1787 /* issue VIRTIO_NET_CTRL_MAC_TABLE_SET command and wait for completion */
1788 static int
1789 vioif_set_rx_filter(struct vioif_softc *sc)
1790 {
1791 /* filter already set in ctrlq->ctrlq_mac_tbl */
1792 struct virtio_net_ctrl_mac_tbl *mac_tbl_uc, *mac_tbl_mc;
1793 struct vioif_ctrl_cmdspec specs[2];
1794 int nspecs = __arraycount(specs);
1795 int r;
1796
1797 mac_tbl_uc = sc->sc_ctrlq.ctrlq_mac_tbl_uc;
1798 mac_tbl_mc = sc->sc_ctrlq.ctrlq_mac_tbl_mc;
1799
1800 if (!sc->sc_has_ctrl)
1801 return ENOTSUP;
1802
1803 vioif_ctrl_acquire(sc);
1804
1805 specs[0].dmamap = sc->sc_ctrlq.ctrlq_tbl_uc_dmamap;
1806 specs[0].buf = mac_tbl_uc;
1807 specs[0].bufsize = sizeof(*mac_tbl_uc)
1808 + (ETHER_ADDR_LEN * mac_tbl_uc->nentries);
1809
1810 specs[1].dmamap = sc->sc_ctrlq.ctrlq_tbl_mc_dmamap;
1811 specs[1].buf = mac_tbl_mc;
1812 specs[1].bufsize = sizeof(*mac_tbl_mc)
1813 + (ETHER_ADDR_LEN * mac_tbl_mc->nentries);
1814
1815 r = vioif_ctrl_load_cmdspec(sc, specs, nspecs);
1816 if (r != 0)
1817 goto out;
1818
1819 r = vioif_ctrl_send_command(sc,
1820 VIRTIO_NET_CTRL_MAC, VIRTIO_NET_CTRL_MAC_TABLE_SET,
1821 specs, nspecs);
1822
1823 vioif_ctrl_unload_cmdspec(sc, specs, nspecs);
1824
1825 out:
1826 vioif_ctrl_release(sc);
1827
1828 return r;
1829 }
1830
1831 static int
1832 vioif_ctrl_mq_vq_pairs_set(struct vioif_softc *sc, int nvq_pairs)
1833 {
1834 struct virtio_net_ctrl_mq *mq = sc->sc_ctrlq.ctrlq_mq;
1835 struct vioif_ctrl_cmdspec specs[1];
1836 int r;
1837
1838 if (!sc->sc_has_ctrl)
1839 return ENOTSUP;
1840
1841 if (nvq_pairs <= 1)
1842 return EINVAL;
1843
1844 vioif_ctrl_acquire(sc);
1845
1846 mq->virtqueue_pairs = nvq_pairs;
1847 specs[0].dmamap = sc->sc_ctrlq.ctrlq_mq_dmamap;
1848 specs[0].buf = mq;
1849 specs[0].bufsize = sizeof(*mq);
1850
1851 r = vioif_ctrl_send_command(sc,
1852 VIRTIO_NET_CTRL_MQ, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET,
1853 specs, __arraycount(specs));
1854
1855 vioif_ctrl_release(sc);
1856
1857 return r;
1858 }
1859
1860 /* ctrl vq interrupt; wake up the command issuer */
1861 static int
1862 vioif_ctrl_vq_done(struct virtqueue *vq)
1863 {
1864 struct virtio_softc *vsc = vq->vq_owner;
1865 struct vioif_ctrlqueue *ctrlq = vq->vq_done_ctx;
1866 int r, slot;
1867
1868 r = virtio_dequeue(vsc, vq, &slot, NULL);
1869 if (r == ENOENT)
1870 return 0;
1871 virtio_dequeue_commit(vsc, vq, slot);
1872
1873 mutex_enter(&ctrlq->ctrlq_wait_lock);
1874 ctrlq->ctrlq_inuse = DONE;
1875 cv_signal(&ctrlq->ctrlq_wait);
1876 mutex_exit(&ctrlq->ctrlq_wait_lock);
1877
1878 return 1;
1879 }
1880
1881 /*
1882 * If IFF_PROMISC requested, set promiscuous
1883 * If multicast filter small enough (<=MAXENTRIES) set rx filter
1884 * If large multicast filter exist use ALLMULTI
1885 */
1886 /*
1887 * If setting rx filter fails fall back to ALLMULTI
1888 * If ALLMULTI fails fall back to PROMISC
1889 */
1890 static int
1891 vioif_rx_filter(struct vioif_softc *sc)
1892 {
1893 struct ethercom *ec = &sc->sc_ethercom;
1894 struct ifnet *ifp = &ec->ec_if;
1895 struct ether_multi *enm;
1896 struct ether_multistep step;
1897 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1898 int nentries;
1899 int promisc = 0, allmulti = 0, rxfilter = 0;
1900 int r;
1901
1902 if (!sc->sc_has_ctrl) { /* no ctrl vq; always promisc */
1903 ifp->if_flags |= IFF_PROMISC;
1904 return 0;
1905 }
1906
1907 if (ifp->if_flags & IFF_PROMISC) {
1908 promisc = 1;
1909 goto set;
1910 }
1911
1912 nentries = -1;
1913 ETHER_LOCK(ec);
1914 ETHER_FIRST_MULTI(step, ec, enm);
1915 while (nentries++, enm != NULL) {
1916 if (nentries >= VIRTIO_NET_CTRL_MAC_MAXENTRIES) {
1917 allmulti = 1;
1918 goto set_unlock;
1919 }
1920 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
1921 allmulti = 1;
1922 goto set_unlock;
1923 }
1924 memcpy(ctrlq->ctrlq_mac_tbl_mc->macs[nentries],
1925 enm->enm_addrlo, ETHER_ADDR_LEN);
1926 ETHER_NEXT_MULTI(step, enm);
1927 }
1928 rxfilter = 1;
1929
1930 set_unlock:
1931 ETHER_UNLOCK(ec);
1932
1933 set:
1934 if (rxfilter) {
1935 ctrlq->ctrlq_mac_tbl_uc->nentries = 0;
1936 ctrlq->ctrlq_mac_tbl_mc->nentries = nentries;
1937 r = vioif_set_rx_filter(sc);
1938 if (r != 0) {
1939 rxfilter = 0;
1940 allmulti = 1; /* fallback */
1941 }
1942 } else {
1943 /* remove rx filter */
1944 ctrlq->ctrlq_mac_tbl_uc->nentries = 0;
1945 ctrlq->ctrlq_mac_tbl_mc->nentries = 0;
1946 r = vioif_set_rx_filter(sc);
1947 /* what to do on failure? */
1948 }
1949 if (allmulti) {
1950 r = vioif_set_allmulti(sc, true);
1951 if (r != 0) {
1952 allmulti = 0;
1953 promisc = 1; /* fallback */
1954 }
1955 } else {
1956 r = vioif_set_allmulti(sc, false);
1957 /* what to do on failure? */
1958 }
1959 if (promisc) {
1960 r = vioif_set_promisc(sc, true);
1961 } else {
1962 r = vioif_set_promisc(sc, false);
1963 }
1964
1965 return r;
1966 }
1967
1968 static bool
1969 vioif_is_link_up(struct vioif_softc *sc)
1970 {
1971 struct virtio_softc *vsc = sc->sc_virtio;
1972 uint16_t status;
1973
1974 if (virtio_features(vsc) & VIRTIO_NET_F_STATUS)
1975 status = virtio_read_device_config_2(vsc,
1976 VIRTIO_NET_CONFIG_STATUS);
1977 else
1978 status = VIRTIO_NET_S_LINK_UP;
1979
1980 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
1981 }
1982
1983 /* change link status */
1984 static void
1985 vioif_update_link_status(struct vioif_softc *sc)
1986 {
1987 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1988 struct vioif_txqueue *txq;
1989 bool active, changed;
1990 int link, i;
1991
1992 active = vioif_is_link_up(sc);
1993 changed = false;
1994
1995 if (active) {
1996 if (!sc->sc_link_active)
1997 changed = true;
1998
1999 link = LINK_STATE_UP;
2000 sc->sc_link_active = true;
2001 } else {
2002 if (sc->sc_link_active)
2003 changed = true;
2004
2005 link = LINK_STATE_DOWN;
2006 sc->sc_link_active = false;
2007 }
2008
2009 if (changed) {
2010 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
2011 txq = &sc->sc_txq[i];
2012
2013 mutex_enter(txq->txq_lock);
2014 txq->txq_link_active = sc->sc_link_active;
2015 mutex_exit(txq->txq_lock);
2016 }
2017
2018 if_link_state_change(ifp, link);
2019 }
2020 }
2021
2022 static int
2023 vioif_config_change(struct virtio_softc *vsc)
2024 {
2025 struct vioif_softc *sc = device_private(virtio_child(vsc));
2026
2027 #ifdef VIOIF_SOFTINT_INTR
2028 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2029 #endif
2030
2031 #ifdef VIOIF_SOFTINT_INTR
2032 KASSERT(!cpu_intr_p());
2033 vioif_update_link_status(sc);
2034 vioif_start(ifp);
2035 #else
2036 softint_schedule(sc->sc_ctl_softint);
2037 #endif
2038
2039 return 0;
2040 }
2041
2042 static void
2043 vioif_ctl_softint(void *arg)
2044 {
2045 struct vioif_softc *sc = arg;
2046 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2047
2048 vioif_update_link_status(sc);
2049 vioif_start(ifp);
2050 }
2051
2052 MODULE(MODULE_CLASS_DRIVER, if_vioif, "virtio");
2053
2054 #ifdef _MODULE
2055 #include "ioconf.c"
2056 #endif
2057
2058 static int
2059 if_vioif_modcmd(modcmd_t cmd, void *opaque)
2060 {
2061 int error = 0;
2062
2063 #ifdef _MODULE
2064 switch (cmd) {
2065 case MODULE_CMD_INIT:
2066 error = config_init_component(cfdriver_ioconf_if_vioif,
2067 cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
2068 break;
2069 case MODULE_CMD_FINI:
2070 error = config_fini_component(cfdriver_ioconf_if_vioif,
2071 cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
2072 break;
2073 default:
2074 error = ENOTTY;
2075 break;
2076 }
2077 #endif
2078
2079 return error;
2080 }
2081