nfp_net.h 26 KB

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