nfp_net.h 30.4 KB
Newer Older
1 2
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
/* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3 4 5 6 7 8 9 10 11 12 13 14

/*
 * nfp_net.h
 * Declarations for Netronome network device driver.
 * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
 *          Jason McMullan <jason.mcmullan@netronome.com>
 *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
 */

#ifndef _NFP_NET_H_
#define _NFP_NET_H_

J
Jakub Kicinski 已提交
15
#include <linux/atomic.h>
16
#include <linux/interrupt.h>
J
Jakub Kicinski 已提交
17
#include <linux/list.h>
18 19
#include <linux/netdevice.h>
#include <linux/pci.h>
20
#include <linux/dim.h>
21
#include <linux/io-64-nonatomic-hi-lo.h>
J
Jakub Kicinski 已提交
22
#include <linux/semaphore.h>
23
#include <linux/workqueue.h>
J
Jesper Dangaard Brouer 已提交
24
#include <net/xdp.h>
25 26 27

#include "nfp_net_ctrl.h"

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
#define nn_pr(nn, lvl, fmt, args...)					\
	({								\
		struct nfp_net *__nn = (nn);				\
									\
		if (__nn->dp.netdev)					\
			netdev_printk(lvl, __nn->dp.netdev, fmt, ## args); \
		else							\
			dev_printk(lvl, __nn->dp.dev, "ctrl: " fmt, ## args); \
	})

#define nn_err(nn, fmt, args...)	nn_pr(nn, KERN_ERR, fmt, ## args)
#define nn_warn(nn, fmt, args...)	nn_pr(nn, KERN_WARNING, fmt, ## args)
#define nn_info(nn, fmt, args...)	nn_pr(nn, KERN_INFO, fmt, ## args)
#define nn_dbg(nn, fmt, args...)	nn_pr(nn, KERN_DEBUG, fmt, ## args)

43
#define nn_dp_warn(dp, fmt, args...)					\
44 45 46 47 48 49 50 51 52 53
	({								\
		struct nfp_net_dp *__dp = (dp);				\
									\
		if (unlikely(net_ratelimit())) {			\
			if (__dp->netdev)				\
				netdev_warn(__dp->netdev, fmt, ## args); \
			else						\
				dev_warn(__dp->dev, fmt, ## args);	\
		}							\
	})
54

55 56
/* Max time to wait for NFP to respond on updates (in seconds) */
#define NFP_NET_POLL_TIMEOUT	5
57

58 59 60
/* Interval for reading offloaded filter stats */
#define NFP_NET_STAT_POLL_IVL	msecs_to_jiffies(100)

61
/* Bar allocation */
62
#define NFP_NET_CTRL_BAR	0
63 64 65 66 67 68 69
#define NFP_NET_Q0_BAR		2
#define NFP_NET_Q1_BAR		4	/* OBSOLETE */

/* Max bits in DMA address */
#define NFP_NET_MAX_DMA_BITS	40

/* Default size for MTU and freelist buffer sizes */
J
Jakub Kicinski 已提交
70
#define NFP_NET_DEFAULT_MTU		1500U
71 72 73 74 75 76 77 78

/* Maximum number of bytes prepended to a packet */
#define NFP_NET_MAX_PREPEND		64

/* Interrupt definitions */
#define NFP_NET_NON_Q_VECTORS		2
#define NFP_NET_IRQ_LSC_IDX		0
#define NFP_NET_IRQ_EXN_IDX		1
J
Jakub Kicinski 已提交
79
#define NFP_NET_MIN_VNIC_IRQS		(NFP_NET_NON_Q_VECTORS + 1)
80 81 82 83

/* Queue/Ring definitions */
#define NFP_NET_MAX_TX_RINGS	64	/* Max. # of Tx rings per device */
#define NFP_NET_MAX_RX_RINGS	64	/* Max. # of Rx rings per device */
84 85 86
#define NFP_NET_MAX_R_VECS	(NFP_NET_MAX_TX_RINGS > NFP_NET_MAX_RX_RINGS ? \
				 NFP_NET_MAX_TX_RINGS : NFP_NET_MAX_RX_RINGS)
#define NFP_NET_MAX_IRQS	(NFP_NET_NON_Q_VECTORS + NFP_NET_MAX_R_VECS)
87 88 89 90 91 92 93 94 95 96

#define NFP_NET_MIN_TX_DESCS	256	/* Min. # of Tx descs per ring */
#define NFP_NET_MIN_RX_DESCS	256	/* Min. # of Rx descs per ring */
#define NFP_NET_MAX_TX_DESCS	(256 * 1024) /* Max. # of Tx descs per ring */
#define NFP_NET_MAX_RX_DESCS	(256 * 1024) /* Max. # of Rx descs per ring */

#define NFP_NET_TX_DESCS_DEFAULT 4096	/* Default # of Tx descs per ring */
#define NFP_NET_RX_DESCS_DEFAULT 4096	/* Default # of Rx descs per ring */

#define NFP_NET_FL_BATCH	16	/* Add freelist in this Batch size */
97
#define NFP_NET_XDP_MAX_COMPLETE 2048	/* XDP bufs to reclaim in NAPI poll */
98 99 100 101

/* Offload definitions */
#define NFP_NET_N_VXLAN_PORTS	(NFP_NET_CFG_VXLAN_SZ / sizeof(__be16))

102 103 104 105
#define NFP_NET_RX_BUF_HEADROOM	(NET_SKB_PAD + NET_IP_ALIGN)
#define NFP_NET_RX_BUF_NON_DATA	(NFP_NET_RX_BUF_HEADROOM +		\
				 SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))

106
/* Forward declarations */
107
struct nfp_cpp;
108
struct nfp_eth_table_port;
109 110
struct nfp_net;
struct nfp_net_r_vector;
J
Jakub Kicinski 已提交
111
struct nfp_port;
112
struct xsk_buff_pool;
113

114 115 116
/* Convenience macro for wrapping descriptor index on ring size */
#define D_IDX(ring, idx)	((idx) & ((ring)->cnt - 1))

117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
/* Convenience macro for writing dma address into RX/TX descriptors */
#define nfp_desc_set_dma_addr(desc, dma_addr)				\
	do {								\
		__typeof(desc) __d = (desc);				\
		dma_addr_t __addr = (dma_addr);				\
									\
		__d->dma_addr_lo = cpu_to_le32(lower_32_bits(__addr));	\
		__d->dma_addr_hi = upper_32_bits(__addr) & 0xff;	\
	} while (0)

/* TX descriptor format */

#define PCIE_DESC_TX_EOP		BIT(7)
#define PCIE_DESC_TX_OFFSET_MASK	GENMASK(6, 0)
#define PCIE_DESC_TX_MSS_MASK		GENMASK(13, 0)

/* Flags in the host TX descriptor */
#define PCIE_DESC_TX_CSUM		BIT(7)
#define PCIE_DESC_TX_IP4_CSUM		BIT(6)
#define PCIE_DESC_TX_TCP_CSUM		BIT(5)
#define PCIE_DESC_TX_UDP_CSUM		BIT(4)
#define PCIE_DESC_TX_VLAN		BIT(3)
#define PCIE_DESC_TX_LSO		BIT(2)
#define PCIE_DESC_TX_ENCAP		BIT(1)
#define PCIE_DESC_TX_O_IP4_CSUM	BIT(0)

struct nfp_net_tx_desc {
	union {
		struct {
			u8 dma_addr_hi; /* High bits of host buf address */
			__le16 dma_len;	/* Length to DMA for this desc */
			u8 offset_eop;	/* Offset in buf where pkt starts +
					 * highest bit is eop flag.
					 */
			__le32 dma_addr_lo; /* Low 32bit of host buf addr */

			__le16 mss;	/* MSS to be used for LSO */
154
			u8 lso_hdrlen;	/* LSO, TCP payload offset */
155
			u8 flags;	/* TX Flags, see @PCIE_DESC_TX_* */
E
Edwin Peer 已提交
156 157 158 159 160 161 162
			union {
				struct {
					u8 l3_offset; /* L3 header offset */
					u8 l4_offset; /* L4 header offset */
				};
				__le16 vlan; /* VLAN tag to add if indicated */
			};
163 164 165
			__le16 data_len; /* Length of frame + meta data */
		} __packed;
		__le32 vals[4];
166
		__le64 vals8[2];
167 168 169 170 171
	};
};

/**
 * struct nfp_net_tx_buf - software TX buffer descriptor
172 173
 * @skb:	normal ring, sk_buff associated with this buffer
 * @frag:	XDP ring, page frag associated with this buffer
174
 * @xdp:	XSK buffer pool handle (for AF_XDP)
175 176 177 178 179
 * @dma_addr:	DMA mapping address of the buffer
 * @fidx:	Fragment index (-1 for the head and [0..nr_frags-1] for frags)
 * @pkt_cnt:	Number of packets to be produced out of the skb associated
 *		with this buffer (valid only on the head's buffer).
 *		Will be 1 for all non-TSO packets.
180 181
 * @is_xsk_tx:	Flag if buffer is a RX buffer after a XDP_TX action and not a
 *		buffer from the TX queue (for AF_XDP).
182 183 184 185
 * @real_len:	Number of bytes which to be produced out of the skb (valid only
 *		on the head's buffer). Equal to skb->len for non-TSO packets.
 */
struct nfp_net_tx_buf {
186 187 188
	union {
		struct sk_buff *skb;
		void *frag;
189
		struct xdp_buff *xdp;
190
	};
191
	dma_addr_t dma_addr;
192 193 194 195 196 197 198 199 200
	union {
		struct {
			short int fidx;
			u16 pkt_cnt;
		};
		struct {
			bool is_xsk_tx;
		};
	};
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
	u32 real_len;
};

/**
 * struct nfp_net_tx_ring - TX ring structure
 * @r_vec:      Back pointer to ring vector structure
 * @idx:        Ring index from Linux's perspective
 * @qcidx:      Queue Controller Peripheral (QCP) queue index for the TX queue
 * @qcp_q:      Pointer to base of the QCP TX queue
 * @cnt:        Size of the queue in number of descriptors
 * @wr_p:       TX ring write pointer (free running)
 * @rd_p:       TX ring read pointer (free running)
 * @qcp_rd_p:   Local copy of QCP TX queue read pointer
 * @wr_ptr_add:	Accumulated number of buffers to add to QCP write pointer
 *		(used for .xmit_more delayed kick)
 * @txbufs:     Array of transmitted TX buffers, to free on transmit
 * @txds:       Virtual address of TX ring in host memory
 * @dma:        DMA address of the TX ring
 * @size:       Size, in bytes, of the TX ring (needed to free)
220
 * @is_xdp:	Is this a XDP TX ring?
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
 */
struct nfp_net_tx_ring {
	struct nfp_net_r_vector *r_vec;

	u32 idx;
	int qcidx;
	u8 __iomem *qcp_q;

	u32 cnt;
	u32 wr_p;
	u32 rd_p;
	u32 qcp_rd_p;

	u32 wr_ptr_add;

	struct nfp_net_tx_buf *txbufs;
	struct nfp_net_tx_desc *txds;

	dma_addr_t dma;
240
	size_t size;
241
	bool is_xdp;
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
} ____cacheline_aligned;

/* RX and freelist descriptor format */

#define PCIE_DESC_RX_DD			BIT(7)
#define PCIE_DESC_RX_META_LEN_MASK	GENMASK(6, 0)

/* Flags in the RX descriptor */
#define PCIE_DESC_RX_RSS		cpu_to_le16(BIT(15))
#define PCIE_DESC_RX_I_IP4_CSUM		cpu_to_le16(BIT(14))
#define PCIE_DESC_RX_I_IP4_CSUM_OK	cpu_to_le16(BIT(13))
#define PCIE_DESC_RX_I_TCP_CSUM		cpu_to_le16(BIT(12))
#define PCIE_DESC_RX_I_TCP_CSUM_OK	cpu_to_le16(BIT(11))
#define PCIE_DESC_RX_I_UDP_CSUM		cpu_to_le16(BIT(10))
#define PCIE_DESC_RX_I_UDP_CSUM_OK	cpu_to_le16(BIT(9))
257
#define PCIE_DESC_RX_DECRYPTED		cpu_to_le16(BIT(8))
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
#define PCIE_DESC_RX_EOP		cpu_to_le16(BIT(7))
#define PCIE_DESC_RX_IP4_CSUM		cpu_to_le16(BIT(6))
#define PCIE_DESC_RX_IP4_CSUM_OK	cpu_to_le16(BIT(5))
#define PCIE_DESC_RX_TCP_CSUM		cpu_to_le16(BIT(4))
#define PCIE_DESC_RX_TCP_CSUM_OK	cpu_to_le16(BIT(3))
#define PCIE_DESC_RX_UDP_CSUM		cpu_to_le16(BIT(2))
#define PCIE_DESC_RX_UDP_CSUM_OK	cpu_to_le16(BIT(1))
#define PCIE_DESC_RX_VLAN		cpu_to_le16(BIT(0))

#define PCIE_DESC_RX_CSUM_ALL		(PCIE_DESC_RX_IP4_CSUM |	\
					 PCIE_DESC_RX_TCP_CSUM |	\
					 PCIE_DESC_RX_UDP_CSUM |	\
					 PCIE_DESC_RX_I_IP4_CSUM |	\
					 PCIE_DESC_RX_I_TCP_CSUM |	\
					 PCIE_DESC_RX_I_UDP_CSUM)
