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