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