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