net/hns3: support Rx interrupt
[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 void
399 hns3_tqp_intr_enable(struct hns3_hw *hw, uint16_t tpq_int_num, bool en)
400 {
401         uint32_t addr, value;
402
403         addr = HNS3_TQP_INTR_CTRL_REG + tpq_int_num * HNS3_VECTOR_REG_OFFSET;
404         value = en ? 1 : 0;
405
406         hns3_write_dev(hw, addr, value);
407 }
408
409 int
410 hns3_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
411 {
412         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
413         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
414         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
415
416         if (dev->data->dev_conf.intr_conf.rxq == 0)
417                 return -ENOTSUP;
418
419         /* enable the vectors */
420         hns3_tqp_intr_enable(hw, queue_id, true);
421
422         return rte_intr_ack(intr_handle);
423 }
424
425 int
426 hns3_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
427 {
428         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
429
430         if (dev->data->dev_conf.intr_conf.rxq == 0)
431                 return -ENOTSUP;
432
433         /* disable the vectors */
434         hns3_tqp_intr_enable(hw, queue_id, false);
435
436         return 0;
437 }
438
439 static int
440 hns3_dev_rx_queue_start(struct hns3_adapter *hns, uint16_t idx)
441 {
442         struct hns3_hw *hw = &hns->hw;
443         struct hns3_rx_queue *rxq;
444         int ret;
445
446         PMD_INIT_FUNC_TRACE();
447
448         rxq = hw->data->rx_queues[idx];
449
450         ret = hns3_alloc_rx_queue_mbufs(hw, rxq);
451         if (ret) {
452                 hns3_err(hw, "Failed to alloc mbuf for No.%d rx queue: %d",
453                             idx, ret);
454                 return ret;
455         }
456
457         rxq->next_to_use = 0;
458         rxq->next_to_clean = 0;
459         hns3_init_rx_queue_hw(rxq);
460
461         return 0;
462 }
463
464 static void
465 hns3_dev_tx_queue_start(struct hns3_adapter *hns, uint16_t idx)
466 {
467         struct hns3_hw *hw = &hns->hw;
468         struct hns3_tx_queue *txq;
469         struct hns3_desc *desc;
470         int i;
471
472         txq = hw->data->tx_queues[idx];
473
474         /* Clear tx bd */
475         desc = txq->tx_ring;
476         for (i = 0; i < txq->nb_tx_desc; i++) {
477                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
478                 desc++;
479         }
480
481         txq->next_to_use = 0;
482         txq->next_to_clean = 0;
483         txq->tx_bd_ready   = txq->nb_tx_desc;
484         hns3_init_tx_queue_hw(txq);
485 }
486
487 static void
488 hns3_init_tx_ring_tc(struct hns3_adapter *hns)
489 {
490         struct hns3_hw *hw = &hns->hw;
491         struct hns3_tx_queue *txq;
492         int i, num;
493
494         for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
495                 struct hns3_tc_queue_info *tc_queue = &hw->tc_queue[i];
496                 int j;
497
498                 if (!tc_queue->enable)
499                         continue;
500
501                 for (j = 0; j < tc_queue->tqp_count; j++) {
502                         num = tc_queue->tqp_offset + j;
503                         txq = hw->data->tx_queues[num];
504                         if (txq == NULL)
505                                 continue;
506
507                         hns3_write_dev(txq, HNS3_RING_TX_TC_REG, tc_queue->tc);
508                 }
509         }
510 }
511
512 int
513 hns3_start_queues(struct hns3_adapter *hns, bool reset_queue)
514 {
515         struct hns3_hw *hw = &hns->hw;
516         struct rte_eth_dev_data *dev_data = hw->data;
517         struct hns3_rx_queue *rxq;
518         struct hns3_tx_queue *txq;
519         int ret;
520         int i;
521         int j;
522
523         /* Initialize RSS for queues */
524         ret = hns3_config_rss(hns);
525         if (ret) {
526                 hns3_err(hw, "Failed to configure rss %d", ret);
527                 return ret;
528         }
529
530         if (reset_queue) {
531                 ret = hns3_reset_all_queues(hns);
532                 if (ret) {
533                         hns3_err(hw, "Failed to reset all queues %d", ret);
534                         return ret;
535                 }
536         }
537
538         /*
539          * Hardware does not support where the number of rx and tx queues is
540          * not equal in hip08. In .dev_configure callback function we will
541          * check the two values, here we think that the number of rx and tx
542          * queues is equal.
543          */
544         for (i = 0; i < hw->data->nb_rx_queues; i++) {
545                 rxq = dev_data->rx_queues[i];
546                 txq = dev_data->tx_queues[i];
547                 if (rxq == NULL || txq == NULL || rxq->rx_deferred_start ||
548                     txq->tx_deferred_start)
549                         continue;
550
551                 ret = hns3_dev_rx_queue_start(hns, i);
552                 if (ret) {
553                         hns3_err(hw, "Failed to start No.%d rx queue: %d", i,
554                                  ret);
555                         goto out;
556                 }
557                 hns3_dev_tx_queue_start(hns, i);
558         }
559         hns3_init_tx_ring_tc(hns);
560
561         hns3_enable_all_queues(hw, true);
562         return 0;
563
564 out:
565         for (j = 0; j < i; j++) {
566                 rxq = dev_data->rx_queues[j];
567                 hns3_rx_queue_release_mbufs(rxq);
568         }
569
570         return ret;
571 }
572
573 int
574 hns3_stop_queues(struct hns3_adapter *hns, bool reset_queue)
575 {
576         struct hns3_hw *hw = &hns->hw;
577         int ret;
578
579         hns3_enable_all_queues(hw, false);
580         if (reset_queue) {
581                 ret = hns3_reset_all_queues(hns);
582                 if (ret) {
583                         hns3_err(hw, "Failed to reset all queues %d", ret);
584                         return ret;
585                 }
586         }
587         return 0;
588 }
589
590 void
591 hns3_dev_release_mbufs(struct hns3_adapter *hns)
592 {
593         struct rte_eth_dev_data *dev_data = hns->hw.data;
594         struct hns3_rx_queue *rxq;
595         struct hns3_tx_queue *txq;
596         int i;
597
598         if (dev_data->rx_queues)
599                 for (i = 0; i < dev_data->nb_rx_queues; i++) {
600                         rxq = dev_data->rx_queues[i];
601                         if (rxq == NULL || rxq->rx_deferred_start)
602                                 continue;
603                         hns3_rx_queue_release_mbufs(rxq);
604                 }
605
606         if (dev_data->tx_queues)
607                 for (i = 0; i < dev_data->nb_tx_queues; i++) {
608                         txq = dev_data->tx_queues[i];
609                         if (txq == NULL || txq->tx_deferred_start)
610                                 continue;
611                         hns3_tx_queue_release_mbufs(txq);
612                 }
613 }
614
615 int
616 hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
617                     unsigned int socket_id, const struct rte_eth_rxconf *conf,
618                     struct rte_mempool *mp)
619 {
620         struct hns3_adapter *hns = dev->data->dev_private;
621         const struct rte_memzone *rx_mz;
622         struct hns3_hw *hw = &hns->hw;
623         struct hns3_rx_queue *rxq;
624         unsigned int desc_size = sizeof(struct hns3_desc);
625         unsigned int rx_desc;
626         int rx_entry_len;
627
628         if (dev->data->dev_started) {
629                 hns3_err(hw, "rx_queue_setup after dev_start no supported");
630                 return -EINVAL;
631         }
632
633         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
634             nb_desc % HNS3_ALIGN_RING_DESC) {
635                 hns3_err(hw, "Number (%u) of rx descriptors is invalid",
636                          nb_desc);
637                 return -EINVAL;
638         }
639
640         if (dev->data->rx_queues[idx]) {
641                 hns3_rx_queue_release(dev->data->rx_queues[idx]);
642                 dev->data->rx_queues[idx] = NULL;
643         }
644
645         rxq = rte_zmalloc_socket("hns3 RX queue", sizeof(struct hns3_rx_queue),
646                                  RTE_CACHE_LINE_SIZE, socket_id);
647         if (rxq == NULL) {
648                 hns3_err(hw, "Failed to allocate memory for rx queue!");
649                 return -ENOMEM;
650         }
651
652         rxq->hns = hns;
653         rxq->mb_pool = mp;
654         rxq->nb_rx_desc = nb_desc;
655         rxq->queue_id = idx;
656         if (conf->rx_free_thresh <= 0)
657                 rxq->rx_free_thresh = DEFAULT_RX_FREE_THRESH;
658         else
659                 rxq->rx_free_thresh = conf->rx_free_thresh;
660         rxq->rx_deferred_start = conf->rx_deferred_start;
661
662         rx_entry_len = sizeof(struct hns3_entry) * rxq->nb_rx_desc;
663         rxq->sw_ring = rte_zmalloc_socket("hns3 RX sw ring", rx_entry_len,
664                                           RTE_CACHE_LINE_SIZE, socket_id);
665         if (rxq->sw_ring == NULL) {
666                 hns3_err(hw, "Failed to allocate memory for rx sw ring!");
667                 hns3_rx_queue_release(rxq);
668                 return -ENOMEM;
669         }
670
671         /* Allocate rx ring hardware descriptors. */
672         rx_desc = rxq->nb_rx_desc * desc_size;
673         rx_mz = rte_eth_dma_zone_reserve(dev, "rx_ring", idx, rx_desc,
674                                          HNS3_RING_BASE_ALIGN, socket_id);
675         if (rx_mz == NULL) {
676                 hns3_err(hw, "Failed to reserve DMA memory for No.%d rx ring!",
677                          idx);
678                 hns3_rx_queue_release(rxq);
679                 return -ENOMEM;
680         }
681         rxq->mz = rx_mz;
682         rxq->rx_ring = (struct hns3_desc *)rx_mz->addr;
683         rxq->rx_ring_phys_addr = rx_mz->iova;
684
685         hns3_dbg(hw, "No.%d rx descriptors iova 0x%" PRIx64, idx,
686                  rxq->rx_ring_phys_addr);
687
688         rxq->next_to_use = 0;
689         rxq->next_to_clean = 0;
690         rxq->nb_rx_hold = 0;
691         rxq->pkt_first_seg = NULL;
692         rxq->pkt_last_seg = NULL;
693         rxq->port_id = dev->data->port_id;
694         rxq->configured = true;
695         rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
696                                 idx * HNS3_TQP_REG_SIZE);
697         rxq->rx_buf_len = hw->rx_buf_len;
698         rxq->non_vld_descs = 0;
699         rxq->l2_errors = 0;
700         rxq->pkt_len_errors = 0;
701         rxq->l3_csum_erros = 0;
702         rxq->l4_csum_erros = 0;
703         rxq->ol3_csum_erros = 0;
704         rxq->ol4_csum_erros = 0;
705
706         rte_spinlock_lock(&hw->lock);
707         dev->data->rx_queues[idx] = rxq;
708         rte_spinlock_unlock(&hw->lock);
709
710         return 0;
711 }
712
713 static inline uint32_t
714 rxd_pkt_info_to_pkt_type(uint32_t pkt_info, uint32_t ol_info)
715 {
716 #define HNS3_L2TBL_NUM  4
717 #define HNS3_L3TBL_NUM  16
718 #define HNS3_L4TBL_NUM  16
719 #define HNS3_OL3TBL_NUM 16
720 #define HNS3_OL4TBL_NUM 16
721         uint32_t pkt_type = 0;
722         uint32_t l2id, l3id, l4id;
723         uint32_t ol3id, ol4id;
724
725         static const uint32_t l2table[HNS3_L2TBL_NUM] = {
726                 RTE_PTYPE_L2_ETHER,
727                 RTE_PTYPE_L2_ETHER_VLAN,
728                 RTE_PTYPE_L2_ETHER_QINQ,
729                 0
730         };
731
732         static const uint32_t l3table[HNS3_L3TBL_NUM] = {
733                 RTE_PTYPE_L3_IPV4,
734                 RTE_PTYPE_L3_IPV6,
735                 RTE_PTYPE_L2_ETHER_ARP,
736                 RTE_PTYPE_L2_ETHER,
737                 RTE_PTYPE_L3_IPV4_EXT,
738                 RTE_PTYPE_L3_IPV6_EXT,
739                 RTE_PTYPE_L2_ETHER_LLDP,
740                 0, 0, 0, 0, 0, 0, 0, 0, 0
741         };
742
743         static const uint32_t l4table[HNS3_L4TBL_NUM] = {
744                 RTE_PTYPE_L4_UDP,
745                 RTE_PTYPE_L4_TCP,
746                 RTE_PTYPE_TUNNEL_GRE,
747                 RTE_PTYPE_L4_SCTP,
748                 RTE_PTYPE_L4_IGMP,
749                 RTE_PTYPE_L4_ICMP,
750                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
751         };
752
753         static const uint32_t inner_l2table[HNS3_L2TBL_NUM] = {
754                 RTE_PTYPE_INNER_L2_ETHER,
755                 RTE_PTYPE_INNER_L2_ETHER_VLAN,
756                 RTE_PTYPE_INNER_L2_ETHER_QINQ,
757                 0
758         };
759
760         static const uint32_t inner_l3table[HNS3_L3TBL_NUM] = {
761                 RTE_PTYPE_INNER_L3_IPV4,
762                 RTE_PTYPE_INNER_L3_IPV6,
763                 0,
764                 RTE_PTYPE_INNER_L2_ETHER,
765                 RTE_PTYPE_INNER_L3_IPV4_EXT,
766                 RTE_PTYPE_INNER_L3_IPV6_EXT,
767                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
768         };
769
770         static const uint32_t inner_l4table[HNS3_L4TBL_NUM] = {
771                 RTE_PTYPE_INNER_L4_UDP,
772                 RTE_PTYPE_INNER_L4_TCP,
773                 RTE_PTYPE_TUNNEL_GRE,
774                 RTE_PTYPE_INNER_L4_SCTP,
775                 RTE_PTYPE_L4_IGMP,
776                 RTE_PTYPE_INNER_L4_ICMP,
777                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
778         };
779
780         static const uint32_t ol3table[HNS3_OL3TBL_NUM] = {
781                 RTE_PTYPE_L3_IPV4,
782                 RTE_PTYPE_L3_IPV6,
783                 0, 0,
784                 RTE_PTYPE_L3_IPV4_EXT,
785                 RTE_PTYPE_L3_IPV6_EXT,
786                 0, 0, 0, 0, 0, 0, 0, 0, 0,
787                 RTE_PTYPE_UNKNOWN
788         };
789
790         static const uint32_t ol4table[HNS3_OL4TBL_NUM] = {
791                 0,
792                 RTE_PTYPE_TUNNEL_VXLAN,
793                 RTE_PTYPE_TUNNEL_NVGRE,
794                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
795         };
796
797         l2id = hns3_get_field(pkt_info, HNS3_RXD_STRP_TAGP_M,
798                               HNS3_RXD_STRP_TAGP_S);
799         l3id = hns3_get_field(pkt_info, HNS3_RXD_L3ID_M, HNS3_RXD_L3ID_S);
800         l4id = hns3_get_field(pkt_info, HNS3_RXD_L4ID_M, HNS3_RXD_L4ID_S);
801         ol3id = hns3_get_field(ol_info, HNS3_RXD_OL3ID_M, HNS3_RXD_OL3ID_S);
802         ol4id = hns3_get_field(ol_info, HNS3_RXD_OL4ID_M, HNS3_RXD_OL4ID_S);
803
804         if (ol4table[ol4id])
805                 pkt_type |= (inner_l2table[l2id] | inner_l3table[l3id] |
806                              inner_l4table[l4id] | ol3table[ol3id] |
807                              ol4table[ol4id]);
808         else
809                 pkt_type |= (l2table[l2id] | l3table[l3id] | l4table[l4id]);
810         return pkt_type;
811 }
812
813 const uint32_t *
814 hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev)
815 {
816         static const uint32_t ptypes[] = {
817                 RTE_PTYPE_L2_ETHER,
818                 RTE_PTYPE_L2_ETHER_VLAN,
819                 RTE_PTYPE_L2_ETHER_QINQ,
820                 RTE_PTYPE_L2_ETHER_LLDP,
821                 RTE_PTYPE_L2_ETHER_ARP,
822                 RTE_PTYPE_L3_IPV4,
823                 RTE_PTYPE_L3_IPV4_EXT,
824                 RTE_PTYPE_L3_IPV6,
825                 RTE_PTYPE_L3_IPV6_EXT,
826                 RTE_PTYPE_L4_IGMP,
827                 RTE_PTYPE_L4_ICMP,
828                 RTE_PTYPE_L4_SCTP,
829                 RTE_PTYPE_L4_TCP,
830                 RTE_PTYPE_L4_UDP,
831                 RTE_PTYPE_TUNNEL_GRE,
832                 RTE_PTYPE_UNKNOWN
833         };
834
835         if (dev->rx_pkt_burst == hns3_recv_pkts)
836                 return ptypes;
837
838         return NULL;
839 }
840
841 static void
842 hns3_clean_rx_buffers(struct hns3_rx_queue *rxq, int count)
843 {
844         rxq->next_to_use += count;
845         if (rxq->next_to_use >= rxq->nb_rx_desc)
846                 rxq->next_to_use -= rxq->nb_rx_desc;
847
848         hns3_write_dev(rxq, HNS3_RING_RX_HEAD_REG, count);
849 }
850
851 static int
852 hns3_handle_bdinfo(struct hns3_rx_queue *rxq, struct rte_mbuf *rxm,
853                    uint32_t bd_base_info, uint32_t l234_info,
854                    uint32_t *cksum_err)
855 {
856         uint32_t tmp = 0;
857
858         if (unlikely(l234_info & BIT(HNS3_RXD_L2E_B))) {
859                 rxq->l2_errors++;
860                 return -EINVAL;
861         }
862
863         if (unlikely(rxm->pkt_len == 0 ||
864                 (l234_info & BIT(HNS3_RXD_TRUNCAT_B)))) {
865                 rxq->pkt_len_errors++;
866                 return -EINVAL;
867         }
868
869         if (bd_base_info & BIT(HNS3_RXD_L3L4P_B)) {
870                 if (unlikely(l234_info & BIT(HNS3_RXD_L3E_B))) {
871                         rxm->ol_flags |= PKT_RX_IP_CKSUM_BAD;
872                         rxq->l3_csum_erros++;
873                         tmp |= HNS3_L3_CKSUM_ERR;
874                 }
875
876                 if (unlikely(l234_info & BIT(HNS3_RXD_L4E_B))) {
877                         rxm->ol_flags |= PKT_RX_L4_CKSUM_BAD;
878                         rxq->l4_csum_erros++;
879                         tmp |= HNS3_L4_CKSUM_ERR;
880                 }
881
882                 if (unlikely(l234_info & BIT(HNS3_RXD_OL3E_B))) {
883                         rxq->ol3_csum_erros++;
884                         tmp |= HNS3_OUTER_L3_CKSUM_ERR;
885                 }
886
887                 if (unlikely(l234_info & BIT(HNS3_RXD_OL4E_B))) {
888                         rxm->ol_flags |= PKT_RX_OUTER_L4_CKSUM_BAD;
889                         rxq->ol4_csum_erros++;
890                         tmp |= HNS3_OUTER_L4_CKSUM_ERR;
891                 }
892         }
893         *cksum_err = tmp;
894
895         return 0;
896 }
897
898 static void
899 hns3_rx_set_cksum_flag(struct rte_mbuf *rxm, uint64_t packet_type,
900                        const uint32_t cksum_err)
901 {
902         if (unlikely((packet_type & RTE_PTYPE_TUNNEL_MASK))) {
903                 if (likely(packet_type & RTE_PTYPE_INNER_L3_MASK) &&
904                     (cksum_err & HNS3_L3_CKSUM_ERR) == 0)
905                         rxm->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
906                 if (likely(packet_type & RTE_PTYPE_INNER_L4_MASK) &&
907                     (cksum_err & HNS3_L4_CKSUM_ERR) == 0)
908                         rxm->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
909                 if (likely(packet_type & RTE_PTYPE_L4_MASK) &&
910                     (cksum_err & HNS3_OUTER_L4_CKSUM_ERR) == 0)
911                         rxm->ol_flags |= PKT_RX_OUTER_L4_CKSUM_GOOD;
912         } else {
913                 if (likely(packet_type & RTE_PTYPE_L3_MASK) &&
914                     (cksum_err & HNS3_L3_CKSUM_ERR) == 0)
915                         rxm->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
916                 if (likely(packet_type & RTE_PTYPE_L4_MASK) &&
917                     (cksum_err & HNS3_L4_CKSUM_ERR) == 0)
918                         rxm->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
919         }
920 }
921
922 uint16_t
923 hns3_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
924 {
925         struct hns3_rx_queue *rxq;      /* RX queue */
926         struct hns3_desc *rx_ring;      /* RX ring (desc) */
927         struct hns3_entry *sw_ring;
928         struct hns3_entry *rxe;
929         struct hns3_desc *rxdp;         /* pointer of the current desc */
930         struct rte_mbuf *first_seg;
931         struct rte_mbuf *last_seg;
932         struct rte_mbuf *nmb;           /* pointer of the new mbuf */
933         struct rte_mbuf *rxm;
934         struct rte_eth_dev *dev;
935         uint32_t bd_base_info;
936         uint32_t cksum_err;
937         uint32_t l234_info;
938         uint32_t ol_info;
939         uint64_t dma_addr;
940         uint16_t data_len;
941         uint16_t nb_rx_bd;
942         uint16_t pkt_len;
943         uint16_t nb_rx;
944         uint16_t rx_id;
945         int num;                        /* num of desc in ring */
946         int ret;
947
948         nb_rx = 0;
949         nb_rx_bd = 0;
950         rxq = rx_queue;
951         dev = &rte_eth_devices[rxq->port_id];
952
953         rx_id = rxq->next_to_clean;
954         rx_ring = rxq->rx_ring;
955         first_seg = rxq->pkt_first_seg;
956         last_seg = rxq->pkt_last_seg;
957         sw_ring = rxq->sw_ring;
958
959         /* Get num of packets in descriptor ring */
960         num = hns3_read_dev(rxq, HNS3_RING_RX_FBDNUM_REG);
961         while (nb_rx_bd < num && nb_rx < nb_pkts) {
962                 rxdp = &rx_ring[rx_id];
963                 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
964                 if (unlikely(!hns3_get_bit(bd_base_info, HNS3_RXD_VLD_B))) {
965                         rxq->non_vld_descs++;
966                         break;
967                 }
968
969                 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
970                 if (unlikely(nmb == NULL)) {
971                         dev->data->rx_mbuf_alloc_failed++;
972                         break;
973                 }
974
975                 nb_rx_bd++;
976                 rxe = &sw_ring[rx_id];
977                 rx_id++;
978                 if (rx_id == rxq->nb_rx_desc)
979                         rx_id = 0;
980
981                 rte_prefetch0(sw_ring[rx_id].mbuf);
982                 if ((rx_id & 0x3) == 0) {
983                         rte_prefetch0(&rx_ring[rx_id]);
984                         rte_prefetch0(&sw_ring[rx_id]);
985                 }
986
987                 rxm = rxe->mbuf;
988                 rxe->mbuf = nmb;
989
990                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
991                 rxdp->addr = dma_addr;
992                 rxdp->rx.bd_base_info = 0;
993
994                 rte_cio_rmb();
995                 /* Load remained descriptor data and extract necessary fields */
996                 data_len = (uint16_t)(rte_le_to_cpu_16(rxdp->rx.size));
997                 l234_info = rte_le_to_cpu_32(rxdp->rx.l234_info);
998                 ol_info = rte_le_to_cpu_32(rxdp->rx.ol_info);
999
1000                 if (first_seg == NULL) {
1001                         first_seg = rxm;
1002                         first_seg->nb_segs = 1;
1003                 } else {
1004                         first_seg->nb_segs++;
1005                         last_seg->next = rxm;
1006                 }
1007
1008                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1009                 rxm->data_len = data_len;
1010
1011                 if (!hns3_get_bit(bd_base_info, HNS3_RXD_FE_B)) {
1012                         last_seg = rxm;
1013                         continue;
1014                 }
1015
1016                 /* The last buffer of the received packet */
1017                 pkt_len = (uint16_t)(rte_le_to_cpu_16(rxdp->rx.pkt_len));
1018                 first_seg->pkt_len = pkt_len;
1019                 first_seg->port = rxq->port_id;
1020                 first_seg->hash.rss = rte_le_to_cpu_32(rxdp->rx.rss_hash);
1021                 first_seg->ol_flags |= PKT_RX_RSS_HASH;
1022                 if (unlikely(hns3_get_bit(bd_base_info, HNS3_RXD_LUM_B))) {
1023                         first_seg->hash.fdir.hi =
1024                                 rte_le_to_cpu_32(rxdp->rx.fd_id);
1025                         first_seg->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
1026                 }
1027                 rxm->next = NULL;
1028
1029                 ret = hns3_handle_bdinfo(rxq, first_seg, bd_base_info,
1030                                          l234_info, &cksum_err);
1031                 if (unlikely(ret))
1032                         goto pkt_err;
1033
1034                 first_seg->packet_type = rxd_pkt_info_to_pkt_type(l234_info,
1035                                                                   ol_info);
1036
1037                 if (bd_base_info & BIT(HNS3_RXD_L3L4P_B))
1038                         hns3_rx_set_cksum_flag(rxm, first_seg->packet_type,
1039                                                cksum_err);
1040
1041                 first_seg->vlan_tci = rte_le_to_cpu_16(rxdp->rx.vlan_tag);
1042                 first_seg->vlan_tci_outer =
1043                         rte_le_to_cpu_16(rxdp->rx.ot_vlan_tag);
1044                 rx_pkts[nb_rx++] = first_seg;
1045                 first_seg = NULL;
1046                 continue;
1047 pkt_err:
1048                 rte_pktmbuf_free(first_seg);
1049                 first_seg = NULL;
1050         }
1051
1052         rxq->next_to_clean = rx_id;
1053         rxq->pkt_first_seg = first_seg;
1054         rxq->pkt_last_seg = last_seg;
1055         hns3_clean_rx_buffers(rxq, nb_rx_bd);
1056
1057         return nb_rx;
1058 }
1059
1060 int
1061 hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
1062                     unsigned int socket_id, const struct rte_eth_txconf *conf)
1063 {
1064         struct hns3_adapter *hns = dev->data->dev_private;
1065         const struct rte_memzone *tx_mz;
1066         struct hns3_hw *hw = &hns->hw;
1067         struct hns3_tx_queue *txq;
1068         struct hns3_desc *desc;
1069         unsigned int desc_size = sizeof(struct hns3_desc);
1070         unsigned int tx_desc;
1071         int tx_entry_len;
1072         int i;
1073
1074         if (dev->data->dev_started) {
1075                 hns3_err(hw, "tx_queue_setup after dev_start no supported");
1076                 return -EINVAL;
1077         }
1078
1079         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
1080             nb_desc % HNS3_ALIGN_RING_DESC) {
1081                 hns3_err(hw, "Number (%u) of tx descriptors is invalid",
1082                             nb_desc);
1083                 return -EINVAL;
1084         }
1085
1086         if (dev->data->tx_queues[idx] != NULL) {
1087                 hns3_tx_queue_release(dev->data->tx_queues[idx]);
1088                 dev->data->tx_queues[idx] = NULL;
1089         }
1090
1091         txq = rte_zmalloc_socket("hns3 TX queue", sizeof(struct hns3_tx_queue),
1092                                  RTE_CACHE_LINE_SIZE, socket_id);
1093         if (txq == NULL) {
1094                 hns3_err(hw, "Failed to allocate memory for tx queue!");
1095                 return -ENOMEM;
1096         }
1097
1098         txq->nb_tx_desc = nb_desc;
1099         txq->queue_id = idx;
1100         txq->tx_deferred_start = conf->tx_deferred_start;
1101
1102         tx_entry_len = sizeof(struct hns3_entry) * txq->nb_tx_desc;
1103         txq->sw_ring = rte_zmalloc_socket("hns3 TX sw ring", tx_entry_len,
1104                                           RTE_CACHE_LINE_SIZE, socket_id);
1105         if (txq->sw_ring == NULL) {
1106                 hns3_err(hw, "Failed to allocate memory for tx sw ring!");
1107                 hns3_tx_queue_release(txq);
1108                 return -ENOMEM;
1109         }
1110
1111         /* Allocate tx ring hardware descriptors. */
1112         tx_desc = txq->nb_tx_desc * desc_size;
1113         tx_mz = rte_eth_dma_zone_reserve(dev, "tx_ring", idx, tx_desc,
1114                                          HNS3_RING_BASE_ALIGN, socket_id);
1115         if (tx_mz == NULL) {
1116                 hns3_err(hw, "Failed to reserve DMA memory for No.%d tx ring!",
1117                          idx);
1118                 hns3_tx_queue_release(txq);
1119                 return -ENOMEM;
1120         }
1121         txq->mz = tx_mz;
1122         txq->tx_ring = (struct hns3_desc *)tx_mz->addr;
1123         txq->tx_ring_phys_addr = tx_mz->iova;
1124
1125         hns3_dbg(hw, "No.%d tx descriptors iova 0x%" PRIx64, idx,
1126                  txq->tx_ring_phys_addr);
1127
1128         /* Clear tx bd */
1129         desc = txq->tx_ring;
1130         for (i = 0; i < txq->nb_tx_desc; i++) {
1131                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
1132                 desc++;
1133         }
1134
1135         txq->hns = hns;
1136         txq->next_to_use = 0;
1137         txq->next_to_clean = 0;
1138         txq->tx_bd_ready   = txq->nb_tx_desc;
1139         txq->port_id = dev->data->port_id;
1140         txq->configured = true;
1141         txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1142                                 idx * HNS3_TQP_REG_SIZE);
1143         rte_spinlock_lock(&hw->lock);
1144         dev->data->tx_queues[idx] = txq;
1145         rte_spinlock_unlock(&hw->lock);
1146
1147         return 0;
1148 }
1149
1150 static inline int
1151 tx_ring_dist(struct hns3_tx_queue *txq, int begin, int end)
1152 {
1153         return (end - begin + txq->nb_tx_desc) % txq->nb_tx_desc;
1154 }
1155
1156 static inline int
1157 tx_ring_space(struct hns3_tx_queue *txq)
1158 {
1159         return txq->nb_tx_desc -
1160                 tx_ring_dist(txq, txq->next_to_clean, txq->next_to_use) - 1;
1161 }
1162
1163 static inline void
1164 hns3_queue_xmit(struct hns3_tx_queue *txq, uint32_t buf_num)
1165 {
1166         hns3_write_dev(txq, HNS3_RING_TX_TAIL_REG, buf_num);
1167 }
1168
1169 static void
1170 hns3_tx_free_useless_buffer(struct hns3_tx_queue *txq)
1171 {
1172         uint16_t tx_next_clean = txq->next_to_clean;
1173         uint16_t tx_next_use   = txq->next_to_use;
1174         uint16_t tx_bd_ready   = txq->tx_bd_ready;
1175         uint16_t tx_bd_max     = txq->nb_tx_desc;
1176         struct hns3_entry *tx_bak_pkt = &txq->sw_ring[tx_next_clean];
1177         struct hns3_desc *desc = &txq->tx_ring[tx_next_clean];
1178         struct rte_mbuf *mbuf;
1179
1180         while ((!hns3_get_bit(desc->tx.tp_fe_sc_vld_ra_ri, HNS3_TXD_VLD_B)) &&
1181                 (tx_next_use != tx_next_clean || tx_bd_ready < tx_bd_max)) {
1182                 mbuf = tx_bak_pkt->mbuf;
1183                 if (mbuf) {
1184                         mbuf->next = NULL;
1185                         rte_pktmbuf_free(mbuf);
1186                         tx_bak_pkt->mbuf = NULL;
1187                 }
1188
1189                 desc++;
1190                 tx_bak_pkt++;
1191                 tx_next_clean++;
1192                 tx_bd_ready++;
1193
1194                 if (tx_next_clean >= tx_bd_max) {
1195                         tx_next_clean = 0;
1196                         desc = txq->tx_ring;
1197                         tx_bak_pkt = txq->sw_ring;
1198                 }
1199         }
1200
1201         txq->next_to_clean = tx_next_clean;
1202         txq->tx_bd_ready   = tx_bd_ready;
1203 }
1204
1205 static void
1206 fill_desc(struct hns3_tx_queue *txq, uint16_t tx_desc_id, struct rte_mbuf *rxm,
1207           bool first, int offset)
1208 {
1209         struct hns3_desc *tx_ring = txq->tx_ring;
1210         struct hns3_desc *desc = &tx_ring[tx_desc_id];
1211         uint8_t frag_end = rxm->next == NULL ? 1 : 0;
1212         uint16_t size = rxm->data_len;
1213         uint16_t rrcfv = 0;
1214         uint64_t ol_flags = rxm->ol_flags;
1215         uint32_t hdr_len;
1216         uint32_t paylen;
1217         uint32_t tmp;
1218
1219         desc->addr = rte_mbuf_data_iova(rxm) + offset;
1220         desc->tx.send_size = rte_cpu_to_le_16(size);
1221         hns3_set_bit(rrcfv, HNS3_TXD_VLD_B, 1);
1222
1223         if (first) {
1224                 hdr_len = rxm->l2_len + rxm->l3_len + rxm->l4_len;
1225                 hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
1226                            rxm->outer_l2_len + rxm->outer_l3_len : 0;
1227                 paylen = rxm->pkt_len - hdr_len;
1228                 desc->tx.paylen = rte_cpu_to_le_32(paylen);
1229         }
1230
1231         hns3_set_bit(rrcfv, HNS3_TXD_FE_B, frag_end);
1232         desc->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(rrcfv);
1233
1234         if (frag_end) {
1235                 if (ol_flags & (PKT_TX_VLAN_PKT | PKT_TX_QINQ_PKT)) {
1236                         tmp = rte_le_to_cpu_32(desc->tx.type_cs_vlan_tso_len);
1237                         hns3_set_bit(tmp, HNS3_TXD_VLAN_B, 1);
1238                         desc->tx.type_cs_vlan_tso_len = rte_cpu_to_le_32(tmp);
1239                         desc->tx.vlan_tag = rte_cpu_to_le_16(rxm->vlan_tci);
1240                 }
1241
1242                 if (ol_flags & PKT_TX_QINQ_PKT) {
1243                         tmp = rte_le_to_cpu_32(desc->tx.ol_type_vlan_len_msec);
1244                         hns3_set_bit(tmp, HNS3_TXD_OVLAN_B, 1);
1245                         desc->tx.ol_type_vlan_len_msec = rte_cpu_to_le_32(tmp);
1246                         desc->tx.outer_vlan_tag =
1247                                 rte_cpu_to_le_16(rxm->vlan_tci_outer);
1248                 }
1249         }
1250 }
1251
1252 static int
1253 hns3_tx_alloc_mbufs(struct hns3_tx_queue *txq, struct rte_mempool *mb_pool,
1254                     uint16_t nb_new_buf, struct rte_mbuf **alloc_mbuf)
1255 {
1256         struct rte_mbuf *new_mbuf = NULL;
1257         struct rte_eth_dev *dev;
1258         struct rte_mbuf *temp;
1259         struct hns3_hw *hw;
1260         uint16_t i;
1261
1262         /* Allocate enough mbufs */
1263         for (i = 0; i < nb_new_buf; i++) {
1264                 temp = rte_pktmbuf_alloc(mb_pool);
1265                 if (unlikely(temp == NULL)) {
1266                         dev = &rte_eth_devices[txq->port_id];
1267                         hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1268                         hns3_err(hw, "Failed to alloc TX mbuf port_id=%d,"
1269                                      "queue_id=%d in reassemble tx pkts.",
1270                                      txq->port_id, txq->queue_id);
1271                         rte_pktmbuf_free(new_mbuf);
1272                         return -ENOMEM;
1273                 }
1274                 temp->next = new_mbuf;
1275                 new_mbuf = temp;
1276         }
1277
1278         if (new_mbuf == NULL)
1279                 return -ENOMEM;
1280
1281         new_mbuf->nb_segs = nb_new_buf;
1282         *alloc_mbuf = new_mbuf;
1283
1284         return 0;
1285 }
1286
1287 static int
1288 hns3_reassemble_tx_pkts(void *tx_queue, struct rte_mbuf *tx_pkt,
1289                         struct rte_mbuf **new_pkt)
1290 {
1291         struct hns3_tx_queue *txq = tx_queue;
1292         struct rte_mempool *mb_pool;
1293         struct rte_mbuf *new_mbuf;
1294         struct rte_mbuf *temp_new;
1295         struct rte_mbuf *temp;
1296         uint16_t last_buf_len;
1297         uint16_t nb_new_buf;
1298         uint16_t buf_size;
1299         uint16_t buf_len;
1300         uint16_t len_s;
1301         uint16_t len_d;
1302         uint16_t len;
1303         uint16_t i;
1304         int ret;
1305         char *s;
1306         char *d;
1307
1308         mb_pool = tx_pkt->pool;
1309         buf_size = tx_pkt->buf_len - RTE_PKTMBUF_HEADROOM;
1310         nb_new_buf = (tx_pkt->pkt_len - 1) / buf_size + 1;
1311
1312         last_buf_len = tx_pkt->pkt_len % buf_size;
1313         if (last_buf_len == 0)
1314                 last_buf_len = buf_size;
1315
1316         /* Allocate enough mbufs */
1317         ret = hns3_tx_alloc_mbufs(txq, mb_pool, nb_new_buf, &new_mbuf);
1318         if (ret)
1319                 return ret;
1320
1321         /* Copy the original packet content to the new mbufs */
1322         temp = tx_pkt;
1323         s = rte_pktmbuf_mtod(temp, char *);
1324         len_s = temp->data_len;
1325         temp_new = new_mbuf;
1326         for (i = 0; i < nb_new_buf; i++) {
1327                 d = rte_pktmbuf_mtod(temp_new, char *);
1328                 if (i < nb_new_buf - 1)
1329                         buf_len = buf_size;
1330                 else
1331                         buf_len = last_buf_len;
1332                 len_d = buf_len;
1333
1334                 while (len_d) {
1335                         len = RTE_MIN(len_s, len_d);
1336                         memcpy(d, s, len);
1337                         s = s + len;
1338                         d = d + len;
1339                         len_d = len_d - len;
1340                         len_s = len_s - len;
1341
1342                         if (len_s == 0) {
1343                                 temp = temp->next;
1344                                 if (temp == NULL)
1345                                         break;
1346                                 s = rte_pktmbuf_mtod(temp, char *);
1347                                 len_s = temp->data_len;
1348                         }
1349                 }
1350
1351                 temp_new->data_len = buf_len;
1352                 temp_new = temp_new->next;
1353         }
1354
1355         /* free original mbufs */
1356         rte_pktmbuf_free(tx_pkt);
1357
1358         *new_pkt = new_mbuf;
1359
1360         return 0;
1361 }
1362
1363 static void
1364 hns3_parse_outer_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec)
1365 {
1366         uint32_t tmp = *ol_type_vlan_len_msec;
1367
1368         /* (outer) IP header type */
1369         if (ol_flags & PKT_TX_OUTER_IPV4) {
1370                 /* OL3 header size, defined in 4 bytes */
1371                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
1372                                sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
1373                 if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
1374                         hns3_set_field(tmp, HNS3_TXD_OL3T_M,
1375                                        HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_CSUM);
1376                 else
1377                         hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
1378                                        HNS3_OL3T_IPV4_NO_CSUM);
1379         } else if (ol_flags & PKT_TX_OUTER_IPV6) {
1380                 hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
1381                                HNS3_OL3T_IPV6);
1382                 /* OL3 header size, defined in 4 bytes */
1383                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
1384                                sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
1385         }
1386
1387         *ol_type_vlan_len_msec = tmp;
1388 }
1389
1390 static int
1391 hns3_parse_inner_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec,
1392                         struct rte_net_hdr_lens *hdr_lens)
1393 {
1394         uint32_t tmp = *ol_type_vlan_len_msec;
1395         uint8_t l4_len;
1396
1397         /* OL2 header size, defined in 2 bytes */
1398         hns3_set_field(tmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
1399                        sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
1400
1401         /* L4TUNT: L4 Tunneling Type */
1402         switch (ol_flags & PKT_TX_TUNNEL_MASK) {
1403         case PKT_TX_TUNNEL_GENEVE:
1404         case PKT_TX_TUNNEL_VXLAN:
1405                 /* MAC in UDP tunnelling packet, include VxLAN */
1406                 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
1407                                HNS3_TUN_MAC_IN_UDP);
1408                 /*
1409                  * OL4 header size, defined in 4 Bytes, it contains outer
1410                  * L4(UDP) length and tunneling length.
1411                  */
1412                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1413                                (uint8_t)RTE_ETHER_VXLAN_HLEN >>
1414                                HNS3_L4_LEN_UNIT);
1415                 break;
1416         case PKT_TX_TUNNEL_GRE:
1417                 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
1418                                HNS3_TUN_NVGRE);
1419                 /*
1420                  * OL4 header size, defined in 4 Bytes, it contains outer
1421                  * L4(GRE) length and tunneling length.
1422                  */
1423                 l4_len = hdr_lens->l4_len + hdr_lens->tunnel_len;
1424                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1425                                l4_len >> HNS3_L4_LEN_UNIT);
1426                 break;
1427         default:
1428                 /* For non UDP / GRE tunneling, drop the tunnel packet */
1429                 return -EINVAL;
1430         }
1431
1432         *ol_type_vlan_len_msec = tmp;
1433
1434         return 0;
1435 }
1436
1437 static int
1438 hns3_parse_tunneling_params(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
1439                             uint64_t ol_flags,
1440                             struct rte_net_hdr_lens *hdr_lens)
1441 {
1442         struct hns3_desc *tx_ring = txq->tx_ring;
1443         struct hns3_desc *desc = &tx_ring[tx_desc_id];
1444         uint32_t value = 0;
1445         int ret;
1446
1447         hns3_parse_outer_params(ol_flags, &value);
1448         ret = hns3_parse_inner_params(ol_flags, &value, hdr_lens);
1449         if (ret)
1450                 return -EINVAL;
1451
1452         desc->tx.ol_type_vlan_len_msec |= rte_cpu_to_le_32(value);
1453
1454         return 0;
1455 }
1456
1457 static void
1458 hns3_parse_l3_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
1459 {
1460         uint32_t tmp;
1461
1462         /* Enable L3 checksum offloads */
1463         if (ol_flags & PKT_TX_IPV4) {
1464                 tmp = *type_cs_vlan_tso_len;
1465                 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
1466                                HNS3_L3T_IPV4);
1467                 /* inner(/normal) L3 header size, defined in 4 bytes */
1468                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
1469                                sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
1470                 if (ol_flags & PKT_TX_IP_CKSUM)
1471                         hns3_set_bit(tmp, HNS3_TXD_L3CS_B, 1);
1472                 *type_cs_vlan_tso_len = tmp;
1473         } else if (ol_flags & PKT_TX_IPV6) {
1474                 tmp = *type_cs_vlan_tso_len;
1475                 /* L3T, IPv6 don't do checksum */
1476                 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
1477                                HNS3_L3T_IPV6);
1478                 /* inner(/normal) L3 header size, defined in 4 bytes */
1479                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
1480                                sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
1481                 *type_cs_vlan_tso_len = tmp;
1482         }
1483 }
1484
1485 static void
1486 hns3_parse_l4_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
1487 {
1488         uint32_t tmp;
1489
1490         /* Enable L4 checksum offloads */
1491         switch (ol_flags & PKT_TX_L4_MASK) {
1492         case PKT_TX_TCP_CKSUM:
1493                 tmp = *type_cs_vlan_tso_len;
1494                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
1495                                HNS3_L4T_TCP);
1496                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
1497                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1498                                sizeof(struct rte_tcp_hdr) >> HNS3_L4_LEN_UNIT);
1499                 *type_cs_vlan_tso_len = tmp;
1500                 break;
1501         case PKT_TX_UDP_CKSUM:
1502                 tmp = *type_cs_vlan_tso_len;
1503                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
1504                                HNS3_L4T_UDP);
1505                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
1506                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1507                                sizeof(struct rte_udp_hdr) >> HNS3_L4_LEN_UNIT);
1508                 *type_cs_vlan_tso_len = tmp;
1509                 break;
1510         case PKT_TX_SCTP_CKSUM:
1511                 tmp = *type_cs_vlan_tso_len;
1512                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
1513                                HNS3_L4T_SCTP);
1514                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
1515                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1516                                sizeof(struct rte_sctp_hdr) >> HNS3_L4_LEN_UNIT);
1517                 *type_cs_vlan_tso_len = tmp;
1518                 break;
1519         default:
1520                 break;
1521         }
1522 }
1523
1524 static void
1525 hns3_txd_enable_checksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
1526                          uint64_t ol_flags)
1527 {
1528         struct hns3_desc *tx_ring = txq->tx_ring;
1529         struct hns3_desc *desc = &tx_ring[tx_desc_id];
1530         uint32_t value = 0;
1531
1532         /* inner(/normal) L2 header size, defined in 2 bytes */
1533         hns3_set_field(value, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
1534                        sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
1535
1536         hns3_parse_l3_cksum_params(ol_flags, &value);
1537         hns3_parse_l4_cksum_params(ol_flags, &value);
1538
1539         desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(value);
1540 }
1541
1542 uint16_t
1543 hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
1544                uint16_t nb_pkts)
1545 {
1546         struct rte_mbuf *m;
1547         uint16_t i;
1548         int ret;
1549
1550         for (i = 0; i < nb_pkts; i++) {
1551                 m = tx_pkts[i];
1552
1553                 /* check the size of packet */
1554                 if (m->pkt_len < HNS3_MIN_FRAME_LEN) {
1555                         rte_errno = EINVAL;
1556                         return i;
1557                 }
1558
1559 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
1560                 ret = rte_validate_tx_offload(m);
1561                 if (ret != 0) {
1562                         rte_errno = -ret;
1563                         return i;
1564                 }
1565 #endif
1566                 ret = rte_net_intel_cksum_prepare(m);
1567                 if (ret != 0) {
1568                         rte_errno = -ret;
1569                         return i;
1570                 }
1571         }
1572
1573         return i;
1574 }
1575
1576 static int
1577 hns3_parse_cksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
1578                  const struct rte_mbuf *m, struct rte_net_hdr_lens *hdr_lens)
1579 {
1580         /* Fill in tunneling parameters if necessary */
1581         if (m->ol_flags & PKT_TX_TUNNEL_MASK) {
1582                 (void)rte_net_get_ptype(m, hdr_lens, RTE_PTYPE_ALL_MASK);
1583                 if (hns3_parse_tunneling_params(txq, tx_desc_id, m->ol_flags,
1584                                                 hdr_lens))
1585                         return -EINVAL;
1586         }
1587         /* Enable checksum offloading */
1588         if (m->ol_flags & HNS3_TX_CKSUM_OFFLOAD_MASK)
1589                 hns3_txd_enable_checksum(txq, tx_desc_id, m->ol_flags);
1590
1591         return 0;
1592 }
1593
1594 uint16_t
1595 hns3_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
1596 {
1597         struct rte_net_hdr_lens hdr_lens = {0};
1598         struct hns3_tx_queue *txq = tx_queue;
1599         struct hns3_entry *tx_bak_pkt;
1600         struct rte_mbuf *new_pkt;
1601         struct rte_mbuf *tx_pkt;
1602         struct rte_mbuf *m_seg;
1603         struct rte_mbuf *temp;
1604         uint32_t nb_hold = 0;
1605         uint16_t tx_next_clean;
1606         uint16_t tx_next_use;
1607         uint16_t tx_bd_ready;
1608         uint16_t tx_pkt_num;
1609         uint16_t tx_bd_max;
1610         uint16_t nb_buf;
1611         uint16_t nb_tx;
1612         uint16_t i;
1613
1614         /* free useless buffer */
1615         hns3_tx_free_useless_buffer(txq);
1616         tx_bd_ready = txq->tx_bd_ready;
1617         if (tx_bd_ready == 0)
1618                 return 0;
1619
1620         tx_next_clean = txq->next_to_clean;
1621         tx_next_use   = txq->next_to_use;
1622         tx_bd_max     = txq->nb_tx_desc;
1623         tx_bak_pkt = &txq->sw_ring[tx_next_clean];
1624
1625         tx_pkt_num = (tx_bd_ready < nb_pkts) ? tx_bd_ready : nb_pkts;
1626
1627         /* send packets */
1628         tx_bak_pkt = &txq->sw_ring[tx_next_use];
1629         for (nb_tx = 0; nb_tx < tx_pkt_num; nb_tx++) {
1630                 tx_pkt = *tx_pkts++;
1631
1632                 nb_buf = tx_pkt->nb_segs;
1633
1634                 if (nb_buf > tx_ring_space(txq)) {
1635                         if (nb_tx == 0)
1636                                 return 0;
1637
1638                         goto end_of_tx;
1639                 }
1640
1641                 /*
1642                  * If packet length is greater than HNS3_MAX_FRAME_LEN
1643                  * driver support, the packet will be ignored.
1644                  */
1645                 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) > HNS3_MAX_FRAME_LEN))
1646                         break;
1647
1648                 /*
1649                  * If packet length is less than minimum packet size, driver
1650                  * need to pad it.
1651                  */
1652                 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) < HNS3_MIN_PKT_SIZE)) {
1653                         uint16_t add_len;
1654                         char *appended;
1655
1656                         add_len = HNS3_MIN_PKT_SIZE -
1657                                          rte_pktmbuf_pkt_len(tx_pkt);
1658                         appended = rte_pktmbuf_append(tx_pkt, add_len);
1659                         if (appended == NULL)
1660                                 break;
1661
1662                         memset(appended, 0, add_len);
1663                 }
1664
1665                 m_seg = tx_pkt;
1666                 if (unlikely(nb_buf > HNS3_MAX_TX_BD_PER_PKT)) {
1667                         if (hns3_reassemble_tx_pkts(txq, tx_pkt, &new_pkt))
1668                                 goto end_of_tx;
1669                         m_seg = new_pkt;
1670                         nb_buf = m_seg->nb_segs;
1671                 }
1672
1673                 if (hns3_parse_cksum(txq, tx_next_use, m_seg, &hdr_lens))
1674                         goto end_of_tx;
1675
1676                 i = 0;
1677                 do {
1678                         fill_desc(txq, tx_next_use, m_seg, (i == 0), 0);
1679                         temp = m_seg->next;
1680                         tx_bak_pkt->mbuf = m_seg;
1681                         m_seg = temp;
1682                         tx_next_use++;
1683                         tx_bak_pkt++;
1684                         if (tx_next_use >= tx_bd_max) {
1685                                 tx_next_use = 0;
1686                                 tx_bak_pkt = txq->sw_ring;
1687                         }
1688
1689                         i++;
1690                 } while (m_seg != NULL);
1691
1692                 nb_hold += i;
1693                 txq->next_to_use = tx_next_use;
1694         }
1695
1696 end_of_tx:
1697
1698         if (likely(nb_tx)) {
1699                 hns3_queue_xmit(txq, nb_hold);
1700                 txq->next_to_clean = tx_next_clean;
1701                 txq->tx_bd_ready   = tx_bd_ready - nb_hold;
1702         }
1703
1704         return nb_tx;
1705 }
1706
1707 static uint16_t
1708 hns3_dummy_rxtx_burst(void *dpdk_txq __rte_unused,
1709                       struct rte_mbuf **pkts __rte_unused,
1710                       uint16_t pkts_n __rte_unused)
1711 {
1712         return 0;
1713 }
1714
1715 void hns3_set_rxtx_function(struct rte_eth_dev *eth_dev)
1716 {
1717         struct hns3_adapter *hns = eth_dev->data->dev_private;
1718
1719         if (hns->hw.adapter_state == HNS3_NIC_STARTED &&
1720             rte_atomic16_read(&hns->hw.reset.resetting) == 0) {
1721                 eth_dev->rx_pkt_burst = hns3_recv_pkts;
1722                 eth_dev->tx_pkt_burst = hns3_xmit_pkts;
1723                 eth_dev->tx_pkt_prepare = hns3_prep_pkts;
1724         } else {
1725                 eth_dev->rx_pkt_burst = hns3_dummy_rxtx_burst;
1726                 eth_dev->tx_pkt_burst = hns3_dummy_rxtx_burst;
1727                 eth_dev->tx_pkt_prepare = hns3_dummy_rxtx_burst;
1728         }
1729 }