4e3391d97bfd0dc814585a4727e118b6c8a74574
[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  32
34
35 static void
36 hns3_rx_queue_release_mbufs(struct hns3_rx_queue *rxq)
37 {
38         uint16_t i;
39
40         /* Note: Fake rx queue will not enter here */
41         if (rxq->sw_ring) {
42                 for (i = 0; i < rxq->nb_rx_desc; i++) {
43                         if (rxq->sw_ring[i].mbuf) {
44                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
45                                 rxq->sw_ring[i].mbuf = NULL;
46                         }
47                 }
48         }
49 }
50
51 static void
52 hns3_tx_queue_release_mbufs(struct hns3_tx_queue *txq)
53 {
54         uint16_t i;
55
56         /* Note: Fake rx queue will not enter here */
57         if (txq->sw_ring) {
58                 for (i = 0; i < txq->nb_tx_desc; i++) {
59                         if (txq->sw_ring[i].mbuf) {
60                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
61                                 txq->sw_ring[i].mbuf = NULL;
62                         }
63                 }
64         }
65 }
66
67 static void
68 hns3_rx_queue_release(void *queue)
69 {
70         struct hns3_rx_queue *rxq = queue;
71         if (rxq) {
72                 hns3_rx_queue_release_mbufs(rxq);
73                 if (rxq->mz)
74                         rte_memzone_free(rxq->mz);
75                 if (rxq->sw_ring)
76                         rte_free(rxq->sw_ring);
77                 rte_free(rxq);
78         }
79 }
80
81 static void
82 hns3_tx_queue_release(void *queue)
83 {
84         struct hns3_tx_queue *txq = queue;
85         if (txq) {
86                 hns3_tx_queue_release_mbufs(txq);
87                 if (txq->mz)
88                         rte_memzone_free(txq->mz);
89                 if (txq->sw_ring)
90                         rte_free(txq->sw_ring);
91                 rte_free(txq);
92         }
93 }
94
95 void
96 hns3_dev_rx_queue_release(void *queue)
97 {
98         struct hns3_rx_queue *rxq = queue;
99         struct hns3_adapter *hns;
100
101         if (rxq == NULL)
102                 return;
103
104         hns = rxq->hns;
105         rte_spinlock_lock(&hns->hw.lock);
106         hns3_rx_queue_release(queue);
107         rte_spinlock_unlock(&hns->hw.lock);
108 }
109
110 void
111 hns3_dev_tx_queue_release(void *queue)
112 {
113         struct hns3_tx_queue *txq = queue;
114         struct hns3_adapter *hns;
115
116         if (txq == NULL)
117                 return;
118
119         hns = txq->hns;
120         rte_spinlock_lock(&hns->hw.lock);
121         hns3_tx_queue_release(queue);
122         rte_spinlock_unlock(&hns->hw.lock);
123 }
124
125 static void
126 hns3_fake_rx_queue_release(struct hns3_rx_queue *queue)
127 {
128         struct hns3_rx_queue *rxq = queue;
129         struct hns3_adapter *hns;
130         struct hns3_hw *hw;
131         uint16_t idx;
132
133         if (rxq == NULL)
134                 return;
135
136         hns = rxq->hns;
137         hw = &hns->hw;
138         idx = rxq->queue_id;
139         if (hw->fkq_data.rx_queues[idx]) {
140                 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
141                 hw->fkq_data.rx_queues[idx] = NULL;
142         }
143
144         /* free fake rx queue arrays */
145         if (idx == (hw->fkq_data.nb_fake_rx_queues - 1)) {
146                 hw->fkq_data.nb_fake_rx_queues = 0;
147                 rte_free(hw->fkq_data.rx_queues);
148                 hw->fkq_data.rx_queues = NULL;
149         }
150 }
151
152 static void
153 hns3_fake_tx_queue_release(struct hns3_tx_queue *queue)
154 {
155         struct hns3_tx_queue *txq = queue;
156         struct hns3_adapter *hns;
157         struct hns3_hw *hw;
158         uint16_t idx;
159
160         if (txq == NULL)
161                 return;
162
163         hns = txq->hns;
164         hw = &hns->hw;
165         idx = txq->queue_id;
166         if (hw->fkq_data.tx_queues[idx]) {
167                 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
168                 hw->fkq_data.tx_queues[idx] = NULL;
169         }
170
171         /* free fake tx queue arrays */
172         if (idx == (hw->fkq_data.nb_fake_tx_queues - 1)) {
173                 hw->fkq_data.nb_fake_tx_queues = 0;
174                 rte_free(hw->fkq_data.tx_queues);
175                 hw->fkq_data.tx_queues = NULL;
176         }
177 }
178
179 static void
180 hns3_free_rx_queues(struct rte_eth_dev *dev)
181 {
182         struct hns3_adapter *hns = dev->data->dev_private;
183         struct hns3_fake_queue_data *fkq_data;
184         struct hns3_hw *hw = &hns->hw;
185         uint16_t nb_rx_q;
186         uint16_t i;
187
188         nb_rx_q = hw->data->nb_rx_queues;
189         for (i = 0; i < nb_rx_q; i++) {
190                 if (dev->data->rx_queues[i]) {
191                         hns3_rx_queue_release(dev->data->rx_queues[i]);
192                         dev->data->rx_queues[i] = NULL;
193                 }
194         }
195
196         /* Free fake Rx queues */
197         fkq_data = &hw->fkq_data;
198         for (i = 0; i < fkq_data->nb_fake_rx_queues; i++) {
199                 if (fkq_data->rx_queues[i])
200                         hns3_fake_rx_queue_release(fkq_data->rx_queues[i]);
201         }
202 }
203
204 static void
205 hns3_free_tx_queues(struct rte_eth_dev *dev)
206 {
207         struct hns3_adapter *hns = dev->data->dev_private;
208         struct hns3_fake_queue_data *fkq_data;
209         struct hns3_hw *hw = &hns->hw;
210         uint16_t nb_tx_q;
211         uint16_t i;
212
213         nb_tx_q = hw->data->nb_tx_queues;
214         for (i = 0; i < nb_tx_q; i++) {
215                 if (dev->data->tx_queues[i]) {
216                         hns3_tx_queue_release(dev->data->tx_queues[i]);
217                         dev->data->tx_queues[i] = NULL;
218                 }
219         }
220
221         /* Free fake Tx queues */
222         fkq_data = &hw->fkq_data;
223         for (i = 0; i < fkq_data->nb_fake_tx_queues; i++) {
224                 if (fkq_data->tx_queues[i])
225                         hns3_fake_tx_queue_release(fkq_data->tx_queues[i]);
226         }
227 }
228
229 void
230 hns3_free_all_queues(struct rte_eth_dev *dev)
231 {
232         hns3_free_rx_queues(dev);
233         hns3_free_tx_queues(dev);
234 }
235
236 static int
237 hns3_alloc_rx_queue_mbufs(struct hns3_hw *hw, struct hns3_rx_queue *rxq)
238 {
239         struct rte_mbuf *mbuf;
240         uint64_t dma_addr;
241         uint16_t i;
242
243         for (i = 0; i < rxq->nb_rx_desc; i++) {
244                 mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
245                 if (unlikely(mbuf == NULL)) {
246                         hns3_err(hw, "Failed to allocate RXD[%d] for rx queue!",
247                                  i);
248                         hns3_rx_queue_release_mbufs(rxq);
249                         return -ENOMEM;
250                 }
251
252                 rte_mbuf_refcnt_set(mbuf, 1);
253                 mbuf->next = NULL;
254                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
255                 mbuf->nb_segs = 1;
256                 mbuf->port = rxq->port_id;
257
258                 rxq->sw_ring[i].mbuf = mbuf;
259                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
260                 rxq->rx_ring[i].addr = dma_addr;
261                 rxq->rx_ring[i].rx.bd_base_info = 0;
262         }
263
264         return 0;
265 }
266
267 static int
268 hns3_buf_size2type(uint32_t buf_size)
269 {
270         int bd_size_type;
271
272         switch (buf_size) {
273         case 512:
274                 bd_size_type = HNS3_BD_SIZE_512_TYPE;
275                 break;
276         case 1024:
277                 bd_size_type = HNS3_BD_SIZE_1024_TYPE;
278                 break;
279         case 4096:
280                 bd_size_type = HNS3_BD_SIZE_4096_TYPE;
281                 break;
282         default:
283                 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
284         }
285
286         return bd_size_type;
287 }
288
289 static void
290 hns3_init_rx_queue_hw(struct hns3_rx_queue *rxq)
291 {
292         uint32_t rx_buf_len = rxq->rx_buf_len;
293         uint64_t dma_addr = rxq->rx_ring_phys_addr;
294
295         hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_L_REG, (uint32_t)dma_addr);
296         hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_H_REG,
297                        (uint32_t)((dma_addr >> 31) >> 1));
298
299         hns3_write_dev(rxq, HNS3_RING_RX_BD_LEN_REG,
300                        hns3_buf_size2type(rx_buf_len));
301         hns3_write_dev(rxq, HNS3_RING_RX_BD_NUM_REG,
302                        HNS3_CFG_DESC_NUM(rxq->nb_rx_desc));
303 }
304
305 static void
306 hns3_init_tx_queue_hw(struct hns3_tx_queue *txq)
307 {
308         uint64_t dma_addr = txq->tx_ring_phys_addr;
309
310         hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_L_REG, (uint32_t)dma_addr);
311         hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_H_REG,
312                        (uint32_t)((dma_addr >> 31) >> 1));
313
314         hns3_write_dev(txq, HNS3_RING_TX_BD_NUM_REG,
315                        HNS3_CFG_DESC_NUM(txq->nb_tx_desc));
316 }
317
318 void
319 hns3_enable_all_queues(struct hns3_hw *hw, bool en)
320 {
321         uint16_t nb_rx_q = hw->data->nb_rx_queues;
322         uint16_t nb_tx_q = hw->data->nb_tx_queues;
323         struct hns3_rx_queue *rxq;
324         struct hns3_tx_queue *txq;
325         uint32_t rcb_reg;
326         int i;
327
328         for (i = 0; i < hw->cfg_max_queues; i++) {
329                 if (i < nb_rx_q)
330                         rxq = hw->data->rx_queues[i];
331                 else
332                         rxq = hw->fkq_data.rx_queues[i - nb_rx_q];
333                 if (i < nb_tx_q)
334                         txq = hw->data->tx_queues[i];
335                 else
336                         txq = hw->fkq_data.tx_queues[i - nb_tx_q];
337                 if (rxq == NULL || txq == NULL ||
338                     (en && (rxq->rx_deferred_start || txq->tx_deferred_start)))
339                         continue;
340
341                 rcb_reg = hns3_read_dev(rxq, HNS3_RING_EN_REG);
342                 if (en)
343                         rcb_reg |= BIT(HNS3_RING_EN_B);
344                 else
345                         rcb_reg &= ~BIT(HNS3_RING_EN_B);
346                 hns3_write_dev(rxq, HNS3_RING_EN_REG, rcb_reg);
347         }
348 }
349
350 static int
351 hns3_tqp_enable(struct hns3_hw *hw, uint16_t queue_id, bool enable)
352 {
353         struct hns3_cfg_com_tqp_queue_cmd *req;
354         struct hns3_cmd_desc desc;
355         int ret;
356
357         req = (struct hns3_cfg_com_tqp_queue_cmd *)desc.data;
358
359         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_COM_TQP_QUEUE, false);
360         req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
361         req->stream_id = 0;
362         hns3_set_bit(req->enable, HNS3_TQP_ENABLE_B, enable ? 1 : 0);
363
364         ret = hns3_cmd_send(hw, &desc, 1);
365         if (ret)
366                 hns3_err(hw, "TQP enable fail, ret = %d", ret);
367
368         return ret;
369 }
370
371 static int
372 hns3_send_reset_tqp_cmd(struct hns3_hw *hw, uint16_t queue_id, bool enable)
373 {
374         struct hns3_reset_tqp_queue_cmd *req;
375         struct hns3_cmd_desc desc;
376         int ret;
377
378         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, false);
379
380         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
381         req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
382         hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);
383
384         ret = hns3_cmd_send(hw, &desc, 1);
385         if (ret)
386                 hns3_err(hw, "Send tqp reset cmd error, ret = %d", ret);
387
388         return ret;
389 }
390
391 static int
392 hns3_get_reset_status(struct hns3_hw *hw, uint16_t queue_id)
393 {
394         struct hns3_reset_tqp_queue_cmd *req;
395         struct hns3_cmd_desc desc;
396         int ret;
397
398         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, true);
399
400         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
401         req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
402
403         ret = hns3_cmd_send(hw, &desc, 1);
404         if (ret) {
405                 hns3_err(hw, "Get reset status error, ret =%d", ret);
406                 return ret;
407         }
408
409         return hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
410 }
411
412 static int
413 hns3_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
414 {
415 #define HNS3_TQP_RESET_TRY_MS   200
416         uint64_t end;
417         int reset_status;
418         int ret;
419
420         ret = hns3_tqp_enable(hw, queue_id, false);
421         if (ret)
422                 return ret;
423
424         /*
425          * In current version VF is not supported when PF is driven by DPDK
426          * driver, all task queue pairs are mapped to PF function, so PF's queue
427          * id is equals to the global queue id in PF range.
428          */
429         ret = hns3_send_reset_tqp_cmd(hw, queue_id, true);
430         if (ret) {
431                 hns3_err(hw, "Send reset tqp cmd fail, ret = %d", ret);
432                 return ret;
433         }
434         ret = -ETIMEDOUT;
435         end = get_timeofday_ms() + HNS3_TQP_RESET_TRY_MS;
436         do {
437                 /* Wait for tqp hw reset */
438                 rte_delay_ms(HNS3_POLL_RESPONE_MS);
439                 reset_status = hns3_get_reset_status(hw, queue_id);
440                 if (reset_status) {
441                         ret = 0;
442                         break;
443                 }
444         } while (get_timeofday_ms() < end);
445
446         if (ret) {
447                 hns3_err(hw, "Reset TQP fail, ret = %d", ret);
448                 return ret;
449         }
450
451         ret = hns3_send_reset_tqp_cmd(hw, queue_id, false);
452         if (ret)
453                 hns3_err(hw, "Deassert the soft reset fail, ret = %d", ret);
454
455         return ret;
456 }
457
458 static int
459 hns3vf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
460 {
461         uint8_t msg_data[2];
462         int ret;
463
464         /* Disable VF's queue before send queue reset msg to PF */
465         ret = hns3_tqp_enable(hw, queue_id, false);
466         if (ret)
467                 return ret;
468
469         memcpy(msg_data, &queue_id, sizeof(uint16_t));
470
471         return hns3_send_mbx_msg(hw, HNS3_MBX_QUEUE_RESET, 0, msg_data,
472                                  sizeof(msg_data), true, NULL, 0);
473 }
474
475 static int
476 hns3_reset_queue(struct hns3_adapter *hns, uint16_t queue_id)
477 {
478         struct hns3_hw *hw = &hns->hw;
479         if (hns->is_vf)
480                 return hns3vf_reset_tqp(hw, queue_id);
481         else
482                 return hns3_reset_tqp(hw, queue_id);
483 }
484
485 int
486 hns3_reset_all_queues(struct hns3_adapter *hns)
487 {
488         struct hns3_hw *hw = &hns->hw;
489         int ret, i;
490
491         for (i = 0; i < hw->cfg_max_queues; i++) {
492                 ret = hns3_reset_queue(hns, i);
493                 if (ret) {
494                         hns3_err(hw, "Failed to reset No.%d queue: %d", i, ret);
495                         return ret;
496                 }
497         }
498         return 0;
499 }
500
501 void
502 hns3_set_queue_intr_gl(struct hns3_hw *hw, uint16_t queue_id,
503                        uint8_t gl_idx, uint16_t gl_value)
504 {
505         uint32_t offset[] = {HNS3_TQP_INTR_GL0_REG,
506                              HNS3_TQP_INTR_GL1_REG,
507                              HNS3_TQP_INTR_GL2_REG};
508         uint32_t addr, value;
509
510         if (gl_idx >= RTE_DIM(offset) || gl_value > HNS3_TQP_INTR_GL_MAX)
511                 return;
512
513         addr = offset[gl_idx] + queue_id * HNS3_TQP_INTR_REG_SIZE;
514         value = HNS3_GL_USEC_TO_REG(gl_value);
515
516         hns3_write_dev(hw, addr, value);
517 }
518
519 void
520 hns3_set_queue_intr_rl(struct hns3_hw *hw, uint16_t queue_id, uint16_t rl_value)
521 {
522         uint32_t addr, value;
523
524         if (rl_value > HNS3_TQP_INTR_RL_MAX)
525                 return;
526
527         addr = HNS3_TQP_INTR_RL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
528         value = HNS3_RL_USEC_TO_REG(rl_value);
529         if (value > 0)
530                 value |= HNS3_TQP_INTR_RL_ENABLE_MASK;
531
532         hns3_write_dev(hw, addr, value);
533 }
534
535 static void
536 hns3_queue_intr_enable(struct hns3_hw *hw, uint16_t queue_id, bool en)
537 {
538         uint32_t addr, value;
539
540         addr = HNS3_TQP_INTR_CTRL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
541         value = en ? 1 : 0;
542
543         hns3_write_dev(hw, addr, value);
544 }
545
546 /*
547  * Enable all rx queue interrupt when in interrupt rx mode.
548  * This api was called before enable queue rx&tx (in normal start or reset
549  * recover scenes), used to fix hardware rx queue interrupt enable was clear
550  * when FLR.
551  */
552 void
553 hns3_dev_all_rx_queue_intr_enable(struct hns3_hw *hw, bool en)
554 {
555         struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
556         uint16_t nb_rx_q = hw->data->nb_rx_queues;
557         int i;
558
559         if (dev->data->dev_conf.intr_conf.rxq == 0)
560                 return;
561
562         for (i = 0; i < nb_rx_q; i++)
563                 hns3_queue_intr_enable(hw, i, en);
564 }
565
566 int
567 hns3_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
568 {
569         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
570         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
571         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
572
573         if (dev->data->dev_conf.intr_conf.rxq == 0)
574                 return -ENOTSUP;
575
576         hns3_queue_intr_enable(hw, queue_id, true);
577
578         return rte_intr_ack(intr_handle);
579 }
580
581 int
582 hns3_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
583 {
584         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
585
586         if (dev->data->dev_conf.intr_conf.rxq == 0)
587                 return -ENOTSUP;
588
589         hns3_queue_intr_enable(hw, queue_id, false);
590
591         return 0;
592 }
593
594 static int
595 hns3_dev_rx_queue_start(struct hns3_adapter *hns, uint16_t idx)
596 {
597         struct hns3_hw *hw = &hns->hw;
598         struct hns3_rx_queue *rxq;
599         int ret;
600
601         PMD_INIT_FUNC_TRACE();
602
603         rxq = (struct hns3_rx_queue *)hw->data->rx_queues[idx];
604         ret = hns3_alloc_rx_queue_mbufs(hw, rxq);
605         if (ret) {
606                 hns3_err(hw, "Failed to alloc mbuf for No.%d rx queue: %d",
607                          idx, ret);
608                 return ret;
609         }
610
611         rxq->next_to_use = 0;
612         rxq->next_to_clean = 0;
613         rxq->nb_rx_hold = 0;
614         hns3_init_rx_queue_hw(rxq);
615
616         return 0;
617 }
618
619 static void
620 hns3_fake_rx_queue_start(struct hns3_adapter *hns, uint16_t idx)
621 {
622         struct hns3_hw *hw = &hns->hw;
623         struct hns3_rx_queue *rxq;
624
625         rxq = (struct hns3_rx_queue *)hw->fkq_data.rx_queues[idx];
626         rxq->next_to_use = 0;
627         rxq->next_to_clean = 0;
628         rxq->nb_rx_hold = 0;
629         hns3_init_rx_queue_hw(rxq);
630 }
631
632 static void
633 hns3_init_tx_queue(struct hns3_tx_queue *queue)
634 {
635         struct hns3_tx_queue *txq = queue;
636         struct hns3_desc *desc;
637         int i;
638
639         /* Clear tx bd */
640         desc = txq->tx_ring;
641         for (i = 0; i < txq->nb_tx_desc; i++) {
642                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
643                 desc++;
644         }
645
646         txq->next_to_use = 0;
647         txq->next_to_clean = 0;
648         txq->tx_bd_ready = txq->nb_tx_desc - 1;
649         hns3_init_tx_queue_hw(txq);
650 }
651
652 static void
653 hns3_dev_tx_queue_start(struct hns3_adapter *hns, uint16_t idx)
654 {
655         struct hns3_hw *hw = &hns->hw;
656         struct hns3_tx_queue *txq;
657
658         txq = (struct hns3_tx_queue *)hw->data->tx_queues[idx];
659         hns3_init_tx_queue(txq);
660 }
661
662 static void
663 hns3_fake_tx_queue_start(struct hns3_adapter *hns, uint16_t idx)
664 {
665         struct hns3_hw *hw = &hns->hw;
666         struct hns3_tx_queue *txq;
667
668         txq = (struct hns3_tx_queue *)hw->fkq_data.tx_queues[idx];
669         hns3_init_tx_queue(txq);
670 }
671
672 static void
673 hns3_init_tx_ring_tc(struct hns3_adapter *hns)
674 {
675         struct hns3_hw *hw = &hns->hw;
676         struct hns3_tx_queue *txq;
677         int i, num;
678
679         for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
680                 struct hns3_tc_queue_info *tc_queue = &hw->tc_queue[i];
681                 int j;
682
683                 if (!tc_queue->enable)
684                         continue;
685
686                 for (j = 0; j < tc_queue->tqp_count; j++) {
687                         num = tc_queue->tqp_offset + j;
688                         txq = (struct hns3_tx_queue *)hw->data->tx_queues[num];
689                         if (txq == NULL)
690                                 continue;
691
692                         hns3_write_dev(txq, HNS3_RING_TX_TC_REG, tc_queue->tc);
693                 }
694         }
695 }
696
697 static int
698 hns3_start_rx_queues(struct hns3_adapter *hns)
699 {
700         struct hns3_hw *hw = &hns->hw;
701         struct hns3_rx_queue *rxq;
702         int i, j;
703         int ret;
704
705         /* Initialize RSS for queues */
706         ret = hns3_config_rss(hns);
707         if (ret) {
708                 hns3_err(hw, "Failed to configure rss %d", ret);
709                 return ret;
710         }
711
712         for (i = 0; i < hw->data->nb_rx_queues; i++) {
713                 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[i];
714                 if (rxq == NULL || rxq->rx_deferred_start)
715                         continue;
716                 ret = hns3_dev_rx_queue_start(hns, i);
717                 if (ret) {
718                         hns3_err(hw, "Failed to start No.%d rx queue: %d", i,
719                                  ret);
720                         goto out;
721                 }
722         }
723
724         for (i = 0; i < hw->fkq_data.nb_fake_rx_queues; i++) {
725                 rxq = (struct hns3_rx_queue *)hw->fkq_data.rx_queues[i];
726                 if (rxq == NULL || rxq->rx_deferred_start)
727                         continue;
728                 hns3_fake_rx_queue_start(hns, i);
729         }
730         return 0;
731
732 out:
733         for (j = 0; j < i; j++) {
734                 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[j];
735                 hns3_rx_queue_release_mbufs(rxq);
736         }
737
738         return ret;
739 }
740
741 static void
742 hns3_start_tx_queues(struct hns3_adapter *hns)
743 {
744         struct hns3_hw *hw = &hns->hw;
745         struct hns3_tx_queue *txq;
746         int i;
747
748         for (i = 0; i < hw->data->nb_tx_queues; i++) {
749                 txq = (struct hns3_tx_queue *)hw->data->tx_queues[i];
750                 if (txq == NULL || txq->tx_deferred_start)
751                         continue;
752                 hns3_dev_tx_queue_start(hns, i);
753         }
754
755         for (i = 0; i < hw->fkq_data.nb_fake_tx_queues; i++) {
756                 txq = (struct hns3_tx_queue *)hw->fkq_data.tx_queues[i];
757                 if (txq == NULL || txq->tx_deferred_start)
758                         continue;
759                 hns3_fake_tx_queue_start(hns, i);
760         }
761
762         hns3_init_tx_ring_tc(hns);
763 }
764
765 /*
766  * Start all queues.
767  * Note: just init and setup queues, and don't enable queue rx&tx.
768  */
769 int
770 hns3_start_queues(struct hns3_adapter *hns, bool reset_queue)
771 {
772         struct hns3_hw *hw = &hns->hw;
773         int ret;
774
775         if (reset_queue) {
776                 ret = hns3_reset_all_queues(hns);
777                 if (ret) {
778                         hns3_err(hw, "Failed to reset all queues %d", ret);
779                         return ret;
780                 }
781         }
782
783         ret = hns3_start_rx_queues(hns);
784         if (ret) {
785                 hns3_err(hw, "Failed to start rx queues: %d", ret);
786                 return ret;
787         }
788
789         hns3_start_tx_queues(hns);
790
791         return 0;
792 }
793
794 int
795 hns3_stop_queues(struct hns3_adapter *hns, bool reset_queue)
796 {
797         struct hns3_hw *hw = &hns->hw;
798         int ret;
799
800         hns3_enable_all_queues(hw, false);
801         if (reset_queue) {
802                 ret = hns3_reset_all_queues(hns);
803                 if (ret) {
804                         hns3_err(hw, "Failed to reset all queues %d", ret);
805                         return ret;
806                 }
807         }
808         return 0;
809 }
810
811 static void*
812 hns3_alloc_rxq_and_dma_zone(struct rte_eth_dev *dev,
813                             struct hns3_queue_info *q_info)
814 {
815         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
816         const struct rte_memzone *rx_mz;
817         struct hns3_rx_queue *rxq;
818         unsigned int rx_desc;
819
820         rxq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_rx_queue),
821                                  RTE_CACHE_LINE_SIZE, q_info->socket_id);
822         if (rxq == NULL) {
823                 hns3_err(hw, "Failed to allocate memory for No.%d rx ring!",
824                          q_info->idx);
825                 return NULL;
826         }
827
828         /* Allocate rx ring hardware descriptors. */
829         rxq->queue_id = q_info->idx;
830         rxq->nb_rx_desc = q_info->nb_desc;
831         rx_desc = rxq->nb_rx_desc * sizeof(struct hns3_desc);
832         rx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
833                                          rx_desc, HNS3_RING_BASE_ALIGN,
834                                          q_info->socket_id);
835         if (rx_mz == NULL) {
836                 hns3_err(hw, "Failed to reserve DMA memory for No.%d rx ring!",
837                          q_info->idx);
838                 hns3_rx_queue_release(rxq);
839                 return NULL;
840         }
841         rxq->mz = rx_mz;
842         rxq->rx_ring = (struct hns3_desc *)rx_mz->addr;
843         rxq->rx_ring_phys_addr = rx_mz->iova;
844
845         hns3_dbg(hw, "No.%d rx descriptors iova 0x%" PRIx64, q_info->idx,
846                  rxq->rx_ring_phys_addr);
847
848         return rxq;
849 }
850
851 static int
852 hns3_fake_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
853                          uint16_t nb_desc, unsigned int socket_id)
854 {
855         struct hns3_adapter *hns = dev->data->dev_private;
856         struct hns3_hw *hw = &hns->hw;
857         struct hns3_queue_info q_info;
858         struct hns3_rx_queue *rxq;
859         uint16_t nb_rx_q;
860
861         if (hw->fkq_data.rx_queues[idx]) {
862                 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
863                 hw->fkq_data.rx_queues[idx] = NULL;
864         }
865
866         q_info.idx = idx;
867         q_info.socket_id = socket_id;
868         q_info.nb_desc = nb_desc;
869         q_info.type = "hns3 fake RX queue";
870         q_info.ring_name = "rx_fake_ring";
871         rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
872         if (rxq == NULL) {
873                 hns3_err(hw, "Failed to setup No.%d fake rx ring.", idx);
874                 return -ENOMEM;
875         }
876
877         /* Don't need alloc sw_ring, because upper applications don't use it */
878         rxq->sw_ring = NULL;
879
880         rxq->hns = hns;
881         rxq->rx_deferred_start = false;
882         rxq->port_id = dev->data->port_id;
883         rxq->configured = true;
884         nb_rx_q = dev->data->nb_rx_queues;
885         rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
886                                 (nb_rx_q + idx) * HNS3_TQP_REG_SIZE);
887         rxq->rx_buf_len = hw->rx_buf_len;
888
889         rte_spinlock_lock(&hw->lock);
890         hw->fkq_data.rx_queues[idx] = rxq;
891         rte_spinlock_unlock(&hw->lock);
892
893         return 0;
894 }
895
896 static void*
897 hns3_alloc_txq_and_dma_zone(struct rte_eth_dev *dev,
898                             struct hns3_queue_info *q_info)
899 {
900         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
901         const struct rte_memzone *tx_mz;
902         struct hns3_tx_queue *txq;
903         struct hns3_desc *desc;
904         unsigned int tx_desc;
905         int i;
906
907         txq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_tx_queue),
908                                  RTE_CACHE_LINE_SIZE, q_info->socket_id);
909         if (txq == NULL) {
910                 hns3_err(hw, "Failed to allocate memory for No.%d tx ring!",
911                          q_info->idx);
912                 return NULL;
913         }
914
915         /* Allocate tx ring hardware descriptors. */
916         txq->queue_id = q_info->idx;
917         txq->nb_tx_desc = q_info->nb_desc;
918         tx_desc = txq->nb_tx_desc * sizeof(struct hns3_desc);
919         tx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
920                                          tx_desc, HNS3_RING_BASE_ALIGN,
921                                          q_info->socket_id);
922         if (tx_mz == NULL) {
923                 hns3_err(hw, "Failed to reserve DMA memory for No.%d tx ring!",
924                          q_info->idx);
925                 hns3_tx_queue_release(txq);
926                 return NULL;
927         }
928         txq->mz = tx_mz;
929         txq->tx_ring = (struct hns3_desc *)tx_mz->addr;
930         txq->tx_ring_phys_addr = tx_mz->iova;
931
932         hns3_dbg(hw, "No.%d tx descriptors iova 0x%" PRIx64, q_info->idx,
933                  txq->tx_ring_phys_addr);
934
935         /* Clear tx bd */
936         desc = txq->tx_ring;
937         for (i = 0; i < txq->nb_tx_desc; i++) {
938                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
939                 desc++;
940         }
941
942         return txq;
943 }
944
945 static int
946 hns3_fake_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
947                          uint16_t nb_desc, unsigned int socket_id)
948 {
949         struct hns3_adapter *hns = dev->data->dev_private;
950         struct hns3_hw *hw = &hns->hw;
951         struct hns3_queue_info q_info;
952         struct hns3_tx_queue *txq;
953         uint16_t nb_tx_q;
954
955         if (hw->fkq_data.tx_queues[idx] != NULL) {
956                 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
957                 hw->fkq_data.tx_queues[idx] = NULL;
958         }
959
960         q_info.idx = idx;
961         q_info.socket_id = socket_id;
962         q_info.nb_desc = nb_desc;
963         q_info.type = "hns3 fake TX queue";
964         q_info.ring_name = "tx_fake_ring";
965         txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
966         if (txq == NULL) {
967                 hns3_err(hw, "Failed to setup No.%d fake tx ring.", idx);
968                 return -ENOMEM;
969         }
970
971         /* Don't need alloc sw_ring, because upper applications don't use it */
972         txq->sw_ring = NULL;
973
974         txq->hns = hns;
975         txq->tx_deferred_start = false;
976         txq->port_id = dev->data->port_id;
977         txq->configured = true;
978         nb_tx_q = dev->data->nb_tx_queues;
979         txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
980                                 (nb_tx_q + idx) * HNS3_TQP_REG_SIZE);
981
982         rte_spinlock_lock(&hw->lock);
983         hw->fkq_data.tx_queues[idx] = txq;
984         rte_spinlock_unlock(&hw->lock);
985
986         return 0;
987 }
988
989 static int
990 hns3_fake_rx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
991 {
992         uint16_t old_nb_queues = hw->fkq_data.nb_fake_rx_queues;
993         void **rxq;
994         uint8_t i;
995
996         if (hw->fkq_data.rx_queues == NULL && nb_queues != 0) {
997                 /* first time configuration */
998                 uint32_t size;
999                 size = sizeof(hw->fkq_data.rx_queues[0]) * nb_queues;
1000                 hw->fkq_data.rx_queues = rte_zmalloc("fake_rx_queues", size,
1001                                                      RTE_CACHE_LINE_SIZE);
1002                 if (hw->fkq_data.rx_queues == NULL) {
1003                         hw->fkq_data.nb_fake_rx_queues = 0;
1004                         return -ENOMEM;
1005                 }
1006         } else if (hw->fkq_data.rx_queues != NULL && nb_queues != 0) {
1007                 /* re-configure */
1008                 rxq = hw->fkq_data.rx_queues;
1009                 for (i = nb_queues; i < old_nb_queues; i++)
1010                         hns3_dev_rx_queue_release(rxq[i]);
1011
1012                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
1013                                   RTE_CACHE_LINE_SIZE);
1014                 if (rxq == NULL)
1015                         return -ENOMEM;
1016                 if (nb_queues > old_nb_queues) {
1017                         uint16_t new_qs = nb_queues - old_nb_queues;
1018                         memset(rxq + old_nb_queues, 0, sizeof(rxq[0]) * new_qs);
1019                 }
1020
1021                 hw->fkq_data.rx_queues = rxq;
1022         } else if (hw->fkq_data.rx_queues != NULL && nb_queues == 0) {
1023                 rxq = hw->fkq_data.rx_queues;
1024                 for (i = nb_queues; i < old_nb_queues; i++)
1025                         hns3_dev_rx_queue_release(rxq[i]);
1026
1027                 rte_free(hw->fkq_data.rx_queues);
1028                 hw->fkq_data.rx_queues = NULL;
1029         }
1030
1031         hw->fkq_data.nb_fake_rx_queues = nb_queues;
1032
1033         return 0;
1034 }
1035
1036 static int
1037 hns3_fake_tx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
1038 {
1039         uint16_t old_nb_queues = hw->fkq_data.nb_fake_tx_queues;
1040         void **txq;
1041         uint8_t i;
1042
1043         if (hw->fkq_data.tx_queues == NULL && nb_queues != 0) {
1044                 /* first time configuration */
1045                 uint32_t size;
1046                 size = sizeof(hw->fkq_data.tx_queues[0]) * nb_queues;
1047                 hw->fkq_data.tx_queues = rte_zmalloc("fake_tx_queues", size,
1048                                                      RTE_CACHE_LINE_SIZE);
1049                 if (hw->fkq_data.tx_queues == NULL) {
1050                         hw->fkq_data.nb_fake_tx_queues = 0;
1051                         return -ENOMEM;
1052                 }
1053         } else if (hw->fkq_data.tx_queues != NULL && nb_queues != 0) {
1054                 /* re-configure */
1055                 txq = hw->fkq_data.tx_queues;
1056                 for (i = nb_queues; i < old_nb_queues; i++)
1057                         hns3_dev_tx_queue_release(txq[i]);
1058                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
1059                                   RTE_CACHE_LINE_SIZE);
1060                 if (txq == NULL)
1061                         return -ENOMEM;
1062                 if (nb_queues > old_nb_queues) {
1063                         uint16_t new_qs = nb_queues - old_nb_queues;
1064                         memset(txq + old_nb_queues, 0, sizeof(txq[0]) * new_qs);
1065                 }
1066
1067                 hw->fkq_data.tx_queues = txq;
1068         } else if (hw->fkq_data.tx_queues != NULL && nb_queues == 0) {
1069                 txq = hw->fkq_data.tx_queues;
1070                 for (i = nb_queues; i < old_nb_queues; i++)
1071                         hns3_dev_tx_queue_release(txq[i]);
1072
1073                 rte_free(hw->fkq_data.tx_queues);
1074                 hw->fkq_data.tx_queues = NULL;
1075         }
1076         hw->fkq_data.nb_fake_tx_queues = nb_queues;
1077
1078         return 0;
1079 }
1080
1081 int
1082 hns3_set_fake_rx_or_tx_queues(struct rte_eth_dev *dev, uint16_t nb_rx_q,
1083                               uint16_t nb_tx_q)
1084 {
1085         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1086         uint16_t rx_need_add_nb_q;
1087         uint16_t tx_need_add_nb_q;
1088         uint16_t port_id;
1089         uint16_t q;
1090         int ret;
1091
1092         /* Setup new number of fake RX/TX queues and reconfigure device. */
1093         hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
1094         rx_need_add_nb_q = hw->cfg_max_queues - nb_rx_q;
1095         tx_need_add_nb_q = hw->cfg_max_queues - nb_tx_q;
1096         ret = hns3_fake_rx_queue_config(hw, rx_need_add_nb_q);
1097         if (ret) {
1098                 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1099                 goto cfg_fake_rx_q_fail;
1100         }
1101
1102         ret = hns3_fake_tx_queue_config(hw, tx_need_add_nb_q);
1103         if (ret) {
1104                 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1105                 goto cfg_fake_tx_q_fail;
1106         }
1107
1108         /* Allocate and set up fake RX queue per Ethernet port. */
1109         port_id = hw->data->port_id;
1110         for (q = 0; q < rx_need_add_nb_q; q++) {
1111                 ret = hns3_fake_rx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1112                                                rte_eth_dev_socket_id(port_id));
1113                 if (ret)
1114                         goto setup_fake_rx_q_fail;
1115         }
1116
1117         /* Allocate and set up fake TX queue per Ethernet port. */
1118         for (q = 0; q < tx_need_add_nb_q; q++) {
1119                 ret = hns3_fake_tx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1120                                                rte_eth_dev_socket_id(port_id));
1121                 if (ret)
1122                         goto setup_fake_tx_q_fail;
1123         }
1124
1125         return 0;
1126
1127 setup_fake_tx_q_fail:
1128 setup_fake_rx_q_fail:
1129         (void)hns3_fake_tx_queue_config(hw, 0);
1130 cfg_fake_tx_q_fail:
1131         (void)hns3_fake_rx_queue_config(hw, 0);
1132 cfg_fake_rx_q_fail:
1133         hw->cfg_max_queues = 0;
1134
1135         return ret;
1136 }
1137
1138 void
1139 hns3_dev_release_mbufs(struct hns3_adapter *hns)
1140 {
1141         struct rte_eth_dev_data *dev_data = hns->hw.data;
1142         struct hns3_rx_queue *rxq;
1143         struct hns3_tx_queue *txq;
1144         int i;
1145
1146         if (dev_data->rx_queues)
1147                 for (i = 0; i < dev_data->nb_rx_queues; i++) {
1148                         rxq = dev_data->rx_queues[i];
1149                         if (rxq == NULL || rxq->rx_deferred_start)
1150                                 continue;
1151                         hns3_rx_queue_release_mbufs(rxq);
1152                 }
1153
1154         if (dev_data->tx_queues)
1155                 for (i = 0; i < dev_data->nb_tx_queues; i++) {
1156                         txq = dev_data->tx_queues[i];
1157                         if (txq == NULL || txq->tx_deferred_start)
1158                                 continue;
1159                         hns3_tx_queue_release_mbufs(txq);
1160                 }
1161 }
1162
1163 int
1164 hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
1165                     unsigned int socket_id, const struct rte_eth_rxconf *conf,
1166                     struct rte_mempool *mp)
1167 {
1168         struct hns3_adapter *hns = dev->data->dev_private;
1169         struct hns3_hw *hw = &hns->hw;
1170         struct hns3_queue_info q_info;
1171         struct hns3_rx_queue *rxq;
1172         int rx_entry_len;
1173
1174         if (dev->data->dev_started) {
1175                 hns3_err(hw, "rx_queue_setup after dev_start no supported");
1176                 return -EINVAL;
1177         }
1178
1179         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
1180             nb_desc % HNS3_ALIGN_RING_DESC) {
1181                 hns3_err(hw, "Number (%u) of rx descriptors is invalid",
1182                          nb_desc);
1183                 return -EINVAL;
1184         }
1185
1186         if (dev->data->rx_queues[idx]) {
1187                 hns3_rx_queue_release(dev->data->rx_queues[idx]);
1188                 dev->data->rx_queues[idx] = NULL;
1189         }
1190
1191         q_info.idx = idx;
1192         q_info.socket_id = socket_id;
1193         q_info.nb_desc = nb_desc;
1194         q_info.type = "hns3 RX queue";
1195         q_info.ring_name = "rx_ring";
1196         rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
1197         if (rxq == NULL) {
1198                 hns3_err(hw,
1199                          "Failed to alloc mem and reserve DMA mem for rx ring!");
1200                 return -ENOMEM;
1201         }
1202
1203         rxq->hns = hns;
1204         rxq->mb_pool = mp;
1205         if (conf->rx_free_thresh <= 0)
1206                 rxq->rx_free_thresh = DEFAULT_RX_FREE_THRESH;
1207         else
1208                 rxq->rx_free_thresh = conf->rx_free_thresh;
1209         rxq->rx_deferred_start = conf->rx_deferred_start;
1210
1211         rx_entry_len = sizeof(struct hns3_entry) * rxq->nb_rx_desc;
1212         rxq->sw_ring = rte_zmalloc_socket("hns3 RX sw ring", rx_entry_len,
1213                                           RTE_CACHE_LINE_SIZE, socket_id);
1214         if (rxq->sw_ring == NULL) {
1215                 hns3_err(hw, "Failed to allocate memory for rx sw ring!");
1216                 hns3_rx_queue_release(rxq);
1217                 return -ENOMEM;
1218         }
1219
1220         rxq->next_to_use = 0;
1221         rxq->next_to_clean = 0;
1222         rxq->nb_rx_hold = 0;
1223         rxq->pkt_first_seg = NULL;
1224         rxq->pkt_last_seg = NULL;
1225         rxq->port_id = dev->data->port_id;
1226         rxq->configured = true;
1227         rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1228                                 idx * HNS3_TQP_REG_SIZE);
1229         rxq->rx_buf_len = hw->rx_buf_len;
1230         rxq->l2_errors = 0;
1231         rxq->pkt_len_errors = 0;
1232         rxq->l3_csum_erros = 0;
1233         rxq->l4_csum_erros = 0;
1234         rxq->ol3_csum_erros = 0;
1235         rxq->ol4_csum_erros = 0;
1236
1237         rte_spinlock_lock(&hw->lock);
1238         dev->data->rx_queues[idx] = rxq;
1239         rte_spinlock_unlock(&hw->lock);
1240
1241         return 0;
1242 }
1243
1244 static inline uint32_t
1245 rxd_pkt_info_to_pkt_type(uint32_t pkt_info, uint32_t ol_info)
1246 {
1247 #define HNS3_L2TBL_NUM  4
1248 #define HNS3_L3TBL_NUM  16
1249 #define HNS3_L4TBL_NUM  16
1250 #define HNS3_OL3TBL_NUM 16
1251 #define HNS3_OL4TBL_NUM 16
1252         uint32_t pkt_type = 0;
1253         uint32_t l2id, l3id, l4id;
1254         uint32_t ol3id, ol4id;
1255
1256         static const uint32_t l2table[HNS3_L2TBL_NUM] = {
1257                 RTE_PTYPE_L2_ETHER,
1258                 RTE_PTYPE_L2_ETHER_QINQ,
1259                 RTE_PTYPE_L2_ETHER_VLAN,
1260                 RTE_PTYPE_L2_ETHER_VLAN
1261         };
1262
1263         static const uint32_t l3table[HNS3_L3TBL_NUM] = {
1264                 RTE_PTYPE_L3_IPV4,
1265                 RTE_PTYPE_L3_IPV6,
1266                 RTE_PTYPE_L2_ETHER_ARP,
1267                 RTE_PTYPE_L2_ETHER,
1268                 RTE_PTYPE_L3_IPV4_EXT,
1269                 RTE_PTYPE_L3_IPV6_EXT,
1270                 RTE_PTYPE_L2_ETHER_LLDP,
1271                 0, 0, 0, 0, 0, 0, 0, 0, 0
1272         };
1273
1274         static const uint32_t l4table[HNS3_L4TBL_NUM] = {
1275                 RTE_PTYPE_L4_UDP,
1276                 RTE_PTYPE_L4_TCP,
1277                 RTE_PTYPE_TUNNEL_GRE,
1278                 RTE_PTYPE_L4_SCTP,
1279                 RTE_PTYPE_L4_IGMP,
1280                 RTE_PTYPE_L4_ICMP,
1281                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1282         };
1283
1284         static const uint32_t inner_l2table[HNS3_L2TBL_NUM] = {
1285                 RTE_PTYPE_INNER_L2_ETHER,
1286                 RTE_PTYPE_INNER_L2_ETHER_VLAN,
1287                 RTE_PTYPE_INNER_L2_ETHER_QINQ,
1288                 0
1289         };
1290
1291         static const uint32_t inner_l3table[HNS3_L3TBL_NUM] = {
1292                 RTE_PTYPE_INNER_L3_IPV4,
1293                 RTE_PTYPE_INNER_L3_IPV6,
1294                 0,
1295                 RTE_PTYPE_INNER_L2_ETHER,
1296                 RTE_PTYPE_INNER_L3_IPV4_EXT,
1297                 RTE_PTYPE_INNER_L3_IPV6_EXT,
1298                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1299         };
1300
1301         static const uint32_t inner_l4table[HNS3_L4TBL_NUM] = {
1302                 RTE_PTYPE_INNER_L4_UDP,
1303                 RTE_PTYPE_INNER_L4_TCP,
1304                 RTE_PTYPE_TUNNEL_GRE,
1305                 RTE_PTYPE_INNER_L4_SCTP,
1306                 RTE_PTYPE_L4_IGMP,
1307                 RTE_PTYPE_INNER_L4_ICMP,
1308                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1309         };
1310
1311         static const uint32_t ol3table[HNS3_OL3TBL_NUM] = {
1312                 RTE_PTYPE_L3_IPV4,
1313                 RTE_PTYPE_L3_IPV6,
1314                 0, 0,
1315                 RTE_PTYPE_L3_IPV4_EXT,
1316                 RTE_PTYPE_L3_IPV6_EXT,
1317                 0, 0, 0, 0, 0, 0, 0, 0, 0,
1318                 RTE_PTYPE_UNKNOWN
1319         };
1320
1321         static const uint32_t ol4table[HNS3_OL4TBL_NUM] = {
1322                 0,
1323                 RTE_PTYPE_TUNNEL_VXLAN,
1324                 RTE_PTYPE_TUNNEL_NVGRE,
1325                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
1326         };
1327
1328         l2id = hns3_get_field(pkt_info, HNS3_RXD_STRP_TAGP_M,
1329                               HNS3_RXD_STRP_TAGP_S);
1330         l3id = hns3_get_field(pkt_info, HNS3_RXD_L3ID_M, HNS3_RXD_L3ID_S);
1331         l4id = hns3_get_field(pkt_info, HNS3_RXD_L4ID_M, HNS3_RXD_L4ID_S);
1332         ol3id = hns3_get_field(ol_info, HNS3_RXD_OL3ID_M, HNS3_RXD_OL3ID_S);
1333         ol4id = hns3_get_field(ol_info, HNS3_RXD_OL4ID_M, HNS3_RXD_OL4ID_S);
1334
1335         if (ol4table[ol4id])
1336                 pkt_type |= (inner_l2table[l2id] | inner_l3table[l3id] |
1337                              inner_l4table[l4id] | ol3table[ol3id] |
1338                              ol4table[ol4id]);
1339         else
1340                 pkt_type |= (l2table[l2id] | l3table[l3id] | l4table[l4id]);
1341         return pkt_type;
1342 }
1343
1344 const uint32_t *
1345 hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev)
1346 {
1347         static const uint32_t ptypes[] = {
1348                 RTE_PTYPE_L2_ETHER,
1349                 RTE_PTYPE_L2_ETHER_VLAN,
1350                 RTE_PTYPE_L2_ETHER_QINQ,
1351                 RTE_PTYPE_L2_ETHER_LLDP,
1352                 RTE_PTYPE_L2_ETHER_ARP,
1353                 RTE_PTYPE_L3_IPV4,
1354                 RTE_PTYPE_L3_IPV4_EXT,
1355                 RTE_PTYPE_L3_IPV6,
1356                 RTE_PTYPE_L3_IPV6_EXT,
1357                 RTE_PTYPE_L4_IGMP,
1358                 RTE_PTYPE_L4_ICMP,
1359                 RTE_PTYPE_L4_SCTP,
1360                 RTE_PTYPE_L4_TCP,
1361                 RTE_PTYPE_L4_UDP,
1362                 RTE_PTYPE_TUNNEL_GRE,
1363                 RTE_PTYPE_UNKNOWN
1364         };
1365
1366         if (dev->rx_pkt_burst == hns3_recv_pkts)
1367                 return ptypes;
1368
1369         return NULL;
1370 }
1371
1372 static void
1373 hns3_clean_rx_buffers(struct hns3_rx_queue *rxq, int count)
1374 {
1375         rxq->next_to_use += count;
1376         if (rxq->next_to_use >= rxq->nb_rx_desc)
1377                 rxq->next_to_use -= rxq->nb_rx_desc;
1378
1379         hns3_write_dev(rxq, HNS3_RING_RX_HEAD_REG, count);
1380 }
1381
1382 static int
1383 hns3_handle_bdinfo(struct hns3_rx_queue *rxq, struct rte_mbuf *rxm,
1384                    uint32_t bd_base_info, uint32_t l234_info,
1385                    uint32_t *cksum_err)
1386 {
1387         uint32_t tmp = 0;
1388
1389         if (unlikely(l234_info & BIT(HNS3_RXD_L2E_B))) {
1390                 rxq->l2_errors++;
1391                 return -EINVAL;
1392         }
1393
1394         if (unlikely(rxm->pkt_len == 0 ||
1395                 (l234_info & BIT(HNS3_RXD_TRUNCAT_B)))) {
1396                 rxq->pkt_len_errors++;
1397                 return -EINVAL;
1398         }
1399
1400         if (bd_base_info & BIT(HNS3_RXD_L3L4P_B)) {
1401                 if (unlikely(l234_info & BIT(HNS3_RXD_L3E_B))) {
1402                         rxm->ol_flags |= PKT_RX_IP_CKSUM_BAD;
1403                         rxq->l3_csum_erros++;
1404                         tmp |= HNS3_L3_CKSUM_ERR;
1405                 }
1406
1407                 if (unlikely(l234_info & BIT(HNS3_RXD_L4E_B))) {
1408                         rxm->ol_flags |= PKT_RX_L4_CKSUM_BAD;
1409                         rxq->l4_csum_erros++;
1410                         tmp |= HNS3_L4_CKSUM_ERR;
1411                 }
1412
1413                 if (unlikely(l234_info & BIT(HNS3_RXD_OL3E_B))) {
1414                         rxq->ol3_csum_erros++;
1415                         tmp |= HNS3_OUTER_L3_CKSUM_ERR;
1416                 }
1417
1418                 if (unlikely(l234_info & BIT(HNS3_RXD_OL4E_B))) {
1419                         rxm->ol_flags |= PKT_RX_OUTER_L4_CKSUM_BAD;
1420                         rxq->ol4_csum_erros++;
1421                         tmp |= HNS3_OUTER_L4_CKSUM_ERR;
1422                 }
1423         }
1424         *cksum_err = tmp;
1425
1426         return 0;
1427 }
1428
1429 static void
1430 hns3_rx_set_cksum_flag(struct rte_mbuf *rxm, uint64_t packet_type,
1431                        const uint32_t cksum_err)
1432 {
1433         if (unlikely((packet_type & RTE_PTYPE_TUNNEL_MASK))) {
1434                 if (likely(packet_type & RTE_PTYPE_INNER_L3_MASK) &&
1435                     (cksum_err & HNS3_L3_CKSUM_ERR) == 0)
1436                         rxm->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
1437                 if (likely(packet_type & RTE_PTYPE_INNER_L4_MASK) &&
1438                     (cksum_err & HNS3_L4_CKSUM_ERR) == 0)
1439                         rxm->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1440                 if (likely(packet_type & RTE_PTYPE_L4_MASK) &&
1441                     (cksum_err & HNS3_OUTER_L4_CKSUM_ERR) == 0)
1442                         rxm->ol_flags |= PKT_RX_OUTER_L4_CKSUM_GOOD;
1443         } else {
1444                 if (likely(packet_type & RTE_PTYPE_L3_MASK) &&
1445                     (cksum_err & HNS3_L3_CKSUM_ERR) == 0)
1446                         rxm->ol_flags |= PKT_RX_IP_CKSUM_GOOD;
1447                 if (likely(packet_type & RTE_PTYPE_L4_MASK) &&
1448                     (cksum_err & HNS3_L4_CKSUM_ERR) == 0)
1449                         rxm->ol_flags |= PKT_RX_L4_CKSUM_GOOD;
1450         }
1451 }
1452
1453 static inline void
1454 hns3_rxd_to_vlan_tci(struct rte_eth_dev *dev, struct rte_mbuf *mb,
1455                      uint32_t l234_info, const struct hns3_desc *rxd)
1456 {
1457 #define HNS3_STRP_STATUS_NUM            0x4
1458
1459 #define HNS3_NO_STRP_VLAN_VLD           0x0
1460 #define HNS3_INNER_STRP_VLAN_VLD        0x1
1461 #define HNS3_OUTER_STRP_VLAN_VLD        0x2
1462         struct hns3_adapter *hns = dev->data->dev_private;
1463         struct hns3_hw *hw = &hns->hw;
1464         uint32_t strip_status;
1465         uint32_t report_mode;
1466
1467         /*
1468          * Since HW limitation, the vlan tag will always be inserted into RX
1469          * descriptor when strip the tag from packet, driver needs to determine
1470          * reporting which tag to mbuf according to the PVID configuration
1471          * and vlan striped status.
1472          */
1473         static const uint32_t report_type[][HNS3_STRP_STATUS_NUM] = {
1474                 {
1475                         HNS3_NO_STRP_VLAN_VLD,
1476                         HNS3_OUTER_STRP_VLAN_VLD,
1477                         HNS3_INNER_STRP_VLAN_VLD,
1478                         HNS3_OUTER_STRP_VLAN_VLD
1479                 },
1480                 {
1481                         HNS3_NO_STRP_VLAN_VLD,
1482                         HNS3_NO_STRP_VLAN_VLD,
1483                         HNS3_NO_STRP_VLAN_VLD,
1484                         HNS3_INNER_STRP_VLAN_VLD
1485                 }
1486         };
1487         strip_status = hns3_get_field(l234_info, HNS3_RXD_STRP_TAGP_M,
1488                                       HNS3_RXD_STRP_TAGP_S);
1489         report_mode = report_type[hw->port_base_vlan_cfg.state][strip_status];
1490         switch (report_mode) {
1491         case HNS3_NO_STRP_VLAN_VLD:
1492                 mb->vlan_tci = 0;
1493                 return;
1494         case HNS3_INNER_STRP_VLAN_VLD:
1495                 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
1496                 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.vlan_tag);
1497                 return;
1498         case HNS3_OUTER_STRP_VLAN_VLD:
1499                 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
1500                 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.ot_vlan_tag);
1501                 return;
1502         }
1503 }
1504
1505 uint16_t
1506 hns3_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1507 {
1508         volatile struct hns3_desc *rx_ring;  /* RX ring (desc) */
1509         volatile struct hns3_desc *rxdp;     /* pointer of the current desc */
1510         struct hns3_rx_queue *rxq;      /* RX queue */
1511         struct hns3_entry *sw_ring;
1512         struct hns3_entry *rxe;
1513         struct rte_mbuf *first_seg;
1514         struct rte_mbuf *last_seg;
1515         struct hns3_desc rxd;
1516         struct rte_mbuf *nmb;           /* pointer of the new mbuf */
1517         struct rte_mbuf *rxm;
1518         struct rte_eth_dev *dev;
1519         uint32_t bd_base_info;
1520         uint32_t cksum_err;
1521         uint32_t l234_info;
1522         uint32_t gro_size;
1523         uint32_t ol_info;
1524         uint64_t dma_addr;
1525         uint16_t data_len;
1526         uint16_t nb_rx_bd;
1527         uint16_t pkt_len;
1528         uint16_t nb_rx;
1529         uint16_t rx_id;
1530         int ret;
1531
1532         nb_rx = 0;
1533         nb_rx_bd = 0;
1534         rxq = rx_queue;
1535         dev = &rte_eth_devices[rxq->port_id];
1536
1537         rx_id = rxq->next_to_clean;
1538         rx_ring = rxq->rx_ring;
1539         first_seg = rxq->pkt_first_seg;
1540         last_seg = rxq->pkt_last_seg;
1541         sw_ring = rxq->sw_ring;
1542
1543         while (nb_rx < nb_pkts) {
1544                 rxdp = &rx_ring[rx_id];
1545                 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
1546                 if (unlikely(!hns3_get_bit(bd_base_info, HNS3_RXD_VLD_B)))
1547                         break;
1548                 /*
1549                  * The interactive process between software and hardware of
1550                  * receiving a new packet in hns3 network engine:
1551                  * 1. Hardware network engine firstly writes the packet content
1552                  *    to the memory pointed by the 'addr' field of the Rx Buffer
1553                  *    Descriptor, secondly fills the result of parsing the
1554                  *    packet include the valid field into the Rx Buffer
1555                  *    Descriptor in one write operation.
1556                  * 2. Driver reads the Rx BD's valid field in the loop to check
1557                  *    whether it's valid, if valid then assign a new address to
1558                  *    the addr field, clear the valid field, get the other
1559                  *    information of the packet by parsing Rx BD's other fields,
1560                  *    finally write back the number of Rx BDs processed by the
1561                  *    driver to the HNS3_RING_RX_HEAD_REG register to inform
1562                  *    hardware.
1563                  * In the above process, the ordering is very important. We must
1564                  * make sure that CPU read Rx BD's other fields only after the
1565                  * Rx BD is valid.
1566                  *
1567                  * There are two type of re-ordering: compiler re-ordering and
1568                  * CPU re-ordering under the ARMv8 architecture.
1569                  * 1. we use volatile to deal with compiler re-ordering, so you
1570                  *    can see that rx_ring/rxdp defined with volatile.
1571                  * 2. we commonly use memory barrier to deal with CPU
1572                  *    re-ordering, but the cost is high.
1573                  *
1574                  * In order to solve the high cost of using memory barrier, we
1575                  * use the data dependency order under the ARMv8 architecture,
1576                  * for example:
1577                  *      instr01: load A
1578                  *      instr02: load B <- A
1579                  * the instr02 will always execute after instr01.
1580                  *
1581                  * To construct the data dependency ordering, we use the
1582                  * following assignment:
1583                  *      rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
1584                  *                 (1u<<HNS3_RXD_VLD_B)]
1585                  * Using gcc compiler under the ARMv8 architecture, the related
1586                  * assembly code example as follows:
1587                  * note: (1u << HNS3_RXD_VLD_B) equal 0x10
1588                  *      instr01: ldr w26, [x22, #28]  --read bd_base_info
1589                  *      instr02: and w0, w26, #0x10   --calc bd_base_info & 0x10
1590                  *      instr03: sub w0, w0, #0x10    --calc (bd_base_info &
1591                  *                                            0x10) - 0x10
1592                  *      instr04: add x0, x22, x0, lsl #5 --calc copy source addr
1593                  *      instr05: ldp x2, x3, [x0]
1594                  *      instr06: stp x2, x3, [x29, #256] --copy BD's [0 ~ 15]B
1595                  *      instr07: ldp x4, x5, [x0, #16]
1596                  *      instr08: stp x4, x5, [x29, #272] --copy BD's [16 ~ 31]B
1597                  * the instr05~08 depend on x0's value, x0 depent on w26's
1598                  * value, the w26 is the bd_base_info, this form the data
1599                  * dependency ordering.
1600                  * note: if BD is valid, (bd_base_info & (1u<<HNS3_RXD_VLD_B)) -
1601                  *       (1u<<HNS3_RXD_VLD_B) will always zero, so the
1602                  *       assignment is correct.
1603                  *
1604                  * So we use the data dependency ordering instead of memory
1605                  * barrier to improve receive performance.
1606                  */
1607                 rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
1608                            (1u << HNS3_RXD_VLD_B)];
1609
1610                 nmb = rte_mbuf_raw_alloc(rxq->mb_pool);
1611                 if (unlikely(nmb == NULL)) {
1612                         dev->data->rx_mbuf_alloc_failed++;
1613                         break;
1614                 }
1615
1616                 nb_rx_bd++;
1617                 rxe = &sw_ring[rx_id];
1618                 rx_id++;
1619                 if (unlikely(rx_id == rxq->nb_rx_desc))
1620                         rx_id = 0;
1621
1622                 rte_prefetch0(sw_ring[rx_id].mbuf);
1623                 if ((rx_id & 0x3) == 0) {
1624                         rte_prefetch0(&rx_ring[rx_id]);
1625                         rte_prefetch0(&sw_ring[rx_id]);
1626                 }
1627
1628                 rxm = rxe->mbuf;
1629                 rxe->mbuf = nmb;
1630
1631                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1632                 rxdp->rx.bd_base_info = 0;
1633                 rxdp->addr = dma_addr;
1634
1635                 /* Load remained descriptor data and extract necessary fields */
1636                 data_len = (uint16_t)(rte_le_to_cpu_16(rxd.rx.size));
1637                 l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
1638                 ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
1639
1640                 if (first_seg == NULL) {
1641                         first_seg = rxm;
1642                         first_seg->nb_segs = 1;
1643                 } else {
1644                         first_seg->nb_segs++;
1645                         last_seg->next = rxm;
1646                 }
1647
1648                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1649                 rxm->data_len = data_len;
1650
1651                 if (!hns3_get_bit(bd_base_info, HNS3_RXD_FE_B)) {
1652                         last_seg = rxm;
1653                         continue;
1654                 }
1655
1656                 /* The last buffer of the received packet */
1657                 pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.rx.pkt_len));
1658                 first_seg->pkt_len = pkt_len;
1659                 first_seg->port = rxq->port_id;
1660                 first_seg->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
1661                 first_seg->ol_flags = PKT_RX_RSS_HASH;
1662                 if (unlikely(hns3_get_bit(bd_base_info, HNS3_RXD_LUM_B))) {
1663                         first_seg->hash.fdir.hi =
1664                                 rte_le_to_cpu_32(rxd.rx.fd_id);
1665                         first_seg->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
1666                 }
1667                 rxm->next = NULL;
1668
1669                 gro_size = hns3_get_field(bd_base_info, HNS3_RXD_GRO_SIZE_M,
1670                                           HNS3_RXD_GRO_SIZE_S);
1671                 if (gro_size != 0) {
1672                         first_seg->ol_flags |= PKT_RX_LRO;
1673                         first_seg->tso_segsz = gro_size;
1674                 }
1675
1676                 ret = hns3_handle_bdinfo(rxq, first_seg, bd_base_info,
1677                                          l234_info, &cksum_err);
1678                 if (unlikely(ret))
1679                         goto pkt_err;
1680
1681                 first_seg->packet_type = rxd_pkt_info_to_pkt_type(l234_info,
1682                                                                   ol_info);
1683
1684                 if (bd_base_info & BIT(HNS3_RXD_L3L4P_B))
1685                         hns3_rx_set_cksum_flag(first_seg,
1686                                                first_seg->packet_type,
1687                                                cksum_err);
1688                 hns3_rxd_to_vlan_tci(dev, first_seg, l234_info, &rxd);
1689
1690                 rx_pkts[nb_rx++] = first_seg;
1691                 first_seg = NULL;
1692                 continue;
1693 pkt_err:
1694                 rte_pktmbuf_free(first_seg);
1695                 first_seg = NULL;
1696         }
1697
1698         rxq->next_to_clean = rx_id;
1699         rxq->pkt_first_seg = first_seg;
1700         rxq->pkt_last_seg = last_seg;
1701
1702         nb_rx_bd = nb_rx_bd + rxq->nb_rx_hold;
1703         if (nb_rx_bd > rxq->rx_free_thresh) {
1704                 hns3_clean_rx_buffers(rxq, nb_rx_bd);
1705                 nb_rx_bd = 0;
1706         }
1707         rxq->nb_rx_hold = nb_rx_bd;
1708
1709         return nb_rx;
1710 }
1711
1712 int
1713 hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
1714                     unsigned int socket_id, const struct rte_eth_txconf *conf)
1715 {
1716         struct hns3_adapter *hns = dev->data->dev_private;
1717         struct hns3_hw *hw = &hns->hw;
1718         struct hns3_queue_info q_info;
1719         struct hns3_tx_queue *txq;
1720         int tx_entry_len;
1721
1722         if (dev->data->dev_started) {
1723                 hns3_err(hw, "tx_queue_setup after dev_start no supported");
1724                 return -EINVAL;
1725         }
1726
1727         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
1728             nb_desc % HNS3_ALIGN_RING_DESC) {
1729                 hns3_err(hw, "Number (%u) of tx descriptors is invalid",
1730                             nb_desc);
1731                 return -EINVAL;
1732         }
1733
1734         if (dev->data->tx_queues[idx] != NULL) {
1735                 hns3_tx_queue_release(dev->data->tx_queues[idx]);
1736                 dev->data->tx_queues[idx] = NULL;
1737         }
1738
1739         q_info.idx = idx;
1740         q_info.socket_id = socket_id;
1741         q_info.nb_desc = nb_desc;
1742         q_info.type = "hns3 TX queue";
1743         q_info.ring_name = "tx_ring";
1744         txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
1745         if (txq == NULL) {
1746                 hns3_err(hw,
1747                          "Failed to alloc mem and reserve DMA mem for tx ring!");
1748                 return -ENOMEM;
1749         }
1750
1751         txq->tx_deferred_start = conf->tx_deferred_start;
1752         tx_entry_len = sizeof(struct hns3_entry) * txq->nb_tx_desc;
1753         txq->sw_ring = rte_zmalloc_socket("hns3 TX sw ring", tx_entry_len,
1754                                           RTE_CACHE_LINE_SIZE, socket_id);
1755         if (txq->sw_ring == NULL) {
1756                 hns3_err(hw, "Failed to allocate memory for tx sw ring!");
1757                 hns3_tx_queue_release(txq);
1758                 return -ENOMEM;
1759         }
1760
1761         txq->hns = hns;
1762         txq->next_to_use = 0;
1763         txq->next_to_clean = 0;
1764         txq->tx_bd_ready = txq->nb_tx_desc - 1;
1765         txq->port_id = dev->data->port_id;
1766         txq->configured = true;
1767         txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1768                                 idx * HNS3_TQP_REG_SIZE);
1769         txq->over_length_pkt_cnt = 0;
1770         txq->exceed_limit_bd_pkt_cnt = 0;
1771         txq->exceed_limit_bd_reassem_fail = 0;
1772         txq->unsupported_tunnel_pkt_cnt = 0;
1773         txq->queue_full_cnt = 0;
1774         txq->pkt_padding_fail_cnt = 0;
1775         rte_spinlock_lock(&hw->lock);
1776         dev->data->tx_queues[idx] = txq;
1777         rte_spinlock_unlock(&hw->lock);
1778
1779         return 0;
1780 }
1781
1782 static inline void
1783 hns3_queue_xmit(struct hns3_tx_queue *txq, uint32_t buf_num)
1784 {
1785         hns3_write_dev(txq, HNS3_RING_TX_TAIL_REG, buf_num);
1786 }
1787
1788 static void
1789 hns3_tx_free_useless_buffer(struct hns3_tx_queue *txq)
1790 {
1791         uint16_t tx_next_clean = txq->next_to_clean;
1792         uint16_t tx_next_use   = txq->next_to_use;
1793         uint16_t tx_bd_ready   = txq->tx_bd_ready;
1794         uint16_t tx_bd_max     = txq->nb_tx_desc;
1795         struct hns3_entry *tx_bak_pkt = &txq->sw_ring[tx_next_clean];
1796         struct hns3_desc *desc = &txq->tx_ring[tx_next_clean];
1797         struct rte_mbuf *mbuf;
1798
1799         while ((!hns3_get_bit(desc->tx.tp_fe_sc_vld_ra_ri, HNS3_TXD_VLD_B)) &&
1800                 tx_next_use != tx_next_clean) {
1801                 mbuf = tx_bak_pkt->mbuf;
1802                 if (mbuf) {
1803                         rte_pktmbuf_free_seg(mbuf);
1804                         tx_bak_pkt->mbuf = NULL;
1805                 }
1806
1807                 desc++;
1808                 tx_bak_pkt++;
1809                 tx_next_clean++;
1810                 tx_bd_ready++;
1811
1812                 if (tx_next_clean >= tx_bd_max) {
1813                         tx_next_clean = 0;
1814                         desc = txq->tx_ring;
1815                         tx_bak_pkt = txq->sw_ring;
1816                 }
1817         }
1818
1819         txq->next_to_clean = tx_next_clean;
1820         txq->tx_bd_ready   = tx_bd_ready;
1821 }
1822
1823 static int
1824 hns3_tso_proc_tunnel(struct hns3_desc *desc, uint64_t ol_flags,
1825                      struct rte_mbuf *rxm, uint8_t *l2_len)
1826 {
1827         uint64_t tun_flags;
1828         uint8_t ol4_len;
1829         uint32_t otmp;
1830
1831         tun_flags = ol_flags & PKT_TX_TUNNEL_MASK;
1832         if (tun_flags == 0)
1833                 return 0;
1834
1835         otmp = rte_le_to_cpu_32(desc->tx.ol_type_vlan_len_msec);
1836         switch (tun_flags) {
1837         case PKT_TX_TUNNEL_GENEVE:
1838         case PKT_TX_TUNNEL_VXLAN:
1839                 *l2_len = rxm->l2_len - RTE_ETHER_VXLAN_HLEN;
1840                 break;
1841         case PKT_TX_TUNNEL_GRE:
1842                 /*
1843                  * OL4 header size, defined in 4 Bytes, it contains outer
1844                  * L4(GRE) length and tunneling length.
1845                  */
1846                 ol4_len = hns3_get_field(otmp, HNS3_TXD_L4LEN_M,
1847                                          HNS3_TXD_L4LEN_S);
1848                 *l2_len = rxm->l2_len - (ol4_len << HNS3_L4_LEN_UNIT);
1849                 break;
1850         default:
1851                 /* For non UDP / GRE tunneling, drop the tunnel packet */
1852                 return -EINVAL;
1853         }
1854         hns3_set_field(otmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
1855                        rxm->outer_l2_len >> HNS3_L2_LEN_UNIT);
1856         desc->tx.ol_type_vlan_len_msec = rte_cpu_to_le_32(otmp);
1857
1858         return 0;
1859 }
1860
1861 int
1862 hns3_config_gro(struct hns3_hw *hw, bool en)
1863 {
1864         struct hns3_cfg_gro_status_cmd *req;
1865         struct hns3_cmd_desc desc;
1866         int ret;
1867
1868         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GRO_GENERIC_CONFIG, false);
1869         req = (struct hns3_cfg_gro_status_cmd *)desc.data;
1870
1871         req->gro_en = rte_cpu_to_le_16(en ? 1 : 0);
1872
1873         ret = hns3_cmd_send(hw, &desc, 1);
1874         if (ret)
1875                 hns3_err(hw, "%s hardware GRO failed, ret = %d",
1876                          en ? "enable" : "disable", ret);
1877
1878         return ret;
1879 }
1880
1881 int
1882 hns3_restore_gro_conf(struct hns3_hw *hw)
1883 {
1884         uint64_t offloads;
1885         bool gro_en;
1886         int ret;
1887
1888         offloads = hw->data->dev_conf.rxmode.offloads;
1889         gro_en = offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
1890         ret = hns3_config_gro(hw, gro_en);
1891         if (ret)
1892                 hns3_err(hw, "restore hardware GRO to %s failed, ret = %d",
1893                          gro_en ? "enabled" : "disabled", ret);
1894
1895         return ret;
1896 }
1897
1898 static inline bool
1899 hns3_pkt_is_tso(struct rte_mbuf *m)
1900 {
1901         return (m->tso_segsz != 0 && m->ol_flags & PKT_TX_TCP_SEG);
1902 }
1903
1904 static void
1905 hns3_set_tso(struct hns3_desc *desc,
1906              uint64_t ol_flags, struct rte_mbuf *rxm)
1907 {
1908         uint32_t paylen, hdr_len;
1909         uint32_t tmp;
1910         uint8_t l2_len = rxm->l2_len;
1911
1912         if (!hns3_pkt_is_tso(rxm))
1913                 return;
1914
1915         if (hns3_tso_proc_tunnel(desc, ol_flags, rxm, &l2_len))
1916                 return;
1917
1918         hdr_len = rxm->l2_len + rxm->l3_len + rxm->l4_len;
1919         hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
1920                     rxm->outer_l2_len + rxm->outer_l3_len : 0;
1921         paylen = rxm->pkt_len - hdr_len;
1922         if (paylen <= rxm->tso_segsz)
1923                 return;
1924
1925         tmp = rte_le_to_cpu_32(desc->tx.type_cs_vlan_tso_len);
1926         hns3_set_bit(tmp, HNS3_TXD_TSO_B, 1);
1927         hns3_set_bit(tmp, HNS3_TXD_L3CS_B, 1);
1928         hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S, HNS3_L4T_TCP);
1929         hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
1930         hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
1931                        sizeof(struct rte_tcp_hdr) >> HNS3_L4_LEN_UNIT);
1932         hns3_set_field(tmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
1933                        l2_len >> HNS3_L2_LEN_UNIT);
1934         desc->tx.type_cs_vlan_tso_len = rte_cpu_to_le_32(tmp);
1935         desc->tx.mss = rte_cpu_to_le_16(rxm->tso_segsz);
1936 }
1937
1938 static void
1939 fill_desc(struct hns3_tx_queue *txq, uint16_t tx_desc_id, struct rte_mbuf *rxm,
1940           bool first, int offset)
1941 {
1942         struct hns3_desc *tx_ring = txq->tx_ring;
1943         struct hns3_desc *desc = &tx_ring[tx_desc_id];
1944         uint8_t frag_end = rxm->next == NULL ? 1 : 0;
1945         uint64_t ol_flags = rxm->ol_flags;
1946         uint16_t size = rxm->data_len;
1947         uint16_t rrcfv = 0;
1948         uint32_t hdr_len;
1949         uint32_t paylen;
1950         uint32_t tmp;
1951
1952         desc->addr = rte_mbuf_data_iova(rxm) + offset;
1953         desc->tx.send_size = rte_cpu_to_le_16(size);
1954         hns3_set_bit(rrcfv, HNS3_TXD_VLD_B, 1);
1955
1956         if (first) {
1957                 hdr_len = rxm->l2_len + rxm->l3_len + rxm->l4_len;
1958                 hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
1959                            rxm->outer_l2_len + rxm->outer_l3_len : 0;
1960                 paylen = rxm->pkt_len - hdr_len;
1961                 desc->tx.paylen = rte_cpu_to_le_32(paylen);
1962                 hns3_set_tso(desc, ol_flags, rxm);
1963         }
1964
1965         hns3_set_bit(rrcfv, HNS3_TXD_FE_B, frag_end);
1966         desc->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(rrcfv);
1967
1968         if (frag_end) {
1969                 if (ol_flags & (PKT_TX_VLAN_PKT | PKT_TX_QINQ_PKT)) {
1970                         tmp = rte_le_to_cpu_32(desc->tx.type_cs_vlan_tso_len);
1971                         hns3_set_bit(tmp, HNS3_TXD_VLAN_B, 1);
1972                         desc->tx.type_cs_vlan_tso_len = rte_cpu_to_le_32(tmp);
1973                         desc->tx.vlan_tag = rte_cpu_to_le_16(rxm->vlan_tci);
1974                 }
1975
1976                 if (ol_flags & PKT_TX_QINQ_PKT) {
1977                         tmp = rte_le_to_cpu_32(desc->tx.ol_type_vlan_len_msec);
1978                         hns3_set_bit(tmp, HNS3_TXD_OVLAN_B, 1);
1979                         desc->tx.ol_type_vlan_len_msec = rte_cpu_to_le_32(tmp);
1980                         desc->tx.outer_vlan_tag =
1981                                 rte_cpu_to_le_16(rxm->vlan_tci_outer);
1982                 }
1983         }
1984 }
1985
1986 static int
1987 hns3_tx_alloc_mbufs(struct hns3_tx_queue *txq, struct rte_mempool *mb_pool,
1988                     uint16_t nb_new_buf, struct rte_mbuf **alloc_mbuf)
1989 {
1990         struct rte_mbuf *new_mbuf = NULL;
1991         struct rte_eth_dev *dev;
1992         struct rte_mbuf *temp;
1993         struct hns3_hw *hw;
1994         uint16_t i;
1995
1996         /* Allocate enough mbufs */
1997         for (i = 0; i < nb_new_buf; i++) {
1998                 temp = rte_pktmbuf_alloc(mb_pool);
1999                 if (unlikely(temp == NULL)) {
2000                         dev = &rte_eth_devices[txq->port_id];
2001                         hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2002                         hns3_err(hw, "Failed to alloc TX mbuf port_id=%d,"
2003                                      "queue_id=%d in reassemble tx pkts.",
2004                                      txq->port_id, txq->queue_id);
2005                         rte_pktmbuf_free(new_mbuf);
2006                         return -ENOMEM;
2007                 }
2008                 temp->next = new_mbuf;
2009                 new_mbuf = temp;
2010         }
2011
2012         if (new_mbuf == NULL)
2013                 return -ENOMEM;
2014
2015         new_mbuf->nb_segs = nb_new_buf;
2016         *alloc_mbuf = new_mbuf;
2017
2018         return 0;
2019 }
2020
2021 static int
2022 hns3_reassemble_tx_pkts(void *tx_queue, struct rte_mbuf *tx_pkt,
2023                         struct rte_mbuf **new_pkt)
2024 {
2025         struct hns3_tx_queue *txq = tx_queue;
2026         struct rte_mempool *mb_pool;
2027         struct rte_mbuf *new_mbuf;
2028         struct rte_mbuf *temp_new;
2029         struct rte_mbuf *temp;
2030         uint16_t last_buf_len;
2031         uint16_t nb_new_buf;
2032         uint16_t buf_size;
2033         uint16_t buf_len;
2034         uint16_t len_s;
2035         uint16_t len_d;
2036         uint16_t len;
2037         uint16_t i;
2038         int ret;
2039         char *s;
2040         char *d;
2041
2042         mb_pool = tx_pkt->pool;
2043         buf_size = tx_pkt->buf_len - RTE_PKTMBUF_HEADROOM;
2044         nb_new_buf = (tx_pkt->pkt_len - 1) / buf_size + 1;
2045
2046         last_buf_len = tx_pkt->pkt_len % buf_size;
2047         if (last_buf_len == 0)
2048                 last_buf_len = buf_size;
2049
2050         /* Allocate enough mbufs */
2051         ret = hns3_tx_alloc_mbufs(txq, mb_pool, nb_new_buf, &new_mbuf);
2052         if (ret)
2053                 return ret;
2054
2055         /* Copy the original packet content to the new mbufs */
2056         temp = tx_pkt;
2057         s = rte_pktmbuf_mtod(temp, char *);
2058         len_s = temp->data_len;
2059         temp_new = new_mbuf;
2060         for (i = 0; i < nb_new_buf; i++) {
2061                 d = rte_pktmbuf_mtod(temp_new, char *);
2062                 if (i < nb_new_buf - 1)
2063                         buf_len = buf_size;
2064                 else
2065                         buf_len = last_buf_len;
2066                 len_d = buf_len;
2067
2068                 while (len_d) {
2069                         len = RTE_MIN(len_s, len_d);
2070                         memcpy(d, s, len);
2071                         s = s + len;
2072                         d = d + len;
2073                         len_d = len_d - len;
2074                         len_s = len_s - len;
2075
2076                         if (len_s == 0) {
2077                                 temp = temp->next;
2078                                 if (temp == NULL)
2079                                         break;
2080                                 s = rte_pktmbuf_mtod(temp, char *);
2081                                 len_s = temp->data_len;
2082                         }
2083                 }
2084
2085                 temp_new->data_len = buf_len;
2086                 temp_new = temp_new->next;
2087         }
2088
2089         /* free original mbufs */
2090         rte_pktmbuf_free(tx_pkt);
2091
2092         *new_pkt = new_mbuf;
2093
2094         return 0;
2095 }
2096
2097 static void
2098 hns3_parse_outer_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec)
2099 {
2100         uint32_t tmp = *ol_type_vlan_len_msec;
2101
2102         /* (outer) IP header type */
2103         if (ol_flags & PKT_TX_OUTER_IPV4) {
2104                 /* OL3 header size, defined in 4 bytes */
2105                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2106                                sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
2107                 if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
2108                         hns3_set_field(tmp, HNS3_TXD_OL3T_M,
2109                                        HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_CSUM);
2110                 else
2111                         hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
2112                                        HNS3_OL3T_IPV4_NO_CSUM);
2113         } else if (ol_flags & PKT_TX_OUTER_IPV6) {
2114                 hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
2115                                HNS3_OL3T_IPV6);
2116                 /* OL3 header size, defined in 4 bytes */
2117                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2118                                sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
2119         }
2120
2121         *ol_type_vlan_len_msec = tmp;
2122 }
2123
2124 static int
2125 hns3_parse_inner_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec,
2126                         struct rte_net_hdr_lens *hdr_lens)
2127 {
2128         uint32_t tmp = *ol_type_vlan_len_msec;
2129         uint8_t l4_len;
2130
2131         /* OL2 header size, defined in 2 bytes */
2132         hns3_set_field(tmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2133                        sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
2134
2135         /* L4TUNT: L4 Tunneling Type */
2136         switch (ol_flags & PKT_TX_TUNNEL_MASK) {
2137         case PKT_TX_TUNNEL_GENEVE:
2138         case PKT_TX_TUNNEL_VXLAN:
2139                 /* MAC in UDP tunnelling packet, include VxLAN */
2140                 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
2141                                HNS3_TUN_MAC_IN_UDP);
2142                 /*
2143                  * OL4 header size, defined in 4 Bytes, it contains outer
2144                  * L4(UDP) length and tunneling length.
2145                  */
2146                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2147                                (uint8_t)RTE_ETHER_VXLAN_HLEN >>
2148                                HNS3_L4_LEN_UNIT);
2149                 break;
2150         case PKT_TX_TUNNEL_GRE:
2151                 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
2152                                HNS3_TUN_NVGRE);
2153                 /*
2154                  * OL4 header size, defined in 4 Bytes, it contains outer
2155                  * L4(GRE) length and tunneling length.
2156                  */
2157                 l4_len = hdr_lens->l4_len + hdr_lens->tunnel_len;
2158                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2159                                l4_len >> HNS3_L4_LEN_UNIT);
2160                 break;
2161         default:
2162                 /* For non UDP / GRE tunneling, drop the tunnel packet */
2163                 return -EINVAL;
2164         }
2165
2166         *ol_type_vlan_len_msec = tmp;
2167
2168         return 0;
2169 }
2170
2171 static int
2172 hns3_parse_tunneling_params(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
2173                             uint64_t ol_flags,
2174                             struct rte_net_hdr_lens *hdr_lens)
2175 {
2176         struct hns3_desc *tx_ring = txq->tx_ring;
2177         struct hns3_desc *desc = &tx_ring[tx_desc_id];
2178         uint32_t value = 0;
2179         int ret;
2180
2181         hns3_parse_outer_params(ol_flags, &value);
2182         ret = hns3_parse_inner_params(ol_flags, &value, hdr_lens);
2183         if (ret)
2184                 return -EINVAL;
2185
2186         desc->tx.ol_type_vlan_len_msec |= rte_cpu_to_le_32(value);
2187
2188         return 0;
2189 }
2190
2191 static void
2192 hns3_parse_l3_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
2193 {
2194         uint32_t tmp;
2195
2196         /* Enable L3 checksum offloads */
2197         if (ol_flags & PKT_TX_IPV4) {
2198                 tmp = *type_cs_vlan_tso_len;
2199                 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
2200                                HNS3_L3T_IPV4);
2201                 /* inner(/normal) L3 header size, defined in 4 bytes */
2202                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2203                                sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
2204                 if (ol_flags & PKT_TX_IP_CKSUM)
2205                         hns3_set_bit(tmp, HNS3_TXD_L3CS_B, 1);
2206                 *type_cs_vlan_tso_len = tmp;
2207         } else if (ol_flags & PKT_TX_IPV6) {
2208                 tmp = *type_cs_vlan_tso_len;
2209                 /* L3T, IPv6 don't do checksum */
2210                 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
2211                                HNS3_L3T_IPV6);
2212                 /* inner(/normal) L3 header size, defined in 4 bytes */
2213                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2214                                sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
2215                 *type_cs_vlan_tso_len = tmp;
2216         }
2217 }
2218
2219 static void
2220 hns3_parse_l4_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
2221 {
2222         uint32_t tmp;
2223
2224         /* Enable L4 checksum offloads */
2225         switch (ol_flags & PKT_TX_L4_MASK) {
2226         case PKT_TX_TCP_CKSUM:
2227                 tmp = *type_cs_vlan_tso_len;
2228                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
2229                                HNS3_L4T_TCP);
2230                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2231                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2232                                sizeof(struct rte_tcp_hdr) >> HNS3_L4_LEN_UNIT);
2233                 *type_cs_vlan_tso_len = tmp;
2234                 break;
2235         case PKT_TX_UDP_CKSUM:
2236                 tmp = *type_cs_vlan_tso_len;
2237                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
2238                                HNS3_L4T_UDP);
2239                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2240                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2241                                sizeof(struct rte_udp_hdr) >> HNS3_L4_LEN_UNIT);
2242                 *type_cs_vlan_tso_len = tmp;
2243                 break;
2244         case PKT_TX_SCTP_CKSUM:
2245                 tmp = *type_cs_vlan_tso_len;
2246                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
2247                                HNS3_L4T_SCTP);
2248                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2249                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2250                                sizeof(struct rte_sctp_hdr) >> HNS3_L4_LEN_UNIT);
2251                 *type_cs_vlan_tso_len = tmp;
2252                 break;
2253         default:
2254                 break;
2255         }
2256 }
2257
2258 static void
2259 hns3_txd_enable_checksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
2260                          uint64_t ol_flags)
2261 {
2262         struct hns3_desc *tx_ring = txq->tx_ring;
2263         struct hns3_desc *desc = &tx_ring[tx_desc_id];
2264         uint32_t value = 0;
2265
2266         /* inner(/normal) L2 header size, defined in 2 bytes */
2267         hns3_set_field(value, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2268                        sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
2269
2270         hns3_parse_l3_cksum_params(ol_flags, &value);
2271         hns3_parse_l4_cksum_params(ol_flags, &value);
2272
2273         desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(value);
2274 }
2275
2276 static bool
2277 hns3_pkt_need_linearized(struct rte_mbuf *tx_pkts, uint32_t bd_num)
2278 {
2279         struct rte_mbuf *m_first = tx_pkts;
2280         struct rte_mbuf *m_last = tx_pkts;
2281         uint32_t tot_len = 0;
2282         uint32_t hdr_len;
2283         uint32_t i;
2284
2285         /*
2286          * Hardware requires that the sum of the data length of every 8
2287          * consecutive buffers is greater than MSS in hns3 network engine.
2288          * We simplify it by ensuring pkt_headlen + the first 8 consecutive
2289          * frags greater than gso header len + mss, and the remaining 7
2290          * consecutive frags greater than MSS except the last 7 frags.
2291          */
2292         if (bd_num <= HNS3_MAX_NON_TSO_BD_PER_PKT)
2293                 return false;
2294
2295         for (i = 0; m_last && i < HNS3_MAX_NON_TSO_BD_PER_PKT - 1;
2296              i++, m_last = m_last->next)
2297                 tot_len += m_last->data_len;
2298
2299         if (!m_last)
2300                 return true;
2301
2302         /* ensure the first 8 frags is greater than mss + header */
2303         hdr_len = tx_pkts->l2_len + tx_pkts->l3_len + tx_pkts->l4_len;
2304         hdr_len += (tx_pkts->ol_flags & PKT_TX_TUNNEL_MASK) ?
2305                    tx_pkts->outer_l2_len + tx_pkts->outer_l3_len : 0;
2306         if (tot_len + m_last->data_len < tx_pkts->tso_segsz + hdr_len)
2307                 return true;
2308
2309         /*
2310          * ensure the sum of the data length of every 7 consecutive buffer
2311          * is greater than mss except the last one.
2312          */
2313         for (i = 0; m_last && i < bd_num - HNS3_MAX_NON_TSO_BD_PER_PKT; i++) {
2314                 tot_len -= m_first->data_len;
2315                 tot_len += m_last->data_len;
2316
2317                 if (tot_len < tx_pkts->tso_segsz)
2318                         return true;
2319
2320                 m_first = m_first->next;
2321                 m_last = m_last->next;
2322         }
2323
2324         return false;
2325 }
2326
2327 static void
2328 hns3_outer_header_cksum_prepare(struct rte_mbuf *m)
2329 {
2330         uint64_t ol_flags = m->ol_flags;
2331         struct rte_ipv4_hdr *ipv4_hdr;
2332         struct rte_udp_hdr *udp_hdr;
2333         uint32_t paylen, hdr_len;
2334
2335         if (!(ol_flags & (PKT_TX_OUTER_IPV4 | PKT_TX_OUTER_IPV6)))
2336                 return;
2337
2338         if (ol_flags & PKT_TX_IPV4) {
2339                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2340                                                    m->outer_l2_len);
2341
2342                 if (ol_flags & PKT_TX_IP_CKSUM)
2343                         ipv4_hdr->hdr_checksum = 0;
2344         }
2345
2346         if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM &&
2347             ol_flags & PKT_TX_TCP_SEG) {
2348                 hdr_len = m->l2_len + m->l3_len + m->l4_len;
2349                 hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
2350                                 m->outer_l2_len + m->outer_l3_len : 0;
2351                 paylen = m->pkt_len - hdr_len;
2352                 if (paylen <= m->tso_segsz)
2353                         return;
2354                 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
2355                                                   m->outer_l2_len +
2356                                                   m->outer_l3_len);
2357                 udp_hdr->dgram_cksum = 0;
2358         }
2359 }
2360
2361 static int
2362 hns3_check_tso_pkt_valid(struct rte_mbuf *m)
2363 {
2364         uint32_t tmp_data_len_sum = 0;
2365         uint16_t nb_buf = m->nb_segs;
2366         uint32_t paylen, hdr_len;
2367         struct rte_mbuf *m_seg;
2368         int i;
2369
2370         if (nb_buf > HNS3_MAX_TSO_BD_PER_PKT)
2371                 return -EINVAL;
2372
2373         hdr_len = m->l2_len + m->l3_len + m->l4_len;
2374         hdr_len += (m->ol_flags & PKT_TX_TUNNEL_MASK) ?
2375                         m->outer_l2_len + m->outer_l3_len : 0;
2376         if (hdr_len > HNS3_MAX_TSO_HDR_SIZE)
2377                 return -EINVAL;
2378
2379         paylen = m->pkt_len - hdr_len;
2380         if (paylen > HNS3_MAX_BD_PAYLEN)
2381                 return -EINVAL;
2382
2383         /*
2384          * The TSO header (include outer and inner L2, L3 and L4 header)
2385          * should be provided by three descriptors in maximum in hns3 network
2386          * engine.
2387          */
2388         m_seg = m;
2389         for (i = 0; m_seg != NULL && i < HNS3_MAX_TSO_HDR_BD_NUM && i < nb_buf;
2390              i++, m_seg = m_seg->next) {
2391                 tmp_data_len_sum += m_seg->data_len;
2392         }
2393
2394         if (hdr_len > tmp_data_len_sum)
2395                 return -EINVAL;
2396
2397         return 0;
2398 }
2399
2400 uint16_t
2401 hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
2402                uint16_t nb_pkts)
2403 {
2404         struct rte_mbuf *m;
2405         uint16_t i;
2406         int ret;
2407
2408         for (i = 0; i < nb_pkts; i++) {
2409                 m = tx_pkts[i];
2410
2411                 if (hns3_pkt_is_tso(m) &&
2412                     (hns3_pkt_need_linearized(m, m->nb_segs) ||
2413                      hns3_check_tso_pkt_valid(m))) {
2414                         rte_errno = EINVAL;
2415                         return i;
2416                 }
2417
2418 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2419                 ret = rte_validate_tx_offload(m);
2420                 if (ret != 0) {
2421                         rte_errno = -ret;
2422                         return i;
2423                 }
2424 #endif
2425                 ret = rte_net_intel_cksum_prepare(m);
2426                 if (ret != 0) {
2427                         rte_errno = -ret;
2428                         return i;
2429                 }
2430
2431                 hns3_outer_header_cksum_prepare(m);
2432         }
2433
2434         return i;
2435 }
2436
2437 static int
2438 hns3_parse_cksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
2439                  const struct rte_mbuf *m, struct rte_net_hdr_lens *hdr_lens)
2440 {
2441         /* Fill in tunneling parameters if necessary */
2442         if (m->ol_flags & PKT_TX_TUNNEL_MASK) {
2443                 (void)rte_net_get_ptype(m, hdr_lens, RTE_PTYPE_ALL_MASK);
2444                 if (hns3_parse_tunneling_params(txq, tx_desc_id, m->ol_flags,
2445                                                 hdr_lens)) {
2446                         txq->unsupported_tunnel_pkt_cnt++;
2447                         return -EINVAL;
2448                 }
2449         }
2450         /* Enable checksum offloading */
2451         if (m->ol_flags & HNS3_TX_CKSUM_OFFLOAD_MASK)
2452                 hns3_txd_enable_checksum(txq, tx_desc_id, m->ol_flags);
2453
2454         return 0;
2455 }
2456
2457 static int
2458 hns3_check_non_tso_pkt(uint16_t nb_buf, struct rte_mbuf **m_seg,
2459                       struct rte_mbuf *tx_pkt, struct hns3_tx_queue *txq)
2460 {
2461         struct rte_mbuf *new_pkt;
2462         int ret;
2463
2464         if (hns3_pkt_is_tso(*m_seg))
2465                 return 0;
2466
2467         /*
2468          * If packet length is greater than HNS3_MAX_FRAME_LEN
2469          * driver support, the packet will be ignored.
2470          */
2471         if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) > HNS3_MAX_FRAME_LEN)) {
2472                 txq->over_length_pkt_cnt++;
2473                 return -EINVAL;
2474         }
2475
2476         if (unlikely(nb_buf > HNS3_MAX_NON_TSO_BD_PER_PKT)) {
2477                 txq->exceed_limit_bd_pkt_cnt++;
2478                 ret = hns3_reassemble_tx_pkts(txq, tx_pkt, &new_pkt);
2479                 if (ret) {
2480                         txq->exceed_limit_bd_reassem_fail++;
2481                         return ret;
2482                 }
2483                 *m_seg = new_pkt;
2484         }
2485
2486         return 0;
2487 }
2488
2489 uint16_t
2490 hns3_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
2491 {
2492         struct rte_net_hdr_lens hdr_lens = {0};
2493         struct hns3_tx_queue *txq = tx_queue;
2494         struct hns3_entry *tx_bak_pkt;
2495         struct rte_mbuf *tx_pkt;
2496         struct rte_mbuf *m_seg;
2497         uint32_t nb_hold = 0;
2498         uint16_t tx_next_use;
2499         uint16_t tx_pkt_num;
2500         uint16_t tx_bd_max;
2501         uint16_t nb_buf;
2502         uint16_t nb_tx;
2503         uint16_t i;
2504
2505         /* free useless buffer */
2506         hns3_tx_free_useless_buffer(txq);
2507
2508         tx_next_use   = txq->next_to_use;
2509         tx_bd_max     = txq->nb_tx_desc;
2510         tx_pkt_num = nb_pkts;
2511
2512         /* send packets */
2513         tx_bak_pkt = &txq->sw_ring[tx_next_use];
2514         for (nb_tx = 0; nb_tx < tx_pkt_num; nb_tx++) {
2515                 tx_pkt = *tx_pkts++;
2516
2517                 nb_buf = tx_pkt->nb_segs;
2518
2519                 if (nb_buf > txq->tx_bd_ready) {
2520                         txq->queue_full_cnt++;
2521                         if (nb_tx == 0)
2522                                 return 0;
2523
2524                         goto end_of_tx;
2525                 }
2526
2527                 /*
2528                  * If packet length is less than minimum packet size, driver
2529                  * need to pad it.
2530                  */
2531                 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) < HNS3_MIN_PKT_SIZE)) {
2532                         uint16_t add_len;
2533                         char *appended;
2534
2535                         add_len = HNS3_MIN_PKT_SIZE -
2536                                          rte_pktmbuf_pkt_len(tx_pkt);
2537                         appended = rte_pktmbuf_append(tx_pkt, add_len);
2538                         if (appended == NULL) {
2539                                 txq->pkt_padding_fail_cnt++;
2540                                 break;
2541                         }
2542
2543                         memset(appended, 0, add_len);
2544                 }
2545
2546                 m_seg = tx_pkt;
2547
2548                 if (hns3_check_non_tso_pkt(nb_buf, &m_seg, tx_pkt, txq))
2549                         goto end_of_tx;
2550
2551                 if (hns3_parse_cksum(txq, tx_next_use, m_seg, &hdr_lens))
2552                         goto end_of_tx;
2553
2554                 i = 0;
2555                 do {
2556                         fill_desc(txq, tx_next_use, m_seg, (i == 0), 0);
2557                         tx_bak_pkt->mbuf = m_seg;
2558                         m_seg = m_seg->next;
2559                         tx_next_use++;
2560                         tx_bak_pkt++;
2561                         if (tx_next_use >= tx_bd_max) {
2562                                 tx_next_use = 0;
2563                                 tx_bak_pkt = txq->sw_ring;
2564                         }
2565
2566                         i++;
2567                 } while (m_seg != NULL);
2568
2569                 nb_hold += i;
2570                 txq->next_to_use = tx_next_use;
2571                 txq->tx_bd_ready -= i;
2572         }
2573
2574 end_of_tx:
2575
2576         if (likely(nb_tx))
2577                 hns3_queue_xmit(txq, nb_hold);
2578
2579         return nb_tx;
2580 }
2581
2582 static uint16_t
2583 hns3_dummy_rxtx_burst(void *dpdk_txq __rte_unused,
2584                       struct rte_mbuf **pkts __rte_unused,
2585                       uint16_t pkts_n __rte_unused)
2586 {
2587         return 0;
2588 }
2589
2590 void hns3_set_rxtx_function(struct rte_eth_dev *eth_dev)
2591 {
2592         struct hns3_adapter *hns = eth_dev->data->dev_private;
2593
2594         if (hns->hw.adapter_state == HNS3_NIC_STARTED &&
2595             rte_atomic16_read(&hns->hw.reset.resetting) == 0) {
2596                 eth_dev->rx_pkt_burst = hns3_recv_pkts;
2597                 eth_dev->tx_pkt_burst = hns3_xmit_pkts;
2598                 eth_dev->tx_pkt_prepare = hns3_prep_pkts;
2599         } else {
2600                 eth_dev->rx_pkt_burst = hns3_dummy_rxtx_burst;
2601                 eth_dev->tx_pkt_burst = hns3_dummy_rxtx_burst;
2602                 eth_dev->tx_pkt_prepare = hns3_dummy_rxtx_burst;
2603         }
2604 }