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