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