net/hns3: fix Tx checksum for UDP packets with special port
[dpdk.git] / drivers / net / hns3 / hns3_rxtx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2019 Hisilicon Limited.
3  */
4
5 #include <rte_bus_pci.h>
6 #include <rte_common.h>
7 #include <rte_cycles.h>
8 #include <rte_geneve.h>
9 #include <rte_vxlan.h>
10 #include <ethdev_driver.h>
11 #include <rte_io.h>
12 #include <rte_net.h>
13 #include <rte_malloc.h>
14 #if defined(RTE_ARCH_ARM64) && defined(__ARM_FEATURE_SVE)
15 #include <rte_cpuflags.h>
16 #endif
17
18 #include "hns3_ethdev.h"
19 #include "hns3_rxtx.h"
20 #include "hns3_regs.h"
21 #include "hns3_logs.h"
22
23 #define HNS3_CFG_DESC_NUM(num)  ((num) / 8 - 1)
24 #define HNS3_RX_RING_PREFETCTH_MASK     3
25
26 static void
27 hns3_rx_queue_release_mbufs(struct hns3_rx_queue *rxq)
28 {
29         uint16_t i;
30
31         /* Note: Fake rx queue will not enter here */
32         if (rxq->sw_ring == NULL)
33                 return;
34
35         if (rxq->rx_rearm_nb == 0) {
36                 for (i = 0; i < rxq->nb_rx_desc; i++) {
37                         if (rxq->sw_ring[i].mbuf != NULL) {
38                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
39                                 rxq->sw_ring[i].mbuf = NULL;
40                         }
41                 }
42         } else {
43                 for (i = rxq->next_to_use;
44                      i != rxq->rx_rearm_start;
45                      i = (i + 1) % rxq->nb_rx_desc) {
46                         if (rxq->sw_ring[i].mbuf != NULL) {
47                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
48                                 rxq->sw_ring[i].mbuf = NULL;
49                         }
50                 }
51         }
52
53         for (i = 0; i < rxq->bulk_mbuf_num; i++)
54                 rte_pktmbuf_free_seg(rxq->bulk_mbuf[i]);
55         rxq->bulk_mbuf_num = 0;
56
57         if (rxq->pkt_first_seg) {
58                 rte_pktmbuf_free(rxq->pkt_first_seg);
59                 rxq->pkt_first_seg = NULL;
60         }
61 }
62
63 static void
64 hns3_tx_queue_release_mbufs(struct hns3_tx_queue *txq)
65 {
66         uint16_t i;
67
68         /* Note: Fake tx queue will not enter here */
69         if (txq->sw_ring) {
70                 for (i = 0; i < txq->nb_tx_desc; i++) {
71                         if (txq->sw_ring[i].mbuf) {
72                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
73                                 txq->sw_ring[i].mbuf = NULL;
74                         }
75                 }
76         }
77 }
78
79 static void
80 hns3_rx_queue_release(void *queue)
81 {
82         struct hns3_rx_queue *rxq = queue;
83         if (rxq) {
84                 hns3_rx_queue_release_mbufs(rxq);
85                 if (rxq->mz)
86                         rte_memzone_free(rxq->mz);
87                 if (rxq->sw_ring)
88                         rte_free(rxq->sw_ring);
89                 rte_free(rxq);
90         }
91 }
92
93 static void
94 hns3_tx_queue_release(void *queue)
95 {
96         struct hns3_tx_queue *txq = queue;
97         if (txq) {
98                 hns3_tx_queue_release_mbufs(txq);
99                 if (txq->mz)
100                         rte_memzone_free(txq->mz);
101                 if (txq->sw_ring)
102                         rte_free(txq->sw_ring);
103                 if (txq->free)
104                         rte_free(txq->free);
105                 rte_free(txq);
106         }
107 }
108
109 void
110 hns3_dev_rx_queue_release(void *queue)
111 {
112         struct hns3_rx_queue *rxq = queue;
113         struct hns3_adapter *hns;
114
115         if (rxq == NULL)
116                 return;
117
118         hns = rxq->hns;
119         rte_spinlock_lock(&hns->hw.lock);
120         hns3_rx_queue_release(queue);
121         rte_spinlock_unlock(&hns->hw.lock);
122 }
123
124 void
125 hns3_dev_tx_queue_release(void *queue)
126 {
127         struct hns3_tx_queue *txq = queue;
128         struct hns3_adapter *hns;
129
130         if (txq == NULL)
131                 return;
132
133         hns = txq->hns;
134         rte_spinlock_lock(&hns->hw.lock);
135         hns3_tx_queue_release(queue);
136         rte_spinlock_unlock(&hns->hw.lock);
137 }
138
139 static void
140 hns3_fake_rx_queue_release(struct hns3_rx_queue *queue)
141 {
142         struct hns3_rx_queue *rxq = queue;
143         struct hns3_adapter *hns;
144         struct hns3_hw *hw;
145         uint16_t idx;
146
147         if (rxq == NULL)
148                 return;
149
150         hns = rxq->hns;
151         hw = &hns->hw;
152         idx = rxq->queue_id;
153         if (hw->fkq_data.rx_queues[idx]) {
154                 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
155                 hw->fkq_data.rx_queues[idx] = NULL;
156         }
157
158         /* free fake rx queue arrays */
159         if (idx == (hw->fkq_data.nb_fake_rx_queues - 1)) {
160                 hw->fkq_data.nb_fake_rx_queues = 0;
161                 rte_free(hw->fkq_data.rx_queues);
162                 hw->fkq_data.rx_queues = NULL;
163         }
164 }
165
166 static void
167 hns3_fake_tx_queue_release(struct hns3_tx_queue *queue)
168 {
169         struct hns3_tx_queue *txq = queue;
170         struct hns3_adapter *hns;
171         struct hns3_hw *hw;
172         uint16_t idx;
173
174         if (txq == NULL)
175                 return;
176
177         hns = txq->hns;
178         hw = &hns->hw;
179         idx = txq->queue_id;
180         if (hw->fkq_data.tx_queues[idx]) {
181                 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
182                 hw->fkq_data.tx_queues[idx] = NULL;
183         }
184
185         /* free fake tx queue arrays */
186         if (idx == (hw->fkq_data.nb_fake_tx_queues - 1)) {
187                 hw->fkq_data.nb_fake_tx_queues = 0;
188                 rte_free(hw->fkq_data.tx_queues);
189                 hw->fkq_data.tx_queues = NULL;
190         }
191 }
192
193 static void
194 hns3_free_rx_queues(struct rte_eth_dev *dev)
195 {
196         struct hns3_adapter *hns = dev->data->dev_private;
197         struct hns3_fake_queue_data *fkq_data;
198         struct hns3_hw *hw = &hns->hw;
199         uint16_t nb_rx_q;
200         uint16_t i;
201
202         nb_rx_q = hw->data->nb_rx_queues;
203         for (i = 0; i < nb_rx_q; i++) {
204                 if (dev->data->rx_queues[i]) {
205                         hns3_rx_queue_release(dev->data->rx_queues[i]);
206                         dev->data->rx_queues[i] = NULL;
207                 }
208         }
209
210         /* Free fake Rx queues */
211         fkq_data = &hw->fkq_data;
212         for (i = 0; i < fkq_data->nb_fake_rx_queues; i++) {
213                 if (fkq_data->rx_queues[i])
214                         hns3_fake_rx_queue_release(fkq_data->rx_queues[i]);
215         }
216 }
217
218 static void
219 hns3_free_tx_queues(struct rte_eth_dev *dev)
220 {
221         struct hns3_adapter *hns = dev->data->dev_private;
222         struct hns3_fake_queue_data *fkq_data;
223         struct hns3_hw *hw = &hns->hw;
224         uint16_t nb_tx_q;
225         uint16_t i;
226
227         nb_tx_q = hw->data->nb_tx_queues;
228         for (i = 0; i < nb_tx_q; i++) {
229                 if (dev->data->tx_queues[i]) {
230                         hns3_tx_queue_release(dev->data->tx_queues[i]);
231                         dev->data->tx_queues[i] = NULL;
232                 }
233         }
234
235         /* Free fake Tx queues */
236         fkq_data = &hw->fkq_data;
237         for (i = 0; i < fkq_data->nb_fake_tx_queues; i++) {
238                 if (fkq_data->tx_queues[i])
239                         hns3_fake_tx_queue_release(fkq_data->tx_queues[i]);
240         }
241 }
242
243 void
244 hns3_free_all_queues(struct rte_eth_dev *dev)
245 {
246         hns3_free_rx_queues(dev);
247         hns3_free_tx_queues(dev);
248 }
249
250 static int
251 hns3_alloc_rx_queue_mbufs(struct hns3_hw *hw, struct hns3_rx_queue *rxq)
252 {
253         struct rte_mbuf *mbuf;
254         uint64_t dma_addr;
255         uint16_t i;
256
257         for (i = 0; i < rxq->nb_rx_desc; i++) {
258                 mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
259                 if (unlikely(mbuf == NULL)) {
260                         hns3_err(hw, "Failed to allocate RXD[%u] for rx queue!",
261                                  i);
262                         hns3_rx_queue_release_mbufs(rxq);
263                         return -ENOMEM;
264                 }
265
266                 rte_mbuf_refcnt_set(mbuf, 1);
267                 mbuf->next = NULL;
268                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
269                 mbuf->nb_segs = 1;
270                 mbuf->port = rxq->port_id;
271
272                 rxq->sw_ring[i].mbuf = mbuf;
273                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
274                 rxq->rx_ring[i].addr = dma_addr;
275                 rxq->rx_ring[i].rx.bd_base_info = 0;
276         }
277
278         return 0;
279 }
280
281 static int
282 hns3_buf_size2type(uint32_t buf_size)
283 {
284         int bd_size_type;
285
286         switch (buf_size) {
287         case 512:
288                 bd_size_type = HNS3_BD_SIZE_512_TYPE;
289                 break;
290         case 1024:
291                 bd_size_type = HNS3_BD_SIZE_1024_TYPE;
292                 break;
293         case 4096:
294                 bd_size_type = HNS3_BD_SIZE_4096_TYPE;
295                 break;
296         default:
297                 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
298         }
299
300         return bd_size_type;
301 }
302
303 static void
304 hns3_init_rx_queue_hw(struct hns3_rx_queue *rxq)
305 {
306         uint32_t rx_buf_len = rxq->rx_buf_len;
307         uint64_t dma_addr = rxq->rx_ring_phys_addr;
308
309         hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_L_REG, (uint32_t)dma_addr);
310         hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_H_REG,
311                        (uint32_t)((dma_addr >> 31) >> 1));
312
313         hns3_write_dev(rxq, HNS3_RING_RX_BD_LEN_REG,
314                        hns3_buf_size2type(rx_buf_len));
315         hns3_write_dev(rxq, HNS3_RING_RX_BD_NUM_REG,
316                        HNS3_CFG_DESC_NUM(rxq->nb_rx_desc));
317 }
318
319 static void
320 hns3_init_tx_queue_hw(struct hns3_tx_queue *txq)
321 {
322         uint64_t dma_addr = txq->tx_ring_phys_addr;
323
324         hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_L_REG, (uint32_t)dma_addr);
325         hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_H_REG,
326                        (uint32_t)((dma_addr >> 31) >> 1));
327
328         hns3_write_dev(txq, HNS3_RING_TX_BD_NUM_REG,
329                        HNS3_CFG_DESC_NUM(txq->nb_tx_desc));
330 }
331
332 void
333 hns3_update_all_queues_pvid_proc_en(struct hns3_hw *hw)
334 {
335         uint16_t nb_rx_q = hw->data->nb_rx_queues;
336         uint16_t nb_tx_q = hw->data->nb_tx_queues;
337         struct hns3_rx_queue *rxq;
338         struct hns3_tx_queue *txq;
339         bool pvid_en;
340         int i;
341
342         pvid_en = hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE;
343         for (i = 0; i < hw->cfg_max_queues; i++) {
344                 if (i < nb_rx_q) {
345                         rxq = hw->data->rx_queues[i];
346                         if (rxq != NULL)
347                                 rxq->pvid_sw_discard_en = pvid_en;
348                 }
349                 if (i < nb_tx_q) {
350                         txq = hw->data->tx_queues[i];
351                         if (txq != NULL)
352                                 txq->pvid_sw_shift_en = pvid_en;
353                 }
354         }
355 }
356
357 static void
358 hns3_stop_unused_queue(void *tqp_base, enum hns3_ring_type queue_type)
359 {
360         uint32_t reg_offset;
361         uint32_t reg;
362
363         reg_offset = queue_type == HNS3_RING_TYPE_TX ?
364                                    HNS3_RING_TX_EN_REG : HNS3_RING_RX_EN_REG;
365         reg = hns3_read_reg(tqp_base, reg_offset);
366         reg &= ~BIT(HNS3_RING_EN_B);
367         hns3_write_reg(tqp_base, reg_offset, reg);
368 }
369
370 void
371 hns3_enable_all_queues(struct hns3_hw *hw, bool en)
372 {
373         uint16_t nb_rx_q = hw->data->nb_rx_queues;
374         uint16_t nb_tx_q = hw->data->nb_tx_queues;
375         struct hns3_rx_queue *rxq;
376         struct hns3_tx_queue *txq;
377         uint32_t rcb_reg;
378         void *tqp_base;
379         int i;
380
381         for (i = 0; i < hw->cfg_max_queues; i++) {
382                 if (hns3_dev_indep_txrx_supported(hw)) {
383                         rxq = i < nb_rx_q ? hw->data->rx_queues[i] : NULL;
384                         txq = i < nb_tx_q ? hw->data->tx_queues[i] : NULL;
385
386                         tqp_base = (void *)((char *)hw->io_base +
387                                         hns3_get_tqp_reg_offset(i));
388                         /*
389                          * If queue struct is not initialized, it means the
390                          * related HW ring has not been initialized yet.
391                          * So, these queues should be disabled before enable
392                          * the tqps to avoid a HW exception since the queues
393                          * are enabled by default.
394                          */
395                         if (rxq == NULL)
396                                 hns3_stop_unused_queue(tqp_base,
397                                                         HNS3_RING_TYPE_RX);
398                         if (txq == NULL)
399                                 hns3_stop_unused_queue(tqp_base,
400                                                         HNS3_RING_TYPE_TX);
401                 } else {
402                         rxq = i < nb_rx_q ? hw->data->rx_queues[i] :
403                               hw->fkq_data.rx_queues[i - nb_rx_q];
404
405                         tqp_base = rxq->io_base;
406                 }
407                 /*
408                  * This is the master switch that used to control the enabling
409                  * of a pair of Tx and Rx queues. Both the Rx and Tx point to
410                  * the same register
411                  */
412                 rcb_reg = hns3_read_reg(tqp_base, HNS3_RING_EN_REG);
413                 if (en)
414                         rcb_reg |= BIT(HNS3_RING_EN_B);
415                 else
416                         rcb_reg &= ~BIT(HNS3_RING_EN_B);
417                 hns3_write_reg(tqp_base, HNS3_RING_EN_REG, rcb_reg);
418         }
419 }
420
421 static void
422 hns3_enable_txq(struct hns3_tx_queue *txq, bool en)
423 {
424         struct hns3_hw *hw = &txq->hns->hw;
425         uint32_t reg;
426
427         if (hns3_dev_indep_txrx_supported(hw)) {
428                 reg = hns3_read_dev(txq, HNS3_RING_TX_EN_REG);
429                 if (en)
430                         reg |= BIT(HNS3_RING_EN_B);
431                 else
432                         reg &= ~BIT(HNS3_RING_EN_B);
433                 hns3_write_dev(txq, HNS3_RING_TX_EN_REG, reg);
434         }
435         txq->enabled = en;
436 }
437
438 static void
439 hns3_enable_rxq(struct hns3_rx_queue *rxq, bool en)
440 {
441         struct hns3_hw *hw = &rxq->hns->hw;
442         uint32_t reg;
443
444         if (hns3_dev_indep_txrx_supported(hw)) {
445                 reg = hns3_read_dev(rxq, HNS3_RING_RX_EN_REG);
446                 if (en)
447                         reg |= BIT(HNS3_RING_EN_B);
448                 else
449                         reg &= ~BIT(HNS3_RING_EN_B);
450                 hns3_write_dev(rxq, HNS3_RING_RX_EN_REG, reg);
451         }
452         rxq->enabled = en;
453 }
454
455 int
456 hns3_start_all_txqs(struct rte_eth_dev *dev)
457 {
458         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
459         struct hns3_tx_queue *txq;
460         uint16_t i, j;
461
462         for (i = 0; i < dev->data->nb_tx_queues; i++) {
463                 txq = hw->data->tx_queues[i];
464                 if (!txq) {
465                         hns3_err(hw, "Tx queue %u not available or setup.", i);
466                         goto start_txqs_fail;
467                 }
468                 /*
469                  * Tx queue is enabled by default. Therefore, the Tx queues
470                  * needs to be disabled when deferred_start is set. There is
471                  * another master switch used to control the enabling of a pair
472                  * of Tx and Rx queues. And the master switch is disabled by
473                  * default.
474                  */
475                 if (txq->tx_deferred_start)
476                         hns3_enable_txq(txq, false);
477                 else
478                         hns3_enable_txq(txq, true);
479         }
480         return 0;
481
482 start_txqs_fail:
483         for (j = 0; j < i; j++) {
484                 txq = hw->data->tx_queues[j];
485                 hns3_enable_txq(txq, false);
486         }
487         return -EINVAL;
488 }
489
490 int
491 hns3_start_all_rxqs(struct rte_eth_dev *dev)
492 {
493         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
494         struct hns3_rx_queue *rxq;
495         uint16_t i, j;
496
497         for (i = 0; i < dev->data->nb_rx_queues; i++) {
498                 rxq = hw->data->rx_queues[i];
499                 if (!rxq) {
500                         hns3_err(hw, "Rx queue %u not available or setup.", i);
501                         goto start_rxqs_fail;
502                 }
503                 /*
504                  * Rx queue is enabled by default. Therefore, the Rx queues
505                  * needs to be disabled when deferred_start is set. There is
506                  * another master switch used to control the enabling of a pair
507                  * of Tx and Rx queues. And the master switch is disabled by
508                  * default.
509                  */
510                 if (rxq->rx_deferred_start)
511                         hns3_enable_rxq(rxq, false);
512                 else
513                         hns3_enable_rxq(rxq, true);
514         }
515         return 0;
516
517 start_rxqs_fail:
518         for (j = 0; j < i; j++) {
519                 rxq = hw->data->rx_queues[j];
520                 hns3_enable_rxq(rxq, false);
521         }
522         return -EINVAL;
523 }
524
525 void
526 hns3_restore_tqp_enable_state(struct hns3_hw *hw)
527 {
528         struct hns3_rx_queue *rxq;
529         struct hns3_tx_queue *txq;
530         uint16_t i;
531
532         for (i = 0; i < hw->data->nb_rx_queues; i++) {
533                 rxq = hw->data->rx_queues[i];
534                 if (rxq != NULL)
535                         hns3_enable_rxq(rxq, rxq->enabled);
536         }
537
538         for (i = 0; i < hw->data->nb_tx_queues; i++) {
539                 txq = hw->data->tx_queues[i];
540                 if (txq != NULL)
541                         hns3_enable_txq(txq, txq->enabled);
542         }
543 }
544
545 void
546 hns3_stop_all_txqs(struct rte_eth_dev *dev)
547 {
548         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
549         struct hns3_tx_queue *txq;
550         uint16_t i;
551
552         for (i = 0; i < dev->data->nb_tx_queues; i++) {
553                 txq = hw->data->tx_queues[i];
554                 if (!txq)
555                         continue;
556                 hns3_enable_txq(txq, false);
557         }
558 }
559
560 static int
561 hns3_tqp_enable(struct hns3_hw *hw, uint16_t queue_id, bool enable)
562 {
563         struct hns3_cfg_com_tqp_queue_cmd *req;
564         struct hns3_cmd_desc desc;
565         int ret;
566
567         req = (struct hns3_cfg_com_tqp_queue_cmd *)desc.data;
568
569         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_COM_TQP_QUEUE, false);
570         req->tqp_id = rte_cpu_to_le_16(queue_id);
571         req->stream_id = 0;
572         hns3_set_bit(req->enable, HNS3_TQP_ENABLE_B, enable ? 1 : 0);
573
574         ret = hns3_cmd_send(hw, &desc, 1);
575         if (ret)
576                 hns3_err(hw, "TQP enable fail, ret = %d", ret);
577
578         return ret;
579 }
580
581 static int
582 hns3_send_reset_tqp_cmd(struct hns3_hw *hw, uint16_t queue_id, bool enable)
583 {
584         struct hns3_reset_tqp_queue_cmd *req;
585         struct hns3_cmd_desc desc;
586         int ret;
587
588         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, false);
589
590         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
591         req->tqp_id = rte_cpu_to_le_16(queue_id);
592         hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);
593         ret = hns3_cmd_send(hw, &desc, 1);
594         if (ret)
595                 hns3_err(hw, "send tqp reset cmd error, queue_id = %u, "
596                              "ret = %d", queue_id, ret);
597
598         return ret;
599 }
600
601 static int
602 hns3_get_tqp_reset_status(struct hns3_hw *hw, uint16_t queue_id,
603                           uint8_t *reset_status)
604 {
605         struct hns3_reset_tqp_queue_cmd *req;
606         struct hns3_cmd_desc desc;
607         int ret;
608
609         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, true);
610
611         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
612         req->tqp_id = rte_cpu_to_le_16(queue_id);
613
614         ret = hns3_cmd_send(hw, &desc, 1);
615         if (ret) {
616                 hns3_err(hw, "get tqp reset status error, queue_id = %u, "
617                              "ret = %d.", queue_id, ret);
618                 return ret;
619         }
620         *reset_status = hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
621         return ret;
622 }
623
624 static int
625 hns3pf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
626 {
627 #define HNS3_TQP_RESET_TRY_MS   200
628         uint8_t reset_status;
629         uint64_t end;
630         int ret;
631
632         ret = hns3_tqp_enable(hw, queue_id, false);
633         if (ret)
634                 return ret;
635
636         /*
637          * In current version VF is not supported when PF is driven by DPDK
638          * driver, all task queue pairs are mapped to PF function, so PF's queue
639          * id is equals to the global queue id in PF range.
640          */
641         ret = hns3_send_reset_tqp_cmd(hw, queue_id, true);
642         if (ret) {
643                 hns3_err(hw, "Send reset tqp cmd fail, ret = %d", ret);
644                 return ret;
645         }
646         end = get_timeofday_ms() + HNS3_TQP_RESET_TRY_MS;
647         do {
648                 /* Wait for tqp hw reset */
649                 rte_delay_ms(HNS3_POLL_RESPONE_MS);
650                 ret = hns3_get_tqp_reset_status(hw, queue_id, &reset_status);
651                 if (ret)
652                         goto tqp_reset_fail;
653
654                 if (reset_status)
655                         break;
656         } while (get_timeofday_ms() < end);
657
658         if (!reset_status) {
659                 ret = -ETIMEDOUT;
660                 hns3_err(hw, "reset tqp timeout, queue_id = %u, ret = %d",
661                              queue_id, ret);
662                 goto tqp_reset_fail;
663         }
664
665         ret = hns3_send_reset_tqp_cmd(hw, queue_id, false);
666         if (ret)
667                 hns3_err(hw, "Deassert the soft reset fail, ret = %d", ret);
668
669         return ret;
670
671 tqp_reset_fail:
672         hns3_send_reset_tqp_cmd(hw, queue_id, false);
673         return ret;
674 }
675
676 static int
677 hns3vf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
678 {
679         uint8_t msg_data[2];
680         int ret;
681
682         /* Disable VF's queue before send queue reset msg to PF */
683         ret = hns3_tqp_enable(hw, queue_id, false);
684         if (ret)
685                 return ret;
686
687         memcpy(msg_data, &queue_id, sizeof(uint16_t));
688
689         ret = hns3_send_mbx_msg(hw, HNS3_MBX_QUEUE_RESET, 0, msg_data,
690                                  sizeof(msg_data), true, NULL, 0);
691         if (ret)
692                 hns3_err(hw, "fail to reset tqp, queue_id = %u, ret = %d.",
693                          queue_id, ret);
694         return ret;
695 }
696
697 static int
698 hns3_reset_tqp(struct hns3_adapter *hns, uint16_t queue_id)
699 {
700         struct hns3_hw *hw = &hns->hw;
701
702         if (hns->is_vf)
703                 return hns3vf_reset_tqp(hw, queue_id);
704         else
705                 return hns3pf_reset_tqp(hw, queue_id);
706 }
707
708 int
709 hns3_reset_all_tqps(struct hns3_adapter *hns)
710 {
711         struct hns3_hw *hw = &hns->hw;
712         int ret, i;
713
714         for (i = 0; i < hw->cfg_max_queues; i++) {
715                 ret = hns3_reset_tqp(hns, i);
716                 if (ret) {
717                         hns3_err(hw, "Failed to reset No.%d queue: %d", i, ret);
718                         return ret;
719                 }
720         }
721         return 0;
722 }
723
724 static int
725 hns3_send_reset_queue_cmd(struct hns3_hw *hw, uint16_t queue_id,
726                           enum hns3_ring_type queue_type, bool enable)
727 {
728         struct hns3_reset_tqp_queue_cmd *req;
729         struct hns3_cmd_desc desc;
730         int queue_direction;
731         int ret;
732
733         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE_INDEP, false);
734
735         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
736         req->tqp_id = rte_cpu_to_le_16(queue_id);
737         queue_direction = queue_type == HNS3_RING_TYPE_TX ? 0 : 1;
738         req->queue_direction = rte_cpu_to_le_16(queue_direction);
739         hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);
740
741         ret = hns3_cmd_send(hw, &desc, 1);
742         if (ret)
743                 hns3_err(hw, "send queue reset cmd error, queue_id = %u, "
744                          "queue_type = %s, ret = %d.", queue_id,
745                          queue_type == HNS3_RING_TYPE_TX ? "Tx" : "Rx", ret);
746         return ret;
747 }
748
749 static int
750 hns3_get_queue_reset_status(struct hns3_hw *hw, uint16_t queue_id,
751                             enum hns3_ring_type queue_type,
752                             uint8_t *reset_status)
753 {
754         struct hns3_reset_tqp_queue_cmd *req;
755         struct hns3_cmd_desc desc;
756         int queue_direction;
757         int ret;
758
759         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE_INDEP, true);
760
761         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
762         req->tqp_id = rte_cpu_to_le_16(queue_id);
763         queue_direction = queue_type == HNS3_RING_TYPE_TX ? 0 : 1;
764         req->queue_direction = rte_cpu_to_le_16(queue_direction);
765
766         ret = hns3_cmd_send(hw, &desc, 1);
767         if (ret) {
768                 hns3_err(hw, "get queue reset status error, queue_id = %u "
769                          "queue_type = %s, ret = %d.", queue_id,
770                          queue_type == HNS3_RING_TYPE_TX ? "Tx" : "Rx", ret);
771                 return ret;
772         }
773
774         *reset_status = hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
775         return  ret;
776 }
777
778 static int
779 hns3_reset_queue(struct hns3_hw *hw, uint16_t queue_id,
780                  enum hns3_ring_type queue_type)
781 {
782 #define HNS3_QUEUE_RESET_TRY_MS 200
783         struct hns3_tx_queue *txq;
784         struct hns3_rx_queue *rxq;
785         uint32_t reset_wait_times;
786         uint32_t max_wait_times;
787         uint8_t reset_status;
788         int ret;
789
790         if (queue_type == HNS3_RING_TYPE_TX) {
791                 txq = hw->data->tx_queues[queue_id];
792                 hns3_enable_txq(txq, false);
793         } else {
794                 rxq = hw->data->rx_queues[queue_id];
795                 hns3_enable_rxq(rxq, false);
796         }
797
798         ret = hns3_send_reset_queue_cmd(hw, queue_id, queue_type, true);
799         if (ret) {
800                 hns3_err(hw, "send reset queue cmd fail, ret = %d.", ret);
801                 return ret;
802         }
803
804         reset_wait_times = 0;
805         max_wait_times = HNS3_QUEUE_RESET_TRY_MS / HNS3_POLL_RESPONE_MS;
806         while (reset_wait_times < max_wait_times) {
807                 /* Wait for queue hw reset */
808                 rte_delay_ms(HNS3_POLL_RESPONE_MS);
809                 ret = hns3_get_queue_reset_status(hw, queue_id,
810                                                 queue_type, &reset_status);
811                 if (ret)
812                         goto queue_reset_fail;
813
814                 if (reset_status)
815                         break;
816                 reset_wait_times++;
817         }
818
819         if (!reset_status) {
820                 hns3_err(hw, "reset queue timeout, queue_id = %u, "
821                              "queue_type = %s", queue_id,
822                              queue_type == HNS3_RING_TYPE_TX ? "Tx" : "Rx");
823                 ret = -ETIMEDOUT;
824                 goto queue_reset_fail;
825         }
826
827         ret = hns3_send_reset_queue_cmd(hw, queue_id, queue_type, false);
828         if (ret)
829                 hns3_err(hw, "deassert queue reset fail, ret = %d.", ret);
830
831         return ret;
832
833 queue_reset_fail:
834         hns3_send_reset_queue_cmd(hw, queue_id, queue_type, false);
835         return ret;
836 }
837
838 uint32_t
839 hns3_get_tqp_intr_reg_offset(uint16_t tqp_intr_id)
840 {
841         uint32_t reg_offset;
842
843         /* Need an extend offset to config queues > 64 */
844         if (tqp_intr_id < HNS3_MIN_EXT_TQP_INTR_ID)
845                 reg_offset = HNS3_TQP_INTR_REG_BASE +
846                              tqp_intr_id * HNS3_TQP_INTR_LOW_ORDER_OFFSET;
847         else
848                 reg_offset = HNS3_TQP_INTR_EXT_REG_BASE +
849                              tqp_intr_id / HNS3_MIN_EXT_TQP_INTR_ID *
850                              HNS3_TQP_INTR_HIGH_ORDER_OFFSET +
851                              tqp_intr_id % HNS3_MIN_EXT_TQP_INTR_ID *
852                              HNS3_TQP_INTR_LOW_ORDER_OFFSET;
853
854         return reg_offset;
855 }
856
857 void
858 hns3_set_queue_intr_gl(struct hns3_hw *hw, uint16_t queue_id,
859                        uint8_t gl_idx, uint16_t gl_value)
860 {
861         uint32_t offset[] = {HNS3_TQP_INTR_GL0_REG,
862                              HNS3_TQP_INTR_GL1_REG,
863                              HNS3_TQP_INTR_GL2_REG};
864         uint32_t addr, value;
865
866         if (gl_idx >= RTE_DIM(offset) || gl_value > HNS3_TQP_INTR_GL_MAX)
867                 return;
868
869         addr = offset[gl_idx] + hns3_get_tqp_intr_reg_offset(queue_id);
870         if (hw->intr.gl_unit == HNS3_INTR_COALESCE_GL_UINT_1US)
871                 value = gl_value | HNS3_TQP_INTR_GL_UNIT_1US;
872         else
873                 value = HNS3_GL_USEC_TO_REG(gl_value);
874
875         hns3_write_dev(hw, addr, value);
876 }
877
878 void
879 hns3_set_queue_intr_rl(struct hns3_hw *hw, uint16_t queue_id, uint16_t rl_value)
880 {
881         uint32_t addr, value;
882
883         if (rl_value > HNS3_TQP_INTR_RL_MAX)
884                 return;
885
886         addr = HNS3_TQP_INTR_RL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
887         value = HNS3_RL_USEC_TO_REG(rl_value);
888         if (value > 0)
889                 value |= HNS3_TQP_INTR_RL_ENABLE_MASK;
890
891         hns3_write_dev(hw, addr, value);
892 }
893
894 void
895 hns3_set_queue_intr_ql(struct hns3_hw *hw, uint16_t queue_id, uint16_t ql_value)
896 {
897         uint32_t addr;
898
899         /*
900          * int_ql_max == 0 means the hardware does not support QL,
901          * QL regs config is not permitted if QL is not supported,
902          * here just return.
903          */
904         if (hw->intr.int_ql_max == HNS3_INTR_QL_NONE)
905                 return;
906
907         addr = HNS3_TQP_INTR_TX_QL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
908         hns3_write_dev(hw, addr, ql_value);
909
910         addr = HNS3_TQP_INTR_RX_QL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
911         hns3_write_dev(hw, addr, ql_value);
912 }
913
914 static void
915 hns3_queue_intr_enable(struct hns3_hw *hw, uint16_t queue_id, bool en)
916 {
917         uint32_t addr, value;
918
919         addr = HNS3_TQP_INTR_CTRL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
920         value = en ? 1 : 0;
921
922         hns3_write_dev(hw, addr, value);
923 }
924
925 /*
926  * Enable all rx queue interrupt when in interrupt rx mode.
927  * This api was called before enable queue rx&tx (in normal start or reset
928  * recover scenes), used to fix hardware rx queue interrupt enable was clear
929  * when FLR.
930  */
931 void
932 hns3_dev_all_rx_queue_intr_enable(struct hns3_hw *hw, bool en)
933 {
934         struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
935         uint16_t nb_rx_q = hw->data->nb_rx_queues;
936         int i;
937
938         if (dev->data->dev_conf.intr_conf.rxq == 0)
939                 return;
940
941         for (i = 0; i < nb_rx_q; i++)
942                 hns3_queue_intr_enable(hw, i, en);
943 }
944
945 int
946 hns3_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
947 {
948         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
949         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
950         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
951
952         if (dev->data->dev_conf.intr_conf.rxq == 0)
953                 return -ENOTSUP;
954
955         hns3_queue_intr_enable(hw, queue_id, true);
956
957         return rte_intr_ack(intr_handle);
958 }
959
960 int
961 hns3_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
962 {
963         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
964
965         if (dev->data->dev_conf.intr_conf.rxq == 0)
966                 return -ENOTSUP;
967
968         hns3_queue_intr_enable(hw, queue_id, false);
969
970         return 0;
971 }
972
973 static int
974 hns3_init_rxq(struct hns3_adapter *hns, uint16_t idx)
975 {
976         struct hns3_hw *hw = &hns->hw;
977         struct hns3_rx_queue *rxq;
978         int ret;
979
980         PMD_INIT_FUNC_TRACE();
981
982         rxq = (struct hns3_rx_queue *)hw->data->rx_queues[idx];
983         ret = hns3_alloc_rx_queue_mbufs(hw, rxq);
984         if (ret) {
985                 hns3_err(hw, "fail to alloc mbuf for Rx queue %u, ret = %d.",
986                          idx, ret);
987                 return ret;
988         }
989
990         rxq->next_to_use = 0;
991         rxq->rx_rearm_start = 0;
992         rxq->rx_free_hold = 0;
993         rxq->rx_rearm_nb = 0;
994         rxq->pkt_first_seg = NULL;
995         rxq->pkt_last_seg = NULL;
996         hns3_init_rx_queue_hw(rxq);
997         hns3_rxq_vec_setup(rxq);
998
999         return 0;
1000 }
1001
1002 static void
1003 hns3_init_fake_rxq(struct hns3_adapter *hns, uint16_t idx)
1004 {
1005         struct hns3_hw *hw = &hns->hw;
1006         struct hns3_rx_queue *rxq;
1007
1008         rxq = (struct hns3_rx_queue *)hw->fkq_data.rx_queues[idx];
1009         rxq->next_to_use = 0;
1010         rxq->rx_free_hold = 0;
1011         rxq->rx_rearm_start = 0;
1012         rxq->rx_rearm_nb = 0;
1013         hns3_init_rx_queue_hw(rxq);
1014 }
1015
1016 static void
1017 hns3_init_txq(struct hns3_tx_queue *txq)
1018 {
1019         struct hns3_desc *desc;
1020         int i;
1021
1022         /* Clear tx bd */
1023         desc = txq->tx_ring;
1024         for (i = 0; i < txq->nb_tx_desc; i++) {
1025                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
1026                 desc++;
1027         }
1028
1029         txq->next_to_use = 0;
1030         txq->next_to_clean = 0;
1031         txq->tx_bd_ready = txq->nb_tx_desc - 1;
1032         hns3_init_tx_queue_hw(txq);
1033 }
1034
1035 static void
1036 hns3_init_tx_ring_tc(struct hns3_adapter *hns)
1037 {
1038         struct hns3_hw *hw = &hns->hw;
1039         struct hns3_tx_queue *txq;
1040         int i, num;
1041
1042         for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1043                 struct hns3_tc_queue_info *tc_queue = &hw->tc_queue[i];
1044                 int j;
1045
1046                 if (!tc_queue->enable)
1047                         continue;
1048
1049                 for (j = 0; j < tc_queue->tqp_count; j++) {
1050                         num = tc_queue->tqp_offset + j;
1051                         txq = (struct hns3_tx_queue *)hw->data->tx_queues[num];
1052                         if (txq == NULL)
1053                                 continue;
1054
1055                         hns3_write_dev(txq, HNS3_RING_TX_TC_REG, tc_queue->tc);
1056                 }
1057         }
1058 }
1059
1060 static int
1061 hns3_init_rx_queues(struct hns3_adapter *hns)
1062 {
1063         struct hns3_hw *hw = &hns->hw;
1064         struct hns3_rx_queue *rxq;
1065         uint16_t i, j;
1066         int ret;
1067
1068         /* Initialize RSS for queues */
1069         ret = hns3_config_rss(hns);
1070         if (ret) {
1071                 hns3_err(hw, "failed to configure rss, ret = %d.", ret);
1072                 return ret;
1073         }
1074
1075         for (i = 0; i < hw->data->nb_rx_queues; i++) {
1076                 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[i];
1077                 if (!rxq) {
1078                         hns3_err(hw, "Rx queue %u not available or setup.", i);
1079                         goto out;
1080                 }
1081
1082                 if (rxq->rx_deferred_start)
1083                         continue;
1084
1085                 ret = hns3_init_rxq(hns, i);
1086                 if (ret) {
1087                         hns3_err(hw, "failed to init Rx queue %u, ret = %d.", i,
1088                                  ret);
1089                         goto out;
1090                 }
1091         }
1092
1093         for (i = 0; i < hw->fkq_data.nb_fake_rx_queues; i++)
1094                 hns3_init_fake_rxq(hns, i);
1095
1096         return 0;
1097
1098 out:
1099         for (j = 0; j < i; j++) {
1100                 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[j];
1101                 hns3_rx_queue_release_mbufs(rxq);
1102         }
1103
1104         return ret;
1105 }
1106
1107 static int
1108 hns3_init_tx_queues(struct hns3_adapter *hns)
1109 {
1110         struct hns3_hw *hw = &hns->hw;
1111         struct hns3_tx_queue *txq;
1112         uint16_t i;
1113
1114         for (i = 0; i < hw->data->nb_tx_queues; i++) {
1115                 txq = (struct hns3_tx_queue *)hw->data->tx_queues[i];
1116                 if (!txq) {
1117                         hns3_err(hw, "Tx queue %u not available or setup.", i);
1118                         return -EINVAL;
1119                 }
1120
1121                 if (txq->tx_deferred_start)
1122                         continue;
1123                 hns3_init_txq(txq);
1124         }
1125
1126         for (i = 0; i < hw->fkq_data.nb_fake_tx_queues; i++) {
1127                 txq = (struct hns3_tx_queue *)hw->fkq_data.tx_queues[i];
1128                 hns3_init_txq(txq);
1129         }
1130         hns3_init_tx_ring_tc(hns);
1131
1132         return 0;
1133 }
1134
1135 /*
1136  * Init all queues.
1137  * Note: just init and setup queues, and don't enable tqps.
1138  */
1139 int
1140 hns3_init_queues(struct hns3_adapter *hns, bool reset_queue)
1141 {
1142         struct hns3_hw *hw = &hns->hw;
1143         int ret;
1144
1145         if (reset_queue) {
1146                 ret = hns3_reset_all_tqps(hns);
1147                 if (ret) {
1148                         hns3_err(hw, "failed to reset all queues, ret = %d.",
1149                                  ret);
1150                         return ret;
1151                 }
1152         }
1153
1154         ret = hns3_init_rx_queues(hns);
1155         if (ret) {
1156                 hns3_err(hw, "failed to init rx queues, ret = %d.", ret);
1157                 return ret;
1158         }
1159
1160         ret = hns3_init_tx_queues(hns);
1161         if (ret) {
1162                 hns3_dev_release_mbufs(hns);
1163                 hns3_err(hw, "failed to init tx queues, ret = %d.", ret);
1164         }
1165
1166         return ret;
1167 }
1168
1169 void
1170 hns3_start_tqps(struct hns3_hw *hw)
1171 {
1172         struct hns3_tx_queue *txq;
1173         struct hns3_rx_queue *rxq;
1174         uint16_t i;
1175
1176         hns3_enable_all_queues(hw, true);
1177
1178         for (i = 0; i < hw->data->nb_tx_queues; i++) {
1179                 txq = hw->data->tx_queues[i];
1180                 if (txq->enabled)
1181                         hw->data->tx_queue_state[i] =
1182                                 RTE_ETH_QUEUE_STATE_STARTED;
1183         }
1184
1185         for (i = 0; i < hw->data->nb_rx_queues; i++) {
1186                 rxq = hw->data->rx_queues[i];
1187                 if (rxq->enabled)
1188                         hw->data->rx_queue_state[i] =
1189                                 RTE_ETH_QUEUE_STATE_STARTED;
1190         }
1191 }
1192
1193 void
1194 hns3_stop_tqps(struct hns3_hw *hw)
1195 {
1196         uint16_t i;
1197
1198         hns3_enable_all_queues(hw, false);
1199
1200         for (i = 0; i < hw->data->nb_tx_queues; i++)
1201                 hw->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1202
1203         for (i = 0; i < hw->data->nb_rx_queues; i++)
1204                 hw->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1205 }
1206
1207 /*
1208  * Iterate over all Rx Queue, and call the callback() function for each Rx
1209  * queue.
1210  *
1211  * @param[in] dev
1212  *   The target eth dev.
1213  * @param[in] callback
1214  *   The function to call for each queue.
1215  *   if callback function return nonzero will stop iterate and return it's value
1216  * @param[in] arg
1217  *   The arguments to provide the callback function with.
1218  *
1219  * @return
1220  *   0 on success, otherwise with errno set.
1221  */
1222 int
1223 hns3_rxq_iterate(struct rte_eth_dev *dev,
1224                  int (*callback)(struct hns3_rx_queue *, void *), void *arg)
1225 {
1226         uint32_t i;
1227         int ret;
1228
1229         if (dev->data->rx_queues == NULL)
1230                 return -EINVAL;
1231
1232         for (i = 0; i < dev->data->nb_rx_queues; i++) {
1233                 ret = callback(dev->data->rx_queues[i], arg);
1234                 if (ret != 0)
1235                         return ret;
1236         }
1237
1238         return 0;
1239 }
1240
1241 static void*
1242 hns3_alloc_rxq_and_dma_zone(struct rte_eth_dev *dev,
1243                             struct hns3_queue_info *q_info)
1244 {
1245         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1246         const struct rte_memzone *rx_mz;
1247         struct hns3_rx_queue *rxq;
1248         unsigned int rx_desc;
1249
1250         rxq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_rx_queue),
1251                                  RTE_CACHE_LINE_SIZE, q_info->socket_id);
1252         if (rxq == NULL) {
1253                 hns3_err(hw, "Failed to allocate memory for No.%u rx ring!",
1254                          q_info->idx);
1255                 return NULL;
1256         }
1257
1258         /* Allocate rx ring hardware descriptors. */
1259         rxq->queue_id = q_info->idx;
1260         rxq->nb_rx_desc = q_info->nb_desc;
1261
1262         /*
1263          * Allocate a litter more memory because rx vector functions
1264          * don't check boundaries each time.
1265          */
1266         rx_desc = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
1267                         sizeof(struct hns3_desc);
1268         rx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
1269                                          rx_desc, HNS3_RING_BASE_ALIGN,
1270                                          q_info->socket_id);
1271         if (rx_mz == NULL) {
1272                 hns3_err(hw, "Failed to reserve DMA memory for No.%u rx ring!",
1273                          q_info->idx);
1274                 hns3_rx_queue_release(rxq);
1275                 return NULL;
1276         }
1277         rxq->mz = rx_mz;
1278         rxq->rx_ring = (struct hns3_desc *)rx_mz->addr;
1279         rxq->rx_ring_phys_addr = rx_mz->iova;
1280
1281         hns3_dbg(hw, "No.%u rx descriptors iova 0x%" PRIx64, q_info->idx,
1282                  rxq->rx_ring_phys_addr);
1283
1284         return rxq;
1285 }
1286
1287 static int
1288 hns3_fake_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
1289                          uint16_t nb_desc, unsigned int socket_id)
1290 {
1291         struct hns3_adapter *hns = dev->data->dev_private;
1292         struct hns3_hw *hw = &hns->hw;
1293         struct hns3_queue_info q_info;
1294         struct hns3_rx_queue *rxq;
1295         uint16_t nb_rx_q;
1296
1297         if (hw->fkq_data.rx_queues[idx]) {
1298                 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
1299                 hw->fkq_data.rx_queues[idx] = NULL;
1300         }
1301
1302         q_info.idx = idx;
1303         q_info.socket_id = socket_id;
1304         q_info.nb_desc = nb_desc;
1305         q_info.type = "hns3 fake RX queue";
1306         q_info.ring_name = "rx_fake_ring";
1307         rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
1308         if (rxq == NULL) {
1309                 hns3_err(hw, "Failed to setup No.%u fake rx ring.", idx);
1310                 return -ENOMEM;
1311         }
1312
1313         /* Don't need alloc sw_ring, because upper applications don't use it */
1314         rxq->sw_ring = NULL;
1315
1316         rxq->hns = hns;
1317         rxq->rx_deferred_start = false;
1318         rxq->port_id = dev->data->port_id;
1319         rxq->configured = true;
1320         nb_rx_q = dev->data->nb_rx_queues;
1321         rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1322                                 (nb_rx_q + idx) * HNS3_TQP_REG_SIZE);
1323         rxq->rx_buf_len = HNS3_MIN_BD_BUF_SIZE;
1324
1325         rte_spinlock_lock(&hw->lock);
1326         hw->fkq_data.rx_queues[idx] = rxq;
1327         rte_spinlock_unlock(&hw->lock);
1328
1329         return 0;
1330 }
1331
1332 static void*
1333 hns3_alloc_txq_and_dma_zone(struct rte_eth_dev *dev,
1334                             struct hns3_queue_info *q_info)
1335 {
1336         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1337         const struct rte_memzone *tx_mz;
1338         struct hns3_tx_queue *txq;
1339         struct hns3_desc *desc;
1340         unsigned int tx_desc;
1341         int i;
1342
1343         txq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_tx_queue),
1344                                  RTE_CACHE_LINE_SIZE, q_info->socket_id);
1345         if (txq == NULL) {
1346                 hns3_err(hw, "Failed to allocate memory for No.%u tx ring!",
1347                          q_info->idx);
1348                 return NULL;
1349         }
1350
1351         /* Allocate tx ring hardware descriptors. */
1352         txq->queue_id = q_info->idx;
1353         txq->nb_tx_desc = q_info->nb_desc;
1354         tx_desc = txq->nb_tx_desc * sizeof(struct hns3_desc);
1355         tx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
1356                                          tx_desc, HNS3_RING_BASE_ALIGN,
1357                                          q_info->socket_id);
1358         if (tx_mz == NULL) {
1359                 hns3_err(hw, "Failed to reserve DMA memory for No.%u tx ring!",
1360                          q_info->idx);
1361                 hns3_tx_queue_release(txq);
1362                 return NULL;
1363         }
1364         txq->mz = tx_mz;
1365         txq->tx_ring = (struct hns3_desc *)tx_mz->addr;
1366         txq->tx_ring_phys_addr = tx_mz->iova;
1367
1368         hns3_dbg(hw, "No.%u tx descriptors iova 0x%" PRIx64, q_info->idx,
1369                  txq->tx_ring_phys_addr);
1370
1371         /* Clear tx bd */
1372         desc = txq->tx_ring;
1373         for (i = 0; i < txq->nb_tx_desc; i++) {
1374                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
1375                 desc++;
1376         }
1377
1378         return txq;
1379 }
1380
1381 static int
1382 hns3_fake_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
1383                          uint16_t nb_desc, unsigned int socket_id)
1384 {
1385         struct hns3_adapter *hns = dev->data->dev_private;
1386         struct hns3_hw *hw = &hns->hw;
1387         struct hns3_queue_info q_info;
1388         struct hns3_tx_queue *txq;
1389         uint16_t nb_tx_q;
1390
1391         if (hw->fkq_data.tx_queues[idx] != NULL) {
1392                 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
1393                 hw->fkq_data.tx_queues[idx] = NULL;
1394         }
1395
1396         q_info.idx = idx;
1397         q_info.socket_id = socket_id;
1398         q_info.nb_desc = nb_desc;
1399         q_info.type = "hns3 fake TX queue";
1400         q_info.ring_name = "tx_fake_ring";
1401         txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
1402         if (txq == NULL) {
1403                 hns3_err(hw, "Failed to setup No.%u fake tx ring.", idx);
1404                 return -ENOMEM;
1405         }
1406
1407         /* Don't need alloc sw_ring, because upper applications don't use it */
1408         txq->sw_ring = NULL;
1409         txq->free = NULL;
1410
1411         txq->hns = hns;
1412         txq->tx_deferred_start = false;
1413         txq->port_id = dev->data->port_id;
1414         txq->configured = true;
1415         nb_tx_q = dev->data->nb_tx_queues;
1416         txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1417                                 (nb_tx_q + idx) * HNS3_TQP_REG_SIZE);
1418
1419         rte_spinlock_lock(&hw->lock);
1420         hw->fkq_data.tx_queues[idx] = txq;
1421         rte_spinlock_unlock(&hw->lock);
1422
1423         return 0;
1424 }
1425
1426 static int
1427 hns3_fake_rx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
1428 {
1429         uint16_t old_nb_queues = hw->fkq_data.nb_fake_rx_queues;
1430         void **rxq;
1431         uint16_t i;
1432
1433         if (hw->fkq_data.rx_queues == NULL && nb_queues != 0) {
1434                 /* first time configuration */
1435                 uint32_t size;
1436                 size = sizeof(hw->fkq_data.rx_queues[0]) * nb_queues;
1437                 hw->fkq_data.rx_queues = rte_zmalloc("fake_rx_queues", size,
1438                                                      RTE_CACHE_LINE_SIZE);
1439                 if (hw->fkq_data.rx_queues == NULL) {
1440                         hw->fkq_data.nb_fake_rx_queues = 0;
1441                         return -ENOMEM;
1442                 }
1443         } else if (hw->fkq_data.rx_queues != NULL && nb_queues != 0) {
1444                 /* re-configure */
1445                 rxq = hw->fkq_data.rx_queues;
1446                 for (i = nb_queues; i < old_nb_queues; i++)
1447                         hns3_dev_rx_queue_release(rxq[i]);
1448
1449                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
1450                                   RTE_CACHE_LINE_SIZE);
1451                 if (rxq == NULL)
1452                         return -ENOMEM;
1453                 if (nb_queues > old_nb_queues) {
1454                         uint16_t new_qs = nb_queues - old_nb_queues;
1455                         memset(rxq + old_nb_queues, 0, sizeof(rxq[0]) * new_qs);
1456                 }
1457
1458                 hw->fkq_data.rx_queues = rxq;
1459         } else if (hw->fkq_data.rx_queues != NULL && nb_queues == 0) {
1460                 rxq = hw->fkq_data.rx_queues;
1461                 for (i = nb_queues; i < old_nb_queues; i++)
1462                         hns3_dev_rx_queue_release(rxq[i]);
1463
1464                 rte_free(hw->fkq_data.rx_queues);
1465                 hw->fkq_data.rx_queues = NULL;
1466         }
1467
1468         hw->fkq_data.nb_fake_rx_queues = nb_queues;
1469
1470         return 0;
1471 }
1472
1473 static int
1474 hns3_fake_tx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
1475 {
1476         uint16_t old_nb_queues = hw->fkq_data.nb_fake_tx_queues;
1477         void **txq;
1478         uint16_t i;
1479
1480         if (hw->fkq_data.tx_queues == NULL && nb_queues != 0) {
1481                 /* first time configuration */
1482                 uint32_t size;
1483                 size = sizeof(hw->fkq_data.tx_queues[0]) * nb_queues;
1484                 hw->fkq_data.tx_queues = rte_zmalloc("fake_tx_queues", size,
1485                                                      RTE_CACHE_LINE_SIZE);
1486                 if (hw->fkq_data.tx_queues == NULL) {
1487                         hw->fkq_data.nb_fake_tx_queues = 0;
1488                         return -ENOMEM;
1489                 }
1490         } else if (hw->fkq_data.tx_queues != NULL && nb_queues != 0) {
1491                 /* re-configure */
1492                 txq = hw->fkq_data.tx_queues;
1493                 for (i = nb_queues; i < old_nb_queues; i++)
1494                         hns3_dev_tx_queue_release(txq[i]);
1495                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
1496                                   RTE_CACHE_LINE_SIZE);
1497                 if (txq == NULL)
1498                         return -ENOMEM;
1499                 if (nb_queues > old_nb_queues) {
1500                         uint16_t new_qs = nb_queues - old_nb_queues;
1501                         memset(txq + old_nb_queues, 0, sizeof(txq[0]) * new_qs);
1502                 }
1503
1504                 hw->fkq_data.tx_queues = txq;
1505         } else if (hw->fkq_data.tx_queues != NULL && nb_queues == 0) {
1506                 txq = hw->fkq_data.tx_queues;
1507                 for (i = nb_queues; i < old_nb_queues; i++)
1508                         hns3_dev_tx_queue_release(txq[i]);
1509
1510                 rte_free(hw->fkq_data.tx_queues);
1511                 hw->fkq_data.tx_queues = NULL;
1512         }
1513         hw->fkq_data.nb_fake_tx_queues = nb_queues;
1514
1515         return 0;
1516 }
1517
1518 int
1519 hns3_set_fake_rx_or_tx_queues(struct rte_eth_dev *dev, uint16_t nb_rx_q,
1520                               uint16_t nb_tx_q)
1521 {
1522         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1523         uint16_t rx_need_add_nb_q;
1524         uint16_t tx_need_add_nb_q;
1525         uint16_t port_id;
1526         uint16_t q;
1527         int ret;
1528
1529         /* Setup new number of fake RX/TX queues and reconfigure device. */
1530         rx_need_add_nb_q = hw->cfg_max_queues - nb_rx_q;
1531         tx_need_add_nb_q = hw->cfg_max_queues - nb_tx_q;
1532         ret = hns3_fake_rx_queue_config(hw, rx_need_add_nb_q);
1533         if (ret) {
1534                 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1535                 return ret;
1536         }
1537
1538         ret = hns3_fake_tx_queue_config(hw, tx_need_add_nb_q);
1539         if (ret) {
1540                 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1541                 goto cfg_fake_tx_q_fail;
1542         }
1543
1544         /* Allocate and set up fake RX queue per Ethernet port. */
1545         port_id = hw->data->port_id;
1546         for (q = 0; q < rx_need_add_nb_q; q++) {
1547                 ret = hns3_fake_rx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1548                                                rte_eth_dev_socket_id(port_id));
1549                 if (ret)
1550                         goto setup_fake_rx_q_fail;
1551         }
1552
1553         /* Allocate and set up fake TX queue per Ethernet port. */
1554         for (q = 0; q < tx_need_add_nb_q; q++) {
1555                 ret = hns3_fake_tx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1556                                                rte_eth_dev_socket_id(port_id));
1557                 if (ret)
1558                         goto setup_fake_tx_q_fail;
1559         }
1560
1561         return 0;
1562
1563 setup_fake_tx_q_fail:
1564 setup_fake_rx_q_fail:
1565         (void)hns3_fake_tx_queue_config(hw, 0);
1566 cfg_fake_tx_q_fail:
1567         (void)hns3_fake_rx_queue_config(hw, 0);
1568
1569         return ret;
1570 }
1571
1572 void
1573 hns3_dev_release_mbufs(struct hns3_adapter *hns)
1574 {
1575         struct rte_eth_dev_data *dev_data = hns->hw.data;
1576         struct hns3_rx_queue *rxq;
1577         struct hns3_tx_queue *txq;
1578         int i;
1579
1580         if (dev_data->rx_queues)
1581                 for (i = 0; i < dev_data->nb_rx_queues; i++) {
1582                         rxq = dev_data->rx_queues[i];
1583                         if (rxq == NULL)
1584                                 continue;
1585                         hns3_rx_queue_release_mbufs(rxq);
1586                 }
1587
1588         if (dev_data->tx_queues)
1589                 for (i = 0; i < dev_data->nb_tx_queues; i++) {
1590                         txq = dev_data->tx_queues[i];
1591                         if (txq == NULL)
1592                                 continue;
1593                         hns3_tx_queue_release_mbufs(txq);
1594                 }
1595 }
1596
1597 static int
1598 hns3_rx_buf_len_calc(struct rte_mempool *mp, uint16_t *rx_buf_len)
1599 {
1600         uint16_t vld_buf_size;
1601         uint16_t num_hw_specs;
1602         uint16_t i;
1603
1604         /*
1605          * hns3 network engine only support to set 4 typical specification, and
1606          * different buffer size will affect the max packet_len and the max
1607          * number of segmentation when hw gro is turned on in receive side. The
1608          * relationship between them is as follows:
1609          *      rx_buf_size     |  max_gro_pkt_len  |  max_gro_nb_seg
1610          * ---------------------|-------------------|----------------
1611          * HNS3_4K_BD_BUF_SIZE  |        60KB       |       15
1612          * HNS3_2K_BD_BUF_SIZE  |        62KB       |       31
1613          * HNS3_1K_BD_BUF_SIZE  |        63KB       |       63
1614          * HNS3_512_BD_BUF_SIZE |      31.5KB       |       63
1615          */
1616         static const uint16_t hw_rx_buf_size[] = {
1617                 HNS3_4K_BD_BUF_SIZE,
1618                 HNS3_2K_BD_BUF_SIZE,
1619                 HNS3_1K_BD_BUF_SIZE,
1620                 HNS3_512_BD_BUF_SIZE
1621         };
1622
1623         vld_buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
1624                         RTE_PKTMBUF_HEADROOM);
1625         if (vld_buf_size < HNS3_MIN_BD_BUF_SIZE)
1626                 return -EINVAL;
1627
1628         num_hw_specs = RTE_DIM(hw_rx_buf_size);
1629         for (i = 0; i < num_hw_specs; i++) {
1630                 if (vld_buf_size >= hw_rx_buf_size[i]) {
1631                         *rx_buf_len = hw_rx_buf_size[i];
1632                         break;
1633                 }
1634         }
1635         return 0;
1636 }
1637
1638 static int
1639 hns3_rxq_conf_runtime_check(struct hns3_hw *hw, uint16_t buf_size,
1640                                 uint16_t nb_desc)
1641 {
1642         struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
1643         struct rte_eth_rxmode *rxmode = &hw->data->dev_conf.rxmode;
1644         eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
1645         uint16_t min_vec_bds;
1646
1647         /*
1648          * HNS3 hardware network engine set scattered as default. If the driver
1649          * is not work in scattered mode and the pkts greater than buf_size
1650          * but smaller than max_rx_pkt_len will be distributed to multiple BDs.
1651          * Driver cannot handle this situation.
1652          */
1653         if (!hw->data->scattered_rx && rxmode->max_rx_pkt_len > buf_size) {
1654                 hns3_err(hw, "max_rx_pkt_len is not allowed to be set greater "
1655                              "than rx_buf_len if scattered is off.");
1656                 return -EINVAL;
1657         }
1658
1659         if (pkt_burst == hns3_recv_pkts_vec) {
1660                 min_vec_bds = HNS3_DEFAULT_RXQ_REARM_THRESH +
1661                               HNS3_DEFAULT_RX_BURST;
1662                 if (nb_desc < min_vec_bds ||
1663                     nb_desc % HNS3_DEFAULT_RXQ_REARM_THRESH) {
1664                         hns3_err(hw, "if Rx burst mode is vector, "
1665                                  "number of descriptor is required to be "
1666                                  "bigger than min vector bds:%u, and could be "
1667                                  "divided by rxq rearm thresh:%u.",
1668                                  min_vec_bds, HNS3_DEFAULT_RXQ_REARM_THRESH);
1669                         return -EINVAL;
1670                 }
1671         }
1672         return 0;
1673 }
1674
1675 static int
1676 hns3_rx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_rxconf *conf,
1677                          struct rte_mempool *mp, uint16_t nb_desc,
1678                          uint16_t *buf_size)
1679 {
1680         int ret;
1681
1682         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
1683             nb_desc % HNS3_ALIGN_RING_DESC) {
1684                 hns3_err(hw, "Number (%u) of rx descriptors is invalid",
1685                          nb_desc);
1686                 return -EINVAL;
1687         }
1688
1689         if (conf->rx_drop_en == 0)
1690                 hns3_warn(hw, "if no descriptors available, packets are always "
1691                           "dropped and rx_drop_en (1) is fixed on");
1692
1693         if (hns3_rx_buf_len_calc(mp, buf_size)) {
1694                 hns3_err(hw, "rxq mbufs' data room size (%u) is not enough! "
1695                                 "minimal data room size (%u).",
1696                                 rte_pktmbuf_data_room_size(mp),
1697                                 HNS3_MIN_BD_BUF_SIZE + RTE_PKTMBUF_HEADROOM);
1698                 return -EINVAL;
1699         }
1700
1701         if (hw->data->dev_started) {
1702                 ret = hns3_rxq_conf_runtime_check(hw, *buf_size, nb_desc);
1703                 if (ret) {
1704                         hns3_err(hw, "Rx queue runtime setup fail.");
1705                         return ret;
1706                 }
1707         }
1708
1709         return 0;
1710 }
1711
1712 uint32_t
1713 hns3_get_tqp_reg_offset(uint16_t queue_id)
1714 {
1715         uint32_t reg_offset;
1716
1717         /* Need an extend offset to config queue > 1024 */
1718         if (queue_id < HNS3_MIN_EXTEND_QUEUE_ID)
1719                 reg_offset = HNS3_TQP_REG_OFFSET + queue_id * HNS3_TQP_REG_SIZE;
1720         else
1721                 reg_offset = HNS3_TQP_REG_OFFSET + HNS3_TQP_EXT_REG_OFFSET +
1722                              (queue_id - HNS3_MIN_EXTEND_QUEUE_ID) *
1723                              HNS3_TQP_REG_SIZE;
1724
1725         return reg_offset;
1726 }
1727
1728 int
1729 hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
1730                     unsigned int socket_id, const struct rte_eth_rxconf *conf,
1731                     struct rte_mempool *mp)
1732 {
1733         struct hns3_adapter *hns = dev->data->dev_private;
1734         struct hns3_hw *hw = &hns->hw;
1735         struct hns3_queue_info q_info;
1736         struct hns3_rx_queue *rxq;
1737         uint16_t rx_buf_size;
1738         int rx_entry_len;
1739         int ret;
1740
1741         ret = hns3_rx_queue_conf_check(hw, conf, mp, nb_desc, &rx_buf_size);
1742         if (ret)
1743                 return ret;
1744
1745         if (dev->data->rx_queues[idx]) {
1746                 hns3_rx_queue_release(dev->data->rx_queues[idx]);
1747                 dev->data->rx_queues[idx] = NULL;
1748         }
1749
1750         q_info.idx = idx;
1751         q_info.socket_id = socket_id;
1752         q_info.nb_desc = nb_desc;
1753         q_info.type = "hns3 RX queue";
1754         q_info.ring_name = "rx_ring";
1755
1756         rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
1757         if (rxq == NULL) {
1758                 hns3_err(hw,
1759                          "Failed to alloc mem and reserve DMA mem for rx ring!");
1760                 return -ENOMEM;
1761         }
1762
1763         rxq->hns = hns;
1764         rxq->ptype_tbl = &hns->ptype_tbl;
1765         rxq->mb_pool = mp;
1766         rxq->rx_free_thresh = (conf->rx_free_thresh > 0) ?
1767                 conf->rx_free_thresh : HNS3_DEFAULT_RX_FREE_THRESH;
1768
1769         rxq->rx_deferred_start = conf->rx_deferred_start;
1770         if (rxq->rx_deferred_start && !hns3_dev_indep_txrx_supported(hw)) {
1771                 hns3_warn(hw, "deferred start is not supported.");
1772                 rxq->rx_deferred_start = false;
1773         }
1774
1775         rx_entry_len = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
1776                         sizeof(struct hns3_entry);
1777         rxq->sw_ring = rte_zmalloc_socket("hns3 RX sw ring", rx_entry_len,
1778                                           RTE_CACHE_LINE_SIZE, socket_id);
1779         if (rxq->sw_ring == NULL) {
1780                 hns3_err(hw, "Failed to allocate memory for rx sw ring!");
1781                 hns3_rx_queue_release(rxq);
1782                 return -ENOMEM;
1783         }
1784
1785         rxq->next_to_use = 0;
1786         rxq->rx_free_hold = 0;
1787         rxq->rx_rearm_start = 0;
1788         rxq->rx_rearm_nb = 0;
1789         rxq->pkt_first_seg = NULL;
1790         rxq->pkt_last_seg = NULL;
1791         rxq->port_id = dev->data->port_id;
1792         /*
1793          * For hns3 PF device, if the VLAN mode is HW_SHIFT_AND_DISCARD_MODE,
1794          * the pvid_sw_discard_en in the queue struct should not be changed,
1795          * because PVID-related operations do not need to be processed by PMD
1796          * driver. For hns3 VF device, whether it needs to process PVID depends
1797          * on the configuration of PF kernel mode netdevice driver. And the
1798          * related PF configuration is delivered through the mailbox and finally
1799          * reflectd in port_base_vlan_cfg.
1800          */
1801         if (hns->is_vf || hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
1802                 rxq->pvid_sw_discard_en = hw->port_base_vlan_cfg.state ==
1803                                        HNS3_PORT_BASE_VLAN_ENABLE;
1804         else
1805                 rxq->pvid_sw_discard_en = false;
1806         rxq->ptype_en = hns3_dev_rxd_adv_layout_supported(hw) ? true : false;
1807         rxq->configured = true;
1808         rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1809                                 idx * HNS3_TQP_REG_SIZE);
1810         rxq->io_base = (void *)((char *)hw->io_base +
1811                                         hns3_get_tqp_reg_offset(idx));
1812         rxq->io_head_reg = (volatile void *)((char *)rxq->io_base +
1813                            HNS3_RING_RX_HEAD_REG);
1814         rxq->rx_buf_len = rx_buf_size;
1815         memset(&rxq->basic_stats, 0, sizeof(struct hns3_rx_basic_stats));
1816         memset(&rxq->err_stats, 0, sizeof(struct hns3_rx_bd_errors_stats));
1817         memset(&rxq->dfx_stats, 0, sizeof(struct hns3_rx_dfx_stats));
1818
1819         /* CRC len set here is used for amending packet length */
1820         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
1821                 rxq->crc_len = RTE_ETHER_CRC_LEN;
1822         else
1823                 rxq->crc_len = 0;
1824
1825         rxq->bulk_mbuf_num = 0;
1826
1827         rte_spinlock_lock(&hw->lock);
1828         dev->data->rx_queues[idx] = rxq;
1829         rte_spinlock_unlock(&hw->lock);
1830
1831         return 0;
1832 }
1833
1834 void
1835 hns3_rx_scattered_reset(struct rte_eth_dev *dev)
1836 {
1837         struct hns3_adapter *hns = dev->data->dev_private;
1838         struct hns3_hw *hw = &hns->hw;
1839
1840         hw->rx_buf_len = 0;
1841         dev->data->scattered_rx = false;
1842 }
1843
1844 void
1845 hns3_rx_scattered_calc(struct rte_eth_dev *dev)
1846 {
1847         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1848         struct hns3_adapter *hns = dev->data->dev_private;
1849         struct hns3_hw *hw = &hns->hw;
1850         struct hns3_rx_queue *rxq;
1851         uint32_t queue_id;
1852
1853         if (dev->data->rx_queues == NULL)
1854                 return;
1855
1856         for (queue_id = 0; queue_id < dev->data->nb_rx_queues; queue_id++) {
1857                 rxq = dev->data->rx_queues[queue_id];
1858                 if (hw->rx_buf_len == 0)
1859                         hw->rx_buf_len = rxq->rx_buf_len;
1860                 else
1861                         hw->rx_buf_len = RTE_MIN(hw->rx_buf_len,
1862                                                  rxq->rx_buf_len);
1863         }
1864
1865         if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_SCATTER ||
1866             dev_conf->rxmode.max_rx_pkt_len > hw->rx_buf_len)
1867                 dev->data->scattered_rx = true;
1868 }
1869
1870 const uint32_t *
1871 hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev)
1872 {
1873         static const uint32_t ptypes[] = {
1874                 RTE_PTYPE_L2_ETHER,
1875                 RTE_PTYPE_L2_ETHER_VLAN,
1876                 RTE_PTYPE_L2_ETHER_QINQ,
1877                 RTE_PTYPE_L2_ETHER_LLDP,
1878                 RTE_PTYPE_L2_ETHER_ARP,
1879                 RTE_PTYPE_L3_IPV4,
1880                 RTE_PTYPE_L3_IPV4_EXT,
1881                 RTE_PTYPE_L3_IPV6,
1882                 RTE_PTYPE_L3_IPV6_EXT,
1883                 RTE_PTYPE_L4_IGMP,
1884                 RTE_PTYPE_L4_ICMP,
1885                 RTE_PTYPE_L4_SCTP,
1886                 RTE_PTYPE_L4_TCP,
1887                 RTE_PTYPE_L4_UDP,
1888                 RTE_PTYPE_TUNNEL_GRE,
1889                 RTE_PTYPE_INNER_L2_ETHER,
1890                 RTE_PTYPE_INNER_L2_ETHER_VLAN,
1891                 RTE_PTYPE_INNER_L2_ETHER_QINQ,
1892                 RTE_PTYPE_INNER_L3_IPV4,
1893                 RTE_PTYPE_INNER_L3_IPV6,
1894                 RTE_PTYPE_INNER_L3_IPV4_EXT,
1895                 RTE_PTYPE_INNER_L3_IPV6_EXT,
1896                 RTE_PTYPE_INNER_L4_UDP,
1897                 RTE_PTYPE_INNER_L4_TCP,
1898                 RTE_PTYPE_INNER_L4_SCTP,
1899                 RTE_PTYPE_INNER_L4_ICMP,
1900                 RTE_PTYPE_TUNNEL_VXLAN,
1901                 RTE_PTYPE_TUNNEL_NVGRE,
1902                 RTE_PTYPE_UNKNOWN
1903         };
1904
1905         if (dev->rx_pkt_burst == hns3_recv_pkts ||
1906             dev->rx_pkt_burst == hns3_recv_scattered_pkts ||
1907             dev->rx_pkt_burst == hns3_recv_pkts_vec ||
1908             dev->rx_pkt_burst == hns3_recv_pkts_vec_sve)
1909                 return ptypes;
1910
1911         return NULL;
1912 }
1913
1914 static void
1915 hns3_init_non_tunnel_ptype_tbl(struct hns3_ptype_table *tbl)
1916 {
1917         tbl->l2l3table[0][0] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4;
1918         tbl->l2l3table[0][1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6;
1919         tbl->l2l3table[0][2] = RTE_PTYPE_L2_ETHER_ARP;
1920         tbl->l2l3table[0][3] = RTE_PTYPE_L2_ETHER;
1921         tbl->l2l3table[0][4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT;
1922         tbl->l2l3table[0][5] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT;
1923         tbl->l2l3table[0][6] = RTE_PTYPE_L2_ETHER_LLDP;
1924         tbl->l2l3table[0][15] = RTE_PTYPE_L2_ETHER;
1925
1926         tbl->l2l3table[1][0] = RTE_PTYPE_L2_ETHER_VLAN | RTE_PTYPE_L3_IPV4;
1927         tbl->l2l3table[1][1] = RTE_PTYPE_L2_ETHER_VLAN | RTE_PTYPE_L3_IPV6;
1928         tbl->l2l3table[1][2] = RTE_PTYPE_L2_ETHER_ARP;
1929         tbl->l2l3table[1][3] = RTE_PTYPE_L2_ETHER_VLAN;
1930         tbl->l2l3table[1][4] = RTE_PTYPE_L2_ETHER_VLAN | RTE_PTYPE_L3_IPV4_EXT;
1931         tbl->l2l3table[1][5] = RTE_PTYPE_L2_ETHER_VLAN | RTE_PTYPE_L3_IPV6_EXT;
1932         tbl->l2l3table[1][6] = RTE_PTYPE_L2_ETHER_LLDP;
1933         tbl->l2l3table[1][15] = RTE_PTYPE_L2_ETHER_VLAN;
1934
1935         tbl->l2l3table[2][0] = RTE_PTYPE_L2_ETHER_QINQ | RTE_PTYPE_L3_IPV4;
1936         tbl->l2l3table[2][1] = RTE_PTYPE_L2_ETHER_QINQ | RTE_PTYPE_L3_IPV6;
1937         tbl->l2l3table[2][2] = RTE_PTYPE_L2_ETHER_ARP;
1938         tbl->l2l3table[2][3] = RTE_PTYPE_L2_ETHER_QINQ;
1939         tbl->l2l3table[2][4] = RTE_PTYPE_L2_ETHER_QINQ | RTE_PTYPE_L3_IPV4_EXT;
1940         tbl->l2l3table[2][5] = RTE_PTYPE_L2_ETHER_QINQ | RTE_PTYPE_L3_IPV6_EXT;
1941         tbl->l2l3table[2][6] = RTE_PTYPE_L2_ETHER_LLDP;
1942         tbl->l2l3table[2][15] = RTE_PTYPE_L2_ETHER_QINQ;
1943
1944         tbl->l4table[0] = RTE_PTYPE_L4_UDP;
1945         tbl->l4table[1] = RTE_PTYPE_L4_TCP;
1946         tbl->l4table[2] = RTE_PTYPE_TUNNEL_GRE;
1947         tbl->l4table[3] = RTE_PTYPE_L4_SCTP;
1948         tbl->l4table[4] = RTE_PTYPE_L4_IGMP;
1949         tbl->l4table[5] = RTE_PTYPE_L4_ICMP;
1950 }
1951
1952 static void
1953 hns3_init_tunnel_ptype_tbl(struct hns3_ptype_table *tbl)
1954 {
1955         tbl->inner_l2table[0] = RTE_PTYPE_INNER_L2_ETHER;
1956         tbl->inner_l2table[1] = RTE_PTYPE_INNER_L2_ETHER_VLAN;
1957         tbl->inner_l2table[2] = RTE_PTYPE_INNER_L2_ETHER_QINQ;
1958
1959         tbl->inner_l3table[0] = RTE_PTYPE_INNER_L3_IPV4;
1960         tbl->inner_l3table[1] = RTE_PTYPE_INNER_L3_IPV6;
1961         /* There is not a ptype for inner ARP/RARP */
1962         tbl->inner_l3table[2] = RTE_PTYPE_UNKNOWN;
1963         tbl->inner_l3table[3] = RTE_PTYPE_UNKNOWN;
1964         tbl->inner_l3table[4] = RTE_PTYPE_INNER_L3_IPV4_EXT;
1965         tbl->inner_l3table[5] = RTE_PTYPE_INNER_L3_IPV6_EXT;
1966
1967         tbl->inner_l4table[0] = RTE_PTYPE_INNER_L4_UDP;
1968         tbl->inner_l4table[1] = RTE_PTYPE_INNER_L4_TCP;
1969         /* There is not a ptype for inner GRE */
1970         tbl->inner_l4table[2] = RTE_PTYPE_UNKNOWN;
1971         tbl->inner_l4table[3] = RTE_PTYPE_INNER_L4_SCTP;
1972         /* There is not a ptype for inner IGMP */
1973         tbl->inner_l4table[4] = RTE_PTYPE_UNKNOWN;
1974         tbl->inner_l4table[5] = RTE_PTYPE_INNER_L4_ICMP;
1975
1976         tbl->ol2table[0] = RTE_PTYPE_L2_ETHER;
1977         tbl->ol2table[1] = RTE_PTYPE_L2_ETHER_VLAN;
1978         tbl->ol2table[2] = RTE_PTYPE_L2_ETHER_QINQ;
1979
1980         tbl->ol3table[0] = RTE_PTYPE_L3_IPV4;
1981         tbl->ol3table[1] = RTE_PTYPE_L3_IPV6;
1982         tbl->ol3table[2] = RTE_PTYPE_UNKNOWN;
1983         tbl->ol3table[3] = RTE_PTYPE_UNKNOWN;
1984         tbl->ol3table[4] = RTE_PTYPE_L3_IPV4_EXT;
1985         tbl->ol3table[5] = RTE_PTYPE_L3_IPV6_EXT;
1986
1987         tbl->ol4table[0] = RTE_PTYPE_UNKNOWN;
1988         tbl->ol4table[1] = RTE_PTYPE_TUNNEL_VXLAN;
1989         tbl->ol4table[2] = RTE_PTYPE_TUNNEL_NVGRE;
1990 }
1991
1992 static void
1993 hns3_init_adv_layout_ptype(struct hns3_ptype_table *tbl)
1994 {
1995         uint32_t *ptype = tbl->ptype;
1996
1997         /* Non-tunnel L2 */
1998         ptype[1] = RTE_PTYPE_L2_ETHER_ARP;
1999         ptype[3] = RTE_PTYPE_L2_ETHER_LLDP;
2000         ptype[8] = RTE_PTYPE_L2_ETHER_TIMESYNC;
2001
2002         /* Non-tunnel IPv4 */
2003         ptype[17] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2004                     RTE_PTYPE_L4_FRAG;
2005         ptype[18] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2006                     RTE_PTYPE_L4_NONFRAG;
2007         ptype[19] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2008                     RTE_PTYPE_L4_UDP;
2009         ptype[20] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2010                     RTE_PTYPE_L4_TCP;
2011         /* The next ptype is GRE over IPv4 */
2012         ptype[21] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN;
2013         ptype[22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2014                     RTE_PTYPE_L4_SCTP;
2015         ptype[23] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2016                     RTE_PTYPE_L4_IGMP;
2017         ptype[24] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2018                     RTE_PTYPE_L4_ICMP;
2019         /* The next ptype is PTP over IPv4 + UDP */
2020         ptype[25] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2021                     RTE_PTYPE_L4_UDP;
2022
2023         /* IPv4 --> GRE/Teredo/VXLAN */
2024         ptype[29] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2025                     RTE_PTYPE_TUNNEL_GRENAT;
2026         /* IPv4 --> GRE/Teredo/VXLAN --> MAC */
2027         ptype[30] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2028                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER;
2029
2030         /* IPv4 --> GRE/Teredo/VXLAN --> MAC --> IPv4 */
2031         ptype[31] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2032                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2033                     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2034                     RTE_PTYPE_INNER_L4_FRAG;
2035         ptype[32] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2036                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2037                     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2038                     RTE_PTYPE_INNER_L4_NONFRAG;
2039         ptype[33] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2040                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2041                     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2042                     RTE_PTYPE_INNER_L4_UDP;
2043         ptype[34] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2044                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2045                     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2046                     RTE_PTYPE_INNER_L4_TCP;
2047         ptype[35] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2048                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2049                     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2050                     RTE_PTYPE_INNER_L4_SCTP;
2051         /* The next ptype's inner L4 is IGMP */
2052         ptype[36] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2053                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2054                     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN;
2055         ptype[37] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2056                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2057                     RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2058                     RTE_PTYPE_INNER_L4_ICMP;
2059
2060         /* IPv4 --> GRE/Teredo/VXLAN --> MAC --> IPv6 */
2061         ptype[39] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2062                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2063                     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2064                     RTE_PTYPE_INNER_L4_FRAG;
2065         ptype[40] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2066                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2067                     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2068                     RTE_PTYPE_INNER_L4_NONFRAG;
2069         ptype[41] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2070                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2071                     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2072                     RTE_PTYPE_INNER_L4_UDP;
2073         ptype[42] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2074                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2075                     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2076                     RTE_PTYPE_INNER_L4_TCP;
2077         ptype[43] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2078                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2079                     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2080                     RTE_PTYPE_INNER_L4_SCTP;
2081         /* The next ptype's inner L4 is IGMP */
2082         ptype[44] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2083                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2084                     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN;
2085         ptype[45] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2086                     RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2087                     RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2088                     RTE_PTYPE_INNER_L4_ICMP;
2089
2090         /* Non-tunnel IPv6 */
2091         ptype[111] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2092                      RTE_PTYPE_L4_FRAG;
2093         ptype[112] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2094                      RTE_PTYPE_L4_NONFRAG;
2095         ptype[113] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2096                      RTE_PTYPE_L4_UDP;
2097         ptype[114] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2098                      RTE_PTYPE_L4_TCP;
2099         /* The next ptype is GRE over IPv6 */
2100         ptype[115] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN;
2101         ptype[116] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2102                      RTE_PTYPE_L4_SCTP;
2103         ptype[117] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2104                      RTE_PTYPE_L4_IGMP;
2105         ptype[118] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2106                      RTE_PTYPE_L4_ICMP;
2107         /* Special for PTP over IPv6 + UDP */
2108         ptype[119] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2109                      RTE_PTYPE_L4_UDP;
2110
2111         /* IPv6 --> GRE/Teredo/VXLAN */
2112         ptype[123] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2113                      RTE_PTYPE_TUNNEL_GRENAT;
2114         /* IPv6 --> GRE/Teredo/VXLAN --> MAC */
2115         ptype[124] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2116                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER;
2117
2118         /* IPv6 --> GRE/Teredo/VXLAN --> MAC --> IPv4 */
2119         ptype[125] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2120                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2121                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2122                      RTE_PTYPE_INNER_L4_FRAG;
2123         ptype[126] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2124                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2125                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2126                      RTE_PTYPE_INNER_L4_NONFRAG;
2127         ptype[127] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2128                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2129                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2130                      RTE_PTYPE_INNER_L4_UDP;
2131         ptype[128] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2132                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2133                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2134                      RTE_PTYPE_INNER_L4_TCP;
2135         ptype[129] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2136                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2137                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2138                      RTE_PTYPE_INNER_L4_SCTP;
2139         /* The next ptype's inner L4 is IGMP */
2140         ptype[130] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2141                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2142                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN;
2143         ptype[131] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2144                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2145                      RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2146                      RTE_PTYPE_INNER_L4_ICMP;
2147
2148         /* IPv6 --> GRE/Teredo/VXLAN --> MAC --> IPv6 */
2149         ptype[133] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2150                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2151                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2152                      RTE_PTYPE_INNER_L4_FRAG;
2153         ptype[134] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2154                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2155                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2156                      RTE_PTYPE_INNER_L4_NONFRAG;
2157         ptype[135] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2158                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2159                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2160                      RTE_PTYPE_INNER_L4_UDP;
2161         ptype[136] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2162                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2163                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2164                      RTE_PTYPE_INNER_L4_TCP;
2165         ptype[137] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2166                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2167                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2168                      RTE_PTYPE_INNER_L4_SCTP;
2169         /* The next ptype's inner L4 is IGMP */
2170         ptype[138] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2171                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2172                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN;
2173         ptype[139] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2174                      RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2175                      RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2176                      RTE_PTYPE_INNER_L4_ICMP;
2177 }
2178
2179 void
2180 hns3_init_rx_ptype_tble(struct rte_eth_dev *dev)
2181 {
2182         struct hns3_adapter *hns = dev->data->dev_private;
2183         struct hns3_ptype_table *tbl = &hns->ptype_tbl;
2184
2185         memset(tbl, 0, sizeof(*tbl));
2186
2187         hns3_init_non_tunnel_ptype_tbl(tbl);
2188         hns3_init_tunnel_ptype_tbl(tbl);
2189         hns3_init_adv_layout_ptype(tbl);
2190 }
2191
2192 static inline void
2193 hns3_rxd_to_vlan_tci(struct hns3_rx_queue *rxq, struct rte_mbuf *mb,
2194                      uint32_t l234_info, const struct hns3_desc *rxd)
2195 {
2196 #define HNS3_STRP_STATUS_NUM            0x4
2197
2198 #define HNS3_NO_STRP_VLAN_VLD           0x0
2199 #define HNS3_INNER_STRP_VLAN_VLD        0x1
2200 #define HNS3_OUTER_STRP_VLAN_VLD        0x2
2201         uint32_t strip_status;
2202         uint32_t report_mode;
2203
2204         /*
2205          * Since HW limitation, the vlan tag will always be inserted into RX
2206          * descriptor when strip the tag from packet, driver needs to determine
2207          * reporting which tag to mbuf according to the PVID configuration
2208          * and vlan striped status.
2209          */
2210         static const uint32_t report_type[][HNS3_STRP_STATUS_NUM] = {
2211                 {
2212                         HNS3_NO_STRP_VLAN_VLD,
2213                         HNS3_OUTER_STRP_VLAN_VLD,
2214                         HNS3_INNER_STRP_VLAN_VLD,
2215                         HNS3_OUTER_STRP_VLAN_VLD
2216                 },
2217                 {
2218                         HNS3_NO_STRP_VLAN_VLD,
2219                         HNS3_NO_STRP_VLAN_VLD,
2220                         HNS3_NO_STRP_VLAN_VLD,
2221                         HNS3_INNER_STRP_VLAN_VLD
2222                 }
2223         };
2224         strip_status = hns3_get_field(l234_info, HNS3_RXD_STRP_TAGP_M,
2225                                       HNS3_RXD_STRP_TAGP_S);
2226         report_mode = report_type[rxq->pvid_sw_discard_en][strip_status];
2227         switch (report_mode) {
2228         case HNS3_NO_STRP_VLAN_VLD:
2229                 mb->vlan_tci = 0;
2230                 return;
2231         case HNS3_INNER_STRP_VLAN_VLD:
2232                 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
2233                 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.vlan_tag);
2234                 return;
2235         case HNS3_OUTER_STRP_VLAN_VLD:
2236                 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
2237                 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.ot_vlan_tag);
2238                 return;
2239         default:
2240                 mb->vlan_tci = 0;
2241                 return;
2242         }
2243 }
2244
2245 static inline void
2246 recalculate_data_len(struct rte_mbuf *first_seg, struct rte_mbuf *last_seg,
2247                     struct rte_mbuf *rxm, struct hns3_rx_queue *rxq,
2248                     uint16_t data_len)
2249 {
2250         uint8_t crc_len = rxq->crc_len;
2251
2252         if (data_len <= crc_len) {
2253                 rte_pktmbuf_free_seg(rxm);
2254                 first_seg->nb_segs--;
2255                 last_seg->data_len = (uint16_t)(last_seg->data_len -
2256                         (crc_len - data_len));
2257                 last_seg->next = NULL;
2258         } else
2259                 rxm->data_len = (uint16_t)(data_len - crc_len);
2260 }
2261
2262 static inline struct rte_mbuf *
2263 hns3_rx_alloc_buffer(struct hns3_rx_queue *rxq)
2264 {
2265         int ret;
2266
2267         if (likely(rxq->bulk_mbuf_num > 0))
2268                 return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];
2269
2270         ret = rte_mempool_get_bulk(rxq->mb_pool, (void **)rxq->bulk_mbuf,
2271                                    HNS3_BULK_ALLOC_MBUF_NUM);
2272         if (likely(ret == 0)) {
2273                 rxq->bulk_mbuf_num = HNS3_BULK_ALLOC_MBUF_NUM;
2274                 return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];
2275         } else
2276                 return rte_mbuf_raw_alloc(rxq->mb_pool);
2277 }
2278
2279 uint16_t
2280 hns3_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
2281 {
2282         volatile struct hns3_desc *rx_ring;  /* RX ring (desc) */
2283         volatile struct hns3_desc *rxdp;     /* pointer of the current desc */
2284         struct hns3_rx_queue *rxq;      /* RX queue */
2285         struct hns3_entry *sw_ring;
2286         struct hns3_entry *rxe;
2287         struct hns3_desc rxd;
2288         struct rte_mbuf *nmb;           /* pointer of the new mbuf */
2289         struct rte_mbuf *rxm;
2290         uint32_t bd_base_info;
2291         uint32_t cksum_err;
2292         uint32_t l234_info;
2293         uint32_t ol_info;
2294         uint64_t dma_addr;
2295         uint16_t nb_rx_bd;
2296         uint16_t nb_rx;
2297         uint16_t rx_id;
2298         int ret;
2299
2300         nb_rx = 0;
2301         nb_rx_bd = 0;
2302         rxq = rx_queue;
2303         rx_ring = rxq->rx_ring;
2304         sw_ring = rxq->sw_ring;
2305         rx_id = rxq->next_to_use;
2306
2307         while (nb_rx < nb_pkts) {
2308                 rxdp = &rx_ring[rx_id];
2309                 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
2310                 if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
2311                         break;
2312
2313                 rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
2314                            (1u << HNS3_RXD_VLD_B)];
2315
2316                 nmb = hns3_rx_alloc_buffer(rxq);
2317                 if (unlikely(nmb == NULL)) {
2318                         uint16_t port_id;
2319
2320                         port_id = rxq->port_id;
2321                         rte_eth_devices[port_id].data->rx_mbuf_alloc_failed++;
2322                         break;
2323                 }
2324
2325                 nb_rx_bd++;
2326                 rxe = &sw_ring[rx_id];
2327                 rx_id++;
2328                 if (unlikely(rx_id == rxq->nb_rx_desc))
2329                         rx_id = 0;
2330
2331                 rte_prefetch0(sw_ring[rx_id].mbuf);
2332                 if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
2333                         rte_prefetch0(&rx_ring[rx_id]);
2334                         rte_prefetch0(&sw_ring[rx_id]);
2335                 }
2336
2337                 rxm = rxe->mbuf;
2338                 rxe->mbuf = nmb;
2339
2340                 dma_addr = rte_mbuf_data_iova_default(nmb);
2341                 rxdp->addr = rte_cpu_to_le_64(dma_addr);
2342                 rxdp->rx.bd_base_info = 0;
2343
2344                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
2345                 rxm->pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.rx.pkt_len)) -
2346                                 rxq->crc_len;
2347                 rxm->data_len = rxm->pkt_len;
2348                 rxm->port = rxq->port_id;
2349                 rxm->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
2350                 rxm->ol_flags = PKT_RX_RSS_HASH;
2351                 if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
2352                         rxm->hash.fdir.hi =
2353                                 rte_le_to_cpu_16(rxd.rx.fd_id);
2354                         rxm->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
2355                 }
2356                 rxm->nb_segs = 1;
2357                 rxm->next = NULL;
2358
2359                 /* Load remained descriptor data and extract necessary fields */
2360                 l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
2361                 ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
2362                 ret = hns3_handle_bdinfo(rxq, rxm, bd_base_info,
2363                                          l234_info, &cksum_err);
2364                 if (unlikely(ret))
2365                         goto pkt_err;
2366
2367                 rxm->packet_type = hns3_rx_calc_ptype(rxq, l234_info, ol_info);
2368
2369                 if (likely(bd_base_info & BIT(HNS3_RXD_L3L4P_B)))
2370                         hns3_rx_set_cksum_flag(rxm, rxm->packet_type,
2371                                                cksum_err);
2372                 hns3_rxd_to_vlan_tci(rxq, rxm, l234_info, &rxd);
2373
2374                 /* Increment bytes counter  */
2375                 rxq->basic_stats.bytes += rxm->pkt_len;
2376
2377                 rx_pkts[nb_rx++] = rxm;
2378                 continue;
2379 pkt_err:
2380                 rte_pktmbuf_free(rxm);
2381         }
2382
2383         rxq->next_to_use = rx_id;
2384         rxq->rx_free_hold += nb_rx_bd;
2385         if (rxq->rx_free_hold > rxq->rx_free_thresh) {
2386                 hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
2387                 rxq->rx_free_hold = 0;
2388         }
2389
2390         return nb_rx;
2391 }
2392
2393 uint16_t
2394 hns3_recv_scattered_pkts(void *rx_queue,
2395                          struct rte_mbuf **rx_pkts,
2396                          uint16_t nb_pkts)
2397 {
2398         volatile struct hns3_desc *rx_ring;  /* RX ring (desc) */
2399         volatile struct hns3_desc *rxdp;     /* pointer of the current desc */
2400         struct hns3_rx_queue *rxq;      /* RX queue */
2401         struct hns3_entry *sw_ring;
2402         struct hns3_entry *rxe;
2403         struct rte_mbuf *first_seg;
2404         struct rte_mbuf *last_seg;
2405         struct hns3_desc rxd;
2406         struct rte_mbuf *nmb;           /* pointer of the new mbuf */
2407         struct rte_mbuf *rxm;
2408         struct rte_eth_dev *dev;
2409         uint32_t bd_base_info;
2410         uint32_t cksum_err;
2411         uint32_t l234_info;
2412         uint32_t gro_size;
2413         uint32_t ol_info;
2414         uint64_t dma_addr;
2415         uint16_t nb_rx_bd;
2416         uint16_t nb_rx;
2417         uint16_t rx_id;
2418         int ret;
2419
2420         nb_rx = 0;
2421         nb_rx_bd = 0;
2422         rxq = rx_queue;
2423
2424         rx_id = rxq->next_to_use;
2425         rx_ring = rxq->rx_ring;
2426         sw_ring = rxq->sw_ring;
2427         first_seg = rxq->pkt_first_seg;
2428         last_seg = rxq->pkt_last_seg;
2429
2430         while (nb_rx < nb_pkts) {
2431                 rxdp = &rx_ring[rx_id];
2432                 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
2433                 if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
2434                         break;
2435
2436                 /*
2437                  * The interactive process between software and hardware of
2438                  * receiving a new packet in hns3 network engine:
2439                  * 1. Hardware network engine firstly writes the packet content
2440                  *    to the memory pointed by the 'addr' field of the Rx Buffer
2441                  *    Descriptor, secondly fills the result of parsing the
2442                  *    packet include the valid field into the Rx Buffer
2443                  *    Descriptor in one write operation.
2444                  * 2. Driver reads the Rx BD's valid field in the loop to check
2445                  *    whether it's valid, if valid then assign a new address to
2446                  *    the addr field, clear the valid field, get the other
2447                  *    information of the packet by parsing Rx BD's other fields,
2448                  *    finally write back the number of Rx BDs processed by the
2449                  *    driver to the HNS3_RING_RX_HEAD_REG register to inform
2450                  *    hardware.
2451                  * In the above process, the ordering is very important. We must
2452                  * make sure that CPU read Rx BD's other fields only after the
2453                  * Rx BD is valid.
2454                  *
2455                  * There are two type of re-ordering: compiler re-ordering and
2456                  * CPU re-ordering under the ARMv8 architecture.
2457                  * 1. we use volatile to deal with compiler re-ordering, so you
2458                  *    can see that rx_ring/rxdp defined with volatile.
2459                  * 2. we commonly use memory barrier to deal with CPU
2460                  *    re-ordering, but the cost is high.
2461                  *
2462                  * In order to solve the high cost of using memory barrier, we
2463                  * use the data dependency order under the ARMv8 architecture,
2464                  * for example:
2465                  *      instr01: load A
2466                  *      instr02: load B <- A
2467                  * the instr02 will always execute after instr01.
2468                  *
2469                  * To construct the data dependency ordering, we use the
2470                  * following assignment:
2471                  *      rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
2472                  *                 (1u<<HNS3_RXD_VLD_B)]
2473                  * Using gcc compiler under the ARMv8 architecture, the related
2474                  * assembly code example as follows:
2475                  * note: (1u << HNS3_RXD_VLD_B) equal 0x10
2476                  *      instr01: ldr w26, [x22, #28]  --read bd_base_info
2477                  *      instr02: and w0, w26, #0x10   --calc bd_base_info & 0x10
2478                  *      instr03: sub w0, w0, #0x10    --calc (bd_base_info &
2479                  *                                            0x10) - 0x10
2480                  *      instr04: add x0, x22, x0, lsl #5 --calc copy source addr
2481                  *      instr05: ldp x2, x3, [x0]
2482                  *      instr06: stp x2, x3, [x29, #256] --copy BD's [0 ~ 15]B
2483                  *      instr07: ldp x4, x5, [x0, #16]
2484                  *      instr08: stp x4, x5, [x29, #272] --copy BD's [16 ~ 31]B
2485                  * the instr05~08 depend on x0's value, x0 depent on w26's
2486                  * value, the w26 is the bd_base_info, this form the data
2487                  * dependency ordering.
2488                  * note: if BD is valid, (bd_base_info & (1u<<HNS3_RXD_VLD_B)) -
2489                  *       (1u<<HNS3_RXD_VLD_B) will always zero, so the
2490                  *       assignment is correct.
2491                  *
2492                  * So we use the data dependency ordering instead of memory
2493                  * barrier to improve receive performance.
2494                  */
2495                 rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
2496                            (1u << HNS3_RXD_VLD_B)];
2497
2498                 nmb = hns3_rx_alloc_buffer(rxq);
2499                 if (unlikely(nmb == NULL)) {
2500                         dev = &rte_eth_devices[rxq->port_id];
2501                         dev->data->rx_mbuf_alloc_failed++;
2502                         break;
2503                 }
2504
2505                 nb_rx_bd++;
2506                 rxe = &sw_ring[rx_id];
2507                 rx_id++;
2508                 if (unlikely(rx_id == rxq->nb_rx_desc))
2509                         rx_id = 0;
2510
2511                 rte_prefetch0(sw_ring[rx_id].mbuf);
2512                 if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
2513                         rte_prefetch0(&rx_ring[rx_id]);
2514                         rte_prefetch0(&sw_ring[rx_id]);
2515                 }
2516
2517                 rxm = rxe->mbuf;
2518                 rxe->mbuf = nmb;
2519
2520                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
2521                 rxdp->rx.bd_base_info = 0;
2522                 rxdp->addr = dma_addr;
2523
2524                 if (first_seg == NULL) {
2525                         first_seg = rxm;
2526                         first_seg->nb_segs = 1;
2527                 } else {
2528                         first_seg->nb_segs++;
2529                         last_seg->next = rxm;
2530                 }
2531
2532                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
2533                 rxm->data_len = rte_le_to_cpu_16(rxd.rx.size);
2534
2535                 if (!(bd_base_info & BIT(HNS3_RXD_FE_B))) {
2536                         last_seg = rxm;
2537                         rxm->next = NULL;
2538                         continue;
2539                 }
2540
2541                 /*
2542                  * The last buffer of the received packet. packet len from
2543                  * buffer description may contains CRC len, packet len should
2544                  * subtract it, same as data len.
2545                  */
2546                 first_seg->pkt_len = rte_le_to_cpu_16(rxd.rx.pkt_len);
2547
2548                 /*
2549                  * This is the last buffer of the received packet. If the CRC
2550                  * is not stripped by the hardware:
2551                  *  - Subtract the CRC length from the total packet length.
2552                  *  - If the last buffer only contains the whole CRC or a part
2553                  *  of it, free the mbuf associated to the last buffer. If part
2554                  *  of the CRC is also contained in the previous mbuf, subtract
2555                  *  the length of that CRC part from the data length of the
2556                  *  previous mbuf.
2557                  */
2558                 rxm->next = NULL;
2559                 if (unlikely(rxq->crc_len > 0)) {
2560                         first_seg->pkt_len -= rxq->crc_len;
2561                         recalculate_data_len(first_seg, last_seg, rxm, rxq,
2562                                 rxm->data_len);
2563                 }
2564
2565                 first_seg->port = rxq->port_id;
2566                 first_seg->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
2567                 first_seg->ol_flags = PKT_RX_RSS_HASH;
2568                 if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
2569                         first_seg->hash.fdir.hi =
2570                                 rte_le_to_cpu_16(rxd.rx.fd_id);
2571                         first_seg->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
2572                 }
2573
2574                 gro_size = hns3_get_field(bd_base_info, HNS3_RXD_GRO_SIZE_M,
2575                                           HNS3_RXD_GRO_SIZE_S);
2576                 if (gro_size != 0) {
2577                         first_seg->ol_flags |= PKT_RX_LRO;
2578                         first_seg->tso_segsz = gro_size;
2579                 }
2580
2581                 l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
2582                 ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
2583                 ret = hns3_handle_bdinfo(rxq, first_seg, bd_base_info,
2584                                          l234_info, &cksum_err);
2585                 if (unlikely(ret))
2586                         goto pkt_err;
2587
2588                 first_seg->packet_type = hns3_rx_calc_ptype(rxq,
2589                                                 l234_info, ol_info);
2590
2591                 if (bd_base_info & BIT(HNS3_RXD_L3L4P_B))
2592                         hns3_rx_set_cksum_flag(first_seg,
2593                                                first_seg->packet_type,
2594                                                cksum_err);
2595                 hns3_rxd_to_vlan_tci(rxq, first_seg, l234_info, &rxd);
2596
2597                 /* Increment bytes counter */
2598                 rxq->basic_stats.bytes += first_seg->pkt_len;
2599
2600                 rx_pkts[nb_rx++] = first_seg;
2601                 first_seg = NULL;
2602                 continue;
2603 pkt_err:
2604                 rte_pktmbuf_free(first_seg);
2605                 first_seg = NULL;
2606         }
2607
2608         rxq->next_to_use = rx_id;
2609         rxq->pkt_first_seg = first_seg;
2610         rxq->pkt_last_seg = last_seg;
2611
2612         rxq->rx_free_hold += nb_rx_bd;
2613         if (rxq->rx_free_hold > rxq->rx_free_thresh) {
2614                 hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
2615                 rxq->rx_free_hold = 0;
2616         }
2617
2618         return nb_rx;
2619 }
2620
2621 void __rte_weak
2622 hns3_rxq_vec_setup(__rte_unused struct hns3_rx_queue *rxq)
2623 {
2624 }
2625
2626 int __rte_weak
2627 hns3_rx_check_vec_support(__rte_unused struct rte_eth_dev *dev)
2628 {
2629         return -ENOTSUP;
2630 }
2631
2632 uint16_t __rte_weak
2633 hns3_recv_pkts_vec(__rte_unused void *tx_queue,
2634                    __rte_unused struct rte_mbuf **rx_pkts,
2635                    __rte_unused uint16_t nb_pkts)
2636 {
2637         return 0;
2638 }
2639
2640 uint16_t __rte_weak
2641 hns3_recv_pkts_vec_sve(__rte_unused void *tx_queue,
2642                        __rte_unused struct rte_mbuf **rx_pkts,
2643                        __rte_unused uint16_t nb_pkts)
2644 {
2645         return 0;
2646 }
2647
2648 int
2649 hns3_rx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
2650                        struct rte_eth_burst_mode *mode)
2651 {
2652         static const struct {
2653                 eth_rx_burst_t pkt_burst;
2654                 const char *info;
2655         } burst_infos[] = {
2656                 { hns3_recv_pkts,               "Scalar" },
2657                 { hns3_recv_scattered_pkts,     "Scalar Scattered" },
2658                 { hns3_recv_pkts_vec,           "Vector Neon" },
2659                 { hns3_recv_pkts_vec_sve,       "Vector Sve" },
2660         };
2661
2662         eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
2663         int ret = -EINVAL;
2664         unsigned int i;
2665
2666         for (i = 0; i < RTE_DIM(burst_infos); i++) {
2667                 if (pkt_burst == burst_infos[i].pkt_burst) {
2668                         snprintf(mode->info, sizeof(mode->info), "%s",
2669                                  burst_infos[i].info);
2670                         ret = 0;
2671                         break;
2672                 }
2673         }
2674
2675         return ret;
2676 }
2677
2678 static bool
2679 hns3_check_sve_support(void)
2680 {
2681 #if defined(RTE_ARCH_ARM64) && defined(__ARM_FEATURE_SVE)
2682         if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SVE))
2683                 return true;
2684 #endif
2685         return false;
2686 }
2687
2688 static eth_rx_burst_t
2689 hns3_get_rx_function(struct rte_eth_dev *dev)
2690 {
2691         struct hns3_adapter *hns = dev->data->dev_private;
2692         uint64_t offloads = dev->data->dev_conf.rxmode.offloads;
2693         bool vec_allowed, sve_allowed, simple_allowed;
2694
2695         vec_allowed = hns->rx_vec_allowed &&
2696                       hns3_rx_check_vec_support(dev) == 0;
2697         sve_allowed = vec_allowed && hns3_check_sve_support();
2698         simple_allowed = hns->rx_simple_allowed && !dev->data->scattered_rx &&
2699                          (offloads & DEV_RX_OFFLOAD_TCP_LRO) == 0;
2700
2701         if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_VEC && vec_allowed)
2702                 return hns3_recv_pkts_vec;
2703         if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_SVE && sve_allowed)
2704                 return hns3_recv_pkts_vec_sve;
2705         if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_SIMPLE && simple_allowed)
2706                 return hns3_recv_pkts;
2707         if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_COMMON)
2708                 return hns3_recv_scattered_pkts;
2709
2710         if (vec_allowed)
2711                 return hns3_recv_pkts_vec;
2712         if (simple_allowed)
2713                 return hns3_recv_pkts;
2714
2715         return hns3_recv_scattered_pkts;
2716 }
2717
2718 static int
2719 hns3_tx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_txconf *conf,
2720                          uint16_t nb_desc, uint16_t *tx_rs_thresh,
2721                          uint16_t *tx_free_thresh, uint16_t idx)
2722 {
2723 #define HNS3_TX_RS_FREE_THRESH_GAP      8
2724         uint16_t rs_thresh, free_thresh, fast_free_thresh;
2725
2726         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
2727             nb_desc % HNS3_ALIGN_RING_DESC) {
2728                 hns3_err(hw, "number (%u) of tx descriptors is invalid",
2729                          nb_desc);
2730                 return -EINVAL;
2731         }
2732
2733         rs_thresh = (conf->tx_rs_thresh > 0) ?
2734                         conf->tx_rs_thresh : HNS3_DEFAULT_TX_RS_THRESH;
2735         free_thresh = (conf->tx_free_thresh > 0) ?
2736                         conf->tx_free_thresh : HNS3_DEFAULT_TX_FREE_THRESH;
2737         if (rs_thresh + free_thresh > nb_desc || nb_desc % rs_thresh ||
2738             rs_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP ||
2739             free_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP) {
2740                 hns3_err(hw, "tx_rs_thresh (%u) tx_free_thresh (%u) nb_desc "
2741                          "(%u) of tx descriptors for port=%u queue=%u check "
2742                          "fail!",
2743                          rs_thresh, free_thresh, nb_desc, hw->data->port_id,
2744                          idx);
2745                 return -EINVAL;
2746         }
2747
2748         if (conf->tx_free_thresh == 0) {
2749                 /* Fast free Tx memory buffer to improve cache hit rate */
2750                 fast_free_thresh = nb_desc - rs_thresh;
2751                 if (fast_free_thresh >=
2752                     HNS3_TX_FAST_FREE_AHEAD + HNS3_DEFAULT_TX_FREE_THRESH)
2753                         free_thresh = fast_free_thresh -
2754                                         HNS3_TX_FAST_FREE_AHEAD;
2755         }
2756
2757         *tx_rs_thresh = rs_thresh;
2758         *tx_free_thresh = free_thresh;
2759         return 0;
2760 }
2761
2762 int
2763 hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
2764                     unsigned int socket_id, const struct rte_eth_txconf *conf)
2765 {
2766         struct hns3_adapter *hns = dev->data->dev_private;
2767         uint16_t tx_rs_thresh, tx_free_thresh;
2768         struct hns3_hw *hw = &hns->hw;
2769         struct hns3_queue_info q_info;
2770         struct hns3_tx_queue *txq;
2771         int tx_entry_len;
2772         int ret;
2773
2774         ret = hns3_tx_queue_conf_check(hw, conf, nb_desc,
2775                                        &tx_rs_thresh, &tx_free_thresh, idx);
2776         if (ret)
2777                 return ret;
2778
2779         if (dev->data->tx_queues[idx] != NULL) {
2780                 hns3_tx_queue_release(dev->data->tx_queues[idx]);
2781                 dev->data->tx_queues[idx] = NULL;
2782         }
2783
2784         q_info.idx = idx;
2785         q_info.socket_id = socket_id;
2786         q_info.nb_desc = nb_desc;
2787         q_info.type = "hns3 TX queue";
2788         q_info.ring_name = "tx_ring";
2789         txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
2790         if (txq == NULL) {
2791                 hns3_err(hw,
2792                          "Failed to alloc mem and reserve DMA mem for tx ring!");
2793                 return -ENOMEM;
2794         }
2795
2796         txq->tx_deferred_start = conf->tx_deferred_start;
2797         if (txq->tx_deferred_start && !hns3_dev_indep_txrx_supported(hw)) {
2798                 hns3_warn(hw, "deferred start is not supported.");
2799                 txq->tx_deferred_start = false;
2800         }
2801
2802         tx_entry_len = sizeof(struct hns3_entry) * txq->nb_tx_desc;
2803         txq->sw_ring = rte_zmalloc_socket("hns3 TX sw ring", tx_entry_len,
2804                                           RTE_CACHE_LINE_SIZE, socket_id);
2805         if (txq->sw_ring == NULL) {
2806                 hns3_err(hw, "Failed to allocate memory for tx sw ring!");
2807                 hns3_tx_queue_release(txq);
2808                 return -ENOMEM;
2809         }
2810
2811         txq->hns = hns;
2812         txq->next_to_use = 0;
2813         txq->next_to_clean = 0;
2814         txq->tx_bd_ready = txq->nb_tx_desc - 1;
2815         txq->tx_free_thresh = tx_free_thresh;
2816         txq->tx_rs_thresh = tx_rs_thresh;
2817         txq->free = rte_zmalloc_socket("hns3 TX mbuf free array",
2818                                 sizeof(struct rte_mbuf *) * txq->tx_rs_thresh,
2819                                 RTE_CACHE_LINE_SIZE, socket_id);
2820         if (!txq->free) {
2821                 hns3_err(hw, "failed to allocate tx mbuf free array!");
2822                 hns3_tx_queue_release(txq);
2823                 return -ENOMEM;
2824         }
2825
2826         txq->port_id = dev->data->port_id;
2827         /*
2828          * For hns3 PF device, if the VLAN mode is HW_SHIFT_AND_DISCARD_MODE,
2829          * the pvid_sw_shift_en in the queue struct should not be changed,
2830          * because PVID-related operations do not need to be processed by PMD
2831          * driver. For hns3 VF device, whether it needs to process PVID depends
2832          * on the configuration of PF kernel mode netdev driver. And the
2833          * related PF configuration is delivered through the mailbox and finally
2834          * reflectd in port_base_vlan_cfg.
2835          */
2836         if (hns->is_vf || hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
2837                 txq->pvid_sw_shift_en = hw->port_base_vlan_cfg.state ==
2838                                         HNS3_PORT_BASE_VLAN_ENABLE;
2839         else
2840                 txq->pvid_sw_shift_en = false;
2841         txq->max_non_tso_bd_num = hw->max_non_tso_bd_num;
2842         txq->configured = true;
2843         txq->io_base = (void *)((char *)hw->io_base +
2844                                                 hns3_get_tqp_reg_offset(idx));
2845         txq->io_tail_reg = (volatile void *)((char *)txq->io_base +
2846                                              HNS3_RING_TX_TAIL_REG);
2847         txq->min_tx_pkt_len = hw->min_tx_pkt_len;
2848         txq->tso_mode = hw->tso_mode;
2849         txq->udp_cksum_mode = hw->udp_cksum_mode;
2850         memset(&txq->basic_stats, 0, sizeof(struct hns3_tx_basic_stats));
2851         memset(&txq->dfx_stats, 0, sizeof(struct hns3_tx_dfx_stats));
2852
2853         rte_spinlock_lock(&hw->lock);
2854         dev->data->tx_queues[idx] = txq;
2855         rte_spinlock_unlock(&hw->lock);
2856
2857         return 0;
2858 }
2859
2860 static void
2861 hns3_tx_free_useless_buffer(struct hns3_tx_queue *txq)
2862 {
2863         uint16_t tx_next_clean = txq->next_to_clean;
2864         uint16_t tx_next_use   = txq->next_to_use;
2865         uint16_t tx_bd_ready   = txq->tx_bd_ready;
2866         uint16_t tx_bd_max     = txq->nb_tx_desc;
2867         struct hns3_entry *tx_bak_pkt = &txq->sw_ring[tx_next_clean];
2868         struct hns3_desc *desc = &txq->tx_ring[tx_next_clean];
2869         struct rte_mbuf *mbuf;
2870
2871         while ((!(desc->tx.tp_fe_sc_vld_ra_ri &
2872                 rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B)))) &&
2873                 tx_next_use != tx_next_clean) {
2874                 mbuf = tx_bak_pkt->mbuf;
2875                 if (mbuf) {
2876                         rte_pktmbuf_free_seg(mbuf);
2877                         tx_bak_pkt->mbuf = NULL;
2878                 }
2879
2880                 desc++;
2881                 tx_bak_pkt++;
2882                 tx_next_clean++;
2883                 tx_bd_ready++;
2884
2885                 if (tx_next_clean >= tx_bd_max) {
2886                         tx_next_clean = 0;
2887                         desc = txq->tx_ring;
2888                         tx_bak_pkt = txq->sw_ring;
2889                 }
2890         }
2891
2892         txq->next_to_clean = tx_next_clean;
2893         txq->tx_bd_ready   = tx_bd_ready;
2894 }
2895
2896 int
2897 hns3_config_gro(struct hns3_hw *hw, bool en)
2898 {
2899         struct hns3_cfg_gro_status_cmd *req;
2900         struct hns3_cmd_desc desc;
2901         int ret;
2902
2903         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GRO_GENERIC_CONFIG, false);
2904         req = (struct hns3_cfg_gro_status_cmd *)desc.data;
2905
2906         req->gro_en = rte_cpu_to_le_16(en ? 1 : 0);
2907
2908         ret = hns3_cmd_send(hw, &desc, 1);
2909         if (ret)
2910                 hns3_err(hw, "%s hardware GRO failed, ret = %d",
2911                          en ? "enable" : "disable", ret);
2912
2913         return ret;
2914 }
2915
2916 int
2917 hns3_restore_gro_conf(struct hns3_hw *hw)
2918 {
2919         uint64_t offloads;
2920         bool gro_en;
2921         int ret;
2922
2923         offloads = hw->data->dev_conf.rxmode.offloads;
2924         gro_en = offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
2925         ret = hns3_config_gro(hw, gro_en);
2926         if (ret)
2927                 hns3_err(hw, "restore hardware GRO to %s failed, ret = %d",
2928                          gro_en ? "enabled" : "disabled", ret);
2929
2930         return ret;
2931 }
2932
2933 static inline bool
2934 hns3_pkt_is_tso(struct rte_mbuf *m)
2935 {
2936         return (m->tso_segsz != 0 && m->ol_flags & PKT_TX_TCP_SEG);
2937 }
2938
2939 static void
2940 hns3_set_tso(struct hns3_desc *desc, uint32_t paylen, struct rte_mbuf *rxm)
2941 {
2942         if (!hns3_pkt_is_tso(rxm))
2943                 return;
2944
2945         if (paylen <= rxm->tso_segsz)
2946                 return;
2947
2948         desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(BIT(HNS3_TXD_TSO_B));
2949         desc->tx.mss = rte_cpu_to_le_16(rxm->tso_segsz);
2950 }
2951
2952 static inline void
2953 hns3_fill_per_desc(struct hns3_desc *desc, struct rte_mbuf *rxm)
2954 {
2955         desc->addr = rte_mbuf_data_iova(rxm);
2956         desc->tx.send_size = rte_cpu_to_le_16(rte_pktmbuf_data_len(rxm));
2957         desc->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B));
2958 }
2959
2960 static void
2961 hns3_fill_first_desc(struct hns3_tx_queue *txq, struct hns3_desc *desc,
2962                      struct rte_mbuf *rxm)
2963 {
2964         uint64_t ol_flags = rxm->ol_flags;
2965         uint32_t hdr_len;
2966         uint32_t paylen;
2967
2968         hdr_len = rxm->l2_len + rxm->l3_len + rxm->l4_len;
2969         hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
2970                            rxm->outer_l2_len + rxm->outer_l3_len : 0;
2971         paylen = rxm->pkt_len - hdr_len;
2972         desc->tx.paylen_fd_dop_ol4cs |= rte_cpu_to_le_32(paylen);
2973         hns3_set_tso(desc, paylen, rxm);
2974
2975         /*
2976          * Currently, hardware doesn't support more than two layers VLAN offload
2977          * in Tx direction based on hns3 network engine. So when the number of
2978          * VLANs in the packets represented by rxm plus the number of VLAN
2979          * offload by hardware such as PVID etc, exceeds two, the packets will
2980          * be discarded or the original VLAN of the packets will be overwitted
2981          * by hardware. When the PF PVID is enabled by calling the API function
2982          * named rte_eth_dev_set_vlan_pvid or the VF PVID is enabled by the hns3
2983          * PF kernel ether driver, the outer VLAN tag will always be the PVID.
2984          * To avoid the VLAN of Tx descriptor is overwritten by PVID, it should
2985          * be added to the position close to the IP header when PVID is enabled.
2986          */
2987         if (!txq->pvid_sw_shift_en && ol_flags & (PKT_TX_VLAN_PKT |
2988                                 PKT_TX_QINQ_PKT)) {
2989                 desc->tx.ol_type_vlan_len_msec |=
2990                                 rte_cpu_to_le_32(BIT(HNS3_TXD_OVLAN_B));
2991                 if (ol_flags & PKT_TX_QINQ_PKT)
2992                         desc->tx.outer_vlan_tag =
2993                                         rte_cpu_to_le_16(rxm->vlan_tci_outer);
2994                 else
2995                         desc->tx.outer_vlan_tag =
2996                                         rte_cpu_to_le_16(rxm->vlan_tci);
2997         }
2998
2999         if (ol_flags & PKT_TX_QINQ_PKT ||
3000             ((ol_flags & PKT_TX_VLAN_PKT) && txq->pvid_sw_shift_en)) {
3001                 desc->tx.type_cs_vlan_tso_len |=
3002                                         rte_cpu_to_le_32(BIT(HNS3_TXD_VLAN_B));
3003                 desc->tx.vlan_tag = rte_cpu_to_le_16(rxm->vlan_tci);
3004         }
3005 }
3006
3007 static inline int
3008 hns3_tx_alloc_mbufs(struct rte_mempool *mb_pool, uint16_t nb_new_buf,
3009                         struct rte_mbuf **alloc_mbuf)
3010 {
3011 #define MAX_NON_TSO_BD_PER_PKT 18
3012         struct rte_mbuf *pkt_segs[MAX_NON_TSO_BD_PER_PKT];
3013         uint16_t i;
3014
3015         /* Allocate enough mbufs */
3016         if (rte_mempool_get_bulk(mb_pool, (void **)pkt_segs, nb_new_buf))
3017                 return -ENOMEM;
3018
3019         for (i = 0; i < nb_new_buf - 1; i++)
3020                 pkt_segs[i]->next = pkt_segs[i + 1];
3021
3022         pkt_segs[nb_new_buf - 1]->next = NULL;
3023         pkt_segs[0]->nb_segs = nb_new_buf;
3024         *alloc_mbuf = pkt_segs[0];
3025
3026         return 0;
3027 }
3028
3029 static inline void
3030 hns3_pktmbuf_copy_hdr(struct rte_mbuf *new_pkt, struct rte_mbuf *old_pkt)
3031 {
3032         new_pkt->ol_flags = old_pkt->ol_flags;
3033         new_pkt->pkt_len = rte_pktmbuf_pkt_len(old_pkt);
3034         new_pkt->outer_l2_len = old_pkt->outer_l2_len;
3035         new_pkt->outer_l3_len = old_pkt->outer_l3_len;
3036         new_pkt->l2_len = old_pkt->l2_len;
3037         new_pkt->l3_len = old_pkt->l3_len;
3038         new_pkt->l4_len = old_pkt->l4_len;
3039         new_pkt->vlan_tci_outer = old_pkt->vlan_tci_outer;
3040         new_pkt->vlan_tci = old_pkt->vlan_tci;
3041 }
3042
3043 static int
3044 hns3_reassemble_tx_pkts(struct rte_mbuf *tx_pkt, struct rte_mbuf **new_pkt,
3045                                   uint8_t max_non_tso_bd_num)
3046 {
3047         struct rte_mempool *mb_pool;
3048         struct rte_mbuf *new_mbuf;
3049         struct rte_mbuf *temp_new;
3050         struct rte_mbuf *temp;
3051         uint16_t last_buf_len;
3052         uint16_t nb_new_buf;
3053         uint16_t buf_size;
3054         uint16_t buf_len;
3055         uint16_t len_s;
3056         uint16_t len_d;
3057         uint16_t len;
3058         int ret;
3059         char *s;
3060         char *d;
3061
3062         mb_pool = tx_pkt->pool;
3063         buf_size = tx_pkt->buf_len - RTE_PKTMBUF_HEADROOM;
3064         nb_new_buf = (rte_pktmbuf_pkt_len(tx_pkt) - 1) / buf_size + 1;
3065         if (nb_new_buf > max_non_tso_bd_num)
3066                 return -EINVAL;
3067
3068         last_buf_len = rte_pktmbuf_pkt_len(tx_pkt) % buf_size;
3069         if (last_buf_len == 0)
3070                 last_buf_len = buf_size;
3071
3072         /* Allocate enough mbufs */
3073         ret = hns3_tx_alloc_mbufs(mb_pool, nb_new_buf, &new_mbuf);
3074         if (ret)
3075                 return ret;
3076
3077         /* Copy the original packet content to the new mbufs */
3078         temp = tx_pkt;
3079         s = rte_pktmbuf_mtod(temp, char *);
3080         len_s = rte_pktmbuf_data_len(temp);
3081         temp_new = new_mbuf;
3082         while (temp != NULL && temp_new != NULL) {
3083                 d = rte_pktmbuf_mtod(temp_new, char *);
3084                 buf_len = temp_new->next == NULL ? last_buf_len : buf_size;
3085                 len_d = buf_len;
3086
3087                 while (len_d) {
3088                         len = RTE_MIN(len_s, len_d);
3089                         memcpy(d, s, len);
3090                         s = s + len;
3091                         d = d + len;
3092                         len_d = len_d - len;
3093                         len_s = len_s - len;
3094
3095                         if (len_s == 0) {
3096                                 temp = temp->next;
3097                                 if (temp == NULL)
3098                                         break;
3099                                 s = rte_pktmbuf_mtod(temp, char *);
3100                                 len_s = rte_pktmbuf_data_len(temp);
3101                         }
3102                 }
3103
3104                 temp_new->data_len = buf_len;
3105                 temp_new = temp_new->next;
3106         }
3107         hns3_pktmbuf_copy_hdr(new_mbuf, tx_pkt);
3108
3109         /* free original mbufs */
3110         rte_pktmbuf_free(tx_pkt);
3111
3112         *new_pkt = new_mbuf;
3113
3114         return 0;
3115 }
3116
3117 static void
3118 hns3_parse_outer_params(struct rte_mbuf *m, uint32_t *ol_type_vlan_len_msec)
3119 {
3120         uint32_t tmp = *ol_type_vlan_len_msec;
3121         uint64_t ol_flags = m->ol_flags;
3122
3123         /* (outer) IP header type */
3124         if (ol_flags & PKT_TX_OUTER_IPV4) {
3125                 if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
3126                         tmp |= hns3_gen_field_val(HNS3_TXD_OL3T_M,
3127                                         HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_CSUM);
3128                 else
3129                         tmp |= hns3_gen_field_val(HNS3_TXD_OL3T_M,
3130                                 HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_NO_CSUM);
3131         } else if (ol_flags & PKT_TX_OUTER_IPV6) {
3132                 tmp |= hns3_gen_field_val(HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
3133                                         HNS3_OL3T_IPV6);
3134         }
3135         /* OL3 header size, defined in 4 bytes */
3136         tmp |= hns3_gen_field_val(HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
3137                                 m->outer_l3_len >> HNS3_L3_LEN_UNIT);
3138         *ol_type_vlan_len_msec = tmp;
3139 }
3140
3141 static int
3142 hns3_parse_inner_params(struct rte_mbuf *m, uint32_t *ol_type_vlan_len_msec,
3143                         uint32_t *type_cs_vlan_tso_len)
3144 {
3145 #define HNS3_NVGRE_HLEN 8
3146         uint32_t tmp_outer = *ol_type_vlan_len_msec;
3147         uint32_t tmp_inner = *type_cs_vlan_tso_len;
3148         uint64_t ol_flags = m->ol_flags;
3149         uint16_t inner_l2_len;
3150
3151         switch (ol_flags & PKT_TX_TUNNEL_MASK) {
3152         case PKT_TX_TUNNEL_VXLAN_GPE:
3153         case PKT_TX_TUNNEL_GENEVE:
3154         case PKT_TX_TUNNEL_VXLAN:
3155                 /* MAC in UDP tunnelling packet, include VxLAN and GENEVE */
3156                 tmp_outer |= hns3_gen_field_val(HNS3_TXD_TUNTYPE_M,
3157                                 HNS3_TXD_TUNTYPE_S, HNS3_TUN_MAC_IN_UDP);
3158                 /*
3159                  * The inner l2 length of mbuf is the sum of outer l4 length,
3160                  * tunneling header length and inner l2 length for a tunnel
3161                  * packect. But in hns3 tx descriptor, the tunneling header
3162                  * length is contained in the field of outer L4 length.
3163                  * Therefore, driver need to calculate the outer L4 length and
3164                  * inner L2 length.
3165                  */
3166                 tmp_outer |= hns3_gen_field_val(HNS3_TXD_L4LEN_M,
3167                                                 HNS3_TXD_L4LEN_S,
3168                                                 (uint8_t)RTE_ETHER_VXLAN_HLEN >>
3169                                                 HNS3_L4_LEN_UNIT);
3170
3171                 inner_l2_len = m->l2_len - RTE_ETHER_VXLAN_HLEN;
3172                 break;
3173         case PKT_TX_TUNNEL_GRE:
3174                 tmp_outer |= hns3_gen_field_val(HNS3_TXD_TUNTYPE_M,
3175                                         HNS3_TXD_TUNTYPE_S, HNS3_TUN_NVGRE);
3176                 /*
3177                  * For NVGRE tunnel packect, the outer L4 is empty. So only
3178                  * fill the NVGRE header length to the outer L4 field.
3179                  */
3180                 tmp_outer |= hns3_gen_field_val(HNS3_TXD_L4LEN_M,
3181                                 HNS3_TXD_L4LEN_S,
3182                                 (uint8_t)HNS3_NVGRE_HLEN >> HNS3_L4_LEN_UNIT);
3183
3184                 inner_l2_len = m->l2_len - HNS3_NVGRE_HLEN;
3185                 break;
3186         default:
3187                 /* For non UDP / GRE tunneling, drop the tunnel packet */
3188                 return -EINVAL;
3189         }
3190
3191         tmp_inner |= hns3_gen_field_val(HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
3192                                         inner_l2_len >> HNS3_L2_LEN_UNIT);
3193         /* OL2 header size, defined in 2 bytes */
3194         tmp_outer |= hns3_gen_field_val(HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
3195                                         m->outer_l2_len >> HNS3_L2_LEN_UNIT);
3196
3197         *type_cs_vlan_tso_len = tmp_inner;
3198         *ol_type_vlan_len_msec = tmp_outer;
3199
3200         return 0;
3201 }
3202
3203 static int
3204 hns3_parse_tunneling_params(struct hns3_tx_queue *txq, struct rte_mbuf *m,
3205                             uint16_t tx_desc_id)
3206 {
3207         struct hns3_desc *tx_ring = txq->tx_ring;
3208         struct hns3_desc *desc = &tx_ring[tx_desc_id];
3209         uint64_t ol_flags = m->ol_flags;
3210         uint32_t tmp_outer = 0;
3211         uint32_t tmp_inner = 0;
3212         uint32_t tmp_ol4cs;
3213         int ret;
3214
3215         /*
3216          * The tunnel header is contained in the inner L2 header field of the
3217          * mbuf, but for hns3 descriptor, it is contained in the outer L4. So,
3218          * there is a need that switching between them. To avoid multiple
3219          * calculations, the length of the L2 header include the outer and
3220          * inner, will be filled during the parsing of tunnel packects.
3221          */
3222         if (!(ol_flags & PKT_TX_TUNNEL_MASK)) {
3223                 /*
3224                  * For non tunnel type the tunnel type id is 0, so no need to
3225                  * assign a value to it. Only the inner(normal) L2 header length
3226                  * is assigned.
3227                  */
3228                 tmp_inner |= hns3_gen_field_val(HNS3_TXD_L2LEN_M,
3229                                HNS3_TXD_L2LEN_S, m->l2_len >> HNS3_L2_LEN_UNIT);
3230         } else {
3231                 /*
3232                  * If outer csum is not offload, the outer length may be filled
3233                  * with 0. And the length of the outer header is added to the
3234                  * inner l2_len. It would lead a cksum error. So driver has to
3235                  * calculate the header length.
3236                  */
3237                 if (unlikely(!(ol_flags &
3238                         (PKT_TX_OUTER_IP_CKSUM | PKT_TX_OUTER_UDP_CKSUM)) &&
3239                                         m->outer_l2_len == 0)) {
3240                         struct rte_net_hdr_lens hdr_len;
3241                         (void)rte_net_get_ptype(m, &hdr_len,
3242                                         RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK);
3243                         m->outer_l3_len = hdr_len.l3_len;
3244                         m->outer_l2_len = hdr_len.l2_len;
3245                         m->l2_len = m->l2_len - hdr_len.l2_len - hdr_len.l3_len;
3246                 }
3247                 hns3_parse_outer_params(m, &tmp_outer);
3248                 ret = hns3_parse_inner_params(m, &tmp_outer, &tmp_inner);
3249                 if (ret)
3250                         return -EINVAL;
3251         }
3252
3253         desc->tx.ol_type_vlan_len_msec = rte_cpu_to_le_32(tmp_outer);
3254         desc->tx.type_cs_vlan_tso_len = rte_cpu_to_le_32(tmp_inner);
3255         tmp_ol4cs = ol_flags & PKT_TX_OUTER_UDP_CKSUM ?
3256                         BIT(HNS3_TXD_OL4CS_B) : 0;
3257         desc->tx.paylen_fd_dop_ol4cs = rte_cpu_to_le_32(tmp_ol4cs);
3258
3259         return 0;
3260 }
3261
3262 static void
3263 hns3_parse_l3_cksum_params(struct rte_mbuf *m, uint32_t *type_cs_vlan_tso_len)
3264 {
3265         uint64_t ol_flags = m->ol_flags;
3266         uint32_t l3_type;
3267         uint32_t tmp;
3268
3269         tmp = *type_cs_vlan_tso_len;
3270         if (ol_flags & PKT_TX_IPV4)
3271                 l3_type = HNS3_L3T_IPV4;
3272         else if (ol_flags & PKT_TX_IPV6)
3273                 l3_type = HNS3_L3T_IPV6;
3274         else
3275                 l3_type = HNS3_L3T_NONE;
3276
3277         /* inner(/normal) L3 header size, defined in 4 bytes */
3278         tmp |= hns3_gen_field_val(HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
3279                                         m->l3_len >> HNS3_L3_LEN_UNIT);
3280
3281         tmp |= hns3_gen_field_val(HNS3_TXD_L3T_M, HNS3_TXD_L3T_S, l3_type);
3282
3283         /* Enable L3 checksum offloads */
3284         if (ol_flags & PKT_TX_IP_CKSUM)
3285                 tmp |= BIT(HNS3_TXD_L3CS_B);
3286         *type_cs_vlan_tso_len = tmp;
3287 }
3288
3289 static void
3290 hns3_parse_l4_cksum_params(struct rte_mbuf *m, uint32_t *type_cs_vlan_tso_len)
3291 {
3292         uint64_t ol_flags = m->ol_flags;
3293         uint32_t tmp;
3294         /* Enable L4 checksum offloads */
3295         switch (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG)) {
3296         case PKT_TX_TCP_CKSUM | PKT_TX_TCP_SEG:
3297         case PKT_TX_TCP_CKSUM:
3298         case PKT_TX_TCP_SEG:
3299                 tmp = *type_cs_vlan_tso_len;
3300                 tmp |= hns3_gen_field_val(HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
3301                                         HNS3_L4T_TCP);
3302                 break;
3303         case PKT_TX_UDP_CKSUM:
3304                 tmp = *type_cs_vlan_tso_len;
3305                 tmp |= hns3_gen_field_val(HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
3306                                         HNS3_L4T_UDP);
3307                 break;
3308         case PKT_TX_SCTP_CKSUM:
3309                 tmp = *type_cs_vlan_tso_len;
3310                 tmp |= hns3_gen_field_val(HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
3311                                         HNS3_L4T_SCTP);
3312                 break;
3313         default:
3314                 return;
3315         }
3316         tmp |= BIT(HNS3_TXD_L4CS_B);
3317         tmp |= hns3_gen_field_val(HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
3318                                         m->l4_len >> HNS3_L4_LEN_UNIT);
3319         *type_cs_vlan_tso_len = tmp;
3320 }
3321
3322 static void
3323 hns3_txd_enable_checksum(struct hns3_tx_queue *txq, struct rte_mbuf *m,
3324                          uint16_t tx_desc_id)
3325 {
3326         struct hns3_desc *tx_ring = txq->tx_ring;
3327         struct hns3_desc *desc = &tx_ring[tx_desc_id];
3328         uint32_t value = 0;
3329
3330         hns3_parse_l3_cksum_params(m, &value);
3331         hns3_parse_l4_cksum_params(m, &value);
3332
3333         desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(value);
3334 }
3335
3336 static bool
3337 hns3_pkt_need_linearized(struct rte_mbuf *tx_pkts, uint32_t bd_num,
3338                                  uint32_t max_non_tso_bd_num)
3339 {
3340         struct rte_mbuf *m_first = tx_pkts;
3341         struct rte_mbuf *m_last = tx_pkts;
3342         uint32_t tot_len = 0;
3343         uint32_t hdr_len;
3344         uint32_t i;
3345
3346         /*
3347          * Hardware requires that the sum of the data length of every 8
3348          * consecutive buffers is greater than MSS in hns3 network engine.
3349          * We simplify it by ensuring pkt_headlen + the first 8 consecutive
3350          * frags greater than gso header len + mss, and the remaining 7
3351          * consecutive frags greater than MSS except the last 7 frags.
3352          */
3353         if (bd_num <= max_non_tso_bd_num)
3354                 return false;
3355
3356         for (i = 0; m_last && i < max_non_tso_bd_num - 1;
3357              i++, m_last = m_last->next)
3358                 tot_len += m_last->data_len;
3359
3360         if (!m_last)
3361                 return true;
3362
3363         /* ensure the first 8 frags is greater than mss + header */
3364         hdr_len = tx_pkts->l2_len + tx_pkts->l3_len + tx_pkts->l4_len;
3365         hdr_len += (tx_pkts->ol_flags & PKT_TX_TUNNEL_MASK) ?
3366                    tx_pkts->outer_l2_len + tx_pkts->outer_l3_len : 0;
3367         if (tot_len + m_last->data_len < tx_pkts->tso_segsz + hdr_len)
3368                 return true;
3369
3370         /*
3371          * ensure the sum of the data length of every 7 consecutive buffer
3372          * is greater than mss except the last one.
3373          */
3374         for (i = 0; m_last && i < bd_num - max_non_tso_bd_num; i++) {
3375                 tot_len -= m_first->data_len;
3376                 tot_len += m_last->data_len;
3377
3378                 if (tot_len < tx_pkts->tso_segsz)
3379                         return true;
3380
3381                 m_first = m_first->next;
3382                 m_last = m_last->next;
3383         }
3384
3385         return false;
3386 }
3387
3388 static bool
3389 hns3_outer_ipv4_cksum_prepared(struct rte_mbuf *m, uint64_t ol_flags,
3390                                 uint32_t *l4_proto)
3391 {
3392         struct rte_ipv4_hdr *ipv4_hdr;
3393         ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
3394                                            m->outer_l2_len);
3395         if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
3396                 ipv4_hdr->hdr_checksum = 0;
3397         if (ol_flags & PKT_TX_OUTER_UDP_CKSUM) {
3398                 struct rte_udp_hdr *udp_hdr;
3399                 /*
3400                  * If OUTER_UDP_CKSUM is support, HW can caclulate the pseudo
3401                  * header for TSO packets
3402                  */
3403                 if (ol_flags & PKT_TX_TCP_SEG)
3404                         return true;
3405                 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
3406                                 m->outer_l2_len + m->outer_l3_len);
3407                 udp_hdr->dgram_cksum = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);
3408
3409                 return true;
3410         }
3411         *l4_proto = ipv4_hdr->next_proto_id;
3412         return false;
3413 }
3414
3415 static bool
3416 hns3_outer_ipv6_cksum_prepared(struct rte_mbuf *m, uint64_t ol_flags,
3417                                 uint32_t *l4_proto)
3418 {
3419         struct rte_ipv6_hdr *ipv6_hdr;
3420         ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
3421                                            m->outer_l2_len);
3422         if (ol_flags & PKT_TX_OUTER_UDP_CKSUM) {
3423                 struct rte_udp_hdr *udp_hdr;
3424                 /*
3425                  * If OUTER_UDP_CKSUM is support, HW can caclulate the pseudo
3426                  * header for TSO packets
3427                  */
3428                 if (ol_flags & PKT_TX_TCP_SEG)
3429                         return true;
3430                 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
3431                                 m->outer_l2_len + m->outer_l3_len);
3432                 udp_hdr->dgram_cksum = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);
3433
3434                 return true;
3435         }
3436         *l4_proto = ipv6_hdr->proto;
3437         return false;
3438 }
3439
3440 static void
3441 hns3_outer_header_cksum_prepare(struct rte_mbuf *m)
3442 {
3443         uint64_t ol_flags = m->ol_flags;
3444         uint32_t paylen, hdr_len, l4_proto;
3445         struct rte_udp_hdr *udp_hdr;
3446
3447         if (!(ol_flags & (PKT_TX_OUTER_IPV4 | PKT_TX_OUTER_IPV6)))
3448                 return;
3449
3450         if (ol_flags & PKT_TX_OUTER_IPV4) {
3451                 if (hns3_outer_ipv4_cksum_prepared(m, ol_flags, &l4_proto))
3452                         return;
3453         } else {
3454                 if (hns3_outer_ipv6_cksum_prepared(m, ol_flags, &l4_proto))
3455                         return;
3456         }
3457
3458         /* driver should ensure the outer udp cksum is 0 for TUNNEL TSO */
3459         if (l4_proto == IPPROTO_UDP && (ol_flags & PKT_TX_TCP_SEG)) {
3460                 hdr_len = m->l2_len + m->l3_len + m->l4_len;
3461                 hdr_len += m->outer_l2_len + m->outer_l3_len;
3462                 paylen = m->pkt_len - hdr_len;
3463                 if (paylen <= m->tso_segsz)
3464                         return;
3465                 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
3466                                                   m->outer_l2_len +
3467                                                   m->outer_l3_len);
3468                 udp_hdr->dgram_cksum = 0;
3469         }
3470 }
3471
3472 static int
3473 hns3_check_tso_pkt_valid(struct rte_mbuf *m)
3474 {
3475         uint32_t tmp_data_len_sum = 0;
3476         uint16_t nb_buf = m->nb_segs;
3477         uint32_t paylen, hdr_len;
3478         struct rte_mbuf *m_seg;
3479         int i;
3480
3481         if (nb_buf > HNS3_MAX_TSO_BD_PER_PKT)
3482                 return -EINVAL;
3483
3484         hdr_len = m->l2_len + m->l3_len + m->l4_len;
3485         hdr_len += (m->ol_flags & PKT_TX_TUNNEL_MASK) ?
3486                         m->outer_l2_len + m->outer_l3_len : 0;
3487         if (hdr_len > HNS3_MAX_TSO_HDR_SIZE)
3488                 return -EINVAL;
3489
3490         paylen = m->pkt_len - hdr_len;
3491         if (paylen > HNS3_MAX_BD_PAYLEN)
3492                 return -EINVAL;
3493
3494         /*
3495          * The TSO header (include outer and inner L2, L3 and L4 header)
3496          * should be provided by three descriptors in maximum in hns3 network
3497          * engine.
3498          */
3499         m_seg = m;
3500         for (i = 0; m_seg != NULL && i < HNS3_MAX_TSO_HDR_BD_NUM && i < nb_buf;
3501              i++, m_seg = m_seg->next) {
3502                 tmp_data_len_sum += m_seg->data_len;
3503         }
3504
3505         if (hdr_len > tmp_data_len_sum)
3506                 return -EINVAL;
3507
3508         return 0;
3509 }
3510
3511 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
3512 static inline int
3513 hns3_vld_vlan_chk(struct hns3_tx_queue *txq, struct rte_mbuf *m)
3514 {
3515         struct rte_ether_hdr *eh;
3516         struct rte_vlan_hdr *vh;
3517
3518         if (!txq->pvid_sw_shift_en)
3519                 return 0;
3520
3521         /*
3522          * Due to hardware limitations, we only support two-layer VLAN hardware
3523          * offload in Tx direction based on hns3 network engine, so when PVID is
3524          * enabled, QinQ insert is no longer supported.
3525          * And when PVID is enabled, in the following two cases:
3526          *  i) packets with more than two VLAN tags.
3527          *  ii) packets with one VLAN tag while the hardware VLAN insert is
3528          *      enabled.
3529          * The packets will be regarded as abnormal packets and discarded by
3530          * hardware in Tx direction. For debugging purposes, a validation check
3531          * for these types of packets is added to the '.tx_pkt_prepare' ops
3532          * implementation function named hns3_prep_pkts to inform users that
3533          * these packets will be discarded.
3534          */
3535         if (m->ol_flags & PKT_TX_QINQ_PKT)
3536                 return -EINVAL;
3537
3538         eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
3539         if (eh->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
3540                 if (m->ol_flags & PKT_TX_VLAN_PKT)
3541                         return -EINVAL;
3542
3543                 /* Ensure the incoming packet is not a QinQ packet */
3544                 vh = (struct rte_vlan_hdr *)(eh + 1);
3545                 if (vh->eth_proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
3546                         return -EINVAL;
3547         }
3548
3549         return 0;
3550 }
3551 #endif
3552
3553 static uint16_t
3554 hns3_udp_cksum_help(struct rte_mbuf *m)
3555 {
3556         uint64_t ol_flags = m->ol_flags;
3557         uint16_t cksum = 0;
3558         uint32_t l4_len;
3559
3560         if (ol_flags & PKT_TX_IPV4) {
3561                 struct rte_ipv4_hdr *ipv4_hdr = rte_pktmbuf_mtod_offset(m,
3562                                 struct rte_ipv4_hdr *, m->l2_len);
3563                 l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) - m->l3_len;
3564         } else {
3565                 struct rte_ipv6_hdr *ipv6_hdr = rte_pktmbuf_mtod_offset(m,
3566                                 struct rte_ipv6_hdr *, m->l2_len);
3567                 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
3568         }
3569
3570         rte_raw_cksum_mbuf(m, m->l2_len + m->l3_len, l4_len, &cksum);
3571
3572         cksum = ~cksum;
3573         /*
3574          * RFC 768:If the computed checksum is zero for UDP, it is transmitted
3575          * as all ones
3576          */
3577         if (cksum == 0)
3578                 cksum = 0xffff;
3579
3580         return (uint16_t)cksum;
3581 }
3582
3583 static bool
3584 hns3_validate_tunnel_cksum(struct hns3_tx_queue *tx_queue, struct rte_mbuf *m)
3585 {
3586         uint64_t ol_flags = m->ol_flags;
3587         struct rte_udp_hdr *udp_hdr;
3588         uint16_t dst_port;
3589
3590         if (tx_queue->udp_cksum_mode == HNS3_SPECIAL_PORT_HW_CKSUM_MODE ||
3591             ol_flags & PKT_TX_TUNNEL_MASK ||
3592             (ol_flags & PKT_TX_L4_MASK) != PKT_TX_UDP_CKSUM)
3593                 return true;
3594         /*
3595          * A UDP packet with the same dst_port as VXLAN\VXLAN_GPE\GENEVE will
3596          * be recognized as a tunnel packet in HW. In this case, if UDP CKSUM
3597          * offload is set and the tunnel mask has not been set, the CKSUM will
3598          * be wrong since the header length is wrong and driver should complete
3599          * the CKSUM to avoid CKSUM error.
3600          */
3601         udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
3602                                                 m->l2_len + m->l3_len);
3603         dst_port = rte_be_to_cpu_16(udp_hdr->dst_port);
3604         switch (dst_port) {
3605         case RTE_VXLAN_DEFAULT_PORT:
3606         case RTE_VXLAN_GPE_DEFAULT_PORT:
3607         case RTE_GENEVE_DEFAULT_PORT:
3608                 udp_hdr->dgram_cksum = hns3_udp_cksum_help(m);
3609                 m->ol_flags = ol_flags & ~PKT_TX_L4_MASK;
3610                 return false;
3611         default:
3612                 return true;
3613         }
3614 }
3615
3616 static int
3617 hns3_prep_pkt_proc(struct hns3_tx_queue *tx_queue, struct rte_mbuf *m)
3618 {
3619         int ret;
3620
3621 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
3622         ret = rte_validate_tx_offload(m);
3623         if (ret != 0) {
3624                 rte_errno = -ret;
3625                 return ret;
3626         }
3627
3628         ret = hns3_vld_vlan_chk(tx_queue, m);
3629         if (ret != 0) {
3630                 rte_errno = EINVAL;
3631                 return ret;
3632         }
3633 #endif
3634         if (hns3_pkt_is_tso(m)) {
3635                 if (hns3_pkt_need_linearized(m, m->nb_segs,
3636                                              tx_queue->max_non_tso_bd_num) ||
3637                     hns3_check_tso_pkt_valid(m)) {
3638                         rte_errno = EINVAL;
3639                         return -EINVAL;
3640                 }
3641
3642                 if (tx_queue->tso_mode != HNS3_TSO_SW_CAL_PSEUDO_H_CSUM) {
3643                         /*
3644                          * (tso mode != HNS3_TSO_SW_CAL_PSEUDO_H_CSUM) means
3645                          * hardware support recalculate the TCP pseudo header
3646                          * checksum of packets that need TSO, so network driver
3647                          * software not need to recalculate it.
3648                          */
3649                         hns3_outer_header_cksum_prepare(m);
3650                         return 0;
3651                 }
3652         }
3653
3654         ret = rte_net_intel_cksum_prepare(m);
3655         if (ret != 0) {
3656                 rte_errno = -ret;
3657                 return ret;
3658         }
3659
3660         if (!hns3_validate_tunnel_cksum(tx_queue, m))
3661                 return 0;
3662
3663         hns3_outer_header_cksum_prepare(m);
3664
3665         return 0;
3666 }
3667
3668 uint16_t
3669 hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
3670                uint16_t nb_pkts)
3671 {
3672         struct rte_mbuf *m;
3673         uint16_t i;
3674
3675         for (i = 0; i < nb_pkts; i++) {
3676                 m = tx_pkts[i];
3677                 if (hns3_prep_pkt_proc(tx_queue, m))
3678                         return i;
3679         }
3680
3681         return i;
3682 }
3683
3684 static int
3685 hns3_parse_cksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
3686                  struct rte_mbuf *m)
3687 {
3688         struct hns3_desc *tx_ring = txq->tx_ring;
3689         struct hns3_desc *desc = &tx_ring[tx_desc_id];
3690
3691         /* Enable checksum offloading */
3692         if (m->ol_flags & HNS3_TX_CKSUM_OFFLOAD_MASK) {
3693                 /* Fill in tunneling parameters if necessary */
3694                 if (hns3_parse_tunneling_params(txq, m, tx_desc_id)) {
3695                         txq->dfx_stats.unsupported_tunnel_pkt_cnt++;
3696                                 return -EINVAL;
3697                 }
3698
3699                 hns3_txd_enable_checksum(txq, m, tx_desc_id);
3700         } else {
3701                 /* clear the control bit */
3702                 desc->tx.type_cs_vlan_tso_len  = 0;
3703                 desc->tx.ol_type_vlan_len_msec = 0;
3704         }
3705
3706         return 0;
3707 }
3708
3709 static int
3710 hns3_check_non_tso_pkt(uint16_t nb_buf, struct rte_mbuf **m_seg,
3711                       struct rte_mbuf *tx_pkt, struct hns3_tx_queue *txq)
3712 {
3713         uint8_t max_non_tso_bd_num;
3714         struct rte_mbuf *new_pkt;
3715         int ret;
3716
3717         if (hns3_pkt_is_tso(*m_seg))
3718                 return 0;
3719
3720         /*
3721          * If packet length is greater than HNS3_MAX_FRAME_LEN
3722          * driver support, the packet will be ignored.
3723          */
3724         if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) > HNS3_MAX_FRAME_LEN)) {
3725                 txq->dfx_stats.over_length_pkt_cnt++;
3726                 return -EINVAL;
3727         }
3728
3729         max_non_tso_bd_num = txq->max_non_tso_bd_num;
3730         if (unlikely(nb_buf > max_non_tso_bd_num)) {
3731                 txq->dfx_stats.exceed_limit_bd_pkt_cnt++;
3732                 ret = hns3_reassemble_tx_pkts(tx_pkt, &new_pkt,
3733                                               max_non_tso_bd_num);
3734                 if (ret) {
3735                         txq->dfx_stats.exceed_limit_bd_reassem_fail++;
3736                         return ret;
3737                 }
3738                 *m_seg = new_pkt;
3739         }
3740
3741         return 0;
3742 }
3743
3744 static inline void
3745 hns3_tx_free_buffer_simple(struct hns3_tx_queue *txq)
3746 {
3747         struct hns3_entry *tx_entry;
3748         struct hns3_desc *desc;
3749         uint16_t tx_next_clean;
3750         int i;
3751
3752         while (1) {
3753                 if (HNS3_GET_TX_QUEUE_PEND_BD_NUM(txq) < txq->tx_rs_thresh)
3754                         break;
3755
3756                 /*
3757                  * All mbufs can be released only when the VLD bits of all
3758                  * descriptors in a batch are cleared.
3759                  */
3760                 tx_next_clean = (txq->next_to_clean + txq->tx_rs_thresh - 1) %
3761                                 txq->nb_tx_desc;
3762                 desc = &txq->tx_ring[tx_next_clean];
3763                 for (i = 0; i < txq->tx_rs_thresh; i++) {
3764                         if (rte_le_to_cpu_16(desc->tx.tp_fe_sc_vld_ra_ri) &
3765                                         BIT(HNS3_TXD_VLD_B))
3766                                 return;
3767                         desc--;
3768                 }
3769
3770                 tx_entry = &txq->sw_ring[txq->next_to_clean];
3771
3772                 for (i = 0; i < txq->tx_rs_thresh; i++)
3773                         rte_prefetch0((tx_entry + i)->mbuf);
3774                 for (i = 0; i < txq->tx_rs_thresh; i++, tx_entry++) {
3775                         rte_mempool_put(tx_entry->mbuf->pool, tx_entry->mbuf);
3776                         tx_entry->mbuf = NULL;
3777                 }
3778
3779                 txq->next_to_clean = (tx_next_clean + 1) % txq->nb_tx_desc;
3780                 txq->tx_bd_ready += txq->tx_rs_thresh;
3781         }
3782 }
3783
3784 static inline void
3785 hns3_tx_backup_1mbuf(struct hns3_entry *tx_entry, struct rte_mbuf **pkts)
3786 {
3787         tx_entry->mbuf = pkts[0];
3788 }
3789
3790 static inline void
3791 hns3_tx_backup_4mbuf(struct hns3_entry *tx_entry, struct rte_mbuf **pkts)
3792 {
3793         hns3_tx_backup_1mbuf(&tx_entry[0], &pkts[0]);
3794         hns3_tx_backup_1mbuf(&tx_entry[1], &pkts[1]);
3795         hns3_tx_backup_1mbuf(&tx_entry[2], &pkts[2]);
3796         hns3_tx_backup_1mbuf(&tx_entry[3], &pkts[3]);
3797 }
3798
3799 static inline void
3800 hns3_tx_setup_4bd(struct hns3_desc *txdp, struct rte_mbuf **pkts)
3801 {
3802 #define PER_LOOP_NUM    4
3803         const uint16_t bd_flag = BIT(HNS3_TXD_VLD_B) | BIT(HNS3_TXD_FE_B);
3804         uint64_t dma_addr;
3805         uint32_t i;
3806
3807         for (i = 0; i < PER_LOOP_NUM; i++, txdp++, pkts++) {
3808                 dma_addr = rte_mbuf_data_iova(*pkts);
3809                 txdp->addr = rte_cpu_to_le_64(dma_addr);
3810                 txdp->tx.send_size = rte_cpu_to_le_16((*pkts)->data_len);
3811                 txdp->tx.paylen_fd_dop_ol4cs = 0;
3812                 txdp->tx.type_cs_vlan_tso_len = 0;
3813                 txdp->tx.ol_type_vlan_len_msec = 0;
3814                 txdp->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(bd_flag);
3815         }
3816 }
3817
3818 static inline void
3819 hns3_tx_setup_1bd(struct hns3_desc *txdp, struct rte_mbuf **pkts)
3820 {
3821         const uint16_t bd_flag = BIT(HNS3_TXD_VLD_B) | BIT(HNS3_TXD_FE_B);
3822         uint64_t dma_addr;
3823
3824         dma_addr = rte_mbuf_data_iova(*pkts);
3825         txdp->addr = rte_cpu_to_le_64(dma_addr);
3826         txdp->tx.send_size = rte_cpu_to_le_16((*pkts)->data_len);
3827         txdp->tx.paylen_fd_dop_ol4cs = 0;
3828         txdp->tx.type_cs_vlan_tso_len = 0;
3829         txdp->tx.ol_type_vlan_len_msec = 0;
3830         txdp->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(bd_flag);
3831 }
3832
3833 static inline void
3834 hns3_tx_fill_hw_ring(struct hns3_tx_queue *txq,
3835                      struct rte_mbuf **pkts,
3836                      uint16_t nb_pkts)
3837 {
3838 #define PER_LOOP_NUM    4
3839 #define PER_LOOP_MASK   (PER_LOOP_NUM - 1)
3840         struct hns3_desc *txdp = &txq->tx_ring[txq->next_to_use];
3841         struct hns3_entry *tx_entry = &txq->sw_ring[txq->next_to_use];
3842         const uint32_t mainpart = (nb_pkts & ((uint32_t)~PER_LOOP_MASK));
3843         const uint32_t leftover = (nb_pkts & ((uint32_t)PER_LOOP_MASK));
3844         uint32_t i;
3845
3846         for (i = 0; i < mainpart; i += PER_LOOP_NUM) {
3847                 hns3_tx_backup_4mbuf(tx_entry + i, pkts + i);
3848                 hns3_tx_setup_4bd(txdp + i, pkts + i);
3849
3850                 /* Increment bytes counter */
3851                 uint32_t j;
3852                 for (j = 0; j < PER_LOOP_NUM; j++)
3853                         txq->basic_stats.bytes += pkts[i + j]->pkt_len;
3854         }
3855         if (unlikely(leftover > 0)) {
3856                 for (i = 0; i < leftover; i++) {
3857                         hns3_tx_backup_1mbuf(tx_entry + mainpart + i,
3858                                              pkts + mainpart + i);
3859                         hns3_tx_setup_1bd(txdp + mainpart + i,
3860                                           pkts + mainpart + i);
3861
3862                         /* Increment bytes counter */
3863                         txq->basic_stats.bytes += pkts[mainpart + i]->pkt_len;
3864                 }
3865         }
3866 }
3867
3868 uint16_t
3869 hns3_xmit_pkts_simple(void *tx_queue,
3870                       struct rte_mbuf **tx_pkts,
3871                       uint16_t nb_pkts)
3872 {
3873         struct hns3_tx_queue *txq = tx_queue;
3874         uint16_t nb_tx = 0;
3875
3876         hns3_tx_free_buffer_simple(txq);
3877
3878         nb_pkts = RTE_MIN(txq->tx_bd_ready, nb_pkts);
3879         if (unlikely(nb_pkts == 0)) {
3880                 if (txq->tx_bd_ready == 0)
3881                         txq->dfx_stats.queue_full_cnt++;
3882                 return 0;
3883         }
3884
3885         txq->tx_bd_ready -= nb_pkts;
3886         if (txq->next_to_use + nb_pkts > txq->nb_tx_desc) {
3887                 nb_tx = txq->nb_tx_desc - txq->next_to_use;
3888                 hns3_tx_fill_hw_ring(txq, tx_pkts, nb_tx);
3889                 txq->next_to_use = 0;
3890         }
3891
3892         hns3_tx_fill_hw_ring(txq, tx_pkts + nb_tx, nb_pkts - nb_tx);
3893         txq->next_to_use += nb_pkts - nb_tx;
3894
3895         hns3_write_reg_opt(txq->io_tail_reg, nb_pkts);
3896
3897         return nb_pkts;
3898 }
3899
3900 uint16_t
3901 hns3_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
3902 {
3903         struct hns3_tx_queue *txq = tx_queue;
3904         struct hns3_entry *tx_bak_pkt;
3905         struct hns3_desc *tx_ring;
3906         struct rte_mbuf *tx_pkt;
3907         struct rte_mbuf *m_seg;
3908         struct hns3_desc *desc;
3909         uint32_t nb_hold = 0;
3910         uint16_t tx_next_use;
3911         uint16_t tx_pkt_num;
3912         uint16_t tx_bd_max;
3913         uint16_t nb_buf;
3914         uint16_t nb_tx;
3915         uint16_t i;
3916
3917         /* free useless buffer */
3918         hns3_tx_free_useless_buffer(txq);
3919
3920         tx_next_use   = txq->next_to_use;
3921         tx_bd_max     = txq->nb_tx_desc;
3922         tx_pkt_num = nb_pkts;
3923         tx_ring = txq->tx_ring;
3924
3925         /* send packets */
3926         tx_bak_pkt = &txq->sw_ring[tx_next_use];
3927         for (nb_tx = 0; nb_tx < tx_pkt_num; nb_tx++) {
3928                 tx_pkt = *tx_pkts++;
3929
3930                 nb_buf = tx_pkt->nb_segs;
3931
3932                 if (nb_buf > txq->tx_bd_ready) {
3933                         txq->dfx_stats.queue_full_cnt++;
3934                         if (nb_tx == 0)
3935                                 return 0;
3936
3937                         goto end_of_tx;
3938                 }
3939
3940                 /*
3941                  * If packet length is less than minimum packet length supported
3942                  * by hardware in Tx direction, driver need to pad it to avoid
3943                  * error.
3944                  */
3945                 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) <
3946                                                 txq->min_tx_pkt_len)) {
3947                         uint16_t add_len;
3948                         char *appended;
3949
3950                         add_len = txq->min_tx_pkt_len -
3951                                          rte_pktmbuf_pkt_len(tx_pkt);
3952                         appended = rte_pktmbuf_append(tx_pkt, add_len);
3953                         if (appended == NULL) {
3954                                 txq->dfx_stats.pkt_padding_fail_cnt++;
3955                                 break;
3956                         }
3957
3958                         memset(appended, 0, add_len);
3959                 }
3960
3961                 m_seg = tx_pkt;
3962
3963                 if (hns3_check_non_tso_pkt(nb_buf, &m_seg, tx_pkt, txq))
3964                         goto end_of_tx;
3965
3966                 if (hns3_parse_cksum(txq, tx_next_use, m_seg))
3967                         goto end_of_tx;
3968
3969                 i = 0;
3970                 desc = &tx_ring[tx_next_use];
3971
3972                 /*
3973                  * If the packet is divided into multiple Tx Buffer Descriptors,
3974                  * only need to fill vlan, paylen and tso into the first Tx
3975                  * Buffer Descriptor.
3976                  */
3977                 hns3_fill_first_desc(txq, desc, m_seg);
3978
3979                 do {
3980                         desc = &tx_ring[tx_next_use];
3981                         /*
3982                          * Fill valid bits, DMA address and data length for each
3983                          * Tx Buffer Descriptor.
3984                          */
3985                         hns3_fill_per_desc(desc, m_seg);
3986                         tx_bak_pkt->mbuf = m_seg;
3987                         m_seg = m_seg->next;
3988                         tx_next_use++;
3989                         tx_bak_pkt++;
3990                         if (tx_next_use >= tx_bd_max) {
3991                                 tx_next_use = 0;
3992                                 tx_bak_pkt = txq->sw_ring;
3993                         }
3994
3995                         i++;
3996                 } while (m_seg != NULL);
3997
3998                 /* Add end flag for the last Tx Buffer Descriptor */
3999                 desc->tx.tp_fe_sc_vld_ra_ri |=
4000                                  rte_cpu_to_le_16(BIT(HNS3_TXD_FE_B));
4001
4002                 /* Increment bytes counter */
4003                 txq->basic_stats.bytes += tx_pkt->pkt_len;
4004                 nb_hold += i;
4005                 txq->next_to_use = tx_next_use;
4006                 txq->tx_bd_ready -= i;
4007         }
4008
4009 end_of_tx:
4010
4011         if (likely(nb_tx))
4012                 hns3_write_reg_opt(txq->io_tail_reg, nb_hold);
4013
4014         return nb_tx;
4015 }
4016
4017 int __rte_weak
4018 hns3_tx_check_vec_support(__rte_unused struct rte_eth_dev *dev)
4019 {
4020         return -ENOTSUP;
4021 }
4022
4023 uint16_t __rte_weak
4024 hns3_xmit_pkts_vec(__rte_unused void *tx_queue,
4025                    __rte_unused struct rte_mbuf **tx_pkts,
4026                    __rte_unused uint16_t nb_pkts)
4027 {
4028         return 0;
4029 }
4030
4031 uint16_t __rte_weak
4032 hns3_xmit_pkts_vec_sve(void __rte_unused * tx_queue,
4033                        struct rte_mbuf __rte_unused **tx_pkts,
4034                        uint16_t __rte_unused nb_pkts)
4035 {
4036         return 0;
4037 }
4038
4039 int
4040 hns3_tx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
4041                        struct rte_eth_burst_mode *mode)
4042 {
4043         eth_tx_burst_t pkt_burst = dev->tx_pkt_burst;
4044         const char *info = NULL;
4045
4046         if (pkt_burst == hns3_xmit_pkts_simple)
4047                 info = "Scalar Simple";
4048         else if (pkt_burst == hns3_xmit_pkts)
4049                 info = "Scalar";
4050         else if (pkt_burst == hns3_xmit_pkts_vec)
4051                 info = "Vector Neon";
4052         else if (pkt_burst == hns3_xmit_pkts_vec_sve)
4053                 info = "Vector Sve";
4054
4055         if (info == NULL)
4056                 return -EINVAL;
4057
4058         snprintf(mode->info, sizeof(mode->info), "%s", info);
4059
4060         return 0;
4061 }
4062
4063 static eth_tx_burst_t
4064 hns3_get_tx_function(struct rte_eth_dev *dev, eth_tx_prep_t *prep)
4065 {
4066         uint64_t offloads = dev->data->dev_conf.txmode.offloads;
4067         struct hns3_adapter *hns = dev->data->dev_private;
4068         bool vec_allowed, sve_allowed, simple_allowed;
4069
4070         vec_allowed = hns->tx_vec_allowed &&
4071                       hns3_tx_check_vec_support(dev) == 0;
4072         sve_allowed = vec_allowed && hns3_check_sve_support();
4073         simple_allowed = hns->tx_simple_allowed &&
4074                          offloads == (offloads & DEV_TX_OFFLOAD_MBUF_FAST_FREE);
4075
4076         *prep = NULL;
4077
4078         if (hns->tx_func_hint == HNS3_IO_FUNC_HINT_VEC && vec_allowed)
4079                 return hns3_xmit_pkts_vec;
4080         if (hns->tx_func_hint == HNS3_IO_FUNC_HINT_SVE && sve_allowed)
4081                 return hns3_xmit_pkts_vec_sve;
4082         if (hns->tx_func_hint == HNS3_IO_FUNC_HINT_SIMPLE && simple_allowed)
4083                 return hns3_xmit_pkts_simple;
4084         if (hns->tx_func_hint == HNS3_IO_FUNC_HINT_COMMON) {
4085                 *prep = hns3_prep_pkts;
4086                 return hns3_xmit_pkts;
4087         }
4088
4089         if (vec_allowed)
4090                 return hns3_xmit_pkts_vec;
4091         if (simple_allowed)
4092                 return hns3_xmit_pkts_simple;
4093
4094         *prep = hns3_prep_pkts;
4095         return hns3_xmit_pkts;
4096 }
4097
4098 static uint16_t
4099 hns3_dummy_rxtx_burst(void *dpdk_txq __rte_unused,
4100                       struct rte_mbuf **pkts __rte_unused,
4101                       uint16_t pkts_n __rte_unused)
4102 {
4103         return 0;
4104 }
4105
4106 void hns3_set_rxtx_function(struct rte_eth_dev *eth_dev)
4107 {
4108         struct hns3_adapter *hns = eth_dev->data->dev_private;
4109         eth_tx_prep_t prep = NULL;
4110
4111         if (hns->hw.adapter_state == HNS3_NIC_STARTED &&
4112             __atomic_load_n(&hns->hw.reset.resetting, __ATOMIC_RELAXED) == 0) {
4113                 eth_dev->rx_pkt_burst = hns3_get_rx_function(eth_dev);
4114                 eth_dev->rx_descriptor_status = hns3_dev_rx_descriptor_status;
4115                 eth_dev->tx_pkt_burst = hns3_get_tx_function(eth_dev, &prep);
4116                 eth_dev->tx_pkt_prepare = prep;
4117                 eth_dev->tx_descriptor_status = hns3_dev_tx_descriptor_status;
4118         } else {
4119                 eth_dev->rx_pkt_burst = hns3_dummy_rxtx_burst;
4120                 eth_dev->tx_pkt_burst = hns3_dummy_rxtx_burst;
4121                 eth_dev->tx_pkt_prepare = hns3_dummy_rxtx_burst;
4122         }
4123 }
4124
4125 void
4126 hns3_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
4127                   struct rte_eth_rxq_info *qinfo)
4128 {
4129         struct hns3_rx_queue *rxq = dev->data->rx_queues[queue_id];
4130
4131         qinfo->mp = rxq->mb_pool;
4132         qinfo->nb_desc = rxq->nb_rx_desc;
4133         qinfo->scattered_rx = dev->data->scattered_rx;
4134         /* Report the HW Rx buffer length to user */
4135         qinfo->rx_buf_size = rxq->rx_buf_len;
4136
4137         /*
4138          * If there are no available Rx buffer descriptors, incoming packets
4139          * are always dropped by hardware based on hns3 network engine.
4140          */
4141         qinfo->conf.rx_drop_en = 1;
4142         qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
4143         qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
4144         qinfo->conf.rx_deferred_start = rxq->rx_deferred_start;
4145 }
4146
4147 void
4148 hns3_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
4149                   struct rte_eth_txq_info *qinfo)
4150 {
4151         struct hns3_tx_queue *txq = dev->data->tx_queues[queue_id];
4152
4153         qinfo->nb_desc = txq->nb_tx_desc;
4154         qinfo->conf.offloads = dev->data->dev_conf.txmode.offloads;
4155         qinfo->conf.tx_rs_thresh = txq->tx_rs_thresh;
4156         qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
4157         qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
4158 }
4159
4160 int
4161 hns3_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
4162 {
4163         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4164         struct hns3_rx_queue *rxq = dev->data->rx_queues[rx_queue_id];
4165         struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4166         int ret;
4167
4168         if (!hns3_dev_indep_txrx_supported(hw))
4169                 return -ENOTSUP;
4170
4171         ret = hns3_reset_queue(hw, rx_queue_id, HNS3_RING_TYPE_RX);
4172         if (ret) {
4173                 hns3_err(hw, "fail to reset Rx queue %u, ret = %d.",
4174                          rx_queue_id, ret);
4175                 return ret;
4176         }
4177
4178         ret = hns3_init_rxq(hns, rx_queue_id);
4179         if (ret) {
4180                 hns3_err(hw, "fail to init Rx queue %u, ret = %d.",
4181                          rx_queue_id, ret);
4182                 return ret;
4183         }
4184
4185         hns3_enable_rxq(rxq, true);
4186         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
4187
4188         return ret;
4189 }
4190
4191 static void
4192 hns3_reset_sw_rxq(struct hns3_rx_queue *rxq)
4193 {
4194         rxq->next_to_use = 0;
4195         rxq->rx_rearm_start = 0;
4196         rxq->rx_free_hold = 0;
4197         rxq->rx_rearm_nb = 0;
4198         rxq->pkt_first_seg = NULL;
4199         rxq->pkt_last_seg = NULL;
4200         memset(&rxq->rx_ring[0], 0, rxq->nb_rx_desc * sizeof(struct hns3_desc));
4201         hns3_rxq_vec_setup(rxq);
4202 }
4203
4204 int
4205 hns3_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
4206 {
4207         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4208         struct hns3_rx_queue *rxq = dev->data->rx_queues[rx_queue_id];
4209
4210         if (!hns3_dev_indep_txrx_supported(hw))
4211                 return -ENOTSUP;
4212
4213         hns3_enable_rxq(rxq, false);
4214
4215         hns3_rx_queue_release_mbufs(rxq);
4216
4217         hns3_reset_sw_rxq(rxq);
4218         dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
4219
4220         return 0;
4221 }
4222
4223 int
4224 hns3_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
4225 {
4226         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4227         struct hns3_tx_queue *txq = dev->data->tx_queues[tx_queue_id];
4228         int ret;
4229
4230         if (!hns3_dev_indep_txrx_supported(hw))
4231                 return -ENOTSUP;
4232
4233         ret = hns3_reset_queue(hw, tx_queue_id, HNS3_RING_TYPE_TX);
4234         if (ret) {
4235                 hns3_err(hw, "fail to reset Tx queue %u, ret = %d.",
4236                          tx_queue_id, ret);
4237                 return ret;
4238         }
4239
4240         hns3_init_txq(txq);
4241         hns3_enable_txq(txq, true);
4242         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
4243
4244         return ret;
4245 }
4246
4247 int
4248 hns3_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
4249 {
4250         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4251         struct hns3_tx_queue *txq = dev->data->tx_queues[tx_queue_id];
4252
4253         if (!hns3_dev_indep_txrx_supported(hw))
4254                 return -ENOTSUP;
4255
4256         hns3_enable_txq(txq, false);
4257         hns3_tx_queue_release_mbufs(txq);
4258         /*
4259          * All the mbufs in sw_ring are released and all the pointers in sw_ring
4260          * are set to NULL. If this queue is still called by upper layer,
4261          * residual SW status of this txq may cause these pointers in sw_ring
4262          * which have been set to NULL to be released again. To avoid it,
4263          * reinit the txq.
4264          */
4265         hns3_init_txq(txq);
4266         dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
4267
4268         return 0;
4269 }
4270
4271 static int
4272 hns3_tx_done_cleanup_full(struct hns3_tx_queue *txq, uint32_t free_cnt)
4273 {
4274         uint16_t next_to_clean = txq->next_to_clean;
4275         uint16_t next_to_use   = txq->next_to_use;
4276         uint16_t tx_bd_ready   = txq->tx_bd_ready;
4277         struct hns3_entry *tx_pkt = &txq->sw_ring[next_to_clean];
4278         struct hns3_desc *desc = &txq->tx_ring[next_to_clean];
4279         uint32_t idx;
4280
4281         if (free_cnt == 0 || free_cnt > txq->nb_tx_desc)
4282                 free_cnt = txq->nb_tx_desc;
4283
4284         for (idx = 0; idx < free_cnt; idx++) {
4285                 if (next_to_clean == next_to_use)
4286                         break;
4287
4288                 if (desc->tx.tp_fe_sc_vld_ra_ri &
4289                     rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B)))
4290                         break;
4291
4292                 if (tx_pkt->mbuf != NULL) {
4293                         rte_pktmbuf_free_seg(tx_pkt->mbuf);
4294                         tx_pkt->mbuf = NULL;
4295                 }
4296
4297                 next_to_clean++;
4298                 tx_bd_ready++;
4299                 tx_pkt++;
4300                 desc++;
4301                 if (next_to_clean == txq->nb_tx_desc) {
4302                         tx_pkt = txq->sw_ring;
4303                         desc = txq->tx_ring;
4304                         next_to_clean = 0;
4305                 }
4306         }
4307
4308         if (idx > 0) {
4309                 txq->next_to_clean = next_to_clean;
4310                 txq->tx_bd_ready = tx_bd_ready;
4311         }
4312
4313         return (int)idx;
4314 }
4315
4316 int
4317 hns3_tx_done_cleanup(void *txq, uint32_t free_cnt)
4318 {
4319         struct hns3_tx_queue *q = (struct hns3_tx_queue *)txq;
4320         struct rte_eth_dev *dev = &rte_eth_devices[q->port_id];
4321
4322         if (dev->tx_pkt_burst == hns3_xmit_pkts)
4323                 return hns3_tx_done_cleanup_full(q, free_cnt);
4324         else if (dev->tx_pkt_burst == hns3_dummy_rxtx_burst)
4325                 return 0;
4326         else
4327                 return -ENOTSUP;
4328 }
4329
4330 int
4331 hns3_dev_rx_descriptor_status(void *rx_queue, uint16_t offset)
4332 {
4333         volatile struct hns3_desc *rxdp;
4334         struct hns3_rx_queue *rxq;
4335         struct rte_eth_dev *dev;
4336         uint32_t bd_base_info;
4337         uint16_t desc_id;
4338
4339         rxq = (struct hns3_rx_queue *)rx_queue;
4340         if (offset >= rxq->nb_rx_desc)
4341                 return -EINVAL;
4342
4343         desc_id = (rxq->next_to_use + offset) % rxq->nb_rx_desc;
4344         rxdp = &rxq->rx_ring[desc_id];
4345         bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
4346         dev = &rte_eth_devices[rxq->port_id];
4347         if (dev->rx_pkt_burst == hns3_recv_pkts ||
4348             dev->rx_pkt_burst == hns3_recv_scattered_pkts) {
4349                 if (offset >= rxq->nb_rx_desc - rxq->rx_free_hold)
4350                         return RTE_ETH_RX_DESC_UNAVAIL;
4351         } else if (dev->rx_pkt_burst == hns3_recv_pkts_vec ||
4352                    dev->rx_pkt_burst == hns3_recv_pkts_vec_sve){
4353                 if (offset >= rxq->nb_rx_desc - rxq->rx_rearm_nb)
4354                         return RTE_ETH_RX_DESC_UNAVAIL;
4355         } else {
4356                 return RTE_ETH_RX_DESC_UNAVAIL;
4357         }
4358
4359         if (!(bd_base_info & BIT(HNS3_RXD_VLD_B)))
4360                 return RTE_ETH_RX_DESC_AVAIL;
4361         else
4362                 return RTE_ETH_RX_DESC_DONE;
4363 }
4364
4365 int
4366 hns3_dev_tx_descriptor_status(void *tx_queue, uint16_t offset)
4367 {
4368         volatile struct hns3_desc *txdp;
4369         struct hns3_tx_queue *txq;
4370         struct rte_eth_dev *dev;
4371         uint16_t desc_id;
4372
4373         txq = (struct hns3_tx_queue *)tx_queue;
4374         if (offset >= txq->nb_tx_desc)
4375                 return -EINVAL;
4376
4377         dev = &rte_eth_devices[txq->port_id];
4378         if (dev->tx_pkt_burst != hns3_xmit_pkts_simple &&
4379             dev->tx_pkt_burst != hns3_xmit_pkts &&
4380             dev->tx_pkt_burst != hns3_xmit_pkts_vec_sve &&
4381             dev->tx_pkt_burst != hns3_xmit_pkts_vec)
4382                 return RTE_ETH_TX_DESC_UNAVAIL;
4383
4384         desc_id = (txq->next_to_use + offset) % txq->nb_tx_desc;
4385         txdp = &txq->tx_ring[desc_id];
4386         if (txdp->tx.tp_fe_sc_vld_ra_ri & rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B)))
4387                 return RTE_ETH_TX_DESC_FULL;
4388         else
4389                 return RTE_ETH_TX_DESC_DONE;
4390 }
4391
4392 uint32_t
4393 hns3_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
4394 {
4395         /*
4396          * Number of BDs that have been processed by the driver
4397          * but have not been notified to the hardware.
4398          */
4399         uint32_t driver_hold_bd_num;
4400         struct hns3_rx_queue *rxq;
4401         uint32_t fbd_num;
4402
4403         rxq = dev->data->rx_queues[rx_queue_id];
4404         fbd_num = hns3_read_dev(rxq, HNS3_RING_RX_FBDNUM_REG);
4405         if (dev->rx_pkt_burst == hns3_recv_pkts_vec ||
4406             dev->rx_pkt_burst == hns3_recv_pkts_vec_sve)
4407                 driver_hold_bd_num = rxq->rx_rearm_nb;
4408         else
4409                 driver_hold_bd_num = rxq->rx_free_hold;
4410
4411         if (fbd_num <= driver_hold_bd_num)
4412                 return 0;
4413         else
4414                 return fbd_num - driver_hold_bd_num;
4415 }
4416
4417 void
4418 hns3_enable_rxd_adv_layout(struct hns3_hw *hw)
4419 {
4420         /*
4421          * If the hardware support rxd advanced layout, then driver enable it
4422          * default.
4423          */
4424         if (hns3_dev_rxd_adv_layout_supported(hw))
4425                 hns3_write_dev(hw, HNS3_RXD_ADV_LAYOUT_EN_REG, 1);
4426 }