#define PCIE_DESC_RX_CSUM_OK_SHIFT	1
#define __PCIE_DESC_RX_CSUM_ALL		le16_to_cpu(PCIE_DESC_RX_CSUM_ALL)
#define __PCIE_DESC_RX_CSUM_ALL_OK	(__PCIE_DESC_RX_CSUM_ALL >>	\
					 PCIE_DESC_RX_CSUM_OK_SHIFT)

struct nfp_net_rx_desc {
	union {
		struct {
			u8 dma_addr_hi;	/* High bits of the buf address */
			__le16 reserved; /* Must be zero */
			u8 meta_len_dd; /* Must be zero */

			__le32 dma_addr_lo; /* Low bits of the buffer address */
		} __packed fld;

		struct {
			__le16 data_len; /* Length of the frame + meta data */
			u8 reserved;
			u8 meta_len_dd;	/* Length of meta data prepended +
					 * descriptor done flag.
					 */

			__le16 flags;	/* RX flags. See @PCIE_DESC_RX_* */
			__le16 vlan;	/* VLAN if stripped */
		} __packed rxd;

		__le32 vals[2];
	};
};

303 304
#define NFP_NET_META_FIELD_MASK GENMASK(NFP_NET_META_FIELD_SIZE - 1, 0)

305
struct nfp_meta_parsed {
306 307
	u8 hash_type;
	u8 csum_type;
308 309
	u32 hash;
	u32 mark;
310
	u32 portid;
311
	__wsum csum;
312 313
};

