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