nfp_net.h 28 KB

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