net/hns3: fix statistics
[dpdk.git] / drivers / net / hns3 / hns3_rxtx.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2018-2019 Hisilicon Limited.
3  */
4
5 #include <stdarg.h>
6 #include <stdbool.h>
7 #include <stdint.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <inttypes.h>
11 #include <rte_bus_pci.h>
12 #include <rte_byteorder.h>
13 #include <rte_common.h>
14 #include <rte_cycles.h>
15 #include <rte_dev.h>
16 #include <rte_eal.h>
17 #include <rte_ether.h>
18 #include <rte_vxlan.h>
19 #include <rte_ethdev_driver.h>
20 #include <rte_io.h>
21 #include <rte_ip.h>
22 #include <rte_gre.h>
23 #include <rte_net.h>
24 #include <rte_malloc.h>
25 #include <rte_pci.h>
26
27 #include "hns3_ethdev.h"
28 #include "hns3_rxtx.h"
29 #include "hns3_regs.h"
30 #include "hns3_logs.h"
31
32 #define HNS3_CFG_DESC_NUM(num)  ((num) / 8 - 1)
33 #define DEFAULT_RX_FREE_THRESH  16
34
35 static void
36 hns3_rx_queue_release_mbufs(struct hns3_rx_queue *rxq)
37 {
38         uint16_t i;
39
40         if (rxq->sw_ring) {
41                 for (i = 0; i < rxq->nb_rx_desc; i++) {
42                         if (rxq->sw_ring[i].mbuf) {
43                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
44                                 rxq->sw_ring[i].mbuf = NULL;
45                         }
46                 }
47         }
48 }
49
50 static void
51 hns3_tx_queue_release_mbufs(struct hns3_tx_queue *txq)
52 {
53         uint16_t i;
54
55         if (txq->sw_ring) {
56                 for (i = 0; i < txq->nb_tx_desc; i++) {
57                         if (txq->sw_ring[i].mbuf) {
58                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
59                                 txq->sw_ring[i].mbuf = NULL;
60                         }
61                 }
62         }
63 }
64
65 static void
66 hns3_rx_queue_release(void *queue)
67 {
68         struct hns3_rx_queue *rxq = queue;
69         if (rxq) {
70                 hns3_rx_queue_release_mbufs(rxq);
71                 if (rxq->mz)
72                         rte_memzone_free(rxq->mz);
73                 if (rxq->sw_ring)
74                         rte_free(rxq->sw_ring);
75                 rte_free(rxq);
76         }
77 }
78
79 static void
80 hns3_tx_queue_release(void *queue)
81 {
82         struct hns3_tx_queue *txq = queue;
83         if (txq) {
84                 hns3_tx_queue_release_mbufs(txq);
85                 if (txq->mz)
86                         rte_memzone_free(txq->mz);
87                 if (txq->sw_ring)
88                         rte_free(txq->sw_ring);
89                 rte_free(txq);
90         }
91 }
92
93 void
94 hns3_dev_rx_queue_release(void *queue)
95 {
96         struct hns3_rx_queue *rxq = queue;
97         struct hns3_adapter *hns;
98
99         if (rxq == NULL)
100                 return;
101
102         hns = rxq->hns;
103         rte_spinlock_lock(&hns->hw.lock);
104         hns3_rx_queue_release(queue);
105         rte_spinlock_unlock(&hns->hw.lock);
106 }
107
108 void
109 hns3_dev_tx_queue_release(void *queue)
110 {
111         struct hns3_tx_queue *txq = queue;
112         struct hns3_adapter *hns;
113
114         if (txq == NULL)
115                 return;
116
117         hns = txq->hns;
118         rte_spinlock_lock(&hns->hw.lock);
119         hns3_tx_queue_release(queue);
120         rte_spinlock_unlock(&hns->hw.lock);
121 }
122
123 void
124 hns3_free_all_queues(struct rte_eth_dev *dev)
125 {
126         uint16_t i;
127
128         if (dev->data->rx_queues)
129                 for (i = 0; i < dev->data->nb_rx_queues; i++) {
130                         hns3_rx_queue_release(dev->data->rx_queues[i]);
131                         dev->data->rx_queues[i] = NULL;
132                 }
133
134         if (dev->data->tx_queues)
135                 for (i = 0; i < dev->data->nb_tx_queues; i++) {
136                         hns3_tx_queue_release(dev->data->tx_queues[i]);
137                         dev->data->tx_queues[i] = NULL;
138                 }
139 }
140
141 static int
142 hns3_alloc_rx_queue_mbufs(struct hns3_hw *hw, struct hns3_rx_queue *rxq)
143 {
144         struct rte_mbuf *mbuf;
145         uint64_t dma_addr;
146         uint16_t i;
147
148         for (i = 0; i < rxq->nb_rx_desc; i++) {
149                 mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
150                 if (unlikely(mbuf == NULL)) {
151                         hns3_err(hw, "Failed to allocate RXD[%d] for rx queue!",
152                                  i);
153                         hns3_rx_queue_release_mbufs(rxq);
154                         return -ENOMEM;
155                 }
156
157                 rte_mbuf_refcnt_set(mbuf, 1);
158                 mbuf->next = NULL;
159                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
160                 mbuf->nb_segs = 1;
161                 mbuf->port = rxq->port_id;
162
163                 rxq->sw_ring[i].mbuf = mbuf;
164                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
165                 rxq->rx_ring[i].addr = dma_addr;
166                 rxq->rx_ring[i].rx.bd_base_info = 0;
167         }
168
169         return 0;
170 }
171
172 static int
173 hns3_buf_size2type(uint32_t buf_size)
174 {
175         int bd_size_type;
176
177         switch (buf_size) {
178         case 512:
179                 bd_size_type = HNS3_BD_SIZE_512_TYPE;
180                 break;
181         case 1024:
182                 bd_size_type = HNS3_BD_SIZE_1024_TYPE;
183                 break;
184         case 4096:
185                 bd_size_type = HNS3_BD_SIZE_4096_TYPE;
186                 break;
187         default:
188                 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
189         }
190
191         return bd_size_type;
192 }
193
194 static void
195 hns3_init_rx_queue_hw(struct hns3_rx_queue *rxq)
196 {
197         uint32_t rx_buf_len = rxq->rx_buf_len;
198         uint64_t dma_addr = rxq->rx_ring_phys_addr;
199
200         hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_L_REG, (uint32_t)dma_addr);
201         hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_H_REG,
202                        (uint32_t)((dma_addr >> 31) >> 1));
203
204         hns3_write_dev(rxq, HNS3_RING_RX_BD_LEN_REG,
205                        hns3_buf_size2type(rx_buf_len));
206         hns3_write_dev(rxq, HNS3_RING_RX_BD_NUM_REG,
207                        HNS3_CFG_DESC_NUM(rxq->nb_rx_desc));
208 }
209
210 static void
211 hns3_init_tx_queue_hw(struct hns3_tx_queue *txq)
212 {
213         uint64_t dma_addr = txq->tx_ring_phys_addr;
214
215         hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_L_REG, (uint32_t)dma_addr);
216         hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_H_REG,
217                        (uint32_t)((dma_addr >> 31) >> 1));
218
219         hns3_write_dev(txq, HNS3_RING_TX_BD_NUM_REG,
220                        HNS3_CFG_DESC_NUM(txq->nb_tx_desc));
221 }
222
223 static void
224 hns3_enable_all_queues(struct hns3_hw *hw, bool en)
225 {
226         struct hns3_rx_queue *rxq;
227         struct hns3_tx_queue *txq;
228         uint32_t rcb_reg;
229         int i;
230
231         for (i = 0; i < hw->data->nb_rx_queues; i++) {
232                 rxq = hw->data->rx_queues[i];
233                 txq = hw->data->tx_queues[i];
234                 if (rxq == NULL || txq == NULL ||
235                     (en && (rxq->rx_deferred_start || txq->tx_deferred_start)))
236                         continue;
237                 rcb_reg = hns3_read_dev(rxq, HNS3_RING_EN_REG);
238                 if (en)
239                         rcb_reg |= BIT(HNS3_RING_EN_B);
240                 else
241                         rcb_reg &= ~BIT(HNS3_RING_EN_B);
242                 hns3_write_dev(rxq, HNS3_RING_EN_REG, rcb_reg);
243         }
244 }
245
246 static int
247 hns3_tqp_enable(struct hns3_hw *hw, uint16_t queue_id, bool enable)
248 {
249         struct hns3_cfg_com_tqp_queue_cmd *req;
250         struct hns3_cmd_desc desc;
251         int ret;
252
253         req = (struct hns3_cfg_com_tqp_queue_cmd *)desc.data;
254
255         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_COM_TQP_QUEUE, false);
256         req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
257         req->stream_id = 0;
258         hns3_set_bit(req->enable, HNS3_TQP_ENABLE_B, enable ? 1 : 0);
259
260         ret = hns3_cmd_send(hw, &desc, 1);
261         if (ret)
262                 hns3_err(hw, "TQP enable fail, ret = %d", ret);
263
264         return ret;
265 }
266
267 static int
268 hns3_send_reset_tqp_cmd(struct hns3_hw *hw, uint16_t queue_id, bool enable)
269 {
270         struct hns3_reset_tqp_queue_cmd *req;
271         struct hns3_cmd_desc desc;
272         int ret;
273
274         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, false);
275
276         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
277         req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
278         hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);
279
280         ret = hns3_cmd_send(hw, &desc, 1);
281         if (ret)
282                 hns3_err(hw, "Send tqp reset cmd error, ret = %d", ret);
283
284         return ret;
285 }
286
287 static int
288 hns3_get_reset_status(struct hns3_hw *hw, uint16_t queue_id)
289 {
290         struct hns3_reset_tqp_queue_cmd *req;
291         struct hns3_cmd_desc desc;
292         int ret;
293
294         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, true);
295
296         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
297         req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
298
299         ret = hns3_cmd_send(hw, &desc, 1);
300         if (ret) {
301                 hns3_err(hw, "Get reset status error, ret =%d", ret);
302                 return ret;
303         }
304
305         return hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
306 }
307
308 static int
309 hns3_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
310 {
311 #define HNS3_TQP_RESET_TRY_MS   200
312         uint64_t end;
313         int reset_status;
314         int ret;
315
316         ret = hns3_tqp_enable(hw, queue_id, false);
317         if (ret)
318                 return ret;
319
320         /*
321          * In current version VF is not supported when PF is driven by DPDK
322          * driver, all task queue pairs are mapped to PF function, so PF's queue
323          * id is equals to the global queue id in PF range.
324          */
325         ret = hns3_send_reset_tqp_cmd(hw, queue_id, true);
326         if (ret) {
327                 hns3_err(hw, "Send reset tqp cmd fail, ret = %d", ret);
328                 return ret;
329         }
330         ret = -ETIMEDOUT;
331         end = get_timeofday_ms() + HNS3_TQP_RESET_TRY_MS;
332         do {
333                 /* Wait for tqp hw reset */
334                 rte_delay_ms(HNS3_POLL_RESPONE_MS);
335                 reset_status = hns3_get_reset_status(hw, queue_id);
336                 if (reset_status) {
337                         ret = 0;
338                         break;
339                 }
340         } while (get_timeofday_ms() < end);
341
342         if (ret) {
343                 hns3_err(hw, "Reset TQP fail, ret = %d", ret);
344                 return ret;
345         }
346
347         ret = hns3_send_reset_tqp_cmd(hw, queue_id, false);
348         if (ret)
349                 hns3_err(hw, "Deassert the soft reset fail, ret = %d", ret);
350
351         return ret;
352 }
353
354 static int
355 hns3vf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
356 {
357         uint8_t msg_data[2];
358         int ret;
359
360         /* Disable VF's queue before send queue reset msg to PF */
361         ret = hns3_tqp_enable(hw, queue_id, false);
362         if (ret)
363                 return ret;
364
365         memcpy(msg_data, &queue_id, sizeof(uint16_t));
366
367         return hns3_send_mbx_msg(hw, HNS3_MBX_QUEUE_RESET, 0, msg_data,
368                                  sizeof(msg_data), true, NULL, 0);
369 }
370
371 static int
372 hns3_reset_queue(struct hns3_adapter *hns, uint16_t queue_id)
373 {
374         struct hns3_hw *hw = &hns->hw;
375         if (hns->is_vf)
376                 return hns3vf_reset_tqp(hw, queue_id);
377         else
378                 return hns3_reset_tqp(hw, queue_id);
379 }
380
381 int
382 hns3_reset_all_queues(struct hns3_adapter *hns)
383 {
384         struct hns3_hw *hw = &hns->hw;
385         int ret;
386         uint16_t i;
387
388         for (i = 0; i < hw->data->nb_rx_queues; i++) {
389                 ret = hns3_reset_queue(hns, i);
390                 if (ret) {
391                         hns3_err(hw, "Failed to reset No.%d queue: %d", i, ret);
392                         return ret;
393                 }
394         }
395         return 0;
396 }
397
398 static int
399 hns3_dev_rx_queue_start(struct hns3_adapter *hns, uint16_t idx)
400 {
401         struct hns3_hw *hw = &hns->hw;
402         struct hns3_rx_queue *rxq;
403         int ret;
404
405         PMD_INIT_FUNC_TRACE();
406
407         rxq = hw->data->rx_queues[idx];
408
409         ret = hns3_alloc_rx_queue_mbufs(hw, rxq);
410         if (ret) {
411                 hns3_err(hw, "Failed to alloc mbuf for No.%d rx queue: %d",
412                             idx, ret);
413                 return ret;
414         }
415
416         rxq->next_to_use = 0;
417         rxq->next_to_clean = 0;
418         hns3_init_rx_queue_hw(rxq);
419
420         return 0;
421 }
422
423 static void
424 hns3_dev_tx_queue_start(struct hns3_adapter *hns, uint16_t idx)
425 {
426         struct hns3_hw *hw = &hns->hw;
427         struct hns3_tx_queue *txq;
428         struct hns3_desc *desc;
429         int i;
430
431         txq = hw->data->tx_queues[idx];
432
433         /* Clear tx bd */
434         desc = txq->tx_ring;
435         for (i = 0; i < txq->nb_tx_desc; i++) {
436                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
437                 desc++;
438         }
439
440         txq->next_to_use = 0;
441         txq->next_to_clean = 0;
442         txq->tx_bd_ready   = txq->nb_tx_desc;
443         hns3_init_tx_queue_hw(txq);
444 }
445
446 static void
447 hns3_init_tx_ring_tc(struct hns3_adapter *hns)
448 {
449         struct hns3_hw *hw = &hns->hw;
450         struct hns3_tx_queue *txq;
451         int i, num;
452
453         for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
454                 struct hns3_tc_queue_info *tc_queue = &hw->tc_queue[i];
455                 int j;
456
457                 if (!tc_queue->enable)
458                         continue;
459
460                 for (j = 0; j < tc_queue->tqp_count; j++) {
461                         num = tc_queue->tqp_offset + j;
462                         txq = hw->data->tx_queues[num];
463                         if (txq == NULL)
464                                 continue;
465
466                         hns3_write_dev(txq, HNS3_RING_TX_TC_REG, tc_queue->tc);
467                 }
468         }
469 }
470
471 int
472 hns3_start_queues(struct hns3_adapter *hns, bool reset_queue)
473 {
474         struct hns3_hw *hw = &hns->hw;
475         struct rte_eth_dev_data *dev_data = hw->data;
476         struct hns3_rx_queue *rxq;
477         struct hns3_tx_queue *txq;
478         int ret;
479         int i;
480         int j;
481
482         /* Initialize RSS for queues */
483         ret = hns3_config_rss(hns);
484         if (ret) {
485                 hns3_err(hw, "Failed to configure rss %d", ret);
486                 return ret;
487         }
488
489         if (reset_queue) {
490                 ret = hns3_reset_all_queues(hns);
491                 if (ret) {
492                         hns3_err(hw, "Failed to reset all queues %d", ret);
493                         return ret;
494                 }
495         }
496
497         /*
498          * Hardware does not support where the number of rx and tx queues is
499          * not equal in hip08. In .dev_configure callback function we will
500          * check the two values, here we think that the number of rx and tx
501          * queues is equal.
502          */
503         for (i = 0; i < hw->data->nb_rx_queues; i++) {
504                 rxq = dev_data->rx_queues[i];
505                 txq = dev_data->tx_queues[i];
506                 if (rxq == NULL || txq == NULL || rxq->rx_deferred_start ||
507                     txq->tx_deferred_start)
508                         continue;
509
510                 ret = hns3_dev_rx_queue_start(hns, i);
511                 if (ret) {
512                         hns3_err(hw, "Failed to start No.%d rx queue: %d", i,
513                                  ret);
514                         goto out;
515                 }
516                 hns3_dev_tx_queue_start(hns, i);
517         }
518         hns3_init_tx_ring_tc(hns);
519
520         hns3_enable_all_queues(hw, true);
521         return 0;
522
523 out:
524         for (j = 0; j < i; j++) {
525                 rxq = dev_data->rx_queues[j];
526                 hns3_rx_queue_release_mbufs(rxq);
527         }
528
529         return ret;
530 }
531
532 int
533 hns3_stop_queues(struct hns3_adapter *hns, bool reset_queue)
534 {
535         struct hns3_hw *hw = &hns->hw;
536         int ret;
537
538         hns3_enable_all_queues(hw, false);
539         if (reset_queue) {
540                 ret = hns3_reset_all_queues(hns);
541                 if (ret) {
542                         hns3_err(hw, "Failed to reset all queues %d", ret);
543                         return ret;
544                 }
545         }
546         return 0;
547 }
548
549 void
550 hns3_dev_release_mbufs(struct hns3_adapter *hns)
551 {
552         struct rte_eth_dev_data *dev_data = hns->hw.data;
553         struct hns3_rx_queue *rxq;
554         struct hns3_tx_queue *txq;
555         int i;
556
557         if (dev_data->rx_queues)
558                 for (i = 0; i < dev_data->nb_rx_queues; i++) {
559                         rxq = dev_data->rx_queues[i];
560                         if (rxq == NULL || rxq->rx_deferred_start)
561                                 continue;
562                         hns3_rx_queue_release_mbufs(rxq);
563                 }
564
565         if (dev_data->tx_queues)
566                 for (i = 0; i < dev_data->nb_tx_queues; i++) {
567                         txq = dev_data->tx_queues[i];
568                         if (txq == NULL || txq->tx_deferred_start)
569                                 continue;
570                         hns3_tx_queue_release_mbufs(txq);
571                 }
572 }
573
574 int
575 hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
576                     unsigned int socket_id, const struct rte_eth_rxconf *conf,
577                     struct rte_mempool *mp)
578 {
579         struct hns3_adapter *hns = dev->data->dev_private;
580         const struct rte_memzone *rx_mz;
581         struct hns3_hw *hw = &hns->hw;
582         struct hns3_rx_queue *rxq;
583         unsigned int desc_size = sizeof(struct hns3_desc);
584         unsigned int rx_desc;
585         int rx_entry_len;
586
587         if (dev->data->dev_started) {
588                 hns3_err(hw, "rx_queue_setup after dev_start no supported");
589                 return -EINVAL;
590         }
591
592         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
593             nb_desc % HNS3_ALIGN_RING_DESC) {
594                 hns3_err(hw, "Number (%u) of rx descriptors is invalid",
595                          nb_desc);
596                 return -EINVAL;
597         }
598
599         if (dev->data->rx_queues[idx]) {
600                 hns3_rx_queue_release(dev->data->rx_queues[idx]);
601                 dev->data->rx_queues[idx] = NULL;
602         }
603
604         rxq = rte_zmalloc_socket("hns3 RX queue", sizeof(struct hns3_rx_queue),
605                                  RTE_CACHE_LINE_SIZE, socket_id);
606         if (rxq == NULL) {
607                 hns3_err(hw, "Failed to allocate memory for rx queue!");
608                 return -ENOMEM;
609         }
610
611         rxq->hns = hns;
612         rxq->mb_pool = mp;
613         rxq->nb_rx_desc = nb_desc;
614         rxq->queue_id = idx;
615         if (conf->rx_free_thresh <= 0)
616                 rxq->rx_free_thresh = DEFAULT_RX_FREE_THRESH;
617         else
618                 rxq->rx_free_thresh = conf->rx_free_thresh;
619         rxq->rx_deferred_start = conf->rx_deferred_start;
620
621         rx_entry_len = sizeof(struct hns3_entry) * rxq->nb_rx_desc;
622         rxq->sw_ring = rte_zmalloc_socket("hns3 RX sw ring", rx_entry_len,
623                                           RTE_CACHE_LINE_SIZE, socket_id);
624         if (rxq->sw_ring == NULL) {
625                 hns3_err(hw, "Failed to allocate memory for rx sw ring!");
626                 hns3_rx_queue_release(rxq);
627                 return -ENOMEM;
628         }
629
630         /* Allocate rx ring hardware descriptors. */
631         rx_desc = rxq->nb_rx_desc * desc_size;
632         rx_mz = rte_eth_dma_zone_reserve(dev, "rx_ring", idx, rx_desc,
633                                          HNS3_RING_BASE_ALIGN, socket_id);
634         if (rx_mz == NULL) {
635                 hns3_err(hw, "Failed to reserve DMA memory for No.%d rx ring!",
636                          idx);
637                 hns3_rx_queue_release(rxq);
638                 return -ENOMEM;
639         }
640         rxq->mz = rx_mz;
641         rxq->rx_ring = (struct hns3_desc *)rx_mz->addr;
642         rxq->rx_ring_phys_addr = rx_mz->iova;
643
644         hns3_dbg(hw, "No.%d rx descriptors iova 0x%" PRIx64, idx,
645                  rxq->rx_ring_phys_addr);
646
647         rxq->next_to_use = 0;
648         rxq->next_to_clean = 0;
649         rxq->nb_rx_hold = 0;
650         rxq->pkt_first_seg = NULL;
651         rxq->pkt_last_seg = NULL;
652         rxq->port_id = dev->data->port_id;
653         rxq->configured = true;
654         rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
655                                 idx * HNS3_TQP_REG_SIZE);
656         rxq->rx_buf_len = hw->rx_buf_len;
657         rxq->non_vld_descs = 0;
658         rxq->l2_errors = 0;
659         rxq->pkt_len_errors = 0;
660         rxq->l3_csum_erros = 0;
661         rxq->l4_csum_erros = 0;
662         rxq->ol3_csum_erros = 0;
663         rxq->ol4_csum_erros = 0;
664
665         rte_spinlock_lock(&hw->lock);
666         dev->data->rx_queues[idx] = rxq;
667         rte_spinlock_unlock(&hw->lock);
668
669         return 0;
670 }
671
672 static inline uint32_t
673 rxd_pkt_info_to_pkt_type(uint32_t pkt_info, uint32_t ol_info)
674 {
675 #define HNS3_L2TBL_NUM  4
676 #define HNS3_L3TBL_NUM  16
677 #define HNS3_L4TBL_NUM  16
678 #define HNS3_OL3TBL_NUM 16
679 #define HNS3_OL4TBL_NUM 16
680         uint32_t pkt_type = 0;
681         uint32_t l2id, l3id, l4id;
682         uint32_t ol3id, ol4id;
683
684         static const uint32_t l2table[HNS3_L2TBL_NUM] = {
685                 RTE_PTYPE_L2_ETHER,
686                 RTE_PTYPE_L2_ETHER_VLAN,
687                 RTE_PTYPE_L2_ETHER_QINQ,
688                 0
689         };
690
691         static const uint32_t l3table[HNS3_L3TBL_NUM] = {
692                 RTE_PTYPE_L3_IPV4,
693                 RTE_PTYPE_L3_IPV6,
694                 RTE_PTYPE_L2_ETHER_ARP,
695                 RTE_PTYPE_L2_ETHER,
696                 RTE_PTYPE_L3_IPV4_EXT,
697                 RTE_PTYPE_L3_IPV6_EXT,
698                 RTE_PTYPE_L2_ETHER_LLDP,
699                 0, 0, 0, 0, 0, 0, 0, 0, 0
700         };
701
702         static const uint32_t l4table[HNS3_L4TBL_NUM] = {
703                 RTE_PTYPE_L4_UDP,
704                 RTE_PTYPE_L4_TCP,
705                 RTE_PTYPE_TUNNEL_GRE,
706                 RTE_PTYPE_L4_SCTP,
707                 RTE_PTYPE_L4_IGMP,
708                 RTE_PTYPE_L4_ICMP,
709                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
710         };
711
712         static const uint32_t inner_l2table[HNS3_L2TBL_NUM] = {
713                 RTE_PTYPE_INNER_L2_ETHER,
714                 RTE_PTYPE_INNER_L2_ETHER_VLAN,
715                 RTE_PTYPE_INNER_L2_ETHER_QINQ,
716                 0
717         };
718
719         static const uint32_t inner_l3table[HNS3_L3TBL_NUM] = {
720                 RTE_PTYPE_INNER_L3_IPV4,
721                 RTE_PTYPE_INNER_L3_IPV6,
722                 0,
723                 RTE_PTYPE_INNER_L2_ETHER,
724                 RTE_PTYPE_INNER_L3_IPV4_EXT,
725                 RTE_PTYPE_INNER_L3_IPV6_EXT,
726                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
727         };
728
729         static const uint32_t inner_l4table[HNS3_L4TBL_NUM] = {
730                 RTE_PTYPE_INNER_L4_UDP,
731                 RTE_PTYPE_INNER_L4_TCP,
732                 RTE_PTYPE_TUNNEL_GRE,
733                 RTE_PTYPE_INNER_L4_SCTP,
734                 RTE_PTYPE_L4_IGMP,
735                 RTE_PTYPE_INNER_L4_ICMP,
736                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
737         };
738
739         static const uint32_t ol3table[HNS3_OL3TBL_NUM] = {
740                 RTE_PTYPE_L3_IPV4,
741                 RTE_PTYPE_L3_IPV6,
742                 0, 0,
743                 RTE_PTYPE_L3_IPV4_EXT,
744                 RTE_PTYPE_L3_IPV6_EXT,
745                 0, 0, 0, 0, 0, 0, 0, 0, 0,
746                 RTE_PTYPE_UNKNOWN
747         };
748
749         static const uint32_t ol4table[HNS3_OL4TBL_NUM] = {
750                 0,
751                 RTE_PTYPE_TUNNEL_VXLAN,
752                 RTE_PTYPE_TUNNEL_NVGRE,
753                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
754         };
755
756         l2id = hns3_get_field(pkt_info, HNS3_RXD_STRP_TAGP_M,
757                               HNS3_RXD_STRP_TAGP_S);
758         l3id = hns3_get_field(pkt_info, HNS3_RXD_L3ID_M, HNS3_RXD_L3ID_S);
759         l4id = hns3_get_field(pkt_info, HNS3_RXD_L4ID_M, HNS3_RXD_L4ID_S);
760         ol3id = hns3_get_field(ol_info, HNS3_RXD_OL3ID_M, HNS3_RXD_OL3ID_S);
761         ol4id = hns3_get_field(ol_info, HNS3_RXD_OL4ID_M, HNS3_RXD_OL4ID_S);
762
763         if (ol4table[ol4id])
764                 pkt_type |= (inner_l2table[l2id] | inner_l3table[l3id] |
765                              inner_l4table[l4id] | ol3table[ol3id] |
766                              ol4table[ol4id]);
767         else
768                 pkt_type |= (l2table[l2id] | l3table[l3id] | l4table[l4id]);
769         return pkt_type;
770 }
771
772 const uint32_t *
773 hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev)
774 {
775         static const uint32_t ptypes[] = {
776                 RTE_PTYPE_L2_ETHER,
777                 RTE_PTYPE_L2_ETHER_VLAN,
778                 RTE_PTYPE_L2_ETHER_QINQ,
779                 RTE_PTYPE_L2_ETHER_LLDP,
780                 RTE_PTYPE_L2_ETHER_ARP,
781                 RTE_PTYPE_L3_IPV4,
782                 RTE_PTYPE_L3_IPV4_EXT,
783                 RTE_PTYPE_L3_IPV6,
784                 RTE_PTYPE_L3_IPV6_EXT,
785                 RTE_PTYPE_L4_IGMP,
786                 RTE_PTYPE_L4_ICMP,
787                 RTE_PTYPE_L4_SCTP,
788                 RTE_PTYPE_L4_TCP,
789                 RTE_PTYPE_L4_UDP,
790                 RTE_PTYPE_TUNNEL_GRE,
791                 RTE_PTYPE_UNKNOWN
792         };
793
794         if (dev->rx_pkt_burst == hns3_recv_pkts)
795                 return ptypes;
796
797         return NULL;
798 }
799
800 static void
801 hns3_clean_rx_buffers(struct hns3_rx_queue *rxq, int count)
802 {
803         rxq->next_to_use += count;
804         if (rxq->next_to_use >= rxq->nb_rx_desc)
805                 rxq->next_to_use -= rxq->nb_rx_desc;
806
807         hns3_write_dev(rxq, HNS3_RING_RX_HEAD_REG, count);
808 }
809
810 static int
811 hns3_handle_bdinfo(struct hns3_rx_queue *rxq, struct rte_mbuf *rxm,
812                    uint32_t bd_base_info, uint32_t l234_info,
813                    uint32_t *cksum_err)
814 {
815         uint32_t tmp = 0;
816
817         if (unlikely(l234_info & BIT(HNS3_RXD_L2E_B))) {
818                 rxq->l2_errors++;
819                 return -EINVAL;
820         }
821
822         if (unlikely(rxm->pkt_len == 0 ||
823                 (l234_info & BIT(HNS3_RXD_TRUNCAT_B)))) {
824                 rxq->pkt_len_errors++;
825                 return -EINVAL;
826         }
827
828         if (bd_base_info & BIT(HNS3_RXD_L3L4P_B)) {
829                 if (unlikely(l234_info & BIT(HNS3_RXD_L3E_B))) {
830                         rxm->ol_flags |= PKT_RX_IP_CKSUM_BAD;
831                         rxq->l3_csum_erros++;
832                         tmp |= HNS3_L3_CKSUM_ERR;
833                 }
834
835                 if (unlikely(l234_info & BIT(HNS3_RXD_L4E_B))) {
836                         rxm->ol_flags |= PKT_RX_L4_CKSUM_BAD;
837                         rxq->l4_csum_erros++;
838                         tmp |= HNS3_L4_CKSUM_ERR;
839                 }
840
841                 if (unlikely(l234_info & BIT(HNS3_RXD_OL3E_B))) {
842                         rxq->ol3_csum_erros++;
843                         tmp |= HNS3_OUTER_L3_CKSUM_ERR;
844                 }
845
846                 if (unlikely(l234_info & BIT(HNS3_RXD_OL4E_B))) {
847                         rxm->ol_flags |= PKT_RX_OUTER_L4_CKSUM_BAD;
848                         rxq->ol4_csum_erros++;
849                         tmp |= HNS3_OUTER_L4_CKSUM_ERR;
850                 }
851         }
852         *cksum_err = tmp;
853
854         return 0;
855 }
856
857 static void
858 hns3_rx_set_cksum_flag(struct rte_mbuf *rxm, uint64_t packet_type,
859                        const uint32_t cksum_err)
860 {
861         if (unlikely((packet_type & RTE_PTYPE_TUNNEL_MASK))) {
862                 if (likely(packet_type & RTE_PTYPE_INNER_L3_MASK) &&
863                     (cksum_err & HNS3_L3_CKSUM_ERR) == 0)
864                         rxm->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
865                 if (likely(packet_type & RTE_PTYPE_INNER_L4_MASK) &&
866                     (cksum_err & HNS3_L4_CKSUM_ERR) == 0)
867                         rxm->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
868                 if (likely(packet_type & RTE_PTYPE_L4_MASK) &&
869                     (cksum_err & HNS3_OUTER_L4_CKSUM_ERR) == 0)
870                         rxm->ol_flags |= PKT_RX_OUTER_L4_CKSUM_GOOD;
871         } else {
872                 if (likely(packet_type & RTE_PTYPE_L3_MASK) &&
873                     (cksum_err & HNS3_L3_CKSUM_ERR) == 0)
874                         rxm->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
875                 if (likely(packet_type & RTE_PTYPE_L4_MASK) &&
876                     (cksum_err & HNS3_L4_CKSUM_ERR) == 0)
877                         rxm->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
878         }
879 }
880
881 uint16_t
882 hns3_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
883 {
884         struct hns3_rx_queue *rxq;      /* RX queue */
885         struct hns3_desc *rx_ring;      /* RX ring (desc) */
886         struct hns3_entry *sw_ring;
887         struct hns3_entry *rxe;
888         struct hns3_desc *rxdp;         /* pointer of the current desc */
889         struct rte_mbuf *first_seg;
890         struct rte_mbuf *last_seg;
891         struct rte_mbuf *nmb;           /* pointer of the new mbuf */
892         struct rte_mbuf *rxm;
893         struct rte_eth_dev *dev;
894         uint32_t bd_base_info;
895         uint32_t cksum_err;
896         uint32_t l234_info;
897         uint32_t ol_info;
898         uint64_t dma_addr;
899         uint16_t data_len;
900         uint16_t nb_rx_bd;
901         uint16_t pkt_len;
902         uint16_t nb_rx;
903         uint16_t rx_id;
904         int num;                        /* num of desc in ring */
905         int ret;
906
907         nb_rx = 0;
908         nb_rx_bd = 0;
909         rxq = rx_queue;
910         dev = &rte_eth_devices[rxq->port_id];
911
912         rx_id = rxq->next_to_clean;
913         rx_ring = rxq->rx_ring;
914         first_seg = rxq->pkt_first_seg;
915         last_seg = rxq->pkt_last_seg;
916         sw_ring = rxq->sw_ring;
917
918         /* Get num of packets in descriptor ring */
919         num = hns3_read_dev(rxq, HNS3_RING_RX_FBDNUM_REG);
920         while (nb_rx_bd < num && nb_rx < nb_pkts) {
921                 rxdp = &rx_ring[rx_id];
922                 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
923                 if (unlikely(!hns3_get_bit(bd_base_info, HNS3_RXD_VLD_B))) {
924                         rxq->non_vld_descs++;
925                         break;
926                 }
927
928                 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
929                 if (unlikely(nmb == NULL)) {
930                         dev->data->rx_mbuf_alloc_failed++;
931                         break;
932                 }
933
934                 nb_rx_bd++;
935                 rxe = &sw_ring[rx_id];
936                 rx_id++;
937                 if (rx_id == rxq->nb_rx_desc)
938                         rx_id = 0;
939
940                 rte_prefetch0(sw_ring[rx_id].mbuf);
941                 if ((rx_id & 0x3) == 0) {
942                         rte_prefetch0(&rx_ring[rx_id]);
943                         rte_prefetch0(&sw_ring[rx_id]);
944                 }
945
946                 rxm = rxe->mbuf;
947                 rxe->mbuf = nmb;
948
949                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
950                 rxdp->addr = dma_addr;
951                 rxdp->rx.bd_base_info = 0;
952
953                 rte_cio_rmb();
954                 /* Load remained descriptor data and extract necessary fields */
955                 data_len = (uint16_t)(rte_le_to_cpu_16(rxdp->rx.size));
956                 l234_info = rte_le_to_cpu_32(rxdp->rx.l234_info);
957                 ol_info = rte_le_to_cpu_32(rxdp->rx.ol_info);
958
959                 if (first_seg == NULL) {
960                         first_seg = rxm;
961                         first_seg->nb_segs = 1;
962                 } else {
963                         first_seg->nb_segs++;
964                         last_seg->next = rxm;
965                 }
966
967                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
968                 rxm->data_len = data_len;
969
970                 if (!hns3_get_bit(bd_base_info, HNS3_RXD_FE_B)) {
971                         last_seg = rxm;
972                         continue;
973                 }
974
975                 /* The last buffer of the received packet */
976                 pkt_len = (uint16_t)(rte_le_to_cpu_16(rxdp->rx.pkt_len));
977                 first_seg->pkt_len = pkt_len;
978                 first_seg->port = rxq->port_id;
979                 first_seg->hash.rss = rte_le_to_cpu_32(rxdp->rx.rss_hash);
980                 if (unlikely(hns3_get_bit(bd_base_info, HNS3_RXD_LUM_B))) {
981                         first_seg->hash.fdir.hi =
982                                 rte_le_to_cpu_32(rxdp->rx.fd_id);
983                         first_seg->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
984                 }
985                 rxm->next = NULL;
986
987                 ret = hns3_handle_bdinfo(rxq, first_seg, bd_base_info,
988                                          l234_info, &cksum_err);
989                 if (unlikely(ret))
990                         goto pkt_err;
991
992                 first_seg->packet_type = rxd_pkt_info_to_pkt_type(l234_info,
993                                                                   ol_info);
994
995                 if (bd_base_info & BIT(HNS3_RXD_L3L4P_B))
996                         hns3_rx_set_cksum_flag(rxm, first_seg->packet_type,
997                                                cksum_err);
998
999                 first_seg->vlan_tci = rte_le_to_cpu_16(rxdp->rx.vlan_tag);
1000                 first_seg->vlan_tci_outer =
1001                         rte_le_to_cpu_16(rxdp->rx.ot_vlan_tag);
1002                 rx_pkts[nb_rx++] = first_seg;
1003                 first_seg = NULL;
1004                 continue;
1005 pkt_err:
1006                 rte_pktmbuf_free(first_seg);
1007                 first_seg = NULL;
1008         }
1009
1010         rxq->next_to_clean = rx_id;
1011         rxq->pkt_first_seg = first_seg;
1012         rxq->pkt_last_seg = last_seg;
1013         hns3_clean_rx_buffers(rxq, nb_rx_bd);
1014
1015         return nb_rx;
1016 }
1017
1018 int
1019 hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
1020                     unsigned int socket_id, const struct rte_eth_txconf *conf)
1021 {
1022         struct hns3_adapter *hns = dev->data->dev_private;
1023         const struct rte_memzone *tx_mz;
1024         struct hns3_hw *hw = &hns->hw;
1025         struct hns3_tx_queue *txq;
1026         struct hns3_desc *desc;
1027         unsigned int desc_size = sizeof(struct hns3_desc);
1028         unsigned int tx_desc;
1029         int tx_entry_len;
1030         int i;
1031
1032         if (dev->data->dev_started) {
1033                 hns3_err(hw, "tx_queue_setup after dev_start no supported");
1034                 return -EINVAL;
1035         }
1036
1037         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
1038             nb_desc % HNS3_ALIGN_RING_DESC) {
1039                 hns3_err(hw, "Number (%u) of tx descriptors is invalid",
1040                             nb_desc);
1041                 return -EINVAL;
1042         }
1043
1044         if (dev->data->tx_queues[idx] != NULL) {
1045                 hns3_tx_queue_release(dev->data->tx_queues[idx]);
1046                 dev->data->tx_queues[idx] = NULL;
1047         }
1048
1049         txq = rte_zmalloc_socket("hns3 TX queue", sizeof(struct hns3_tx_queue),
1050                                  RTE_CACHE_LINE_SIZE, socket_id);
1051         if (txq == NULL) {
1052                 hns3_err(hw, "Failed to allocate memory for tx queue!");
1053                 return -ENOMEM;
1054         }
1055
1056         txq->nb_tx_desc = nb_desc;
1057         txq->queue_id = idx;
1058         txq->tx_deferred_start = conf->tx_deferred_start;
1059
1060         tx_entry_len = sizeof(struct hns3_entry) * txq->nb_tx_desc;
1061         txq->sw_ring = rte_zmalloc_socket("hns3 TX sw ring", tx_entry_len,
1062                                           RTE_CACHE_LINE_SIZE, socket_id);
1063         if (txq->sw_ring == NULL) {
1064                 hns3_err(hw, "Failed to allocate memory for tx sw ring!");
1065                 hns3_tx_queue_release(txq);
1066                 return -ENOMEM;
1067         }
1068
1069         /* Allocate tx ring hardware descriptors. */
1070         tx_desc = txq->nb_tx_desc * desc_size;
1071         tx_mz = rte_eth_dma_zone_reserve(dev, "tx_ring", idx, tx_desc,
1072                                          HNS3_RING_BASE_ALIGN, socket_id);
1073         if (tx_mz == NULL) {
1074                 hns3_err(hw, "Failed to reserve DMA memory for No.%d tx ring!",
1075                          idx);
1076                 hns3_tx_queue_release(txq);
1077                 return -ENOMEM;
1078         }
1079         txq->mz = tx_mz;
1080         txq->tx_ring = (struct hns3_desc *)tx_mz->addr;
1081         txq->tx_ring_phys_addr = tx_mz->iova;
1082
1083         hns3_dbg(hw, "No.%d tx descriptors iova 0x%" PRIx64, idx,
1084                  txq->tx_ring_phys_addr);
1085
1086         /* Clear tx bd */
1087         desc = txq->tx_ring;
1088         for (i = 0; i < txq->nb_tx_desc; i++) {
1089                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
1090                 desc++;
1091         }
1092
1093         txq->hns = hns;
1094         txq->next_to_use = 0;
1095         txq->next_to_clean = 0;
1096         txq->tx_bd_ready   = txq->nb_tx_desc;
1097         txq->port_id = dev->data->port_id;
1098         txq->configured = true;
1099         txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1100                                 idx * HNS3_TQP_REG_SIZE);
1101         rte_spinlock_lock(&hw->lock);
1102         dev->data->tx_queues[idx] = txq;
1103         rte_spinlock_unlock(&hw->lock);
1104
1105         return 0;
1106 }
1107
1108 static inline int
1109 tx_ring_dist(struct hns3_tx_queue *txq, int begin, int end)
1110 {
1111         return (end - begin + txq->nb_tx_desc) % txq->nb_tx_desc;
1112 }
1113
1114 static inline int
1115 tx_ring_space(struct hns3_tx_queue *txq)
1116 {
1117         return txq->nb_tx_desc -
1118                 tx_ring_dist(txq, txq->next_to_clean, txq->next_to_use) - 1;
1119 }
1120
1121 static inline void
1122 hns3_queue_xmit(struct hns3_tx_queue *txq, uint32_t buf_num)
1123 {
1124         hns3_write_dev(txq, HNS3_RING_TX_TAIL_REG, buf_num);
1125 }
1126
1127 static void
1128 hns3_tx_free_useless_buffer(struct hns3_tx_queue *txq)
1129 {
1130         uint16_t tx_next_clean = txq->next_to_clean;
1131         uint16_t tx_next_use   = txq->next_to_use;
1132         uint16_t tx_bd_ready   = txq->tx_bd_ready;
1133         uint16_t tx_bd_max     = txq->nb_tx_desc;
1134         struct hns3_entry *tx_bak_pkt = &txq->sw_ring[tx_next_clean];
1135         struct hns3_desc *desc = &txq->tx_ring[tx_next_clean];
1136         struct rte_mbuf *mbuf;
1137
1138         while ((!hns3_get_bit(desc->tx.tp_fe_sc_vld_ra_ri, HNS3_TXD_VLD_B)) &&
1139                 (tx_next_use != tx_next_clean || tx_bd_ready < tx_bd_max)) {
1140                 mbuf = tx_bak_pkt->mbuf;
1141                 if (mbuf) {
1142                         mbuf->next = NULL;
1143                         rte_pktmbuf_free(mbuf);
1144                         tx_bak_pkt->mbuf = NULL;
1145                 }
1146
1147                 desc++;
1148                 tx_bak_pkt++;
1149                 tx_next_clean++;
1150                 tx_bd_ready++;
1151
1152                 if (tx_next_clean >= tx_bd_max) {
1153                         tx_next_clean = 0;
1154                         desc = txq->tx_ring;
1155                         tx_bak_pkt = txq->sw_ring;
1156                 }
1157         }
1158
1159         txq->next_to_clean = tx_next_clean;
1160         txq->tx_bd_ready   = tx_bd_ready;
1161 }
1162
1163 static void
1164 fill_desc(struct hns3_tx_queue *txq, uint16_t tx_desc_id, struct rte_mbuf *rxm,
1165           bool first, int offset)
1166 {
1167         struct hns3_desc *tx_ring = txq->tx_ring;
1168         struct hns3_desc *desc = &tx_ring[tx_desc_id];
1169         uint8_t frag_end = rxm->next == NULL ? 1 : 0;
1170         uint16_t size = rxm->data_len;
1171         uint16_t rrcfv = 0;
1172         uint64_t ol_flags = rxm->ol_flags;
1173         uint32_t hdr_len;
1174         uint32_t paylen;
1175         uint32_t tmp;
1176
1177         desc->addr = rte_mbuf_data_iova(rxm) + offset;
1178         desc->tx.send_size = rte_cpu_to_le_16(size);
1179         hns3_set_bit(rrcfv, HNS3_TXD_VLD_B, 1);
1180
1181         if (first) {
1182                 hdr_len = rxm->l2_len + rxm->l3_len + rxm->l4_len;
1183                 hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
1184                            rxm->outer_l2_len + rxm->outer_l3_len : 0;
1185                 paylen = rxm->pkt_len - hdr_len;
1186                 desc->tx.paylen = rte_cpu_to_le_32(paylen);
1187         }
1188
1189         hns3_set_bit(rrcfv, HNS3_TXD_FE_B, frag_end);
1190         desc->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(rrcfv);
1191
1192         if (frag_end) {
1193                 if (ol_flags & (PKT_TX_VLAN_PKT | PKT_TX_QINQ_PKT)) {
1194                         tmp = rte_le_to_cpu_32(desc->tx.type_cs_vlan_tso_len);
1195                         hns3_set_bit(tmp, HNS3_TXD_VLAN_B, 1);
1196                         desc->tx.type_cs_vlan_tso_len = rte_cpu_to_le_32(tmp);
1197                         desc->tx.vlan_tag = rte_cpu_to_le_16(rxm->vlan_tci);
1198                 }
1199
1200                 if (ol_flags & PKT_TX_QINQ_PKT) {
1201                         tmp = rte_le_to_cpu_32(desc->tx.ol_type_vlan_len_msec);
1202                         hns3_set_bit(tmp, HNS3_TXD_OVLAN_B, 1);
1203                         desc->tx.ol_type_vlan_len_msec = rte_cpu_to_le_32(tmp);
1204                         desc->tx.outer_vlan_tag =
1205                                 rte_cpu_to_le_16(rxm->vlan_tci_outer);
1206                 }
1207         }
1208 }
1209
1210 static int
1211 hns3_tx_alloc_mbufs(struct hns3_tx_queue *txq, struct rte_mempool *mb_pool,
1212                     uint16_t nb_new_buf, struct rte_mbuf **alloc_mbuf)
1213 {
1214         struct rte_mbuf *new_mbuf = NULL;
1215         struct rte_eth_dev *dev;
1216         struct rte_mbuf *temp;
1217         struct hns3_hw *hw;
1218         uint16_t i;
1219
1220         /* Allocate enough mbufs */
1221         for (i = 0; i < nb_new_buf; i++) {
1222                 temp = rte_pktmbuf_alloc(mb_pool);
1223                 if (unlikely(temp == NULL)) {
1224                         dev = &rte_eth_devices[txq->port_id];
1225                         hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1226                         hns3_err(hw, "Failed to alloc TX mbuf port_id=%d,"
1227                                      "queue_id=%d in reassemble tx pkts.",
1228                                      txq->port_id, txq->queue_id);
1229                         rte_pktmbuf_free(new_mbuf);
1230                         return -ENOMEM;
1231                 }
1232                 temp->next = new_mbuf;
1233                 new_mbuf = temp;
1234         }
1235
1236         if (new_mbuf == NULL)
1237                 return -ENOMEM;
1238
1239         new_mbuf->nb_segs = nb_new_buf;
1240         *alloc_mbuf = new_mbuf;
1241
1242         return 0;
1243 }
1244
1245 static int
1246 hns3_reassemble_tx_pkts(void *tx_queue, struct rte_mbuf *tx_pkt,
1247                         struct rte_mbuf **new_pkt)
1248 {
1249         struct hns3_tx_queue *txq = tx_queue;
1250         struct rte_mempool *mb_pool;
1251         struct rte_mbuf *new_mbuf;
1252         struct rte_mbuf *temp_new;
1253         struct rte_mbuf *temp;
1254         uint16_t last_buf_len;
1255         uint16_t nb_new_buf;
1256         uint16_t buf_size;
1257         uint16_t buf_len;
1258         uint16_t len_s;
1259         uint16_t len_d;
1260         uint16_t len;
1261         uint16_t i;
1262         int ret;
1263         char *s;
1264         char *d;
1265
1266         mb_pool = tx_pkt->pool;
1267         buf_size = tx_pkt->buf_len - RTE_PKTMBUF_HEADROOM;
1268         nb_new_buf = (tx_pkt->pkt_len - 1) / buf_size + 1;
1269
1270         last_buf_len = tx_pkt->pkt_len % buf_size;
1271         if (last_buf_len == 0)
1272                 last_buf_len = buf_size;
1273
1274         /* Allocate enough mbufs */
1275         ret = hns3_tx_alloc_mbufs(txq, mb_pool, nb_new_buf, &new_mbuf);
1276         if (ret)
1277                 return ret;
1278
1279         /* Copy the original packet content to the new mbufs */
1280         temp = tx_pkt;
1281         s = rte_pktmbuf_mtod(temp, char *);
1282         len_s = temp->data_len;
1283         temp_new = new_mbuf;
1284         for (i = 0; i < nb_new_buf; i++) {
1285                 d = rte_pktmbuf_mtod(temp_new, char *);
1286                 if (i < nb_new_buf - 1)
1287                         buf_len = buf_size;
1288                 else
1289                         buf_len = last_buf_len;
1290                 len_d = buf_len;
1291
1292                 while (len_d) {
1293                         len = RTE_MIN(len_s, len_d);
1294                         memcpy(d, s, len);
1295                         s = s + len;
1296                         d = d + len;
1297                         len_d = len_d - len;
1298                         len_s = len_s - len;
1299
1300                         if (len_s == 0) {
1301                                 temp = temp->next;
1302                                 if (temp == NULL)
1303                                         break;
1304                                 s = rte_pktmbuf_mtod(temp, char *);
1305                                 len_s = temp->data_len;
1306                         }
1307                 }
1308
1309                 temp_new->data_len = buf_len;
1310                 temp_new = temp_new->next;
1311         }
1312
1313         /* free original mbufs */
1314         rte_pktmbuf_free(tx_pkt);
1315
1316         *new_pkt = new_mbuf;
1317
1318         return 0;
1319 }
1320
1321 static void
1322 hns3_parse_outer_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec)
1323 {
1324         uint32_t tmp = *ol_type_vlan_len_msec;
1325
1326         /* (outer) IP header type */
1327         if (ol_flags & PKT_TX_OUTER_IPV4) {
1328                 /* OL3 header size, defined in 4 bytes */
1329                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
1330                                sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
1331                 if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
1332                         hns3_set_field(tmp, HNS3_TXD_OL3T_M,
1333                                        HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_CSUM);
1334                 else
1335                         hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
1336                                        HNS3_OL3T_IPV4_NO_CSUM);
1337         } else if (ol_flags & PKT_TX_OUTER_IPV6) {
1338                 hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
1339                                HNS3_OL3T_IPV6);
1340                 /* OL3 header size, defined in 4 bytes */
1341                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
1342                                sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
1343         }
1344
1345         *ol_type_vlan_len_msec = tmp;
1346 }
1347
1348 static int
1349 hns3_parse_inner_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec,
1350                         struct rte_net_hdr_lens *hdr_lens)
1351 {
1352         uint32_t tmp = *ol_type_vlan_len_msec;
1353         uint8_t l4_len;
1354
1355         /* OL2 header size, defined in 2 bytes */
1356         hns3_set_field(tmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
1357                        sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
1358
1359         /* L4TUNT: L4 Tunneling Type */
1360         switch (ol_flags & PKT_TX_TUNNEL_MASK) {
1361         case PKT_TX_TUNNEL_GENEVE:
1362         case PKT_TX_TUNNEL_VXLAN:
1363                 /* MAC in UDP tunnelling packet, include VxLAN */
1364                 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
1365                                HNS3_TUN_MAC_IN_UDP);
1366                 /*
1367                  * OL4 header size, defined in 4 Bytes, it contains outer
1368                  * L4(UDP) length and tunneling length.
1369                  */
1370                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1371                                (uint8_t)RTE_ETHER_VXLAN_HLEN >>
1372                                HNS3_L4_LEN_UNIT);
1373                 break;
1374         case PKT_TX_TUNNEL_GRE:
1375                 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
1376                                HNS3_TUN_NVGRE);
1377                 /*
1378                  * OL4 header size, defined in 4 Bytes, it contains outer
1379                  * L4(GRE) length and tunneling length.
1380                  */
1381                 l4_len = hdr_lens->l4_len + hdr_lens->tunnel_len;
1382                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1383                                l4_len >> HNS3_L4_LEN_UNIT);
1384                 break;
1385         default:
1386                 /* For non UDP / GRE tunneling, drop the tunnel packet */
1387                 return -EINVAL;
1388         }
1389
1390         *ol_type_vlan_len_msec = tmp;
1391
1392         return 0;
1393 }
1394
1395 static int
1396 hns3_parse_tunneling_params(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
1397                             uint64_t ol_flags,
1398                             struct rte_net_hdr_lens *hdr_lens)
1399 {
1400         struct hns3_desc *tx_ring = txq->tx_ring;
1401         struct hns3_desc *desc = &tx_ring[tx_desc_id];
1402         uint32_t value = 0;
1403         int ret;
1404
1405         hns3_parse_outer_params(ol_flags, &value);
1406         ret = hns3_parse_inner_params(ol_flags, &value, hdr_lens);
1407         if (ret)
1408                 return -EINVAL;
1409
1410         desc->tx.ol_type_vlan_len_msec |= rte_cpu_to_le_32(value);
1411
1412         return 0;
1413 }
1414
1415 static void
1416 hns3_parse_l3_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
1417 {
1418         uint32_t tmp;
1419
1420         /* Enable L3 checksum offloads */
1421         if (ol_flags & PKT_TX_IPV4) {
1422                 tmp = *type_cs_vlan_tso_len;
1423                 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
1424                                HNS3_L3T_IPV4);
1425                 /* inner(/normal) L3 header size, defined in 4 bytes */
1426                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
1427                                sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
1428                 if (ol_flags & PKT_TX_IP_CKSUM)
1429                         hns3_set_bit(tmp, HNS3_TXD_L3CS_B, 1);
1430                 *type_cs_vlan_tso_len = tmp;
1431         } else if (ol_flags & PKT_TX_IPV6) {
1432                 tmp = *type_cs_vlan_tso_len;
1433                 /* L3T, IPv6 don't do checksum */
1434                 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
1435                                HNS3_L3T_IPV6);
1436                 /* inner(/normal) L3 header size, defined in 4 bytes */
1437                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
1438                                sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
1439                 *type_cs_vlan_tso_len = tmp;
1440         }
1441 }
1442
1443 static void
1444 hns3_parse_l4_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
1445 {
1446         uint32_t tmp;
1447
1448         /* Enable L4 checksum offloads */
1449         switch (ol_flags & PKT_TX_L4_MASK) {
1450         case PKT_TX_TCP_CKSUM:
1451                 tmp = *type_cs_vlan_tso_len;
1452                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
1453                                HNS3_L4T_TCP);
1454                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
1455                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1456                                sizeof(struct rte_tcp_hdr) >> HNS3_L4_LEN_UNIT);
1457                 *type_cs_vlan_tso_len = tmp;
1458                 break;
1459         case PKT_TX_UDP_CKSUM:
1460                 tmp = *type_cs_vlan_tso_len;
1461                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
1462                                HNS3_L4T_UDP);
1463                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
1464                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1465                                sizeof(struct rte_udp_hdr) >> HNS3_L4_LEN_UNIT);
1466                 *type_cs_vlan_tso_len = tmp;
1467                 break;
1468         case PKT_TX_SCTP_CKSUM:
1469                 tmp = *type_cs_vlan_tso_len;
1470                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
1471                                HNS3_L4T_SCTP);
1472                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
1473                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1474                                sizeof(struct rte_sctp_hdr) >> HNS3_L4_LEN_UNIT);
1475                 *type_cs_vlan_tso_len = tmp;
1476                 break;
1477         default:
1478                 break;
1479         }
1480 }
1481
1482 static void
1483 hns3_txd_enable_checksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
1484                          uint64_t ol_flags)
1485 {
1486         struct hns3_desc *tx_ring = txq->tx_ring;
1487         struct hns3_desc *desc = &tx_ring[tx_desc_id];
1488         uint32_t value = 0;
1489
1490         /* inner(/normal) L2 header size, defined in 2 bytes */
1491         hns3_set_field(value, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
1492                        sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
1493
1494         hns3_parse_l3_cksum_params(ol_flags, &value);
1495         hns3_parse_l4_cksum_params(ol_flags, &value);
1496
1497         desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(value);
1498 }
1499
1500 uint16_t
1501 hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
1502                uint16_t nb_pkts)
1503 {
1504         struct rte_mbuf *m;
1505         uint16_t i;
1506         int ret;
1507
1508         for (i = 0; i < nb_pkts; i++) {
1509                 m = tx_pkts[i];
1510
1511                 /* check the size of packet */
1512                 if (m->pkt_len < HNS3_MIN_FRAME_LEN) {
1513                         rte_errno = EINVAL;
1514                         return i;
1515                 }
1516
1517 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1518                 ret = rte_validate_tx_offload(m);
1519                 if (ret != 0) {
1520                         rte_errno = -ret;
1521                         return i;
1522                 }
1523 #endif
1524                 ret = rte_net_intel_cksum_prepare(m);
1525                 if (ret != 0) {
1526                         rte_errno = -ret;
1527                         return i;
1528                 }
1529         }
1530
1531         return i;
1532 }
1533
1534 static int
1535 hns3_parse_cksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
1536                  const struct rte_mbuf *m, struct rte_net_hdr_lens *hdr_lens)
1537 {
1538         /* Fill in tunneling parameters if necessary */
1539         if (m->ol_flags & PKT_TX_TUNNEL_MASK) {
1540                 (void)rte_net_get_ptype(m, hdr_lens, RTE_PTYPE_ALL_MASK);
1541                 if (hns3_parse_tunneling_params(txq, tx_desc_id, m->ol_flags,
1542                                                 hdr_lens))
1543                         return -EINVAL;
1544         }
1545         /* Enable checksum offloading */
1546         if (m->ol_flags & HNS3_TX_CKSUM_OFFLOAD_MASK)
1547                 hns3_txd_enable_checksum(txq, tx_desc_id, m->ol_flags);
1548
1549         return 0;
1550 }
1551
1552 uint16_t
1553 hns3_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1554 {
1555         struct rte_net_hdr_lens hdr_lens = {0};
1556         struct hns3_tx_queue *txq = tx_queue;
1557         struct hns3_entry *tx_bak_pkt;
1558         struct rte_mbuf *new_pkt;
1559         struct rte_mbuf *tx_pkt;
1560         struct rte_mbuf *m_seg;
1561         struct rte_mbuf *temp;
1562         uint32_t nb_hold = 0;
1563         uint16_t tx_next_clean;
1564         uint16_t tx_next_use;
1565         uint16_t tx_bd_ready;
1566         uint16_t tx_pkt_num;
1567         uint16_t tx_bd_max;
1568         uint16_t nb_buf;
1569         uint16_t nb_tx;
1570         uint16_t i;
1571
1572         /* free useless buffer */
1573         hns3_tx_free_useless_buffer(txq);
1574         tx_bd_ready = txq->tx_bd_ready;
1575         if (tx_bd_ready == 0)
1576                 return 0;
1577
1578         tx_next_clean = txq->next_to_clean;
1579         tx_next_use   = txq->next_to_use;
1580         tx_bd_max     = txq->nb_tx_desc;
1581         tx_bak_pkt = &txq->sw_ring[tx_next_clean];
1582
1583         tx_pkt_num = (tx_bd_ready < nb_pkts) ? tx_bd_ready : nb_pkts;
1584
1585         /* send packets */
1586         tx_bak_pkt = &txq->sw_ring[tx_next_use];
1587         for (nb_tx = 0; nb_tx < tx_pkt_num; nb_tx++) {
1588                 tx_pkt = *tx_pkts++;
1589
1590                 nb_buf = tx_pkt->nb_segs;
1591
1592                 if (nb_buf > tx_ring_space(txq)) {
1593                         if (nb_tx == 0)
1594                                 return 0;
1595
1596                         goto end_of_tx;
1597                 }
1598
1599                 /*
1600                  * If the length of the packet is too long or zero, the packet
1601                  * will be ignored.
1602                  */
1603                 if (unlikely(tx_pkt->pkt_len > HNS3_MAX_FRAME_LEN ||
1604                              tx_pkt->pkt_len == 0))
1605                         break;
1606
1607                 m_seg = tx_pkt;
1608                 if (unlikely(nb_buf > HNS3_MAX_TX_BD_PER_PKT)) {
1609                         if (hns3_reassemble_tx_pkts(txq, tx_pkt, &new_pkt))
1610                                 goto end_of_tx;
1611                         m_seg = new_pkt;
1612                         nb_buf = m_seg->nb_segs;
1613                 }
1614
1615                 if (hns3_parse_cksum(txq, tx_next_use, m_seg, &hdr_lens))
1616                         goto end_of_tx;
1617
1618                 i = 0;
1619                 do {
1620                         fill_desc(txq, tx_next_use, m_seg, (i == 0), 0);
1621                         temp = m_seg->next;
1622                         tx_bak_pkt->mbuf = m_seg;
1623                         m_seg = temp;
1624                         tx_next_use++;
1625                         tx_bak_pkt++;
1626                         if (tx_next_use >= tx_bd_max) {
1627                                 tx_next_use = 0;
1628                                 tx_bak_pkt = txq->sw_ring;
1629                         }
1630
1631                         i++;
1632                 } while (m_seg != NULL);
1633
1634                 nb_hold += i;
1635         }
1636
1637 end_of_tx:
1638
1639         if (likely(nb_tx)) {
1640                 hns3_queue_xmit(txq, nb_hold);
1641                 txq->next_to_clean = tx_next_clean;
1642                 txq->next_to_use   = tx_next_use;
1643                 txq->tx_bd_ready   = tx_bd_ready - nb_hold;
1644         }
1645
1646         return nb_tx;
1647 }
1648
1649 static uint16_t
1650 hns3_dummy_rxtx_burst(void *dpdk_txq __rte_unused,
1651                       struct rte_mbuf **pkts __rte_unused,
1652                       uint16_t pkts_n __rte_unused)
1653 {
1654         return 0;
1655 }
1656
1657 void hns3_set_rxtx_function(struct rte_eth_dev *eth_dev)
1658 {
1659         struct hns3_adapter *hns = eth_dev->data->dev_private;
1660
1661         if (hns->hw.adapter_state == HNS3_NIC_STARTED &&
1662             rte_atomic16_read(&hns->hw.reset.resetting) == 0) {
1663                 eth_dev->rx_pkt_burst = hns3_recv_pkts;
1664                 eth_dev->tx_pkt_burst = hns3_xmit_pkts;
1665                 eth_dev->tx_pkt_prepare = hns3_prep_pkts;
1666         } else {
1667                 eth_dev->rx_pkt_burst = hns3_dummy_rxtx_burst;
1668                 eth_dev->tx_pkt_burst = hns3_dummy_rxtx_burst;
1669                 eth_dev->tx_pkt_prepare = hns3_dummy_rxtx_burst;
1670         }
1671 }