314 315 316 317 318 319 320
struct nfp_net_rx_hash {
	__be32 hash_type;
	__be32 hash;
};

/**
 * struct nfp_net_rx_buf - software RX buffer descriptor
321
 * @frag:	page fragment buffer
322 323 324
 * @dma_addr:	DMA mapping address of the buffer
 */
struct nfp_net_rx_buf {
325
	void *frag;
326 327 328
	dma_addr_t dma_addr;
};

329 330 331 332 333 334 335 336 337 338
/**
 * struct nfp_net_xsk_rx_buf - software RX XSK buffer descriptor
 * @dma_addr:	DMA mapping address of the buffer
 * @xdp:	XSK buffer pool handle (for AF_XDP)
 */
struct nfp_net_xsk_rx_buf {
	dma_addr_t dma_addr;
	struct xdp_buff *xdp;
};

339 340 341 342 343 344 345 346 347 348
/**
 * struct nfp_net_rx_ring - RX ring structure
 * @r_vec:      Back pointer to ring vector structure
 * @cnt:        Size of the queue in number of descriptors
 * @wr_p:       FL/RX ring write pointer (free running)
 * @rd_p:       FL/RX ring read pointer (free running)
 * @idx:        Ring index from Linux's perspective
 * @fl_qcidx:   Queue Controller Peripheral (QCP) queue index for the freelist
 * @qcp_fl:     Pointer to base of the QCP freelist queue
 * @rxbufs:     Array of transmitted FL/RX buffers
349
 * @xsk_rxbufs: Array of transmitted FL/RX buffers (for AF_XDP)
350
 * @rxds:       Virtual address of FL/RX ring in host memory
351
 * @xdp_rxq:    RX-ring info avail for XDP
352 353 354 355 356 357 358 359 360 361
 * @dma:        DMA address of the FL/RX ring
 * @size:       Size, in bytes, of the FL/RX ring (needed to free)
 */
struct nfp_net_rx_ring {
	struct nfp_net_r_vector *r_vec;

	u32 cnt;
	u32 wr_p;
	u32 rd_p;

J
Jakub Kicinski 已提交
362
	u32 idx;
363 364 365 366 367

	int fl_qcidx;
	u8 __iomem *qcp_fl;

	struct nfp_net_rx_buf *rxbufs;
368
	struct nfp_net_xsk_rx_buf *xsk_rxbufs;
369 370
	struct nfp_net_rx_desc *rxds;

J
Jesper Dangaard Brouer 已提交
371
	struct xdp_rxq_info xdp_rxq;
372 373

