nfp_net.h 26 KB

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