net/octeontx_ep: add Rx path
[dpdk.git] / drivers / net / octeontx_ep / otx_ep_common.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4 #ifndef _OTX_EP_COMMON_H_
5 #define _OTX_EP_COMMON_H_
6
7 #define OTX_EP_MAX_RINGS_PER_VF        (8)
8 #define OTX_EP_CFG_IO_QUEUES        OTX_EP_MAX_RINGS_PER_VF
9 #define OTX_EP_64BYTE_INSTR         (64)
10 #define OTX_EP_MIN_IQ_DESCRIPTORS   (128)
11 #define OTX_EP_MIN_OQ_DESCRIPTORS   (128)
12 #define OTX_EP_MAX_IQ_DESCRIPTORS   (8192)
13 #define OTX_EP_MAX_OQ_DESCRIPTORS   (8192)
14 #define OTX_EP_OQ_BUF_SIZE          (2048)
15 #define OTX_EP_MIN_RX_BUF_SIZE      (64)
16
17 #define OTX_EP_OQ_INFOPTR_MODE      (0)
18 #define OTX_EP_OQ_REFIL_THRESHOLD   (16)
19 #define OTX_EP_PCI_RING_ALIGN   65536
20 #define SDP_PKIND 40
21 #define SDP_OTX2_PKIND 57
22 #define OTX_EP_BUSY_LOOP_COUNT      (10000)
23 #define OTX_EP_MAX_IOQS_PER_VF 8
24 #define OTX_CUST_DATA_LEN 0
25
26 #define otx_ep_info(fmt, args...)                               \
27         rte_log(RTE_LOG_INFO, otx_net_ep_logtype,               \
28                 "%s():%u " fmt "\n",                            \
29                 __func__, __LINE__, ##args)
30
31 #define otx_ep_err(fmt, args...)                                \
32         rte_log(RTE_LOG_ERR, otx_net_ep_logtype,                \
33                 "%s():%u " fmt "\n",                            \
34                 __func__, __LINE__, ##args)
35
36 #define otx_ep_dbg(fmt, args...)                                \
37         rte_log(RTE_LOG_DEBUG, otx_net_ep_logtype,              \
38                 "%s():%u " fmt "\n",                            \
39                 __func__, __LINE__, ##args)
40
41 /* Input Request Header format */
42 union otx_ep_instr_irh {
43         uint64_t u64;
44         struct {
45                 /* Request ID  */
46                 uint64_t rid:16;
47
48                 /* PCIe port to use for response */
49                 uint64_t pcie_port:3;
50
51                 /* Scatter indicator  1=scatter */
52                 uint64_t scatter:1;
53
54                 /* Size of Expected result OR no. of entries in scatter list */
55                 uint64_t rlenssz:14;
56
57                 /* Desired destination port for result */
58                 uint64_t dport:6;
59
60                 /* Opcode Specific parameters */
61                 uint64_t param:8;
62
63                 /* Opcode for the return packet  */
64                 uint64_t opcode:16;
65         } s;
66 };
67
68 #define otx_ep_write64(value, base_addr, reg_off) \
69         {\
70         typeof(value) val = (value); \
71         typeof(reg_off) off = (reg_off); \
72         otx_ep_dbg("octeon_write_csr64: reg: 0x%08lx val: 0x%016llx\n", \
73                    (unsigned long)off, (unsigned long long)val); \
74         rte_write64(val, ((base_addr) + off)); \
75         }
76
77 /* Instruction Header - for OCTEON-TX models */
78 typedef union otx_ep_instr_ih {
79         uint64_t u64;
80         struct {
81           /** Data Len */
82                 uint64_t tlen:16;
83
84           /** Reserved */
85                 uint64_t rsvd:20;
86
87           /** PKIND for OTX_EP */
88                 uint64_t pkind:6;
89
90           /** Front Data size */
91                 uint64_t fsz:6;
92
93           /** No. of entries in gather list */
94                 uint64_t gsz:14;
95
96           /** Gather indicator 1=gather*/
97                 uint64_t gather:1;
98
99           /** Reserved3 */
100                 uint64_t reserved3:1;
101         } s;
102 } otx_ep_instr_ih_t;
103
104 /* OTX_EP IQ request list */
105 struct otx_ep_instr_list {
106         void *buf;
107         uint32_t reqtype;
108 };
109 #define OTX_EP_IQREQ_LIST_SIZE  (sizeof(struct otx_ep_instr_list))
110
111 /* Input Queue statistics. Each input queue has four stats fields. */
112 struct otx_ep_iq_stats {
113         uint64_t instr_posted; /* Instructions posted to this queue. */
114         uint64_t instr_processed; /* Instructions processed in this queue. */
115         uint64_t instr_dropped; /* Instructions that could not be processed */
116         uint64_t tx_pkts;
117         uint64_t tx_bytes;
118 };
119
120 /* Structure to define the configuration attributes for each Input queue. */
121 struct otx_ep_iq_config {
122         /* Max number of IQs available */
123         uint16_t max_iqs;
124
125         /* Command size - 32 or 64 bytes */
126         uint16_t instr_type;
127
128         /* Pending list size, usually set to the sum of the size of all IQs */
129         uint32_t pending_list_size;
130 };
131
132 /** The instruction (input) queue.
133  *  The input queue is used to post raw (instruction) mode data or packet data
134  *  to OCTEON TX2 device from the host. Each IQ of a OTX_EP EP VF device has one
135  *  such structure to represent it.
136  */
137 struct otx_ep_instr_queue {
138         struct otx_ep_device *otx_ep_dev;
139
140         uint32_t q_no;
141         uint32_t pkt_in_done;
142
143         /* Flag for 64 byte commands. */
144         uint32_t iqcmd_64B:1;
145         uint32_t rsvd:17;
146         uint32_t status:8;
147
148         /* Number of  descriptors in this ring. */
149         uint32_t nb_desc;
150
151         /* Input ring index, where the driver should write the next packet */
152         uint32_t host_write_index;
153
154         /* Input ring index, where the OCTEON TX2 should read the next packet */
155         uint32_t otx_read_index;
156
157         uint32_t reset_instr_cnt;
158
159         /** This index aids in finding the window in the queue where OCTEON TX2
160          *  has read the commands.
161          */
162         uint32_t flush_index;
163
164         /* This keeps track of the instructions pending in this queue. */
165         uint64_t instr_pending;
166
167         /* Pointer to the Virtual Base addr of the input ring. */
168         uint8_t *base_addr;
169
170         /* This IQ request list */
171         struct otx_ep_instr_list *req_list;
172
173         /* OTX_EP doorbell register for the ring. */
174         void *doorbell_reg;
175
176         /* OTX_EP instruction count register for this ring. */
177         void *inst_cnt_reg;
178
179         /* Number of instructions pending to be posted to OCTEON TX2. */
180         uint32_t fill_cnt;
181
182         /* Statistics for this input queue. */
183         struct otx_ep_iq_stats stats;
184
185         /* DMA mapped base address of the input descriptor ring. */
186         uint64_t base_addr_dma;
187
188         /* Memory zone */
189         const struct rte_memzone *iq_mz;
190 };
191
192 /** Descriptor format.
193  *  The descriptor ring is made of descriptors which have 2 64-bit values:
194  *  -# Physical (bus) address of the data buffer.
195  *  -# Physical (bus) address of a otx_ep_droq_info structure.
196  *  The device DMA's incoming packets and its information at the address
197  *  given by these descriptor fields.
198  */
199 struct otx_ep_droq_desc {
200         /* The buffer pointer */
201         uint64_t buffer_ptr;
202
203         /* The Info pointer */
204         uint64_t info_ptr;
205 };
206 #define OTX_EP_DROQ_DESC_SIZE   (sizeof(struct otx_ep_droq_desc))
207
208 /* Receive Header */
209 union otx_ep_rh {
210         uint64_t rh64;
211 };
212 #define OTX_EP_RH_SIZE (sizeof(union otx_ep_rh))
213
214 /** Information about packet DMA'ed by OCTEON TX2.
215  *  The format of the information available at Info Pointer after OCTEON TX2
216  *  has posted a packet. Not all descriptors have valid information. Only
217  *  the Info field of the first descriptor for a packet has information
218  *  about the packet.
219  */
220 struct otx_ep_droq_info {
221         /* The Length of the packet. */
222         uint64_t length;
223
224         /* The Output Receive Header. */
225         union otx_ep_rh rh;
226 };
227 #define OTX_EP_DROQ_INFO_SIZE   (sizeof(struct otx_ep_droq_info))
228
229 /* DROQ statistics. Each output queue has four stats fields. */
230 struct otx_ep_droq_stats {
231         /* Number of packets received in this queue. */
232         uint64_t pkts_received;
233
234         /* Bytes received by this queue. */
235         uint64_t bytes_received;
236
237         /* Num of failures of rte_pktmbuf_alloc() */
238         uint64_t rx_alloc_failure;
239
240         /* Rx error */
241         uint64_t rx_err;
242
243         /* packets with data got ready after interrupt arrived */
244         uint64_t pkts_delayed_data;
245
246         /* packets dropped due to zero length */
247         uint64_t dropped_zlp;
248 };
249
250 /* Structure to define the configuration attributes for each Output queue. */
251 struct otx_ep_oq_config {
252         /* Max number of OQs available */
253         uint16_t max_oqs;
254
255         /* If set, the Output queue uses info-pointer mode. (Default: 1 ) */
256         uint16_t info_ptr;
257
258         /** The number of buffers that were consumed during packet processing by
259          *  the driver on this Output queue before the driver attempts to
260          *  replenish the descriptor ring with new buffers.
261          */
262         uint32_t refill_threshold;
263 };
264
265 /* The Descriptor Ring Output Queue(DROQ) structure. */
266 struct otx_ep_droq {
267         struct otx_ep_device *otx_ep_dev;
268         /* The 8B aligned descriptor ring starts at this address. */
269         struct otx_ep_droq_desc *desc_ring;
270
271         uint32_t q_no;
272         uint64_t last_pkt_count;
273
274         struct rte_mempool *mpool;
275
276         /* Driver should read the next packet at this index */
277         uint32_t read_idx;
278
279         /* OCTEON TX2 will write the next packet at this index */
280         uint32_t write_idx;
281
282         /* At this index, the driver will refill the descriptor's buffer */
283         uint32_t refill_idx;
284
285         /* Packets pending to be processed */
286         uint64_t pkts_pending;
287
288         /* Number of descriptors in this ring. */
289         uint32_t nb_desc;
290
291         /* The number of descriptors pending to refill. */
292         uint32_t refill_count;
293
294         uint32_t refill_threshold;
295
296         /* The 8B aligned info ptrs begin from this address. */
297         struct otx_ep_droq_info *info_list;
298
299         /* receive buffer list contains mbuf ptr list */
300         struct rte_mbuf **recv_buf_list;
301
302         /* The size of each buffer pointed by the buffer pointer. */
303         uint32_t buffer_size;
304
305         /** Pointer to the mapped packet credit register.
306          *  Host writes number of info/buffer ptrs available to this register
307          */
308         void *pkts_credit_reg;
309
310         /** Pointer to the mapped packet sent register. OCTEON TX2 writes the
311          *  number of packets DMA'ed to host memory in this register.
312          */
313         void *pkts_sent_reg;
314
315         /* Statistics for this DROQ. */
316         struct otx_ep_droq_stats stats;
317
318         /* DMA mapped address of the DROQ descriptor ring. */
319         size_t desc_ring_dma;
320
321         /* Info_ptr list is allocated at this virtual address. */
322         size_t info_base_addr;
323
324         /* DMA mapped address of the info list */
325         size_t info_list_dma;
326
327         /* Allocated size of info list. */
328         uint32_t info_alloc_size;
329
330         /* Memory zone **/
331         const struct rte_memzone *desc_ring_mz;
332
333         const struct rte_memzone *info_mz;
334 };
335 #define OTX_EP_DROQ_SIZE                (sizeof(struct otx_ep_droq))
336
337 /* IQ/OQ mask */
338 struct otx_ep_io_enable {
339         uint64_t iq;
340         uint64_t oq;
341         uint64_t iq64B;
342 };
343
344 /* Structure to define the configuration. */
345 struct otx_ep_config {
346         /* Input Queue attributes. */
347         struct otx_ep_iq_config iq;
348
349         /* Output Queue attributes. */
350         struct otx_ep_oq_config oq;
351
352         /* Num of desc for IQ rings */
353         uint32_t num_iqdef_descs;
354
355         /* Num of desc for OQ rings */
356         uint32_t num_oqdef_descs;
357
358         /* OQ buffer size */
359         uint32_t oqdef_buf_size;
360 };
361
362 /* SRIOV information */
363 struct otx_ep_sriov_info {
364         /* Number of rings assigned to VF */
365         uint32_t rings_per_vf;
366
367         /* Number of VF devices enabled */
368         uint32_t num_vfs;
369 };
370
371 /* Required functions for each VF device */
372 struct otx_ep_fn_list {
373         void (*setup_iq_regs)(struct otx_ep_device *otx_ep, uint32_t q_no);
374
375         void (*setup_oq_regs)(struct otx_ep_device *otx_ep, uint32_t q_no);
376
377         void (*setup_device_regs)(struct otx_ep_device *otx_ep);
378
379         int (*enable_io_queues)(struct otx_ep_device *otx_ep);
380         void (*disable_io_queues)(struct otx_ep_device *otx_ep);
381
382         int (*enable_iq)(struct otx_ep_device *otx_ep, uint32_t q_no);
383         void (*disable_iq)(struct otx_ep_device *otx_ep, uint32_t q_no);
384
385         int (*enable_oq)(struct otx_ep_device *otx_ep, uint32_t q_no);
386         void (*disable_oq)(struct otx_ep_device *otx_ep, uint32_t q_no);
387 };
388
389 /* OTX_EP EP VF device data structure */
390 struct otx_ep_device {
391         /* PCI device pointer */
392         struct rte_pci_device *pdev;
393
394         uint16_t chip_id;
395
396         uint32_t pkind;
397
398         struct rte_eth_dev *eth_dev;
399
400         int port_id;
401
402         /* Memory mapped h/w address */
403         uint8_t *hw_addr;
404
405         struct otx_ep_fn_list fn_list;
406
407         uint32_t max_tx_queues;
408
409         uint32_t max_rx_queues;
410
411         /* Num IQs */
412         uint32_t nb_tx_queues;
413
414         /* The input instruction queues */
415         struct otx_ep_instr_queue *instr_queue[OTX_EP_MAX_IOQS_PER_VF];
416
417         /* Num OQs */
418         uint32_t nb_rx_queues;
419
420         /* The DROQ output queues  */
421         struct otx_ep_droq *droq[OTX_EP_MAX_IOQS_PER_VF];
422
423         /* IOQ mask */
424         struct otx_ep_io_enable io_qmask;
425
426         /* SR-IOV info */
427         struct otx_ep_sriov_info sriov_info;
428
429         /* Device configuration */
430         const struct otx_ep_config *conf;
431
432         uint64_t rx_offloads;
433
434         uint64_t tx_offloads;
435 };
436
437 int otx_ep_setup_iqs(struct otx_ep_device *otx_ep, uint32_t iq_no,
438                      int num_descs, unsigned int socket_id);
439 int otx_ep_delete_iqs(struct otx_ep_device *otx_ep, uint32_t iq_no);
440
441 int otx_ep_setup_oqs(struct otx_ep_device *otx_ep, int oq_no, int num_descs,
442                      int desc_size, struct rte_mempool *mpool,
443                      unsigned int socket_id);
444 int otx_ep_delete_oqs(struct otx_ep_device *otx_ep, uint32_t oq_no);
445
446 #define OTX_EP_MAX_PKT_SZ 64000U
447 #define OTX_EP_MAX_MAC_ADDRS 1
448 #define OTX_EP_CLEAR_ISIZE_BSIZE 0x7FFFFFULL
449 #define OTX_EP_CLEAR_OUT_INT_LVLS 0x3FFFFFFFFFFFFFULL
450 #define OTX_EP_CLEAR_IN_INT_LVLS 0xFFFFFFFF
451 #define OTX_EP_CLEAR_SDP_IN_INT_LVLS 0x3FFFFFFFFFFFFFUL
452 #define OTX_EP_DROQ_BUFSZ_MASK 0xFFFF
453 #define OTX_EP_CLEAR_SLIST_DBELL 0xFFFFFFFF
454 #define OTX_EP_CLEAR_SDP_OUT_PKT_CNT 0xFFFFFFFFF
455
456 extern int otx_net_ep_logtype;
457 #endif  /* _OTX_EP_COMMON_H_ */