	dma_addr_t dma;
374
	size_t size;
375 376 377 378 379 380
} ____cacheline_aligned;

/**
 * struct nfp_net_r_vector - Per ring interrupt vector configuration
 * @nfp_net:        Backpointer to nfp_net structure
 * @napi:           NAPI structure for this ring vec
381 382 383
 * @tasklet:        ctrl vNIC, tasklet for servicing the r_vec
 * @queue:          ctrl vNIC, send queue
 * @lock:           ctrl vNIC, r_vec lock protects @queue
384 385
 * @tx_ring:        Pointer to TX ring
 * @rx_ring:        Pointer to RX ring
386
 * @xdp_ring:	    Pointer to an extra TX ring for XDP
387
 * @xsk_pool:	    XSK buffer pool active on vector queue pair (for AF_XDP)
388
 * @irq_entry:      MSI-X table entry (use for talking to the device)
389 390 391
 * @event_ctr:	    Number of interrupt
 * @rx_dim:	    Dynamic interrupt moderation structure for RX
 * @tx_dim:	    Dynamic interrupt moderation structure for TX
392 393 394 395 396 397
 * @rx_sync:	    Seqlock for atomic updates of RX stats
 * @rx_pkts:        Number of received packets
 * @rx_bytes:	    Number of received bytes
 * @rx_drops:	    Number of packets dropped on RX due to lack of resources
 * @hw_csum_rx_ok:  Counter of packets where the HW checksum was OK
 * @hw_csum_rx_inner_ok: Counter of packets where the inner HW checksum was OK
398
 * @hw_csum_rx_complete: Counter of packets with CHECKSUM_COMPLETE reported
399
 * @hw_csum_rx_error:	 Counter of packets with bad checksums
400
 * @hw_tls_rx:	    Number of packets with TLS decrypted by hardware
401 402 403 404 405 406 407
 * @tx_sync:	    Seqlock for atomic updates of TX stats
 * @tx_pkts:	    Number of Transmitted packets
 * @tx_bytes:	    Number of Transmitted bytes
 * @hw_csum_tx:	    Counter of packets with TX checksum offload requested
 * @hw_csum_tx_inner:	 Counter of inner TX checksum offload requests
 * @tx_gather:	    Counter of packets with Gather DMA
 * @tx_lso:	    Counter of LSO packets sent
J
Jakub Kicinski 已提交
408 409 410 411 412
 * @hw_tls_tx:	    Counter of TLS packets sent with crypto offloaded to HW
 * @tls_tx_fallback:	Counter of TLS packets sent which had to be encrypted
 *			by the fallback path because packets came out of order
 * @tls_tx_no_fallback:	Counter of TLS packets not sent because the fallback
 *			path could not encrypt them
413 414
 * @tx_errors:	    How many TX errors were encountered
 * @tx_busy:        How often was TX busy (no space)?
415
 * @rx_replace_buf_alloc_fail:	Counter of RX buffer allocation failures
416
 * @irq_vector:     Interrupt vector number (use for talking to the OS)
417 418 419 420 421 422 423 424 425 426 427
 * @handler:        Interrupt handler for this ring vector
 * @name:           Name of the interrupt vector
 * @affinity_mask:  SMP affinity mask for this vector
 *
 * This structure ties RX and TX rings to interrupt vectors and a NAPI
 * context. This currently only supports one RX and TX ring per
 * interrupt vector but might be extended in the future to allow
 * association of multiple rings per vector.
 */
struct nfp_net_r_vector {
	struct nfp_net *nfp_net;
J
Jakub Kicinski 已提交
428 429 430 431 432
	union {
		struct napi_struct napi;
		struct {
			struct tasklet_struct tasklet;
			struct sk_buff_head queue;
433
			spinlock_t lock;
J
Jakub Kicinski 已提交
434 435
		};
	};
436 437 438 439

	struct nfp_net_tx_ring *tx_ring;
	struct nfp_net_rx_ring *rx_ring;

440
	u16 irq_entry;
441

442 443 444 445
	u16 event_ctr;
	struct dim rx_dim;
	struct dim tx_dim;

446 447 448 449 450 451
	struct u64_stats_sync rx_sync;
	u64 rx_pkts;
	u64 rx_bytes;
	u64 rx_drops;
	u64 hw_csum_rx_ok;
	u64 hw_csum_rx_inner_ok;
452
	u64 hw_csum_rx_complete;
453
	u64 hw_tls_rx;
454

J
Jakub Kicinski 已提交
455 456 457
	u64 hw_csum_rx_error;
	u64 rx_replace_buf_alloc_fail;

458
	struct nfp_net_tx_ring *xdp_ring;
459
	struct xsk_buff_pool *xsk_pool;
460

461 462 463
	struct u64_stats_sync tx_sync;
	u64 tx_pkts;
	u64 tx_bytes;
J
Jakub Kicinski 已提交
464 465

	u64 ____cacheline_aligned_in_smp hw_csum_tx;
466 467 468
	u64 hw_csum_tx_inner;
	u64 tx_gather;
	u64 tx_lso;
J
Jakub Kicinski 已提交
469
	u64 hw_tls_tx;
470

J
Jakub Kicinski 已提交
471 472
	u64 tls_tx_fallback;
	u64 tls_tx_no_fallback;
473 474 475
	u64 tx_errors;
	u64 tx_busy;

J
Jakub Kicinski 已提交
476 477
	/* Cold data follows */

478
	u32 irq_vector;
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	irq_handler_t handler;
	char name[IFNAMSIZ + 8];
	cpumask_t affinity_mask;
} ____cacheline_aligned;

/* Firmware version as it is written in the 32bit value in the BAR */
struct nfp_net_fw_version {
	u8 minor;
	u8 major;
	u8 class;
	u8 resv;
} __packed;

static inline bool nfp_net_fw_ver_eq(struct nfp_net_fw_version *fw_ver,
				     u8 resv, u8 class, u8 major, u8 minor)
{
	return fw_ver->resv == resv &&
	       fw_ver->class == class &&
	       fw_ver->major == major &&
	       fw_ver->minor == minor;
}

