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