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