501 502 503 504 505
struct nfp_stat_pair {
	u64 pkts;
	u64 bytes;
};

506
/**
507
 * struct nfp_net_dp - NFP network device datapath data structure
508
 * @dev:		Backpointer to struct device
509 510
 * @netdev:		Backpointer to net_device structure
 * @is_vf:		Is the driver attached to a VF?
J
Jakub Kicinski 已提交
511
 * @chained_metadata_format:  Firemware will use new metadata format
512
 * @ktls_tx:		Is kTLS TX enabled?
513
 * @rx_dma_dir:		Mapping direction for RX buffers
514
 * @rx_dma_off:		Offset at which DMA packets (for XDP headroom)
515
 * @rx_offset:		Offset in the RX buffers where packet data starts
516 517
 * @ctrl:		Local copy of the control register/word.
 * @fl_bufsz:		Currently configured size of the freelist buffers
518
 * @xdp_prog:		Installed XDP program
519 520
 * @tx_rings:		Array of pre-allocated TX ring structures
 * @rx_rings:		Array of pre-allocated RX ring structures
521
 * @ctrl_bar:		Pointer to mapped control BAR
522 523 524 525 526 527 528
 *
 * @txd_cnt:		Size of the TX ring in number of descriptors
 * @rxd_cnt:		Size of the RX ring in number of descriptors
 * @num_r_vecs:		Number of used ring vectors
 * @num_tx_rings:	Currently configured number of TX rings
 * @num_stack_tx_rings:	Number of TX rings used by the stack (not XDP)
 * @num_rx_rings:	Currently configured number of RX rings
529
 * @mtu:		Device MTU
530
 * @xsk_pools:		XSK buffer pools, @max_r_vecs in size (for AF_XDP).
531 532 533 534 535
 */
struct nfp_net_dp {
	struct device *dev;
	struct net_device *netdev;

536 537
	u8 is_vf:1;
	u8 chained_metadata_format:1;
538
	u8 ktls_tx:1;
539 540

	u8 rx_dma_dir;
541 542
	u8 rx_offset;

543 544
	u32 rx_dma_off;

545 546 547 548 549 550 551 552
	u32 ctrl;
	u32 fl_bufsz;

	struct bpf_prog *xdp_prog;

	struct nfp_net_tx_ring *tx_rings;
	struct nfp_net_rx_ring *rx_rings;

553 554
	u8 __iomem *ctrl_bar;

555 556 557 558 559 560 561 562 563 564
	/* Cold data follows */

	unsigned int txd_cnt;
	unsigned int rxd_cnt;

	unsigned int num_r_vecs;

	unsigned int num_tx_rings;
	unsigned int num_stack_tx_rings;
	unsigned int num_rx_rings;
565 566

	unsigned int mtu;
567 568

	struct xsk_buff_pool **xsk_pools;
569 570 571 572 573
};

/**
 * struct nfp_net - NFP network device structure
 * @dp:			Datapath structure
574
 * @id:			vNIC id within the PF (0 for VFs)
575
 * @fw_ver:		Firmware version
576 577
 * @cap:                Capabilities advertised by the Firmware
 * @max_mtu:            Maximum support MTU advertised by the Firmware
578
 * @rss_hfunc:		RSS selected hash function
579 580 581
 * @rss_cfg:            RSS configuration
 * @rss_key:            RSS secret key
 * @rss_itbl:           RSS indirection table
582 583
 * @xdp:		Information about the driver XDP program
 * @xdp_hw:		Information about the HW XDP program
584
 * @max_r_vecs:		Number of allocated interrupt vectors for RX/TX
585 586
 * @max_tx_rings:       Maximum number of TX rings supported by the Firmware
 * @max_rx_rings:       Maximum number of RX rings supported by the Firmware
J
Jakub Kicinski 已提交
587 588
 * @stride_rx:		Queue controller RX queue spacing
 * @stride_tx:		Queue controller TX queue spacing
589 590 591 592 593 594 595 596
 * @r_vecs:             Pre-allocated array of ring vectors
 * @irq_entries:        Pre-allocated array of MSI-X entries
 * @lsc_handler:        Handler for Link State Change interrupt
 * @lsc_name:           Name for Link State Change interrupt
 * @exn_handler:        Handler for Exception interrupt
 * @exn_name:           Name for Exception interrupt
 * @shared_handler:     Handler for shared interrupts
 * @shared_name:        Name for shared interrupt
597 598 599 600
 * @reconfig_lock:	Protects @reconfig_posted, @reconfig_timer_active,
 *			@reconfig_sync_present and HW reconfiguration request
 *			regs/machinery from async requests (sync must take
 *			@bar_lock)
601 602 603 604
 * @reconfig_posted:	Pending reconfig bits coming from async sources
 * @reconfig_timer_active:  Timer for reading reconfiguration results is pending
 * @reconfig_sync_present:  Some thread is performing synchronous reconfig
 * @reconfig_timer:	Timer for async reading of reconfig results
605
 * @reconfig_in_progress_update:	Update FW is processing now (debug only)
606
 * @bar_lock:		vNIC config BAR access lock, protects: update,
607
 *			mailbox area, crypto TLV
608
 * @link_up:            Is the link up?
J
Jakub Kicinski 已提交
609
 * @link_status_lock:	Protects @link_* and ensures atomicity with BAR reading
610 611
 * @rx_coalesce_adapt_on:   Is RX interrupt moderation adaptive?
 * @tx_coalesce_adapt_on:   Is TX interrupt moderation adaptive?
612 613 614 615 616 617 618
 * @rx_coalesce_usecs:      RX interrupt moderation usecs delay parameter
 * @rx_coalesce_max_frames: RX interrupt moderation frame count parameter
 * @tx_coalesce_usecs:      TX interrupt moderation usecs delay parameter
 * @tx_coalesce_max_frames: TX interrupt moderation frame count parameter
 * @qcp_cfg:            Pointer to QCP queue used for configuration notification
 * @tx_bar:             Pointer to mapped TX queues
 * @rx_bar:             Pointer to mapped FL/RX queues
619
 * @tlv_caps:		Parsed TLV capabilities
620
 * @ktls_tx_conn_cnt:	Number of offloaded kTLS TX connections
J
Jakub Kicinski 已提交
621
 * @ktls_rx_conn_cnt:	Number of offloaded kTLS RX connections
622
 * @ktls_conn_id_gen:	Trivial generator for kTLS connection ids (for TX)
J
Jakub Kicinski 已提交
623 624
 * @ktls_no_space:	Counter of firmware rejecting kTLS connection due to
 *			lack of space
625 626 627
 * @ktls_rx_resync_req:	Counter of TLS RX resync requested
 * @ktls_rx_resync_ign:	Counter of TLS RX resync requests ignored
 * @ktls_rx_resync_sent:    Counter of TLS RX resync completed
628 629 630
 * @mbox_cmsg:		Common Control Message via vNIC mailbox state
 * @mbox_cmsg.queue:	CCM mbox queue of pending messages
 * @mbox_cmsg.wq:	CCM mbox wait queue of waiting processes
631 632 633
 * @mbox_cmsg.workq:	CCM mbox work queue for @wait_work and @runq_work
 * @mbox_cmsg.wait_work:    CCM mbox posted msg reconfig wait work
 * @mbox_cmsg.runq_work:    CCM mbox posted msg queue runner work
634
 * @mbox_cmsg.tag:	CCM mbox message tag allocator
635
 * @debugfs_dir:	Device directory in debugfs
J
Jakub Kicinski 已提交
636
 * @vnic_list:		Entry on device vNIC list
637
 * @pdev:		Backpointer to PCI device
638
 * @app:		APP handle if available
639 640
 * @vnic_no_name:	For non-port PF vNIC make ndo_get_phys_port_name return
 *			-EOPNOTSUPP to keep backwards compatibility (set by app)
J
Jakub Kicinski 已提交
641
 * @port:		Pointer to nfp_port structure if vNIC is a port
642
 * @app_priv:		APP private data for this vNIC
643 644
 */
