if_vioif.c revision 1.74 1 /* $NetBSD: if_vioif.c,v 1.74 2022/03/24 07:57:10 yamaguchi Exp $ */
2
3 /*
4 * Copyright (c) 2020 The NetBSD Foundation, Inc.
5 * Copyright (c) 2010 Minoura Makoto.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: if_vioif.c,v 1.74 2022/03/24 07:57:10 yamaguchi Exp $");
31
32 #ifdef _KERNEL_OPT
33 #include "opt_net_mpsafe.h"
34 #endif
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/atomic.h>
40 #include <sys/bus.h>
41 #include <sys/condvar.h>
42 #include <sys/device.h>
43 #include <sys/evcnt.h>
44 #include <sys/intr.h>
45 #include <sys/kmem.h>
46 #include <sys/mbuf.h>
47 #include <sys/mutex.h>
48 #include <sys/sockio.h>
49 #include <sys/syslog.h>
50 #include <sys/cpu.h>
51 #include <sys/module.h>
52 #include <sys/pcq.h>
53 #include <sys/workqueue.h>
54
55 #include <dev/pci/virtioreg.h>
56 #include <dev/pci/virtiovar.h>
57
58 #include <net/if.h>
59 #include <net/if_dl.h>
60 #include <net/if_media.h>
61 #include <net/if_ether.h>
62
63 #include <net/bpf.h>
64
65 #include "ioconf.h"
66
67 #ifdef NET_MPSAFE
68 #define VIOIF_MPSAFE 1
69 #define VIOIF_MULTIQ 1
70 #endif
71
72 /*
73 * if_vioifreg.h:
74 */
75 /* Configuration registers */
76 #define VIRTIO_NET_CONFIG_MAC 0 /* 8bit x 6byte */
77 #define VIRTIO_NET_CONFIG_STATUS 6 /* 16bit */
78 #define VIRTIO_NET_CONFIG_MAX_VQ_PAIRS 8 /* 16bit */
79 #define VIRTIO_NET_CONFIG_MTU 10 /* 16bit */
80
81 /* Feature bits */
82 #define VIRTIO_NET_F_CSUM __BIT(0)
83 #define VIRTIO_NET_F_GUEST_CSUM __BIT(1)
84 #define VIRTIO_NET_F_MAC __BIT(5)
85 #define VIRTIO_NET_F_GSO __BIT(6)
86 #define VIRTIO_NET_F_GUEST_TSO4 __BIT(7)
87 #define VIRTIO_NET_F_GUEST_TSO6 __BIT(8)
88 #define VIRTIO_NET_F_GUEST_ECN __BIT(9)
89 #define VIRTIO_NET_F_GUEST_UFO __BIT(10)
90 #define VIRTIO_NET_F_HOST_TSO4 __BIT(11)
91 #define VIRTIO_NET_F_HOST_TSO6 __BIT(12)
92 #define VIRTIO_NET_F_HOST_ECN __BIT(13)
93 #define VIRTIO_NET_F_HOST_UFO __BIT(14)
94 #define VIRTIO_NET_F_MRG_RXBUF __BIT(15)
95 #define VIRTIO_NET_F_STATUS __BIT(16)
96 #define VIRTIO_NET_F_CTRL_VQ __BIT(17)
97 #define VIRTIO_NET_F_CTRL_RX __BIT(18)
98 #define VIRTIO_NET_F_CTRL_VLAN __BIT(19)
99 #define VIRTIO_NET_F_CTRL_RX_EXTRA __BIT(20)
100 #define VIRTIO_NET_F_GUEST_ANNOUNCE __BIT(21)
101 #define VIRTIO_NET_F_MQ __BIT(22)
102
103 #define VIRTIO_NET_FLAG_BITS \
104 VIRTIO_COMMON_FLAG_BITS \
105 "\x17""MQ" \
106 "\x16""GUEST_ANNOUNCE" \
107 "\x15""CTRL_RX_EXTRA" \
108 "\x14""CTRL_VLAN" \
109 "\x13""CTRL_RX" \
110 "\x12""CTRL_VQ" \
111 "\x11""STATUS" \
112 "\x10""MRG_RXBUF" \
113 "\x0f""HOST_UFO" \
114 "\x0e""HOST_ECN" \
115 "\x0d""HOST_TSO6" \
116 "\x0c""HOST_TSO4" \
117 "\x0b""GUEST_UFO" \
118 "\x0a""GUEST_ECN" \
119 "\x09""GUEST_TSO6" \
120 "\x08""GUEST_TSO4" \
121 "\x07""GSO" \
122 "\x06""MAC" \
123 "\x02""GUEST_CSUM" \
124 "\x01""CSUM"
125
126 /* Status */
127 #define VIRTIO_NET_S_LINK_UP 1
128
129 /* Packet header structure */
130 struct virtio_net_hdr {
131 uint8_t flags;
132 uint8_t gso_type;
133 uint16_t hdr_len;
134 uint16_t gso_size;
135 uint16_t csum_start;
136 uint16_t csum_offset;
137
138 uint16_t num_buffers; /* VIRTIO_NET_F_MRG_RXBUF enabled or v1 */
139 } __packed;
140
141 #define VIRTIO_NET_HDR_F_NEEDS_CSUM 1 /* flags */
142 #define VIRTIO_NET_HDR_GSO_NONE 0 /* gso_type */
143 #define VIRTIO_NET_HDR_GSO_TCPV4 1 /* gso_type */
144 #define VIRTIO_NET_HDR_GSO_UDP 3 /* gso_type */
145 #define VIRTIO_NET_HDR_GSO_TCPV6 4 /* gso_type */
146 #define VIRTIO_NET_HDR_GSO_ECN 0x80 /* gso_type, |'ed */
147
148 #define VIRTIO_NET_MAX_GSO_LEN (65536+ETHER_HDR_LEN)
149
150 /* Control virtqueue */
151 struct virtio_net_ctrl_cmd {
152 uint8_t class;
153 uint8_t command;
154 } __packed;
155 #define VIRTIO_NET_CTRL_RX 0
156 # define VIRTIO_NET_CTRL_RX_PROMISC 0
157 # define VIRTIO_NET_CTRL_RX_ALLMULTI 1
158
159 #define VIRTIO_NET_CTRL_MAC 1
160 # define VIRTIO_NET_CTRL_MAC_TABLE_SET 0
161 # define VIRTIO_NET_CTRL_MAC_ADDR_SET 1
162
163 #define VIRTIO_NET_CTRL_VLAN 2
164 # define VIRTIO_NET_CTRL_VLAN_ADD 0
165 # define VIRTIO_NET_CTRL_VLAN_DEL 1
166
167 #define VIRTIO_NET_CTRL_MQ 4
168 # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET 0
169 # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN 1
170 # define VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX 0x8000
171
172 struct virtio_net_ctrl_status {
173 uint8_t ack;
174 } __packed;
175 #define VIRTIO_NET_OK 0
176 #define VIRTIO_NET_ERR 1
177
178 struct virtio_net_ctrl_rx {
179 uint8_t onoff;
180 } __packed;
181
182 struct virtio_net_ctrl_mac_tbl {
183 uint32_t nentries;
184 uint8_t macs[][ETHER_ADDR_LEN];
185 } __packed;
186
187 struct virtio_net_ctrl_mac_addr {
188 uint8_t mac[ETHER_ADDR_LEN];
189 } __packed;
190
191 struct virtio_net_ctrl_vlan {
192 uint16_t id;
193 } __packed;
194
195 struct virtio_net_ctrl_mq {
196 uint16_t virtqueue_pairs;
197 } __packed;
198
199 /*
200 * if_vioifvar.h:
201 */
202
203 /*
204 * Locking notes:
205 * + a field in vioif_txqueue is protected by txq_lock (a spin mutex), and
206 * a field in vioif_rxqueue is protected by rxq_lock (a spin mutex).
207 * - more than one lock cannot be held at onece
208 * + ctrlq_inuse is protected by ctrlq_wait_lock.
209 * - other fields in vioif_ctrlqueue are protected by ctrlq_inuse
210 * - txq_lock or rxq_lock cannot be held along with ctrlq_wait_lock
211 * + fields in vioif_softc except queues are protected by
212 * sc->sc_lock(an adaptive mutex)
213 * - the lock is held before acquisition of other locks
214 */
215
216 struct vioif_ctrl_cmdspec {
217 bus_dmamap_t dmamap;
218 void *buf;
219 bus_size_t bufsize;
220 };
221
222 struct vioif_work {
223 struct work cookie;
224 void (*func)(void *);
225 void *arg;
226 unsigned int added;
227 };
228
229 struct vioif_txqueue {
230 kmutex_t *txq_lock; /* lock for tx operations */
231
232 struct virtqueue *txq_vq;
233 bool txq_stopping;
234 bool txq_link_active;
235 pcq_t *txq_intrq;
236
237 struct virtio_net_hdr *txq_hdrs;
238 bus_dmamap_t *txq_hdr_dmamaps;
239
240 struct mbuf **txq_mbufs;
241 bus_dmamap_t *txq_dmamaps;
242
243 void *txq_deferred_transmit;
244 void *txq_handle_si;
245 struct vioif_work txq_work;
246 bool txq_workqueue;
247 bool txq_active;
248
249 char txq_evgroup[16];
250 struct evcnt txq_defrag_failed;
251 struct evcnt txq_mbuf_load_failed;
252 struct evcnt txq_enqueue_reserve_failed;
253 };
254
255 struct vioif_rxqueue {
256 kmutex_t *rxq_lock; /* lock for rx operations */
257
258 struct virtqueue *rxq_vq;
259 bool rxq_stopping;
260
261 struct virtio_net_hdr *rxq_hdrs;
262 bus_dmamap_t *rxq_hdr_dmamaps;
263
264 struct mbuf **rxq_mbufs;
265 bus_dmamap_t *rxq_dmamaps;
266
267 void *rxq_handle_si;
268 struct vioif_work rxq_work;
269 bool rxq_workqueue;
270 bool rxq_active;
271
272 char rxq_evgroup[16];
273 struct evcnt rxq_mbuf_add_failed;
274 };
275
276 struct vioif_ctrlqueue {
277 struct virtqueue *ctrlq_vq;
278 enum {
279 FREE, INUSE, DONE
280 } ctrlq_inuse;
281 kcondvar_t ctrlq_wait;
282 kmutex_t ctrlq_wait_lock;
283 struct lwp *ctrlq_owner;
284
285 struct virtio_net_ctrl_cmd *ctrlq_cmd;
286 struct virtio_net_ctrl_status *ctrlq_status;
287 struct virtio_net_ctrl_rx *ctrlq_rx;
288 struct virtio_net_ctrl_mac_tbl *ctrlq_mac_tbl_uc;
289 struct virtio_net_ctrl_mac_tbl *ctrlq_mac_tbl_mc;
290 struct virtio_net_ctrl_mac_addr *ctrlq_mac_addr;
291 struct virtio_net_ctrl_mq *ctrlq_mq;
292
293 bus_dmamap_t ctrlq_cmd_dmamap;
294 bus_dmamap_t ctrlq_status_dmamap;
295 bus_dmamap_t ctrlq_rx_dmamap;
296 bus_dmamap_t ctrlq_tbl_uc_dmamap;
297 bus_dmamap_t ctrlq_tbl_mc_dmamap;
298 bus_dmamap_t ctrlq_mac_addr_dmamap;
299 bus_dmamap_t ctrlq_mq_dmamap;
300
301 struct evcnt ctrlq_cmd_load_failed;
302 struct evcnt ctrlq_cmd_failed;
303 };
304
305 struct vioif_softc {
306 device_t sc_dev;
307 kmutex_t sc_lock;
308 struct sysctllog *sc_sysctllog;
309
310 struct virtio_softc *sc_virtio;
311 struct virtqueue *sc_vqs;
312 u_int sc_hdr_size;
313
314 int sc_max_nvq_pairs;
315 int sc_req_nvq_pairs;
316 int sc_act_nvq_pairs;
317
318 uint8_t sc_mac[ETHER_ADDR_LEN];
319 struct ethercom sc_ethercom;
320 short sc_deferred_init_done;
321 bool sc_link_active;
322
323 struct vioif_txqueue *sc_txq;
324 struct vioif_rxqueue *sc_rxq;
325
326 bool sc_has_ctrl;
327 struct vioif_ctrlqueue sc_ctrlq;
328
329 bus_dma_segment_t sc_hdr_segs[1];
330 void *sc_dmamem;
331 void *sc_kmem;
332
333 void *sc_ctl_softint;
334
335 struct workqueue *sc_txrx_workqueue;
336 bool sc_txrx_workqueue_sysctl;
337 u_int sc_tx_intr_process_limit;
338 u_int sc_tx_process_limit;
339 u_int sc_rx_intr_process_limit;
340 u_int sc_rx_process_limit;
341 };
342 #define VIRTIO_NET_TX_MAXNSEGS (16) /* XXX */
343 #define VIRTIO_NET_CTRL_MAC_MAXENTRIES (64) /* XXX */
344
345 #define VIOIF_TX_INTR_PROCESS_LIMIT 256
346 #define VIOIF_TX_PROCESS_LIMIT 256
347 #define VIOIF_RX_INTR_PROCESS_LIMIT 0U
348 #define VIOIF_RX_PROCESS_LIMIT 256
349
350 #define VIOIF_WORKQUEUE_PRI PRI_SOFTNET
351
352 /* cfattach interface functions */
353 static int vioif_match(device_t, cfdata_t, void *);
354 static void vioif_attach(device_t, device_t, void *);
355 static void vioif_deferred_init(device_t);
356 static int vioif_finalize_teardown(device_t);
357
358 /* ifnet interface functions */
359 static int vioif_init(struct ifnet *);
360 static void vioif_stop(struct ifnet *, int);
361 static void vioif_start(struct ifnet *);
362 static void vioif_start_locked(struct ifnet *, struct vioif_txqueue *);
363 static int vioif_transmit(struct ifnet *, struct mbuf *);
364 static void vioif_transmit_locked(struct ifnet *, struct vioif_txqueue *);
365 static int vioif_ioctl(struct ifnet *, u_long, void *);
366 static void vioif_watchdog(struct ifnet *);
367
368 /* rx */
369 static int vioif_add_rx_mbuf(struct vioif_rxqueue *, int);
370 static void vioif_free_rx_mbuf(struct vioif_rxqueue *, int);
371 static void vioif_populate_rx_mbufs_locked(struct vioif_softc *,
372 struct vioif_rxqueue *);
373 static void vioif_rx_queue_clear(struct vioif_rxqueue *);
374 static bool vioif_rx_deq_locked(struct vioif_softc *, struct virtio_softc *,
375 struct vioif_rxqueue *, u_int);
376 static int vioif_rx_intr(void *);
377 static void vioif_rx_handle(void *);
378 static void vioif_rx_sched_handle(struct vioif_softc *,
379 struct vioif_rxqueue *);
380 static void vioif_rx_drain(struct vioif_rxqueue *);
381
382 /* tx */
383 static int vioif_tx_intr(void *);
384 static void vioif_tx_handle(void *);
385 static void vioif_tx_sched_handle(struct vioif_softc *,
386 struct vioif_txqueue *);
387 static void vioif_tx_queue_clear(struct vioif_txqueue *);
388 static bool vioif_tx_deq_locked(struct vioif_softc *, struct virtio_softc *,
389 struct vioif_txqueue *, u_int);
390 static void vioif_tx_drain(struct vioif_txqueue *);
391 static void vioif_deferred_transmit(void *);
392
393 /* workqueue */
394 static struct workqueue*
395 vioif_workq_create(const char *, pri_t, int, int);
396 static void vioif_workq_destroy(struct workqueue *);
397 static void vioif_workq_work(struct work *, void *);
398 static void vioif_work_set(struct vioif_work *, void(*)(void *), void *);
399 static void vioif_work_add(struct workqueue *, struct vioif_work *);
400 static void vioif_work_wait(struct workqueue *, struct vioif_work *);
401
402 /* other control */
403 static bool vioif_is_link_up(struct vioif_softc *);
404 static void vioif_update_link_status(struct vioif_softc *);
405 static int vioif_ctrl_rx(struct vioif_softc *, int, bool);
406 static int vioif_set_promisc(struct vioif_softc *, bool);
407 static int vioif_set_allmulti(struct vioif_softc *, bool);
408 static int vioif_set_rx_filter(struct vioif_softc *);
409 static int vioif_rx_filter(struct vioif_softc *);
410 static int vioif_set_mac_addr(struct vioif_softc *);
411 static int vioif_ctrl_intr(void *);
412 static int vioif_config_change(struct virtio_softc *);
413 static void vioif_ctl_softint(void *);
414 static int vioif_ctrl_mq_vq_pairs_set(struct vioif_softc *, int);
415 static void vioif_enable_interrupt_vqpairs(struct vioif_softc *);
416 static void vioif_disable_interrupt_vqpairs(struct vioif_softc *);
417 static int vioif_setup_sysctl(struct vioif_softc *);
418 static void vioif_setup_stats(struct vioif_softc *);
419
420 CFATTACH_DECL_NEW(vioif, sizeof(struct vioif_softc),
421 vioif_match, vioif_attach, NULL, NULL);
422
423 static int
424 vioif_match(device_t parent, cfdata_t match, void *aux)
425 {
426 struct virtio_attach_args *va = aux;
427
428 if (va->sc_childdevid == VIRTIO_DEVICE_ID_NETWORK)
429 return 1;
430
431 return 0;
432 }
433
434 static int
435 vioif_dmamap_create(struct vioif_softc *sc, bus_dmamap_t *map,
436 bus_size_t size, int nsegs, const char *usage)
437 {
438 int r;
439
440 r = bus_dmamap_create(virtio_dmat(sc->sc_virtio), size,
441 nsegs, size, 0, BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, map);
442
443 if (r != 0) {
444 aprint_error_dev(sc->sc_dev, "%s dmamap creation failed, "
445 "error code %d\n", usage, r);
446 }
447
448 return r;
449 }
450
451 static void
452 vioif_dmamap_destroy(struct vioif_softc *sc, bus_dmamap_t *map)
453 {
454
455 if (*map) {
456 bus_dmamap_destroy(virtio_dmat(sc->sc_virtio), *map);
457 *map = NULL;
458 }
459 }
460
461 static int
462 vioif_dmamap_create_load(struct vioif_softc *sc, bus_dmamap_t *map,
463 void *buf, bus_size_t size, int nsegs, int rw, const char *usage)
464 {
465 int r;
466
467 r = vioif_dmamap_create(sc, map, size, nsegs, usage);
468 if (r != 0)
469 return 1;
470
471 r = bus_dmamap_load(virtio_dmat(sc->sc_virtio), *map, buf,
472 size, NULL, rw | BUS_DMA_NOWAIT);
473 if (r != 0) {
474 vioif_dmamap_destroy(sc, map);
475 aprint_error_dev(sc->sc_dev, "%s dmamap load failed. "
476 "error code %d\n", usage, r);
477 }
478
479 return r;
480 }
481
482 static void *
483 vioif_assign_mem(intptr_t *p, size_t size)
484 {
485 intptr_t rv;
486
487 rv = *p;
488 *p += size;
489
490 return (void *)rv;
491 }
492
493 static void
494 vioif_alloc_queues(struct vioif_softc *sc)
495 {
496 int nvq_pairs = sc->sc_max_nvq_pairs;
497 int nvqs = nvq_pairs * 2;
498 int i;
499
500 KASSERT(nvq_pairs <= VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX);
501
502 sc->sc_rxq = kmem_zalloc(sizeof(sc->sc_rxq[0]) * nvq_pairs,
503 KM_SLEEP);
504 sc->sc_txq = kmem_zalloc(sizeof(sc->sc_txq[0]) * nvq_pairs,
505 KM_SLEEP);
506
507 if (sc->sc_has_ctrl)
508 nvqs++;
509
510 sc->sc_vqs = kmem_zalloc(sizeof(sc->sc_vqs[0]) * nvqs, KM_SLEEP);
511 nvqs = 0;
512 for (i = 0; i < nvq_pairs; i++) {
513 sc->sc_rxq[i].rxq_vq = &sc->sc_vqs[nvqs++];
514 sc->sc_txq[i].txq_vq = &sc->sc_vqs[nvqs++];
515 }
516
517 if (sc->sc_has_ctrl)
518 sc->sc_ctrlq.ctrlq_vq = &sc->sc_vqs[nvqs++];
519 }
520
521 static void
522 vioif_free_queues(struct vioif_softc *sc)
523 {
524 int nvq_pairs = sc->sc_max_nvq_pairs;
525 int nvqs = nvq_pairs * 2;
526
527 if (sc->sc_ctrlq.ctrlq_vq)
528 nvqs++;
529
530 if (sc->sc_txq) {
531 kmem_free(sc->sc_txq, sizeof(sc->sc_txq[0]) * nvq_pairs);
532 sc->sc_txq = NULL;
533 }
534
535 if (sc->sc_rxq) {
536 kmem_free(sc->sc_rxq, sizeof(sc->sc_rxq[0]) * nvq_pairs);
537 sc->sc_rxq = NULL;
538 }
539
540 if (sc->sc_vqs) {
541 kmem_free(sc->sc_vqs, sizeof(sc->sc_vqs[0]) * nvqs);
542 sc->sc_vqs = NULL;
543 }
544 }
545
546 /* allocate memory */
547 /*
548 * dma memory is used for:
549 * rxq_hdrs[slot]: metadata array for received frames (READ)
550 * txq_hdrs[slot]: metadata array for frames to be sent (WRITE)
551 * ctrlq_cmd: command to be sent via ctrl vq (WRITE)
552 * ctrlq_status: return value for a command via ctrl vq (READ)
553 * ctrlq_rx: parameter for a VIRTIO_NET_CTRL_RX class command
554 * (WRITE)
555 * ctrlq_mac_tbl_uc: unicast MAC address filter for a VIRTIO_NET_CTRL_MAC
556 * class command (WRITE)
557 * ctrlq_mac_tbl_mc: multicast MAC address filter for a VIRTIO_NET_CTRL_MAC
558 * class command (WRITE)
559 * ctrlq_* structures are allocated only one each; they are protected by
560 * ctrlq_inuse variable and ctrlq_wait condvar.
561 */
562 /*
563 * dynamically allocated memory is used for:
564 * rxq_hdr_dmamaps[slot]: bus_dmamap_t array for sc_rx_hdrs[slot]
565 * txq_hdr_dmamaps[slot]: bus_dmamap_t array for sc_tx_hdrs[slot]
566 * rxq_dmamaps[slot]: bus_dmamap_t array for received payload
567 * txq_dmamaps[slot]: bus_dmamap_t array for sent payload
568 * rxq_mbufs[slot]: mbuf pointer array for received frames
569 * txq_mbufs[slot]: mbuf pointer array for sent frames
570 */
571 static int
572 vioif_alloc_mems(struct vioif_softc *sc)
573 {
574 struct virtio_softc *vsc = sc->sc_virtio;
575 struct vioif_txqueue *txq;
576 struct vioif_rxqueue *rxq;
577 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
578 int allocsize, allocsize2, r, rsegs, i, qid;
579 void *vaddr;
580 intptr_t p;
581
582 allocsize = 0;
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 allocsize += sizeof(struct virtio_net_hdr) *
588 (rxq->rxq_vq->vq_num + txq->txq_vq->vq_num);
589 }
590 if (sc->sc_has_ctrl) {
591 allocsize += sizeof(struct virtio_net_ctrl_cmd);
592 allocsize += sizeof(struct virtio_net_ctrl_status);
593 allocsize += sizeof(struct virtio_net_ctrl_rx);
594 allocsize += sizeof(struct virtio_net_ctrl_mac_tbl)
595 + ETHER_ADDR_LEN;
596 allocsize += sizeof(struct virtio_net_ctrl_mac_tbl)
597 + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES;
598 allocsize += sizeof(struct virtio_net_ctrl_mac_addr);
599 allocsize += sizeof(struct virtio_net_ctrl_mq);
600 }
601 r = bus_dmamem_alloc(virtio_dmat(vsc), allocsize, 0, 0,
602 &sc->sc_hdr_segs[0], 1, &rsegs, BUS_DMA_NOWAIT);
603 if (r != 0) {
604 aprint_error_dev(sc->sc_dev,
605 "DMA memory allocation failed, size %d, "
606 "error code %d\n", allocsize, r);
607 goto err_none;
608 }
609 r = bus_dmamem_map(virtio_dmat(vsc),
610 &sc->sc_hdr_segs[0], 1, allocsize, &vaddr, BUS_DMA_NOWAIT);
611 if (r != 0) {
612 aprint_error_dev(sc->sc_dev,
613 "DMA memory map failed, error code %d\n", r);
614 goto err_dmamem_alloc;
615 }
616
617 memset(vaddr, 0, allocsize);
618 sc->sc_dmamem = vaddr;
619 p = (intptr_t) vaddr;
620
621 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
622 rxq = &sc->sc_rxq[qid];
623 txq = &sc->sc_txq[qid];
624
625 rxq->rxq_hdrs = vioif_assign_mem(&p,
626 sizeof(struct virtio_net_hdr) * rxq->rxq_vq->vq_num);
627 txq->txq_hdrs = vioif_assign_mem(&p,
628 sizeof(struct virtio_net_hdr) * txq->txq_vq->vq_num);
629 }
630 if (sc->sc_has_ctrl) {
631 ctrlq->ctrlq_cmd = vioif_assign_mem(&p,
632 sizeof(*ctrlq->ctrlq_cmd));
633 ctrlq->ctrlq_status = vioif_assign_mem(&p,
634 sizeof(*ctrlq->ctrlq_status));
635 ctrlq->ctrlq_rx = vioif_assign_mem(&p,
636 sizeof(*ctrlq->ctrlq_rx));
637 ctrlq->ctrlq_mac_tbl_uc = vioif_assign_mem(&p,
638 sizeof(*ctrlq->ctrlq_mac_tbl_uc)
639 + ETHER_ADDR_LEN);
640 ctrlq->ctrlq_mac_tbl_mc = vioif_assign_mem(&p,
641 sizeof(*ctrlq->ctrlq_mac_tbl_mc)
642 + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES);
643 ctrlq->ctrlq_mac_addr = vioif_assign_mem(&p,
644 sizeof(*ctrlq->ctrlq_mac_addr));
645 ctrlq->ctrlq_mq = vioif_assign_mem(&p, sizeof(*ctrlq->ctrlq_mq));
646 }
647
648 allocsize2 = 0;
649 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
650 int rxqsize, txqsize;
651
652 rxq = &sc->sc_rxq[qid];
653 txq = &sc->sc_txq[qid];
654 rxqsize = rxq->rxq_vq->vq_num;
655 txqsize = txq->txq_vq->vq_num;
656
657 allocsize2 += sizeof(rxq->rxq_dmamaps[0]) * rxqsize;
658 allocsize2 += sizeof(rxq->rxq_hdr_dmamaps[0]) * rxqsize;
659 allocsize2 += sizeof(rxq->rxq_mbufs[0]) * rxqsize;
660
661 allocsize2 += sizeof(txq->txq_dmamaps[0]) * txqsize;
662 allocsize2 += sizeof(txq->txq_hdr_dmamaps[0]) * txqsize;
663 allocsize2 += sizeof(txq->txq_mbufs[0]) * txqsize;
664 }
665 vaddr = kmem_zalloc(allocsize2, KM_SLEEP);
666 sc->sc_kmem = vaddr;
667 p = (intptr_t) vaddr;
668
669 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
670 int rxqsize, txqsize;
671 rxq = &sc->sc_rxq[qid];
672 txq = &sc->sc_txq[qid];
673 rxqsize = rxq->rxq_vq->vq_num;
674 txqsize = txq->txq_vq->vq_num;
675
676 rxq->rxq_hdr_dmamaps = vioif_assign_mem(&p,
677 sizeof(rxq->rxq_hdr_dmamaps[0]) * rxqsize);
678 txq->txq_hdr_dmamaps = vioif_assign_mem(&p,
679 sizeof(txq->txq_hdr_dmamaps[0]) * txqsize);
680 rxq->rxq_dmamaps = vioif_assign_mem(&p,
681 sizeof(rxq->rxq_dmamaps[0]) * rxqsize);
682 txq->txq_dmamaps = vioif_assign_mem(&p,
683 sizeof(txq->txq_dmamaps[0]) * txqsize);
684 rxq->rxq_mbufs = vioif_assign_mem(&p,
685 sizeof(rxq->rxq_mbufs[0]) * rxqsize);
686 txq->txq_mbufs = vioif_assign_mem(&p,
687 sizeof(txq->txq_mbufs[0]) * txqsize);
688 }
689
690 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
691 rxq = &sc->sc_rxq[qid];
692 txq = &sc->sc_txq[qid];
693
694 for (i = 0; i < rxq->rxq_vq->vq_num; i++) {
695 r = vioif_dmamap_create_load(sc, &rxq->rxq_hdr_dmamaps[i],
696 &rxq->rxq_hdrs[i], sc->sc_hdr_size, 1,
697 BUS_DMA_READ, "rx header");
698 if (r != 0)
699 goto err_reqs;
700
701 r = vioif_dmamap_create(sc, &rxq->rxq_dmamaps[i],
702 MCLBYTES, 1, "rx payload");
703 if (r != 0)
704 goto err_reqs;
705 }
706
707 for (i = 0; i < txq->txq_vq->vq_num; i++) {
708 r = vioif_dmamap_create_load(sc, &txq->txq_hdr_dmamaps[i],
709 &txq->txq_hdrs[i], sc->sc_hdr_size, 1,
710 BUS_DMA_READ, "tx header");
711 if (r != 0)
712 goto err_reqs;
713
714 r = vioif_dmamap_create(sc, &txq->txq_dmamaps[i], ETHER_MAX_LEN,
715 VIRTIO_NET_TX_MAXNSEGS, "tx payload");
716 if (r != 0)
717 goto err_reqs;
718 }
719 }
720
721 if (sc->sc_has_ctrl) {
722 /* control vq class & command */
723 r = vioif_dmamap_create_load(sc, &ctrlq->ctrlq_cmd_dmamap,
724 ctrlq->ctrlq_cmd, sizeof(*ctrlq->ctrlq_cmd), 1,
725 BUS_DMA_WRITE, "control command");
726 if (r != 0)
727 goto err_reqs;
728
729 r = vioif_dmamap_create_load(sc, &ctrlq->ctrlq_status_dmamap,
730 ctrlq->ctrlq_status, sizeof(*ctrlq->ctrlq_status), 1,
731 BUS_DMA_READ, "control status");
732 if (r != 0)
733 goto err_reqs;
734
735 /* control vq rx mode command parameter */
736 r = vioif_dmamap_create_load(sc, &ctrlq->ctrlq_rx_dmamap,
737 ctrlq->ctrlq_rx, sizeof(*ctrlq->ctrlq_rx), 1,
738 BUS_DMA_WRITE, "rx mode control command");
739 if (r != 0)
740 goto err_reqs;
741
742 /* multiqueue set command */
743 r = vioif_dmamap_create_load(sc, &ctrlq->ctrlq_mq_dmamap,
744 ctrlq->ctrlq_mq, sizeof(*ctrlq->ctrlq_mq), 1,
745 BUS_DMA_WRITE, "multiqueue set command");
746 if (r != 0)
747 goto err_reqs;
748
749 /* control vq MAC filter table for unicast */
750 /* do not load now since its length is variable */
751 r = vioif_dmamap_create(sc, &ctrlq->ctrlq_tbl_uc_dmamap,
752 sizeof(*ctrlq->ctrlq_mac_tbl_uc)
753 + ETHER_ADDR_LEN, 1,
754 "unicast MAC address filter command");
755 if (r != 0)
756 goto err_reqs;
757
758 /* control vq MAC filter table for multicast */
759 r = vioif_dmamap_create(sc, &ctrlq->ctrlq_tbl_mc_dmamap,
760 sizeof(*ctrlq->ctrlq_mac_tbl_mc)
761 + ETHER_ADDR_LEN * VIRTIO_NET_CTRL_MAC_MAXENTRIES, 1,
762 "multicast MAC address filter command");
763 if (r != 0)
764 goto err_reqs;
765
766 /* control vq MAC address set command */
767 r = vioif_dmamap_create_load(sc,
768 &ctrlq->ctrlq_mac_addr_dmamap,
769 ctrlq->ctrlq_mac_addr,
770 sizeof(*ctrlq->ctrlq_mac_addr), 1,
771 BUS_DMA_WRITE, "mac addr set command");
772 if (r != 0)
773 goto err_reqs;
774 }
775
776 return 0;
777
778 err_reqs:
779 vioif_dmamap_destroy(sc, &ctrlq->ctrlq_tbl_mc_dmamap);
780 vioif_dmamap_destroy(sc, &ctrlq->ctrlq_tbl_uc_dmamap);
781 vioif_dmamap_destroy(sc, &ctrlq->ctrlq_rx_dmamap);
782 vioif_dmamap_destroy(sc, &ctrlq->ctrlq_status_dmamap);
783 vioif_dmamap_destroy(sc, &ctrlq->ctrlq_cmd_dmamap);
784 vioif_dmamap_destroy(sc, &ctrlq->ctrlq_mac_addr_dmamap);
785 for (qid = 0; qid < sc->sc_max_nvq_pairs; qid++) {
786 rxq = &sc->sc_rxq[qid];
787 txq = &sc->sc_txq[qid];
788
789 for (i = 0; i < txq->txq_vq->vq_num; i++) {
790 vioif_dmamap_destroy(sc, &txq->txq_dmamaps[i]);
791 vioif_dmamap_destroy(sc, &txq->txq_hdr_dmamaps[i]);
792 }
793 for (i = 0; i < rxq->rxq_vq->vq_num; i++) {
794 vioif_dmamap_destroy(sc, &rxq->rxq_dmamaps[i]);
795 vioif_dmamap_destroy(sc, &rxq->rxq_hdr_dmamaps[i]);
796 }
797 }
798 if (sc->sc_kmem) {
799 kmem_free(sc->sc_kmem, allocsize2);
800 sc->sc_kmem = NULL;
801 }
802 bus_dmamem_unmap(virtio_dmat(vsc), sc->sc_dmamem, allocsize);
803 err_dmamem_alloc:
804 bus_dmamem_free(virtio_dmat(vsc), &sc->sc_hdr_segs[0], 1);
805 err_none:
806 return -1;
807 }
808
809 static void
810 vioif_attach(device_t parent, device_t self, void *aux)
811 {
812 struct vioif_softc *sc = device_private(self);
813 struct virtio_softc *vsc = device_private(parent);
814 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
815 struct vioif_txqueue *txq;
816 struct vioif_rxqueue *rxq;
817 uint64_t features, req_features;
818 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
819 u_int softint_flags;
820 int r, i, nvqs=0, req_flags;
821 char xnamebuf[MAXCOMLEN];
822
823 if (virtio_child(vsc) != NULL) {
824 aprint_normal(": child already attached for %s; "
825 "something wrong...\n", device_xname(parent));
826 return;
827 }
828
829 sc->sc_dev = self;
830 sc->sc_virtio = vsc;
831 sc->sc_link_active = false;
832
833 sc->sc_max_nvq_pairs = 1;
834 sc->sc_req_nvq_pairs = 1;
835 sc->sc_act_nvq_pairs = 1;
836 sc->sc_txrx_workqueue_sysctl = true;
837 sc->sc_tx_intr_process_limit = VIOIF_TX_INTR_PROCESS_LIMIT;
838 sc->sc_tx_process_limit = VIOIF_TX_PROCESS_LIMIT;
839 sc->sc_rx_intr_process_limit = VIOIF_RX_INTR_PROCESS_LIMIT;
840 sc->sc_rx_process_limit = VIOIF_RX_PROCESS_LIMIT;
841
842 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_NONE);
843
844 snprintf(xnamebuf, sizeof(xnamebuf), "%s_txrx", device_xname(self));
845 sc->sc_txrx_workqueue = vioif_workq_create(xnamebuf, VIOIF_WORKQUEUE_PRI,
846 IPL_NET, WQ_PERCPU | WQ_MPSAFE);
847 if (sc->sc_txrx_workqueue == NULL)
848 goto err;
849
850 req_flags = 0;
851
852 #ifdef VIOIF_MPSAFE
853 req_flags |= VIRTIO_F_INTR_MPSAFE;
854 #endif
855 req_flags |= VIRTIO_F_INTR_MSIX;
856
857 req_features =
858 VIRTIO_NET_F_MAC | VIRTIO_NET_F_STATUS | VIRTIO_NET_F_CTRL_VQ |
859 VIRTIO_NET_F_CTRL_RX | VIRTIO_F_NOTIFY_ON_EMPTY;
860 req_features |= VIRTIO_F_RING_EVENT_IDX;
861 #ifdef VIOIF_MULTIQ
862 req_features |= VIRTIO_NET_F_MQ;
863 #endif
864 virtio_child_attach_start(vsc, self, IPL_NET, NULL,
865 vioif_config_change, virtio_vq_intrhand, req_flags,
866 req_features, VIRTIO_NET_FLAG_BITS);
867
868 features = virtio_features(vsc);
869 if (features == 0)
870 goto err;
871
872 if (features & VIRTIO_NET_F_MAC) {
873 for (i = 0; i < __arraycount(sc->sc_mac); i++) {
874 sc->sc_mac[i] = virtio_read_device_config_1(vsc,
875 VIRTIO_NET_CONFIG_MAC + i);
876 }
877 } else {
878 /* code stolen from sys/net/if_tap.c */
879 struct timeval tv;
880 uint32_t ui;
881 getmicrouptime(&tv);
882 ui = (tv.tv_sec ^ tv.tv_usec) & 0xffffff;
883 memcpy(sc->sc_mac+3, (uint8_t *)&ui, 3);
884 for (i = 0; i < __arraycount(sc->sc_mac); i++) {
885 virtio_write_device_config_1(vsc,
886 VIRTIO_NET_CONFIG_MAC + i, sc->sc_mac[i]);
887 }
888 }
889
890 /* 'Ethernet' with capital follows other ethernet driver attachment */
891 aprint_normal_dev(self, "Ethernet address %s\n",
892 ether_sprintf(sc->sc_mac));
893
894 if (features & (VIRTIO_NET_F_MRG_RXBUF | VIRTIO_F_VERSION_1)) {
895 sc->sc_hdr_size = sizeof(struct virtio_net_hdr);
896 } else {
897 sc->sc_hdr_size = offsetof(struct virtio_net_hdr, num_buffers);
898 }
899
900 if ((features & VIRTIO_NET_F_CTRL_VQ) &&
901 (features & VIRTIO_NET_F_CTRL_RX)) {
902 sc->sc_has_ctrl = true;
903
904 cv_init(&ctrlq->ctrlq_wait, "ctrl_vq");
905 mutex_init(&ctrlq->ctrlq_wait_lock, MUTEX_DEFAULT, IPL_NET);
906 ctrlq->ctrlq_inuse = FREE;
907 } else {
908 sc->sc_has_ctrl = false;
909 }
910
911 if (sc->sc_has_ctrl && (features & VIRTIO_NET_F_MQ)) {
912 sc->sc_max_nvq_pairs = virtio_read_device_config_2(vsc,
913 VIRTIO_NET_CONFIG_MAX_VQ_PAIRS);
914
915 if (sc->sc_max_nvq_pairs > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX)
916 goto err;
917
918 /* Limit the number of queue pairs to use */
919 sc->sc_req_nvq_pairs = MIN(sc->sc_max_nvq_pairs, ncpu);
920 }
921
922 vioif_alloc_queues(sc);
923 virtio_child_attach_set_vqs(vsc, sc->sc_vqs, sc->sc_req_nvq_pairs);
924
925 #ifdef VIOIF_MPSAFE
926 softint_flags = SOFTINT_NET | SOFTINT_MPSAFE;
927 #else
928 softint_flags = SOFTINT_NET;
929 #endif
930
931 /*
932 * Allocating virtqueues
933 */
934 for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
935 rxq = &sc->sc_rxq[i];
936 txq = &sc->sc_txq[i];
937 char qname[32];
938
939 rxq->rxq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
940
941 rxq->rxq_handle_si = softint_establish(softint_flags,
942 vioif_rx_handle, rxq);
943 if (rxq->rxq_handle_si == NULL) {
944 aprint_error_dev(self, "cannot establish rx softint\n");
945 goto err;
946 }
947
948 snprintf(qname, sizeof(qname), "rx%d", i);
949 r = virtio_alloc_vq(vsc, rxq->rxq_vq, nvqs,
950 MCLBYTES + sc->sc_hdr_size, 2, qname);
951 if (r != 0)
952 goto err;
953 nvqs++;
954 rxq->rxq_vq->vq_intrhand = vioif_rx_intr;
955 rxq->rxq_vq->vq_intrhand_arg = (void *)rxq;
956 rxq->rxq_stopping = true;
957 vioif_work_set(&rxq->rxq_work, vioif_rx_handle, rxq);
958
959 txq->txq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NET);
960
961 txq->txq_deferred_transmit = softint_establish(softint_flags,
962 vioif_deferred_transmit, txq);
963 if (txq->txq_deferred_transmit == NULL) {
964 aprint_error_dev(self, "cannot establish tx softint\n");
965 goto err;
966 }
967 txq->txq_handle_si = softint_establish(softint_flags,
968 vioif_tx_handle, txq);
969 if (txq->txq_handle_si == NULL) {
970 aprint_error_dev(self, "cannot establish tx softint\n");
971 goto err;
972 }
973
974 snprintf(qname, sizeof(qname), "tx%d", i);
975 r = virtio_alloc_vq(vsc, txq->txq_vq, nvqs,
976 sc->sc_hdr_size + (ETHER_MAX_LEN - ETHER_HDR_LEN),
977 VIRTIO_NET_TX_MAXNSEGS + 1, qname);
978 if (r != 0)
979 goto err;
980 nvqs++;
981 txq->txq_vq->vq_intrhand = vioif_tx_intr;
982 txq->txq_vq->vq_intrhand_arg = (void *)txq;
983 txq->txq_link_active = sc->sc_link_active;
984 txq->txq_stopping = false;
985 txq->txq_intrq = pcq_create(txq->txq_vq->vq_num, KM_SLEEP);
986 vioif_work_set(&txq->txq_work, vioif_tx_handle, txq);
987 }
988
989 if (sc->sc_has_ctrl) {
990 /*
991 * Allocating a virtqueue for control channel
992 */
993 r = virtio_alloc_vq(vsc, ctrlq->ctrlq_vq, nvqs,
994 NBPG, 1, "control");
995 if (r != 0) {
996 aprint_error_dev(self, "failed to allocate "
997 "a virtqueue for control channel, error code %d\n",
998 r);
999
1000 sc->sc_has_ctrl = false;
1001 cv_destroy(&ctrlq->ctrlq_wait);
1002 mutex_destroy(&ctrlq->ctrlq_wait_lock);
1003 } else {
1004 nvqs++;
1005 ctrlq->ctrlq_vq->vq_intrhand = vioif_ctrl_intr;
1006 ctrlq->ctrlq_vq->vq_intrhand_arg = (void *) ctrlq;
1007 }
1008 }
1009
1010 sc->sc_ctl_softint = softint_establish(softint_flags,
1011 vioif_ctl_softint, sc);
1012 if (sc->sc_ctl_softint == NULL) {
1013 aprint_error_dev(self, "cannot establish ctl softint\n");
1014 goto err;
1015 }
1016
1017 if (vioif_alloc_mems(sc) < 0)
1018 goto err;
1019
1020 if (virtio_child_attach_finish(vsc) != 0)
1021 goto err;
1022
1023 if (vioif_setup_sysctl(sc) != 0) {
1024 aprint_error_dev(self, "unable to create sysctl node\n");
1025 /* continue */
1026 }
1027
1028 vioif_setup_stats(sc);
1029
1030 strlcpy(ifp->if_xname, device_xname(self), IFNAMSIZ);
1031 ifp->if_softc = sc;
1032 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
1033 #ifdef VIOIF_MPSAFE
1034 ifp->if_extflags = IFEF_MPSAFE;
1035 #endif
1036 ifp->if_start = vioif_start;
1037 if (sc->sc_req_nvq_pairs > 1)
1038 ifp->if_transmit = vioif_transmit;
1039 ifp->if_ioctl = vioif_ioctl;
1040 ifp->if_init = vioif_init;
1041 ifp->if_stop = vioif_stop;
1042 ifp->if_capabilities = 0;
1043 ifp->if_watchdog = vioif_watchdog;
1044 txq = &sc->sc_txq[0];
1045 IFQ_SET_MAXLEN(&ifp->if_snd, MAX(txq->txq_vq->vq_num, IFQ_MAXLEN));
1046 IFQ_SET_READY(&ifp->if_snd);
1047
1048 sc->sc_ethercom.ec_capabilities |= ETHERCAP_VLAN_MTU;
1049
1050 if_attach(ifp);
1051 if_deferred_start_init(ifp, NULL);
1052 ether_ifattach(ifp, sc->sc_mac);
1053
1054 return;
1055
1056 err:
1057 for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
1058 rxq = &sc->sc_rxq[i];
1059 txq = &sc->sc_txq[i];
1060
1061 if (rxq->rxq_lock) {
1062 mutex_obj_free(rxq->rxq_lock);
1063 rxq->rxq_lock = NULL;
1064 }
1065
1066 if (rxq->rxq_handle_si) {
1067 softint_disestablish(rxq->rxq_handle_si);
1068 rxq->rxq_handle_si = NULL;
1069 }
1070
1071 if (txq->txq_lock) {
1072 mutex_obj_free(txq->txq_lock);
1073 txq->txq_lock = NULL;
1074 }
1075
1076 if (txq->txq_handle_si) {
1077 softint_disestablish(txq->txq_handle_si);
1078 txq->txq_handle_si = NULL;
1079 }
1080
1081 if (txq->txq_deferred_transmit) {
1082 softint_disestablish(txq->txq_deferred_transmit);
1083 txq->txq_deferred_transmit = NULL;
1084 }
1085
1086 if (txq->txq_intrq) {
1087 pcq_destroy(txq->txq_intrq);
1088 txq->txq_intrq = NULL;
1089 }
1090 }
1091
1092 if (sc->sc_has_ctrl) {
1093 cv_destroy(&ctrlq->ctrlq_wait);
1094 mutex_destroy(&ctrlq->ctrlq_wait_lock);
1095 }
1096
1097 while (nvqs > 0)
1098 virtio_free_vq(vsc, &sc->sc_vqs[--nvqs]);
1099
1100 vioif_free_queues(sc);
1101 mutex_destroy(&sc->sc_lock);
1102 virtio_child_attach_failed(vsc);
1103 config_finalize_register(self, vioif_finalize_teardown);
1104
1105 return;
1106 }
1107
1108 static int
1109 vioif_finalize_teardown(device_t self)
1110 {
1111 struct vioif_softc *sc = device_private(self);
1112
1113 if (sc->sc_txrx_workqueue != NULL) {
1114 vioif_workq_destroy(sc->sc_txrx_workqueue);
1115 sc->sc_txrx_workqueue = NULL;
1116 }
1117
1118 return 0;
1119 }
1120
1121 /* we need interrupts to make promiscuous mode off */
1122 static void
1123 vioif_deferred_init(device_t self)
1124 {
1125 struct vioif_softc *sc = device_private(self);
1126 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1127 int r;
1128
1129 if (ifp->if_flags & IFF_PROMISC)
1130 return;
1131
1132 r = vioif_set_promisc(sc, false);
1133 if (r != 0)
1134 aprint_error_dev(self, "resetting promisc mode failed, "
1135 "error code %d\n", r);
1136 }
1137
1138 static void
1139 vioif_enable_interrupt_vqpairs(struct vioif_softc *sc)
1140 {
1141 struct virtio_softc *vsc = sc->sc_virtio;
1142 struct vioif_txqueue *txq;
1143 struct vioif_rxqueue *rxq;
1144 int i;
1145
1146 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1147 txq = &sc->sc_txq[i];
1148 rxq = &sc->sc_rxq[i];
1149
1150 virtio_start_vq_intr(vsc, txq->txq_vq);
1151 virtio_start_vq_intr(vsc, rxq->rxq_vq);
1152 }
1153 }
1154
1155 static void
1156 vioif_disable_interrupt_vqpairs(struct vioif_softc *sc)
1157 {
1158 struct virtio_softc *vsc = sc->sc_virtio;
1159 struct vioif_txqueue *txq;
1160 struct vioif_rxqueue *rxq;
1161 int i;
1162
1163 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1164 rxq = &sc->sc_rxq[i];
1165 txq = &sc->sc_txq[i];
1166
1167 virtio_stop_vq_intr(vsc, rxq->rxq_vq);
1168 virtio_stop_vq_intr(vsc, txq->txq_vq);
1169 }
1170 }
1171
1172 /*
1173 * Interface functions for ifnet
1174 */
1175 static int
1176 vioif_init(struct ifnet *ifp)
1177 {
1178 struct vioif_softc *sc = ifp->if_softc;
1179 struct virtio_softc *vsc = sc->sc_virtio;
1180 struct vioif_rxqueue *rxq;
1181 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1182 int r, i;
1183
1184 vioif_stop(ifp, 0);
1185
1186 r = virtio_reinit_start(vsc);
1187 if (r != 0) {
1188 log(LOG_ERR, "%s: reset failed\n", ifp->if_xname);
1189 return EIO;
1190 }
1191
1192 virtio_negotiate_features(vsc, virtio_features(vsc));
1193
1194 for (i = 0; i < sc->sc_req_nvq_pairs; i++) {
1195 rxq = &sc->sc_rxq[i];
1196
1197 /* Have to set false before vioif_populate_rx_mbufs */
1198 mutex_enter(rxq->rxq_lock);
1199 rxq->rxq_stopping = false;
1200 vioif_populate_rx_mbufs_locked(sc, rxq);
1201 mutex_exit(rxq->rxq_lock);
1202
1203 }
1204
1205 virtio_reinit_end(vsc);
1206
1207 if (sc->sc_has_ctrl)
1208 virtio_start_vq_intr(vsc, ctrlq->ctrlq_vq);
1209
1210 r = vioif_ctrl_mq_vq_pairs_set(sc, sc->sc_req_nvq_pairs);
1211 if (r == 0)
1212 sc->sc_act_nvq_pairs = sc->sc_req_nvq_pairs;
1213 else
1214 sc->sc_act_nvq_pairs = 1;
1215
1216 for (i = 0; i < sc->sc_act_nvq_pairs; i++)
1217 sc->sc_txq[i].txq_stopping = false;
1218
1219 vioif_enable_interrupt_vqpairs(sc);
1220
1221 if (!sc->sc_deferred_init_done) {
1222 sc->sc_deferred_init_done = 1;
1223 if (sc->sc_has_ctrl)
1224 vioif_deferred_init(sc->sc_dev);
1225 }
1226
1227 vioif_update_link_status(sc);
1228 ifp->if_flags |= IFF_RUNNING;
1229 ifp->if_flags &= ~IFF_OACTIVE;
1230 vioif_rx_filter(sc);
1231
1232 return 0;
1233 }
1234
1235 static void
1236 vioif_stop(struct ifnet *ifp, int disable)
1237 {
1238 struct vioif_softc *sc = ifp->if_softc;
1239 struct virtio_softc *vsc = sc->sc_virtio;
1240 struct vioif_txqueue *txq;
1241 struct vioif_rxqueue *rxq;
1242 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1243 int i;
1244
1245 /* Take the locks to ensure that ongoing TX/RX finish */
1246 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1247 txq = &sc->sc_txq[i];
1248 rxq = &sc->sc_rxq[i];
1249
1250 mutex_enter(rxq->rxq_lock);
1251 rxq->rxq_stopping = true;
1252 mutex_exit(rxq->rxq_lock);
1253
1254 mutex_enter(txq->txq_lock);
1255 txq->txq_stopping = true;
1256 mutex_exit(txq->txq_lock);
1257 }
1258
1259 /* disable interrupts */
1260 vioif_disable_interrupt_vqpairs(sc);
1261
1262 if (sc->sc_has_ctrl)
1263 virtio_stop_vq_intr(vsc, ctrlq->ctrlq_vq);
1264
1265 /* only way to stop I/O and DMA is resetting... */
1266 virtio_reset(vsc);
1267
1268 /* rendezvous for finish of handlers */
1269 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1270 txq = &sc->sc_txq[i];
1271 rxq = &sc->sc_rxq[i];
1272
1273 mutex_enter(rxq->rxq_lock);
1274 mutex_exit(rxq->rxq_lock);
1275 vioif_work_wait(sc->sc_txrx_workqueue, &rxq->rxq_work);
1276
1277 mutex_enter(txq->txq_lock);
1278 mutex_exit(txq->txq_lock);
1279 vioif_work_wait(sc->sc_txrx_workqueue, &txq->txq_work);
1280 }
1281
1282 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1283 vioif_rx_queue_clear(&sc->sc_rxq[i]);
1284 vioif_tx_queue_clear(&sc->sc_txq[i]);
1285 }
1286
1287 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
1288 sc->sc_link_active = false;
1289
1290 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1291 txq = &sc->sc_txq[i];
1292 rxq = &sc->sc_rxq[i];
1293
1294 txq->txq_link_active = false;
1295
1296 if (disable)
1297 vioif_rx_drain(rxq);
1298
1299 vioif_tx_drain(txq);
1300 }
1301 }
1302
1303 static void
1304 vioif_send_common_locked(struct ifnet *ifp, struct vioif_txqueue *txq,
1305 bool is_transmit)
1306 {
1307 struct vioif_softc *sc = ifp->if_softc;
1308 struct virtio_softc *vsc = sc->sc_virtio;
1309 struct virtqueue *vq = txq->txq_vq;
1310 struct virtio_net_hdr *hdr;
1311 struct mbuf *m;
1312 int queued = 0;
1313
1314 KASSERT(mutex_owned(txq->txq_lock));
1315
1316 if ((ifp->if_flags & IFF_RUNNING) == 0)
1317 return;
1318
1319 if (!txq->txq_link_active || txq->txq_stopping)
1320 return;
1321
1322 if ((ifp->if_flags & IFF_OACTIVE) != 0 && !is_transmit)
1323 return;
1324
1325 for (;;) {
1326 int slot, r;
1327
1328 if (is_transmit)
1329 m = pcq_get(txq->txq_intrq);
1330 else
1331 IFQ_DEQUEUE(&ifp->if_snd, m);
1332
1333 if (m == NULL)
1334 break;
1335
1336 r = virtio_enqueue_prep(vsc, vq, &slot);
1337 if (r == EAGAIN) {
1338 ifp->if_flags |= IFF_OACTIVE;
1339 m_freem(m);
1340 break;
1341 }
1342 if (r != 0)
1343 panic("enqueue_prep for a tx buffer");
1344
1345 r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
1346 txq->txq_dmamaps[slot], m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1347 if (r != 0) {
1348 /* maybe just too fragmented */
1349 struct mbuf *newm;
1350
1351 newm = m_defrag(m, M_NOWAIT);
1352 if (newm == NULL) {
1353 txq->txq_defrag_failed.ev_count++;
1354 goto skip;
1355 }
1356
1357 m = newm;
1358 r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
1359 txq->txq_dmamaps[slot], m,
1360 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1361 if (r != 0) {
1362 txq->txq_mbuf_load_failed.ev_count++;
1363 skip:
1364 m_freem(m);
1365 virtio_enqueue_abort(vsc, vq, slot);
1366 continue;
1367 }
1368 }
1369
1370 /* This should actually never fail */
1371 r = virtio_enqueue_reserve(vsc, vq, slot,
1372 txq->txq_dmamaps[slot]->dm_nsegs + 1);
1373 if (r != 0) {
1374 txq->txq_enqueue_reserve_failed.ev_count++;
1375 bus_dmamap_unload(virtio_dmat(vsc),
1376 txq->txq_dmamaps[slot]);
1377 /* slot already freed by virtio_enqueue_reserve */
1378 m_freem(m);
1379 continue;
1380 }
1381
1382 txq->txq_mbufs[slot] = m;
1383
1384 hdr = &txq->txq_hdrs[slot];
1385 memset(hdr, 0, sc->sc_hdr_size);
1386 bus_dmamap_sync(virtio_dmat(vsc), txq->txq_dmamaps[slot],
1387 0, txq->txq_dmamaps[slot]->dm_mapsize,
1388 BUS_DMASYNC_PREWRITE);
1389 bus_dmamap_sync(virtio_dmat(vsc), txq->txq_hdr_dmamaps[slot],
1390 0, txq->txq_hdr_dmamaps[slot]->dm_mapsize,
1391 BUS_DMASYNC_PREWRITE);
1392 virtio_enqueue(vsc, vq, slot, txq->txq_hdr_dmamaps[slot], true);
1393 virtio_enqueue(vsc, vq, slot, txq->txq_dmamaps[slot], true);
1394 virtio_enqueue_commit(vsc, vq, slot, false);
1395
1396 queued++;
1397 bpf_mtap(ifp, m, BPF_D_OUT);
1398 }
1399
1400 if (queued > 0) {
1401 virtio_enqueue_commit(vsc, vq, -1, true);
1402 ifp->if_timer = 5;
1403 }
1404 }
1405
1406 static void
1407 vioif_start_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
1408 {
1409
1410 /*
1411 * ifp->if_obytes and ifp->if_omcasts are added in if_transmit()@if.c.
1412 */
1413 vioif_send_common_locked(ifp, txq, false);
1414
1415 }
1416
1417 static void
1418 vioif_start(struct ifnet *ifp)
1419 {
1420 struct vioif_softc *sc = ifp->if_softc;
1421 struct vioif_txqueue *txq = &sc->sc_txq[0];
1422
1423 #ifdef VIOIF_MPSAFE
1424 KASSERT(if_is_mpsafe(ifp));
1425 #endif
1426
1427 mutex_enter(txq->txq_lock);
1428 vioif_start_locked(ifp, txq);
1429 mutex_exit(txq->txq_lock);
1430 }
1431
1432 static inline int
1433 vioif_select_txqueue(struct ifnet *ifp, struct mbuf *m)
1434 {
1435 struct vioif_softc *sc = ifp->if_softc;
1436 u_int cpuid = cpu_index(curcpu());
1437
1438 return cpuid % sc->sc_act_nvq_pairs;
1439 }
1440
1441 static void
1442 vioif_transmit_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
1443 {
1444
1445 vioif_send_common_locked(ifp, txq, true);
1446 }
1447
1448 static int
1449 vioif_transmit(struct ifnet *ifp, struct mbuf *m)
1450 {
1451 struct vioif_softc *sc = ifp->if_softc;
1452 struct vioif_txqueue *txq;
1453 int qid;
1454
1455 qid = vioif_select_txqueue(ifp, m);
1456 txq = &sc->sc_txq[qid];
1457
1458 if (__predict_false(!pcq_put(txq->txq_intrq, m))) {
1459 m_freem(m);
1460 return ENOBUFS;
1461 }
1462
1463 net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
1464 if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
1465 if (m->m_flags & M_MCAST)
1466 if_statinc_ref(nsr, if_omcasts);
1467 IF_STAT_PUTREF(ifp);
1468
1469 if (mutex_tryenter(txq->txq_lock)) {
1470 vioif_transmit_locked(ifp, txq);
1471 mutex_exit(txq->txq_lock);
1472 }
1473
1474 return 0;
1475 }
1476
1477 static void
1478 vioif_deferred_transmit(void *arg)
1479 {
1480 struct vioif_txqueue *txq = arg;
1481 struct virtio_softc *vsc = txq->txq_vq->vq_owner;
1482 struct vioif_softc *sc = device_private(virtio_child(vsc));
1483 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1484
1485 mutex_enter(txq->txq_lock);
1486 vioif_send_common_locked(ifp, txq, true);
1487 mutex_exit(txq->txq_lock);
1488 }
1489
1490 static int
1491 vioif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1492 {
1493 int s, r;
1494
1495 s = splnet();
1496
1497 r = ether_ioctl(ifp, cmd, data);
1498 if ((r == 0 && cmd == SIOCSIFFLAGS) ||
1499 (r == ENETRESET && (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI))) {
1500 if (ifp->if_flags & IFF_RUNNING)
1501 r = vioif_rx_filter(ifp->if_softc);
1502 else
1503 r = 0;
1504 }
1505
1506 splx(s);
1507
1508 return r;
1509 }
1510
1511 void
1512 vioif_watchdog(struct ifnet *ifp)
1513 {
1514 struct vioif_softc *sc = ifp->if_softc;
1515 int i;
1516
1517 if (ifp->if_flags & IFF_RUNNING) {
1518 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1519 vioif_tx_queue_clear(&sc->sc_txq[i]);
1520 }
1521 }
1522 }
1523
1524 /*
1525 * Receive implementation
1526 */
1527 /* allocate and initialize a mbuf for receive */
1528 static int
1529 vioif_add_rx_mbuf(struct vioif_rxqueue *rxq, int i)
1530 {
1531 struct virtio_softc *vsc = rxq->rxq_vq->vq_owner;
1532 struct mbuf *m;
1533 int r;
1534
1535 MGETHDR(m, M_DONTWAIT, MT_DATA);
1536 if (m == NULL)
1537 return ENOBUFS;
1538 MCLGET(m, M_DONTWAIT);
1539 if ((m->m_flags & M_EXT) == 0) {
1540 m_freem(m);
1541 return ENOBUFS;
1542 }
1543 rxq->rxq_mbufs[i] = m;
1544 m->m_len = m->m_pkthdr.len = m->m_ext.ext_size;
1545 r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
1546 rxq->rxq_dmamaps[i], m, BUS_DMA_READ | BUS_DMA_NOWAIT);
1547 if (r) {
1548 m_freem(m);
1549 rxq->rxq_mbufs[i] = NULL;
1550 return r;
1551 }
1552
1553 return 0;
1554 }
1555
1556 /* free a mbuf for receive */
1557 static void
1558 vioif_free_rx_mbuf(struct vioif_rxqueue *rxq, int i)
1559 {
1560 struct virtio_softc *vsc = rxq->rxq_vq->vq_owner;
1561
1562 bus_dmamap_unload(virtio_dmat(vsc), rxq->rxq_dmamaps[i]);
1563 m_freem(rxq->rxq_mbufs[i]);
1564 rxq->rxq_mbufs[i] = NULL;
1565 }
1566
1567 /* add mbufs for all the empty receive slots */
1568 static void
1569 vioif_populate_rx_mbufs_locked(struct vioif_softc *sc, struct vioif_rxqueue *rxq)
1570 {
1571 struct virtqueue *vq = rxq->rxq_vq;
1572 struct virtio_softc *vsc = vq->vq_owner;
1573 int i, r, ndone = 0;
1574
1575 KASSERT(mutex_owned(rxq->rxq_lock));
1576
1577 if (rxq->rxq_stopping)
1578 return;
1579
1580 for (i = 0; i < vq->vq_num; i++) {
1581 int slot;
1582 r = virtio_enqueue_prep(vsc, vq, &slot);
1583 if (r == EAGAIN)
1584 break;
1585 if (r != 0)
1586 panic("enqueue_prep for rx buffers");
1587 if (rxq->rxq_mbufs[slot] == NULL) {
1588 r = vioif_add_rx_mbuf(rxq, slot);
1589 if (r != 0) {
1590 rxq->rxq_mbuf_add_failed.ev_count++;
1591 break;
1592 }
1593 }
1594 r = virtio_enqueue_reserve(vsc, vq, slot,
1595 rxq->rxq_dmamaps[slot]->dm_nsegs + 1);
1596 if (r != 0) {
1597 vioif_free_rx_mbuf(rxq, slot);
1598 break;
1599 }
1600 bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_hdr_dmamaps[slot],
1601 0, sc->sc_hdr_size, BUS_DMASYNC_PREREAD);
1602 bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_dmamaps[slot],
1603 0, MCLBYTES, BUS_DMASYNC_PREREAD);
1604 virtio_enqueue(vsc, vq, slot, rxq->rxq_hdr_dmamaps[slot],
1605 false);
1606 virtio_enqueue(vsc, vq, slot, rxq->rxq_dmamaps[slot], false);
1607 virtio_enqueue_commit(vsc, vq, slot, false);
1608 ndone++;
1609 }
1610 if (ndone > 0)
1611 virtio_enqueue_commit(vsc, vq, -1, true);
1612 }
1613
1614 static void
1615 vioif_rx_queue_clear(struct vioif_rxqueue *rxq)
1616 {
1617 struct virtqueue *vq = rxq->rxq_vq;
1618 struct virtio_softc *vsc = vq->vq_owner;
1619 struct vioif_softc *sc = device_private(virtio_child(vsc));
1620 u_int limit = UINT_MAX;
1621 bool more;
1622
1623 KASSERT(rxq->rxq_stopping);
1624
1625 mutex_enter(rxq->rxq_lock);
1626 for (;;) {
1627 more = vioif_rx_deq_locked(sc, vsc, rxq, limit);
1628 if (more == false)
1629 break;
1630 }
1631 mutex_exit(rxq->rxq_lock);
1632 }
1633
1634 /* dequeue received packets */
1635 static bool
1636 vioif_rx_deq_locked(struct vioif_softc *sc, struct virtio_softc *vsc,
1637 struct vioif_rxqueue *rxq, u_int limit)
1638 {
1639 struct virtqueue *vq = rxq->rxq_vq;
1640 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1641 struct mbuf *m;
1642 int slot, len;
1643 bool more = false, dequeued = false;
1644
1645 KASSERT(mutex_owned(rxq->rxq_lock));
1646
1647 if (virtio_vq_is_enqueued(vsc, vq) == false)
1648 return false;
1649
1650 for (;;) {
1651 if (limit-- == 0) {
1652 more = true;
1653 break;
1654 }
1655
1656 if (virtio_dequeue(vsc, vq, &slot, &len) != 0)
1657 break;
1658
1659 dequeued = true;
1660
1661 len -= sc->sc_hdr_size;
1662 bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_hdr_dmamaps[slot],
1663 0, sc->sc_hdr_size, BUS_DMASYNC_POSTREAD);
1664 bus_dmamap_sync(virtio_dmat(vsc), rxq->rxq_dmamaps[slot],
1665 0, MCLBYTES, BUS_DMASYNC_POSTREAD);
1666 m = rxq->rxq_mbufs[slot];
1667 KASSERT(m != NULL);
1668 bus_dmamap_unload(virtio_dmat(vsc), rxq->rxq_dmamaps[slot]);
1669 rxq->rxq_mbufs[slot] = NULL;
1670 virtio_dequeue_commit(vsc, vq, slot);
1671 m_set_rcvif(m, ifp);
1672 m->m_len = m->m_pkthdr.len = len;
1673
1674 mutex_exit(rxq->rxq_lock);
1675 if_percpuq_enqueue(ifp->if_percpuq, m);
1676 mutex_enter(rxq->rxq_lock);
1677
1678 if (rxq->rxq_stopping)
1679 break;
1680 }
1681
1682 if (dequeued)
1683 vioif_populate_rx_mbufs_locked(sc, rxq);
1684
1685 return more;
1686 }
1687
1688 /* rx interrupt; call _dequeue above and schedule a softint */
1689
1690 static void
1691 vioif_rx_handle_locked(void *xrxq, u_int limit)
1692 {
1693 struct vioif_rxqueue *rxq = xrxq;
1694 struct virtqueue *vq = rxq->rxq_vq;
1695 struct virtio_softc *vsc = vq->vq_owner;
1696 struct vioif_softc *sc = device_private(virtio_child(vsc));
1697 bool more;
1698
1699 KASSERT(!rxq->rxq_stopping);
1700
1701 more = vioif_rx_deq_locked(sc, vsc, rxq, limit);
1702 if (more) {
1703 vioif_rx_sched_handle(sc, rxq);
1704 return;
1705 }
1706 more = virtio_start_vq_intr(vsc, rxq->rxq_vq);
1707 if (more) {
1708 vioif_rx_sched_handle(sc, rxq);
1709 return;
1710 }
1711 atomic_store_relaxed(&rxq->rxq_active, false);
1712 }
1713
1714 static int
1715 vioif_rx_intr(void *arg)
1716 {
1717 struct vioif_rxqueue *rxq = arg;
1718 struct virtqueue *vq = rxq->rxq_vq;
1719 struct virtio_softc *vsc = vq->vq_owner;
1720 struct vioif_softc *sc = device_private(virtio_child(vsc));
1721 u_int limit;
1722
1723 limit = sc->sc_rx_intr_process_limit;
1724
1725 if (atomic_load_relaxed(&rxq->rxq_active) == true)
1726 return 1;
1727
1728 mutex_enter(rxq->rxq_lock);
1729
1730 if (!rxq->rxq_stopping) {
1731 rxq->rxq_workqueue = sc->sc_txrx_workqueue_sysctl;
1732
1733 virtio_stop_vq_intr(vsc, vq);
1734 atomic_store_relaxed(&rxq->rxq_active, true);
1735
1736 vioif_rx_handle_locked(rxq, limit);
1737 }
1738
1739 mutex_exit(rxq->rxq_lock);
1740 return 1;
1741 }
1742
1743 static void
1744 vioif_rx_handle(void *xrxq)
1745 {
1746 struct vioif_rxqueue *rxq = xrxq;
1747 struct virtqueue *vq = rxq->rxq_vq;
1748 struct virtio_softc *vsc = vq->vq_owner;
1749 struct vioif_softc *sc = device_private(virtio_child(vsc));
1750 u_int limit;
1751
1752 limit = sc->sc_rx_process_limit;
1753
1754 mutex_enter(rxq->rxq_lock);
1755
1756 if (!rxq->rxq_stopping)
1757 vioif_rx_handle_locked(rxq, limit);
1758
1759 mutex_exit(rxq->rxq_lock);
1760 }
1761
1762 static void
1763 vioif_rx_sched_handle(struct vioif_softc *sc, struct vioif_rxqueue *rxq)
1764 {
1765
1766 KASSERT(mutex_owned(rxq->rxq_lock));
1767
1768 if (rxq->rxq_stopping)
1769 return;
1770
1771 if (rxq->rxq_workqueue)
1772 vioif_work_add(sc->sc_txrx_workqueue, &rxq->rxq_work);
1773 else
1774 softint_schedule(rxq->rxq_handle_si);
1775 }
1776
1777 /* free all the mbufs; called from if_stop(disable) */
1778 static void
1779 vioif_rx_drain(struct vioif_rxqueue *rxq)
1780 {
1781 struct virtqueue *vq = rxq->rxq_vq;
1782 int i;
1783
1784 for (i = 0; i < vq->vq_num; i++) {
1785 if (rxq->rxq_mbufs[i] == NULL)
1786 continue;
1787 vioif_free_rx_mbuf(rxq, i);
1788 }
1789 }
1790
1791 /*
1792 * Transmition implementation
1793 */
1794 /* actual transmission is done in if_start */
1795 /* tx interrupt; dequeue and free mbufs */
1796 /*
1797 * tx interrupt is actually disabled; this should be called upon
1798 * tx vq full and watchdog
1799 */
1800
1801 static void
1802 vioif_tx_handle_locked(struct vioif_txqueue *txq, u_int limit)
1803 {
1804 struct virtqueue *vq = txq->txq_vq;
1805 struct virtio_softc *vsc = vq->vq_owner;
1806 struct vioif_softc *sc = device_private(virtio_child(vsc));
1807 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1808 bool more;
1809
1810 KASSERT(!txq->txq_stopping);
1811
1812 more = vioif_tx_deq_locked(sc, vsc, txq, limit);
1813 if (more) {
1814 vioif_tx_sched_handle(sc, txq);
1815 return;
1816 }
1817
1818 if (virtio_features(vsc) & VIRTIO_F_RING_EVENT_IDX)
1819 more = virtio_postpone_intr_smart(vsc, vq);
1820 else
1821 more = virtio_start_vq_intr(vsc, vq);
1822 if (more) {
1823 vioif_tx_sched_handle(sc, txq);
1824 return;
1825 }
1826
1827 atomic_store_relaxed(&txq->txq_active, false);
1828 /* for ALTQ */
1829 if (txq == &sc->sc_txq[0]) {
1830 if_schedule_deferred_start(ifp);
1831 ifp->if_flags &= ~IFF_OACTIVE;
1832 }
1833 softint_schedule(txq->txq_deferred_transmit);
1834 }
1835
1836
1837 static int
1838 vioif_tx_intr(void *arg)
1839 {
1840 struct vioif_txqueue *txq = arg;
1841 struct virtqueue *vq = txq->txq_vq;
1842 struct virtio_softc *vsc = vq->vq_owner;
1843 struct vioif_softc *sc = device_private(virtio_child(vsc));
1844 u_int limit;
1845
1846 limit = sc->sc_tx_intr_process_limit;
1847
1848 if (atomic_load_relaxed(&txq->txq_active) == true)
1849 return 1;
1850
1851 mutex_enter(txq->txq_lock);
1852
1853 if (!txq->txq_stopping) {
1854 txq->txq_workqueue = sc->sc_txrx_workqueue_sysctl;
1855
1856 virtio_stop_vq_intr(vsc, vq);
1857 atomic_store_relaxed(&txq->txq_active, true);
1858
1859 vioif_tx_handle_locked(txq, limit);
1860 }
1861
1862 mutex_exit(txq->txq_lock);
1863
1864 return 1;
1865 }
1866
1867 static void
1868 vioif_tx_handle(void *xtxq)
1869 {
1870 struct vioif_txqueue *txq = xtxq;
1871 struct virtqueue *vq = txq->txq_vq;
1872 struct virtio_softc *vsc = vq->vq_owner;
1873 struct vioif_softc *sc = device_private(virtio_child(vsc));
1874 u_int limit;
1875
1876 limit = sc->sc_tx_process_limit;
1877
1878 mutex_enter(txq->txq_lock);
1879 if (!txq->txq_stopping)
1880 vioif_tx_handle_locked(txq, limit);
1881 mutex_exit(txq->txq_lock);
1882 }
1883
1884 static void
1885 vioif_tx_sched_handle(struct vioif_softc *sc, struct vioif_txqueue *txq)
1886 {
1887
1888 KASSERT(mutex_owned(txq->txq_lock));
1889
1890 if (txq->txq_stopping)
1891 return;
1892
1893 if (txq->txq_workqueue)
1894 vioif_work_add(sc->sc_txrx_workqueue, &txq->txq_work);
1895 else
1896 softint_schedule(txq->txq_handle_si);
1897 }
1898
1899 static void
1900 vioif_tx_queue_clear(struct vioif_txqueue *txq)
1901 {
1902 struct virtqueue *vq = txq->txq_vq;
1903 struct virtio_softc *vsc = vq->vq_owner;
1904 struct vioif_softc *sc = device_private(virtio_child(vsc));
1905 u_int limit = UINT_MAX;
1906 bool more;
1907
1908 mutex_enter(txq->txq_lock);
1909 for (;;) {
1910 more = vioif_tx_deq_locked(sc, vsc, txq, limit);
1911 if (more == false)
1912 break;
1913 }
1914 mutex_exit(txq->txq_lock);
1915 }
1916
1917 static bool
1918 vioif_tx_deq_locked(struct vioif_softc *sc, struct virtio_softc *vsc,
1919 struct vioif_txqueue *txq, u_int limit)
1920 {
1921 struct virtqueue *vq = txq->txq_vq;
1922 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1923 struct mbuf *m;
1924 int slot, len;
1925 bool more = false;
1926
1927 KASSERT(mutex_owned(txq->txq_lock));
1928
1929 if (virtio_vq_is_enqueued(vsc, vq) == false)
1930 return false;
1931
1932 for (;;) {
1933 if (limit-- == 0) {
1934 more = true;
1935 break;
1936 }
1937
1938 if (virtio_dequeue(vsc, vq, &slot, &len) != 0)
1939 break;
1940
1941 bus_dmamap_sync(virtio_dmat(vsc), txq->txq_hdr_dmamaps[slot],
1942 0, sc->sc_hdr_size, BUS_DMASYNC_POSTWRITE);
1943 bus_dmamap_sync(virtio_dmat(vsc), txq->txq_dmamaps[slot],
1944 0, txq->txq_dmamaps[slot]->dm_mapsize,
1945 BUS_DMASYNC_POSTWRITE);
1946 m = txq->txq_mbufs[slot];
1947 bus_dmamap_unload(virtio_dmat(vsc), txq->txq_dmamaps[slot]);
1948 txq->txq_mbufs[slot] = NULL;
1949 virtio_dequeue_commit(vsc, vq, slot);
1950 if_statinc(ifp, if_opackets);
1951 m_freem(m);
1952 }
1953
1954 return more;
1955 }
1956
1957 /* free all the mbufs already put on vq; called from if_stop(disable) */
1958 static void
1959 vioif_tx_drain(struct vioif_txqueue *txq)
1960 {
1961 struct virtqueue *vq = txq->txq_vq;
1962 struct virtio_softc *vsc = vq->vq_owner;
1963 int i;
1964
1965 KASSERT(txq->txq_stopping);
1966
1967 for (i = 0; i < vq->vq_num; i++) {
1968 if (txq->txq_mbufs[i] == NULL)
1969 continue;
1970 bus_dmamap_unload(virtio_dmat(vsc), txq->txq_dmamaps[i]);
1971 m_freem(txq->txq_mbufs[i]);
1972 txq->txq_mbufs[i] = NULL;
1973 }
1974 }
1975
1976 /*
1977 * Control vq
1978 */
1979 /* issue a VIRTIO_NET_CTRL_RX class command and wait for completion */
1980 static void
1981 vioif_ctrl_acquire(struct vioif_softc *sc)
1982 {
1983 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1984
1985 mutex_enter(&ctrlq->ctrlq_wait_lock);
1986 while (ctrlq->ctrlq_inuse != FREE)
1987 cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
1988 ctrlq->ctrlq_inuse = INUSE;
1989 ctrlq->ctrlq_owner = curlwp;
1990 mutex_exit(&ctrlq->ctrlq_wait_lock);
1991 }
1992
1993 static void
1994 vioif_ctrl_release(struct vioif_softc *sc)
1995 {
1996 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1997
1998 KASSERT(ctrlq->ctrlq_inuse != FREE);
1999 KASSERT(ctrlq->ctrlq_owner == curlwp);
2000
2001 mutex_enter(&ctrlq->ctrlq_wait_lock);
2002 ctrlq->ctrlq_inuse = FREE;
2003 ctrlq->ctrlq_owner = NULL;
2004 cv_signal(&ctrlq->ctrlq_wait);
2005 mutex_exit(&ctrlq->ctrlq_wait_lock);
2006 }
2007
2008 static int
2009 vioif_ctrl_load_cmdspec(struct vioif_softc *sc,
2010 struct vioif_ctrl_cmdspec *specs, int nspecs)
2011 {
2012 struct virtio_softc *vsc = sc->sc_virtio;
2013 int i, r, loaded;
2014
2015 loaded = 0;
2016 for (i = 0; i < nspecs; i++) {
2017 r = bus_dmamap_load(virtio_dmat(vsc),
2018 specs[i].dmamap, specs[i].buf, specs[i].bufsize,
2019 NULL, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
2020 if (r) {
2021 sc->sc_ctrlq.ctrlq_cmd_load_failed.ev_count++;
2022 goto err;
2023 }
2024 loaded++;
2025
2026 }
2027
2028 return r;
2029
2030 err:
2031 for (i = 0; i < loaded; i++) {
2032 bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
2033 }
2034
2035 return r;
2036 }
2037
2038 static void
2039 vioif_ctrl_unload_cmdspec(struct vioif_softc *sc,
2040 struct vioif_ctrl_cmdspec *specs, int nspecs)
2041 {
2042 struct virtio_softc *vsc = sc->sc_virtio;
2043 int i;
2044
2045 for (i = 0; i < nspecs; i++) {
2046 bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
2047 }
2048 }
2049
2050 static int
2051 vioif_ctrl_send_command(struct vioif_softc *sc, uint8_t class, uint8_t cmd,
2052 struct vioif_ctrl_cmdspec *specs, int nspecs)
2053 {
2054 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
2055 struct virtqueue *vq = ctrlq->ctrlq_vq;
2056 struct virtio_softc *vsc = sc->sc_virtio;
2057 int i, r, slot;
2058
2059 ctrlq->ctrlq_cmd->class = class;
2060 ctrlq->ctrlq_cmd->command = cmd;
2061
2062 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap,
2063 0, sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_PREWRITE);
2064 for (i = 0; i < nspecs; i++) {
2065 bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap,
2066 0, specs[i].bufsize, BUS_DMASYNC_PREWRITE);
2067 }
2068 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap,
2069 0, sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_PREREAD);
2070
2071 /* we need to explicitly (re)start vq intr when using RING EVENT IDX */
2072 if (virtio_features(vsc) & VIRTIO_F_RING_EVENT_IDX)
2073 virtio_start_vq_intr(vsc, ctrlq->ctrlq_vq);
2074
2075 r = virtio_enqueue_prep(vsc, vq, &slot);
2076 if (r != 0)
2077 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
2078 r = virtio_enqueue_reserve(vsc, vq, slot, nspecs + 2);
2079 if (r != 0)
2080 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
2081 virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_cmd_dmamap, true);
2082 for (i = 0; i < nspecs; i++) {
2083 virtio_enqueue(vsc, vq, slot, specs[i].dmamap, true);
2084 }
2085 virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_status_dmamap, false);
2086 virtio_enqueue_commit(vsc, vq, slot, true);
2087
2088 /* wait for done */
2089 mutex_enter(&ctrlq->ctrlq_wait_lock);
2090 while (ctrlq->ctrlq_inuse != DONE)
2091 cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
2092 mutex_exit(&ctrlq->ctrlq_wait_lock);
2093 /* already dequeueued */
2094
2095 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap, 0,
2096 sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_POSTWRITE);
2097 for (i = 0; i < nspecs; i++) {
2098 bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap, 0,
2099 specs[i].bufsize, BUS_DMASYNC_POSTWRITE);
2100 }
2101 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap, 0,
2102 sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_POSTREAD);
2103
2104 if (ctrlq->ctrlq_status->ack == VIRTIO_NET_OK)
2105 r = 0;
2106 else {
2107 device_printf(sc->sc_dev, "failed setting rx mode\n");
2108 sc->sc_ctrlq.ctrlq_cmd_failed.ev_count++;
2109 r = EIO;
2110 }
2111
2112 return r;
2113 }
2114
2115 static int
2116 vioif_ctrl_rx(struct vioif_softc *sc, int cmd, bool onoff)
2117 {
2118 struct virtio_net_ctrl_rx *rx = sc->sc_ctrlq.ctrlq_rx;
2119 struct vioif_ctrl_cmdspec specs[1];
2120 int r;
2121
2122 if (!sc->sc_has_ctrl)
2123 return ENOTSUP;
2124
2125 vioif_ctrl_acquire(sc);
2126
2127 rx->onoff = onoff;
2128 specs[0].dmamap = sc->sc_ctrlq.ctrlq_rx_dmamap;
2129 specs[0].buf = rx;
2130 specs[0].bufsize = sizeof(*rx);
2131
2132 r = vioif_ctrl_send_command(sc, VIRTIO_NET_CTRL_RX, cmd,
2133 specs, __arraycount(specs));
2134
2135 vioif_ctrl_release(sc);
2136 return r;
2137 }
2138
2139 static int
2140 vioif_set_promisc(struct vioif_softc *sc, bool onoff)
2141 {
2142 return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_PROMISC, onoff);
2143 }
2144
2145 static int
2146 vioif_set_allmulti(struct vioif_softc *sc, bool onoff)
2147 {
2148 return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, onoff);
2149 }
2150
2151 /* issue VIRTIO_NET_CTRL_MAC_TABLE_SET command and wait for completion */
2152 static int
2153 vioif_set_rx_filter(struct vioif_softc *sc)
2154 {
2155 /* filter already set in ctrlq->ctrlq_mac_tbl */
2156 struct virtio_softc *vsc = sc->sc_virtio;
2157 struct virtio_net_ctrl_mac_tbl *mac_tbl_uc, *mac_tbl_mc;
2158 struct vioif_ctrl_cmdspec specs[2];
2159 int nspecs = __arraycount(specs);
2160 int r;
2161
2162 mac_tbl_uc = sc->sc_ctrlq.ctrlq_mac_tbl_uc;
2163 mac_tbl_mc = sc->sc_ctrlq.ctrlq_mac_tbl_mc;
2164
2165 if (!sc->sc_has_ctrl)
2166 return ENOTSUP;
2167
2168 vioif_ctrl_acquire(sc);
2169
2170 specs[0].dmamap = sc->sc_ctrlq.ctrlq_tbl_uc_dmamap;
2171 specs[0].buf = mac_tbl_uc;
2172 specs[0].bufsize = sizeof(*mac_tbl_uc)
2173 + (ETHER_ADDR_LEN * virtio_rw32(vsc, mac_tbl_uc->nentries));
2174
2175 specs[1].dmamap = sc->sc_ctrlq.ctrlq_tbl_mc_dmamap;
2176 specs[1].buf = mac_tbl_mc;
2177 specs[1].bufsize = sizeof(*mac_tbl_mc)
2178 + (ETHER_ADDR_LEN * virtio_rw32(vsc, mac_tbl_mc->nentries));
2179
2180 r = vioif_ctrl_load_cmdspec(sc, specs, nspecs);
2181 if (r != 0)
2182 goto out;
2183
2184 r = vioif_ctrl_send_command(sc,
2185 VIRTIO_NET_CTRL_MAC, VIRTIO_NET_CTRL_MAC_TABLE_SET,
2186 specs, nspecs);
2187
2188 vioif_ctrl_unload_cmdspec(sc, specs, nspecs);
2189
2190 out:
2191 vioif_ctrl_release(sc);
2192
2193 return r;
2194 }
2195
2196 static int
2197 vioif_set_mac_addr(struct vioif_softc *sc)
2198 {
2199 struct virtio_net_ctrl_mac_addr *ma =
2200 sc->sc_ctrlq.ctrlq_mac_addr;
2201 struct vioif_ctrl_cmdspec specs[1];
2202 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2203 int nspecs = __arraycount(specs);
2204 int r;
2205
2206 if (!sc->sc_has_ctrl)
2207 return ENOTSUP;
2208
2209 vioif_ctrl_acquire(sc);
2210
2211 memcpy(ma->mac, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
2212 specs[0].dmamap = sc->sc_ctrlq.ctrlq_mac_addr_dmamap;
2213 specs[0].buf = ma;
2214 specs[0].bufsize = sizeof(*ma);
2215
2216 r = vioif_ctrl_send_command(sc,
2217 VIRTIO_NET_CTRL_MAC, VIRTIO_NET_CTRL_MAC_ADDR_SET,
2218 specs, nspecs);
2219
2220 vioif_ctrl_release(sc);
2221
2222 return r;
2223 }
2224
2225 static int
2226 vioif_ctrl_mq_vq_pairs_set(struct vioif_softc *sc, int nvq_pairs)
2227 {
2228 struct virtio_net_ctrl_mq *mq = sc->sc_ctrlq.ctrlq_mq;
2229 struct vioif_ctrl_cmdspec specs[1];
2230 int r;
2231
2232 if (!sc->sc_has_ctrl)
2233 return ENOTSUP;
2234
2235 if (nvq_pairs <= 1)
2236 return EINVAL;
2237
2238 vioif_ctrl_acquire(sc);
2239
2240 mq->virtqueue_pairs = virtio_rw16(sc->sc_virtio, nvq_pairs);
2241 specs[0].dmamap = sc->sc_ctrlq.ctrlq_mq_dmamap;
2242 specs[0].buf = mq;
2243 specs[0].bufsize = sizeof(*mq);
2244
2245 r = vioif_ctrl_send_command(sc,
2246 VIRTIO_NET_CTRL_MQ, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET,
2247 specs, __arraycount(specs));
2248
2249 vioif_ctrl_release(sc);
2250
2251 return r;
2252 }
2253
2254 /* ctrl vq interrupt; wake up the command issuer */
2255 static int
2256 vioif_ctrl_intr(void *arg)
2257 {
2258 struct vioif_ctrlqueue *ctrlq = arg;
2259 struct virtqueue *vq = ctrlq->ctrlq_vq;
2260 struct virtio_softc *vsc = vq->vq_owner;
2261 int r, slot;
2262
2263 if (virtio_vq_is_enqueued(vsc, vq) == false)
2264 return 0;
2265
2266 r = virtio_dequeue(vsc, vq, &slot, NULL);
2267 if (r == ENOENT)
2268 return 0;
2269 virtio_dequeue_commit(vsc, vq, slot);
2270
2271 mutex_enter(&ctrlq->ctrlq_wait_lock);
2272 ctrlq->ctrlq_inuse = DONE;
2273 cv_signal(&ctrlq->ctrlq_wait);
2274 mutex_exit(&ctrlq->ctrlq_wait_lock);
2275
2276 return 1;
2277 }
2278
2279 /*
2280 * If IFF_PROMISC requested, set promiscuous
2281 * If multicast filter small enough (<=MAXENTRIES) set rx filter
2282 * If large multicast filter exist use ALLMULTI
2283 */
2284 /*
2285 * If setting rx filter fails fall back to ALLMULTI
2286 * If ALLMULTI fails fall back to PROMISC
2287 */
2288 static int
2289 vioif_rx_filter(struct vioif_softc *sc)
2290 {
2291 struct virtio_softc *vsc = sc->sc_virtio;
2292 struct ethercom *ec = &sc->sc_ethercom;
2293 struct ifnet *ifp = &ec->ec_if;
2294 struct ether_multi *enm;
2295 struct ether_multistep step;
2296 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
2297 int nentries;
2298 int promisc = 0, allmulti = 0, rxfilter = 0;
2299 int r;
2300
2301 if (!sc->sc_has_ctrl) { /* no ctrl vq; always promisc */
2302 ifp->if_flags |= IFF_PROMISC;
2303 return 0;
2304 }
2305
2306 if (ifp->if_flags & IFF_PROMISC) {
2307 promisc = 1;
2308 goto set;
2309 }
2310
2311 memcpy(ctrlq->ctrlq_mac_tbl_uc->macs[0],
2312 CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
2313
2314 nentries = -1;
2315 ETHER_LOCK(ec);
2316 ETHER_FIRST_MULTI(step, ec, enm);
2317 while (nentries++, enm != NULL) {
2318 if (nentries >= VIRTIO_NET_CTRL_MAC_MAXENTRIES) {
2319 allmulti = 1;
2320 goto set_unlock;
2321 }
2322 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2323 allmulti = 1;
2324 goto set_unlock;
2325 }
2326 memcpy(ctrlq->ctrlq_mac_tbl_mc->macs[nentries],
2327 enm->enm_addrlo, ETHER_ADDR_LEN);
2328 ETHER_NEXT_MULTI(step, enm);
2329 }
2330 rxfilter = 1;
2331
2332 set_unlock:
2333 ETHER_UNLOCK(ec);
2334
2335 set:
2336 r = vioif_set_mac_addr(sc);
2337 if (r != 0) {
2338 log(LOG_WARNING, "%s: couldn't set MAC address\n",
2339 ifp->if_xname);
2340 }
2341
2342 if (rxfilter) {
2343 ctrlq->ctrlq_mac_tbl_uc->nentries = virtio_rw32(vsc, 1);
2344 ctrlq->ctrlq_mac_tbl_mc->nentries = virtio_rw32(vsc, nentries);
2345 r = vioif_set_rx_filter(sc);
2346 if (r != 0) {
2347 rxfilter = 0;
2348 allmulti = 1; /* fallback */
2349 }
2350 } else {
2351 /* remove rx filter */
2352 ctrlq->ctrlq_mac_tbl_uc->nentries = virtio_rw32(vsc, 0);
2353 ctrlq->ctrlq_mac_tbl_mc->nentries = virtio_rw32(vsc, 0);
2354 r = vioif_set_rx_filter(sc);
2355 /* what to do on failure? */
2356 }
2357 if (allmulti) {
2358 r = vioif_set_allmulti(sc, true);
2359 if (r != 0) {
2360 allmulti = 0;
2361 promisc = 1; /* fallback */
2362 }
2363 } else {
2364 r = vioif_set_allmulti(sc, false);
2365 /* what to do on failure? */
2366 }
2367 if (promisc) {
2368 r = vioif_set_promisc(sc, true);
2369 } else {
2370 r = vioif_set_promisc(sc, false);
2371 }
2372
2373 return r;
2374 }
2375
2376 static bool
2377 vioif_is_link_up(struct vioif_softc *sc)
2378 {
2379 struct virtio_softc *vsc = sc->sc_virtio;
2380 uint16_t status;
2381
2382 if (virtio_features(vsc) & VIRTIO_NET_F_STATUS)
2383 status = virtio_read_device_config_2(vsc,
2384 VIRTIO_NET_CONFIG_STATUS);
2385 else
2386 status = VIRTIO_NET_S_LINK_UP;
2387
2388 return ((status & VIRTIO_NET_S_LINK_UP) != 0);
2389 }
2390
2391 /* change link status */
2392 static void
2393 vioif_update_link_status(struct vioif_softc *sc)
2394 {
2395 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2396 struct vioif_txqueue *txq;
2397 bool active, changed;
2398 int link, i;
2399
2400 mutex_enter(&sc->sc_lock);
2401
2402 active = vioif_is_link_up(sc);
2403 changed = false;
2404
2405 if (active) {
2406 if (!sc->sc_link_active)
2407 changed = true;
2408
2409 link = LINK_STATE_UP;
2410 sc->sc_link_active = true;
2411 } else {
2412 if (sc->sc_link_active)
2413 changed = true;
2414
2415 link = LINK_STATE_DOWN;
2416 sc->sc_link_active = false;
2417 }
2418
2419 if (changed) {
2420 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
2421 txq = &sc->sc_txq[i];
2422
2423 mutex_enter(txq->txq_lock);
2424 txq->txq_link_active = sc->sc_link_active;
2425 mutex_exit(txq->txq_lock);
2426 }
2427
2428 if_link_state_change(ifp, link);
2429 }
2430
2431 mutex_exit(&sc->sc_lock);
2432 }
2433
2434 static int
2435 vioif_config_change(struct virtio_softc *vsc)
2436 {
2437 struct vioif_softc *sc = device_private(virtio_child(vsc));
2438
2439 softint_schedule(sc->sc_ctl_softint);
2440 return 0;
2441 }
2442
2443 static void
2444 vioif_ctl_softint(void *arg)
2445 {
2446 struct vioif_softc *sc = arg;
2447 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2448
2449 vioif_update_link_status(sc);
2450 vioif_start(ifp);
2451 }
2452
2453 static struct workqueue *
2454 vioif_workq_create(const char *name, pri_t prio, int ipl, int flags)
2455 {
2456 struct workqueue *wq;
2457 int error;
2458
2459 error = workqueue_create(&wq, name, vioif_workq_work, NULL,
2460 prio, ipl, flags);
2461
2462 if (error)
2463 return NULL;
2464
2465 return wq;
2466 }
2467
2468 static void
2469 vioif_workq_destroy(struct workqueue *wq)
2470 {
2471
2472 workqueue_destroy(wq);
2473 }
2474
2475 static void
2476 vioif_workq_work(struct work *wk, void *context)
2477 {
2478 struct vioif_work *work;
2479
2480 work = container_of(wk, struct vioif_work, cookie);
2481
2482 atomic_store_relaxed(&work->added, 0);
2483 work->func(work->arg);
2484 }
2485
2486 static void
2487 vioif_work_set(struct vioif_work *work, void (*func)(void *), void *arg)
2488 {
2489
2490 memset(work, 0, sizeof(*work));
2491 work->func = func;
2492 work->arg = arg;
2493 }
2494
2495 static void
2496 vioif_work_add(struct workqueue *wq, struct vioif_work *work)
2497 {
2498
2499 if (atomic_load_relaxed(&work->added) != 0)
2500 return;
2501
2502 atomic_store_relaxed(&work->added, 1);
2503 kpreempt_disable();
2504 workqueue_enqueue(wq, &work->cookie, NULL);
2505 kpreempt_enable();
2506 }
2507
2508 static void
2509 vioif_work_wait(struct workqueue *wq, struct vioif_work *work)
2510 {
2511
2512 workqueue_wait(wq, &work->cookie);
2513 }
2514
2515 static int
2516 vioif_setup_sysctl(struct vioif_softc *sc)
2517 {
2518 const char *devname;
2519 struct sysctllog **log;
2520 const struct sysctlnode *rnode, *rxnode, *txnode;
2521 int error;
2522
2523 log = &sc->sc_sysctllog;
2524 devname = device_xname(sc->sc_dev);
2525
2526 error = sysctl_createv(log, 0, NULL, &rnode,
2527 0, CTLTYPE_NODE, devname,
2528 SYSCTL_DESCR("virtio-net information and settings"),
2529 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
2530 if (error)
2531 goto out;
2532
2533 error = sysctl_createv(log, 0, &rnode, NULL,
2534 CTLFLAG_READWRITE, CTLTYPE_BOOL, "txrx_workqueue",
2535 SYSCTL_DESCR("Use workqueue for packet processing"),
2536 NULL, 0, &sc->sc_txrx_workqueue_sysctl, 0, CTL_CREATE, CTL_EOL);
2537 if (error)
2538 goto out;
2539
2540 error = sysctl_createv(log, 0, &rnode, &rxnode,
2541 0, CTLTYPE_NODE, "rx",
2542 SYSCTL_DESCR("virtio-net information and settings for Rx"),
2543 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
2544 if (error)
2545 goto out;
2546
2547 error = sysctl_createv(log, 0, &rxnode, NULL,
2548 CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
2549 SYSCTL_DESCR("max number of Rx packets to process for interrupt processing"),
2550 NULL, 0, &sc->sc_rx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
2551 if (error)
2552 goto out;
2553
2554 error = sysctl_createv(log, 0, &rxnode, NULL,
2555 CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
2556 SYSCTL_DESCR("max number of Rx packets to process for deferred processing"),
2557 NULL, 0, &sc->sc_rx_process_limit, 0, CTL_CREATE, CTL_EOL);
2558 if (error)
2559 goto out;
2560
2561 error = sysctl_createv(log, 0, &rnode, &txnode,
2562 0, CTLTYPE_NODE, "tx",
2563 SYSCTL_DESCR("virtio-net information and settings for Tx"),
2564 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
2565 if (error)
2566 goto out;
2567
2568 error = sysctl_createv(log, 0, &txnode, NULL,
2569 CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
2570 SYSCTL_DESCR("max number of Tx packets to process for interrupt processing"),
2571 NULL, 0, &sc->sc_tx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
2572 if (error)
2573 goto out;
2574
2575 error = sysctl_createv(log, 0, &txnode, NULL,
2576 CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
2577 SYSCTL_DESCR("max number of Tx packets to process for deferred processing"),
2578 NULL, 0, &sc->sc_tx_process_limit, 0, CTL_CREATE, CTL_EOL);
2579
2580 out:
2581 if (error)
2582 sysctl_teardown(log);
2583
2584 return error;
2585 }
2586
2587 static void
2588 vioif_setup_stats(struct vioif_softc *sc)
2589 {
2590 struct vioif_rxqueue *rxq;
2591 struct vioif_txqueue *txq;
2592 int i;
2593
2594 for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
2595 rxq = &sc->sc_rxq[i];
2596 txq = &sc->sc_txq[i];
2597
2598 snprintf(txq->txq_evgroup, sizeof(txq->txq_evgroup), "%s-TX%d",
2599 device_xname(sc->sc_dev), i);
2600 evcnt_attach_dynamic(&txq->txq_defrag_failed, EVCNT_TYPE_MISC,
2601 NULL, txq->txq_evgroup, "tx m_defrag() failed");
2602 evcnt_attach_dynamic(&txq->txq_mbuf_load_failed, EVCNT_TYPE_MISC,
2603 NULL, txq->txq_evgroup, "tx dmamap load failed");
2604 evcnt_attach_dynamic(&txq->txq_enqueue_reserve_failed, EVCNT_TYPE_MISC,
2605 NULL, txq->txq_evgroup, "virtio_enqueue_reserve failed");
2606
2607 snprintf(rxq->rxq_evgroup, sizeof(rxq->rxq_evgroup), "%s-RX%d",
2608 device_xname(sc->sc_dev), i);
2609 evcnt_attach_dynamic(&rxq->rxq_mbuf_add_failed, EVCNT_TYPE_MISC,
2610 NULL, rxq->rxq_evgroup, "rx mbuf allocation failed");
2611 }
2612
2613 evcnt_attach_dynamic(&sc->sc_ctrlq.ctrlq_cmd_load_failed, EVCNT_TYPE_MISC,
2614 NULL, device_xname(sc->sc_dev), "control command dmamap load failed");
2615 evcnt_attach_dynamic(&sc->sc_ctrlq.ctrlq_cmd_failed, EVCNT_TYPE_MISC,
2616 NULL, device_xname(sc->sc_dev), "control command failed");
2617 }
2618
2619 MODULE(MODULE_CLASS_DRIVER, if_vioif, "virtio");
2620
2621 #ifdef _MODULE
2622 #include "ioconf.c"
2623 #endif
2624
2625 static int
2626 if_vioif_modcmd(modcmd_t cmd, void *opaque)
2627 {
2628 int error = 0;
2629
2630 #ifdef _MODULE
2631 switch (cmd) {
2632 case MODULE_CMD_INIT:
2633 error = config_init_component(cfdriver_ioconf_if_vioif,
2634 cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
2635 break;
2636 case MODULE_CMD_FINI:
2637 error = config_fini_component(cfdriver_ioconf_if_vioif,
2638 cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
2639 break;
2640 default:
2641 error = ENOTTY;
2642 break;
2643 }
2644 #endif
2645
2646 return error;
2647 }
2648