14c352362SJakub Kicinski /*
22633beb9SJakub Kicinski  * Copyright (C) 2015-2017 Netronome Systems, Inc.
34c352362SJakub Kicinski  *
44c352362SJakub Kicinski  * This software is dual licensed under the GNU General License Version 2,
54c352362SJakub Kicinski  * June 1991 as shown in the file COPYING in the top-level directory of this
64c352362SJakub Kicinski  * source tree or the BSD 2-Clause License provided below.  You have the
74c352362SJakub Kicinski  * option to license this software under the complete terms of either license.
84c352362SJakub Kicinski  *
94c352362SJakub Kicinski  * The BSD 2-Clause License:
104c352362SJakub Kicinski  *
114c352362SJakub Kicinski  *     Redistribution and use in source and binary forms, with or
124c352362SJakub Kicinski  *     without modification, are permitted provided that the following
134c352362SJakub Kicinski  *     conditions are met:
144c352362SJakub Kicinski  *
154c352362SJakub Kicinski  *      1. Redistributions of source code must retain the above
164c352362SJakub Kicinski  *         copyright notice, this list of conditions and the following
174c352362SJakub Kicinski  *         disclaimer.
184c352362SJakub Kicinski  *
194c352362SJakub Kicinski  *      2. Redistributions in binary form must reproduce the above
204c352362SJakub Kicinski  *         copyright notice, this list of conditions and the following
214c352362SJakub Kicinski  *         disclaimer in the documentation and/or other materials
224c352362SJakub Kicinski  *         provided with the distribution.
234c352362SJakub Kicinski  *
244c352362SJakub Kicinski  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
254c352362SJakub Kicinski  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
264c352362SJakub Kicinski  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
274c352362SJakub Kicinski  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
284c352362SJakub Kicinski  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
294c352362SJakub Kicinski  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
304c352362SJakub Kicinski  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
314c352362SJakub Kicinski  * SOFTWARE.
324c352362SJakub Kicinski  */
334c352362SJakub Kicinski 
344c352362SJakub Kicinski /*
354c352362SJakub Kicinski  * nfp_net.h
364c352362SJakub Kicinski  * Declarations for Netronome network device driver.
374c352362SJakub Kicinski  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
384c352362SJakub Kicinski  *          Jason McMullan <jason.mcmullan@netronome.com>
394c352362SJakub Kicinski  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
404c352362SJakub Kicinski  */
414c352362SJakub Kicinski 
424c352362SJakub Kicinski #ifndef _NFP_NET_H_
434c352362SJakub Kicinski #define _NFP_NET_H_
444c352362SJakub Kicinski 
454c352362SJakub Kicinski #include <linux/interrupt.h>
4663461a02SJakub Kicinski #include <linux/list.h>
474c352362SJakub Kicinski #include <linux/netdevice.h>
484c352362SJakub Kicinski #include <linux/pci.h>
49a1cbaad7SArnd Bergmann #include <linux/io-64-nonatomic-hi-lo.h>
507f1c684aSJesper Dangaard Brouer #include <net/xdp.h>
514c352362SJakub Kicinski 
524c352362SJakub Kicinski #include "nfp_net_ctrl.h"
534c352362SJakub Kicinski 
54c821e617SJakub Kicinski #define nn_pr(nn, lvl, fmt, args...)					\
55c821e617SJakub Kicinski 	({								\
56c821e617SJakub Kicinski 		struct nfp_net *__nn = (nn);				\
57c821e617SJakub Kicinski 									\
58c821e617SJakub Kicinski 		if (__nn->dp.netdev)					\
59c821e617SJakub Kicinski 			netdev_printk(lvl, __nn->dp.netdev, fmt, ## args); \
60c821e617SJakub Kicinski 		else							\
61c821e617SJakub Kicinski 			dev_printk(lvl, __nn->dp.dev, "ctrl: " fmt, ## args); \
62c821e617SJakub Kicinski 	})
63c821e617SJakub Kicinski 
64c821e617SJakub Kicinski #define nn_err(nn, fmt, args...)	nn_pr(nn, KERN_ERR, fmt, ## args)
65c821e617SJakub Kicinski #define nn_warn(nn, fmt, args...)	nn_pr(nn, KERN_WARNING, fmt, ## args)
66c821e617SJakub Kicinski #define nn_info(nn, fmt, args...)	nn_pr(nn, KERN_INFO, fmt, ## args)
67c821e617SJakub Kicinski #define nn_dbg(nn, fmt, args...)	nn_pr(nn, KERN_DEBUG, fmt, ## args)
68c821e617SJakub Kicinski 
6979c12a75SJakub Kicinski #define nn_dp_warn(dp, fmt, args...)					\
70c821e617SJakub Kicinski 	({								\
71c821e617SJakub Kicinski 		struct nfp_net_dp *__dp = (dp);				\
72c821e617SJakub Kicinski 									\
73c821e617SJakub Kicinski 		if (unlikely(net_ratelimit())) {			\
74c821e617SJakub Kicinski 			if (__dp->netdev)				\
75c821e617SJakub Kicinski 				netdev_warn(__dp->netdev, fmt, ## args); \
76c821e617SJakub Kicinski 			else						\
77c821e617SJakub Kicinski 				dev_warn(__dp->dev, fmt, ## args);	\
78c821e617SJakub Kicinski 		}							\
79c821e617SJakub Kicinski 	})
804c352362SJakub Kicinski 
813d780b92SJakub Kicinski /* Max time to wait for NFP to respond on updates (in seconds) */
823d780b92SJakub Kicinski #define NFP_NET_POLL_TIMEOUT	5
834c352362SJakub Kicinski 
8466860bebSJakub Kicinski /* Interval for reading offloaded filter stats */
8566860bebSJakub Kicinski #define NFP_NET_STAT_POLL_IVL	msecs_to_jiffies(100)
8666860bebSJakub Kicinski 
874c352362SJakub Kicinski /* Bar allocation */
88796312cdSJakub Kicinski #define NFP_NET_CTRL_BAR	0
894c352362SJakub Kicinski #define NFP_NET_Q0_BAR		2
904c352362SJakub Kicinski #define NFP_NET_Q1_BAR		4	/* OBSOLETE */
914c352362SJakub Kicinski 
924c352362SJakub Kicinski /* Max bits in DMA address */
934c352362SJakub Kicinski #define NFP_NET_MAX_DMA_BITS	40
944c352362SJakub Kicinski 
954c352362SJakub Kicinski /* Default size for MTU and freelist buffer sizes */
964c352362SJakub Kicinski #define NFP_NET_DEFAULT_MTU		1500
974c352362SJakub Kicinski 
984c352362SJakub Kicinski /* Maximum number of bytes prepended to a packet */
994c352362SJakub Kicinski #define NFP_NET_MAX_PREPEND		64
1004c352362SJakub Kicinski 
1014c352362SJakub Kicinski /* Interrupt definitions */
1024c352362SJakub Kicinski #define NFP_NET_NON_Q_VECTORS		2
1034c352362SJakub Kicinski #define NFP_NET_IRQ_LSC_IDX		0
1044c352362SJakub Kicinski #define NFP_NET_IRQ_EXN_IDX		1
105d4e7f092SJakub Kicinski #define NFP_NET_MIN_VNIC_IRQS		(NFP_NET_NON_Q_VECTORS + 1)
1064c352362SJakub Kicinski 
1074c352362SJakub Kicinski /* Queue/Ring definitions */
1084c352362SJakub Kicinski #define NFP_NET_MAX_TX_RINGS	64	/* Max. # of Tx rings per device */
1094c352362SJakub Kicinski #define NFP_NET_MAX_RX_RINGS	64	/* Max. # of Rx rings per device */
11023a4e389SJakub Kicinski #define NFP_NET_MAX_R_VECS	(NFP_NET_MAX_TX_RINGS > NFP_NET_MAX_RX_RINGS ? \
11123a4e389SJakub Kicinski 				 NFP_NET_MAX_TX_RINGS : NFP_NET_MAX_RX_RINGS)
11223a4e389SJakub Kicinski #define NFP_NET_MAX_IRQS	(NFP_NET_NON_Q_VECTORS + NFP_NET_MAX_R_VECS)
1134c352362SJakub Kicinski 
1144c352362SJakub Kicinski #define NFP_NET_MIN_TX_DESCS	256	/* Min. # of Tx descs per ring */
1154c352362SJakub Kicinski #define NFP_NET_MIN_RX_DESCS	256	/* Min. # of Rx descs per ring */
1164c352362SJakub Kicinski #define NFP_NET_MAX_TX_DESCS	(256 * 1024) /* Max. # of Tx descs per ring */
1174c352362SJakub Kicinski #define NFP_NET_MAX_RX_DESCS	(256 * 1024) /* Max. # of Rx descs per ring */
1184c352362SJakub Kicinski 
1194c352362SJakub Kicinski #define NFP_NET_TX_DESCS_DEFAULT 4096	/* Default # of Tx descs per ring */
1204c352362SJakub Kicinski #define NFP_NET_RX_DESCS_DEFAULT 4096	/* Default # of Rx descs per ring */
1214c352362SJakub Kicinski 
1224c352362SJakub Kicinski #define NFP_NET_FL_BATCH	16	/* Add freelist in this Batch size */
123abeeec4aSJakub Kicinski #define NFP_NET_XDP_MAX_COMPLETE 2048	/* XDP bufs to reclaim in NAPI poll */
1244c352362SJakub Kicinski 
1254c352362SJakub Kicinski /* Offload definitions */
1264c352362SJakub Kicinski #define NFP_NET_N_VXLAN_PORTS	(NFP_NET_CFG_VXLAN_SZ / sizeof(__be16))
1274c352362SJakub Kicinski 
128c0f031bcSJakub Kicinski #define NFP_NET_RX_BUF_HEADROOM	(NET_SKB_PAD + NET_IP_ALIGN)
129c0f031bcSJakub Kicinski #define NFP_NET_RX_BUF_NON_DATA	(NFP_NET_RX_BUF_HEADROOM +		\
130c0f031bcSJakub Kicinski 				 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
131c0f031bcSJakub Kicinski 
1324c352362SJakub Kicinski /* Forward declarations */
133bd5ca062SJakub Kicinski struct nfp_cpp;
13447465aedSJakub Kicinski struct nfp_eth_table_port;
1354c352362SJakub Kicinski struct nfp_net;
1364c352362SJakub Kicinski struct nfp_net_r_vector;
137eb488c26SJakub Kicinski struct nfp_port;
1384c352362SJakub Kicinski 
1394aa3b766SJakub Kicinski /* Convenience macro for wrapping descriptor index on ring size */
1404aa3b766SJakub Kicinski #define D_IDX(ring, idx)	((idx) & ((ring)->cnt - 1))
1414aa3b766SJakub Kicinski 
1424c352362SJakub Kicinski /* Convenience macro for writing dma address into RX/TX descriptors */
1434c352362SJakub Kicinski #define nfp_desc_set_dma_addr(desc, dma_addr)				\
1444c352362SJakub Kicinski 	do {								\
1454c352362SJakub Kicinski 		__typeof(desc) __d = (desc);				\
1464c352362SJakub Kicinski 		dma_addr_t __addr = (dma_addr);				\
1474c352362SJakub Kicinski 									\
1484c352362SJakub Kicinski 		__d->dma_addr_lo = cpu_to_le32(lower_32_bits(__addr));	\
1494c352362SJakub Kicinski 		__d->dma_addr_hi = upper_32_bits(__addr) & 0xff;	\
1504c352362SJakub Kicinski 	} while (0)
1514c352362SJakub Kicinski 
1524c352362SJakub Kicinski /* TX descriptor format */
1534c352362SJakub Kicinski 
1544c352362SJakub Kicinski #define PCIE_DESC_TX_EOP		BIT(7)
1554c352362SJakub Kicinski #define PCIE_DESC_TX_OFFSET_MASK	GENMASK(6, 0)
1564c352362SJakub Kicinski #define PCIE_DESC_TX_MSS_MASK		GENMASK(13, 0)
1574c352362SJakub Kicinski 
1584c352362SJakub Kicinski /* Flags in the host TX descriptor */
1594c352362SJakub Kicinski #define PCIE_DESC_TX_CSUM		BIT(7)
1604c352362SJakub Kicinski #define PCIE_DESC_TX_IP4_CSUM		BIT(6)
1614c352362SJakub Kicinski #define PCIE_DESC_TX_TCP_CSUM		BIT(5)
1624c352362SJakub Kicinski #define PCIE_DESC_TX_UDP_CSUM		BIT(4)
1634c352362SJakub Kicinski #define PCIE_DESC_TX_VLAN		BIT(3)
1644c352362SJakub Kicinski #define PCIE_DESC_TX_LSO		BIT(2)
1654c352362SJakub Kicinski #define PCIE_DESC_TX_ENCAP		BIT(1)
1664c352362SJakub Kicinski #define PCIE_DESC_TX_O_IP4_CSUM	BIT(0)
1674c352362SJakub Kicinski 
1684c352362SJakub Kicinski struct nfp_net_tx_desc {
1694c352362SJakub Kicinski 	union {
1704c352362SJakub Kicinski 		struct {
1714c352362SJakub Kicinski 			u8 dma_addr_hi; /* High bits of host buf address */
1724c352362SJakub Kicinski 			__le16 dma_len;	/* Length to DMA for this desc */
1734c352362SJakub Kicinski 			u8 offset_eop;	/* Offset in buf where pkt starts +
1744c352362SJakub Kicinski 					 * highest bit is eop flag.
1754c352362SJakub Kicinski 					 */
1764c352362SJakub Kicinski 			__le32 dma_addr_lo; /* Low 32bit of host buf addr */
1774c352362SJakub Kicinski 
1784c352362SJakub Kicinski 			__le16 mss;	/* MSS to be used for LSO */
179e53fc9faSEdwin Peer 			u8 lso_hdrlen;	/* LSO, TCP payload offset */
1804c352362SJakub Kicinski 			u8 flags;	/* TX Flags, see @PCIE_DESC_TX_* */
18128063be6SEdwin Peer 			union {
18228063be6SEdwin Peer 				struct {
18328063be6SEdwin Peer 					u8 l3_offset; /* L3 header offset */
18428063be6SEdwin Peer 					u8 l4_offset; /* L4 header offset */
18528063be6SEdwin Peer 				};
1864c352362SJakub Kicinski 				__le16 vlan; /* VLAN tag to add if indicated */
18728063be6SEdwin Peer 			};
1884c352362SJakub Kicinski 			__le16 data_len; /* Length of frame + meta data */
1894c352362SJakub Kicinski 		} __packed;
1904c352362SJakub Kicinski 		__le32 vals[4];
1914c352362SJakub Kicinski 	};
1924c352362SJakub Kicinski };
1934c352362SJakub Kicinski 
1944c352362SJakub Kicinski /**
1954c352362SJakub Kicinski  * struct nfp_net_tx_buf - software TX buffer descriptor
1964c352362SJakub Kicinski  * @skb:	sk_buff associated with this buffer
1974c352362SJakub Kicinski  * @dma_addr:	DMA mapping address of the buffer
1984c352362SJakub Kicinski  * @fidx:	Fragment index (-1 for the head and [0..nr_frags-1] for frags)
1994c352362SJakub Kicinski  * @pkt_cnt:	Number of packets to be produced out of the skb associated
2004c352362SJakub Kicinski  *		with this buffer (valid only on the head's buffer).
2014c352362SJakub Kicinski  *		Will be 1 for all non-TSO packets.
2024c352362SJakub Kicinski  * @real_len:	Number of bytes which to be produced out of the skb (valid only
2034c352362SJakub Kicinski  *		on the head's buffer). Equal to skb->len for non-TSO packets.
2044c352362SJakub Kicinski  */
2054c352362SJakub Kicinski struct nfp_net_tx_buf {
206ecd63a02SJakub Kicinski 	union {
2074c352362SJakub Kicinski 		struct sk_buff *skb;
208ecd63a02SJakub Kicinski 		void *frag;
209ecd63a02SJakub Kicinski 	};
2104c352362SJakub Kicinski 	dma_addr_t dma_addr;
2114c352362SJakub Kicinski 	short int fidx;
2124c352362SJakub Kicinski 	u16 pkt_cnt;
2134c352362SJakub Kicinski 	u32 real_len;
2144c352362SJakub Kicinski };
2154c352362SJakub Kicinski 
2164c352362SJakub Kicinski /**
2174c352362SJakub Kicinski  * struct nfp_net_tx_ring - TX ring structure
2184c352362SJakub Kicinski  * @r_vec:      Back pointer to ring vector structure
2194c352362SJakub Kicinski  * @idx:        Ring index from Linux's perspective
2204c352362SJakub Kicinski  * @qcidx:      Queue Controller Peripheral (QCP) queue index for the TX queue
2214c352362SJakub Kicinski  * @qcp_q:      Pointer to base of the QCP TX queue
2224c352362SJakub Kicinski  * @cnt:        Size of the queue in number of descriptors
2234c352362SJakub Kicinski  * @wr_p:       TX ring write pointer (free running)
2244c352362SJakub Kicinski  * @rd_p:       TX ring read pointer (free running)
2254c352362SJakub Kicinski  * @qcp_rd_p:   Local copy of QCP TX queue read pointer
2264c352362SJakub Kicinski  * @wr_ptr_add:	Accumulated number of buffers to add to QCP write pointer
2274c352362SJakub Kicinski  *		(used for .xmit_more delayed kick)
2284c352362SJakub Kicinski  * @txbufs:     Array of transmitted TX buffers, to free on transmit
2294c352362SJakub Kicinski  * @txds:       Virtual address of TX ring in host memory
2304c352362SJakub Kicinski  * @dma:        DMA address of the TX ring
2314c352362SJakub Kicinski  * @size:       Size, in bytes, of the TX ring (needed to free)
23292e68195SJakub Kicinski  * @is_xdp:	Is this a XDP TX ring?
2334c352362SJakub Kicinski  */
2344c352362SJakub Kicinski struct nfp_net_tx_ring {
2354c352362SJakub Kicinski 	struct nfp_net_r_vector *r_vec;
2364c352362SJakub Kicinski 
2374c352362SJakub Kicinski 	u32 idx;
2384c352362SJakub Kicinski 	int qcidx;
2394c352362SJakub Kicinski 	u8 __iomem *qcp_q;
2404c352362SJakub Kicinski 
2414c352362SJakub Kicinski 	u32 cnt;
2424c352362SJakub Kicinski 	u32 wr_p;
2434c352362SJakub Kicinski 	u32 rd_p;
2444c352362SJakub Kicinski 	u32 qcp_rd_p;
2454c352362SJakub Kicinski 
2464c352362SJakub Kicinski 	u32 wr_ptr_add;
2474c352362SJakub Kicinski 
2484c352362SJakub Kicinski 	struct nfp_net_tx_buf *txbufs;
2494c352362SJakub Kicinski 	struct nfp_net_tx_desc *txds;
2504c352362SJakub Kicinski 
2514c352362SJakub Kicinski 	dma_addr_t dma;
2524c352362SJakub Kicinski 	unsigned int size;
25392e68195SJakub Kicinski 	bool is_xdp;
2544c352362SJakub Kicinski } ____cacheline_aligned;
2554c352362SJakub Kicinski 
2564c352362SJakub Kicinski /* RX and freelist descriptor format */
2574c352362SJakub Kicinski 
2584c352362SJakub Kicinski #define PCIE_DESC_RX_DD			BIT(7)
2594c352362SJakub Kicinski #define PCIE_DESC_RX_META_LEN_MASK	GENMASK(6, 0)
2604c352362SJakub Kicinski 
2614c352362SJakub Kicinski /* Flags in the RX descriptor */
2624c352362SJakub Kicinski #define PCIE_DESC_RX_RSS		cpu_to_le16(BIT(15))
2634c352362SJakub Kicinski #define PCIE_DESC_RX_I_IP4_CSUM		cpu_to_le16(BIT(14))
2644c352362SJakub Kicinski #define PCIE_DESC_RX_I_IP4_CSUM_OK	cpu_to_le16(BIT(13))
2654c352362SJakub Kicinski #define PCIE_DESC_RX_I_TCP_CSUM		cpu_to_le16(BIT(12))
2664c352362SJakub Kicinski #define PCIE_DESC_RX_I_TCP_CSUM_OK	cpu_to_le16(BIT(11))
2674c352362SJakub Kicinski #define PCIE_DESC_RX_I_UDP_CSUM		cpu_to_le16(BIT(10))
2684c352362SJakub Kicinski #define PCIE_DESC_RX_I_UDP_CSUM_OK	cpu_to_le16(BIT(9))
2697533fdc0SJakub Kicinski #define PCIE_DESC_RX_BPF		cpu_to_le16(BIT(8))
2704c352362SJakub Kicinski #define PCIE_DESC_RX_EOP		cpu_to_le16(BIT(7))
2714c352362SJakub Kicinski #define PCIE_DESC_RX_IP4_CSUM		cpu_to_le16(BIT(6))
2724c352362SJakub Kicinski #define PCIE_DESC_RX_IP4_CSUM_OK	cpu_to_le16(BIT(5))
2734c352362SJakub Kicinski #define PCIE_DESC_RX_TCP_CSUM		cpu_to_le16(BIT(4))
2744c352362SJakub Kicinski #define PCIE_DESC_RX_TCP_CSUM_OK	cpu_to_le16(BIT(3))
2754c352362SJakub Kicinski #define PCIE_DESC_RX_UDP_CSUM		cpu_to_le16(BIT(2))
2764c352362SJakub Kicinski #define PCIE_DESC_RX_UDP_CSUM_OK	cpu_to_le16(BIT(1))
2774c352362SJakub Kicinski #define PCIE_DESC_RX_VLAN		cpu_to_le16(BIT(0))
2784c352362SJakub Kicinski 
2794c352362SJakub Kicinski #define PCIE_DESC_RX_CSUM_ALL		(PCIE_DESC_RX_IP4_CSUM |	\
2804c352362SJakub Kicinski 					 PCIE_DESC_RX_TCP_CSUM |	\
2814c352362SJakub Kicinski 					 PCIE_DESC_RX_UDP_CSUM |	\
2824c352362SJakub Kicinski 					 PCIE_DESC_RX_I_IP4_CSUM |	\
2834c352362SJakub Kicinski 					 PCIE_DESC_RX_I_TCP_CSUM |	\
2844c352362SJakub Kicinski 					 PCIE_DESC_RX_I_UDP_CSUM)
2854c352362SJakub Kicinski #define PCIE_DESC_RX_CSUM_OK_SHIFT	1
2864c352362SJakub Kicinski #define __PCIE_DESC_RX_CSUM_ALL		le16_to_cpu(PCIE_DESC_RX_CSUM_ALL)
2874c352362SJakub Kicinski #define __PCIE_DESC_RX_CSUM_ALL_OK	(__PCIE_DESC_RX_CSUM_ALL >>	\
2884c352362SJakub Kicinski 					 PCIE_DESC_RX_CSUM_OK_SHIFT)
2894c352362SJakub Kicinski 
2904c352362SJakub Kicinski struct nfp_net_rx_desc {
2914c352362SJakub Kicinski 	union {
2924c352362SJakub Kicinski 		struct {
2934c352362SJakub Kicinski 			u8 dma_addr_hi;	/* High bits of the buf address */
2944c352362SJakub Kicinski 			__le16 reserved; /* Must be zero */
2954c352362SJakub Kicinski 			u8 meta_len_dd; /* Must be zero */
2964c352362SJakub Kicinski 
2974c352362SJakub Kicinski 			__le32 dma_addr_lo; /* Low bits of the buffer address */
2984c352362SJakub Kicinski 		} __packed fld;
2994c352362SJakub Kicinski 
3004c352362SJakub Kicinski 		struct {
3014c352362SJakub Kicinski 			__le16 data_len; /* Length of the frame + meta data */
3024c352362SJakub Kicinski 			u8 reserved;
3034c352362SJakub Kicinski 			u8 meta_len_dd;	/* Length of meta data prepended +
3044c352362SJakub Kicinski 					 * descriptor done flag.
3054c352362SJakub Kicinski 					 */
3064c352362SJakub Kicinski 
3074c352362SJakub Kicinski 			__le16 flags;	/* RX flags. See @PCIE_DESC_RX_* */
3084c352362SJakub Kicinski 			__le16 vlan;	/* VLAN if stripped */
3094c352362SJakub Kicinski 		} __packed rxd;
3104c352362SJakub Kicinski 
3114c352362SJakub Kicinski 		__le32 vals[2];
3124c352362SJakub Kicinski 	};
3134c352362SJakub Kicinski };
3144c352362SJakub Kicinski 
31519d0f54eSJakub Kicinski #define NFP_NET_META_FIELD_MASK GENMASK(NFP_NET_META_FIELD_SIZE - 1, 0)
31619d0f54eSJakub Kicinski 
317e524a6a9SJakub Kicinski struct nfp_meta_parsed {
318ddb98d94SJakub Kicinski 	u8 hash_type;
319ddb98d94SJakub Kicinski 	u8 csum_type;
320e524a6a9SJakub Kicinski 	u32 hash;
321e524a6a9SJakub Kicinski 	u32 mark;
32291bf82caSSimon Horman 	u32 portid;
323ddb98d94SJakub Kicinski 	__wsum csum;
324e524a6a9SJakub Kicinski };
325e524a6a9SJakub Kicinski 
3264c352362SJakub Kicinski struct nfp_net_rx_hash {
3274c352362SJakub Kicinski 	__be32 hash_type;
3284c352362SJakub Kicinski 	__be32 hash;
3294c352362SJakub Kicinski };
3304c352362SJakub Kicinski 
3314c352362SJakub Kicinski /**
3324c352362SJakub Kicinski  * struct nfp_net_rx_buf - software RX buffer descriptor
333c0f031bcSJakub Kicinski  * @frag:	page fragment buffer
3344c352362SJakub Kicinski  * @dma_addr:	DMA mapping address of the buffer
3354c352362SJakub Kicinski  */
3364c352362SJakub Kicinski struct nfp_net_rx_buf {
337c0f031bcSJakub Kicinski 	void *frag;
3384c352362SJakub Kicinski 	dma_addr_t dma_addr;
3394c352362SJakub Kicinski };
3404c352362SJakub Kicinski 
3414c352362SJakub Kicinski /**
3424c352362SJakub Kicinski  * struct nfp_net_rx_ring - RX ring structure
3434c352362SJakub Kicinski  * @r_vec:      Back pointer to ring vector structure
3444c352362SJakub Kicinski  * @cnt:        Size of the queue in number of descriptors
3454c352362SJakub Kicinski  * @wr_p:       FL/RX ring write pointer (free running)
3464c352362SJakub Kicinski  * @rd_p:       FL/RX ring read pointer (free running)
3474c352362SJakub Kicinski  * @idx:        Ring index from Linux's perspective
3484c352362SJakub Kicinski  * @fl_qcidx:   Queue Controller Peripheral (QCP) queue index for the freelist
3494c352362SJakub Kicinski  * @qcp_fl:     Pointer to base of the QCP freelist queue
3504c352362SJakub Kicinski  * @rxbufs:     Array of transmitted FL/RX buffers
3514c352362SJakub Kicinski  * @rxds:       Virtual address of FL/RX ring in host memory
3524c352362SJakub Kicinski  * @dma:        DMA address of the FL/RX ring
3534c352362SJakub Kicinski  * @size:       Size, in bytes, of the FL/RX ring (needed to free)
3547f1c684aSJesper Dangaard Brouer  * @xdp_rxq:    RX-ring info avail for XDP
3554c352362SJakub Kicinski  */
3564c352362SJakub Kicinski struct nfp_net_rx_ring {
3574c352362SJakub Kicinski 	struct nfp_net_r_vector *r_vec;
3584c352362SJakub Kicinski 
3594c352362SJakub Kicinski 	u32 cnt;
3604c352362SJakub Kicinski 	u32 wr_p;
3614c352362SJakub Kicinski 	u32 rd_p;
3624c352362SJakub Kicinski 
36383d08a1dSJakub Kicinski 	u32 idx;
3644c352362SJakub Kicinski 
3654c352362SJakub Kicinski 	int fl_qcidx;
3667f1c684aSJesper Dangaard Brouer 	unsigned int size;
3674c352362SJakub Kicinski 	u8 __iomem *qcp_fl;
3684c352362SJakub Kicinski 
3694c352362SJakub Kicinski 	struct nfp_net_rx_buf *rxbufs;
3704c352362SJakub Kicinski 	struct nfp_net_rx_desc *rxds;
3714c352362SJakub Kicinski 
3724c352362SJakub Kicinski 	dma_addr_t dma;
3737f1c684aSJesper Dangaard Brouer 	struct xdp_rxq_info xdp_rxq;
3744c352362SJakub Kicinski } ____cacheline_aligned;
3754c352362SJakub Kicinski 
3764c352362SJakub Kicinski /**
3774c352362SJakub Kicinski  * struct nfp_net_r_vector - Per ring interrupt vector configuration
3784c352362SJakub Kicinski  * @nfp_net:        Backpointer to nfp_net structure
3794c352362SJakub Kicinski  * @napi:           NAPI structure for this ring vec
3804c352362SJakub Kicinski  * @tx_ring:        Pointer to TX ring
3814c352362SJakub Kicinski  * @rx_ring:        Pointer to RX ring
382ecd63a02SJakub Kicinski  * @xdp_ring:	    Pointer to an extra TX ring for XDP
383fdace6c2SJakub Kicinski  * @irq_entry:      MSI-X table entry (use for talking to the device)
3844c352362SJakub Kicinski  * @rx_sync:	    Seqlock for atomic updates of RX stats
3854c352362SJakub Kicinski  * @rx_pkts:        Number of received packets
3864c352362SJakub Kicinski  * @rx_bytes:	    Number of received bytes
3874c352362SJakub Kicinski  * @rx_drops:	    Number of packets dropped on RX due to lack of resources
3884c352362SJakub Kicinski  * @hw_csum_rx_ok:  Counter of packets where the HW checksum was OK
3894c352362SJakub Kicinski  * @hw_csum_rx_inner_ok: Counter of packets where the inner HW checksum was OK
3904c352362SJakub Kicinski  * @hw_csum_rx_error:	 Counter of packets with bad checksums
3914c352362SJakub Kicinski  * @tx_sync:	    Seqlock for atomic updates of TX stats
3924c352362SJakub Kicinski  * @tx_pkts:	    Number of Transmitted packets
3934c352362SJakub Kicinski  * @tx_bytes:	    Number of Transmitted bytes
3944c352362SJakub Kicinski  * @hw_csum_tx:	    Counter of packets with TX checksum offload requested
3954c352362SJakub Kicinski  * @hw_csum_tx_inner:	 Counter of inner TX checksum offload requests
3964c352362SJakub Kicinski  * @tx_gather:	    Counter of packets with Gather DMA
3974c352362SJakub Kicinski  * @tx_lso:	    Counter of LSO packets sent
3984c352362SJakub Kicinski  * @tx_errors:	    How many TX errors were encountered
3994c352362SJakub Kicinski  * @tx_busy:        How often was TX busy (no space)?
40016f50cdaSJakub Kicinski  * @rx_replace_buf_alloc_fail:	Counter of RX buffer allocation failures
401fdace6c2SJakub Kicinski  * @irq_vector:     Interrupt vector number (use for talking to the OS)
4024c352362SJakub Kicinski  * @handler:        Interrupt handler for this ring vector
4034c352362SJakub Kicinski  * @name:           Name of the interrupt vector
4044c352362SJakub Kicinski  * @affinity_mask:  SMP affinity mask for this vector
4054c352362SJakub Kicinski  *
4064c352362SJakub Kicinski  * This structure ties RX and TX rings to interrupt vectors and a NAPI
4074c352362SJakub Kicinski  * context. This currently only supports one RX and TX ring per
4084c352362SJakub Kicinski  * interrupt vector but might be extended in the future to allow
4094c352362SJakub Kicinski  * association of multiple rings per vector.
4104c352362SJakub Kicinski  */
4114c352362SJakub Kicinski struct nfp_net_r_vector {
4124c352362SJakub Kicinski 	struct nfp_net *nfp_net;
41377ece8d5SJakub Kicinski 	union {
4144c352362SJakub Kicinski 		struct napi_struct napi;
41577ece8d5SJakub Kicinski 		struct {
41677ece8d5SJakub Kicinski 			struct tasklet_struct tasklet;
41777ece8d5SJakub Kicinski 			struct sk_buff_head queue;
41877ece8d5SJakub Kicinski 			struct spinlock lock;
41977ece8d5SJakub Kicinski 		};
42077ece8d5SJakub Kicinski 	};
4214c352362SJakub Kicinski 
4224c352362SJakub Kicinski 	struct nfp_net_tx_ring *tx_ring;
4234c352362SJakub Kicinski 	struct nfp_net_rx_ring *rx_ring;
4244c352362SJakub Kicinski 
425fdace6c2SJakub Kicinski 	u16 irq_entry;
4264c352362SJakub Kicinski 
4274c352362SJakub Kicinski 	struct u64_stats_sync rx_sync;
4284c352362SJakub Kicinski 	u64 rx_pkts;
4294c352362SJakub Kicinski 	u64 rx_bytes;
4304c352362SJakub Kicinski 	u64 rx_drops;
4314c352362SJakub Kicinski 	u64 hw_csum_rx_ok;
4324c352362SJakub Kicinski 	u64 hw_csum_rx_inner_ok;
4334c352362SJakub Kicinski 	u64 hw_csum_rx_error;
4344c352362SJakub Kicinski 
435ecd63a02SJakub Kicinski 	struct nfp_net_tx_ring *xdp_ring;
436ecd63a02SJakub Kicinski 
4374c352362SJakub Kicinski 	struct u64_stats_sync tx_sync;
4384c352362SJakub Kicinski 	u64 tx_pkts;
4394c352362SJakub Kicinski 	u64 tx_bytes;
4404c352362SJakub Kicinski 	u64 hw_csum_tx;
4414c352362SJakub Kicinski 	u64 hw_csum_tx_inner;
4424c352362SJakub Kicinski 	u64 tx_gather;
4434c352362SJakub Kicinski 	u64 tx_lso;
44416f50cdaSJakub Kicinski 
44516f50cdaSJakub Kicinski 	u64 rx_replace_buf_alloc_fail;
4464c352362SJakub Kicinski 	u64 tx_errors;
4474c352362SJakub Kicinski 	u64 tx_busy;
4484c352362SJakub Kicinski 
449fdace6c2SJakub Kicinski 	u32 irq_vector;
4504c352362SJakub Kicinski 	irq_handler_t handler;
4514c352362SJakub Kicinski 	char name[IFNAMSIZ + 8];
4524c352362SJakub Kicinski 	cpumask_t affinity_mask;
4534c352362SJakub Kicinski } ____cacheline_aligned;
4544c352362SJakub Kicinski 
4554c352362SJakub Kicinski /* Firmware version as it is written in the 32bit value in the BAR */
4564c352362SJakub Kicinski struct nfp_net_fw_version {
4574c352362SJakub Kicinski 	u8 minor;
4584c352362SJakub Kicinski 	u8 major;
4594c352362SJakub Kicinski 	u8 class;
4604c352362SJakub Kicinski 	u8 resv;
4614c352362SJakub Kicinski } __packed;
4624c352362SJakub Kicinski 
4634c352362SJakub Kicinski static inline bool nfp_net_fw_ver_eq(struct nfp_net_fw_version *fw_ver,
4644c352362SJakub Kicinski 				     u8 resv, u8 class, u8 major, u8 minor)
4654c352362SJakub Kicinski {
4664c352362SJakub Kicinski 	return fw_ver->resv == resv &&
4674c352362SJakub Kicinski 	       fw_ver->class == class &&
4684c352362SJakub Kicinski 	       fw_ver->major == major &&
4694c352362SJakub Kicinski 	       fw_ver->minor == minor;
4704c352362SJakub Kicinski }
4714c352362SJakub Kicinski 
47266860bebSJakub Kicinski struct nfp_stat_pair {
47366860bebSJakub Kicinski 	u64 pkts;
47466860bebSJakub Kicinski 	u64 bytes;
47566860bebSJakub Kicinski };
47666860bebSJakub Kicinski 
4774c352362SJakub Kicinski /**
47879c12a75SJakub Kicinski  * struct nfp_net_dp - NFP network device datapath data structure
479fa43d2a8SJakub Kicinski  * @dev:		Backpointer to struct device
4804c352362SJakub Kicinski  * @netdev:		Backpointer to net_device structure
4814c352362SJakub Kicinski  * @is_vf:		Is the driver attached to a VF?
4826d677075SJakub Kicinski  * @bpf_offload_xdp:	Offloaded BPF program is XDP
483b9dcf88aSJakub Kicinski  * @chained_metadata_format:  Firemware will use new metadata format
484c487e6b1SJakub Kicinski  * @rx_dma_dir:		Mapping direction for RX buffers
4856fe0c3b4SJakub Kicinski  * @rx_dma_off:		Offset at which DMA packets (for XDP headroom)
48697717acaSJakub Kicinski  * @rx_offset:		Offset in the RX buffers where packet data starts
4874c352362SJakub Kicinski  * @ctrl:		Local copy of the control register/word.
4884c352362SJakub Kicinski  * @fl_bufsz:		Currently configured size of the freelist buffers
489ecd63a02SJakub Kicinski  * @xdp_prog:		Installed XDP program
49079c12a75SJakub Kicinski  * @tx_rings:		Array of pre-allocated TX ring structures
49179c12a75SJakub Kicinski  * @rx_rings:		Array of pre-allocated RX ring structures
492d2b84397SJakub Kicinski  * @ctrl_bar:		Pointer to mapped control BAR
49379c12a75SJakub Kicinski  *
49479c12a75SJakub Kicinski  * @txd_cnt:		Size of the TX ring in number of descriptors
49579c12a75SJakub Kicinski  * @rxd_cnt:		Size of the RX ring in number of descriptors
49679c12a75SJakub Kicinski  * @num_r_vecs:		Number of used ring vectors
49779c12a75SJakub Kicinski  * @num_tx_rings:	Currently configured number of TX rings
49879c12a75SJakub Kicinski  * @num_stack_tx_rings:	Number of TX rings used by the stack (not XDP)
49979c12a75SJakub Kicinski  * @num_rx_rings:	Currently configured number of RX rings
50076e1e1a8SJakub Kicinski  * @mtu:		Device MTU
50179c12a75SJakub Kicinski  */
50279c12a75SJakub Kicinski struct nfp_net_dp {
50379c12a75SJakub Kicinski 	struct device *dev;
50479c12a75SJakub Kicinski 	struct net_device *netdev;
50579c12a75SJakub Kicinski 
506c487e6b1SJakub Kicinski 	u8 is_vf:1;
507c487e6b1SJakub Kicinski 	u8 bpf_offload_xdp:1;
508c487e6b1SJakub Kicinski 	u8 chained_metadata_format:1;
509c487e6b1SJakub Kicinski 
510c487e6b1SJakub Kicinski 	u8 rx_dma_dir;
51197717acaSJakub Kicinski 	u8 rx_offset;
51297717acaSJakub Kicinski 
513dbf637ffSJakub Kicinski 	u32 rx_dma_off;
514dbf637ffSJakub Kicinski 
51579c12a75SJakub Kicinski 	u32 ctrl;
51679c12a75SJakub Kicinski 	u32 fl_bufsz;
51779c12a75SJakub Kicinski 
51879c12a75SJakub Kicinski 	struct bpf_prog *xdp_prog;
51979c12a75SJakub Kicinski 
52079c12a75SJakub Kicinski 	struct nfp_net_tx_ring *tx_rings;
52179c12a75SJakub Kicinski 	struct nfp_net_rx_ring *rx_rings;
52279c12a75SJakub Kicinski 
523d2b84397SJakub Kicinski 	u8 __iomem *ctrl_bar;
524d2b84397SJakub Kicinski 
52579c12a75SJakub Kicinski 	/* Cold data follows */
52679c12a75SJakub Kicinski 
52779c12a75SJakub Kicinski 	unsigned int txd_cnt;
52879c12a75SJakub Kicinski 	unsigned int rxd_cnt;
52979c12a75SJakub Kicinski 
53079c12a75SJakub Kicinski 	unsigned int num_r_vecs;
53179c12a75SJakub Kicinski 
53279c12a75SJakub Kicinski 	unsigned int num_tx_rings;
53379c12a75SJakub Kicinski 	unsigned int num_stack_tx_rings;
53479c12a75SJakub Kicinski 	unsigned int num_rx_rings;
53576e1e1a8SJakub Kicinski 
53676e1e1a8SJakub Kicinski 	unsigned int mtu;
53779c12a75SJakub Kicinski };
53879c12a75SJakub Kicinski 
53979c12a75SJakub Kicinski /**
54079c12a75SJakub Kicinski  * struct nfp_net - NFP network device structure
54179c12a75SJakub Kicinski  * @dp:			Datapath structure
5424c352362SJakub Kicinski  * @fw_ver:		Firmware version
5434c352362SJakub Kicinski  * @cap:                Capabilities advertised by the Firmware
5444c352362SJakub Kicinski  * @max_mtu:            Maximum support MTU advertised by the Firmware
5459ff304bfSJakub Kicinski  * @rss_hfunc:		RSS selected hash function
5464c352362SJakub Kicinski  * @rss_cfg:            RSS configuration
5474c352362SJakub Kicinski  * @rss_key:            RSS secret key
5484c352362SJakub Kicinski  * @rss_itbl:           RSS indirection table
5499f82fca9SJakub Kicinski  * @xdp_flags:		Flags with which XDP prog was loaded
5506a8ef542SJakub Kicinski  * @xdp_prog:		XDP prog (for ctrl path, both DRV and HW modes)
55179c12a75SJakub Kicinski  * @max_r_vecs:		Number of allocated interrupt vectors for RX/TX
5524c352362SJakub Kicinski  * @max_tx_rings:       Maximum number of TX rings supported by the Firmware
5534c352362SJakub Kicinski  * @max_rx_rings:       Maximum number of RX rings supported by the Firmware
554a646c9b2SJakub Kicinski  * @stride_rx:		Queue controller RX queue spacing
555a646c9b2SJakub Kicinski  * @stride_tx:		Queue controller TX queue spacing
5564c352362SJakub Kicinski  * @r_vecs:             Pre-allocated array of ring vectors
5574c352362SJakub Kicinski  * @irq_entries:        Pre-allocated array of MSI-X entries
5584c352362SJakub Kicinski  * @lsc_handler:        Handler for Link State Change interrupt
5594c352362SJakub Kicinski  * @lsc_name:           Name for Link State Change interrupt
5604c352362SJakub Kicinski  * @exn_handler:        Handler for Exception interrupt
5614c352362SJakub Kicinski  * @exn_name:           Name for Exception interrupt
5624c352362SJakub Kicinski  * @shared_handler:     Handler for shared interrupts
5634c352362SJakub Kicinski  * @shared_name:        Name for shared interrupt
5644c352362SJakub Kicinski  * @me_freq_mhz:        ME clock_freq (MHz)
5654c352362SJakub Kicinski  * @reconfig_lock:	Protects HW reconfiguration request regs/machinery
5663d780b92SJakub Kicinski  * @reconfig_posted:	Pending reconfig bits coming from async sources
5673d780b92SJakub Kicinski  * @reconfig_timer_active:  Timer for reading reconfiguration results is pending
5683d780b92SJakub Kicinski  * @reconfig_sync_present:  Some thread is performing synchronous reconfig
5693d780b92SJakub Kicinski  * @reconfig_timer:	Timer for async reading of reconfig results
5704c352362SJakub Kicinski  * @link_up:            Is the link up?
571cee42951SJakub Kicinski  * @link_status_lock:	Protects @link_* and ensures atomicity with BAR reading
5724c352362SJakub Kicinski  * @rx_coalesce_usecs:      RX interrupt moderation usecs delay parameter
5734c352362SJakub Kicinski  * @rx_coalesce_max_frames: RX interrupt moderation frame count parameter
5744c352362SJakub Kicinski  * @tx_coalesce_usecs:      TX interrupt moderation usecs delay parameter
5754c352362SJakub Kicinski  * @tx_coalesce_max_frames: TX interrupt moderation frame count parameter
5764c352362SJakub Kicinski  * @vxlan_ports:	VXLAN ports for RX inner csum offload communicated to HW
5774c352362SJakub Kicinski  * @vxlan_usecnt:	IPv4/IPv6 VXLAN port use counts
5784c352362SJakub Kicinski  * @qcp_cfg:            Pointer to QCP queue used for configuration notification
5794c352362SJakub Kicinski  * @tx_bar:             Pointer to mapped TX queues
5804c352362SJakub Kicinski  * @rx_bar:             Pointer to mapped FL/RX queues
5814c352362SJakub Kicinski  * @debugfs_dir:	Device directory in debugfs
582d4e7f092SJakub Kicinski  * @vnic_list:		Entry on device vNIC list
583fa43d2a8SJakub Kicinski  * @pdev:		Backpointer to PCI device
5847ac9ebd5SJakub Kicinski  * @app:		APP handle if available
585eb488c26SJakub Kicinski  * @port:		Pointer to nfp_port structure if vNIC is a port
586c66a9cf4SJakub Kicinski  * @app_priv:		APP private data for this vNIC
5874c352362SJakub Kicinski  */
5884c352362SJakub Kicinski struct nfp_net {
58979c12a75SJakub Kicinski 	struct nfp_net_dp dp;
59073725d9dSJakub Kicinski 
5914c352362SJakub Kicinski 	struct nfp_net_fw_version fw_ver;
59279c12a75SJakub Kicinski 
5934c352362SJakub Kicinski 	u32 cap;
5944c352362SJakub Kicinski 	u32 max_mtu;
5954c352362SJakub Kicinski 
5969ff304bfSJakub Kicinski 	u8 rss_hfunc;
5974c352362SJakub Kicinski 	u32 rss_cfg;
5984c352362SJakub Kicinski 	u8 rss_key[NFP_NET_CFG_RSS_KEY_SZ];
5994c352362SJakub Kicinski 	u8 rss_itbl[NFP_NET_CFG_RSS_ITBL_SZ];
6004c352362SJakub Kicinski 
6019f82fca9SJakub Kicinski 	u32 xdp_flags;
6026a8ef542SJakub Kicinski 	struct bpf_prog *xdp_prog;
6039f82fca9SJakub Kicinski 
604a4b562bbSJakub Kicinski 	unsigned int max_tx_rings;
605a4b562bbSJakub Kicinski 	unsigned int max_rx_rings;
6064c352362SJakub Kicinski 
6074c352362SJakub Kicinski 	int stride_tx;
6084c352362SJakub Kicinski 	int stride_rx;
6094c352362SJakub Kicinski 
610b33ae997SJakub Kicinski 	unsigned int max_r_vecs;
61123a4e389SJakub Kicinski 	struct nfp_net_r_vector r_vecs[NFP_NET_MAX_R_VECS];
61223a4e389SJakub Kicinski 	struct msix_entry irq_entries[NFP_NET_MAX_IRQS];
6134c352362SJakub Kicinski 
6144c352362SJakub Kicinski 	irq_handler_t lsc_handler;
6154c352362SJakub Kicinski 	char lsc_name[IFNAMSIZ + 8];
6164c352362SJakub Kicinski 
6174c352362SJakub Kicinski 	irq_handler_t exn_handler;
6184c352362SJakub Kicinski 	char exn_name[IFNAMSIZ + 8];
6194c352362SJakub Kicinski 
6204c352362SJakub Kicinski 	irq_handler_t shared_handler;
6214c352362SJakub Kicinski 	char shared_name[IFNAMSIZ + 8];
6224c352362SJakub Kicinski 
6234c352362SJakub Kicinski 	u32 me_freq_mhz;
6244c352362SJakub Kicinski 
6254c352362SJakub Kicinski 	bool link_up;
6264c352362SJakub Kicinski 	spinlock_t link_status_lock;
6274c352362SJakub Kicinski 
6284c352362SJakub Kicinski 	spinlock_t reconfig_lock;
6293d780b92SJakub Kicinski 	u32 reconfig_posted;
6303d780b92SJakub Kicinski 	bool reconfig_timer_active;
6313d780b92SJakub Kicinski 	bool reconfig_sync_present;
6323d780b92SJakub Kicinski 	struct timer_list reconfig_timer;
6334c352362SJakub Kicinski 
6344c352362SJakub Kicinski 	u32 rx_coalesce_usecs;
6354c352362SJakub Kicinski 	u32 rx_coalesce_max_frames;
6364c352362SJakub Kicinski 	u32 tx_coalesce_usecs;
6374c352362SJakub Kicinski 	u32 tx_coalesce_max_frames;
6384c352362SJakub Kicinski 
6394c352362SJakub Kicinski 	__be16 vxlan_ports[NFP_NET_N_VXLAN_PORTS];
6404c352362SJakub Kicinski 	u8 vxlan_usecnt[NFP_NET_N_VXLAN_PORTS];
6414c352362SJakub Kicinski 
6424c352362SJakub Kicinski 	u8 __iomem *qcp_cfg;
6434c352362SJakub Kicinski 
6444c352362SJakub Kicinski 	u8 __iomem *tx_bar;
6454c352362SJakub Kicinski 	u8 __iomem *rx_bar;
6464c352362SJakub Kicinski 
6474c352362SJakub Kicinski 	struct dentry *debugfs_dir;
64863461a02SJakub Kicinski 
649d4e7f092SJakub Kicinski 	struct list_head vnic_list;
650bd5ca062SJakub Kicinski 
651fa43d2a8SJakub Kicinski 	struct pci_dev *pdev;
6527ac9ebd5SJakub Kicinski 	struct nfp_app *app;
65347465aedSJakub Kicinski 
654eb488c26SJakub Kicinski 	struct nfp_port *port;
655c66a9cf4SJakub Kicinski 
656c66a9cf4SJakub Kicinski 	void *app_priv;
6574c352362SJakub Kicinski };
6584c352362SJakub Kicinski 
6594c352362SJakub Kicinski /* Functions to read/write from/to a BAR
6604c352362SJakub Kicinski  * Performs any endian conversion necessary.
6614c352362SJakub Kicinski  */
6627533fdc0SJakub Kicinski static inline u16 nn_readb(struct nfp_net *nn, int off)
6637533fdc0SJakub Kicinski {
664d2b84397SJakub Kicinski 	return readb(nn->dp.ctrl_bar + off);
6657533fdc0SJakub Kicinski }
6667533fdc0SJakub Kicinski 
6674c352362SJakub Kicinski static inline void nn_writeb(struct nfp_net *nn, int off, u8 val)
6684c352362SJakub Kicinski {
669d2b84397SJakub Kicinski 	writeb(val, nn->dp.ctrl_bar + off);
6704c352362SJakub Kicinski }
6714c352362SJakub Kicinski 
6727533fdc0SJakub Kicinski static inline u16 nn_readw(struct nfp_net *nn, int off)
6737533fdc0SJakub Kicinski {
674d2b84397SJakub Kicinski 	return readw(nn->dp.ctrl_bar + off);
6757533fdc0SJakub Kicinski }
6767533fdc0SJakub Kicinski 
6777533fdc0SJakub Kicinski static inline void nn_writew(struct nfp_net *nn, int off, u16 val)
6787533fdc0SJakub Kicinski {
679d2b84397SJakub Kicinski 	writew(val, nn->dp.ctrl_bar + off);
6807533fdc0SJakub Kicinski }
6814c352362SJakub Kicinski 
6824c352362SJakub Kicinski static inline u32 nn_readl(struct nfp_net *nn, int off)
6834c352362SJakub Kicinski {
684d2b84397SJakub Kicinski 	return readl(nn->dp.ctrl_bar + off);
6854c352362SJakub Kicinski }
6864c352362SJakub Kicinski 
6874c352362SJakub Kicinski static inline void nn_writel(struct nfp_net *nn, int off, u32 val)
6884c352362SJakub Kicinski {
689d2b84397SJakub Kicinski 	writel(val, nn->dp.ctrl_bar + off);
6904c352362SJakub Kicinski }
6914c352362SJakub Kicinski 
6924c352362SJakub Kicinski static inline u64 nn_readq(struct nfp_net *nn, int off)
6934c352362SJakub Kicinski {
694d2b84397SJakub Kicinski 	return readq(nn->dp.ctrl_bar + off);
6954c352362SJakub Kicinski }
6964c352362SJakub Kicinski 
6974c352362SJakub Kicinski static inline void nn_writeq(struct nfp_net *nn, int off, u64 val)
6984c352362SJakub Kicinski {
699d2b84397SJakub Kicinski 	writeq(val, nn->dp.ctrl_bar + off);
7004c352362SJakub Kicinski }
7014c352362SJakub Kicinski 
7024c352362SJakub Kicinski /* Flush posted PCI writes by reading something without side effects */
7034c352362SJakub Kicinski static inline void nn_pci_flush(struct nfp_net *nn)
7044c352362SJakub Kicinski {
7054c352362SJakub Kicinski 	nn_readl(nn, NFP_NET_CFG_VERSION);
7064c352362SJakub Kicinski }
7074c352362SJakub Kicinski 
7084c352362SJakub Kicinski /* Queue Controller Peripheral access functions and definitions.
7094c352362SJakub Kicinski  *
7104c352362SJakub Kicinski  * Some of the BARs of the NFP are mapped to portions of the Queue
7114c352362SJakub Kicinski  * Controller Peripheral (QCP) address space on the NFP.  A QCP queue
7124c352362SJakub Kicinski  * has a read and a write pointer (as well as a size and flags,
7134c352362SJakub Kicinski  * indicating overflow etc).  The QCP offers a number of different
7144c352362SJakub Kicinski  * operation on queue pointers, but here we only offer function to
7154c352362SJakub Kicinski  * either add to a pointer or to read the pointer value.
7164c352362SJakub Kicinski  */
7174c352362SJakub Kicinski #define NFP_QCP_QUEUE_ADDR_SZ			0x800
71873e253f0SJakub Kicinski #define NFP_QCP_QUEUE_AREA_SZ			0x80000
7194c352362SJakub Kicinski #define NFP_QCP_QUEUE_OFF(_x)			((_x) * NFP_QCP_QUEUE_ADDR_SZ)
7204c352362SJakub Kicinski #define NFP_QCP_QUEUE_ADD_RPTR			0x0000
7214c352362SJakub Kicinski #define NFP_QCP_QUEUE_ADD_WPTR			0x0004
7224c352362SJakub Kicinski #define NFP_QCP_QUEUE_STS_LO			0x0008
7234c352362SJakub Kicinski #define NFP_QCP_QUEUE_STS_LO_READPTR_mask	0x3ffff
7244c352362SJakub Kicinski #define NFP_QCP_QUEUE_STS_HI			0x000c
7254c352362SJakub Kicinski #define NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask	0x3ffff
7264c352362SJakub Kicinski 
727416db5c1SJakub Kicinski /* The offset of a QCP queues in the PCIe Target */
7284c352362SJakub Kicinski #define NFP_PCIE_QUEUE(_q) (0x80000 + (NFP_QCP_QUEUE_ADDR_SZ * ((_q) & 0xff)))
7294c352362SJakub Kicinski 
7304c352362SJakub Kicinski /* nfp_qcp_ptr - Read or Write Pointer of a queue */
7314c352362SJakub Kicinski enum nfp_qcp_ptr {
7324c352362SJakub Kicinski 	NFP_QCP_READ_PTR = 0,
7334c352362SJakub Kicinski 	NFP_QCP_WRITE_PTR
7344c352362SJakub Kicinski };
7354c352362SJakub Kicinski 
7364c352362SJakub Kicinski /* There appear to be an *undocumented* upper limit on the value which
7374c352362SJakub Kicinski  * one can add to a queue and that value is either 0x3f or 0x7f.  We
7384c352362SJakub Kicinski  * go with 0x3f as a conservative measure.
7394c352362SJakub Kicinski  */
7404c352362SJakub Kicinski #define NFP_QCP_MAX_ADD				0x3f
7414c352362SJakub Kicinski 
7424c352362SJakub Kicinski static inline void _nfp_qcp_ptr_add(u8 __iomem *q,
7434c352362SJakub Kicinski 				    enum nfp_qcp_ptr ptr, u32 val)
7444c352362SJakub Kicinski {
7454c352362SJakub Kicinski 	u32 off;
7464c352362SJakub Kicinski 
7474c352362SJakub Kicinski 	if (ptr == NFP_QCP_READ_PTR)
7484c352362SJakub Kicinski 		off = NFP_QCP_QUEUE_ADD_RPTR;
7494c352362SJakub Kicinski 	else
7504c352362SJakub Kicinski 		off = NFP_QCP_QUEUE_ADD_WPTR;
7514c352362SJakub Kicinski 
7524c352362SJakub Kicinski 	while (val > NFP_QCP_MAX_ADD) {
7534c352362SJakub Kicinski 		writel(NFP_QCP_MAX_ADD, q + off);
7544c352362SJakub Kicinski 		val -= NFP_QCP_MAX_ADD;
7554c352362SJakub Kicinski 	}
7564c352362SJakub Kicinski 
7574c352362SJakub Kicinski 	writel(val, q + off);
7584c352362SJakub Kicinski }
7594c352362SJakub Kicinski 
7604c352362SJakub Kicinski /**
7614c352362SJakub Kicinski  * nfp_qcp_rd_ptr_add() - Add the value to the read pointer of a queue
7624c352362SJakub Kicinski  *
7634c352362SJakub Kicinski  * @q:   Base address for queue structure
7644c352362SJakub Kicinski  * @val: Value to add to the queue pointer
7654c352362SJakub Kicinski  *
7664c352362SJakub Kicinski  * If @val is greater than @NFP_QCP_MAX_ADD multiple writes are performed.
7674c352362SJakub Kicinski  */
7684c352362SJakub Kicinski static inline void nfp_qcp_rd_ptr_add(u8 __iomem *q, u32 val)
7694c352362SJakub Kicinski {
7704c352362SJakub Kicinski 	_nfp_qcp_ptr_add(q, NFP_QCP_READ_PTR, val);
7714c352362SJakub Kicinski }
7724c352362SJakub Kicinski 
7734c352362SJakub Kicinski /**
7744c352362SJakub Kicinski  * nfp_qcp_wr_ptr_add() - Add the value to the write pointer of a queue
7754c352362SJakub Kicinski  *
7764c352362SJakub Kicinski  * @q:   Base address for queue structure
7774c352362SJakub Kicinski  * @val: Value to add to the queue pointer
7784c352362SJakub Kicinski  *
7794c352362SJakub Kicinski  * If @val is greater than @NFP_QCP_MAX_ADD multiple writes are performed.
7804c352362SJakub Kicinski  */
7814c352362SJakub Kicinski static inline void nfp_qcp_wr_ptr_add(u8 __iomem *q, u32 val)
7824c352362SJakub Kicinski {
7834c352362SJakub Kicinski 	_nfp_qcp_ptr_add(q, NFP_QCP_WRITE_PTR, val);
7844c352362SJakub Kicinski }
7854c352362SJakub Kicinski 
7864c352362SJakub Kicinski static inline u32 _nfp_qcp_read(u8 __iomem *q, enum nfp_qcp_ptr ptr)
7874c352362SJakub Kicinski {
7884c352362SJakub Kicinski 	u32 off;
7894c352362SJakub Kicinski 	u32 val;
7904c352362SJakub Kicinski 
7914c352362SJakub Kicinski 	if (ptr == NFP_QCP_READ_PTR)
7924c352362SJakub Kicinski 		off = NFP_QCP_QUEUE_STS_LO;
7934c352362SJakub Kicinski 	else
7944c352362SJakub Kicinski 		off = NFP_QCP_QUEUE_STS_HI;
7954c352362SJakub Kicinski 
7964c352362SJakub Kicinski 	val = readl(q + off);
7974c352362SJakub Kicinski 
7984c352362SJakub Kicinski 	if (ptr == NFP_QCP_READ_PTR)
7994c352362SJakub Kicinski 		return val & NFP_QCP_QUEUE_STS_LO_READPTR_mask;
8004c352362SJakub Kicinski 	else
8014c352362SJakub Kicinski 		return val & NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask;
8024c352362SJakub Kicinski }
8034c352362SJakub Kicinski 
8044c352362SJakub Kicinski /**
8054c352362SJakub Kicinski  * nfp_qcp_rd_ptr_read() - Read the current read pointer value for a queue
8064c352362SJakub Kicinski  * @q:  Base address for queue structure
8074c352362SJakub Kicinski  *
8084c352362SJakub Kicinski  * Return: Value read.
8094c352362SJakub Kicinski  */
8104c352362SJakub Kicinski static inline u32 nfp_qcp_rd_ptr_read(u8 __iomem *q)
8114c352362SJakub Kicinski {
8124c352362SJakub Kicinski 	return _nfp_qcp_read(q, NFP_QCP_READ_PTR);
8134c352362SJakub Kicinski }
8144c352362SJakub Kicinski 
8154c352362SJakub Kicinski /**
8164c352362SJakub Kicinski  * nfp_qcp_wr_ptr_read() - Read the current write pointer value for a queue
8174c352362SJakub Kicinski  * @q:  Base address for queue structure
8184c352362SJakub Kicinski  *
8194c352362SJakub Kicinski  * Return: Value read.
8204c352362SJakub Kicinski  */
8214c352362SJakub Kicinski static inline u32 nfp_qcp_wr_ptr_read(u8 __iomem *q)
8224c352362SJakub Kicinski {
8234c352362SJakub Kicinski 	return _nfp_qcp_read(q, NFP_QCP_WRITE_PTR);
8244c352362SJakub Kicinski }
8254c352362SJakub Kicinski 
826042f4ba6SJakub Kicinski static inline bool nfp_net_is_data_vnic(struct nfp_net *nn)
827042f4ba6SJakub Kicinski {
828042f4ba6SJakub Kicinski 	WARN_ON_ONCE(!nn->dp.netdev && nn->port);
829042f4ba6SJakub Kicinski 	return !!nn->dp.netdev;
830042f4ba6SJakub Kicinski }
831042f4ba6SJakub Kicinski 
832042f4ba6SJakub Kicinski static inline bool nfp_net_running(struct nfp_net *nn)
833042f4ba6SJakub Kicinski {
834042f4ba6SJakub Kicinski 	return nn->dp.ctrl & NFP_NET_CFG_CTRL_ENABLE;
835042f4ba6SJakub Kicinski }
836042f4ba6SJakub Kicinski 
83777ece8d5SJakub Kicinski static inline const char *nfp_net_name(struct nfp_net *nn)
83877ece8d5SJakub Kicinski {
83977ece8d5SJakub Kicinski 	return nn->dp.netdev ? nn->dp.netdev->name : "ctrl";
84077ece8d5SJakub Kicinski }
84177ece8d5SJakub Kicinski 
8424c352362SJakub Kicinski /* Globals */
8432633beb9SJakub Kicinski extern const char nfp_driver_version[];
8444c352362SJakub Kicinski 
845eb488c26SJakub Kicinski extern const struct net_device_ops nfp_net_netdev_ops;
846eb488c26SJakub Kicinski 
847eb488c26SJakub Kicinski static inline bool nfp_netdev_is_nfp_net(struct net_device *netdev)
848eb488c26SJakub Kicinski {
849eb488c26SJakub Kicinski 	return netdev->netdev_ops == &nfp_net_netdev_ops;
850eb488c26SJakub Kicinski }
851eb488c26SJakub Kicinski 
8524c352362SJakub Kicinski /* Prototypes */
8534c352362SJakub Kicinski void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
8544c352362SJakub Kicinski 			    void __iomem *ctrl_bar);
8554c352362SJakub Kicinski 
856a4b562bbSJakub Kicinski struct nfp_net *
857a7b1ad08SJakub Kicinski nfp_net_alloc(struct pci_dev *pdev, bool needs_netdev,
858a4b562bbSJakub Kicinski 	      unsigned int max_tx_rings, unsigned int max_rx_rings);
859beba69caSJakub Kicinski void nfp_net_free(struct nfp_net *nn);
860beba69caSJakub Kicinski 
861beba69caSJakub Kicinski int nfp_net_init(struct nfp_net *nn);
862beba69caSJakub Kicinski void nfp_net_clean(struct nfp_net *nn);
863beba69caSJakub Kicinski 
86477ece8d5SJakub Kicinski int nfp_ctrl_open(struct nfp_net *nn);
86577ece8d5SJakub Kicinski void nfp_ctrl_close(struct nfp_net *nn);
86677ece8d5SJakub Kicinski 
8674c352362SJakub Kicinski void nfp_net_set_ethtool_ops(struct net_device *netdev);
8684c352362SJakub Kicinski void nfp_net_info(struct nfp_net *nn);
8694c352362SJakub Kicinski int nfp_net_reconfig(struct nfp_net *nn, u32 update);
8709ff304bfSJakub Kicinski unsigned int nfp_net_rss_key_sz(struct nfp_net *nn);
8714c352362SJakub Kicinski void nfp_net_rss_write_itbl(struct nfp_net *nn);
8724c352362SJakub Kicinski void nfp_net_rss_write_key(struct nfp_net *nn);
8734c352362SJakub Kicinski void nfp_net_coalesce_write_cfg(struct nfp_net *nn);
874fdace6c2SJakub Kicinski 
875fdace6c2SJakub Kicinski unsigned int
876fdace6c2SJakub Kicinski nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
877fdace6c2SJakub Kicinski 		   unsigned int min_irqs, unsigned int want_irqs);
878fdace6c2SJakub Kicinski void nfp_net_irqs_disable(struct pci_dev *pdev);
879fdace6c2SJakub Kicinski void
880fdace6c2SJakub Kicinski nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
881fdace6c2SJakub Kicinski 		    unsigned int n);
882783496b0SJakub Kicinski 
883783496b0SJakub Kicinski struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn);
884d957c0f7SJakub Kicinski int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *new,
885d957c0f7SJakub Kicinski 			  struct netlink_ext_ack *extack);
8864c352362SJakub Kicinski 
8872633beb9SJakub Kicinski #ifdef CONFIG_NFP_DEBUG
8884c352362SJakub Kicinski void nfp_net_debugfs_create(void);
8894c352362SJakub Kicinski void nfp_net_debugfs_destroy(void);
8906f1cd5caSJakub Kicinski struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev);
891d4e7f092SJakub Kicinski void nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir, int id);
8926f1cd5caSJakub Kicinski void nfp_net_debugfs_dir_clean(struct dentry **dir);
8934c352362SJakub Kicinski #else
8944c352362SJakub Kicinski static inline void nfp_net_debugfs_create(void)
8954c352362SJakub Kicinski {
8964c352362SJakub Kicinski }
8974c352362SJakub Kicinski 
8984c352362SJakub Kicinski static inline void nfp_net_debugfs_destroy(void)
8994c352362SJakub Kicinski {
9004c352362SJakub Kicinski }
9014c352362SJakub Kicinski 
9026f1cd5caSJakub Kicinski static inline struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev)
9036f1cd5caSJakub Kicinski {
9046f1cd5caSJakub Kicinski 	return NULL;
9056f1cd5caSJakub Kicinski }
9066f1cd5caSJakub Kicinski 
9076f1cd5caSJakub Kicinski static inline void
908d4e7f092SJakub Kicinski nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir, int id)
9094c352362SJakub Kicinski {
9104c352362SJakub Kicinski }
9114c352362SJakub Kicinski 
9126f1cd5caSJakub Kicinski static inline void nfp_net_debugfs_dir_clean(struct dentry **dir)
9134c352362SJakub Kicinski {
9144c352362SJakub Kicinski }
9152633beb9SJakub Kicinski #endif /* CONFIG_NFP_DEBUG */
9164c352362SJakub Kicinski 
9174c352362SJakub Kicinski #endif /* _NFP_NET_H_ */
918