c1a34e2f691ef87923ae03a3d14ee37345db8473
[dpdk.git] / drivers / net / hns3 / hns3_rxtx.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2019 Hisilicon Limited.
3  */
4
5 #ifndef _HNS3_RXTX_H_
6 #define _HNS3_RXTX_H_
7
8 #define HNS3_MIN_RING_DESC      64
9 #define HNS3_MAX_RING_DESC      32768
10 #define HNS3_DEFAULT_RING_DESC  1024
11 #define HNS3_ALIGN_RING_DESC    32
12 #define HNS3_RING_BASE_ALIGN    128
13 #define HNS3_DEFAULT_RX_FREE_THRESH     32
14
15 #define HNS3_512_BD_BUF_SIZE    512
16 #define HNS3_1K_BD_BUF_SIZE     1024
17 #define HNS3_2K_BD_BUF_SIZE     2048
18 #define HNS3_4K_BD_BUF_SIZE     4096
19
20 #define HNS3_MIN_BD_BUF_SIZE    HNS3_512_BD_BUF_SIZE
21 #define HNS3_MAX_BD_BUF_SIZE    HNS3_4K_BD_BUF_SIZE
22
23 #define HNS3_BD_SIZE_512_TYPE                   0
24 #define HNS3_BD_SIZE_1024_TYPE                  1
25 #define HNS3_BD_SIZE_2048_TYPE                  2
26 #define HNS3_BD_SIZE_4096_TYPE                  3
27
28 #define HNS3_RX_FLAG_VLAN_PRESENT               0x1
29 #define HNS3_RX_FLAG_L3ID_IPV4                  0x0
30 #define HNS3_RX_FLAG_L3ID_IPV6                  0x1
31 #define HNS3_RX_FLAG_L4ID_UDP                   0x0
32 #define HNS3_RX_FLAG_L4ID_TCP                   0x1
33
34 #define HNS3_RXD_DMAC_S                         0
35 #define HNS3_RXD_DMAC_M                         (0x3 << HNS3_RXD_DMAC_S)
36 #define HNS3_RXD_VLAN_S                         2
37 #define HNS3_RXD_VLAN_M                         (0x3 << HNS3_RXD_VLAN_S)
38 #define HNS3_RXD_L3ID_S                         4
39 #define HNS3_RXD_L3ID_M                         (0xf << HNS3_RXD_L3ID_S)
40 #define HNS3_RXD_L4ID_S                         8
41 #define HNS3_RXD_L4ID_M                         (0xf << HNS3_RXD_L4ID_S)
42 #define HNS3_RXD_FRAG_B                         12
43 #define HNS3_RXD_STRP_TAGP_S                    13
44 #define HNS3_RXD_STRP_TAGP_M                    (0x3 << HNS3_RXD_STRP_TAGP_S)
45
46 #define HNS3_RXD_L2E_B                          16
47 #define HNS3_RXD_L3E_B                          17
48 #define HNS3_RXD_L4E_B                          18
49 #define HNS3_RXD_TRUNCAT_B                      19
50 #define HNS3_RXD_HOI_B                          20
51 #define HNS3_RXD_DOI_B                          21
52 #define HNS3_RXD_OL3E_B                         22
53 #define HNS3_RXD_OL4E_B                         23
54 #define HNS3_RXD_GRO_COUNT_S                    24
55 #define HNS3_RXD_GRO_COUNT_M                    (0x3f << HNS3_RXD_GRO_COUNT_S)
56 #define HNS3_RXD_GRO_FIXID_B                    30
57 #define HNS3_RXD_GRO_ECN_B                      31
58
59 #define HNS3_RXD_ODMAC_S                        0
60 #define HNS3_RXD_ODMAC_M                        (0x3 << HNS3_RXD_ODMAC_S)
61 #define HNS3_RXD_OVLAN_S                        2
62 #define HNS3_RXD_OVLAN_M                        (0x3 << HNS3_RXD_OVLAN_S)
63 #define HNS3_RXD_OL3ID_S                        4
64 #define HNS3_RXD_OL3ID_M                        (0xf << HNS3_RXD_OL3ID_S)
65 #define HNS3_RXD_OL4ID_S                        8
66 #define HNS3_RXD_OL4ID_M                        (0xf << HNS3_RXD_OL4ID_S)
67 #define HNS3_RXD_FBHI_S                         12
68 #define HNS3_RXD_FBHI_M                         (0x3 << HNS3_RXD_FBHI_S)
69 #define HNS3_RXD_FBLI_S                         14
70 #define HNS3_RXD_FBLI_M                         (0x3 << HNS3_RXD_FBLI_S)
71
72 #define HNS3_RXD_BDTYPE_S                       0
73 #define HNS3_RXD_BDTYPE_M                       (0xf << HNS3_RXD_BDTYPE_S)
74 #define HNS3_RXD_VLD_B                          4
75 #define HNS3_RXD_UDP0_B                         5
76 #define HNS3_RXD_EXTEND_B                       7
77 #define HNS3_RXD_FE_B                           8
78 #define HNS3_RXD_LUM_B                          9
79 #define HNS3_RXD_CRCP_B                         10
80 #define HNS3_RXD_L3L4P_B                        11
81 #define HNS3_RXD_TSIND_S                        12
82 #define HNS3_RXD_TSIND_M                        (0x7 << HNS3_RXD_TSIND_S)
83 #define HNS3_RXD_LKBK_B                         15
84 #define HNS3_RXD_GRO_SIZE_S                     16
85 #define HNS3_RXD_GRO_SIZE_M                     (0x3fff << HNS3_RXD_GRO_SIZE_S)
86
87 #define HNS3_TXD_L3T_S                          0
88 #define HNS3_TXD_L3T_M                          (0x3 << HNS3_TXD_L3T_S)
89 #define HNS3_TXD_L4T_S                          2
90 #define HNS3_TXD_L4T_M                          (0x3 << HNS3_TXD_L4T_S)
91 #define HNS3_TXD_L3CS_B                         4
92 #define HNS3_TXD_L4CS_B                         5
93 #define HNS3_TXD_VLAN_B                         6
94 #define HNS3_TXD_TSO_B                          7
95
96 #define HNS3_TXD_L2LEN_S                        8
97 #define HNS3_TXD_L2LEN_M                        (0xff << HNS3_TXD_L2LEN_S)
98 #define HNS3_TXD_L3LEN_S                        16
99 #define HNS3_TXD_L3LEN_M                        (0xff << HNS3_TXD_L3LEN_S)
100 #define HNS3_TXD_L4LEN_S                        24
101 #define HNS3_TXD_L4LEN_M                        (0xffUL << HNS3_TXD_L4LEN_S)
102
103 #define HNS3_TXD_OL3T_S                         0
104 #define HNS3_TXD_OL3T_M                         (0x3 << HNS3_TXD_OL3T_S)
105 #define HNS3_TXD_OVLAN_B                        2
106 #define HNS3_TXD_MACSEC_B                       3
107 #define HNS3_TXD_TUNTYPE_S                      4
108 #define HNS3_TXD_TUNTYPE_M                      (0xf << HNS3_TXD_TUNTYPE_S)
109
110 #define HNS3_TXD_BDTYPE_S                       0
111 #define HNS3_TXD_BDTYPE_M                       (0xf << HNS3_TXD_BDTYPE_S)
112 #define HNS3_TXD_FE_B                           4
113 #define HNS3_TXD_SC_S                           5
114 #define HNS3_TXD_SC_M                           (0x3 << HNS3_TXD_SC_S)
115 #define HNS3_TXD_EXTEND_B                       7
116 #define HNS3_TXD_VLD_B                          8
117 #define HNS3_TXD_RI_B                           9
118 #define HNS3_TXD_RA_B                           10
119 #define HNS3_TXD_TSYN_B                         11
120 #define HNS3_TXD_DECTTL_S                       12
121 #define HNS3_TXD_DECTTL_M                       (0xf << HNS3_TXD_DECTTL_S)
122
123 #define HNS3_TXD_MSS_S                          0
124 #define HNS3_TXD_MSS_M                          (0x3fff << HNS3_TXD_MSS_S)
125
126 #define HNS3_L2_LEN_UNIT                        1UL
127 #define HNS3_L3_LEN_UNIT                        2UL
128 #define HNS3_L4_LEN_UNIT                        2UL
129
130 enum hns3_pkt_l2t_type {
131         HNS3_L2_TYPE_UNICAST,
132         HNS3_L2_TYPE_MULTICAST,
133         HNS3_L2_TYPE_BROADCAST,
134         HNS3_L2_TYPE_INVALID,
135 };
136
137 enum hns3_pkt_l3t_type {
138         HNS3_L3T_NONE,
139         HNS3_L3T_IPV6,
140         HNS3_L3T_IPV4,
141         HNS3_L3T_RESERVED
142 };
143
144 enum hns3_pkt_l4t_type {
145         HNS3_L4T_UNKNOWN,
146         HNS3_L4T_TCP,
147         HNS3_L4T_UDP,
148         HNS3_L4T_SCTP
149 };
150
151 enum hns3_pkt_ol3t_type {
152         HNS3_OL3T_NONE,
153         HNS3_OL3T_IPV6,
154         HNS3_OL3T_IPV4_NO_CSUM,
155         HNS3_OL3T_IPV4_CSUM
156 };
157
158 enum hns3_pkt_tun_type {
159         HNS3_TUN_NONE,
160         HNS3_TUN_MAC_IN_UDP,
161         HNS3_TUN_NVGRE,
162         HNS3_TUN_OTHER
163 };
164
165 /* hardware spec ring buffer format */
166 struct hns3_desc {
167         union {
168                 uint64_t addr;
169                 struct {
170                         uint32_t addr0;
171                         uint32_t addr1;
172                 };
173         };
174         union {
175                 struct {
176                         uint16_t vlan_tag;
177                         uint16_t send_size;
178                         union {
179                                 /*
180                                  * L3T | L4T | L3CS | L4CS | VLAN | TSO |
181                                  * L2_LEN
182                                  */
183                                 uint32_t type_cs_vlan_tso_len;
184                                 struct {
185                                         uint8_t type_cs_vlan_tso;
186                                         uint8_t l2_len;
187                                         uint8_t l3_len;
188                                         uint8_t l4_len;
189                                 };
190                         };
191                         uint16_t outer_vlan_tag;
192                         uint16_t tv;
193                         union {
194                                 /* OL3T | OVALAN | MACSEC */
195                                 uint32_t ol_type_vlan_len_msec;
196                                 struct {
197                                         uint8_t ol_type_vlan_msec;
198                                         uint8_t ol2_len;
199                                         uint8_t ol3_len;
200                                         uint8_t ol4_len;
201                                 };
202                         };
203
204                         uint32_t paylen;
205                         uint16_t tp_fe_sc_vld_ra_ri;
206                         uint16_t mss;
207                 } tx;
208
209                 struct {
210                         uint32_t l234_info;
211                         uint16_t pkt_len;
212                         uint16_t size;
213                         uint32_t rss_hash;
214                         uint16_t fd_id;
215                         uint16_t vlan_tag;
216                         union {
217                                 uint32_t ol_info;
218                                 struct {
219                                         uint16_t o_dm_vlan_id_fb;
220                                         uint16_t ot_vlan_tag;
221                                 };
222                         };
223                         uint32_t bd_base_info;
224                 } rx;
225         };
226 } __rte_packed;
227
228 struct hns3_entry {
229         struct rte_mbuf *mbuf;
230 };
231
232 struct hns3_rx_queue {
233         void *io_base;
234         volatile void *io_head_reg;
235         struct hns3_adapter *hns;
236         struct rte_mempool *mb_pool;
237         struct hns3_desc *rx_ring;
238         uint64_t rx_ring_phys_addr; /* RX ring DMA address */
239         const struct rte_memzone *mz;
240         struct hns3_entry *sw_ring;
241
242         struct rte_mbuf *pkt_first_seg;
243         struct rte_mbuf *pkt_last_seg;
244
245         uint16_t queue_id;
246         uint16_t port_id;
247         uint16_t nb_rx_desc;
248         uint16_t next_to_use;
249         uint16_t rx_buf_len;
250         /*
251          * threshold for the number of BDs waited to passed to hardware. If the
252          * number exceeds the threshold, driver will pass these BDs to hardware.
253          */
254         uint16_t rx_free_thresh;
255         uint16_t rx_free_hold;   /* num of BDs waited to passed to hardware */
256
257         /*
258          * port based vlan configuration state.
259          * value range: HNS3_PORT_BASE_VLAN_DISABLE / HNS3_PORT_BASE_VLAN_ENABLE
260          */
261         uint16_t pvid_state;
262
263         /* 4 if DEV_RX_OFFLOAD_KEEP_CRC offload set, 0 otherwise */
264         uint8_t crc_len;
265
266         bool rx_deferred_start; /* don't start this queue in dev start */
267         bool configured;        /* indicate if rx queue has been configured */
268
269         uint64_t l2_errors;
270         uint64_t pkt_len_errors;
271         uint64_t l3_csum_erros;
272         uint64_t l4_csum_erros;
273         uint64_t ol3_csum_erros;
274         uint64_t ol4_csum_erros;
275 };
276
277 struct hns3_tx_queue {
278         void *io_base;
279         struct hns3_adapter *hns;
280         struct hns3_desc *tx_ring;
281         uint64_t tx_ring_phys_addr; /* TX ring DMA address */
282         const struct rte_memzone *mz;
283         struct hns3_entry *sw_ring;
284
285         uint16_t queue_id;
286         uint16_t port_id;
287         uint16_t nb_tx_desc;
288         uint16_t next_to_clean;
289         uint16_t next_to_use;
290         uint16_t tx_bd_ready;
291
292         /*
293          * port based vlan configuration state.
294          * value range: HNS3_PORT_BASE_VLAN_DISABLE / HNS3_PORT_BASE_VLAN_ENABLE
295          */
296         uint16_t pvid_state;
297
298         /*
299          * The minimum length of the packet supported by hardware in the Tx
300          * direction.
301          */
302         uint32_t min_tx_pkt_len;
303
304         bool tx_deferred_start; /* don't start this queue in dev start */
305         bool configured;        /* indicate if tx queue has been configured */
306
307         /*
308          * The following items are used for the abnormal errors statistics in
309          * the Tx datapath. When upper level application calls the
310          * rte_eth_tx_burst API function to send multiple packets at a time with
311          * burst mode based on hns3 network engine, there are some abnormal
312          * conditions that cause the driver to fail to operate the hardware to
313          * send packets correctly.
314          * Note: When using burst mode to call the rte_eth_tx_burst API function
315          * to send multiple packets at a time. When the first abnormal error is
316          * detected, add one to the relevant error statistics item, and then
317          * exit the loop of sending multiple packets of the function. That is to
318          * say, even if there are multiple packets in which abnormal errors may
319          * be detected in the burst, the relevant error statistics in the driver
320          * will only be increased by one.
321          * The detail description of the Tx abnormal errors statistic items as
322          * below:
323          *  - over_length_pkt_cnt
324          *     Total number of greater than HNS3_MAX_FRAME_LEN the driver
325          *     supported.
326          *
327          * - exceed_limit_bd_pkt_cnt
328          *     Total number of exceeding the hardware limited bd which process
329          *     a packet needed bd numbers.
330          *
331          * - exceed_limit_bd_reassem_fail
332          *     Total number of exceeding the hardware limited bd fail which
333          *     process a packet needed bd numbers and reassemble fail.
334          *
335          * - unsupported_tunnel_pkt_cnt
336          *     Total number of unsupported tunnel packet. The unsupported tunnel
337          *     type: vxlan_gpe, gtp, ipip and MPLSINUDP, MPLSINUDP is a packet
338          *     with MPLS-in-UDP RFC 7510 header.
339          *
340          * - queue_full_cnt
341          *     Total count which the available bd numbers in current bd queue is
342          *     less than the bd numbers with the pkt process needed.
343          *
344          * - pkt_padding_fail_cnt
345          *     Total count which the packet length is less than minimum packet
346          *     length(struct hns3_tx_queue::min_tx_pkt_len) supported by
347          *     hardware in Tx direction and fail to be appended with 0.
348          */
349         uint64_t over_length_pkt_cnt;
350         uint64_t exceed_limit_bd_pkt_cnt;
351         uint64_t exceed_limit_bd_reassem_fail;
352         uint64_t unsupported_tunnel_pkt_cnt;
353         uint64_t queue_full_cnt;
354         uint64_t pkt_padding_fail_cnt;
355 };
356
357 struct hns3_queue_info {
358         const char *type;   /* point to queue memory name */
359         const char *ring_name;  /* point to hardware ring name */
360         uint16_t idx;
361         uint16_t nb_desc;
362         unsigned int socket_id;
363 };
364
365 #define HNS3_TX_CKSUM_OFFLOAD_MASK ( \
366         PKT_TX_OUTER_IPV6 | \
367         PKT_TX_OUTER_IPV4 | \
368         PKT_TX_OUTER_IP_CKSUM | \
369         PKT_TX_IPV6 | \
370         PKT_TX_IPV4 | \
371         PKT_TX_IP_CKSUM | \
372         PKT_TX_L4_MASK | \
373         PKT_TX_TUNNEL_MASK)
374
375 enum hns3_cksum_status {
376         HNS3_CKSUM_NONE = 0,
377         HNS3_L3_CKSUM_ERR = 1,
378         HNS3_L4_CKSUM_ERR = 2,
379         HNS3_OUTER_L3_CKSUM_ERR = 4,
380         HNS3_OUTER_L4_CKSUM_ERR = 8
381 };
382
383 void hns3_dev_rx_queue_release(void *queue);
384 void hns3_dev_tx_queue_release(void *queue);
385 void hns3_free_all_queues(struct rte_eth_dev *dev);
386 int hns3_reset_all_queues(struct hns3_adapter *hns);
387 void hns3_dev_all_rx_queue_intr_enable(struct hns3_hw *hw, bool en);
388 int hns3_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id);
389 int hns3_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id);
390 void hns3_enable_all_queues(struct hns3_hw *hw, bool en);
391 int hns3_start_queues(struct hns3_adapter *hns, bool reset_queue);
392 int hns3_stop_queues(struct hns3_adapter *hns, bool reset_queue);
393 void hns3_dev_release_mbufs(struct hns3_adapter *hns);
394 int hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
395                         unsigned int socket, const struct rte_eth_rxconf *conf,
396                         struct rte_mempool *mp);
397 int hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
398                         unsigned int socket, const struct rte_eth_txconf *conf);
399 uint16_t hns3_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts,
400                         uint16_t nb_pkts);
401 uint16_t hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
402                         uint16_t nb_pkts);
403 uint16_t hns3_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts,
404                         uint16_t nb_pkts);
405 const uint32_t *hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev);
406 void hns3_set_rxtx_function(struct rte_eth_dev *eth_dev);
407 void hns3_set_queue_intr_gl(struct hns3_hw *hw, uint16_t queue_id,
408                             uint8_t gl_idx, uint16_t gl_value);
409 void hns3_set_queue_intr_rl(struct hns3_hw *hw, uint16_t queue_id,
410                             uint16_t rl_value);
411 void hns3_set_queue_intr_ql(struct hns3_hw *hw, uint16_t queue_id,
412                             uint16_t ql_value);
413 int hns3_set_fake_rx_or_tx_queues(struct rte_eth_dev *dev, uint16_t nb_rx_q,
414                                   uint16_t nb_tx_q);
415 int hns3_config_gro(struct hns3_hw *hw, bool en);
416 int hns3_restore_gro_conf(struct hns3_hw *hw);
417 void hns3_update_all_queues_pvid_state(struct hns3_hw *hw);
418 void hns3_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
419                        struct rte_eth_rxq_info *qinfo);
420 void hns3_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
421                        struct rte_eth_txq_info *qinfo);
422 #endif /* _HNS3_RXTX_H_ */