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