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