xref: /freebsd-13-stable/sys/dev/ice/ice_lib.h (revision 2c6edd6dc94e7d429ce022262b9e25d159ce61e5)
1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*  Copyright (c) 2024, 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 ice_lib.h
34  * @brief header for generic device and sysctl functions
35  *
36  * Contains definitions and function declarations for the ice_lib.c file. It
37  * does not depend on the iflib networking stack.
38  */
39 
40 #ifndef _ICE_LIB_H_
41 #define _ICE_LIB_H_
42 
43 /* include kernel options first */
44 #include "ice_opts.h"
45 
46 #include <sys/types.h>
47 #include <sys/bus.h>
48 #include <sys/rman.h>
49 #include <sys/socket.h>
50 #include <sys/sbuf.h>
51 #include <sys/sysctl.h>
52 #include <sys/syslog.h>
53 #include <sys/module.h>
54 #include <sys/proc.h>
55 
56 #include <net/if.h>
57 #include <net/if_var.h>
58 #include <net/if_media.h>
59 #include <net/ethernet.h>
60 #include <net/if_types.h>
61 
62 #include <sys/bitstring.h>
63 
64 #include "ice_dcb.h"
65 #include "ice_type.h"
66 #include "ice_common.h"
67 #include "ice_flow.h"
68 #include "ice_sched.h"
69 #include "ice_resmgr.h"
70 
71 #include "ice_rdma_internal.h"
72 
73 #include "ice_rss.h"
74 
75 /* Hide debug sysctls unless INVARIANTS is enabled */
76 #ifdef INVARIANTS
77 #define ICE_CTLFLAG_DEBUG 0
78 #else
79 #define ICE_CTLFLAG_DEBUG CTLFLAG_SKIP
80 #endif
81 
82 /**
83  * for_each_set_bit - For loop over each set bit in a bit string
84  * @bit: storage for the bit index
85  * @data: address of data block to loop over
86  * @nbits: maximum number of bits to loop over
87  *
88  * macro to create a for loop over a bit string, which runs the body once for
89  * each bit that is set in the string. The bit variable will be set to the
90  * index of each set bit in the string, with zero representing the first bit.
91  */
92 #define for_each_set_bit(bit, data, nbits) \
93 	for (bit_ffs((bitstr_t *)(data), (nbits), &(bit)); \
94 	     (bit) != -1; \
95 	     bit_ffs_at((bitstr_t *)(data), (bit) + 1, (nbits), &(bit)))
96 
97 /**
98  * @var broadcastaddr
99  * @brief broadcast MAC address
100  *
101  * constant defining the broadcast MAC address, used for programming the
102  * broadcast address as a MAC filter for the PF VSI.
103  */
104 static const u8 broadcastaddr[ETHER_ADDR_LEN] = {
105 	0xff, 0xff, 0xff, 0xff, 0xff, 0xff
106 };
107 
108 MALLOC_DECLARE(M_ICE);
109 
110 extern const char ice_driver_version[];
111 extern const uint8_t ice_major_version;
112 extern const uint8_t ice_minor_version;
113 extern const uint8_t ice_patch_version;
114 extern const uint8_t ice_rc_version;
115 
116 /* global sysctl indicating whether the Tx FC filter should be enabled */
117 extern bool ice_enable_tx_fc_filter;
118 
119 /* global sysctl indicating whether the Tx LLDP filter should be enabled */
120 extern bool ice_enable_tx_lldp_filter;
121 
122 /* global sysctl indicating whether FW health status events should be enabled */
123 extern bool ice_enable_health_events;
124 
125 /* global sysctl indicating whether to enable 5-layer scheduler topology */
126 extern bool ice_tx_balance_en;
127 
128 /**
129  * @struct ice_bar_info
130  * @brief PCI BAR mapping information
131  *
132  * Contains data about a PCI BAR that the driver has mapped for use.
133  */
134 struct ice_bar_info {
135 	struct resource		*res;
136 	bus_space_tag_t		tag;
137 	bus_space_handle_t	handle;
138 	bus_size_t		size;
139 	int			rid;
140 };
141 
142 /* Alignment for queues */
143 #define DBA_ALIGN		128
144 
145 /* Maximum TSO size is (256K)-1 */
146 #define ICE_TSO_SIZE		((256*1024) - 1)
147 
148 /* Minimum size for TSO MSS */
149 #define ICE_MIN_TSO_MSS		64
150 
151 #define ICE_MAX_TX_SEGS		8
152 #define ICE_MAX_TSO_SEGS	128
153 
154 #define ICE_MAX_DMA_SEG_SIZE	((16*1024) - 1)
155 
156 #define ICE_MAX_RX_SEGS		5
157 
158 #define ICE_MAX_TSO_HDR_SEGS	3
159 
160 #define ICE_MSIX_BAR		3
161 
162 #define ICE_DEFAULT_DESC_COUNT	1024
163 #define ICE_MAX_DESC_COUNT	8160
164 #define ICE_MIN_DESC_COUNT	64
165 #define ICE_DESC_COUNT_INCR	32
166 
167 /* List of hardware offloads we support */
168 #define ICE_CSUM_OFFLOAD (CSUM_IP | CSUM_IP_TCP | CSUM_IP_UDP | CSUM_IP_SCTP |	\
169 			  CSUM_IP6_TCP| CSUM_IP6_UDP | CSUM_IP6_SCTP |		\
170 			  CSUM_IP_TSO | CSUM_IP6_TSO)
171 
172 /* Macros to decide what kind of hardware offload to enable */
173 #define ICE_CSUM_TCP (CSUM_IP_TCP|CSUM_IP_TSO|CSUM_IP6_TSO|CSUM_IP6_TCP)
174 #define ICE_CSUM_UDP (CSUM_IP_UDP|CSUM_IP6_UDP)
175 #define ICE_CSUM_SCTP (CSUM_IP_SCTP|CSUM_IP6_SCTP)
176 #define ICE_CSUM_IP (CSUM_IP|CSUM_IP_TSO)
177 
178 /* List of known RX CSUM offload flags */
179 #define ICE_RX_CSUM_FLAGS (CSUM_L3_CALC | CSUM_L3_VALID | CSUM_L4_CALC | \
180 			   CSUM_L4_VALID | CSUM_L5_CALC | CSUM_L5_VALID | \
181 			   CSUM_COALESCED)
182 
183 /* List of interface capabilities supported by ice hardware */
184 #define ICE_FULL_CAPS \
185 	(IFCAP_TSO4 | IFCAP_TSO6 | \
186 	 IFCAP_TXCSUM | IFCAP_TXCSUM_IPV6 | \
187 	 IFCAP_RXCSUM | IFCAP_RXCSUM_IPV6 | \
188 	 IFCAP_VLAN_HWFILTER | IFCAP_VLAN_HWTSO | \
189 	 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWTSO | \
190 	 IFCAP_VLAN_MTU | IFCAP_JUMBO_MTU | IFCAP_LRO)
191 
192 /* Safe mode disables support for hardware checksums and TSO */
193 #define ICE_SAFE_CAPS \
194 	(ICE_FULL_CAPS & ~(IFCAP_HWCSUM | IFCAP_TSO | \
195 			   IFCAP_VLAN_HWTSO | IFCAP_VLAN_HWCSUM))
196 
197 #define ICE_CAPS(sc) \
198 	(ice_is_bit_set(sc->feat_en, ICE_FEATURE_SAFE_MODE) ? ICE_SAFE_CAPS : ICE_FULL_CAPS)
199 
200 /**
201  * ICE_NVM_ACCESS
202  * @brief Private ioctl command number for NVM access ioctls
203  *
204  * The ioctl command number used by NVM update for accessing the driver for
205  * NVM access commands.
206  */
207 #define ICE_NVM_ACCESS \
208 	(((((((('E' << 4) + '1') << 4) + 'K') << 4) + 'G') << 4) | 5)
209 
210 /**
211  * ICE_DEBUG_DUMP
212  * @brief Private ioctl command number for retrieving debug dump data
213  *
214  * The ioctl command number used by a userspace tool for accessing the driver for
215  * getting debug dump data from the firmware.
216  */
217 #define ICE_DEBUG_DUMP \
218 	(((((((('E' << 4) + '1') << 4) + 'K') << 4) + 'G') << 4) | 6)
219 
220 #define ICE_AQ_LEN		1023
221 #define ICE_MBXQ_LEN		512
222 #define ICE_SBQ_LEN		512
223 
224 #define ICE_CTRLQ_WORK_LIMIT 256
225 
226 #define ICE_DFLT_TRAFFIC_CLASS BIT(0)
227 
228 /* wait up to 50 microseconds for queue state change */
229 #define ICE_Q_WAIT_RETRY_LIMIT	5
230 
231 #define ICE_UP_TABLE_TRANSLATE(val, i) \
232 		(((val) << ICE_AQ_VSI_UP_TABLE_UP##i##_S) & \
233 		ICE_AQ_VSI_UP_TABLE_UP##i##_M)
234 
235 /*
236  * For now, set this to the hardware maximum. Each function gets a smaller
237  * number assigned to it in hw->func_caps.guar_num_vsi, though there
238  * appears to be no guarantee that is the maximum number that a function
239  * can use.
240  */
241 #define ICE_MAX_VSI_AVAILABLE	768
242 
243 /* Maximum size of a single frame (for Tx and Rx) */
244 #define ICE_MAX_FRAME_SIZE ICE_AQ_SET_MAC_FRAME_SIZE_MAX
245 
246 /* Maximum MTU size */
247 #define ICE_MAX_MTU (ICE_MAX_FRAME_SIZE - \
248 		     ETHER_HDR_LEN - ETHER_CRC_LEN - ETHER_VLAN_ENCAP_LEN)
249 
250 /*
251  * Hardware requires that TSO packets have an segment size of at least 64
252  * bytes. To avoid sending bad frames to the hardware, the driver forces the
253  * MSS for all TSO packets to have a segment size of at least 64 bytes.
254  *
255  * However, if the MTU is reduced below a certain size, then the resulting
256  * larger MSS can result in transmitting segmented frames with a packet size
257  * larger than the MTU.
258  *
259  * Avoid this by preventing the MTU from being lowered below this limit.
260  * Alternative solutions require changing the TCP stack to disable offloading
261  * the segmentation when the requested segment size goes below 64 bytes.
262  */
263 #define ICE_MIN_MTU 112
264 
265 /*
266  * The default number of queues reserved for a VF is 4, according to the
267  * AVF Base Mode specification.
268  */
269 #define ICE_DEFAULT_VF_QUEUES	4
270 
271 /*
272  * An invalid VSI number to indicate that mirroring should be disabled.
273  */
274 #define ICE_INVALID_MIRROR_VSI	((u16)-1)
275 /*
276  * The maximum number of RX queues allowed per TC in a VSI.
277  */
278 #define ICE_MAX_RXQS_PER_TC	256
279 
280 /*
281  * There are three settings that can be updated independently or
282  * altogether: Link speed, FEC, and Flow Control.  These macros allow
283  * the caller to specify which setting(s) to update.
284  */
285 #define ICE_APPLY_LS        BIT(0)
286 #define ICE_APPLY_FEC       BIT(1)
287 #define ICE_APPLY_FC        BIT(2)
288 #define ICE_APPLY_LS_FEC    (ICE_APPLY_LS | ICE_APPLY_FEC)
289 #define ICE_APPLY_LS_FC     (ICE_APPLY_LS | ICE_APPLY_FC)
290 #define ICE_APPLY_FEC_FC    (ICE_APPLY_FEC | ICE_APPLY_FC)
291 #define ICE_APPLY_LS_FEC_FC (ICE_APPLY_LS_FEC | ICE_APPLY_FC)
292 
293 /**
294  * @enum ice_dyn_idx_t
295  * @brief Dynamic Control ITR indexes
296  *
297  * This enum matches hardware bits and is meant to be used by DYN_CTLN
298  * registers and QINT registers or more generally anywhere in the manual
299  * mentioning ITR_INDX, ITR_NONE cannot be used as an index 'n' into any
300  * register but instead is a special value meaning "don't update" ITR0/1/2.
301  */
302 enum ice_dyn_idx_t {
303 	ICE_IDX_ITR0 = 0,
304 	ICE_IDX_ITR1 = 1,
305 	ICE_IDX_ITR2 = 2,
306 	ICE_ITR_NONE = 3	/* ITR_NONE must not be used as an index */
307 };
308 
309 /* By convenction ITR0 is used for RX, and ITR1 is used for TX */
310 #define ICE_RX_ITR ICE_IDX_ITR0
311 #define ICE_TX_ITR ICE_IDX_ITR1
312 
313 #define ICE_ITR_MAX		8160
314 
315 /* Define the default Tx and Rx ITR as 50us (translates to ~20k int/sec max) */
316 #define ICE_DFLT_TX_ITR		50
317 #define ICE_DFLT_RX_ITR		50
318 
319 /**
320  * ice_itr_to_reg - Convert an ITR setting into its register equivalent
321  * @hw: The device HW structure
322  * @itr_setting: the ITR setting to convert
323  *
324  * Based on the hardware ITR granularity, convert an ITR setting into the
325  * correct value to prepare programming to the HW.
326  */
ice_itr_to_reg(struct ice_hw * hw,u16 itr_setting)327 static inline u16 ice_itr_to_reg(struct ice_hw *hw, u16 itr_setting)
328 {
329 	return itr_setting / hw->itr_gran;
330 }
331 
332 /**
333  * @enum ice_rx_dtype
334  * @brief DTYPE header split options
335  *
336  * This enum matches the Rx context bits to define whether header split is
337  * enabled or not.
338  */
339 enum ice_rx_dtype {
340 	ICE_RX_DTYPE_NO_SPLIT		= 0,
341 	ICE_RX_DTYPE_HEADER_SPLIT	= 1,
342 	ICE_RX_DTYPE_SPLIT_ALWAYS	= 2,
343 };
344 
345 /* Strings used for displaying FEC mode
346  *
347  * Use ice_fec_str() to get these unless these need to be embedded in a
348  * string constant.
349  */
350 #define ICE_FEC_STRING_AUTO	"Auto"
351 #define ICE_FEC_STRING_RS	"RS-FEC"
352 #define ICE_FEC_STRING_BASER	"FC-FEC/BASE-R"
353 #define ICE_FEC_STRING_NONE	"None"
354 #define ICE_FEC_STRING_DIS_AUTO	"Auto (w/ No-FEC)"
355 
356 /* Strings used for displaying Flow Control mode
357  *
358  * Use ice_fc_str() to get these unless these need to be embedded in a
359  * string constant.
360  */
361 #define ICE_FC_STRING_FULL	"Full"
362 #define ICE_FC_STRING_TX	"Tx"
363 #define ICE_FC_STRING_RX	"Rx"
364 #define ICE_FC_STRING_NONE	"None"
365 
366 /*
367  * The number of times the ice_handle_i2c_req function will retry reading
368  * I2C data via the Admin Queue before returning EBUSY.
369  */
370 #define ICE_I2C_MAX_RETRIES		10
371 
372 /*
373  * The Start LLDP Agent AQ command will fail if it's sent too soon after
374  * the LLDP agent is stopped. The period between the stop and start
375  * commands must currently be at least 2 seconds.
376  */
377 #define ICE_START_LLDP_RETRY_WAIT	(2 * hz)
378 
379 /*
380  * Only certain cluster IDs are valid for the FW debug dump functionality,
381  * so define a mask of those here.
382  */
383 #define ICE_FW_DEBUG_DUMP_VALID_CLUSTER_MASK	0x4001AF
384 
385 struct ice_softc;
386 
387 /**
388  * @enum ice_rx_cso_stat
389  * @brief software checksum offload statistics
390  *
391  * Enumeration of possible checksum offload statistics captured by software
392  * during the Rx path.
393  */
394 enum ice_rx_cso_stat {
395 	ICE_CSO_STAT_RX_IP4_ERR,
396 	ICE_CSO_STAT_RX_IP6_ERR,
397 	ICE_CSO_STAT_RX_L3_ERR,
398 	ICE_CSO_STAT_RX_TCP_ERR,
399 	ICE_CSO_STAT_RX_UDP_ERR,
400 	ICE_CSO_STAT_RX_SCTP_ERR,
401 	ICE_CSO_STAT_RX_L4_ERR,
402 	ICE_CSO_STAT_RX_COUNT
403 };
404 
405 /**
406  * @enum ice_tx_cso_stat
407  * @brief software checksum offload statistics
408  *
409  * Enumeration of possible checksum offload statistics captured by software
410  * during the Tx path.
411  */
412 enum ice_tx_cso_stat {
413 	ICE_CSO_STAT_TX_TCP,
414 	ICE_CSO_STAT_TX_UDP,
415 	ICE_CSO_STAT_TX_SCTP,
416 	ICE_CSO_STAT_TX_IP4,
417 	ICE_CSO_STAT_TX_IP6,
418 	ICE_CSO_STAT_TX_L3_ERR,
419 	ICE_CSO_STAT_TX_L4_ERR,
420 	ICE_CSO_STAT_TX_COUNT
421 };
422 
423 /**
424  * @struct tx_stats
425  * @brief software Tx statistics
426  *
427  * Contains software counted Tx statistics for a single queue
428  */
429 struct tx_stats {
430 	/* Soft Stats */
431 	u64			tx_bytes;
432 	u64			tx_packets;
433 	u64			mss_too_small;
434 	u64			cso[ICE_CSO_STAT_TX_COUNT];
435 };
436 
437 /**
438  * @struct rx_stats
439  * @brief software Rx statistics
440  *
441  * Contains software counted Rx statistics for a single queue
442  */
443 struct rx_stats {
444 	/* Soft Stats */
445 	u64			rx_packets;
446 	u64			rx_bytes;
447 	u64			desc_errs;
448 	u64			cso[ICE_CSO_STAT_RX_COUNT];
449 };
450 
451 /**
452  * @struct ice_vsi_hw_stats
453  * @brief hardware statistics for a VSI
454  *
455  * Stores statistics that are generated by hardware for a VSI.
456  */
457 struct ice_vsi_hw_stats {
458 	struct ice_eth_stats prev;
459 	struct ice_eth_stats cur;
460 	bool offsets_loaded;
461 };
462 
463 /**
464  * @struct ice_pf_hw_stats
465  * @brief hardware statistics for a PF
466  *
467  * Stores statistics that are generated by hardware for each PF.
468  */
469 struct ice_pf_hw_stats {
470 	struct ice_hw_port_stats prev;
471 	struct ice_hw_port_stats cur;
472 	bool offsets_loaded;
473 };
474 
475 /**
476  * @struct ice_pf_sw_stats
477  * @brief software statistics for a PF
478  *
479  * Contains software generated statistics relevant to a PF.
480  */
481 struct ice_pf_sw_stats {
482 	/* # of reset events handled, by type */
483 	u32 corer_count;
484 	u32 globr_count;
485 	u32 empr_count;
486 	u32 pfr_count;
487 
488 	/* # of detected MDD events for Tx and Rx */
489 	u32 tx_mdd_count;
490 	u32 rx_mdd_count;
491 };
492 
493 /**
494  * @struct ice_tc_info
495  * @brief Traffic class information for a VSI
496  *
497  * Stores traffic class information used in configuring
498  * a VSI.
499  */
500 struct ice_tc_info {
501 	u16 qoffset;	/* Offset in VSI queue space */
502 	u16 qcount_tx;	/* TX queues for this Traffic Class */
503 	u16 qcount_rx;	/* RX queues */
504 };
505 
506 /**
507  * @struct ice_vsi
508  * @brief VSI structure
509  *
510  * Contains data relevant to a single VSI
511  */
512 struct ice_vsi {
513 	/* back pointer to the softc */
514 	struct ice_softc	*sc;
515 
516 	bool dynamic;		/* if true, dynamically allocated */
517 
518 	enum ice_vsi_type type;	/* type of this VSI */
519 	u16 idx;		/* software index to sc->all_vsi[] */
520 
521 	u16 *tx_qmap; /* Tx VSI to PF queue mapping */
522 	u16 *rx_qmap; /* Rx VSI to PF queue mapping */
523 
524 	enum ice_resmgr_alloc_type qmap_type;
525 
526 	struct ice_tx_queue *tx_queues;	/* Tx queue array */
527 	struct ice_rx_queue *rx_queues;	/* Rx queue array */
528 
529 	int num_tx_queues;
530 	int num_rx_queues;
531 	int num_vectors;
532 
533 	int16_t rx_itr;
534 	int16_t tx_itr;
535 
536 	/* RSS configuration */
537 	u16 rss_table_size; /* HW RSS table size */
538 	u8 rss_lut_type; /* Used to configure Get/Set RSS LUT AQ call */
539 
540 	int max_frame_size;
541 	u16 mbuf_sz;
542 
543 	struct ice_aqc_vsi_props info;
544 
545 	/* DCB configuration */
546 	u8 num_tcs;	/* Total number of enabled TCs */
547 	u16 tc_map;	/* bitmap of enabled Traffic Classes */
548 	/* Information for each traffic class */
549 	struct ice_tc_info tc_info[ICE_MAX_TRAFFIC_CLASS];
550 
551 	/* context for per-VSI sysctls */
552 	struct sysctl_ctx_list ctx;
553 	struct sysctl_oid *vsi_node;
554 
555 	/* context for per-txq sysctls */
556 	struct sysctl_ctx_list txqs_ctx;
557 	struct sysctl_oid *txqs_node;
558 
559 	/* context for per-rxq sysctls */
560 	struct sysctl_ctx_list rxqs_ctx;
561 	struct sysctl_oid *rxqs_node;
562 
563 	/* VSI-level stats */
564 	struct ice_vsi_hw_stats hw_stats;
565 
566 	/* VSI mirroring details */
567 	u16 mirror_src_vsi;
568 	u16 rule_mir_ingress;
569 	u16 rule_mir_egress;
570 };
571 
572 /**
573  * @struct ice_debug_dump_cmd
574  * @brief arguments/return value for debug dump ioctl
575  */
576 struct ice_debug_dump_cmd {
577 	u32 offset;		/* offset to read/write from table, in bytes */
578 	u16 cluster_id;		/* also used to get next cluster id */
579 	u16 table_id;
580 	u16 data_size;		/* size of data field, in bytes */
581 	u16 reserved1;
582 	u32 reserved2;
583 	u8 data[];
584 };
585 
586 /**
587  * @enum ice_state
588  * @brief Driver state flags
589  *
590  * Used to indicate the status of various driver events. Intended to be
591  * modified only using atomic operations, so that we can use it even in places
592  * which aren't locked.
593  */
594 enum ice_state {
595 	ICE_STATE_CONTROLQ_EVENT_PENDING,
596 	ICE_STATE_VFLR_PENDING,
597 	ICE_STATE_MDD_PENDING,
598 	ICE_STATE_RESET_OICR_RECV,
599 	ICE_STATE_RESET_PFR_REQ,
600 	ICE_STATE_PREPARED_FOR_RESET,
601 	ICE_STATE_SUBIF_NEEDS_REINIT,
602 	ICE_STATE_RESET_FAILED,
603 	ICE_STATE_DRIVER_INITIALIZED,
604 	ICE_STATE_NO_MEDIA,
605 	ICE_STATE_RECOVERY_MODE,
606 	ICE_STATE_ROLLBACK_MODE,
607 	ICE_STATE_LINK_STATUS_REPORTED,
608 	ICE_STATE_ATTACHING,
609 	ICE_STATE_DETACHING,
610 	ICE_STATE_LINK_DEFAULT_OVERRIDE_PENDING,
611 	ICE_STATE_LLDP_RX_FLTR_FROM_DRIVER,
612 	ICE_STATE_MULTIPLE_TCS,
613 	ICE_STATE_DO_FW_DEBUG_DUMP,
614 	ICE_STATE_LINK_ACTIVE_ON_DOWN,
615 	ICE_STATE_FIRST_INIT_LINK,
616 	ICE_STATE_DO_CREATE_MIRR_INTFC,
617 	ICE_STATE_DO_DESTROY_MIRR_INTFC,
618 	/* This entry must be last */
619 	ICE_STATE_LAST,
620 };
621 
622 /* Functions for setting and checking driver state. Note the functions take
623  * bit positions, not bitmasks. The atomic_testandset_32 and
624  * atomic_testandclear_32 operations require bit positions, while the
625  * atomic_set_32 and atomic_clear_32 require bitmasks. This can easily lead to
626  * programming error, so we provide wrapper functions to avoid this.
627  */
628 
629 /**
630  * ice_set_state - Set the specified state
631  * @s: the state bitmap
632  * @bit: the state to set
633  *
634  * Atomically update the state bitmap with the specified bit set.
635  */
636 static inline void
ice_set_state(volatile u32 * s,enum ice_state bit)637 ice_set_state(volatile u32 *s, enum ice_state bit)
638 {
639 	/* atomic_set_32 expects a bitmask */
640 	atomic_set_32(s, BIT(bit));
641 }
642 
643 /**
644  * ice_clear_state - Clear the specified state
645  * @s: the state bitmap
646  * @bit: the state to clear
647  *
648  * Atomically update the state bitmap with the specified bit cleared.
649  */
650 static inline void
ice_clear_state(volatile u32 * s,enum ice_state bit)651 ice_clear_state(volatile u32 *s, enum ice_state bit)
652 {
653 	/* atomic_clear_32 expects a bitmask */
654 	atomic_clear_32(s, BIT(bit));
655 }
656 
657 /**
658  * ice_testandset_state - Test and set the specified state
659  * @s: the state bitmap
660  * @bit: the bit to test
661  *
662  * Atomically update the state bitmap, setting the specified bit. Returns the
663  * previous value of the bit.
664  */
665 static inline u32
ice_testandset_state(volatile u32 * s,enum ice_state bit)666 ice_testandset_state(volatile u32 *s, enum ice_state bit)
667 {
668 	/* atomic_testandset_32 expects a bit position */
669 	return atomic_testandset_32(s, bit);
670 }
671 
672 /**
673  * ice_testandclear_state - Test and clear the specified state
674  * @s: the state bitmap
675  * @bit: the bit to test
676  *
677  * Atomically update the state bitmap, clearing the specified bit. Returns the
678  * previous value of the bit.
679  */
680 static inline u32
ice_testandclear_state(volatile u32 * s,enum ice_state bit)681 ice_testandclear_state(volatile u32 *s, enum ice_state bit)
682 {
683 	/* atomic_testandclear_32 expects a bit position */
684 	return atomic_testandclear_32(s, bit);
685 }
686 
687 /**
688  * ice_test_state - Test the specified state
689  * @s: the state bitmap
690  * @bit: the bit to test
691  *
692  * Return true if the state is set, false otherwise. Use this only if the flow
693  * does not need to update the state. If you must update the state as well,
694  * prefer ice_testandset_state or ice_testandclear_state.
695  */
696 static inline u32
ice_test_state(volatile u32 * s,enum ice_state bit)697 ice_test_state(volatile u32 *s, enum ice_state bit)
698 {
699 	return (*s & BIT(bit)) ? true : false;
700 }
701 
702 /**
703  * @struct ice_str_buf
704  * @brief static length buffer for string returning
705  *
706  * Structure containing a fixed size string buffer, used to implement
707  * numeric->string conversion functions that may want to return non-constant
708  * strings.
709  *
710  * This allows returning a fixed size string that is generated by a conversion
711  * function, and then copied to the used location without needing to use an
712  * explicit local variable passed by reference.
713  */
714 struct ice_str_buf {
715 	char str[ICE_STR_BUF_LEN];
716 };
717 
718 struct ice_str_buf _ice_aq_str(enum ice_aq_err aq_err);
719 struct ice_str_buf _ice_status_str(enum ice_status status);
720 struct ice_str_buf _ice_err_str(int err);
721 struct ice_str_buf _ice_fltr_flag_str(u16 flag);
722 struct ice_str_buf _ice_log_sev_str(u8 log_level);
723 struct ice_str_buf _ice_mdd_tx_tclan_str(u8 event);
724 struct ice_str_buf _ice_mdd_tx_pqm_str(u8 event);
725 struct ice_str_buf _ice_mdd_rx_str(u8 event);
726 struct ice_str_buf _ice_fw_lldp_status(u32 lldp_status);
727 
728 #define ice_aq_str(err)		_ice_aq_str(err).str
729 #define ice_status_str(err)	_ice_status_str(err).str
730 #define ice_err_str(err)	_ice_err_str(err).str
731 #define ice_fltr_flag_str(flag)	_ice_fltr_flag_str(flag).str
732 
733 #define ice_mdd_tx_tclan_str(event)	_ice_mdd_tx_tclan_str(event).str
734 #define ice_mdd_tx_pqm_str(event)	_ice_mdd_tx_pqm_str(event).str
735 #define ice_mdd_rx_str(event)		_ice_mdd_rx_str(event).str
736 
737 #define ice_log_sev_str(log_level)	_ice_log_sev_str(log_level).str
738 #define ice_fw_lldp_status(lldp_status) _ice_fw_lldp_status(lldp_status).str
739 
740 /**
741  * ice_enable_intr - Enable interrupts for given vector
742  * @hw: the device private HW structure
743  * @vector: the interrupt index in PF space
744  *
745  * In MSI or Legacy interrupt mode, interrupt 0 is the only valid index.
746  */
747 static inline void
ice_enable_intr(struct ice_hw * hw,int vector)748 ice_enable_intr(struct ice_hw *hw, int vector)
749 {
750 	u32 dyn_ctl;
751 
752 	/* Use ITR_NONE so that ITR configuration is not changed. */
753 	dyn_ctl = GLINT_DYN_CTL_INTENA_M | GLINT_DYN_CTL_CLEARPBA_M |
754 		  (ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
755 	wr32(hw, GLINT_DYN_CTL(vector), dyn_ctl);
756 }
757 
758 /**
759  * ice_disable_intr - Disable interrupts for given vector
760  * @hw: the device private HW structure
761  * @vector: the interrupt index in PF space
762  *
763  * In MSI or Legacy interrupt mode, interrupt 0 is the only valid index.
764  */
765 static inline void
ice_disable_intr(struct ice_hw * hw,int vector)766 ice_disable_intr(struct ice_hw *hw, int vector)
767 {
768 	u32 dyn_ctl;
769 
770 	/* Use ITR_NONE so that ITR configuration is not changed. */
771 	dyn_ctl = ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S;
772 	wr32(hw, GLINT_DYN_CTL(vector), dyn_ctl);
773 }
774 
775 /**
776  * ice_is_tx_desc_done - determine if a Tx descriptor is done
777  * @txd: the Tx descriptor to check
778  *
779  * Returns true if hardware is done with a Tx descriptor and software is
780  * capable of re-using it.
781  */
782 static inline bool
ice_is_tx_desc_done(struct ice_tx_desc * txd)783 ice_is_tx_desc_done(struct ice_tx_desc *txd)
784 {
785 	return (((txd->cmd_type_offset_bsz & ICE_TXD_QW1_DTYPE_M)
786 		 >> ICE_TXD_QW1_DTYPE_S) == ICE_TX_DESC_DTYPE_DESC_DONE);
787 }
788 
789 /**
790  * ice_get_pf_id - Get the PF id from the hardware registers
791  * @hw: the ice hardware structure
792  *
793  * Reads the PF_FUNC_RID register and extracts the function number from it.
794  * Intended to be used in cases where hw->pf_id hasn't yet been assigned by
795  * ice_init_hw.
796  *
797  * @pre this function should be called only after PCI register access has been
798  * setup, and prior to ice_init_hw. After hardware has been initialized, the
799  * cached hw->pf_id value can be used.
800  */
801 static inline u8
ice_get_pf_id(struct ice_hw * hw)802 ice_get_pf_id(struct ice_hw *hw)
803 {
804 	return (u8)((rd32(hw, PF_FUNC_RID) & PF_FUNC_RID_FUNCTION_NUMBER_M) >>
805 		    PF_FUNC_RID_FUNCTION_NUMBER_S);
806 }
807 
808 /* Details of how to re-initialize depend on the networking stack */
809 void ice_request_stack_reinit(struct ice_softc *sc);
810 
811 /* Details of how to check if the network stack is detaching us */
812 bool ice_driver_is_detaching(struct ice_softc *sc);
813 
814 /* Details of how to setup/teardown a mirror interface */
815 /**
816  * @brief Create an interface for mirroring
817  */
818 int ice_create_mirror_interface(struct ice_softc *sc);
819 /**
820  * @brief Destroy created mirroring interface
821  */
822 void ice_destroy_mirror_interface(struct ice_softc *sc);
823 
824 const char * ice_fw_module_str(enum ice_aqc_fw_logging_mod module);
825 void ice_add_fw_logging_tunables(struct ice_softc *sc,
826 				 struct sysctl_oid *parent);
827 void ice_handle_fw_log_event(struct ice_softc *sc, struct ice_aq_desc *desc,
828 			     void *buf);
829 
830 int  ice_process_ctrlq(struct ice_softc *sc, enum ice_ctl_q q_type, u16 *pending);
831 int  ice_map_bar(device_t dev, struct ice_bar_info *bar, int bar_num);
832 void ice_free_bar(device_t dev, struct ice_bar_info *bar);
833 void ice_set_ctrlq_len(struct ice_hw *hw);
834 void ice_release_vsi(struct ice_vsi *vsi);
835 struct ice_vsi *ice_alloc_vsi(struct ice_softc *sc, enum ice_vsi_type type);
836 void ice_alloc_vsi_qmap(struct ice_vsi *vsi, const int max_tx_queues,
837 		       const int max_rx_queues);
838 void ice_free_vsi_qmaps(struct ice_vsi *vsi);
839 int  ice_initialize_vsi(struct ice_vsi *vsi);
840 void ice_deinit_vsi(struct ice_vsi *vsi);
841 uint64_t ice_aq_speed_to_rate(struct ice_port_info *pi);
842 int  ice_get_phy_type_low(uint64_t phy_type_low);
843 int  ice_get_phy_type_high(uint64_t phy_type_high);
844 enum ice_status ice_add_media_types(struct ice_softc *sc, struct ifmedia *media);
845 void ice_configure_rxq_interrupt(struct ice_hw *hw, u16 rxqid, u16 vector, u8 itr_idx);
846 void ice_configure_all_rxq_interrupts(struct ice_vsi *vsi);
847 void ice_configure_txq_interrupt(struct ice_hw *hw, u16 txqid, u16 vector, u8 itr_idx);
848 void ice_configure_all_txq_interrupts(struct ice_vsi *vsi);
849 void ice_flush_rxq_interrupts(struct ice_vsi *vsi);
850 void ice_flush_txq_interrupts(struct ice_vsi *vsi);
851 int  ice_cfg_vsi_for_tx(struct ice_vsi *vsi);
852 int  ice_cfg_vsi_for_rx(struct ice_vsi *vsi);
853 int  ice_control_rx_queue(struct ice_vsi *vsi, u16 qidx, bool enable);
854 int  ice_control_all_rx_queues(struct ice_vsi *vsi, bool enable);
855 int  ice_cfg_pf_default_mac_filters(struct ice_softc *sc);
856 int  ice_rm_pf_default_mac_filters(struct ice_softc *sc);
857 void ice_print_nvm_version(struct ice_softc *sc);
858 void ice_update_vsi_hw_stats(struct ice_vsi *vsi);
859 void ice_reset_vsi_stats(struct ice_vsi *vsi);
860 void ice_update_pf_stats(struct ice_softc *sc);
861 void ice_reset_pf_stats(struct ice_softc *sc);
862 void ice_add_device_sysctls(struct ice_softc *sc);
863 void ice_log_hmc_error(struct ice_hw *hw, device_t dev);
864 void ice_add_sysctls_eth_stats(struct sysctl_ctx_list *ctx,
865 			       struct sysctl_oid *parent,
866 			       struct ice_eth_stats *stats);
867 void ice_add_vsi_sysctls(struct ice_vsi *vsi);
868 void ice_add_sysctls_mac_stats(struct sysctl_ctx_list *ctx,
869 			       struct sysctl_oid *parent,
870 			       struct ice_hw_port_stats *stats);
871 void ice_configure_misc_interrupts(struct ice_softc *sc);
872 int  ice_sync_multicast_filters(struct ice_softc *sc);
873 enum ice_status ice_add_vlan_hw_filters(struct ice_vsi *vsi, u16 *vid,
874 					u16 length);
875 enum ice_status ice_add_vlan_hw_filter(struct ice_vsi *vsi, u16 vid);
876 enum ice_status ice_remove_vlan_hw_filters(struct ice_vsi *vsi, u16 *vid,
877 					   u16 length);
878 enum ice_status ice_remove_vlan_hw_filter(struct ice_vsi *vsi, u16 vid);
879 void ice_add_vsi_tunables(struct ice_vsi *vsi, struct sysctl_oid *parent);
880 void ice_del_vsi_sysctl_ctx(struct ice_vsi *vsi);
881 void ice_add_device_tunables(struct ice_softc *sc);
882 int  ice_add_vsi_mac_filter(struct ice_vsi *vsi, const u8 *addr);
883 int  ice_remove_vsi_mac_filter(struct ice_vsi *vsi, const u8 *addr);
884 int  ice_vsi_disable_tx(struct ice_vsi *vsi);
885 void ice_vsi_add_txqs_ctx(struct ice_vsi *vsi);
886 void ice_vsi_add_rxqs_ctx(struct ice_vsi *vsi);
887 void ice_vsi_del_txqs_ctx(struct ice_vsi *vsi);
888 void ice_vsi_del_rxqs_ctx(struct ice_vsi *vsi);
889 void ice_add_txq_sysctls(struct ice_tx_queue *txq);
890 void ice_add_rxq_sysctls(struct ice_rx_queue *rxq);
891 int  ice_config_rss(struct ice_vsi *vsi);
892 void ice_clean_all_vsi_rss_cfg(struct ice_softc *sc);
893 enum ice_status ice_load_pkg_file(struct ice_softc *sc);
894 void ice_log_pkg_init(struct ice_softc *sc, enum ice_ddp_state pkg_status);
895 uint64_t ice_get_ifnet_counter(struct ice_vsi *vsi, ift_counter counter);
896 void ice_save_pci_info(struct ice_hw *hw, device_t dev);
897 int  ice_replay_all_vsi_cfg(struct ice_softc *sc);
898 void ice_link_up_msg(struct ice_softc *sc);
899 int  ice_update_laa_mac(struct ice_softc *sc);
900 void ice_get_and_print_bus_info(struct ice_softc *sc);
901 const char *ice_fec_str(enum ice_fec_mode mode);
902 const char *ice_fc_str(enum ice_fc_mode mode);
903 const char *ice_fwd_act_str(enum ice_sw_fwd_act_type action);
904 const char *ice_state_to_str(enum ice_state state);
905 int  ice_init_link_events(struct ice_softc *sc);
906 void ice_configure_rx_itr(struct ice_vsi *vsi);
907 void ice_configure_tx_itr(struct ice_vsi *vsi);
908 void ice_setup_pf_vsi(struct ice_softc *sc);
909 void ice_handle_mdd_event(struct ice_softc *sc);
910 void ice_init_dcb_setup(struct ice_softc *sc);
911 int  ice_send_version(struct ice_softc *sc);
912 int  ice_cfg_pf_ethertype_filters(struct ice_softc *sc);
913 void ice_init_link_configuration(struct ice_softc *sc);
914 void ice_init_saved_phy_cfg(struct ice_softc *sc);
915 int  ice_apply_saved_phy_cfg(struct ice_softc *sc, u8 settings);
916 void ice_set_link_management_mode(struct ice_softc *sc);
917 int  ice_module_event_handler(module_t mod, int what, void *arg);
918 int  ice_handle_nvm_access_ioctl(struct ice_softc *sc, struct ifdrv *ifd);
919 int  ice_handle_i2c_req(struct ice_softc *sc, struct ifi2creq *req);
920 int  ice_read_sff_eeprom(struct ice_softc *sc, u16 dev_addr, u16 offset, u8* data, u16 length);
921 int  ice_alloc_intr_tracking(struct ice_softc *sc);
922 void ice_free_intr_tracking(struct ice_softc *sc);
923 void ice_set_default_local_lldp_mib(struct ice_softc *sc);
924 void ice_set_link(struct ice_softc *sc, bool enabled);
925 void ice_add_rx_lldp_filter(struct ice_softc *sc);
926 void ice_init_health_events(struct ice_softc *sc);
927 void ice_cfg_pba_num(struct ice_softc *sc);
928 int ice_handle_debug_dump_ioctl(struct ice_softc *sc, struct ifdrv *ifd);
929 u8 ice_dcb_get_tc_map(const struct ice_dcbx_cfg *dcbcfg);
930 void ice_do_dcb_reconfig(struct ice_softc *sc, bool pending_mib);
931 int ice_setup_vsi_mirroring(struct ice_vsi *vsi);
932 
933 #endif /* _ICE_LIB_H_ */
934