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