xref: /freebsd/sys/dev/iavf/iavf_lib.h (revision 02e9120893770924227138ba49df1edb3896112a)
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_lib.h
34  * @brief header for structures and functions common to legacy and iflib
35  *
36  * Contains definitions and function declarations which are shared between the
37  * legacy and iflib driver implementation.
38  */
39 #ifndef _IAVF_LIB_H_
40 #define _IAVF_LIB_H_
41 
42 #include <sys/malloc.h>
43 #include <machine/stdarg.h>
44 #include <sys/sysctl.h>
45 #ifdef RSS
46 #include <net/rss_config.h>
47 #endif
48 
49 #include "iavf_debug.h"
50 #include "iavf_osdep.h"
51 #include "iavf_type.h"
52 #include "iavf_prototype.h"
53 
54 MALLOC_DECLARE(M_IAVF);
55 
56 /*
57  * Ring Descriptors Valid Range: 32-4096 Default Value: 1024 This value is the
58  * number of tx/rx descriptors allocated by the driver. Increasing this
59  * value allows the driver to queue more operations.
60  *
61  * Tx descriptors are always 16 bytes, but Rx descriptors can be 32 bytes.
62  * The driver currently always uses 32 byte Rx descriptors.
63  */
64 #define IAVF_DEFAULT_RING	1024
65 #define IAVF_MAX_RING		4096
66 #define IAVF_MIN_RING		64
67 #define IAVF_RING_INCREMENT	32
68 
69 #define IAVF_AQ_LEN		256
70 #define IAVF_AQ_LEN_MAX		1024
71 
72 /*
73 ** Default number of entries in Tx queue buf_ring.
74 */
75 #define DEFAULT_TXBRSZ		4096
76 
77 /* Alignment for rings */
78 #define DBA_ALIGN		128
79 
80 /*
81  * Max number of multicast MAC addrs added to the driver's
82  * internal lists before converting to promiscuous mode
83  */
84 #define MAX_MULTICAST_ADDR	128
85 
86 /* Byte alignment for Tx/Rx descriptor rings */
87 #define DBA_ALIGN		128
88 
89 #define IAVF_MSIX_BAR		3
90 #define IAVF_ADM_LIMIT		2
91 #define IAVF_TSO_SIZE		((255*1024)-1)
92 #define IAVF_AQ_BUF_SZ		((u32) 4096)
93 #define IAVF_RX_HDR		128
94 #define IAVF_RX_LIMIT		512
95 #define IAVF_RX_ITR		0
96 #define IAVF_TX_ITR		1
97 /**
98  * The maximum packet length allowed to be sent or received by the adapter.
99  */
100 #define IAVF_MAX_FRAME		9728
101 /**
102  * The minimum packet length allowed to be sent by the adapter.
103  */
104 #define IAVF_MIN_FRAME		17
105 #define IAVF_MAX_TX_SEGS	8
106 #define IAVF_MAX_RX_SEGS	5
107 #define IAVF_MAX_TSO_SEGS	128
108 #define IAVF_SPARSE_CHAIN	7
109 #define IAVF_MIN_TSO_MSS	64
110 #define IAVF_MAX_TSO_MSS	9668
111 #define IAVF_MAX_DMA_SEG_SIZE	((16 * 1024) - 1)
112 #define IAVF_AQ_MAX_ERR		30
113 #define IAVF_MAX_INIT_WAIT	120
114 #define IAVF_AQ_TIMEOUT		(1 * hz)
115 #define IAVF_ADV_LINK_SPEED_SCALE	((u64)1000000)
116 #define IAVF_MAX_DIS_Q_RETRY	10
117 
118 #define IAVF_RSS_KEY_SIZE_REG		13
119 #define IAVF_RSS_KEY_SIZE		(IAVF_RSS_KEY_SIZE_REG * 4)
120 #define IAVF_RSS_VSI_LUT_SIZE		64	/* X722 -> VSI, X710 -> VF */
121 #define IAVF_RSS_VSI_LUT_ENTRY_MASK	0x3F
122 #define IAVF_RSS_VF_LUT_ENTRY_MASK	0xF
123 
124 /* Maximum MTU size */
125 #define IAVF_MAX_MTU (IAVF_MAX_FRAME - \
126 		     ETHER_HDR_LEN - ETHER_CRC_LEN - ETHER_VLAN_ENCAP_LEN)
127 
128 /*
129  * Hardware requires that TSO packets have an segment size of at least 64
130  * bytes. To avoid sending bad frames to the hardware, the driver forces the
131  * MSS for all TSO packets to have a segment size of at least 64 bytes.
132  *
133  * However, if the MTU is reduced below a certain size, then the resulting
134  * larger MSS can result in transmitting segmented frames with a packet size
135  * larger than the MTU.
136  *
137  * Avoid this by preventing the MTU from being lowered below this limit.
138  * Alternative solutions require changing the TCP stack to disable offloading
139  * the segmentation when the requested segment size goes below 64 bytes.
140  */
141 #define IAVF_MIN_MTU 112
142 
143 /*
144  * Interrupt Moderation parameters
145  * Multiply ITR values by 2 for real ITR value
146  */
147 #define IAVF_MAX_ITR		0x0FF0
148 #define IAVF_ITR_100K		0x0005
149 #define IAVF_ITR_20K		0x0019
150 #define IAVF_ITR_8K		0x003E
151 #define IAVF_ITR_4K		0x007A
152 #define IAVF_ITR_1K		0x01F4
153 #define IAVF_ITR_DYNAMIC	0x8000
154 #define IAVF_LOW_LATENCY	0
155 #define IAVF_AVE_LATENCY	1
156 #define IAVF_BULK_LATENCY	2
157 
158 /* MacVlan Flags */
159 #define IAVF_FILTER_USED	(u16)(1 << 0)
160 #define IAVF_FILTER_VLAN	(u16)(1 << 1)
161 #define IAVF_FILTER_ADD		(u16)(1 << 2)
162 #define IAVF_FILTER_DEL		(u16)(1 << 3)
163 #define IAVF_FILTER_MC		(u16)(1 << 4)
164 /* used in the vlan field of the filter when not a vlan */
165 #define IAVF_VLAN_ANY		-1
166 
167 #define CSUM_OFFLOAD_IPV4	(CSUM_IP|CSUM_TCP|CSUM_UDP|CSUM_SCTP)
168 #define CSUM_OFFLOAD_IPV6	(CSUM_TCP_IPV6|CSUM_UDP_IPV6|CSUM_SCTP_IPV6)
169 #define CSUM_OFFLOAD		(CSUM_OFFLOAD_IPV4|CSUM_OFFLOAD_IPV6|CSUM_TSO)
170 
171 /* Misc flags for iavf_vsi.flags */
172 #define IAVF_FLAGS_KEEP_TSO4	(1 << 0)
173 #define IAVF_FLAGS_KEEP_TSO6	(1 << 1)
174 
175 #define IAVF_DEFAULT_RSS_HENA_BASE (\
176 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_UDP) |	\
177 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_TCP) |	\
178 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_SCTP) |	\
179 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_OTHER) |	\
180 	BIT_ULL(IAVF_FILTER_PCTYPE_FRAG_IPV4) |		\
181 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_UDP) |	\
182 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_TCP) |	\
183 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_SCTP) |	\
184 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_OTHER) |	\
185 	BIT_ULL(IAVF_FILTER_PCTYPE_FRAG_IPV6))
186 
187 #define IAVF_DEFAULT_ADV_RSS_HENA (\
188 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_UNICAST_IPV4_UDP) | \
189 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_MULTICAST_IPV4_UDP) | \
190 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_UNICAST_IPV6_UDP) | \
191 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_MULTICAST_IPV6_UDP) | \
192 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV4_TCP_SYN_NO_ACK) | \
193 	BIT_ULL(IAVF_FILTER_PCTYPE_NONF_IPV6_TCP_SYN_NO_ACK))
194 
195 #define IAVF_DEFAULT_RSS_HENA_XL710 (\
196 	IAVF_DEFAULT_RSS_HENA_BASE |			\
197 	BIT_ULL(IAVF_FILTER_PCTYPE_L2_PAYLOAD))
198 
199 #define IAVF_DEFAULT_RSS_HENA_X722 (\
200 	IAVF_DEFAULT_RSS_HENA_XL710 |			\
201 	IAVF_DEFAULT_ADV_RSS_HENA)
202 
203 #define IAVF_DEFAULT_RSS_HENA_AVF (\
204 	IAVF_DEFAULT_RSS_HENA_BASE |			\
205 	IAVF_DEFAULT_ADV_RSS_HENA)
206 
207 /* For stats sysctl naming */
208 #define IAVF_QUEUE_NAME_LEN 32
209 
210 #define IAVF_FLAG_AQ_ENABLE_QUEUES            (u32)(1 << 0)
211 #define IAVF_FLAG_AQ_DISABLE_QUEUES           (u32)(1 << 1)
212 #define IAVF_FLAG_AQ_ADD_MAC_FILTER           (u32)(1 << 2)
213 #define IAVF_FLAG_AQ_ADD_VLAN_FILTER          (u32)(1 << 3)
214 #define IAVF_FLAG_AQ_DEL_MAC_FILTER           (u32)(1 << 4)
215 #define IAVF_FLAG_AQ_DEL_VLAN_FILTER          (u32)(1 << 5)
216 #define IAVF_FLAG_AQ_CONFIGURE_QUEUES         (u32)(1 << 6)
217 #define IAVF_FLAG_AQ_MAP_VECTORS              (u32)(1 << 7)
218 #define IAVF_FLAG_AQ_HANDLE_RESET             (u32)(1 << 8)
219 #define IAVF_FLAG_AQ_CONFIGURE_PROMISC        (u32)(1 << 9)
220 #define IAVF_FLAG_AQ_GET_STATS                (u32)(1 << 10)
221 #define IAVF_FLAG_AQ_CONFIG_RSS_KEY           (u32)(1 << 11)
222 #define IAVF_FLAG_AQ_SET_RSS_HENA             (u32)(1 << 12)
223 #define IAVF_FLAG_AQ_GET_RSS_HENA_CAPS        (u32)(1 << 13)
224 #define IAVF_FLAG_AQ_CONFIG_RSS_LUT           (u32)(1 << 14)
225 
226 #define IAVF_CAP_ADV_LINK_SPEED(_sc) \
227     ((_sc)->vf_res->vf_cap_flags & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
228 
229 #define IAVF_NRXQS(_vsi) ((_vsi)->num_rx_queues)
230 #define IAVF_NTXQS(_vsi) ((_vsi)->num_tx_queues)
231 
232 /**
233  * printf %b flag args
234  */
235 #define IAVF_FLAGS \
236     "\20\1ENABLE_QUEUES\2DISABLE_QUEUES\3ADD_MAC_FILTER" \
237     "\4ADD_VLAN_FILTER\5DEL_MAC_FILTER\6DEL_VLAN_FILTER" \
238     "\7CONFIGURE_QUEUES\10MAP_VECTORS\11HANDLE_RESET" \
239     "\12CONFIGURE_PROMISC\13GET_STATS\14CONFIG_RSS_KEY" \
240     "\15SET_RSS_HENA\16GET_RSS_HENA_CAPS\17CONFIG_RSS_LUT"
241 /**
242  * printf %b flag args for offloads from virtchnl.h
243  */
244 #define IAVF_PRINTF_VF_OFFLOAD_FLAGS \
245     "\20\1L2" \
246     "\2IWARP" \
247     "\3FCOE" \
248     "\4RSS_AQ" \
249     "\5RSS_REG" \
250     "\6WB_ON_ITR" \
251     "\7REQ_QUEUES" \
252     "\10ADV_LINK_SPEED" \
253     "\21VLAN" \
254     "\22RX_POLLING" \
255     "\23RSS_PCTYPE_V2" \
256     "\24RSS_PF" \
257     "\25ENCAP" \
258     "\26ENCAP_CSUM" \
259     "\27RX_ENCAP_CSUM" \
260     "\30ADQ"
261 
262 /**
263  * @enum iavf_ext_link_speed
264  * @brief Extended link speed enumeration
265  *
266  * Enumeration of possible link speeds that the device could be operating in.
267  * Contains an extended list compared to the virtchnl_link_speed, including
268  * additional higher speeds such as 50GB, and 100GB.
269  *
270  * The enumeration is used to convert between the old virtchnl_link_speed, the
271  * newer advanced speed reporting value specified in Mb/s, and the ifmedia
272  * link speeds reported to the operating system.
273  */
274 enum iavf_ext_link_speed {
275 	IAVF_EXT_LINK_SPEED_UNKNOWN,
276 	IAVF_EXT_LINK_SPEED_10MB,
277 	IAVF_EXT_LINK_SPEED_100MB,
278 	IAVF_EXT_LINK_SPEED_1000MB,
279 	IAVF_EXT_LINK_SPEED_2500MB,
280 	IAVF_EXT_LINK_SPEED_5GB,
281 	IAVF_EXT_LINK_SPEED_10GB,
282 	IAVF_EXT_LINK_SPEED_20GB,
283 	IAVF_EXT_LINK_SPEED_25GB,
284 	IAVF_EXT_LINK_SPEED_40GB,
285 	IAVF_EXT_LINK_SPEED_50GB,
286 	IAVF_EXT_LINK_SPEED_100GB,
287 };
288 
289 /**
290  * @struct iavf_sysctl_info
291  * @brief sysctl statistic info
292  *
293  * Structure describing a single statistics sysctl, used for reporting
294  * specific hardware and software statistics via the sysctl interface.
295  */
296 struct iavf_sysctl_info {
297 	u64	*stat;
298 	char	*name;
299 	char	*description;
300 };
301 
302 /* Forward struct declarations */
303 struct iavf_sc;
304 struct iavf_vsi;
305 
306 /**
307  * @enum iavf_state
308  * @brief Driver state flags
309  *
310  * Used to indicate the status of various driver events. Intended to be
311  * modified only using atomic operations, so that we can use it even in places
312  * which aren't locked.
313  */
314 enum iavf_state {
315 	IAVF_STATE_INITIALIZED,
316 	IAVF_STATE_RESET_REQUIRED,
317 	IAVF_STATE_RESET_PENDING,
318 	IAVF_STATE_RUNNING,
319 	/* This entry must be last */
320 	IAVF_STATE_LAST,
321 };
322 
323 /* Functions for setting and checking driver state. Note the functions take
324  * bit positions, not bitmasks. The atomic_testandset_32 and
325  * atomic_testandclear_32 operations require bit positions, while the
326  * atomic_set_32 and atomic_clear_32 require bitmasks. This can easily lead to
327  * programming error, so we provide wrapper functions to avoid this.
328  */
329 
330 /**
331  * iavf_set_state - Set the specified state
332  * @s: the state bitmap
333  * @bit: the state to set
334  *
335  * Atomically update the state bitmap with the specified bit set.
336  */
337 static inline void
338 iavf_set_state(volatile u32 *s, enum iavf_state bit)
339 {
340 	/* atomic_set_32 expects a bitmask */
341 	atomic_set_32(s, BIT(bit));
342 }
343 
344 /**
345  * iavf_clear_state - Clear the specified state
346  * @s: the state bitmap
347  * @bit: the state to clear
348  *
349  * Atomically update the state bitmap with the specified bit cleared.
350  */
351 static inline void
352 iavf_clear_state(volatile u32 *s, enum iavf_state bit)
353 {
354 	/* atomic_clear_32 expects a bitmask */
355 	atomic_clear_32(s, BIT(bit));
356 }
357 
358 /**
359  * iavf_testandset_state - Test and set the specified state
360  * @s: the state bitmap
361  * @bit: the bit to test
362  *
363  * Atomically update the state bitmap, setting the specified bit.
364  *
365  * @returns the previous value of the bit.
366  */
367 static inline u32
368 iavf_testandset_state(volatile u32 *s, enum iavf_state bit)
369 {
370 	/* atomic_testandset_32 expects a bit position */
371 	return atomic_testandset_32(s, bit);
372 }
373 
374 /**
375  * iavf_testandclear_state - Test and clear the specified state
376  * @s: the state bitmap
377  * @bit: the bit to test
378  *
379  * Atomically update the state bitmap, clearing the specified bit.
380  *
381  * @returns the previous value of the bit.
382  */
383 static inline u32
384 iavf_testandclear_state(volatile u32 *s, enum iavf_state bit)
385 {
386 	/* atomic_testandclear_32 expects a bit position */
387 	return atomic_testandclear_32(s, bit);
388 }
389 
390 /**
391  * iavf_test_state - Test the specified state
392  * @s: the state bitmap
393  * @bit: the bit to test
394  *
395  * @returns true if the state is set, false otherwise.
396  *
397  * @remark Use this only if the flow does not need to update the state. If you
398  * must update the state as well, prefer iavf_testandset_state or
399  * iavf_testandclear_state.
400  */
401 static inline u32
402 iavf_test_state(volatile u32 *s, enum iavf_state bit)
403 {
404 	return (*s & BIT(bit)) ? true : false;
405 }
406 
407 /**
408  * cmp_etheraddr - Compare two ethernet addresses
409  * @ea1: first ethernet address
410  * @ea2: second ethernet address
411  *
412  * Compares two ethernet addresses.
413  *
414  * @returns true if the addresses are equal, false otherwise.
415  */
416 static inline bool
417 cmp_etheraddr(const u8 *ea1, const u8 *ea2)
418 {
419 	bool cmp = FALSE;
420 
421 	if ((ea1[0] == ea2[0]) && (ea1[1] == ea2[1]) &&
422 	    (ea1[2] == ea2[2]) && (ea1[3] == ea2[3]) &&
423 	    (ea1[4] == ea2[4]) && (ea1[5] == ea2[5]))
424 		cmp = TRUE;
425 
426 	return (cmp);
427 }
428 
429 int iavf_send_vc_msg(struct iavf_sc *sc, u32 op);
430 int iavf_send_vc_msg_sleep(struct iavf_sc *sc, u32 op);
431 void iavf_update_link_status(struct iavf_sc *);
432 bool iavf_driver_is_detaching(struct iavf_sc *sc);
433 void iavf_msec_pause(int msecs);
434 void iavf_get_default_rss_key(u32 *key);
435 int iavf_allocate_pci_resources_common(struct iavf_sc *sc);
436 int iavf_reset_complete(struct iavf_hw *hw);
437 int iavf_setup_vc(struct iavf_sc *sc);
438 int iavf_reset(struct iavf_sc *sc);
439 void iavf_enable_adminq_irq(struct iavf_hw *hw);
440 void iavf_disable_adminq_irq(struct iavf_hw *hw);
441 int iavf_vf_config(struct iavf_sc *sc);
442 void iavf_print_device_info(struct iavf_sc *sc);
443 int iavf_get_vsi_res_from_vf_res(struct iavf_sc *sc);
444 void iavf_set_mac_addresses(struct iavf_sc *sc);
445 void iavf_init_filters(struct iavf_sc *sc);
446 void iavf_free_filters(struct iavf_sc *sc);
447 void iavf_add_device_sysctls_common(struct iavf_sc *sc);
448 void iavf_configure_tx_itr(struct iavf_sc *sc);
449 void iavf_configure_rx_itr(struct iavf_sc *sc);
450 struct sysctl_oid_list *
451     iavf_create_debug_sysctl_tree(struct iavf_sc *sc);
452 void iavf_add_debug_sysctls_common(struct iavf_sc *sc,
453     struct sysctl_oid_list *debug_list);
454 void iavf_add_vsi_sysctls(device_t dev, struct iavf_vsi *vsi,
455     struct sysctl_ctx_list *ctx, const char *sysctl_name);
456 void iavf_add_sysctls_eth_stats(struct sysctl_ctx_list *ctx,
457     struct sysctl_oid_list *child, struct iavf_eth_stats *eth_stats);
458 void iavf_media_status_common(struct iavf_sc *sc,
459     struct ifmediareq *ifmr);
460 int iavf_media_change_common(if_t ifp);
461 void iavf_set_initial_baudrate(if_t ifp);
462 u64 iavf_max_vc_speed_to_value(u8 link_speeds);
463 void iavf_config_rss_reg(struct iavf_sc *sc);
464 void iavf_config_rss_pf(struct iavf_sc *sc);
465 void iavf_config_rss(struct iavf_sc *sc);
466 int iavf_config_promisc(struct iavf_sc *sc, int flags);
467 void iavf_init_multi(struct iavf_sc *sc);
468 void iavf_multi_set(struct iavf_sc *sc);
469 int iavf_add_mac_filter(struct iavf_sc *sc, u8 *macaddr, u16 flags);
470 struct iavf_mac_filter *
471     iavf_find_mac_filter(struct iavf_sc *sc, u8 *macaddr);
472 struct iavf_mac_filter *
473     iavf_get_mac_filter(struct iavf_sc *sc);
474 u64 iavf_baudrate_from_link_speed(struct iavf_sc *sc);
475 void iavf_add_vlan_filter(struct iavf_sc *sc, u16 vtag);
476 int iavf_mark_del_vlan_filter(struct iavf_sc *sc, u16 vtag);
477 void iavf_update_msix_devinfo(device_t dev);
478 void iavf_disable_queues_with_retries(struct iavf_sc *);
479 
480 int iavf_sysctl_current_speed(SYSCTL_HANDLER_ARGS);
481 int iavf_sysctl_tx_itr(SYSCTL_HANDLER_ARGS);
482 int iavf_sysctl_rx_itr(SYSCTL_HANDLER_ARGS);
483 int iavf_sysctl_sw_filter_list(SYSCTL_HANDLER_ARGS);
484 
485 #endif /* _IAVF_LIB_H_ */
486