]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/ethernet/netronome/nfp/nfp_net.h
UBUNTU: Ubuntu-4.13.0-45.50
[mirror_ubuntu-artful-kernel.git] / drivers / net / ethernet / netronome / nfp / nfp_net.h
CommitLineData
4c352362 1/*
2633beb9 2 * Copyright (C) 2015-2017 Netronome Systems, Inc.
4c352362
JK
3 *
4 * This software is dual licensed under the GNU General License Version 2,
5 * June 1991 as shown in the file COPYING in the top-level directory of this
6 * source tree or the BSD 2-Clause License provided below. You have the
7 * option to license this software under the complete terms of either license.
8 *
9 * The BSD 2-Clause License:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * 2. Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
34/*
35 * nfp_net.h
36 * Declarations for Netronome network device driver.
37 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
38 * Jason McMullan <jason.mcmullan@netronome.com>
39 * Rolf Neugebauer <rolf.neugebauer@netronome.com>
40 */
41
42#ifndef _NFP_NET_H_
43#define _NFP_NET_H_
44
45#include <linux/interrupt.h>
63461a02 46#include <linux/list.h>
4c352362
JK
47#include <linux/netdevice.h>
48#include <linux/pci.h>
a1cbaad7 49#include <linux/io-64-nonatomic-hi-lo.h>
4c352362
JK
50
51#include "nfp_net_ctrl.h"
52
c821e617
JK
53#define nn_pr(nn, lvl, fmt, args...) \
54 ({ \
55 struct nfp_net *__nn = (nn); \
56 \
57 if (__nn->dp.netdev) \
58 netdev_printk(lvl, __nn->dp.netdev, fmt, ## args); \
59 else \
60 dev_printk(lvl, __nn->dp.dev, "ctrl: " fmt, ## args); \
61 })
62
63#define nn_err(nn, fmt, args...) nn_pr(nn, KERN_ERR, fmt, ## args)
64#define nn_warn(nn, fmt, args...) nn_pr(nn, KERN_WARNING, fmt, ## args)
65#define nn_info(nn, fmt, args...) nn_pr(nn, KERN_INFO, fmt, ## args)
66#define nn_dbg(nn, fmt, args...) nn_pr(nn, KERN_DEBUG, fmt, ## args)
67
79c12a75 68#define nn_dp_warn(dp, fmt, args...) \
c821e617
JK
69 ({ \
70 struct nfp_net_dp *__dp = (dp); \
71 \
72 if (unlikely(net_ratelimit())) { \
73 if (__dp->netdev) \
74 netdev_warn(__dp->netdev, fmt, ## args); \
75 else \
76 dev_warn(__dp->dev, fmt, ## args); \
77 } \
78 })
4c352362 79
3d780b92
JK
80/* Max time to wait for NFP to respond on updates (in seconds) */
81#define NFP_NET_POLL_TIMEOUT 5
4c352362 82
66860beb
JK
83/* Interval for reading offloaded filter stats */
84#define NFP_NET_STAT_POLL_IVL msecs_to_jiffies(100)
85
4c352362 86/* Bar allocation */
796312cd 87#define NFP_NET_CTRL_BAR 0
4c352362
JK
88#define NFP_NET_Q0_BAR 2
89#define NFP_NET_Q1_BAR 4 /* OBSOLETE */
90
91/* Max bits in DMA address */
92#define NFP_NET_MAX_DMA_BITS 40
93
94/* Default size for MTU and freelist buffer sizes */
95#define NFP_NET_DEFAULT_MTU 1500
4c352362
JK
96
97/* Maximum number of bytes prepended to a packet */
98#define NFP_NET_MAX_PREPEND 64
99
100/* Interrupt definitions */
101#define NFP_NET_NON_Q_VECTORS 2
102#define NFP_NET_IRQ_LSC_IDX 0
103#define NFP_NET_IRQ_EXN_IDX 1
d4e7f092 104#define NFP_NET_MIN_VNIC_IRQS (NFP_NET_NON_Q_VECTORS + 1)
4c352362
JK
105
106/* Queue/Ring definitions */
107#define NFP_NET_MAX_TX_RINGS 64 /* Max. # of Tx rings per device */
108#define NFP_NET_MAX_RX_RINGS 64 /* Max. # of Rx rings per device */
23a4e389
JK
109#define NFP_NET_MAX_R_VECS (NFP_NET_MAX_TX_RINGS > NFP_NET_MAX_RX_RINGS ? \
110 NFP_NET_MAX_TX_RINGS : NFP_NET_MAX_RX_RINGS)
111#define NFP_NET_MAX_IRQS (NFP_NET_NON_Q_VECTORS + NFP_NET_MAX_R_VECS)
4c352362
JK
112
113#define NFP_NET_MIN_TX_DESCS 256 /* Min. # of Tx descs per ring */
114#define NFP_NET_MIN_RX_DESCS 256 /* Min. # of Rx descs per ring */
115#define NFP_NET_MAX_TX_DESCS (256 * 1024) /* Max. # of Tx descs per ring */
116#define NFP_NET_MAX_RX_DESCS (256 * 1024) /* Max. # of Rx descs per ring */
117
118#define NFP_NET_TX_DESCS_DEFAULT 4096 /* Default # of Tx descs per ring */
119#define NFP_NET_RX_DESCS_DEFAULT 4096 /* Default # of Rx descs per ring */
120
121#define NFP_NET_FL_BATCH 16 /* Add freelist in this Batch size */
abeeec4a 122#define NFP_NET_XDP_MAX_COMPLETE 2048 /* XDP bufs to reclaim in NAPI poll */
4c352362
JK
123
124/* Offload definitions */
125#define NFP_NET_N_VXLAN_PORTS (NFP_NET_CFG_VXLAN_SZ / sizeof(__be16))
126
c0f031bc
JK
127#define NFP_NET_RX_BUF_HEADROOM (NET_SKB_PAD + NET_IP_ALIGN)
128#define NFP_NET_RX_BUF_NON_DATA (NFP_NET_RX_BUF_HEADROOM + \
129 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
130
4c352362 131/* Forward declarations */
bd5ca062 132struct nfp_cpp;
47465aed 133struct nfp_eth_table_port;
4c352362
JK
134struct nfp_net;
135struct nfp_net_r_vector;
eb488c26 136struct nfp_port;
4c352362 137
4aa3b766
JK
138/* Convenience macro for wrapping descriptor index on ring size */
139#define D_IDX(ring, idx) ((idx) & ((ring)->cnt - 1))
140
4c352362
JK
141/* Convenience macro for writing dma address into RX/TX descriptors */
142#define nfp_desc_set_dma_addr(desc, dma_addr) \
143 do { \
144 __typeof(desc) __d = (desc); \
145 dma_addr_t __addr = (dma_addr); \
146 \
147 __d->dma_addr_lo = cpu_to_le32(lower_32_bits(__addr)); \
148 __d->dma_addr_hi = upper_32_bits(__addr) & 0xff; \
149 } while (0)
150
151/* TX descriptor format */
152
153#define PCIE_DESC_TX_EOP BIT(7)
154#define PCIE_DESC_TX_OFFSET_MASK GENMASK(6, 0)
155#define PCIE_DESC_TX_MSS_MASK GENMASK(13, 0)
156
157/* Flags in the host TX descriptor */
158#define PCIE_DESC_TX_CSUM BIT(7)
159#define PCIE_DESC_TX_IP4_CSUM BIT(6)
160#define PCIE_DESC_TX_TCP_CSUM BIT(5)
161#define PCIE_DESC_TX_UDP_CSUM BIT(4)
162#define PCIE_DESC_TX_VLAN BIT(3)
163#define PCIE_DESC_TX_LSO BIT(2)
164#define PCIE_DESC_TX_ENCAP BIT(1)
165#define PCIE_DESC_TX_O_IP4_CSUM BIT(0)
166
167struct nfp_net_tx_desc {
168 union {
169 struct {
170 u8 dma_addr_hi; /* High bits of host buf address */
171 __le16 dma_len; /* Length to DMA for this desc */
172 u8 offset_eop; /* Offset in buf where pkt starts +
173 * highest bit is eop flag.
174 */
175 __le32 dma_addr_lo; /* Low 32bit of host buf addr */
176
177 __le16 mss; /* MSS to be used for LSO */
e53fc9fa 178 u8 lso_hdrlen; /* LSO, TCP payload offset */
4c352362 179 u8 flags; /* TX Flags, see @PCIE_DESC_TX_* */
28063be6
EP
180 union {
181 struct {
182 u8 l3_offset; /* L3 header offset */
183 u8 l4_offset; /* L4 header offset */
184 };
185 __le16 vlan; /* VLAN tag to add if indicated */
186 };
4c352362
JK
187 __le16 data_len; /* Length of frame + meta data */
188 } __packed;
189 __le32 vals[4];
190 };
191};
192
193/**
194 * struct nfp_net_tx_buf - software TX buffer descriptor
195 * @skb: sk_buff associated with this buffer
196 * @dma_addr: DMA mapping address of the buffer
197 * @fidx: Fragment index (-1 for the head and [0..nr_frags-1] for frags)
198 * @pkt_cnt: Number of packets to be produced out of the skb associated
199 * with this buffer (valid only on the head's buffer).
200 * Will be 1 for all non-TSO packets.
201 * @real_len: Number of bytes which to be produced out of the skb (valid only
202 * on the head's buffer). Equal to skb->len for non-TSO packets.
203 */
204struct nfp_net_tx_buf {
ecd63a02
JK
205 union {
206 struct sk_buff *skb;
207 void *frag;
208 };
4c352362
JK
209 dma_addr_t dma_addr;
210 short int fidx;
211 u16 pkt_cnt;
212 u32 real_len;
213};
214
215/**
216 * struct nfp_net_tx_ring - TX ring structure
217 * @r_vec: Back pointer to ring vector structure
218 * @idx: Ring index from Linux's perspective
219 * @qcidx: Queue Controller Peripheral (QCP) queue index for the TX queue
220 * @qcp_q: Pointer to base of the QCP TX queue
221 * @cnt: Size of the queue in number of descriptors
222 * @wr_p: TX ring write pointer (free running)
223 * @rd_p: TX ring read pointer (free running)
224 * @qcp_rd_p: Local copy of QCP TX queue read pointer
225 * @wr_ptr_add: Accumulated number of buffers to add to QCP write pointer
226 * (used for .xmit_more delayed kick)
227 * @txbufs: Array of transmitted TX buffers, to free on transmit
228 * @txds: Virtual address of TX ring in host memory
229 * @dma: DMA address of the TX ring
230 * @size: Size, in bytes, of the TX ring (needed to free)
92e68195 231 * @is_xdp: Is this a XDP TX ring?
4c352362
JK
232 */
233struct nfp_net_tx_ring {
234 struct nfp_net_r_vector *r_vec;
235
236 u32 idx;
237 int qcidx;
238 u8 __iomem *qcp_q;
239
240 u32 cnt;
241 u32 wr_p;
242 u32 rd_p;
243 u32 qcp_rd_p;
244
245 u32 wr_ptr_add;
246
247 struct nfp_net_tx_buf *txbufs;
248 struct nfp_net_tx_desc *txds;
249
250 dma_addr_t dma;
251 unsigned int size;
92e68195 252 bool is_xdp;
4c352362
JK
253} ____cacheline_aligned;
254
255/* RX and freelist descriptor format */
256
257#define PCIE_DESC_RX_DD BIT(7)
258#define PCIE_DESC_RX_META_LEN_MASK GENMASK(6, 0)
259
260/* Flags in the RX descriptor */
261#define PCIE_DESC_RX_RSS cpu_to_le16(BIT(15))
262#define PCIE_DESC_RX_I_IP4_CSUM cpu_to_le16(BIT(14))
263#define PCIE_DESC_RX_I_IP4_CSUM_OK cpu_to_le16(BIT(13))
264#define PCIE_DESC_RX_I_TCP_CSUM cpu_to_le16(BIT(12))
265#define PCIE_DESC_RX_I_TCP_CSUM_OK cpu_to_le16(BIT(11))
266#define PCIE_DESC_RX_I_UDP_CSUM cpu_to_le16(BIT(10))
267#define PCIE_DESC_RX_I_UDP_CSUM_OK cpu_to_le16(BIT(9))
7533fdc0 268#define PCIE_DESC_RX_BPF cpu_to_le16(BIT(8))
4c352362
JK
269#define PCIE_DESC_RX_EOP cpu_to_le16(BIT(7))
270#define PCIE_DESC_RX_IP4_CSUM cpu_to_le16(BIT(6))
271#define PCIE_DESC_RX_IP4_CSUM_OK cpu_to_le16(BIT(5))
272#define PCIE_DESC_RX_TCP_CSUM cpu_to_le16(BIT(4))
273#define PCIE_DESC_RX_TCP_CSUM_OK cpu_to_le16(BIT(3))
274#define PCIE_DESC_RX_UDP_CSUM cpu_to_le16(BIT(2))
275#define PCIE_DESC_RX_UDP_CSUM_OK cpu_to_le16(BIT(1))
276#define PCIE_DESC_RX_VLAN cpu_to_le16(BIT(0))
277
278#define PCIE_DESC_RX_CSUM_ALL (PCIE_DESC_RX_IP4_CSUM | \
279 PCIE_DESC_RX_TCP_CSUM | \
280 PCIE_DESC_RX_UDP_CSUM | \
281 PCIE_DESC_RX_I_IP4_CSUM | \
282 PCIE_DESC_RX_I_TCP_CSUM | \
283 PCIE_DESC_RX_I_UDP_CSUM)
284#define PCIE_DESC_RX_CSUM_OK_SHIFT 1
285#define __PCIE_DESC_RX_CSUM_ALL le16_to_cpu(PCIE_DESC_RX_CSUM_ALL)
286#define __PCIE_DESC_RX_CSUM_ALL_OK (__PCIE_DESC_RX_CSUM_ALL >> \
287 PCIE_DESC_RX_CSUM_OK_SHIFT)
288
289struct nfp_net_rx_desc {
290 union {
291 struct {
292 u8 dma_addr_hi; /* High bits of the buf address */
293 __le16 reserved; /* Must be zero */
294 u8 meta_len_dd; /* Must be zero */
295
296 __le32 dma_addr_lo; /* Low bits of the buffer address */
297 } __packed fld;
298
299 struct {
300 __le16 data_len; /* Length of the frame + meta data */
301 u8 reserved;
302 u8 meta_len_dd; /* Length of meta data prepended +
303 * descriptor done flag.
304 */
305
306 __le16 flags; /* RX flags. See @PCIE_DESC_RX_* */
307 __le16 vlan; /* VLAN if stripped */
308 } __packed rxd;
309
310 __le32 vals[2];
311 };
312};
313
19d0f54e
JK
314#define NFP_NET_META_FIELD_MASK GENMASK(NFP_NET_META_FIELD_SIZE - 1, 0)
315
e524a6a9 316struct nfp_meta_parsed {
ddb98d94
JK
317 u8 hash_type;
318 u8 csum_type;
e524a6a9
JK
319 u32 hash;
320 u32 mark;
91bf82ca 321 u32 portid;
ddb98d94 322 __wsum csum;
e524a6a9
JK
323};
324
4c352362
JK
325struct nfp_net_rx_hash {
326 __be32 hash_type;
327 __be32 hash;
328};
329
330/**
331 * struct nfp_net_rx_buf - software RX buffer descriptor
c0f031bc 332 * @frag: page fragment buffer
4c352362
JK
333 * @dma_addr: DMA mapping address of the buffer
334 */
335struct nfp_net_rx_buf {
c0f031bc 336 void *frag;
4c352362
JK
337 dma_addr_t dma_addr;
338};
339
340/**
341 * struct nfp_net_rx_ring - RX ring structure
342 * @r_vec: Back pointer to ring vector structure
343 * @cnt: Size of the queue in number of descriptors
344 * @wr_p: FL/RX ring write pointer (free running)
345 * @rd_p: FL/RX ring read pointer (free running)
346 * @idx: Ring index from Linux's perspective
347 * @fl_qcidx: Queue Controller Peripheral (QCP) queue index for the freelist
4c352362 348 * @qcp_fl: Pointer to base of the QCP freelist queue
4c352362
JK
349 * @rxbufs: Array of transmitted FL/RX buffers
350 * @rxds: Virtual address of FL/RX ring in host memory
351 * @dma: DMA address of the FL/RX ring
352 * @size: Size, in bytes, of the FL/RX ring (needed to free)
353 */
354struct nfp_net_rx_ring {
355 struct nfp_net_r_vector *r_vec;
356
357 u32 cnt;
358 u32 wr_p;
359 u32 rd_p;
360
83d08a1d 361 u32 idx;
4c352362
JK
362
363 int fl_qcidx;
4c352362 364 u8 __iomem *qcp_fl;
4c352362
JK
365
366 struct nfp_net_rx_buf *rxbufs;
367 struct nfp_net_rx_desc *rxds;
368
369 dma_addr_t dma;
370 unsigned int size;
371} ____cacheline_aligned;
372
373/**
374 * struct nfp_net_r_vector - Per ring interrupt vector configuration
375 * @nfp_net: Backpointer to nfp_net structure
376 * @napi: NAPI structure for this ring vec
377 * @tx_ring: Pointer to TX ring
378 * @rx_ring: Pointer to RX ring
ecd63a02 379 * @xdp_ring: Pointer to an extra TX ring for XDP
fdace6c2 380 * @irq_entry: MSI-X table entry (use for talking to the device)
4c352362
JK
381 * @rx_sync: Seqlock for atomic updates of RX stats
382 * @rx_pkts: Number of received packets
383 * @rx_bytes: Number of received bytes
384 * @rx_drops: Number of packets dropped on RX due to lack of resources
385 * @hw_csum_rx_ok: Counter of packets where the HW checksum was OK
386 * @hw_csum_rx_inner_ok: Counter of packets where the inner HW checksum was OK
387 * @hw_csum_rx_error: Counter of packets with bad checksums
388 * @tx_sync: Seqlock for atomic updates of TX stats
389 * @tx_pkts: Number of Transmitted packets
390 * @tx_bytes: Number of Transmitted bytes
391 * @hw_csum_tx: Counter of packets with TX checksum offload requested
392 * @hw_csum_tx_inner: Counter of inner TX checksum offload requests
393 * @tx_gather: Counter of packets with Gather DMA
394 * @tx_lso: Counter of LSO packets sent
395 * @tx_errors: How many TX errors were encountered
396 * @tx_busy: How often was TX busy (no space)?
fdace6c2 397 * @irq_vector: Interrupt vector number (use for talking to the OS)
4c352362
JK
398 * @handler: Interrupt handler for this ring vector
399 * @name: Name of the interrupt vector
400 * @affinity_mask: SMP affinity mask for this vector
401 *
402 * This structure ties RX and TX rings to interrupt vectors and a NAPI
403 * context. This currently only supports one RX and TX ring per
404 * interrupt vector but might be extended in the future to allow
405 * association of multiple rings per vector.
406 */
407struct nfp_net_r_vector {
408 struct nfp_net *nfp_net;
77ece8d5
JK
409 union {
410 struct napi_struct napi;
411 struct {
412 struct tasklet_struct tasklet;
413 struct sk_buff_head queue;
414 struct spinlock lock;
415 };
416 };
4c352362
JK
417
418 struct nfp_net_tx_ring *tx_ring;
419 struct nfp_net_rx_ring *rx_ring;
420
fdace6c2 421 u16 irq_entry;
4c352362
JK
422
423 struct u64_stats_sync rx_sync;
424 u64 rx_pkts;
425 u64 rx_bytes;
426 u64 rx_drops;
427 u64 hw_csum_rx_ok;
428 u64 hw_csum_rx_inner_ok;
429 u64 hw_csum_rx_error;
430
ecd63a02
JK
431 struct nfp_net_tx_ring *xdp_ring;
432
4c352362
JK
433 struct u64_stats_sync tx_sync;
434 u64 tx_pkts;
435 u64 tx_bytes;
436 u64 hw_csum_tx;
437 u64 hw_csum_tx_inner;
438 u64 tx_gather;
439 u64 tx_lso;
440 u64 tx_errors;
441 u64 tx_busy;
442
fdace6c2 443 u32 irq_vector;
4c352362
JK
444 irq_handler_t handler;
445 char name[IFNAMSIZ + 8];
446 cpumask_t affinity_mask;
447} ____cacheline_aligned;
448
449/* Firmware version as it is written in the 32bit value in the BAR */
450struct nfp_net_fw_version {
451 u8 minor;
452 u8 major;
453 u8 class;
454 u8 resv;
455} __packed;
456
457static inline bool nfp_net_fw_ver_eq(struct nfp_net_fw_version *fw_ver,
458 u8 resv, u8 class, u8 major, u8 minor)
459{
460 return fw_ver->resv == resv &&
461 fw_ver->class == class &&
462 fw_ver->major == major &&
463 fw_ver->minor == minor;
464}
465
66860beb
JK
466struct nfp_stat_pair {
467 u64 pkts;
468 u64 bytes;
469};
470
4c352362 471/**
79c12a75 472 * struct nfp_net_dp - NFP network device datapath data structure
fa43d2a8 473 * @dev: Backpointer to struct device
79c12a75
JK
474 * @netdev: Backpointer to net_device structure
475 * @is_vf: Is the driver attached to a VF?
7533fdc0 476 * @bpf_offload_skip_sw: Offloaded BPF program will not be rerun by cls_bpf
6d677075 477 * @bpf_offload_xdp: Offloaded BPF program is XDP
b9dcf88a 478 * @chained_metadata_format: Firemware will use new metadata format
c487e6b1 479 * @rx_dma_dir: Mapping direction for RX buffers
6fe0c3b4 480 * @rx_dma_off: Offset at which DMA packets (for XDP headroom)
97717aca 481 * @rx_offset: Offset in the RX buffers where packet data starts
79c12a75
JK
482 * @ctrl: Local copy of the control register/word.
483 * @fl_bufsz: Currently configured size of the freelist buffers
ecd63a02 484 * @xdp_prog: Installed XDP program
79c12a75
JK
485 * @tx_rings: Array of pre-allocated TX ring structures
486 * @rx_rings: Array of pre-allocated RX ring structures
d2b84397 487 * @ctrl_bar: Pointer to mapped control BAR
79c12a75
JK
488 *
489 * @txd_cnt: Size of the TX ring in number of descriptors
490 * @rxd_cnt: Size of the RX ring in number of descriptors
491 * @num_r_vecs: Number of used ring vectors
492 * @num_tx_rings: Currently configured number of TX rings
493 * @num_stack_tx_rings: Number of TX rings used by the stack (not XDP)
494 * @num_rx_rings: Currently configured number of RX rings
76e1e1a8 495 * @mtu: Device MTU
79c12a75
JK
496 */
497struct nfp_net_dp {
498 struct device *dev;
499 struct net_device *netdev;
500
c487e6b1
JK
501 u8 is_vf:1;
502 u8 bpf_offload_skip_sw:1;
503 u8 bpf_offload_xdp:1;
504 u8 chained_metadata_format:1;
505
506 u8 rx_dma_dir;
97717aca
JK
507 u8 rx_offset;
508
dbf637ff
JK
509 u32 rx_dma_off;
510
79c12a75
JK
511 u32 ctrl;
512 u32 fl_bufsz;
513
79c12a75
JK
514 struct bpf_prog *xdp_prog;
515
516 struct nfp_net_tx_ring *tx_rings;
517 struct nfp_net_rx_ring *rx_rings;
518
d2b84397
JK
519 u8 __iomem *ctrl_bar;
520
79c12a75
JK
521 /* Cold data follows */
522
523 unsigned int txd_cnt;
524 unsigned int rxd_cnt;
525
526 unsigned int num_r_vecs;
527
528 unsigned int num_tx_rings;
529 unsigned int num_stack_tx_rings;
530 unsigned int num_rx_rings;
76e1e1a8
JK
531
532 unsigned int mtu;
79c12a75
JK
533};
534
535/**
536 * struct nfp_net - NFP network device structure
537 * @dp: Datapath structure
538 * @fw_ver: Firmware version
4c352362
JK
539 * @cap: Capabilities advertised by the Firmware
540 * @max_mtu: Maximum support MTU advertised by the Firmware
9ff304bf 541 * @rss_hfunc: RSS selected hash function
4c352362
JK
542 * @rss_cfg: RSS configuration
543 * @rss_key: RSS secret key
544 * @rss_itbl: RSS indirection table
9f82fca9 545 * @xdp_flags: Flags with which XDP prog was loaded
6a8ef542 546 * @xdp_prog: XDP prog (for ctrl path, both DRV and HW modes)
79c12a75 547 * @max_r_vecs: Number of allocated interrupt vectors for RX/TX
4c352362
JK
548 * @max_tx_rings: Maximum number of TX rings supported by the Firmware
549 * @max_rx_rings: Maximum number of RX rings supported by the Firmware
4c352362
JK
550 * @r_vecs: Pre-allocated array of ring vectors
551 * @irq_entries: Pre-allocated array of MSI-X entries
552 * @lsc_handler: Handler for Link State Change interrupt
553 * @lsc_name: Name for Link State Change interrupt
554 * @exn_handler: Handler for Exception interrupt
555 * @exn_name: Name for Exception interrupt
556 * @shared_handler: Handler for shared interrupts
557 * @shared_name: Name for shared interrupt
558 * @me_freq_mhz: ME clock_freq (MHz)
559 * @reconfig_lock: Protects HW reconfiguration request regs/machinery
3d780b92
JK
560 * @reconfig_posted: Pending reconfig bits coming from async sources
561 * @reconfig_timer_active: Timer for reading reconfiguration results is pending
562 * @reconfig_sync_present: Some thread is performing synchronous reconfig
563 * @reconfig_timer: Timer for async reading of reconfig results
4c352362 564 * @link_up: Is the link up?
cee42951 565 * @link_status_lock: Protects @link_* and ensures atomicity with BAR reading
4c352362
JK
566 * @rx_coalesce_usecs: RX interrupt moderation usecs delay parameter
567 * @rx_coalesce_max_frames: RX interrupt moderation frame count parameter
568 * @tx_coalesce_usecs: TX interrupt moderation usecs delay parameter
569 * @tx_coalesce_max_frames: TX interrupt moderation frame count parameter
570 * @vxlan_ports: VXLAN ports for RX inner csum offload communicated to HW
571 * @vxlan_usecnt: IPv4/IPv6 VXLAN port use counts
572 * @qcp_cfg: Pointer to QCP queue used for configuration notification
4c352362
JK
573 * @tx_bar: Pointer to mapped TX queues
574 * @rx_bar: Pointer to mapped FL/RX queues
575 * @debugfs_dir: Device directory in debugfs
af623682 576 * @ethtool_dump_flag: Ethtool dump flag
d4e7f092 577 * @vnic_list: Entry on device vNIC list
fa43d2a8 578 * @pdev: Backpointer to PCI device
7ac9ebd5 579 * @app: APP handle if available
eb488c26 580 * @port: Pointer to nfp_port structure if vNIC is a port
c66a9cf4 581 * @app_priv: APP private data for this vNIC
4c352362
JK
582 */
583struct nfp_net {
79c12a75 584 struct nfp_net_dp dp;
73725d9d 585
4c352362 586 struct nfp_net_fw_version fw_ver;
79c12a75 587
4c352362
JK
588 u32 cap;
589 u32 max_mtu;
590
9ff304bf 591 u8 rss_hfunc;
4c352362
JK
592 u32 rss_cfg;
593 u8 rss_key[NFP_NET_CFG_RSS_KEY_SZ];
594 u8 rss_itbl[NFP_NET_CFG_RSS_ITBL_SZ];
595
9f82fca9 596 u32 xdp_flags;
6a8ef542 597 struct bpf_prog *xdp_prog;
9f82fca9 598
a4b562bb
JK
599 unsigned int max_tx_rings;
600 unsigned int max_rx_rings;
4c352362 601
4c352362
JK
602 int stride_tx;
603 int stride_rx;
604
b33ae997 605 unsigned int max_r_vecs;
23a4e389
JK
606 struct nfp_net_r_vector r_vecs[NFP_NET_MAX_R_VECS];
607 struct msix_entry irq_entries[NFP_NET_MAX_IRQS];
4c352362
JK
608
609 irq_handler_t lsc_handler;
610 char lsc_name[IFNAMSIZ + 8];
611
612 irq_handler_t exn_handler;
613 char exn_name[IFNAMSIZ + 8];
614
615 irq_handler_t shared_handler;
616 char shared_name[IFNAMSIZ + 8];
617
618 u32 me_freq_mhz;
619
620 bool link_up;
621 spinlock_t link_status_lock;
622
623 spinlock_t reconfig_lock;
3d780b92
JK
624 u32 reconfig_posted;
625 bool reconfig_timer_active;
626 bool reconfig_sync_present;
627 struct timer_list reconfig_timer;
4c352362
JK
628
629 u32 rx_coalesce_usecs;
630 u32 rx_coalesce_max_frames;
631 u32 tx_coalesce_usecs;
632 u32 tx_coalesce_max_frames;
633
634 __be16 vxlan_ports[NFP_NET_N_VXLAN_PORTS];
635 u8 vxlan_usecnt[NFP_NET_N_VXLAN_PORTS];
636
637 u8 __iomem *qcp_cfg;
638
4c352362
JK
639 u8 __iomem *tx_bar;
640 u8 __iomem *rx_bar;
641
642 struct dentry *debugfs_dir;
af623682 643 u32 ethtool_dump_flag;
63461a02 644
d4e7f092 645 struct list_head vnic_list;
bd5ca062 646
fa43d2a8 647 struct pci_dev *pdev;
7ac9ebd5 648 struct nfp_app *app;
47465aed 649
eb488c26 650 struct nfp_port *port;
c66a9cf4
JK
651
652 void *app_priv;
4c352362
JK
653};
654
655/* Functions to read/write from/to a BAR
656 * Performs any endian conversion necessary.
657 */
7533fdc0
JK
658static inline u16 nn_readb(struct nfp_net *nn, int off)
659{
d2b84397 660 return readb(nn->dp.ctrl_bar + off);
7533fdc0
JK
661}
662
4c352362
JK
663static inline void nn_writeb(struct nfp_net *nn, int off, u8 val)
664{
d2b84397 665 writeb(val, nn->dp.ctrl_bar + off);
4c352362
JK
666}
667
7533fdc0
JK
668static inline u16 nn_readw(struct nfp_net *nn, int off)
669{
d2b84397 670 return readw(nn->dp.ctrl_bar + off);
7533fdc0
JK
671}
672
673static inline void nn_writew(struct nfp_net *nn, int off, u16 val)
674{
d2b84397 675 writew(val, nn->dp.ctrl_bar + off);
7533fdc0 676}
4c352362
JK
677
678static inline u32 nn_readl(struct nfp_net *nn, int off)
679{
d2b84397 680 return readl(nn->dp.ctrl_bar + off);
4c352362
JK
681}
682
683static inline void nn_writel(struct nfp_net *nn, int off, u32 val)
684{
d2b84397 685 writel(val, nn->dp.ctrl_bar + off);
4c352362
JK
686}
687
688static inline u64 nn_readq(struct nfp_net *nn, int off)
689{
d2b84397 690 return readq(nn->dp.ctrl_bar + off);
4c352362
JK
691}
692
693static inline void nn_writeq(struct nfp_net *nn, int off, u64 val)
694{
d2b84397 695 writeq(val, nn->dp.ctrl_bar + off);
4c352362
JK
696}
697
698/* Flush posted PCI writes by reading something without side effects */
699static inline void nn_pci_flush(struct nfp_net *nn)
700{
701 nn_readl(nn, NFP_NET_CFG_VERSION);
702}
703
704/* Queue Controller Peripheral access functions and definitions.
705 *
706 * Some of the BARs of the NFP are mapped to portions of the Queue
707 * Controller Peripheral (QCP) address space on the NFP. A QCP queue
708 * has a read and a write pointer (as well as a size and flags,
709 * indicating overflow etc). The QCP offers a number of different
710 * operation on queue pointers, but here we only offer function to
711 * either add to a pointer or to read the pointer value.
712 */
713#define NFP_QCP_QUEUE_ADDR_SZ 0x800
73e253f0 714#define NFP_QCP_QUEUE_AREA_SZ 0x80000
4c352362
JK
715#define NFP_QCP_QUEUE_OFF(_x) ((_x) * NFP_QCP_QUEUE_ADDR_SZ)
716#define NFP_QCP_QUEUE_ADD_RPTR 0x0000
717#define NFP_QCP_QUEUE_ADD_WPTR 0x0004
718#define NFP_QCP_QUEUE_STS_LO 0x0008
719#define NFP_QCP_QUEUE_STS_LO_READPTR_mask 0x3ffff
720#define NFP_QCP_QUEUE_STS_HI 0x000c
721#define NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask 0x3ffff
722
416db5c1 723/* The offset of a QCP queues in the PCIe Target */
4c352362
JK
724#define NFP_PCIE_QUEUE(_q) (0x80000 + (NFP_QCP_QUEUE_ADDR_SZ * ((_q) & 0xff)))
725
726/* nfp_qcp_ptr - Read or Write Pointer of a queue */
727enum nfp_qcp_ptr {
728 NFP_QCP_READ_PTR = 0,
729 NFP_QCP_WRITE_PTR
730};
731
732/* There appear to be an *undocumented* upper limit on the value which
733 * one can add to a queue and that value is either 0x3f or 0x7f. We
734 * go with 0x3f as a conservative measure.
735 */
736#define NFP_QCP_MAX_ADD 0x3f
737
738static inline void _nfp_qcp_ptr_add(u8 __iomem *q,
739 enum nfp_qcp_ptr ptr, u32 val)
740{
741 u32 off;
742
743 if (ptr == NFP_QCP_READ_PTR)
744 off = NFP_QCP_QUEUE_ADD_RPTR;
745 else
746 off = NFP_QCP_QUEUE_ADD_WPTR;
747
748 while (val > NFP_QCP_MAX_ADD) {
749 writel(NFP_QCP_MAX_ADD, q + off);
750 val -= NFP_QCP_MAX_ADD;
751 }
752
753 writel(val, q + off);
754}
755
756/**
757 * nfp_qcp_rd_ptr_add() - Add the value to the read pointer of a queue
758 *
759 * @q: Base address for queue structure
760 * @val: Value to add to the queue pointer
761 *
762 * If @val is greater than @NFP_QCP_MAX_ADD multiple writes are performed.
763 */
764static inline void nfp_qcp_rd_ptr_add(u8 __iomem *q, u32 val)
765{
766 _nfp_qcp_ptr_add(q, NFP_QCP_READ_PTR, val);
767}
768
769/**
770 * nfp_qcp_wr_ptr_add() - Add the value to the write pointer of a queue
771 *
772 * @q: Base address for queue structure
773 * @val: Value to add to the queue pointer
774 *
775 * If @val is greater than @NFP_QCP_MAX_ADD multiple writes are performed.
776 */
777static inline void nfp_qcp_wr_ptr_add(u8 __iomem *q, u32 val)
778{
779 _nfp_qcp_ptr_add(q, NFP_QCP_WRITE_PTR, val);
780}
781
782static inline u32 _nfp_qcp_read(u8 __iomem *q, enum nfp_qcp_ptr ptr)
783{
784 u32 off;
785 u32 val;
786
787 if (ptr == NFP_QCP_READ_PTR)
788 off = NFP_QCP_QUEUE_STS_LO;
789 else
790 off = NFP_QCP_QUEUE_STS_HI;
791
792 val = readl(q + off);
793
794 if (ptr == NFP_QCP_READ_PTR)
795 return val & NFP_QCP_QUEUE_STS_LO_READPTR_mask;
796 else
797 return val & NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask;
798}
799
800/**
801 * nfp_qcp_rd_ptr_read() - Read the current read pointer value for a queue
802 * @q: Base address for queue structure
803 *
804 * Return: Value read.
805 */
806static inline u32 nfp_qcp_rd_ptr_read(u8 __iomem *q)
807{
808 return _nfp_qcp_read(q, NFP_QCP_READ_PTR);
809}
810
811/**
812 * nfp_qcp_wr_ptr_read() - Read the current write pointer value for a queue
813 * @q: Base address for queue structure
814 *
815 * Return: Value read.
816 */
817static inline u32 nfp_qcp_wr_ptr_read(u8 __iomem *q)
818{
819 return _nfp_qcp_read(q, NFP_QCP_WRITE_PTR);
820}
821
042f4ba6
JK
822static inline bool nfp_net_is_data_vnic(struct nfp_net *nn)
823{
824 WARN_ON_ONCE(!nn->dp.netdev && nn->port);
825 return !!nn->dp.netdev;
826}
827
828static inline bool nfp_net_running(struct nfp_net *nn)
829{
830 return nn->dp.ctrl & NFP_NET_CFG_CTRL_ENABLE;
831}
832
77ece8d5
JK
833static inline const char *nfp_net_name(struct nfp_net *nn)
834{
835 return nn->dp.netdev ? nn->dp.netdev->name : "ctrl";
836}
837
4c352362 838/* Globals */
2633beb9 839extern const char nfp_driver_version[];
4c352362 840
eb488c26
JK
841extern const struct net_device_ops nfp_net_netdev_ops;
842
843static inline bool nfp_netdev_is_nfp_net(struct net_device *netdev)
844{
845 return netdev->netdev_ops == &nfp_net_netdev_ops;
846}
847
4c352362
JK
848/* Prototypes */
849void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
850 void __iomem *ctrl_bar);
851
a4b562bb 852struct nfp_net *
a7b1ad08 853nfp_net_alloc(struct pci_dev *pdev, bool needs_netdev,
beba69ca
JK
854 unsigned int max_tx_rings, unsigned int max_rx_rings);
855void nfp_net_free(struct nfp_net *nn);
856
857int nfp_net_init(struct nfp_net *nn);
858void nfp_net_clean(struct nfp_net *nn);
859
77ece8d5
JK
860int nfp_ctrl_open(struct nfp_net *nn);
861void nfp_ctrl_close(struct nfp_net *nn);
862
4c352362
JK
863void nfp_net_set_ethtool_ops(struct net_device *netdev);
864void nfp_net_info(struct nfp_net *nn);
865int nfp_net_reconfig(struct nfp_net *nn, u32 update);
9ff304bf 866unsigned int nfp_net_rss_key_sz(struct nfp_net *nn);
4c352362
JK
867void nfp_net_rss_write_itbl(struct nfp_net *nn);
868void nfp_net_rss_write_key(struct nfp_net *nn);
869void nfp_net_coalesce_write_cfg(struct nfp_net *nn);
fdace6c2
JK
870
871unsigned int
872nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
873 unsigned int min_irqs, unsigned int want_irqs);
874void nfp_net_irqs_disable(struct pci_dev *pdev);
875void
876nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
877 unsigned int n);
783496b0
JK
878
879struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn);
d957c0f7
JK
880int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *new,
881 struct netlink_ext_ack *extack);
4c352362 882
2633beb9 883#ifdef CONFIG_NFP_DEBUG
4c352362
JK
884void nfp_net_debugfs_create(void);
885void nfp_net_debugfs_destroy(void);
6f1cd5ca 886struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev);
d4e7f092 887void nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir, int id);
6f1cd5ca 888void nfp_net_debugfs_dir_clean(struct dentry **dir);
4c352362
JK
889#else
890static inline void nfp_net_debugfs_create(void)
891{
892}
893
894static inline void nfp_net_debugfs_destroy(void)
895{
896}
897
6f1cd5ca 898static inline struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev)
4c352362 899{
6f1cd5ca 900 return NULL;
4c352362
JK
901}
902
6f1cd5ca 903static inline void
d4e7f092 904nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir, int id)
6f1cd5ca
JK
905{
906}
907
908static inline void nfp_net_debugfs_dir_clean(struct dentry **dir)
4c352362
JK
909{
910}
2633beb9 911#endif /* CONFIG_NFP_DEBUG */
4c352362
JK
912
913#endif /* _NFP_NET_H_ */