struct nfp_net {
645
	struct nfp_net_dp dp;
646

647
	struct nfp_net_fw_version fw_ver;
648

649 650
	u32 id;

651 652 653
	u32 cap;
	u32 max_mtu;

654
	u8 rss_hfunc;
655 656 657 658
	u32 rss_cfg;
	u8 rss_key[NFP_NET_CFG_RSS_KEY_SZ];
	u8 rss_itbl[NFP_NET_CFG_RSS_ITBL_SZ];

659
	struct xdp_attachment_info xdp;
660
	struct xdp_attachment_info xdp_hw;
661

662 663
	unsigned int max_tx_rings;
	unsigned int max_rx_rings;
664 665 666 667

	int stride_tx;
	int stride_rx;

668
	unsigned int max_r_vecs;
669 670
	struct nfp_net_r_vector r_vecs[NFP_NET_MAX_R_VECS];
	struct msix_entry irq_entries[NFP_NET_MAX_IRQS];
671 672 673 674 675 676 677 678 679 680 681 682 683 684

	irq_handler_t lsc_handler;
	char lsc_name[IFNAMSIZ + 8];

	irq_handler_t exn_handler;
	char exn_name[IFNAMSIZ + 8];

	irq_handler_t shared_handler;
	char shared_name[IFNAMSIZ + 8];

	bool link_up;
	spinlock_t link_status_lock;

	spinlock_t reconfig_lock;
685 686 687 688
	u32 reconfig_posted;
	bool reconfig_timer_active;
	bool reconfig_sync_present;
	struct timer_list reconfig_timer;
689
	u32 reconfig_in_progress_update;
690

J
Jakub Kicinski 已提交
691
	struct semaphore bar_lock;
692

693 694
	bool rx_coalesce_adapt_on;
	bool tx_coalesce_adapt_on;
695 696 697 698 699 700 701 702 703 704
	u32 rx_coalesce_usecs;
	u32 rx_coalesce_max_frames;
	u32 tx_coalesce_usecs;
	u32 tx_coalesce_max_frames;

	u8 __iomem *qcp_cfg;

	u8 __iomem *tx_bar;
	u8 __iomem *rx_bar;

705 706
	struct nfp_net_tlv_caps tlv_caps;

707
	unsigned int ktls_tx_conn_cnt;
J
Jakub Kicinski 已提交
708
	unsigned int ktls_rx_conn_cnt;
709

710 711
	atomic64_t ktls_conn_id_gen;

J
Jakub Kicinski 已提交
712
	atomic_t ktls_no_space;
713 714 715
	atomic_t ktls_rx_resync_req;
	atomic_t ktls_rx_resync_ign;
	atomic_t ktls_rx_resync_sent;
J
Jakub Kicinski 已提交
716

717 718 719
	struct {
		struct sk_buff_head queue;
		wait_queue_head_t wq;
720 721 722
		struct workqueue_struct *workq;
		struct work_struct wait_work;
		struct work_struct runq_work;
723 724 725
		u16 tag;
	} mbox_cmsg;

726
	struct dentry *debugfs_dir;
J
Jakub Kicinski 已提交
727

J
Jakub Kicinski 已提交
728
	struct list_head vnic_list;
729

730
	struct pci_dev *pdev;
731
	struct nfp_app *app;
732

733 734
	bool vnic_no_name;

J
Jakub Kicinski 已提交
735
	struct nfp_port *port;
736 737

	void *app_priv;
738 739 740 741 742
};

/* Functions to read/write from/to a BAR
 * Performs any endian conversion necessary.
 */
