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