net/hns3: support NEON Rx
[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 HNS3_RX_RING_PREFETCTH_MASK     3
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 == NULL)
42                 return;
43
44         if (rxq->rx_rearm_nb == 0) {
45                 for (i = 0; i < rxq->nb_rx_desc; i++) {
46                         if (rxq->sw_ring[i].mbuf != NULL)
47                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
48                 }
49         } else {
50                 for (i = rxq->next_to_use;
51                      i != rxq->rx_rearm_start;
52                      i = (i + 1) % rxq->nb_rx_desc) {
53                         if (rxq->sw_ring[i].mbuf != NULL)
54                                 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
55                 }
56         }
57
58         for (i = 0; i < rxq->bulk_mbuf_num; i++)
59                 rte_pktmbuf_free_seg(rxq->bulk_mbuf[i]);
60         rxq->bulk_mbuf_num = 0;
61
62         if (rxq->pkt_first_seg) {
63                 rte_pktmbuf_free(rxq->pkt_first_seg);
64                 rxq->pkt_first_seg = NULL;
65         }
66 }
67
68 static void
69 hns3_tx_queue_release_mbufs(struct hns3_tx_queue *txq)
70 {
71         uint16_t i;
72
73         /* Note: Fake rx queue will not enter here */
74         if (txq->sw_ring) {
75                 for (i = 0; i < txq->nb_tx_desc; i++) {
76                         if (txq->sw_ring[i].mbuf) {
77                                 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
78                                 txq->sw_ring[i].mbuf = NULL;
79                         }
80                 }
81         }
82 }
83
84 static void
85 hns3_rx_queue_release(void *queue)
86 {
87         struct hns3_rx_queue *rxq = queue;
88         if (rxq) {
89                 hns3_rx_queue_release_mbufs(rxq);
90                 if (rxq->mz)
91                         rte_memzone_free(rxq->mz);
92                 if (rxq->sw_ring)
93                         rte_free(rxq->sw_ring);
94                 rte_free(rxq);
95         }
96 }
97
98 static void
99 hns3_tx_queue_release(void *queue)
100 {
101         struct hns3_tx_queue *txq = queue;
102         if (txq) {
103                 hns3_tx_queue_release_mbufs(txq);
104                 if (txq->mz)
105                         rte_memzone_free(txq->mz);
106                 if (txq->sw_ring)
107                         rte_free(txq->sw_ring);
108                 if (txq->free)
109                         rte_free(txq->free);
110                 rte_free(txq);
111         }
112 }
113
114 void
115 hns3_dev_rx_queue_release(void *queue)
116 {
117         struct hns3_rx_queue *rxq = queue;
118         struct hns3_adapter *hns;
119
120         if (rxq == NULL)
121                 return;
122
123         hns = rxq->hns;
124         rte_spinlock_lock(&hns->hw.lock);
125         hns3_rx_queue_release(queue);
126         rte_spinlock_unlock(&hns->hw.lock);
127 }
128
129 void
130 hns3_dev_tx_queue_release(void *queue)
131 {
132         struct hns3_tx_queue *txq = queue;
133         struct hns3_adapter *hns;
134
135         if (txq == NULL)
136                 return;
137
138         hns = txq->hns;
139         rte_spinlock_lock(&hns->hw.lock);
140         hns3_tx_queue_release(queue);
141         rte_spinlock_unlock(&hns->hw.lock);
142 }
143
144 static void
145 hns3_fake_rx_queue_release(struct hns3_rx_queue *queue)
146 {
147         struct hns3_rx_queue *rxq = queue;
148         struct hns3_adapter *hns;
149         struct hns3_hw *hw;
150         uint16_t idx;
151
152         if (rxq == NULL)
153                 return;
154
155         hns = rxq->hns;
156         hw = &hns->hw;
157         idx = rxq->queue_id;
158         if (hw->fkq_data.rx_queues[idx]) {
159                 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
160                 hw->fkq_data.rx_queues[idx] = NULL;
161         }
162
163         /* free fake rx queue arrays */
164         if (idx == (hw->fkq_data.nb_fake_rx_queues - 1)) {
165                 hw->fkq_data.nb_fake_rx_queues = 0;
166                 rte_free(hw->fkq_data.rx_queues);
167                 hw->fkq_data.rx_queues = NULL;
168         }
169 }
170
171 static void
172 hns3_fake_tx_queue_release(struct hns3_tx_queue *queue)
173 {
174         struct hns3_tx_queue *txq = queue;
175         struct hns3_adapter *hns;
176         struct hns3_hw *hw;
177         uint16_t idx;
178
179         if (txq == NULL)
180                 return;
181
182         hns = txq->hns;
183         hw = &hns->hw;
184         idx = txq->queue_id;
185         if (hw->fkq_data.tx_queues[idx]) {
186                 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
187                 hw->fkq_data.tx_queues[idx] = NULL;
188         }
189
190         /* free fake tx queue arrays */
191         if (idx == (hw->fkq_data.nb_fake_tx_queues - 1)) {
192                 hw->fkq_data.nb_fake_tx_queues = 0;
193                 rte_free(hw->fkq_data.tx_queues);
194                 hw->fkq_data.tx_queues = NULL;
195         }
196 }
197
198 static void
199 hns3_free_rx_queues(struct rte_eth_dev *dev)
200 {
201         struct hns3_adapter *hns = dev->data->dev_private;
202         struct hns3_fake_queue_data *fkq_data;
203         struct hns3_hw *hw = &hns->hw;
204         uint16_t nb_rx_q;
205         uint16_t i;
206
207         nb_rx_q = hw->data->nb_rx_queues;
208         for (i = 0; i < nb_rx_q; i++) {
209                 if (dev->data->rx_queues[i]) {
210                         hns3_rx_queue_release(dev->data->rx_queues[i]);
211                         dev->data->rx_queues[i] = NULL;
212                 }
213         }
214
215         /* Free fake Rx queues */
216         fkq_data = &hw->fkq_data;
217         for (i = 0; i < fkq_data->nb_fake_rx_queues; i++) {
218                 if (fkq_data->rx_queues[i])
219                         hns3_fake_rx_queue_release(fkq_data->rx_queues[i]);
220         }
221 }
222
223 static void
224 hns3_free_tx_queues(struct rte_eth_dev *dev)
225 {
226         struct hns3_adapter *hns = dev->data->dev_private;
227         struct hns3_fake_queue_data *fkq_data;
228         struct hns3_hw *hw = &hns->hw;
229         uint16_t nb_tx_q;
230         uint16_t i;
231
232         nb_tx_q = hw->data->nb_tx_queues;
233         for (i = 0; i < nb_tx_q; i++) {
234                 if (dev->data->tx_queues[i]) {
235                         hns3_tx_queue_release(dev->data->tx_queues[i]);
236                         dev->data->tx_queues[i] = NULL;
237                 }
238         }
239
240         /* Free fake Tx queues */
241         fkq_data = &hw->fkq_data;
242         for (i = 0; i < fkq_data->nb_fake_tx_queues; i++) {
243                 if (fkq_data->tx_queues[i])
244                         hns3_fake_tx_queue_release(fkq_data->tx_queues[i]);
245         }
246 }
247
248 void
249 hns3_free_all_queues(struct rte_eth_dev *dev)
250 {
251         hns3_free_rx_queues(dev);
252         hns3_free_tx_queues(dev);
253 }
254
255 static int
256 hns3_alloc_rx_queue_mbufs(struct hns3_hw *hw, struct hns3_rx_queue *rxq)
257 {
258         struct rte_mbuf *mbuf;
259         uint64_t dma_addr;
260         uint16_t i;
261
262         for (i = 0; i < rxq->nb_rx_desc; i++) {
263                 mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
264                 if (unlikely(mbuf == NULL)) {
265                         hns3_err(hw, "Failed to allocate RXD[%d] for rx queue!",
266                                  i);
267                         hns3_rx_queue_release_mbufs(rxq);
268                         return -ENOMEM;
269                 }
270
271                 rte_mbuf_refcnt_set(mbuf, 1);
272                 mbuf->next = NULL;
273                 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
274                 mbuf->nb_segs = 1;
275                 mbuf->port = rxq->port_id;
276
277                 rxq->sw_ring[i].mbuf = mbuf;
278                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
279                 rxq->rx_ring[i].addr = dma_addr;
280                 rxq->rx_ring[i].rx.bd_base_info = 0;
281         }
282
283         return 0;
284 }
285
286 static int
287 hns3_buf_size2type(uint32_t buf_size)
288 {
289         int bd_size_type;
290
291         switch (buf_size) {
292         case 512:
293                 bd_size_type = HNS3_BD_SIZE_512_TYPE;
294                 break;
295         case 1024:
296                 bd_size_type = HNS3_BD_SIZE_1024_TYPE;
297                 break;
298         case 4096:
299                 bd_size_type = HNS3_BD_SIZE_4096_TYPE;
300                 break;
301         default:
302                 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
303         }
304
305         return bd_size_type;
306 }
307
308 static void
309 hns3_init_rx_queue_hw(struct hns3_rx_queue *rxq)
310 {
311         uint32_t rx_buf_len = rxq->rx_buf_len;
312         uint64_t dma_addr = rxq->rx_ring_phys_addr;
313
314         hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_L_REG, (uint32_t)dma_addr);
315         hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_H_REG,
316                        (uint32_t)((dma_addr >> 31) >> 1));
317
318         hns3_write_dev(rxq, HNS3_RING_RX_BD_LEN_REG,
319                        hns3_buf_size2type(rx_buf_len));
320         hns3_write_dev(rxq, HNS3_RING_RX_BD_NUM_REG,
321                        HNS3_CFG_DESC_NUM(rxq->nb_rx_desc));
322 }
323
324 static void
325 hns3_init_tx_queue_hw(struct hns3_tx_queue *txq)
326 {
327         uint64_t dma_addr = txq->tx_ring_phys_addr;
328
329         hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_L_REG, (uint32_t)dma_addr);
330         hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_H_REG,
331                        (uint32_t)((dma_addr >> 31) >> 1));
332
333         hns3_write_dev(txq, HNS3_RING_TX_BD_NUM_REG,
334                        HNS3_CFG_DESC_NUM(txq->nb_tx_desc));
335 }
336
337 void
338 hns3_update_all_queues_pvid_state(struct hns3_hw *hw)
339 {
340         uint16_t nb_rx_q = hw->data->nb_rx_queues;
341         uint16_t nb_tx_q = hw->data->nb_tx_queues;
342         struct hns3_rx_queue *rxq;
343         struct hns3_tx_queue *txq;
344         int pvid_state;
345         int i;
346
347         pvid_state = hw->port_base_vlan_cfg.state;
348         for (i = 0; i < hw->cfg_max_queues; i++) {
349                 if (i < nb_rx_q) {
350                         rxq = hw->data->rx_queues[i];
351                         if (rxq != NULL)
352                                 rxq->pvid_state = pvid_state;
353                 }
354                 if (i < nb_tx_q) {
355                         txq = hw->data->tx_queues[i];
356                         if (txq != NULL)
357                                 txq->pvid_state = pvid_state;
358                 }
359         }
360 }
361
362 void
363 hns3_enable_all_queues(struct hns3_hw *hw, bool en)
364 {
365         uint16_t nb_rx_q = hw->data->nb_rx_queues;
366         uint16_t nb_tx_q = hw->data->nb_tx_queues;
367         struct hns3_rx_queue *rxq;
368         struct hns3_tx_queue *txq;
369         uint32_t rcb_reg;
370         int i;
371
372         for (i = 0; i < hw->cfg_max_queues; i++) {
373                 if (i < nb_rx_q)
374                         rxq = hw->data->rx_queues[i];
375                 else
376                         rxq = hw->fkq_data.rx_queues[i - nb_rx_q];
377                 if (i < nb_tx_q)
378                         txq = hw->data->tx_queues[i];
379                 else
380                         txq = hw->fkq_data.tx_queues[i - nb_tx_q];
381                 if (rxq == NULL || txq == NULL ||
382                     (en && (rxq->rx_deferred_start || txq->tx_deferred_start)))
383                         continue;
384
385                 rcb_reg = hns3_read_dev(rxq, HNS3_RING_EN_REG);
386                 if (en)
387                         rcb_reg |= BIT(HNS3_RING_EN_B);
388                 else
389                         rcb_reg &= ~BIT(HNS3_RING_EN_B);
390                 hns3_write_dev(rxq, HNS3_RING_EN_REG, rcb_reg);
391         }
392 }
393
394 static int
395 hns3_tqp_enable(struct hns3_hw *hw, uint16_t queue_id, bool enable)
396 {
397         struct hns3_cfg_com_tqp_queue_cmd *req;
398         struct hns3_cmd_desc desc;
399         int ret;
400
401         req = (struct hns3_cfg_com_tqp_queue_cmd *)desc.data;
402
403         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_COM_TQP_QUEUE, false);
404         req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
405         req->stream_id = 0;
406         hns3_set_bit(req->enable, HNS3_TQP_ENABLE_B, enable ? 1 : 0);
407
408         ret = hns3_cmd_send(hw, &desc, 1);
409         if (ret)
410                 hns3_err(hw, "TQP enable fail, ret = %d", ret);
411
412         return ret;
413 }
414
415 static int
416 hns3_send_reset_tqp_cmd(struct hns3_hw *hw, uint16_t queue_id, bool enable)
417 {
418         struct hns3_reset_tqp_queue_cmd *req;
419         struct hns3_cmd_desc desc;
420         int ret;
421
422         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, false);
423
424         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
425         req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
426         hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);
427
428         ret = hns3_cmd_send(hw, &desc, 1);
429         if (ret)
430                 hns3_err(hw, "Send tqp reset cmd error, ret = %d", ret);
431
432         return ret;
433 }
434
435 static int
436 hns3_get_reset_status(struct hns3_hw *hw, uint16_t queue_id)
437 {
438         struct hns3_reset_tqp_queue_cmd *req;
439         struct hns3_cmd_desc desc;
440         int ret;
441
442         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, true);
443
444         req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
445         req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
446
447         ret = hns3_cmd_send(hw, &desc, 1);
448         if (ret) {
449                 hns3_err(hw, "Get reset status error, ret =%d", ret);
450                 return ret;
451         }
452
453         return hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
454 }
455
456 static int
457 hns3_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
458 {
459 #define HNS3_TQP_RESET_TRY_MS   200
460         uint64_t end;
461         int reset_status;
462         int ret;
463
464         ret = hns3_tqp_enable(hw, queue_id, false);
465         if (ret)
466                 return ret;
467
468         /*
469          * In current version VF is not supported when PF is driven by DPDK
470          * driver, all task queue pairs are mapped to PF function, so PF's queue
471          * id is equals to the global queue id in PF range.
472          */
473         ret = hns3_send_reset_tqp_cmd(hw, queue_id, true);
474         if (ret) {
475                 hns3_err(hw, "Send reset tqp cmd fail, ret = %d", ret);
476                 return ret;
477         }
478         ret = -ETIMEDOUT;
479         end = get_timeofday_ms() + HNS3_TQP_RESET_TRY_MS;
480         do {
481                 /* Wait for tqp hw reset */
482                 rte_delay_ms(HNS3_POLL_RESPONE_MS);
483                 reset_status = hns3_get_reset_status(hw, queue_id);
484                 if (reset_status) {
485                         ret = 0;
486                         break;
487                 }
488         } while (get_timeofday_ms() < end);
489
490         if (ret) {
491                 hns3_err(hw, "Reset TQP fail, ret = %d", ret);
492                 return ret;
493         }
494
495         ret = hns3_send_reset_tqp_cmd(hw, queue_id, false);
496         if (ret)
497                 hns3_err(hw, "Deassert the soft reset fail, ret = %d", ret);
498
499         return ret;
500 }
501
502 static int
503 hns3vf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
504 {
505         uint8_t msg_data[2];
506         int ret;
507
508         /* Disable VF's queue before send queue reset msg to PF */
509         ret = hns3_tqp_enable(hw, queue_id, false);
510         if (ret)
511                 return ret;
512
513         memcpy(msg_data, &queue_id, sizeof(uint16_t));
514
515         return hns3_send_mbx_msg(hw, HNS3_MBX_QUEUE_RESET, 0, msg_data,
516                                  sizeof(msg_data), true, NULL, 0);
517 }
518
519 static int
520 hns3_reset_queue(struct hns3_adapter *hns, uint16_t queue_id)
521 {
522         struct hns3_hw *hw = &hns->hw;
523         if (hns->is_vf)
524                 return hns3vf_reset_tqp(hw, queue_id);
525         else
526                 return hns3_reset_tqp(hw, queue_id);
527 }
528
529 int
530 hns3_reset_all_queues(struct hns3_adapter *hns)
531 {
532         struct hns3_hw *hw = &hns->hw;
533         int ret, i;
534
535         for (i = 0; i < hw->cfg_max_queues; i++) {
536                 ret = hns3_reset_queue(hns, i);
537                 if (ret) {
538                         hns3_err(hw, "Failed to reset No.%d queue: %d", i, ret);
539                         return ret;
540                 }
541         }
542         return 0;
543 }
544
545 void
546 hns3_set_queue_intr_gl(struct hns3_hw *hw, uint16_t queue_id,
547                        uint8_t gl_idx, uint16_t gl_value)
548 {
549         uint32_t offset[] = {HNS3_TQP_INTR_GL0_REG,
550                              HNS3_TQP_INTR_GL1_REG,
551                              HNS3_TQP_INTR_GL2_REG};
552         uint32_t addr, value;
553
554         if (gl_idx >= RTE_DIM(offset) || gl_value > HNS3_TQP_INTR_GL_MAX)
555                 return;
556
557         addr = offset[gl_idx] + queue_id * HNS3_TQP_INTR_REG_SIZE;
558         if (hw->intr.gl_unit == HNS3_INTR_COALESCE_GL_UINT_1US)
559                 value = gl_value | HNS3_TQP_INTR_GL_UNIT_1US;
560         else
561                 value = HNS3_GL_USEC_TO_REG(gl_value);
562
563         hns3_write_dev(hw, addr, value);
564 }
565
566 void
567 hns3_set_queue_intr_rl(struct hns3_hw *hw, uint16_t queue_id, uint16_t rl_value)
568 {
569         uint32_t addr, value;
570
571         if (rl_value > HNS3_TQP_INTR_RL_MAX)
572                 return;
573
574         addr = HNS3_TQP_INTR_RL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
575         value = HNS3_RL_USEC_TO_REG(rl_value);
576         if (value > 0)
577                 value |= HNS3_TQP_INTR_RL_ENABLE_MASK;
578
579         hns3_write_dev(hw, addr, value);
580 }
581
582 void
583 hns3_set_queue_intr_ql(struct hns3_hw *hw, uint16_t queue_id, uint16_t ql_value)
584 {
585         uint32_t addr;
586
587         if (hw->intr.coalesce_mode == HNS3_INTR_COALESCE_NON_QL)
588                 return;
589
590         addr = HNS3_TQP_INTR_TX_QL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
591         hns3_write_dev(hw, addr, ql_value);
592
593         addr = HNS3_TQP_INTR_RX_QL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
594         hns3_write_dev(hw, addr, ql_value);
595 }
596
597 static void
598 hns3_queue_intr_enable(struct hns3_hw *hw, uint16_t queue_id, bool en)
599 {
600         uint32_t addr, value;
601
602         addr = HNS3_TQP_INTR_CTRL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
603         value = en ? 1 : 0;
604
605         hns3_write_dev(hw, addr, value);
606 }
607
608 /*
609  * Enable all rx queue interrupt when in interrupt rx mode.
610  * This api was called before enable queue rx&tx (in normal start or reset
611  * recover scenes), used to fix hardware rx queue interrupt enable was clear
612  * when FLR.
613  */
614 void
615 hns3_dev_all_rx_queue_intr_enable(struct hns3_hw *hw, bool en)
616 {
617         struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
618         uint16_t nb_rx_q = hw->data->nb_rx_queues;
619         int i;
620
621         if (dev->data->dev_conf.intr_conf.rxq == 0)
622                 return;
623
624         for (i = 0; i < nb_rx_q; i++)
625                 hns3_queue_intr_enable(hw, i, en);
626 }
627
628 int
629 hns3_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
630 {
631         struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
632         struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
633         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
634
635         if (dev->data->dev_conf.intr_conf.rxq == 0)
636                 return -ENOTSUP;
637
638         hns3_queue_intr_enable(hw, queue_id, true);
639
640         return rte_intr_ack(intr_handle);
641 }
642
643 int
644 hns3_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
645 {
646         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
647
648         if (dev->data->dev_conf.intr_conf.rxq == 0)
649                 return -ENOTSUP;
650
651         hns3_queue_intr_enable(hw, queue_id, false);
652
653         return 0;
654 }
655
656 static int
657 hns3_dev_rx_queue_start(struct hns3_adapter *hns, uint16_t idx)
658 {
659         struct hns3_hw *hw = &hns->hw;
660         struct hns3_rx_queue *rxq;
661         int ret;
662
663         PMD_INIT_FUNC_TRACE();
664
665         rxq = (struct hns3_rx_queue *)hw->data->rx_queues[idx];
666         ret = hns3_alloc_rx_queue_mbufs(hw, rxq);
667         if (ret) {
668                 hns3_err(hw, "Failed to alloc mbuf for No.%d rx queue: %d",
669                          idx, ret);
670                 return ret;
671         }
672
673         rxq->next_to_use = 0;
674         rxq->rx_rearm_start = 0;
675         rxq->rx_free_hold = 0;
676         rxq->rx_rearm_nb = 0;
677         rxq->pkt_first_seg = NULL;
678         rxq->pkt_last_seg = NULL;
679         hns3_init_rx_queue_hw(rxq);
680         hns3_rxq_vec_setup(rxq);
681
682         return 0;
683 }
684
685 static void
686 hns3_fake_rx_queue_start(struct hns3_adapter *hns, uint16_t idx)
687 {
688         struct hns3_hw *hw = &hns->hw;
689         struct hns3_rx_queue *rxq;
690
691         rxq = (struct hns3_rx_queue *)hw->fkq_data.rx_queues[idx];
692         rxq->next_to_use = 0;
693         rxq->rx_free_hold = 0;
694         rxq->rx_rearm_start = 0;
695         rxq->rx_rearm_nb = 0;
696         hns3_init_rx_queue_hw(rxq);
697 }
698
699 static void
700 hns3_init_tx_queue(struct hns3_tx_queue *queue)
701 {
702         struct hns3_tx_queue *txq = queue;
703         struct hns3_desc *desc;
704         int i;
705
706         /* Clear tx bd */
707         desc = txq->tx_ring;
708         for (i = 0; i < txq->nb_tx_desc; i++) {
709                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
710                 desc++;
711         }
712
713         txq->next_to_use = 0;
714         txq->next_to_clean = 0;
715         txq->tx_bd_ready = txq->nb_tx_desc - 1;
716         hns3_init_tx_queue_hw(txq);
717 }
718
719 static void
720 hns3_dev_tx_queue_start(struct hns3_adapter *hns, uint16_t idx)
721 {
722         struct hns3_hw *hw = &hns->hw;
723         struct hns3_tx_queue *txq;
724
725         txq = (struct hns3_tx_queue *)hw->data->tx_queues[idx];
726         hns3_init_tx_queue(txq);
727 }
728
729 static void
730 hns3_fake_tx_queue_start(struct hns3_adapter *hns, uint16_t idx)
731 {
732         struct hns3_hw *hw = &hns->hw;
733         struct hns3_tx_queue *txq;
734
735         txq = (struct hns3_tx_queue *)hw->fkq_data.tx_queues[idx];
736         hns3_init_tx_queue(txq);
737 }
738
739 static void
740 hns3_init_tx_ring_tc(struct hns3_adapter *hns)
741 {
742         struct hns3_hw *hw = &hns->hw;
743         struct hns3_tx_queue *txq;
744         int i, num;
745
746         for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
747                 struct hns3_tc_queue_info *tc_queue = &hw->tc_queue[i];
748                 int j;
749
750                 if (!tc_queue->enable)
751                         continue;
752
753                 for (j = 0; j < tc_queue->tqp_count; j++) {
754                         num = tc_queue->tqp_offset + j;
755                         txq = (struct hns3_tx_queue *)hw->data->tx_queues[num];
756                         if (txq == NULL)
757                                 continue;
758
759                         hns3_write_dev(txq, HNS3_RING_TX_TC_REG, tc_queue->tc);
760                 }
761         }
762 }
763
764 static int
765 hns3_start_rx_queues(struct hns3_adapter *hns)
766 {
767         struct hns3_hw *hw = &hns->hw;
768         struct hns3_rx_queue *rxq;
769         int i, j;
770         int ret;
771
772         /* Initialize RSS for queues */
773         ret = hns3_config_rss(hns);
774         if (ret) {
775                 hns3_err(hw, "Failed to configure rss %d", ret);
776                 return ret;
777         }
778
779         for (i = 0; i < hw->data->nb_rx_queues; i++) {
780                 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[i];
781                 if (rxq == NULL || rxq->rx_deferred_start)
782                         continue;
783                 ret = hns3_dev_rx_queue_start(hns, i);
784                 if (ret) {
785                         hns3_err(hw, "Failed to start No.%d rx queue: %d", i,
786                                  ret);
787                         goto out;
788                 }
789         }
790
791         for (i = 0; i < hw->fkq_data.nb_fake_rx_queues; i++) {
792                 rxq = (struct hns3_rx_queue *)hw->fkq_data.rx_queues[i];
793                 if (rxq == NULL || rxq->rx_deferred_start)
794                         continue;
795                 hns3_fake_rx_queue_start(hns, i);
796         }
797         return 0;
798
799 out:
800         for (j = 0; j < i; j++) {
801                 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[j];
802                 hns3_rx_queue_release_mbufs(rxq);
803         }
804
805         return ret;
806 }
807
808 static void
809 hns3_start_tx_queues(struct hns3_adapter *hns)
810 {
811         struct hns3_hw *hw = &hns->hw;
812         struct hns3_tx_queue *txq;
813         int i;
814
815         for (i = 0; i < hw->data->nb_tx_queues; i++) {
816                 txq = (struct hns3_tx_queue *)hw->data->tx_queues[i];
817                 if (txq == NULL || txq->tx_deferred_start)
818                         continue;
819                 hns3_dev_tx_queue_start(hns, i);
820         }
821
822         for (i = 0; i < hw->fkq_data.nb_fake_tx_queues; i++) {
823                 txq = (struct hns3_tx_queue *)hw->fkq_data.tx_queues[i];
824                 if (txq == NULL || txq->tx_deferred_start)
825                         continue;
826                 hns3_fake_tx_queue_start(hns, i);
827         }
828
829         hns3_init_tx_ring_tc(hns);
830 }
831
832 /*
833  * Start all queues.
834  * Note: just init and setup queues, and don't enable queue rx&tx.
835  */
836 int
837 hns3_start_queues(struct hns3_adapter *hns, bool reset_queue)
838 {
839         struct hns3_hw *hw = &hns->hw;
840         int ret;
841
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
850         ret = hns3_start_rx_queues(hns);
851         if (ret) {
852                 hns3_err(hw, "Failed to start rx queues: %d", ret);
853                 return ret;
854         }
855
856         hns3_start_tx_queues(hns);
857
858         return 0;
859 }
860
861 int
862 hns3_stop_queues(struct hns3_adapter *hns, bool reset_queue)
863 {
864         struct hns3_hw *hw = &hns->hw;
865         int ret;
866
867         hns3_enable_all_queues(hw, false);
868         if (reset_queue) {
869                 ret = hns3_reset_all_queues(hns);
870                 if (ret) {
871                         hns3_err(hw, "Failed to reset all queues %d", ret);
872                         return ret;
873                 }
874         }
875         return 0;
876 }
877
878 /*
879  * Iterate over all Rx Queue, and call the callback() function for each Rx
880  * queue.
881  *
882  * @param[in] dev
883  *   The target eth dev.
884  * @param[in] callback
885  *   The function to call for each queue.
886  *   if callback function return nonzero will stop iterate and return it's value
887  * @param[in] arg
888  *   The arguments to provide the callback function with.
889  *
890  * @return
891  *   0 on success, otherwise with errno set.
892  */
893 int
894 hns3_rxq_iterate(struct rte_eth_dev *dev,
895                  int (*callback)(struct hns3_rx_queue *, void *), void *arg)
896 {
897         uint32_t i;
898         int ret;
899
900         if (dev->data->rx_queues == NULL)
901                 return -EINVAL;
902
903         for (i = 0; i < dev->data->nb_rx_queues; i++) {
904                 ret = callback(dev->data->rx_queues[i], arg);
905                 if (ret != 0)
906                         return ret;
907         }
908
909         return 0;
910 }
911
912 static void*
913 hns3_alloc_rxq_and_dma_zone(struct rte_eth_dev *dev,
914                             struct hns3_queue_info *q_info)
915 {
916         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
917         const struct rte_memzone *rx_mz;
918         struct hns3_rx_queue *rxq;
919         unsigned int rx_desc;
920
921         rxq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_rx_queue),
922                                  RTE_CACHE_LINE_SIZE, q_info->socket_id);
923         if (rxq == NULL) {
924                 hns3_err(hw, "Failed to allocate memory for No.%d rx ring!",
925                          q_info->idx);
926                 return NULL;
927         }
928
929         /* Allocate rx ring hardware descriptors. */
930         rxq->queue_id = q_info->idx;
931         rxq->nb_rx_desc = q_info->nb_desc;
932
933         /*
934          * Allocate a litter more memory because rx vector functions
935          * don't check boundaries each time.
936          */
937         rx_desc = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
938                         sizeof(struct hns3_desc);
939         rx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
940                                          rx_desc, HNS3_RING_BASE_ALIGN,
941                                          q_info->socket_id);
942         if (rx_mz == NULL) {
943                 hns3_err(hw, "Failed to reserve DMA memory for No.%d rx ring!",
944                          q_info->idx);
945                 hns3_rx_queue_release(rxq);
946                 return NULL;
947         }
948         rxq->mz = rx_mz;
949         rxq->rx_ring = (struct hns3_desc *)rx_mz->addr;
950         rxq->rx_ring_phys_addr = rx_mz->iova;
951
952         hns3_dbg(hw, "No.%d rx descriptors iova 0x%" PRIx64, q_info->idx,
953                  rxq->rx_ring_phys_addr);
954
955         return rxq;
956 }
957
958 static int
959 hns3_fake_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
960                          uint16_t nb_desc, unsigned int socket_id)
961 {
962         struct hns3_adapter *hns = dev->data->dev_private;
963         struct hns3_hw *hw = &hns->hw;
964         struct hns3_queue_info q_info;
965         struct hns3_rx_queue *rxq;
966         uint16_t nb_rx_q;
967
968         if (hw->fkq_data.rx_queues[idx]) {
969                 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
970                 hw->fkq_data.rx_queues[idx] = NULL;
971         }
972
973         q_info.idx = idx;
974         q_info.socket_id = socket_id;
975         q_info.nb_desc = nb_desc;
976         q_info.type = "hns3 fake RX queue";
977         q_info.ring_name = "rx_fake_ring";
978         rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
979         if (rxq == NULL) {
980                 hns3_err(hw, "Failed to setup No.%d fake rx ring.", idx);
981                 return -ENOMEM;
982         }
983
984         /* Don't need alloc sw_ring, because upper applications don't use it */
985         rxq->sw_ring = NULL;
986
987         rxq->hns = hns;
988         rxq->rx_deferred_start = false;
989         rxq->port_id = dev->data->port_id;
990         rxq->configured = true;
991         nb_rx_q = dev->data->nb_rx_queues;
992         rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
993                                 (nb_rx_q + idx) * HNS3_TQP_REG_SIZE);
994         rxq->rx_buf_len = HNS3_MIN_BD_BUF_SIZE;
995
996         rte_spinlock_lock(&hw->lock);
997         hw->fkq_data.rx_queues[idx] = rxq;
998         rte_spinlock_unlock(&hw->lock);
999
1000         return 0;
1001 }
1002
1003 static void*
1004 hns3_alloc_txq_and_dma_zone(struct rte_eth_dev *dev,
1005                             struct hns3_queue_info *q_info)
1006 {
1007         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1008         const struct rte_memzone *tx_mz;
1009         struct hns3_tx_queue *txq;
1010         struct hns3_desc *desc;
1011         unsigned int tx_desc;
1012         int i;
1013
1014         txq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_tx_queue),
1015                                  RTE_CACHE_LINE_SIZE, q_info->socket_id);
1016         if (txq == NULL) {
1017                 hns3_err(hw, "Failed to allocate memory for No.%d tx ring!",
1018                          q_info->idx);
1019                 return NULL;
1020         }
1021
1022         /* Allocate tx ring hardware descriptors. */
1023         txq->queue_id = q_info->idx;
1024         txq->nb_tx_desc = q_info->nb_desc;
1025         tx_desc = txq->nb_tx_desc * sizeof(struct hns3_desc);
1026         tx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
1027                                          tx_desc, HNS3_RING_BASE_ALIGN,
1028                                          q_info->socket_id);
1029         if (tx_mz == NULL) {
1030                 hns3_err(hw, "Failed to reserve DMA memory for No.%d tx ring!",
1031                          q_info->idx);
1032                 hns3_tx_queue_release(txq);
1033                 return NULL;
1034         }
1035         txq->mz = tx_mz;
1036         txq->tx_ring = (struct hns3_desc *)tx_mz->addr;
1037         txq->tx_ring_phys_addr = tx_mz->iova;
1038
1039         hns3_dbg(hw, "No.%d tx descriptors iova 0x%" PRIx64, q_info->idx,
1040                  txq->tx_ring_phys_addr);
1041
1042         /* Clear tx bd */
1043         desc = txq->tx_ring;
1044         for (i = 0; i < txq->nb_tx_desc; i++) {
1045                 desc->tx.tp_fe_sc_vld_ra_ri = 0;
1046                 desc++;
1047         }
1048
1049         return txq;
1050 }
1051
1052 static int
1053 hns3_fake_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
1054                          uint16_t nb_desc, unsigned int socket_id)
1055 {
1056         struct hns3_adapter *hns = dev->data->dev_private;
1057         struct hns3_hw *hw = &hns->hw;
1058         struct hns3_queue_info q_info;
1059         struct hns3_tx_queue *txq;
1060         uint16_t nb_tx_q;
1061
1062         if (hw->fkq_data.tx_queues[idx] != NULL) {
1063                 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
1064                 hw->fkq_data.tx_queues[idx] = NULL;
1065         }
1066
1067         q_info.idx = idx;
1068         q_info.socket_id = socket_id;
1069         q_info.nb_desc = nb_desc;
1070         q_info.type = "hns3 fake TX queue";
1071         q_info.ring_name = "tx_fake_ring";
1072         txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
1073         if (txq == NULL) {
1074                 hns3_err(hw, "Failed to setup No.%d fake tx ring.", idx);
1075                 return -ENOMEM;
1076         }
1077
1078         /* Don't need alloc sw_ring, because upper applications don't use it */
1079         txq->sw_ring = NULL;
1080         txq->free = NULL;
1081
1082         txq->hns = hns;
1083         txq->tx_deferred_start = false;
1084         txq->port_id = dev->data->port_id;
1085         txq->configured = true;
1086         nb_tx_q = dev->data->nb_tx_queues;
1087         txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1088                                 (nb_tx_q + idx) * HNS3_TQP_REG_SIZE);
1089
1090         rte_spinlock_lock(&hw->lock);
1091         hw->fkq_data.tx_queues[idx] = txq;
1092         rte_spinlock_unlock(&hw->lock);
1093
1094         return 0;
1095 }
1096
1097 static int
1098 hns3_fake_rx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
1099 {
1100         uint16_t old_nb_queues = hw->fkq_data.nb_fake_rx_queues;
1101         void **rxq;
1102         uint8_t i;
1103
1104         if (hw->fkq_data.rx_queues == NULL && nb_queues != 0) {
1105                 /* first time configuration */
1106                 uint32_t size;
1107                 size = sizeof(hw->fkq_data.rx_queues[0]) * nb_queues;
1108                 hw->fkq_data.rx_queues = rte_zmalloc("fake_rx_queues", size,
1109                                                      RTE_CACHE_LINE_SIZE);
1110                 if (hw->fkq_data.rx_queues == NULL) {
1111                         hw->fkq_data.nb_fake_rx_queues = 0;
1112                         return -ENOMEM;
1113                 }
1114         } else if (hw->fkq_data.rx_queues != NULL && nb_queues != 0) {
1115                 /* re-configure */
1116                 rxq = hw->fkq_data.rx_queues;
1117                 for (i = nb_queues; i < old_nb_queues; i++)
1118                         hns3_dev_rx_queue_release(rxq[i]);
1119
1120                 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
1121                                   RTE_CACHE_LINE_SIZE);
1122                 if (rxq == NULL)
1123                         return -ENOMEM;
1124                 if (nb_queues > old_nb_queues) {
1125                         uint16_t new_qs = nb_queues - old_nb_queues;
1126                         memset(rxq + old_nb_queues, 0, sizeof(rxq[0]) * new_qs);
1127                 }
1128
1129                 hw->fkq_data.rx_queues = rxq;
1130         } else if (hw->fkq_data.rx_queues != NULL && nb_queues == 0) {
1131                 rxq = hw->fkq_data.rx_queues;
1132                 for (i = nb_queues; i < old_nb_queues; i++)
1133                         hns3_dev_rx_queue_release(rxq[i]);
1134
1135                 rte_free(hw->fkq_data.rx_queues);
1136                 hw->fkq_data.rx_queues = NULL;
1137         }
1138
1139         hw->fkq_data.nb_fake_rx_queues = nb_queues;
1140
1141         return 0;
1142 }
1143
1144 static int
1145 hns3_fake_tx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
1146 {
1147         uint16_t old_nb_queues = hw->fkq_data.nb_fake_tx_queues;
1148         void **txq;
1149         uint8_t i;
1150
1151         if (hw->fkq_data.tx_queues == NULL && nb_queues != 0) {
1152                 /* first time configuration */
1153                 uint32_t size;
1154                 size = sizeof(hw->fkq_data.tx_queues[0]) * nb_queues;
1155                 hw->fkq_data.tx_queues = rte_zmalloc("fake_tx_queues", size,
1156                                                      RTE_CACHE_LINE_SIZE);
1157                 if (hw->fkq_data.tx_queues == NULL) {
1158                         hw->fkq_data.nb_fake_tx_queues = 0;
1159                         return -ENOMEM;
1160                 }
1161         } else if (hw->fkq_data.tx_queues != NULL && nb_queues != 0) {
1162                 /* re-configure */
1163                 txq = hw->fkq_data.tx_queues;
1164                 for (i = nb_queues; i < old_nb_queues; i++)
1165                         hns3_dev_tx_queue_release(txq[i]);
1166                 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
1167                                   RTE_CACHE_LINE_SIZE);
1168                 if (txq == NULL)
1169                         return -ENOMEM;
1170                 if (nb_queues > old_nb_queues) {
1171                         uint16_t new_qs = nb_queues - old_nb_queues;
1172                         memset(txq + old_nb_queues, 0, sizeof(txq[0]) * new_qs);
1173                 }
1174
1175                 hw->fkq_data.tx_queues = txq;
1176         } else if (hw->fkq_data.tx_queues != NULL && nb_queues == 0) {
1177                 txq = hw->fkq_data.tx_queues;
1178                 for (i = nb_queues; i < old_nb_queues; i++)
1179                         hns3_dev_tx_queue_release(txq[i]);
1180
1181                 rte_free(hw->fkq_data.tx_queues);
1182                 hw->fkq_data.tx_queues = NULL;
1183         }
1184         hw->fkq_data.nb_fake_tx_queues = nb_queues;
1185
1186         return 0;
1187 }
1188
1189 int
1190 hns3_set_fake_rx_or_tx_queues(struct rte_eth_dev *dev, uint16_t nb_rx_q,
1191                               uint16_t nb_tx_q)
1192 {
1193         struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1194         uint16_t rx_need_add_nb_q;
1195         uint16_t tx_need_add_nb_q;
1196         uint16_t port_id;
1197         uint16_t q;
1198         int ret;
1199
1200         /* Setup new number of fake RX/TX queues and reconfigure device. */
1201         hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
1202         rx_need_add_nb_q = hw->cfg_max_queues - nb_rx_q;
1203         tx_need_add_nb_q = hw->cfg_max_queues - nb_tx_q;
1204         ret = hns3_fake_rx_queue_config(hw, rx_need_add_nb_q);
1205         if (ret) {
1206                 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1207                 goto cfg_fake_rx_q_fail;
1208         }
1209
1210         ret = hns3_fake_tx_queue_config(hw, tx_need_add_nb_q);
1211         if (ret) {
1212                 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1213                 goto cfg_fake_tx_q_fail;
1214         }
1215
1216         /* Allocate and set up fake RX queue per Ethernet port. */
1217         port_id = hw->data->port_id;
1218         for (q = 0; q < rx_need_add_nb_q; q++) {
1219                 ret = hns3_fake_rx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1220                                                rte_eth_dev_socket_id(port_id));
1221                 if (ret)
1222                         goto setup_fake_rx_q_fail;
1223         }
1224
1225         /* Allocate and set up fake TX queue per Ethernet port. */
1226         for (q = 0; q < tx_need_add_nb_q; q++) {
1227                 ret = hns3_fake_tx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1228                                                rte_eth_dev_socket_id(port_id));
1229                 if (ret)
1230                         goto setup_fake_tx_q_fail;
1231         }
1232
1233         return 0;
1234
1235 setup_fake_tx_q_fail:
1236 setup_fake_rx_q_fail:
1237         (void)hns3_fake_tx_queue_config(hw, 0);
1238 cfg_fake_tx_q_fail:
1239         (void)hns3_fake_rx_queue_config(hw, 0);
1240 cfg_fake_rx_q_fail:
1241         hw->cfg_max_queues = 0;
1242
1243         return ret;
1244 }
1245
1246 void
1247 hns3_dev_release_mbufs(struct hns3_adapter *hns)
1248 {
1249         struct rte_eth_dev_data *dev_data = hns->hw.data;
1250         struct hns3_rx_queue *rxq;
1251         struct hns3_tx_queue *txq;
1252         int i;
1253
1254         if (dev_data->rx_queues)
1255                 for (i = 0; i < dev_data->nb_rx_queues; i++) {
1256                         rxq = dev_data->rx_queues[i];
1257                         if (rxq == NULL || rxq->rx_deferred_start)
1258                                 continue;
1259                         hns3_rx_queue_release_mbufs(rxq);
1260                 }
1261
1262         if (dev_data->tx_queues)
1263                 for (i = 0; i < dev_data->nb_tx_queues; i++) {
1264                         txq = dev_data->tx_queues[i];
1265                         if (txq == NULL || txq->tx_deferred_start)
1266                                 continue;
1267                         hns3_tx_queue_release_mbufs(txq);
1268                 }
1269 }
1270
1271 static int
1272 hns3_rx_buf_len_calc(struct rte_mempool *mp, uint16_t *rx_buf_len)
1273 {
1274         uint16_t vld_buf_size;
1275         uint16_t num_hw_specs;
1276         uint16_t i;
1277
1278         /*
1279          * hns3 network engine only support to set 4 typical specification, and
1280          * different buffer size will affect the max packet_len and the max
1281          * number of segmentation when hw gro is turned on in receive side. The
1282          * relationship between them is as follows:
1283          *      rx_buf_size     |  max_gro_pkt_len  |  max_gro_nb_seg
1284          * ---------------------|-------------------|----------------
1285          * HNS3_4K_BD_BUF_SIZE  |        60KB       |       15
1286          * HNS3_2K_BD_BUF_SIZE  |        62KB       |       31
1287          * HNS3_1K_BD_BUF_SIZE  |        63KB       |       63
1288          * HNS3_512_BD_BUF_SIZE |      31.5KB       |       63
1289          */
1290         static const uint16_t hw_rx_buf_size[] = {
1291                 HNS3_4K_BD_BUF_SIZE,
1292                 HNS3_2K_BD_BUF_SIZE,
1293                 HNS3_1K_BD_BUF_SIZE,
1294                 HNS3_512_BD_BUF_SIZE
1295         };
1296
1297         vld_buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
1298                         RTE_PKTMBUF_HEADROOM);
1299
1300         if (vld_buf_size < HNS3_MIN_BD_BUF_SIZE)
1301                 return -EINVAL;
1302
1303         num_hw_specs = RTE_DIM(hw_rx_buf_size);
1304         for (i = 0; i < num_hw_specs; i++) {
1305                 if (vld_buf_size >= hw_rx_buf_size[i]) {
1306                         *rx_buf_len = hw_rx_buf_size[i];
1307                         break;
1308                 }
1309         }
1310         return 0;
1311 }
1312
1313 static int
1314 hns3_rx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_rxconf *conf,
1315                          struct rte_mempool *mp, uint16_t nb_desc,
1316                          uint16_t *buf_size)
1317 {
1318         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
1319             nb_desc % HNS3_ALIGN_RING_DESC) {
1320                 hns3_err(hw, "Number (%u) of rx descriptors is invalid",
1321                          nb_desc);
1322                 return -EINVAL;
1323         }
1324
1325         if (conf->rx_drop_en == 0)
1326                 hns3_warn(hw, "if no descriptors available, packets are always "
1327                           "dropped and rx_drop_en (1) is fixed on");
1328
1329         if (hns3_rx_buf_len_calc(mp, buf_size)) {
1330                 hns3_err(hw, "rxq mbufs' data room size (%u) is not enough! "
1331                                 "minimal data room size (%u).",
1332                                 rte_pktmbuf_data_room_size(mp),
1333                                 HNS3_MIN_BD_BUF_SIZE + RTE_PKTMBUF_HEADROOM);
1334                 return -EINVAL;
1335         }
1336
1337         return 0;
1338 }
1339
1340 int
1341 hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
1342                     unsigned int socket_id, const struct rte_eth_rxconf *conf,
1343                     struct rte_mempool *mp)
1344 {
1345         struct hns3_adapter *hns = dev->data->dev_private;
1346         struct hns3_hw *hw = &hns->hw;
1347         struct hns3_queue_info q_info;
1348         struct hns3_rx_queue *rxq;
1349         uint16_t rx_buf_size;
1350         int rx_entry_len;
1351         int ret;
1352
1353         if (dev->data->dev_started) {
1354                 hns3_err(hw, "rx_queue_setup after dev_start no supported");
1355                 return -EINVAL;
1356         }
1357
1358         ret = hns3_rx_queue_conf_check(hw, conf, mp, nb_desc, &rx_buf_size);
1359         if (ret)
1360                 return ret;
1361
1362         if (dev->data->rx_queues[idx]) {
1363                 hns3_rx_queue_release(dev->data->rx_queues[idx]);
1364                 dev->data->rx_queues[idx] = NULL;
1365         }
1366
1367         q_info.idx = idx;
1368         q_info.socket_id = socket_id;
1369         q_info.nb_desc = nb_desc;
1370         q_info.type = "hns3 RX queue";
1371         q_info.ring_name = "rx_ring";
1372
1373         rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
1374         if (rxq == NULL) {
1375                 hns3_err(hw,
1376                          "Failed to alloc mem and reserve DMA mem for rx ring!");
1377                 return -ENOMEM;
1378         }
1379
1380         rxq->hns = hns;
1381         rxq->ptype_tbl = &hns->ptype_tbl;
1382         rxq->mb_pool = mp;
1383         rxq->rx_free_thresh = (conf->rx_free_thresh > 0) ?
1384                 conf->rx_free_thresh : HNS3_DEFAULT_RX_FREE_THRESH;
1385         rxq->rx_deferred_start = conf->rx_deferred_start;
1386
1387         rx_entry_len = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
1388                         sizeof(struct hns3_entry);
1389         rxq->sw_ring = rte_zmalloc_socket("hns3 RX sw ring", rx_entry_len,
1390                                           RTE_CACHE_LINE_SIZE, socket_id);
1391         if (rxq->sw_ring == NULL) {
1392                 hns3_err(hw, "Failed to allocate memory for rx sw ring!");
1393                 hns3_rx_queue_release(rxq);
1394                 return -ENOMEM;
1395         }
1396
1397         rxq->next_to_use = 0;
1398         rxq->rx_free_hold = 0;
1399         rxq->rx_rearm_start = 0;
1400         rxq->rx_rearm_nb = 0;
1401         rxq->pkt_first_seg = NULL;
1402         rxq->pkt_last_seg = NULL;
1403         rxq->port_id = dev->data->port_id;
1404         rxq->pvid_state = hw->port_base_vlan_cfg.state;
1405         rxq->configured = true;
1406         rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1407                                 idx * HNS3_TQP_REG_SIZE);
1408         rxq->io_head_reg = (volatile void *)((char *)rxq->io_base +
1409                            HNS3_RING_RX_HEAD_REG);
1410         rxq->rx_buf_len = rx_buf_size;
1411         rxq->l2_errors = 0;
1412         rxq->pkt_len_errors = 0;
1413         rxq->l3_csum_errors = 0;
1414         rxq->l4_csum_errors = 0;
1415         rxq->ol3_csum_errors = 0;
1416         rxq->ol4_csum_errors = 0;
1417
1418         /* CRC len set here is used for amending packet length */
1419         if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
1420                 rxq->crc_len = RTE_ETHER_CRC_LEN;
1421         else
1422                 rxq->crc_len = 0;
1423
1424         rxq->bulk_mbuf_num = 0;
1425
1426         rte_spinlock_lock(&hw->lock);
1427         dev->data->rx_queues[idx] = rxq;
1428         rte_spinlock_unlock(&hw->lock);
1429
1430         return 0;
1431 }
1432
1433 void
1434 hns3_rx_scattered_reset(struct rte_eth_dev *dev)
1435 {
1436         struct hns3_adapter *hns = dev->data->dev_private;
1437         struct hns3_hw *hw = &hns->hw;
1438
1439         hw->rx_buf_len = 0;
1440         dev->data->scattered_rx = false;
1441 }
1442
1443 void
1444 hns3_rx_scattered_calc(struct rte_eth_dev *dev)
1445 {
1446         struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1447         struct hns3_adapter *hns = dev->data->dev_private;
1448         struct hns3_hw *hw = &hns->hw;
1449         struct hns3_rx_queue *rxq;
1450         uint32_t queue_id;
1451
1452         if (dev->data->rx_queues == NULL)
1453                 return;
1454
1455         for (queue_id = 0; queue_id < dev->data->nb_rx_queues; queue_id++) {
1456                 rxq = dev->data->rx_queues[queue_id];
1457                 if (hw->rx_buf_len == 0)
1458                         hw->rx_buf_len = rxq->rx_buf_len;
1459                 else
1460                         hw->rx_buf_len = RTE_MIN(hw->rx_buf_len,
1461                                                  rxq->rx_buf_len);
1462         }
1463
1464         if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_SCATTER ||
1465             dev_conf->rxmode.max_rx_pkt_len > hw->rx_buf_len)
1466                 dev->data->scattered_rx = true;
1467 }
1468
1469 const uint32_t *
1470 hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev)
1471 {
1472         static const uint32_t ptypes[] = {
1473                 RTE_PTYPE_L2_ETHER,
1474                 RTE_PTYPE_L2_ETHER_VLAN,
1475                 RTE_PTYPE_L2_ETHER_QINQ,
1476                 RTE_PTYPE_L2_ETHER_LLDP,
1477                 RTE_PTYPE_L2_ETHER_ARP,
1478                 RTE_PTYPE_L3_IPV4,
1479                 RTE_PTYPE_L3_IPV4_EXT,
1480                 RTE_PTYPE_L3_IPV6,
1481                 RTE_PTYPE_L3_IPV6_EXT,
1482                 RTE_PTYPE_L4_IGMP,
1483                 RTE_PTYPE_L4_ICMP,
1484                 RTE_PTYPE_L4_SCTP,
1485                 RTE_PTYPE_L4_TCP,
1486                 RTE_PTYPE_L4_UDP,
1487                 RTE_PTYPE_TUNNEL_GRE,
1488                 RTE_PTYPE_UNKNOWN
1489         };
1490
1491         if (dev->rx_pkt_burst == hns3_recv_pkts ||
1492             dev->rx_pkt_burst == hns3_recv_scattered_pkts ||
1493             dev->rx_pkt_burst == hns3_recv_pkts_vec)
1494                 return ptypes;
1495
1496         return NULL;
1497 }
1498
1499 void
1500 hns3_init_rx_ptype_tble(struct rte_eth_dev *dev)
1501 {
1502         struct hns3_adapter *hns = dev->data->dev_private;
1503         struct hns3_ptype_table *tbl = &hns->ptype_tbl;
1504
1505         memset(tbl, 0, sizeof(*tbl));
1506
1507         tbl->l2table[0] = RTE_PTYPE_L2_ETHER;
1508         tbl->l2table[1] = RTE_PTYPE_L2_ETHER_QINQ;
1509         tbl->l2table[2] = RTE_PTYPE_L2_ETHER_VLAN;
1510         tbl->l2table[3] = RTE_PTYPE_L2_ETHER_VLAN;
1511
1512         tbl->l3table[0] = RTE_PTYPE_L3_IPV4;
1513         tbl->l3table[1] = RTE_PTYPE_L3_IPV6;
1514         tbl->l3table[2] = RTE_PTYPE_L2_ETHER_ARP;
1515         tbl->l3table[3] = RTE_PTYPE_L2_ETHER;
1516         tbl->l3table[4] = RTE_PTYPE_L3_IPV4_EXT;
1517         tbl->l3table[5] = RTE_PTYPE_L3_IPV6_EXT;
1518         tbl->l3table[6] = RTE_PTYPE_L2_ETHER_LLDP;
1519
1520         tbl->l4table[0] = RTE_PTYPE_L4_UDP;
1521         tbl->l4table[1] = RTE_PTYPE_L4_TCP;
1522         tbl->l4table[2] = RTE_PTYPE_TUNNEL_GRE;
1523         tbl->l4table[3] = RTE_PTYPE_L4_SCTP;
1524         tbl->l4table[4] = RTE_PTYPE_L4_IGMP;
1525         tbl->l4table[5] = RTE_PTYPE_L4_ICMP;
1526
1527         tbl->inner_l2table[0] = RTE_PTYPE_INNER_L2_ETHER;
1528         tbl->inner_l2table[1] = RTE_PTYPE_INNER_L2_ETHER_VLAN;
1529         tbl->inner_l2table[2] = RTE_PTYPE_INNER_L2_ETHER_QINQ;
1530
1531         tbl->inner_l3table[0] = RTE_PTYPE_INNER_L3_IPV4;
1532         tbl->inner_l3table[1] = RTE_PTYPE_INNER_L3_IPV6;
1533         tbl->inner_l3table[2] = 0;
1534         tbl->inner_l3table[3] = RTE_PTYPE_INNER_L2_ETHER;
1535         tbl->inner_l3table[4] = RTE_PTYPE_INNER_L3_IPV4_EXT;
1536         tbl->inner_l3table[5] = RTE_PTYPE_INNER_L3_IPV6_EXT;
1537
1538         tbl->inner_l4table[0] = RTE_PTYPE_INNER_L4_UDP;
1539         tbl->inner_l4table[1] = RTE_PTYPE_INNER_L4_TCP;
1540         tbl->inner_l4table[2] = RTE_PTYPE_TUNNEL_GRE;
1541         tbl->inner_l4table[3] = RTE_PTYPE_INNER_L4_SCTP;
1542         tbl->inner_l4table[4] = RTE_PTYPE_L4_IGMP;
1543         tbl->inner_l4table[5] = RTE_PTYPE_INNER_L4_ICMP;
1544
1545         tbl->ol3table[0] = RTE_PTYPE_L3_IPV4;
1546         tbl->ol3table[1] = RTE_PTYPE_L3_IPV6;
1547         tbl->ol3table[2] = 0;
1548         tbl->ol3table[3] = 0;
1549         tbl->ol3table[4] = RTE_PTYPE_L3_IPV4_EXT;
1550         tbl->ol3table[5] = RTE_PTYPE_L3_IPV6_EXT;
1551
1552         tbl->ol4table[0] = 0;
1553         tbl->ol4table[1] = RTE_PTYPE_TUNNEL_VXLAN;
1554         tbl->ol4table[2] = RTE_PTYPE_TUNNEL_NVGRE;
1555 }
1556
1557 static inline void
1558 hns3_rxd_to_vlan_tci(struct hns3_rx_queue *rxq, struct rte_mbuf *mb,
1559                      uint32_t l234_info, const struct hns3_desc *rxd)
1560 {
1561 #define HNS3_STRP_STATUS_NUM            0x4
1562
1563 #define HNS3_NO_STRP_VLAN_VLD           0x0
1564 #define HNS3_INNER_STRP_VLAN_VLD        0x1
1565 #define HNS3_OUTER_STRP_VLAN_VLD        0x2
1566         uint32_t strip_status;
1567         uint32_t report_mode;
1568
1569         /*
1570          * Since HW limitation, the vlan tag will always be inserted into RX
1571          * descriptor when strip the tag from packet, driver needs to determine
1572          * reporting which tag to mbuf according to the PVID configuration
1573          * and vlan striped status.
1574          */
1575         static const uint32_t report_type[][HNS3_STRP_STATUS_NUM] = {
1576                 {
1577                         HNS3_NO_STRP_VLAN_VLD,
1578                         HNS3_OUTER_STRP_VLAN_VLD,
1579                         HNS3_INNER_STRP_VLAN_VLD,
1580                         HNS3_OUTER_STRP_VLAN_VLD
1581                 },
1582                 {
1583                         HNS3_NO_STRP_VLAN_VLD,
1584                         HNS3_NO_STRP_VLAN_VLD,
1585                         HNS3_NO_STRP_VLAN_VLD,
1586                         HNS3_INNER_STRP_VLAN_VLD
1587                 }
1588         };
1589         strip_status = hns3_get_field(l234_info, HNS3_RXD_STRP_TAGP_M,
1590                                       HNS3_RXD_STRP_TAGP_S);
1591         report_mode = report_type[rxq->pvid_state][strip_status];
1592         switch (report_mode) {
1593         case HNS3_NO_STRP_VLAN_VLD:
1594                 mb->vlan_tci = 0;
1595                 return;
1596         case HNS3_INNER_STRP_VLAN_VLD:
1597                 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
1598                 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.vlan_tag);
1599                 return;
1600         case HNS3_OUTER_STRP_VLAN_VLD:
1601                 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
1602                 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.ot_vlan_tag);
1603                 return;
1604         }
1605 }
1606
1607 static inline void
1608 recalculate_data_len(struct rte_mbuf *first_seg, struct rte_mbuf *last_seg,
1609                     struct rte_mbuf *rxm, struct hns3_rx_queue *rxq,
1610                     uint16_t data_len)
1611 {
1612         uint8_t crc_len = rxq->crc_len;
1613
1614         if (data_len <= crc_len) {
1615                 rte_pktmbuf_free_seg(rxm);
1616                 first_seg->nb_segs--;
1617                 last_seg->data_len = (uint16_t)(last_seg->data_len -
1618                         (crc_len - data_len));
1619                 last_seg->next = NULL;
1620         } else
1621                 rxm->data_len = (uint16_t)(data_len - crc_len);
1622 }
1623
1624 static inline struct rte_mbuf *
1625 hns3_rx_alloc_buffer(struct hns3_rx_queue *rxq)
1626 {
1627         int ret;
1628
1629         if (likely(rxq->bulk_mbuf_num > 0))
1630                 return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];
1631
1632         ret = rte_mempool_get_bulk(rxq->mb_pool, (void **)rxq->bulk_mbuf,
1633                                    HNS3_BULK_ALLOC_MBUF_NUM);
1634         if (likely(ret == 0)) {
1635                 rxq->bulk_mbuf_num = HNS3_BULK_ALLOC_MBUF_NUM;
1636                 return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];
1637         } else
1638                 return rte_mbuf_raw_alloc(rxq->mb_pool);
1639 }
1640
1641 uint16_t
1642 hns3_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1643 {
1644         volatile struct hns3_desc *rx_ring;  /* RX ring (desc) */
1645         volatile struct hns3_desc *rxdp;     /* pointer of the current desc */
1646         struct hns3_rx_queue *rxq;      /* RX queue */
1647         struct hns3_entry *sw_ring;
1648         struct hns3_entry *rxe;
1649         struct hns3_desc rxd;
1650         struct rte_mbuf *nmb;           /* pointer of the new mbuf */
1651         struct rte_mbuf *rxm;
1652         uint32_t bd_base_info;
1653         uint32_t cksum_err;
1654         uint32_t l234_info;
1655         uint32_t ol_info;
1656         uint64_t dma_addr;
1657         uint16_t nb_rx_bd;
1658         uint16_t nb_rx;
1659         uint16_t rx_id;
1660         int ret;
1661
1662         nb_rx = 0;
1663         nb_rx_bd = 0;
1664         rxq = rx_queue;
1665         rx_ring = rxq->rx_ring;
1666         sw_ring = rxq->sw_ring;
1667         rx_id = rxq->next_to_use;
1668
1669         while (nb_rx < nb_pkts) {
1670                 rxdp = &rx_ring[rx_id];
1671                 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
1672                 if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
1673                         break;
1674
1675                 rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
1676                            (1u << HNS3_RXD_VLD_B)];
1677
1678                 nmb = hns3_rx_alloc_buffer(rxq);
1679                 if (unlikely(nmb == NULL)) {
1680                         uint16_t port_id;
1681
1682                         port_id = rxq->port_id;
1683                         rte_eth_devices[port_id].data->rx_mbuf_alloc_failed++;
1684                         break;
1685                 }
1686
1687                 nb_rx_bd++;
1688                 rxe = &sw_ring[rx_id];
1689                 rx_id++;
1690                 if (unlikely(rx_id == rxq->nb_rx_desc))
1691                         rx_id = 0;
1692
1693                 rte_prefetch0(sw_ring[rx_id].mbuf);
1694                 if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
1695                         rte_prefetch0(&rx_ring[rx_id]);
1696                         rte_prefetch0(&sw_ring[rx_id]);
1697                 }
1698
1699                 rxm = rxe->mbuf;
1700                 rxe->mbuf = nmb;
1701
1702                 dma_addr = rte_mbuf_data_iova_default(nmb);
1703                 rxdp->addr = rte_cpu_to_le_64(dma_addr);
1704                 rxdp->rx.bd_base_info = 0;
1705
1706                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1707                 rxm->pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.rx.pkt_len)) -
1708                                 rxq->crc_len;
1709                 rxm->data_len = rxm->pkt_len;
1710                 rxm->port = rxq->port_id;
1711                 rxm->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
1712                 rxm->ol_flags = PKT_RX_RSS_HASH;
1713                 if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
1714                         rxm->hash.fdir.hi =
1715                                 rte_le_to_cpu_16(rxd.rx.fd_id);
1716                         rxm->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
1717                 }
1718                 rxm->nb_segs = 1;
1719                 rxm->next = NULL;
1720
1721                 /* Load remained descriptor data and extract necessary fields */
1722                 l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
1723                 ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
1724                 ret = hns3_handle_bdinfo(rxq, rxm, bd_base_info,
1725                                          l234_info, &cksum_err);
1726                 if (unlikely(ret))
1727                         goto pkt_err;
1728
1729                 rxm->packet_type = hns3_rx_calc_ptype(rxq, l234_info, ol_info);
1730
1731                 if (likely(bd_base_info & BIT(HNS3_RXD_L3L4P_B)))
1732                         hns3_rx_set_cksum_flag(rxm, rxm->packet_type,
1733                                                cksum_err);
1734                 hns3_rxd_to_vlan_tci(rxq, rxm, l234_info, &rxd);
1735
1736                 rx_pkts[nb_rx++] = rxm;
1737                 continue;
1738 pkt_err:
1739                 rte_pktmbuf_free(rxm);
1740         }
1741
1742         rxq->next_to_use = rx_id;
1743         rxq->rx_free_hold += nb_rx_bd;
1744         if (rxq->rx_free_hold > rxq->rx_free_thresh) {
1745                 hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
1746                 rxq->rx_free_hold = 0;
1747         }
1748
1749         return nb_rx;
1750 }
1751
1752 uint16_t
1753 hns3_recv_scattered_pkts(void *rx_queue,
1754                          struct rte_mbuf **rx_pkts,
1755                          uint16_t nb_pkts)
1756 {
1757         volatile struct hns3_desc *rx_ring;  /* RX ring (desc) */
1758         volatile struct hns3_desc *rxdp;     /* pointer of the current desc */
1759         struct hns3_rx_queue *rxq;      /* RX queue */
1760         struct hns3_entry *sw_ring;
1761         struct hns3_entry *rxe;
1762         struct rte_mbuf *first_seg;
1763         struct rte_mbuf *last_seg;
1764         struct hns3_desc rxd;
1765         struct rte_mbuf *nmb;           /* pointer of the new mbuf */
1766         struct rte_mbuf *rxm;
1767         struct rte_eth_dev *dev;
1768         uint32_t bd_base_info;
1769         uint32_t cksum_err;
1770         uint32_t l234_info;
1771         uint32_t gro_size;
1772         uint32_t ol_info;
1773         uint64_t dma_addr;
1774         uint16_t nb_rx_bd;
1775         uint16_t nb_rx;
1776         uint16_t rx_id;
1777         int ret;
1778
1779         nb_rx = 0;
1780         nb_rx_bd = 0;
1781         rxq = rx_queue;
1782
1783         rx_id = rxq->next_to_use;
1784         rx_ring = rxq->rx_ring;
1785         sw_ring = rxq->sw_ring;
1786         first_seg = rxq->pkt_first_seg;
1787         last_seg = rxq->pkt_last_seg;
1788
1789         while (nb_rx < nb_pkts) {
1790                 rxdp = &rx_ring[rx_id];
1791                 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
1792                 if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
1793                         break;
1794
1795                 /*
1796                  * The interactive process between software and hardware of
1797                  * receiving a new packet in hns3 network engine:
1798                  * 1. Hardware network engine firstly writes the packet content
1799                  *    to the memory pointed by the 'addr' field of the Rx Buffer
1800                  *    Descriptor, secondly fills the result of parsing the
1801                  *    packet include the valid field into the Rx Buffer
1802                  *    Descriptor in one write operation.
1803                  * 2. Driver reads the Rx BD's valid field in the loop to check
1804                  *    whether it's valid, if valid then assign a new address to
1805                  *    the addr field, clear the valid field, get the other
1806                  *    information of the packet by parsing Rx BD's other fields,
1807                  *    finally write back the number of Rx BDs processed by the
1808                  *    driver to the HNS3_RING_RX_HEAD_REG register to inform
1809                  *    hardware.
1810                  * In the above process, the ordering is very important. We must
1811                  * make sure that CPU read Rx BD's other fields only after the
1812                  * Rx BD is valid.
1813                  *
1814                  * There are two type of re-ordering: compiler re-ordering and
1815                  * CPU re-ordering under the ARMv8 architecture.
1816                  * 1. we use volatile to deal with compiler re-ordering, so you
1817                  *    can see that rx_ring/rxdp defined with volatile.
1818                  * 2. we commonly use memory barrier to deal with CPU
1819                  *    re-ordering, but the cost is high.
1820                  *
1821                  * In order to solve the high cost of using memory barrier, we
1822                  * use the data dependency order under the ARMv8 architecture,
1823                  * for example:
1824                  *      instr01: load A
1825                  *      instr02: load B <- A
1826                  * the instr02 will always execute after instr01.
1827                  *
1828                  * To construct the data dependency ordering, we use the
1829                  * following assignment:
1830                  *      rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
1831                  *                 (1u<<HNS3_RXD_VLD_B)]
1832                  * Using gcc compiler under the ARMv8 architecture, the related
1833                  * assembly code example as follows:
1834                  * note: (1u << HNS3_RXD_VLD_B) equal 0x10
1835                  *      instr01: ldr w26, [x22, #28]  --read bd_base_info
1836                  *      instr02: and w0, w26, #0x10   --calc bd_base_info & 0x10
1837                  *      instr03: sub w0, w0, #0x10    --calc (bd_base_info &
1838                  *                                            0x10) - 0x10
1839                  *      instr04: add x0, x22, x0, lsl #5 --calc copy source addr
1840                  *      instr05: ldp x2, x3, [x0]
1841                  *      instr06: stp x2, x3, [x29, #256] --copy BD's [0 ~ 15]B
1842                  *      instr07: ldp x4, x5, [x0, #16]
1843                  *      instr08: stp x4, x5, [x29, #272] --copy BD's [16 ~ 31]B
1844                  * the instr05~08 depend on x0's value, x0 depent on w26's
1845                  * value, the w26 is the bd_base_info, this form the data
1846                  * dependency ordering.
1847                  * note: if BD is valid, (bd_base_info & (1u<<HNS3_RXD_VLD_B)) -
1848                  *       (1u<<HNS3_RXD_VLD_B) will always zero, so the
1849                  *       assignment is correct.
1850                  *
1851                  * So we use the data dependency ordering instead of memory
1852                  * barrier to improve receive performance.
1853                  */
1854                 rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
1855                            (1u << HNS3_RXD_VLD_B)];
1856
1857                 nmb = hns3_rx_alloc_buffer(rxq);
1858                 if (unlikely(nmb == NULL)) {
1859                         dev = &rte_eth_devices[rxq->port_id];
1860                         dev->data->rx_mbuf_alloc_failed++;
1861                         break;
1862                 }
1863
1864                 nb_rx_bd++;
1865                 rxe = &sw_ring[rx_id];
1866                 rx_id++;
1867                 if (unlikely(rx_id == rxq->nb_rx_desc))
1868                         rx_id = 0;
1869
1870                 rte_prefetch0(sw_ring[rx_id].mbuf);
1871                 if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
1872                         rte_prefetch0(&rx_ring[rx_id]);
1873                         rte_prefetch0(&sw_ring[rx_id]);
1874                 }
1875
1876                 rxm = rxe->mbuf;
1877                 rxe->mbuf = nmb;
1878
1879                 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1880                 rxdp->rx.bd_base_info = 0;
1881                 rxdp->addr = dma_addr;
1882
1883                 if (first_seg == NULL) {
1884                         first_seg = rxm;
1885                         first_seg->nb_segs = 1;
1886                 } else {
1887                         first_seg->nb_segs++;
1888                         last_seg->next = rxm;
1889                 }
1890
1891                 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1892                 rxm->data_len = rte_le_to_cpu_16(rxd.rx.size);
1893
1894                 if (!(bd_base_info & BIT(HNS3_RXD_FE_B))) {
1895                         last_seg = rxm;
1896                         rxm->next = NULL;
1897                         continue;
1898                 }
1899
1900                 /*
1901                  * The last buffer of the received packet. packet len from
1902                  * buffer description may contains CRC len, packet len should
1903                  * subtract it, same as data len.
1904                  */
1905                 first_seg->pkt_len = rte_le_to_cpu_16(rxd.rx.pkt_len);
1906
1907                 /*
1908                  * This is the last buffer of the received packet. If the CRC
1909                  * is not stripped by the hardware:
1910                  *  - Subtract the CRC length from the total packet length.
1911                  *  - If the last buffer only contains the whole CRC or a part
1912                  *  of it, free the mbuf associated to the last buffer. If part
1913                  *  of the CRC is also contained in the previous mbuf, subtract
1914                  *  the length of that CRC part from the data length of the
1915                  *  previous mbuf.
1916                  */
1917                 rxm->next = NULL;
1918                 if (unlikely(rxq->crc_len > 0)) {
1919                         first_seg->pkt_len -= rxq->crc_len;
1920                         recalculate_data_len(first_seg, last_seg, rxm, rxq,
1921                                 rxm->data_len);
1922                 }
1923
1924                 first_seg->port = rxq->port_id;
1925                 first_seg->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
1926                 first_seg->ol_flags = PKT_RX_RSS_HASH;
1927                 if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
1928                         first_seg->hash.fdir.hi =
1929                                 rte_le_to_cpu_16(rxd.rx.fd_id);
1930                         first_seg->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
1931                 }
1932
1933                 gro_size = hns3_get_field(bd_base_info, HNS3_RXD_GRO_SIZE_M,
1934                                           HNS3_RXD_GRO_SIZE_S);
1935                 if (gro_size != 0) {
1936                         first_seg->ol_flags |= PKT_RX_LRO;
1937                         first_seg->tso_segsz = gro_size;
1938                 }
1939
1940                 l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
1941                 ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
1942                 ret = hns3_handle_bdinfo(rxq, first_seg, bd_base_info,
1943                                          l234_info, &cksum_err);
1944                 if (unlikely(ret))
1945                         goto pkt_err;
1946
1947                 first_seg->packet_type = hns3_rx_calc_ptype(rxq,
1948                                                 l234_info, ol_info);
1949
1950                 if (bd_base_info & BIT(HNS3_RXD_L3L4P_B))
1951                         hns3_rx_set_cksum_flag(first_seg,
1952                                                first_seg->packet_type,
1953                                                cksum_err);
1954                 hns3_rxd_to_vlan_tci(rxq, first_seg, l234_info, &rxd);
1955
1956                 rx_pkts[nb_rx++] = first_seg;
1957                 first_seg = NULL;
1958                 continue;
1959 pkt_err:
1960                 rte_pktmbuf_free(first_seg);
1961                 first_seg = NULL;
1962         }
1963
1964         rxq->next_to_use = rx_id;
1965         rxq->pkt_first_seg = first_seg;
1966         rxq->pkt_last_seg = last_seg;
1967
1968         rxq->rx_free_hold += nb_rx_bd;
1969         if (rxq->rx_free_hold > rxq->rx_free_thresh) {
1970                 hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
1971                 rxq->rx_free_hold = 0;
1972         }
1973
1974         return nb_rx;
1975 }
1976
1977 void __rte_weak
1978 hns3_rxq_vec_setup(__rte_unused struct hns3_rx_queue *rxq)
1979 {
1980 }
1981
1982 int __rte_weak
1983 hns3_rx_check_vec_support(__rte_unused struct rte_eth_dev *dev)
1984 {
1985         return -ENOTSUP;
1986 }
1987
1988 uint16_t __rte_weak
1989 hns3_recv_pkts_vec(__rte_unused void *tx_queue,
1990                    __rte_unused struct rte_mbuf **tx_pkts,
1991                    __rte_unused uint16_t nb_pkts)
1992 {
1993         return 0;
1994 }
1995
1996 int
1997 hns3_rx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
1998                        struct rte_eth_burst_mode *mode)
1999 {
2000         static const struct {
2001                 eth_rx_burst_t pkt_burst;
2002                 const char *info;
2003         } burst_infos[] = {
2004                 { hns3_recv_pkts,               "Scalar" },
2005                 { hns3_recv_scattered_pkts,     "Scalar Scattered" },
2006                 { hns3_recv_pkts_vec,           "Vector Neon" },
2007         };
2008
2009         eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
2010         int ret = -EINVAL;
2011         unsigned int i;
2012
2013         for (i = 0; i < RTE_DIM(burst_infos); i++) {
2014                 if (pkt_burst == burst_infos[i].pkt_burst) {
2015                         snprintf(mode->info, sizeof(mode->info), "%s",
2016                                  burst_infos[i].info);
2017                         ret = 0;
2018                         break;
2019                 }
2020         }
2021
2022         return ret;
2023 }
2024
2025 static eth_rx_burst_t
2026 hns3_get_rx_function(struct rte_eth_dev *dev)
2027 {
2028         struct hns3_adapter *hns = dev->data->dev_private;
2029         uint64_t offloads = dev->data->dev_conf.rxmode.offloads;
2030
2031         if (hns->rx_vec_allowed && hns3_rx_check_vec_support(dev) == 0)
2032                 return hns3_recv_pkts_vec;
2033
2034         if (hns->rx_simple_allowed && !dev->data->scattered_rx &&
2035             (offloads & DEV_RX_OFFLOAD_TCP_LRO) == 0)
2036                 return hns3_recv_pkts;
2037
2038         return hns3_recv_scattered_pkts;
2039 }
2040
2041 static int
2042 hns3_tx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_txconf *conf,
2043                          uint16_t nb_desc, uint16_t *tx_rs_thresh,
2044                          uint16_t *tx_free_thresh, uint16_t idx)
2045 {
2046 #define HNS3_TX_RS_FREE_THRESH_GAP      8
2047         uint16_t rs_thresh, free_thresh, fast_free_thresh;
2048
2049         if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
2050             nb_desc % HNS3_ALIGN_RING_DESC) {
2051                 hns3_err(hw, "number (%u) of tx descriptors is invalid",
2052                          nb_desc);
2053                 return -EINVAL;
2054         }
2055
2056         rs_thresh = (conf->tx_rs_thresh > 0) ?
2057                         conf->tx_rs_thresh : HNS3_DEFAULT_TX_RS_THRESH;
2058         free_thresh = (conf->tx_free_thresh > 0) ?
2059                         conf->tx_free_thresh : HNS3_DEFAULT_TX_FREE_THRESH;
2060         if (rs_thresh + free_thresh > nb_desc || nb_desc % rs_thresh ||
2061             rs_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP ||
2062             free_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP) {
2063                 hns3_err(hw, "tx_rs_thresh (%d) tx_free_thresh (%d) nb_desc "
2064                          "(%d) of tx descriptors for port=%d queue=%d check "
2065                          "fail!",
2066                          rs_thresh, free_thresh, nb_desc, hw->data->port_id,
2067                          idx);
2068                 return -EINVAL;
2069         }
2070
2071         if (conf->tx_free_thresh == 0) {
2072                 /* Fast free Tx memory buffer to improve cache hit rate */
2073                 fast_free_thresh = nb_desc - rs_thresh;
2074                 if (fast_free_thresh >=
2075                     HNS3_TX_FAST_FREE_AHEAD + HNS3_DEFAULT_TX_FREE_THRESH)
2076                         free_thresh = fast_free_thresh -
2077                                         HNS3_TX_FAST_FREE_AHEAD;
2078         }
2079
2080         *tx_rs_thresh = rs_thresh;
2081         *tx_free_thresh = free_thresh;
2082         return 0;
2083 }
2084
2085 int
2086 hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
2087                     unsigned int socket_id, const struct rte_eth_txconf *conf)
2088 {
2089         struct hns3_adapter *hns = dev->data->dev_private;
2090         uint16_t tx_rs_thresh, tx_free_thresh;
2091         struct hns3_hw *hw = &hns->hw;
2092         struct hns3_queue_info q_info;
2093         struct hns3_tx_queue *txq;
2094         int tx_entry_len;
2095         int ret;
2096
2097         if (dev->data->dev_started) {
2098                 hns3_err(hw, "tx_queue_setup after dev_start no supported");
2099                 return -EINVAL;
2100         }
2101
2102         ret = hns3_tx_queue_conf_check(hw, conf, nb_desc,
2103                                        &tx_rs_thresh, &tx_free_thresh, idx);
2104         if (ret)
2105                 return ret;
2106
2107         if (dev->data->tx_queues[idx] != NULL) {
2108                 hns3_tx_queue_release(dev->data->tx_queues[idx]);
2109                 dev->data->tx_queues[idx] = NULL;
2110         }
2111
2112         q_info.idx = idx;
2113         q_info.socket_id = socket_id;
2114         q_info.nb_desc = nb_desc;
2115         q_info.type = "hns3 TX queue";
2116         q_info.ring_name = "tx_ring";
2117         txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
2118         if (txq == NULL) {
2119                 hns3_err(hw,
2120                          "Failed to alloc mem and reserve DMA mem for tx ring!");
2121                 return -ENOMEM;
2122         }
2123
2124         txq->tx_deferred_start = conf->tx_deferred_start;
2125         tx_entry_len = sizeof(struct hns3_entry) * txq->nb_tx_desc;
2126         txq->sw_ring = rte_zmalloc_socket("hns3 TX sw ring", tx_entry_len,
2127                                           RTE_CACHE_LINE_SIZE, socket_id);
2128         if (txq->sw_ring == NULL) {
2129                 hns3_err(hw, "Failed to allocate memory for tx sw ring!");
2130                 hns3_tx_queue_release(txq);
2131                 return -ENOMEM;
2132         }
2133
2134         txq->hns = hns;
2135         txq->next_to_use = 0;
2136         txq->next_to_clean = 0;
2137         txq->tx_bd_ready = txq->nb_tx_desc - 1;
2138         txq->tx_free_thresh = tx_free_thresh;
2139         txq->tx_rs_thresh = tx_rs_thresh;
2140         txq->free = rte_zmalloc_socket("hns3 TX mbuf free array",
2141                                 sizeof(struct rte_mbuf *) * txq->tx_rs_thresh,
2142                                 RTE_CACHE_LINE_SIZE, socket_id);
2143         if (!txq->free) {
2144                 hns3_err(hw, "failed to allocate tx mbuf free array!");
2145                 hns3_tx_queue_release(txq);
2146                 return -ENOMEM;
2147         }
2148
2149         txq->port_id = dev->data->port_id;
2150         txq->pvid_state = hw->port_base_vlan_cfg.state;
2151         txq->configured = true;
2152         txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
2153                                 idx * HNS3_TQP_REG_SIZE);
2154         txq->io_tail_reg = (volatile void *)((char *)txq->io_base +
2155                                              HNS3_RING_TX_TAIL_REG);
2156         txq->min_tx_pkt_len = hw->min_tx_pkt_len;
2157         txq->over_length_pkt_cnt = 0;
2158         txq->exceed_limit_bd_pkt_cnt = 0;
2159         txq->exceed_limit_bd_reassem_fail = 0;
2160         txq->unsupported_tunnel_pkt_cnt = 0;
2161         txq->queue_full_cnt = 0;
2162         txq->pkt_padding_fail_cnt = 0;
2163         rte_spinlock_lock(&hw->lock);
2164         dev->data->tx_queues[idx] = txq;
2165         rte_spinlock_unlock(&hw->lock);
2166
2167         return 0;
2168 }
2169
2170 static void
2171 hns3_tx_free_useless_buffer(struct hns3_tx_queue *txq)
2172 {
2173         uint16_t tx_next_clean = txq->next_to_clean;
2174         uint16_t tx_next_use   = txq->next_to_use;
2175         uint16_t tx_bd_ready   = txq->tx_bd_ready;
2176         uint16_t tx_bd_max     = txq->nb_tx_desc;
2177         struct hns3_entry *tx_bak_pkt = &txq->sw_ring[tx_next_clean];
2178         struct hns3_desc *desc = &txq->tx_ring[tx_next_clean];
2179         struct rte_mbuf *mbuf;
2180
2181         while ((!(desc->tx.tp_fe_sc_vld_ra_ri &
2182                 rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B)))) &&
2183                 tx_next_use != tx_next_clean) {
2184                 mbuf = tx_bak_pkt->mbuf;
2185                 if (mbuf) {
2186                         rte_pktmbuf_free_seg(mbuf);
2187                         tx_bak_pkt->mbuf = NULL;
2188                 }
2189
2190                 desc++;
2191                 tx_bak_pkt++;
2192                 tx_next_clean++;
2193                 tx_bd_ready++;
2194
2195                 if (tx_next_clean >= tx_bd_max) {
2196                         tx_next_clean = 0;
2197                         desc = txq->tx_ring;
2198                         tx_bak_pkt = txq->sw_ring;
2199                 }
2200         }
2201
2202         txq->next_to_clean = tx_next_clean;
2203         txq->tx_bd_ready   = tx_bd_ready;
2204 }
2205
2206 static int
2207 hns3_tso_proc_tunnel(struct hns3_desc *desc, uint64_t ol_flags,
2208                      struct rte_mbuf *rxm, uint8_t *l2_len)
2209 {
2210         uint64_t tun_flags;
2211         uint8_t ol4_len;
2212         uint32_t otmp;
2213
2214         tun_flags = ol_flags & PKT_TX_TUNNEL_MASK;
2215         if (tun_flags == 0)
2216                 return 0;
2217
2218         otmp = rte_le_to_cpu_32(desc->tx.ol_type_vlan_len_msec);
2219         switch (tun_flags) {
2220         case PKT_TX_TUNNEL_GENEVE:
2221         case PKT_TX_TUNNEL_VXLAN:
2222                 *l2_len = rxm->l2_len - RTE_ETHER_VXLAN_HLEN;
2223                 break;
2224         case PKT_TX_TUNNEL_GRE:
2225                 /*
2226                  * OL4 header size, defined in 4 Bytes, it contains outer
2227                  * L4(GRE) length and tunneling length.
2228                  */
2229                 ol4_len = hns3_get_field(otmp, HNS3_TXD_L4LEN_M,
2230                                          HNS3_TXD_L4LEN_S);
2231                 *l2_len = rxm->l2_len - (ol4_len << HNS3_L4_LEN_UNIT);
2232                 break;
2233         default:
2234                 /* For non UDP / GRE tunneling, drop the tunnel packet */
2235                 return -EINVAL;
2236         }
2237         hns3_set_field(otmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2238                        rxm->outer_l2_len >> HNS3_L2_LEN_UNIT);
2239         desc->tx.ol_type_vlan_len_msec = rte_cpu_to_le_32(otmp);
2240
2241         return 0;
2242 }
2243
2244 int
2245 hns3_config_gro(struct hns3_hw *hw, bool en)
2246 {
2247         struct hns3_cfg_gro_status_cmd *req;
2248         struct hns3_cmd_desc desc;
2249         int ret;
2250
2251         hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GRO_GENERIC_CONFIG, false);
2252         req = (struct hns3_cfg_gro_status_cmd *)desc.data;
2253
2254         req->gro_en = rte_cpu_to_le_16(en ? 1 : 0);
2255
2256         ret = hns3_cmd_send(hw, &desc, 1);
2257         if (ret)
2258                 hns3_err(hw, "%s hardware GRO failed, ret = %d",
2259                          en ? "enable" : "disable", ret);
2260
2261         return ret;
2262 }
2263
2264 int
2265 hns3_restore_gro_conf(struct hns3_hw *hw)
2266 {
2267         uint64_t offloads;
2268         bool gro_en;
2269         int ret;
2270
2271         offloads = hw->data->dev_conf.rxmode.offloads;
2272         gro_en = offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
2273         ret = hns3_config_gro(hw, gro_en);
2274         if (ret)
2275                 hns3_err(hw, "restore hardware GRO to %s failed, ret = %d",
2276                          gro_en ? "enabled" : "disabled", ret);
2277
2278         return ret;
2279 }
2280
2281 static inline bool
2282 hns3_pkt_is_tso(struct rte_mbuf *m)
2283 {
2284         return (m->tso_segsz != 0 && m->ol_flags & PKT_TX_TCP_SEG);
2285 }
2286
2287 static void
2288 hns3_set_tso(struct hns3_desc *desc, uint64_t ol_flags,
2289                 uint32_t paylen, struct rte_mbuf *rxm)
2290 {
2291         uint8_t l2_len = rxm->l2_len;
2292         uint32_t tmp;
2293
2294         if (!hns3_pkt_is_tso(rxm))
2295                 return;
2296
2297         if (hns3_tso_proc_tunnel(desc, ol_flags, rxm, &l2_len))
2298                 return;
2299
2300         if (paylen <= rxm->tso_segsz)
2301                 return;
2302
2303         tmp = rte_le_to_cpu_32(desc->tx.type_cs_vlan_tso_len);
2304         hns3_set_bit(tmp, HNS3_TXD_TSO_B, 1);
2305         hns3_set_bit(tmp, HNS3_TXD_L3CS_B, 1);
2306         hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S, HNS3_L4T_TCP);
2307         hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2308         hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2309                        sizeof(struct rte_tcp_hdr) >> HNS3_L4_LEN_UNIT);
2310         hns3_set_field(tmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2311                        l2_len >> HNS3_L2_LEN_UNIT);
2312         desc->tx.type_cs_vlan_tso_len = rte_cpu_to_le_32(tmp);
2313         desc->tx.mss = rte_cpu_to_le_16(rxm->tso_segsz);
2314 }
2315
2316 static inline void
2317 hns3_fill_per_desc(struct hns3_desc *desc, struct rte_mbuf *rxm)
2318 {
2319         desc->addr = rte_mbuf_data_iova(rxm);
2320         desc->tx.send_size = rte_cpu_to_le_16(rte_pktmbuf_data_len(rxm));
2321         desc->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B));
2322 }
2323
2324 static void
2325 hns3_fill_first_desc(struct hns3_tx_queue *txq, struct hns3_desc *desc,
2326                      struct rte_mbuf *rxm)
2327 {
2328         uint64_t ol_flags = rxm->ol_flags;
2329         uint32_t hdr_len;
2330         uint32_t paylen;
2331
2332         hdr_len = rxm->l2_len + rxm->l3_len + rxm->l4_len;
2333         hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
2334                            rxm->outer_l2_len + rxm->outer_l3_len : 0;
2335         paylen = rxm->pkt_len - hdr_len;
2336         desc->tx.paylen = rte_cpu_to_le_32(paylen);
2337         hns3_set_tso(desc, ol_flags, paylen, rxm);
2338
2339         /*
2340          * Currently, hardware doesn't support more than two layers VLAN offload
2341          * in Tx direction based on hns3 network engine. So when the number of
2342          * VLANs in the packets represented by rxm plus the number of VLAN
2343          * offload by hardware such as PVID etc, exceeds two, the packets will
2344          * be discarded or the original VLAN of the packets will be overwitted
2345          * by hardware. When the PF PVID is enabled by calling the API function
2346          * named rte_eth_dev_set_vlan_pvid or the VF PVID is enabled by the hns3
2347          * PF kernel ether driver, the outer VLAN tag will always be the PVID.
2348          * To avoid the VLAN of Tx descriptor is overwritten by PVID, it should
2349          * be added to the position close to the IP header when PVID is enabled.
2350          */
2351         if (!txq->pvid_state && ol_flags & (PKT_TX_VLAN_PKT |
2352                                 PKT_TX_QINQ_PKT)) {
2353                 desc->tx.ol_type_vlan_len_msec |=
2354                                 rte_cpu_to_le_32(BIT(HNS3_TXD_OVLAN_B));
2355                 if (ol_flags & PKT_TX_QINQ_PKT)
2356                         desc->tx.outer_vlan_tag =
2357                                         rte_cpu_to_le_16(rxm->vlan_tci_outer);
2358                 else
2359                         desc->tx.outer_vlan_tag =
2360                                         rte_cpu_to_le_16(rxm->vlan_tci);
2361         }
2362
2363         if (ol_flags & PKT_TX_QINQ_PKT ||
2364             ((ol_flags & PKT_TX_VLAN_PKT) && txq->pvid_state)) {
2365                 desc->tx.type_cs_vlan_tso_len |=
2366                                         rte_cpu_to_le_32(BIT(HNS3_TXD_VLAN_B));
2367                 desc->tx.vlan_tag = rte_cpu_to_le_16(rxm->vlan_tci);
2368         }
2369 }
2370
2371 static int
2372 hns3_tx_alloc_mbufs(struct hns3_tx_queue *txq, struct rte_mempool *mb_pool,
2373                     uint16_t nb_new_buf, struct rte_mbuf **alloc_mbuf)
2374 {
2375         struct rte_mbuf *new_mbuf = NULL;
2376         struct rte_eth_dev *dev;
2377         struct rte_mbuf *temp;
2378         struct hns3_hw *hw;
2379         uint16_t i;
2380
2381         /* Allocate enough mbufs */
2382         for (i = 0; i < nb_new_buf; i++) {
2383                 temp = rte_pktmbuf_alloc(mb_pool);
2384                 if (unlikely(temp == NULL)) {
2385                         dev = &rte_eth_devices[txq->port_id];
2386                         hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2387                         hns3_err(hw, "Failed to alloc TX mbuf port_id=%d,"
2388                                      "queue_id=%d in reassemble tx pkts.",
2389                                      txq->port_id, txq->queue_id);
2390                         rte_pktmbuf_free(new_mbuf);
2391                         return -ENOMEM;
2392                 }
2393                 temp->next = new_mbuf;
2394                 new_mbuf = temp;
2395         }
2396
2397         if (new_mbuf == NULL)
2398                 return -ENOMEM;
2399
2400         new_mbuf->nb_segs = nb_new_buf;
2401         *alloc_mbuf = new_mbuf;
2402
2403         return 0;
2404 }
2405
2406 static inline void
2407 hns3_pktmbuf_copy_hdr(struct rte_mbuf *new_pkt, struct rte_mbuf *old_pkt)
2408 {
2409         new_pkt->ol_flags = old_pkt->ol_flags;
2410         new_pkt->pkt_len = rte_pktmbuf_pkt_len(old_pkt);
2411         new_pkt->outer_l2_len = old_pkt->outer_l2_len;
2412         new_pkt->outer_l3_len = old_pkt->outer_l3_len;
2413         new_pkt->l2_len = old_pkt->l2_len;
2414         new_pkt->l3_len = old_pkt->l3_len;
2415         new_pkt->l4_len = old_pkt->l4_len;
2416         new_pkt->vlan_tci_outer = old_pkt->vlan_tci_outer;
2417         new_pkt->vlan_tci = old_pkt->vlan_tci;
2418 }
2419
2420 static int
2421 hns3_reassemble_tx_pkts(void *tx_queue, struct rte_mbuf *tx_pkt,
2422                         struct rte_mbuf **new_pkt)
2423 {
2424         struct hns3_tx_queue *txq = tx_queue;
2425         struct rte_mempool *mb_pool;
2426         struct rte_mbuf *new_mbuf;
2427         struct rte_mbuf *temp_new;
2428         struct rte_mbuf *temp;
2429         uint16_t last_buf_len;
2430         uint16_t nb_new_buf;
2431         uint16_t buf_size;
2432         uint16_t buf_len;
2433         uint16_t len_s;
2434         uint16_t len_d;
2435         uint16_t len;
2436         uint16_t i;
2437         int ret;
2438         char *s;
2439         char *d;
2440
2441         mb_pool = tx_pkt->pool;
2442         buf_size = tx_pkt->buf_len - RTE_PKTMBUF_HEADROOM;
2443         nb_new_buf = (rte_pktmbuf_pkt_len(tx_pkt) - 1) / buf_size + 1;
2444         if (nb_new_buf > HNS3_MAX_NON_TSO_BD_PER_PKT)
2445                 return -EINVAL;
2446
2447         last_buf_len = rte_pktmbuf_pkt_len(tx_pkt) % buf_size;
2448         if (last_buf_len == 0)
2449                 last_buf_len = buf_size;
2450
2451         /* Allocate enough mbufs */
2452         ret = hns3_tx_alloc_mbufs(txq, mb_pool, nb_new_buf, &new_mbuf);
2453         if (ret)
2454                 return ret;
2455
2456         /* Copy the original packet content to the new mbufs */
2457         temp = tx_pkt;
2458         s = rte_pktmbuf_mtod(temp, char *);
2459         len_s = rte_pktmbuf_data_len(temp);
2460         temp_new = new_mbuf;
2461         for (i = 0; i < nb_new_buf; i++) {
2462                 d = rte_pktmbuf_mtod(temp_new, char *);
2463                 if (i < nb_new_buf - 1)
2464                         buf_len = buf_size;
2465                 else
2466                         buf_len = last_buf_len;
2467                 len_d = buf_len;
2468
2469                 while (len_d) {
2470                         len = RTE_MIN(len_s, len_d);
2471                         memcpy(d, s, len);
2472                         s = s + len;
2473                         d = d + len;
2474                         len_d = len_d - len;
2475                         len_s = len_s - len;
2476
2477                         if (len_s == 0) {
2478                                 temp = temp->next;
2479                                 if (temp == NULL)
2480                                         break;
2481                                 s = rte_pktmbuf_mtod(temp, char *);
2482                                 len_s = rte_pktmbuf_data_len(temp);
2483                         }
2484                 }
2485
2486                 temp_new->data_len = buf_len;
2487                 temp_new = temp_new->next;
2488         }
2489         hns3_pktmbuf_copy_hdr(new_mbuf, tx_pkt);
2490
2491         /* free original mbufs */
2492         rte_pktmbuf_free(tx_pkt);
2493
2494         *new_pkt = new_mbuf;
2495
2496         return 0;
2497 }
2498
2499 static void
2500 hns3_parse_outer_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec)
2501 {
2502         uint32_t tmp = *ol_type_vlan_len_msec;
2503
2504         /* (outer) IP header type */
2505         if (ol_flags & PKT_TX_OUTER_IPV4) {
2506                 /* OL3 header size, defined in 4 bytes */
2507                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2508                                sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
2509                 if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
2510                         hns3_set_field(tmp, HNS3_TXD_OL3T_M,
2511                                        HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_CSUM);
2512                 else
2513                         hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
2514                                        HNS3_OL3T_IPV4_NO_CSUM);
2515         } else if (ol_flags & PKT_TX_OUTER_IPV6) {
2516                 hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
2517                                HNS3_OL3T_IPV6);
2518                 /* OL3 header size, defined in 4 bytes */
2519                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2520                                sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
2521         }
2522
2523         *ol_type_vlan_len_msec = tmp;
2524 }
2525
2526 static int
2527 hns3_parse_inner_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec,
2528                         struct rte_net_hdr_lens *hdr_lens)
2529 {
2530         uint32_t tmp = *ol_type_vlan_len_msec;
2531         uint8_t l4_len;
2532
2533         /* OL2 header size, defined in 2 bytes */
2534         hns3_set_field(tmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2535                        sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
2536
2537         /* L4TUNT: L4 Tunneling Type */
2538         switch (ol_flags & PKT_TX_TUNNEL_MASK) {
2539         case PKT_TX_TUNNEL_GENEVE:
2540         case PKT_TX_TUNNEL_VXLAN:
2541                 /* MAC in UDP tunnelling packet, include VxLAN */
2542                 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
2543                                HNS3_TUN_MAC_IN_UDP);
2544                 /*
2545                  * OL4 header size, defined in 4 Bytes, it contains outer
2546                  * L4(UDP) length and tunneling length.
2547                  */
2548                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2549                                (uint8_t)RTE_ETHER_VXLAN_HLEN >>
2550                                HNS3_L4_LEN_UNIT);
2551                 break;
2552         case PKT_TX_TUNNEL_GRE:
2553                 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
2554                                HNS3_TUN_NVGRE);
2555                 /*
2556                  * OL4 header size, defined in 4 Bytes, it contains outer
2557                  * L4(GRE) length and tunneling length.
2558                  */
2559                 l4_len = hdr_lens->l4_len + hdr_lens->tunnel_len;
2560                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2561                                l4_len >> HNS3_L4_LEN_UNIT);
2562                 break;
2563         default:
2564                 /* For non UDP / GRE tunneling, drop the tunnel packet */
2565                 return -EINVAL;
2566         }
2567
2568         *ol_type_vlan_len_msec = tmp;
2569
2570         return 0;
2571 }
2572
2573 static int
2574 hns3_parse_tunneling_params(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
2575                             uint64_t ol_flags,
2576                             struct rte_net_hdr_lens *hdr_lens)
2577 {
2578         struct hns3_desc *tx_ring = txq->tx_ring;
2579         struct hns3_desc *desc = &tx_ring[tx_desc_id];
2580         uint32_t value = 0;
2581         int ret;
2582
2583         hns3_parse_outer_params(ol_flags, &value);
2584         ret = hns3_parse_inner_params(ol_flags, &value, hdr_lens);
2585         if (ret)
2586                 return -EINVAL;
2587
2588         desc->tx.ol_type_vlan_len_msec |= rte_cpu_to_le_32(value);
2589
2590         return 0;
2591 }
2592
2593 static void
2594 hns3_parse_l3_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
2595 {
2596         uint32_t tmp;
2597
2598         /* Enable L3 checksum offloads */
2599         if (ol_flags & PKT_TX_IPV4) {
2600                 tmp = *type_cs_vlan_tso_len;
2601                 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
2602                                HNS3_L3T_IPV4);
2603                 /* inner(/normal) L3 header size, defined in 4 bytes */
2604                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2605                                sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
2606                 if (ol_flags & PKT_TX_IP_CKSUM)
2607                         hns3_set_bit(tmp, HNS3_TXD_L3CS_B, 1);
2608                 *type_cs_vlan_tso_len = tmp;
2609         } else if (ol_flags & PKT_TX_IPV6) {
2610                 tmp = *type_cs_vlan_tso_len;
2611                 /* L3T, IPv6 don't do checksum */
2612                 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
2613                                HNS3_L3T_IPV6);
2614                 /* inner(/normal) L3 header size, defined in 4 bytes */
2615                 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2616                                sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
2617                 *type_cs_vlan_tso_len = tmp;
2618         }
2619 }
2620
2621 static void
2622 hns3_parse_l4_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
2623 {
2624         uint32_t tmp;
2625
2626         /* Enable L4 checksum offloads */
2627         switch (ol_flags & PKT_TX_L4_MASK) {
2628         case PKT_TX_TCP_CKSUM:
2629                 tmp = *type_cs_vlan_tso_len;
2630                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
2631                                HNS3_L4T_TCP);
2632                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2633                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2634                                sizeof(struct rte_tcp_hdr) >> HNS3_L4_LEN_UNIT);
2635                 *type_cs_vlan_tso_len = tmp;
2636                 break;
2637         case PKT_TX_UDP_CKSUM:
2638                 tmp = *type_cs_vlan_tso_len;
2639                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
2640                                HNS3_L4T_UDP);
2641                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2642                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2643                                sizeof(struct rte_udp_hdr) >> HNS3_L4_LEN_UNIT);
2644                 *type_cs_vlan_tso_len = tmp;
2645                 break;
2646         case PKT_TX_SCTP_CKSUM:
2647                 tmp = *type_cs_vlan_tso_len;
2648                 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
2649                                HNS3_L4T_SCTP);
2650                 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2651                 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2652                                sizeof(struct rte_sctp_hdr) >> HNS3_L4_LEN_UNIT);
2653                 *type_cs_vlan_tso_len = tmp;
2654                 break;
2655         default:
2656                 break;
2657         }
2658 }
2659
2660 static void
2661 hns3_txd_enable_checksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
2662                          uint64_t ol_flags)
2663 {
2664         struct hns3_desc *tx_ring = txq->tx_ring;
2665         struct hns3_desc *desc = &tx_ring[tx_desc_id];
2666         uint32_t value = 0;
2667
2668         /* inner(/normal) L2 header size, defined in 2 bytes */
2669         hns3_set_field(value, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2670                        sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
2671
2672         hns3_parse_l3_cksum_params(ol_flags, &value);
2673         hns3_parse_l4_cksum_params(ol_flags, &value);
2674
2675         desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(value);
2676 }
2677
2678 static bool
2679 hns3_pkt_need_linearized(struct rte_mbuf *tx_pkts, uint32_t bd_num)
2680 {
2681         struct rte_mbuf *m_first = tx_pkts;
2682         struct rte_mbuf *m_last = tx_pkts;
2683         uint32_t tot_len = 0;
2684         uint32_t hdr_len;
2685         uint32_t i;
2686
2687         /*
2688          * Hardware requires that the sum of the data length of every 8
2689          * consecutive buffers is greater than MSS in hns3 network engine.
2690          * We simplify it by ensuring pkt_headlen + the first 8 consecutive
2691          * frags greater than gso header len + mss, and the remaining 7
2692          * consecutive frags greater than MSS except the last 7 frags.
2693          */
2694         if (bd_num <= HNS3_MAX_NON_TSO_BD_PER_PKT)
2695                 return false;
2696
2697         for (i = 0; m_last && i < HNS3_MAX_NON_TSO_BD_PER_PKT - 1;
2698              i++, m_last = m_last->next)
2699                 tot_len += m_last->data_len;
2700
2701         if (!m_last)
2702                 return true;
2703
2704         /* ensure the first 8 frags is greater than mss + header */
2705         hdr_len = tx_pkts->l2_len + tx_pkts->l3_len + tx_pkts->l4_len;
2706         hdr_len += (tx_pkts->ol_flags & PKT_TX_TUNNEL_MASK) ?
2707                    tx_pkts->outer_l2_len + tx_pkts->outer_l3_len : 0;
2708         if (tot_len + m_last->data_len < tx_pkts->tso_segsz + hdr_len)
2709                 return true;
2710
2711         /*
2712          * ensure the sum of the data length of every 7 consecutive buffer
2713          * is greater than mss except the last one.
2714          */
2715         for (i = 0; m_last && i < bd_num - HNS3_MAX_NON_TSO_BD_PER_PKT; i++) {
2716                 tot_len -= m_first->data_len;
2717                 tot_len += m_last->data_len;
2718
2719                 if (tot_len < tx_pkts->tso_segsz)
2720                         return true;
2721
2722                 m_first = m_first->next;
2723                 m_last = m_last->next;
2724         }
2725
2726         return false;
2727 }
2728
2729 static void
2730 hns3_outer_header_cksum_prepare(struct rte_mbuf *m)
2731 {
2732         uint64_t ol_flags = m->ol_flags;
2733         struct rte_ipv4_hdr *ipv4_hdr;
2734         struct rte_udp_hdr *udp_hdr;
2735         uint32_t paylen, hdr_len;
2736
2737         if (!(ol_flags & (PKT_TX_OUTER_IPV4 | PKT_TX_OUTER_IPV6)))
2738                 return;
2739
2740         if (ol_flags & PKT_TX_IPV4) {
2741                 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2742                                                    m->outer_l2_len);
2743
2744                 if (ol_flags & PKT_TX_IP_CKSUM)
2745                         ipv4_hdr->hdr_checksum = 0;
2746         }
2747
2748         if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM &&
2749             ol_flags & PKT_TX_TCP_SEG) {
2750                 hdr_len = m->l2_len + m->l3_len + m->l4_len;
2751                 hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
2752                                 m->outer_l2_len + m->outer_l3_len : 0;
2753                 paylen = m->pkt_len - hdr_len;
2754                 if (paylen <= m->tso_segsz)
2755                         return;
2756                 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
2757                                                   m->outer_l2_len +
2758                                                   m->outer_l3_len);
2759                 udp_hdr->dgram_cksum = 0;
2760         }
2761 }
2762
2763 static int
2764 hns3_check_tso_pkt_valid(struct rte_mbuf *m)
2765 {
2766         uint32_t tmp_data_len_sum = 0;
2767         uint16_t nb_buf = m->nb_segs;
2768         uint32_t paylen, hdr_len;
2769         struct rte_mbuf *m_seg;
2770         int i;
2771
2772         if (nb_buf > HNS3_MAX_TSO_BD_PER_PKT)
2773                 return -EINVAL;
2774
2775         hdr_len = m->l2_len + m->l3_len + m->l4_len;
2776         hdr_len += (m->ol_flags & PKT_TX_TUNNEL_MASK) ?
2777                         m->outer_l2_len + m->outer_l3_len : 0;
2778         if (hdr_len > HNS3_MAX_TSO_HDR_SIZE)
2779                 return -EINVAL;
2780
2781         paylen = m->pkt_len - hdr_len;
2782         if (paylen > HNS3_MAX_BD_PAYLEN)
2783                 return -EINVAL;
2784
2785         /*
2786          * The TSO header (include outer and inner L2, L3 and L4 header)
2787          * should be provided by three descriptors in maximum in hns3 network
2788          * engine.
2789          */
2790         m_seg = m;
2791         for (i = 0; m_seg != NULL && i < HNS3_MAX_TSO_HDR_BD_NUM && i < nb_buf;
2792              i++, m_seg = m_seg->next) {
2793                 tmp_data_len_sum += m_seg->data_len;
2794         }
2795
2796         if (hdr_len > tmp_data_len_sum)
2797                 return -EINVAL;
2798
2799         return 0;
2800 }
2801
2802 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2803 static inline int
2804 hns3_vld_vlan_chk(struct hns3_tx_queue *txq, struct rte_mbuf *m)
2805 {
2806         struct rte_ether_hdr *eh;
2807         struct rte_vlan_hdr *vh;
2808
2809         if (!txq->pvid_state)
2810                 return 0;
2811
2812         /*
2813          * Due to hardware limitations, we only support two-layer VLAN hardware
2814          * offload in Tx direction based on hns3 network engine, so when PVID is
2815          * enabled, QinQ insert is no longer supported.
2816          * And when PVID is enabled, in the following two cases:
2817          *  i) packets with more than two VLAN tags.
2818          *  ii) packets with one VLAN tag while the hardware VLAN insert is
2819          *      enabled.
2820          * The packets will be regarded as abnormal packets and discarded by
2821          * hardware in Tx direction. For debugging purposes, a validation check
2822          * for these types of packets is added to the '.tx_pkt_prepare' ops
2823          * implementation function named hns3_prep_pkts to inform users that
2824          * these packets will be discarded.
2825          */
2826         if (m->ol_flags & PKT_TX_QINQ_PKT)
2827                 return -EINVAL;
2828
2829         eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2830         if (eh->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
2831                 if (m->ol_flags & PKT_TX_VLAN_PKT)
2832                         return -EINVAL;
2833
2834                 /* Ensure the incoming packet is not a QinQ packet */
2835                 vh = (struct rte_vlan_hdr *)(eh + 1);
2836                 if (vh->eth_proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
2837                         return -EINVAL;
2838         }
2839
2840         return 0;
2841 }
2842 #endif
2843
2844 uint16_t
2845 hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
2846                uint16_t nb_pkts)
2847 {
2848         struct rte_mbuf *m;
2849         uint16_t i;
2850         int ret;
2851
2852         for (i = 0; i < nb_pkts; i++) {
2853                 m = tx_pkts[i];
2854
2855                 if (hns3_pkt_is_tso(m) &&
2856                     (hns3_pkt_need_linearized(m, m->nb_segs) ||
2857                      hns3_check_tso_pkt_valid(m))) {
2858                         rte_errno = EINVAL;
2859                         return i;
2860                 }
2861
2862 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2863                 ret = rte_validate_tx_offload(m);
2864                 if (ret != 0) {
2865                         rte_errno = -ret;
2866                         return i;
2867                 }
2868
2869                 if (hns3_vld_vlan_chk(tx_queue, m)) {
2870                         rte_errno = EINVAL;
2871                         return i;
2872                 }
2873 #endif
2874                 ret = rte_net_intel_cksum_prepare(m);
2875                 if (ret != 0) {
2876                         rte_errno = -ret;
2877                         return i;
2878                 }
2879
2880                 hns3_outer_header_cksum_prepare(m);
2881         }
2882
2883         return i;
2884 }
2885
2886 static int
2887 hns3_parse_cksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
2888                  const struct rte_mbuf *m, struct rte_net_hdr_lens *hdr_lens)
2889 {
2890         /* Fill in tunneling parameters if necessary */
2891         if (m->ol_flags & PKT_TX_TUNNEL_MASK) {
2892                 (void)rte_net_get_ptype(m, hdr_lens, RTE_PTYPE_ALL_MASK);
2893                 if (hns3_parse_tunneling_params(txq, tx_desc_id, m->ol_flags,
2894                                                 hdr_lens)) {
2895                         txq->unsupported_tunnel_pkt_cnt++;
2896                         return -EINVAL;
2897                 }
2898         }
2899         /* Enable checksum offloading */
2900         if (m->ol_flags & HNS3_TX_CKSUM_OFFLOAD_MASK)
2901                 hns3_txd_enable_checksum(txq, tx_desc_id, m->ol_flags);
2902
2903         return 0;
2904 }
2905
2906 static int
2907 hns3_check_non_tso_pkt(uint16_t nb_buf, struct rte_mbuf **m_seg,
2908                       struct rte_mbuf *tx_pkt, struct hns3_tx_queue *txq)
2909 {
2910         struct rte_mbuf *new_pkt;
2911         int ret;
2912
2913         if (hns3_pkt_is_tso(*m_seg))
2914                 return 0;
2915
2916         /*
2917          * If packet length is greater than HNS3_MAX_FRAME_LEN
2918          * driver support, the packet will be ignored.
2919          */
2920         if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) > HNS3_MAX_FRAME_LEN)) {
2921                 txq->over_length_pkt_cnt++;
2922                 return -EINVAL;
2923         }
2924
2925         if (unlikely(nb_buf > HNS3_MAX_NON_TSO_BD_PER_PKT)) {
2926                 txq->exceed_limit_bd_pkt_cnt++;
2927                 ret = hns3_reassemble_tx_pkts(txq, tx_pkt, &new_pkt);
2928                 if (ret) {
2929                         txq->exceed_limit_bd_reassem_fail++;
2930                         return ret;
2931                 }
2932                 *m_seg = new_pkt;
2933         }
2934
2935         return 0;
2936 }
2937
2938 static inline void
2939 hns3_tx_free_buffer_simple(struct hns3_tx_queue *txq)
2940 {
2941         struct hns3_entry *tx_entry;
2942         struct hns3_desc *desc;
2943         uint16_t tx_next_clean;
2944         int i;
2945
2946         while (1) {
2947                 if (HNS3_GET_TX_QUEUE_PEND_BD_NUM(txq) < txq->tx_rs_thresh)
2948                         break;
2949
2950                 /*
2951                  * All mbufs can be released only when the VLD bits of all
2952                  * descriptors in a batch are cleared.
2953                  */
2954                 tx_next_clean = (txq->next_to_clean + txq->tx_rs_thresh - 1) %
2955                                 txq->nb_tx_desc;
2956                 desc = &txq->tx_ring[tx_next_clean];
2957                 for (i = 0; i < txq->tx_rs_thresh; i++) {
2958                         if (rte_le_to_cpu_16(desc->tx.tp_fe_sc_vld_ra_ri) &
2959                                         BIT(HNS3_TXD_VLD_B))
2960                                 return;
2961                         desc--;
2962                 }
2963
2964                 tx_entry = &txq->sw_ring[txq->next_to_clean];
2965
2966                 for (i = 0; i < txq->tx_rs_thresh; i++)
2967                         rte_prefetch0((tx_entry + i)->mbuf);
2968                 for (i = 0; i < txq->tx_rs_thresh; i++, tx_entry++) {
2969                         rte_mempool_put(tx_entry->mbuf->pool, tx_entry->mbuf);
2970                         tx_entry->mbuf = NULL;
2971                 }
2972
2973                 txq->next_to_clean = (tx_next_clean + 1) % txq->nb_tx_desc;
2974                 txq->tx_bd_ready += txq->tx_rs_thresh;
2975         }
2976 }
2977
2978 static inline void
2979 hns3_tx_backup_1mbuf(struct hns3_entry *tx_entry, struct rte_mbuf **pkts)
2980 {
2981         tx_entry->mbuf = pkts[0];
2982 }
2983
2984 static inline void
2985 hns3_tx_backup_4mbuf(struct hns3_entry *tx_entry, struct rte_mbuf **pkts)
2986 {
2987         hns3_tx_backup_1mbuf(&tx_entry[0], &pkts[0]);
2988         hns3_tx_backup_1mbuf(&tx_entry[1], &pkts[1]);
2989         hns3_tx_backup_1mbuf(&tx_entry[2], &pkts[2]);
2990         hns3_tx_backup_1mbuf(&tx_entry[3], &pkts[3]);
2991 }
2992
2993 static inline void
2994 hns3_tx_setup_4bd(struct hns3_desc *txdp, struct rte_mbuf **pkts)
2995 {
2996 #define PER_LOOP_NUM    4
2997         const uint16_t bd_flag = BIT(HNS3_TXD_VLD_B) | BIT(HNS3_TXD_FE_B);
2998         uint64_t dma_addr;
2999         uint32_t i;
3000
3001         for (i = 0; i < PER_LOOP_NUM; i++, txdp++, pkts++) {
3002                 dma_addr = rte_mbuf_data_iova(*pkts);
3003                 txdp->addr = rte_cpu_to_le_64(dma_addr);
3004                 txdp->tx.send_size = rte_cpu_to_le_16((*pkts)->data_len);
3005                 txdp->tx.paylen = 0;
3006                 txdp->tx.type_cs_vlan_tso_len = 0;
3007                 txdp->tx.ol_type_vlan_len_msec = 0;
3008                 txdp->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(bd_flag);
3009         }
3010 }
3011
3012 static inline void
3013 hns3_tx_setup_1bd(struct hns3_desc *txdp, struct rte_mbuf **pkts)
3014 {
3015         const uint16_t bd_flag = BIT(HNS3_TXD_VLD_B) | BIT(HNS3_TXD_FE_B);
3016         uint64_t dma_addr;
3017
3018         dma_addr = rte_mbuf_data_iova(*pkts);
3019         txdp->addr = rte_cpu_to_le_64(dma_addr);
3020         txdp->tx.send_size = rte_cpu_to_le_16((*pkts)->data_len);
3021         txdp->tx.paylen = 0;
3022         txdp->tx.type_cs_vlan_tso_len = 0;
3023         txdp->tx.ol_type_vlan_len_msec = 0;
3024         txdp->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(bd_flag);
3025 }
3026
3027 static inline void
3028 hns3_tx_fill_hw_ring(struct hns3_tx_queue *txq,
3029                      struct rte_mbuf **pkts,
3030                      uint16_t nb_pkts)
3031 {
3032 #define PER_LOOP_NUM    4
3033 #define PER_LOOP_MASK   (PER_LOOP_NUM - 1)
3034         struct hns3_desc *txdp = &txq->tx_ring[txq->next_to_use];
3035         struct hns3_entry *tx_entry = &txq->sw_ring[txq->next_to_use];
3036         const uint32_t mainpart = (nb_pkts & ((uint32_t)~PER_LOOP_MASK));
3037         const uint32_t leftover = (nb_pkts & ((uint32_t)PER_LOOP_MASK));
3038         uint32_t i;
3039
3040         for (i = 0; i < mainpart; i += PER_LOOP_NUM) {
3041                 hns3_tx_backup_4mbuf(tx_entry + i, pkts + i);
3042                 hns3_tx_setup_4bd(txdp + i, pkts + i);
3043         }
3044         if (unlikely(leftover > 0)) {
3045                 for (i = 0; i < leftover; i++) {
3046                         hns3_tx_backup_1mbuf(tx_entry + mainpart + i,
3047                                              pkts + mainpart + i);
3048                         hns3_tx_setup_1bd(txdp + mainpart + i,
3049                                           pkts + mainpart + i);
3050                 }
3051         }
3052 }
3053
3054 uint16_t
3055 hns3_xmit_pkts_simple(void *tx_queue,
3056                       struct rte_mbuf **tx_pkts,
3057                       uint16_t nb_pkts)
3058 {
3059         struct hns3_tx_queue *txq = tx_queue;
3060         uint16_t nb_tx = 0;
3061
3062         hns3_tx_free_buffer_simple(txq);
3063
3064         nb_pkts = RTE_MIN(txq->tx_bd_ready, nb_pkts);
3065         if (unlikely(nb_pkts == 0)) {
3066                 if (txq->tx_bd_ready == 0)
3067                         txq->queue_full_cnt++;
3068                 return 0;
3069         }
3070
3071         txq->tx_bd_ready -= nb_pkts;
3072         if (txq->next_to_use + nb_pkts > txq->nb_tx_desc) {
3073                 nb_tx = txq->nb_tx_desc - txq->next_to_use;
3074                 hns3_tx_fill_hw_ring(txq, tx_pkts, nb_tx);
3075                 txq->next_to_use = 0;
3076         }
3077
3078         hns3_tx_fill_hw_ring(txq, tx_pkts + nb_tx, nb_pkts - nb_tx);
3079         txq->next_to_use += nb_pkts - nb_tx;
3080
3081         hns3_write_reg_opt(txq->io_tail_reg, nb_pkts);
3082
3083         return nb_pkts;
3084 }
3085
3086 uint16_t
3087 hns3_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
3088 {
3089         struct rte_net_hdr_lens hdr_lens = {0};
3090         struct hns3_tx_queue *txq = tx_queue;
3091         struct hns3_entry *tx_bak_pkt;
3092         struct hns3_desc *tx_ring;
3093         struct rte_mbuf *tx_pkt;
3094         struct rte_mbuf *m_seg;
3095         struct hns3_desc *desc;
3096         uint32_t nb_hold = 0;
3097         uint16_t tx_next_use;
3098         uint16_t tx_pkt_num;
3099         uint16_t tx_bd_max;
3100         uint16_t nb_buf;
3101         uint16_t nb_tx;
3102         uint16_t i;
3103
3104         /* free useless buffer */
3105         hns3_tx_free_useless_buffer(txq);
3106
3107         tx_next_use   = txq->next_to_use;
3108         tx_bd_max     = txq->nb_tx_desc;
3109         tx_pkt_num = nb_pkts;
3110         tx_ring = txq->tx_ring;
3111
3112         /* send packets */
3113         tx_bak_pkt = &txq->sw_ring[tx_next_use];
3114         for (nb_tx = 0; nb_tx < tx_pkt_num; nb_tx++) {
3115                 tx_pkt = *tx_pkts++;
3116
3117                 nb_buf = tx_pkt->nb_segs;
3118
3119                 if (nb_buf > txq->tx_bd_ready) {
3120                         txq->queue_full_cnt++;
3121                         if (nb_tx == 0)
3122                                 return 0;
3123
3124                         goto end_of_tx;
3125                 }
3126
3127                 /*
3128                  * If packet length is less than minimum packet length supported
3129                  * by hardware in Tx direction, driver need to pad it to avoid
3130                  * error.
3131                  */
3132                 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) <
3133                                                 txq->min_tx_pkt_len)) {
3134                         uint16_t add_len;
3135                         char *appended;
3136
3137                         add_len = txq->min_tx_pkt_len -
3138                                          rte_pktmbuf_pkt_len(tx_pkt);
3139                         appended = rte_pktmbuf_append(tx_pkt, add_len);
3140                         if (appended == NULL) {
3141                                 txq->pkt_padding_fail_cnt++;
3142                                 break;
3143                         }
3144
3145                         memset(appended, 0, add_len);
3146                 }
3147
3148                 m_seg = tx_pkt;
3149
3150                 if (hns3_check_non_tso_pkt(nb_buf, &m_seg, tx_pkt, txq))
3151                         goto end_of_tx;
3152
3153                 if (hns3_parse_cksum(txq, tx_next_use, m_seg, &hdr_lens))
3154                         goto end_of_tx;
3155
3156                 i = 0;
3157                 desc = &tx_ring[tx_next_use];
3158
3159                 /*
3160                  * If the packet is divided into multiple Tx Buffer Descriptors,
3161                  * only need to fill vlan, paylen and tso into the first Tx
3162                  * Buffer Descriptor.
3163                  */
3164                 hns3_fill_first_desc(txq, desc, m_seg);
3165
3166                 do {
3167                         desc = &tx_ring[tx_next_use];
3168                         /*
3169                          * Fill valid bits, DMA address and data length for each
3170                          * Tx Buffer Descriptor.
3171                          */
3172                         hns3_fill_per_desc(desc, m_seg);
3173                         tx_bak_pkt->mbuf = m_seg;
3174                         m_seg = m_seg->next;
3175                         tx_next_use++;
3176                         tx_bak_pkt++;
3177                         if (tx_next_use >= tx_bd_max) {
3178                                 tx_next_use = 0;
3179                                 tx_bak_pkt = txq->sw_ring;
3180                         }
3181
3182                         i++;
3183                 } while (m_seg != NULL);
3184
3185                 /* Add end flag for the last Tx Buffer Descriptor */
3186                 desc->tx.tp_fe_sc_vld_ra_ri |=
3187                                  rte_cpu_to_le_16(BIT(HNS3_TXD_FE_B));
3188
3189                 nb_hold += i;
3190                 txq->next_to_use = tx_next_use;
3191                 txq->tx_bd_ready -= i;
3192         }
3193
3194 end_of_tx:
3195
3196         if (likely(nb_tx))
3197                 hns3_write_reg_opt(txq->io_tail_reg, nb_hold);
3198
3199         return nb_tx;
3200 }
3201
3202 int __rte_weak
3203 hns3_tx_check_vec_support(__rte_unused struct rte_eth_dev *dev)
3204 {
3205         return -ENOTSUP;
3206 }
3207
3208 uint16_t __rte_weak
3209 hns3_xmit_pkts_vec(__rte_unused void *tx_queue,
3210                    __rte_unused struct rte_mbuf **tx_pkts,
3211                    __rte_unused uint16_t nb_pkts)
3212 {
3213         return 0;
3214 }
3215
3216 int
3217 hns3_tx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
3218                        struct rte_eth_burst_mode *mode)
3219 {
3220         eth_tx_burst_t pkt_burst = dev->tx_pkt_burst;
3221         const char *info = NULL;
3222
3223         if (pkt_burst == hns3_xmit_pkts_simple)
3224                 info = "Scalar Simple";
3225         else if (pkt_burst == hns3_xmit_pkts)
3226                 info = "Scalar";
3227         else if (pkt_burst == hns3_xmit_pkts_vec)
3228                 info = "Vector Neon";
3229
3230         if (info == NULL)
3231                 return -EINVAL;
3232
3233         snprintf(mode->info, sizeof(mode->info), "%s", info);
3234
3235         return 0;
3236 }
3237
3238 static eth_tx_burst_t
3239 hns3_get_tx_function(struct rte_eth_dev *dev, eth_tx_prep_t *prep)
3240 {
3241         uint64_t offloads = dev->data->dev_conf.txmode.offloads;
3242         struct hns3_adapter *hns = dev->data->dev_private;
3243
3244         if (hns->tx_vec_allowed && hns3_tx_check_vec_support(dev) == 0) {
3245                 *prep = NULL;
3246                 return hns3_xmit_pkts_vec;
3247         }
3248
3249         if (hns->tx_simple_allowed &&
3250             offloads == (offloads & DEV_TX_OFFLOAD_MBUF_FAST_FREE)) {
3251                 *prep = NULL;
3252                 return hns3_xmit_pkts_simple;
3253         }
3254
3255         *prep = hns3_prep_pkts;
3256         return hns3_xmit_pkts;
3257 }
3258
3259 static uint16_t
3260 hns3_dummy_rxtx_burst(void *dpdk_txq __rte_unused,
3261                       struct rte_mbuf **pkts __rte_unused,
3262                       uint16_t pkts_n __rte_unused)
3263 {
3264         return 0;
3265 }
3266
3267 void hns3_set_rxtx_function(struct rte_eth_dev *eth_dev)
3268 {
3269         struct hns3_adapter *hns = eth_dev->data->dev_private;
3270         eth_tx_prep_t prep = NULL;
3271
3272         if (hns->hw.adapter_state == HNS3_NIC_STARTED &&
3273             rte_atomic16_read(&hns->hw.reset.resetting) == 0) {
3274                 eth_dev->rx_pkt_burst = hns3_get_rx_function(eth_dev);
3275                 eth_dev->tx_pkt_burst = hns3_get_tx_function(eth_dev, &prep);
3276                 eth_dev->tx_pkt_prepare = prep;
3277         } else {
3278                 eth_dev->rx_pkt_burst = hns3_dummy_rxtx_burst;
3279                 eth_dev->tx_pkt_burst = hns3_dummy_rxtx_burst;
3280                 eth_dev->tx_pkt_prepare = hns3_dummy_rxtx_burst;
3281         }
3282 }
3283
3284 void
3285 hns3_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
3286                   struct rte_eth_rxq_info *qinfo)
3287 {
3288         struct hns3_rx_queue *rxq = dev->data->rx_queues[queue_id];
3289
3290         qinfo->mp = rxq->mb_pool;
3291         qinfo->nb_desc = rxq->nb_rx_desc;
3292         qinfo->scattered_rx = dev->data->scattered_rx;
3293
3294         /*
3295          * If there are no available Rx buffer descriptors, incoming packets
3296          * are always dropped by hardware based on hns3 network engine.
3297          */
3298         qinfo->conf.rx_drop_en = 1;
3299         qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
3300         qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
3301         qinfo->conf.rx_deferred_start = rxq->rx_deferred_start;
3302 }
3303
3304 void
3305 hns3_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
3306                   struct rte_eth_txq_info *qinfo)
3307 {
3308         struct hns3_tx_queue *txq = dev->data->tx_queues[queue_id];
3309
3310         qinfo->nb_desc = txq->nb_tx_desc;
3311         qinfo->conf.offloads = dev->data->dev_conf.txmode.offloads;
3312         qinfo->conf.tx_rs_thresh = txq->tx_rs_thresh;
3313         qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
3314         qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
3315 }