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