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