743 744
static inline u16 nn_readb(struct nfp_net *nn, int off)
{
745
	return readb(nn->dp.ctrl_bar + off);
746 747
}

748 749
static inline void nn_writeb(struct nfp_net *nn, int off, u8 val)
{
750
	writeb(val, nn->dp.ctrl_bar + off);
751 752
}

753 754
static inline u16 nn_readw(struct nfp_net *nn, int off)
{
755
	return readw(nn->dp.ctrl_bar + off);
756 757 758 759
}

static inline void nn_writew(struct nfp_net *nn, int off, u16 val)
{
760
	writew(val, nn->dp.ctrl_bar + off);
761
}
762 763 764

static inline u32 nn_readl(struct nfp_net *nn, int off)
{
765
	return readl(nn->dp.ctrl_bar + off);
766 767 768 769
}

static inline void nn_writel(struct nfp_net *nn, int off, u32 val)
{
770
	writel(val, nn->dp.ctrl_bar + off);
771 772 773 774
}

static inline u64 nn_readq(struct nfp_net *nn, int off)
{
775
	return readq(nn->dp.ctrl_bar + off);
776 777 778 779
}

static inline void nn_writeq(struct nfp_net *nn, int off, u64 val)
{
780
	writeq(val, nn->dp.ctrl_bar + off);
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
}

/* Flush posted PCI writes by reading something without side effects */
static inline void nn_pci_flush(struct nfp_net *nn)
{
	nn_readl(nn, NFP_NET_CFG_VERSION);
}

/* Queue Controller Peripheral access functions and definitions.
 *
 * Some of the BARs of the NFP are mapped to portions of the Queue
 * Controller Peripheral (QCP) address space on the NFP.  A QCP queue
 * has a read and a write pointer (as well as a size and flags,
 * indicating overflow etc).  The QCP offers a number of different
 * operation on queue pointers, but here we only offer function to
 * either add to a pointer or to read the pointer value.
 */
#define NFP_QCP_QUEUE_ADDR_SZ			0x800
799
#define NFP_QCP_QUEUE_AREA_SZ			0x80000
800 801 802 803 804 805 806 807
#define NFP_QCP_QUEUE_OFF(_x)			((_x) * NFP_QCP_QUEUE_ADDR_SZ)
#define NFP_QCP_QUEUE_ADD_RPTR			0x0000
#define NFP_QCP_QUEUE_ADD_WPTR			0x0004
#define NFP_QCP_QUEUE_STS_LO			0x0008
#define NFP_QCP_QUEUE_STS_LO_READPTR_mask	0x3ffff
#define NFP_QCP_QUEUE_STS_HI			0x000c
#define NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask	0x3ffff

J
Jakub Kicinski 已提交
808
/* The offset of a QCP queues in the PCIe Target */
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
#define NFP_PCIE_QUEUE(_q) (0x80000 + (NFP_QCP_QUEUE_ADDR_SZ * ((_q) & 0xff)))

/* nfp_qcp_ptr - Read or Write Pointer of a queue */
enum nfp_qcp_ptr {
	NFP_QCP_READ_PTR = 0,
	NFP_QCP_WRITE_PTR
};

/**
 * nfp_qcp_rd_ptr_add() - Add the value to the read pointer of a queue
 *
 * @q:   Base address for queue structure
 * @val: Value to add to the queue pointer
 */
static inline void nfp_qcp_rd_ptr_add(u8 __iomem *q, u32 val)
{
825
	writel(val, q + NFP_QCP_QUEUE_ADD_RPTR);
826 827 828 829 830 831 832 833 834 835
}

/**
 * nfp_qcp_wr_ptr_add() - Add the value to the write pointer of a queue
 *
 * @q:   Base address for queue structure
 * @val: Value to add to the queue pointer
 */
static inline void nfp_qcp_wr_ptr_add(u8 __iomem *q, u32 val)
{
836
	writel(val, q + NFP_QCP_QUEUE_ADD_WPTR);
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
}

static inline u32 _nfp_qcp_read(u8 __iomem *q, enum nfp_qcp_ptr ptr)
{
	u32 off;
	u32 val;

	if (ptr == NFP_QCP_READ_PTR)
		off = NFP_QCP_QUEUE_STS_LO;
	else
		off = NFP_QCP_QUEUE_STS_HI;

	val = readl(q + off);

	if (ptr == NFP_QCP_READ_PTR)
		return val & NFP_QCP_QUEUE_STS_LO_READPTR_mask;
	else
		return val & NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask;
}

/**
 * nfp_qcp_rd_ptr_read() - Read the current read pointer value for a queue
 * @q:  Base address for queue structure
 *
 * Return: Value read.
 */
static inline u32 nfp_qcp_rd_ptr_read(u8 __iomem *q)
{
	return _nfp_qcp_read(q, NFP_QCP_READ_PTR);
}

/**
 * nfp_qcp_wr_ptr_read() - Read the current write pointer value for a queue
 * @q:  Base address for queue structure
 *
 * Return: Value read.
 */
static inline u32 nfp_qcp_wr_ptr_read(u8 __iomem *q)
{
	return _nfp_qcp_read(q, NFP_QCP_WRITE_PTR);
}

879 880 881 882 883 884 885 886 887 888 889
static inline bool nfp_net_is_data_vnic(struct nfp_net *nn)
{
	WARN_ON_ONCE(!nn->dp.netdev && nn->port);
	return !!nn->dp.netdev;
}

static inline bool nfp_net_running(struct nfp_net *nn)
{
	return nn->dp.ctrl & NFP_NET_CFG_CTRL_ENABLE;
}

J
Jakub Kicinski 已提交
890 891 892 893 894
static inline const char *nfp_net_name(struct nfp_net *nn)
{
	return nn->dp.netdev ? nn->dp.netdev->name : "ctrl";
}

895 896 897 898 899 900 901 902 903 904 905 906
static inline void nfp_ctrl_lock(struct nfp_net *nn)
	__acquires(&nn->r_vecs[0].lock)
{
	spin_lock_bh(&nn->r_vecs[0].lock);
}

