ad85d0da94d34f2f9ec27110eb543f67f8dd9513
[dpdk.git] / drivers / net / hns3 / hns3_rxtx.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2021 HiSilicon Limited.
3  */
4
5 #ifndef _HNS3_RXTX_H_
6 #define _HNS3_RXTX_H_
7
8 #include <stdint.h>
9 #include <rte_mbuf_core.h>
10
11 #define HNS3_MIN_RING_DESC      64
12 #define HNS3_MAX_RING_DESC      32768
13 #define HNS3_DEFAULT_RING_DESC  1024
14 #define HNS3_ALIGN_RING_DESC    32
15 #define HNS3_RING_BASE_ALIGN    128
16 #define HNS3_BULK_ALLOC_MBUF_NUM        32
17
18 #define HNS3_DEFAULT_RX_FREE_THRESH     32
19 #define HNS3_DEFAULT_TX_FREE_THRESH     32
20 #define HNS3_DEFAULT_TX_RS_THRESH       32
21 #define HNS3_TX_FAST_FREE_AHEAD         64
22
23 #define HNS3_DEFAULT_RX_BURST           32
24 #if (HNS3_DEFAULT_RX_BURST > 64)
25 #error "PMD HNS3: HNS3_DEFAULT_RX_BURST must <= 64\n"
26 #endif
27 #define HNS3_DEFAULT_DESCS_PER_LOOP     4
28 #define HNS3_SVE_DEFAULT_DESCS_PER_LOOP 8
29 #if (HNS3_DEFAULT_DESCS_PER_LOOP > HNS3_SVE_DEFAULT_DESCS_PER_LOOP)
30 #define HNS3_VECTOR_RX_OFFSET_TABLE_LEN HNS3_DEFAULT_DESCS_PER_LOOP
31 #else
32 #define HNS3_VECTOR_RX_OFFSET_TABLE_LEN HNS3_SVE_DEFAULT_DESCS_PER_LOOP
33 #endif
34 #define HNS3_DEFAULT_RXQ_REARM_THRESH   64
35 #define HNS3_UINT8_BIT                  8
36 #define HNS3_UINT16_BIT                 16
37 #define HNS3_UINT32_BIT                 32
38
39 #define HNS3_512_BD_BUF_SIZE    512
40 #define HNS3_1K_BD_BUF_SIZE     1024
41 #define HNS3_2K_BD_BUF_SIZE     2048
42 #define HNS3_4K_BD_BUF_SIZE     4096
43
44 #define HNS3_MIN_BD_BUF_SIZE    HNS3_512_BD_BUF_SIZE
45 #define HNS3_MAX_BD_BUF_SIZE    HNS3_4K_BD_BUF_SIZE
46
47 #define HNS3_BD_SIZE_512_TYPE                   0
48 #define HNS3_BD_SIZE_1024_TYPE                  1
49 #define HNS3_BD_SIZE_2048_TYPE                  2
50 #define HNS3_BD_SIZE_4096_TYPE                  3
51
52 #define HNS3_RX_FLAG_VLAN_PRESENT               0x1
53 #define HNS3_RX_FLAG_L3ID_IPV4                  0x0
54 #define HNS3_RX_FLAG_L3ID_IPV6                  0x1
55 #define HNS3_RX_FLAG_L4ID_UDP                   0x0
56 #define HNS3_RX_FLAG_L4ID_TCP                   0x1
57
58 #define HNS3_RXD_DMAC_S                         0
59 #define HNS3_RXD_DMAC_M                         (0x3 << HNS3_RXD_DMAC_S)
60 #define HNS3_RXD_VLAN_S                         2
61 #define HNS3_RXD_VLAN_M                         (0x3 << HNS3_RXD_VLAN_S)
62 #define HNS3_RXD_L3ID_S                         4
63 #define HNS3_RXD_L3ID_M                         (0xf << HNS3_RXD_L3ID_S)
64 #define HNS3_RXD_L4ID_S                         8
65 #define HNS3_RXD_L4ID_M                         (0xf << HNS3_RXD_L4ID_S)
66 #define HNS3_RXD_FRAG_B                         12
67 #define HNS3_RXD_STRP_TAGP_S                    13
68 #define HNS3_RXD_STRP_TAGP_M                    (0x3 << HNS3_RXD_STRP_TAGP_S)
69
70 #define HNS3_RXD_L2E_B                          16
71 #define HNS3_RXD_L3E_B                          17
72 #define HNS3_RXD_L4E_B                          18
73 #define HNS3_RXD_TRUNCATE_B                     19
74 #define HNS3_RXD_HOI_B                          20
75 #define HNS3_RXD_DOI_B                          21
76 #define HNS3_RXD_OL3E_B                         22
77 #define HNS3_RXD_OL4E_B                         23
78 #define HNS3_RXD_GRO_COUNT_S                    24
79 #define HNS3_RXD_GRO_COUNT_M                    (0x3f << HNS3_RXD_GRO_COUNT_S)
80 #define HNS3_RXD_GRO_FIXID_B                    30
81 #define HNS3_RXD_GRO_ECN_B                      31
82
83 #define HNS3_RXD_ODMAC_S                        0
84 #define HNS3_RXD_ODMAC_M                        (0x3 << HNS3_RXD_ODMAC_S)
85 #define HNS3_RXD_OVLAN_S                        2
86 #define HNS3_RXD_OVLAN_M                        (0x3 << HNS3_RXD_OVLAN_S)
87 #define HNS3_RXD_OL3ID_S                        4
88 #define HNS3_RXD_OL3ID_M                        (0xf << HNS3_RXD_OL3ID_S)
89 #define HNS3_RXD_OL4ID_S                        8
90 #define HNS3_RXD_OL4ID_M                        (0xf << HNS3_RXD_OL4ID_S)
91 #define HNS3_RXD_PTYPE_S                        4
92 #define HNS3_RXD_PTYPE_M                        (0xff << HNS3_RXD_PTYPE_S)
93 #define HNS3_RXD_FBHI_S                         12
94 #define HNS3_RXD_FBHI_M                         (0x3 << HNS3_RXD_FBHI_S)
95 #define HNS3_RXD_FBLI_S                         14
96 #define HNS3_RXD_FBLI_M                         (0x3 << HNS3_RXD_FBLI_S)
97
98 #define HNS3_RXD_BDTYPE_S                       0
99 #define HNS3_RXD_BDTYPE_M                       (0xf << HNS3_RXD_BDTYPE_S)
100 #define HNS3_RXD_VLD_B                          4
101 #define HNS3_RXD_UDP0_B                         5
102 #define HNS3_RXD_EXTEND_B                       7
103 #define HNS3_RXD_FE_B                           8
104 #define HNS3_RXD_LUM_B                          9
105 #define HNS3_RXD_CRCP_B                         10
106 #define HNS3_RXD_L3L4P_B                        11
107 #define HNS3_RXD_TSIND_S                        12
108 #define HNS3_RXD_TSIND_M                        (0x7 << HNS3_RXD_TSIND_S)
109
110 #define HNS3_RXD_TS_VLD_B                       14
111 #define HNS3_RXD_LKBK_B                         15
112 #define HNS3_RXD_GRO_SIZE_S                     16
113 #define HNS3_RXD_GRO_SIZE_M                     (0x3fff << HNS3_RXD_GRO_SIZE_S)
114
115 #define HNS3_TXD_L3T_S                          0
116 #define HNS3_TXD_L3T_M                          (0x3 << HNS3_TXD_L3T_S)
117 #define HNS3_TXD_L4T_S                          2
118 #define HNS3_TXD_L4T_M                          (0x3 << HNS3_TXD_L4T_S)
119 #define HNS3_TXD_L3CS_B                         4
120 #define HNS3_TXD_L4CS_B                         5
121 #define HNS3_TXD_VLAN_B                         6
122 #define HNS3_TXD_TSO_B                          7
123
124 #define HNS3_TXD_L2LEN_S                        8
125 #define HNS3_TXD_L2LEN_M                        (0xff << HNS3_TXD_L2LEN_S)
126 #define HNS3_TXD_L3LEN_S                        16
127 #define HNS3_TXD_L3LEN_M                        (0xff << HNS3_TXD_L3LEN_S)
128 #define HNS3_TXD_L4LEN_S                        24
129 #define HNS3_TXD_L4LEN_M                        (0xffUL << HNS3_TXD_L4LEN_S)
130
131 #define HNS3_TXD_OL3T_S                         0
132 #define HNS3_TXD_OL3T_M                         (0x3 << HNS3_TXD_OL3T_S)
133 #define HNS3_TXD_OVLAN_B                        2
134 #define HNS3_TXD_MACSEC_B                       3
135 #define HNS3_TXD_TUNTYPE_S                      4
136 #define HNS3_TXD_TUNTYPE_M                      (0xf << HNS3_TXD_TUNTYPE_S)
137
138 #define HNS3_TXD_BDTYPE_S                       0
139 #define HNS3_TXD_BDTYPE_M                       (0xf << HNS3_TXD_BDTYPE_S)
140 #define HNS3_TXD_FE_B                           4
141 #define HNS3_TXD_SC_S                           5
142 #define HNS3_TXD_SC_M                           (0x3 << HNS3_TXD_SC_S)
143 #define HNS3_TXD_EXTEND_B                       7
144 #define HNS3_TXD_VLD_B                          8
145 #define HNS3_TXD_RI_B                           9
146 #define HNS3_TXD_RA_B                           10
147 #define HNS3_TXD_TSYN_B                         11
148 #define HNS3_TXD_DECTTL_S                       12
149 #define HNS3_TXD_DECTTL_M                       (0xf << HNS3_TXD_DECTTL_S)
150
151 #define HNS3_TXD_MSS_S                          0
152 #define HNS3_TXD_MSS_M                          (0x3fff << HNS3_TXD_MSS_S)
153
154 #define HNS3_TXD_OL4CS_B                        22
155 #define HNS3_L2_LEN_UNIT                        1UL
156 #define HNS3_L3_LEN_UNIT                        2UL
157 #define HNS3_L4_LEN_UNIT                        2UL
158
159 #define HNS3_TXD_DEFAULT_BDTYPE         0
160 #define HNS3_TXD_VLD_CMD                (0x1 << HNS3_TXD_VLD_B)
161 #define HNS3_TXD_FE_CMD                 (0x1 << HNS3_TXD_FE_B)
162 #define HNS3_TXD_DEFAULT_VLD_FE_BDTYPE          \
163                 (HNS3_TXD_VLD_CMD | HNS3_TXD_FE_CMD | HNS3_TXD_DEFAULT_BDTYPE)
164 #define HNS3_TXD_SEND_SIZE_SHIFT        16
165
166 enum hns3_pkt_l2t_type {
167         HNS3_L2_TYPE_UNICAST,
168         HNS3_L2_TYPE_MULTICAST,
169         HNS3_L2_TYPE_BROADCAST,
170         HNS3_L2_TYPE_INVALID,
171 };
172
173 enum hns3_pkt_l3t_type {
174         HNS3_L3T_NONE,
175         HNS3_L3T_IPV6,
176         HNS3_L3T_IPV4,
177         HNS3_L3T_RESERVED
178 };
179
180 enum hns3_pkt_l4t_type {
181         HNS3_L4T_UNKNOWN,
182         HNS3_L4T_TCP,
183         HNS3_L4T_UDP,
184         HNS3_L4T_SCTP
185 };
186
187 enum hns3_pkt_ol3t_type {
188         HNS3_OL3T_NONE,
189         HNS3_OL3T_IPV6,
190         HNS3_OL3T_IPV4_NO_CSUM,
191         HNS3_OL3T_IPV4_CSUM
192 };
193
194 enum hns3_pkt_tun_type {
195         HNS3_TUN_NONE,
196         HNS3_TUN_MAC_IN_UDP,
197         HNS3_TUN_NVGRE,
198         HNS3_TUN_OTHER
199 };
200
201 /* hardware spec ring buffer format */
202 struct hns3_desc {
203         union {
204                 uint64_t addr;
205                 uint64_t timestamp;
206
207                 struct {
208                         uint32_t addr0;
209                         uint32_t addr1;
210                 };
211         };
212         union {
213                 struct {
214                         uint16_t vlan_tag;
215                         uint16_t send_size;
216                         union {
217                                 /*
218                                  * L3T | L4T | L3CS | L4CS | VLAN | TSO |
219                                  * L2_LEN
220                                  */
221                                 uint32_t type_cs_vlan_tso_len;
222                                 struct {
223                                         uint8_t type_cs_vlan_tso;
224                                         uint8_t l2_len;
225                                         uint8_t l3_len;
226                                         uint8_t l4_len;
227                                 };
228                         };
229                         uint16_t outer_vlan_tag;
230                         uint16_t tv;
231                         union {
232                                 /* OL3T | OVALAN | MACSEC */
233                                 uint32_t ol_type_vlan_len_msec;
234                                 struct {
235                                         uint8_t ol_type_vlan_msec;
236                                         uint8_t ol2_len;
237                                         uint8_t ol3_len;
238                                         uint8_t ol4_len;
239                                 };
240                         };
241
242                         uint32_t paylen_fd_dop_ol4cs;
243                         uint16_t tp_fe_sc_vld_ra_ri;
244                         uint16_t mss;
245                 } tx;
246
247                 struct {
248                         uint32_t l234_info;
249                         uint16_t pkt_len;
250                         uint16_t size;
251                         uint32_t rss_hash;
252                         uint16_t fd_id;
253                         uint16_t vlan_tag;
254                         union {
255                                 uint32_t ol_info;
256                                 struct {
257                                         uint16_t o_dm_vlan_id_fb;
258                                         uint16_t ot_vlan_tag;
259                                 };
260                         };
261                         union {
262                                 uint32_t bd_base_info;
263                                 struct {
264                                         uint16_t bdtype_vld_udp0;
265                                         uint16_t fe_lum_crcp_l3l4p;
266                                 };
267                         };
268                 } rx;
269         };
270 } __rte_packed;
271
272 struct hns3_entry {
273         struct rte_mbuf *mbuf;
274 };
275
276 struct hns3_rx_basic_stats {
277         uint64_t packets;
278         uint64_t bytes;
279         uint64_t errors;
280 };
281
282 struct hns3_rx_dfx_stats {
283         uint64_t l3_csum_errors;
284         uint64_t l4_csum_errors;
285         uint64_t ol3_csum_errors;
286         uint64_t ol4_csum_errors;
287 };
288
289 struct hns3_rx_bd_errors_stats {
290         uint64_t l2_errors;
291         uint64_t pkt_len_errors;
292 };
293
294 struct hns3_rx_queue {
295         void *io_base;
296         volatile void *io_head_reg;
297         struct hns3_adapter *hns;
298         struct hns3_ptype_table *ptype_tbl;
299         struct rte_mempool *mb_pool;
300         struct hns3_desc *rx_ring;
301         uint64_t rx_ring_phys_addr; /* RX ring DMA address */
302         const struct rte_memzone *mz;
303         struct hns3_entry *sw_ring;
304         struct rte_mbuf *pkt_first_seg;
305         struct rte_mbuf *pkt_last_seg;
306
307         uint16_t queue_id;
308         uint16_t port_id;
309         uint16_t nb_rx_desc;
310         uint16_t rx_buf_len;
311         /*
312          * threshold for the number of BDs waited to passed to hardware. If the
313          * number exceeds the threshold, driver will pass these BDs to hardware.
314          */
315         uint16_t rx_free_thresh;
316         uint16_t next_to_use;    /* index of next BD to be polled */
317         uint16_t rx_free_hold;   /* num of BDs waited to passed to hardware */
318         uint16_t rx_rearm_start; /* index of BD that driver re-arming from */
319         uint16_t rx_rearm_nb;    /* number of remaining BDs to be re-armed */
320
321         /* 4 if DEV_RX_OFFLOAD_KEEP_CRC offload set, 0 otherwise */
322         uint8_t crc_len;
323
324         bool rx_deferred_start; /* don't start this queue in dev start */
325         bool configured;        /* indicate if rx queue has been configured */
326         /*
327          * Indicate whether ignore the outer VLAN field in the Rx BD reported
328          * by the Hardware. Because the outer VLAN is the PVID if the PVID is
329          * set for some version of hardware network engine whose vlan mode is
330          * HNS3_SW_SHIFT_AND_DISCARD_MODE, such as kunpeng 920. And this VLAN
331          * should not be transitted to the upper-layer application. For hardware
332          * network engine whose vlan mode is HNS3_HW_SHIFT_AND_DISCARD_MODE,
333          * such as kunpeng 930, PVID will not be reported to the BDs. So, PMD
334          * driver does not need to perform PVID-related operation in Rx. At this
335          * point, the pvid_sw_discard_en will be false.
336          */
337         bool pvid_sw_discard_en;
338         bool ptype_en;          /* indicate if the ptype field enabled */
339         bool enabled;           /* indicate if Rx queue has been enabled */
340
341         struct hns3_rx_basic_stats basic_stats;
342         /* DFX statistics that driver does not need to discard packets */
343         struct hns3_rx_dfx_stats dfx_stats;
344         /* Error statistics that driver needs to discard packets */
345         struct hns3_rx_bd_errors_stats err_stats;
346
347         struct rte_mbuf *bulk_mbuf[HNS3_BULK_ALLOC_MBUF_NUM];
348         uint16_t bulk_mbuf_num;
349
350         /* offset_table: used for vector, to solve execute re-order problem */
351         uint8_t offset_table[HNS3_VECTOR_RX_OFFSET_TABLE_LEN + 1];
352         uint64_t mbuf_initializer; /* value to init mbufs used with vector rx */
353         struct rte_mbuf fake_mbuf; /* fake mbuf used with vector rx */
354 };
355
356 struct hns3_tx_basic_stats {
357         uint64_t packets;
358         uint64_t bytes;
359 };
360
361 /*
362  * The following items are used for the abnormal errors statistics in
363  * the Tx datapath. When upper level application calls the
364  * rte_eth_tx_burst API function to send multiple packets at a time with
365  * burst mode based on hns3 network engine, there are some abnormal
366  * conditions that cause the driver to fail to operate the hardware to
367  * send packets correctly.
368  * Note: When using burst mode to call the rte_eth_tx_burst API function
369  * to send multiple packets at a time. When the first abnormal error is
370  * detected, add one to the relevant error statistics item, and then
371  * exit the loop of sending multiple packets of the function. That is to
372  * say, even if there are multiple packets in which abnormal errors may
373  * be detected in the burst, the relevant error statistics in the driver
374  * will only be increased by one.
375  * The detail description of the Tx abnormal errors statistic items as
376  * below:
377  *  - over_length_pkt_cnt
378  *     Total number of greater than HNS3_MAX_FRAME_LEN the driver
379  *     supported.
380  *
381  * - exceed_limit_bd_pkt_cnt
382  *     Total number of exceeding the hardware limited bd which process
383  *     a packet needed bd numbers.
384  *
385  * - exceed_limit_bd_reassem_fail
386  *     Total number of exceeding the hardware limited bd fail which
387  *     process a packet needed bd numbers and reassemble fail.
388  *
389  * - unsupported_tunnel_pkt_cnt
390  *     Total number of unsupported tunnel packet. The unsupported tunnel
391  *     type: vxlan_gpe, gtp, ipip and MPLSINUDP, MPLSINUDP is a packet
392  *     with MPLS-in-UDP RFC 7510 header.
393  *
394  * - queue_full_cnt
395  *     Total count which the available bd numbers in current bd queue is
396  *     less than the bd numbers with the pkt process needed.
397  *
398  * - pkt_padding_fail_cnt
399  *     Total count which the packet length is less than minimum packet
400  *     length(struct hns3_tx_queue::min_tx_pkt_len) supported by
401  *     hardware in Tx direction and fail to be appended with 0.
402  */
403 struct hns3_tx_dfx_stats {
404         uint64_t over_length_pkt_cnt;
405         uint64_t exceed_limit_bd_pkt_cnt;
406         uint64_t exceed_limit_bd_reassem_fail;
407         uint64_t unsupported_tunnel_pkt_cnt;
408         uint64_t queue_full_cnt;
409         uint64_t pkt_padding_fail_cnt;
410 };
411
412 struct hns3_tx_queue {
413         void *io_base;
414         volatile void *io_tail_reg;
415         struct hns3_adapter *hns;
416         struct hns3_desc *tx_ring;
417         uint64_t tx_ring_phys_addr; /* TX ring DMA address */
418         const struct rte_memzone *mz;
419         struct hns3_entry *sw_ring;
420
421         uint16_t queue_id;
422         uint16_t port_id;
423         uint16_t nb_tx_desc;
424         /*
425          * index of next BD whose corresponding rte_mbuf can be released by
426          * driver.
427          */
428         uint16_t next_to_clean;
429         /* index of next BD to be filled by driver to send packet */
430         uint16_t next_to_use;
431         /* num of remaining BDs ready to be filled by driver to send packet */
432         uint16_t tx_bd_ready;
433
434         /* threshold for free tx buffer if available BDs less than this value */
435         uint16_t tx_free_thresh;
436
437         /*
438          * For better performance in tx datapath, releasing mbuf in batches is
439          * required.
440          * Only checking the VLD bit of the last descriptor in a batch of the
441          * thresh descriptors does not mean that these descriptors are all sent
442          * by hardware successfully. So we need to check that the VLD bits of
443          * all descriptors are cleared. and then free all mbufs in the batch.
444          * - tx_rs_thresh
445          *   Number of mbufs released at a time.
446          *
447          * - free
448          *   Tx mbuf free array used for preserving temporarily address of mbuf
449          *   released back to mempool, when releasing mbuf in batches.
450          */
451         uint16_t tx_rs_thresh;
452         struct rte_mbuf **free;
453
454         /*
455          * tso mode.
456          * value range:
457          *      HNS3_TSO_SW_CAL_PSEUDO_H_CSUM/HNS3_TSO_HW_CAL_PSEUDO_H_CSUM
458          *
459          *  - HNS3_TSO_SW_CAL_PSEUDO_H_CSUM
460          *     In this mode, because of the hardware constraint, network driver
461          *     software need erase the L4 len value of the TCP pseudo header
462          *     and recalculate the TCP pseudo header checksum of packets that
463          *     need TSO.
464          *
465          *  - HNS3_TSO_HW_CAL_PSEUDO_H_CSUM
466          *     In this mode, hardware support recalculate the TCP pseudo header
467          *     checksum of packets that need TSO, so network driver software
468          *     not need to recalculate it.
469          */
470         uint8_t tso_mode;
471         /*
472          * udp checksum mode.
473          * value range:
474          *      HNS3_SPECIAL_PORT_HW_CKSUM_MODE/HNS3_SPECIAL_PORT_SW_CKSUM_MODE
475          *
476          *  - HNS3_SPECIAL_PORT_SW_CKSUM_MODE
477          *     In this mode, HW can not do checksum for special UDP port like
478          *     4789, 4790, 6081 for non-tunnel UDP packets and UDP tunnel
479          *     packets without the PKT_TX_TUNEL_MASK in the mbuf. So, PMD need
480          *     do the checksum for these packets to avoid a checksum error.
481          *
482          *  - HNS3_SPECIAL_PORT_HW_CKSUM_MODE
483          *     In this mode, HW does not have the preceding problems and can
484          *     directly calculate the checksum of these UDP packets.
485          */
486         uint8_t udp_cksum_mode;
487         /*
488          * The minimum length of the packet supported by hardware in the Tx
489          * direction.
490          */
491         uint32_t min_tx_pkt_len;
492
493         uint8_t max_non_tso_bd_num; /* max BD number of one non-TSO packet */
494         bool tx_deferred_start; /* don't start this queue in dev start */
495         bool configured;        /* indicate if tx queue has been configured */
496         /*
497          * Indicate whether add the vlan_tci of the mbuf to the inner VLAN field
498          * of Tx BD. Because the outer VLAN will always be the PVID when the
499          * PVID is set and for some version of hardware network engine whose
500          * vlan mode is HNS3_SW_SHIFT_AND_DISCARD_MODE, such as kunpeng 920, the
501          * PVID will overwrite the outer VLAN field of Tx BD. For the hardware
502          * network engine whose vlan mode is HNS3_HW_SHIFT_AND_DISCARD_MODE,
503          * such as kunpeng 930, if the PVID is set, the hardware will shift the
504          * VLAN field automatically. So, PMD driver does not need to do
505          * PVID-related operations in Tx. And pvid_sw_shift_en will be false at
506          * this point.
507          */
508         bool pvid_sw_shift_en;
509         bool enabled;           /* indicate if Tx queue has been enabled */
510
511         struct hns3_tx_basic_stats basic_stats;
512         struct hns3_tx_dfx_stats dfx_stats;
513 };
514
515 #define HNS3_GET_TX_QUEUE_PEND_BD_NUM(txq) \
516                 ((txq)->nb_tx_desc - 1 - (txq)->tx_bd_ready)
517
518 struct hns3_queue_info {
519         const char *type;   /* point to queue memory name */
520         const char *ring_name;  /* point to hardware ring name */
521         uint16_t idx;
522         uint16_t nb_desc;
523         unsigned int socket_id;
524 };
525
526 #define HNS3_TX_CKSUM_OFFLOAD_MASK ( \
527         PKT_TX_OUTER_UDP_CKSUM | \
528         PKT_TX_OUTER_IP_CKSUM | \
529         PKT_TX_IP_CKSUM | \
530         PKT_TX_TCP_SEG | \
531         PKT_TX_L4_MASK)
532
533 enum hns3_cksum_status {
534         HNS3_CKSUM_NONE = 0,
535         HNS3_L3_CKSUM_ERR = 1,
536         HNS3_L4_CKSUM_ERR = 2,
537         HNS3_OUTER_L3_CKSUM_ERR = 4,
538         HNS3_OUTER_L4_CKSUM_ERR = 8
539 };
540
541 extern uint64_t hns3_timestamp_rx_dynflag;
542 extern int hns3_timestamp_dynfield_offset;
543
544 static inline int
545 hns3_handle_bdinfo(struct hns3_rx_queue *rxq, struct rte_mbuf *rxm,
546                    uint32_t bd_base_info, uint32_t l234_info,
547                    uint32_t *cksum_err)
548 {
549 #define L2E_TRUNC_ERR_FLAG      (BIT(HNS3_RXD_L2E_B) | \
550                                  BIT(HNS3_RXD_TRUNCATE_B))
551 #define CHECKSUM_ERR_FLAG       (BIT(HNS3_RXD_L3E_B) | \
552                                  BIT(HNS3_RXD_L4E_B) | \
553                                  BIT(HNS3_RXD_OL3E_B) | \
554                                  BIT(HNS3_RXD_OL4E_B))
555
556         uint32_t tmp = 0;
557
558         /*
559          * If packet len bigger than mtu when recv with no-scattered algorithm,
560          * the first n bd will without FE bit, we need process this sisution.
561          * Note: we don't need add statistic counter because latest BD which
562          *       with FE bit will mark HNS3_RXD_L2E_B bit.
563          */
564         if (unlikely((bd_base_info & BIT(HNS3_RXD_FE_B)) == 0))
565                 return -EINVAL;
566
567         if (unlikely((l234_info & L2E_TRUNC_ERR_FLAG) || rxm->pkt_len == 0)) {
568                 if (l234_info & BIT(HNS3_RXD_L2E_B))
569                         rxq->err_stats.l2_errors++;
570                 else
571                         rxq->err_stats.pkt_len_errors++;
572                 return -EINVAL;
573         }
574
575         if (bd_base_info & BIT(HNS3_RXD_L3L4P_B)) {
576                 if (likely((l234_info & CHECKSUM_ERR_FLAG) == 0)) {
577                         *cksum_err = 0;
578                         return 0;
579                 }
580
581                 if (unlikely(l234_info & BIT(HNS3_RXD_L3E_B))) {
582                         rxm->ol_flags |= PKT_RX_IP_CKSUM_BAD;
583                         rxq->dfx_stats.l3_csum_errors++;
584                         tmp |= HNS3_L3_CKSUM_ERR;
585                 }
586
587                 if (unlikely(l234_info & BIT(HNS3_RXD_L4E_B))) {
588                         rxm->ol_flags |= PKT_RX_L4_CKSUM_BAD;
589                         rxq->dfx_stats.l4_csum_errors++;
590                         tmp |= HNS3_L4_CKSUM_ERR;
591                 }
592
593                 if (unlikely(l234_info & BIT(HNS3_RXD_OL3E_B))) {
594                         rxq->dfx_stats.ol3_csum_errors++;
595                         tmp |= HNS3_OUTER_L3_CKSUM_ERR;
596                 }
597
598                 if (unlikely(l234_info & BIT(HNS3_RXD_OL4E_B))) {
599                         rxm->ol_flags |= PKT_RX_OUTER_L4_CKSUM_BAD;
600                         rxq->dfx_stats.ol4_csum_errors++;
601                         tmp |= HNS3_OUTER_L4_CKSUM_ERR;
602                 }
603         }
604         *cksum_err = tmp;
605
606         return 0;
607 }
608
609 static inline void
610 hns3_rx_set_cksum_flag(struct rte_mbuf *rxm, const uint64_t packet_type,
611                        const uint32_t cksum_err)
612 {
613         if (unlikely((packet_type & RTE_PTYPE_TUNNEL_MASK))) {
614                 if (likely(packet_type & RTE_PTYPE_INNER_L3_MASK) &&
615                     (cksum_err & HNS3_L3_CKSUM_ERR) == 0)
616                         rxm->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
617                 if (likely(packet_type & RTE_PTYPE_INNER_L4_MASK) &&
618                     (cksum_err & HNS3_L4_CKSUM_ERR) == 0)
619                         rxm->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
620                 if (likely(packet_type & RTE_PTYPE_L4_MASK) &&
621                     (cksum_err & HNS3_OUTER_L4_CKSUM_ERR) == 0)
622                         rxm->ol_flags |= PKT_RX_OUTER_L4_CKSUM_GOOD;
623         } else {
624                 if (likely(packet_type & RTE_PTYPE_L3_MASK) &&
625                     (cksum_err & HNS3_L3_CKSUM_ERR) == 0)
626                         rxm->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
627                 if (likely(packet_type & RTE_PTYPE_L4_MASK) &&
628                     (cksum_err & HNS3_L4_CKSUM_ERR) == 0)
629                         rxm->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
630         }
631 }
632
633 static inline uint32_t
634 hns3_rx_calc_ptype(struct hns3_rx_queue *rxq, const uint32_t l234_info,
635                    const uint32_t ol_info)
636 {
637         const struct hns3_ptype_table * const ptype_tbl = rxq->ptype_tbl;
638         uint32_t ol3id, ol4id;
639         uint32_t l3id, l4id;
640         uint32_t ptype;
641
642         if (rxq->ptype_en) {
643                 ptype = hns3_get_field(ol_info, HNS3_RXD_PTYPE_M,
644                                        HNS3_RXD_PTYPE_S);
645                 return ptype_tbl->ptype[ptype];
646         }
647
648         ol4id = hns3_get_field(ol_info, HNS3_RXD_OL4ID_M, HNS3_RXD_OL4ID_S);
649         ol3id = hns3_get_field(ol_info, HNS3_RXD_OL3ID_M, HNS3_RXD_OL3ID_S);
650         l3id = hns3_get_field(l234_info, HNS3_RXD_L3ID_M, HNS3_RXD_L3ID_S);
651         l4id = hns3_get_field(l234_info, HNS3_RXD_L4ID_M, HNS3_RXD_L4ID_S);
652
653         if (unlikely(ptype_tbl->ol4table[ol4id]))
654                 return ptype_tbl->inner_l3table[l3id] |
655                         ptype_tbl->inner_l4table[l4id] |
656                         ptype_tbl->ol3table[ol3id] |
657                         ptype_tbl->ol4table[ol4id];
658         else
659                 return ptype_tbl->l3table[l3id] | ptype_tbl->l4table[l4id];
660 }
661
662 void hns3_dev_rx_queue_release(void *queue);
663 void hns3_dev_tx_queue_release(void *queue);
664 void hns3_free_all_queues(struct rte_eth_dev *dev);
665 int hns3_reset_all_tqps(struct hns3_adapter *hns);
666 void hns3_dev_all_rx_queue_intr_enable(struct hns3_hw *hw, bool en);
667 int hns3_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id);
668 int hns3_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id);
669 void hns3_enable_all_queues(struct hns3_hw *hw, bool en);
670 int hns3_init_queues(struct hns3_adapter *hns, bool reset_queue);
671 void hns3_start_tqps(struct hns3_hw *hw);
672 void hns3_stop_tqps(struct hns3_hw *hw);
673 int hns3_rxq_iterate(struct rte_eth_dev *dev,
674                  int (*callback)(struct hns3_rx_queue *, void *), void *arg);
675 void hns3_dev_release_mbufs(struct hns3_adapter *hns);
676 int hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
677                         unsigned int socket, const struct rte_eth_rxconf *conf,
678                         struct rte_mempool *mp);
679 int hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
680                         unsigned int socket, const struct rte_eth_txconf *conf);
681 uint32_t hns3_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id);
682 int hns3_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id);
683 int hns3_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id);
684 int hns3_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id);
685 int hns3_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id);
686 uint16_t hns3_recv_pkts_simple(void *rx_queue, struct rte_mbuf **rx_pkts,
687                                 uint16_t nb_pkts);
688 uint16_t hns3_recv_scattered_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
689                                   uint16_t nb_pkts);
690 uint16_t hns3_recv_pkts_vec(void *rx_queue, struct rte_mbuf **rx_pkts,
691                             uint16_t nb_pkts);
692 uint16_t hns3_recv_pkts_vec_sve(void *rx_queue, struct rte_mbuf **rx_pkts,
693                                 uint16_t nb_pkts);
694 int hns3_rx_burst_mode_get(struct rte_eth_dev *dev,
695                            __rte_unused uint16_t queue_id,
696                            struct rte_eth_burst_mode *mode);
697 int hns3_rx_check_vec_support(struct rte_eth_dev *dev);
698 uint16_t hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
699                         uint16_t nb_pkts);
700 uint16_t hns3_xmit_pkts_simple(void *tx_queue, struct rte_mbuf **tx_pkts,
701                                uint16_t nb_pkts);
702 uint16_t hns3_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
703                         uint16_t nb_pkts);
704 uint16_t hns3_xmit_pkts_vec(void *tx_queue, struct rte_mbuf **tx_pkts,
705                                                         uint16_t nb_pkts);
706 uint16_t hns3_xmit_pkts_vec_sve(void *tx_queue, struct rte_mbuf **tx_pkts,
707                                 uint16_t nb_pkts);
708 int hns3_tx_burst_mode_get(struct rte_eth_dev *dev,
709                            __rte_unused uint16_t queue_id,
710                            struct rte_eth_burst_mode *mode);
711 const uint32_t *hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev);
712 void hns3_init_rx_ptype_tble(struct rte_eth_dev *dev);
713 void hns3_set_rxtx_function(struct rte_eth_dev *eth_dev);
714 uint32_t hns3_get_tqp_intr_reg_offset(uint16_t tqp_intr_id);
715 void hns3_set_queue_intr_gl(struct hns3_hw *hw, uint16_t queue_id,
716                             uint8_t gl_idx, uint16_t gl_value);
717 void hns3_set_queue_intr_rl(struct hns3_hw *hw, uint16_t queue_id,
718                             uint16_t rl_value);
719 void hns3_set_queue_intr_ql(struct hns3_hw *hw, uint16_t queue_id,
720                             uint16_t ql_value);
721 int hns3_set_fake_rx_or_tx_queues(struct rte_eth_dev *dev, uint16_t nb_rx_q,
722                                   uint16_t nb_tx_q);
723 int hns3_config_gro(struct hns3_hw *hw, bool en);
724 int hns3_restore_gro_conf(struct hns3_hw *hw);
725 void hns3_update_all_queues_pvid_proc_en(struct hns3_hw *hw);
726 void hns3_rx_scattered_reset(struct rte_eth_dev *dev);
727 void hns3_rx_scattered_calc(struct rte_eth_dev *dev);
728 int hns3_rx_check_vec_support(struct rte_eth_dev *dev);
729 int hns3_tx_check_vec_support(struct rte_eth_dev *dev);
730 void hns3_rxq_vec_setup(struct hns3_rx_queue *rxq);
731 void hns3_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
732                        struct rte_eth_rxq_info *qinfo);
733 void hns3_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
734                        struct rte_eth_txq_info *qinfo);
735 uint32_t hns3_get_tqp_reg_offset(uint16_t idx);
736 int hns3_start_all_txqs(struct rte_eth_dev *dev);
737 int hns3_start_all_rxqs(struct rte_eth_dev *dev);
738 void hns3_stop_all_txqs(struct rte_eth_dev *dev);
739 void hns3_restore_tqp_enable_state(struct hns3_hw *hw);
740 int hns3_tx_done_cleanup(void *txq, uint32_t free_cnt);
741 void hns3_enable_rxd_adv_layout(struct hns3_hw *hw);
742 int hns3_dev_rx_descriptor_status(void *rx_queue, uint16_t offset);
743 int hns3_dev_tx_descriptor_status(void *tx_queue, uint16_t offset);
744
745 #endif /* _HNS3_RXTX_H_ */