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 #ifdef CONFIG_NFP_NET_IPSEC
268 	u32 ipsec_saidx;
269 #endif
270 };
271 
272 struct nfp_net_rx_hash {
273 	__be32 hash_type;
274 	__be32 hash;
275 };
276 
277 /**
278  * struct nfp_net_rx_buf - software RX buffer descriptor
279  * @frag:	page fragment buffer
280  * @dma_addr:	DMA mapping address of the buffer
281  */
282 struct nfp_net_rx_buf {
283 	void *frag;
284 	dma_addr_t dma_addr;
285 };
286 
287 /**
288  * struct nfp_net_xsk_rx_buf - software RX XSK buffer descriptor
289  * @dma_addr:	DMA mapping address of the buffer
290  * @xdp:	XSK buffer pool handle (for AF_XDP)
291  */
292 struct nfp_net_xsk_rx_buf {
293 	dma_addr_t dma_addr;
294 	struct xdp_buff *xdp;
295 };
296 
297 /**
298  * struct nfp_net_rx_ring - RX ring structure
299  * @r_vec:      Back pointer to ring vector structure
300  * @cnt:        Size of the queue in number of descriptors
301  * @wr_p:       FL/RX ring write pointer (free running)
302  * @rd_p:       FL/RX ring read pointer (free running)
303  * @idx:        Ring index from Linux's perspective
304  * @fl_qcidx:   Queue Controller Peripheral (QCP) queue index for the freelist
305  * @qcp_fl:     Pointer to base of the QCP freelist queue
306  * @rxbufs:     Array of transmitted FL/RX buffers
307  * @xsk_rxbufs: Array of transmitted FL/RX buffers (for AF_XDP)
308  * @rxds:       Virtual address of FL/RX ring in host memory
309  * @xdp_rxq:    RX-ring info avail for XDP
310  * @dma:        DMA address of the FL/RX ring
311  * @size:       Size, in bytes, of the FL/RX ring (needed to free)
312  */
313 struct nfp_net_rx_ring {
314 	struct nfp_net_r_vector *r_vec;
315 
316 	u32 cnt;
317 	u32 wr_p;
318 	u32 rd_p;
319 
320 	u32 idx;
321 
322 	int fl_qcidx;
323 	u8 __iomem *qcp_fl;
324 
325 	struct nfp_net_rx_buf *rxbufs;
326 	struct nfp_net_xsk_rx_buf *xsk_rxbufs;
327 	struct nfp_net_rx_desc *rxds;
328 
329 	struct xdp_rxq_info xdp_rxq;
330 
331 	dma_addr_t dma;
332 	size_t size;
333 } ____cacheline_aligned;
334 
335 /**
336  * struct nfp_net_r_vector - Per ring interrupt vector configuration
337  * @nfp_net:        Backpointer to nfp_net structure
338  * @napi:           NAPI structure for this ring vec
339  * @tasklet:        ctrl vNIC, tasklet for servicing the r_vec
340  * @queue:          ctrl vNIC, send queue
341  * @lock:           ctrl vNIC, r_vec lock protects @queue
342  * @tx_ring:        Pointer to TX ring
343  * @rx_ring:        Pointer to RX ring
344  * @xdp_ring:	    Pointer to an extra TX ring for XDP
345  * @xsk_pool:	    XSK buffer pool active on vector queue pair (for AF_XDP)
346  * @irq_entry:      MSI-X table entry (use for talking to the device)
347  * @event_ctr:	    Number of interrupt
348  * @rx_dim:	    Dynamic interrupt moderation structure for RX
349  * @tx_dim:	    Dynamic interrupt moderation structure for TX
350  * @rx_sync:	    Seqlock for atomic updates of RX stats
351  * @rx_pkts:        Number of received packets
352  * @rx_bytes:	    Number of received bytes
353  * @rx_drops:	    Number of packets dropped on RX due to lack of resources
354  * @hw_csum_rx_ok:  Counter of packets where the HW checksum was OK
355  * @hw_csum_rx_inner_ok: Counter of packets where the inner HW checksum was OK
356  * @hw_csum_rx_complete: Counter of packets with CHECKSUM_COMPLETE reported
357  * @hw_csum_rx_error:	 Counter of packets with bad checksums
358  * @hw_tls_rx:	    Number of packets with TLS decrypted by hardware
359  * @tx_sync:	    Seqlock for atomic updates of TX stats
360  * @tx_pkts:	    Number of Transmitted packets
361  * @tx_bytes:	    Number of Transmitted bytes
362  * @hw_csum_tx:	    Counter of packets with TX checksum offload requested
363  * @hw_csum_tx_inner:	 Counter of inner TX checksum offload requests
364  * @tx_gather:	    Counter of packets with Gather DMA
365  * @tx_lso:	    Counter of LSO packets sent
366  * @hw_tls_tx:	    Counter of TLS packets sent with crypto offloaded to HW
367  * @tls_tx_fallback:	Counter of TLS packets sent which had to be encrypted
368  *			by the fallback path because packets came out of order
369  * @tls_tx_no_fallback:	Counter of TLS packets not sent because the fallback
370  *			path could not encrypt them
371  * @tx_errors:	    How many TX errors were encountered
372  * @tx_busy:        How often was TX busy (no space)?
373  * @rx_replace_buf_alloc_fail:	Counter of RX buffer allocation failures
374  * @irq_vector:     Interrupt vector number (use for talking to the OS)
375  * @handler:        Interrupt handler for this ring vector
376  * @name:           Name of the interrupt vector
377  * @affinity_mask:  SMP affinity mask for this vector
378  *
379  * This structure ties RX and TX rings to interrupt vectors and a NAPI
380  * context. This currently only supports one RX and TX ring per
381  * interrupt vector but might be extended in the future to allow
382  * association of multiple rings per vector.
383  */
384 struct nfp_net_r_vector {
385 	struct nfp_net *nfp_net;
386 	union {
387 		struct napi_struct napi;
388 		struct {
389 			struct tasklet_struct tasklet;
390 			struct sk_buff_head queue;
391 			spinlock_t lock;
392 		};
393 	};
394 
395 	struct nfp_net_tx_ring *tx_ring;
396 	struct nfp_net_rx_ring *rx_ring;
397 
398 	u16 irq_entry;
399 
400 	u16 event_ctr;
401 	struct dim rx_dim;
402 	struct dim tx_dim;
403 
404 	struct u64_stats_sync rx_sync;
405 	u64 rx_pkts;
406 	u64 rx_bytes;
407 	u64 rx_drops;
408 	u64 hw_csum_rx_ok;
409 	u64 hw_csum_rx_inner_ok;
410 	u64 hw_csum_rx_complete;
411 	u64 hw_tls_rx;
412 
413 	u64 hw_csum_rx_error;
414 	u64 rx_replace_buf_alloc_fail;
415 
416 	struct nfp_net_tx_ring *xdp_ring;
417 	struct xsk_buff_pool *xsk_pool;
418 
419 	struct u64_stats_sync tx_sync;
420 	u64 tx_pkts;
421 	u64 tx_bytes;
422 
423 	u64 ____cacheline_aligned_in_smp hw_csum_tx;
424 	u64 hw_csum_tx_inner;
425 	u64 tx_gather;
426 	u64 tx_lso;
427 	u64 hw_tls_tx;
428 
429 	u64 tls_tx_fallback;
430 	u64 tls_tx_no_fallback;
431 	u64 tx_errors;
432 	u64 tx_busy;
433 
434 	/* Cold data follows */
435 
436 	u32 irq_vector;
437 	irq_handler_t handler;
438 	char name[IFNAMSIZ + 8];
439 	cpumask_t affinity_mask;
440 } ____cacheline_aligned;
441 
442 /* Firmware version as it is written in the 32bit value in the BAR */
443 struct nfp_net_fw_version {
444 	u8 minor;
445 	u8 major;
446 	u8 class;
447 
448 	/* This byte can be exploited for more use, currently,
449 	 * BIT0: dp type, BIT[7:1]: reserved
450 	 */
451 	u8 extend;
452 } __packed;
453 
454 static inline bool nfp_net_fw_ver_eq(struct nfp_net_fw_version *fw_ver,
455 				     u8 extend, u8 class, u8 major, u8 minor)
456 {
457 	return fw_ver->extend == extend &&
458 	       fw_ver->class == class &&
459 	       fw_ver->major == major &&
460 	       fw_ver->minor == minor;
461 }
462 
463 struct nfp_stat_pair {
464 	u64 pkts;
465 	u64 bytes;
466 };
467 
468 /**
469  * struct nfp_net_dp - NFP network device datapath data structure
470  * @dev:		Backpointer to struct device
471  * @netdev:		Backpointer to net_device structure
472  * @is_vf:		Is the driver attached to a VF?
473  * @chained_metadata_format:  Firemware will use new metadata format
474  * @ktls_tx:		Is kTLS TX enabled?
475  * @rx_dma_dir:		Mapping direction for RX buffers
476  * @rx_dma_off:		Offset at which DMA packets (for XDP headroom)
477  * @rx_offset:		Offset in the RX buffers where packet data starts
478  * @ctrl:		Local copy of the control register/word.
479  * @fl_bufsz:		Currently configured size of the freelist buffers
480  * @xdp_prog:		Installed XDP program
481  * @tx_rings:		Array of pre-allocated TX ring structures
482  * @rx_rings:		Array of pre-allocated RX ring structures
483  * @ctrl_bar:		Pointer to mapped control BAR
484  *
485  * @ops:		Callbacks and parameters for this vNIC's NFD version
486  * @txrwb:		TX pointer write back area (indexed by queue id)
487  * @txrwb_dma:		TX pointer write back area DMA address
488  * @txd_cnt:		Size of the TX ring in number of min size packets
489  * @rxd_cnt:		Size of the RX ring in number of min size packets
490  * @num_r_vecs:		Number of used ring vectors
491  * @num_tx_rings:	Currently configured number of TX rings
492  * @num_stack_tx_rings:	Number of TX rings used by the stack (not XDP)
493  * @num_rx_rings:	Currently configured number of RX rings
494  * @mtu:		Device MTU
495  * @xsk_pools:		XSK buffer pools, @max_r_vecs in size (for AF_XDP).
496  */
497 struct nfp_net_dp {
498 	struct device *dev;
499 	struct net_device *netdev;
500 
501 	u8 is_vf:1;
502 	u8 chained_metadata_format:1;
503 	u8 ktls_tx:1;
504 
505 	u8 rx_dma_dir;
506 	u8 rx_offset;
507 
508 	u32 rx_dma_off;
509 
510 	u32 ctrl;
511 	u32 fl_bufsz;
512 
513 	struct bpf_prog *xdp_prog;
514 
515 	struct nfp_net_tx_ring *tx_rings;
516 	struct nfp_net_rx_ring *rx_rings;
517 
518 	u8 __iomem *ctrl_bar;
519 
520 	/* Cold data follows */
521 
522 	const struct nfp_dp_ops *ops;
523 
524 	u64 *txrwb;
525 	dma_addr_t txrwb_dma;
526 
527 	unsigned int txd_cnt;
528 	unsigned int rxd_cnt;
529 
530 	unsigned int num_r_vecs;
531 
532 	unsigned int num_tx_rings;
533 	unsigned int num_stack_tx_rings;
534 	unsigned int num_rx_rings;
535 
536 	unsigned int mtu;
537 
538 	struct xsk_buff_pool **xsk_pools;
539 };
540 
541 /**
542  * struct nfp_net - NFP network device structure
543  * @dp:			Datapath structure
544  * @dev_info:		NFP ASIC params
545  * @id:			vNIC id within the PF (0 for VFs)
546  * @fw_ver:		Firmware version
547  * @cap:                Capabilities advertised by the Firmware
548  * @cap_w1:             Extended capabilities word advertised by the Firmware
549  * @max_mtu:            Maximum support MTU advertised by the Firmware
550  * @rss_hfunc:		RSS selected hash function
551  * @rss_cfg:            RSS configuration
552  * @rss_key:            RSS secret key
553  * @rss_itbl:           RSS indirection table
554  * @xdp:		Information about the driver XDP program
555  * @xdp_hw:		Information about the HW XDP program
556  * @max_r_vecs:		Number of allocated interrupt vectors for RX/TX
557  * @max_tx_rings:       Maximum number of TX rings supported by the Firmware
558  * @max_rx_rings:       Maximum number of RX rings supported by the Firmware
559  * @stride_rx:		Queue controller RX queue spacing
560  * @stride_tx:		Queue controller TX queue spacing
561  * @r_vecs:             Pre-allocated array of ring vectors
562  * @irq_entries:        Pre-allocated array of MSI-X entries
563  * @lsc_handler:        Handler for Link State Change interrupt
564  * @lsc_name:           Name for Link State Change interrupt
565  * @exn_handler:        Handler for Exception interrupt
566  * @exn_name:           Name for Exception interrupt
567  * @shared_handler:     Handler for shared interrupts
568  * @shared_name:        Name for shared interrupt
569  * @reconfig_lock:	Protects @reconfig_posted, @reconfig_timer_active,
570  *			@reconfig_sync_present and HW reconfiguration request
571  *			regs/machinery from async requests (sync must take
572  *			@bar_lock)
573  * @reconfig_posted:	Pending reconfig bits coming from async sources
574  * @reconfig_timer_active:  Timer for reading reconfiguration results is pending
575  * @reconfig_sync_present:  Some thread is performing synchronous reconfig
576  * @reconfig_timer:	Timer for async reading of reconfig results
577  * @reconfig_in_progress_update:	Update FW is processing now (debug only)
578  * @bar_lock:		vNIC config BAR access lock, protects: update,
579  *			mailbox area, crypto TLV
580  * @link_up:            Is the link up?
581  * @link_status_lock:	Protects @link_* and ensures atomicity with BAR reading
582  * @rx_coalesce_adapt_on:   Is RX interrupt moderation adaptive?
583  * @tx_coalesce_adapt_on:   Is TX interrupt moderation adaptive?
584  * @rx_coalesce_usecs:      RX interrupt moderation usecs delay parameter
585  * @rx_coalesce_max_frames: RX interrupt moderation frame count parameter
586  * @tx_coalesce_usecs:      TX interrupt moderation usecs delay parameter
587  * @tx_coalesce_max_frames: TX interrupt moderation frame count parameter
588  * @qcp_cfg:            Pointer to QCP queue used for configuration notification
589  * @tx_bar:             Pointer to mapped TX queues
590  * @rx_bar:             Pointer to mapped FL/RX queues
591  * @xa_ipsec:           IPsec xarray SA data
592  * @tlv_caps:		Parsed TLV capabilities
593  * @ktls_tx_conn_cnt:	Number of offloaded kTLS TX connections
594  * @ktls_rx_conn_cnt:	Number of offloaded kTLS RX connections
595  * @ktls_conn_id_gen:	Trivial generator for kTLS connection ids (for TX)
596  * @ktls_no_space:	Counter of firmware rejecting kTLS connection due to
597  *			lack of space
598  * @ktls_rx_resync_req:	Counter of TLS RX resync requested
599  * @ktls_rx_resync_ign:	Counter of TLS RX resync requests ignored
600  * @ktls_rx_resync_sent:    Counter of TLS RX resync completed
601  * @mbox_cmsg:		Common Control Message via vNIC mailbox state
602  * @mbox_cmsg.queue:	CCM mbox queue of pending messages
603  * @mbox_cmsg.wq:	CCM mbox wait queue of waiting processes
604  * @mbox_cmsg.workq:	CCM mbox work queue for @wait_work and @runq_work
605  * @mbox_cmsg.wait_work:    CCM mbox posted msg reconfig wait work
606  * @mbox_cmsg.runq_work:    CCM mbox posted msg queue runner work
607  * @mbox_cmsg.tag:	CCM mbox message tag allocator
608  * @debugfs_dir:	Device directory in debugfs
609  * @vnic_list:		Entry on device vNIC list
610  * @pdev:		Backpointer to PCI device
611  * @app:		APP handle if available
612  * @vnic_no_name:	For non-port PF vNIC make ndo_get_phys_port_name return
613  *			-EOPNOTSUPP to keep backwards compatibility (set by app)
614  * @port:		Pointer to nfp_port structure if vNIC is a port
615  * @app_priv:		APP private data for this vNIC
616  */
617 struct nfp_net {
618 	struct nfp_net_dp dp;
619 
620 	const struct nfp_dev_info *dev_info;
621 	struct nfp_net_fw_version fw_ver;
622 
623 	u32 id;
624 
625 	u32 cap;
626 	u32 cap_w1;
627 	u32 max_mtu;
628 
629 	u8 rss_hfunc;
630 	u32 rss_cfg;
631 	u8 rss_key[NFP_NET_CFG_RSS_KEY_SZ];
632 	u8 rss_itbl[NFP_NET_CFG_RSS_ITBL_SZ];
633 
634 	struct xdp_attachment_info xdp;
635 	struct xdp_attachment_info xdp_hw;
636 
637 	unsigned int max_tx_rings;
638 	unsigned int max_rx_rings;
639 
640 	int stride_tx;
641 	int stride_rx;
642 
643 	unsigned int max_r_vecs;
644 	struct nfp_net_r_vector r_vecs[NFP_NET_MAX_R_VECS];
645 	struct msix_entry irq_entries[NFP_NET_MAX_IRQS];
646 
647 	irq_handler_t lsc_handler;
648 	char lsc_name[IFNAMSIZ + 8];
649 
650 	irq_handler_t exn_handler;
651 	char exn_name[IFNAMSIZ + 8];
652 
653 	irq_handler_t shared_handler;
654 	char shared_name[IFNAMSIZ + 8];
655 
656 	bool link_up;
657 	spinlock_t link_status_lock;
658 
659 	spinlock_t reconfig_lock;
660 	u32 reconfig_posted;
661 	bool reconfig_timer_active;
662 	bool reconfig_sync_present;
663 	struct timer_list reconfig_timer;
664 	u32 reconfig_in_progress_update;
665 
666 	struct semaphore bar_lock;
667 
668 	bool rx_coalesce_adapt_on;
669 	bool tx_coalesce_adapt_on;
670 	u32 rx_coalesce_usecs;
671 	u32 rx_coalesce_max_frames;
672 	u32 tx_coalesce_usecs;
673 	u32 tx_coalesce_max_frames;
674 
675 	u8 __iomem *qcp_cfg;
676 
677 	u8 __iomem *tx_bar;
678 	u8 __iomem *rx_bar;
679 
680 #ifdef CONFIG_NFP_NET_IPSEC
681 	struct xarray xa_ipsec;
682 #endif
683 
684 	struct nfp_net_tlv_caps tlv_caps;
685 
686 	unsigned int ktls_tx_conn_cnt;
687 	unsigned int ktls_rx_conn_cnt;
688 
689 	atomic64_t ktls_conn_id_gen;
690 
691 	atomic_t ktls_no_space;
692 	atomic_t ktls_rx_resync_req;
693 	atomic_t ktls_rx_resync_ign;
694 	atomic_t ktls_rx_resync_sent;
695 
696 	struct {
697 		struct sk_buff_head queue;
698 		wait_queue_head_t wq;
699 		struct workqueue_struct *workq;
700 		struct work_struct wait_work;
701 		struct work_struct runq_work;
702 		u16 tag;
703 	} mbox_cmsg;
704 
705 	struct dentry *debugfs_dir;
706 
707 	struct list_head vnic_list;
708 
709 	struct pci_dev *pdev;
710 	struct nfp_app *app;
711 
712 	bool vnic_no_name;
713 
714 	struct nfp_port *port;
715 
716 	void *app_priv;
717 };
718 
719 /* Functions to read/write from/to a BAR
720  * Performs any endian conversion necessary.
721  */
722 static inline u16 nn_readb(struct nfp_net *nn, int off)
723 {
724 	return readb(nn->dp.ctrl_bar + off);
725 }
726 
727 static inline void nn_writeb(struct nfp_net *nn, int off, u8 val)
728 {
729 	writeb(val, nn->dp.ctrl_bar + off);
730 }
731 
732 static inline u16 nn_readw(struct nfp_net *nn, int off)
733 {
734 	return readw(nn->dp.ctrl_bar + off);
735 }
736 
737 static inline void nn_writew(struct nfp_net *nn, int off, u16 val)
738 {
739 	writew(val, nn->dp.ctrl_bar + off);
740 }
741 
742 static inline u32 nn_readl(struct nfp_net *nn, int off)
743 {
744 	return readl(nn->dp.ctrl_bar + off);
745 }
746 
747 static inline void nn_writel(struct nfp_net *nn, int off, u32 val)
748 {
749 	writel(val, nn->dp.ctrl_bar + off);
750 }
751 
752 static inline u64 nn_readq(struct nfp_net *nn, int off)
753 {
754 	return readq(nn->dp.ctrl_bar + off);
755 }
756 
757 static inline void nn_writeq(struct nfp_net *nn, int off, u64 val)
758 {
759 	writeq(val, nn->dp.ctrl_bar + off);
760 }
761 
762 /* Flush posted PCI writes by reading something without side effects */
763 static inline void nn_pci_flush(struct nfp_net *nn)
764 {
765 	nn_readl(nn, NFP_NET_CFG_VERSION);
766 }
767 
768 /* Queue Controller Peripheral access functions and definitions.
769  *
770  * Some of the BARs of the NFP are mapped to portions of the Queue
771  * Controller Peripheral (QCP) address space on the NFP.  A QCP queue
772  * has a read and a write pointer (as well as a size and flags,
773  * indicating overflow etc).  The QCP offers a number of different
774  * operation on queue pointers, but here we only offer function to
775  * either add to a pointer or to read the pointer value.
776  */
777 #define NFP_QCP_QUEUE_ADDR_SZ			0x800
778 #define NFP_QCP_QUEUE_OFF(_x)			((_x) * NFP_QCP_QUEUE_ADDR_SZ)
779 #define NFP_QCP_QUEUE_ADD_RPTR			0x0000
780 #define NFP_QCP_QUEUE_ADD_WPTR			0x0004
781 #define NFP_QCP_QUEUE_STS_LO			0x0008
782 #define NFP_QCP_QUEUE_STS_LO_READPTR_mask	0x3ffff
783 #define NFP_QCP_QUEUE_STS_HI			0x000c
784 #define NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask	0x3ffff
785 
786 /* nfp_qcp_ptr - Read or Write Pointer of a queue */
787 enum nfp_qcp_ptr {
788 	NFP_QCP_READ_PTR = 0,
789 	NFP_QCP_WRITE_PTR
790 };
791 
792 /**
793  * nfp_qcp_rd_ptr_add() - Add the value to the read 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_rd_ptr_add(u8 __iomem *q, u32 val)
799 {
800 	writel(val, q + NFP_QCP_QUEUE_ADD_RPTR);
801 }
802 
803 /**
804  * nfp_qcp_wr_ptr_add() - Add the value to the write pointer of a queue
805  *
806  * @q:   Base address for queue structure
807  * @val: Value to add to the queue pointer
808  */
809 static inline void nfp_qcp_wr_ptr_add(u8 __iomem *q, u32 val)
810 {
811 	writel(val, q + NFP_QCP_QUEUE_ADD_WPTR);
812 }
813 
814 static inline u32 _nfp_qcp_read(u8 __iomem *q, enum nfp_qcp_ptr ptr)
815 {
816 	u32 off;
817 	u32 val;
818 
819 	if (ptr == NFP_QCP_READ_PTR)
820 		off = NFP_QCP_QUEUE_STS_LO;
821 	else
822 		off = NFP_QCP_QUEUE_STS_HI;
823 
824 	val = readl(q + off);
825 
826 	if (ptr == NFP_QCP_READ_PTR)
827 		return val & NFP_QCP_QUEUE_STS_LO_READPTR_mask;
828 	else
829 		return val & NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask;
830 }
831 
832 /**
833  * nfp_qcp_rd_ptr_read() - Read the current read pointer value for a queue
834  * @q:  Base address for queue structure
835  *
836  * Return: Value read.
837  */
838 static inline u32 nfp_qcp_rd_ptr_read(u8 __iomem *q)
839 {
840 	return _nfp_qcp_read(q, NFP_QCP_READ_PTR);
841 }
842 
843 /**
844  * nfp_qcp_wr_ptr_read() - Read the current write pointer value for a queue
845  * @q:  Base address for queue structure
846  *
847  * Return: Value read.
848  */
849 static inline u32 nfp_qcp_wr_ptr_read(u8 __iomem *q)
850 {
851 	return _nfp_qcp_read(q, NFP_QCP_WRITE_PTR);
852 }
853 
854 u32 nfp_qcp_queue_offset(const struct nfp_dev_info *dev_info, u16 queue);
855 
856 static inline bool nfp_net_is_data_vnic(struct nfp_net *nn)
857 {
858 	WARN_ON_ONCE(!nn->dp.netdev && nn->port);
859 	return !!nn->dp.netdev;
860 }
861 
862 static inline bool nfp_net_running(struct nfp_net *nn)
863 {
864 	return nn->dp.ctrl & NFP_NET_CFG_CTRL_ENABLE;
865 }
866 
867 static inline const char *nfp_net_name(struct nfp_net *nn)
868 {
869 	return nn->dp.netdev ? nn->dp.netdev->name : "ctrl";
870 }
871 
872 static inline void nfp_ctrl_lock(struct nfp_net *nn)
873 	__acquires(&nn->r_vecs[0].lock)
874 {
875 	spin_lock_bh(&nn->r_vecs[0].lock);
876 }
877 
878 static inline void nfp_ctrl_unlock(struct nfp_net *nn)
879 	__releases(&nn->r_vecs[0].lock)
880 {
881 	spin_unlock_bh(&nn->r_vecs[0].lock);
882 }
883 
884 static inline void nn_ctrl_bar_lock(struct nfp_net *nn)
885 {
886 	down(&nn->bar_lock);
887 }
888 
889 static inline bool nn_ctrl_bar_trylock(struct nfp_net *nn)
890 {
891 	return !down_trylock(&nn->bar_lock);
892 }
893 
894 static inline void nn_ctrl_bar_unlock(struct nfp_net *nn)
895 {
896 	up(&nn->bar_lock);
897 }
898 
899 /* Globals */
900 extern const char nfp_driver_version[];
901 
902 extern const struct net_device_ops nfp_nfd3_netdev_ops;
903 extern const struct net_device_ops nfp_nfdk_netdev_ops;
904 
905 static inline bool nfp_netdev_is_nfp_net(struct net_device *netdev)
906 {
907 	return netdev->netdev_ops == &nfp_nfd3_netdev_ops ||
908 	       netdev->netdev_ops == &nfp_nfdk_netdev_ops;
909 }
910 
911 static inline int nfp_net_coalesce_para_check(u32 usecs, u32 pkts)
912 {
913 	if ((usecs >= ((1 << 16) - 1)) || (pkts >= ((1 << 16) - 1)))
914 		return -EINVAL;
915 
916 	return 0;
917 }
918 
919 /* Prototypes */
920 void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
921 			    void __iomem *ctrl_bar);
922 
923 struct nfp_net *
924 nfp_net_alloc(struct pci_dev *pdev, const struct nfp_dev_info *dev_info,
925 	      void __iomem *ctrl_bar, bool needs_netdev,
926 	      unsigned int max_tx_rings, unsigned int max_rx_rings);
927 void nfp_net_free(struct nfp_net *nn);
928 
929 int nfp_net_init(struct nfp_net *nn);
930 void nfp_net_clean(struct nfp_net *nn);
931 
932 int nfp_ctrl_open(struct nfp_net *nn);
933 void nfp_ctrl_close(struct nfp_net *nn);
934 
935 void nfp_net_set_ethtool_ops(struct net_device *netdev);
936 void nfp_net_info(struct nfp_net *nn);
937 int __nfp_net_reconfig(struct nfp_net *nn, u32 update);
938 int nfp_net_reconfig(struct nfp_net *nn, u32 update);
939 unsigned int nfp_net_rss_key_sz(struct nfp_net *nn);
940 void nfp_net_rss_write_itbl(struct nfp_net *nn);
941 void nfp_net_rss_write_key(struct nfp_net *nn);
942 void nfp_net_coalesce_write_cfg(struct nfp_net *nn);
943 int nfp_net_mbox_lock(struct nfp_net *nn, unsigned int data_size);
944 int nfp_net_mbox_reconfig(struct nfp_net *nn, u32 mbox_cmd);
945 int nfp_net_mbox_reconfig_and_unlock(struct nfp_net *nn, u32 mbox_cmd);
946 void nfp_net_mbox_reconfig_post(struct nfp_net *nn, u32 update);
947 int nfp_net_mbox_reconfig_wait_posted(struct nfp_net *nn);
948 
949 unsigned int
950 nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
951 		   unsigned int min_irqs, unsigned int want_irqs);
952 void nfp_net_irqs_disable(struct pci_dev *pdev);
953 void
954 nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
955 		    unsigned int n);
956 struct sk_buff *
957 nfp_net_tls_tx(struct nfp_net_dp *dp, struct nfp_net_r_vector *r_vec,
958 	       struct sk_buff *skb, u64 *tls_handle, int *nr_frags);
959 void nfp_net_tls_tx_undo(struct sk_buff *skb, u64 tls_handle);
960 
961 struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn);
962 int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *new,
963 			  struct netlink_ext_ack *extack);
964 
965 #ifdef CONFIG_NFP_DEBUG
966 void nfp_net_debugfs_create(void);
967 void nfp_net_debugfs_destroy(void);
968 struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev);
969 void nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir);
970 void nfp_net_debugfs_dir_clean(struct dentry **dir);
971 #else
972 static inline void nfp_net_debugfs_create(void)
973 {
974 }
975 
976 static inline void nfp_net_debugfs_destroy(void)
977 {
978 }
979 
980 static inline struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev)
981 {
982 	return NULL;
983 }
984 
985 static inline void
986 nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir)
987 {
988 }
989 
990 static inline void nfp_net_debugfs_dir_clean(struct dentry **dir)
991 {
992 }
993 #endif /* CONFIG_NFP_DEBUG */
994 
995 #endif /* _NFP_NET_H_ */
996