if_vioif.c revision 1.95 1 /* $NetBSD: if_vioif.c,v 1.95 2023/03/23 02:26:43 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.95 2023/03/23 02:26:43 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
1311 if (is_transmit)
1312 m = pcq_get(txq->txq_intrq);
1313 else
1314 IFQ_DEQUEUE(&ifp->if_snd, m);
1315
1316 if (m == NULL)
1317 break;
1318
1319 r = virtio_enqueue_prep(vsc, vq, &slot);
1320 if (r == EAGAIN) {
1321 ifp->if_flags |= IFF_OACTIVE;
1322 m_freem(m);
1323 if_statinc(ifp, if_oerrors);
1324 break;
1325 }
1326 if (r != 0)
1327 panic("enqueue_prep for a tx buffer");
1328
1329 map = &txq->txq_maps[slot];
1330 KASSERT(map->vnm_mbuf == NULL);
1331
1332 r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
1333 map->vnm_mbuf_map, m, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1334 if (r != 0) {
1335 /* maybe just too fragmented */
1336 struct mbuf *newm;
1337
1338 newm = m_defrag(m, M_NOWAIT);
1339 if (newm == NULL) {
1340 txq->txq_defrag_failed.ev_count++;
1341 goto skip;
1342 }
1343
1344 m = newm;
1345 r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
1346 map->vnm_mbuf_map, m,
1347 BUS_DMA_WRITE | BUS_DMA_NOWAIT);
1348 if (r != 0) {
1349 txq->txq_mbuf_load_failed.ev_count++;
1350 skip:
1351 m_freem(m);
1352 if_statinc(ifp, if_oerrors);
1353 virtio_enqueue_abort(vsc, vq, slot);
1354 continue;
1355 }
1356 }
1357
1358 /* This should actually never fail */
1359 r = virtio_enqueue_reserve(vsc, vq, slot,
1360 map->vnm_mbuf_map->dm_nsegs + 1);
1361 if (r != 0) {
1362 txq->txq_enqueue_reserve_failed.ev_count++;
1363 bus_dmamap_unload(virtio_dmat(vsc),
1364 map->vnm_mbuf_map);
1365 /* slot already freed by virtio_enqueue_reserve */
1366 m_freem(m);
1367 if_statinc(ifp, if_oerrors);
1368 continue;
1369 }
1370
1371 map->vnm_mbuf = m;
1372 hdr = map->vnm_hdr;
1373 memset(hdr, 0, sc->sc_hdr_size);
1374 bus_dmamap_sync(virtio_dmat(vsc), map->vnm_mbuf_map,
1375 0, map->vnm_mbuf_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
1376 bus_dmamap_sync(virtio_dmat(vsc), map->vnm_hdr_map,
1377 0, map->vnm_hdr_map->dm_mapsize, BUS_DMASYNC_PREWRITE);
1378 virtio_enqueue(vsc, vq, slot, map->vnm_hdr_map, true);
1379 virtio_enqueue(vsc, vq, slot, map->vnm_mbuf_map, true);
1380 virtio_enqueue_commit(vsc, vq, slot, false);
1381
1382 queued++;
1383 bpf_mtap(ifp, m, BPF_D_OUT);
1384 }
1385
1386 if (queued > 0) {
1387 virtio_enqueue_commit(vsc, vq, -1, true);
1388 ifp->if_timer = 5;
1389 }
1390 }
1391
1392 static void
1393 vioif_start_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
1394 {
1395
1396 /*
1397 * ifp->if_obytes and ifp->if_omcasts are added in if_transmit()@if.c.
1398 */
1399 vioif_send_common_locked(ifp, txq, false);
1400
1401 }
1402
1403 static void
1404 vioif_start(struct ifnet *ifp)
1405 {
1406 struct vioif_softc *sc = ifp->if_softc;
1407 struct vioif_txqueue *txq = &sc->sc_txq[0];
1408
1409 #ifdef VIOIF_MPSAFE
1410 KASSERT(if_is_mpsafe(ifp));
1411 #endif
1412
1413 mutex_enter(txq->txq_lock);
1414 vioif_start_locked(ifp, txq);
1415 mutex_exit(txq->txq_lock);
1416 }
1417
1418 static inline int
1419 vioif_select_txqueue(struct ifnet *ifp, struct mbuf *m)
1420 {
1421 struct vioif_softc *sc = ifp->if_softc;
1422 u_int cpuid = cpu_index(curcpu());
1423
1424 return cpuid % sc->sc_act_nvq_pairs;
1425 }
1426
1427 static void
1428 vioif_transmit_locked(struct ifnet *ifp, struct vioif_txqueue *txq)
1429 {
1430
1431 vioif_send_common_locked(ifp, txq, true);
1432 }
1433
1434 static int
1435 vioif_transmit(struct ifnet *ifp, struct mbuf *m)
1436 {
1437 struct vioif_softc *sc = ifp->if_softc;
1438 struct vioif_txqueue *txq;
1439 int qid;
1440
1441 qid = vioif_select_txqueue(ifp, m);
1442 txq = &sc->sc_txq[qid];
1443
1444 if (__predict_false(!pcq_put(txq->txq_intrq, m))) {
1445 m_freem(m);
1446 return ENOBUFS;
1447 }
1448
1449 net_stat_ref_t nsr = IF_STAT_GETREF(ifp);
1450 if_statadd_ref(nsr, if_obytes, m->m_pkthdr.len);
1451 if (m->m_flags & M_MCAST)
1452 if_statinc_ref(nsr, if_omcasts);
1453 IF_STAT_PUTREF(ifp);
1454
1455 if (mutex_tryenter(txq->txq_lock)) {
1456 vioif_transmit_locked(ifp, txq);
1457 mutex_exit(txq->txq_lock);
1458 }
1459
1460 return 0;
1461 }
1462
1463 static void
1464 vioif_deferred_transmit(void *arg)
1465 {
1466 struct vioif_txqueue *txq = arg;
1467 struct virtio_softc *vsc = txq->txq_vq->vq_owner;
1468 struct vioif_softc *sc = device_private(virtio_child(vsc));
1469 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1470
1471 mutex_enter(txq->txq_lock);
1472 vioif_send_common_locked(ifp, txq, true);
1473 mutex_exit(txq->txq_lock);
1474 }
1475
1476 static int
1477 vioif_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1478 {
1479 int s, r;
1480
1481 s = splnet();
1482
1483 r = ether_ioctl(ifp, cmd, data);
1484 if (r == ENETRESET && (cmd == SIOCADDMULTI || cmd == SIOCDELMULTI)) {
1485 if (ifp->if_flags & IFF_RUNNING) {
1486 r = vioif_rx_filter(ifp->if_softc);
1487 } else {
1488 r = 0;
1489 }
1490 }
1491
1492 splx(s);
1493
1494 return r;
1495 }
1496
1497 void
1498 vioif_watchdog(struct ifnet *ifp)
1499 {
1500 struct vioif_softc *sc = ifp->if_softc;
1501 struct vioif_txqueue *txq;
1502 int i;
1503
1504 if (ifp->if_flags & IFF_RUNNING) {
1505 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
1506 txq = &sc->sc_txq[i];
1507
1508 mutex_enter(txq->txq_lock);
1509 if (!txq->txq_running_handle) {
1510 txq->txq_running_handle = true;
1511 vioif_tx_sched_handle(sc, txq);
1512 }
1513 mutex_exit(txq->txq_lock);
1514 }
1515 }
1516 }
1517
1518 /*
1519 * Receive implementation
1520 */
1521 /* add mbufs for all the empty receive slots */
1522 static void
1523 vioif_populate_rx_mbufs_locked(struct vioif_softc *sc, struct vioif_rxqueue *rxq)
1524 {
1525 struct virtqueue *vq = rxq->rxq_vq;
1526 struct virtio_softc *vsc = vq->vq_owner;
1527 struct vioif_net_map *map;
1528 struct mbuf *m;
1529 int i, r, ndone = 0;
1530
1531 KASSERT(mutex_owned(rxq->rxq_lock));
1532
1533 for (i = 0; i < vq->vq_num; i++) {
1534 int slot;
1535 r = virtio_enqueue_prep(vsc, vq, &slot);
1536 if (r == EAGAIN)
1537 break;
1538 if (r != 0)
1539 panic("enqueue_prep for rx buffers");
1540
1541 map = &rxq->rxq_maps[slot];
1542 KASSERT(map->vnm_mbuf == NULL);
1543
1544 MGETHDR(m, M_DONTWAIT, MT_DATA);
1545 if (m == NULL) {
1546 virtio_enqueue_abort(vsc, vq, slot);
1547 rxq->rxq_mbuf_enobufs.ev_count++;
1548 break;
1549 }
1550 MCLGET(m, M_DONTWAIT);
1551 if ((m->m_flags & M_EXT) == 0) {
1552 virtio_enqueue_abort(vsc, vq, slot);
1553 m_freem(m);
1554 rxq->rxq_mbuf_enobufs.ev_count++;
1555 break;
1556 }
1557
1558 m->m_len = m->m_pkthdr.len = MCLBYTES;
1559 m_adj(m, ETHER_ALIGN);
1560
1561 r = bus_dmamap_load_mbuf(virtio_dmat(vsc),
1562 map->vnm_mbuf_map, m, BUS_DMA_READ | BUS_DMA_NOWAIT);
1563
1564 if (r != 0) {
1565 virtio_enqueue_abort(vsc, vq, slot);
1566 m_freem(m);
1567 rxq->rxq_mbuf_load_failed.ev_count++;
1568 break;
1569 }
1570
1571 r = virtio_enqueue_reserve(vsc, vq, slot,
1572 map->vnm_mbuf_map->dm_nsegs + 1);
1573 if (r != 0) {
1574 rxq->rxq_enqueue_reserve_failed.ev_count++;
1575 bus_dmamap_unload(virtio_dmat(vsc), map->vnm_mbuf_map);
1576 m_freem(m);
1577 /* slot already freed by virtio_enqueue_reserve */
1578 break;
1579 }
1580
1581 map->vnm_mbuf = m;
1582 bus_dmamap_sync(virtio_dmat(vsc), map->vnm_hdr_map,
1583 0, sc->sc_hdr_size, BUS_DMASYNC_PREREAD);
1584 bus_dmamap_sync(virtio_dmat(vsc), map->vnm_mbuf_map,
1585 0, map->vnm_mbuf_map->dm_mapsize, BUS_DMASYNC_PREREAD);
1586 virtio_enqueue(vsc, vq, slot, map->vnm_hdr_map, false);
1587 virtio_enqueue(vsc, vq, slot, map->vnm_mbuf_map, false);
1588 virtio_enqueue_commit(vsc, vq, slot, false);
1589 ndone++;
1590 }
1591 if (ndone > 0)
1592 virtio_enqueue_commit(vsc, vq, -1, true);
1593 }
1594
1595 static void
1596 vioif_rx_queue_clear(struct vioif_softc *sc, struct virtio_softc *vsc,
1597 struct vioif_rxqueue *rxq)
1598 {
1599 struct vioif_net_map *map;
1600 unsigned int i, vq_num;
1601 bool more;
1602
1603 mutex_enter(rxq->rxq_lock);
1604 vq_num = rxq->rxq_vq->vq_num;
1605
1606 for (;;) {
1607 more = vioif_rx_deq_locked(sc, vsc, rxq, vq_num, NULL);
1608 if (more == false)
1609 break;
1610 }
1611
1612 for (i = 0; i < vq_num; i++) {
1613 map = &rxq->rxq_maps[i];
1614
1615 if (map->vnm_mbuf == NULL)
1616 continue;
1617
1618 bus_dmamap_unload(virtio_dmat(vsc), map->vnm_mbuf_map);
1619 m_freem(map->vnm_mbuf);
1620 map->vnm_mbuf = NULL;
1621 }
1622 mutex_exit(rxq->rxq_lock);
1623 }
1624
1625 /* dequeue received packets */
1626 static bool
1627 vioif_rx_deq_locked(struct vioif_softc *sc, struct virtio_softc *vsc,
1628 struct vioif_rxqueue *rxq, u_int limit, size_t *ndeqp)
1629 {
1630 struct virtqueue *vq = rxq->rxq_vq;
1631 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1632 struct vioif_net_map *map;
1633 struct mbuf *m;
1634 int slot, len;
1635 bool more;
1636 size_t ndeq;
1637
1638 KASSERT(mutex_owned(rxq->rxq_lock));
1639
1640 more = false;
1641 ndeq = 0;
1642
1643 if (virtio_vq_is_enqueued(vsc, vq) == false)
1644 goto done;
1645
1646 for (;;ndeq++) {
1647 if (ndeq >= limit) {
1648 more = true;
1649 break;
1650 }
1651
1652 if (virtio_dequeue(vsc, vq, &slot, &len) != 0)
1653 break;
1654
1655 map = &rxq->rxq_maps[slot];
1656 KASSERT(map->vnm_mbuf != NULL);
1657
1658 bus_dmamap_sync(virtio_dmat(vsc), map->vnm_hdr_map,
1659 0, sc->sc_hdr_size, BUS_DMASYNC_POSTREAD);
1660 bus_dmamap_sync(virtio_dmat(vsc), map->vnm_mbuf_map,
1661 0, map->vnm_mbuf_map->dm_mapsize, BUS_DMASYNC_POSTREAD);
1662
1663 bus_dmamap_unload(virtio_dmat(vsc), map->vnm_mbuf_map);
1664 m = map->vnm_mbuf;
1665 map->vnm_mbuf = NULL;
1666 virtio_dequeue_commit(vsc, vq, slot);
1667
1668 m->m_len = m->m_pkthdr.len = len - sc->sc_hdr_size;
1669 m_set_rcvif(m, ifp);
1670 if_percpuq_enqueue(ifp->if_percpuq, m);
1671 }
1672
1673 done:
1674 if (ndeqp != NULL)
1675 *ndeqp = ndeq;
1676
1677 return more;
1678 }
1679
1680 /* rx interrupt; call _dequeue above and schedule a softint */
1681
1682 static void
1683 vioif_rx_handle_locked(void *xrxq, u_int limit)
1684 {
1685 struct vioif_rxqueue *rxq = xrxq;
1686 struct virtqueue *vq = rxq->rxq_vq;
1687 struct virtio_softc *vsc = vq->vq_owner;
1688 struct vioif_softc *sc = device_private(virtio_child(vsc));
1689 bool more;
1690 int enqueued;
1691 size_t ndeq;
1692
1693 KASSERT(mutex_owned(rxq->rxq_lock));
1694 KASSERT(!rxq->rxq_stopping);
1695
1696 more = vioif_rx_deq_locked(sc, vsc, rxq, limit, &ndeq);
1697 if (ndeq > 0)
1698 vioif_populate_rx_mbufs_locked(sc, rxq);
1699
1700 if (more) {
1701 vioif_rx_sched_handle(sc, rxq);
1702 return;
1703 }
1704
1705 enqueued = virtio_start_vq_intr(vsc, rxq->rxq_vq);
1706 if (enqueued != 0) {
1707 virtio_stop_vq_intr(vsc, rxq->rxq_vq);
1708 vioif_rx_sched_handle(sc, rxq);
1709 return;
1710 }
1711
1712 rxq->rxq_running_handle = false;
1713 }
1714
1715 static int
1716 vioif_rx_intr(void *arg)
1717 {
1718 struct vioif_rxqueue *rxq = arg;
1719 struct virtqueue *vq = rxq->rxq_vq;
1720 struct virtio_softc *vsc = vq->vq_owner;
1721 struct vioif_softc *sc = device_private(virtio_child(vsc));
1722 u_int limit;
1723
1724
1725 mutex_enter(rxq->rxq_lock);
1726
1727 /* rx handler is already running in softint/workqueue */
1728 if (rxq->rxq_running_handle)
1729 goto done;
1730
1731 if (rxq->rxq_stopping)
1732 goto done;
1733
1734 rxq->rxq_running_handle = true;
1735
1736 limit = sc->sc_rx_intr_process_limit;
1737 virtio_stop_vq_intr(vsc, vq);
1738 vioif_rx_handle_locked(rxq, limit);
1739
1740 done:
1741 mutex_exit(rxq->rxq_lock);
1742 return 1;
1743 }
1744
1745 static void
1746 vioif_rx_handle(void *xrxq)
1747 {
1748 struct vioif_rxqueue *rxq = xrxq;
1749 struct virtqueue *vq = rxq->rxq_vq;
1750 struct virtio_softc *vsc = vq->vq_owner;
1751 struct vioif_softc *sc = device_private(virtio_child(vsc));
1752 u_int limit;
1753
1754 mutex_enter(rxq->rxq_lock);
1755
1756 KASSERT(rxq->rxq_running_handle);
1757
1758 if (rxq->rxq_stopping) {
1759 rxq->rxq_running_handle = false;
1760 goto done;
1761 }
1762
1763 limit = sc->sc_rx_process_limit;
1764 vioif_rx_handle_locked(rxq, limit);
1765
1766 done:
1767 mutex_exit(rxq->rxq_lock);
1768 }
1769
1770 static void
1771 vioif_rx_sched_handle(struct vioif_softc *sc, struct vioif_rxqueue *rxq)
1772 {
1773
1774 KASSERT(mutex_owned(rxq->rxq_lock));
1775
1776 if (rxq->rxq_stopping)
1777 return;
1778
1779 if (rxq->rxq_workqueue)
1780 vioif_work_add(sc->sc_txrx_workqueue, &rxq->rxq_work);
1781 else
1782 softint_schedule(rxq->rxq_handle_si);
1783 }
1784
1785 /*
1786 * Transmition implementation
1787 */
1788 /* actual transmission is done in if_start */
1789 /* tx interrupt; dequeue and free mbufs */
1790 /*
1791 * tx interrupt is actually disabled; this should be called upon
1792 * tx vq full and watchdog
1793 */
1794
1795 static void
1796 vioif_tx_handle_locked(struct vioif_txqueue *txq, u_int limit)
1797 {
1798 struct virtqueue *vq = txq->txq_vq;
1799 struct virtio_softc *vsc = vq->vq_owner;
1800 struct vioif_softc *sc = device_private(virtio_child(vsc));
1801 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1802 bool more;
1803 int enqueued;
1804
1805 KASSERT(mutex_owned(txq->txq_lock));
1806 KASSERT(!txq->txq_stopping);
1807
1808 more = vioif_tx_deq_locked(sc, vsc, txq, limit);
1809 if (more) {
1810 vioif_tx_sched_handle(sc, txq);
1811 return;
1812 }
1813
1814 enqueued = (virtio_features(vsc) & VIRTIO_F_RING_EVENT_IDX) ?
1815 virtio_postpone_intr_smart(vsc, vq):
1816 virtio_start_vq_intr(vsc, vq);
1817 if (enqueued != 0) {
1818 virtio_stop_vq_intr(vsc, vq);
1819 vioif_tx_sched_handle(sc, txq);
1820 return;
1821 }
1822
1823 txq->txq_running_handle = false;
1824
1825 /* for ALTQ */
1826 if (txq == &sc->sc_txq[0]) {
1827 if_schedule_deferred_start(ifp);
1828 ifp->if_flags &= ~IFF_OACTIVE;
1829 }
1830 softint_schedule(txq->txq_deferred_transmit);
1831 }
1832
1833
1834 static int
1835 vioif_tx_intr(void *arg)
1836 {
1837 struct vioif_txqueue *txq = arg;
1838 struct virtqueue *vq = txq->txq_vq;
1839 struct virtio_softc *vsc = vq->vq_owner;
1840 struct vioif_softc *sc = device_private(virtio_child(vsc));
1841 u_int limit;
1842
1843 limit = sc->sc_tx_intr_process_limit;
1844
1845 mutex_enter(txq->txq_lock);
1846
1847 /* tx handler is already running in softint/workqueue */
1848 if (txq->txq_running_handle)
1849 goto done;
1850
1851 if (txq->txq_stopping)
1852 goto done;
1853
1854 txq->txq_running_handle = true;
1855
1856 virtio_stop_vq_intr(vsc, vq);
1857 txq->txq_workqueue = sc->sc_txrx_workqueue_sysctl;
1858 vioif_tx_handle_locked(txq, limit);
1859
1860 done:
1861 mutex_exit(txq->txq_lock);
1862 return 1;
1863 }
1864
1865 static void
1866 vioif_tx_handle(void *xtxq)
1867 {
1868 struct vioif_txqueue *txq = xtxq;
1869 struct virtqueue *vq = txq->txq_vq;
1870 struct virtio_softc *vsc = vq->vq_owner;
1871 struct vioif_softc *sc = device_private(virtio_child(vsc));
1872 u_int limit;
1873
1874 mutex_enter(txq->txq_lock);
1875
1876 KASSERT(txq->txq_running_handle);
1877
1878 if (txq->txq_stopping) {
1879 txq->txq_running_handle = false;
1880 goto done;
1881 }
1882
1883 limit = sc->sc_tx_process_limit;
1884 vioif_tx_handle_locked(txq, limit);
1885
1886 done:
1887 mutex_exit(txq->txq_lock);
1888 }
1889
1890 static void
1891 vioif_tx_sched_handle(struct vioif_softc *sc, struct vioif_txqueue *txq)
1892 {
1893
1894 KASSERT(mutex_owned(txq->txq_lock));
1895
1896 if (txq->txq_stopping)
1897 return;
1898
1899 if (txq->txq_workqueue)
1900 vioif_work_add(sc->sc_txrx_workqueue, &txq->txq_work);
1901 else
1902 softint_schedule(txq->txq_handle_si);
1903 }
1904
1905 static void
1906 vioif_tx_queue_clear(struct vioif_softc *sc, struct virtio_softc *vsc,
1907 struct vioif_txqueue *txq)
1908 {
1909 struct vioif_net_map *map;
1910 unsigned int i, vq_num;
1911 bool more;
1912
1913 mutex_enter(txq->txq_lock);
1914
1915 vq_num = txq->txq_vq->vq_num;
1916 for (;;) {
1917 more = vioif_tx_deq_locked(sc, vsc, txq, vq_num);
1918 if (more == false)
1919 break;
1920 }
1921
1922 for (i = 0; i < vq_num; i++) {
1923 map = &txq->txq_maps[i];
1924 if (map->vnm_mbuf == NULL)
1925 continue;
1926
1927 bus_dmamap_unload(virtio_dmat(vsc), map->vnm_mbuf_map);
1928 m_freem(map->vnm_mbuf);
1929 map->vnm_mbuf = NULL;
1930 }
1931 mutex_exit(txq->txq_lock);
1932 }
1933
1934 static bool
1935 vioif_tx_deq_locked(struct vioif_softc *sc, struct virtio_softc *vsc,
1936 struct vioif_txqueue *txq, u_int limit)
1937 {
1938 struct virtqueue *vq = txq->txq_vq;
1939 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
1940 struct vioif_net_map *map;
1941 struct mbuf *m;
1942 int slot, len;
1943 bool more = false;
1944
1945 KASSERT(mutex_owned(txq->txq_lock));
1946
1947 if (virtio_vq_is_enqueued(vsc, vq) == false)
1948 return false;
1949
1950 for (;;) {
1951 if (limit-- == 0) {
1952 more = true;
1953 break;
1954 }
1955
1956 if (virtio_dequeue(vsc, vq, &slot, &len) != 0)
1957 break;
1958
1959 map = &txq->txq_maps[slot];
1960 KASSERT(map->vnm_mbuf != NULL);
1961
1962 bus_dmamap_sync(virtio_dmat(vsc), map->vnm_hdr_map,
1963 0, sc->sc_hdr_size, BUS_DMASYNC_POSTWRITE);
1964 bus_dmamap_sync(virtio_dmat(vsc), map->vnm_mbuf_map,
1965 0, map->vnm_mbuf_map->dm_mapsize, BUS_DMASYNC_POSTWRITE);
1966
1967 bus_dmamap_unload(virtio_dmat(vsc), map->vnm_mbuf_map);
1968 m = map->vnm_mbuf;
1969 map->vnm_mbuf = NULL;
1970 virtio_dequeue_commit(vsc, vq, slot);
1971
1972 if_statinc(ifp, if_opackets);
1973 m_freem(m);
1974 }
1975
1976 return more;
1977 }
1978
1979 /*
1980 * Control vq
1981 */
1982 /* issue a VIRTIO_NET_CTRL_RX class command and wait for completion */
1983 static void
1984 vioif_ctrl_acquire(struct vioif_softc *sc)
1985 {
1986 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
1987
1988 mutex_enter(&ctrlq->ctrlq_wait_lock);
1989 while (ctrlq->ctrlq_inuse != FREE)
1990 cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
1991 ctrlq->ctrlq_inuse = INUSE;
1992 ctrlq->ctrlq_owner = curlwp;
1993 mutex_exit(&ctrlq->ctrlq_wait_lock);
1994 }
1995
1996 static void
1997 vioif_ctrl_release(struct vioif_softc *sc)
1998 {
1999 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
2000
2001 KASSERT(ctrlq->ctrlq_inuse != FREE);
2002 KASSERT(ctrlq->ctrlq_owner == curlwp);
2003
2004 mutex_enter(&ctrlq->ctrlq_wait_lock);
2005 ctrlq->ctrlq_inuse = FREE;
2006 ctrlq->ctrlq_owner = NULL;
2007 cv_signal(&ctrlq->ctrlq_wait);
2008 mutex_exit(&ctrlq->ctrlq_wait_lock);
2009 }
2010
2011 static int
2012 vioif_ctrl_load_cmdspec(struct vioif_softc *sc,
2013 struct vioif_ctrl_cmdspec *specs, int nspecs)
2014 {
2015 struct virtio_softc *vsc = sc->sc_virtio;
2016 int i, r, loaded;
2017
2018 loaded = 0;
2019 for (i = 0; i < nspecs; i++) {
2020 r = bus_dmamap_load(virtio_dmat(vsc),
2021 specs[i].dmamap, specs[i].buf, specs[i].bufsize,
2022 NULL, BUS_DMA_WRITE | BUS_DMA_NOWAIT);
2023 if (r) {
2024 sc->sc_ctrlq.ctrlq_cmd_load_failed.ev_count++;
2025 goto err;
2026 }
2027 loaded++;
2028
2029 }
2030
2031 return r;
2032
2033 err:
2034 for (i = 0; i < loaded; i++) {
2035 bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
2036 }
2037
2038 return r;
2039 }
2040
2041 static void
2042 vioif_ctrl_unload_cmdspec(struct vioif_softc *sc,
2043 struct vioif_ctrl_cmdspec *specs, int nspecs)
2044 {
2045 struct virtio_softc *vsc = sc->sc_virtio;
2046 int i;
2047
2048 for (i = 0; i < nspecs; i++) {
2049 bus_dmamap_unload(virtio_dmat(vsc), specs[i].dmamap);
2050 }
2051 }
2052
2053 static int
2054 vioif_ctrl_send_command(struct vioif_softc *sc, uint8_t class, uint8_t cmd,
2055 struct vioif_ctrl_cmdspec *specs, int nspecs)
2056 {
2057 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
2058 struct virtqueue *vq = ctrlq->ctrlq_vq;
2059 struct virtio_softc *vsc = sc->sc_virtio;
2060 int i, r, slot;
2061
2062 ctrlq->ctrlq_cmd->class = class;
2063 ctrlq->ctrlq_cmd->command = cmd;
2064
2065 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap,
2066 0, sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_PREWRITE);
2067 for (i = 0; i < nspecs; i++) {
2068 bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap,
2069 0, specs[i].bufsize, BUS_DMASYNC_PREWRITE);
2070 }
2071 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap,
2072 0, sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_PREREAD);
2073
2074 /* we need to explicitly (re)start vq intr when using RING EVENT IDX */
2075 if (virtio_features(vsc) & VIRTIO_F_RING_EVENT_IDX)
2076 virtio_start_vq_intr(vsc, ctrlq->ctrlq_vq);
2077
2078 r = virtio_enqueue_prep(vsc, vq, &slot);
2079 if (r != 0)
2080 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
2081 r = virtio_enqueue_reserve(vsc, vq, slot, nspecs + 2);
2082 if (r != 0)
2083 panic("%s: control vq busy!?", device_xname(sc->sc_dev));
2084 virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_cmd_dmamap, true);
2085 for (i = 0; i < nspecs; i++) {
2086 virtio_enqueue(vsc, vq, slot, specs[i].dmamap, true);
2087 }
2088 virtio_enqueue(vsc, vq, slot, ctrlq->ctrlq_status_dmamap, false);
2089 virtio_enqueue_commit(vsc, vq, slot, true);
2090
2091 /* wait for done */
2092 mutex_enter(&ctrlq->ctrlq_wait_lock);
2093 while (ctrlq->ctrlq_inuse != DONE)
2094 cv_wait(&ctrlq->ctrlq_wait, &ctrlq->ctrlq_wait_lock);
2095 mutex_exit(&ctrlq->ctrlq_wait_lock);
2096 /* already dequeueued */
2097
2098 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_cmd_dmamap, 0,
2099 sizeof(struct virtio_net_ctrl_cmd), BUS_DMASYNC_POSTWRITE);
2100 for (i = 0; i < nspecs; i++) {
2101 bus_dmamap_sync(virtio_dmat(vsc), specs[i].dmamap, 0,
2102 specs[i].bufsize, BUS_DMASYNC_POSTWRITE);
2103 }
2104 bus_dmamap_sync(virtio_dmat(vsc), ctrlq->ctrlq_status_dmamap, 0,
2105 sizeof(struct virtio_net_ctrl_status), BUS_DMASYNC_POSTREAD);
2106
2107 if (ctrlq->ctrlq_status->ack == VIRTIO_NET_OK)
2108 r = 0;
2109 else {
2110 device_printf(sc->sc_dev, "failed setting rx mode\n");
2111 sc->sc_ctrlq.ctrlq_cmd_failed.ev_count++;
2112 r = EIO;
2113 }
2114
2115 return r;
2116 }
2117
2118 static int
2119 vioif_ctrl_rx(struct vioif_softc *sc, int cmd, bool onoff)
2120 {
2121 struct virtio_net_ctrl_rx *rx = sc->sc_ctrlq.ctrlq_rx;
2122 struct vioif_ctrl_cmdspec specs[1];
2123 int r;
2124
2125 if (!sc->sc_has_ctrl)
2126 return ENOTSUP;
2127
2128 vioif_ctrl_acquire(sc);
2129
2130 rx->onoff = onoff;
2131 specs[0].dmamap = sc->sc_ctrlq.ctrlq_rx_dmamap;
2132 specs[0].buf = rx;
2133 specs[0].bufsize = sizeof(*rx);
2134
2135 r = vioif_ctrl_send_command(sc, VIRTIO_NET_CTRL_RX, cmd,
2136 specs, __arraycount(specs));
2137
2138 vioif_ctrl_release(sc);
2139 return r;
2140 }
2141
2142 static int
2143 vioif_set_promisc(struct vioif_softc *sc, bool onoff)
2144 {
2145 return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_PROMISC, onoff);
2146 }
2147
2148 static int
2149 vioif_set_allmulti(struct vioif_softc *sc, bool onoff)
2150 {
2151 return vioif_ctrl_rx(sc, VIRTIO_NET_CTRL_RX_ALLMULTI, onoff);
2152 }
2153
2154 /* issue VIRTIO_NET_CTRL_MAC_TABLE_SET command and wait for completion */
2155 static int
2156 vioif_set_rx_filter(struct vioif_softc *sc)
2157 {
2158 /* filter already set in ctrlq->ctrlq_mac_tbl */
2159 struct virtio_softc *vsc = sc->sc_virtio;
2160 struct virtio_net_ctrl_mac_tbl *mac_tbl_uc, *mac_tbl_mc;
2161 struct vioif_ctrl_cmdspec specs[2];
2162 int nspecs = __arraycount(specs);
2163 int r;
2164
2165 mac_tbl_uc = sc->sc_ctrlq.ctrlq_mac_tbl_uc;
2166 mac_tbl_mc = sc->sc_ctrlq.ctrlq_mac_tbl_mc;
2167
2168 if (!sc->sc_has_ctrl)
2169 return ENOTSUP;
2170
2171 vioif_ctrl_acquire(sc);
2172
2173 specs[0].dmamap = sc->sc_ctrlq.ctrlq_tbl_uc_dmamap;
2174 specs[0].buf = mac_tbl_uc;
2175 specs[0].bufsize = sizeof(*mac_tbl_uc)
2176 + (ETHER_ADDR_LEN * virtio_rw32(vsc, mac_tbl_uc->nentries));
2177
2178 specs[1].dmamap = sc->sc_ctrlq.ctrlq_tbl_mc_dmamap;
2179 specs[1].buf = mac_tbl_mc;
2180 specs[1].bufsize = sizeof(*mac_tbl_mc)
2181 + (ETHER_ADDR_LEN * virtio_rw32(vsc, mac_tbl_mc->nentries));
2182
2183 r = vioif_ctrl_load_cmdspec(sc, specs, nspecs);
2184 if (r != 0)
2185 goto out;
2186
2187 r = vioif_ctrl_send_command(sc,
2188 VIRTIO_NET_CTRL_MAC, VIRTIO_NET_CTRL_MAC_TABLE_SET,
2189 specs, nspecs);
2190
2191 vioif_ctrl_unload_cmdspec(sc, specs, nspecs);
2192
2193 out:
2194 vioif_ctrl_release(sc);
2195
2196 return r;
2197 }
2198
2199 static int
2200 vioif_set_mac_addr(struct vioif_softc *sc)
2201 {
2202 struct virtio_net_ctrl_mac_addr *ma =
2203 sc->sc_ctrlq.ctrlq_mac_addr;
2204 struct vioif_ctrl_cmdspec specs[1];
2205 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2206 int nspecs = __arraycount(specs);
2207 uint64_t features;
2208 int r;
2209 size_t i;
2210
2211 if (!sc->sc_has_ctrl)
2212 return ENOTSUP;
2213
2214 if (memcmp(CLLADDR(ifp->if_sadl), sc->sc_mac,
2215 ETHER_ADDR_LEN) == 0) {
2216 return 0;
2217 }
2218
2219 memcpy(sc->sc_mac, CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
2220
2221 features = virtio_features(sc->sc_virtio);
2222 if (features & VIRTIO_NET_F_CTRL_MAC_ADDR) {
2223 vioif_ctrl_acquire(sc);
2224
2225 memcpy(ma->mac, sc->sc_mac, ETHER_ADDR_LEN);
2226 specs[0].dmamap = sc->sc_ctrlq.ctrlq_mac_addr_dmamap;
2227 specs[0].buf = ma;
2228 specs[0].bufsize = sizeof(*ma);
2229
2230 r = vioif_ctrl_send_command(sc,
2231 VIRTIO_NET_CTRL_MAC, VIRTIO_NET_CTRL_MAC_ADDR_SET,
2232 specs, nspecs);
2233
2234 vioif_ctrl_release(sc);
2235 } else {
2236 for (i = 0; i < __arraycount(sc->sc_mac); i++) {
2237 virtio_write_device_config_1(sc->sc_virtio,
2238 VIRTIO_NET_CONFIG_MAC + i, sc->sc_mac[i]);
2239 }
2240 r = 0;
2241 }
2242
2243 return r;
2244 }
2245
2246 static int
2247 vioif_ctrl_mq_vq_pairs_set(struct vioif_softc *sc, int nvq_pairs)
2248 {
2249 struct virtio_net_ctrl_mq *mq = sc->sc_ctrlq.ctrlq_mq;
2250 struct vioif_ctrl_cmdspec specs[1];
2251 int r;
2252
2253 if (!sc->sc_has_ctrl)
2254 return ENOTSUP;
2255
2256 if (nvq_pairs <= 1)
2257 return EINVAL;
2258
2259 vioif_ctrl_acquire(sc);
2260
2261 mq->virtqueue_pairs = virtio_rw16(sc->sc_virtio, nvq_pairs);
2262 specs[0].dmamap = sc->sc_ctrlq.ctrlq_mq_dmamap;
2263 specs[0].buf = mq;
2264 specs[0].bufsize = sizeof(*mq);
2265
2266 r = vioif_ctrl_send_command(sc,
2267 VIRTIO_NET_CTRL_MQ, VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET,
2268 specs, __arraycount(specs));
2269
2270 vioif_ctrl_release(sc);
2271
2272 return r;
2273 }
2274
2275 /* ctrl vq interrupt; wake up the command issuer */
2276 static int
2277 vioif_ctrl_intr(void *arg)
2278 {
2279 struct vioif_ctrlqueue *ctrlq = arg;
2280 struct virtqueue *vq = ctrlq->ctrlq_vq;
2281 struct virtio_softc *vsc = vq->vq_owner;
2282 int r, slot;
2283
2284 if (virtio_vq_is_enqueued(vsc, vq) == false)
2285 return 0;
2286
2287 r = virtio_dequeue(vsc, vq, &slot, NULL);
2288 if (r == ENOENT)
2289 return 0;
2290 virtio_dequeue_commit(vsc, vq, slot);
2291
2292 mutex_enter(&ctrlq->ctrlq_wait_lock);
2293 ctrlq->ctrlq_inuse = DONE;
2294 cv_signal(&ctrlq->ctrlq_wait);
2295 mutex_exit(&ctrlq->ctrlq_wait_lock);
2296
2297 return 1;
2298 }
2299
2300 static int
2301 vioif_ifflags(struct vioif_softc *sc)
2302 {
2303 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2304 bool onoff;
2305 int r;
2306
2307 if (!sc->sc_has_ctrl) {
2308 /* no ctrl vq; always promisc and allmulti */
2309 ifp->if_flags |= (IFF_PROMISC | IFF_ALLMULTI);
2310 return 0;
2311 }
2312
2313 onoff = ifp->if_flags & IFF_ALLMULTI ? true : false;
2314 r = vioif_set_allmulti(sc, onoff);
2315 if (r != 0) {
2316 log(LOG_WARNING,
2317 "%s: couldn't %sable ALLMULTI\n",
2318 ifp->if_xname, onoff ? "en" : "dis");
2319 if (onoff == false) {
2320 ifp->if_flags |= IFF_ALLMULTI;
2321 }
2322 }
2323
2324 onoff = ifp->if_flags & IFF_PROMISC ? true : false;
2325 r = vioif_set_promisc(sc, onoff);
2326 if (r != 0) {
2327 log(LOG_WARNING,
2328 "%s: couldn't %sable PROMISC\n",
2329 ifp->if_xname, onoff ? "en" : "dis");
2330 if (onoff == false) {
2331 ifp->if_flags |= IFF_PROMISC;
2332 }
2333 }
2334
2335 return 0;
2336 }
2337
2338 static int
2339 vioif_ifflags_cb(struct ethercom *ec)
2340 {
2341 struct ifnet *ifp = &ec->ec_if;
2342 struct vioif_softc *sc = ifp->if_softc;
2343
2344 return vioif_ifflags(sc);
2345 }
2346
2347 /*
2348 * If multicast filter small enough (<=MAXENTRIES) set rx filter
2349 * If large multicast filter exist use ALLMULTI
2350 * If setting rx filter fails fall back to ALLMULTI
2351 */
2352 static int
2353 vioif_rx_filter(struct vioif_softc *sc)
2354 {
2355 struct virtio_softc *vsc = sc->sc_virtio;
2356 struct ethercom *ec = &sc->sc_ethercom;
2357 struct ifnet *ifp = &ec->ec_if;
2358 struct ether_multi *enm;
2359 struct ether_multistep step;
2360 struct vioif_ctrlqueue *ctrlq = &sc->sc_ctrlq;
2361 int nentries;
2362 bool allmulti = 0;
2363 int r;
2364
2365 if (!sc->sc_has_ctrl) {
2366 goto set_ifflags;
2367 }
2368
2369 memcpy(ctrlq->ctrlq_mac_tbl_uc->macs[0],
2370 CLLADDR(ifp->if_sadl), ETHER_ADDR_LEN);
2371
2372 nentries = 0;
2373 allmulti = false;
2374
2375 ETHER_LOCK(ec);
2376 for (ETHER_FIRST_MULTI(step, ec, enm); enm != NULL;
2377 ETHER_NEXT_MULTI(step, enm)) {
2378 if (nentries >= VIRTIO_NET_CTRL_MAC_MAXENTRIES) {
2379 allmulti = true;
2380 break;
2381 }
2382 if (memcmp(enm->enm_addrlo, enm->enm_addrhi, ETHER_ADDR_LEN)) {
2383 allmulti = true;
2384 break;
2385 }
2386
2387 memcpy(ctrlq->ctrlq_mac_tbl_mc->macs[nentries],
2388 enm->enm_addrlo, ETHER_ADDR_LEN);
2389 nentries++;
2390 }
2391 ETHER_UNLOCK(ec);
2392
2393 r = vioif_set_mac_addr(sc);
2394 if (r != 0) {
2395 log(LOG_WARNING, "%s: couldn't set MAC address\n",
2396 ifp->if_xname);
2397 }
2398
2399 if (!allmulti) {
2400 ctrlq->ctrlq_mac_tbl_uc->nentries = virtio_rw32(vsc, 1);
2401 ctrlq->ctrlq_mac_tbl_mc->nentries = virtio_rw32(vsc, nentries);
2402 r = vioif_set_rx_filter(sc);
2403 if (r != 0) {
2404 allmulti = true; /* fallback */
2405 }
2406 }
2407
2408 if (allmulti) {
2409 ctrlq->ctrlq_mac_tbl_uc->nentries = virtio_rw32(vsc, 0);
2410 ctrlq->ctrlq_mac_tbl_mc->nentries = virtio_rw32(vsc, 0);
2411 r = vioif_set_rx_filter(sc);
2412 if (r != 0) {
2413 log(LOG_DEBUG, "%s: couldn't clear RX filter\n",
2414 ifp->if_xname);
2415 /* what to do on failure? */
2416 }
2417
2418 ifp->if_flags |= IFF_ALLMULTI;
2419 }
2420
2421 set_ifflags:
2422 r = vioif_ifflags(sc);
2423
2424 return r;
2425 }
2426
2427 static int
2428 vioif_get_link_status(struct vioif_softc *sc)
2429 {
2430 struct virtio_softc *vsc = sc->sc_virtio;
2431 uint16_t status;
2432
2433 if (virtio_features(vsc) & VIRTIO_NET_F_STATUS)
2434 status = virtio_read_device_config_2(vsc,
2435 VIRTIO_NET_CONFIG_STATUS);
2436 else
2437 status = VIRTIO_NET_S_LINK_UP;
2438
2439 if ((status & VIRTIO_NET_S_LINK_UP) != 0)
2440 return LINK_STATE_UP;
2441
2442 return LINK_STATE_DOWN;
2443 }
2444
2445 /* change link status */
2446 static void
2447 vioif_update_link_status(struct vioif_softc *sc)
2448 {
2449 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2450 struct vioif_txqueue *txq;
2451 bool active;
2452 int link, i;
2453
2454 mutex_enter(&sc->sc_lock);
2455
2456 link = vioif_get_link_status(sc);
2457
2458 if (link == sc->sc_link_state)
2459 goto done;
2460
2461 sc->sc_link_state = link;
2462
2463 active = VIOIF_IS_LINK_ACTIVE(sc);
2464 for (i = 0; i < sc->sc_act_nvq_pairs; i++) {
2465 txq = &sc->sc_txq[i];
2466
2467 mutex_enter(txq->txq_lock);
2468 txq->txq_link_active = active;
2469 mutex_exit(txq->txq_lock);
2470 }
2471
2472 if_link_state_change(ifp, sc->sc_link_state);
2473
2474 done:
2475 mutex_exit(&sc->sc_lock);
2476 }
2477
2478 static int
2479 vioif_config_change(struct virtio_softc *vsc)
2480 {
2481 struct vioif_softc *sc = device_private(virtio_child(vsc));
2482
2483 softint_schedule(sc->sc_ctl_softint);
2484 return 0;
2485 }
2486
2487 static void
2488 vioif_ctl_softint(void *arg)
2489 {
2490 struct vioif_softc *sc = arg;
2491 struct ifnet *ifp = &sc->sc_ethercom.ec_if;
2492
2493 vioif_update_link_status(sc);
2494 vioif_start(ifp);
2495 }
2496
2497 static struct workqueue *
2498 vioif_workq_create(const char *name, pri_t prio, int ipl, int flags)
2499 {
2500 struct workqueue *wq;
2501 int error;
2502
2503 error = workqueue_create(&wq, name, vioif_workq_work, NULL,
2504 prio, ipl, flags);
2505
2506 if (error)
2507 return NULL;
2508
2509 return wq;
2510 }
2511
2512 static void
2513 vioif_workq_destroy(struct workqueue *wq)
2514 {
2515
2516 workqueue_destroy(wq);
2517 }
2518
2519 static void
2520 vioif_workq_work(struct work *wk, void *context)
2521 {
2522 struct vioif_work *work;
2523
2524 work = container_of(wk, struct vioif_work, cookie);
2525
2526 atomic_store_relaxed(&work->added, 0);
2527 work->func(work->arg);
2528 }
2529
2530 static void
2531 vioif_work_set(struct vioif_work *work, void (*func)(void *), void *arg)
2532 {
2533
2534 memset(work, 0, sizeof(*work));
2535 work->func = func;
2536 work->arg = arg;
2537 }
2538
2539 static void
2540 vioif_work_add(struct workqueue *wq, struct vioif_work *work)
2541 {
2542
2543 if (atomic_load_relaxed(&work->added) != 0)
2544 return;
2545
2546 atomic_store_relaxed(&work->added, 1);
2547 kpreempt_disable();
2548 workqueue_enqueue(wq, &work->cookie, NULL);
2549 kpreempt_enable();
2550 }
2551
2552 static void
2553 vioif_work_wait(struct workqueue *wq, struct vioif_work *work)
2554 {
2555
2556 workqueue_wait(wq, &work->cookie);
2557 }
2558
2559 static int
2560 vioif_setup_sysctl(struct vioif_softc *sc)
2561 {
2562 const char *devname;
2563 struct sysctllog **log;
2564 const struct sysctlnode *rnode, *rxnode, *txnode;
2565 int error;
2566
2567 log = &sc->sc_sysctllog;
2568 devname = device_xname(sc->sc_dev);
2569
2570 error = sysctl_createv(log, 0, NULL, &rnode,
2571 0, CTLTYPE_NODE, devname,
2572 SYSCTL_DESCR("virtio-net information and settings"),
2573 NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
2574 if (error)
2575 goto out;
2576
2577 error = sysctl_createv(log, 0, &rnode, NULL,
2578 CTLFLAG_READWRITE, CTLTYPE_BOOL, "txrx_workqueue",
2579 SYSCTL_DESCR("Use workqueue for packet processing"),
2580 NULL, 0, &sc->sc_txrx_workqueue_sysctl, 0, CTL_CREATE, CTL_EOL);
2581 if (error)
2582 goto out;
2583
2584 error = sysctl_createv(log, 0, &rnode, &rxnode,
2585 0, CTLTYPE_NODE, "rx",
2586 SYSCTL_DESCR("virtio-net information and settings for Rx"),
2587 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
2588 if (error)
2589 goto out;
2590
2591 error = sysctl_createv(log, 0, &rxnode, NULL,
2592 CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
2593 SYSCTL_DESCR("max number of Rx packets to process for interrupt processing"),
2594 NULL, 0, &sc->sc_rx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
2595 if (error)
2596 goto out;
2597
2598 error = sysctl_createv(log, 0, &rxnode, NULL,
2599 CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
2600 SYSCTL_DESCR("max number of Rx packets to process for deferred processing"),
2601 NULL, 0, &sc->sc_rx_process_limit, 0, CTL_CREATE, CTL_EOL);
2602 if (error)
2603 goto out;
2604
2605 error = sysctl_createv(log, 0, &rnode, &txnode,
2606 0, CTLTYPE_NODE, "tx",
2607 SYSCTL_DESCR("virtio-net information and settings for Tx"),
2608 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL);
2609 if (error)
2610 goto out;
2611
2612 error = sysctl_createv(log, 0, &txnode, NULL,
2613 CTLFLAG_READWRITE, CTLTYPE_INT, "intr_process_limit",
2614 SYSCTL_DESCR("max number of Tx packets to process for interrupt processing"),
2615 NULL, 0, &sc->sc_tx_intr_process_limit, 0, CTL_CREATE, CTL_EOL);
2616 if (error)
2617 goto out;
2618
2619 error = sysctl_createv(log, 0, &txnode, NULL,
2620 CTLFLAG_READWRITE, CTLTYPE_INT, "process_limit",
2621 SYSCTL_DESCR("max number of Tx packets to process for deferred processing"),
2622 NULL, 0, &sc->sc_tx_process_limit, 0, CTL_CREATE, CTL_EOL);
2623
2624 out:
2625 if (error)
2626 sysctl_teardown(log);
2627
2628 return error;
2629 }
2630
2631 static void
2632 vioif_setup_stats(struct vioif_softc *sc)
2633 {
2634 struct vioif_rxqueue *rxq;
2635 struct vioif_txqueue *txq;
2636 int i;
2637
2638 for (i = 0; i < sc->sc_max_nvq_pairs; i++) {
2639 rxq = &sc->sc_rxq[i];
2640 txq = &sc->sc_txq[i];
2641
2642 snprintf(txq->txq_evgroup, sizeof(txq->txq_evgroup), "%s-TX%d",
2643 device_xname(sc->sc_dev), i);
2644 evcnt_attach_dynamic(&txq->txq_defrag_failed, EVCNT_TYPE_MISC,
2645 NULL, txq->txq_evgroup, "tx m_defrag() failed");
2646 evcnt_attach_dynamic(&txq->txq_mbuf_load_failed, EVCNT_TYPE_MISC,
2647 NULL, txq->txq_evgroup, "tx dmamap load failed");
2648 evcnt_attach_dynamic(&txq->txq_enqueue_reserve_failed, EVCNT_TYPE_MISC,
2649 NULL, txq->txq_evgroup, "virtio_enqueue_reserve failed");
2650
2651 snprintf(rxq->rxq_evgroup, sizeof(rxq->rxq_evgroup), "%s-RX%d",
2652 device_xname(sc->sc_dev), i);
2653 evcnt_attach_dynamic(&rxq->rxq_mbuf_enobufs, EVCNT_TYPE_MISC,
2654 NULL, rxq->rxq_evgroup, "no receive buffer");
2655 evcnt_attach_dynamic(&rxq->rxq_mbuf_load_failed, EVCNT_TYPE_MISC,
2656 NULL, rxq->rxq_evgroup, "tx dmamap load failed");
2657 evcnt_attach_dynamic(&rxq->rxq_enqueue_reserve_failed, EVCNT_TYPE_MISC,
2658 NULL, rxq->rxq_evgroup, "virtio_enqueue_reserve failed");
2659 }
2660
2661 evcnt_attach_dynamic(&sc->sc_ctrlq.ctrlq_cmd_load_failed, EVCNT_TYPE_MISC,
2662 NULL, device_xname(sc->sc_dev), "control command dmamap load failed");
2663 evcnt_attach_dynamic(&sc->sc_ctrlq.ctrlq_cmd_failed, EVCNT_TYPE_MISC,
2664 NULL, device_xname(sc->sc_dev), "control command failed");
2665 }
2666
2667 static void
2668 vioif_intr_barrier(void)
2669 {
2670
2671 /* wait for finish all interrupt handler */
2672 xc_barrier(0);
2673 }
2674
2675 MODULE(MODULE_CLASS_DRIVER, if_vioif, "virtio");
2676
2677 #ifdef _MODULE
2678 #include "ioconf.c"
2679 #endif
2680
2681 static int
2682 if_vioif_modcmd(modcmd_t cmd, void *opaque)
2683 {
2684 int error = 0;
2685
2686 #ifdef _MODULE
2687 switch (cmd) {
2688 case MODULE_CMD_INIT:
2689 error = config_init_component(cfdriver_ioconf_if_vioif,
2690 cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
2691 break;
2692 case MODULE_CMD_FINI:
2693 error = config_fini_component(cfdriver_ioconf_if_vioif,
2694 cfattach_ioconf_if_vioif, cfdata_ioconf_if_vioif);
2695 break;
2696 default:
2697 error = ENOTTY;
2698 break;
2699 }
2700 #endif
2701
2702 return error;
2703 }
2704