xref: /linux/drivers/net/ethernet/intel/idpf/idpf.h (revision 021bc4b9)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* Copyright (C) 2023 Intel Corporation */
3 
4 #ifndef _IDPF_H_
5 #define _IDPF_H_
6 
7 /* Forward declaration */
8 struct idpf_adapter;
9 struct idpf_vport;
10 struct idpf_vport_max_q;
11 
12 #include <net/pkt_sched.h>
13 #include <linux/aer.h>
14 #include <linux/etherdevice.h>
15 #include <linux/pci.h>
16 #include <linux/bitfield.h>
17 #include <linux/sctp.h>
18 #include <linux/ethtool_netlink.h>
19 #include <net/gro.h>
20 #include <linux/dim.h>
21 
22 #include "virtchnl2.h"
23 #include "idpf_lan_txrx.h"
24 #include "idpf_txrx.h"
25 #include "idpf_controlq.h"
26 
27 #define GETMAXVAL(num_bits)		GENMASK((num_bits) - 1, 0)
28 
29 #define IDPF_NO_FREE_SLOT		0xffff
30 
31 /* Default Mailbox settings */
32 #define IDPF_NUM_FILTERS_PER_MSG	20
33 #define IDPF_NUM_DFLT_MBX_Q		2	/* includes both TX and RX */
34 #define IDPF_DFLT_MBX_Q_LEN		64
35 #define IDPF_DFLT_MBX_ID		-1
36 /* maximum number of times to try before resetting mailbox */
37 #define IDPF_MB_MAX_ERR			20
38 #define IDPF_NUM_CHUNKS_PER_MSG(struct_sz, chunk_sz)	\
39 	((IDPF_CTLQ_MAX_BUF_LEN - (struct_sz)) / (chunk_sz))
40 #define IDPF_WAIT_FOR_EVENT_TIMEO_MIN	2000
41 #define IDPF_WAIT_FOR_EVENT_TIMEO	60000
42 
43 #define IDPF_MAX_WAIT			500
44 
45 /* available message levels */
46 #define IDPF_AVAIL_NETIF_M (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
47 
48 #define IDPF_DIM_PROFILE_SLOTS  5
49 
50 #define IDPF_VIRTCHNL_VERSION_MAJOR VIRTCHNL2_VERSION_MAJOR_2
51 #define IDPF_VIRTCHNL_VERSION_MINOR VIRTCHNL2_VERSION_MINOR_0
52 
53 /**
54  * struct idpf_mac_filter
55  * @list: list member field
56  * @macaddr: MAC address
57  * @remove: filter should be removed (virtchnl)
58  * @add: filter should be added (virtchnl)
59  */
60 struct idpf_mac_filter {
61 	struct list_head list;
62 	u8 macaddr[ETH_ALEN];
63 	bool remove;
64 	bool add;
65 };
66 
67 /**
68  * enum idpf_state - State machine to handle bring up
69  * @__IDPF_STARTUP: Start the state machine
70  * @__IDPF_VER_CHECK: Negotiate virtchnl version
71  * @__IDPF_GET_CAPS: Negotiate capabilities
72  * @__IDPF_INIT_SW: Init based on given capabilities
73  * @__IDPF_STATE_LAST: Must be last, used to determine size
74  */
75 enum idpf_state {
76 	__IDPF_STARTUP,
77 	__IDPF_VER_CHECK,
78 	__IDPF_GET_CAPS,
79 	__IDPF_INIT_SW,
80 	__IDPF_STATE_LAST,
81 };
82 
83 /**
84  * enum idpf_flags - Hard reset causes.
85  * @IDPF_HR_FUNC_RESET: Hard reset when TxRx timeout
86  * @IDPF_HR_DRV_LOAD: Set on driver load for a clean HW
87  * @IDPF_HR_RESET_IN_PROG: Reset in progress
88  * @IDPF_REMOVE_IN_PROG: Driver remove in progress
89  * @IDPF_MB_INTR_MODE: Mailbox in interrupt mode
90  * @IDPF_FLAGS_NBITS: Must be last
91  */
92 enum idpf_flags {
93 	IDPF_HR_FUNC_RESET,
94 	IDPF_HR_DRV_LOAD,
95 	IDPF_HR_RESET_IN_PROG,
96 	IDPF_REMOVE_IN_PROG,
97 	IDPF_MB_INTR_MODE,
98 	IDPF_FLAGS_NBITS,
99 };
100 
101 /**
102  * enum idpf_cap_field - Offsets into capabilities struct for specific caps
103  * @IDPF_BASE_CAPS: generic base capabilities
104  * @IDPF_CSUM_CAPS: checksum offload capabilities
105  * @IDPF_SEG_CAPS: segmentation offload capabilities
106  * @IDPF_RSS_CAPS: RSS offload capabilities
107  * @IDPF_HSPLIT_CAPS: Header split capabilities
108  * @IDPF_RSC_CAPS: RSC offload capabilities
109  * @IDPF_OTHER_CAPS: miscellaneous offloads
110  *
111  * Used when checking for a specific capability flag since different capability
112  * sets are not mutually exclusive numerically, the caller must specify which
113  * type of capability they are checking for.
114  */
115 enum idpf_cap_field {
116 	IDPF_BASE_CAPS		= -1,
117 	IDPF_CSUM_CAPS		= offsetof(struct virtchnl2_get_capabilities,
118 					   csum_caps),
119 	IDPF_SEG_CAPS		= offsetof(struct virtchnl2_get_capabilities,
120 					   seg_caps),
121 	IDPF_RSS_CAPS		= offsetof(struct virtchnl2_get_capabilities,
122 					   rss_caps),
123 	IDPF_HSPLIT_CAPS	= offsetof(struct virtchnl2_get_capabilities,
124 					   hsplit_caps),
125 	IDPF_RSC_CAPS		= offsetof(struct virtchnl2_get_capabilities,
126 					   rsc_caps),
127 	IDPF_OTHER_CAPS		= offsetof(struct virtchnl2_get_capabilities,
128 					   other_caps),
129 };
130 
131 /**
132  * enum idpf_vport_state - Current vport state
133  * @__IDPF_VPORT_DOWN: Vport is down
134  * @__IDPF_VPORT_UP: Vport is up
135  * @__IDPF_VPORT_STATE_LAST: Must be last, number of states
136  */
137 enum idpf_vport_state {
138 	__IDPF_VPORT_DOWN,
139 	__IDPF_VPORT_UP,
140 	__IDPF_VPORT_STATE_LAST,
141 };
142 
143 /**
144  * struct idpf_netdev_priv - Struct to store vport back pointer
145  * @adapter: Adapter back pointer
146  * @vport: Vport back pointer
147  * @vport_id: Vport identifier
148  * @vport_idx: Relative vport index
149  * @state: See enum idpf_vport_state
150  * @netstats: Packet and byte stats
151  * @stats_lock: Lock to protect stats update
152  */
153 struct idpf_netdev_priv {
154 	struct idpf_adapter *adapter;
155 	struct idpf_vport *vport;
156 	u32 vport_id;
157 	u16 vport_idx;
158 	enum idpf_vport_state state;
159 	struct rtnl_link_stats64 netstats;
160 	spinlock_t stats_lock;
161 };
162 
163 /**
164  * struct idpf_reset_reg - Reset register offsets/masks
165  * @rstat: Reset status register
166  * @rstat_m: Reset status mask
167  */
168 struct idpf_reset_reg {
169 	void __iomem *rstat;
170 	u32 rstat_m;
171 };
172 
173 /**
174  * struct idpf_vport_max_q - Queue limits
175  * @max_rxq: Maximum number of RX queues supported
176  * @max_txq: Maixmum number of TX queues supported
177  * @max_bufq: In splitq, maximum number of buffer queues supported
178  * @max_complq: In splitq, maximum number of completion queues supported
179  */
180 struct idpf_vport_max_q {
181 	u16 max_rxq;
182 	u16 max_txq;
183 	u16 max_bufq;
184 	u16 max_complq;
185 };
186 
187 /**
188  * struct idpf_reg_ops - Device specific register operation function pointers
189  * @ctlq_reg_init: Mailbox control queue register initialization
190  * @intr_reg_init: Traffic interrupt register initialization
191  * @mb_intr_reg_init: Mailbox interrupt register initialization
192  * @reset_reg_init: Reset register initialization
193  * @trigger_reset: Trigger a reset to occur
194  */
195 struct idpf_reg_ops {
196 	void (*ctlq_reg_init)(struct idpf_ctlq_create_info *cq);
197 	int (*intr_reg_init)(struct idpf_vport *vport);
198 	void (*mb_intr_reg_init)(struct idpf_adapter *adapter);
199 	void (*reset_reg_init)(struct idpf_adapter *adapter);
200 	void (*trigger_reset)(struct idpf_adapter *adapter,
201 			      enum idpf_flags trig_cause);
202 };
203 
204 /**
205  * struct idpf_dev_ops - Device specific operations
206  * @reg_ops: Register operations
207  */
208 struct idpf_dev_ops {
209 	struct idpf_reg_ops reg_ops;
210 };
211 
212 /* These macros allow us to generate an enum and a matching char * array of
213  * stringified enums that are always in sync. Checkpatch issues a bogus warning
214  * about this being a complex macro; but it's wrong, these are never used as a
215  * statement and instead only used to define the enum and array.
216  */
217 #define IDPF_FOREACH_VPORT_VC_STATE(STATE)	\
218 	STATE(IDPF_VC_CREATE_VPORT)		\
219 	STATE(IDPF_VC_CREATE_VPORT_ERR)		\
220 	STATE(IDPF_VC_ENA_VPORT)		\
221 	STATE(IDPF_VC_ENA_VPORT_ERR)		\
222 	STATE(IDPF_VC_DIS_VPORT)		\
223 	STATE(IDPF_VC_DIS_VPORT_ERR)		\
224 	STATE(IDPF_VC_DESTROY_VPORT)		\
225 	STATE(IDPF_VC_DESTROY_VPORT_ERR)	\
226 	STATE(IDPF_VC_CONFIG_TXQ)		\
227 	STATE(IDPF_VC_CONFIG_TXQ_ERR)		\
228 	STATE(IDPF_VC_CONFIG_RXQ)		\
229 	STATE(IDPF_VC_CONFIG_RXQ_ERR)		\
230 	STATE(IDPF_VC_ENA_QUEUES)		\
231 	STATE(IDPF_VC_ENA_QUEUES_ERR)		\
232 	STATE(IDPF_VC_DIS_QUEUES)		\
233 	STATE(IDPF_VC_DIS_QUEUES_ERR)		\
234 	STATE(IDPF_VC_MAP_IRQ)			\
235 	STATE(IDPF_VC_MAP_IRQ_ERR)		\
236 	STATE(IDPF_VC_UNMAP_IRQ)		\
237 	STATE(IDPF_VC_UNMAP_IRQ_ERR)		\
238 	STATE(IDPF_VC_ADD_QUEUES)		\
239 	STATE(IDPF_VC_ADD_QUEUES_ERR)		\
240 	STATE(IDPF_VC_DEL_QUEUES)		\
241 	STATE(IDPF_VC_DEL_QUEUES_ERR)		\
242 	STATE(IDPF_VC_ALLOC_VECTORS)		\
243 	STATE(IDPF_VC_ALLOC_VECTORS_ERR)	\
244 	STATE(IDPF_VC_DEALLOC_VECTORS)		\
245 	STATE(IDPF_VC_DEALLOC_VECTORS_ERR)	\
246 	STATE(IDPF_VC_SET_SRIOV_VFS)		\
247 	STATE(IDPF_VC_SET_SRIOV_VFS_ERR)	\
248 	STATE(IDPF_VC_GET_RSS_LUT)		\
249 	STATE(IDPF_VC_GET_RSS_LUT_ERR)		\
250 	STATE(IDPF_VC_SET_RSS_LUT)		\
251 	STATE(IDPF_VC_SET_RSS_LUT_ERR)		\
252 	STATE(IDPF_VC_GET_RSS_KEY)		\
253 	STATE(IDPF_VC_GET_RSS_KEY_ERR)		\
254 	STATE(IDPF_VC_SET_RSS_KEY)		\
255 	STATE(IDPF_VC_SET_RSS_KEY_ERR)		\
256 	STATE(IDPF_VC_GET_STATS)		\
257 	STATE(IDPF_VC_GET_STATS_ERR)		\
258 	STATE(IDPF_VC_ADD_MAC_ADDR)		\
259 	STATE(IDPF_VC_ADD_MAC_ADDR_ERR)		\
260 	STATE(IDPF_VC_DEL_MAC_ADDR)		\
261 	STATE(IDPF_VC_DEL_MAC_ADDR_ERR)		\
262 	STATE(IDPF_VC_GET_PTYPE_INFO)		\
263 	STATE(IDPF_VC_GET_PTYPE_INFO_ERR)	\
264 	STATE(IDPF_VC_LOOPBACK_STATE)		\
265 	STATE(IDPF_VC_LOOPBACK_STATE_ERR)	\
266 	STATE(IDPF_VC_NBITS)
267 
268 #define IDPF_GEN_ENUM(ENUM) ENUM,
269 #define IDPF_GEN_STRING(STRING) #STRING,
270 
271 enum idpf_vport_vc_state {
272 	IDPF_FOREACH_VPORT_VC_STATE(IDPF_GEN_ENUM)
273 };
274 
275 extern const char * const idpf_vport_vc_state_str[];
276 
277 /**
278  * enum idpf_vport_reset_cause - Vport soft reset causes
279  * @IDPF_SR_Q_CHANGE: Soft reset queue change
280  * @IDPF_SR_Q_DESC_CHANGE: Soft reset descriptor change
281  * @IDPF_SR_MTU_CHANGE: Soft reset MTU change
282  * @IDPF_SR_RSC_CHANGE: Soft reset RSC change
283  */
284 enum idpf_vport_reset_cause {
285 	IDPF_SR_Q_CHANGE,
286 	IDPF_SR_Q_DESC_CHANGE,
287 	IDPF_SR_MTU_CHANGE,
288 	IDPF_SR_RSC_CHANGE,
289 };
290 
291 /**
292  * enum idpf_vport_flags - Vport flags
293  * @IDPF_VPORT_DEL_QUEUES: To send delete queues message
294  * @IDPF_VPORT_SW_MARKER: Indicate TX pipe drain software marker packets
295  *			  processing is done
296  * @IDPF_VPORT_FLAGS_NBITS: Must be last
297  */
298 enum idpf_vport_flags {
299 	IDPF_VPORT_DEL_QUEUES,
300 	IDPF_VPORT_SW_MARKER,
301 	IDPF_VPORT_FLAGS_NBITS,
302 };
303 
304 struct idpf_port_stats {
305 	struct u64_stats_sync stats_sync;
306 	u64_stats_t rx_hw_csum_err;
307 	u64_stats_t rx_hsplit;
308 	u64_stats_t rx_hsplit_hbo;
309 	u64_stats_t rx_bad_descs;
310 	u64_stats_t tx_linearize;
311 	u64_stats_t tx_busy;
312 	u64_stats_t tx_drops;
313 	u64_stats_t tx_dma_map_errs;
314 	struct virtchnl2_vport_stats vport_stats;
315 };
316 
317 /**
318  * struct idpf_vport - Handle for netdevices and queue resources
319  * @num_txq: Number of allocated TX queues
320  * @num_complq: Number of allocated completion queues
321  * @txq_desc_count: TX queue descriptor count
322  * @complq_desc_count: Completion queue descriptor count
323  * @compln_clean_budget: Work budget for completion clean
324  * @num_txq_grp: Number of TX queue groups
325  * @txq_grps: Array of TX queue groups
326  * @txq_model: Split queue or single queue queuing model
327  * @txqs: Used only in hotpath to get to the right queue very fast
328  * @crc_enable: Enable CRC insertion offload
329  * @num_rxq: Number of allocated RX queues
330  * @num_bufq: Number of allocated buffer queues
331  * @rxq_desc_count: RX queue descriptor count. *MUST* have enough descriptors
332  *		    to complete all buffer descriptors for all buffer queues in
333  *		    the worst case.
334  * @num_bufqs_per_qgrp: Buffer queues per RX queue in a given grouping
335  * @bufq_desc_count: Buffer queue descriptor count
336  * @bufq_size: Size of buffers in ring (e.g. 2K, 4K, etc)
337  * @num_rxq_grp: Number of RX queues in a group
338  * @rxq_grps: Total number of RX groups. Number of groups * number of RX per
339  *	      group will yield total number of RX queues.
340  * @rxq_model: Splitq queue or single queue queuing model
341  * @rx_ptype_lkup: Lookup table for ptypes on RX
342  * @adapter: back pointer to associated adapter
343  * @netdev: Associated net_device. Each vport should have one and only one
344  *	    associated netdev.
345  * @flags: See enum idpf_vport_flags
346  * @vport_type: Default SRIOV, SIOV, etc.
347  * @vport_id: Device given vport identifier
348  * @idx: Software index in adapter vports struct
349  * @default_vport: Use this vport if one isn't specified
350  * @base_rxd: True if the driver should use base descriptors instead of flex
351  * @num_q_vectors: Number of IRQ vectors allocated
352  * @q_vectors: Array of queue vectors
353  * @q_vector_idxs: Starting index of queue vectors
354  * @max_mtu: device given max possible MTU
355  * @default_mac_addr: device will give a default MAC to use
356  * @rx_itr_profile: RX profiles for Dynamic Interrupt Moderation
357  * @tx_itr_profile: TX profiles for Dynamic Interrupt Moderation
358  * @port_stats: per port csum, header split, and other offload stats
359  * @link_up: True if link is up
360  * @link_speed_mbps: Link speed in mbps
361  * @vc_msg: Virtchnl message buffer
362  * @vc_state: Virtchnl message state
363  * @vchnl_wq: Wait queue for virtchnl messages
364  * @sw_marker_wq: workqueue for marker packets
365  * @vc_buf_lock: Lock to protect virtchnl buffer
366  */
367 struct idpf_vport {
368 	u16 num_txq;
369 	u16 num_complq;
370 	u32 txq_desc_count;
371 	u32 complq_desc_count;
372 	u32 compln_clean_budget;
373 	u16 num_txq_grp;
374 	struct idpf_txq_group *txq_grps;
375 	u32 txq_model;
376 	struct idpf_queue **txqs;
377 	bool crc_enable;
378 
379 	u16 num_rxq;
380 	u16 num_bufq;
381 	u32 rxq_desc_count;
382 	u8 num_bufqs_per_qgrp;
383 	u32 bufq_desc_count[IDPF_MAX_BUFQS_PER_RXQ_GRP];
384 	u32 bufq_size[IDPF_MAX_BUFQS_PER_RXQ_GRP];
385 	u16 num_rxq_grp;
386 	struct idpf_rxq_group *rxq_grps;
387 	u32 rxq_model;
388 	struct idpf_rx_ptype_decoded rx_ptype_lkup[IDPF_RX_MAX_PTYPE];
389 
390 	struct idpf_adapter *adapter;
391 	struct net_device *netdev;
392 	DECLARE_BITMAP(flags, IDPF_VPORT_FLAGS_NBITS);
393 	u16 vport_type;
394 	u32 vport_id;
395 	u16 idx;
396 	bool default_vport;
397 	bool base_rxd;
398 
399 	u16 num_q_vectors;
400 	struct idpf_q_vector *q_vectors;
401 	u16 *q_vector_idxs;
402 	u16 max_mtu;
403 	u8 default_mac_addr[ETH_ALEN];
404 	u16 rx_itr_profile[IDPF_DIM_PROFILE_SLOTS];
405 	u16 tx_itr_profile[IDPF_DIM_PROFILE_SLOTS];
406 	struct idpf_port_stats port_stats;
407 
408 	bool link_up;
409 	u32 link_speed_mbps;
410 
411 	char vc_msg[IDPF_CTLQ_MAX_BUF_LEN];
412 	DECLARE_BITMAP(vc_state, IDPF_VC_NBITS);
413 
414 	wait_queue_head_t vchnl_wq;
415 	wait_queue_head_t sw_marker_wq;
416 	struct mutex vc_buf_lock;
417 };
418 
419 /**
420  * enum idpf_user_flags
421  * @__IDPF_USER_FLAG_HSPLIT: header split state
422  * @__IDPF_PROMISC_UC: Unicast promiscuous mode
423  * @__IDPF_PROMISC_MC: Multicast promiscuous mode
424  * @__IDPF_USER_FLAGS_NBITS: Must be last
425  */
426 enum idpf_user_flags {
427 	__IDPF_USER_FLAG_HSPLIT = 0U,
428 	__IDPF_PROMISC_UC = 32,
429 	__IDPF_PROMISC_MC,
430 
431 	__IDPF_USER_FLAGS_NBITS,
432 };
433 
434 /**
435  * struct idpf_rss_data - Associated RSS data
436  * @rss_key_size: Size of RSS hash key
437  * @rss_key: RSS hash key
438  * @rss_lut_size: Size of RSS lookup table
439  * @rss_lut: RSS lookup table
440  * @cached_lut: Used to restore previously init RSS lut
441  */
442 struct idpf_rss_data {
443 	u16 rss_key_size;
444 	u8 *rss_key;
445 	u16 rss_lut_size;
446 	u32 *rss_lut;
447 	u32 *cached_lut;
448 };
449 
450 /**
451  * struct idpf_vport_user_config_data - User defined configuration values for
452  *					each vport.
453  * @rss_data: See struct idpf_rss_data
454  * @num_req_tx_qs: Number of user requested TX queues through ethtool
455  * @num_req_rx_qs: Number of user requested RX queues through ethtool
456  * @num_req_txq_desc: Number of user requested TX queue descriptors through
457  *		      ethtool
458  * @num_req_rxq_desc: Number of user requested RX queue descriptors through
459  *		      ethtool
460  * @user_flags: User toggled config flags
461  * @mac_filter_list: List of MAC filters
462  *
463  * Used to restore configuration after a reset as the vport will get wiped.
464  */
465 struct idpf_vport_user_config_data {
466 	struct idpf_rss_data rss_data;
467 	u16 num_req_tx_qs;
468 	u16 num_req_rx_qs;
469 	u32 num_req_txq_desc;
470 	u32 num_req_rxq_desc;
471 	DECLARE_BITMAP(user_flags, __IDPF_USER_FLAGS_NBITS);
472 	struct list_head mac_filter_list;
473 };
474 
475 /**
476  * enum idpf_vport_config_flags - Vport config flags
477  * @IDPF_VPORT_REG_NETDEV: Register netdev
478  * @IDPF_VPORT_UP_REQUESTED: Set if interface up is requested on core reset
479  * @IDPF_VPORT_ADD_MAC_REQ: Asynchronous add ether address in flight
480  * @IDPF_VPORT_DEL_MAC_REQ: Asynchronous delete ether address in flight
481  * @IDPF_VPORT_CONFIG_FLAGS_NBITS: Must be last
482  */
483 enum idpf_vport_config_flags {
484 	IDPF_VPORT_REG_NETDEV,
485 	IDPF_VPORT_UP_REQUESTED,
486 	IDPF_VPORT_ADD_MAC_REQ,
487 	IDPF_VPORT_DEL_MAC_REQ,
488 	IDPF_VPORT_CONFIG_FLAGS_NBITS,
489 };
490 
491 /**
492  * struct idpf_avail_queue_info
493  * @avail_rxq: Available RX queues
494  * @avail_txq: Available TX queues
495  * @avail_bufq: Available buffer queues
496  * @avail_complq: Available completion queues
497  *
498  * Maintain total queues available after allocating max queues to each vport.
499  */
500 struct idpf_avail_queue_info {
501 	u16 avail_rxq;
502 	u16 avail_txq;
503 	u16 avail_bufq;
504 	u16 avail_complq;
505 };
506 
507 /**
508  * struct idpf_vector_info - Utility structure to pass function arguments as a
509  *			     structure
510  * @num_req_vecs: Vectors required based on the number of queues updated by the
511  *		  user via ethtool
512  * @num_curr_vecs: Current number of vectors, must be >= @num_req_vecs
513  * @index: Relative starting index for vectors
514  * @default_vport: Vectors are for default vport
515  */
516 struct idpf_vector_info {
517 	u16 num_req_vecs;
518 	u16 num_curr_vecs;
519 	u16 index;
520 	bool default_vport;
521 };
522 
523 /**
524  * struct idpf_vector_lifo - Stack to maintain vector indexes used for vector
525  *			     distribution algorithm
526  * @top: Points to stack top i.e. next available vector index
527  * @base: Always points to start of the free pool
528  * @size: Total size of the vector stack
529  * @vec_idx: Array to store all the vector indexes
530  *
531  * Vector stack maintains all the relative vector indexes at the *adapter*
532  * level. This stack is divided into 2 parts, first one is called as 'default
533  * pool' and other one is called 'free pool'.  Vector distribution algorithm
534  * gives priority to default vports in a way that at least IDPF_MIN_Q_VEC
535  * vectors are allocated per default vport and the relative vector indexes for
536  * those are maintained in default pool. Free pool contains all the unallocated
537  * vector indexes which can be allocated on-demand basis. Mailbox vector index
538  * is maintained in the default pool of the stack.
539  */
540 struct idpf_vector_lifo {
541 	u16 top;
542 	u16 base;
543 	u16 size;
544 	u16 *vec_idx;
545 };
546 
547 /**
548  * struct idpf_vport_config - Vport configuration data
549  * @user_config: see struct idpf_vport_user_config_data
550  * @max_q: Maximum possible queues
551  * @req_qs_chunks: Queue chunk data for requested queues
552  * @mac_filter_list_lock: Lock to protect mac filters
553  * @flags: See enum idpf_vport_config_flags
554  */
555 struct idpf_vport_config {
556 	struct idpf_vport_user_config_data user_config;
557 	struct idpf_vport_max_q max_q;
558 	void *req_qs_chunks;
559 	spinlock_t mac_filter_list_lock;
560 	DECLARE_BITMAP(flags, IDPF_VPORT_CONFIG_FLAGS_NBITS);
561 };
562 
563 /**
564  * struct idpf_adapter - Device data struct generated on probe
565  * @pdev: PCI device struct given on probe
566  * @virt_ver_maj: Virtchnl version major
567  * @virt_ver_min: Virtchnl version minor
568  * @msg_enable: Debug message level enabled
569  * @mb_wait_count: Number of times mailbox was attempted initialization
570  * @state: Init state machine
571  * @flags: See enum idpf_flags
572  * @reset_reg: See struct idpf_reset_reg
573  * @hw: Device access data
574  * @num_req_msix: Requested number of MSIX vectors
575  * @num_avail_msix: Available number of MSIX vectors
576  * @num_msix_entries: Number of entries in MSIX table
577  * @msix_entries: MSIX table
578  * @req_vec_chunks: Requested vector chunk data
579  * @mb_vector: Mailbox vector data
580  * @vector_stack: Stack to store the msix vector indexes
581  * @irq_mb_handler: Handler for hard interrupt for mailbox
582  * @tx_timeout_count: Number of TX timeouts that have occurred
583  * @avail_queues: Device given queue limits
584  * @vports: Array to store vports created by the driver
585  * @netdevs: Associated Vport netdevs
586  * @vport_params_reqd: Vport params requested
587  * @vport_params_recvd: Vport params received
588  * @vport_ids: Array of device given vport identifiers
589  * @vport_config: Vport config parameters
590  * @max_vports: Maximum vports that can be allocated
591  * @num_alloc_vports: Current number of vports allocated
592  * @next_vport: Next free slot in pf->vport[] - 0-based!
593  * @init_task: Initialization task
594  * @init_wq: Workqueue for initialization task
595  * @serv_task: Periodically recurring maintenance task
596  * @serv_wq: Workqueue for service task
597  * @mbx_task: Task to handle mailbox interrupts
598  * @mbx_wq: Workqueue for mailbox responses
599  * @vc_event_task: Task to handle out of band virtchnl event notifications
600  * @vc_event_wq: Workqueue for virtchnl events
601  * @stats_task: Periodic statistics retrieval task
602  * @stats_wq: Workqueue for statistics task
603  * @caps: Negotiated capabilities with device
604  * @vchnl_wq: Wait queue for virtchnl messages
605  * @vc_state: Virtchnl message state
606  * @vc_msg: Virtchnl message buffer
607  * @dev_ops: See idpf_dev_ops
608  * @num_vfs: Number of allocated VFs through sysfs. PF does not directly talk
609  *	     to VFs but is used to initialize them
610  * @crc_enable: Enable CRC insertion offload
611  * @req_tx_splitq: TX split or single queue model to request
612  * @req_rx_splitq: RX split or single queue model to request
613  * @vport_ctrl_lock: Lock to protect the vport control flow
614  * @vector_lock: Lock to protect vector distribution
615  * @queue_lock: Lock to protect queue distribution
616  * @vc_buf_lock: Lock to protect virtchnl buffer
617  */
618 struct idpf_adapter {
619 	struct pci_dev *pdev;
620 	u32 virt_ver_maj;
621 	u32 virt_ver_min;
622 
623 	u32 msg_enable;
624 	u32 mb_wait_count;
625 	enum idpf_state state;
626 	DECLARE_BITMAP(flags, IDPF_FLAGS_NBITS);
627 	struct idpf_reset_reg reset_reg;
628 	struct idpf_hw hw;
629 	u16 num_req_msix;
630 	u16 num_avail_msix;
631 	u16 num_msix_entries;
632 	struct msix_entry *msix_entries;
633 	struct virtchnl2_alloc_vectors *req_vec_chunks;
634 	struct idpf_q_vector mb_vector;
635 	struct idpf_vector_lifo vector_stack;
636 	irqreturn_t (*irq_mb_handler)(int irq, void *data);
637 
638 	u32 tx_timeout_count;
639 	struct idpf_avail_queue_info avail_queues;
640 	struct idpf_vport **vports;
641 	struct net_device **netdevs;
642 	struct virtchnl2_create_vport **vport_params_reqd;
643 	struct virtchnl2_create_vport **vport_params_recvd;
644 	u32 *vport_ids;
645 
646 	struct idpf_vport_config **vport_config;
647 	u16 max_vports;
648 	u16 num_alloc_vports;
649 	u16 next_vport;
650 
651 	struct delayed_work init_task;
652 	struct workqueue_struct *init_wq;
653 	struct delayed_work serv_task;
654 	struct workqueue_struct *serv_wq;
655 	struct delayed_work mbx_task;
656 	struct workqueue_struct *mbx_wq;
657 	struct delayed_work vc_event_task;
658 	struct workqueue_struct *vc_event_wq;
659 	struct delayed_work stats_task;
660 	struct workqueue_struct *stats_wq;
661 	struct virtchnl2_get_capabilities caps;
662 
663 	wait_queue_head_t vchnl_wq;
664 	DECLARE_BITMAP(vc_state, IDPF_VC_NBITS);
665 	char vc_msg[IDPF_CTLQ_MAX_BUF_LEN];
666 	struct idpf_dev_ops dev_ops;
667 	int num_vfs;
668 	bool crc_enable;
669 	bool req_tx_splitq;
670 	bool req_rx_splitq;
671 
672 	struct mutex vport_ctrl_lock;
673 	struct mutex vector_lock;
674 	struct mutex queue_lock;
675 	struct mutex vc_buf_lock;
676 };
677 
678 /**
679  * idpf_is_queue_model_split - check if queue model is split
680  * @q_model: queue model single or split
681  *
682  * Returns true if queue model is split else false
683  */
684 static inline int idpf_is_queue_model_split(u16 q_model)
685 {
686 	return q_model == VIRTCHNL2_QUEUE_MODEL_SPLIT;
687 }
688 
689 #define idpf_is_cap_ena(adapter, field, flag) \
690 	idpf_is_capability_ena(adapter, false, field, flag)
691 #define idpf_is_cap_ena_all(adapter, field, flag) \
692 	idpf_is_capability_ena(adapter, true, field, flag)
693 
694 bool idpf_is_capability_ena(struct idpf_adapter *adapter, bool all,
695 			    enum idpf_cap_field field, u64 flag);
696 
697 #define IDPF_CAP_RSS (\
698 	VIRTCHNL2_CAP_RSS_IPV4_TCP	|\
699 	VIRTCHNL2_CAP_RSS_IPV4_TCP	|\
700 	VIRTCHNL2_CAP_RSS_IPV4_UDP	|\
701 	VIRTCHNL2_CAP_RSS_IPV4_SCTP	|\
702 	VIRTCHNL2_CAP_RSS_IPV4_OTHER	|\
703 	VIRTCHNL2_CAP_RSS_IPV6_TCP	|\
704 	VIRTCHNL2_CAP_RSS_IPV6_TCP	|\
705 	VIRTCHNL2_CAP_RSS_IPV6_UDP	|\
706 	VIRTCHNL2_CAP_RSS_IPV6_SCTP	|\
707 	VIRTCHNL2_CAP_RSS_IPV6_OTHER)
708 
709 #define IDPF_CAP_RSC (\
710 	VIRTCHNL2_CAP_RSC_IPV4_TCP	|\
711 	VIRTCHNL2_CAP_RSC_IPV6_TCP)
712 
713 #define IDPF_CAP_HSPLIT	(\
714 	VIRTCHNL2_CAP_RX_HSPLIT_AT_L4V4	|\
715 	VIRTCHNL2_CAP_RX_HSPLIT_AT_L4V6)
716 
717 #define IDPF_CAP_RX_CSUM_L4V4 (\
718 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_TCP	|\
719 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_UDP)
720 
721 #define IDPF_CAP_RX_CSUM_L4V6 (\
722 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_TCP	|\
723 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_UDP)
724 
725 #define IDPF_CAP_RX_CSUM (\
726 	VIRTCHNL2_CAP_RX_CSUM_L3_IPV4		|\
727 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_TCP	|\
728 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_UDP	|\
729 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_TCP	|\
730 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_UDP)
731 
732 #define IDPF_CAP_SCTP_CSUM (\
733 	VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_SCTP	|\
734 	VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_SCTP	|\
735 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_SCTP	|\
736 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_SCTP)
737 
738 #define IDPF_CAP_TUNNEL_TX_CSUM (\
739 	VIRTCHNL2_CAP_TX_CSUM_L3_SINGLE_TUNNEL	|\
740 	VIRTCHNL2_CAP_TX_CSUM_L4_SINGLE_TUNNEL)
741 
742 /**
743  * idpf_get_reserved_vecs - Get reserved vectors
744  * @adapter: private data struct
745  */
746 static inline u16 idpf_get_reserved_vecs(struct idpf_adapter *adapter)
747 {
748 	return le16_to_cpu(adapter->caps.num_allocated_vectors);
749 }
750 
751 /**
752  * idpf_get_default_vports - Get default number of vports
753  * @adapter: private data struct
754  */
755 static inline u16 idpf_get_default_vports(struct idpf_adapter *adapter)
756 {
757 	return le16_to_cpu(adapter->caps.default_num_vports);
758 }
759 
760 /**
761  * idpf_get_max_vports - Get max number of vports
762  * @adapter: private data struct
763  */
764 static inline u16 idpf_get_max_vports(struct idpf_adapter *adapter)
765 {
766 	return le16_to_cpu(adapter->caps.max_vports);
767 }
768 
769 /**
770  * idpf_get_max_tx_bufs - Get max scatter-gather buffers supported by the device
771  * @adapter: private data struct
772  */
773 static inline unsigned int idpf_get_max_tx_bufs(struct idpf_adapter *adapter)
774 {
775 	return adapter->caps.max_sg_bufs_per_tx_pkt;
776 }
777 
778 /**
779  * idpf_get_min_tx_pkt_len - Get min packet length supported by the device
780  * @adapter: private data struct
781  */
782 static inline u8 idpf_get_min_tx_pkt_len(struct idpf_adapter *adapter)
783 {
784 	u8 pkt_len = adapter->caps.min_sso_packet_len;
785 
786 	return pkt_len ? pkt_len : IDPF_TX_MIN_PKT_LEN;
787 }
788 
789 /**
790  * idpf_get_reg_addr - Get BAR0 register address
791  * @adapter: private data struct
792  * @reg_offset: register offset value
793  *
794  * Based on the register offset, return the actual BAR0 register address
795  */
796 static inline void __iomem *idpf_get_reg_addr(struct idpf_adapter *adapter,
797 					      resource_size_t reg_offset)
798 {
799 	return (void __iomem *)(adapter->hw.hw_addr + reg_offset);
800 }
801 
802 /**
803  * idpf_is_reset_detected - check if we were reset at some point
804  * @adapter: driver specific private structure
805  *
806  * Returns true if we are either in reset currently or were previously reset.
807  */
808 static inline bool idpf_is_reset_detected(struct idpf_adapter *adapter)
809 {
810 	if (!adapter->hw.arq)
811 		return true;
812 
813 	return !(readl(idpf_get_reg_addr(adapter, adapter->hw.arq->reg.len)) &
814 		 adapter->hw.arq->reg.len_mask);
815 }
816 
817 /**
818  * idpf_is_reset_in_prog - check if reset is in progress
819  * @adapter: driver specific private structure
820  *
821  * Returns true if hard reset is in progress, false otherwise
822  */
823 static inline bool idpf_is_reset_in_prog(struct idpf_adapter *adapter)
824 {
825 	return (test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags) ||
826 		test_bit(IDPF_HR_FUNC_RESET, adapter->flags) ||
827 		test_bit(IDPF_HR_DRV_LOAD, adapter->flags));
828 }
829 
830 /**
831  * idpf_netdev_to_vport - get a vport handle from a netdev
832  * @netdev: network interface device structure
833  */
834 static inline struct idpf_vport *idpf_netdev_to_vport(struct net_device *netdev)
835 {
836 	struct idpf_netdev_priv *np = netdev_priv(netdev);
837 
838 	return np->vport;
839 }
840 
841 /**
842  * idpf_netdev_to_adapter - Get adapter handle from a netdev
843  * @netdev: Network interface device structure
844  */
845 static inline struct idpf_adapter *idpf_netdev_to_adapter(struct net_device *netdev)
846 {
847 	struct idpf_netdev_priv *np = netdev_priv(netdev);
848 
849 	return np->adapter;
850 }
851 
852 /**
853  * idpf_is_feature_ena - Determine if a particular feature is enabled
854  * @vport: Vport to check
855  * @feature: Netdev flag to check
856  *
857  * Returns true or false if a particular feature is enabled.
858  */
859 static inline bool idpf_is_feature_ena(const struct idpf_vport *vport,
860 				       netdev_features_t feature)
861 {
862 	return vport->netdev->features & feature;
863 }
864 
865 /**
866  * idpf_get_max_tx_hdr_size -- get the size of tx header
867  * @adapter: Driver specific private structure
868  */
869 static inline u16 idpf_get_max_tx_hdr_size(struct idpf_adapter *adapter)
870 {
871 	return le16_to_cpu(adapter->caps.max_tx_hdr_size);
872 }
873 
874 /**
875  * idpf_vport_ctrl_lock - Acquire the vport control lock
876  * @netdev: Network interface device structure
877  *
878  * This lock should be used by non-datapath code to protect against vport
879  * destruction.
880  */
881 static inline void idpf_vport_ctrl_lock(struct net_device *netdev)
882 {
883 	struct idpf_netdev_priv *np = netdev_priv(netdev);
884 
885 	mutex_lock(&np->adapter->vport_ctrl_lock);
886 }
887 
888 /**
889  * idpf_vport_ctrl_unlock - Release the vport control lock
890  * @netdev: Network interface device structure
891  */
892 static inline void idpf_vport_ctrl_unlock(struct net_device *netdev)
893 {
894 	struct idpf_netdev_priv *np = netdev_priv(netdev);
895 
896 	mutex_unlock(&np->adapter->vport_ctrl_lock);
897 }
898 
899 void idpf_statistics_task(struct work_struct *work);
900 void idpf_init_task(struct work_struct *work);
901 void idpf_service_task(struct work_struct *work);
902 void idpf_mbx_task(struct work_struct *work);
903 void idpf_vc_event_task(struct work_struct *work);
904 void idpf_dev_ops_init(struct idpf_adapter *adapter);
905 void idpf_vf_dev_ops_init(struct idpf_adapter *adapter);
906 int idpf_vport_adjust_qs(struct idpf_vport *vport);
907 int idpf_init_dflt_mbx(struct idpf_adapter *adapter);
908 void idpf_deinit_dflt_mbx(struct idpf_adapter *adapter);
909 int idpf_vc_core_init(struct idpf_adapter *adapter);
910 void idpf_vc_core_deinit(struct idpf_adapter *adapter);
911 int idpf_intr_req(struct idpf_adapter *adapter);
912 void idpf_intr_rel(struct idpf_adapter *adapter);
913 int idpf_get_reg_intr_vecs(struct idpf_vport *vport,
914 			   struct idpf_vec_regs *reg_vals);
915 u16 idpf_get_max_tx_hdr_size(struct idpf_adapter *adapter);
916 int idpf_send_delete_queues_msg(struct idpf_vport *vport);
917 int idpf_send_add_queues_msg(const struct idpf_vport *vport, u16 num_tx_q,
918 			     u16 num_complq, u16 num_rx_q, u16 num_rx_bufq);
919 int idpf_initiate_soft_reset(struct idpf_vport *vport,
920 			     enum idpf_vport_reset_cause reset_cause);
921 int idpf_send_enable_vport_msg(struct idpf_vport *vport);
922 int idpf_send_disable_vport_msg(struct idpf_vport *vport);
923 int idpf_send_destroy_vport_msg(struct idpf_vport *vport);
924 int idpf_send_get_rx_ptype_msg(struct idpf_vport *vport);
925 int idpf_send_ena_dis_loopback_msg(struct idpf_vport *vport);
926 int idpf_send_get_set_rss_key_msg(struct idpf_vport *vport, bool get);
927 int idpf_send_get_set_rss_lut_msg(struct idpf_vport *vport, bool get);
928 int idpf_send_dealloc_vectors_msg(struct idpf_adapter *adapter);
929 int idpf_send_alloc_vectors_msg(struct idpf_adapter *adapter, u16 num_vectors);
930 void idpf_deinit_task(struct idpf_adapter *adapter);
931 int idpf_req_rel_vector_indexes(struct idpf_adapter *adapter,
932 				u16 *q_vector_idxs,
933 				struct idpf_vector_info *vec_info);
934 int idpf_vport_alloc_vec_indexes(struct idpf_vport *vport);
935 int idpf_send_get_stats_msg(struct idpf_vport *vport);
936 int idpf_get_vec_ids(struct idpf_adapter *adapter,
937 		     u16 *vecids, int num_vecids,
938 		     struct virtchnl2_vector_chunks *chunks);
939 int idpf_recv_mb_msg(struct idpf_adapter *adapter, u32 op,
940 		     void *msg, int msg_size);
941 int idpf_send_mb_msg(struct idpf_adapter *adapter, u32 op,
942 		     u16 msg_size, u8 *msg);
943 void idpf_set_ethtool_ops(struct net_device *netdev);
944 int idpf_vport_alloc_max_qs(struct idpf_adapter *adapter,
945 			    struct idpf_vport_max_q *max_q);
946 void idpf_vport_dealloc_max_qs(struct idpf_adapter *adapter,
947 			       struct idpf_vport_max_q *max_q);
948 int idpf_add_del_mac_filters(struct idpf_vport *vport,
949 			     struct idpf_netdev_priv *np,
950 			     bool add, bool async);
951 int idpf_set_promiscuous(struct idpf_adapter *adapter,
952 			 struct idpf_vport_user_config_data *config_data,
953 			 u32 vport_id);
954 int idpf_send_disable_queues_msg(struct idpf_vport *vport);
955 void idpf_vport_init(struct idpf_vport *vport, struct idpf_vport_max_q *max_q);
956 u32 idpf_get_vport_id(struct idpf_vport *vport);
957 int idpf_vport_queue_ids_init(struct idpf_vport *vport);
958 int idpf_queue_reg_init(struct idpf_vport *vport);
959 int idpf_send_config_queues_msg(struct idpf_vport *vport);
960 int idpf_send_enable_queues_msg(struct idpf_vport *vport);
961 int idpf_send_create_vport_msg(struct idpf_adapter *adapter,
962 			       struct idpf_vport_max_q *max_q);
963 int idpf_check_supported_desc_ids(struct idpf_vport *vport);
964 void idpf_vport_intr_write_itr(struct idpf_q_vector *q_vector,
965 			       u16 itr, bool tx);
966 int idpf_send_map_unmap_queue_vector_msg(struct idpf_vport *vport, bool map);
967 int idpf_send_set_sriov_vfs_msg(struct idpf_adapter *adapter, u16 num_vfs);
968 int idpf_sriov_configure(struct pci_dev *pdev, int num_vfs);
969 
970 u8 idpf_vport_get_hsplit(const struct idpf_vport *vport);
971 bool idpf_vport_set_hsplit(const struct idpf_vport *vport, u8 val);
972 
973 #endif /* !_IDPF_H_ */
974