static inline void nfp_ctrl_unlock(struct nfp_net *nn)
	__releases(&nn->r_vecs[0].lock)
{
	spin_unlock_bh(&nn->r_vecs[0].lock);
}

907 908
static inline void nn_ctrl_bar_lock(struct nfp_net *nn)
{
J
Jakub Kicinski 已提交
909
	down(&nn->bar_lock);
910 911
}

912 913 914 915 916
static inline bool nn_ctrl_bar_trylock(struct nfp_net *nn)
{
	return !down_trylock(&nn->bar_lock);
}

917 918
static inline void nn_ctrl_bar_unlock(struct nfp_net *nn)
{
J
Jakub Kicinski 已提交
919
	up(&nn->bar_lock);
920 921
}

922
/* Globals */
923
extern const char nfp_driver_version[];
924

J
Jakub Kicinski 已提交
925 926 927 928 929 930 931
extern const struct net_device_ops nfp_net_netdev_ops;

static inline bool nfp_netdev_is_nfp_net(struct net_device *netdev)
{
	return netdev->netdev_ops == &nfp_net_netdev_ops;
}

932 933 934 935 936 937 938 939
static inline int nfp_net_coalesce_para_check(u32 usecs, u32 pkts)
{
	if ((usecs >= ((1 << 16) - 1)) || (pkts >= ((1 << 16) - 1)))
		return -EINVAL;

	return 0;
}

940 941 942 943
/* Prototypes */
void nfp_net_get_fw_version(struct nfp_net_fw_version *fw_ver,
			    void __iomem *ctrl_bar);

944
struct nfp_net *
945
nfp_net_alloc(struct pci_dev *pdev, void __iomem *ctrl_bar, bool needs_netdev,
946 947 948 949 950 951
	      unsigned int max_tx_rings, unsigned int max_rx_rings);
void nfp_net_free(struct nfp_net *nn);

int nfp_net_init(struct nfp_net *nn);
void nfp_net_clean(struct nfp_net *nn);

J
Jakub Kicinski 已提交
952 953 954
int nfp_ctrl_open(struct nfp_net *nn);
void nfp_ctrl_close(struct nfp_net *nn);

955 956
void nfp_net_set_ethtool_ops(struct net_device *netdev);
void nfp_net_info(struct nfp_net *nn);
J
Jakub Kicinski 已提交
957
int __nfp_net_reconfig(struct nfp_net *nn, u32 update);
958
int nfp_net_reconfig(struct nfp_net *nn, u32 update);
959
unsigned int nfp_net_rss_key_sz(struct nfp_net *nn);
960 961 962
void nfp_net_rss_write_itbl(struct nfp_net *nn);
void nfp_net_rss_write_key(struct nfp_net *nn);
void nfp_net_coalesce_write_cfg(struct nfp_net *nn);
963 964 965
int nfp_net_mbox_lock(struct nfp_net *nn, unsigned int data_size);
int nfp_net_mbox_reconfig(struct nfp_net *nn, u32 mbox_cmd);
int nfp_net_mbox_reconfig_and_unlock(struct nfp_net *nn, u32 mbox_cmd);
966 967
void nfp_net_mbox_reconfig_post(struct nfp_net *nn, u32 update);
int nfp_net_mbox_reconfig_wait_posted(struct nfp_net *nn);
968

969
void nfp_net_irq_unmask(struct nfp_net *nn, unsigned int entry_nr);
970 971 972 973 974 975 976
unsigned int
nfp_net_irqs_alloc(struct pci_dev *pdev, struct msix_entry *irq_entries,
		   unsigned int min_irqs, unsigned int want_irqs);
void nfp_net_irqs_disable(struct pci_dev *pdev);
void
nfp_net_irqs_assign(struct nfp_net *nn, struct msix_entry *irq_entries,
		    unsigned int n);
977

978 979 980 981 982 983 984 985 986 987 988 989 990
void nfp_net_tx_xmit_more_flush(struct nfp_net_tx_ring *tx_ring);
void nfp_net_tx_complete(struct nfp_net_tx_ring *tx_ring, int budget);

bool
nfp_net_parse_meta(struct net_device *netdev, struct nfp_meta_parsed *meta,
		   void *data, void *pkt, unsigned int pkt_len, int meta_len);

void nfp_net_rx_csum(const struct nfp_net_dp *dp,
		     struct nfp_net_r_vector *r_vec,
		     const struct nfp_net_rx_desc *rxd,
		     const struct nfp_meta_parsed *meta,
		     struct sk_buff *skb);

991
struct nfp_net_dp *nfp_net_clone_dp(struct nfp_net *nn);
992 993
int nfp_net_ring_reconfig(struct nfp_net *nn, struct nfp_net_dp *new,
			  struct netlink_ext_ack *extack);
994

995
#ifdef CONFIG_NFP_DEBUG
996 997
void nfp_net_debugfs_create(void);
void nfp_net_debugfs_destroy(void);
998
struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev);
999
void nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir);
1000
void nfp_net_debugfs_dir_clean(struct dentry **dir);
1001 1002 1003 1004 1005 1006 1007 1008 1009
#else
static inline void nfp_net_debugfs_create(void)
{
}

static inline void nfp_net_debugfs_destroy(void)
{
}

1010
static inline struct dentry *nfp_net_debugfs_device_add(struct pci_dev *pdev)
1011
{
1012
	return NULL;
1013 1014
}

1015
static inline void
1016
nfp_net_debugfs_vnic_add(struct nfp_net *nn, struct dentry *ddir)
1017 1018 1019 1020
{
}

static inline void nfp_net_debugfs_dir_clean(struct dentry **dir)
1021 1022
{
}
1023
#endif /* CONFIG_NFP_DEBUG */
1024 1025

#endif /* _NFP_NET_H_ */