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