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