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