f0142e1ff7467f1723728ea750482a44a0f31240
[dpdk.git] / drivers / net / nfp / nfp_net_pmd.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2014-2018 Netronome Systems, Inc.
3  * All rights reserved.
4  */
5
6 /*
7  * vim:shiftwidth=8:noexpandtab
8  *
9  * @file dpdk/pmd/nfp_net_pmd.h
10  *
11  * Netronome NFP_NET PMD driver
12  */
13
14 #ifndef _NFP_NET_PMD_H_
15 #define _NFP_NET_PMD_H_
16
17 #define NFP_NET_PMD_VERSION "0.1"
18 #define PCI_VENDOR_ID_NETRONOME         0x19ee
19 #define PCI_DEVICE_ID_NFP4000_PF_NIC    0x4000
20 #define PCI_DEVICE_ID_NFP6000_PF_NIC    0x6000
21 #define PCI_DEVICE_ID_NFP6000_VF_NIC    0x6003
22
23 /* Forward declaration */
24 struct nfp_net_adapter;
25
26 /*
27  * The maximum number of descriptors is limited by design as
28  * DPDK uses uint16_t variables for these values
29  */
30 #define NFP_NET_MAX_TX_DESC (32 * 1024)
31 #define NFP_NET_MIN_TX_DESC 64
32
33 #define NFP_NET_MAX_RX_DESC (32 * 1024)
34 #define NFP_NET_MIN_RX_DESC 64
35
36 /* Bar allocation */
37 #define NFP_NET_CRTL_BAR        0
38 #define NFP_NET_TX_BAR          2
39 #define NFP_NET_RX_BAR          2
40 #define NFP_QCP_QUEUE_AREA_SZ                   0x80000
41
42 /* Macros for accessing the Queue Controller Peripheral 'CSRs' */
43 #define NFP_QCP_QUEUE_OFF(_x)                 ((_x) * 0x800)
44 #define NFP_QCP_QUEUE_ADD_RPTR                  0x0000
45 #define NFP_QCP_QUEUE_ADD_WPTR                  0x0004
46 #define NFP_QCP_QUEUE_STS_LO                    0x0008
47 #define NFP_QCP_QUEUE_STS_LO_READPTR_mask     (0x3ffff)
48 #define NFP_QCP_QUEUE_STS_HI                    0x000c
49 #define NFP_QCP_QUEUE_STS_HI_WRITEPTR_mask    (0x3ffff)
50
51 /* Interrupt definitions */
52 #define NFP_NET_IRQ_LSC_IDX             0
53
54 /* Default values for RX/TX configuration */
55 #define DEFAULT_RX_FREE_THRESH  32
56 #define DEFAULT_RX_PTHRESH      8
57 #define DEFAULT_RX_HTHRESH      8
58 #define DEFAULT_RX_WTHRESH      0
59
60 #define DEFAULT_TX_RS_THRESH    32
61 #define DEFAULT_TX_FREE_THRESH  32
62 #define DEFAULT_TX_PTHRESH      32
63 #define DEFAULT_TX_HTHRESH      0
64 #define DEFAULT_TX_WTHRESH      0
65 #define DEFAULT_TX_RSBIT_THRESH 32
66
67 /* Alignment for dma zones */
68 #define NFP_MEMZONE_ALIGN       128
69
70 /*
71  * This is used by the reconfig protocol. It sets the maximum time waiting in
72  * milliseconds before a reconfig timeout happens.
73  */
74 #define NFP_NET_POLL_TIMEOUT    5000
75
76 #define NFP_QCP_QUEUE_ADDR_SZ   (0x800)
77
78 #define NFP_NET_LINK_DOWN_CHECK_TIMEOUT 4000 /* ms */
79 #define NFP_NET_LINK_UP_CHECK_TIMEOUT   1000 /* ms */
80
81 /* Version number helper defines */
82 #define NFD_CFG_CLASS_VER_msk       0xff
83 #define NFD_CFG_CLASS_VER_shf       24
84 #define NFD_CFG_CLASS_VER(x)        (((x) & 0xff) << 24)
85 #define NFD_CFG_CLASS_VER_of(x)     (((x) >> 24) & 0xff)
86 #define NFD_CFG_CLASS_TYPE_msk      0xff
87 #define NFD_CFG_CLASS_TYPE_shf      16
88 #define NFD_CFG_CLASS_TYPE(x)       (((x) & 0xff) << 16)
89 #define NFD_CFG_CLASS_TYPE_of(x)    (((x) >> 16) & 0xff)
90 #define NFD_CFG_MAJOR_VERSION_msk   0xff
91 #define NFD_CFG_MAJOR_VERSION_shf   8
92 #define NFD_CFG_MAJOR_VERSION(x)    (((x) & 0xff) << 8)
93 #define NFD_CFG_MAJOR_VERSION_of(x) (((x) >> 8) & 0xff)
94 #define NFD_CFG_MINOR_VERSION_msk   0xff
95 #define NFD_CFG_MINOR_VERSION_shf   0
96 #define NFD_CFG_MINOR_VERSION(x)    (((x) & 0xff) << 0)
97 #define NFD_CFG_MINOR_VERSION_of(x) (((x) >> 0) & 0xff)
98
99 #include <linux/types.h>
100 #include <rte_io.h>
101
102 static inline uint8_t nn_readb(volatile const void *addr)
103 {
104         return rte_read8(addr);
105 }
106
107 static inline void nn_writeb(uint8_t val, volatile void *addr)
108 {
109         rte_write8(val, addr);
110 }
111
112 static inline uint32_t nn_readl(volatile const void *addr)
113 {
114         return rte_read32(addr);
115 }
116
117 static inline void nn_writel(uint32_t val, volatile void *addr)
118 {
119         rte_write32(val, addr);
120 }
121
122 static inline void nn_writew(uint16_t val, volatile void *addr)
123 {
124         rte_write16(val, addr);
125 }
126
127 static inline uint64_t nn_readq(volatile void *addr)
128 {
129         const volatile uint32_t *p = addr;
130         uint32_t low, high;
131
132         high = nn_readl((volatile const void *)(p + 1));
133         low = nn_readl((volatile const void *)p);
134
135         return low + ((uint64_t)high << 32);
136 }
137
138 static inline void nn_writeq(uint64_t val, volatile void *addr)
139 {
140         nn_writel(val >> 32, (volatile char *)addr + 4);
141         nn_writel(val, addr);
142 }
143
144 /* TX descriptor format */
145 #define PCIE_DESC_TX_EOP                (1 << 7)
146 #define PCIE_DESC_TX_OFFSET_MASK        (0x7f)
147
148 /* Flags in the host TX descriptor */
149 #define PCIE_DESC_TX_CSUM               (1 << 7)
150 #define PCIE_DESC_TX_IP4_CSUM           (1 << 6)
151 #define PCIE_DESC_TX_TCP_CSUM           (1 << 5)
152 #define PCIE_DESC_TX_UDP_CSUM           (1 << 4)
153 #define PCIE_DESC_TX_VLAN               (1 << 3)
154 #define PCIE_DESC_TX_LSO                (1 << 2)
155 #define PCIE_DESC_TX_ENCAP_NONE         (0)
156 #define PCIE_DESC_TX_ENCAP_VXLAN        (1 << 1)
157 #define PCIE_DESC_TX_ENCAP_GRE          (1 << 0)
158
159 struct nfp_net_tx_desc {
160         union {
161                 struct {
162                         uint8_t dma_addr_hi; /* High bits of host buf address */
163                         __le16 dma_len;     /* Length to DMA for this desc */
164                         uint8_t offset_eop; /* Offset in buf where pkt starts +
165                                              * highest bit is eop flag.
166                                              */
167                         __le32 dma_addr_lo; /* Low 32bit of host buf addr */
168
169                         __le16 mss;         /* MSS to be used for LSO */
170                         uint8_t lso_hdrlen; /* LSO, where the data starts */
171                         uint8_t flags;      /* TX Flags, see @PCIE_DESC_TX_* */
172
173                         union {
174                                 struct {
175                                         /*
176                                          * L3 and L4 header offsets required
177                                          * for TSOv2
178                                          */
179                                         uint8_t l3_offset;
180                                         uint8_t l4_offset;
181                                 };
182                                 __le16 vlan; /* VLAN tag to add if indicated */
183                         };
184                         __le16 data_len;    /* Length of frame + meta data */
185                 } __attribute__((__packed__));
186                 __le32 vals[4];
187         };
188 };
189
190 struct nfp_net_txq {
191         struct nfp_net_hw *hw; /* Backpointer to nfp_net structure */
192
193         /*
194          * Queue information: @qidx is the queue index from Linux's
195          * perspective.  @tx_qcidx is the index of the Queue
196          * Controller Peripheral queue relative to the TX queue BAR.
197          * @cnt is the size of the queue in number of
198          * descriptors. @qcp_q is a pointer to the base of the queue
199          * structure on the NFP
200          */
201         uint8_t *qcp_q;
202
203         /*
204          * Read and Write pointers.  @wr_p and @rd_p are host side pointer,
205          * they are free running and have little relation to the QCP pointers *
206          * @qcp_rd_p is a local copy queue controller peripheral read pointer
207          */
208
209         uint32_t wr_p;
210         uint32_t rd_p;
211
212         uint32_t tx_count;
213
214         uint32_t tx_free_thresh;
215
216         /*
217          * For each descriptor keep a reference to the mbuf and
218          * DMA address used until completion is signalled.
219          */
220         struct {
221                 struct rte_mbuf *mbuf;
222         } *txbufs;
223
224         /*
225          * Information about the host side queue location. @txds is
226          * the virtual address for the queue, @dma is the DMA address
227          * of the queue and @size is the size in bytes for the queue
228          * (needed for free)
229          */
230         struct nfp_net_tx_desc *txds;
231
232         /*
233          * At this point 48 bytes have been used for all the fields in the
234          * TX critical path. We have room for 8 bytes and still all placed
235          * in a cache line. We are not using the threshold values below but
236          * if we need to, we can add the most used in the remaining bytes.
237          */
238         uint32_t tx_rs_thresh; /* not used by now. Future? */
239         uint32_t tx_pthresh;   /* not used by now. Future? */
240         uint32_t tx_hthresh;   /* not used by now. Future? */
241         uint32_t tx_wthresh;   /* not used by now. Future? */
242         uint16_t port_id;
243         int qidx;
244         int tx_qcidx;
245         __le64 dma;
246 } __rte_aligned(64);
247
248 /* RX and freelist descriptor format */
249 #define PCIE_DESC_RX_DD                 (1 << 7)
250 #define PCIE_DESC_RX_META_LEN_MASK      (0x7f)
251
252 /* Flags in the RX descriptor */
253 #define PCIE_DESC_RX_RSS                (1 << 15)
254 #define PCIE_DESC_RX_I_IP4_CSUM         (1 << 14)
255 #define PCIE_DESC_RX_I_IP4_CSUM_OK      (1 << 13)
256 #define PCIE_DESC_RX_I_TCP_CSUM         (1 << 12)
257 #define PCIE_DESC_RX_I_TCP_CSUM_OK      (1 << 11)
258 #define PCIE_DESC_RX_I_UDP_CSUM         (1 << 10)
259 #define PCIE_DESC_RX_I_UDP_CSUM_OK      (1 <<  9)
260 #define PCIE_DESC_RX_SPARE              (1 <<  8)
261 #define PCIE_DESC_RX_EOP                (1 <<  7)
262 #define PCIE_DESC_RX_IP4_CSUM           (1 <<  6)
263 #define PCIE_DESC_RX_IP4_CSUM_OK        (1 <<  5)
264 #define PCIE_DESC_RX_TCP_CSUM           (1 <<  4)
265 #define PCIE_DESC_RX_TCP_CSUM_OK        (1 <<  3)
266 #define PCIE_DESC_RX_UDP_CSUM           (1 <<  2)
267 #define PCIE_DESC_RX_UDP_CSUM_OK        (1 <<  1)
268 #define PCIE_DESC_RX_VLAN               (1 <<  0)
269
270 #define PCIE_DESC_RX_L4_CSUM_OK         (PCIE_DESC_RX_TCP_CSUM_OK | \
271                                          PCIE_DESC_RX_UDP_CSUM_OK)
272 struct nfp_net_rx_desc {
273         union {
274                 /* Freelist descriptor */
275                 struct {
276                         uint8_t dma_addr_hi;
277                         __le16 spare;
278                         uint8_t dd;
279
280                         __le32 dma_addr_lo;
281                 } __attribute__((__packed__)) fld;
282
283                 /* RX descriptor */
284                 struct {
285                         __le16 data_len;
286                         uint8_t reserved;
287                         uint8_t meta_len_dd;
288
289                         __le16 flags;
290                         __le16 vlan;
291                 } __attribute__((__packed__)) rxd;
292
293                 __le32 vals[2];
294         };
295 };
296
297 struct nfp_net_rx_buff {
298         struct rte_mbuf *mbuf;
299 };
300
301 struct nfp_net_rxq {
302         struct nfp_net_hw *hw;  /* Backpointer to nfp_net structure */
303
304          /*
305           * @qcp_fl and @qcp_rx are pointers to the base addresses of the
306           * freelist and RX queue controller peripheral queue structures on the
307           * NFP
308           */
309         uint8_t *qcp_fl;
310         uint8_t *qcp_rx;
311
312         /*
313          * Read and Write pointers.  @wr_p and @rd_p are host side
314          * pointer, they are free running and have little relation to
315          * the QCP pointers. @wr_p is where the driver adds new
316          * freelist descriptors and @rd_p is where the driver start
317          * reading descriptors for newly arrive packets from.
318          */
319         uint32_t rd_p;
320
321         /*
322          * For each buffer placed on the freelist, record the
323          * associated SKB
324          */
325         struct nfp_net_rx_buff *rxbufs;
326
327         /*
328          * Information about the host side queue location.  @rxds is
329          * the virtual address for the queue
330          */
331         struct nfp_net_rx_desc *rxds;
332
333         /*
334          * The mempool is created by the user specifying a mbuf size.
335          * We save here the reference of the mempool needed in the RX
336          * path and the mbuf size for checking received packets can be
337          * safely copied to the mbuf using the NFP_NET_RX_OFFSET
338          */
339         struct rte_mempool *mem_pool;
340         uint16_t mbuf_size;
341
342         /*
343          * Next two fields are used for giving more free descriptors
344          * to the NFP
345          */
346         uint16_t rx_free_thresh;
347         uint16_t nb_rx_hold;
348
349          /* the size of the queue in number of descriptors */
350         uint16_t rx_count;
351
352         /*
353          * Fields above this point fit in a single cache line and are all used
354          * in the RX critical path. Fields below this point are just used
355          * during queue configuration or not used at all (yet)
356          */
357
358         /* referencing dev->data->port_id */
359         uint16_t port_id;
360
361         uint8_t  crc_len; /* Not used by now */
362         uint8_t  drop_en; /* Not used by now */
363
364         /* DMA address of the queue */
365         __le64 dma;
366
367         /*
368          * Queue information: @qidx is the queue index from Linux's
369          * perspective.  @fl_qcidx is the index of the Queue
370          * Controller peripheral queue relative to the RX queue BAR
371          * used for the freelist and @rx_qcidx is the Queue Controller
372          * Peripheral index for the RX queue.
373          */
374         int qidx;
375         int fl_qcidx;
376         int rx_qcidx;
377 } __rte_aligned(64);
378
379 struct nfp_net_hw {
380         /* Info from the firmware */
381         uint32_t ver;
382         uint32_t cap;
383         uint32_t max_mtu;
384         uint32_t mtu;
385         uint32_t rx_offset;
386
387         /* Current values for control */
388         uint32_t ctrl;
389
390         uint8_t *ctrl_bar;
391         uint8_t *tx_bar;
392         uint8_t *rx_bar;
393
394         int stride_rx;
395         int stride_tx;
396
397         uint8_t *qcp_cfg;
398         rte_spinlock_t reconfig_lock;
399
400         uint32_t max_tx_queues;
401         uint32_t max_rx_queues;
402         uint16_t flbufsz;
403         uint16_t device_id;
404         uint16_t vendor_id;
405         uint16_t subsystem_device_id;
406         uint16_t subsystem_vendor_id;
407 #if defined(DSTQ_SELECTION)
408 #if DSTQ_SELECTION
409         uint16_t device_function;
410 #endif
411 #endif
412
413         uint8_t mac_addr[RTE_ETHER_ADDR_LEN];
414
415         /* Records starting point for counters */
416         struct rte_eth_stats eth_stats_base;
417
418         struct nfp_cpp *cpp;
419         struct nfp_cpp_area *ctrl_area;
420         struct nfp_cpp_area *hwqueues_area;
421         struct nfp_cpp_area *msix_area;
422
423         uint8_t *hw_queues;
424         uint8_t is_pf;
425         uint8_t pf_port_idx;
426         uint8_t pf_multiport_enabled;
427         uint8_t total_ports;
428
429         union eth_table_entry *eth_table;
430
431         struct nfp_hwinfo *hwinfo;
432         struct nfp_rtsym_table *sym_tbl;
433         uint32_t nfp_cpp_service_id;
434 };
435
436 struct nfp_net_adapter {
437         struct nfp_net_hw hw;
438 };
439
440 #define NFP_NET_DEV_PRIVATE_TO_HW(adapter)\
441         (&((struct nfp_net_adapter *)adapter)->hw)
442
443 #endif /* _NFP_NET_PMD_H_ */
444 /*
445  * Local variables:
446  * c-file-style: "Linux"
447  * indent-tabs-mode: t
448  * End:
449  */