1 /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3 
4 /*
5  * nfp_net.h
6  * Declarations for Netronome network device driver.
7  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
8  *          Jason McMullan <jason.mcmullan@netronome.com>
9  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
10  */
11 
12 #ifndef _NFP_NET_H_
13 #define _NFP_NET_H_
14 
15 #include <linux/atomic.h>
16 #include <linux/interrupt.h>
17 #include <linux/list.h>
18 #include <linux/netdevice.h>
19 #include <linux/pci.h>
20 #include <linux/dim.h>
21 #include <linux/io-64-nonatomic-hi-lo.h>
22 #include <linux/semaphore.h>
23 #include <linux/workqueue.h>
24 #include <net/xdp.h>
25 
26 #include "nfp_net_ctrl.h"
27 
28 #define nn_pr(nn, lvl, fmt, args...)					\
29 	({								\
30 		struct nfp_net *__nn = (nn);				\
31 									\
32 		if (__nn->dp.netdev)					\
33 			netdev_printk(lvl, __nn->dp.netdev, fmt, ## args); \
34 		else							\
35 			dev_printk(lvl, __nn->dp.dev, "ctrl: " fmt, ## args); \
36 	})
37 
38 #define nn_err(nn, fmt, args...)	nn_pr(nn, KERN_ERR, fmt, ## args)
39 #define nn_warn(nn, fmt, args...)	nn_pr(nn, KERN_WARNING, fmt, ## args)
40 #define nn_info(nn, fmt, args...)	nn_pr(nn, KERN_INFO, fmt, ## args)
41 #define nn_dbg(nn, fmt, args...)	nn_pr(nn, KERN_DEBUG, fmt, ## args)
42 
43 #define nn_dp_warn(dp, fmt, args...)					\
44 	({								\
45 		struct nfp_net_dp *__dp = (dp);				\
46 									\
47 		if (unlikely(net_ratelimit())) {			\
48 			if (__dp->netdev)				\
49 				netdev_warn(__dp->netdev, fmt, ## args); \
50 			else						\
51 				dev_warn(__dp->dev, fmt, ## args);	\
52 		}							\
53 	})
54 
55 /* Max time to wait for NFP to respond on updates (in seconds) */
56 #define NFP_NET_POLL_TIMEOUT	5
57 
58 /* Interval for reading offloaded filter stats */
59 #define NFP_NET_STAT_POLL_IVL	msecs_to_jiffies(100)
60 
61 /* Bar allocation */
62 #define NFP_NET_CTRL_BAR	0
63 #define NFP_NET_Q0_BAR		2
64 #define NFP_NET_Q1_BAR		4	/* OBSOLETE */
65 
66 /* Default size for MTU and freelist buffer sizes */
67 #define NFP_NET_DEFAULT_MTU		1500U
68 
69 /* Maximum number of bytes prepended to a packet */
70 #define NFP_NET_MAX_PREPEND		64
71 
72 /* Interrupt definitions */
73 #define NFP_NET_NON_Q_VECTORS		2
74 #define NFP_NET_IRQ_LSC_IDX		0
75 #define NFP_NET_IRQ_EXN_IDX		1
76 #define NFP_NET_MIN_VNIC_IRQS		(NFP_NET_NON_Q_VECTORS + 1)
77 
78 /* Queue/Ring definitions */
79 #define NFP_NET_MAX_TX_RINGS	64	/* Max. # of Tx rings per device */
80 #define NFP_NET_MAX_RX_RINGS	64	/* Max. # of Rx rings per device */
81 #define NFP_NET_MAX_R_VECS	(NFP_NET_MAX_TX_RINGS > NFP_NET_MAX_RX_RINGS ? \
82 				 NFP_NET_MAX_TX_RINGS : NFP_NET_MAX_RX_RINGS)
83 #define NFP_NET_MAX_IRQS	(NFP_NET_NON_Q_VECTORS + NFP_NET_MAX_R_VECS)
84 
85 #define NFP_NET_TX_DESCS_DEFAULT 4096	/* Default # of Tx descs per ring */
86 #define NFP_NET_RX_DESCS_DEFAULT 4096	/* Default # of Rx descs per ring */
87 
88 #define NFP_NET_FL_BATCH	16	/* Add freelist in this Batch size */
89 #define NFP_NET_XDP_MAX_COMPLETE 2048	/* XDP bufs to reclaim in NAPI poll */
90 
91 /* Offload definitions */
92 #define NFP_NET_N_VXLAN_PORTS	(NFP_NET_CFG_VXLAN_SZ / sizeof(__be16))
93 
94 #define NFP_NET_RX_BUF_HEADROOM	(NET_SKB_PAD + NET_IP_ALIGN)
95 #define NFP_NET_RX_BUF_NON_DATA	(NFP_NET_RX_BUF_HEADROOM +		\
96 				 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
97 
98 /* Forward declarations */
99 struct nfp_cpp;
100 struct nfp_dev_info;
101 struct nfp_dp_ops;
102 struct nfp_eth_table_port;
103 struct nfp_net;
104 struct nfp_net_r_vector;
105 struct nfp_port;
106 struct xsk_buff_pool;
107 
108 struct nfp_nfd3_tx_desc;
109 struct nfp_nfd3_tx_buf;
110 
111 struct nfp_nfdk_tx_desc;
112 struct nfp_nfdk_tx_buf;
113 
114 /* Convenience macro for wrapping descriptor index on ring size */
115 #define D_IDX(ring, idx)	((idx) & ((ring)->cnt - 1))
116 
117 /* Convenience macro for writing dma address into RX/TX descriptors */
118 #define nfp_desc_set_dma_addr_40b(desc, dma_addr)			\
119 	do {								\
120 		__typeof__(desc) __d = (desc);				\
121 		dma_addr_t __addr = (dma_addr);				\
122 									\
123 		__d->dma_addr_lo = cpu_to_le32(lower_32_bits(__addr));	\
124 		__d->dma_addr_hi = upper_32_bits(__addr) & 0xff;	\
125 	} while (0)
126 
127 #define nfp_desc_set_dma_addr_48b(desc, dma_addr)			\
128 	do {								\
129 		__typeof__(desc) __d = (desc);				\
130 		dma_addr_t __addr = (dma_addr);				\
131 									\
132 		__d->dma_addr_hi = cpu_to_le16(upper_32_bits(__addr));	\
133 		__d->dma_addr_lo = cpu_to_le32(lower_32_bits(__addr));	\
134 	} while (0)
135 
136 /**
137  * struct nfp_net_tx_ring - TX ring structure
138  * @r_vec:      Back pointer to ring vector structure
139  * @idx:        Ring index from Linux's perspective
140  * @data_pending: number of bytes added to current block (NFDK only)
141  * @qcp_q:      Pointer to base of the QCP TX queue
142  * @txrwb:	TX pointer write back area
143  * @cnt:        Size of the queue in number of descriptors
144  * @wr_p:       TX ring write pointer (free running)
145  * @rd_p:       TX ring read pointer (free running)
146  * @qcp_rd_p:   Local copy of QCP TX queue read pointer
147  * @wr_ptr_add:	Accumulated number of buffers to add to QCP write pointer
148  *		(used for .xmit_more delayed kick)
149  * @txbufs:	Array of transmitted TX buffers, to free on transmit (NFD3)
150  * @ktxbufs:	Array of transmitted TX buffers, to free on transmit (NFDK)
151  * @txds:	Virtual address of TX ring in host memory (NFD3)
152  * @ktxds:	Virtual address of TX ring in host memory (NFDK)
153  *
154  * @qcidx:      Queue Controller Peripheral (QCP) queue index for the TX queue
155  * @dma:        DMA address of the TX ring
156  * @size:       Size, in bytes, of the TX ring (needed to free)
157  * @is_xdp:	Is this a XDP TX ring?
158  */
159 struct nfp_net_tx_ring {
160 	struct nfp_net_r_vector *r_vec;
161 
162 	u16 idx;
163 	u16 data_pending;
164 	u8 __iomem *qcp_q;
165 	u64 *txrwb;
166 
167 	u32 cnt;
168 	u32 wr_p;
169 	u32 rd_p;
170 	u32 qcp_rd_p;
171 
172 	u32 wr_ptr_add;
173 
174 	union {
175 		struct nfp_nfd3_tx_buf *txbufs;
176 		struct nfp_nfdk_tx_buf *ktxbufs;
177 	};
178 	union {
179 		struct nfp_nfd3_tx_desc *txds;
180 		struct nfp_nfdk_tx_desc *ktxds;
181 	};
182 
183 	/* Cold data follows */
184 	int qcidx;
185 
186 	dma_addr_t dma;
187 	size_t size;
188 	bool is_xdp;
189 } ____cacheline_aligned;
190 
191 /* RX and freelist descriptor format */
192 
193 #define PCIE_DESC_RX_DD			BIT(7)
194 #define PCIE_DESC_RX_META_LEN_MASK	GENMASK(6, 0)
195 
196 /* Flags in the RX descriptor */
197 #define PCIE_DESC_RX_RSS		cpu_to_le16(BIT(15))
198 #define PCIE_DESC_RX_I_IP4_CSUM		cpu_to_le16(BIT(14))
199 #define PCIE_DESC_RX_I_IP4_CSUM_OK	cpu_to_le16(BIT(13))
200 #define PCIE_DESC_RX_I_TCP_CSUM		cpu_to_le16(BIT(12))
201 #define PCIE_DESC_RX_I_TCP_CSUM_OK	cpu_to_le16(BIT(11))
202 #define PCIE_DESC_RX_I_UDP_CSUM		cpu_to_le16(BIT(10))
203 #define PCIE_DESC_RX_I_UDP_CSUM_OK	cpu_to_le16(BIT(9))
204 #define PCIE_DESC_RX_DECRYPTED		cpu_to_le16(BIT(8))
205 #define PCIE_DESC_RX_EOP		cpu_to_le16(BIT(7))
206 #define PCIE_DESC_RX_IP4_CSUM		cpu_to_le16(BIT(6))
207 #define PCIE_DESC_RX_IP4_CSUM_OK	cpu_to_le16(BIT(5))
208 #define PCIE_DESC_RX_TCP_CSUM		cpu_to_le16(BIT(4))
209 #define PCIE_DESC_RX_TCP_CSUM_OK	cpu_to_le16(BIT(3))
210 #define PCIE_DESC_RX_UDP_CSUM		cpu_to_le16(BIT(2))
211 #define PCIE_DESC_RX_UDP_CSUM_OK	cpu_to_le16(BIT(1))
212 #define PCIE_DESC_RX_VLAN		cpu_to_le16(BIT(0))
213 
214 #define PCIE_DESC_RX_CSUM_ALL		(PCIE_DESC_RX_IP4_CSUM |	\
215 					 PCIE_DESC_RX_TCP_CSUM |	\
216 					 PCIE_DESC_RX_UDP_CSUM |	\
217 					 PCIE_DESC_RX_I_IP4_CSUM |	\
218 					 PCIE_DESC_RX_I_TCP_CSUM |	\
219 					 PCIE_DESC_RX_I_UDP_CSUM)
220 #define PCIE_DESC_RX_CSUM_OK_SHIFT	1
221 #define __PCIE_DESC_RX_CSUM_ALL		le16_to_cpu(PCIE_DESC_RX_CSUM_ALL)
222 #define __PCIE_DESC_RX_CSUM_ALL_OK	(__PCIE_DESC_RX_CSUM_ALL >>	\
223 					 PCIE_DESC_RX_CSUM_OK_SHIFT)
224 
225 struct nfp_net_rx_desc {
226 	union {
227 		struct {
228 			__le16 dma_addr_hi; /* High bits of the buf address */
229 			u8 reserved; /* Must be zero */
230 			u8 meta_len_dd; /* Must be zero */
231 
232 			__le32 dma_addr_lo; /* Low bits of the buffer address */
233 		} __packed fld;
234 
235 		struct {
236 			__le16 data_len; /* Length of the frame + meta data */
237 			u8 reserved;
238 			u8 meta_len_dd;	/* Length of meta data prepended +
239 					 * descriptor done flag.
240 					 */
241 
242 			__le16 flags;	/* RX flags. See @PCIE_DESC_RX_* */
243 			__le16 vlan;	/* VLAN if stripped */
244 		} __packed rxd;
245 
246 		__le32 vals[2];
247 	};
248 };
249 
250 #define NFP_NET_META_FIELD_MASK GENMASK(NFP_NET_META_FIELD_SIZE - 1, 0)
251 #define NFP_NET_VLAN_CTAG	0
252 #define NFP_NET_VLAN_STAG	1
253 
254 struct nfp_meta_parsed {
255 	u8 hash_type;
256 	u8 csum_type;
257 	u32 hash;
258 	u32 mark;
259 	u32 portid;
260 	__wsum csum;
261 	struct {
262 		bool stripped;
263 		u8 tpid;
264 		u16 tci;
265 	} vlan;
266 };
267 
268 struct nfp_net_rx_hash {
269 	__be32 hash_type;
270 	__be32 hash;
271 };
272 
273 /**
274  * struct nfp_net_rx_buf - software RX buffer descriptor
275  * @frag:	page fragment buffer
276  * @dma_addr:	DMA mapping address of the buffer
277  */
278 struct nfp_net_rx_buf {
279 	void *frag;
280 	dma_addr_t dma_addr;
281 };
282 
283 /**
284  * struct nfp_net_xsk_rx_buf - software RX XSK buffer descriptor
285  * @dma_addr:	DMA mapping address of the buffer
286  * @xdp:	XSK buffer pool handle (for AF_XDP)
287  */
288 struct nfp_net_xsk_rx_buf {
289 	dma_addr_t dma_addr;
290 	struct xdp_buff *xdp;
291 };
292 
293 /**
294  * struct nfp_net_rx_ring - RX ring structure
295  * @r_vec:      Back pointer to ring vector structure
296  * @cnt:        Size of the queue in number of descriptors
297  * @wr_p:       FL/RX ring write pointer (free running)
298  * @rd_p:       FL/RX ring read pointer (free running)
299  * @idx:        Ring index from Linux's perspective
300  * @fl_qcidx:   Queue Controller Peripheral (QCP) queue index for the freelist
301  * @qcp_fl:     Pointer to base of the QCP freelist queue
302  * @rxbufs:     Array of transmitted FL/RX buffers
303  * @xsk_rxbufs: Array of transmitted FL/RX buffers (for AF_XDP)
304  * @rxds:       Virtual address of FL/RX ring in host memory
305  * @xdp_rxq:    RX-ring info avail for XDP
306  * @dma:        DMA address of the FL/RX ring
307  * @size:       Size, in bytes, of the FL/RX ring (needed to free)
308  */
309 struct nfp_net_rx_ring {
310 	struct nfp_net_r_vector *r_vec;
311 
312 	u32 cnt;
313 	u32 wr_p;
314 	u32 rd_p;
315 
316 	u32 idx;
317 
318 	int fl_qcidx;
319 	u8 __iomem *qcp_fl;
320 
321 	struct nfp_net_rx_buf *rxbufs;
322 	struct nfp_net_xsk_rx_buf *xsk_rxbufs;
323 	struct nfp_net_rx_desc *rxds;
324 
325 	struct xdp_rxq_info xdp_rxq;
326 
327 	dma_addr_t dma;
328 	size_t size;
329 } ____cacheline_aligned;
330 
331 /**
332  * struct nfp_net_r_vector - Per ring interrupt vector configuration
333  * @nfp_net:        Backpointer to nfp_net structure
334  * @napi:           NAPI structure for this ring vec
335  * @tasklet:        ctrl vNIC, tasklet for servicing the r_vec
336  * @queue:          ctrl vNIC, send queue
337  * @lock:           ctrl vNIC, r_vec lock protects @queue
338  * @tx_ring:        Pointer to TX ring
339  * @rx_ring:        Pointer to RX ring
340  * @xdp_ring:	    Pointer to an extra TX ring for XDP
341  * @xsk_pool:	    XSK buffer pool active on vector queue pair (for AF_XDP)
342  * @irq_entry:      MSI-X table entry (use for talking to the device)
343  * @event_ctr:	    Number of interrupt
344  * @rx_dim:	    Dynamic interrupt moderation structure for RX
345  * @tx_dim:	    Dynamic interrupt moderation structure for TX
346  * @rx_sync:	    Seqlock for atomic updates of RX stats
347  * @rx_pkts:        Number of received packets
348  * @rx_bytes:	    Number of received bytes
349  * @rx_drops:	    Number of packets dropped on RX due to lack of resources
350  * @hw_csum_rx_ok:  Counter of packets where the HW checksum was OK
351  * @hw_csum_rx_inner_ok: Counter of packets where the inner HW checksum was OK
352  * @hw_csum_rx_complete: Counter of packets with CHECKSUM_COMPLETE reported
353  * @hw_csum_rx_error:	 Counter of packets with bad checksums
354  * @hw_tls_rx:	    Number of packets with TLS decrypted by hardware
355  * @tx_sync:	    Seqlock for atomic updates of TX stats
356  * @tx_pkts:	    Number of Transmitted packets
357  * @tx_bytes:	    Number of Transmitted bytes
358  * @hw_csum_tx:	    Counter of packets with TX checksum offload requested
359  * @hw_csum_tx_inner:	 Counter of inner TX checksum offload requests
360  * @tx_gather:	    Counter of packets with Gather DMA
361  * @tx_lso:	    Counter of LSO packets sent
362  * @hw_tls_tx:	    Counter of TLS packets sent with crypto offloaded to HW
363  * @tls_tx_fallback:	Counter of TLS packets sent which had to be encrypted
364  *			by the fallback path because packets came out of order
365  * @tls_tx_no_fallback:	Counter of TLS packets not sent because the fallback
366  *			path could not encrypt them
367  * @tx_errors:	    How many TX errors were encountered
368  * @tx_busy:        How often was TX busy (no space)?
369  * @rx_replace_buf_alloc_fail:	Counter of RX buffer allocation failures
370  * @irq_vector:     Interrupt vector number (use for talking to the OS)
371  * @handler:        Interrupt handler for this ring vector
372  * @name:           Name of the interrupt vector
373  * @affinity_mask:  SMP affinity mask for this vector
374  *
375  * This structure ties RX and TX rings to interrupt vectors and a NAPI
376  * context. This currently only supports one RX and TX ring per
377  * interrupt vector but might be extended in the future to allow
378  * association of multiple rings per vector.
379  */
380 struct nfp_net_r_vector {
381 	struct nfp_net *nfp_net;
382 	union {
383 		struct napi_struct napi;
384 		struct {
385 			struct tasklet_struct tasklet;
386 			struct sk_buff_head queue;
387 			spinlock_t lock;
388 		};
389 	};
390 
391 	struct nfp_net_tx_ring *tx_ring;
392 	struct nfp_net_rx_ring *rx_ring;
393 
394 	u16 irq_entry;
395 
396 	u16 event_ctr;
397 	struct dim rx_dim;
398 	struct dim tx_dim;
399 
400 	struct u64_stats_sync rx_sync;
401 	u64 rx_pkts;
402 	u64 rx_bytes;
403 	u64 rx_drops;
404 	u64 hw_csum_rx_ok;
405 	u64 hw_csum_rx_inner_ok;
406 	u64 hw_csum_rx_complete;
407 	u64 hw_tls_rx;
408 
409 	u64 hw_csum_rx_error;
410 	u64 rx_replace_buf_alloc_fail;
411 
412 	struct nfp_net_tx_ring *xdp_ring;
413 	struct xsk_buff_pool *xsk_pool;
414 
415 	struct u64_stats_sync tx_sync;
416 	u64 tx_pkts;
417 	u64 tx_bytes;
418 
419 	u64 ____cacheline_aligned_in_smp hw_csum_tx;
420 	u64 hw_csum_tx_inner;
421 	u64 tx_gather;
422 	u64 tx_lso;
423 	u64 hw_tls_tx;
424 
425 	u64 tls_tx_fallback;
426 	u64 tls_tx_no_fallback;
427 	u64 tx_errors;
428 	u64 tx_busy;
429 
430 	/* Cold data follows */
431 
432 	u32 irq_vector;
433 	irq_handler_t handler;
434 	char name[IFNAMSIZ + 8];
435 	cpumask_t affinity_mask;
436 } ____cacheline_aligned;
437 
438 /* Firmware version as it is written in the 32bit value in the BAR */
439 struct nfp_net_fw_version {
440 	u8 minor;
441 	u8 major;
442 	u8 class;
443 
444 	/* This byte can be exploited for more use, currently,
445 	 * BIT0: dp type, BIT[7:1]: reserved
446 	 */
447 	u8 extend;
448 } __packed;
449 
450 static inline bool nfp_net_fw_ver_eq(struct nfp_net_fw_version *fw_ver,
451 				     u8 extend, u8 class, u8 major, u8 minor)
452 {
453 	return fw_ver->extend == extend &&
454 	       fw_ver->class == class &&
455 	       fw_ver->major == major &&
456 	       fw_ver->minor == minor;
457 }
458 
459 struct nfp_stat_pair {
460 	u64 pkts;
461 	u64 bytes;
462 };
463 
464 /**
465  * struct nfp_net_dp - NFP network device datapath data structure
466  * @dev:		Backpointer to struct device
467  * @netdev:		Backpointer to net_device structure
468  * @is_vf:		Is the driver attached to a VF?
469  * @chained_metadata_format:  Firemware will use new metadata format
470  * @ktls_tx:		Is kTLS TX enabled?
471  * @rx_dma_dir:		Mapping direction for RX buffers
472  * @rx_dma_off:		Offset at which DMA packets (for XDP headroom)
473  * @rx_offset:		Offset in the RX buffers where packet data starts
474  * @ctrl:		Local copy of the control register/word.
475  * @fl_bufsz:		Currently configured size of the freelist buffers
476  * @xdp_prog:		Installed XDP program
477  * @tx_rings:		Array of pre-allocated TX ring structures
478  * @rx_rings:		Array of pre-allocated RX ring structures
479  * @ctrl_bar:		Pointer to mapped control BAR
480  *
481  * @ops:		Callbacks and parameters for this vNIC's NFD version
482  * @txrwb:		TX pointer write back area (indexed by queue id)
483  * @txrwb_dma:		TX pointer write back area DMA address
484  * @txd_cnt:		Size of the TX ring in number of min size packets
485  * @rxd_cnt:		Size of the RX ring in number of min size packets
486  * @num_r_vecs:		Number of used ring vectors
487  * @num_tx_rings:	Currently configured number of TX rings
488  * @num_stack_tx_rings:	Number of TX rings used by the stack (not XDP)
489  * @num_rx_rings:	Currently configured number of RX rings
490  * @mtu:		Device MTU
491  * @xsk_pools:		XSK buffer pools, @max_r_vecs in size (for AF_XDP).
492  */
493 struct nfp_net_dp {
494 	struct device *dev;
495 	struct net_device *netdev;
496 
497 	u8 is_vf:1;
498 	u8 chained_metadata_format:1;
499 	u8 ktls_tx:1;
500 
501 	u8 rx_dma_dir;
502 	u8 rx_offset;
503 
504 	u32 rx_dma_off;
505 
506 	u32 ctrl;
507 	u32 fl_bufsz;
508 
509 	struct bpf_prog *xdp_prog;
510 
511 	struct nfp_net_tx_ring *tx_rings;
512 	struct nfp_net_rx_ring *rx_rings;
513 
514 	u8 __iomem *ctrl_bar;
515 
516 	/* Cold data follows */
517 
518 	const struct nfp_dp_ops *ops;
519 
520 	u64 *txrwb;
521 	dma_addr_t txrwb_dma;
522 
523 	unsigned int txd_cnt;
524 	unsigned int rxd_cnt;
525 
526 	unsigned int num_r_vecs;
527 
528 	unsigned int num_tx_rings;
529 	unsigned int num_stack_tx_rings;
530 	unsigned int num_rx_rings;
531 
532 	unsigned int mtu;
533 
534 	struct xsk_buff_pool **xsk_pools;
535 };
536 
537 /**
538  * struct nfp_net - NFP network device structure
539  * @dp:			Datapath structure
540  * @dev_info:		NFP ASIC params
541  * @id:			vNIC id within the PF (0 for VFs)
542  * @fw_ver:		Firmware version
543  * @cap:                Capabilities advertised by the Firmware
544  * @max_mtu:            Maximum support MTU advertised by the Firmware
545  * @rss_hfunc:		RSS selected hash function
546  * @rss_cfg:            RSS configuration
547  * @rss_key:            RSS secret key
548  * @rss_itbl:           RSS indirection table
549  * @xdp:		Information about the driver XDP program
550  * @xdp_hw:		Information about the HW XDP program
551  * @max_r_vecs:		Number of allocated interrupt vectors for RX/TX
552  * @max_tx_rings:       Maximum number of TX rings supported by the Firmware
553  * @max_rx_rings:       Maximum number of RX rings supported by the Firmware
554  * @stride_rx:		Queue controller RX queue spacing
555  * @stride_tx:		Queue controller TX queue spacing
556  * @r_vecs:             Pre-allocated array of ring vectors
557  * @irq_entries:        Pre-allocated array of MSI-X entries
558  * @lsc_handler:        Handler for Link State Change interrupt
559  * @lsc_name:           Name for Link State Change interrupt
560  * @exn_handler:        Handler for Exception interrupt
561  * @exn_name:           Name for Exception interrupt
562  * @shared_handler:     Handler for shared interrupts
563  * @shared_name:        Name for shared interrupt
564  * @reconfig_lock:	Protects @reconfig_posted, @reconfig_timer_active,
565  *			@reconfig_sync_present and HW reconfiguration request
566  *			regs/machinery from async requests (sync must take
567  *			@bar_lock)
568  * @reconfig_posted:	Pending reconfig bits coming from async sources
569  * @reconfig_timer_active:  Timer for reading reconfiguration results is pending
570  * @reconfig_sync_present:  Some thread is performing synchronous reconfig
571  * @reconfig_timer:	Timer for async reading of reconfig results
572  * @reconfig_in_progress_update:	Update FW is processing now (debug only)
573  * @bar_lock:		vNIC config BAR access lock, protects: update,
574  *			mailbox area, crypto TLV
575  * @link_up:            Is the link up?
576  * @link_status_lock:	Protects @link_* and ensures atomicity with BAR reading
577  * @rx_coalesce_adapt_on:   Is RX interrupt moderation adaptive?
578  * @tx_coalesce_adapt_on:   Is TX interrupt moderation adaptive?
579  * @rx_coalesce_usecs:      RX interrupt moderation usecs delay parameter
580  * @rx_coalesce_max_frames: RX interrupt moderation frame count parameter
581  * @tx_coalesce_usecs:      TX interrupt moderation usecs delay parameter
582  * @tx_coalesce_max_frames: TX interrupt moderation frame count parameter
583  * @qcp_cfg:            Pointer to QCP queue used for configuration notification
584  * @tx_bar:             Pointer to mapped TX queues
585  * @rx_bar:             Pointer to mapped FL/RX queues
586  * @tlv_caps:		Parsed TLV capabilities
587  * @ktls_tx_conn_cnt:	Number of offloaded kTLS TX connections
588  * @ktls_rx_conn_cnt:	Number of offloaded kTLS RX connections
589  * @ktls_conn_id_gen:	Trivial generator for kTLS connection ids (for TX)
590  * @ktls_no_space:	Counter of firmware rejecting kTLS connection due to
591  *			lack of space
592  * @ktls_rx_resync_req:	Counter of TLS RX resync requested
593  * @ktls_rx_resync_ign:	Counter of TLS RX resync requests ignored
594  * @ktls_rx_resync_sent:    Counter of TLS RX resync completed
595  * @mbox_cmsg:		Common Control Message via vNIC mailbox state
596  * @mbox_cmsg.queue:	CCM mbox queue of pending messages
597  * @mbox_cmsg.wq:	CCM mbox wait queue of waiting processes
598  * @mbox_cmsg.workq:	CCM mbox work queue for @wait_work and @runq_work
599  * @mbox_cmsg.wait_work:    CCM mbox posted msg reconfig wait work
600  * @mbox_cmsg.runq_work:    CCM mbox posted msg queue runner work
601  * @mbox_cmsg.tag:	CCM mbox message tag allocator
602  * @debugfs_dir:	Device directory in debugfs
603  * @vnic_list:		Entry on device vNIC list
604  * @pdev:		Backpointer to PCI device
605  * @app:		APP handle if available
606  * @vnic_no_name:	For non-port PF vNIC make ndo_get_phys_port_name return
607  *			-EOPNOTSUPP to keep backwards compatibility (set by app)
608  * @port:		Pointer to nfp_port structure if vNIC is a port
609  * @app_priv:		APP private data for this vNIC
610  */
611 struct nfp_net {
612 	struct nfp_net_dp dp;
613 
614 	const struct nfp_dev_info *dev_info;
615 	struct nfp_net_fw_version fw_ver;
616 
617 	u32 id;
618 
619 	u32 cap;
620 	u32 max_mtu;
621 
622 	u8 rss_hfunc;
623 	u32 rss_cfg;
624 	u8 rss_key[NFP_NET_CFG_RSS_KEY_SZ];
625 	u8 rss_itbl[NFP_NET_CFG_RSS_ITBL_SZ];
626 
627 	struct xdp_attachment_info xdp;
628 	struct xdp_attachment_info xdp_hw;
629 
630 	unsigned int max_tx_rings;
631 	unsigned int max_rx_rings;
632 
633 	int stride_tx;
634 	int stride_rx;
635 
636 	unsigned int max_r_vecs;
637 	struct nfp_net_r_vector r_vecs[NFP_NET_MAX_R_VECS];
638 	struct msix_entry irq_entries[NFP_NET_MAX_IRQS];
639 
640 	irq_handler_t lsc_handler;
641 	char lsc_name[IFNAMSIZ + 8];
642 
643 	irq_handler_t exn_handler;
644 	char exn_name[IFNAMSIZ + 8];
645 
646 	irq_handler_t shared_handler;
647 	char shared_name[IFNAMSIZ + 8];
648 
649 	bool link_up;
650 	spinlock_t link_status_lock;
651 
652 	spinlock_t reconfig_lock;
653 	u32 reconfig_posted;
654 	bool reconfig_timer_active;
655 	bool reconfig_sync_present;
656 	struct timer_list reconfig_timer;
657 	u32 reconfig_in_progress_update;
658 
659 	struct semaphore bar_lock;
660 
661 	bool rx_coalesce_adapt_on;
662 	bool tx_coalesce_adapt_on;
663 	u32 rx_coalesce_usecs;
664 	u32 rx_coalesce_max_frames;
665 	u32 tx_coalesce_usecs;
666 	u32 tx_coalesce_max_frames;
667 
668 	u8 __iomem *qcp_cfg;
669 
670 	u8 __iomem *tx_bar;
671 	u8 __iomem *rx_bar;
672 
673 	struct nfp_net_tlv_caps tlv_caps;
674 
675 	unsigned int ktls_tx_conn_cnt;
676 	unsigned int ktls_rx_conn_cnt;
677 
678 	atomic64_t ktls_conn_id_gen;
679 
680 	atomic_t ktls_no_space;
681 	atomic_t ktls_rx_resync_req;
682 	atomic_t ktls_rx_resync_ign;
683 	atomic_t ktls_rx_resync_sent;
684 
685 	struct {
686 		struct sk_buff_head queue;
687 		wait_queue_head_t wq;
688 		struct workqueue_struct *workq;
689 		struct work_struct wait_work;
690 		struct work_struct runq_work;
691 		u16 tag;
692 	} mbox_cmsg;
693 
694 	struct dentry *debugfs_dir;
695 
696 	struct list_head vnic_list;
697 
698 	struct pci_dev *pdev;
699 	struct nfp_app *app;
700 
701 	bool vnic_no_name;
702 
703 	struct nfp_port *port;
704 
705 	void *app_priv;
706 };
707 
708 /* Functions to read/write from/to a BAR
709  * Performs any endian conversion necessary.
710  */
711 static inline u16 nn_readb(struct nfp_net *nn, int off)
712 {
713 	return readb(nn->dp.ctrl_bar + off);
714 }
715 
716 static inline void nn_writeb(struct nfp_net *nn, int off, u8 val)
717 {
718 	writeb(val, nn->dp.ctrl_bar + off);
719 }
720 
721 static inline u16 nn_readw(struct nfp_net *nn, int off)
722 {
723 	return readw(nn->dp.ctrl_bar + off);
724 }
725 
726 static inline void nn_writew(struct nfp_net *nn, int off, u16 val)
727 {
728 	writew(val, nn->dp.ctrl_bar + off);
729 }
730 
731 static inline u32 nn_readl(struct nfp_net *nn, int off)
732 {
733 	return readl(nn->dp.ctrl_bar + off);
734 }
735 
736 static inline void nn_writel(struct nfp_net *nn, int off, u32 val)
737 {
738 	writel(val, nn->dp.ctrl_bar + off);
739 }
740 
741 static inline u64 nn_readq(struct nfp_net *nn, int off)
742 {
743 	return readq(nn->dp.ctrl_bar + off);
744 }
745 
746 static inline void nn_writeq(struct nfp_net *nn, int off, u64 val)
747 {
748 	writeq(val, nn->dp.ctrl_bar + off);
749 }
750 
751 /* Flush posted PCI writes by reading something without side effects */
752 static inline void nn_pci_flush(struct nfp_net *nn)
753 {
754 	nn_readl(nn, NFP_NET_CFG_VERSION);
755 }
756 
757 /* Queue Controller Peripheral access functions and definitions.
758  *
759  * Some of the BARs of the NFP are mapped to portions of the Queue
760  * Controller Peripheral (QCP) address space on the NFP.  A QCP queue
761  * has a read and a write pointer (as well as a size and flags,
762  * indicating overflow etc).  The QCP offers a number of different
763  * operation on queue pointers, but here we only offer function to
764  * either add to a pointer or to read the pointer value.
765  */
766 #define NFP_QCP_QUEUE_ADDR_SZ			0x800
767 #define NFP_QCP_QUEUE_OFF(_x)			((_x) * NFP_QCP_QUEUE_ADDR_SZ)
768 #define NFP_QCP_QUEUE_ADD_RPTR			0x0000
769 #define NFP_QCP_QUEUE_ADD_WPTR			0x0004
770 #define NFP_QCP_QUEUE_STS_LO			0x0008
771 #define NFP_QCP_QUEUE_STS_LO_READPTR_mask	0x3ffff
772 #define NFP_QCP_QUEUE_STS_HI			0x000c
773 #define NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask	0x3ffff
774 
775 /* nfp_qcp_ptr - Read or Write Pointer of a queue */
776 enum nfp_qcp_ptr {
777 	NFP_QCP_READ_PTR = 0,
778 	NFP_QCP_WRITE_PTR
779 };
780 
781 /**
782  * nfp_qcp_rd_ptr_add() - Add the value to the read pointer of a queue
783  *
784  * @q:   Base address for queue structure
785  * @val: Value to add to the queue pointer
786  */
787 static inline void nfp_qcp_rd_ptr_add(u8 __iomem *q, u32 val)
788 {
789 	writel(val, q + NFP_QCP_QUEUE_ADD_RPTR);
790 }
791 
792 /**
793  * nfp_qcp_wr_ptr_add() - Add the value to the write pointer of a queue
794  *
795  * @q:   Base address for queue structure
796  * @val: Value to add to the queue pointer
797  */
798 static inline void nfp_qcp_wr_ptr_add(u8 __iomem *q, u32 val)
799 {
800 	writel(val, q + NFP_QCP_QUEUE_ADD_WPTR);
801 }
802 
803 static inline u32 _nfp_qcp_read(u8 __iomem *q, enum nfp_qcp_ptr ptr)
804 {
805 	u32 off;
806 	u32 val;
807 
808 	if (ptr == NFP_QCP_READ_PTR)
809 		off = NFP_QCP_QUEUE_STS_LO;
810 	else
811 		off = NFP_QCP_QUEUE_STS_HI;
812 
813 	val = readl(q + off);
814 
815 	if (ptr == NFP_QCP_READ_PTR)
816 		return val & NFP_QCP_QUEUE_STS_LO_READPTR_mask;
817 	else
818 		return val & NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask;
819 }
820 
821 /**
822  * nfp_qcp_rd_ptr_read() - Read the current read pointer value for a queue
823  * @q:  Base address for queue structure
824  *
825  * Return: Value read.
826  */
827 static inline u32 nfp_qcp_rd_ptr_read(u8 __iomem *q)
828 {
829 	return _nfp_qcp_read(q, NFP_QCP_READ_PTR);
830 }
831 
832 /**
833  * nfp_qcp_wr_ptr_read() - Read the current write pointer value for a queue
834  * @q:  Base address for queue structure
835  *
836  * Return: Value read.
837  */
838 static inline u32 nfp_qcp_wr_ptr_read(u8 __iomem *q)
839 {
840 	return _nfp_qcp_read(q, NFP_QCP_WRITE_PTR);
841 }
842 
843 u32 nfp_qcp_queue_offset(const struct nfp_dev_info *dev_info, u16 queue);
844 
845 static inline bool nfp_net_is_data_vnic(struct nfp_net *nn)
846 {
847 	WARN_ON_ONCE(!nn->dp.netdev && nn->port);
848 	return !!nn->dp.netdev;
849 }
850 
851 static inline bool nfp_net_running(struct nfp_net *nn)
852 {
853 	return nn->dp.ctrl & NFP_NET_CFG_CTRL_ENABLE;
854 }
855 
856 static inline const char *nfp_net_name(struct nfp_net *nn)
857 {
858 	return nn->dp.netdev ? nn->dp.netdev->name : "ctrl";
859 }
860 
861 static inline void nfp_ctrl_lock(struct nfp_net *nn)
862 	__acquires(&nn->r_vecs[0].lock)
863 {
864 	spin_lock_bh(&nn->r_vecs[0].lock);
865 }
866 
867 static inline void nfp_ctrl_unlock(struct nfp_net *nn)
868 	__releases(&nn->r_vecs[0].lock)
869 {
870 	spin_unlock_bh(&nn->r_vecs[0].lock);
871 }
872 
873 static inline void nn_ctrl_bar_lock(struct nfp_net *nn)
874 {
875 	down(&nn->bar_lock);
876 }
877 
878 static inline bool nn_ctrl_bar_trylock(struct nfp_net *nn)
879 {
880 	return !down_trylock(&nn->bar_lock);
881 }
882 
883 static inline void nn_ctrl_bar_unlock(struct nfp_net *nn)
884 {
885 	up(&nn->bar_lock);
886 }
887 
888 /* Globals */
889 extern const char nfp_driver_version[];
890 
891 extern const struct net_device_ops nfp_nfd3_netdev_ops;
892 extern const struct net_device_ops nfp_nfdk_netdev_ops;
893 
894 static inline bool nfp_netdev_is_nfp_net(struct net_device *netdev)
895 {
896 	return netdev->netdev_ops == &nfp_nfd3_netdev_ops ||
897 	       netdev->netdev_ops == &nfp_nfdk_netdev_ops;
898 }
899 
900 static inline int nfp_net_coalesce_para_check(u32 usecs, u32 pkts)
901 {
902 	if ((usecs >= ((1 << 16) - 1)) || (pkts >= ((1 << 16) - 1)))
903 		return -EINVAL;
904 
905 	return 0;
906 }
907 
908 /* Prototypes */
909 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
910 			    void __iomem *ctrl_bar);
911 
912 struct nfp_net *
913 nfp_net_alloc(struct pci_dev *pdev, const struct nfp_dev_info *dev_info,
914 	      void __iomem *ctrl_bar, bool needs_netdev,
915 	      unsigned int max_tx_rings, unsigned int max_rx_rings);
916 void nfp_net_free(struct nfp_net *nn);
917 
918 int nfp_net_init(struct nfp_net *nn);
919 void nfp_net_clean(struct nfp_net *nn);
920 
921 int nfp_ctrl_open(struct nfp_net *nn);
922 void nfp_ctrl_close(struct nfp_net *nn);
923 
924 void nfp_net_set_ethtool_ops(struct net_device *netdev);
925 void nfp_net_info(struct nfp_net *nn);
926 int __nfp_net_reconfig(struct nfp_net *nn, u32 update);
927 int nfp_net_reconfig(struct nfp_net *nn, u32 update);
928 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn);
929 void nfp_net_rss_write_itbl(struct nfp_net *nn);
930 void nfp_net_rss_write_key(struct nfp_net *nn);
931 void nfp_net_coalesce_write_cfg(struct nfp_net *nn);
932 int nfp_net_mbox_lock(struct nfp_net *nn, unsigned int data_size);
933 int nfp_net_mbox_reconfig(struct nfp_net *nn, u32 mbox_cmd);
934 int nfp_net_mbox_reconfig_and_unlock(struct nfp_net *nn, u32 mbox_cmd);
935 void nfp_net_mbox_reconfig_post(struct nfp_net *nn, u32 update);
936 int nfp_net_mbox_reconfig_wait_posted(struct nfp_net *nn);
937 
938 unsigned int
939 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
940 		   unsigned int min_irqs, unsigned int want_irqs);
941 void nfp_net_irqs_disable(struct pci_dev *pdev);
942 void
943 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
944 		    unsigned int n);
945 struct sk_buff *
946 nfp_net_tls_tx(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
947 	       struct sk_buff *skb, u64 *tls_handle, int *nr_frags);
948 void nfp_net_tls_tx_undo(struct sk_buff *skb, u64 tls_handle);
949 
950 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn);
951 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *new,
952 			  struct netlink_ext_ack *extack);
953 
954 #ifdef CONFIG_NFP_DEBUG
955 void nfp_net_debugfs_create(void);
956 void nfp_net_debugfs_destroy(void);
957 struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev);
958 void nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir);
959 void nfp_net_debugfs_dir_clean(struct dentry **dir);
960 #else
961 static inline void nfp_net_debugfs_create(void)
962 {
963 }
964 
965 static inline void nfp_net_debugfs_destroy(void)
966 {
967 }
968 
969 static inline struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev)
970 {
971 	return NULL;
972 }
973 
974 static inline void
975 nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir)
976 {
977 }
978 
979 static inline void nfp_net_debugfs_dir_clean(struct dentry **dir)
980 {
981 }
982 #endif /* CONFIG_NFP_DEBUG */
983 
984 #endif /* _NFP_NET_H_ */
985