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