1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2019 Hisilicon Limited.
11 #include <rte_bus_pci.h>
12 #include <rte_byteorder.h>
13 #include <rte_common.h>
14 #include <rte_cycles.h>
17 #include <rte_ether.h>
18 #include <rte_vxlan.h>
19 #include <rte_ethdev_driver.h>
24 #include <rte_malloc.h>
27 #include "hns3_ethdev.h"
28 #include "hns3_rxtx.h"
29 #include "hns3_regs.h"
30 #include "hns3_logs.h"
32 #define HNS3_CFG_DESC_NUM(num) ((num) / 8 - 1)
33 #define HNS3_RX_RING_PREFETCTH_MASK 3
36 hns3_rx_queue_release_mbufs(struct hns3_rx_queue *rxq)
40 /* Note: Fake rx queue will not enter here */
41 if (rxq->sw_ring == NULL)
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);
50 for (i = rxq->next_to_use;
51 i != rxq->rx_rearm_start;
52 i = (i + 1) % rxq->nb_rx_desc) {
53 if (rxq->sw_ring[i].mbuf != NULL)
54 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
58 for (i = 0; i < rxq->bulk_mbuf_num; i++)
59 rte_pktmbuf_free_seg(rxq->bulk_mbuf[i]);
60 rxq->bulk_mbuf_num = 0;
62 if (rxq->pkt_first_seg) {
63 rte_pktmbuf_free(rxq->pkt_first_seg);
64 rxq->pkt_first_seg = NULL;
69 hns3_tx_queue_release_mbufs(struct hns3_tx_queue *txq)
73 /* Note: Fake rx queue will not enter here */
75 for (i = 0; i < txq->nb_tx_desc; i++) {
76 if (txq->sw_ring[i].mbuf) {
77 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
78 txq->sw_ring[i].mbuf = NULL;
85 hns3_rx_queue_release(void *queue)
87 struct hns3_rx_queue *rxq = queue;
89 hns3_rx_queue_release_mbufs(rxq);
91 rte_memzone_free(rxq->mz);
93 rte_free(rxq->sw_ring);
99 hns3_tx_queue_release(void *queue)
101 struct hns3_tx_queue *txq = queue;
103 hns3_tx_queue_release_mbufs(txq);
105 rte_memzone_free(txq->mz);
107 rte_free(txq->sw_ring);
115 hns3_dev_rx_queue_release(void *queue)
117 struct hns3_rx_queue *rxq = queue;
118 struct hns3_adapter *hns;
124 rte_spinlock_lock(&hns->hw.lock);
125 hns3_rx_queue_release(queue);
126 rte_spinlock_unlock(&hns->hw.lock);
130 hns3_dev_tx_queue_release(void *queue)
132 struct hns3_tx_queue *txq = queue;
133 struct hns3_adapter *hns;
139 rte_spinlock_lock(&hns->hw.lock);
140 hns3_tx_queue_release(queue);
141 rte_spinlock_unlock(&hns->hw.lock);
145 hns3_fake_rx_queue_release(struct hns3_rx_queue *queue)
147 struct hns3_rx_queue *rxq = queue;
148 struct hns3_adapter *hns;
158 if (hw->fkq_data.rx_queues[idx]) {
159 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
160 hw->fkq_data.rx_queues[idx] = NULL;
163 /* free fake rx queue arrays */
164 if (idx == (hw->fkq_data.nb_fake_rx_queues - 1)) {
165 hw->fkq_data.nb_fake_rx_queues = 0;
166 rte_free(hw->fkq_data.rx_queues);
167 hw->fkq_data.rx_queues = NULL;
172 hns3_fake_tx_queue_release(struct hns3_tx_queue *queue)
174 struct hns3_tx_queue *txq = queue;
175 struct hns3_adapter *hns;
185 if (hw->fkq_data.tx_queues[idx]) {
186 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
187 hw->fkq_data.tx_queues[idx] = NULL;
190 /* free fake tx queue arrays */
191 if (idx == (hw->fkq_data.nb_fake_tx_queues - 1)) {
192 hw->fkq_data.nb_fake_tx_queues = 0;
193 rte_free(hw->fkq_data.tx_queues);
194 hw->fkq_data.tx_queues = NULL;
199 hns3_free_rx_queues(struct rte_eth_dev *dev)
201 struct hns3_adapter *hns = dev->data->dev_private;
202 struct hns3_fake_queue_data *fkq_data;
203 struct hns3_hw *hw = &hns->hw;
207 nb_rx_q = hw->data->nb_rx_queues;
208 for (i = 0; i < nb_rx_q; i++) {
209 if (dev->data->rx_queues[i]) {
210 hns3_rx_queue_release(dev->data->rx_queues[i]);
211 dev->data->rx_queues[i] = NULL;
215 /* Free fake Rx queues */
216 fkq_data = &hw->fkq_data;
217 for (i = 0; i < fkq_data->nb_fake_rx_queues; i++) {
218 if (fkq_data->rx_queues[i])
219 hns3_fake_rx_queue_release(fkq_data->rx_queues[i]);
224 hns3_free_tx_queues(struct rte_eth_dev *dev)
226 struct hns3_adapter *hns = dev->data->dev_private;
227 struct hns3_fake_queue_data *fkq_data;
228 struct hns3_hw *hw = &hns->hw;
232 nb_tx_q = hw->data->nb_tx_queues;
233 for (i = 0; i < nb_tx_q; i++) {
234 if (dev->data->tx_queues[i]) {
235 hns3_tx_queue_release(dev->data->tx_queues[i]);
236 dev->data->tx_queues[i] = NULL;
240 /* Free fake Tx queues */
241 fkq_data = &hw->fkq_data;
242 for (i = 0; i < fkq_data->nb_fake_tx_queues; i++) {
243 if (fkq_data->tx_queues[i])
244 hns3_fake_tx_queue_release(fkq_data->tx_queues[i]);
249 hns3_free_all_queues(struct rte_eth_dev *dev)
251 hns3_free_rx_queues(dev);
252 hns3_free_tx_queues(dev);
256 hns3_alloc_rx_queue_mbufs(struct hns3_hw *hw, struct hns3_rx_queue *rxq)
258 struct rte_mbuf *mbuf;
262 for (i = 0; i < rxq->nb_rx_desc; i++) {
263 mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
264 if (unlikely(mbuf == NULL)) {
265 hns3_err(hw, "Failed to allocate RXD[%d] for rx queue!",
267 hns3_rx_queue_release_mbufs(rxq);
271 rte_mbuf_refcnt_set(mbuf, 1);
273 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
275 mbuf->port = rxq->port_id;
277 rxq->sw_ring[i].mbuf = mbuf;
278 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
279 rxq->rx_ring[i].addr = dma_addr;
280 rxq->rx_ring[i].rx.bd_base_info = 0;
287 hns3_buf_size2type(uint32_t buf_size)
293 bd_size_type = HNS3_BD_SIZE_512_TYPE;
296 bd_size_type = HNS3_BD_SIZE_1024_TYPE;
299 bd_size_type = HNS3_BD_SIZE_4096_TYPE;
302 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
309 hns3_init_rx_queue_hw(struct hns3_rx_queue *rxq)
311 uint32_t rx_buf_len = rxq->rx_buf_len;
312 uint64_t dma_addr = rxq->rx_ring_phys_addr;
314 hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_L_REG, (uint32_t)dma_addr);
315 hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_H_REG,
316 (uint32_t)((dma_addr >> 31) >> 1));
318 hns3_write_dev(rxq, HNS3_RING_RX_BD_LEN_REG,
319 hns3_buf_size2type(rx_buf_len));
320 hns3_write_dev(rxq, HNS3_RING_RX_BD_NUM_REG,
321 HNS3_CFG_DESC_NUM(rxq->nb_rx_desc));
325 hns3_init_tx_queue_hw(struct hns3_tx_queue *txq)
327 uint64_t dma_addr = txq->tx_ring_phys_addr;
329 hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_L_REG, (uint32_t)dma_addr);
330 hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_H_REG,
331 (uint32_t)((dma_addr >> 31) >> 1));
333 hns3_write_dev(txq, HNS3_RING_TX_BD_NUM_REG,
334 HNS3_CFG_DESC_NUM(txq->nb_tx_desc));
338 hns3_update_all_queues_pvid_state(struct hns3_hw *hw)
340 uint16_t nb_rx_q = hw->data->nb_rx_queues;
341 uint16_t nb_tx_q = hw->data->nb_tx_queues;
342 struct hns3_rx_queue *rxq;
343 struct hns3_tx_queue *txq;
347 pvid_state = hw->port_base_vlan_cfg.state;
348 for (i = 0; i < hw->cfg_max_queues; i++) {
350 rxq = hw->data->rx_queues[i];
352 rxq->pvid_state = pvid_state;
355 txq = hw->data->tx_queues[i];
357 txq->pvid_state = pvid_state;
363 hns3_enable_all_queues(struct hns3_hw *hw, bool en)
365 uint16_t nb_rx_q = hw->data->nb_rx_queues;
366 uint16_t nb_tx_q = hw->data->nb_tx_queues;
367 struct hns3_rx_queue *rxq;
368 struct hns3_tx_queue *txq;
372 for (i = 0; i < hw->cfg_max_queues; i++) {
374 rxq = hw->data->rx_queues[i];
376 rxq = hw->fkq_data.rx_queues[i - nb_rx_q];
378 txq = hw->data->tx_queues[i];
380 txq = hw->fkq_data.tx_queues[i - nb_tx_q];
381 if (rxq == NULL || txq == NULL ||
382 (en && (rxq->rx_deferred_start || txq->tx_deferred_start)))
385 rcb_reg = hns3_read_dev(rxq, HNS3_RING_EN_REG);
387 rcb_reg |= BIT(HNS3_RING_EN_B);
389 rcb_reg &= ~BIT(HNS3_RING_EN_B);
390 hns3_write_dev(rxq, HNS3_RING_EN_REG, rcb_reg);
395 hns3_tqp_enable(struct hns3_hw *hw, uint16_t queue_id, bool enable)
397 struct hns3_cfg_com_tqp_queue_cmd *req;
398 struct hns3_cmd_desc desc;
401 req = (struct hns3_cfg_com_tqp_queue_cmd *)desc.data;
403 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_COM_TQP_QUEUE, false);
404 req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
406 hns3_set_bit(req->enable, HNS3_TQP_ENABLE_B, enable ? 1 : 0);
408 ret = hns3_cmd_send(hw, &desc, 1);
410 hns3_err(hw, "TQP enable fail, ret = %d", ret);
416 hns3_send_reset_tqp_cmd(struct hns3_hw *hw, uint16_t queue_id, bool enable)
418 struct hns3_reset_tqp_queue_cmd *req;
419 struct hns3_cmd_desc desc;
422 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, false);
424 req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
425 req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
426 hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);
428 ret = hns3_cmd_send(hw, &desc, 1);
430 hns3_err(hw, "Send tqp reset cmd error, ret = %d", ret);
436 hns3_get_reset_status(struct hns3_hw *hw, uint16_t queue_id)
438 struct hns3_reset_tqp_queue_cmd *req;
439 struct hns3_cmd_desc desc;
442 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, true);
444 req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
445 req->tqp_id = rte_cpu_to_le_16(queue_id & HNS3_RING_ID_MASK);
447 ret = hns3_cmd_send(hw, &desc, 1);
449 hns3_err(hw, "Get reset status error, ret =%d", ret);
453 return hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
457 hns3_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
459 #define HNS3_TQP_RESET_TRY_MS 200
464 ret = hns3_tqp_enable(hw, queue_id, false);
469 * In current version VF is not supported when PF is driven by DPDK
470 * driver, all task queue pairs are mapped to PF function, so PF's queue
471 * id is equals to the global queue id in PF range.
473 ret = hns3_send_reset_tqp_cmd(hw, queue_id, true);
475 hns3_err(hw, "Send reset tqp cmd fail, ret = %d", ret);
479 end = get_timeofday_ms() + HNS3_TQP_RESET_TRY_MS;
481 /* Wait for tqp hw reset */
482 rte_delay_ms(HNS3_POLL_RESPONE_MS);
483 reset_status = hns3_get_reset_status(hw, queue_id);
488 } while (get_timeofday_ms() < end);
491 hns3_err(hw, "Reset TQP fail, ret = %d", ret);
495 ret = hns3_send_reset_tqp_cmd(hw, queue_id, false);
497 hns3_err(hw, "Deassert the soft reset fail, ret = %d", ret);
503 hns3vf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
508 /* Disable VF's queue before send queue reset msg to PF */
509 ret = hns3_tqp_enable(hw, queue_id, false);
513 memcpy(msg_data, &queue_id, sizeof(uint16_t));
515 return hns3_send_mbx_msg(hw, HNS3_MBX_QUEUE_RESET, 0, msg_data,
516 sizeof(msg_data), true, NULL, 0);
520 hns3_reset_queue(struct hns3_adapter *hns, uint16_t queue_id)
522 struct hns3_hw *hw = &hns->hw;
524 return hns3vf_reset_tqp(hw, queue_id);
526 return hns3_reset_tqp(hw, queue_id);
530 hns3_reset_all_queues(struct hns3_adapter *hns)
532 struct hns3_hw *hw = &hns->hw;
535 for (i = 0; i < hw->cfg_max_queues; i++) {
536 ret = hns3_reset_queue(hns, i);
538 hns3_err(hw, "Failed to reset No.%d queue: %d", i, ret);
546 hns3_set_queue_intr_gl(struct hns3_hw *hw, uint16_t queue_id,
547 uint8_t gl_idx, uint16_t gl_value)
549 uint32_t offset[] = {HNS3_TQP_INTR_GL0_REG,
550 HNS3_TQP_INTR_GL1_REG,
551 HNS3_TQP_INTR_GL2_REG};
552 uint32_t addr, value;
554 if (gl_idx >= RTE_DIM(offset) || gl_value > HNS3_TQP_INTR_GL_MAX)
557 addr = offset[gl_idx] + queue_id * HNS3_TQP_INTR_REG_SIZE;
558 if (hw->intr.gl_unit == HNS3_INTR_COALESCE_GL_UINT_1US)
559 value = gl_value | HNS3_TQP_INTR_GL_UNIT_1US;
561 value = HNS3_GL_USEC_TO_REG(gl_value);
563 hns3_write_dev(hw, addr, value);
567 hns3_set_queue_intr_rl(struct hns3_hw *hw, uint16_t queue_id, uint16_t rl_value)
569 uint32_t addr, value;
571 if (rl_value > HNS3_TQP_INTR_RL_MAX)
574 addr = HNS3_TQP_INTR_RL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
575 value = HNS3_RL_USEC_TO_REG(rl_value);
577 value |= HNS3_TQP_INTR_RL_ENABLE_MASK;
579 hns3_write_dev(hw, addr, value);
583 hns3_set_queue_intr_ql(struct hns3_hw *hw, uint16_t queue_id, uint16_t ql_value)
587 if (hw->intr.coalesce_mode == HNS3_INTR_COALESCE_NON_QL)
590 addr = HNS3_TQP_INTR_TX_QL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
591 hns3_write_dev(hw, addr, ql_value);
593 addr = HNS3_TQP_INTR_RX_QL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
594 hns3_write_dev(hw, addr, ql_value);
598 hns3_queue_intr_enable(struct hns3_hw *hw, uint16_t queue_id, bool en)
600 uint32_t addr, value;
602 addr = HNS3_TQP_INTR_CTRL_REG + queue_id * HNS3_TQP_INTR_REG_SIZE;
605 hns3_write_dev(hw, addr, value);
609 * Enable all rx queue interrupt when in interrupt rx mode.
610 * This api was called before enable queue rx&tx (in normal start or reset
611 * recover scenes), used to fix hardware rx queue interrupt enable was clear
615 hns3_dev_all_rx_queue_intr_enable(struct hns3_hw *hw, bool en)
617 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
618 uint16_t nb_rx_q = hw->data->nb_rx_queues;
621 if (dev->data->dev_conf.intr_conf.rxq == 0)
624 for (i = 0; i < nb_rx_q; i++)
625 hns3_queue_intr_enable(hw, i, en);
629 hns3_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
631 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
632 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
633 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
635 if (dev->data->dev_conf.intr_conf.rxq == 0)
638 hns3_queue_intr_enable(hw, queue_id, true);
640 return rte_intr_ack(intr_handle);
644 hns3_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
646 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
648 if (dev->data->dev_conf.intr_conf.rxq == 0)
651 hns3_queue_intr_enable(hw, queue_id, false);
657 hns3_dev_rx_queue_start(struct hns3_adapter *hns, uint16_t idx)
659 struct hns3_hw *hw = &hns->hw;
660 struct hns3_rx_queue *rxq;
663 PMD_INIT_FUNC_TRACE();
665 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[idx];
666 ret = hns3_alloc_rx_queue_mbufs(hw, rxq);
668 hns3_err(hw, "Failed to alloc mbuf for No.%d rx queue: %d",
673 rxq->next_to_use = 0;
674 rxq->rx_rearm_start = 0;
675 rxq->rx_free_hold = 0;
676 rxq->rx_rearm_nb = 0;
677 rxq->pkt_first_seg = NULL;
678 rxq->pkt_last_seg = NULL;
679 hns3_init_rx_queue_hw(rxq);
680 hns3_rxq_vec_setup(rxq);
686 hns3_fake_rx_queue_start(struct hns3_adapter *hns, uint16_t idx)
688 struct hns3_hw *hw = &hns->hw;
689 struct hns3_rx_queue *rxq;
691 rxq = (struct hns3_rx_queue *)hw->fkq_data.rx_queues[idx];
692 rxq->next_to_use = 0;
693 rxq->rx_free_hold = 0;
694 rxq->rx_rearm_start = 0;
695 rxq->rx_rearm_nb = 0;
696 hns3_init_rx_queue_hw(rxq);
700 hns3_init_tx_queue(struct hns3_tx_queue *queue)
702 struct hns3_tx_queue *txq = queue;
703 struct hns3_desc *desc;
708 for (i = 0; i < txq->nb_tx_desc; i++) {
709 desc->tx.tp_fe_sc_vld_ra_ri = 0;
713 txq->next_to_use = 0;
714 txq->next_to_clean = 0;
715 txq->tx_bd_ready = txq->nb_tx_desc - 1;
716 hns3_init_tx_queue_hw(txq);
720 hns3_dev_tx_queue_start(struct hns3_adapter *hns, uint16_t idx)
722 struct hns3_hw *hw = &hns->hw;
723 struct hns3_tx_queue *txq;
725 txq = (struct hns3_tx_queue *)hw->data->tx_queues[idx];
726 hns3_init_tx_queue(txq);
730 hns3_fake_tx_queue_start(struct hns3_adapter *hns, uint16_t idx)
732 struct hns3_hw *hw = &hns->hw;
733 struct hns3_tx_queue *txq;
735 txq = (struct hns3_tx_queue *)hw->fkq_data.tx_queues[idx];
736 hns3_init_tx_queue(txq);
740 hns3_init_tx_ring_tc(struct hns3_adapter *hns)
742 struct hns3_hw *hw = &hns->hw;
743 struct hns3_tx_queue *txq;
746 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
747 struct hns3_tc_queue_info *tc_queue = &hw->tc_queue[i];
750 if (!tc_queue->enable)
753 for (j = 0; j < tc_queue->tqp_count; j++) {
754 num = tc_queue->tqp_offset + j;
755 txq = (struct hns3_tx_queue *)hw->data->tx_queues[num];
759 hns3_write_dev(txq, HNS3_RING_TX_TC_REG, tc_queue->tc);
765 hns3_start_rx_queues(struct hns3_adapter *hns)
767 struct hns3_hw *hw = &hns->hw;
768 struct hns3_rx_queue *rxq;
772 /* Initialize RSS for queues */
773 ret = hns3_config_rss(hns);
775 hns3_err(hw, "Failed to configure rss %d", ret);
779 for (i = 0; i < hw->data->nb_rx_queues; i++) {
780 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[i];
781 if (rxq == NULL || rxq->rx_deferred_start)
783 ret = hns3_dev_rx_queue_start(hns, i);
785 hns3_err(hw, "Failed to start No.%d rx queue: %d", i,
791 for (i = 0; i < hw->fkq_data.nb_fake_rx_queues; i++) {
792 rxq = (struct hns3_rx_queue *)hw->fkq_data.rx_queues[i];
793 if (rxq == NULL || rxq->rx_deferred_start)
795 hns3_fake_rx_queue_start(hns, i);
800 for (j = 0; j < i; j++) {
801 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[j];
802 hns3_rx_queue_release_mbufs(rxq);
809 hns3_start_tx_queues(struct hns3_adapter *hns)
811 struct hns3_hw *hw = &hns->hw;
812 struct hns3_tx_queue *txq;
815 for (i = 0; i < hw->data->nb_tx_queues; i++) {
816 txq = (struct hns3_tx_queue *)hw->data->tx_queues[i];
817 if (txq == NULL || txq->tx_deferred_start)
819 hns3_dev_tx_queue_start(hns, i);
822 for (i = 0; i < hw->fkq_data.nb_fake_tx_queues; i++) {
823 txq = (struct hns3_tx_queue *)hw->fkq_data.tx_queues[i];
824 if (txq == NULL || txq->tx_deferred_start)
826 hns3_fake_tx_queue_start(hns, i);
829 hns3_init_tx_ring_tc(hns);
834 * Note: just init and setup queues, and don't enable queue rx&tx.
837 hns3_start_queues(struct hns3_adapter *hns, bool reset_queue)
839 struct hns3_hw *hw = &hns->hw;
843 ret = hns3_reset_all_queues(hns);
845 hns3_err(hw, "Failed to reset all queues %d", ret);
850 ret = hns3_start_rx_queues(hns);
852 hns3_err(hw, "Failed to start rx queues: %d", ret);
856 hns3_start_tx_queues(hns);
862 hns3_stop_queues(struct hns3_adapter *hns, bool reset_queue)
864 struct hns3_hw *hw = &hns->hw;
867 hns3_enable_all_queues(hw, false);
869 ret = hns3_reset_all_queues(hns);
871 hns3_err(hw, "Failed to reset all queues %d", ret);
879 * Iterate over all Rx Queue, and call the callback() function for each Rx
883 * The target eth dev.
884 * @param[in] callback
885 * The function to call for each queue.
886 * if callback function return nonzero will stop iterate and return it's value
888 * The arguments to provide the callback function with.
891 * 0 on success, otherwise with errno set.
894 hns3_rxq_iterate(struct rte_eth_dev *dev,
895 int (*callback)(struct hns3_rx_queue *, void *), void *arg)
900 if (dev->data->rx_queues == NULL)
903 for (i = 0; i < dev->data->nb_rx_queues; i++) {
904 ret = callback(dev->data->rx_queues[i], arg);
913 hns3_alloc_rxq_and_dma_zone(struct rte_eth_dev *dev,
914 struct hns3_queue_info *q_info)
916 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
917 const struct rte_memzone *rx_mz;
918 struct hns3_rx_queue *rxq;
919 unsigned int rx_desc;
921 rxq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_rx_queue),
922 RTE_CACHE_LINE_SIZE, q_info->socket_id);
924 hns3_err(hw, "Failed to allocate memory for No.%d rx ring!",
929 /* Allocate rx ring hardware descriptors. */
930 rxq->queue_id = q_info->idx;
931 rxq->nb_rx_desc = q_info->nb_desc;
934 * Allocate a litter more memory because rx vector functions
935 * don't check boundaries each time.
937 rx_desc = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
938 sizeof(struct hns3_desc);
939 rx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
940 rx_desc, HNS3_RING_BASE_ALIGN,
943 hns3_err(hw, "Failed to reserve DMA memory for No.%d rx ring!",
945 hns3_rx_queue_release(rxq);
949 rxq->rx_ring = (struct hns3_desc *)rx_mz->addr;
950 rxq->rx_ring_phys_addr = rx_mz->iova;
952 hns3_dbg(hw, "No.%d rx descriptors iova 0x%" PRIx64, q_info->idx,
953 rxq->rx_ring_phys_addr);
959 hns3_fake_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
960 uint16_t nb_desc, unsigned int socket_id)
962 struct hns3_adapter *hns = dev->data->dev_private;
963 struct hns3_hw *hw = &hns->hw;
964 struct hns3_queue_info q_info;
965 struct hns3_rx_queue *rxq;
968 if (hw->fkq_data.rx_queues[idx]) {
969 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
970 hw->fkq_data.rx_queues[idx] = NULL;
974 q_info.socket_id = socket_id;
975 q_info.nb_desc = nb_desc;
976 q_info.type = "hns3 fake RX queue";
977 q_info.ring_name = "rx_fake_ring";
978 rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
980 hns3_err(hw, "Failed to setup No.%d fake rx ring.", idx);
984 /* Don't need alloc sw_ring, because upper applications don't use it */
988 rxq->rx_deferred_start = false;
989 rxq->port_id = dev->data->port_id;
990 rxq->configured = true;
991 nb_rx_q = dev->data->nb_rx_queues;
992 rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
993 (nb_rx_q + idx) * HNS3_TQP_REG_SIZE);
994 rxq->rx_buf_len = HNS3_MIN_BD_BUF_SIZE;
996 rte_spinlock_lock(&hw->lock);
997 hw->fkq_data.rx_queues[idx] = rxq;
998 rte_spinlock_unlock(&hw->lock);
1004 hns3_alloc_txq_and_dma_zone(struct rte_eth_dev *dev,
1005 struct hns3_queue_info *q_info)
1007 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1008 const struct rte_memzone *tx_mz;
1009 struct hns3_tx_queue *txq;
1010 struct hns3_desc *desc;
1011 unsigned int tx_desc;
1014 txq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_tx_queue),
1015 RTE_CACHE_LINE_SIZE, q_info->socket_id);
1017 hns3_err(hw, "Failed to allocate memory for No.%d tx ring!",
1022 /* Allocate tx ring hardware descriptors. */
1023 txq->queue_id = q_info->idx;
1024 txq->nb_tx_desc = q_info->nb_desc;
1025 tx_desc = txq->nb_tx_desc * sizeof(struct hns3_desc);
1026 tx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
1027 tx_desc, HNS3_RING_BASE_ALIGN,
1029 if (tx_mz == NULL) {
1030 hns3_err(hw, "Failed to reserve DMA memory for No.%d tx ring!",
1032 hns3_tx_queue_release(txq);
1036 txq->tx_ring = (struct hns3_desc *)tx_mz->addr;
1037 txq->tx_ring_phys_addr = tx_mz->iova;
1039 hns3_dbg(hw, "No.%d tx descriptors iova 0x%" PRIx64, q_info->idx,
1040 txq->tx_ring_phys_addr);
1043 desc = txq->tx_ring;
1044 for (i = 0; i < txq->nb_tx_desc; i++) {
1045 desc->tx.tp_fe_sc_vld_ra_ri = 0;
1053 hns3_fake_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
1054 uint16_t nb_desc, unsigned int socket_id)
1056 struct hns3_adapter *hns = dev->data->dev_private;
1057 struct hns3_hw *hw = &hns->hw;
1058 struct hns3_queue_info q_info;
1059 struct hns3_tx_queue *txq;
1062 if (hw->fkq_data.tx_queues[idx] != NULL) {
1063 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
1064 hw->fkq_data.tx_queues[idx] = NULL;
1068 q_info.socket_id = socket_id;
1069 q_info.nb_desc = nb_desc;
1070 q_info.type = "hns3 fake TX queue";
1071 q_info.ring_name = "tx_fake_ring";
1072 txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
1074 hns3_err(hw, "Failed to setup No.%d fake tx ring.", idx);
1078 /* Don't need alloc sw_ring, because upper applications don't use it */
1079 txq->sw_ring = NULL;
1083 txq->tx_deferred_start = false;
1084 txq->port_id = dev->data->port_id;
1085 txq->configured = true;
1086 nb_tx_q = dev->data->nb_tx_queues;
1087 txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1088 (nb_tx_q + idx) * HNS3_TQP_REG_SIZE);
1090 rte_spinlock_lock(&hw->lock);
1091 hw->fkq_data.tx_queues[idx] = txq;
1092 rte_spinlock_unlock(&hw->lock);
1098 hns3_fake_rx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
1100 uint16_t old_nb_queues = hw->fkq_data.nb_fake_rx_queues;
1104 if (hw->fkq_data.rx_queues == NULL && nb_queues != 0) {
1105 /* first time configuration */
1107 size = sizeof(hw->fkq_data.rx_queues[0]) * nb_queues;
1108 hw->fkq_data.rx_queues = rte_zmalloc("fake_rx_queues", size,
1109 RTE_CACHE_LINE_SIZE);
1110 if (hw->fkq_data.rx_queues == NULL) {
1111 hw->fkq_data.nb_fake_rx_queues = 0;
1114 } else if (hw->fkq_data.rx_queues != NULL && nb_queues != 0) {
1116 rxq = hw->fkq_data.rx_queues;
1117 for (i = nb_queues; i < old_nb_queues; i++)
1118 hns3_dev_rx_queue_release(rxq[i]);
1120 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
1121 RTE_CACHE_LINE_SIZE);
1124 if (nb_queues > old_nb_queues) {
1125 uint16_t new_qs = nb_queues - old_nb_queues;
1126 memset(rxq + old_nb_queues, 0, sizeof(rxq[0]) * new_qs);
1129 hw->fkq_data.rx_queues = rxq;
1130 } else if (hw->fkq_data.rx_queues != NULL && nb_queues == 0) {
1131 rxq = hw->fkq_data.rx_queues;
1132 for (i = nb_queues; i < old_nb_queues; i++)
1133 hns3_dev_rx_queue_release(rxq[i]);
1135 rte_free(hw->fkq_data.rx_queues);
1136 hw->fkq_data.rx_queues = NULL;
1139 hw->fkq_data.nb_fake_rx_queues = nb_queues;
1145 hns3_fake_tx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
1147 uint16_t old_nb_queues = hw->fkq_data.nb_fake_tx_queues;
1151 if (hw->fkq_data.tx_queues == NULL && nb_queues != 0) {
1152 /* first time configuration */
1154 size = sizeof(hw->fkq_data.tx_queues[0]) * nb_queues;
1155 hw->fkq_data.tx_queues = rte_zmalloc("fake_tx_queues", size,
1156 RTE_CACHE_LINE_SIZE);
1157 if (hw->fkq_data.tx_queues == NULL) {
1158 hw->fkq_data.nb_fake_tx_queues = 0;
1161 } else if (hw->fkq_data.tx_queues != NULL && nb_queues != 0) {
1163 txq = hw->fkq_data.tx_queues;
1164 for (i = nb_queues; i < old_nb_queues; i++)
1165 hns3_dev_tx_queue_release(txq[i]);
1166 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
1167 RTE_CACHE_LINE_SIZE);
1170 if (nb_queues > old_nb_queues) {
1171 uint16_t new_qs = nb_queues - old_nb_queues;
1172 memset(txq + old_nb_queues, 0, sizeof(txq[0]) * new_qs);
1175 hw->fkq_data.tx_queues = txq;
1176 } else if (hw->fkq_data.tx_queues != NULL && nb_queues == 0) {
1177 txq = hw->fkq_data.tx_queues;
1178 for (i = nb_queues; i < old_nb_queues; i++)
1179 hns3_dev_tx_queue_release(txq[i]);
1181 rte_free(hw->fkq_data.tx_queues);
1182 hw->fkq_data.tx_queues = NULL;
1184 hw->fkq_data.nb_fake_tx_queues = nb_queues;
1190 hns3_set_fake_rx_or_tx_queues(struct rte_eth_dev *dev, uint16_t nb_rx_q,
1193 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1194 uint16_t rx_need_add_nb_q;
1195 uint16_t tx_need_add_nb_q;
1200 /* Setup new number of fake RX/TX queues and reconfigure device. */
1201 hw->cfg_max_queues = RTE_MAX(nb_rx_q, nb_tx_q);
1202 rx_need_add_nb_q = hw->cfg_max_queues - nb_rx_q;
1203 tx_need_add_nb_q = hw->cfg_max_queues - nb_tx_q;
1204 ret = hns3_fake_rx_queue_config(hw, rx_need_add_nb_q);
1206 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1207 goto cfg_fake_rx_q_fail;
1210 ret = hns3_fake_tx_queue_config(hw, tx_need_add_nb_q);
1212 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1213 goto cfg_fake_tx_q_fail;
1216 /* Allocate and set up fake RX queue per Ethernet port. */
1217 port_id = hw->data->port_id;
1218 for (q = 0; q < rx_need_add_nb_q; q++) {
1219 ret = hns3_fake_rx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1220 rte_eth_dev_socket_id(port_id));
1222 goto setup_fake_rx_q_fail;
1225 /* Allocate and set up fake TX queue per Ethernet port. */
1226 for (q = 0; q < tx_need_add_nb_q; q++) {
1227 ret = hns3_fake_tx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1228 rte_eth_dev_socket_id(port_id));
1230 goto setup_fake_tx_q_fail;
1235 setup_fake_tx_q_fail:
1236 setup_fake_rx_q_fail:
1237 (void)hns3_fake_tx_queue_config(hw, 0);
1239 (void)hns3_fake_rx_queue_config(hw, 0);
1241 hw->cfg_max_queues = 0;
1247 hns3_dev_release_mbufs(struct hns3_adapter *hns)
1249 struct rte_eth_dev_data *dev_data = hns->hw.data;
1250 struct hns3_rx_queue *rxq;
1251 struct hns3_tx_queue *txq;
1254 if (dev_data->rx_queues)
1255 for (i = 0; i < dev_data->nb_rx_queues; i++) {
1256 rxq = dev_data->rx_queues[i];
1257 if (rxq == NULL || rxq->rx_deferred_start)
1259 hns3_rx_queue_release_mbufs(rxq);
1262 if (dev_data->tx_queues)
1263 for (i = 0; i < dev_data->nb_tx_queues; i++) {
1264 txq = dev_data->tx_queues[i];
1265 if (txq == NULL || txq->tx_deferred_start)
1267 hns3_tx_queue_release_mbufs(txq);
1272 hns3_rx_buf_len_calc(struct rte_mempool *mp, uint16_t *rx_buf_len)
1274 uint16_t vld_buf_size;
1275 uint16_t num_hw_specs;
1279 * hns3 network engine only support to set 4 typical specification, and
1280 * different buffer size will affect the max packet_len and the max
1281 * number of segmentation when hw gro is turned on in receive side. The
1282 * relationship between them is as follows:
1283 * rx_buf_size | max_gro_pkt_len | max_gro_nb_seg
1284 * ---------------------|-------------------|----------------
1285 * HNS3_4K_BD_BUF_SIZE | 60KB | 15
1286 * HNS3_2K_BD_BUF_SIZE | 62KB | 31
1287 * HNS3_1K_BD_BUF_SIZE | 63KB | 63
1288 * HNS3_512_BD_BUF_SIZE | 31.5KB | 63
1290 static const uint16_t hw_rx_buf_size[] = {
1291 HNS3_4K_BD_BUF_SIZE,
1292 HNS3_2K_BD_BUF_SIZE,
1293 HNS3_1K_BD_BUF_SIZE,
1294 HNS3_512_BD_BUF_SIZE
1297 vld_buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
1298 RTE_PKTMBUF_HEADROOM);
1300 if (vld_buf_size < HNS3_MIN_BD_BUF_SIZE)
1303 num_hw_specs = RTE_DIM(hw_rx_buf_size);
1304 for (i = 0; i < num_hw_specs; i++) {
1305 if (vld_buf_size >= hw_rx_buf_size[i]) {
1306 *rx_buf_len = hw_rx_buf_size[i];
1314 hns3_rx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_rxconf *conf,
1315 struct rte_mempool *mp, uint16_t nb_desc,
1318 if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
1319 nb_desc % HNS3_ALIGN_RING_DESC) {
1320 hns3_err(hw, "Number (%u) of rx descriptors is invalid",
1325 if (conf->rx_drop_en == 0)
1326 hns3_warn(hw, "if no descriptors available, packets are always "
1327 "dropped and rx_drop_en (1) is fixed on");
1329 if (hns3_rx_buf_len_calc(mp, buf_size)) {
1330 hns3_err(hw, "rxq mbufs' data room size (%u) is not enough! "
1331 "minimal data room size (%u).",
1332 rte_pktmbuf_data_room_size(mp),
1333 HNS3_MIN_BD_BUF_SIZE + RTE_PKTMBUF_HEADROOM);
1341 hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
1342 unsigned int socket_id, const struct rte_eth_rxconf *conf,
1343 struct rte_mempool *mp)
1345 struct hns3_adapter *hns = dev->data->dev_private;
1346 struct hns3_hw *hw = &hns->hw;
1347 struct hns3_queue_info q_info;
1348 struct hns3_rx_queue *rxq;
1349 uint16_t rx_buf_size;
1353 if (dev->data->dev_started) {
1354 hns3_err(hw, "rx_queue_setup after dev_start no supported");
1358 ret = hns3_rx_queue_conf_check(hw, conf, mp, nb_desc, &rx_buf_size);
1362 if (dev->data->rx_queues[idx]) {
1363 hns3_rx_queue_release(dev->data->rx_queues[idx]);
1364 dev->data->rx_queues[idx] = NULL;
1368 q_info.socket_id = socket_id;
1369 q_info.nb_desc = nb_desc;
1370 q_info.type = "hns3 RX queue";
1371 q_info.ring_name = "rx_ring";
1373 rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
1376 "Failed to alloc mem and reserve DMA mem for rx ring!");
1381 rxq->ptype_tbl = &hns->ptype_tbl;
1383 rxq->rx_free_thresh = (conf->rx_free_thresh > 0) ?
1384 conf->rx_free_thresh : HNS3_DEFAULT_RX_FREE_THRESH;
1385 rxq->rx_deferred_start = conf->rx_deferred_start;
1387 rx_entry_len = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
1388 sizeof(struct hns3_entry);
1389 rxq->sw_ring = rte_zmalloc_socket("hns3 RX sw ring", rx_entry_len,
1390 RTE_CACHE_LINE_SIZE, socket_id);
1391 if (rxq->sw_ring == NULL) {
1392 hns3_err(hw, "Failed to allocate memory for rx sw ring!");
1393 hns3_rx_queue_release(rxq);
1397 rxq->next_to_use = 0;
1398 rxq->rx_free_hold = 0;
1399 rxq->rx_rearm_start = 0;
1400 rxq->rx_rearm_nb = 0;
1401 rxq->pkt_first_seg = NULL;
1402 rxq->pkt_last_seg = NULL;
1403 rxq->port_id = dev->data->port_id;
1404 rxq->pvid_state = hw->port_base_vlan_cfg.state;
1405 rxq->configured = true;
1406 rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1407 idx * HNS3_TQP_REG_SIZE);
1408 rxq->io_head_reg = (volatile void *)((char *)rxq->io_base +
1409 HNS3_RING_RX_HEAD_REG);
1410 rxq->rx_buf_len = rx_buf_size;
1412 rxq->pkt_len_errors = 0;
1413 rxq->l3_csum_errors = 0;
1414 rxq->l4_csum_errors = 0;
1415 rxq->ol3_csum_errors = 0;
1416 rxq->ol4_csum_errors = 0;
1418 /* CRC len set here is used for amending packet length */
1419 if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
1420 rxq->crc_len = RTE_ETHER_CRC_LEN;
1424 rxq->bulk_mbuf_num = 0;
1426 rte_spinlock_lock(&hw->lock);
1427 dev->data->rx_queues[idx] = rxq;
1428 rte_spinlock_unlock(&hw->lock);
1434 hns3_rx_scattered_reset(struct rte_eth_dev *dev)
1436 struct hns3_adapter *hns = dev->data->dev_private;
1437 struct hns3_hw *hw = &hns->hw;
1440 dev->data->scattered_rx = false;
1444 hns3_rx_scattered_calc(struct rte_eth_dev *dev)
1446 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1447 struct hns3_adapter *hns = dev->data->dev_private;
1448 struct hns3_hw *hw = &hns->hw;
1449 struct hns3_rx_queue *rxq;
1452 if (dev->data->rx_queues == NULL)
1455 for (queue_id = 0; queue_id < dev->data->nb_rx_queues; queue_id++) {
1456 rxq = dev->data->rx_queues[queue_id];
1457 if (hw->rx_buf_len == 0)
1458 hw->rx_buf_len = rxq->rx_buf_len;
1460 hw->rx_buf_len = RTE_MIN(hw->rx_buf_len,
1464 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_SCATTER ||
1465 dev_conf->rxmode.max_rx_pkt_len > hw->rx_buf_len)
1466 dev->data->scattered_rx = true;
1470 hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev)
1472 static const uint32_t ptypes[] = {
1474 RTE_PTYPE_L2_ETHER_VLAN,
1475 RTE_PTYPE_L2_ETHER_QINQ,
1476 RTE_PTYPE_L2_ETHER_LLDP,
1477 RTE_PTYPE_L2_ETHER_ARP,
1479 RTE_PTYPE_L3_IPV4_EXT,
1481 RTE_PTYPE_L3_IPV6_EXT,
1487 RTE_PTYPE_TUNNEL_GRE,
1491 if (dev->rx_pkt_burst == hns3_recv_pkts ||
1492 dev->rx_pkt_burst == hns3_recv_scattered_pkts ||
1493 dev->rx_pkt_burst == hns3_recv_pkts_vec)
1500 hns3_init_rx_ptype_tble(struct rte_eth_dev *dev)
1502 struct hns3_adapter *hns = dev->data->dev_private;
1503 struct hns3_ptype_table *tbl = &hns->ptype_tbl;
1505 memset(tbl, 0, sizeof(*tbl));
1507 tbl->l2table[0] = RTE_PTYPE_L2_ETHER;
1508 tbl->l2table[1] = RTE_PTYPE_L2_ETHER_QINQ;
1509 tbl->l2table[2] = RTE_PTYPE_L2_ETHER_VLAN;
1510 tbl->l2table[3] = RTE_PTYPE_L2_ETHER_VLAN;
1512 tbl->l3table[0] = RTE_PTYPE_L3_IPV4;
1513 tbl->l3table[1] = RTE_PTYPE_L3_IPV6;
1514 tbl->l3table[2] = RTE_PTYPE_L2_ETHER_ARP;
1515 tbl->l3table[3] = RTE_PTYPE_L2_ETHER;
1516 tbl->l3table[4] = RTE_PTYPE_L3_IPV4_EXT;
1517 tbl->l3table[5] = RTE_PTYPE_L3_IPV6_EXT;
1518 tbl->l3table[6] = RTE_PTYPE_L2_ETHER_LLDP;
1520 tbl->l4table[0] = RTE_PTYPE_L4_UDP;
1521 tbl->l4table[1] = RTE_PTYPE_L4_TCP;
1522 tbl->l4table[2] = RTE_PTYPE_TUNNEL_GRE;
1523 tbl->l4table[3] = RTE_PTYPE_L4_SCTP;
1524 tbl->l4table[4] = RTE_PTYPE_L4_IGMP;
1525 tbl->l4table[5] = RTE_PTYPE_L4_ICMP;
1527 tbl->inner_l2table[0] = RTE_PTYPE_INNER_L2_ETHER;
1528 tbl->inner_l2table[1] = RTE_PTYPE_INNER_L2_ETHER_VLAN;
1529 tbl->inner_l2table[2] = RTE_PTYPE_INNER_L2_ETHER_QINQ;
1531 tbl->inner_l3table[0] = RTE_PTYPE_INNER_L3_IPV4;
1532 tbl->inner_l3table[1] = RTE_PTYPE_INNER_L3_IPV6;
1533 tbl->inner_l3table[2] = 0;
1534 tbl->inner_l3table[3] = RTE_PTYPE_INNER_L2_ETHER;
1535 tbl->inner_l3table[4] = RTE_PTYPE_INNER_L3_IPV4_EXT;
1536 tbl->inner_l3table[5] = RTE_PTYPE_INNER_L3_IPV6_EXT;
1538 tbl->inner_l4table[0] = RTE_PTYPE_INNER_L4_UDP;
1539 tbl->inner_l4table[1] = RTE_PTYPE_INNER_L4_TCP;
1540 tbl->inner_l4table[2] = RTE_PTYPE_TUNNEL_GRE;
1541 tbl->inner_l4table[3] = RTE_PTYPE_INNER_L4_SCTP;
1542 tbl->inner_l4table[4] = RTE_PTYPE_L4_IGMP;
1543 tbl->inner_l4table[5] = RTE_PTYPE_INNER_L4_ICMP;
1545 tbl->ol3table[0] = RTE_PTYPE_L3_IPV4;
1546 tbl->ol3table[1] = RTE_PTYPE_L3_IPV6;
1547 tbl->ol3table[2] = 0;
1548 tbl->ol3table[3] = 0;
1549 tbl->ol3table[4] = RTE_PTYPE_L3_IPV4_EXT;
1550 tbl->ol3table[5] = RTE_PTYPE_L3_IPV6_EXT;
1552 tbl->ol4table[0] = 0;
1553 tbl->ol4table[1] = RTE_PTYPE_TUNNEL_VXLAN;
1554 tbl->ol4table[2] = RTE_PTYPE_TUNNEL_NVGRE;
1558 hns3_rxd_to_vlan_tci(struct hns3_rx_queue *rxq, struct rte_mbuf *mb,
1559 uint32_t l234_info, const struct hns3_desc *rxd)
1561 #define HNS3_STRP_STATUS_NUM 0x4
1563 #define HNS3_NO_STRP_VLAN_VLD 0x0
1564 #define HNS3_INNER_STRP_VLAN_VLD 0x1
1565 #define HNS3_OUTER_STRP_VLAN_VLD 0x2
1566 uint32_t strip_status;
1567 uint32_t report_mode;
1570 * Since HW limitation, the vlan tag will always be inserted into RX
1571 * descriptor when strip the tag from packet, driver needs to determine
1572 * reporting which tag to mbuf according to the PVID configuration
1573 * and vlan striped status.
1575 static const uint32_t report_type[][HNS3_STRP_STATUS_NUM] = {
1577 HNS3_NO_STRP_VLAN_VLD,
1578 HNS3_OUTER_STRP_VLAN_VLD,
1579 HNS3_INNER_STRP_VLAN_VLD,
1580 HNS3_OUTER_STRP_VLAN_VLD
1583 HNS3_NO_STRP_VLAN_VLD,
1584 HNS3_NO_STRP_VLAN_VLD,
1585 HNS3_NO_STRP_VLAN_VLD,
1586 HNS3_INNER_STRP_VLAN_VLD
1589 strip_status = hns3_get_field(l234_info, HNS3_RXD_STRP_TAGP_M,
1590 HNS3_RXD_STRP_TAGP_S);
1591 report_mode = report_type[rxq->pvid_state][strip_status];
1592 switch (report_mode) {
1593 case HNS3_NO_STRP_VLAN_VLD:
1596 case HNS3_INNER_STRP_VLAN_VLD:
1597 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
1598 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.vlan_tag);
1600 case HNS3_OUTER_STRP_VLAN_VLD:
1601 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
1602 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.ot_vlan_tag);
1608 recalculate_data_len(struct rte_mbuf *first_seg, struct rte_mbuf *last_seg,
1609 struct rte_mbuf *rxm, struct hns3_rx_queue *rxq,
1612 uint8_t crc_len = rxq->crc_len;
1614 if (data_len <= crc_len) {
1615 rte_pktmbuf_free_seg(rxm);
1616 first_seg->nb_segs--;
1617 last_seg->data_len = (uint16_t)(last_seg->data_len -
1618 (crc_len - data_len));
1619 last_seg->next = NULL;
1621 rxm->data_len = (uint16_t)(data_len - crc_len);
1624 static inline struct rte_mbuf *
1625 hns3_rx_alloc_buffer(struct hns3_rx_queue *rxq)
1629 if (likely(rxq->bulk_mbuf_num > 0))
1630 return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];
1632 ret = rte_mempool_get_bulk(rxq->mb_pool, (void **)rxq->bulk_mbuf,
1633 HNS3_BULK_ALLOC_MBUF_NUM);
1634 if (likely(ret == 0)) {
1635 rxq->bulk_mbuf_num = HNS3_BULK_ALLOC_MBUF_NUM;
1636 return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];
1638 return rte_mbuf_raw_alloc(rxq->mb_pool);
1642 hns3_recv_pkts(void *rx_queue, struct rte_mbuf **rx_pkts, uint16_t nb_pkts)
1644 volatile struct hns3_desc *rx_ring; /* RX ring (desc) */
1645 volatile struct hns3_desc *rxdp; /* pointer of the current desc */
1646 struct hns3_rx_queue *rxq; /* RX queue */
1647 struct hns3_entry *sw_ring;
1648 struct hns3_entry *rxe;
1649 struct hns3_desc rxd;
1650 struct rte_mbuf *nmb; /* pointer of the new mbuf */
1651 struct rte_mbuf *rxm;
1652 uint32_t bd_base_info;
1665 rx_ring = rxq->rx_ring;
1666 sw_ring = rxq->sw_ring;
1667 rx_id = rxq->next_to_use;
1669 while (nb_rx < nb_pkts) {
1670 rxdp = &rx_ring[rx_id];
1671 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
1672 if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
1675 rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
1676 (1u << HNS3_RXD_VLD_B)];
1678 nmb = hns3_rx_alloc_buffer(rxq);
1679 if (unlikely(nmb == NULL)) {
1682 port_id = rxq->port_id;
1683 rte_eth_devices[port_id].data->rx_mbuf_alloc_failed++;
1688 rxe = &sw_ring[rx_id];
1690 if (unlikely(rx_id == rxq->nb_rx_desc))
1693 rte_prefetch0(sw_ring[rx_id].mbuf);
1694 if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
1695 rte_prefetch0(&rx_ring[rx_id]);
1696 rte_prefetch0(&sw_ring[rx_id]);
1702 dma_addr = rte_mbuf_data_iova_default(nmb);
1703 rxdp->addr = rte_cpu_to_le_64(dma_addr);
1704 rxdp->rx.bd_base_info = 0;
1706 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1707 rxm->pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.rx.pkt_len)) -
1709 rxm->data_len = rxm->pkt_len;
1710 rxm->port = rxq->port_id;
1711 rxm->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
1712 rxm->ol_flags = PKT_RX_RSS_HASH;
1713 if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
1715 rte_le_to_cpu_16(rxd.rx.fd_id);
1716 rxm->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
1721 /* Load remained descriptor data and extract necessary fields */
1722 l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
1723 ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
1724 ret = hns3_handle_bdinfo(rxq, rxm, bd_base_info,
1725 l234_info, &cksum_err);
1729 rxm->packet_type = hns3_rx_calc_ptype(rxq, l234_info, ol_info);
1731 if (likely(bd_base_info & BIT(HNS3_RXD_L3L4P_B)))
1732 hns3_rx_set_cksum_flag(rxm, rxm->packet_type,
1734 hns3_rxd_to_vlan_tci(rxq, rxm, l234_info, &rxd);
1736 rx_pkts[nb_rx++] = rxm;
1739 rte_pktmbuf_free(rxm);
1742 rxq->next_to_use = rx_id;
1743 rxq->rx_free_hold += nb_rx_bd;
1744 if (rxq->rx_free_hold > rxq->rx_free_thresh) {
1745 hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
1746 rxq->rx_free_hold = 0;
1753 hns3_recv_scattered_pkts(void *rx_queue,
1754 struct rte_mbuf **rx_pkts,
1757 volatile struct hns3_desc *rx_ring; /* RX ring (desc) */
1758 volatile struct hns3_desc *rxdp; /* pointer of the current desc */
1759 struct hns3_rx_queue *rxq; /* RX queue */
1760 struct hns3_entry *sw_ring;
1761 struct hns3_entry *rxe;
1762 struct rte_mbuf *first_seg;
1763 struct rte_mbuf *last_seg;
1764 struct hns3_desc rxd;
1765 struct rte_mbuf *nmb; /* pointer of the new mbuf */
1766 struct rte_mbuf *rxm;
1767 struct rte_eth_dev *dev;
1768 uint32_t bd_base_info;
1783 rx_id = rxq->next_to_use;
1784 rx_ring = rxq->rx_ring;
1785 sw_ring = rxq->sw_ring;
1786 first_seg = rxq->pkt_first_seg;
1787 last_seg = rxq->pkt_last_seg;
1789 while (nb_rx < nb_pkts) {
1790 rxdp = &rx_ring[rx_id];
1791 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
1792 if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
1796 * The interactive process between software and hardware of
1797 * receiving a new packet in hns3 network engine:
1798 * 1. Hardware network engine firstly writes the packet content
1799 * to the memory pointed by the 'addr' field of the Rx Buffer
1800 * Descriptor, secondly fills the result of parsing the
1801 * packet include the valid field into the Rx Buffer
1802 * Descriptor in one write operation.
1803 * 2. Driver reads the Rx BD's valid field in the loop to check
1804 * whether it's valid, if valid then assign a new address to
1805 * the addr field, clear the valid field, get the other
1806 * information of the packet by parsing Rx BD's other fields,
1807 * finally write back the number of Rx BDs processed by the
1808 * driver to the HNS3_RING_RX_HEAD_REG register to inform
1810 * In the above process, the ordering is very important. We must
1811 * make sure that CPU read Rx BD's other fields only after the
1814 * There are two type of re-ordering: compiler re-ordering and
1815 * CPU re-ordering under the ARMv8 architecture.
1816 * 1. we use volatile to deal with compiler re-ordering, so you
1817 * can see that rx_ring/rxdp defined with volatile.
1818 * 2. we commonly use memory barrier to deal with CPU
1819 * re-ordering, but the cost is high.
1821 * In order to solve the high cost of using memory barrier, we
1822 * use the data dependency order under the ARMv8 architecture,
1825 * instr02: load B <- A
1826 * the instr02 will always execute after instr01.
1828 * To construct the data dependency ordering, we use the
1829 * following assignment:
1830 * rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
1831 * (1u<<HNS3_RXD_VLD_B)]
1832 * Using gcc compiler under the ARMv8 architecture, the related
1833 * assembly code example as follows:
1834 * note: (1u << HNS3_RXD_VLD_B) equal 0x10
1835 * instr01: ldr w26, [x22, #28] --read bd_base_info
1836 * instr02: and w0, w26, #0x10 --calc bd_base_info & 0x10
1837 * instr03: sub w0, w0, #0x10 --calc (bd_base_info &
1839 * instr04: add x0, x22, x0, lsl #5 --calc copy source addr
1840 * instr05: ldp x2, x3, [x0]
1841 * instr06: stp x2, x3, [x29, #256] --copy BD's [0 ~ 15]B
1842 * instr07: ldp x4, x5, [x0, #16]
1843 * instr08: stp x4, x5, [x29, #272] --copy BD's [16 ~ 31]B
1844 * the instr05~08 depend on x0's value, x0 depent on w26's
1845 * value, the w26 is the bd_base_info, this form the data
1846 * dependency ordering.
1847 * note: if BD is valid, (bd_base_info & (1u<<HNS3_RXD_VLD_B)) -
1848 * (1u<<HNS3_RXD_VLD_B) will always zero, so the
1849 * assignment is correct.
1851 * So we use the data dependency ordering instead of memory
1852 * barrier to improve receive performance.
1854 rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
1855 (1u << HNS3_RXD_VLD_B)];
1857 nmb = hns3_rx_alloc_buffer(rxq);
1858 if (unlikely(nmb == NULL)) {
1859 dev = &rte_eth_devices[rxq->port_id];
1860 dev->data->rx_mbuf_alloc_failed++;
1865 rxe = &sw_ring[rx_id];
1867 if (unlikely(rx_id == rxq->nb_rx_desc))
1870 rte_prefetch0(sw_ring[rx_id].mbuf);
1871 if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
1872 rte_prefetch0(&rx_ring[rx_id]);
1873 rte_prefetch0(&sw_ring[rx_id]);
1879 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
1880 rxdp->rx.bd_base_info = 0;
1881 rxdp->addr = dma_addr;
1883 if (first_seg == NULL) {
1885 first_seg->nb_segs = 1;
1887 first_seg->nb_segs++;
1888 last_seg->next = rxm;
1891 rxm->data_off = RTE_PKTMBUF_HEADROOM;
1892 rxm->data_len = rte_le_to_cpu_16(rxd.rx.size);
1894 if (!(bd_base_info & BIT(HNS3_RXD_FE_B))) {
1901 * The last buffer of the received packet. packet len from
1902 * buffer description may contains CRC len, packet len should
1903 * subtract it, same as data len.
1905 first_seg->pkt_len = rte_le_to_cpu_16(rxd.rx.pkt_len);
1908 * This is the last buffer of the received packet. If the CRC
1909 * is not stripped by the hardware:
1910 * - Subtract the CRC length from the total packet length.
1911 * - If the last buffer only contains the whole CRC or a part
1912 * of it, free the mbuf associated to the last buffer. If part
1913 * of the CRC is also contained in the previous mbuf, subtract
1914 * the length of that CRC part from the data length of the
1918 if (unlikely(rxq->crc_len > 0)) {
1919 first_seg->pkt_len -= rxq->crc_len;
1920 recalculate_data_len(first_seg, last_seg, rxm, rxq,
1924 first_seg->port = rxq->port_id;
1925 first_seg->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
1926 first_seg->ol_flags = PKT_RX_RSS_HASH;
1927 if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
1928 first_seg->hash.fdir.hi =
1929 rte_le_to_cpu_16(rxd.rx.fd_id);
1930 first_seg->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
1933 gro_size = hns3_get_field(bd_base_info, HNS3_RXD_GRO_SIZE_M,
1934 HNS3_RXD_GRO_SIZE_S);
1935 if (gro_size != 0) {
1936 first_seg->ol_flags |= PKT_RX_LRO;
1937 first_seg->tso_segsz = gro_size;
1940 l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
1941 ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
1942 ret = hns3_handle_bdinfo(rxq, first_seg, bd_base_info,
1943 l234_info, &cksum_err);
1947 first_seg->packet_type = hns3_rx_calc_ptype(rxq,
1948 l234_info, ol_info);
1950 if (bd_base_info & BIT(HNS3_RXD_L3L4P_B))
1951 hns3_rx_set_cksum_flag(first_seg,
1952 first_seg->packet_type,
1954 hns3_rxd_to_vlan_tci(rxq, first_seg, l234_info, &rxd);
1956 rx_pkts[nb_rx++] = first_seg;
1960 rte_pktmbuf_free(first_seg);
1964 rxq->next_to_use = rx_id;
1965 rxq->pkt_first_seg = first_seg;
1966 rxq->pkt_last_seg = last_seg;
1968 rxq->rx_free_hold += nb_rx_bd;
1969 if (rxq->rx_free_hold > rxq->rx_free_thresh) {
1970 hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
1971 rxq->rx_free_hold = 0;
1978 hns3_rxq_vec_setup(__rte_unused struct hns3_rx_queue *rxq)
1983 hns3_rx_check_vec_support(__rte_unused struct rte_eth_dev *dev)
1989 hns3_recv_pkts_vec(__rte_unused void *tx_queue,
1990 __rte_unused struct rte_mbuf **tx_pkts,
1991 __rte_unused uint16_t nb_pkts)
1997 hns3_rx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
1998 struct rte_eth_burst_mode *mode)
2000 static const struct {
2001 eth_rx_burst_t pkt_burst;
2004 { hns3_recv_pkts, "Scalar" },
2005 { hns3_recv_scattered_pkts, "Scalar Scattered" },
2006 { hns3_recv_pkts_vec, "Vector Neon" },
2009 eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
2013 for (i = 0; i < RTE_DIM(burst_infos); i++) {
2014 if (pkt_burst == burst_infos[i].pkt_burst) {
2015 snprintf(mode->info, sizeof(mode->info), "%s",
2016 burst_infos[i].info);
2025 static eth_rx_burst_t
2026 hns3_get_rx_function(struct rte_eth_dev *dev)
2028 struct hns3_adapter *hns = dev->data->dev_private;
2029 uint64_t offloads = dev->data->dev_conf.rxmode.offloads;
2031 if (hns->rx_vec_allowed && hns3_rx_check_vec_support(dev) == 0)
2032 return hns3_recv_pkts_vec;
2034 if (hns->rx_simple_allowed && !dev->data->scattered_rx &&
2035 (offloads & DEV_RX_OFFLOAD_TCP_LRO) == 0)
2036 return hns3_recv_pkts;
2038 return hns3_recv_scattered_pkts;
2042 hns3_tx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_txconf *conf,
2043 uint16_t nb_desc, uint16_t *tx_rs_thresh,
2044 uint16_t *tx_free_thresh, uint16_t idx)
2046 #define HNS3_TX_RS_FREE_THRESH_GAP 8
2047 uint16_t rs_thresh, free_thresh, fast_free_thresh;
2049 if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
2050 nb_desc % HNS3_ALIGN_RING_DESC) {
2051 hns3_err(hw, "number (%u) of tx descriptors is invalid",
2056 rs_thresh = (conf->tx_rs_thresh > 0) ?
2057 conf->tx_rs_thresh : HNS3_DEFAULT_TX_RS_THRESH;
2058 free_thresh = (conf->tx_free_thresh > 0) ?
2059 conf->tx_free_thresh : HNS3_DEFAULT_TX_FREE_THRESH;
2060 if (rs_thresh + free_thresh > nb_desc || nb_desc % rs_thresh ||
2061 rs_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP ||
2062 free_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP) {
2063 hns3_err(hw, "tx_rs_thresh (%d) tx_free_thresh (%d) nb_desc "
2064 "(%d) of tx descriptors for port=%d queue=%d check "
2066 rs_thresh, free_thresh, nb_desc, hw->data->port_id,
2071 if (conf->tx_free_thresh == 0) {
2072 /* Fast free Tx memory buffer to improve cache hit rate */
2073 fast_free_thresh = nb_desc - rs_thresh;
2074 if (fast_free_thresh >=
2075 HNS3_TX_FAST_FREE_AHEAD + HNS3_DEFAULT_TX_FREE_THRESH)
2076 free_thresh = fast_free_thresh -
2077 HNS3_TX_FAST_FREE_AHEAD;
2080 *tx_rs_thresh = rs_thresh;
2081 *tx_free_thresh = free_thresh;
2086 hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
2087 unsigned int socket_id, const struct rte_eth_txconf *conf)
2089 struct hns3_adapter *hns = dev->data->dev_private;
2090 uint16_t tx_rs_thresh, tx_free_thresh;
2091 struct hns3_hw *hw = &hns->hw;
2092 struct hns3_queue_info q_info;
2093 struct hns3_tx_queue *txq;
2097 if (dev->data->dev_started) {
2098 hns3_err(hw, "tx_queue_setup after dev_start no supported");
2102 ret = hns3_tx_queue_conf_check(hw, conf, nb_desc,
2103 &tx_rs_thresh, &tx_free_thresh, idx);
2107 if (dev->data->tx_queues[idx] != NULL) {
2108 hns3_tx_queue_release(dev->data->tx_queues[idx]);
2109 dev->data->tx_queues[idx] = NULL;
2113 q_info.socket_id = socket_id;
2114 q_info.nb_desc = nb_desc;
2115 q_info.type = "hns3 TX queue";
2116 q_info.ring_name = "tx_ring";
2117 txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
2120 "Failed to alloc mem and reserve DMA mem for tx ring!");
2124 txq->tx_deferred_start = conf->tx_deferred_start;
2125 tx_entry_len = sizeof(struct hns3_entry) * txq->nb_tx_desc;
2126 txq->sw_ring = rte_zmalloc_socket("hns3 TX sw ring", tx_entry_len,
2127 RTE_CACHE_LINE_SIZE, socket_id);
2128 if (txq->sw_ring == NULL) {
2129 hns3_err(hw, "Failed to allocate memory for tx sw ring!");
2130 hns3_tx_queue_release(txq);
2135 txq->next_to_use = 0;
2136 txq->next_to_clean = 0;
2137 txq->tx_bd_ready = txq->nb_tx_desc - 1;
2138 txq->tx_free_thresh = tx_free_thresh;
2139 txq->tx_rs_thresh = tx_rs_thresh;
2140 txq->free = rte_zmalloc_socket("hns3 TX mbuf free array",
2141 sizeof(struct rte_mbuf *) * txq->tx_rs_thresh,
2142 RTE_CACHE_LINE_SIZE, socket_id);
2144 hns3_err(hw, "failed to allocate tx mbuf free array!");
2145 hns3_tx_queue_release(txq);
2149 txq->port_id = dev->data->port_id;
2150 txq->pvid_state = hw->port_base_vlan_cfg.state;
2151 txq->configured = true;
2152 txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
2153 idx * HNS3_TQP_REG_SIZE);
2154 txq->io_tail_reg = (volatile void *)((char *)txq->io_base +
2155 HNS3_RING_TX_TAIL_REG);
2156 txq->min_tx_pkt_len = hw->min_tx_pkt_len;
2157 txq->over_length_pkt_cnt = 0;
2158 txq->exceed_limit_bd_pkt_cnt = 0;
2159 txq->exceed_limit_bd_reassem_fail = 0;
2160 txq->unsupported_tunnel_pkt_cnt = 0;
2161 txq->queue_full_cnt = 0;
2162 txq->pkt_padding_fail_cnt = 0;
2163 rte_spinlock_lock(&hw->lock);
2164 dev->data->tx_queues[idx] = txq;
2165 rte_spinlock_unlock(&hw->lock);
2171 hns3_tx_free_useless_buffer(struct hns3_tx_queue *txq)
2173 uint16_t tx_next_clean = txq->next_to_clean;
2174 uint16_t tx_next_use = txq->next_to_use;
2175 uint16_t tx_bd_ready = txq->tx_bd_ready;
2176 uint16_t tx_bd_max = txq->nb_tx_desc;
2177 struct hns3_entry *tx_bak_pkt = &txq->sw_ring[tx_next_clean];
2178 struct hns3_desc *desc = &txq->tx_ring[tx_next_clean];
2179 struct rte_mbuf *mbuf;
2181 while ((!(desc->tx.tp_fe_sc_vld_ra_ri &
2182 rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B)))) &&
2183 tx_next_use != tx_next_clean) {
2184 mbuf = tx_bak_pkt->mbuf;
2186 rte_pktmbuf_free_seg(mbuf);
2187 tx_bak_pkt->mbuf = NULL;
2195 if (tx_next_clean >= tx_bd_max) {
2197 desc = txq->tx_ring;
2198 tx_bak_pkt = txq->sw_ring;
2202 txq->next_to_clean = tx_next_clean;
2203 txq->tx_bd_ready = tx_bd_ready;
2207 hns3_tso_proc_tunnel(struct hns3_desc *desc, uint64_t ol_flags,
2208 struct rte_mbuf *rxm, uint8_t *l2_len)
2214 tun_flags = ol_flags & PKT_TX_TUNNEL_MASK;
2218 otmp = rte_le_to_cpu_32(desc->tx.ol_type_vlan_len_msec);
2219 switch (tun_flags) {
2220 case PKT_TX_TUNNEL_GENEVE:
2221 case PKT_TX_TUNNEL_VXLAN:
2222 *l2_len = rxm->l2_len - RTE_ETHER_VXLAN_HLEN;
2224 case PKT_TX_TUNNEL_GRE:
2226 * OL4 header size, defined in 4 Bytes, it contains outer
2227 * L4(GRE) length and tunneling length.
2229 ol4_len = hns3_get_field(otmp, HNS3_TXD_L4LEN_M,
2231 *l2_len = rxm->l2_len - (ol4_len << HNS3_L4_LEN_UNIT);
2234 /* For non UDP / GRE tunneling, drop the tunnel packet */
2237 hns3_set_field(otmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2238 rxm->outer_l2_len >> HNS3_L2_LEN_UNIT);
2239 desc->tx.ol_type_vlan_len_msec = rte_cpu_to_le_32(otmp);
2245 hns3_config_gro(struct hns3_hw *hw, bool en)
2247 struct hns3_cfg_gro_status_cmd *req;
2248 struct hns3_cmd_desc desc;
2251 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GRO_GENERIC_CONFIG, false);
2252 req = (struct hns3_cfg_gro_status_cmd *)desc.data;
2254 req->gro_en = rte_cpu_to_le_16(en ? 1 : 0);
2256 ret = hns3_cmd_send(hw, &desc, 1);
2258 hns3_err(hw, "%s hardware GRO failed, ret = %d",
2259 en ? "enable" : "disable", ret);
2265 hns3_restore_gro_conf(struct hns3_hw *hw)
2271 offloads = hw->data->dev_conf.rxmode.offloads;
2272 gro_en = offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
2273 ret = hns3_config_gro(hw, gro_en);
2275 hns3_err(hw, "restore hardware GRO to %s failed, ret = %d",
2276 gro_en ? "enabled" : "disabled", ret);
2282 hns3_pkt_is_tso(struct rte_mbuf *m)
2284 return (m->tso_segsz != 0 && m->ol_flags & PKT_TX_TCP_SEG);
2288 hns3_set_tso(struct hns3_desc *desc, uint64_t ol_flags,
2289 uint32_t paylen, struct rte_mbuf *rxm)
2291 uint8_t l2_len = rxm->l2_len;
2294 if (!hns3_pkt_is_tso(rxm))
2297 if (hns3_tso_proc_tunnel(desc, ol_flags, rxm, &l2_len))
2300 if (paylen <= rxm->tso_segsz)
2303 tmp = rte_le_to_cpu_32(desc->tx.type_cs_vlan_tso_len);
2304 hns3_set_bit(tmp, HNS3_TXD_TSO_B, 1);
2305 hns3_set_bit(tmp, HNS3_TXD_L3CS_B, 1);
2306 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S, HNS3_L4T_TCP);
2307 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2308 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2309 sizeof(struct rte_tcp_hdr) >> HNS3_L4_LEN_UNIT);
2310 hns3_set_field(tmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2311 l2_len >> HNS3_L2_LEN_UNIT);
2312 desc->tx.type_cs_vlan_tso_len = rte_cpu_to_le_32(tmp);
2313 desc->tx.mss = rte_cpu_to_le_16(rxm->tso_segsz);
2317 hns3_fill_per_desc(struct hns3_desc *desc, struct rte_mbuf *rxm)
2319 desc->addr = rte_mbuf_data_iova(rxm);
2320 desc->tx.send_size = rte_cpu_to_le_16(rte_pktmbuf_data_len(rxm));
2321 desc->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B));
2325 hns3_fill_first_desc(struct hns3_tx_queue *txq, struct hns3_desc *desc,
2326 struct rte_mbuf *rxm)
2328 uint64_t ol_flags = rxm->ol_flags;
2332 hdr_len = rxm->l2_len + rxm->l3_len + rxm->l4_len;
2333 hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
2334 rxm->outer_l2_len + rxm->outer_l3_len : 0;
2335 paylen = rxm->pkt_len - hdr_len;
2336 desc->tx.paylen = rte_cpu_to_le_32(paylen);
2337 hns3_set_tso(desc, ol_flags, paylen, rxm);
2340 * Currently, hardware doesn't support more than two layers VLAN offload
2341 * in Tx direction based on hns3 network engine. So when the number of
2342 * VLANs in the packets represented by rxm plus the number of VLAN
2343 * offload by hardware such as PVID etc, exceeds two, the packets will
2344 * be discarded or the original VLAN of the packets will be overwitted
2345 * by hardware. When the PF PVID is enabled by calling the API function
2346 * named rte_eth_dev_set_vlan_pvid or the VF PVID is enabled by the hns3
2347 * PF kernel ether driver, the outer VLAN tag will always be the PVID.
2348 * To avoid the VLAN of Tx descriptor is overwritten by PVID, it should
2349 * be added to the position close to the IP header when PVID is enabled.
2351 if (!txq->pvid_state && ol_flags & (PKT_TX_VLAN_PKT |
2353 desc->tx.ol_type_vlan_len_msec |=
2354 rte_cpu_to_le_32(BIT(HNS3_TXD_OVLAN_B));
2355 if (ol_flags & PKT_TX_QINQ_PKT)
2356 desc->tx.outer_vlan_tag =
2357 rte_cpu_to_le_16(rxm->vlan_tci_outer);
2359 desc->tx.outer_vlan_tag =
2360 rte_cpu_to_le_16(rxm->vlan_tci);
2363 if (ol_flags & PKT_TX_QINQ_PKT ||
2364 ((ol_flags & PKT_TX_VLAN_PKT) && txq->pvid_state)) {
2365 desc->tx.type_cs_vlan_tso_len |=
2366 rte_cpu_to_le_32(BIT(HNS3_TXD_VLAN_B));
2367 desc->tx.vlan_tag = rte_cpu_to_le_16(rxm->vlan_tci);
2372 hns3_tx_alloc_mbufs(struct hns3_tx_queue *txq, struct rte_mempool *mb_pool,
2373 uint16_t nb_new_buf, struct rte_mbuf **alloc_mbuf)
2375 struct rte_mbuf *new_mbuf = NULL;
2376 struct rte_eth_dev *dev;
2377 struct rte_mbuf *temp;
2381 /* Allocate enough mbufs */
2382 for (i = 0; i < nb_new_buf; i++) {
2383 temp = rte_pktmbuf_alloc(mb_pool);
2384 if (unlikely(temp == NULL)) {
2385 dev = &rte_eth_devices[txq->port_id];
2386 hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2387 hns3_err(hw, "Failed to alloc TX mbuf port_id=%d,"
2388 "queue_id=%d in reassemble tx pkts.",
2389 txq->port_id, txq->queue_id);
2390 rte_pktmbuf_free(new_mbuf);
2393 temp->next = new_mbuf;
2397 if (new_mbuf == NULL)
2400 new_mbuf->nb_segs = nb_new_buf;
2401 *alloc_mbuf = new_mbuf;
2407 hns3_pktmbuf_copy_hdr(struct rte_mbuf *new_pkt, struct rte_mbuf *old_pkt)
2409 new_pkt->ol_flags = old_pkt->ol_flags;
2410 new_pkt->pkt_len = rte_pktmbuf_pkt_len(old_pkt);
2411 new_pkt->outer_l2_len = old_pkt->outer_l2_len;
2412 new_pkt->outer_l3_len = old_pkt->outer_l3_len;
2413 new_pkt->l2_len = old_pkt->l2_len;
2414 new_pkt->l3_len = old_pkt->l3_len;
2415 new_pkt->l4_len = old_pkt->l4_len;
2416 new_pkt->vlan_tci_outer = old_pkt->vlan_tci_outer;
2417 new_pkt->vlan_tci = old_pkt->vlan_tci;
2421 hns3_reassemble_tx_pkts(void *tx_queue, struct rte_mbuf *tx_pkt,
2422 struct rte_mbuf **new_pkt)
2424 struct hns3_tx_queue *txq = tx_queue;
2425 struct rte_mempool *mb_pool;
2426 struct rte_mbuf *new_mbuf;
2427 struct rte_mbuf *temp_new;
2428 struct rte_mbuf *temp;
2429 uint16_t last_buf_len;
2430 uint16_t nb_new_buf;
2441 mb_pool = tx_pkt->pool;
2442 buf_size = tx_pkt->buf_len - RTE_PKTMBUF_HEADROOM;
2443 nb_new_buf = (rte_pktmbuf_pkt_len(tx_pkt) - 1) / buf_size + 1;
2444 if (nb_new_buf > HNS3_MAX_NON_TSO_BD_PER_PKT)
2447 last_buf_len = rte_pktmbuf_pkt_len(tx_pkt) % buf_size;
2448 if (last_buf_len == 0)
2449 last_buf_len = buf_size;
2451 /* Allocate enough mbufs */
2452 ret = hns3_tx_alloc_mbufs(txq, mb_pool, nb_new_buf, &new_mbuf);
2456 /* Copy the original packet content to the new mbufs */
2458 s = rte_pktmbuf_mtod(temp, char *);
2459 len_s = rte_pktmbuf_data_len(temp);
2460 temp_new = new_mbuf;
2461 for (i = 0; i < nb_new_buf; i++) {
2462 d = rte_pktmbuf_mtod(temp_new, char *);
2463 if (i < nb_new_buf - 1)
2466 buf_len = last_buf_len;
2470 len = RTE_MIN(len_s, len_d);
2474 len_d = len_d - len;
2475 len_s = len_s - len;
2481 s = rte_pktmbuf_mtod(temp, char *);
2482 len_s = rte_pktmbuf_data_len(temp);
2486 temp_new->data_len = buf_len;
2487 temp_new = temp_new->next;
2489 hns3_pktmbuf_copy_hdr(new_mbuf, tx_pkt);
2491 /* free original mbufs */
2492 rte_pktmbuf_free(tx_pkt);
2494 *new_pkt = new_mbuf;
2500 hns3_parse_outer_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec)
2502 uint32_t tmp = *ol_type_vlan_len_msec;
2504 /* (outer) IP header type */
2505 if (ol_flags & PKT_TX_OUTER_IPV4) {
2506 /* OL3 header size, defined in 4 bytes */
2507 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2508 sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
2509 if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
2510 hns3_set_field(tmp, HNS3_TXD_OL3T_M,
2511 HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_CSUM);
2513 hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
2514 HNS3_OL3T_IPV4_NO_CSUM);
2515 } else if (ol_flags & PKT_TX_OUTER_IPV6) {
2516 hns3_set_field(tmp, HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
2518 /* OL3 header size, defined in 4 bytes */
2519 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2520 sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
2523 *ol_type_vlan_len_msec = tmp;
2527 hns3_parse_inner_params(uint64_t ol_flags, uint32_t *ol_type_vlan_len_msec,
2528 struct rte_net_hdr_lens *hdr_lens)
2530 uint32_t tmp = *ol_type_vlan_len_msec;
2533 /* OL2 header size, defined in 2 bytes */
2534 hns3_set_field(tmp, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2535 sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
2537 /* L4TUNT: L4 Tunneling Type */
2538 switch (ol_flags & PKT_TX_TUNNEL_MASK) {
2539 case PKT_TX_TUNNEL_GENEVE:
2540 case PKT_TX_TUNNEL_VXLAN:
2541 /* MAC in UDP tunnelling packet, include VxLAN */
2542 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
2543 HNS3_TUN_MAC_IN_UDP);
2545 * OL4 header size, defined in 4 Bytes, it contains outer
2546 * L4(UDP) length and tunneling length.
2548 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2549 (uint8_t)RTE_ETHER_VXLAN_HLEN >>
2552 case PKT_TX_TUNNEL_GRE:
2553 hns3_set_field(tmp, HNS3_TXD_TUNTYPE_M, HNS3_TXD_TUNTYPE_S,
2556 * OL4 header size, defined in 4 Bytes, it contains outer
2557 * L4(GRE) length and tunneling length.
2559 l4_len = hdr_lens->l4_len + hdr_lens->tunnel_len;
2560 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2561 l4_len >> HNS3_L4_LEN_UNIT);
2564 /* For non UDP / GRE tunneling, drop the tunnel packet */
2568 *ol_type_vlan_len_msec = tmp;
2574 hns3_parse_tunneling_params(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
2576 struct rte_net_hdr_lens *hdr_lens)
2578 struct hns3_desc *tx_ring = txq->tx_ring;
2579 struct hns3_desc *desc = &tx_ring[tx_desc_id];
2583 hns3_parse_outer_params(ol_flags, &value);
2584 ret = hns3_parse_inner_params(ol_flags, &value, hdr_lens);
2588 desc->tx.ol_type_vlan_len_msec |= rte_cpu_to_le_32(value);
2594 hns3_parse_l3_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
2598 /* Enable L3 checksum offloads */
2599 if (ol_flags & PKT_TX_IPV4) {
2600 tmp = *type_cs_vlan_tso_len;
2601 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
2603 /* inner(/normal) L3 header size, defined in 4 bytes */
2604 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2605 sizeof(struct rte_ipv4_hdr) >> HNS3_L3_LEN_UNIT);
2606 if (ol_flags & PKT_TX_IP_CKSUM)
2607 hns3_set_bit(tmp, HNS3_TXD_L3CS_B, 1);
2608 *type_cs_vlan_tso_len = tmp;
2609 } else if (ol_flags & PKT_TX_IPV6) {
2610 tmp = *type_cs_vlan_tso_len;
2611 /* L3T, IPv6 don't do checksum */
2612 hns3_set_field(tmp, HNS3_TXD_L3T_M, HNS3_TXD_L3T_S,
2614 /* inner(/normal) L3 header size, defined in 4 bytes */
2615 hns3_set_field(tmp, HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
2616 sizeof(struct rte_ipv6_hdr) >> HNS3_L3_LEN_UNIT);
2617 *type_cs_vlan_tso_len = tmp;
2622 hns3_parse_l4_cksum_params(uint64_t ol_flags, uint32_t *type_cs_vlan_tso_len)
2626 /* Enable L4 checksum offloads */
2627 switch (ol_flags & PKT_TX_L4_MASK) {
2628 case PKT_TX_TCP_CKSUM:
2629 tmp = *type_cs_vlan_tso_len;
2630 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
2632 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2633 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2634 sizeof(struct rte_tcp_hdr) >> HNS3_L4_LEN_UNIT);
2635 *type_cs_vlan_tso_len = tmp;
2637 case PKT_TX_UDP_CKSUM:
2638 tmp = *type_cs_vlan_tso_len;
2639 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
2641 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2642 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2643 sizeof(struct rte_udp_hdr) >> HNS3_L4_LEN_UNIT);
2644 *type_cs_vlan_tso_len = tmp;
2646 case PKT_TX_SCTP_CKSUM:
2647 tmp = *type_cs_vlan_tso_len;
2648 hns3_set_field(tmp, HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
2650 hns3_set_bit(tmp, HNS3_TXD_L4CS_B, 1);
2651 hns3_set_field(tmp, HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
2652 sizeof(struct rte_sctp_hdr) >> HNS3_L4_LEN_UNIT);
2653 *type_cs_vlan_tso_len = tmp;
2661 hns3_txd_enable_checksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
2664 struct hns3_desc *tx_ring = txq->tx_ring;
2665 struct hns3_desc *desc = &tx_ring[tx_desc_id];
2668 /* inner(/normal) L2 header size, defined in 2 bytes */
2669 hns3_set_field(value, HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
2670 sizeof(struct rte_ether_hdr) >> HNS3_L2_LEN_UNIT);
2672 hns3_parse_l3_cksum_params(ol_flags, &value);
2673 hns3_parse_l4_cksum_params(ol_flags, &value);
2675 desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(value);
2679 hns3_pkt_need_linearized(struct rte_mbuf *tx_pkts, uint32_t bd_num)
2681 struct rte_mbuf *m_first = tx_pkts;
2682 struct rte_mbuf *m_last = tx_pkts;
2683 uint32_t tot_len = 0;
2688 * Hardware requires that the sum of the data length of every 8
2689 * consecutive buffers is greater than MSS in hns3 network engine.
2690 * We simplify it by ensuring pkt_headlen + the first 8 consecutive
2691 * frags greater than gso header len + mss, and the remaining 7
2692 * consecutive frags greater than MSS except the last 7 frags.
2694 if (bd_num <= HNS3_MAX_NON_TSO_BD_PER_PKT)
2697 for (i = 0; m_last && i < HNS3_MAX_NON_TSO_BD_PER_PKT - 1;
2698 i++, m_last = m_last->next)
2699 tot_len += m_last->data_len;
2704 /* ensure the first 8 frags is greater than mss + header */
2705 hdr_len = tx_pkts->l2_len + tx_pkts->l3_len + tx_pkts->l4_len;
2706 hdr_len += (tx_pkts->ol_flags & PKT_TX_TUNNEL_MASK) ?
2707 tx_pkts->outer_l2_len + tx_pkts->outer_l3_len : 0;
2708 if (tot_len + m_last->data_len < tx_pkts->tso_segsz + hdr_len)
2712 * ensure the sum of the data length of every 7 consecutive buffer
2713 * is greater than mss except the last one.
2715 for (i = 0; m_last && i < bd_num - HNS3_MAX_NON_TSO_BD_PER_PKT; i++) {
2716 tot_len -= m_first->data_len;
2717 tot_len += m_last->data_len;
2719 if (tot_len < tx_pkts->tso_segsz)
2722 m_first = m_first->next;
2723 m_last = m_last->next;
2730 hns3_outer_header_cksum_prepare(struct rte_mbuf *m)
2732 uint64_t ol_flags = m->ol_flags;
2733 struct rte_ipv4_hdr *ipv4_hdr;
2734 struct rte_udp_hdr *udp_hdr;
2735 uint32_t paylen, hdr_len;
2737 if (!(ol_flags & (PKT_TX_OUTER_IPV4 | PKT_TX_OUTER_IPV6)))
2740 if (ol_flags & PKT_TX_IPV4) {
2741 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
2744 if (ol_flags & PKT_TX_IP_CKSUM)
2745 ipv4_hdr->hdr_checksum = 0;
2748 if ((ol_flags & PKT_TX_L4_MASK) == PKT_TX_UDP_CKSUM &&
2749 ol_flags & PKT_TX_TCP_SEG) {
2750 hdr_len = m->l2_len + m->l3_len + m->l4_len;
2751 hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
2752 m->outer_l2_len + m->outer_l3_len : 0;
2753 paylen = m->pkt_len - hdr_len;
2754 if (paylen <= m->tso_segsz)
2756 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
2759 udp_hdr->dgram_cksum = 0;
2764 hns3_check_tso_pkt_valid(struct rte_mbuf *m)
2766 uint32_t tmp_data_len_sum = 0;
2767 uint16_t nb_buf = m->nb_segs;
2768 uint32_t paylen, hdr_len;
2769 struct rte_mbuf *m_seg;
2772 if (nb_buf > HNS3_MAX_TSO_BD_PER_PKT)
2775 hdr_len = m->l2_len + m->l3_len + m->l4_len;
2776 hdr_len += (m->ol_flags & PKT_TX_TUNNEL_MASK) ?
2777 m->outer_l2_len + m->outer_l3_len : 0;
2778 if (hdr_len > HNS3_MAX_TSO_HDR_SIZE)
2781 paylen = m->pkt_len - hdr_len;
2782 if (paylen > HNS3_MAX_BD_PAYLEN)
2786 * The TSO header (include outer and inner L2, L3 and L4 header)
2787 * should be provided by three descriptors in maximum in hns3 network
2791 for (i = 0; m_seg != NULL && i < HNS3_MAX_TSO_HDR_BD_NUM && i < nb_buf;
2792 i++, m_seg = m_seg->next) {
2793 tmp_data_len_sum += m_seg->data_len;
2796 if (hdr_len > tmp_data_len_sum)
2802 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2804 hns3_vld_vlan_chk(struct hns3_tx_queue *txq, struct rte_mbuf *m)
2806 struct rte_ether_hdr *eh;
2807 struct rte_vlan_hdr *vh;
2809 if (!txq->pvid_state)
2813 * Due to hardware limitations, we only support two-layer VLAN hardware
2814 * offload in Tx direction based on hns3 network engine, so when PVID is
2815 * enabled, QinQ insert is no longer supported.
2816 * And when PVID is enabled, in the following two cases:
2817 * i) packets with more than two VLAN tags.
2818 * ii) packets with one VLAN tag while the hardware VLAN insert is
2820 * The packets will be regarded as abnormal packets and discarded by
2821 * hardware in Tx direction. For debugging purposes, a validation check
2822 * for these types of packets is added to the '.tx_pkt_prepare' ops
2823 * implementation function named hns3_prep_pkts to inform users that
2824 * these packets will be discarded.
2826 if (m->ol_flags & PKT_TX_QINQ_PKT)
2829 eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
2830 if (eh->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
2831 if (m->ol_flags & PKT_TX_VLAN_PKT)
2834 /* Ensure the incoming packet is not a QinQ packet */
2835 vh = (struct rte_vlan_hdr *)(eh + 1);
2836 if (vh->eth_proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
2845 hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
2852 for (i = 0; i < nb_pkts; i++) {
2855 if (hns3_pkt_is_tso(m) &&
2856 (hns3_pkt_need_linearized(m, m->nb_segs) ||
2857 hns3_check_tso_pkt_valid(m))) {
2862 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
2863 ret = rte_validate_tx_offload(m);
2869 if (hns3_vld_vlan_chk(tx_queue, m)) {
2874 ret = rte_net_intel_cksum_prepare(m);
2880 hns3_outer_header_cksum_prepare(m);
2887 hns3_parse_cksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
2888 const struct rte_mbuf *m, struct rte_net_hdr_lens *hdr_lens)
2890 /* Fill in tunneling parameters if necessary */
2891 if (m->ol_flags & PKT_TX_TUNNEL_MASK) {
2892 (void)rte_net_get_ptype(m, hdr_lens, RTE_PTYPE_ALL_MASK);
2893 if (hns3_parse_tunneling_params(txq, tx_desc_id, m->ol_flags,
2895 txq->unsupported_tunnel_pkt_cnt++;
2899 /* Enable checksum offloading */
2900 if (m->ol_flags & HNS3_TX_CKSUM_OFFLOAD_MASK)
2901 hns3_txd_enable_checksum(txq, tx_desc_id, m->ol_flags);
2907 hns3_check_non_tso_pkt(uint16_t nb_buf, struct rte_mbuf **m_seg,
2908 struct rte_mbuf *tx_pkt, struct hns3_tx_queue *txq)
2910 struct rte_mbuf *new_pkt;
2913 if (hns3_pkt_is_tso(*m_seg))
2917 * If packet length is greater than HNS3_MAX_FRAME_LEN
2918 * driver support, the packet will be ignored.
2920 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) > HNS3_MAX_FRAME_LEN)) {
2921 txq->over_length_pkt_cnt++;
2925 if (unlikely(nb_buf > HNS3_MAX_NON_TSO_BD_PER_PKT)) {
2926 txq->exceed_limit_bd_pkt_cnt++;
2927 ret = hns3_reassemble_tx_pkts(txq, tx_pkt, &new_pkt);
2929 txq->exceed_limit_bd_reassem_fail++;
2939 hns3_tx_free_buffer_simple(struct hns3_tx_queue *txq)
2941 struct hns3_entry *tx_entry;
2942 struct hns3_desc *desc;
2943 uint16_t tx_next_clean;
2947 if (HNS3_GET_TX_QUEUE_PEND_BD_NUM(txq) < txq->tx_rs_thresh)
2951 * All mbufs can be released only when the VLD bits of all
2952 * descriptors in a batch are cleared.
2954 tx_next_clean = (txq->next_to_clean + txq->tx_rs_thresh - 1) %
2956 desc = &txq->tx_ring[tx_next_clean];
2957 for (i = 0; i < txq->tx_rs_thresh; i++) {
2958 if (rte_le_to_cpu_16(desc->tx.tp_fe_sc_vld_ra_ri) &
2959 BIT(HNS3_TXD_VLD_B))
2964 tx_entry = &txq->sw_ring[txq->next_to_clean];
2966 for (i = 0; i < txq->tx_rs_thresh; i++)
2967 rte_prefetch0((tx_entry + i)->mbuf);
2968 for (i = 0; i < txq->tx_rs_thresh; i++, tx_entry++) {
2969 rte_mempool_put(tx_entry->mbuf->pool, tx_entry->mbuf);
2970 tx_entry->mbuf = NULL;
2973 txq->next_to_clean = (tx_next_clean + 1) % txq->nb_tx_desc;
2974 txq->tx_bd_ready += txq->tx_rs_thresh;
2979 hns3_tx_backup_1mbuf(struct hns3_entry *tx_entry, struct rte_mbuf **pkts)
2981 tx_entry->mbuf = pkts[0];
2985 hns3_tx_backup_4mbuf(struct hns3_entry *tx_entry, struct rte_mbuf **pkts)
2987 hns3_tx_backup_1mbuf(&tx_entry[0], &pkts[0]);
2988 hns3_tx_backup_1mbuf(&tx_entry[1], &pkts[1]);
2989 hns3_tx_backup_1mbuf(&tx_entry[2], &pkts[2]);
2990 hns3_tx_backup_1mbuf(&tx_entry[3], &pkts[3]);
2994 hns3_tx_setup_4bd(struct hns3_desc *txdp, struct rte_mbuf **pkts)
2996 #define PER_LOOP_NUM 4
2997 const uint16_t bd_flag = BIT(HNS3_TXD_VLD_B) | BIT(HNS3_TXD_FE_B);
3001 for (i = 0; i < PER_LOOP_NUM; i++, txdp++, pkts++) {
3002 dma_addr = rte_mbuf_data_iova(*pkts);
3003 txdp->addr = rte_cpu_to_le_64(dma_addr);
3004 txdp->tx.send_size = rte_cpu_to_le_16((*pkts)->data_len);
3005 txdp->tx.paylen = 0;
3006 txdp->tx.type_cs_vlan_tso_len = 0;
3007 txdp->tx.ol_type_vlan_len_msec = 0;
3008 txdp->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(bd_flag);
3013 hns3_tx_setup_1bd(struct hns3_desc *txdp, struct rte_mbuf **pkts)
3015 const uint16_t bd_flag = BIT(HNS3_TXD_VLD_B) | BIT(HNS3_TXD_FE_B);
3018 dma_addr = rte_mbuf_data_iova(*pkts);
3019 txdp->addr = rte_cpu_to_le_64(dma_addr);
3020 txdp->tx.send_size = rte_cpu_to_le_16((*pkts)->data_len);
3021 txdp->tx.paylen = 0;
3022 txdp->tx.type_cs_vlan_tso_len = 0;
3023 txdp->tx.ol_type_vlan_len_msec = 0;
3024 txdp->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(bd_flag);
3028 hns3_tx_fill_hw_ring(struct hns3_tx_queue *txq,
3029 struct rte_mbuf **pkts,
3032 #define PER_LOOP_NUM 4
3033 #define PER_LOOP_MASK (PER_LOOP_NUM - 1)
3034 struct hns3_desc *txdp = &txq->tx_ring[txq->next_to_use];
3035 struct hns3_entry *tx_entry = &txq->sw_ring[txq->next_to_use];
3036 const uint32_t mainpart = (nb_pkts & ((uint32_t)~PER_LOOP_MASK));
3037 const uint32_t leftover = (nb_pkts & ((uint32_t)PER_LOOP_MASK));
3040 for (i = 0; i < mainpart; i += PER_LOOP_NUM) {
3041 hns3_tx_backup_4mbuf(tx_entry + i, pkts + i);
3042 hns3_tx_setup_4bd(txdp + i, pkts + i);
3044 if (unlikely(leftover > 0)) {
3045 for (i = 0; i < leftover; i++) {
3046 hns3_tx_backup_1mbuf(tx_entry + mainpart + i,
3047 pkts + mainpart + i);
3048 hns3_tx_setup_1bd(txdp + mainpart + i,
3049 pkts + mainpart + i);
3055 hns3_xmit_pkts_simple(void *tx_queue,
3056 struct rte_mbuf **tx_pkts,
3059 struct hns3_tx_queue *txq = tx_queue;
3062 hns3_tx_free_buffer_simple(txq);
3064 nb_pkts = RTE_MIN(txq->tx_bd_ready, nb_pkts);
3065 if (unlikely(nb_pkts == 0)) {
3066 if (txq->tx_bd_ready == 0)
3067 txq->queue_full_cnt++;
3071 txq->tx_bd_ready -= nb_pkts;
3072 if (txq->next_to_use + nb_pkts > txq->nb_tx_desc) {
3073 nb_tx = txq->nb_tx_desc - txq->next_to_use;
3074 hns3_tx_fill_hw_ring(txq, tx_pkts, nb_tx);
3075 txq->next_to_use = 0;
3078 hns3_tx_fill_hw_ring(txq, tx_pkts + nb_tx, nb_pkts - nb_tx);
3079 txq->next_to_use += nb_pkts - nb_tx;
3081 hns3_write_reg_opt(txq->io_tail_reg, nb_pkts);
3087 hns3_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
3089 struct rte_net_hdr_lens hdr_lens = {0};
3090 struct hns3_tx_queue *txq = tx_queue;
3091 struct hns3_entry *tx_bak_pkt;
3092 struct hns3_desc *tx_ring;
3093 struct rte_mbuf *tx_pkt;
3094 struct rte_mbuf *m_seg;
3095 struct hns3_desc *desc;
3096 uint32_t nb_hold = 0;
3097 uint16_t tx_next_use;
3098 uint16_t tx_pkt_num;
3104 /* free useless buffer */
3105 hns3_tx_free_useless_buffer(txq);
3107 tx_next_use = txq->next_to_use;
3108 tx_bd_max = txq->nb_tx_desc;
3109 tx_pkt_num = nb_pkts;
3110 tx_ring = txq->tx_ring;
3113 tx_bak_pkt = &txq->sw_ring[tx_next_use];
3114 for (nb_tx = 0; nb_tx < tx_pkt_num; nb_tx++) {
3115 tx_pkt = *tx_pkts++;
3117 nb_buf = tx_pkt->nb_segs;
3119 if (nb_buf > txq->tx_bd_ready) {
3120 txq->queue_full_cnt++;
3128 * If packet length is less than minimum packet length supported
3129 * by hardware in Tx direction, driver need to pad it to avoid
3132 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) <
3133 txq->min_tx_pkt_len)) {
3137 add_len = txq->min_tx_pkt_len -
3138 rte_pktmbuf_pkt_len(tx_pkt);
3139 appended = rte_pktmbuf_append(tx_pkt, add_len);
3140 if (appended == NULL) {
3141 txq->pkt_padding_fail_cnt++;
3145 memset(appended, 0, add_len);
3150 if (hns3_check_non_tso_pkt(nb_buf, &m_seg, tx_pkt, txq))
3153 if (hns3_parse_cksum(txq, tx_next_use, m_seg, &hdr_lens))
3157 desc = &tx_ring[tx_next_use];
3160 * If the packet is divided into multiple Tx Buffer Descriptors,
3161 * only need to fill vlan, paylen and tso into the first Tx
3162 * Buffer Descriptor.
3164 hns3_fill_first_desc(txq, desc, m_seg);
3167 desc = &tx_ring[tx_next_use];
3169 * Fill valid bits, DMA address and data length for each
3170 * Tx Buffer Descriptor.
3172 hns3_fill_per_desc(desc, m_seg);
3173 tx_bak_pkt->mbuf = m_seg;
3174 m_seg = m_seg->next;
3177 if (tx_next_use >= tx_bd_max) {
3179 tx_bak_pkt = txq->sw_ring;
3183 } while (m_seg != NULL);
3185 /* Add end flag for the last Tx Buffer Descriptor */
3186 desc->tx.tp_fe_sc_vld_ra_ri |=
3187 rte_cpu_to_le_16(BIT(HNS3_TXD_FE_B));
3190 txq->next_to_use = tx_next_use;
3191 txq->tx_bd_ready -= i;
3197 hns3_write_reg_opt(txq->io_tail_reg, nb_hold);
3203 hns3_tx_check_vec_support(__rte_unused struct rte_eth_dev *dev)
3209 hns3_xmit_pkts_vec(__rte_unused void *tx_queue,
3210 __rte_unused struct rte_mbuf **tx_pkts,
3211 __rte_unused uint16_t nb_pkts)
3217 hns3_tx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
3218 struct rte_eth_burst_mode *mode)
3220 eth_tx_burst_t pkt_burst = dev->tx_pkt_burst;
3221 const char *info = NULL;
3223 if (pkt_burst == hns3_xmit_pkts_simple)
3224 info = "Scalar Simple";
3225 else if (pkt_burst == hns3_xmit_pkts)
3227 else if (pkt_burst == hns3_xmit_pkts_vec)
3228 info = "Vector Neon";
3233 snprintf(mode->info, sizeof(mode->info), "%s", info);
3238 static eth_tx_burst_t
3239 hns3_get_tx_function(struct rte_eth_dev *dev, eth_tx_prep_t *prep)
3241 uint64_t offloads = dev->data->dev_conf.txmode.offloads;
3242 struct hns3_adapter *hns = dev->data->dev_private;
3244 if (hns->tx_vec_allowed && hns3_tx_check_vec_support(dev) == 0) {
3246 return hns3_xmit_pkts_vec;
3249 if (hns->tx_simple_allowed &&
3250 offloads == (offloads & DEV_TX_OFFLOAD_MBUF_FAST_FREE)) {
3252 return hns3_xmit_pkts_simple;
3255 *prep = hns3_prep_pkts;
3256 return hns3_xmit_pkts;
3260 hns3_dummy_rxtx_burst(void *dpdk_txq __rte_unused,
3261 struct rte_mbuf **pkts __rte_unused,
3262 uint16_t pkts_n __rte_unused)
3267 void hns3_set_rxtx_function(struct rte_eth_dev *eth_dev)
3269 struct hns3_adapter *hns = eth_dev->data->dev_private;
3270 eth_tx_prep_t prep = NULL;
3272 if (hns->hw.adapter_state == HNS3_NIC_STARTED &&
3273 rte_atomic16_read(&hns->hw.reset.resetting) == 0) {
3274 eth_dev->rx_pkt_burst = hns3_get_rx_function(eth_dev);
3275 eth_dev->tx_pkt_burst = hns3_get_tx_function(eth_dev, &prep);
3276 eth_dev->tx_pkt_prepare = prep;
3278 eth_dev->rx_pkt_burst = hns3_dummy_rxtx_burst;
3279 eth_dev->tx_pkt_burst = hns3_dummy_rxtx_burst;
3280 eth_dev->tx_pkt_prepare = hns3_dummy_rxtx_burst;
3285 hns3_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
3286 struct rte_eth_rxq_info *qinfo)
3288 struct hns3_rx_queue *rxq = dev->data->rx_queues[queue_id];
3290 qinfo->mp = rxq->mb_pool;
3291 qinfo->nb_desc = rxq->nb_rx_desc;
3292 qinfo->scattered_rx = dev->data->scattered_rx;
3295 * If there are no available Rx buffer descriptors, incoming packets
3296 * are always dropped by hardware based on hns3 network engine.
3298 qinfo->conf.rx_drop_en = 1;
3299 qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
3300 qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
3301 qinfo->conf.rx_deferred_start = rxq->rx_deferred_start;
3305 hns3_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
3306 struct rte_eth_txq_info *qinfo)
3308 struct hns3_tx_queue *txq = dev->data->tx_queues[queue_id];
3310 qinfo->nb_desc = txq->nb_tx_desc;
3311 qinfo->conf.offloads = dev->data->dev_conf.txmode.offloads;
3312 qinfo->conf.tx_rs_thresh = txq->tx_rs_thresh;
3313 qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
3314 qinfo->conf.tx_deferred_start = txq->tx_deferred_start;