xref: /freebsd/sys/dev/vmware/vmxnet3/if_vmx.c (revision fe00ca2f88718c7751b8953cc8ee7fc0dad0bbe1)
1 /*-
2  * Copyright (c) 2013 Tsubai Masanari
3  * Copyright (c) 2013 Bryan Venteicher <bryanv@FreeBSD.org>
4  * Copyright (c) 2018 Patrick Kelsey
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  *
18  * $OpenBSD: src/sys/dev/pci/if_vmx.c,v 1.11 2013/06/22 00:28:10 uebayasi Exp $
19  */
20 
21 /* Driver for VMware vmxnet3 virtual ethernet devices. */
22 
23 #include <sys/cdefs.h>
24 #include "opt_rss.h"
25 
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/endian.h>
30 #include <sys/sockio.h>
31 #include <sys/mbuf.h>
32 #include <sys/malloc.h>
33 #include <sys/module.h>
34 #include <sys/socket.h>
35 #include <sys/sysctl.h>
36 #include <sys/smp.h>
37 #include <vm/vm.h>
38 #include <vm/pmap.h>
39 
40 #include <net/ethernet.h>
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_arp.h>
44 #include <net/if_dl.h>
45 #include <net/if_types.h>
46 #include <net/if_media.h>
47 #include <net/if_vlan_var.h>
48 #include <net/iflib.h>
49 #include <net/rss_config.h>
50 
51 #include <netinet/in_systm.h>
52 #include <netinet/in.h>
53 #include <netinet/ip.h>
54 #include <netinet/ip6.h>
55 #include <netinet6/ip6_var.h>
56 #include <netinet/udp.h>
57 #include <netinet/tcp.h>
58 
59 #include <machine/bus.h>
60 #include <machine/resource.h>
61 #include <sys/bus.h>
62 #include <sys/rman.h>
63 
64 #include <dev/pci/pcireg.h>
65 #include <dev/pci/pcivar.h>
66 
67 #include "ifdi_if.h"
68 
69 #include "if_vmxreg.h"
70 #include "if_vmxvar.h"
71 
72 #include "opt_inet.h"
73 #include "opt_inet6.h"
74 
75 #define VMXNET3_VMWARE_VENDOR_ID	0x15AD
76 #define VMXNET3_VMWARE_DEVICE_ID	0x07B0
77 
78 static const pci_vendor_info_t vmxnet3_vendor_info_array[] =
79 {
80 	PVID(VMXNET3_VMWARE_VENDOR_ID, VMXNET3_VMWARE_DEVICE_ID, "VMware VMXNET3 Ethernet Adapter"),
81 	/* required last entry */
82 	PVID_END
83 };
84 
85 static void	*vmxnet3_register(device_t);
86 static int	vmxnet3_attach_pre(if_ctx_t);
87 static int	vmxnet3_msix_intr_assign(if_ctx_t, int);
88 static void	vmxnet3_free_irqs(struct vmxnet3_softc *);
89 static int	vmxnet3_attach_post(if_ctx_t);
90 static int	vmxnet3_detach(if_ctx_t);
91 static int	vmxnet3_shutdown(if_ctx_t);
92 static int	vmxnet3_suspend(if_ctx_t);
93 static int	vmxnet3_resume(if_ctx_t);
94 
95 static int	vmxnet3_alloc_resources(struct vmxnet3_softc *);
96 static void	vmxnet3_free_resources(struct vmxnet3_softc *);
97 static int	vmxnet3_check_version(struct vmxnet3_softc *);
98 static void	vmxnet3_set_interrupt_idx(struct vmxnet3_softc *);
99 
100 static int	vmxnet3_queues_shared_alloc(struct vmxnet3_softc *);
101 static void	vmxnet3_init_txq(struct vmxnet3_softc *, int);
102 static int	vmxnet3_tx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
103 static void	vmxnet3_init_rxq(struct vmxnet3_softc *, int, int);
104 static int	vmxnet3_rx_queues_alloc(if_ctx_t, caddr_t *, uint64_t *, int, int);
105 static void	vmxnet3_queues_free(if_ctx_t);
106 
107 static int	vmxnet3_alloc_shared_data(struct vmxnet3_softc *);
108 static void	vmxnet3_free_shared_data(struct vmxnet3_softc *);
109 static int	vmxnet3_alloc_mcast_table(struct vmxnet3_softc *);
110 static void	vmxnet3_free_mcast_table(struct vmxnet3_softc *);
111 static void	vmxnet3_init_shared_data(struct vmxnet3_softc *);
112 static void	vmxnet3_reinit_rss_shared_data(struct vmxnet3_softc *);
113 static void	vmxnet3_reinit_shared_data(struct vmxnet3_softc *);
114 static int	vmxnet3_alloc_data(struct vmxnet3_softc *);
115 static void	vmxnet3_free_data(struct vmxnet3_softc *);
116 
117 static void	vmxnet3_evintr(struct vmxnet3_softc *);
118 static int	vmxnet3_isc_txd_encap(void *, if_pkt_info_t);
119 static void	vmxnet3_isc_txd_flush(void *, uint16_t, qidx_t);
120 static int	vmxnet3_isc_txd_credits_update(void *, uint16_t, bool);
121 static int	vmxnet3_isc_rxd_available(void *, uint16_t, qidx_t, qidx_t);
122 static int	vmxnet3_isc_rxd_pkt_get(void *, if_rxd_info_t);
123 static void	vmxnet3_isc_rxd_refill(void *, if_rxd_update_t);
124 static void	vmxnet3_isc_rxd_flush(void *, uint16_t, uint8_t, qidx_t);
125 static int	vmxnet3_legacy_intr(void *);
126 static int	vmxnet3_rxq_intr(void *);
127 static int	vmxnet3_event_intr(void *);
128 
129 static void	vmxnet3_stop(if_ctx_t);
130 
131 static void	vmxnet3_txinit(struct vmxnet3_softc *, struct vmxnet3_txqueue *);
132 static void	vmxnet3_rxinit(struct vmxnet3_softc *, struct vmxnet3_rxqueue *);
133 static void	vmxnet3_reinit_queues(struct vmxnet3_softc *);
134 static int	vmxnet3_enable_device(struct vmxnet3_softc *);
135 static void	vmxnet3_reinit_rxfilters(struct vmxnet3_softc *);
136 static void	vmxnet3_init(if_ctx_t);
137 static void	vmxnet3_multi_set(if_ctx_t);
138 static int	vmxnet3_mtu_set(if_ctx_t, uint32_t);
139 static void	vmxnet3_media_status(if_ctx_t, struct ifmediareq *);
140 static int	vmxnet3_media_change(if_ctx_t);
141 static int	vmxnet3_promisc_set(if_ctx_t, int);
142 static uint64_t	vmxnet3_get_counter(if_ctx_t, ift_counter);
143 static void	vmxnet3_update_admin_status(if_ctx_t);
144 static void	vmxnet3_txq_timer(if_ctx_t, uint16_t);
145 
146 static void	vmxnet3_update_vlan_filter(struct vmxnet3_softc *, int,
147 		    uint16_t);
148 static void	vmxnet3_vlan_register(if_ctx_t, uint16_t);
149 static void	vmxnet3_vlan_unregister(if_ctx_t, uint16_t);
150 static void	vmxnet3_set_rxfilter(struct vmxnet3_softc *, int);
151 
152 static void	vmxnet3_refresh_host_stats(struct vmxnet3_softc *);
153 static int	vmxnet3_link_is_up(struct vmxnet3_softc *);
154 static void	vmxnet3_link_status(struct vmxnet3_softc *);
155 static void	vmxnet3_set_lladdr(struct vmxnet3_softc *);
156 static void	vmxnet3_get_lladdr(struct vmxnet3_softc *);
157 
158 static void	vmxnet3_setup_txq_sysctl(struct vmxnet3_txqueue *,
159 		    struct sysctl_ctx_list *, struct sysctl_oid_list *);
160 static void	vmxnet3_setup_rxq_sysctl(struct vmxnet3_rxqueue *,
161 		    struct sysctl_ctx_list *, struct sysctl_oid_list *);
162 static void	vmxnet3_setup_queue_sysctl(struct vmxnet3_softc *,
163 		    struct sysctl_ctx_list *, struct sysctl_oid_list *);
164 static void	vmxnet3_setup_sysctl(struct vmxnet3_softc *);
165 
166 static void	vmxnet3_write_bar0(struct vmxnet3_softc *, bus_size_t,
167 		    uint32_t);
168 static uint32_t	vmxnet3_read_bar1(struct vmxnet3_softc *, bus_size_t);
169 static void	vmxnet3_write_bar1(struct vmxnet3_softc *, bus_size_t,
170 		    uint32_t);
171 static void	vmxnet3_write_cmd(struct vmxnet3_softc *, uint32_t);
172 static uint32_t	vmxnet3_read_cmd(struct vmxnet3_softc *, uint32_t);
173 
174 static int	vmxnet3_tx_queue_intr_enable(if_ctx_t, uint16_t);
175 static int	vmxnet3_rx_queue_intr_enable(if_ctx_t, uint16_t);
176 static void	vmxnet3_link_intr_enable(if_ctx_t);
177 static void	vmxnet3_enable_intr(struct vmxnet3_softc *, int);
178 static void	vmxnet3_disable_intr(struct vmxnet3_softc *, int);
179 static void	vmxnet3_intr_enable_all(if_ctx_t);
180 static void	vmxnet3_intr_disable_all(if_ctx_t);
181 static bool	vmxnet3_if_needs_restart(if_ctx_t, enum iflib_restart_event);
182 
183 typedef enum {
184 	VMXNET3_BARRIER_RD,
185 	VMXNET3_BARRIER_WR,
186 	VMXNET3_BARRIER_RDWR,
187 } vmxnet3_barrier_t;
188 
189 static void	vmxnet3_barrier(struct vmxnet3_softc *, vmxnet3_barrier_t);
190 
191 static device_method_t vmxnet3_methods[] = {
192 	/* Device interface */
193 	DEVMETHOD(device_register, vmxnet3_register),
194 	DEVMETHOD(device_probe, iflib_device_probe),
195 	DEVMETHOD(device_attach, iflib_device_attach),
196 	DEVMETHOD(device_detach, iflib_device_detach),
197 	DEVMETHOD(device_shutdown, iflib_device_shutdown),
198 	DEVMETHOD(device_suspend, iflib_device_suspend),
199 	DEVMETHOD(device_resume, iflib_device_resume),
200 	DEVMETHOD_END
201 };
202 
203 static driver_t vmxnet3_driver = {
204 	"vmx", vmxnet3_methods, sizeof(struct vmxnet3_softc)
205 };
206 
207 DRIVER_MODULE(vmx, pci, vmxnet3_driver, 0, 0);
208 IFLIB_PNP_INFO(pci, vmx, vmxnet3_vendor_info_array);
209 MODULE_VERSION(vmx, 2);
210 
211 MODULE_DEPEND(vmx, pci, 1, 1, 1);
212 MODULE_DEPEND(vmx, ether, 1, 1, 1);
213 MODULE_DEPEND(vmx, iflib, 1, 1, 1);
214 
215 static device_method_t vmxnet3_iflib_methods[] = {
216 	DEVMETHOD(ifdi_tx_queues_alloc, vmxnet3_tx_queues_alloc),
217 	DEVMETHOD(ifdi_rx_queues_alloc, vmxnet3_rx_queues_alloc),
218 	DEVMETHOD(ifdi_queues_free, vmxnet3_queues_free),
219 
220 	DEVMETHOD(ifdi_attach_pre, vmxnet3_attach_pre),
221 	DEVMETHOD(ifdi_attach_post, vmxnet3_attach_post),
222 	DEVMETHOD(ifdi_detach, vmxnet3_detach),
223 
224 	DEVMETHOD(ifdi_init, vmxnet3_init),
225 	DEVMETHOD(ifdi_stop, vmxnet3_stop),
226 	DEVMETHOD(ifdi_multi_set, vmxnet3_multi_set),
227 	DEVMETHOD(ifdi_mtu_set, vmxnet3_mtu_set),
228 	DEVMETHOD(ifdi_media_status, vmxnet3_media_status),
229 	DEVMETHOD(ifdi_media_change, vmxnet3_media_change),
230 	DEVMETHOD(ifdi_promisc_set, vmxnet3_promisc_set),
231 	DEVMETHOD(ifdi_get_counter, vmxnet3_get_counter),
232 	DEVMETHOD(ifdi_update_admin_status, vmxnet3_update_admin_status),
233 	DEVMETHOD(ifdi_timer, vmxnet3_txq_timer),
234 
235 	DEVMETHOD(ifdi_tx_queue_intr_enable, vmxnet3_tx_queue_intr_enable),
236 	DEVMETHOD(ifdi_rx_queue_intr_enable, vmxnet3_rx_queue_intr_enable),
237 	DEVMETHOD(ifdi_link_intr_enable, vmxnet3_link_intr_enable),
238 	DEVMETHOD(ifdi_intr_enable, vmxnet3_intr_enable_all),
239 	DEVMETHOD(ifdi_intr_disable, vmxnet3_intr_disable_all),
240 	DEVMETHOD(ifdi_msix_intr_assign, vmxnet3_msix_intr_assign),
241 
242 	DEVMETHOD(ifdi_vlan_register, vmxnet3_vlan_register),
243 	DEVMETHOD(ifdi_vlan_unregister, vmxnet3_vlan_unregister),
244 
245 	DEVMETHOD(ifdi_shutdown, vmxnet3_shutdown),
246 	DEVMETHOD(ifdi_suspend, vmxnet3_suspend),
247 	DEVMETHOD(ifdi_resume, vmxnet3_resume),
248 
249 	DEVMETHOD(ifdi_needs_restart, vmxnet3_if_needs_restart),
250 
251 	DEVMETHOD_END
252 };
253 
254 static driver_t vmxnet3_iflib_driver = {
255 	"vmx", vmxnet3_iflib_methods, sizeof(struct vmxnet3_softc)
256 };
257 
258 struct if_txrx vmxnet3_txrx = {
259 	.ift_txd_encap = vmxnet3_isc_txd_encap,
260 	.ift_txd_flush = vmxnet3_isc_txd_flush,
261 	.ift_txd_credits_update = vmxnet3_isc_txd_credits_update,
262 	.ift_rxd_available = vmxnet3_isc_rxd_available,
263 	.ift_rxd_pkt_get = vmxnet3_isc_rxd_pkt_get,
264 	.ift_rxd_refill = vmxnet3_isc_rxd_refill,
265 	.ift_rxd_flush = vmxnet3_isc_rxd_flush,
266 	.ift_legacy_intr = vmxnet3_legacy_intr
267 };
268 
269 static struct if_shared_ctx vmxnet3_sctx_init = {
270 	.isc_magic = IFLIB_MAGIC,
271 	.isc_q_align = 512,
272 
273 	.isc_tx_maxsize = VMXNET3_TX_MAXSIZE,
274 	.isc_tx_maxsegsize = VMXNET3_TX_MAXSEGSIZE,
275 	.isc_tso_maxsize = VMXNET3_TSO_MAXSIZE + sizeof(struct ether_vlan_header),
276 	.isc_tso_maxsegsize = VMXNET3_TX_MAXSEGSIZE,
277 
278 	/*
279 	 * These values are used to configure the busdma tag used for
280 	 * receive descriptors.  Each receive descriptor only points to one
281 	 * buffer.
282 	 */
283 	.isc_rx_maxsize = VMXNET3_RX_MAXSEGSIZE, /* One buf per descriptor */
284 	.isc_rx_nsegments = 1,  /* One mapping per descriptor */
285 	.isc_rx_maxsegsize = VMXNET3_RX_MAXSEGSIZE,
286 
287 	.isc_admin_intrcnt = 1,
288 	.isc_vendor_info = vmxnet3_vendor_info_array,
289 	.isc_driver_version = "2",
290 	.isc_driver = &vmxnet3_iflib_driver,
291 	.isc_flags = IFLIB_HAS_RXCQ | IFLIB_HAS_TXCQ | IFLIB_SINGLE_IRQ_RX_ONLY,
292 
293 	/*
294 	 * Number of receive queues per receive queue set, with associated
295 	 * descriptor settings for each.
296 	 */
297 	.isc_nrxqs = 3,
298 	.isc_nfl = 2, /* one free list for each receive command queue */
299 	.isc_nrxd_min = {VMXNET3_MIN_RX_NDESC, VMXNET3_MIN_RX_NDESC, VMXNET3_MIN_RX_NDESC},
300 	.isc_nrxd_max = {VMXNET3_MAX_RX_NDESC, VMXNET3_MAX_RX_NDESC, VMXNET3_MAX_RX_NDESC},
301 	.isc_nrxd_default = {VMXNET3_DEF_RX_NDESC, VMXNET3_DEF_RX_NDESC, VMXNET3_DEF_RX_NDESC},
302 
303 	/*
304 	 * Number of transmit queues per transmit queue set, with associated
305 	 * descriptor settings for each.
306 	 */
307 	.isc_ntxqs = 2,
308 	.isc_ntxd_min = {VMXNET3_MIN_TX_NDESC, VMXNET3_MIN_TX_NDESC},
309 	.isc_ntxd_max = {VMXNET3_MAX_TX_NDESC, VMXNET3_MAX_TX_NDESC},
310 	.isc_ntxd_default = {VMXNET3_DEF_TX_NDESC, VMXNET3_DEF_TX_NDESC},
311 };
312 
313 static void *
vmxnet3_register(device_t dev)314 vmxnet3_register(device_t dev)
315 {
316 	return (&vmxnet3_sctx_init);
317 }
318 
319 static int
trunc_powerof2(int val)320 trunc_powerof2(int val)
321 {
322 
323 	return (1U << (fls(val) - 1));
324 }
325 
326 static int
vmxnet3_attach_pre(if_ctx_t ctx)327 vmxnet3_attach_pre(if_ctx_t ctx)
328 {
329 	device_t dev;
330 	if_softc_ctx_t scctx;
331 	struct vmxnet3_softc *sc;
332 	uint32_t intr_config;
333 	int error;
334 
335 	dev = iflib_get_dev(ctx);
336 	sc = iflib_get_softc(ctx);
337 	sc->vmx_dev = dev;
338 	sc->vmx_ctx = ctx;
339 	sc->vmx_sctx = iflib_get_sctx(ctx);
340 	sc->vmx_scctx = iflib_get_softc_ctx(ctx);
341 	sc->vmx_ifp = iflib_get_ifp(ctx);
342 	sc->vmx_media = iflib_get_media(ctx);
343 	scctx = sc->vmx_scctx;
344 
345 	scctx->isc_tx_nsegments = VMXNET3_TX_MAXSEGS;
346 	scctx->isc_tx_tso_segments_max = VMXNET3_TX_MAXSEGS;
347 	/* isc_tx_tso_size_max doesn't include possible vlan header */
348 	scctx->isc_tx_tso_size_max = VMXNET3_TSO_MAXSIZE;
349 	scctx->isc_tx_tso_segsize_max = VMXNET3_TX_MAXSEGSIZE;
350 	scctx->isc_txrx = &vmxnet3_txrx;
351 
352 	/* If 0, the iflib tunable was not set, so set to the default */
353 	if (scctx->isc_nrxqsets == 0)
354 		scctx->isc_nrxqsets = VMXNET3_DEF_RX_QUEUES;
355 	scctx->isc_nrxqsets = trunc_powerof2(scctx->isc_nrxqsets);
356 	scctx->isc_nrxqsets_max = min(VMXNET3_MAX_RX_QUEUES, mp_ncpus);
357 	scctx->isc_nrxqsets_max = trunc_powerof2(scctx->isc_nrxqsets_max);
358 
359 	/* If 0, the iflib tunable was not set, so set to the default */
360 	if (scctx->isc_ntxqsets == 0)
361 		scctx->isc_ntxqsets = VMXNET3_DEF_TX_QUEUES;
362 	scctx->isc_ntxqsets = trunc_powerof2(scctx->isc_ntxqsets);
363 	scctx->isc_ntxqsets_max = min(VMXNET3_MAX_TX_QUEUES, mp_ncpus);
364 	scctx->isc_ntxqsets_max = trunc_powerof2(scctx->isc_ntxqsets_max);
365 
366 	/*
367 	 * Enforce that the transmit completion queue descriptor count is
368 	 * the same as the transmit command queue descriptor count.
369 	 */
370 	scctx->isc_ntxd[0] = scctx->isc_ntxd[1];
371 	scctx->isc_txqsizes[0] =
372 	    sizeof(struct vmxnet3_txcompdesc) * scctx->isc_ntxd[0];
373 	scctx->isc_txqsizes[1] =
374 	    sizeof(struct vmxnet3_txdesc) * scctx->isc_ntxd[1];
375 
376 	/*
377 	 * Enforce that the receive completion queue descriptor count is the
378 	 * sum of the receive command queue descriptor counts, and that the
379 	 * second receive command queue descriptor count is the same as the
380 	 * first one.
381 	 */
382 	scctx->isc_nrxd[2] = scctx->isc_nrxd[1];
383 	scctx->isc_nrxd[0] = scctx->isc_nrxd[1] + scctx->isc_nrxd[2];
384 	scctx->isc_rxqsizes[0] =
385 	    sizeof(struct vmxnet3_rxcompdesc) * scctx->isc_nrxd[0];
386 	scctx->isc_rxqsizes[1] =
387 	    sizeof(struct vmxnet3_rxdesc) * scctx->isc_nrxd[1];
388 	scctx->isc_rxqsizes[2] =
389 	    sizeof(struct vmxnet3_rxdesc) * scctx->isc_nrxd[2];
390 
391 	/*
392 	 * Initialize the max frame size and descriptor queue buffer
393 	 * sizes.
394 	 */
395 	vmxnet3_mtu_set(ctx, if_getmtu(sc->vmx_ifp));
396 
397 	scctx->isc_rss_table_size = UPT1_RSS_MAX_IND_TABLE_SIZE;
398 
399 	/* Map PCI BARs */
400 	error = vmxnet3_alloc_resources(sc);
401 	if (error)
402 		goto fail;
403 
404 	/* Check device versions */
405 	error = vmxnet3_check_version(sc);
406 	if (error)
407 		goto fail;
408 
409 	/*
410 	 * The interrupt mode can be set in the hypervisor configuration via
411 	 * the parameter ethernet<N>.intrMode.
412 	 */
413 	intr_config = vmxnet3_read_cmd(sc, VMXNET3_CMD_GET_INTRCFG);
414 	sc->vmx_intr_mask_mode = (intr_config >> 2) & 0x03;
415 
416 	/*
417 	 * Configure the softc context to attempt to configure the interrupt
418 	 * mode now indicated by intr_config.  iflib will follow the usual
419 	 * fallback path MSI-X -> MSI -> LEGACY, starting at the configured
420 	 * starting mode.
421 	 */
422 	switch (intr_config & 0x03) {
423 	case VMXNET3_IT_AUTO:
424 	case VMXNET3_IT_MSIX:
425 		scctx->isc_msix_bar = pci_msix_table_bar(dev);
426 		break;
427 	case VMXNET3_IT_MSI:
428 		scctx->isc_msix_bar = -1;
429 		scctx->isc_disable_msix = 1;
430 		break;
431 	case VMXNET3_IT_LEGACY:
432 		scctx->isc_msix_bar = 0;
433 		break;
434 	}
435 
436 	scctx->isc_tx_csum_flags = VMXNET3_CSUM_ALL_OFFLOAD;
437 	scctx->isc_capabilities = scctx->isc_capenable =
438 	    IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 |
439 	    IFCAP_TSO4 | IFCAP_TSO6 |
440 	    IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 |
441 	    IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING |
442 	    IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO |
443 	    IFCAP_JUMBO_MTU;
444 
445 	/* These capabilities are not enabled by default. */
446 	scctx->isc_capabilities |= IFCAP_LRO | IFCAP_VLAN_HWFILTER;
447 
448 	vmxnet3_get_lladdr(sc);
449 	iflib_set_mac(ctx, sc->vmx_lladdr);
450 
451 	return (0);
452 fail:
453 	/*
454 	 * We must completely clean up anything allocated above as iflib
455 	 * will not invoke any other driver entry points as a result of this
456 	 * failure.
457 	 */
458 	vmxnet3_free_resources(sc);
459 
460 	return (error);
461 }
462 
463 static int
vmxnet3_msix_intr_assign(if_ctx_t ctx,int msix)464 vmxnet3_msix_intr_assign(if_ctx_t ctx, int msix)
465 {
466 	struct vmxnet3_softc *sc;
467 	if_softc_ctx_t scctx;
468 	struct vmxnet3_rxqueue *rxq;
469 	int error;
470 	int i;
471 	char irq_name[16];
472 
473 	sc = iflib_get_softc(ctx);
474 	scctx = sc->vmx_scctx;
475 
476 	for (i = 0; i < scctx->isc_nrxqsets; i++) {
477 		snprintf(irq_name, sizeof(irq_name), "rxq%d", i);
478 
479 		rxq = &sc->vmx_rxq[i];
480 		error = iflib_irq_alloc_generic(ctx, &rxq->vxrxq_irq, i + 1,
481 		    IFLIB_INTR_RXTX, vmxnet3_rxq_intr, rxq, i, irq_name);
482 		if (error) {
483 			device_printf(iflib_get_dev(ctx),
484 			    "Failed to register rxq %d interrupt handler\n", i);
485 			return (error);
486 		}
487 	}
488 
489 	for (i = 0; i < scctx->isc_ntxqsets; i++) {
490 		snprintf(irq_name, sizeof(irq_name), "txq%d", i);
491 
492 		/*
493 		 * Don't provide the corresponding rxq irq for reference -
494 		 * we want the transmit task to be attached to a task queue
495 		 * that is different from the one used by the corresponding
496 		 * rxq irq.  That is because the TX doorbell writes are very
497 		 * expensive as virtualized MMIO operations, so we want to
498 		 * be able to defer them to another core when possible so
499 		 * that they don't steal receive processing cycles during
500 		 * stack turnarounds like TCP ACK generation.  The other
501 		 * piece to this approach is enabling the iflib abdicate
502 		 * option (currently via an interface-specific
503 		 * tunable/sysctl).
504 		 */
505 		iflib_softirq_alloc_generic(ctx, NULL, IFLIB_INTR_TX, NULL, i,
506 		    irq_name);
507 	}
508 
509 	error = iflib_irq_alloc_generic(ctx, &sc->vmx_event_intr_irq,
510 	    scctx->isc_nrxqsets + 1, IFLIB_INTR_ADMIN, vmxnet3_event_intr, sc, 0,
511 	    "event");
512 	if (error) {
513 		device_printf(iflib_get_dev(ctx),
514 		    "Failed to register event interrupt handler\n");
515 		return (error);
516 	}
517 
518 	return (0);
519 }
520 
521 static void
vmxnet3_free_irqs(struct vmxnet3_softc * sc)522 vmxnet3_free_irqs(struct vmxnet3_softc *sc)
523 {
524 	if_softc_ctx_t scctx;
525 	struct vmxnet3_rxqueue *rxq;
526 	int i;
527 
528 	scctx = sc->vmx_scctx;
529 
530 	if (sc->vmx_rxq != NULL) {
531 		for (i = 0; i < scctx->isc_nrxqsets; i++) {
532 			rxq = &sc->vmx_rxq[i];
533 			iflib_irq_free(sc->vmx_ctx, &rxq->vxrxq_irq);
534 		}
535 	}
536 
537 	iflib_irq_free(sc->vmx_ctx, &sc->vmx_event_intr_irq);
538 }
539 
540 static int
vmxnet3_attach_post(if_ctx_t ctx)541 vmxnet3_attach_post(if_ctx_t ctx)
542 {
543 	if_softc_ctx_t scctx;
544 	struct vmxnet3_softc *sc;
545 	int error;
546 
547 	scctx = iflib_get_softc_ctx(ctx);
548 	sc = iflib_get_softc(ctx);
549 
550 	if (scctx->isc_nrxqsets > 1)
551 		sc->vmx_flags |= VMXNET3_FLAG_RSS;
552 
553 	error = vmxnet3_alloc_data(sc);
554 	if (error)
555 		goto fail;
556 
557 	vmxnet3_set_interrupt_idx(sc);
558 	vmxnet3_setup_sysctl(sc);
559 
560 	ifmedia_add(sc->vmx_media, IFM_ETHER | IFM_AUTO, 0, NULL);
561 	ifmedia_set(sc->vmx_media, IFM_ETHER | IFM_AUTO);
562 
563 fail:
564 	return (error);
565 }
566 
567 static int
vmxnet3_detach(if_ctx_t ctx)568 vmxnet3_detach(if_ctx_t ctx)
569 {
570 	struct vmxnet3_softc *sc;
571 
572 	sc = iflib_get_softc(ctx);
573 
574 	vmxnet3_free_irqs(sc);
575 	vmxnet3_free_data(sc);
576 	vmxnet3_free_resources(sc);
577 
578 	return (0);
579 }
580 
581 static int
vmxnet3_shutdown(if_ctx_t ctx)582 vmxnet3_shutdown(if_ctx_t ctx)
583 {
584 
585 	return (0);
586 }
587 
588 static int
vmxnet3_suspend(if_ctx_t ctx)589 vmxnet3_suspend(if_ctx_t ctx)
590 {
591 
592 	return (0);
593 }
594 
595 static int
vmxnet3_resume(if_ctx_t ctx)596 vmxnet3_resume(if_ctx_t ctx)
597 {
598 
599 	return (0);
600 }
601 
602 static int
vmxnet3_alloc_resources(struct vmxnet3_softc * sc)603 vmxnet3_alloc_resources(struct vmxnet3_softc *sc)
604 {
605 	device_t dev;
606 	int rid;
607 
608 	dev = sc->vmx_dev;
609 
610 	rid = PCIR_BAR(0);
611 	sc->vmx_res0 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
612 	    RF_ACTIVE);
613 	if (sc->vmx_res0 == NULL) {
614 		device_printf(dev,
615 		    "could not map BAR0 memory\n");
616 		return (ENXIO);
617 	}
618 
619 	sc->vmx_iot0 = rman_get_bustag(sc->vmx_res0);
620 	sc->vmx_ioh0 = rman_get_bushandle(sc->vmx_res0);
621 
622 	rid = PCIR_BAR(1);
623 	sc->vmx_res1 = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
624 	    RF_ACTIVE);
625 	if (sc->vmx_res1 == NULL) {
626 		device_printf(dev,
627 		    "could not map BAR1 memory\n");
628 		return (ENXIO);
629 	}
630 
631 	sc->vmx_iot1 = rman_get_bustag(sc->vmx_res1);
632 	sc->vmx_ioh1 = rman_get_bushandle(sc->vmx_res1);
633 
634 	return (0);
635 }
636 
637 static void
vmxnet3_free_resources(struct vmxnet3_softc * sc)638 vmxnet3_free_resources(struct vmxnet3_softc *sc)
639 {
640 	device_t dev;
641 
642 	dev = sc->vmx_dev;
643 
644 	if (sc->vmx_res0 != NULL) {
645 		bus_release_resource(dev, SYS_RES_MEMORY,
646 		    rman_get_rid(sc->vmx_res0), sc->vmx_res0);
647 		sc->vmx_res0 = NULL;
648 	}
649 
650 	if (sc->vmx_res1 != NULL) {
651 		bus_release_resource(dev, SYS_RES_MEMORY,
652 		    rman_get_rid(sc->vmx_res1), sc->vmx_res1);
653 		sc->vmx_res1 = NULL;
654 	}
655 }
656 
657 static int
vmxnet3_check_version(struct vmxnet3_softc * sc)658 vmxnet3_check_version(struct vmxnet3_softc *sc)
659 {
660 	device_t dev;
661 	uint32_t version;
662 
663 	dev = sc->vmx_dev;
664 
665 	version = vmxnet3_read_bar1(sc, VMXNET3_BAR1_VRRS);
666 	if ((version & 0x01) == 0) {
667 		device_printf(dev, "unsupported hardware version %#x\n",
668 		    version);
669 		return (ENOTSUP);
670 	}
671 	vmxnet3_write_bar1(sc, VMXNET3_BAR1_VRRS, 1);
672 
673 	version = vmxnet3_read_bar1(sc, VMXNET3_BAR1_UVRS);
674 	if ((version & 0x01) == 0) {
675 		device_printf(dev, "unsupported UPT version %#x\n", version);
676 		return (ENOTSUP);
677 	}
678 	vmxnet3_write_bar1(sc, VMXNET3_BAR1_UVRS, 1);
679 
680 	return (0);
681 }
682 
683 static void
vmxnet3_set_interrupt_idx(struct vmxnet3_softc * sc)684 vmxnet3_set_interrupt_idx(struct vmxnet3_softc *sc)
685 {
686 	if_softc_ctx_t scctx;
687 	struct vmxnet3_txqueue *txq;
688 	struct vmxnet3_txq_shared *txs;
689 	struct vmxnet3_rxqueue *rxq;
690 	struct vmxnet3_rxq_shared *rxs;
691 	int intr_idx;
692 	int i;
693 
694 	scctx = sc->vmx_scctx;
695 
696 	/*
697 	 * There is always one interrupt per receive queue, assigned
698 	 * starting with the first interrupt.  When there is only one
699 	 * interrupt available, the event interrupt shares the receive queue
700 	 * interrupt, otherwise it uses the interrupt following the last
701 	 * receive queue interrupt.  Transmit queues are not assigned
702 	 * interrupts, so they are given indexes beyond the indexes that
703 	 * correspond to the real interrupts.
704 	 */
705 
706 	/* The event interrupt is always the last vector. */
707 	sc->vmx_event_intr_idx = scctx->isc_vectors - 1;
708 
709 	intr_idx = 0;
710 	for (i = 0; i < scctx->isc_nrxqsets; i++, intr_idx++) {
711 		rxq = &sc->vmx_rxq[i];
712 		rxs = rxq->vxrxq_rs;
713 		rxq->vxrxq_intr_idx = intr_idx;
714 		rxs->intr_idx = rxq->vxrxq_intr_idx;
715 	}
716 
717 	/*
718 	 * Assign the tx queues interrupt indexes above what we are actually
719 	 * using.  These interrupts will never be enabled.
720 	 */
721 	intr_idx = scctx->isc_vectors;
722 	for (i = 0; i < scctx->isc_ntxqsets; i++, intr_idx++) {
723 		txq = &sc->vmx_txq[i];
724 		txs = txq->vxtxq_ts;
725 		txq->vxtxq_intr_idx = intr_idx;
726 		txs->intr_idx = txq->vxtxq_intr_idx;
727 	}
728 }
729 
730 static int
vmxnet3_queues_shared_alloc(struct vmxnet3_softc * sc)731 vmxnet3_queues_shared_alloc(struct vmxnet3_softc *sc)
732 {
733 	if_softc_ctx_t scctx;
734 	int size;
735 	int error;
736 
737 	scctx = sc->vmx_scctx;
738 
739 	/*
740 	 * The txq and rxq shared data areas must be allocated contiguously
741 	 * as vmxnet3_driver_shared contains only a single address member
742 	 * for the shared queue data area.
743 	 */
744 	size = scctx->isc_ntxqsets * sizeof(struct vmxnet3_txq_shared) +
745 	    scctx->isc_nrxqsets * sizeof(struct vmxnet3_rxq_shared);
746 	error = iflib_dma_alloc_align(sc->vmx_ctx, size, 128, &sc->vmx_qs_dma, 0);
747 	if (error) {
748 		device_printf(sc->vmx_dev, "cannot alloc queue shared memory\n");
749 		return (error);
750 	}
751 
752 	return (0);
753 }
754 
755 static void
vmxnet3_init_txq(struct vmxnet3_softc * sc,int q)756 vmxnet3_init_txq(struct vmxnet3_softc *sc, int q)
757 {
758 	struct vmxnet3_txqueue *txq;
759 	struct vmxnet3_comp_ring *txc;
760 	struct vmxnet3_txring *txr;
761 	if_softc_ctx_t scctx;
762 
763 	txq = &sc->vmx_txq[q];
764 	txc = &txq->vxtxq_comp_ring;
765 	txr = &txq->vxtxq_cmd_ring;
766 	scctx = sc->vmx_scctx;
767 
768 	snprintf(txq->vxtxq_name, sizeof(txq->vxtxq_name), "%s-tx%d",
769 	    device_get_nameunit(sc->vmx_dev), q);
770 
771 	txq->vxtxq_sc = sc;
772 	txq->vxtxq_id = q;
773 	txc->vxcr_ndesc = scctx->isc_ntxd[0];
774 	txr->vxtxr_ndesc = scctx->isc_ntxd[1];
775 }
776 
777 static int
vmxnet3_tx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int ntxqs,int ntxqsets)778 vmxnet3_tx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
779     int ntxqs, int ntxqsets)
780 {
781 	struct vmxnet3_softc *sc;
782 	int q;
783 	int error;
784 	caddr_t kva;
785 
786 	sc = iflib_get_softc(ctx);
787 
788 	/* Allocate the array of transmit queues */
789 	sc->vmx_txq = malloc(sizeof(struct vmxnet3_txqueue) *
790 	    ntxqsets, M_DEVBUF, M_NOWAIT | M_ZERO);
791 	if (sc->vmx_txq == NULL)
792 		return (ENOMEM);
793 
794 	/* Initialize driver state for each transmit queue */
795 	for (q = 0; q < ntxqsets; q++)
796 		vmxnet3_init_txq(sc, q);
797 
798 	/*
799 	 * Allocate queue state that is shared with the device.  This check
800 	 * and call is performed in both vmxnet3_tx_queues_alloc() and
801 	 * vmxnet3_rx_queues_alloc() so that we don't have to care which
802 	 * order iflib invokes those routines in.
803 	 */
804 	if (sc->vmx_qs_dma.idi_size == 0) {
805 		error = vmxnet3_queues_shared_alloc(sc);
806 		if (error)
807 			return (error);
808 	}
809 
810 	kva = sc->vmx_qs_dma.idi_vaddr;
811 	for (q = 0; q < ntxqsets; q++) {
812 		sc->vmx_txq[q].vxtxq_ts = (struct vmxnet3_txq_shared *) kva;
813 		kva += sizeof(struct vmxnet3_txq_shared);
814 	}
815 
816 	/* Record descriptor ring vaddrs and paddrs */
817 	for (q = 0; q < ntxqsets; q++) {
818 		struct vmxnet3_txqueue *txq;
819 		struct vmxnet3_txring *txr;
820 		struct vmxnet3_comp_ring *txc;
821 
822 		txq = &sc->vmx_txq[q];
823 		txc = &txq->vxtxq_comp_ring;
824 		txr = &txq->vxtxq_cmd_ring;
825 
826 		/* Completion ring */
827 		txc->vxcr_u.txcd =
828 		    (struct vmxnet3_txcompdesc *) vaddrs[q * ntxqs + 0];
829 		txc->vxcr_paddr = paddrs[q * ntxqs + 0];
830 
831 		/* Command ring */
832 		txr->vxtxr_txd =
833 		    (struct vmxnet3_txdesc *) vaddrs[q * ntxqs + 1];
834 		txr->vxtxr_paddr = paddrs[q * ntxqs + 1];
835 	}
836 
837 	return (0);
838 }
839 
840 static void
vmxnet3_init_rxq(struct vmxnet3_softc * sc,int q,int nrxqs)841 vmxnet3_init_rxq(struct vmxnet3_softc *sc, int q, int nrxqs)
842 {
843 	struct vmxnet3_rxqueue *rxq;
844 	struct vmxnet3_comp_ring *rxc;
845 	struct vmxnet3_rxring *rxr;
846 	if_softc_ctx_t scctx;
847 	int i;
848 
849 	rxq = &sc->vmx_rxq[q];
850 	rxc = &rxq->vxrxq_comp_ring;
851 	scctx = sc->vmx_scctx;
852 
853 	snprintf(rxq->vxrxq_name, sizeof(rxq->vxrxq_name), "%s-rx%d",
854 	    device_get_nameunit(sc->vmx_dev), q);
855 
856 	rxq->vxrxq_sc = sc;
857 	rxq->vxrxq_id = q;
858 
859 	/*
860 	 * First rxq is the completion queue, so there are nrxqs - 1 command
861 	 * rings starting at iflib queue id 1.
862 	 */
863 	rxc->vxcr_ndesc = scctx->isc_nrxd[0];
864 	for (i = 0; i < nrxqs - 1; i++) {
865 		rxr = &rxq->vxrxq_cmd_ring[i];
866 		rxr->vxrxr_ndesc = scctx->isc_nrxd[i + 1];
867 	}
868 }
869 
870 static int
vmxnet3_rx_queues_alloc(if_ctx_t ctx,caddr_t * vaddrs,uint64_t * paddrs,int nrxqs,int nrxqsets)871 vmxnet3_rx_queues_alloc(if_ctx_t ctx, caddr_t *vaddrs, uint64_t *paddrs,
872     int nrxqs, int nrxqsets)
873 {
874 	struct vmxnet3_softc *sc;
875 	if_softc_ctx_t scctx;
876 	int q;
877 	int i;
878 	int error;
879 	caddr_t kva;
880 
881 	sc = iflib_get_softc(ctx);
882 	scctx = sc->vmx_scctx;
883 
884 	/* Allocate the array of receive queues */
885 	sc->vmx_rxq = malloc(sizeof(struct vmxnet3_rxqueue) *
886 	    nrxqsets, M_DEVBUF, M_NOWAIT | M_ZERO);
887 	if (sc->vmx_rxq == NULL)
888 		return (ENOMEM);
889 
890 	/* Initialize driver state for each receive queue */
891 	for (q = 0; q < nrxqsets; q++)
892 		vmxnet3_init_rxq(sc, q, nrxqs);
893 
894 	/*
895 	 * Allocate queue state that is shared with the device.  This check
896 	 * and call is performed in both vmxnet3_tx_queues_alloc() and
897 	 * vmxnet3_rx_queues_alloc() so that we don't have to care which
898 	 * order iflib invokes those routines in.
899 	 */
900 	if (sc->vmx_qs_dma.idi_size == 0) {
901 		error = vmxnet3_queues_shared_alloc(sc);
902 		if (error)
903 			return (error);
904 	}
905 
906 	kva = sc->vmx_qs_dma.idi_vaddr +
907 	    scctx->isc_ntxqsets * sizeof(struct vmxnet3_txq_shared);
908 	for (q = 0; q < nrxqsets; q++) {
909 		sc->vmx_rxq[q].vxrxq_rs = (struct vmxnet3_rxq_shared *) kva;
910 		kva += sizeof(struct vmxnet3_rxq_shared);
911 	}
912 
913 	/* Record descriptor ring vaddrs and paddrs */
914 	for (q = 0; q < nrxqsets; q++) {
915 		struct vmxnet3_rxqueue *rxq;
916 		struct vmxnet3_rxring *rxr;
917 		struct vmxnet3_comp_ring *rxc;
918 
919 		rxq = &sc->vmx_rxq[q];
920 		rxc = &rxq->vxrxq_comp_ring;
921 
922 		/* Completion ring */
923 		rxc->vxcr_u.rxcd =
924 		    (struct vmxnet3_rxcompdesc *) vaddrs[q * nrxqs + 0];
925 		rxc->vxcr_paddr = paddrs[q * nrxqs + 0];
926 
927 		/* Command ring(s) */
928 		for (i = 0; i < nrxqs - 1; i++) {
929 			rxr = &rxq->vxrxq_cmd_ring[i];
930 
931 			rxr->vxrxr_rxd =
932 			    (struct vmxnet3_rxdesc *) vaddrs[q * nrxqs + 1 + i];
933 			rxr->vxrxr_paddr = paddrs[q * nrxqs + 1 + i];
934 		}
935 	}
936 
937 	return (0);
938 }
939 
940 static void
vmxnet3_queues_free(if_ctx_t ctx)941 vmxnet3_queues_free(if_ctx_t ctx)
942 {
943 	struct vmxnet3_softc *sc;
944 
945 	sc = iflib_get_softc(ctx);
946 
947 	/* Free queue state area that is shared with the device */
948 	if (sc->vmx_qs_dma.idi_size != 0) {
949 		iflib_dma_free(&sc->vmx_qs_dma);
950 		sc->vmx_qs_dma.idi_size = 0;
951 	}
952 
953 	/* Free array of receive queues */
954 	if (sc->vmx_rxq != NULL) {
955 		free(sc->vmx_rxq, M_DEVBUF);
956 		sc->vmx_rxq = NULL;
957 	}
958 
959 	/* Free array of transmit queues */
960 	if (sc->vmx_txq != NULL) {
961 		free(sc->vmx_txq, M_DEVBUF);
962 		sc->vmx_txq = NULL;
963 	}
964 }
965 
966 static int
vmxnet3_alloc_shared_data(struct vmxnet3_softc * sc)967 vmxnet3_alloc_shared_data(struct vmxnet3_softc *sc)
968 {
969 	device_t dev;
970 	size_t size;
971 	int error;
972 
973 	dev = sc->vmx_dev;
974 
975 	/* Top level state structure shared with the device */
976 	size = sizeof(struct vmxnet3_driver_shared);
977 	error = iflib_dma_alloc_align(sc->vmx_ctx, size, 1, &sc->vmx_ds_dma, 0);
978 	if (error) {
979 		device_printf(dev, "cannot alloc shared memory\n");
980 		return (error);
981 	}
982 	sc->vmx_ds = (struct vmxnet3_driver_shared *) sc->vmx_ds_dma.idi_vaddr;
983 
984 	/* RSS table state shared with the device */
985 	if (sc->vmx_flags & VMXNET3_FLAG_RSS) {
986 		size = sizeof(struct vmxnet3_rss_shared);
987 		error = iflib_dma_alloc_align(sc->vmx_ctx, size, 128,
988 		    &sc->vmx_rss_dma, 0);
989 		if (error) {
990 			device_printf(dev, "cannot alloc rss shared memory\n");
991 			return (error);
992 		}
993 		sc->vmx_rss =
994 		    (struct vmxnet3_rss_shared *) sc->vmx_rss_dma.idi_vaddr;
995 	}
996 
997 	return (0);
998 }
999 
1000 static void
vmxnet3_free_shared_data(struct vmxnet3_softc * sc)1001 vmxnet3_free_shared_data(struct vmxnet3_softc *sc)
1002 {
1003 
1004 	/* Free RSS table state shared with the device */
1005 	if (sc->vmx_rss != NULL) {
1006 		iflib_dma_free(&sc->vmx_rss_dma);
1007 		sc->vmx_rss = NULL;
1008 	}
1009 
1010 	/* Free top level state structure shared with the device */
1011 	if (sc->vmx_ds != NULL) {
1012 		iflib_dma_free(&sc->vmx_ds_dma);
1013 		sc->vmx_ds = NULL;
1014 	}
1015 }
1016 
1017 static int
vmxnet3_alloc_mcast_table(struct vmxnet3_softc * sc)1018 vmxnet3_alloc_mcast_table(struct vmxnet3_softc *sc)
1019 {
1020 	int error;
1021 
1022 	/* Multicast table state shared with the device */
1023 	error = iflib_dma_alloc_align(sc->vmx_ctx,
1024 	    VMXNET3_MULTICAST_MAX * ETHER_ADDR_LEN, 32, &sc->vmx_mcast_dma, 0);
1025 	if (error)
1026 		device_printf(sc->vmx_dev, "unable to alloc multicast table\n");
1027 	else
1028 		sc->vmx_mcast = sc->vmx_mcast_dma.idi_vaddr;
1029 
1030 	return (error);
1031 }
1032 
1033 static void
vmxnet3_free_mcast_table(struct vmxnet3_softc * sc)1034 vmxnet3_free_mcast_table(struct vmxnet3_softc *sc)
1035 {
1036 
1037 	/* Free multicast table state shared with the device */
1038 	if (sc->vmx_mcast != NULL) {
1039 		iflib_dma_free(&sc->vmx_mcast_dma);
1040 		sc->vmx_mcast = NULL;
1041 	}
1042 }
1043 
1044 static void
vmxnet3_init_shared_data(struct vmxnet3_softc * sc)1045 vmxnet3_init_shared_data(struct vmxnet3_softc *sc)
1046 {
1047 	struct vmxnet3_driver_shared *ds;
1048 	if_softc_ctx_t scctx;
1049 	struct vmxnet3_txqueue *txq;
1050 	struct vmxnet3_txq_shared *txs;
1051 	struct vmxnet3_rxqueue *rxq;
1052 	struct vmxnet3_rxq_shared *rxs;
1053 	int i;
1054 
1055 	ds = sc->vmx_ds;
1056 	scctx = sc->vmx_scctx;
1057 
1058 	/*
1059 	 * Initialize fields of the shared data that remains the same across
1060 	 * reinits. Note the shared data is zero'd when allocated.
1061 	 */
1062 
1063 	ds->magic = VMXNET3_REV1_MAGIC;
1064 
1065 	/* DriverInfo */
1066 	ds->version = VMXNET3_DRIVER_VERSION;
1067 	ds->guest = VMXNET3_GOS_FREEBSD |
1068 #ifdef __LP64__
1069 	    VMXNET3_GOS_64BIT;
1070 #else
1071 	    VMXNET3_GOS_32BIT;
1072 #endif
1073 	ds->vmxnet3_revision = 1;
1074 	ds->upt_version = 1;
1075 
1076 	/* Misc. conf */
1077 	ds->driver_data = vtophys(sc);
1078 	ds->driver_data_len = sizeof(struct vmxnet3_softc);
1079 	ds->queue_shared = sc->vmx_qs_dma.idi_paddr;
1080 	ds->queue_shared_len = sc->vmx_qs_dma.idi_size;
1081 	ds->nrxsg_max = IFLIB_MAX_RX_SEGS;
1082 
1083 	/* RSS conf */
1084 	if (sc->vmx_flags & VMXNET3_FLAG_RSS) {
1085 		ds->rss.version = 1;
1086 		ds->rss.paddr = sc->vmx_rss_dma.idi_paddr;
1087 		ds->rss.len = sc->vmx_rss_dma.idi_size;
1088 	}
1089 
1090 	/* Interrupt control. */
1091 	ds->automask = sc->vmx_intr_mask_mode == VMXNET3_IMM_AUTO;
1092 	/*
1093 	 * Total number of interrupt indexes we are using in the shared
1094 	 * config data, even though we don't actually allocate interrupt
1095 	 * resources for the tx queues.  Some versions of the device will
1096 	 * fail to initialize successfully if interrupt indexes are used in
1097 	 * the shared config that exceed the number of interrupts configured
1098 	 * here.
1099 	 */
1100 	ds->nintr = (scctx->isc_vectors == 1) ?
1101 	    2 : (scctx->isc_nrxqsets + scctx->isc_ntxqsets + 1);
1102 	ds->evintr = sc->vmx_event_intr_idx;
1103 	ds->ictrl = VMXNET3_ICTRL_DISABLE_ALL;
1104 
1105 	for (i = 0; i < ds->nintr; i++)
1106 		ds->modlevel[i] = UPT1_IMOD_ADAPTIVE;
1107 
1108 	/* Receive filter. */
1109 	ds->mcast_table = sc->vmx_mcast_dma.idi_paddr;
1110 	ds->mcast_tablelen = sc->vmx_mcast_dma.idi_size;
1111 
1112 	/* Tx queues */
1113 	for (i = 0; i < scctx->isc_ntxqsets; i++) {
1114 		txq = &sc->vmx_txq[i];
1115 		txs = txq->vxtxq_ts;
1116 
1117 		txs->cmd_ring = txq->vxtxq_cmd_ring.vxtxr_paddr;
1118 		txs->cmd_ring_len = txq->vxtxq_cmd_ring.vxtxr_ndesc;
1119 		txs->comp_ring = txq->vxtxq_comp_ring.vxcr_paddr;
1120 		txs->comp_ring_len = txq->vxtxq_comp_ring.vxcr_ndesc;
1121 		txs->driver_data = vtophys(txq);
1122 		txs->driver_data_len = sizeof(struct vmxnet3_txqueue);
1123 	}
1124 
1125 	/* Rx queues */
1126 	for (i = 0; i < scctx->isc_nrxqsets; i++) {
1127 		rxq = &sc->vmx_rxq[i];
1128 		rxs = rxq->vxrxq_rs;
1129 
1130 		rxs->cmd_ring[0] = rxq->vxrxq_cmd_ring[0].vxrxr_paddr;
1131 		rxs->cmd_ring_len[0] = rxq->vxrxq_cmd_ring[0].vxrxr_ndesc;
1132 		rxs->cmd_ring[1] = rxq->vxrxq_cmd_ring[1].vxrxr_paddr;
1133 		rxs->cmd_ring_len[1] = rxq->vxrxq_cmd_ring[1].vxrxr_ndesc;
1134 		rxs->comp_ring = rxq->vxrxq_comp_ring.vxcr_paddr;
1135 		rxs->comp_ring_len = rxq->vxrxq_comp_ring.vxcr_ndesc;
1136 		rxs->driver_data = vtophys(rxq);
1137 		rxs->driver_data_len = sizeof(struct vmxnet3_rxqueue);
1138 	}
1139 }
1140 
1141 static void
vmxnet3_reinit_rss_shared_data(struct vmxnet3_softc * sc)1142 vmxnet3_reinit_rss_shared_data(struct vmxnet3_softc *sc)
1143 {
1144 	if_softc_ctx_t scctx;
1145 	struct vmxnet3_rss_shared *rss;
1146 #ifdef RSS
1147 	uint8_t rss_algo;
1148 #endif
1149 	int i;
1150 
1151 	scctx = sc->vmx_scctx;
1152 	rss = sc->vmx_rss;
1153 
1154 	rss->hash_type =
1155 	    UPT1_RSS_HASH_TYPE_IPV4 | UPT1_RSS_HASH_TYPE_TCP_IPV4 |
1156 	    UPT1_RSS_HASH_TYPE_IPV6 | UPT1_RSS_HASH_TYPE_TCP_IPV6;
1157 	rss->hash_func = UPT1_RSS_HASH_FUNC_TOEPLITZ;
1158 	rss->hash_key_size = UPT1_RSS_MAX_KEY_SIZE;
1159 	rss->ind_table_size = UPT1_RSS_MAX_IND_TABLE_SIZE;
1160 	/*
1161 	 * Always use the kernel RSS key for consistent hashing.
1162 	 * If software RSS is configured to Toeplitz and RSS CPU steering
1163 	 * is available, use the RSS indirection table. Otherwise use
1164 	 * simple round-robin but still report hash as opaque to disengage
1165 	 * from software RSS when CPU steering is not available.
1166 	 */
1167 	rss_getkey(rss->hash_key);
1168 
1169 #ifdef RSS
1170 	rss_algo = rss_gethashalgo();
1171 	if (rss_algo == RSS_HASH_TOEPLITZ) {
1172 		for (i = 0; i < UPT1_RSS_MAX_IND_TABLE_SIZE; i++) {
1173 			rss->ind_table[i] = rss_get_indirection_to_bucket(i) %
1174 			    scctx->isc_nrxqsets;
1175 		}
1176 		sc->vmx_flags |= VMXNET3_FLAG_SOFT_RSS;
1177 	} else
1178 #endif
1179 	{
1180 		for (i = 0; i < UPT1_RSS_MAX_IND_TABLE_SIZE; i++)
1181 			rss->ind_table[i] = i % scctx->isc_nrxqsets;
1182 		sc->vmx_flags &= ~VMXNET3_FLAG_SOFT_RSS;
1183 	}
1184 }
1185 
1186 static void
vmxnet3_reinit_shared_data(struct vmxnet3_softc * sc)1187 vmxnet3_reinit_shared_data(struct vmxnet3_softc *sc)
1188 {
1189 	if_t ifp;
1190 	struct vmxnet3_driver_shared *ds;
1191 	if_softc_ctx_t scctx;
1192 
1193 	ifp = sc->vmx_ifp;
1194 	ds = sc->vmx_ds;
1195 	scctx = sc->vmx_scctx;
1196 
1197 	ds->mtu = if_getmtu(ifp);
1198 	ds->ntxqueue = scctx->isc_ntxqsets;
1199 	ds->nrxqueue = scctx->isc_nrxqsets;
1200 
1201 	ds->upt_features = 0;
1202 	if (if_getcapenable(ifp) & (IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6))
1203 		ds->upt_features |= UPT1_F_CSUM;
1204 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWTAGGING)
1205 		ds->upt_features |= UPT1_F_VLAN;
1206 	if (if_getcapenable(ifp) & IFCAP_LRO)
1207 		ds->upt_features |= UPT1_F_LRO;
1208 
1209 	if (sc->vmx_flags & VMXNET3_FLAG_RSS) {
1210 		ds->upt_features |= UPT1_F_RSS;
1211 		vmxnet3_reinit_rss_shared_data(sc);
1212 	}
1213 
1214 	vmxnet3_write_bar1(sc, VMXNET3_BAR1_DSL, sc->vmx_ds_dma.idi_paddr);
1215 	vmxnet3_write_bar1(sc, VMXNET3_BAR1_DSH,
1216 	    (uint64_t) sc->vmx_ds_dma.idi_paddr >> 32);
1217 }
1218 
1219 static int
vmxnet3_alloc_data(struct vmxnet3_softc * sc)1220 vmxnet3_alloc_data(struct vmxnet3_softc *sc)
1221 {
1222 	int error;
1223 
1224 	error = vmxnet3_alloc_shared_data(sc);
1225 	if (error)
1226 		return (error);
1227 
1228 	error = vmxnet3_alloc_mcast_table(sc);
1229 	if (error)
1230 		return (error);
1231 
1232 	vmxnet3_init_shared_data(sc);
1233 
1234 	return (0);
1235 }
1236 
1237 static void
vmxnet3_free_data(struct vmxnet3_softc * sc)1238 vmxnet3_free_data(struct vmxnet3_softc *sc)
1239 {
1240 
1241 	vmxnet3_free_mcast_table(sc);
1242 	vmxnet3_free_shared_data(sc);
1243 }
1244 
1245 static void
vmxnet3_evintr(struct vmxnet3_softc * sc)1246 vmxnet3_evintr(struct vmxnet3_softc *sc)
1247 {
1248 	device_t dev;
1249 	struct vmxnet3_txq_shared *ts;
1250 	struct vmxnet3_rxq_shared *rs;
1251 	uint32_t event;
1252 
1253 	dev = sc->vmx_dev;
1254 
1255 	/* Clear events. */
1256 	event = sc->vmx_ds->event;
1257 	vmxnet3_write_bar1(sc, VMXNET3_BAR1_EVENT, event);
1258 
1259 	if (event & VMXNET3_EVENT_LINK)
1260 		vmxnet3_link_status(sc);
1261 
1262 	if (event & (VMXNET3_EVENT_TQERROR | VMXNET3_EVENT_RQERROR)) {
1263 		vmxnet3_read_cmd(sc, VMXNET3_CMD_GET_STATUS);
1264 		ts = sc->vmx_txq[0].vxtxq_ts;
1265 		if (ts->stopped != 0)
1266 			device_printf(dev, "Tx queue error %#x\n", ts->error);
1267 		rs = sc->vmx_rxq[0].vxrxq_rs;
1268 		if (rs->stopped != 0)
1269 			device_printf(dev, "Rx queue error %#x\n", rs->error);
1270 
1271 		/* XXX - rely on liflib watchdog to reset us? */
1272 		device_printf(dev, "Rx/Tx queue error event ... "
1273 		    "waiting for iflib watchdog reset\n");
1274 	}
1275 
1276 	if (event & VMXNET3_EVENT_DIC)
1277 		device_printf(dev, "device implementation change event\n");
1278 	if (event & VMXNET3_EVENT_DEBUG)
1279 		device_printf(dev, "debug event\n");
1280 }
1281 
1282 static int
vmxnet3_isc_txd_encap(void * vsc,if_pkt_info_t pi)1283 vmxnet3_isc_txd_encap(void *vsc, if_pkt_info_t pi)
1284 {
1285 	struct vmxnet3_softc *sc;
1286 	struct vmxnet3_txqueue *txq;
1287 	struct vmxnet3_txring *txr;
1288 	struct vmxnet3_txdesc *txd, *sop;
1289 	bus_dma_segment_t *segs;
1290 	int nsegs;
1291 	int pidx;
1292 	int hdrlen;
1293 	int i;
1294 	int gen;
1295 
1296 	sc = vsc;
1297 	txq = &sc->vmx_txq[pi->ipi_qsidx];
1298 	txr = &txq->vxtxq_cmd_ring;
1299 	segs = pi->ipi_segs;
1300 	nsegs = pi->ipi_nsegs;
1301 	pidx = pi->ipi_pidx;
1302 
1303 	KASSERT(nsegs <= VMXNET3_TX_MAXSEGS,
1304 	    ("%s: packet with too many segments %d", __func__, nsegs));
1305 
1306 	sop = &txr->vxtxr_txd[pidx];
1307 	gen = txr->vxtxr_gen ^ 1;	/* Owned by cpu (yet) */
1308 
1309 	for (i = 0; i < nsegs; i++) {
1310 		txd = &txr->vxtxr_txd[pidx];
1311 
1312 		txd->addr = segs[i].ds_addr;
1313 		txd->len = segs[i].ds_len;
1314 		txd->gen = gen;
1315 		txd->dtype = 0;
1316 		txd->offload_mode = VMXNET3_OM_NONE;
1317 		txd->offload_pos = 0;
1318 		txd->hlen = 0;
1319 		txd->eop = 0;
1320 		txd->compreq = 0;
1321 		txd->vtag_mode = 0;
1322 		txd->vtag = 0;
1323 
1324 		if (++pidx == txr->vxtxr_ndesc) {
1325 			pidx = 0;
1326 			txr->vxtxr_gen ^= 1;
1327 		}
1328 		gen = txr->vxtxr_gen;
1329 	}
1330 	txd->eop = 1;
1331 	txd->compreq = !!(pi->ipi_flags & IPI_TX_INTR);
1332 	pi->ipi_new_pidx = pidx;
1333 
1334 	/*
1335 	 * VLAN
1336 	 */
1337 	if (pi->ipi_mflags & M_VLANTAG) {
1338 		sop->vtag_mode = 1;
1339 		sop->vtag = pi->ipi_vtag;
1340 	}
1341 
1342 	/*
1343 	 * TSO and checksum offloads
1344 	 */
1345 	hdrlen = pi->ipi_ehdrlen + pi->ipi_ip_hlen;
1346 	if (pi->ipi_csum_flags & CSUM_TSO) {
1347 		sop->offload_mode = VMXNET3_OM_TSO;
1348 		sop->hlen = hdrlen + pi->ipi_tcp_hlen;
1349 		sop->offload_pos = pi->ipi_tso_segsz;
1350 	} else if (pi->ipi_csum_flags & (VMXNET3_CSUM_OFFLOAD |
1351 	    VMXNET3_CSUM_OFFLOAD_IPV6)) {
1352 		sop->offload_mode = VMXNET3_OM_CSUM;
1353 		sop->hlen = hdrlen;
1354 		sop->offload_pos = hdrlen +
1355 		    ((pi->ipi_ipproto == IPPROTO_TCP) ?
1356 			offsetof(struct tcphdr, th_sum) :
1357 			offsetof(struct udphdr, uh_sum));
1358 	}
1359 
1360 	/* Finally, change the ownership. */
1361 	vmxnet3_barrier(sc, VMXNET3_BARRIER_WR);
1362 	sop->gen ^= 1;
1363 
1364 	return (0);
1365 }
1366 
1367 static void
vmxnet3_isc_txd_flush(void * vsc,uint16_t txqid,qidx_t pidx)1368 vmxnet3_isc_txd_flush(void *vsc, uint16_t txqid, qidx_t pidx)
1369 {
1370 	struct vmxnet3_softc *sc;
1371 	struct vmxnet3_txqueue *txq;
1372 
1373 	sc = vsc;
1374 	txq = &sc->vmx_txq[txqid];
1375 
1376 	/*
1377 	 * pidx is what we last set ipi_new_pidx to in
1378 	 * vmxnet3_isc_txd_encap()
1379 	 */
1380 
1381 	/*
1382 	 * Avoid expensive register updates if the flush request is
1383 	 * redundant.
1384 	 */
1385 	if (txq->vxtxq_last_flush == pidx)
1386 		return;
1387 	txq->vxtxq_last_flush = pidx;
1388 	vmxnet3_write_bar0(sc, VMXNET3_BAR0_TXH(txq->vxtxq_id), pidx);
1389 }
1390 
1391 static int
vmxnet3_isc_txd_credits_update(void * vsc,uint16_t txqid,bool clear)1392 vmxnet3_isc_txd_credits_update(void *vsc, uint16_t txqid, bool clear)
1393 {
1394 	struct vmxnet3_softc *sc;
1395 	struct vmxnet3_txqueue *txq;
1396 	struct vmxnet3_comp_ring *txc;
1397 	struct vmxnet3_txcompdesc *txcd;
1398 	struct vmxnet3_txring *txr;
1399 	int processed;
1400 
1401 	sc = vsc;
1402 	txq = &sc->vmx_txq[txqid];
1403 	txc = &txq->vxtxq_comp_ring;
1404 	txr = &txq->vxtxq_cmd_ring;
1405 
1406 	/*
1407 	 * If clear is true, we need to report the number of TX command ring
1408 	 * descriptors that have been processed by the device.  If clear is
1409 	 * false, we just need to report whether or not at least one TX
1410 	 * command ring descriptor has been processed by the device.
1411 	 */
1412 	processed = 0;
1413 	for (;;) {
1414 		txcd = &txc->vxcr_u.txcd[txc->vxcr_next];
1415 		if (txcd->gen != txc->vxcr_gen)
1416 			break;
1417 		else if (!clear)
1418 			return (1);
1419 		vmxnet3_barrier(sc, VMXNET3_BARRIER_RD);
1420 
1421 		MPASS(txc->vxcr_next < txc->vxcr_ndesc);
1422 		if (++txc->vxcr_next >= txc->vxcr_ndesc) {
1423 			txc->vxcr_next = 0;
1424 			txc->vxcr_gen ^= 1;
1425 		}
1426 
1427 		if (txcd->eop_idx < txr->vxtxr_next)
1428 			processed += txr->vxtxr_ndesc -
1429 			    (txr->vxtxr_next - txcd->eop_idx) + 1;
1430 		else
1431 			processed += txcd->eop_idx - txr->vxtxr_next + 1;
1432 		txr->vxtxr_next = (txcd->eop_idx + 1) % txr->vxtxr_ndesc;
1433 	}
1434 
1435 	return (processed);
1436 }
1437 
1438 static int
vmxnet3_isc_rxd_available(void * vsc,uint16_t rxqid,qidx_t idx,qidx_t budget)1439 vmxnet3_isc_rxd_available(void *vsc, uint16_t rxqid, qidx_t idx, qidx_t budget)
1440 {
1441 	struct vmxnet3_softc *sc;
1442 	struct vmxnet3_rxqueue *rxq;
1443 	struct vmxnet3_comp_ring *rxc;
1444 	struct vmxnet3_rxcompdesc *rxcd;
1445 	int avail;
1446 	int completed_gen;
1447 #ifdef INVARIANTS
1448 	int expect_sop = 1;
1449 #endif
1450 	sc = vsc;
1451 	rxq = &sc->vmx_rxq[rxqid];
1452 	rxc = &rxq->vxrxq_comp_ring;
1453 
1454 	avail = 0;
1455 	completed_gen = rxc->vxcr_gen;
1456 	for (;;) {
1457 		rxcd = &rxc->vxcr_u.rxcd[idx];
1458 		if (rxcd->gen != completed_gen)
1459 			break;
1460 		vmxnet3_barrier(sc, VMXNET3_BARRIER_RD);
1461 
1462 #ifdef INVARIANTS
1463 		if (expect_sop)
1464 			KASSERT(rxcd->sop, ("%s: expected sop", __func__));
1465 		else
1466 			KASSERT(!rxcd->sop, ("%s: unexpected sop", __func__));
1467 		expect_sop = rxcd->eop;
1468 #endif
1469 		if (rxcd->eop && (rxcd->len != 0))
1470 			avail++;
1471 		if (avail > budget)
1472 			break;
1473 		if (++idx == rxc->vxcr_ndesc) {
1474 			idx = 0;
1475 			completed_gen ^= 1;
1476 		}
1477 	}
1478 
1479 	return (avail);
1480 }
1481 
1482 static int
vmxnet3_isc_rxd_pkt_get(void * vsc,if_rxd_info_t ri)1483 vmxnet3_isc_rxd_pkt_get(void *vsc, if_rxd_info_t ri)
1484 {
1485 	struct vmxnet3_softc *sc;
1486 	if_softc_ctx_t scctx;
1487 	struct vmxnet3_rxqueue *rxq;
1488 	struct vmxnet3_comp_ring *rxc;
1489 	struct vmxnet3_rxcompdesc *rxcd;
1490 	if_rxd_frag_t frag;
1491 	int cqidx;
1492 	uint16_t total_len;
1493 	uint8_t nfrags;
1494 	uint8_t i;
1495 	uint8_t flid;
1496 
1497 	sc = vsc;
1498 	scctx = sc->vmx_scctx;
1499 	rxq = &sc->vmx_rxq[ri->iri_qsidx];
1500 	rxc = &rxq->vxrxq_comp_ring;
1501 
1502 	/*
1503 	 * Get a single packet starting at the given index in the completion
1504 	 * queue.  That we have been called indicates that
1505 	 * vmxnet3_isc_rxd_available() has already verified that either
1506 	 * there is a complete packet available starting at the given index,
1507 	 * or there are one or more zero length packets starting at the
1508 	 * given index followed by a complete packet, so no verification of
1509 	 * ownership of the descriptors (and no associated read barrier) is
1510 	 * required here.
1511 	 */
1512 	cqidx = ri->iri_cidx;
1513 	rxcd = &rxc->vxcr_u.rxcd[cqidx];
1514 	while (rxcd->len == 0) {
1515 		KASSERT(rxcd->sop && rxcd->eop,
1516 		    ("%s: zero-length packet without both sop and eop set",
1517 			__func__));
1518 		rxc->vxcr_zero_length++;
1519 		if (++cqidx == rxc->vxcr_ndesc) {
1520 			cqidx = 0;
1521 			rxc->vxcr_gen ^= 1;
1522 		}
1523 		rxcd = &rxc->vxcr_u.rxcd[cqidx];
1524 	}
1525 	KASSERT(rxcd->sop, ("%s: expected sop", __func__));
1526 
1527 	/*
1528 	 * RSS and flow ID.
1529 	 * Types other than M_HASHTYPE_NONE and M_HASHTYPE_OPAQUE_HASH should
1530 	 * be used only if the software RSS is enabled and it uses the same
1531 	 * algorithm and the hash key as the "hardware".  If the software RSS
1532 	 * is not enabled, then it's simply pointless to use those types.
1533 	 * If it's enabled but with different parameters, then hash values will
1534 	 * not match.
1535 	 */
1536 	ri->iri_flowid = rxcd->rss_hash;
1537 #ifdef RSS
1538 	if ((sc->vmx_flags & VMXNET3_FLAG_SOFT_RSS) != 0) {
1539 		switch (rxcd->rss_type) {
1540 		case VMXNET3_RCD_RSS_TYPE_NONE:
1541 			ri->iri_flowid = ri->iri_qsidx;
1542 			ri->iri_rsstype = M_HASHTYPE_NONE;
1543 			break;
1544 		case VMXNET3_RCD_RSS_TYPE_IPV4:
1545 			ri->iri_rsstype = M_HASHTYPE_RSS_IPV4;
1546 			break;
1547 		case VMXNET3_RCD_RSS_TYPE_TCPIPV4:
1548 			ri->iri_rsstype = M_HASHTYPE_RSS_TCP_IPV4;
1549 			break;
1550 		case VMXNET3_RCD_RSS_TYPE_IPV6:
1551 			ri->iri_rsstype = M_HASHTYPE_RSS_IPV6;
1552 			break;
1553 		case VMXNET3_RCD_RSS_TYPE_TCPIPV6:
1554 			ri->iri_rsstype = M_HASHTYPE_RSS_TCP_IPV6;
1555 			break;
1556 		default:
1557 			ri->iri_rsstype = M_HASHTYPE_OPAQUE_HASH;
1558 			break;
1559 		}
1560 	} else
1561 #endif
1562 	{
1563 		switch (rxcd->rss_type) {
1564 		case VMXNET3_RCD_RSS_TYPE_NONE:
1565 			ri->iri_flowid = ri->iri_qsidx;
1566 			ri->iri_rsstype = M_HASHTYPE_NONE;
1567 			break;
1568 		default:
1569 			ri->iri_rsstype = M_HASHTYPE_OPAQUE_HASH;
1570 			break;
1571 		}
1572 	}
1573 
1574 	/*
1575 	 * The queue numbering scheme used for rxcd->qid is as follows:
1576 	 *  - All of the command ring 0s are numbered [0, nrxqsets - 1]
1577 	 *  - All of the command ring 1s are numbered [nrxqsets, 2*nrxqsets - 1]
1578 	 *
1579 	 * Thus, rxcd->qid less than nrxqsets indicates command ring (and
1580 	 * flid) 0, and rxcd->qid greater than or equal to nrxqsets
1581 	 * indicates command ring (and flid) 1.
1582 	 */
1583 	nfrags = 0;
1584 	total_len = 0;
1585 	do {
1586 		rxcd = &rxc->vxcr_u.rxcd[cqidx];
1587 		KASSERT(rxcd->gen == rxc->vxcr_gen,
1588 		    ("%s: generation mismatch", __func__));
1589 		KASSERT(nfrags < IFLIB_MAX_RX_SEGS,
1590 		    ("%s: too many fragments", __func__));
1591 		if (__predict_true(rxcd->len != 0)) {
1592 			frag = &ri->iri_frags[nfrags];
1593 			flid = (rxcd->qid >= scctx->isc_nrxqsets) ? 1 : 0;
1594 			frag->irf_flid = flid;
1595 			frag->irf_idx = rxcd->rxd_idx;
1596 			frag->irf_len = rxcd->len;
1597 			total_len += rxcd->len;
1598 			nfrags++;
1599 		} else {
1600 			rxc->vcxr_zero_length_frag++;
1601 		}
1602 		if (++cqidx == rxc->vxcr_ndesc) {
1603 			cqidx = 0;
1604 			rxc->vxcr_gen ^= 1;
1605 		}
1606 	} while (!rxcd->eop);
1607 
1608 	ri->iri_cidx = cqidx;
1609 	ri->iri_nfrags = nfrags;
1610 	ri->iri_len = total_len;
1611 
1612 	/*
1613 	 * If there's an error, the last descriptor in the packet will
1614 	 * have the error indicator set.  In this case, set all
1615 	 * fragment lengths to zero.  This will cause iflib to discard
1616 	 * the packet, but process all associated descriptors through
1617 	 * the refill mechanism.
1618 	 */
1619 	if (__predict_false(rxcd->error)) {
1620 		rxc->vxcr_pkt_errors++;
1621 		for (i = 0; i < nfrags; i++) {
1622 			frag = &ri->iri_frags[i];
1623 			frag->irf_len = 0;
1624 		}
1625 	} else {
1626 		/* Checksum offload information is in the last descriptor. */
1627 		if (!rxcd->no_csum) {
1628 			uint32_t csum_flags = 0;
1629 
1630 			if (rxcd->ipv4) {
1631 				csum_flags |= CSUM_IP_CHECKED;
1632 				if (rxcd->ipcsum_ok)
1633 					csum_flags |= CSUM_IP_VALID;
1634 			}
1635 			if (!rxcd->fragment && (rxcd->tcp || rxcd->udp)) {
1636 				csum_flags |= CSUM_L4_CALC;
1637 				if (rxcd->csum_ok) {
1638 					csum_flags |= CSUM_L4_VALID;
1639 					ri->iri_csum_data = 0xffff;
1640 				}
1641 			}
1642 			ri->iri_csum_flags = csum_flags;
1643 		}
1644 
1645 		/* VLAN information is in the last descriptor. */
1646 		if (rxcd->vlan) {
1647 			ri->iri_flags |= M_VLANTAG;
1648 			ri->iri_vtag = rxcd->vtag;
1649 		}
1650 	}
1651 
1652 	return (0);
1653 }
1654 
1655 static void
vmxnet3_isc_rxd_refill(void * vsc,if_rxd_update_t iru)1656 vmxnet3_isc_rxd_refill(void *vsc, if_rxd_update_t iru)
1657 {
1658 	struct vmxnet3_softc *sc;
1659 	struct vmxnet3_rxqueue *rxq;
1660 	struct vmxnet3_rxring *rxr;
1661 	struct vmxnet3_rxdesc *rxd;
1662 	uint64_t *paddrs;
1663 	int count;
1664 	int len;
1665 	int idx;
1666 	int i;
1667 	uint8_t flid;
1668 	uint8_t btype;
1669 
1670 	count = iru->iru_count;
1671 	len = iru->iru_buf_size;
1672 	flid = iru->iru_flidx;
1673 	paddrs = iru->iru_paddrs;
1674 
1675 	sc = vsc;
1676 	rxq = &sc->vmx_rxq[iru->iru_qsidx];
1677 	rxr = &rxq->vxrxq_cmd_ring[flid];
1678 	rxd = rxr->vxrxr_rxd;
1679 
1680 	/*
1681 	 * Command ring 0 is filled with BTYPE_HEAD descriptors, and
1682 	 * command ring 1 is filled with BTYPE_BODY descriptors.
1683 	 */
1684 	btype = (flid == 0) ? VMXNET3_BTYPE_HEAD : VMXNET3_BTYPE_BODY;
1685 	/*
1686 	 * The refill entries from iflib will advance monotonically,
1687 	 * but the refilled descriptors may not be contiguous due to
1688 	 * earlier skipping of descriptors by the device.  The refill
1689 	 * entries from iflib need an entire state update, while the
1690 	 * descriptors previously skipped by the device only need to
1691 	 * have their generation numbers updated.
1692 	 */
1693 	idx = rxr->vxrxr_refill_start;
1694 	i = 0;
1695 	do {
1696 		if (idx == iru->iru_idxs[i]) {
1697 			rxd[idx].addr = paddrs[i];
1698 			rxd[idx].len = len;
1699 			rxd[idx].btype = btype;
1700 			i++;
1701 		} else
1702 			rxr->vxrxr_desc_skips++;
1703 		rxd[idx].gen = rxr->vxrxr_gen;
1704 
1705 		if (++idx == rxr->vxrxr_ndesc) {
1706 			idx = 0;
1707 			rxr->vxrxr_gen ^= 1;
1708 		}
1709 	} while (i != count);
1710 	rxr->vxrxr_refill_start = idx;
1711 }
1712 
1713 static void
vmxnet3_isc_rxd_flush(void * vsc,uint16_t rxqid,uint8_t flid,qidx_t pidx)1714 vmxnet3_isc_rxd_flush(void *vsc, uint16_t rxqid, uint8_t flid, qidx_t pidx)
1715 {
1716 	struct vmxnet3_softc *sc;
1717 	bus_size_t r;
1718 
1719 	sc = vsc;
1720 
1721 	if (flid == 0)
1722 		r = VMXNET3_BAR0_RXH1(rxqid);
1723 	else
1724 		r = VMXNET3_BAR0_RXH2(rxqid);
1725 
1726 	vmxnet3_write_bar0(sc, r, pidx);
1727 }
1728 
1729 static int
vmxnet3_legacy_intr(void * xsc)1730 vmxnet3_legacy_intr(void *xsc)
1731 {
1732 	struct vmxnet3_softc *sc;
1733 	if_softc_ctx_t scctx;
1734 	if_ctx_t ctx;
1735 
1736 	sc = xsc;
1737 	scctx = sc->vmx_scctx;
1738 	ctx = sc->vmx_ctx;
1739 
1740 	/*
1741 	 * When there is only a single interrupt configured, this routine
1742 	 * runs in fast interrupt context, following which the rxq 0 task
1743 	 * will be enqueued.
1744 	 */
1745 	if (scctx->isc_intr == IFLIB_INTR_LEGACY) {
1746 		if (vmxnet3_read_bar1(sc, VMXNET3_BAR1_INTR) == 0)
1747 			return (FILTER_HANDLED);
1748 	}
1749 	if (sc->vmx_intr_mask_mode == VMXNET3_IMM_ACTIVE)
1750 		vmxnet3_intr_disable_all(ctx);
1751 
1752 	if (sc->vmx_ds->event != 0)
1753 		iflib_admin_intr_deferred(ctx);
1754 
1755 	/*
1756 	 * XXX - When there is both rxq and event activity, do we care
1757 	 * whether the rxq 0 task or the admin task re-enables the interrupt
1758 	 * first?
1759 	 */
1760 	return (FILTER_SCHEDULE_THREAD);
1761 }
1762 
1763 static int
vmxnet3_rxq_intr(void * vrxq)1764 vmxnet3_rxq_intr(void *vrxq)
1765 {
1766 	struct vmxnet3_softc *sc;
1767 	struct vmxnet3_rxqueue *rxq;
1768 
1769 	rxq = vrxq;
1770 	sc = rxq->vxrxq_sc;
1771 
1772 	if (sc->vmx_intr_mask_mode == VMXNET3_IMM_ACTIVE)
1773 		vmxnet3_disable_intr(sc, rxq->vxrxq_intr_idx);
1774 
1775 	return (FILTER_SCHEDULE_THREAD);
1776 }
1777 
1778 static int
vmxnet3_event_intr(void * vsc)1779 vmxnet3_event_intr(void *vsc)
1780 {
1781 	struct vmxnet3_softc *sc;
1782 
1783 	sc = vsc;
1784 
1785 	if (sc->vmx_intr_mask_mode == VMXNET3_IMM_ACTIVE)
1786 		vmxnet3_disable_intr(sc, sc->vmx_event_intr_idx);
1787 
1788 	/*
1789 	 * The work will be done via vmxnet3_update_admin_status(), and the
1790 	 * interrupt will be re-enabled in vmxnet3_link_intr_enable().
1791 	 *
1792 	 * The interrupt will be re-enabled by vmxnet3_link_intr_enable().
1793 	 */
1794 	return (FILTER_SCHEDULE_THREAD);
1795 }
1796 
1797 static void
vmxnet3_stop(if_ctx_t ctx)1798 vmxnet3_stop(if_ctx_t ctx)
1799 {
1800 	struct vmxnet3_softc *sc;
1801 
1802 	sc = iflib_get_softc(ctx);
1803 
1804 	sc->vmx_link_active = 0;
1805 	vmxnet3_write_cmd(sc, VMXNET3_CMD_DISABLE);
1806 	vmxnet3_write_cmd(sc, VMXNET3_CMD_RESET);
1807 }
1808 
1809 static void
vmxnet3_txinit(struct vmxnet3_softc * sc,struct vmxnet3_txqueue * txq)1810 vmxnet3_txinit(struct vmxnet3_softc *sc, struct vmxnet3_txqueue *txq)
1811 {
1812 	struct vmxnet3_txring *txr;
1813 	struct vmxnet3_comp_ring *txc;
1814 
1815 	txq->vxtxq_last_flush = -1;
1816 
1817 	txr = &txq->vxtxq_cmd_ring;
1818 	txr->vxtxr_next = 0;
1819 	txr->vxtxr_gen = VMXNET3_INIT_GEN;
1820 	/*
1821 	 * iflib has zeroed out the descriptor array during the prior attach
1822 	 * or stop
1823 	 */
1824 
1825 	txc = &txq->vxtxq_comp_ring;
1826 	txc->vxcr_next = 0;
1827 	txc->vxcr_gen = VMXNET3_INIT_GEN;
1828 	/*
1829 	 * iflib has zeroed out the descriptor array during the prior attach
1830 	 * or stop
1831 	 */
1832 }
1833 
1834 static void
vmxnet3_rxinit(struct vmxnet3_softc * sc,struct vmxnet3_rxqueue * rxq)1835 vmxnet3_rxinit(struct vmxnet3_softc *sc, struct vmxnet3_rxqueue *rxq)
1836 {
1837 	struct vmxnet3_rxring *rxr;
1838 	struct vmxnet3_comp_ring *rxc;
1839 	int i;
1840 
1841 	/*
1842 	 * The descriptors will be populated with buffers during a
1843 	 * subsequent invocation of vmxnet3_isc_rxd_refill()
1844 	 */
1845 	for (i = 0; i < sc->vmx_sctx->isc_nrxqs - 1; i++) {
1846 		rxr = &rxq->vxrxq_cmd_ring[i];
1847 		rxr->vxrxr_gen = VMXNET3_INIT_GEN;
1848 		rxr->vxrxr_desc_skips = 0;
1849 		rxr->vxrxr_refill_start = 0;
1850 		/*
1851 		 * iflib has zeroed out the descriptor array during the
1852 		 * prior attach or stop
1853 		 */
1854 	}
1855 
1856 	for (/**/; i < VMXNET3_RXRINGS_PERQ; i++) {
1857 		rxr = &rxq->vxrxq_cmd_ring[i];
1858 		rxr->vxrxr_gen = 0;
1859 		rxr->vxrxr_desc_skips = 0;
1860 		rxr->vxrxr_refill_start = 0;
1861 		bzero(rxr->vxrxr_rxd,
1862 		    rxr->vxrxr_ndesc * sizeof(struct vmxnet3_rxdesc));
1863 	}
1864 
1865 	rxc = &rxq->vxrxq_comp_ring;
1866 	rxc->vxcr_next = 0;
1867 	rxc->vxcr_gen = VMXNET3_INIT_GEN;
1868 	rxc->vxcr_zero_length = 0;
1869 	rxc->vcxr_zero_length_frag = 0;
1870 	rxc->vxcr_pkt_errors = 0;
1871 	/*
1872 	 * iflib has zeroed out the descriptor array during the prior attach
1873 	 * or stop
1874 	 */
1875 }
1876 
1877 static void
vmxnet3_reinit_queues(struct vmxnet3_softc * sc)1878 vmxnet3_reinit_queues(struct vmxnet3_softc *sc)
1879 {
1880 	if_softc_ctx_t scctx;
1881 	int q;
1882 
1883 	scctx = sc->vmx_scctx;
1884 
1885 	for (q = 0; q < scctx->isc_ntxqsets; q++)
1886 		vmxnet3_txinit(sc, &sc->vmx_txq[q]);
1887 
1888 	for (q = 0; q < scctx->isc_nrxqsets; q++)
1889 		vmxnet3_rxinit(sc, &sc->vmx_rxq[q]);
1890 }
1891 
1892 static int
vmxnet3_enable_device(struct vmxnet3_softc * sc)1893 vmxnet3_enable_device(struct vmxnet3_softc *sc)
1894 {
1895 	if_softc_ctx_t scctx;
1896 	int q;
1897 
1898 	scctx = sc->vmx_scctx;
1899 
1900 	if (vmxnet3_read_cmd(sc, VMXNET3_CMD_ENABLE) != 0) {
1901 		device_printf(sc->vmx_dev, "device enable command failed!\n");
1902 		return (1);
1903 	}
1904 
1905 	/* Reset the Rx queue heads. */
1906 	for (q = 0; q < scctx->isc_nrxqsets; q++) {
1907 		vmxnet3_write_bar0(sc, VMXNET3_BAR0_RXH1(q), 0);
1908 		vmxnet3_write_bar0(sc, VMXNET3_BAR0_RXH2(q), 0);
1909 	}
1910 
1911 	return (0);
1912 }
1913 
1914 static void
vmxnet3_reinit_rxfilters(struct vmxnet3_softc * sc)1915 vmxnet3_reinit_rxfilters(struct vmxnet3_softc *sc)
1916 {
1917 	if_t ifp;
1918 
1919 	ifp = sc->vmx_ifp;
1920 
1921 	vmxnet3_set_rxfilter(sc, if_getflags(ifp));
1922 
1923 	if (if_getcapenable(ifp) & IFCAP_VLAN_HWFILTER)
1924 		bcopy(sc->vmx_vlan_filter, sc->vmx_ds->vlan_filter,
1925 		    sizeof(sc->vmx_ds->vlan_filter));
1926 	else
1927 		bzero(sc->vmx_ds->vlan_filter,
1928 		    sizeof(sc->vmx_ds->vlan_filter));
1929 	vmxnet3_write_cmd(sc, VMXNET3_CMD_VLAN_FILTER);
1930 }
1931 
1932 static void
vmxnet3_init(if_ctx_t ctx)1933 vmxnet3_init(if_ctx_t ctx)
1934 {
1935 	struct vmxnet3_softc *sc;
1936 
1937 	sc = iflib_get_softc(ctx);
1938 
1939 	/* Use the current MAC address. */
1940 	bcopy(if_getlladdr(sc->vmx_ifp), sc->vmx_lladdr, ETHER_ADDR_LEN);
1941 	vmxnet3_set_lladdr(sc);
1942 
1943 	vmxnet3_reinit_shared_data(sc);
1944 	vmxnet3_reinit_queues(sc);
1945 
1946 	vmxnet3_enable_device(sc);
1947 
1948 	vmxnet3_reinit_rxfilters(sc);
1949 	vmxnet3_link_status(sc);
1950 }
1951 
1952 static void
vmxnet3_multi_set(if_ctx_t ctx)1953 vmxnet3_multi_set(if_ctx_t ctx)
1954 {
1955 
1956 	vmxnet3_set_rxfilter(iflib_get_softc(ctx),
1957 	    if_getflags(iflib_get_ifp(ctx)));
1958 }
1959 
1960 static int
vmxnet3_mtu_set(if_ctx_t ctx,uint32_t mtu)1961 vmxnet3_mtu_set(if_ctx_t ctx, uint32_t mtu)
1962 {
1963 	struct vmxnet3_softc *sc;
1964 	if_softc_ctx_t scctx;
1965 
1966 	sc = iflib_get_softc(ctx);
1967 	scctx = sc->vmx_scctx;
1968 
1969 	if (mtu > VMXNET3_TX_MAXSIZE - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN +
1970 		ETHER_CRC_LEN))
1971 		return (EINVAL);
1972 
1973 	/*
1974 	 * Update the max frame size so that the rx mbuf size is
1975 	 * chosen based on the new mtu during the interface init that
1976 	 * will occur after this routine returns.
1977 	 */
1978 	scctx->isc_max_frame_size = mtu +
1979 		ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN + ETHER_CRC_LEN;
1980 	/* RX completion queue - n/a */
1981 	scctx->isc_rxd_buf_size[0] = 0;
1982 	/*
1983 	 * For header-type descriptors (used for first segment of
1984 	 * packet), let iflib determine the buffer size based on the
1985 	 * max frame size.
1986 	 */
1987 	scctx->isc_rxd_buf_size[1] = 0;
1988 	/*
1989 	 * For body-type descriptors (used for jumbo frames and LRO),
1990 	 * always use page-sized buffers.
1991 	 */
1992 	scctx->isc_rxd_buf_size[2] = MJUMPAGESIZE;
1993 
1994 	return (0);
1995 }
1996 
1997 static void
vmxnet3_media_status(if_ctx_t ctx,struct ifmediareq * ifmr)1998 vmxnet3_media_status(if_ctx_t ctx, struct ifmediareq * ifmr)
1999 {
2000 	struct vmxnet3_softc *sc;
2001 
2002 	sc = iflib_get_softc(ctx);
2003 
2004 	ifmr->ifm_status = IFM_AVALID;
2005 	ifmr->ifm_active = IFM_ETHER;
2006 
2007 	if (vmxnet3_link_is_up(sc) != 0) {
2008 		ifmr->ifm_status |= IFM_ACTIVE;
2009 		ifmr->ifm_active |= IFM_AUTO;
2010 	} else
2011 		ifmr->ifm_active |= IFM_NONE;
2012 }
2013 
2014 static int
vmxnet3_media_change(if_ctx_t ctx)2015 vmxnet3_media_change(if_ctx_t ctx)
2016 {
2017 
2018 	/* Ignore. */
2019 	return (0);
2020 }
2021 
2022 static int
vmxnet3_promisc_set(if_ctx_t ctx,int flags)2023 vmxnet3_promisc_set(if_ctx_t ctx, int flags)
2024 {
2025 
2026 	vmxnet3_set_rxfilter(iflib_get_softc(ctx), flags);
2027 
2028 	return (0);
2029 }
2030 
2031 static uint64_t
vmxnet3_get_counter(if_ctx_t ctx,ift_counter cnt)2032 vmxnet3_get_counter(if_ctx_t ctx, ift_counter cnt)
2033 {
2034 	if_t ifp = iflib_get_ifp(ctx);
2035 
2036 	if (cnt < IFCOUNTERS)
2037 		return if_get_counter_default(ifp, cnt);
2038 
2039 	return (0);
2040 }
2041 
2042 static void
vmxnet3_update_admin_status(if_ctx_t ctx)2043 vmxnet3_update_admin_status(if_ctx_t ctx)
2044 {
2045 	struct vmxnet3_softc *sc;
2046 
2047 	sc = iflib_get_softc(ctx);
2048 	/*
2049 	 * iflib may invoke this routine before vmxnet3_attach_post() has
2050 	 * run, which is before the top level shared data area is
2051 	 * initialized and the device made aware of it.
2052 	 */
2053 	if (sc->vmx_ds != NULL && sc->vmx_ds->event != 0)
2054 		vmxnet3_evintr(sc);
2055 
2056 	vmxnet3_refresh_host_stats(sc);
2057 }
2058 
2059 static void
vmxnet3_txq_timer(if_ctx_t ctx,uint16_t qid)2060 vmxnet3_txq_timer(if_ctx_t ctx, uint16_t qid)
2061 {
2062 	/* Host stats refresh is global, so just trigger it on txq 0 */
2063 	if (qid == 0)
2064 		vmxnet3_refresh_host_stats(iflib_get_softc(ctx));
2065 }
2066 
2067 static void
vmxnet3_update_vlan_filter(struct vmxnet3_softc * sc,int add,uint16_t tag)2068 vmxnet3_update_vlan_filter(struct vmxnet3_softc *sc, int add, uint16_t tag)
2069 {
2070 	int idx, bit;
2071 
2072 	if (tag == 0 || tag > 4095)
2073 		return;
2074 
2075 	idx = (tag >> 5) & 0x7F;
2076 	bit = tag & 0x1F;
2077 
2078 	/* Update our private VLAN bitvector. */
2079 	if (add)
2080 		sc->vmx_vlan_filter[idx] |= (1 << bit);
2081 	else
2082 		sc->vmx_vlan_filter[idx] &= ~(1 << bit);
2083 }
2084 
2085 static void
vmxnet3_vlan_register(if_ctx_t ctx,uint16_t tag)2086 vmxnet3_vlan_register(if_ctx_t ctx, uint16_t tag)
2087 {
2088 
2089 	vmxnet3_update_vlan_filter(iflib_get_softc(ctx), 1, tag);
2090 }
2091 
2092 static void
vmxnet3_vlan_unregister(if_ctx_t ctx,uint16_t tag)2093 vmxnet3_vlan_unregister(if_ctx_t ctx, uint16_t tag)
2094 {
2095 
2096 	vmxnet3_update_vlan_filter(iflib_get_softc(ctx), 0, tag);
2097 }
2098 
2099 static u_int
vmxnet3_hash_maddr(void * arg,struct sockaddr_dl * sdl,u_int count)2100 vmxnet3_hash_maddr(void *arg, struct sockaddr_dl *sdl, u_int count)
2101 {
2102 	struct vmxnet3_softc *sc = arg;
2103 
2104 	if (count < VMXNET3_MULTICAST_MAX)
2105 		bcopy(LLADDR(sdl), &sc->vmx_mcast[count * ETHER_ADDR_LEN],
2106 		    ETHER_ADDR_LEN);
2107 
2108 	return (1);
2109 }
2110 
2111 static void
vmxnet3_set_rxfilter(struct vmxnet3_softc * sc,int flags)2112 vmxnet3_set_rxfilter(struct vmxnet3_softc *sc, int flags)
2113 {
2114 	if_t ifp;
2115 	struct vmxnet3_driver_shared *ds;
2116 	u_int mode;
2117 
2118 	ifp = sc->vmx_ifp;
2119 	ds = sc->vmx_ds;
2120 
2121 	mode = VMXNET3_RXMODE_UCAST | VMXNET3_RXMODE_BCAST;
2122 	if (flags & IFF_PROMISC)
2123 		mode |= VMXNET3_RXMODE_PROMISC;
2124 	if (flags & IFF_ALLMULTI)
2125 		mode |= VMXNET3_RXMODE_ALLMULTI;
2126 	else {
2127 		int cnt;
2128 
2129 		cnt = if_foreach_llmaddr(ifp, vmxnet3_hash_maddr, sc);
2130 		if (cnt >= VMXNET3_MULTICAST_MAX) {
2131 			cnt = 0;
2132 			mode |= VMXNET3_RXMODE_ALLMULTI;
2133 		} else if (cnt > 0)
2134 			mode |= VMXNET3_RXMODE_MCAST;
2135 		ds->mcast_tablelen = cnt * ETHER_ADDR_LEN;
2136 	}
2137 
2138 	ds->rxmode = mode;
2139 
2140 	vmxnet3_write_cmd(sc, VMXNET3_CMD_SET_FILTER);
2141 	vmxnet3_write_cmd(sc, VMXNET3_CMD_SET_RXMODE);
2142 }
2143 
2144 static void
vmxnet3_refresh_host_stats(struct vmxnet3_softc * sc)2145 vmxnet3_refresh_host_stats(struct vmxnet3_softc *sc)
2146 {
2147 
2148 	vmxnet3_write_cmd(sc, VMXNET3_CMD_GET_STATS);
2149 }
2150 
2151 static int
vmxnet3_link_is_up(struct vmxnet3_softc * sc)2152 vmxnet3_link_is_up(struct vmxnet3_softc *sc)
2153 {
2154 	uint32_t status;
2155 
2156 	status = vmxnet3_read_cmd(sc, VMXNET3_CMD_GET_LINK);
2157 	return !!(status & 0x1);
2158 }
2159 
2160 static void
vmxnet3_link_status(struct vmxnet3_softc * sc)2161 vmxnet3_link_status(struct vmxnet3_softc *sc)
2162 {
2163 	if_ctx_t ctx;
2164 	uint64_t speed;
2165 	int link;
2166 
2167 	ctx = sc->vmx_ctx;
2168 	link = vmxnet3_link_is_up(sc);
2169 	speed = IF_Gbps(10);
2170 
2171 	if (link != 0 && sc->vmx_link_active == 0) {
2172 		sc->vmx_link_active = 1;
2173 		iflib_link_state_change(ctx, LINK_STATE_UP, speed);
2174 	} else if (link == 0 && sc->vmx_link_active != 0) {
2175 		sc->vmx_link_active = 0;
2176 		iflib_link_state_change(ctx, LINK_STATE_DOWN, speed);
2177 	}
2178 }
2179 
2180 static void
vmxnet3_set_lladdr(struct vmxnet3_softc * sc)2181 vmxnet3_set_lladdr(struct vmxnet3_softc *sc)
2182 {
2183 	uint32_t ml, mh;
2184 
2185 	ml  = sc->vmx_lladdr[0];
2186 	ml |= sc->vmx_lladdr[1] << 8;
2187 	ml |= sc->vmx_lladdr[2] << 16;
2188 	ml |= sc->vmx_lladdr[3] << 24;
2189 	vmxnet3_write_bar1(sc, VMXNET3_BAR1_MACL, ml);
2190 
2191 	mh  = sc->vmx_lladdr[4];
2192 	mh |= sc->vmx_lladdr[5] << 8;
2193 	vmxnet3_write_bar1(sc, VMXNET3_BAR1_MACH, mh);
2194 }
2195 
2196 static void
vmxnet3_get_lladdr(struct vmxnet3_softc * sc)2197 vmxnet3_get_lladdr(struct vmxnet3_softc *sc)
2198 {
2199 	uint32_t ml, mh;
2200 
2201 	ml = vmxnet3_read_cmd(sc, VMXNET3_CMD_GET_MACL);
2202 	mh = vmxnet3_read_cmd(sc, VMXNET3_CMD_GET_MACH);
2203 
2204 	sc->vmx_lladdr[0] = ml;
2205 	sc->vmx_lladdr[1] = ml >> 8;
2206 	sc->vmx_lladdr[2] = ml >> 16;
2207 	sc->vmx_lladdr[3] = ml >> 24;
2208 	sc->vmx_lladdr[4] = mh;
2209 	sc->vmx_lladdr[5] = mh >> 8;
2210 }
2211 
2212 static void
vmxnet3_setup_txq_sysctl(struct vmxnet3_txqueue * txq,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * child)2213 vmxnet3_setup_txq_sysctl(struct vmxnet3_txqueue *txq,
2214     struct sysctl_ctx_list *ctx, struct sysctl_oid_list *child)
2215 {
2216 	struct sysctl_oid *node, *txsnode;
2217 	struct sysctl_oid_list *list, *txslist;
2218 	struct UPT1_TxStats *txstats;
2219 	char namebuf[16];
2220 
2221 	txstats = &txq->vxtxq_ts->stats;
2222 
2223 	snprintf(namebuf, sizeof(namebuf), "txq%d", txq->vxtxq_id);
2224 	node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
2225 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Transmit Queue");
2226 	txq->vxtxq_sysctl = list = SYSCTL_CHILDREN(node);
2227 
2228 	/*
2229 	 * Add statistics reported by the host. These are updated by the
2230 	 * iflib txq timer on txq 0.
2231 	 */
2232 	txsnode = SYSCTL_ADD_NODE(ctx, list, OID_AUTO, "hstats",
2233 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Host Statistics");
2234 	txslist = SYSCTL_CHILDREN(txsnode);
2235 	SYSCTL_ADD_UQUAD(ctx, txslist, OID_AUTO, "tso_packets", CTLFLAG_RD,
2236 	    &txstats->TSO_packets, "TSO packets");
2237 	SYSCTL_ADD_UQUAD(ctx, txslist, OID_AUTO, "tso_bytes", CTLFLAG_RD,
2238 	    &txstats->TSO_bytes, "TSO bytes");
2239 	SYSCTL_ADD_UQUAD(ctx, txslist, OID_AUTO, "ucast_packets", CTLFLAG_RD,
2240 	    &txstats->ucast_packets, "Unicast packets");
2241 	SYSCTL_ADD_UQUAD(ctx, txslist, OID_AUTO, "unicast_bytes", CTLFLAG_RD,
2242 	    &txstats->ucast_bytes, "Unicast bytes");
2243 	SYSCTL_ADD_UQUAD(ctx, txslist, OID_AUTO, "mcast_packets", CTLFLAG_RD,
2244 	    &txstats->mcast_packets, "Multicast packets");
2245 	SYSCTL_ADD_UQUAD(ctx, txslist, OID_AUTO, "mcast_bytes", CTLFLAG_RD,
2246 	    &txstats->mcast_bytes, "Multicast bytes");
2247 	SYSCTL_ADD_UQUAD(ctx, txslist, OID_AUTO, "error", CTLFLAG_RD,
2248 	    &txstats->error, "Errors");
2249 	SYSCTL_ADD_UQUAD(ctx, txslist, OID_AUTO, "discard", CTLFLAG_RD,
2250 	    &txstats->discard, "Discards");
2251 }
2252 
2253 static void
vmxnet3_setup_rxq_sysctl(struct vmxnet3_rxqueue * rxq,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * child)2254 vmxnet3_setup_rxq_sysctl(struct vmxnet3_rxqueue *rxq,
2255     struct sysctl_ctx_list *ctx, struct sysctl_oid_list *child)
2256 {
2257 	struct sysctl_oid *node, *rxsnode;
2258 	struct sysctl_oid_list *list, *rxslist;
2259 	struct UPT1_RxStats *rxstats;
2260 	char namebuf[16];
2261 
2262 	rxstats = &rxq->vxrxq_rs->stats;
2263 
2264 	snprintf(namebuf, sizeof(namebuf), "rxq%d", rxq->vxrxq_id);
2265 	node = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, namebuf,
2266 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Receive Queue");
2267 	rxq->vxrxq_sysctl = list = SYSCTL_CHILDREN(node);
2268 
2269 	/*
2270 	 * Add statistics reported by the host. These are updated by the
2271 	 * iflib txq timer on txq 0.
2272 	 */
2273 	rxsnode = SYSCTL_ADD_NODE(ctx, list, OID_AUTO, "hstats",
2274 	    CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "Host Statistics");
2275 	rxslist = SYSCTL_CHILDREN(rxsnode);
2276 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "lro_packets", CTLFLAG_RD,
2277 	    &rxstats->LRO_packets, "LRO packets");
2278 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "lro_bytes", CTLFLAG_RD,
2279 	    &rxstats->LRO_bytes, "LRO bytes");
2280 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "ucast_packets", CTLFLAG_RD,
2281 	    &rxstats->ucast_packets, "Unicast packets");
2282 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "unicast_bytes", CTLFLAG_RD,
2283 	    &rxstats->ucast_bytes, "Unicast bytes");
2284 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "mcast_packets", CTLFLAG_RD,
2285 	    &rxstats->mcast_packets, "Multicast packets");
2286 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "mcast_bytes", CTLFLAG_RD,
2287 	    &rxstats->mcast_bytes, "Multicast bytes");
2288 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "bcast_packets", CTLFLAG_RD,
2289 	    &rxstats->bcast_packets, "Broadcast packets");
2290 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "bcast_bytes", CTLFLAG_RD,
2291 	    &rxstats->bcast_bytes, "Broadcast bytes");
2292 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "nobuffer", CTLFLAG_RD,
2293 	    &rxstats->nobuffer, "No buffer");
2294 	SYSCTL_ADD_UQUAD(ctx, rxslist, OID_AUTO, "error", CTLFLAG_RD,
2295 	    &rxstats->error, "Errors");
2296 }
2297 
2298 static void
vmxnet3_setup_debug_sysctl(struct vmxnet3_softc * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * child)2299 vmxnet3_setup_debug_sysctl(struct vmxnet3_softc *sc,
2300     struct sysctl_ctx_list *ctx, struct sysctl_oid_list *child)
2301 {
2302 	if_softc_ctx_t scctx;
2303 	struct sysctl_oid *node;
2304 	struct sysctl_oid_list *list;
2305 	int i;
2306 
2307 	scctx = sc->vmx_scctx;
2308 
2309 	for (i = 0; i < scctx->isc_ntxqsets; i++) {
2310 		struct vmxnet3_txqueue *txq = &sc->vmx_txq[i];
2311 
2312 		node = SYSCTL_ADD_NODE(ctx, txq->vxtxq_sysctl, OID_AUTO,
2313 		    "debug", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
2314 		list = SYSCTL_CHILDREN(node);
2315 
2316 		SYSCTL_ADD_UINT(ctx, list, OID_AUTO, "cmd_next", CTLFLAG_RD,
2317 		    &txq->vxtxq_cmd_ring.vxtxr_next, 0, "");
2318 		SYSCTL_ADD_UINT(ctx, list, OID_AUTO, "cmd_ndesc", CTLFLAG_RD,
2319 		    &txq->vxtxq_cmd_ring.vxtxr_ndesc, 0, "");
2320 		SYSCTL_ADD_INT(ctx, list, OID_AUTO, "cmd_gen", CTLFLAG_RD,
2321 		    &txq->vxtxq_cmd_ring.vxtxr_gen, 0, "");
2322 		SYSCTL_ADD_UINT(ctx, list, OID_AUTO, "comp_next", CTLFLAG_RD,
2323 		    &txq->vxtxq_comp_ring.vxcr_next, 0, "");
2324 		SYSCTL_ADD_UINT(ctx, list, OID_AUTO, "comp_ndesc", CTLFLAG_RD,
2325 		    &txq->vxtxq_comp_ring.vxcr_ndesc, 0,"");
2326 		SYSCTL_ADD_INT(ctx, list, OID_AUTO, "comp_gen", CTLFLAG_RD,
2327 		    &txq->vxtxq_comp_ring.vxcr_gen, 0, "");
2328 	}
2329 
2330 	for (i = 0; i < scctx->isc_nrxqsets; i++) {
2331 		struct vmxnet3_rxqueue *rxq = &sc->vmx_rxq[i];
2332 
2333 		node = SYSCTL_ADD_NODE(ctx, rxq->vxrxq_sysctl, OID_AUTO,
2334 		    "debug", CTLFLAG_RD | CTLFLAG_MPSAFE, NULL, "");
2335 		list = SYSCTL_CHILDREN(node);
2336 
2337 		SYSCTL_ADD_UINT(ctx, list, OID_AUTO, "cmd0_ndesc", CTLFLAG_RD,
2338 		    &rxq->vxrxq_cmd_ring[0].vxrxr_ndesc, 0, "");
2339 		SYSCTL_ADD_INT(ctx, list, OID_AUTO, "cmd0_gen", CTLFLAG_RD,
2340 		    &rxq->vxrxq_cmd_ring[0].vxrxr_gen, 0, "");
2341 		SYSCTL_ADD_U64(ctx, list, OID_AUTO, "cmd0_desc_skips", CTLFLAG_RD,
2342 		    &rxq->vxrxq_cmd_ring[0].vxrxr_desc_skips, 0, "");
2343 		SYSCTL_ADD_UINT(ctx, list, OID_AUTO, "cmd1_ndesc", CTLFLAG_RD,
2344 		    &rxq->vxrxq_cmd_ring[1].vxrxr_ndesc, 0, "");
2345 		SYSCTL_ADD_INT(ctx, list, OID_AUTO, "cmd1_gen", CTLFLAG_RD,
2346 		    &rxq->vxrxq_cmd_ring[1].vxrxr_gen, 0, "");
2347 		SYSCTL_ADD_U64(ctx, list, OID_AUTO, "cmd1_desc_skips", CTLFLAG_RD,
2348 		    &rxq->vxrxq_cmd_ring[1].vxrxr_desc_skips, 0, "");
2349 		SYSCTL_ADD_UINT(ctx, list, OID_AUTO, "comp_ndesc", CTLFLAG_RD,
2350 		    &rxq->vxrxq_comp_ring.vxcr_ndesc, 0,"");
2351 		SYSCTL_ADD_INT(ctx, list, OID_AUTO, "comp_gen", CTLFLAG_RD,
2352 		    &rxq->vxrxq_comp_ring.vxcr_gen, 0, "");
2353 		SYSCTL_ADD_U64(ctx, list, OID_AUTO, "comp_zero_length", CTLFLAG_RD,
2354 		    &rxq->vxrxq_comp_ring.vxcr_zero_length, 0, "");
2355 		SYSCTL_ADD_U64(ctx, list, OID_AUTO, "comp_zero_length_frag",
2356 		    CTLFLAG_RD, &rxq->vxrxq_comp_ring.vcxr_zero_length_frag,
2357 		    0, "");
2358 		SYSCTL_ADD_U64(ctx, list, OID_AUTO, "comp_pkt_errors", CTLFLAG_RD,
2359 		    &rxq->vxrxq_comp_ring.vxcr_pkt_errors, 0, "");
2360 	}
2361 }
2362 
2363 static void
vmxnet3_setup_queue_sysctl(struct vmxnet3_softc * sc,struct sysctl_ctx_list * ctx,struct sysctl_oid_list * child)2364 vmxnet3_setup_queue_sysctl(struct vmxnet3_softc *sc,
2365     struct sysctl_ctx_list *ctx, struct sysctl_oid_list *child)
2366 {
2367 	if_softc_ctx_t scctx;
2368 	int i;
2369 
2370 	scctx = sc->vmx_scctx;
2371 
2372 	for (i = 0; i < scctx->isc_ntxqsets; i++)
2373 		vmxnet3_setup_txq_sysctl(&sc->vmx_txq[i], ctx, child);
2374 	for (i = 0; i < scctx->isc_nrxqsets; i++)
2375 		vmxnet3_setup_rxq_sysctl(&sc->vmx_rxq[i], ctx, child);
2376 
2377 	vmxnet3_setup_debug_sysctl(sc, ctx, child);
2378 }
2379 
2380 static void
vmxnet3_setup_sysctl(struct vmxnet3_softc * sc)2381 vmxnet3_setup_sysctl(struct vmxnet3_softc *sc)
2382 {
2383 	device_t dev;
2384 	struct sysctl_ctx_list *ctx;
2385 	struct sysctl_oid *tree;
2386 	struct sysctl_oid_list *child;
2387 
2388 	dev = sc->vmx_dev;
2389 	ctx = device_get_sysctl_ctx(dev);
2390 	tree = device_get_sysctl_tree(dev);
2391 	child = SYSCTL_CHILDREN(tree);
2392 
2393 	vmxnet3_setup_queue_sysctl(sc, ctx, child);
2394 }
2395 
2396 static void
vmxnet3_write_bar0(struct vmxnet3_softc * sc,bus_size_t r,uint32_t v)2397 vmxnet3_write_bar0(struct vmxnet3_softc *sc, bus_size_t r, uint32_t v)
2398 {
2399 
2400 	bus_space_write_4(sc->vmx_iot0, sc->vmx_ioh0, r, v);
2401 }
2402 
2403 static uint32_t
vmxnet3_read_bar1(struct vmxnet3_softc * sc,bus_size_t r)2404 vmxnet3_read_bar1(struct vmxnet3_softc *sc, bus_size_t r)
2405 {
2406 
2407 	return (bus_space_read_4(sc->vmx_iot1, sc->vmx_ioh1, r));
2408 }
2409 
2410 static void
vmxnet3_write_bar1(struct vmxnet3_softc * sc,bus_size_t r,uint32_t v)2411 vmxnet3_write_bar1(struct vmxnet3_softc *sc, bus_size_t r, uint32_t v)
2412 {
2413 
2414 	bus_space_write_4(sc->vmx_iot1, sc->vmx_ioh1, r, v);
2415 }
2416 
2417 static void
vmxnet3_write_cmd(struct vmxnet3_softc * sc,uint32_t cmd)2418 vmxnet3_write_cmd(struct vmxnet3_softc *sc, uint32_t cmd)
2419 {
2420 
2421 	vmxnet3_write_bar1(sc, VMXNET3_BAR1_CMD, cmd);
2422 }
2423 
2424 static uint32_t
vmxnet3_read_cmd(struct vmxnet3_softc * sc,uint32_t cmd)2425 vmxnet3_read_cmd(struct vmxnet3_softc *sc, uint32_t cmd)
2426 {
2427 
2428 	vmxnet3_write_cmd(sc, cmd);
2429 	bus_space_barrier(sc->vmx_iot1, sc->vmx_ioh1, 0, 0,
2430 	    BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE);
2431 	return (vmxnet3_read_bar1(sc, VMXNET3_BAR1_CMD));
2432 }
2433 
2434 static void
vmxnet3_enable_intr(struct vmxnet3_softc * sc,int irq)2435 vmxnet3_enable_intr(struct vmxnet3_softc *sc, int irq)
2436 {
2437 
2438 	vmxnet3_write_bar0(sc, VMXNET3_BAR0_IMASK(irq), 0);
2439 }
2440 
2441 static void
vmxnet3_disable_intr(struct vmxnet3_softc * sc,int irq)2442 vmxnet3_disable_intr(struct vmxnet3_softc *sc, int irq)
2443 {
2444 
2445 	vmxnet3_write_bar0(sc, VMXNET3_BAR0_IMASK(irq), 1);
2446 }
2447 
2448 static int
vmxnet3_tx_queue_intr_enable(if_ctx_t ctx,uint16_t qid)2449 vmxnet3_tx_queue_intr_enable(if_ctx_t ctx, uint16_t qid)
2450 {
2451 	/* Not using interrupts for TX */
2452 	return (0);
2453 }
2454 
2455 static int
vmxnet3_rx_queue_intr_enable(if_ctx_t ctx,uint16_t qid)2456 vmxnet3_rx_queue_intr_enable(if_ctx_t ctx, uint16_t qid)
2457 {
2458 	struct vmxnet3_softc *sc;
2459 
2460 	sc = iflib_get_softc(ctx);
2461 	vmxnet3_enable_intr(sc, sc->vmx_rxq[qid].vxrxq_intr_idx);
2462 	return (0);
2463 }
2464 
2465 static void
vmxnet3_link_intr_enable(if_ctx_t ctx)2466 vmxnet3_link_intr_enable(if_ctx_t ctx)
2467 {
2468 	struct vmxnet3_softc *sc;
2469 
2470 	sc = iflib_get_softc(ctx);
2471 	vmxnet3_enable_intr(sc, sc->vmx_event_intr_idx);
2472 }
2473 
2474 static void
vmxnet3_intr_enable_all(if_ctx_t ctx)2475 vmxnet3_intr_enable_all(if_ctx_t ctx)
2476 {
2477 	struct vmxnet3_softc *sc;
2478 	if_softc_ctx_t scctx;
2479 	int i;
2480 
2481 	sc = iflib_get_softc(ctx);
2482 	scctx = sc->vmx_scctx;
2483 	sc->vmx_ds->ictrl &= ~VMXNET3_ICTRL_DISABLE_ALL;
2484 	for (i = 0; i < scctx->isc_vectors; i++)
2485 		vmxnet3_enable_intr(sc, i);
2486 }
2487 
2488 static void
vmxnet3_intr_disable_all(if_ctx_t ctx)2489 vmxnet3_intr_disable_all(if_ctx_t ctx)
2490 {
2491 	struct vmxnet3_softc *sc;
2492 	int i;
2493 
2494 	sc = iflib_get_softc(ctx);
2495 	/*
2496 	 * iflib may invoke this routine before vmxnet3_attach_post() has
2497 	 * run, which is before the top level shared data area is
2498 	 * initialized and the device made aware of it.
2499 	 */
2500 	if (sc->vmx_ds != NULL)
2501 		sc->vmx_ds->ictrl |= VMXNET3_ICTRL_DISABLE_ALL;
2502 	for (i = 0; i < VMXNET3_MAX_INTRS; i++)
2503 		vmxnet3_disable_intr(sc, i);
2504 }
2505 
2506 static bool
vmxnet3_if_needs_restart(if_ctx_t ctx __unused,enum iflib_restart_event event)2507 vmxnet3_if_needs_restart(if_ctx_t ctx __unused, enum iflib_restart_event event)
2508 {
2509 	switch (event) {
2510 	case IFLIB_RESTART_VLAN_CONFIG:
2511 		return (true);
2512 	default:
2513 		return (false);
2514 	}
2515 }
2516 
2517 /*
2518  * Since this is a purely paravirtualized device, we do not have
2519  * to worry about DMA coherency. But at times, we must make sure
2520  * both the compiler and CPU do not reorder memory operations.
2521  */
2522 static inline void
vmxnet3_barrier(struct vmxnet3_softc * sc,vmxnet3_barrier_t type)2523 vmxnet3_barrier(struct vmxnet3_softc *sc, vmxnet3_barrier_t type)
2524 {
2525 
2526 	switch (type) {
2527 	case VMXNET3_BARRIER_RD:
2528 		rmb();
2529 		break;
2530 	case VMXNET3_BARRIER_WR:
2531 		wmb();
2532 		break;
2533 	case VMXNET3_BARRIER_RDWR:
2534 		mb();
2535 		break;
2536 	default:
2537 		panic("%s: bad barrier type %d", __func__, type);
2538 	}
2539 }
2540