xref: /linux/drivers/net/ethernet/intel/idpf/idpf.h (revision 8a5f956a9fb7d74fff681145082acfad5afa6bb8)
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/ioport.h>
16 #include <linux/pci.h>
17 #include <linux/bitfield.h>
18 #include <linux/sctp.h>
19 #include <linux/ethtool_netlink.h>
20 #include <net/gro.h>
21 
22 #include <linux/net/intel/iidc_rdma.h>
23 #include <linux/net/intel/iidc_rdma_idpf.h>
24 
25 #include "virtchnl2.h"
26 #include "idpf_txrx.h"
27 #include "idpf_controlq.h"
28 
29 #define GETMAXVAL(num_bits)		GENMASK((num_bits) - 1, 0)
30 
31 #define IDPF_NO_FREE_SLOT		0xffff
32 
33 /* Default Mailbox settings */
34 #define IDPF_NUM_FILTERS_PER_MSG	20
35 #define IDPF_NUM_DFLT_MBX_Q		2	/* includes both TX and RX */
36 #define IDPF_DFLT_MBX_Q_LEN		64
37 #define IDPF_DFLT_MBX_ID		-1
38 /* maximum number of times to try before resetting mailbox */
39 #define IDPF_MB_MAX_ERR			20
40 #define IDPF_NUM_CHUNKS_PER_MSG(struct_sz, chunk_sz)	\
41 	((IDPF_CTLQ_MAX_BUF_LEN - (struct_sz)) / (chunk_sz))
42 
43 #define IDPF_WAIT_FOR_MARKER_TIMEO	500
44 #define IDPF_MAX_WAIT			500
45 
46 /* available message levels */
47 #define IDPF_AVAIL_NETIF_M (NETIF_MSG_DRV | NETIF_MSG_PROBE | NETIF_MSG_LINK)
48 
49 #define IDPF_DIM_PROFILE_SLOTS  5
50 
51 #define IDPF_VIRTCHNL_VERSION_MAJOR VIRTCHNL2_VERSION_MAJOR_2
52 #define IDPF_VIRTCHNL_VERSION_MINOR VIRTCHNL2_VERSION_MINOR_0
53 
54 /**
55  * struct idpf_mac_filter
56  * @list: list member field
57  * @macaddr: MAC address
58  * @remove: filter should be removed (virtchnl)
59  * @add: filter should be added (virtchnl)
60  */
61 struct idpf_mac_filter {
62 	struct list_head list;
63 	u8 macaddr[ETH_ALEN];
64 	bool remove;
65 	bool add;
66 };
67 
68 /**
69  * enum idpf_state - State machine to handle bring up
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_VER_CHECK,
77 	__IDPF_GET_CAPS,
78 	__IDPF_INIT_SW,
79 	__IDPF_STATE_LAST,
80 };
81 
82 /**
83  * enum idpf_flags - Hard reset causes.
84  * @IDPF_HR_FUNC_RESET: Hard reset when TxRx timeout
85  * @IDPF_HR_DRV_LOAD: Set on driver load for a clean HW
86  * @IDPF_HR_RESET_IN_PROG: Reset in progress
87  * @IDPF_REMOVE_IN_PROG: Driver remove in progress
88  * @IDPF_MB_INTR_MODE: Mailbox in interrupt mode
89  * @IDPF_VC_CORE_INIT: virtchnl core has been init
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_VC_CORE_INIT,
99 	IDPF_FLAGS_NBITS,
100 };
101 
102 /**
103  * enum idpf_cap_field - Offsets into capabilities struct for specific caps
104  * @IDPF_BASE_CAPS: generic base capabilities
105  * @IDPF_CSUM_CAPS: checksum offload capabilities
106  * @IDPF_SEG_CAPS: segmentation offload capabilities
107  * @IDPF_RSS_CAPS: RSS offload capabilities
108  * @IDPF_HSPLIT_CAPS: Header split capabilities
109  * @IDPF_RSC_CAPS: RSC offload capabilities
110  * @IDPF_OTHER_CAPS: miscellaneous offloads
111  *
112  * Used when checking for a specific capability flag since different capability
113  * sets are not mutually exclusive numerically, the caller must specify which
114  * type of capability they are checking for.
115  */
116 enum idpf_cap_field {
117 	IDPF_BASE_CAPS		= -1,
118 	IDPF_CSUM_CAPS		= offsetof(struct virtchnl2_get_capabilities,
119 					   csum_caps),
120 	IDPF_SEG_CAPS		= offsetof(struct virtchnl2_get_capabilities,
121 					   seg_caps),
122 	IDPF_RSS_CAPS		= offsetof(struct virtchnl2_get_capabilities,
123 					   rss_caps),
124 	IDPF_HSPLIT_CAPS	= offsetof(struct virtchnl2_get_capabilities,
125 					   hsplit_caps),
126 	IDPF_RSC_CAPS		= offsetof(struct virtchnl2_get_capabilities,
127 					   rsc_caps),
128 	IDPF_OTHER_CAPS		= offsetof(struct virtchnl2_get_capabilities,
129 					   other_caps),
130 };
131 
132 /**
133  * enum idpf_vport_state - Current vport state
134  * @__IDPF_VPORT_DOWN: Vport is down
135  * @__IDPF_VPORT_UP: Vport is up
136  * @__IDPF_VPORT_STATE_LAST: Must be last, number of states
137  */
138 enum idpf_vport_state {
139 	__IDPF_VPORT_DOWN,
140 	__IDPF_VPORT_UP,
141 	__IDPF_VPORT_STATE_LAST,
142 };
143 
144 /**
145  * struct idpf_netdev_priv - Struct to store vport back pointer
146  * @adapter: Adapter back pointer
147  * @vport: Vport back pointer
148  * @vport_id: Vport identifier
149  * @link_speed_mbps: Link speed in mbps
150  * @vport_idx: Relative vport index
151  * @max_tx_hdr_size: Max header length hardware can support
152  * @tx_max_bufs: Max buffers that can be transmitted with scatter-gather
153  * @state: See enum idpf_vport_state
154  * @netstats: Packet and byte stats
155  * @stats_lock: Lock to protect stats update
156  */
157 struct idpf_netdev_priv {
158 	struct idpf_adapter *adapter;
159 	struct idpf_vport *vport;
160 	u32 vport_id;
161 	u32 link_speed_mbps;
162 	u16 vport_idx;
163 	u16 max_tx_hdr_size;
164 	u16 tx_max_bufs;
165 	enum idpf_vport_state state;
166 	struct rtnl_link_stats64 netstats;
167 	spinlock_t stats_lock;
168 };
169 
170 /**
171  * struct idpf_reset_reg - Reset register offsets/masks
172  * @rstat: Reset status register
173  * @rstat_m: Reset status mask
174  */
175 struct idpf_reset_reg {
176 	void __iomem *rstat;
177 	u32 rstat_m;
178 };
179 
180 /**
181  * struct idpf_vport_max_q - Queue limits
182  * @max_rxq: Maximum number of RX queues supported
183  * @max_txq: Maixmum number of TX queues supported
184  * @max_bufq: In splitq, maximum number of buffer queues supported
185  * @max_complq: In splitq, maximum number of completion queues supported
186  */
187 struct idpf_vport_max_q {
188 	u16 max_rxq;
189 	u16 max_txq;
190 	u16 max_bufq;
191 	u16 max_complq;
192 };
193 
194 /**
195  * struct idpf_reg_ops - Device specific register operation function pointers
196  * @ctlq_reg_init: Mailbox control queue register initialization
197  * @intr_reg_init: Traffic interrupt register initialization
198  * @mb_intr_reg_init: Mailbox interrupt register initialization
199  * @reset_reg_init: Reset register initialization
200  * @trigger_reset: Trigger a reset to occur
201  * @ptp_reg_init: PTP register initialization
202  */
203 struct idpf_reg_ops {
204 	void (*ctlq_reg_init)(struct idpf_adapter *adapter,
205 			      struct idpf_ctlq_create_info *cq);
206 	int (*intr_reg_init)(struct idpf_vport *vport);
207 	void (*mb_intr_reg_init)(struct idpf_adapter *adapter);
208 	void (*reset_reg_init)(struct idpf_adapter *adapter);
209 	void (*trigger_reset)(struct idpf_adapter *adapter,
210 			      enum idpf_flags trig_cause);
211 	void (*ptp_reg_init)(const struct idpf_adapter *adapter);
212 };
213 
214 #define IDPF_MMIO_REG_NUM_STATIC	2
215 #define IDPF_PF_MBX_REGION_SZ		4096
216 #define IDPF_PF_RSTAT_REGION_SZ		2048
217 #define IDPF_VF_MBX_REGION_SZ		10240
218 #define IDPF_VF_RSTAT_REGION_SZ		2048
219 
220 /**
221  * struct idpf_dev_ops - Device specific operations
222  * @reg_ops: Register operations
223  * @idc_init: IDC initialization
224  * @static_reg_info: array of mailbox and rstat register info
225  */
226 struct idpf_dev_ops {
227 	struct idpf_reg_ops reg_ops;
228 
229 	int (*idc_init)(struct idpf_adapter *adapter);
230 
231 	/* static_reg_info[0] is mailbox region, static_reg_info[1] is rstat */
232 	struct resource static_reg_info[IDPF_MMIO_REG_NUM_STATIC];
233 };
234 
235 /**
236  * enum idpf_vport_reset_cause - Vport soft reset causes
237  * @IDPF_SR_Q_CHANGE: Soft reset queue change
238  * @IDPF_SR_Q_DESC_CHANGE: Soft reset descriptor change
239  * @IDPF_SR_MTU_CHANGE: Soft reset MTU change
240  * @IDPF_SR_RSC_CHANGE: Soft reset RSC change
241  */
242 enum idpf_vport_reset_cause {
243 	IDPF_SR_Q_CHANGE,
244 	IDPF_SR_Q_DESC_CHANGE,
245 	IDPF_SR_MTU_CHANGE,
246 	IDPF_SR_RSC_CHANGE,
247 };
248 
249 /**
250  * enum idpf_vport_flags - Vport flags
251  * @IDPF_VPORT_DEL_QUEUES: To send delete queues message
252  * @IDPF_VPORT_FLAGS_NBITS: Must be last
253  */
254 enum idpf_vport_flags {
255 	IDPF_VPORT_DEL_QUEUES,
256 	IDPF_VPORT_FLAGS_NBITS,
257 };
258 
259 /**
260  * struct idpf_tstamp_stats - Tx timestamp statistics
261  * @stats_sync: See struct u64_stats_sync
262  * @packets: Number of packets successfully timestamped by the hardware
263  * @discarded: Number of Tx skbs discarded due to cached PHC
264  *	       being too old to correctly extend timestamp
265  * @flushed: Number of Tx skbs flushed due to interface closed
266  */
267 struct idpf_tstamp_stats {
268 	struct u64_stats_sync stats_sync;
269 	u64_stats_t packets;
270 	u64_stats_t discarded;
271 	u64_stats_t flushed;
272 };
273 
274 struct idpf_port_stats {
275 	struct u64_stats_sync stats_sync;
276 	u64_stats_t rx_hw_csum_err;
277 	u64_stats_t rx_hsplit;
278 	u64_stats_t rx_hsplit_hbo;
279 	u64_stats_t rx_bad_descs;
280 	u64_stats_t tx_linearize;
281 	u64_stats_t tx_busy;
282 	u64_stats_t tx_drops;
283 	u64_stats_t tx_dma_map_errs;
284 	struct virtchnl2_vport_stats vport_stats;
285 };
286 
287 struct idpf_fsteer_fltr {
288 	struct list_head list;
289 	u32 loc;
290 	u32 q_index;
291 };
292 
293 /**
294  * struct idpf_vport - Handle for netdevices and queue resources
295  * @num_txq: Number of allocated TX queues
296  * @num_complq: Number of allocated completion queues
297  * @txq_desc_count: TX queue descriptor count
298  * @complq_desc_count: Completion queue descriptor count
299  * @compln_clean_budget: Work budget for completion clean
300  * @num_txq_grp: Number of TX queue groups
301  * @txq_grps: Array of TX queue groups
302  * @txq_model: Split queue or single queue queuing model
303  * @txqs: Used only in hotpath to get to the right queue very fast
304  * @crc_enable: Enable CRC insertion offload
305  * @xdpsq_share: whether XDPSQ sharing is enabled
306  * @num_xdp_txq: number of XDPSQs
307  * @xdp_txq_offset: index of the first XDPSQ (== number of regular SQs)
308  * @xdp_prog: installed XDP program
309  * @num_rxq: Number of allocated RX queues
310  * @num_bufq: Number of allocated buffer queues
311  * @rxq_desc_count: RX queue descriptor count. *MUST* have enough descriptors
312  *		    to complete all buffer descriptors for all buffer queues in
313  *		    the worst case.
314  * @num_bufqs_per_qgrp: Buffer queues per RX queue in a given grouping
315  * @bufq_desc_count: Buffer queue descriptor count
316  * @num_rxq_grp: Number of RX queues in a group
317  * @rxq_grps: Total number of RX groups. Number of groups * number of RX per
318  *	      group will yield total number of RX queues.
319  * @rxq_model: Splitq queue or single queue queuing model
320  * @rx_ptype_lkup: Lookup table for ptypes on RX
321  * @vdev_info: IDC vport device info pointer
322  * @adapter: back pointer to associated adapter
323  * @netdev: Associated net_device. Each vport should have one and only one
324  *	    associated netdev.
325  * @flags: See enum idpf_vport_flags
326  * @vport_type: Default SRIOV, SIOV, etc.
327  * @vport_id: Device given vport identifier
328  * @idx: Software index in adapter vports struct
329  * @default_vport: Use this vport if one isn't specified
330  * @base_rxd: True if the driver should use base descriptors instead of flex
331  * @num_q_vectors: Number of IRQ vectors allocated
332  * @q_vectors: Array of queue vectors
333  * @q_vector_idxs: Starting index of queue vectors
334  * @noirq_dyn_ctl: register to enable/disable the vector for NOIRQ queues
335  * @noirq_dyn_ctl_ena: value to write to the above to enable it
336  * @noirq_v_idx: ID of the NOIRQ vector
337  * @max_mtu: device given max possible MTU
338  * @default_mac_addr: device will give a default MAC to use
339  * @rx_itr_profile: RX profiles for Dynamic Interrupt Moderation
340  * @tx_itr_profile: TX profiles for Dynamic Interrupt Moderation
341  * @port_stats: per port csum, header split, and other offload stats
342  * @link_up: True if link is up
343  * @tx_tstamp_caps: Capabilities negotiated for Tx timestamping
344  * @tstamp_config: The Tx tstamp config
345  * @tstamp_task: Tx timestamping task
346  * @tstamp_stats: Tx timestamping statistics
347  */
348 struct idpf_vport {
349 	u16 num_txq;
350 	u16 num_complq;
351 	u32 txq_desc_count;
352 	u32 complq_desc_count;
353 	u32 compln_clean_budget;
354 	u16 num_txq_grp;
355 	struct idpf_txq_group *txq_grps;
356 	u32 txq_model;
357 	struct idpf_tx_queue **txqs;
358 	bool crc_enable;
359 
360 	bool xdpsq_share;
361 	u16 num_xdp_txq;
362 	u16 xdp_txq_offset;
363 	struct bpf_prog *xdp_prog;
364 
365 	u16 num_rxq;
366 	u16 num_bufq;
367 	u32 rxq_desc_count;
368 	u8 num_bufqs_per_qgrp;
369 	u32 bufq_desc_count[IDPF_MAX_BUFQS_PER_RXQ_GRP];
370 	u16 num_rxq_grp;
371 	struct idpf_rxq_group *rxq_grps;
372 	u32 rxq_model;
373 	struct libeth_rx_pt *rx_ptype_lkup;
374 
375 	struct iidc_rdma_vport_dev_info *vdev_info;
376 
377 	struct idpf_adapter *adapter;
378 	struct net_device *netdev;
379 	DECLARE_BITMAP(flags, IDPF_VPORT_FLAGS_NBITS);
380 	u16 vport_type;
381 	u32 vport_id;
382 	u16 idx;
383 	bool default_vport;
384 	bool base_rxd;
385 
386 	u16 num_q_vectors;
387 	struct idpf_q_vector *q_vectors;
388 	u16 *q_vector_idxs;
389 
390 	void __iomem *noirq_dyn_ctl;
391 	u32 noirq_dyn_ctl_ena;
392 	u16 noirq_v_idx;
393 
394 	u16 max_mtu;
395 	u8 default_mac_addr[ETH_ALEN];
396 	u16 rx_itr_profile[IDPF_DIM_PROFILE_SLOTS];
397 	u16 tx_itr_profile[IDPF_DIM_PROFILE_SLOTS];
398 	struct idpf_port_stats port_stats;
399 
400 	bool link_up;
401 
402 	struct idpf_ptp_vport_tx_tstamp_caps *tx_tstamp_caps;
403 	struct kernel_hwtstamp_config tstamp_config;
404 	struct work_struct tstamp_task;
405 	struct idpf_tstamp_stats tstamp_stats;
406 };
407 
408 /**
409  * enum idpf_user_flags
410  * @__IDPF_USER_FLAG_HSPLIT: header split state
411  * @__IDPF_PROMISC_UC: Unicast promiscuous mode
412  * @__IDPF_PROMISC_MC: Multicast promiscuous mode
413  * @__IDPF_USER_FLAGS_NBITS: Must be last
414  */
415 enum idpf_user_flags {
416 	__IDPF_USER_FLAG_HSPLIT = 0U,
417 	__IDPF_PROMISC_UC = 32,
418 	__IDPF_PROMISC_MC,
419 
420 	__IDPF_USER_FLAGS_NBITS,
421 };
422 
423 /**
424  * struct idpf_rss_data - Associated RSS data
425  * @rss_key_size: Size of RSS hash key
426  * @rss_key: RSS hash key
427  * @rss_lut_size: Size of RSS lookup table
428  * @rss_lut: RSS lookup table
429  * @cached_lut: Used to restore previously init RSS lut
430  */
431 struct idpf_rss_data {
432 	u16 rss_key_size;
433 	u8 *rss_key;
434 	u16 rss_lut_size;
435 	u32 *rss_lut;
436 	u32 *cached_lut;
437 };
438 
439 /**
440  * struct idpf_q_coalesce - User defined coalescing configuration values for
441  *			   a single queue.
442  * @tx_intr_mode: Dynamic TX ITR or not
443  * @rx_intr_mode: Dynamic RX ITR or not
444  * @tx_coalesce_usecs: TX interrupt throttling rate
445  * @rx_coalesce_usecs: RX interrupt throttling rate
446  *
447  * Used to restore user coalescing configuration after a reset.
448  */
449 struct idpf_q_coalesce {
450 	u32 tx_intr_mode;
451 	u32 rx_intr_mode;
452 	u32 tx_coalesce_usecs;
453 	u32 rx_coalesce_usecs;
454 };
455 
456 /**
457  * struct idpf_vport_user_config_data - User defined configuration values for
458  *					each vport.
459  * @rss_data: See struct idpf_rss_data
460  * @q_coalesce: Array of per queue coalescing data
461  * @num_req_tx_qs: Number of user requested TX queues through ethtool
462  * @num_req_rx_qs: Number of user requested RX queues through ethtool
463  * @num_req_txq_desc: Number of user requested TX queue descriptors through
464  *		      ethtool
465  * @num_req_rxq_desc: Number of user requested RX queue descriptors through
466  *		      ethtool
467  * @xdp_prog: requested XDP program to install
468  * @user_flags: User toggled config flags
469  * @mac_filter_list: List of MAC filters
470  * @num_fsteer_fltrs: number of flow steering filters
471  * @flow_steer_list: list of flow steering filters
472  *
473  * Used to restore configuration after a reset as the vport will get wiped.
474  */
475 struct idpf_vport_user_config_data {
476 	struct idpf_rss_data rss_data;
477 	struct idpf_q_coalesce *q_coalesce;
478 	u16 num_req_tx_qs;
479 	u16 num_req_rx_qs;
480 	u32 num_req_txq_desc;
481 	u32 num_req_rxq_desc;
482 	struct bpf_prog *xdp_prog;
483 	DECLARE_BITMAP(user_flags, __IDPF_USER_FLAGS_NBITS);
484 	struct list_head mac_filter_list;
485 	u32 num_fsteer_fltrs;
486 	struct list_head flow_steer_list;
487 };
488 
489 /**
490  * enum idpf_vport_config_flags - Vport config flags
491  * @IDPF_VPORT_REG_NETDEV: Register netdev
492  * @IDPF_VPORT_UP_REQUESTED: Set if interface up is requested on core reset
493  * @IDPF_VPORT_CONFIG_FLAGS_NBITS: Must be last
494  */
495 enum idpf_vport_config_flags {
496 	IDPF_VPORT_REG_NETDEV,
497 	IDPF_VPORT_UP_REQUESTED,
498 	IDPF_VPORT_CONFIG_FLAGS_NBITS,
499 };
500 
501 /**
502  * struct idpf_avail_queue_info
503  * @avail_rxq: Available RX queues
504  * @avail_txq: Available TX queues
505  * @avail_bufq: Available buffer queues
506  * @avail_complq: Available completion queues
507  *
508  * Maintain total queues available after allocating max queues to each vport.
509  */
510 struct idpf_avail_queue_info {
511 	u16 avail_rxq;
512 	u16 avail_txq;
513 	u16 avail_bufq;
514 	u16 avail_complq;
515 };
516 
517 /**
518  * struct idpf_vector_info - Utility structure to pass function arguments as a
519  *			     structure
520  * @num_req_vecs: Vectors required based on the number of queues updated by the
521  *		  user via ethtool
522  * @num_curr_vecs: Current number of vectors, must be >= @num_req_vecs
523  * @index: Relative starting index for vectors
524  * @default_vport: Vectors are for default vport
525  */
526 struct idpf_vector_info {
527 	u16 num_req_vecs;
528 	u16 num_curr_vecs;
529 	u16 index;
530 	bool default_vport;
531 };
532 
533 /**
534  * struct idpf_vector_lifo - Stack to maintain vector indexes used for vector
535  *			     distribution algorithm
536  * @top: Points to stack top i.e. next available vector index
537  * @base: Always points to start of the free pool
538  * @size: Total size of the vector stack
539  * @vec_idx: Array to store all the vector indexes
540  *
541  * Vector stack maintains all the relative vector indexes at the *adapter*
542  * level. This stack is divided into 2 parts, first one is called as 'default
543  * pool' and other one is called 'free pool'.  Vector distribution algorithm
544  * gives priority to default vports in a way that at least IDPF_MIN_Q_VEC
545  * vectors are allocated per default vport and the relative vector indexes for
546  * those are maintained in default pool. Free pool contains all the unallocated
547  * vector indexes which can be allocated on-demand basis. Mailbox vector index
548  * is maintained in the default pool of the stack.
549  */
550 struct idpf_vector_lifo {
551 	u16 top;
552 	u16 base;
553 	u16 size;
554 	u16 *vec_idx;
555 };
556 
557 /**
558  * struct idpf_vport_config - Vport configuration data
559  * @user_config: see struct idpf_vport_user_config_data
560  * @max_q: Maximum possible queues
561  * @req_qs_chunks: Queue chunk data for requested queues
562  * @mac_filter_list_lock: Lock to protect mac filters
563  * @flags: See enum idpf_vport_config_flags
564  */
565 struct idpf_vport_config {
566 	struct idpf_vport_user_config_data user_config;
567 	struct idpf_vport_max_q max_q;
568 	struct virtchnl2_add_queues *req_qs_chunks;
569 	spinlock_t mac_filter_list_lock;
570 	DECLARE_BITMAP(flags, IDPF_VPORT_CONFIG_FLAGS_NBITS);
571 };
572 
573 struct idpf_vc_xn_manager;
574 
575 #define idpf_for_each_vport(adapter, iter) \
576 	for (struct idpf_vport **__##iter = &(adapter)->vports[0], \
577 	     *iter = (adapter)->max_vports ? *__##iter : NULL; \
578 	     iter; \
579 	     iter = (++__##iter) < &(adapter)->vports[(adapter)->max_vports] ? \
580 	     *__##iter : NULL)
581 
582 /**
583  * struct idpf_adapter - Device data struct generated on probe
584  * @pdev: PCI device struct given on probe
585  * @virt_ver_maj: Virtchnl version major
586  * @virt_ver_min: Virtchnl version minor
587  * @msg_enable: Debug message level enabled
588  * @mb_wait_count: Number of times mailbox was attempted initialization
589  * @state: Init state machine
590  * @flags: See enum idpf_flags
591  * @reset_reg: See struct idpf_reset_reg
592  * @hw: Device access data
593  * @num_avail_msix: Available number of MSIX vectors
594  * @num_msix_entries: Number of entries in MSIX table
595  * @msix_entries: MSIX table
596  * @num_rdma_msix_entries: Available number of MSIX vectors for RDMA
597  * @rdma_msix_entries: RDMA MSIX table
598  * @req_vec_chunks: Requested vector chunk data
599  * @mb_vector: Mailbox vector data
600  * @vector_stack: Stack to store the msix vector indexes
601  * @irq_mb_handler: Handler for hard interrupt for mailbox
602  * @tx_timeout_count: Number of TX timeouts that have occurred
603  * @avail_queues: Device given queue limits
604  * @vports: Array to store vports created by the driver
605  * @netdevs: Associated Vport netdevs
606  * @vport_params_reqd: Vport params requested
607  * @vport_params_recvd: Vport params received
608  * @vport_ids: Array of device given vport identifiers
609  * @vport_config: Vport config parameters
610  * @max_vports: Maximum vports that can be allocated
611  * @num_alloc_vports: Current number of vports allocated
612  * @next_vport: Next free slot in pf->vport[] - 0-based!
613  * @init_task: Initialization task
614  * @init_wq: Workqueue for initialization task
615  * @serv_task: Periodically recurring maintenance task
616  * @serv_wq: Workqueue for service task
617  * @mbx_task: Task to handle mailbox interrupts
618  * @mbx_wq: Workqueue for mailbox responses
619  * @vc_event_task: Task to handle out of band virtchnl event notifications
620  * @vc_event_wq: Workqueue for virtchnl events
621  * @stats_task: Periodic statistics retrieval task
622  * @stats_wq: Workqueue for statistics task
623  * @caps: Negotiated capabilities with device
624  * @vcxn_mngr: Virtchnl transaction manager
625  * @dev_ops: See idpf_dev_ops
626  * @cdev_info: IDC core device info pointer
627  * @num_vfs: Number of allocated VFs through sysfs. PF does not directly talk
628  *	     to VFs but is used to initialize them
629  * @crc_enable: Enable CRC insertion offload
630  * @req_tx_splitq: TX split or single queue model to request
631  * @req_rx_splitq: RX split or single queue model to request
632  * @vport_ctrl_lock: Lock to protect the vport control flow
633  * @vector_lock: Lock to protect vector distribution
634  * @queue_lock: Lock to protect queue distribution
635  * @vc_buf_lock: Lock to protect virtchnl buffer
636  * @ptp: Storage for PTP-related data
637  */
638 struct idpf_adapter {
639 	struct pci_dev *pdev;
640 	u32 virt_ver_maj;
641 	u32 virt_ver_min;
642 
643 	u32 msg_enable;
644 	u32 mb_wait_count;
645 	enum idpf_state state;
646 	DECLARE_BITMAP(flags, IDPF_FLAGS_NBITS);
647 	struct idpf_reset_reg reset_reg;
648 	struct idpf_hw hw;
649 	u16 num_avail_msix;
650 	u16 num_msix_entries;
651 	struct msix_entry *msix_entries;
652 	u16 num_rdma_msix_entries;
653 	struct msix_entry *rdma_msix_entries;
654 	struct virtchnl2_alloc_vectors *req_vec_chunks;
655 	struct idpf_q_vector mb_vector;
656 	struct idpf_vector_lifo vector_stack;
657 	irqreturn_t (*irq_mb_handler)(int irq, void *data);
658 
659 	u32 tx_timeout_count;
660 	struct idpf_avail_queue_info avail_queues;
661 	struct idpf_vport **vports;
662 	struct net_device **netdevs;
663 	struct virtchnl2_create_vport **vport_params_reqd;
664 	struct virtchnl2_create_vport **vport_params_recvd;
665 	u32 *vport_ids;
666 
667 	struct idpf_vport_config **vport_config;
668 	u16 max_vports;
669 	u16 num_alloc_vports;
670 	u16 next_vport;
671 
672 	struct delayed_work init_task;
673 	struct workqueue_struct *init_wq;
674 	struct delayed_work serv_task;
675 	struct workqueue_struct *serv_wq;
676 	struct delayed_work mbx_task;
677 	struct workqueue_struct *mbx_wq;
678 	struct delayed_work vc_event_task;
679 	struct workqueue_struct *vc_event_wq;
680 	struct delayed_work stats_task;
681 	struct workqueue_struct *stats_wq;
682 	struct virtchnl2_get_capabilities caps;
683 	struct idpf_vc_xn_manager *vcxn_mngr;
684 
685 	struct idpf_dev_ops dev_ops;
686 	struct iidc_rdma_core_dev_info *cdev_info;
687 	int num_vfs;
688 	bool crc_enable;
689 	bool req_tx_splitq;
690 	bool req_rx_splitq;
691 
692 	struct mutex vport_ctrl_lock;
693 	struct mutex vector_lock;
694 	struct mutex queue_lock;
695 	struct mutex vc_buf_lock;
696 
697 	struct idpf_ptp *ptp;
698 };
699 
700 /**
701  * idpf_is_queue_model_split - check if queue model is split
702  * @q_model: queue model single or split
703  *
704  * Returns true if queue model is split else false
705  */
706 static inline int idpf_is_queue_model_split(u16 q_model)
707 {
708 	return !IS_ENABLED(CONFIG_IDPF_SINGLEQ) ||
709 	       q_model == VIRTCHNL2_QUEUE_MODEL_SPLIT;
710 }
711 
712 static inline bool idpf_xdp_enabled(const struct idpf_vport *vport)
713 {
714 	return vport->adapter && vport->xdp_prog;
715 }
716 
717 #define idpf_is_cap_ena(adapter, field, flag) \
718 	idpf_is_capability_ena(adapter, false, field, flag)
719 #define idpf_is_cap_ena_all(adapter, field, flag) \
720 	idpf_is_capability_ena(adapter, true, field, flag)
721 
722 bool idpf_is_capability_ena(struct idpf_adapter *adapter, bool all,
723 			    enum idpf_cap_field field, u64 flag);
724 
725 /**
726  * idpf_is_rdma_cap_ena - Determine if RDMA is supported
727  * @adapter: private data struct
728  *
729  * Return: true if RDMA capability is enabled, false otherwise
730  */
731 static inline bool idpf_is_rdma_cap_ena(struct idpf_adapter *adapter)
732 {
733 	return idpf_is_cap_ena(adapter, IDPF_OTHER_CAPS, VIRTCHNL2_CAP_RDMA);
734 }
735 
736 #define IDPF_CAP_RSS (\
737 	VIRTCHNL2_FLOW_IPV4_TCP		|\
738 	VIRTCHNL2_FLOW_IPV4_TCP		|\
739 	VIRTCHNL2_FLOW_IPV4_UDP		|\
740 	VIRTCHNL2_FLOW_IPV4_SCTP	|\
741 	VIRTCHNL2_FLOW_IPV4_OTHER	|\
742 	VIRTCHNL2_FLOW_IPV6_TCP		|\
743 	VIRTCHNL2_FLOW_IPV6_TCP		|\
744 	VIRTCHNL2_FLOW_IPV6_UDP		|\
745 	VIRTCHNL2_FLOW_IPV6_SCTP	|\
746 	VIRTCHNL2_FLOW_IPV6_OTHER)
747 
748 #define IDPF_CAP_RSC (\
749 	VIRTCHNL2_CAP_RSC_IPV4_TCP	|\
750 	VIRTCHNL2_CAP_RSC_IPV6_TCP)
751 
752 #define IDPF_CAP_HSPLIT	(\
753 	VIRTCHNL2_CAP_RX_HSPLIT_AT_L4V4	|\
754 	VIRTCHNL2_CAP_RX_HSPLIT_AT_L4V6)
755 
756 #define IDPF_CAP_TX_CSUM_L4V4 (\
757 	VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_TCP	|\
758 	VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_UDP)
759 
760 #define IDPF_CAP_TX_CSUM_L4V6 (\
761 	VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_TCP	|\
762 	VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_UDP)
763 
764 #define IDPF_CAP_RX_CSUM (\
765 	VIRTCHNL2_CAP_RX_CSUM_L3_IPV4		|\
766 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_TCP	|\
767 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV4_UDP	|\
768 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_TCP	|\
769 	VIRTCHNL2_CAP_RX_CSUM_L4_IPV6_UDP)
770 
771 #define IDPF_CAP_TX_SCTP_CSUM (\
772 	VIRTCHNL2_CAP_TX_CSUM_L4_IPV4_SCTP	|\
773 	VIRTCHNL2_CAP_TX_CSUM_L4_IPV6_SCTP)
774 
775 #define IDPF_CAP_TUNNEL_TX_CSUM (\
776 	VIRTCHNL2_CAP_TX_CSUM_L3_SINGLE_TUNNEL	|\
777 	VIRTCHNL2_CAP_TX_CSUM_L4_SINGLE_TUNNEL)
778 
779 /**
780  * idpf_get_reserved_vecs - Get reserved vectors
781  * @adapter: private data struct
782  */
783 static inline u16 idpf_get_reserved_vecs(struct idpf_adapter *adapter)
784 {
785 	return le16_to_cpu(adapter->caps.num_allocated_vectors);
786 }
787 
788 /**
789  * idpf_get_reserved_rdma_vecs - Get reserved RDMA vectors
790  * @adapter: private data struct
791  *
792  * Return: number of vectors reserved for RDMA
793  */
794 static inline u16 idpf_get_reserved_rdma_vecs(struct idpf_adapter *adapter)
795 {
796 	return le16_to_cpu(adapter->caps.num_rdma_allocated_vectors);
797 }
798 
799 /**
800  * idpf_get_default_vports - Get default number of vports
801  * @adapter: private data struct
802  */
803 static inline u16 idpf_get_default_vports(struct idpf_adapter *adapter)
804 {
805 	return le16_to_cpu(adapter->caps.default_num_vports);
806 }
807 
808 /**
809  * idpf_get_max_vports - Get max number of vports
810  * @adapter: private data struct
811  */
812 static inline u16 idpf_get_max_vports(struct idpf_adapter *adapter)
813 {
814 	return le16_to_cpu(adapter->caps.max_vports);
815 }
816 
817 /**
818  * idpf_get_max_tx_bufs - Get max scatter-gather buffers supported by the device
819  * @adapter: private data struct
820  */
821 static inline unsigned int idpf_get_max_tx_bufs(struct idpf_adapter *adapter)
822 {
823 	return adapter->caps.max_sg_bufs_per_tx_pkt;
824 }
825 
826 /**
827  * idpf_get_min_tx_pkt_len - Get min packet length supported by the device
828  * @adapter: private data struct
829  */
830 static inline u8 idpf_get_min_tx_pkt_len(struct idpf_adapter *adapter)
831 {
832 	u8 pkt_len = adapter->caps.min_sso_packet_len;
833 
834 	return pkt_len ? pkt_len : IDPF_TX_MIN_PKT_LEN;
835 }
836 
837 /**
838  * idpf_get_mbx_reg_addr - Get BAR0 mailbox register address
839  * @adapter: private data struct
840  * @reg_offset: register offset value
841  *
842  * Return: BAR0 mailbox register address based on register offset.
843  */
844 static inline void __iomem *idpf_get_mbx_reg_addr(struct idpf_adapter *adapter,
845 						  resource_size_t reg_offset)
846 {
847 	return adapter->hw.mbx.vaddr + reg_offset;
848 }
849 
850 /**
851  * idpf_get_rstat_reg_addr - Get BAR0 rstat register address
852  * @adapter: private data struct
853  * @reg_offset: register offset value
854  *
855  * Return: BAR0 rstat register address based on register offset.
856  */
857 static inline void __iomem *idpf_get_rstat_reg_addr(struct idpf_adapter *adapter,
858 						    resource_size_t reg_offset)
859 {
860 	reg_offset -= adapter->dev_ops.static_reg_info[1].start;
861 
862 	return adapter->hw.rstat.vaddr + reg_offset;
863 }
864 
865 /**
866  * idpf_get_reg_addr - Get BAR0 register address
867  * @adapter: private data struct
868  * @reg_offset: register offset value
869  *
870  * Based on the register offset, return the actual BAR0 register address
871  */
872 static inline void __iomem *idpf_get_reg_addr(struct idpf_adapter *adapter,
873 					      resource_size_t reg_offset)
874 {
875 	struct idpf_hw *hw = &adapter->hw;
876 
877 	for (int i = 0; i < hw->num_lan_regs; i++) {
878 		struct idpf_mmio_reg *region = &hw->lan_regs[i];
879 
880 		if (reg_offset >= region->addr_start &&
881 		    reg_offset < (region->addr_start + region->addr_len)) {
882 			/* Convert the offset so that it is relative to the
883 			 * start of the region.  Then add the base address of
884 			 * the region to get the final address.
885 			 */
886 			reg_offset -= region->addr_start;
887 
888 			return region->vaddr + reg_offset;
889 		}
890 	}
891 
892 	/* It's impossible to hit this case with offsets from the CP. But if we
893 	 * do for any other reason, the kernel will panic on that register
894 	 * access. Might as well do it here to make it clear what's happening.
895 	 */
896 	BUG();
897 
898 	return NULL;
899 }
900 
901 /**
902  * idpf_is_reset_detected - check if we were reset at some point
903  * @adapter: driver specific private structure
904  *
905  * Returns true if we are either in reset currently or were previously reset.
906  */
907 static inline bool idpf_is_reset_detected(struct idpf_adapter *adapter)
908 {
909 	if (!adapter->hw.arq)
910 		return true;
911 
912 	return !(readl(idpf_get_mbx_reg_addr(adapter, adapter->hw.arq->reg.len)) &
913 		 adapter->hw.arq->reg.len_mask);
914 }
915 
916 /**
917  * idpf_is_reset_in_prog - check if reset is in progress
918  * @adapter: driver specific private structure
919  *
920  * Returns true if hard reset is in progress, false otherwise
921  */
922 static inline bool idpf_is_reset_in_prog(struct idpf_adapter *adapter)
923 {
924 	return (test_bit(IDPF_HR_RESET_IN_PROG, adapter->flags) ||
925 		test_bit(IDPF_HR_FUNC_RESET, adapter->flags) ||
926 		test_bit(IDPF_HR_DRV_LOAD, adapter->flags));
927 }
928 
929 /**
930  * idpf_netdev_to_vport - get a vport handle from a netdev
931  * @netdev: network interface device structure
932  */
933 static inline struct idpf_vport *idpf_netdev_to_vport(struct net_device *netdev)
934 {
935 	struct idpf_netdev_priv *np = netdev_priv(netdev);
936 
937 	return np->vport;
938 }
939 
940 /**
941  * idpf_netdev_to_adapter - Get adapter handle from a netdev
942  * @netdev: Network interface device structure
943  */
944 static inline struct idpf_adapter *idpf_netdev_to_adapter(struct net_device *netdev)
945 {
946 	struct idpf_netdev_priv *np = netdev_priv(netdev);
947 
948 	return np->adapter;
949 }
950 
951 /**
952  * idpf_is_feature_ena - Determine if a particular feature is enabled
953  * @vport: Vport to check
954  * @feature: Netdev flag to check
955  *
956  * Returns true or false if a particular feature is enabled.
957  */
958 static inline bool idpf_is_feature_ena(const struct idpf_vport *vport,
959 				       netdev_features_t feature)
960 {
961 	return vport->netdev->features & feature;
962 }
963 
964 /**
965  * idpf_get_max_tx_hdr_size -- get the size of tx header
966  * @adapter: Driver specific private structure
967  */
968 static inline u16 idpf_get_max_tx_hdr_size(struct idpf_adapter *adapter)
969 {
970 	return le16_to_cpu(adapter->caps.max_tx_hdr_size);
971 }
972 
973 /**
974  * idpf_vport_ctrl_lock - Acquire the vport control lock
975  * @netdev: Network interface device structure
976  *
977  * This lock should be used by non-datapath code to protect against vport
978  * destruction.
979  */
980 static inline void idpf_vport_ctrl_lock(struct net_device *netdev)
981 {
982 	struct idpf_netdev_priv *np = netdev_priv(netdev);
983 
984 	mutex_lock(&np->adapter->vport_ctrl_lock);
985 }
986 
987 /**
988  * idpf_vport_ctrl_unlock - Release the vport control lock
989  * @netdev: Network interface device structure
990  */
991 static inline void idpf_vport_ctrl_unlock(struct net_device *netdev)
992 {
993 	struct idpf_netdev_priv *np = netdev_priv(netdev);
994 
995 	mutex_unlock(&np->adapter->vport_ctrl_lock);
996 }
997 
998 static inline bool idpf_vport_ctrl_is_locked(struct net_device *netdev)
999 {
1000 	struct idpf_netdev_priv *np = netdev_priv(netdev);
1001 
1002 	return mutex_is_locked(&np->adapter->vport_ctrl_lock);
1003 }
1004 
1005 void idpf_statistics_task(struct work_struct *work);
1006 void idpf_init_task(struct work_struct *work);
1007 void idpf_service_task(struct work_struct *work);
1008 void idpf_mbx_task(struct work_struct *work);
1009 void idpf_vc_event_task(struct work_struct *work);
1010 void idpf_dev_ops_init(struct idpf_adapter *adapter);
1011 void idpf_vf_dev_ops_init(struct idpf_adapter *adapter);
1012 int idpf_intr_req(struct idpf_adapter *adapter);
1013 void idpf_intr_rel(struct idpf_adapter *adapter);
1014 u16 idpf_get_max_tx_hdr_size(struct idpf_adapter *adapter);
1015 int idpf_initiate_soft_reset(struct idpf_vport *vport,
1016 			     enum idpf_vport_reset_cause reset_cause);
1017 void idpf_deinit_task(struct idpf_adapter *adapter);
1018 int idpf_req_rel_vector_indexes(struct idpf_adapter *adapter,
1019 				u16 *q_vector_idxs,
1020 				struct idpf_vector_info *vec_info);
1021 void idpf_set_ethtool_ops(struct net_device *netdev);
1022 void idpf_vport_intr_write_itr(struct idpf_q_vector *q_vector,
1023 			       u16 itr, bool tx);
1024 int idpf_sriov_configure(struct pci_dev *pdev, int num_vfs);
1025 
1026 u8 idpf_vport_get_hsplit(const struct idpf_vport *vport);
1027 bool idpf_vport_set_hsplit(const struct idpf_vport *vport, u8 val);
1028 int idpf_idc_init(struct idpf_adapter *adapter);
1029 int idpf_idc_init_aux_core_dev(struct idpf_adapter *adapter,
1030 			       enum iidc_function_type ftype);
1031 void idpf_idc_deinit_core_aux_device(struct iidc_rdma_core_dev_info *cdev_info);
1032 void idpf_idc_deinit_vport_aux_device(struct iidc_rdma_vport_dev_info *vdev_info);
1033 void idpf_idc_issue_reset_event(struct iidc_rdma_core_dev_info *cdev_info);
1034 void idpf_idc_vdev_mtu_event(struct iidc_rdma_vport_dev_info *vdev_info,
1035 			     enum iidc_rdma_event_type event_type);
1036 
1037 int idpf_add_del_fsteer_filters(struct idpf_adapter *adapter,
1038 				struct virtchnl2_flow_rule_add_del *rule,
1039 				enum virtchnl2_op opcode);
1040 #endif /* !_IDPF_H_ */
1041