xref: /freebsd/sys/dev/iavf/iavf_iflib.h (revision d0b2dbfa)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2021, Intel Corporation
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *
8  *   1. Redistributions of source code must retain the above copyright notice,
9  *      this list of conditions and the following disclaimer.
10  *
11  *   2. Redistributions in binary form must reproduce the above copyright
12  *      notice, this list of conditions and the following disclaimer in the
13  *      documentation and/or other materials provided with the distribution.
14  *
15  *   3. Neither the name of the Intel Corporation nor the names of its
16  *      contributors may be used to endorse or promote products derived from
17  *      this software without specific prior written permission.
18  *
19  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  *  POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /**
33  * @file iavf_iflib.h
34  * @brief main header for the iflib driver
35  *
36  * Contains definitions for various driver structures used throughout the
37  * driver code. This header is used by the iflib implementation.
38  */
39 #ifndef _IAVF_IFLIB_H_
40 #define _IAVF_IFLIB_H_
41 
42 #include "iavf_opts.h"
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/buf_ring.h>
47 #include <sys/mbuf.h>
48 #include <sys/protosw.h>
49 #include <sys/socket.h>
50 #include <sys/malloc.h>
51 #include <sys/kernel.h>
52 #include <sys/module.h>
53 #include <sys/sockio.h>
54 #include <sys/eventhandler.h>
55 #include <sys/syslog.h>
56 
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_arp.h>
60 #include <net/bpf.h>
61 #include <net/if_dl.h>
62 #include <net/if_media.h>
63 
64 #include <net/bpf.h>
65 #include <net/if_types.h>
66 #include <net/if_vlan_var.h>
67 
68 #include <netinet/in_systm.h>
69 #include <netinet/in.h>
70 #include <netinet/if_ether.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip6.h>
73 #include <netinet/tcp.h>
74 #include <netinet/tcp_lro.h>
75 #include <netinet/udp.h>
76 #include <netinet/sctp.h>
77 
78 #include <machine/in_cksum.h>
79 
80 #include <sys/bus.h>
81 #include <sys/pciio.h>
82 #include <machine/bus.h>
83 #include <sys/rman.h>
84 #include <machine/resource.h>
85 #include <vm/vm.h>
86 #include <vm/pmap.h>
87 #include <machine/clock.h>
88 #include <dev/pci/pcivar.h>
89 #include <dev/pci/pcireg.h>
90 #include <sys/proc.h>
91 #include <sys/endian.h>
92 #include <sys/taskqueue.h>
93 #include <sys/pcpu.h>
94 #include <sys/smp.h>
95 #include <sys/sbuf.h>
96 #include <machine/smp.h>
97 #include <machine/stdarg.h>
98 #include <net/ethernet.h>
99 #include <net/iflib.h>
100 #include "ifdi_if.h"
101 
102 #include "iavf_lib.h"
103 
104 #define IAVF_CSUM_TCP \
105 	(CSUM_IP_TCP|CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP6_TCP)
106 #define IAVF_CSUM_UDP \
107 	(CSUM_IP_UDP|CSUM_IP6_UDP)
108 #define IAVF_CSUM_SCTP \
109 	(CSUM_IP_SCTP|CSUM_IP6_SCTP)
110 #define IAVF_CSUM_IPV4 \
111 	(CSUM_IP|CSUM_IP_TSO)
112 
113 #define IAVF_CAPS \
114 	(IFCAP_TSO4 | IFCAP_TSO6 | \
115 	 IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 | \
116 	 IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | \
117 	 IFCAP_VLAN_HWFILTER | IFCAP_VLAN_HWTSO | \
118 	 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM | \
119 	 IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU | IFCAP_LRO)
120 
121 #define iavf_sc_from_ctx(_ctx) \
122     ((struct iavf_sc *)iflib_get_softc(_ctx))
123 
124 /* Use the correct assert function for each lock type */
125 #define IFLIB_CTX_ASSERT(_ctx) \
126     sx_assert(iflib_ctx_lock_get(_ctx), SA_XLOCKED)
127 
128 #define IAVF_VC_LOCK(_sc)                mtx_lock(&(_sc)->vc_mtx)
129 #define IAVF_VC_UNLOCK(_sc)              mtx_unlock(&(_sc)->vc_mtx)
130 #define IAVF_VC_LOCK_DESTROY(_sc)        mtx_destroy(&(_sc)->vc_mtx)
131 #define IAVF_VC_TRYLOCK(_sc)             mtx_trylock(&(_sc)->vc_mtx)
132 #define IAVF_VC_LOCK_ASSERT(_sc)         mtx_assert(&(_sc)->vc_mtx, MA_OWNED)
133 
134 /**
135  * @struct tx_ring
136  * @brief Transmit ring control struct
137  *
138  * Structure used to track the hardware Tx ring data.
139  */
140 struct tx_ring {
141         struct iavf_tx_queue	*que;
142 	u32			tail;
143 	struct iavf_tx_desc	*tx_base;
144 	u64			tx_paddr;
145 	u32			packets;
146 	u32			me;
147 
148 	/*
149 	 * For reporting completed packet status
150 	 * in descriptor writeback mdoe
151 	 */
152 	qidx_t			*tx_rsq;
153 	qidx_t			tx_rs_cidx;
154 	qidx_t			tx_rs_pidx;
155 	qidx_t			tx_cidx_processed;
156 
157 	/* Used for Dynamic ITR calculation */
158 	u32			bytes;
159 	u32			itr;
160 	u32			latency;
161 
162 	/* Soft Stats */
163 	u64			tx_bytes;
164 	u64			tx_packets;
165 	u64			mss_too_small;
166 };
167 
168 /**
169  * @struct rx_ring
170  * @brief Receive ring control struct
171  *
172  * Structure used to track the hardware Rx ring data.
173  */
174 struct rx_ring {
175         struct iavf_rx_queue	*que;
176 	union iavf_rx_desc	*rx_base;
177 	uint64_t		rx_paddr;
178 	bool			discard;
179 	u32			itr;
180 	u32			latency;
181 	u32			mbuf_sz;
182 	u32			tail;
183 	u32			me;
184 
185 	/* Used for Dynamic ITR calculation */
186 	u32			packets;
187 	u32			bytes;
188 
189 	/* Soft stats */
190 	u64			rx_packets;
191 	u64			rx_bytes;
192 	u64			desc_errs;
193 };
194 
195 /**
196  * @struct iavf_tx_queue
197  * @brief Driver Tx queue structure
198  *
199  * Structure to track the Tx ring, IRQ, MSI-X vector, and some software stats
200  * for a Tx queue.
201  */
202 struct iavf_tx_queue {
203 	struct iavf_vsi		*vsi;
204 	struct tx_ring		txr;
205 	struct if_irq		que_irq;
206 	u32			msix;
207 
208 	/* Stats */
209 	u64			irqs;
210 	u64			tso;
211 	u32			pkt_too_small;
212 };
213 
214 /**
215  * @struct iavf_rx_queue
216  * @brief Driver Rx queue structure
217  *
218  * Structure to track the Rx ring, IRQ, MSI-X vector, and some software stats
219  * for an Rx queue.
220  */
221 struct iavf_rx_queue {
222 	struct iavf_vsi		*vsi;
223 	struct rx_ring		rxr;
224 	struct if_irq		que_irq;
225 	u32			msix;
226 
227 	/* Stats */
228 	u64			irqs;
229 };
230 
231 /**
232  * @struct iavf_vsi
233  * @brief Virtual Station Interface
234  *
235  * Data tracking a VSI for an iavf device.
236  */
237 struct iavf_vsi {
238 	if_ctx_t		ctx;
239 	if_softc_ctx_t		shared;
240 	if_t			ifp;
241 	struct iavf_sc		*back;
242 	device_t		dev;
243 	struct iavf_hw		*hw;
244 
245 	int			id;
246 	u16			num_rx_queues;
247 	u16			num_tx_queues;
248 	u32			rx_itr_setting;
249 	u32			tx_itr_setting;
250 	u16			max_frame_size;
251 	bool			enable_head_writeback;
252 
253 	bool			link_active;
254 
255 	struct iavf_tx_queue	*tx_queues;
256 	struct iavf_rx_queue	*rx_queues;
257 	struct if_irq		irq;
258 
259 	u16			num_vlans;
260 	u16			num_macs;
261 
262 	/* Per-VSI stats from hardware */
263 	struct iavf_eth_stats	eth_stats;
264 	struct iavf_eth_stats	eth_stats_offsets;
265 	bool			stat_offsets_loaded;
266 	/* VSI stat counters */
267 	u64			ipackets;
268 	u64			ierrors;
269 	u64			opackets;
270 	u64			oerrors;
271 	u64			ibytes;
272 	u64			obytes;
273 	u64			imcasts;
274 	u64			omcasts;
275 	u64			iqdrops;
276 	u64			oqdrops;
277 	u64			noproto;
278 
279 	/* Misc. */
280 	u64			flags;
281 	struct sysctl_oid	*vsi_node;
282 	struct sysctl_ctx_list  sysctl_ctx;
283 };
284 
285 /**
286  * @struct iavf_mac_filter
287  * @brief MAC Address filter data
288  *
289  * Entry in the MAC filter list describing a MAC address filter used to
290  * program hardware to filter a specific MAC address.
291  */
292 struct iavf_mac_filter {
293 	SLIST_ENTRY(iavf_mac_filter)  next;
294 	u8      macaddr[ETHER_ADDR_LEN];
295 	u16     flags;
296 };
297 
298 /**
299  * @struct mac_list
300  * @brief MAC filter list head
301  *
302  * List head type for a singly-linked list of MAC address filters.
303  */
304 SLIST_HEAD(mac_list, iavf_mac_filter);
305 
306 /**
307  * @struct iavf_vlan_filter
308  * @brief VLAN filter data
309  *
310  * Entry in the VLAN filter list describing a VLAN filter used to
311  * program hardware to filter traffic on a specific VLAN.
312  */
313 struct iavf_vlan_filter {
314 	SLIST_ENTRY(iavf_vlan_filter)  next;
315 	u16     vlan;
316 	u16     flags;
317 };
318 
319 /**
320  * @struct vlan_list
321  * @brief VLAN filter list head
322  *
323  * List head type for a singly-linked list of VLAN filters.
324  */
325 SLIST_HEAD(vlan_list, iavf_vlan_filter);
326 
327 /**
328  * @struct iavf_sc
329  * @brief Main context structure for the iavf driver
330  *
331  * Software context structure used to store information about a single device
332  * that is loaded by the iavf driver.
333  */
334 struct iavf_sc {
335 	struct iavf_vsi		vsi;
336 
337 	struct iavf_hw		hw;
338 	struct iavf_osdep	osdep;
339 	device_t		dev;
340 
341 	struct resource		*pci_mem;
342 
343 	/* driver state flags, only access using atomic functions */
344 	u32			state;
345 
346 	struct ifmedia		*media;
347 	struct virtchnl_version_info version;
348 	enum iavf_dbg_mask	dbg_mask;
349 	u16			promisc_flags;
350 
351 	bool			link_up;
352 	union {
353 		enum virtchnl_link_speed link_speed;
354 		u32		link_speed_adv;
355 	};
356 
357 	/* Tunable settings */
358 	int			tx_itr;
359 	int			rx_itr;
360 	int			dynamic_tx_itr;
361 	int			dynamic_rx_itr;
362 
363 	/* Filter lists */
364 	struct mac_list		*mac_filters;
365 	struct vlan_list	*vlan_filters;
366 
367 	/* Virtual comm channel */
368 	struct virtchnl_vf_resource *vf_res;
369 	struct virtchnl_vsi_resource *vsi_res;
370 
371 	/* Misc stats maintained by the driver */
372 	u64			admin_irq;
373 
374 	/* Buffer used for reading AQ responses */
375 	u8			aq_buffer[IAVF_AQ_BUF_SZ];
376 
377 	/* State flag used in init/stop */
378 	u32			queues_enabled;
379 	u8			enable_queues_chan;
380 	u8			disable_queues_chan;
381 
382 	/* For virtchnl message processing task */
383 	struct task		vc_task;
384 	struct taskqueue	*vc_tq;
385 	char			vc_mtx_name[16];
386 	struct mtx		vc_mtx;
387 };
388 
389 /* Function prototypes */
390 void		 iavf_init_tx_ring(struct iavf_vsi *vsi, struct iavf_tx_queue *que);
391 void		 iavf_get_default_rss_key(u32 *);
392 const char *	iavf_vc_stat_str(struct iavf_hw *hw,
393     enum virtchnl_status_code stat_err);
394 void		iavf_init_tx_rsqs(struct iavf_vsi *vsi);
395 void		iavf_init_tx_cidx(struct iavf_vsi *vsi);
396 u64		iavf_max_vc_speed_to_value(u8 link_speeds);
397 void		iavf_add_vsi_sysctls(device_t dev, struct iavf_vsi *vsi,
398 		    struct sysctl_ctx_list *ctx, const char *sysctl_name);
399 void		iavf_add_sysctls_eth_stats(struct sysctl_ctx_list *ctx,
400 		    struct sysctl_oid_list *child,
401 		    struct iavf_eth_stats *eth_stats);
402 void		iavf_add_queues_sysctls(device_t dev, struct iavf_vsi *vsi);
403 
404 void	iavf_enable_intr(struct iavf_vsi *);
405 void	iavf_disable_intr(struct iavf_vsi *);
406 #endif /* _IAVF_IFLIB_H_ */
407