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 IAVF_VC_LOCK(_sc)                mtx_lock(&(_sc)->vc_mtx)
126#define IAVF_VC_UNLOCK(_sc)              mtx_unlock(&(_sc)->vc_mtx)
127#define IAVF_VC_LOCK_DESTROY(_sc)        mtx_destroy(&(_sc)->vc_mtx)
128#define IAVF_VC_TRYLOCK(_sc)             mtx_trylock(&(_sc)->vc_mtx)
129#define IAVF_VC_LOCK_ASSERT(_sc)         mtx_assert(&(_sc)->vc_mtx, MA_OWNED)
130
131/**
132 * @struct tx_ring
133 * @brief Transmit ring control struct
134 *
135 * Structure used to track the hardware Tx ring data.
136 */
137struct tx_ring {
138        struct iavf_tx_queue	*que;
139	u32			tail;
140	struct iavf_tx_desc	*tx_base;
141	u64			tx_paddr;
142	u32			packets;
143	u32			me;
144
145	/*
146	 * For reporting completed packet status
147	 * in descriptor writeback mdoe
148	 */
149	qidx_t			*tx_rsq;
150	qidx_t			tx_rs_cidx;
151	qidx_t			tx_rs_pidx;
152	qidx_t			tx_cidx_processed;
153
154	/* Used for Dynamic ITR calculation */
155	u32			bytes;
156	u32			itr;
157	u32			latency;
158
159	/* Soft Stats */
160	u64			tx_bytes;
161	u64			tx_packets;
162	u64			mss_too_small;
163};
164
165/**
166 * @struct rx_ring
167 * @brief Receive ring control struct
168 *
169 * Structure used to track the hardware Rx ring data.
170 */
171struct rx_ring {
172        struct iavf_rx_queue	*que;
173	union iavf_rx_desc	*rx_base;
174	uint64_t		rx_paddr;
175	bool			discard;
176	u32			itr;
177	u32			latency;
178	u32			mbuf_sz;
179	u32			tail;
180	u32			me;
181
182	/* Used for Dynamic ITR calculation */
183	u32			packets;
184	u32			bytes;
185
186	/* Soft stats */
187	u64			rx_packets;
188	u64			rx_bytes;
189	u64			desc_errs;
190};
191
192/**
193 * @struct iavf_tx_queue
194 * @brief Driver Tx queue structure
195 *
196 * Structure to track the Tx ring, IRQ, MSI-X vector, and some software stats
197 * for a Tx queue.
198 */
199struct iavf_tx_queue {
200	struct iavf_vsi		*vsi;
201	struct tx_ring		txr;
202	struct if_irq		que_irq;
203	u32			msix;
204
205	/* Stats */
206	u64			irqs;
207	u64			tso;
208	u32			pkt_too_small;
209};
210
211/**
212 * @struct iavf_rx_queue
213 * @brief Driver Rx queue structure
214 *
215 * Structure to track the Rx ring, IRQ, MSI-X vector, and some software stats
216 * for an Rx queue.
217 */
218struct iavf_rx_queue {
219	struct iavf_vsi		*vsi;
220	struct rx_ring		rxr;
221	struct if_irq		que_irq;
222	u32			msix;
223
224	/* Stats */
225	u64			irqs;
226};
227
228/**
229 * @struct iavf_vsi
230 * @brief Virtual Station Interface
231 *
232 * Data tracking a VSI for an iavf device.
233 */
234struct iavf_vsi {
235	if_ctx_t		ctx;
236	if_softc_ctx_t		shared;
237	if_t			ifp;
238	struct iavf_sc		*back;
239	device_t		dev;
240	struct iavf_hw		*hw;
241
242	int			id;
243	u16			num_rx_queues;
244	u16			num_tx_queues;
245	u32			rx_itr_setting;
246	u32			tx_itr_setting;
247	u16			max_frame_size;
248	bool			enable_head_writeback;
249
250	bool			link_active;
251
252	struct iavf_tx_queue	*tx_queues;
253	struct iavf_rx_queue	*rx_queues;
254	struct if_irq		irq;
255
256	u16			num_vlans;
257	u16			num_macs;
258
259	/* Per-VSI stats from hardware */
260	struct iavf_eth_stats	eth_stats;
261	struct iavf_eth_stats	eth_stats_offsets;
262	bool			stat_offsets_loaded;
263	/* VSI stat counters */
264	u64			ipackets;
265	u64			ierrors;
266	u64			opackets;
267	u64			oerrors;
268	u64			ibytes;
269	u64			obytes;
270	u64			imcasts;
271	u64			omcasts;
272	u64			iqdrops;
273	u64			oqdrops;
274	u64			noproto;
275
276	/* Misc. */
277	u64			flags;
278	struct sysctl_oid	*vsi_node;
279	struct sysctl_ctx_list  sysctl_ctx;
280};
281
282/**
283 * @struct iavf_mac_filter
284 * @brief MAC Address filter data
285 *
286 * Entry in the MAC filter list describing a MAC address filter used to
287 * program hardware to filter a specific MAC address.
288 */
289struct iavf_mac_filter {
290	SLIST_ENTRY(iavf_mac_filter)  next;
291	u8      macaddr[ETHER_ADDR_LEN];
292	u16     flags;
293};
294
295/**
296 * @struct mac_list
297 * @brief MAC filter list head
298 *
299 * List head type for a singly-linked list of MAC address filters.
300 */
301SLIST_HEAD(mac_list, iavf_mac_filter);
302
303/**
304 * @struct iavf_vlan_filter
305 * @brief VLAN filter data
306 *
307 * Entry in the VLAN filter list describing a VLAN filter used to
308 * program hardware to filter traffic on a specific VLAN.
309 */
310struct iavf_vlan_filter {
311	SLIST_ENTRY(iavf_vlan_filter)  next;
312	u16     vlan;
313	u16     flags;
314};
315
316/**
317 * @struct vlan_list
318 * @brief VLAN filter list head
319 *
320 * List head type for a singly-linked list of VLAN filters.
321 */
322SLIST_HEAD(vlan_list, iavf_vlan_filter);
323
324/**
325 * @struct iavf_sc
326 * @brief Main context structure for the iavf driver
327 *
328 * Software context structure used to store information about a single device
329 * that is loaded by the iavf driver.
330 */
331struct iavf_sc {
332	struct iavf_vsi		vsi;
333
334	struct iavf_hw		hw;
335	struct iavf_osdep	osdep;
336	device_t		dev;
337
338	struct resource		*pci_mem;
339
340	/* driver state flags, only access using atomic functions */
341	u32			state;
342
343	struct ifmedia		*media;
344	struct virtchnl_version_info version;
345	enum iavf_dbg_mask	dbg_mask;
346	u16			promisc_flags;
347
348	bool			link_up;
349	union {
350		enum virtchnl_link_speed link_speed;
351		u32		link_speed_adv;
352	};
353
354	/* Tunable settings */
355	int			tx_itr;
356	int			rx_itr;
357	int			dynamic_tx_itr;
358	int			dynamic_rx_itr;
359
360	/* Filter lists */
361	struct mac_list		*mac_filters;
362	struct vlan_list	*vlan_filters;
363
364	/* Virtual comm channel */
365	struct virtchnl_vf_resource *vf_res;
366	struct virtchnl_vsi_resource *vsi_res;
367
368	/* Misc stats maintained by the driver */
369	u64			admin_irq;
370
371	/* Buffer used for reading AQ responses */
372	u8			aq_buffer[IAVF_AQ_BUF_SZ];
373
374	/* State flag used in init/stop */
375	u32			queues_enabled;
376	u8			enable_queues_chan;
377	u8			disable_queues_chan;
378
379	/* For virtchnl message processing task */
380	struct task		vc_task;
381	struct taskqueue	*vc_tq;
382	char			vc_mtx_name[16];
383	struct mtx		vc_mtx;
384};
385
386/* Function prototypes */
387void		 iavf_init_tx_ring(struct iavf_vsi *vsi, struct iavf_tx_queue *que);
388void		 iavf_get_default_rss_key(u32 *);
389const char *	iavf_vc_stat_str(struct iavf_hw *hw,
390    enum virtchnl_status_code stat_err);
391void		iavf_init_tx_rsqs(struct iavf_vsi *vsi);
392void		iavf_init_tx_cidx(struct iavf_vsi *vsi);
393u64		iavf_max_vc_speed_to_value(u8 link_speeds);
394void		iavf_add_vsi_sysctls(device_t dev, struct iavf_vsi *vsi,
395		    struct sysctl_ctx_list *ctx, const char *sysctl_name);
396void		iavf_add_sysctls_eth_stats(struct sysctl_ctx_list *ctx,
397		    struct sysctl_oid_list *child,
398		    struct iavf_eth_stats *eth_stats);
399void		iavf_add_queues_sysctls(device_t dev, struct iavf_vsi *vsi);
400
401void	iavf_enable_intr(struct iavf_vsi *);
402void	iavf_disable_intr(struct iavf_vsi *);
403#endif /* _IAVF_IFLIB_H_ */
404