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