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