1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2018-2021 HiSilicon Limited.
5 #include <rte_bus_pci.h>
6 #include <rte_common.h>
7 #include <rte_cycles.h>
8 #include <rte_geneve.h>
10 #include <ethdev_driver.h>
13 #include <rte_malloc.h>
14 #if defined(RTE_ARCH_ARM64)
15 #include <rte_cpuflags.h>
19 #include "hns3_ethdev.h"
20 #include "hns3_rxtx.h"
21 #include "hns3_regs.h"
22 #include "hns3_logs.h"
24 #define HNS3_CFG_DESC_NUM(num) ((num) / 8 - 1)
25 #define HNS3_RX_RING_PREFETCTH_MASK 3
28 hns3_rx_queue_release_mbufs(struct hns3_rx_queue *rxq)
32 /* Note: Fake rx queue will not enter here */
33 if (rxq->sw_ring == NULL)
36 if (rxq->rx_rearm_nb == 0) {
37 for (i = 0; i < rxq->nb_rx_desc; i++) {
38 if (rxq->sw_ring[i].mbuf != NULL) {
39 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
40 rxq->sw_ring[i].mbuf = NULL;
44 for (i = rxq->next_to_use;
45 i != rxq->rx_rearm_start;
46 i = (i + 1) % rxq->nb_rx_desc) {
47 if (rxq->sw_ring[i].mbuf != NULL) {
48 rte_pktmbuf_free_seg(rxq->sw_ring[i].mbuf);
49 rxq->sw_ring[i].mbuf = NULL;
54 for (i = 0; i < rxq->bulk_mbuf_num; i++)
55 rte_pktmbuf_free_seg(rxq->bulk_mbuf[i]);
56 rxq->bulk_mbuf_num = 0;
58 if (rxq->pkt_first_seg) {
59 rte_pktmbuf_free(rxq->pkt_first_seg);
60 rxq->pkt_first_seg = NULL;
65 hns3_tx_queue_release_mbufs(struct hns3_tx_queue *txq)
69 /* Note: Fake tx queue will not enter here */
71 for (i = 0; i < txq->nb_tx_desc; i++) {
72 if (txq->sw_ring[i].mbuf) {
73 rte_pktmbuf_free_seg(txq->sw_ring[i].mbuf);
74 txq->sw_ring[i].mbuf = NULL;
81 hns3_rx_queue_release(void *queue)
83 struct hns3_rx_queue *rxq = queue;
85 hns3_rx_queue_release_mbufs(rxq);
87 rte_memzone_free(rxq->mz);
89 rte_free(rxq->sw_ring);
95 hns3_tx_queue_release(void *queue)
97 struct hns3_tx_queue *txq = queue;
99 hns3_tx_queue_release_mbufs(txq);
101 rte_memzone_free(txq->mz);
103 rte_free(txq->sw_ring);
111 hns3_dev_rx_queue_release(void *queue)
113 struct hns3_rx_queue *rxq = queue;
114 struct hns3_adapter *hns;
120 rte_spinlock_lock(&hns->hw.lock);
121 hns3_rx_queue_release(queue);
122 rte_spinlock_unlock(&hns->hw.lock);
126 hns3_dev_tx_queue_release(void *queue)
128 struct hns3_tx_queue *txq = queue;
129 struct hns3_adapter *hns;
135 rte_spinlock_lock(&hns->hw.lock);
136 hns3_tx_queue_release(queue);
137 rte_spinlock_unlock(&hns->hw.lock);
141 hns3_fake_rx_queue_release(struct hns3_rx_queue *queue)
143 struct hns3_rx_queue *rxq = queue;
144 struct hns3_adapter *hns;
154 if (hw->fkq_data.rx_queues[idx]) {
155 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
156 hw->fkq_data.rx_queues[idx] = NULL;
159 /* free fake rx queue arrays */
160 if (idx == (hw->fkq_data.nb_fake_rx_queues - 1)) {
161 hw->fkq_data.nb_fake_rx_queues = 0;
162 rte_free(hw->fkq_data.rx_queues);
163 hw->fkq_data.rx_queues = NULL;
168 hns3_fake_tx_queue_release(struct hns3_tx_queue *queue)
170 struct hns3_tx_queue *txq = queue;
171 struct hns3_adapter *hns;
181 if (hw->fkq_data.tx_queues[idx]) {
182 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
183 hw->fkq_data.tx_queues[idx] = NULL;
186 /* free fake tx queue arrays */
187 if (idx == (hw->fkq_data.nb_fake_tx_queues - 1)) {
188 hw->fkq_data.nb_fake_tx_queues = 0;
189 rte_free(hw->fkq_data.tx_queues);
190 hw->fkq_data.tx_queues = NULL;
195 hns3_free_rx_queues(struct rte_eth_dev *dev)
197 struct hns3_adapter *hns = dev->data->dev_private;
198 struct hns3_fake_queue_data *fkq_data;
199 struct hns3_hw *hw = &hns->hw;
203 nb_rx_q = hw->data->nb_rx_queues;
204 for (i = 0; i < nb_rx_q; i++) {
205 if (dev->data->rx_queues[i]) {
206 hns3_rx_queue_release(dev->data->rx_queues[i]);
207 dev->data->rx_queues[i] = NULL;
211 /* Free fake Rx queues */
212 fkq_data = &hw->fkq_data;
213 for (i = 0; i < fkq_data->nb_fake_rx_queues; i++) {
214 if (fkq_data->rx_queues[i])
215 hns3_fake_rx_queue_release(fkq_data->rx_queues[i]);
220 hns3_free_tx_queues(struct rte_eth_dev *dev)
222 struct hns3_adapter *hns = dev->data->dev_private;
223 struct hns3_fake_queue_data *fkq_data;
224 struct hns3_hw *hw = &hns->hw;
228 nb_tx_q = hw->data->nb_tx_queues;
229 for (i = 0; i < nb_tx_q; i++) {
230 if (dev->data->tx_queues[i]) {
231 hns3_tx_queue_release(dev->data->tx_queues[i]);
232 dev->data->tx_queues[i] = NULL;
236 /* Free fake Tx queues */
237 fkq_data = &hw->fkq_data;
238 for (i = 0; i < fkq_data->nb_fake_tx_queues; i++) {
239 if (fkq_data->tx_queues[i])
240 hns3_fake_tx_queue_release(fkq_data->tx_queues[i]);
245 hns3_free_all_queues(struct rte_eth_dev *dev)
247 hns3_free_rx_queues(dev);
248 hns3_free_tx_queues(dev);
252 hns3_alloc_rx_queue_mbufs(struct hns3_hw *hw, struct hns3_rx_queue *rxq)
254 struct rte_mbuf *mbuf;
258 for (i = 0; i < rxq->nb_rx_desc; i++) {
259 mbuf = rte_mbuf_raw_alloc(rxq->mb_pool);
260 if (unlikely(mbuf == NULL)) {
261 hns3_err(hw, "Failed to allocate RXD[%u] for rx queue!",
263 hns3_rx_queue_release_mbufs(rxq);
267 rte_mbuf_refcnt_set(mbuf, 1);
269 mbuf->data_off = RTE_PKTMBUF_HEADROOM;
271 mbuf->port = rxq->port_id;
273 rxq->sw_ring[i].mbuf = mbuf;
274 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(mbuf));
275 rxq->rx_ring[i].addr = dma_addr;
276 rxq->rx_ring[i].rx.bd_base_info = 0;
283 hns3_buf_size2type(uint32_t buf_size)
289 bd_size_type = HNS3_BD_SIZE_512_TYPE;
292 bd_size_type = HNS3_BD_SIZE_1024_TYPE;
295 bd_size_type = HNS3_BD_SIZE_4096_TYPE;
298 bd_size_type = HNS3_BD_SIZE_2048_TYPE;
305 hns3_init_rx_queue_hw(struct hns3_rx_queue *rxq)
307 uint32_t rx_buf_len = rxq->rx_buf_len;
308 uint64_t dma_addr = rxq->rx_ring_phys_addr;
310 hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_L_REG, (uint32_t)dma_addr);
311 hns3_write_dev(rxq, HNS3_RING_RX_BASEADDR_H_REG,
312 (uint32_t)((dma_addr >> 31) >> 1));
314 hns3_write_dev(rxq, HNS3_RING_RX_BD_LEN_REG,
315 hns3_buf_size2type(rx_buf_len));
316 hns3_write_dev(rxq, HNS3_RING_RX_BD_NUM_REG,
317 HNS3_CFG_DESC_NUM(rxq->nb_rx_desc));
321 hns3_init_tx_queue_hw(struct hns3_tx_queue *txq)
323 uint64_t dma_addr = txq->tx_ring_phys_addr;
325 hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_L_REG, (uint32_t)dma_addr);
326 hns3_write_dev(txq, HNS3_RING_TX_BASEADDR_H_REG,
327 (uint32_t)((dma_addr >> 31) >> 1));
329 hns3_write_dev(txq, HNS3_RING_TX_BD_NUM_REG,
330 HNS3_CFG_DESC_NUM(txq->nb_tx_desc));
334 hns3_update_all_queues_pvid_proc_en(struct hns3_hw *hw)
336 uint16_t nb_rx_q = hw->data->nb_rx_queues;
337 uint16_t nb_tx_q = hw->data->nb_tx_queues;
338 struct hns3_rx_queue *rxq;
339 struct hns3_tx_queue *txq;
343 pvid_en = hw->port_base_vlan_cfg.state == HNS3_PORT_BASE_VLAN_ENABLE;
344 for (i = 0; i < hw->cfg_max_queues; i++) {
346 rxq = hw->data->rx_queues[i];
348 rxq->pvid_sw_discard_en = pvid_en;
351 txq = hw->data->tx_queues[i];
353 txq->pvid_sw_shift_en = pvid_en;
359 hns3_stop_unused_queue(void *tqp_base, enum hns3_ring_type queue_type)
364 reg_offset = queue_type == HNS3_RING_TYPE_TX ?
365 HNS3_RING_TX_EN_REG : HNS3_RING_RX_EN_REG;
366 reg = hns3_read_reg(tqp_base, reg_offset);
367 reg &= ~BIT(HNS3_RING_EN_B);
368 hns3_write_reg(tqp_base, reg_offset, reg);
372 hns3_enable_all_queues(struct hns3_hw *hw, bool en)
374 uint16_t nb_rx_q = hw->data->nb_rx_queues;
375 uint16_t nb_tx_q = hw->data->nb_tx_queues;
376 struct hns3_rx_queue *rxq;
377 struct hns3_tx_queue *txq;
382 for (i = 0; i < hw->cfg_max_queues; i++) {
383 if (hns3_dev_indep_txrx_supported(hw)) {
384 rxq = i < nb_rx_q ? hw->data->rx_queues[i] : NULL;
385 txq = i < nb_tx_q ? hw->data->tx_queues[i] : NULL;
387 tqp_base = (void *)((char *)hw->io_base +
388 hns3_get_tqp_reg_offset(i));
390 * If queue struct is not initialized, it means the
391 * related HW ring has not been initialized yet.
392 * So, these queues should be disabled before enable
393 * the tqps to avoid a HW exception since the queues
394 * are enabled by default.
397 hns3_stop_unused_queue(tqp_base,
400 hns3_stop_unused_queue(tqp_base,
403 rxq = i < nb_rx_q ? hw->data->rx_queues[i] :
404 hw->fkq_data.rx_queues[i - nb_rx_q];
406 tqp_base = rxq->io_base;
409 * This is the master switch that used to control the enabling
410 * of a pair of Tx and Rx queues. Both the Rx and Tx point to
413 rcb_reg = hns3_read_reg(tqp_base, HNS3_RING_EN_REG);
415 rcb_reg |= BIT(HNS3_RING_EN_B);
417 rcb_reg &= ~BIT(HNS3_RING_EN_B);
418 hns3_write_reg(tqp_base, HNS3_RING_EN_REG, rcb_reg);
423 hns3_enable_txq(struct hns3_tx_queue *txq, bool en)
425 struct hns3_hw *hw = &txq->hns->hw;
428 if (hns3_dev_indep_txrx_supported(hw)) {
429 reg = hns3_read_dev(txq, HNS3_RING_TX_EN_REG);
431 reg |= BIT(HNS3_RING_EN_B);
433 reg &= ~BIT(HNS3_RING_EN_B);
434 hns3_write_dev(txq, HNS3_RING_TX_EN_REG, reg);
440 hns3_enable_rxq(struct hns3_rx_queue *rxq, bool en)
442 struct hns3_hw *hw = &rxq->hns->hw;
445 if (hns3_dev_indep_txrx_supported(hw)) {
446 reg = hns3_read_dev(rxq, HNS3_RING_RX_EN_REG);
448 reg |= BIT(HNS3_RING_EN_B);
450 reg &= ~BIT(HNS3_RING_EN_B);
451 hns3_write_dev(rxq, HNS3_RING_RX_EN_REG, reg);
457 hns3_start_all_txqs(struct rte_eth_dev *dev)
459 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
460 struct hns3_tx_queue *txq;
463 for (i = 0; i < dev->data->nb_tx_queues; i++) {
464 txq = hw->data->tx_queues[i];
466 hns3_err(hw, "Tx queue %u not available or setup.", i);
467 goto start_txqs_fail;
470 * Tx queue is enabled by default. Therefore, the Tx queues
471 * needs to be disabled when deferred_start is set. There is
472 * another master switch used to control the enabling of a pair
473 * of Tx and Rx queues. And the master switch is disabled by
476 if (txq->tx_deferred_start)
477 hns3_enable_txq(txq, false);
479 hns3_enable_txq(txq, true);
484 for (j = 0; j < i; j++) {
485 txq = hw->data->tx_queues[j];
486 hns3_enable_txq(txq, false);
492 hns3_start_all_rxqs(struct rte_eth_dev *dev)
494 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
495 struct hns3_rx_queue *rxq;
498 for (i = 0; i < dev->data->nb_rx_queues; i++) {
499 rxq = hw->data->rx_queues[i];
501 hns3_err(hw, "Rx queue %u not available or setup.", i);
502 goto start_rxqs_fail;
505 * Rx queue is enabled by default. Therefore, the Rx queues
506 * needs to be disabled when deferred_start is set. There is
507 * another master switch used to control the enabling of a pair
508 * of Tx and Rx queues. And the master switch is disabled by
511 if (rxq->rx_deferred_start)
512 hns3_enable_rxq(rxq, false);
514 hns3_enable_rxq(rxq, true);
519 for (j = 0; j < i; j++) {
520 rxq = hw->data->rx_queues[j];
521 hns3_enable_rxq(rxq, false);
527 hns3_restore_tqp_enable_state(struct hns3_hw *hw)
529 struct hns3_rx_queue *rxq;
530 struct hns3_tx_queue *txq;
533 for (i = 0; i < hw->data->nb_rx_queues; i++) {
534 rxq = hw->data->rx_queues[i];
536 hns3_enable_rxq(rxq, rxq->enabled);
539 for (i = 0; i < hw->data->nb_tx_queues; i++) {
540 txq = hw->data->tx_queues[i];
542 hns3_enable_txq(txq, txq->enabled);
547 hns3_stop_all_txqs(struct rte_eth_dev *dev)
549 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
550 struct hns3_tx_queue *txq;
553 for (i = 0; i < dev->data->nb_tx_queues; i++) {
554 txq = hw->data->tx_queues[i];
557 hns3_enable_txq(txq, false);
562 hns3_tqp_enable(struct hns3_hw *hw, uint16_t queue_id, bool enable)
564 struct hns3_cfg_com_tqp_queue_cmd *req;
565 struct hns3_cmd_desc desc;
568 req = (struct hns3_cfg_com_tqp_queue_cmd *)desc.data;
570 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_COM_TQP_QUEUE, false);
571 req->tqp_id = rte_cpu_to_le_16(queue_id);
573 hns3_set_bit(req->enable, HNS3_TQP_ENABLE_B, enable ? 1 : 0);
575 ret = hns3_cmd_send(hw, &desc, 1);
577 hns3_err(hw, "TQP enable fail, ret = %d", ret);
583 hns3_send_reset_tqp_cmd(struct hns3_hw *hw, uint16_t queue_id, bool enable)
585 struct hns3_reset_tqp_queue_cmd *req;
586 struct hns3_cmd_desc desc;
589 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, false);
591 req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
592 req->tqp_id = rte_cpu_to_le_16(queue_id);
593 hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);
594 ret = hns3_cmd_send(hw, &desc, 1);
596 hns3_err(hw, "send tqp reset cmd error, queue_id = %u, "
597 "ret = %d", queue_id, ret);
603 hns3_get_tqp_reset_status(struct hns3_hw *hw, uint16_t queue_id,
604 uint8_t *reset_status)
606 struct hns3_reset_tqp_queue_cmd *req;
607 struct hns3_cmd_desc desc;
610 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE, true);
612 req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
613 req->tqp_id = rte_cpu_to_le_16(queue_id);
615 ret = hns3_cmd_send(hw, &desc, 1);
617 hns3_err(hw, "get tqp reset status error, queue_id = %u, "
618 "ret = %d.", queue_id, ret);
621 *reset_status = hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
626 hns3pf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
628 #define HNS3_TQP_RESET_TRY_MS 200
629 uint16_t wait_time = 0;
630 uint8_t reset_status;
634 * In current version VF is not supported when PF is driven by DPDK
635 * driver, all task queue pairs are mapped to PF function, so PF's queue
636 * id is equals to the global queue id in PF range.
638 ret = hns3_send_reset_tqp_cmd(hw, queue_id, true);
640 hns3_err(hw, "Send reset tqp cmd fail, ret = %d", ret);
645 /* Wait for tqp hw reset */
646 rte_delay_ms(HNS3_POLL_RESPONE_MS);
647 wait_time += HNS3_POLL_RESPONE_MS;
648 ret = hns3_get_tqp_reset_status(hw, queue_id, &reset_status);
654 } while (wait_time < HNS3_TQP_RESET_TRY_MS);
658 hns3_err(hw, "reset tqp timeout, queue_id = %u, ret = %d",
663 ret = hns3_send_reset_tqp_cmd(hw, queue_id, false);
665 hns3_err(hw, "Deassert the soft reset fail, ret = %d", ret);
670 hns3_send_reset_tqp_cmd(hw, queue_id, false);
675 hns3vf_reset_tqp(struct hns3_hw *hw, uint16_t queue_id)
680 memcpy(msg_data, &queue_id, sizeof(uint16_t));
682 ret = hns3_send_mbx_msg(hw, HNS3_MBX_QUEUE_RESET, 0, msg_data,
683 sizeof(msg_data), true, NULL, 0);
685 hns3_err(hw, "fail to reset tqp, queue_id = %u, ret = %d.",
691 hns3_reset_rcb_cmd(struct hns3_hw *hw, uint8_t *reset_status)
693 struct hns3_reset_cmd *req;
694 struct hns3_cmd_desc desc;
697 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_CFG_RST_TRIGGER, false);
698 req = (struct hns3_reset_cmd *)desc.data;
699 hns3_set_bit(req->mac_func_reset, HNS3_CFG_RESET_RCB_B, 1);
702 * The start qid should be the global qid of the first tqp of the
703 * function which should be reset in this port. Since our PF not
704 * support take over of VFs, so we only need to reset function 0,
705 * and its start qid is always 0.
707 req->fun_reset_rcb_vqid_start = rte_cpu_to_le_16(0);
708 req->fun_reset_rcb_vqid_num = rte_cpu_to_le_16(hw->cfg_max_queues);
710 ret = hns3_cmd_send(hw, &desc, 1);
712 hns3_err(hw, "fail to send rcb reset cmd, ret = %d.", ret);
716 *reset_status = req->fun_reset_rcb_return_status;
721 hns3pf_reset_all_tqps(struct hns3_hw *hw)
723 #define HNS3_RESET_RCB_NOT_SUPPORT 0U
724 #define HNS3_RESET_ALL_TQP_SUCCESS 1U
725 uint8_t reset_status;
729 ret = hns3_reset_rcb_cmd(hw, &reset_status);
734 * If the firmware version is low, it may not support the rcb reset
735 * which means reset all the tqps at a time. In this case, we should
736 * reset tqps one by one.
738 if (reset_status == HNS3_RESET_RCB_NOT_SUPPORT) {
739 for (i = 0; i < hw->cfg_max_queues; i++) {
740 ret = hns3pf_reset_tqp(hw, i);
743 "fail to reset tqp, queue_id = %d, ret = %d.",
748 } else if (reset_status != HNS3_RESET_ALL_TQP_SUCCESS) {
749 hns3_err(hw, "fail to reset all tqps, reset_status = %u.",
758 hns3vf_reset_all_tqps(struct hns3_hw *hw)
760 #define HNS3VF_RESET_ALL_TQP_DONE 1U
761 uint8_t reset_status;
766 memset(msg_data, 0, sizeof(uint16_t));
767 ret = hns3_send_mbx_msg(hw, HNS3_MBX_QUEUE_RESET, 0, msg_data,
768 sizeof(msg_data), true, &reset_status,
769 sizeof(reset_status));
771 hns3_err(hw, "fail to send rcb reset mbx, ret = %d.", ret);
775 if (reset_status == HNS3VF_RESET_ALL_TQP_DONE)
779 * If the firmware version or kernel PF version is low, it may not
780 * support the rcb reset which means reset all the tqps at a time.
781 * In this case, we should reset tqps one by one.
783 for (i = 1; i < hw->cfg_max_queues; i++) {
784 ret = hns3vf_reset_tqp(hw, i);
793 hns3_reset_all_tqps(struct hns3_adapter *hns)
795 struct hns3_hw *hw = &hns->hw;
798 /* Disable all queues before reset all queues */
799 for (i = 0; i < hw->cfg_max_queues; i++) {
800 ret = hns3_tqp_enable(hw, i, false);
803 "fail to disable tqps before tqps reset, ret = %d.",
810 return hns3vf_reset_all_tqps(hw);
812 return hns3pf_reset_all_tqps(hw);
816 hns3_send_reset_queue_cmd(struct hns3_hw *hw, uint16_t queue_id,
817 enum hns3_ring_type queue_type, bool enable)
819 struct hns3_reset_tqp_queue_cmd *req;
820 struct hns3_cmd_desc desc;
824 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE_INDEP, false);
826 req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
827 req->tqp_id = rte_cpu_to_le_16(queue_id);
828 queue_direction = queue_type == HNS3_RING_TYPE_TX ? 0 : 1;
829 req->queue_direction = rte_cpu_to_le_16(queue_direction);
830 hns3_set_bit(req->reset_req, HNS3_TQP_RESET_B, enable ? 1 : 0);
832 ret = hns3_cmd_send(hw, &desc, 1);
834 hns3_err(hw, "send queue reset cmd error, queue_id = %u, "
835 "queue_type = %s, ret = %d.", queue_id,
836 queue_type == HNS3_RING_TYPE_TX ? "Tx" : "Rx", ret);
841 hns3_get_queue_reset_status(struct hns3_hw *hw, uint16_t queue_id,
842 enum hns3_ring_type queue_type,
843 uint8_t *reset_status)
845 struct hns3_reset_tqp_queue_cmd *req;
846 struct hns3_cmd_desc desc;
850 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_RESET_TQP_QUEUE_INDEP, true);
852 req = (struct hns3_reset_tqp_queue_cmd *)desc.data;
853 req->tqp_id = rte_cpu_to_le_16(queue_id);
854 queue_direction = queue_type == HNS3_RING_TYPE_TX ? 0 : 1;
855 req->queue_direction = rte_cpu_to_le_16(queue_direction);
857 ret = hns3_cmd_send(hw, &desc, 1);
859 hns3_err(hw, "get queue reset status error, queue_id = %u "
860 "queue_type = %s, ret = %d.", queue_id,
861 queue_type == HNS3_RING_TYPE_TX ? "Tx" : "Rx", ret);
865 *reset_status = hns3_get_bit(req->ready_to_reset, HNS3_TQP_RESET_B);
870 hns3_reset_queue(struct hns3_hw *hw, uint16_t queue_id,
871 enum hns3_ring_type queue_type)
873 #define HNS3_QUEUE_RESET_TRY_MS 200
874 struct hns3_tx_queue *txq;
875 struct hns3_rx_queue *rxq;
876 uint32_t reset_wait_times;
877 uint32_t max_wait_times;
878 uint8_t reset_status;
881 if (queue_type == HNS3_RING_TYPE_TX) {
882 txq = hw->data->tx_queues[queue_id];
883 hns3_enable_txq(txq, false);
885 rxq = hw->data->rx_queues[queue_id];
886 hns3_enable_rxq(rxq, false);
889 ret = hns3_send_reset_queue_cmd(hw, queue_id, queue_type, true);
891 hns3_err(hw, "send reset queue cmd fail, ret = %d.", ret);
895 reset_wait_times = 0;
896 max_wait_times = HNS3_QUEUE_RESET_TRY_MS / HNS3_POLL_RESPONE_MS;
897 while (reset_wait_times < max_wait_times) {
898 /* Wait for queue hw reset */
899 rte_delay_ms(HNS3_POLL_RESPONE_MS);
900 ret = hns3_get_queue_reset_status(hw, queue_id,
901 queue_type, &reset_status);
903 goto queue_reset_fail;
911 hns3_err(hw, "reset queue timeout, queue_id = %u, "
912 "queue_type = %s", queue_id,
913 queue_type == HNS3_RING_TYPE_TX ? "Tx" : "Rx");
915 goto queue_reset_fail;
918 ret = hns3_send_reset_queue_cmd(hw, queue_id, queue_type, false);
920 hns3_err(hw, "deassert queue reset fail, ret = %d.", ret);
925 hns3_send_reset_queue_cmd(hw, queue_id, queue_type, false);
930 hns3_get_tqp_intr_reg_offset(uint16_t tqp_intr_id)
934 /* Need an extend offset to config queues > 64 */
935 if (tqp_intr_id < HNS3_MIN_EXT_TQP_INTR_ID)
936 reg_offset = HNS3_TQP_INTR_REG_BASE +
937 tqp_intr_id * HNS3_TQP_INTR_LOW_ORDER_OFFSET;
939 reg_offset = HNS3_TQP_INTR_EXT_REG_BASE +
940 tqp_intr_id / HNS3_MIN_EXT_TQP_INTR_ID *
941 HNS3_TQP_INTR_HIGH_ORDER_OFFSET +
942 tqp_intr_id % HNS3_MIN_EXT_TQP_INTR_ID *
943 HNS3_TQP_INTR_LOW_ORDER_OFFSET;
949 hns3_set_queue_intr_gl(struct hns3_hw *hw, uint16_t queue_id,
950 uint8_t gl_idx, uint16_t gl_value)
952 uint32_t offset[] = {HNS3_TQP_INTR_GL0_REG,
953 HNS3_TQP_INTR_GL1_REG,
954 HNS3_TQP_INTR_GL2_REG};
955 uint32_t addr, value;
957 if (gl_idx >= RTE_DIM(offset) || gl_value > HNS3_TQP_INTR_GL_MAX)
960 addr = offset[gl_idx] + hns3_get_tqp_intr_reg_offset(queue_id);
961 if (hw->intr.gl_unit == HNS3_INTR_COALESCE_GL_UINT_1US)
962 value = gl_value | HNS3_TQP_INTR_GL_UNIT_1US;
964 value = HNS3_GL_USEC_TO_REG(gl_value);
966 hns3_write_dev(hw, addr, value);
970 hns3_set_queue_intr_rl(struct hns3_hw *hw, uint16_t queue_id, uint16_t rl_value)
972 uint32_t addr, value;
974 if (rl_value > HNS3_TQP_INTR_RL_MAX)
977 addr = HNS3_TQP_INTR_RL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
978 value = HNS3_RL_USEC_TO_REG(rl_value);
980 value |= HNS3_TQP_INTR_RL_ENABLE_MASK;
982 hns3_write_dev(hw, addr, value);
986 hns3_set_queue_intr_ql(struct hns3_hw *hw, uint16_t queue_id, uint16_t ql_value)
991 * int_ql_max == 0 means the hardware does not support QL,
992 * QL regs config is not permitted if QL is not supported,
995 if (hw->intr.int_ql_max == HNS3_INTR_QL_NONE)
998 addr = HNS3_TQP_INTR_TX_QL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
999 hns3_write_dev(hw, addr, ql_value);
1001 addr = HNS3_TQP_INTR_RX_QL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
1002 hns3_write_dev(hw, addr, ql_value);
1006 hns3_queue_intr_enable(struct hns3_hw *hw, uint16_t queue_id, bool en)
1008 uint32_t addr, value;
1010 addr = HNS3_TQP_INTR_CTRL_REG + hns3_get_tqp_intr_reg_offset(queue_id);
1013 hns3_write_dev(hw, addr, value);
1017 * Enable all rx queue interrupt when in interrupt rx mode.
1018 * This api was called before enable queue rx&tx (in normal start or reset
1019 * recover scenes), used to fix hardware rx queue interrupt enable was clear
1023 hns3_dev_all_rx_queue_intr_enable(struct hns3_hw *hw, bool en)
1025 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
1026 uint16_t nb_rx_q = hw->data->nb_rx_queues;
1029 if (dev->data->dev_conf.intr_conf.rxq == 0)
1032 for (i = 0; i < nb_rx_q; i++)
1033 hns3_queue_intr_enable(hw, i, en);
1037 hns3_dev_rx_queue_intr_enable(struct rte_eth_dev *dev, uint16_t queue_id)
1039 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(dev);
1040 struct rte_intr_handle *intr_handle = &pci_dev->intr_handle;
1041 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1043 if (dev->data->dev_conf.intr_conf.rxq == 0)
1046 hns3_queue_intr_enable(hw, queue_id, true);
1048 return rte_intr_ack(intr_handle);
1052 hns3_dev_rx_queue_intr_disable(struct rte_eth_dev *dev, uint16_t queue_id)
1054 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1056 if (dev->data->dev_conf.intr_conf.rxq == 0)
1059 hns3_queue_intr_enable(hw, queue_id, false);
1065 hns3_init_rxq(struct hns3_adapter *hns, uint16_t idx)
1067 struct hns3_hw *hw = &hns->hw;
1068 struct hns3_rx_queue *rxq;
1071 PMD_INIT_FUNC_TRACE();
1073 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[idx];
1074 ret = hns3_alloc_rx_queue_mbufs(hw, rxq);
1076 hns3_err(hw, "fail to alloc mbuf for Rx queue %u, ret = %d.",
1081 rxq->next_to_use = 0;
1082 rxq->rx_rearm_start = 0;
1083 rxq->rx_free_hold = 0;
1084 rxq->rx_rearm_nb = 0;
1085 rxq->pkt_first_seg = NULL;
1086 rxq->pkt_last_seg = NULL;
1087 hns3_init_rx_queue_hw(rxq);
1088 hns3_rxq_vec_setup(rxq);
1094 hns3_init_fake_rxq(struct hns3_adapter *hns, uint16_t idx)
1096 struct hns3_hw *hw = &hns->hw;
1097 struct hns3_rx_queue *rxq;
1099 rxq = (struct hns3_rx_queue *)hw->fkq_data.rx_queues[idx];
1100 rxq->next_to_use = 0;
1101 rxq->rx_free_hold = 0;
1102 rxq->rx_rearm_start = 0;
1103 rxq->rx_rearm_nb = 0;
1104 hns3_init_rx_queue_hw(rxq);
1108 hns3_init_txq(struct hns3_tx_queue *txq)
1110 struct hns3_desc *desc;
1114 desc = txq->tx_ring;
1115 for (i = 0; i < txq->nb_tx_desc; i++) {
1116 desc->tx.tp_fe_sc_vld_ra_ri = 0;
1120 txq->next_to_use = 0;
1121 txq->next_to_clean = 0;
1122 txq->tx_bd_ready = txq->nb_tx_desc - 1;
1123 hns3_init_tx_queue_hw(txq);
1127 hns3_init_tx_ring_tc(struct hns3_adapter *hns)
1129 struct hns3_hw *hw = &hns->hw;
1130 struct hns3_tx_queue *txq;
1133 for (i = 0; i < HNS3_MAX_TC_NUM; i++) {
1134 struct hns3_tc_queue_info *tc_queue = &hw->tc_queue[i];
1137 if (!tc_queue->enable)
1140 for (j = 0; j < tc_queue->tqp_count; j++) {
1141 num = tc_queue->tqp_offset + j;
1142 txq = (struct hns3_tx_queue *)hw->data->tx_queues[num];
1146 hns3_write_dev(txq, HNS3_RING_TX_TC_REG, tc_queue->tc);
1152 hns3_init_rx_queues(struct hns3_adapter *hns)
1154 struct hns3_hw *hw = &hns->hw;
1155 struct hns3_rx_queue *rxq;
1159 /* Initialize RSS for queues */
1160 ret = hns3_config_rss(hns);
1162 hns3_err(hw, "failed to configure rss, ret = %d.", ret);
1166 for (i = 0; i < hw->data->nb_rx_queues; i++) {
1167 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[i];
1169 hns3_err(hw, "Rx queue %u not available or setup.", i);
1173 if (rxq->rx_deferred_start)
1176 ret = hns3_init_rxq(hns, i);
1178 hns3_err(hw, "failed to init Rx queue %u, ret = %d.", i,
1184 for (i = 0; i < hw->fkq_data.nb_fake_rx_queues; i++)
1185 hns3_init_fake_rxq(hns, i);
1190 for (j = 0; j < i; j++) {
1191 rxq = (struct hns3_rx_queue *)hw->data->rx_queues[j];
1192 hns3_rx_queue_release_mbufs(rxq);
1199 hns3_init_tx_queues(struct hns3_adapter *hns)
1201 struct hns3_hw *hw = &hns->hw;
1202 struct hns3_tx_queue *txq;
1205 for (i = 0; i < hw->data->nb_tx_queues; i++) {
1206 txq = (struct hns3_tx_queue *)hw->data->tx_queues[i];
1208 hns3_err(hw, "Tx queue %u not available or setup.", i);
1212 if (txq->tx_deferred_start)
1217 for (i = 0; i < hw->fkq_data.nb_fake_tx_queues; i++) {
1218 txq = (struct hns3_tx_queue *)hw->fkq_data.tx_queues[i];
1221 hns3_init_tx_ring_tc(hns);
1228 * Note: just init and setup queues, and don't enable tqps.
1231 hns3_init_queues(struct hns3_adapter *hns, bool reset_queue)
1233 struct hns3_hw *hw = &hns->hw;
1237 ret = hns3_reset_all_tqps(hns);
1239 hns3_err(hw, "failed to reset all queues, ret = %d.",
1245 ret = hns3_init_rx_queues(hns);
1247 hns3_err(hw, "failed to init rx queues, ret = %d.", ret);
1251 ret = hns3_init_tx_queues(hns);
1253 hns3_dev_release_mbufs(hns);
1254 hns3_err(hw, "failed to init tx queues, ret = %d.", ret);
1261 hns3_start_tqps(struct hns3_hw *hw)
1263 struct hns3_tx_queue *txq;
1264 struct hns3_rx_queue *rxq;
1267 hns3_enable_all_queues(hw, true);
1269 for (i = 0; i < hw->data->nb_tx_queues; i++) {
1270 txq = hw->data->tx_queues[i];
1272 hw->data->tx_queue_state[i] =
1273 RTE_ETH_QUEUE_STATE_STARTED;
1276 for (i = 0; i < hw->data->nb_rx_queues; i++) {
1277 rxq = hw->data->rx_queues[i];
1279 hw->data->rx_queue_state[i] =
1280 RTE_ETH_QUEUE_STATE_STARTED;
1285 hns3_stop_tqps(struct hns3_hw *hw)
1289 hns3_enable_all_queues(hw, false);
1291 for (i = 0; i < hw->data->nb_tx_queues; i++)
1292 hw->data->tx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1294 for (i = 0; i < hw->data->nb_rx_queues; i++)
1295 hw->data->rx_queue_state[i] = RTE_ETH_QUEUE_STATE_STOPPED;
1299 * Iterate over all Rx Queue, and call the callback() function for each Rx
1303 * The target eth dev.
1304 * @param[in] callback
1305 * The function to call for each queue.
1306 * if callback function return nonzero will stop iterate and return it's value
1308 * The arguments to provide the callback function with.
1311 * 0 on success, otherwise with errno set.
1314 hns3_rxq_iterate(struct rte_eth_dev *dev,
1315 int (*callback)(struct hns3_rx_queue *, void *), void *arg)
1320 if (dev->data->rx_queues == NULL)
1323 for (i = 0; i < dev->data->nb_rx_queues; i++) {
1324 ret = callback(dev->data->rx_queues[i], arg);
1333 hns3_alloc_rxq_and_dma_zone(struct rte_eth_dev *dev,
1334 struct hns3_queue_info *q_info)
1336 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1337 const struct rte_memzone *rx_mz;
1338 struct hns3_rx_queue *rxq;
1339 unsigned int rx_desc;
1341 rxq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_rx_queue),
1342 RTE_CACHE_LINE_SIZE, q_info->socket_id);
1344 hns3_err(hw, "Failed to allocate memory for No.%u rx ring!",
1349 /* Allocate rx ring hardware descriptors. */
1350 rxq->queue_id = q_info->idx;
1351 rxq->nb_rx_desc = q_info->nb_desc;
1354 * Allocate a litter more memory because rx vector functions
1355 * don't check boundaries each time.
1357 rx_desc = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
1358 sizeof(struct hns3_desc);
1359 rx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
1360 rx_desc, HNS3_RING_BASE_ALIGN,
1362 if (rx_mz == NULL) {
1363 hns3_err(hw, "Failed to reserve DMA memory for No.%u rx ring!",
1365 hns3_rx_queue_release(rxq);
1369 rxq->rx_ring = (struct hns3_desc *)rx_mz->addr;
1370 rxq->rx_ring_phys_addr = rx_mz->iova;
1372 hns3_dbg(hw, "No.%u rx descriptors iova 0x%" PRIx64, q_info->idx,
1373 rxq->rx_ring_phys_addr);
1379 hns3_fake_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
1380 uint16_t nb_desc, unsigned int socket_id)
1382 struct hns3_adapter *hns = dev->data->dev_private;
1383 struct hns3_hw *hw = &hns->hw;
1384 struct hns3_queue_info q_info;
1385 struct hns3_rx_queue *rxq;
1388 if (hw->fkq_data.rx_queues[idx]) {
1389 hns3_rx_queue_release(hw->fkq_data.rx_queues[idx]);
1390 hw->fkq_data.rx_queues[idx] = NULL;
1394 q_info.socket_id = socket_id;
1395 q_info.nb_desc = nb_desc;
1396 q_info.type = "hns3 fake RX queue";
1397 q_info.ring_name = "rx_fake_ring";
1398 rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
1400 hns3_err(hw, "Failed to setup No.%u fake rx ring.", idx);
1404 /* Don't need alloc sw_ring, because upper applications don't use it */
1405 rxq->sw_ring = NULL;
1408 rxq->rx_deferred_start = false;
1409 rxq->port_id = dev->data->port_id;
1410 rxq->configured = true;
1411 nb_rx_q = dev->data->nb_rx_queues;
1412 rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1413 (nb_rx_q + idx) * HNS3_TQP_REG_SIZE);
1414 rxq->rx_buf_len = HNS3_MIN_BD_BUF_SIZE;
1416 rte_spinlock_lock(&hw->lock);
1417 hw->fkq_data.rx_queues[idx] = rxq;
1418 rte_spinlock_unlock(&hw->lock);
1424 hns3_alloc_txq_and_dma_zone(struct rte_eth_dev *dev,
1425 struct hns3_queue_info *q_info)
1427 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1428 const struct rte_memzone *tx_mz;
1429 struct hns3_tx_queue *txq;
1430 struct hns3_desc *desc;
1431 unsigned int tx_desc;
1434 txq = rte_zmalloc_socket(q_info->type, sizeof(struct hns3_tx_queue),
1435 RTE_CACHE_LINE_SIZE, q_info->socket_id);
1437 hns3_err(hw, "Failed to allocate memory for No.%u tx ring!",
1442 /* Allocate tx ring hardware descriptors. */
1443 txq->queue_id = q_info->idx;
1444 txq->nb_tx_desc = q_info->nb_desc;
1445 tx_desc = txq->nb_tx_desc * sizeof(struct hns3_desc);
1446 tx_mz = rte_eth_dma_zone_reserve(dev, q_info->ring_name, q_info->idx,
1447 tx_desc, HNS3_RING_BASE_ALIGN,
1449 if (tx_mz == NULL) {
1450 hns3_err(hw, "Failed to reserve DMA memory for No.%u tx ring!",
1452 hns3_tx_queue_release(txq);
1456 txq->tx_ring = (struct hns3_desc *)tx_mz->addr;
1457 txq->tx_ring_phys_addr = tx_mz->iova;
1459 hns3_dbg(hw, "No.%u tx descriptors iova 0x%" PRIx64, q_info->idx,
1460 txq->tx_ring_phys_addr);
1463 desc = txq->tx_ring;
1464 for (i = 0; i < txq->nb_tx_desc; i++) {
1465 desc->tx.tp_fe_sc_vld_ra_ri = 0;
1473 hns3_fake_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx,
1474 uint16_t nb_desc, unsigned int socket_id)
1476 struct hns3_adapter *hns = dev->data->dev_private;
1477 struct hns3_hw *hw = &hns->hw;
1478 struct hns3_queue_info q_info;
1479 struct hns3_tx_queue *txq;
1482 if (hw->fkq_data.tx_queues[idx] != NULL) {
1483 hns3_tx_queue_release(hw->fkq_data.tx_queues[idx]);
1484 hw->fkq_data.tx_queues[idx] = NULL;
1488 q_info.socket_id = socket_id;
1489 q_info.nb_desc = nb_desc;
1490 q_info.type = "hns3 fake TX queue";
1491 q_info.ring_name = "tx_fake_ring";
1492 txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
1494 hns3_err(hw, "Failed to setup No.%u fake tx ring.", idx);
1498 /* Don't need alloc sw_ring, because upper applications don't use it */
1499 txq->sw_ring = NULL;
1503 txq->tx_deferred_start = false;
1504 txq->port_id = dev->data->port_id;
1505 txq->configured = true;
1506 nb_tx_q = dev->data->nb_tx_queues;
1507 txq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1508 (nb_tx_q + idx) * HNS3_TQP_REG_SIZE);
1510 rte_spinlock_lock(&hw->lock);
1511 hw->fkq_data.tx_queues[idx] = txq;
1512 rte_spinlock_unlock(&hw->lock);
1518 hns3_fake_rx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
1520 uint16_t old_nb_queues = hw->fkq_data.nb_fake_rx_queues;
1524 if (hw->fkq_data.rx_queues == NULL && nb_queues != 0) {
1525 /* first time configuration */
1527 size = sizeof(hw->fkq_data.rx_queues[0]) * nb_queues;
1528 hw->fkq_data.rx_queues = rte_zmalloc("fake_rx_queues", size,
1529 RTE_CACHE_LINE_SIZE);
1530 if (hw->fkq_data.rx_queues == NULL) {
1531 hw->fkq_data.nb_fake_rx_queues = 0;
1534 } else if (hw->fkq_data.rx_queues != NULL && nb_queues != 0) {
1536 rxq = hw->fkq_data.rx_queues;
1537 for (i = nb_queues; i < old_nb_queues; i++)
1538 hns3_dev_rx_queue_release(rxq[i]);
1540 rxq = rte_realloc(rxq, sizeof(rxq[0]) * nb_queues,
1541 RTE_CACHE_LINE_SIZE);
1544 if (nb_queues > old_nb_queues) {
1545 uint16_t new_qs = nb_queues - old_nb_queues;
1546 memset(rxq + old_nb_queues, 0, sizeof(rxq[0]) * new_qs);
1549 hw->fkq_data.rx_queues = rxq;
1550 } else if (hw->fkq_data.rx_queues != NULL && nb_queues == 0) {
1551 rxq = hw->fkq_data.rx_queues;
1552 for (i = nb_queues; i < old_nb_queues; i++)
1553 hns3_dev_rx_queue_release(rxq[i]);
1555 rte_free(hw->fkq_data.rx_queues);
1556 hw->fkq_data.rx_queues = NULL;
1559 hw->fkq_data.nb_fake_rx_queues = nb_queues;
1565 hns3_fake_tx_queue_config(struct hns3_hw *hw, uint16_t nb_queues)
1567 uint16_t old_nb_queues = hw->fkq_data.nb_fake_tx_queues;
1571 if (hw->fkq_data.tx_queues == NULL && nb_queues != 0) {
1572 /* first time configuration */
1574 size = sizeof(hw->fkq_data.tx_queues[0]) * nb_queues;
1575 hw->fkq_data.tx_queues = rte_zmalloc("fake_tx_queues", size,
1576 RTE_CACHE_LINE_SIZE);
1577 if (hw->fkq_data.tx_queues == NULL) {
1578 hw->fkq_data.nb_fake_tx_queues = 0;
1581 } else if (hw->fkq_data.tx_queues != NULL && nb_queues != 0) {
1583 txq = hw->fkq_data.tx_queues;
1584 for (i = nb_queues; i < old_nb_queues; i++)
1585 hns3_dev_tx_queue_release(txq[i]);
1586 txq = rte_realloc(txq, sizeof(txq[0]) * nb_queues,
1587 RTE_CACHE_LINE_SIZE);
1590 if (nb_queues > old_nb_queues) {
1591 uint16_t new_qs = nb_queues - old_nb_queues;
1592 memset(txq + old_nb_queues, 0, sizeof(txq[0]) * new_qs);
1595 hw->fkq_data.tx_queues = txq;
1596 } else if (hw->fkq_data.tx_queues != NULL && nb_queues == 0) {
1597 txq = hw->fkq_data.tx_queues;
1598 for (i = nb_queues; i < old_nb_queues; i++)
1599 hns3_dev_tx_queue_release(txq[i]);
1601 rte_free(hw->fkq_data.tx_queues);
1602 hw->fkq_data.tx_queues = NULL;
1604 hw->fkq_data.nb_fake_tx_queues = nb_queues;
1610 hns3_set_fake_rx_or_tx_queues(struct rte_eth_dev *dev, uint16_t nb_rx_q,
1613 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
1614 uint16_t rx_need_add_nb_q;
1615 uint16_t tx_need_add_nb_q;
1620 /* Setup new number of fake RX/TX queues and reconfigure device. */
1621 rx_need_add_nb_q = hw->cfg_max_queues - nb_rx_q;
1622 tx_need_add_nb_q = hw->cfg_max_queues - nb_tx_q;
1623 ret = hns3_fake_rx_queue_config(hw, rx_need_add_nb_q);
1625 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1629 ret = hns3_fake_tx_queue_config(hw, tx_need_add_nb_q);
1631 hns3_err(hw, "Fail to configure fake rx queues: %d", ret);
1632 goto cfg_fake_tx_q_fail;
1635 /* Allocate and set up fake RX queue per Ethernet port. */
1636 port_id = hw->data->port_id;
1637 for (q = 0; q < rx_need_add_nb_q; q++) {
1638 ret = hns3_fake_rx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1639 rte_eth_dev_socket_id(port_id));
1641 goto setup_fake_rx_q_fail;
1644 /* Allocate and set up fake TX queue per Ethernet port. */
1645 for (q = 0; q < tx_need_add_nb_q; q++) {
1646 ret = hns3_fake_tx_queue_setup(dev, q, HNS3_MIN_RING_DESC,
1647 rte_eth_dev_socket_id(port_id));
1649 goto setup_fake_tx_q_fail;
1654 setup_fake_tx_q_fail:
1655 setup_fake_rx_q_fail:
1656 (void)hns3_fake_tx_queue_config(hw, 0);
1658 (void)hns3_fake_rx_queue_config(hw, 0);
1664 hns3_dev_release_mbufs(struct hns3_adapter *hns)
1666 struct rte_eth_dev_data *dev_data = hns->hw.data;
1667 struct hns3_rx_queue *rxq;
1668 struct hns3_tx_queue *txq;
1671 if (dev_data->rx_queues)
1672 for (i = 0; i < dev_data->nb_rx_queues; i++) {
1673 rxq = dev_data->rx_queues[i];
1676 hns3_rx_queue_release_mbufs(rxq);
1679 if (dev_data->tx_queues)
1680 for (i = 0; i < dev_data->nb_tx_queues; i++) {
1681 txq = dev_data->tx_queues[i];
1684 hns3_tx_queue_release_mbufs(txq);
1689 hns3_rx_buf_len_calc(struct rte_mempool *mp, uint16_t *rx_buf_len)
1691 uint16_t vld_buf_size;
1692 uint16_t num_hw_specs;
1696 * hns3 network engine only support to set 4 typical specification, and
1697 * different buffer size will affect the max packet_len and the max
1698 * number of segmentation when hw gro is turned on in receive side. The
1699 * relationship between them is as follows:
1700 * rx_buf_size | max_gro_pkt_len | max_gro_nb_seg
1701 * ---------------------|-------------------|----------------
1702 * HNS3_4K_BD_BUF_SIZE | 60KB | 15
1703 * HNS3_2K_BD_BUF_SIZE | 62KB | 31
1704 * HNS3_1K_BD_BUF_SIZE | 63KB | 63
1705 * HNS3_512_BD_BUF_SIZE | 31.5KB | 63
1707 static const uint16_t hw_rx_buf_size[] = {
1708 HNS3_4K_BD_BUF_SIZE,
1709 HNS3_2K_BD_BUF_SIZE,
1710 HNS3_1K_BD_BUF_SIZE,
1711 HNS3_512_BD_BUF_SIZE
1714 vld_buf_size = (uint16_t)(rte_pktmbuf_data_room_size(mp) -
1715 RTE_PKTMBUF_HEADROOM);
1716 if (vld_buf_size < HNS3_MIN_BD_BUF_SIZE)
1719 num_hw_specs = RTE_DIM(hw_rx_buf_size);
1720 for (i = 0; i < num_hw_specs; i++) {
1721 if (vld_buf_size >= hw_rx_buf_size[i]) {
1722 *rx_buf_len = hw_rx_buf_size[i];
1730 hns3_rxq_conf_runtime_check(struct hns3_hw *hw, uint16_t buf_size,
1733 struct rte_eth_dev *dev = &rte_eth_devices[hw->data->port_id];
1734 struct rte_eth_rxmode *rxmode = &hw->data->dev_conf.rxmode;
1735 eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
1736 uint16_t min_vec_bds;
1739 * HNS3 hardware network engine set scattered as default. If the driver
1740 * is not work in scattered mode and the pkts greater than buf_size
1741 * but smaller than max_rx_pkt_len will be distributed to multiple BDs.
1742 * Driver cannot handle this situation.
1744 if (!hw->data->scattered_rx && rxmode->max_rx_pkt_len > buf_size) {
1745 hns3_err(hw, "max_rx_pkt_len is not allowed to be set greater "
1746 "than rx_buf_len if scattered is off.");
1750 if (pkt_burst == hns3_recv_pkts_vec) {
1751 min_vec_bds = HNS3_DEFAULT_RXQ_REARM_THRESH +
1752 HNS3_DEFAULT_RX_BURST;
1753 if (nb_desc < min_vec_bds ||
1754 nb_desc % HNS3_DEFAULT_RXQ_REARM_THRESH) {
1755 hns3_err(hw, "if Rx burst mode is vector, "
1756 "number of descriptor is required to be "
1757 "bigger than min vector bds:%u, and could be "
1758 "divided by rxq rearm thresh:%u.",
1759 min_vec_bds, HNS3_DEFAULT_RXQ_REARM_THRESH);
1767 hns3_rx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_rxconf *conf,
1768 struct rte_mempool *mp, uint16_t nb_desc,
1773 if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
1774 nb_desc % HNS3_ALIGN_RING_DESC) {
1775 hns3_err(hw, "Number (%u) of rx descriptors is invalid",
1780 if (conf->rx_drop_en == 0)
1781 hns3_warn(hw, "if no descriptors available, packets are always "
1782 "dropped and rx_drop_en (1) is fixed on");
1784 if (hns3_rx_buf_len_calc(mp, buf_size)) {
1785 hns3_err(hw, "rxq mbufs' data room size (%u) is not enough! "
1786 "minimal data room size (%u).",
1787 rte_pktmbuf_data_room_size(mp),
1788 HNS3_MIN_BD_BUF_SIZE + RTE_PKTMBUF_HEADROOM);
1792 if (hw->data->dev_started) {
1793 ret = hns3_rxq_conf_runtime_check(hw, *buf_size, nb_desc);
1795 hns3_err(hw, "Rx queue runtime setup fail.");
1804 hns3_get_tqp_reg_offset(uint16_t queue_id)
1806 uint32_t reg_offset;
1808 /* Need an extend offset to config queue > 1024 */
1809 if (queue_id < HNS3_MIN_EXTEND_QUEUE_ID)
1810 reg_offset = HNS3_TQP_REG_OFFSET + queue_id * HNS3_TQP_REG_SIZE;
1812 reg_offset = HNS3_TQP_REG_OFFSET + HNS3_TQP_EXT_REG_OFFSET +
1813 (queue_id - HNS3_MIN_EXTEND_QUEUE_ID) *
1820 hns3_rx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
1821 unsigned int socket_id, const struct rte_eth_rxconf *conf,
1822 struct rte_mempool *mp)
1824 struct hns3_adapter *hns = dev->data->dev_private;
1825 struct hns3_hw *hw = &hns->hw;
1826 struct hns3_queue_info q_info;
1827 struct hns3_rx_queue *rxq;
1828 uint16_t rx_buf_size;
1832 ret = hns3_rx_queue_conf_check(hw, conf, mp, nb_desc, &rx_buf_size);
1836 if (dev->data->rx_queues[idx]) {
1837 hns3_rx_queue_release(dev->data->rx_queues[idx]);
1838 dev->data->rx_queues[idx] = NULL;
1842 q_info.socket_id = socket_id;
1843 q_info.nb_desc = nb_desc;
1844 q_info.type = "hns3 RX queue";
1845 q_info.ring_name = "rx_ring";
1847 rxq = hns3_alloc_rxq_and_dma_zone(dev, &q_info);
1850 "Failed to alloc mem and reserve DMA mem for rx ring!");
1855 rxq->ptype_tbl = &hns->ptype_tbl;
1857 rxq->rx_free_thresh = (conf->rx_free_thresh > 0) ?
1858 conf->rx_free_thresh : HNS3_DEFAULT_RX_FREE_THRESH;
1860 rxq->rx_deferred_start = conf->rx_deferred_start;
1861 if (rxq->rx_deferred_start && !hns3_dev_indep_txrx_supported(hw)) {
1862 hns3_warn(hw, "deferred start is not supported.");
1863 rxq->rx_deferred_start = false;
1866 rx_entry_len = (rxq->nb_rx_desc + HNS3_DEFAULT_RX_BURST) *
1867 sizeof(struct hns3_entry);
1868 rxq->sw_ring = rte_zmalloc_socket("hns3 RX sw ring", rx_entry_len,
1869 RTE_CACHE_LINE_SIZE, socket_id);
1870 if (rxq->sw_ring == NULL) {
1871 hns3_err(hw, "Failed to allocate memory for rx sw ring!");
1872 hns3_rx_queue_release(rxq);
1876 rxq->next_to_use = 0;
1877 rxq->rx_free_hold = 0;
1878 rxq->rx_rearm_start = 0;
1879 rxq->rx_rearm_nb = 0;
1880 rxq->pkt_first_seg = NULL;
1881 rxq->pkt_last_seg = NULL;
1882 rxq->port_id = dev->data->port_id;
1884 * For hns3 PF device, if the VLAN mode is HW_SHIFT_AND_DISCARD_MODE,
1885 * the pvid_sw_discard_en in the queue struct should not be changed,
1886 * because PVID-related operations do not need to be processed by PMD
1887 * driver. For hns3 VF device, whether it needs to process PVID depends
1888 * on the configuration of PF kernel mode netdevice driver. And the
1889 * related PF configuration is delivered through the mailbox and finally
1890 * reflectd in port_base_vlan_cfg.
1892 if (hns->is_vf || hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
1893 rxq->pvid_sw_discard_en = hw->port_base_vlan_cfg.state ==
1894 HNS3_PORT_BASE_VLAN_ENABLE;
1896 rxq->pvid_sw_discard_en = false;
1897 rxq->ptype_en = hns3_dev_rxd_adv_layout_supported(hw) ? true : false;
1898 rxq->configured = true;
1899 rxq->io_base = (void *)((char *)hw->io_base + HNS3_TQP_REG_OFFSET +
1900 idx * HNS3_TQP_REG_SIZE);
1901 rxq->io_base = (void *)((char *)hw->io_base +
1902 hns3_get_tqp_reg_offset(idx));
1903 rxq->io_head_reg = (volatile void *)((char *)rxq->io_base +
1904 HNS3_RING_RX_HEAD_REG);
1905 rxq->rx_buf_len = rx_buf_size;
1906 memset(&rxq->basic_stats, 0, sizeof(struct hns3_rx_basic_stats));
1907 memset(&rxq->err_stats, 0, sizeof(struct hns3_rx_bd_errors_stats));
1908 memset(&rxq->dfx_stats, 0, sizeof(struct hns3_rx_dfx_stats));
1910 /* CRC len set here is used for amending packet length */
1911 if (dev->data->dev_conf.rxmode.offloads & DEV_RX_OFFLOAD_KEEP_CRC)
1912 rxq->crc_len = RTE_ETHER_CRC_LEN;
1916 rxq->bulk_mbuf_num = 0;
1918 rte_spinlock_lock(&hw->lock);
1919 dev->data->rx_queues[idx] = rxq;
1920 rte_spinlock_unlock(&hw->lock);
1926 hns3_rx_scattered_reset(struct rte_eth_dev *dev)
1928 struct hns3_adapter *hns = dev->data->dev_private;
1929 struct hns3_hw *hw = &hns->hw;
1932 dev->data->scattered_rx = false;
1936 hns3_rx_scattered_calc(struct rte_eth_dev *dev)
1938 struct rte_eth_conf *dev_conf = &dev->data->dev_conf;
1939 struct hns3_adapter *hns = dev->data->dev_private;
1940 struct hns3_hw *hw = &hns->hw;
1941 struct hns3_rx_queue *rxq;
1944 if (dev->data->rx_queues == NULL)
1947 for (queue_id = 0; queue_id < dev->data->nb_rx_queues; queue_id++) {
1948 rxq = dev->data->rx_queues[queue_id];
1949 if (hw->rx_buf_len == 0)
1950 hw->rx_buf_len = rxq->rx_buf_len;
1952 hw->rx_buf_len = RTE_MIN(hw->rx_buf_len,
1956 if (dev_conf->rxmode.offloads & DEV_RX_OFFLOAD_SCATTER ||
1957 dev_conf->rxmode.max_rx_pkt_len > hw->rx_buf_len)
1958 dev->data->scattered_rx = true;
1962 hns3_dev_supported_ptypes_get(struct rte_eth_dev *dev)
1964 static const uint32_t ptypes[] = {
1966 RTE_PTYPE_L2_ETHER_LLDP,
1967 RTE_PTYPE_L2_ETHER_ARP,
1969 RTE_PTYPE_L3_IPV4_EXT,
1971 RTE_PTYPE_L3_IPV6_EXT,
1977 RTE_PTYPE_TUNNEL_GRE,
1978 RTE_PTYPE_INNER_L2_ETHER,
1979 RTE_PTYPE_INNER_L3_IPV4,
1980 RTE_PTYPE_INNER_L3_IPV6,
1981 RTE_PTYPE_INNER_L3_IPV4_EXT,
1982 RTE_PTYPE_INNER_L3_IPV6_EXT,
1983 RTE_PTYPE_INNER_L4_UDP,
1984 RTE_PTYPE_INNER_L4_TCP,
1985 RTE_PTYPE_INNER_L4_SCTP,
1986 RTE_PTYPE_INNER_L4_ICMP,
1987 RTE_PTYPE_TUNNEL_VXLAN,
1988 RTE_PTYPE_TUNNEL_NVGRE,
1991 static const uint32_t adv_layout_ptypes[] = {
1993 RTE_PTYPE_L2_ETHER_TIMESYNC,
1994 RTE_PTYPE_L2_ETHER_LLDP,
1995 RTE_PTYPE_L2_ETHER_ARP,
1996 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
1997 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
1999 RTE_PTYPE_L4_NONFRAG,
2005 RTE_PTYPE_TUNNEL_GRE,
2006 RTE_PTYPE_TUNNEL_GRENAT,
2007 RTE_PTYPE_INNER_L2_ETHER,
2008 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN,
2009 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN,
2010 RTE_PTYPE_INNER_L4_FRAG,
2011 RTE_PTYPE_INNER_L4_ICMP,
2012 RTE_PTYPE_INNER_L4_NONFRAG,
2013 RTE_PTYPE_INNER_L4_UDP,
2014 RTE_PTYPE_INNER_L4_TCP,
2015 RTE_PTYPE_INNER_L4_SCTP,
2016 RTE_PTYPE_INNER_L4_ICMP,
2019 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
2021 if (dev->rx_pkt_burst == hns3_recv_pkts_simple ||
2022 dev->rx_pkt_burst == hns3_recv_scattered_pkts ||
2023 dev->rx_pkt_burst == hns3_recv_pkts_vec ||
2024 dev->rx_pkt_burst == hns3_recv_pkts_vec_sve) {
2025 if (hns3_dev_rxd_adv_layout_supported(hw))
2026 return adv_layout_ptypes;
2035 hns3_init_non_tunnel_ptype_tbl(struct hns3_ptype_table *tbl)
2037 tbl->l3table[0] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4;
2038 tbl->l3table[1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6;
2039 tbl->l3table[2] = RTE_PTYPE_L2_ETHER_ARP;
2040 tbl->l3table[4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT;
2041 tbl->l3table[5] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT;
2042 tbl->l3table[6] = RTE_PTYPE_L2_ETHER_LLDP;
2044 tbl->l4table[0] = RTE_PTYPE_L4_UDP;
2045 tbl->l4table[1] = RTE_PTYPE_L4_TCP;
2046 tbl->l4table[2] = RTE_PTYPE_TUNNEL_GRE;
2047 tbl->l4table[3] = RTE_PTYPE_L4_SCTP;
2048 tbl->l4table[4] = RTE_PTYPE_L4_IGMP;
2049 tbl->l4table[5] = RTE_PTYPE_L4_ICMP;
2053 hns3_init_tunnel_ptype_tbl(struct hns3_ptype_table *tbl)
2055 tbl->inner_l3table[0] = RTE_PTYPE_INNER_L2_ETHER |
2056 RTE_PTYPE_INNER_L3_IPV4;
2057 tbl->inner_l3table[1] = RTE_PTYPE_INNER_L2_ETHER |
2058 RTE_PTYPE_INNER_L3_IPV6;
2059 /* There is not a ptype for inner ARP/RARP */
2060 tbl->inner_l3table[2] = RTE_PTYPE_UNKNOWN;
2061 tbl->inner_l3table[3] = RTE_PTYPE_UNKNOWN;
2062 tbl->inner_l3table[4] = RTE_PTYPE_INNER_L2_ETHER |
2063 RTE_PTYPE_INNER_L3_IPV4_EXT;
2064 tbl->inner_l3table[5] = RTE_PTYPE_INNER_L2_ETHER |
2065 RTE_PTYPE_INNER_L3_IPV6_EXT;
2067 tbl->inner_l4table[0] = RTE_PTYPE_INNER_L4_UDP;
2068 tbl->inner_l4table[1] = RTE_PTYPE_INNER_L4_TCP;
2069 /* There is not a ptype for inner GRE */
2070 tbl->inner_l4table[2] = RTE_PTYPE_UNKNOWN;
2071 tbl->inner_l4table[3] = RTE_PTYPE_INNER_L4_SCTP;
2072 /* There is not a ptype for inner IGMP */
2073 tbl->inner_l4table[4] = RTE_PTYPE_UNKNOWN;
2074 tbl->inner_l4table[5] = RTE_PTYPE_INNER_L4_ICMP;
2076 tbl->ol3table[0] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4;
2077 tbl->ol3table[1] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6;
2078 tbl->ol3table[2] = RTE_PTYPE_UNKNOWN;
2079 tbl->ol3table[3] = RTE_PTYPE_UNKNOWN;
2080 tbl->ol3table[4] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT;
2081 tbl->ol3table[5] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT;
2083 tbl->ol4table[0] = RTE_PTYPE_UNKNOWN;
2084 tbl->ol4table[1] = RTE_PTYPE_L4_UDP | RTE_PTYPE_TUNNEL_VXLAN;
2085 tbl->ol4table[2] = RTE_PTYPE_TUNNEL_NVGRE;
2089 hns3_init_adv_layout_ptype(struct hns3_ptype_table *tbl)
2091 uint32_t *ptype = tbl->ptype;
2094 ptype[1] = RTE_PTYPE_L2_ETHER_ARP;
2095 ptype[3] = RTE_PTYPE_L2_ETHER_LLDP;
2096 ptype[8] = RTE_PTYPE_L2_ETHER_TIMESYNC;
2098 /* Non-tunnel IPv4 */
2099 ptype[17] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2101 ptype[18] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2102 RTE_PTYPE_L4_NONFRAG;
2103 ptype[19] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2105 ptype[20] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2107 ptype[21] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2108 RTE_PTYPE_TUNNEL_GRE;
2109 ptype[22] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2111 ptype[23] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2113 ptype[24] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2115 /* The next ptype is PTP over IPv4 + UDP */
2116 ptype[25] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2119 /* IPv4 --> GRE/Teredo/VXLAN */
2120 ptype[29] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2121 RTE_PTYPE_TUNNEL_GRENAT;
2122 /* IPv4 --> GRE/Teredo/VXLAN --> MAC */
2123 ptype[30] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2124 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER;
2126 /* IPv4 --> GRE/Teredo/VXLAN --> MAC --> IPv4 */
2127 ptype[31] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2128 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2129 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2130 RTE_PTYPE_INNER_L4_FRAG;
2131 ptype[32] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2132 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2133 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2134 RTE_PTYPE_INNER_L4_NONFRAG;
2135 ptype[33] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2136 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2137 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2138 RTE_PTYPE_INNER_L4_UDP;
2139 ptype[34] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2140 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2141 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2142 RTE_PTYPE_INNER_L4_TCP;
2143 ptype[35] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2144 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2145 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2146 RTE_PTYPE_INNER_L4_SCTP;
2147 /* The next ptype's inner L4 is IGMP */
2148 ptype[36] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2149 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2150 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN;
2151 ptype[37] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2152 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2153 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2154 RTE_PTYPE_INNER_L4_ICMP;
2156 /* IPv4 --> GRE/Teredo/VXLAN --> MAC --> IPv6 */
2157 ptype[39] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2158 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2159 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2160 RTE_PTYPE_INNER_L4_FRAG;
2161 ptype[40] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2162 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2163 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2164 RTE_PTYPE_INNER_L4_NONFRAG;
2165 ptype[41] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2166 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2167 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2168 RTE_PTYPE_INNER_L4_UDP;
2169 ptype[42] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2170 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2171 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2172 RTE_PTYPE_INNER_L4_TCP;
2173 ptype[43] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2174 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2175 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2176 RTE_PTYPE_INNER_L4_SCTP;
2177 /* The next ptype's inner L4 is IGMP */
2178 ptype[44] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2179 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2180 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN;
2181 ptype[45] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV4_EXT_UNKNOWN |
2182 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2183 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2184 RTE_PTYPE_INNER_L4_ICMP;
2186 /* Non-tunnel IPv6 */
2187 ptype[111] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2189 ptype[112] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2190 RTE_PTYPE_L4_NONFRAG;
2191 ptype[113] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2193 ptype[114] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2195 ptype[115] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2196 RTE_PTYPE_TUNNEL_GRE;
2197 ptype[116] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2199 ptype[117] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2201 ptype[118] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2203 /* Special for PTP over IPv6 + UDP */
2204 ptype[119] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2207 /* IPv6 --> GRE/Teredo/VXLAN */
2208 ptype[123] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2209 RTE_PTYPE_TUNNEL_GRENAT;
2210 /* IPv6 --> GRE/Teredo/VXLAN --> MAC */
2211 ptype[124] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2212 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER;
2214 /* IPv6 --> GRE/Teredo/VXLAN --> MAC --> IPv4 */
2215 ptype[125] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2216 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2217 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2218 RTE_PTYPE_INNER_L4_FRAG;
2219 ptype[126] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2220 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2221 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2222 RTE_PTYPE_INNER_L4_NONFRAG;
2223 ptype[127] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2224 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2225 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2226 RTE_PTYPE_INNER_L4_UDP;
2227 ptype[128] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2228 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2229 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2230 RTE_PTYPE_INNER_L4_TCP;
2231 ptype[129] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2232 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2233 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2234 RTE_PTYPE_INNER_L4_SCTP;
2235 /* The next ptype's inner L4 is IGMP */
2236 ptype[130] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2237 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2238 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN;
2239 ptype[131] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2240 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2241 RTE_PTYPE_INNER_L3_IPV4_EXT_UNKNOWN |
2242 RTE_PTYPE_INNER_L4_ICMP;
2244 /* IPv6 --> GRE/Teredo/VXLAN --> MAC --> IPv6 */
2245 ptype[133] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2246 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2247 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2248 RTE_PTYPE_INNER_L4_FRAG;
2249 ptype[134] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2250 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2251 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2252 RTE_PTYPE_INNER_L4_NONFRAG;
2253 ptype[135] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2254 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2255 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2256 RTE_PTYPE_INNER_L4_UDP;
2257 ptype[136] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2258 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2259 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2260 RTE_PTYPE_INNER_L4_TCP;
2261 ptype[137] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2262 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2263 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2264 RTE_PTYPE_INNER_L4_SCTP;
2265 /* The next ptype's inner L4 is IGMP */
2266 ptype[138] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2267 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2268 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN;
2269 ptype[139] = RTE_PTYPE_L2_ETHER | RTE_PTYPE_L3_IPV6_EXT_UNKNOWN |
2270 RTE_PTYPE_TUNNEL_GRENAT | RTE_PTYPE_INNER_L2_ETHER |
2271 RTE_PTYPE_INNER_L3_IPV6_EXT_UNKNOWN |
2272 RTE_PTYPE_INNER_L4_ICMP;
2276 hns3_init_rx_ptype_tble(struct rte_eth_dev *dev)
2278 struct hns3_adapter *hns = dev->data->dev_private;
2279 struct hns3_ptype_table *tbl = &hns->ptype_tbl;
2281 memset(tbl, 0, sizeof(*tbl));
2283 hns3_init_non_tunnel_ptype_tbl(tbl);
2284 hns3_init_tunnel_ptype_tbl(tbl);
2285 hns3_init_adv_layout_ptype(tbl);
2289 hns3_rxd_to_vlan_tci(struct hns3_rx_queue *rxq, struct rte_mbuf *mb,
2290 uint32_t l234_info, const struct hns3_desc *rxd)
2292 #define HNS3_STRP_STATUS_NUM 0x4
2294 #define HNS3_NO_STRP_VLAN_VLD 0x0
2295 #define HNS3_INNER_STRP_VLAN_VLD 0x1
2296 #define HNS3_OUTER_STRP_VLAN_VLD 0x2
2297 uint32_t strip_status;
2298 uint32_t report_mode;
2301 * Since HW limitation, the vlan tag will always be inserted into RX
2302 * descriptor when strip the tag from packet, driver needs to determine
2303 * reporting which tag to mbuf according to the PVID configuration
2304 * and vlan striped status.
2306 static const uint32_t report_type[][HNS3_STRP_STATUS_NUM] = {
2308 HNS3_NO_STRP_VLAN_VLD,
2309 HNS3_OUTER_STRP_VLAN_VLD,
2310 HNS3_INNER_STRP_VLAN_VLD,
2311 HNS3_OUTER_STRP_VLAN_VLD
2314 HNS3_NO_STRP_VLAN_VLD,
2315 HNS3_NO_STRP_VLAN_VLD,
2316 HNS3_NO_STRP_VLAN_VLD,
2317 HNS3_INNER_STRP_VLAN_VLD
2320 strip_status = hns3_get_field(l234_info, HNS3_RXD_STRP_TAGP_M,
2321 HNS3_RXD_STRP_TAGP_S);
2322 report_mode = report_type[rxq->pvid_sw_discard_en][strip_status];
2323 switch (report_mode) {
2324 case HNS3_NO_STRP_VLAN_VLD:
2327 case HNS3_INNER_STRP_VLAN_VLD:
2328 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
2329 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.vlan_tag);
2331 case HNS3_OUTER_STRP_VLAN_VLD:
2332 mb->ol_flags |= PKT_RX_VLAN | PKT_RX_VLAN_STRIPPED;
2333 mb->vlan_tci = rte_le_to_cpu_16(rxd->rx.ot_vlan_tag);
2342 recalculate_data_len(struct rte_mbuf *first_seg, struct rte_mbuf *last_seg,
2343 struct rte_mbuf *rxm, struct hns3_rx_queue *rxq,
2346 uint8_t crc_len = rxq->crc_len;
2348 if (data_len <= crc_len) {
2349 rte_pktmbuf_free_seg(rxm);
2350 first_seg->nb_segs--;
2351 last_seg->data_len = (uint16_t)(last_seg->data_len -
2352 (crc_len - data_len));
2353 last_seg->next = NULL;
2355 rxm->data_len = (uint16_t)(data_len - crc_len);
2358 static inline struct rte_mbuf *
2359 hns3_rx_alloc_buffer(struct hns3_rx_queue *rxq)
2363 if (likely(rxq->bulk_mbuf_num > 0))
2364 return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];
2366 ret = rte_mempool_get_bulk(rxq->mb_pool, (void **)rxq->bulk_mbuf,
2367 HNS3_BULK_ALLOC_MBUF_NUM);
2368 if (likely(ret == 0)) {
2369 rxq->bulk_mbuf_num = HNS3_BULK_ALLOC_MBUF_NUM;
2370 return rxq->bulk_mbuf[--rxq->bulk_mbuf_num];
2372 return rte_mbuf_raw_alloc(rxq->mb_pool);
2376 hns3_rx_ptp_timestamp_handle(struct hns3_rx_queue *rxq, struct rte_mbuf *mbuf,
2377 volatile struct hns3_desc *rxd)
2379 struct hns3_pf *pf = HNS3_DEV_PRIVATE_TO_PF(rxq->hns);
2380 uint64_t timestamp = rte_le_to_cpu_64(rxd->timestamp);
2382 mbuf->ol_flags |= PKT_RX_IEEE1588_PTP | PKT_RX_IEEE1588_TMST;
2383 if (hns3_timestamp_rx_dynflag > 0) {
2384 *RTE_MBUF_DYNFIELD(mbuf, hns3_timestamp_dynfield_offset,
2385 rte_mbuf_timestamp_t *) = timestamp;
2386 mbuf->ol_flags |= hns3_timestamp_rx_dynflag;
2389 pf->rx_timestamp = timestamp;
2393 hns3_recv_pkts_simple(void *rx_queue,
2394 struct rte_mbuf **rx_pkts,
2397 volatile struct hns3_desc *rx_ring; /* RX ring (desc) */
2398 volatile struct hns3_desc *rxdp; /* pointer of the current desc */
2399 struct hns3_rx_queue *rxq; /* RX queue */
2400 struct hns3_entry *sw_ring;
2401 struct hns3_entry *rxe;
2402 struct hns3_desc rxd;
2403 struct rte_mbuf *nmb; /* pointer of the new mbuf */
2404 struct rte_mbuf *rxm;
2405 uint32_t bd_base_info;
2417 rx_ring = rxq->rx_ring;
2418 sw_ring = rxq->sw_ring;
2419 rx_id = rxq->next_to_use;
2421 while (nb_rx < nb_pkts) {
2422 rxdp = &rx_ring[rx_id];
2423 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
2424 if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
2427 rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
2428 (1u << HNS3_RXD_VLD_B)];
2430 nmb = hns3_rx_alloc_buffer(rxq);
2431 if (unlikely(nmb == NULL)) {
2434 port_id = rxq->port_id;
2435 rte_eth_devices[port_id].data->rx_mbuf_alloc_failed++;
2440 rxe = &sw_ring[rx_id];
2442 if (unlikely(rx_id == rxq->nb_rx_desc))
2445 rte_prefetch0(sw_ring[rx_id].mbuf);
2446 if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
2447 rte_prefetch0(&rx_ring[rx_id]);
2448 rte_prefetch0(&sw_ring[rx_id]);
2455 if (unlikely(bd_base_info & BIT(HNS3_RXD_TS_VLD_B)))
2456 hns3_rx_ptp_timestamp_handle(rxq, rxm, rxdp);
2458 dma_addr = rte_mbuf_data_iova_default(nmb);
2459 rxdp->addr = rte_cpu_to_le_64(dma_addr);
2460 rxdp->rx.bd_base_info = 0;
2462 rxm->data_off = RTE_PKTMBUF_HEADROOM;
2463 rxm->pkt_len = (uint16_t)(rte_le_to_cpu_16(rxd.rx.pkt_len)) -
2465 rxm->data_len = rxm->pkt_len;
2466 rxm->port = rxq->port_id;
2467 rxm->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
2468 rxm->ol_flags |= PKT_RX_RSS_HASH;
2469 if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
2471 rte_le_to_cpu_16(rxd.rx.fd_id);
2472 rxm->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
2477 /* Load remained descriptor data and extract necessary fields */
2478 l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
2479 ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
2480 ret = hns3_handle_bdinfo(rxq, rxm, bd_base_info, l234_info);
2484 rxm->packet_type = hns3_rx_calc_ptype(rxq, l234_info, ol_info);
2486 if (rxm->packet_type == RTE_PTYPE_L2_ETHER_TIMESYNC)
2487 rxm->ol_flags |= PKT_RX_IEEE1588_PTP;
2489 hns3_rxd_to_vlan_tci(rxq, rxm, l234_info, &rxd);
2491 /* Increment bytes counter */
2492 rxq->basic_stats.bytes += rxm->pkt_len;
2494 rx_pkts[nb_rx++] = rxm;
2497 rte_pktmbuf_free(rxm);
2500 rxq->next_to_use = rx_id;
2501 rxq->rx_free_hold += nb_rx_bd;
2502 if (rxq->rx_free_hold > rxq->rx_free_thresh) {
2503 hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
2504 rxq->rx_free_hold = 0;
2511 hns3_recv_scattered_pkts(void *rx_queue,
2512 struct rte_mbuf **rx_pkts,
2515 volatile struct hns3_desc *rx_ring; /* RX ring (desc) */
2516 volatile struct hns3_desc *rxdp; /* pointer of the current desc */
2517 struct hns3_rx_queue *rxq; /* RX queue */
2518 struct hns3_entry *sw_ring;
2519 struct hns3_entry *rxe;
2520 struct rte_mbuf *first_seg;
2521 struct rte_mbuf *last_seg;
2522 struct hns3_desc rxd;
2523 struct rte_mbuf *nmb; /* pointer of the new mbuf */
2524 struct rte_mbuf *rxm;
2525 struct rte_eth_dev *dev;
2526 uint32_t bd_base_info;
2540 rx_id = rxq->next_to_use;
2541 rx_ring = rxq->rx_ring;
2542 sw_ring = rxq->sw_ring;
2543 first_seg = rxq->pkt_first_seg;
2544 last_seg = rxq->pkt_last_seg;
2546 while (nb_rx < nb_pkts) {
2547 rxdp = &rx_ring[rx_id];
2548 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
2549 if (unlikely(!(bd_base_info & BIT(HNS3_RXD_VLD_B))))
2553 * The interactive process between software and hardware of
2554 * receiving a new packet in hns3 network engine:
2555 * 1. Hardware network engine firstly writes the packet content
2556 * to the memory pointed by the 'addr' field of the Rx Buffer
2557 * Descriptor, secondly fills the result of parsing the
2558 * packet include the valid field into the Rx Buffer
2559 * Descriptor in one write operation.
2560 * 2. Driver reads the Rx BD's valid field in the loop to check
2561 * whether it's valid, if valid then assign a new address to
2562 * the addr field, clear the valid field, get the other
2563 * information of the packet by parsing Rx BD's other fields,
2564 * finally write back the number of Rx BDs processed by the
2565 * driver to the HNS3_RING_RX_HEAD_REG register to inform
2567 * In the above process, the ordering is very important. We must
2568 * make sure that CPU read Rx BD's other fields only after the
2571 * There are two type of re-ordering: compiler re-ordering and
2572 * CPU re-ordering under the ARMv8 architecture.
2573 * 1. we use volatile to deal with compiler re-ordering, so you
2574 * can see that rx_ring/rxdp defined with volatile.
2575 * 2. we commonly use memory barrier to deal with CPU
2576 * re-ordering, but the cost is high.
2578 * In order to solve the high cost of using memory barrier, we
2579 * use the data dependency order under the ARMv8 architecture,
2582 * instr02: load B <- A
2583 * the instr02 will always execute after instr01.
2585 * To construct the data dependency ordering, we use the
2586 * following assignment:
2587 * rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
2588 * (1u<<HNS3_RXD_VLD_B)]
2589 * Using gcc compiler under the ARMv8 architecture, the related
2590 * assembly code example as follows:
2591 * note: (1u << HNS3_RXD_VLD_B) equal 0x10
2592 * instr01: ldr w26, [x22, #28] --read bd_base_info
2593 * instr02: and w0, w26, #0x10 --calc bd_base_info & 0x10
2594 * instr03: sub w0, w0, #0x10 --calc (bd_base_info &
2596 * instr04: add x0, x22, x0, lsl #5 --calc copy source addr
2597 * instr05: ldp x2, x3, [x0]
2598 * instr06: stp x2, x3, [x29, #256] --copy BD's [0 ~ 15]B
2599 * instr07: ldp x4, x5, [x0, #16]
2600 * instr08: stp x4, x5, [x29, #272] --copy BD's [16 ~ 31]B
2601 * the instr05~08 depend on x0's value, x0 depent on w26's
2602 * value, the w26 is the bd_base_info, this form the data
2603 * dependency ordering.
2604 * note: if BD is valid, (bd_base_info & (1u<<HNS3_RXD_VLD_B)) -
2605 * (1u<<HNS3_RXD_VLD_B) will always zero, so the
2606 * assignment is correct.
2608 * So we use the data dependency ordering instead of memory
2609 * barrier to improve receive performance.
2611 rxd = rxdp[(bd_base_info & (1u << HNS3_RXD_VLD_B)) -
2612 (1u << HNS3_RXD_VLD_B)];
2614 nmb = hns3_rx_alloc_buffer(rxq);
2615 if (unlikely(nmb == NULL)) {
2616 dev = &rte_eth_devices[rxq->port_id];
2617 dev->data->rx_mbuf_alloc_failed++;
2622 rxe = &sw_ring[rx_id];
2624 if (unlikely(rx_id == rxq->nb_rx_desc))
2627 rte_prefetch0(sw_ring[rx_id].mbuf);
2628 if ((rx_id & HNS3_RX_RING_PREFETCTH_MASK) == 0) {
2629 rte_prefetch0(&rx_ring[rx_id]);
2630 rte_prefetch0(&sw_ring[rx_id]);
2636 dma_addr = rte_cpu_to_le_64(rte_mbuf_data_iova_default(nmb));
2637 rxdp->rx.bd_base_info = 0;
2638 rxdp->addr = dma_addr;
2640 if (first_seg == NULL) {
2642 first_seg->nb_segs = 1;
2644 first_seg->nb_segs++;
2645 last_seg->next = rxm;
2648 rxm->data_off = RTE_PKTMBUF_HEADROOM;
2649 rxm->data_len = rte_le_to_cpu_16(rxd.rx.size);
2651 if (!(bd_base_info & BIT(HNS3_RXD_FE_B))) {
2657 if (unlikely(bd_base_info & BIT(HNS3_RXD_TS_VLD_B)))
2658 hns3_rx_ptp_timestamp_handle(rxq, first_seg, rxdp);
2661 * The last buffer of the received packet. packet len from
2662 * buffer description may contains CRC len, packet len should
2663 * subtract it, same as data len.
2665 first_seg->pkt_len = rte_le_to_cpu_16(rxd.rx.pkt_len);
2668 * This is the last buffer of the received packet. If the CRC
2669 * is not stripped by the hardware:
2670 * - Subtract the CRC length from the total packet length.
2671 * - If the last buffer only contains the whole CRC or a part
2672 * of it, free the mbuf associated to the last buffer. If part
2673 * of the CRC is also contained in the previous mbuf, subtract
2674 * the length of that CRC part from the data length of the
2678 if (unlikely(rxq->crc_len > 0)) {
2679 first_seg->pkt_len -= rxq->crc_len;
2680 recalculate_data_len(first_seg, last_seg, rxm, rxq,
2684 first_seg->port = rxq->port_id;
2685 first_seg->hash.rss = rte_le_to_cpu_32(rxd.rx.rss_hash);
2686 first_seg->ol_flags = PKT_RX_RSS_HASH;
2687 if (unlikely(bd_base_info & BIT(HNS3_RXD_LUM_B))) {
2688 first_seg->hash.fdir.hi =
2689 rte_le_to_cpu_16(rxd.rx.fd_id);
2690 first_seg->ol_flags |= PKT_RX_FDIR | PKT_RX_FDIR_ID;
2693 gro_size = hns3_get_field(bd_base_info, HNS3_RXD_GRO_SIZE_M,
2694 HNS3_RXD_GRO_SIZE_S);
2695 if (gro_size != 0) {
2696 first_seg->ol_flags |= PKT_RX_LRO;
2697 first_seg->tso_segsz = gro_size;
2700 l234_info = rte_le_to_cpu_32(rxd.rx.l234_info);
2701 ol_info = rte_le_to_cpu_32(rxd.rx.ol_info);
2702 ret = hns3_handle_bdinfo(rxq, first_seg, bd_base_info,
2707 first_seg->packet_type = hns3_rx_calc_ptype(rxq,
2708 l234_info, ol_info);
2710 if (first_seg->packet_type == RTE_PTYPE_L2_ETHER_TIMESYNC)
2711 rxm->ol_flags |= PKT_RX_IEEE1588_PTP;
2713 hns3_rxd_to_vlan_tci(rxq, first_seg, l234_info, &rxd);
2715 /* Increment bytes counter */
2716 rxq->basic_stats.bytes += first_seg->pkt_len;
2718 rx_pkts[nb_rx++] = first_seg;
2722 rte_pktmbuf_free(first_seg);
2726 rxq->next_to_use = rx_id;
2727 rxq->pkt_first_seg = first_seg;
2728 rxq->pkt_last_seg = last_seg;
2730 rxq->rx_free_hold += nb_rx_bd;
2731 if (rxq->rx_free_hold > rxq->rx_free_thresh) {
2732 hns3_write_reg_opt(rxq->io_head_reg, rxq->rx_free_hold);
2733 rxq->rx_free_hold = 0;
2740 hns3_rxq_vec_setup(__rte_unused struct hns3_rx_queue *rxq)
2745 hns3_rx_check_vec_support(__rte_unused struct rte_eth_dev *dev)
2751 hns3_recv_pkts_vec(__rte_unused void *tx_queue,
2752 __rte_unused struct rte_mbuf **rx_pkts,
2753 __rte_unused uint16_t nb_pkts)
2759 hns3_recv_pkts_vec_sve(__rte_unused void *tx_queue,
2760 __rte_unused struct rte_mbuf **rx_pkts,
2761 __rte_unused uint16_t nb_pkts)
2767 hns3_rx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
2768 struct rte_eth_burst_mode *mode)
2770 static const struct {
2771 eth_rx_burst_t pkt_burst;
2774 { hns3_recv_pkts_simple, "Scalar Simple" },
2775 { hns3_recv_scattered_pkts, "Scalar Scattered" },
2776 { hns3_recv_pkts_vec, "Vector Neon" },
2777 { hns3_recv_pkts_vec_sve, "Vector Sve" },
2780 eth_rx_burst_t pkt_burst = dev->rx_pkt_burst;
2784 for (i = 0; i < RTE_DIM(burst_infos); i++) {
2785 if (pkt_burst == burst_infos[i].pkt_burst) {
2786 snprintf(mode->info, sizeof(mode->info), "%s",
2787 burst_infos[i].info);
2797 hns3_get_default_vec_support(void)
2799 #if defined(RTE_ARCH_ARM64)
2800 if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_128)
2802 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_NEON))
2809 hns3_get_sve_support(void)
2811 #if defined(RTE_ARCH_ARM64) && defined(__ARM_FEATURE_SVE)
2812 if (rte_vect_get_max_simd_bitwidth() < RTE_VECT_SIMD_256)
2814 if (rte_cpu_get_flag_enabled(RTE_CPUFLAG_SVE))
2820 static eth_rx_burst_t
2821 hns3_get_rx_function(struct rte_eth_dev *dev)
2823 struct hns3_adapter *hns = dev->data->dev_private;
2824 uint64_t offloads = dev->data->dev_conf.rxmode.offloads;
2825 bool vec_allowed, sve_allowed, simple_allowed;
2828 vec_support = hns3_rx_check_vec_support(dev) == 0;
2829 vec_allowed = vec_support && hns3_get_default_vec_support();
2830 sve_allowed = vec_support && hns3_get_sve_support();
2831 simple_allowed = !dev->data->scattered_rx &&
2832 (offloads & DEV_RX_OFFLOAD_TCP_LRO) == 0;
2834 if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_VEC && vec_allowed)
2835 return hns3_recv_pkts_vec;
2836 if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_SVE && sve_allowed)
2837 return hns3_recv_pkts_vec_sve;
2838 if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_SIMPLE && simple_allowed)
2839 return hns3_recv_pkts_simple;
2840 if (hns->rx_func_hint == HNS3_IO_FUNC_HINT_COMMON)
2841 return hns3_recv_scattered_pkts;
2844 return hns3_recv_pkts_vec;
2846 return hns3_recv_pkts_simple;
2848 return hns3_recv_scattered_pkts;
2852 hns3_tx_queue_conf_check(struct hns3_hw *hw, const struct rte_eth_txconf *conf,
2853 uint16_t nb_desc, uint16_t *tx_rs_thresh,
2854 uint16_t *tx_free_thresh, uint16_t idx)
2856 #define HNS3_TX_RS_FREE_THRESH_GAP 8
2857 uint16_t rs_thresh, free_thresh, fast_free_thresh;
2859 if (nb_desc > HNS3_MAX_RING_DESC || nb_desc < HNS3_MIN_RING_DESC ||
2860 nb_desc % HNS3_ALIGN_RING_DESC) {
2861 hns3_err(hw, "number (%u) of tx descriptors is invalid",
2866 rs_thresh = (conf->tx_rs_thresh > 0) ?
2867 conf->tx_rs_thresh : HNS3_DEFAULT_TX_RS_THRESH;
2868 free_thresh = (conf->tx_free_thresh > 0) ?
2869 conf->tx_free_thresh : HNS3_DEFAULT_TX_FREE_THRESH;
2870 if (rs_thresh + free_thresh > nb_desc || nb_desc % rs_thresh ||
2871 rs_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP ||
2872 free_thresh >= nb_desc - HNS3_TX_RS_FREE_THRESH_GAP) {
2873 hns3_err(hw, "tx_rs_thresh (%u) tx_free_thresh (%u) nb_desc "
2874 "(%u) of tx descriptors for port=%u queue=%u check "
2876 rs_thresh, free_thresh, nb_desc, hw->data->port_id,
2881 if (conf->tx_free_thresh == 0) {
2882 /* Fast free Tx memory buffer to improve cache hit rate */
2883 fast_free_thresh = nb_desc - rs_thresh;
2884 if (fast_free_thresh >=
2885 HNS3_TX_FAST_FREE_AHEAD + HNS3_DEFAULT_TX_FREE_THRESH)
2886 free_thresh = fast_free_thresh -
2887 HNS3_TX_FAST_FREE_AHEAD;
2890 *tx_rs_thresh = rs_thresh;
2891 *tx_free_thresh = free_thresh;
2896 hns3_tx_queue_setup(struct rte_eth_dev *dev, uint16_t idx, uint16_t nb_desc,
2897 unsigned int socket_id, const struct rte_eth_txconf *conf)
2899 struct hns3_adapter *hns = dev->data->dev_private;
2900 uint16_t tx_rs_thresh, tx_free_thresh;
2901 struct hns3_hw *hw = &hns->hw;
2902 struct hns3_queue_info q_info;
2903 struct hns3_tx_queue *txq;
2907 ret = hns3_tx_queue_conf_check(hw, conf, nb_desc,
2908 &tx_rs_thresh, &tx_free_thresh, idx);
2912 if (dev->data->tx_queues[idx] != NULL) {
2913 hns3_tx_queue_release(dev->data->tx_queues[idx]);
2914 dev->data->tx_queues[idx] = NULL;
2918 q_info.socket_id = socket_id;
2919 q_info.nb_desc = nb_desc;
2920 q_info.type = "hns3 TX queue";
2921 q_info.ring_name = "tx_ring";
2922 txq = hns3_alloc_txq_and_dma_zone(dev, &q_info);
2925 "Failed to alloc mem and reserve DMA mem for tx ring!");
2929 txq->tx_deferred_start = conf->tx_deferred_start;
2930 if (txq->tx_deferred_start && !hns3_dev_indep_txrx_supported(hw)) {
2931 hns3_warn(hw, "deferred start is not supported.");
2932 txq->tx_deferred_start = false;
2935 tx_entry_len = sizeof(struct hns3_entry) * txq->nb_tx_desc;
2936 txq->sw_ring = rte_zmalloc_socket("hns3 TX sw ring", tx_entry_len,
2937 RTE_CACHE_LINE_SIZE, socket_id);
2938 if (txq->sw_ring == NULL) {
2939 hns3_err(hw, "Failed to allocate memory for tx sw ring!");
2940 hns3_tx_queue_release(txq);
2945 txq->next_to_use = 0;
2946 txq->next_to_clean = 0;
2947 txq->tx_bd_ready = txq->nb_tx_desc - 1;
2948 txq->tx_free_thresh = tx_free_thresh;
2949 txq->tx_rs_thresh = tx_rs_thresh;
2950 txq->free = rte_zmalloc_socket("hns3 TX mbuf free array",
2951 sizeof(struct rte_mbuf *) * txq->tx_rs_thresh,
2952 RTE_CACHE_LINE_SIZE, socket_id);
2954 hns3_err(hw, "failed to allocate tx mbuf free array!");
2955 hns3_tx_queue_release(txq);
2959 txq->port_id = dev->data->port_id;
2961 * For hns3 PF device, if the VLAN mode is HW_SHIFT_AND_DISCARD_MODE,
2962 * the pvid_sw_shift_en in the queue struct should not be changed,
2963 * because PVID-related operations do not need to be processed by PMD
2964 * driver. For hns3 VF device, whether it needs to process PVID depends
2965 * on the configuration of PF kernel mode netdev driver. And the
2966 * related PF configuration is delivered through the mailbox and finally
2967 * reflectd in port_base_vlan_cfg.
2969 if (hns->is_vf || hw->vlan_mode == HNS3_SW_SHIFT_AND_DISCARD_MODE)
2970 txq->pvid_sw_shift_en = hw->port_base_vlan_cfg.state ==
2971 HNS3_PORT_BASE_VLAN_ENABLE;
2973 txq->pvid_sw_shift_en = false;
2974 txq->max_non_tso_bd_num = hw->max_non_tso_bd_num;
2975 txq->configured = true;
2976 txq->io_base = (void *)((char *)hw->io_base +
2977 hns3_get_tqp_reg_offset(idx));
2978 txq->io_tail_reg = (volatile void *)((char *)txq->io_base +
2979 HNS3_RING_TX_TAIL_REG);
2980 txq->min_tx_pkt_len = hw->min_tx_pkt_len;
2981 txq->tso_mode = hw->tso_mode;
2982 txq->udp_cksum_mode = hw->udp_cksum_mode;
2983 memset(&txq->basic_stats, 0, sizeof(struct hns3_tx_basic_stats));
2984 memset(&txq->dfx_stats, 0, sizeof(struct hns3_tx_dfx_stats));
2986 rte_spinlock_lock(&hw->lock);
2987 dev->data->tx_queues[idx] = txq;
2988 rte_spinlock_unlock(&hw->lock);
2994 hns3_tx_free_useless_buffer(struct hns3_tx_queue *txq)
2996 uint16_t tx_next_clean = txq->next_to_clean;
2997 uint16_t tx_next_use = txq->next_to_use;
2998 uint16_t tx_bd_ready = txq->tx_bd_ready;
2999 uint16_t tx_bd_max = txq->nb_tx_desc;
3000 struct hns3_entry *tx_bak_pkt = &txq->sw_ring[tx_next_clean];
3001 struct hns3_desc *desc = &txq->tx_ring[tx_next_clean];
3002 struct rte_mbuf *mbuf;
3004 while ((!(desc->tx.tp_fe_sc_vld_ra_ri &
3005 rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B)))) &&
3006 tx_next_use != tx_next_clean) {
3007 mbuf = tx_bak_pkt->mbuf;
3009 rte_pktmbuf_free_seg(mbuf);
3010 tx_bak_pkt->mbuf = NULL;
3018 if (tx_next_clean >= tx_bd_max) {
3020 desc = txq->tx_ring;
3021 tx_bak_pkt = txq->sw_ring;
3025 txq->next_to_clean = tx_next_clean;
3026 txq->tx_bd_ready = tx_bd_ready;
3030 hns3_config_gro(struct hns3_hw *hw, bool en)
3032 struct hns3_cfg_gro_status_cmd *req;
3033 struct hns3_cmd_desc desc;
3036 hns3_cmd_setup_basic_desc(&desc, HNS3_OPC_GRO_GENERIC_CONFIG, false);
3037 req = (struct hns3_cfg_gro_status_cmd *)desc.data;
3039 req->gro_en = rte_cpu_to_le_16(en ? 1 : 0);
3041 ret = hns3_cmd_send(hw, &desc, 1);
3043 hns3_err(hw, "%s hardware GRO failed, ret = %d",
3044 en ? "enable" : "disable", ret);
3050 hns3_restore_gro_conf(struct hns3_hw *hw)
3056 offloads = hw->data->dev_conf.rxmode.offloads;
3057 gro_en = offloads & DEV_RX_OFFLOAD_TCP_LRO ? true : false;
3058 ret = hns3_config_gro(hw, gro_en);
3060 hns3_err(hw, "restore hardware GRO to %s failed, ret = %d",
3061 gro_en ? "enabled" : "disabled", ret);
3067 hns3_pkt_is_tso(struct rte_mbuf *m)
3069 return (m->tso_segsz != 0 && m->ol_flags & PKT_TX_TCP_SEG);
3073 hns3_set_tso(struct hns3_desc *desc, uint32_t paylen, struct rte_mbuf *rxm)
3075 if (!hns3_pkt_is_tso(rxm))
3078 if (paylen <= rxm->tso_segsz)
3081 desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(BIT(HNS3_TXD_TSO_B));
3082 desc->tx.mss = rte_cpu_to_le_16(rxm->tso_segsz);
3086 hns3_fill_per_desc(struct hns3_desc *desc, struct rte_mbuf *rxm)
3088 desc->addr = rte_mbuf_data_iova(rxm);
3089 desc->tx.send_size = rte_cpu_to_le_16(rte_pktmbuf_data_len(rxm));
3090 desc->tx.tp_fe_sc_vld_ra_ri |= rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B));
3094 hns3_fill_first_desc(struct hns3_tx_queue *txq, struct hns3_desc *desc,
3095 struct rte_mbuf *rxm)
3097 uint64_t ol_flags = rxm->ol_flags;
3101 hdr_len = rxm->l2_len + rxm->l3_len + rxm->l4_len;
3102 hdr_len += (ol_flags & PKT_TX_TUNNEL_MASK) ?
3103 rxm->outer_l2_len + rxm->outer_l3_len : 0;
3104 paylen = rxm->pkt_len - hdr_len;
3105 desc->tx.paylen_fd_dop_ol4cs |= rte_cpu_to_le_32(paylen);
3106 hns3_set_tso(desc, paylen, rxm);
3109 * Currently, hardware doesn't support more than two layers VLAN offload
3110 * in Tx direction based on hns3 network engine. So when the number of
3111 * VLANs in the packets represented by rxm plus the number of VLAN
3112 * offload by hardware such as PVID etc, exceeds two, the packets will
3113 * be discarded or the original VLAN of the packets will be overwitted
3114 * by hardware. When the PF PVID is enabled by calling the API function
3115 * named rte_eth_dev_set_vlan_pvid or the VF PVID is enabled by the hns3
3116 * PF kernel ether driver, the outer VLAN tag will always be the PVID.
3117 * To avoid the VLAN of Tx descriptor is overwritten by PVID, it should
3118 * be added to the position close to the IP header when PVID is enabled.
3120 if (!txq->pvid_sw_shift_en && ol_flags & (PKT_TX_VLAN_PKT |
3122 desc->tx.ol_type_vlan_len_msec |=
3123 rte_cpu_to_le_32(BIT(HNS3_TXD_OVLAN_B));
3124 if (ol_flags & PKT_TX_QINQ_PKT)
3125 desc->tx.outer_vlan_tag =
3126 rte_cpu_to_le_16(rxm->vlan_tci_outer);
3128 desc->tx.outer_vlan_tag =
3129 rte_cpu_to_le_16(rxm->vlan_tci);
3132 if (ol_flags & PKT_TX_QINQ_PKT ||
3133 ((ol_flags & PKT_TX_VLAN_PKT) && txq->pvid_sw_shift_en)) {
3134 desc->tx.type_cs_vlan_tso_len |=
3135 rte_cpu_to_le_32(BIT(HNS3_TXD_VLAN_B));
3136 desc->tx.vlan_tag = rte_cpu_to_le_16(rxm->vlan_tci);
3139 if (ol_flags & PKT_TX_IEEE1588_TMST)
3140 desc->tx.tp_fe_sc_vld_ra_ri |=
3141 rte_cpu_to_le_16(BIT(HNS3_TXD_TSYN_B));
3145 hns3_tx_alloc_mbufs(struct rte_mempool *mb_pool, uint16_t nb_new_buf,
3146 struct rte_mbuf **alloc_mbuf)
3148 #define MAX_NON_TSO_BD_PER_PKT 18
3149 struct rte_mbuf *pkt_segs[MAX_NON_TSO_BD_PER_PKT];
3152 /* Allocate enough mbufs */
3153 if (rte_mempool_get_bulk(mb_pool, (void **)pkt_segs, nb_new_buf))
3156 for (i = 0; i < nb_new_buf - 1; i++)
3157 pkt_segs[i]->next = pkt_segs[i + 1];
3159 pkt_segs[nb_new_buf - 1]->next = NULL;
3160 pkt_segs[0]->nb_segs = nb_new_buf;
3161 *alloc_mbuf = pkt_segs[0];
3167 hns3_pktmbuf_copy_hdr(struct rte_mbuf *new_pkt, struct rte_mbuf *old_pkt)
3169 new_pkt->ol_flags = old_pkt->ol_flags;
3170 new_pkt->pkt_len = rte_pktmbuf_pkt_len(old_pkt);
3171 new_pkt->outer_l2_len = old_pkt->outer_l2_len;
3172 new_pkt->outer_l3_len = old_pkt->outer_l3_len;
3173 new_pkt->l2_len = old_pkt->l2_len;
3174 new_pkt->l3_len = old_pkt->l3_len;
3175 new_pkt->l4_len = old_pkt->l4_len;
3176 new_pkt->vlan_tci_outer = old_pkt->vlan_tci_outer;
3177 new_pkt->vlan_tci = old_pkt->vlan_tci;
3181 hns3_reassemble_tx_pkts(struct rte_mbuf *tx_pkt, struct rte_mbuf **new_pkt,
3182 uint8_t max_non_tso_bd_num)
3184 struct rte_mempool *mb_pool;
3185 struct rte_mbuf *new_mbuf;
3186 struct rte_mbuf *temp_new;
3187 struct rte_mbuf *temp;
3188 uint16_t last_buf_len;
3189 uint16_t nb_new_buf;
3199 mb_pool = tx_pkt->pool;
3200 buf_size = tx_pkt->buf_len - RTE_PKTMBUF_HEADROOM;
3201 nb_new_buf = (rte_pktmbuf_pkt_len(tx_pkt) - 1) / buf_size + 1;
3202 if (nb_new_buf > max_non_tso_bd_num)
3205 last_buf_len = rte_pktmbuf_pkt_len(tx_pkt) % buf_size;
3206 if (last_buf_len == 0)
3207 last_buf_len = buf_size;
3209 /* Allocate enough mbufs */
3210 ret = hns3_tx_alloc_mbufs(mb_pool, nb_new_buf, &new_mbuf);
3214 /* Copy the original packet content to the new mbufs */
3216 s = rte_pktmbuf_mtod(temp, char *);
3217 len_s = rte_pktmbuf_data_len(temp);
3218 temp_new = new_mbuf;
3219 while (temp != NULL && temp_new != NULL) {
3220 d = rte_pktmbuf_mtod(temp_new, char *);
3221 buf_len = temp_new->next == NULL ? last_buf_len : buf_size;
3225 len = RTE_MIN(len_s, len_d);
3229 len_d = len_d - len;
3230 len_s = len_s - len;
3236 s = rte_pktmbuf_mtod(temp, char *);
3237 len_s = rte_pktmbuf_data_len(temp);
3241 temp_new->data_len = buf_len;
3242 temp_new = temp_new->next;
3244 hns3_pktmbuf_copy_hdr(new_mbuf, tx_pkt);
3246 /* free original mbufs */
3247 rte_pktmbuf_free(tx_pkt);
3249 *new_pkt = new_mbuf;
3255 hns3_parse_outer_params(struct rte_mbuf *m, uint32_t *ol_type_vlan_len_msec)
3257 uint32_t tmp = *ol_type_vlan_len_msec;
3258 uint64_t ol_flags = m->ol_flags;
3260 /* (outer) IP header type */
3261 if (ol_flags & PKT_TX_OUTER_IPV4) {
3262 if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
3263 tmp |= hns3_gen_field_val(HNS3_TXD_OL3T_M,
3264 HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_CSUM);
3266 tmp |= hns3_gen_field_val(HNS3_TXD_OL3T_M,
3267 HNS3_TXD_OL3T_S, HNS3_OL3T_IPV4_NO_CSUM);
3268 } else if (ol_flags & PKT_TX_OUTER_IPV6) {
3269 tmp |= hns3_gen_field_val(HNS3_TXD_OL3T_M, HNS3_TXD_OL3T_S,
3272 /* OL3 header size, defined in 4 bytes */
3273 tmp |= hns3_gen_field_val(HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
3274 m->outer_l3_len >> HNS3_L3_LEN_UNIT);
3275 *ol_type_vlan_len_msec = tmp;
3279 hns3_parse_inner_params(struct rte_mbuf *m, uint32_t *ol_type_vlan_len_msec,
3280 uint32_t *type_cs_vlan_tso_len)
3282 #define HNS3_NVGRE_HLEN 8
3283 uint32_t tmp_outer = *ol_type_vlan_len_msec;
3284 uint32_t tmp_inner = *type_cs_vlan_tso_len;
3285 uint64_t ol_flags = m->ol_flags;
3286 uint16_t inner_l2_len;
3288 switch (ol_flags & PKT_TX_TUNNEL_MASK) {
3289 case PKT_TX_TUNNEL_VXLAN_GPE:
3290 case PKT_TX_TUNNEL_GENEVE:
3291 case PKT_TX_TUNNEL_VXLAN:
3292 /* MAC in UDP tunnelling packet, include VxLAN and GENEVE */
3293 tmp_outer |= hns3_gen_field_val(HNS3_TXD_TUNTYPE_M,
3294 HNS3_TXD_TUNTYPE_S, HNS3_TUN_MAC_IN_UDP);
3296 * The inner l2 length of mbuf is the sum of outer l4 length,
3297 * tunneling header length and inner l2 length for a tunnel
3298 * packect. But in hns3 tx descriptor, the tunneling header
3299 * length is contained in the field of outer L4 length.
3300 * Therefore, driver need to calculate the outer L4 length and
3303 tmp_outer |= hns3_gen_field_val(HNS3_TXD_L4LEN_M,
3305 (uint8_t)RTE_ETHER_VXLAN_HLEN >>
3308 inner_l2_len = m->l2_len - RTE_ETHER_VXLAN_HLEN;
3310 case PKT_TX_TUNNEL_GRE:
3311 tmp_outer |= hns3_gen_field_val(HNS3_TXD_TUNTYPE_M,
3312 HNS3_TXD_TUNTYPE_S, HNS3_TUN_NVGRE);
3314 * For NVGRE tunnel packect, the outer L4 is empty. So only
3315 * fill the NVGRE header length to the outer L4 field.
3317 tmp_outer |= hns3_gen_field_val(HNS3_TXD_L4LEN_M,
3319 (uint8_t)HNS3_NVGRE_HLEN >> HNS3_L4_LEN_UNIT);
3321 inner_l2_len = m->l2_len - HNS3_NVGRE_HLEN;
3324 /* For non UDP / GRE tunneling, drop the tunnel packet */
3328 tmp_inner |= hns3_gen_field_val(HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
3329 inner_l2_len >> HNS3_L2_LEN_UNIT);
3330 /* OL2 header size, defined in 2 bytes */
3331 tmp_outer |= hns3_gen_field_val(HNS3_TXD_L2LEN_M, HNS3_TXD_L2LEN_S,
3332 m->outer_l2_len >> HNS3_L2_LEN_UNIT);
3334 *type_cs_vlan_tso_len = tmp_inner;
3335 *ol_type_vlan_len_msec = tmp_outer;
3341 hns3_parse_tunneling_params(struct hns3_tx_queue *txq, struct rte_mbuf *m,
3342 uint16_t tx_desc_id)
3344 struct hns3_desc *tx_ring = txq->tx_ring;
3345 struct hns3_desc *desc = &tx_ring[tx_desc_id];
3346 uint64_t ol_flags = m->ol_flags;
3347 uint32_t tmp_outer = 0;
3348 uint32_t tmp_inner = 0;
3353 * The tunnel header is contained in the inner L2 header field of the
3354 * mbuf, but for hns3 descriptor, it is contained in the outer L4. So,
3355 * there is a need that switching between them. To avoid multiple
3356 * calculations, the length of the L2 header include the outer and
3357 * inner, will be filled during the parsing of tunnel packects.
3359 if (!(ol_flags & PKT_TX_TUNNEL_MASK)) {
3361 * For non tunnel type the tunnel type id is 0, so no need to
3362 * assign a value to it. Only the inner(normal) L2 header length
3365 tmp_inner |= hns3_gen_field_val(HNS3_TXD_L2LEN_M,
3366 HNS3_TXD_L2LEN_S, m->l2_len >> HNS3_L2_LEN_UNIT);
3369 * If outer csum is not offload, the outer length may be filled
3370 * with 0. And the length of the outer header is added to the
3371 * inner l2_len. It would lead a cksum error. So driver has to
3372 * calculate the header length.
3374 if (unlikely(!(ol_flags &
3375 (PKT_TX_OUTER_IP_CKSUM | PKT_TX_OUTER_UDP_CKSUM)) &&
3376 m->outer_l2_len == 0)) {
3377 struct rte_net_hdr_lens hdr_len;
3378 (void)rte_net_get_ptype(m, &hdr_len,
3379 RTE_PTYPE_L2_MASK | RTE_PTYPE_L3_MASK);
3380 m->outer_l3_len = hdr_len.l3_len;
3381 m->outer_l2_len = hdr_len.l2_len;
3382 m->l2_len = m->l2_len - hdr_len.l2_len - hdr_len.l3_len;
3384 hns3_parse_outer_params(m, &tmp_outer);
3385 ret = hns3_parse_inner_params(m, &tmp_outer, &tmp_inner);
3390 desc->tx.ol_type_vlan_len_msec = rte_cpu_to_le_32(tmp_outer);
3391 desc->tx.type_cs_vlan_tso_len = rte_cpu_to_le_32(tmp_inner);
3392 tmp_ol4cs = ol_flags & PKT_TX_OUTER_UDP_CKSUM ?
3393 BIT(HNS3_TXD_OL4CS_B) : 0;
3394 desc->tx.paylen_fd_dop_ol4cs = rte_cpu_to_le_32(tmp_ol4cs);
3400 hns3_parse_l3_cksum_params(struct rte_mbuf *m, uint32_t *type_cs_vlan_tso_len)
3402 uint64_t ol_flags = m->ol_flags;
3406 tmp = *type_cs_vlan_tso_len;
3407 if (ol_flags & PKT_TX_IPV4)
3408 l3_type = HNS3_L3T_IPV4;
3409 else if (ol_flags & PKT_TX_IPV6)
3410 l3_type = HNS3_L3T_IPV6;
3412 l3_type = HNS3_L3T_NONE;
3414 /* inner(/normal) L3 header size, defined in 4 bytes */
3415 tmp |= hns3_gen_field_val(HNS3_TXD_L3LEN_M, HNS3_TXD_L3LEN_S,
3416 m->l3_len >> HNS3_L3_LEN_UNIT);
3418 tmp |= hns3_gen_field_val(HNS3_TXD_L3T_M, HNS3_TXD_L3T_S, l3_type);
3420 /* Enable L3 checksum offloads */
3421 if (ol_flags & PKT_TX_IP_CKSUM)
3422 tmp |= BIT(HNS3_TXD_L3CS_B);
3423 *type_cs_vlan_tso_len = tmp;
3427 hns3_parse_l4_cksum_params(struct rte_mbuf *m, uint32_t *type_cs_vlan_tso_len)
3429 uint64_t ol_flags = m->ol_flags;
3431 /* Enable L4 checksum offloads */
3432 switch (ol_flags & (PKT_TX_L4_MASK | PKT_TX_TCP_SEG)) {
3433 case PKT_TX_TCP_CKSUM | PKT_TX_TCP_SEG:
3434 case PKT_TX_TCP_CKSUM:
3435 case PKT_TX_TCP_SEG:
3436 tmp = *type_cs_vlan_tso_len;
3437 tmp |= hns3_gen_field_val(HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
3440 case PKT_TX_UDP_CKSUM:
3441 tmp = *type_cs_vlan_tso_len;
3442 tmp |= hns3_gen_field_val(HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
3445 case PKT_TX_SCTP_CKSUM:
3446 tmp = *type_cs_vlan_tso_len;
3447 tmp |= hns3_gen_field_val(HNS3_TXD_L4T_M, HNS3_TXD_L4T_S,
3453 tmp |= BIT(HNS3_TXD_L4CS_B);
3454 tmp |= hns3_gen_field_val(HNS3_TXD_L4LEN_M, HNS3_TXD_L4LEN_S,
3455 m->l4_len >> HNS3_L4_LEN_UNIT);
3456 *type_cs_vlan_tso_len = tmp;
3460 hns3_txd_enable_checksum(struct hns3_tx_queue *txq, struct rte_mbuf *m,
3461 uint16_t tx_desc_id)
3463 struct hns3_desc *tx_ring = txq->tx_ring;
3464 struct hns3_desc *desc = &tx_ring[tx_desc_id];
3467 hns3_parse_l3_cksum_params(m, &value);
3468 hns3_parse_l4_cksum_params(m, &value);
3470 desc->tx.type_cs_vlan_tso_len |= rte_cpu_to_le_32(value);
3474 hns3_pkt_need_linearized(struct rte_mbuf *tx_pkts, uint32_t bd_num,
3475 uint32_t max_non_tso_bd_num)
3477 struct rte_mbuf *m_first = tx_pkts;
3478 struct rte_mbuf *m_last = tx_pkts;
3479 uint32_t tot_len = 0;
3484 * Hardware requires that the sum of the data length of every 8
3485 * consecutive buffers is greater than MSS in hns3 network engine.
3486 * We simplify it by ensuring pkt_headlen + the first 8 consecutive
3487 * frags greater than gso header len + mss, and the remaining 7
3488 * consecutive frags greater than MSS except the last 7 frags.
3490 if (bd_num <= max_non_tso_bd_num)
3493 for (i = 0; m_last && i < max_non_tso_bd_num - 1;
3494 i++, m_last = m_last->next)
3495 tot_len += m_last->data_len;
3500 /* ensure the first 8 frags is greater than mss + header */
3501 hdr_len = tx_pkts->l2_len + tx_pkts->l3_len + tx_pkts->l4_len;
3502 hdr_len += (tx_pkts->ol_flags & PKT_TX_TUNNEL_MASK) ?
3503 tx_pkts->outer_l2_len + tx_pkts->outer_l3_len : 0;
3504 if (tot_len + m_last->data_len < tx_pkts->tso_segsz + hdr_len)
3508 * ensure the sum of the data length of every 7 consecutive buffer
3509 * is greater than mss except the last one.
3511 for (i = 0; m_last && i < bd_num - max_non_tso_bd_num; i++) {
3512 tot_len -= m_first->data_len;
3513 tot_len += m_last->data_len;
3515 if (tot_len < tx_pkts->tso_segsz)
3518 m_first = m_first->next;
3519 m_last = m_last->next;
3526 hns3_outer_ipv4_cksum_prepared(struct rte_mbuf *m, uint64_t ol_flags,
3529 struct rte_ipv4_hdr *ipv4_hdr;
3530 ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv4_hdr *,
3532 if (ol_flags & PKT_TX_OUTER_IP_CKSUM)
3533 ipv4_hdr->hdr_checksum = 0;
3534 if (ol_flags & PKT_TX_OUTER_UDP_CKSUM) {
3535 struct rte_udp_hdr *udp_hdr;
3537 * If OUTER_UDP_CKSUM is support, HW can caclulate the pseudo
3538 * header for TSO packets
3540 if (ol_flags & PKT_TX_TCP_SEG)
3542 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
3543 m->outer_l2_len + m->outer_l3_len);
3544 udp_hdr->dgram_cksum = rte_ipv4_phdr_cksum(ipv4_hdr, ol_flags);
3548 *l4_proto = ipv4_hdr->next_proto_id;
3553 hns3_outer_ipv6_cksum_prepared(struct rte_mbuf *m, uint64_t ol_flags,
3556 struct rte_ipv6_hdr *ipv6_hdr;
3557 ipv6_hdr = rte_pktmbuf_mtod_offset(m, struct rte_ipv6_hdr *,
3559 if (ol_flags & PKT_TX_OUTER_UDP_CKSUM) {
3560 struct rte_udp_hdr *udp_hdr;
3562 * If OUTER_UDP_CKSUM is support, HW can caclulate the pseudo
3563 * header for TSO packets
3565 if (ol_flags & PKT_TX_TCP_SEG)
3567 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
3568 m->outer_l2_len + m->outer_l3_len);
3569 udp_hdr->dgram_cksum = rte_ipv6_phdr_cksum(ipv6_hdr, ol_flags);
3573 *l4_proto = ipv6_hdr->proto;
3578 hns3_outer_header_cksum_prepare(struct rte_mbuf *m)
3580 uint64_t ol_flags = m->ol_flags;
3581 uint32_t paylen, hdr_len, l4_proto;
3582 struct rte_udp_hdr *udp_hdr;
3584 if (!(ol_flags & (PKT_TX_OUTER_IPV4 | PKT_TX_OUTER_IPV6)))
3587 if (ol_flags & PKT_TX_OUTER_IPV4) {
3588 if (hns3_outer_ipv4_cksum_prepared(m, ol_flags, &l4_proto))
3591 if (hns3_outer_ipv6_cksum_prepared(m, ol_flags, &l4_proto))
3595 /* driver should ensure the outer udp cksum is 0 for TUNNEL TSO */
3596 if (l4_proto == IPPROTO_UDP && (ol_flags & PKT_TX_TCP_SEG)) {
3597 hdr_len = m->l2_len + m->l3_len + m->l4_len;
3598 hdr_len += m->outer_l2_len + m->outer_l3_len;
3599 paylen = m->pkt_len - hdr_len;
3600 if (paylen <= m->tso_segsz)
3602 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
3605 udp_hdr->dgram_cksum = 0;
3610 hns3_check_tso_pkt_valid(struct rte_mbuf *m)
3612 uint32_t tmp_data_len_sum = 0;
3613 uint16_t nb_buf = m->nb_segs;
3614 uint32_t paylen, hdr_len;
3615 struct rte_mbuf *m_seg;
3618 if (nb_buf > HNS3_MAX_TSO_BD_PER_PKT)
3621 hdr_len = m->l2_len + m->l3_len + m->l4_len;
3622 hdr_len += (m->ol_flags & PKT_TX_TUNNEL_MASK) ?
3623 m->outer_l2_len + m->outer_l3_len : 0;
3624 if (hdr_len > HNS3_MAX_TSO_HDR_SIZE)
3627 paylen = m->pkt_len - hdr_len;
3628 if (paylen > HNS3_MAX_BD_PAYLEN)
3632 * The TSO header (include outer and inner L2, L3 and L4 header)
3633 * should be provided by three descriptors in maximum in hns3 network
3637 for (i = 0; m_seg != NULL && i < HNS3_MAX_TSO_HDR_BD_NUM && i < nb_buf;
3638 i++, m_seg = m_seg->next) {
3639 tmp_data_len_sum += m_seg->data_len;
3642 if (hdr_len > tmp_data_len_sum)
3648 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
3650 hns3_vld_vlan_chk(struct hns3_tx_queue *txq, struct rte_mbuf *m)
3652 struct rte_ether_hdr *eh;
3653 struct rte_vlan_hdr *vh;
3655 if (!txq->pvid_sw_shift_en)
3659 * Due to hardware limitations, we only support two-layer VLAN hardware
3660 * offload in Tx direction based on hns3 network engine, so when PVID is
3661 * enabled, QinQ insert is no longer supported.
3662 * And when PVID is enabled, in the following two cases:
3663 * i) packets with more than two VLAN tags.
3664 * ii) packets with one VLAN tag while the hardware VLAN insert is
3666 * The packets will be regarded as abnormal packets and discarded by
3667 * hardware in Tx direction. For debugging purposes, a validation check
3668 * for these types of packets is added to the '.tx_pkt_prepare' ops
3669 * implementation function named hns3_prep_pkts to inform users that
3670 * these packets will be discarded.
3672 if (m->ol_flags & PKT_TX_QINQ_PKT)
3675 eh = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
3676 if (eh->ether_type == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN)) {
3677 if (m->ol_flags & PKT_TX_VLAN_PKT)
3680 /* Ensure the incoming packet is not a QinQ packet */
3681 vh = (struct rte_vlan_hdr *)(eh + 1);
3682 if (vh->eth_proto == rte_cpu_to_be_16(RTE_ETHER_TYPE_VLAN))
3691 hns3_udp_cksum_help(struct rte_mbuf *m)
3693 uint64_t ol_flags = m->ol_flags;
3697 if (ol_flags & PKT_TX_IPV4) {
3698 struct rte_ipv4_hdr *ipv4_hdr = rte_pktmbuf_mtod_offset(m,
3699 struct rte_ipv4_hdr *, m->l2_len);
3700 l4_len = rte_be_to_cpu_16(ipv4_hdr->total_length) - m->l3_len;
3702 struct rte_ipv6_hdr *ipv6_hdr = rte_pktmbuf_mtod_offset(m,
3703 struct rte_ipv6_hdr *, m->l2_len);
3704 l4_len = rte_be_to_cpu_16(ipv6_hdr->payload_len);
3707 rte_raw_cksum_mbuf(m, m->l2_len + m->l3_len, l4_len, &cksum);
3711 * RFC 768:If the computed checksum is zero for UDP, it is transmitted
3717 return (uint16_t)cksum;
3721 hns3_validate_tunnel_cksum(struct hns3_tx_queue *tx_queue, struct rte_mbuf *m)
3723 uint64_t ol_flags = m->ol_flags;
3724 struct rte_udp_hdr *udp_hdr;
3727 if (tx_queue->udp_cksum_mode == HNS3_SPECIAL_PORT_HW_CKSUM_MODE ||
3728 ol_flags & PKT_TX_TUNNEL_MASK ||
3729 (ol_flags & PKT_TX_L4_MASK) != PKT_TX_UDP_CKSUM)
3732 * A UDP packet with the same dst_port as VXLAN\VXLAN_GPE\GENEVE will
3733 * be recognized as a tunnel packet in HW. In this case, if UDP CKSUM
3734 * offload is set and the tunnel mask has not been set, the CKSUM will
3735 * be wrong since the header length is wrong and driver should complete
3736 * the CKSUM to avoid CKSUM error.
3738 udp_hdr = rte_pktmbuf_mtod_offset(m, struct rte_udp_hdr *,
3739 m->l2_len + m->l3_len);
3740 dst_port = rte_be_to_cpu_16(udp_hdr->dst_port);
3742 case RTE_VXLAN_DEFAULT_PORT:
3743 case RTE_VXLAN_GPE_DEFAULT_PORT:
3744 case RTE_GENEVE_DEFAULT_PORT:
3745 udp_hdr->dgram_cksum = hns3_udp_cksum_help(m);
3746 m->ol_flags = ol_flags & ~PKT_TX_L4_MASK;
3754 hns3_prep_pkt_proc(struct hns3_tx_queue *tx_queue, struct rte_mbuf *m)
3758 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
3759 ret = rte_validate_tx_offload(m);
3765 ret = hns3_vld_vlan_chk(tx_queue, m);
3771 if (hns3_pkt_is_tso(m)) {
3772 if (hns3_pkt_need_linearized(m, m->nb_segs,
3773 tx_queue->max_non_tso_bd_num) ||
3774 hns3_check_tso_pkt_valid(m)) {
3779 if (tx_queue->tso_mode != HNS3_TSO_SW_CAL_PSEUDO_H_CSUM) {
3781 * (tso mode != HNS3_TSO_SW_CAL_PSEUDO_H_CSUM) means
3782 * hardware support recalculate the TCP pseudo header
3783 * checksum of packets that need TSO, so network driver
3784 * software not need to recalculate it.
3786 hns3_outer_header_cksum_prepare(m);
3791 ret = rte_net_intel_cksum_prepare(m);
3797 if (!hns3_validate_tunnel_cksum(tx_queue, m))
3800 hns3_outer_header_cksum_prepare(m);
3806 hns3_prep_pkts(__rte_unused void *tx_queue, struct rte_mbuf **tx_pkts,
3812 for (i = 0; i < nb_pkts; i++) {
3814 if (hns3_prep_pkt_proc(tx_queue, m))
3822 hns3_parse_cksum(struct hns3_tx_queue *txq, uint16_t tx_desc_id,
3825 struct hns3_desc *tx_ring = txq->tx_ring;
3826 struct hns3_desc *desc = &tx_ring[tx_desc_id];
3828 /* Enable checksum offloading */
3829 if (m->ol_flags & HNS3_TX_CKSUM_OFFLOAD_MASK) {
3830 /* Fill in tunneling parameters if necessary */
3831 if (hns3_parse_tunneling_params(txq, m, tx_desc_id)) {
3832 txq->dfx_stats.unsupported_tunnel_pkt_cnt++;
3836 hns3_txd_enable_checksum(txq, m, tx_desc_id);
3838 /* clear the control bit */
3839 desc->tx.type_cs_vlan_tso_len = 0;
3840 desc->tx.ol_type_vlan_len_msec = 0;
3847 hns3_check_non_tso_pkt(uint16_t nb_buf, struct rte_mbuf **m_seg,
3848 struct rte_mbuf *tx_pkt, struct hns3_tx_queue *txq)
3850 uint8_t max_non_tso_bd_num;
3851 struct rte_mbuf *new_pkt;
3854 if (hns3_pkt_is_tso(*m_seg))
3858 * If packet length is greater than HNS3_MAX_FRAME_LEN
3859 * driver support, the packet will be ignored.
3861 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) > HNS3_MAX_FRAME_LEN)) {
3862 txq->dfx_stats.over_length_pkt_cnt++;
3866 max_non_tso_bd_num = txq->max_non_tso_bd_num;
3867 if (unlikely(nb_buf > max_non_tso_bd_num)) {
3868 txq->dfx_stats.exceed_limit_bd_pkt_cnt++;
3869 ret = hns3_reassemble_tx_pkts(tx_pkt, &new_pkt,
3870 max_non_tso_bd_num);
3872 txq->dfx_stats.exceed_limit_bd_reassem_fail++;
3882 hns3_tx_free_buffer_simple(struct hns3_tx_queue *txq)
3884 struct hns3_entry *tx_entry;
3885 struct hns3_desc *desc;
3886 uint16_t tx_next_clean;
3890 if (HNS3_GET_TX_QUEUE_PEND_BD_NUM(txq) < txq->tx_rs_thresh)
3894 * All mbufs can be released only when the VLD bits of all
3895 * descriptors in a batch are cleared.
3897 tx_next_clean = (txq->next_to_clean + txq->tx_rs_thresh - 1) %
3899 desc = &txq->tx_ring[tx_next_clean];
3900 for (i = 0; i < txq->tx_rs_thresh; i++) {
3901 if (rte_le_to_cpu_16(desc->tx.tp_fe_sc_vld_ra_ri) &
3902 BIT(HNS3_TXD_VLD_B))
3907 tx_entry = &txq->sw_ring[txq->next_to_clean];
3909 for (i = 0; i < txq->tx_rs_thresh; i++)
3910 rte_prefetch0((tx_entry + i)->mbuf);
3911 for (i = 0; i < txq->tx_rs_thresh; i++, tx_entry++) {
3912 rte_mempool_put(tx_entry->mbuf->pool, tx_entry->mbuf);
3913 tx_entry->mbuf = NULL;
3916 txq->next_to_clean = (tx_next_clean + 1) % txq->nb_tx_desc;
3917 txq->tx_bd_ready += txq->tx_rs_thresh;
3922 hns3_tx_backup_1mbuf(struct hns3_entry *tx_entry, struct rte_mbuf **pkts)
3924 tx_entry->mbuf = pkts[0];
3928 hns3_tx_backup_4mbuf(struct hns3_entry *tx_entry, struct rte_mbuf **pkts)
3930 hns3_tx_backup_1mbuf(&tx_entry[0], &pkts[0]);
3931 hns3_tx_backup_1mbuf(&tx_entry[1], &pkts[1]);
3932 hns3_tx_backup_1mbuf(&tx_entry[2], &pkts[2]);
3933 hns3_tx_backup_1mbuf(&tx_entry[3], &pkts[3]);
3937 hns3_tx_setup_4bd(struct hns3_desc *txdp, struct rte_mbuf **pkts)
3939 #define PER_LOOP_NUM 4
3940 const uint16_t bd_flag = BIT(HNS3_TXD_VLD_B) | BIT(HNS3_TXD_FE_B);
3944 for (i = 0; i < PER_LOOP_NUM; i++, txdp++, pkts++) {
3945 dma_addr = rte_mbuf_data_iova(*pkts);
3946 txdp->addr = rte_cpu_to_le_64(dma_addr);
3947 txdp->tx.send_size = rte_cpu_to_le_16((*pkts)->data_len);
3948 txdp->tx.paylen_fd_dop_ol4cs = 0;
3949 txdp->tx.type_cs_vlan_tso_len = 0;
3950 txdp->tx.ol_type_vlan_len_msec = 0;
3951 txdp->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(bd_flag);
3956 hns3_tx_setup_1bd(struct hns3_desc *txdp, struct rte_mbuf **pkts)
3958 const uint16_t bd_flag = BIT(HNS3_TXD_VLD_B) | BIT(HNS3_TXD_FE_B);
3961 dma_addr = rte_mbuf_data_iova(*pkts);
3962 txdp->addr = rte_cpu_to_le_64(dma_addr);
3963 txdp->tx.send_size = rte_cpu_to_le_16((*pkts)->data_len);
3964 txdp->tx.paylen_fd_dop_ol4cs = 0;
3965 txdp->tx.type_cs_vlan_tso_len = 0;
3966 txdp->tx.ol_type_vlan_len_msec = 0;
3967 txdp->tx.tp_fe_sc_vld_ra_ri = rte_cpu_to_le_16(bd_flag);
3971 hns3_tx_fill_hw_ring(struct hns3_tx_queue *txq,
3972 struct rte_mbuf **pkts,
3975 #define PER_LOOP_NUM 4
3976 #define PER_LOOP_MASK (PER_LOOP_NUM - 1)
3977 struct hns3_desc *txdp = &txq->tx_ring[txq->next_to_use];
3978 struct hns3_entry *tx_entry = &txq->sw_ring[txq->next_to_use];
3979 const uint32_t mainpart = (nb_pkts & ((uint32_t)~PER_LOOP_MASK));
3980 const uint32_t leftover = (nb_pkts & ((uint32_t)PER_LOOP_MASK));
3983 for (i = 0; i < mainpart; i += PER_LOOP_NUM) {
3984 hns3_tx_backup_4mbuf(tx_entry + i, pkts + i);
3985 hns3_tx_setup_4bd(txdp + i, pkts + i);
3987 /* Increment bytes counter */
3989 for (j = 0; j < PER_LOOP_NUM; j++)
3990 txq->basic_stats.bytes += pkts[i + j]->pkt_len;
3992 if (unlikely(leftover > 0)) {
3993 for (i = 0; i < leftover; i++) {
3994 hns3_tx_backup_1mbuf(tx_entry + mainpart + i,
3995 pkts + mainpart + i);
3996 hns3_tx_setup_1bd(txdp + mainpart + i,
3997 pkts + mainpart + i);
3999 /* Increment bytes counter */
4000 txq->basic_stats.bytes += pkts[mainpart + i]->pkt_len;
4006 hns3_xmit_pkts_simple(void *tx_queue,
4007 struct rte_mbuf **tx_pkts,
4010 struct hns3_tx_queue *txq = tx_queue;
4013 hns3_tx_free_buffer_simple(txq);
4015 nb_pkts = RTE_MIN(txq->tx_bd_ready, nb_pkts);
4016 if (unlikely(nb_pkts == 0)) {
4017 if (txq->tx_bd_ready == 0)
4018 txq->dfx_stats.queue_full_cnt++;
4022 txq->tx_bd_ready -= nb_pkts;
4023 if (txq->next_to_use + nb_pkts > txq->nb_tx_desc) {
4024 nb_tx = txq->nb_tx_desc - txq->next_to_use;
4025 hns3_tx_fill_hw_ring(txq, tx_pkts, nb_tx);
4026 txq->next_to_use = 0;
4029 hns3_tx_fill_hw_ring(txq, tx_pkts + nb_tx, nb_pkts - nb_tx);
4030 txq->next_to_use += nb_pkts - nb_tx;
4032 hns3_write_reg_opt(txq->io_tail_reg, nb_pkts);
4038 hns3_xmit_pkts(void *tx_queue, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
4040 struct hns3_tx_queue *txq = tx_queue;
4041 struct hns3_entry *tx_bak_pkt;
4042 struct hns3_desc *tx_ring;
4043 struct rte_mbuf *tx_pkt;
4044 struct rte_mbuf *m_seg;
4045 struct hns3_desc *desc;
4046 uint32_t nb_hold = 0;
4047 uint16_t tx_next_use;
4048 uint16_t tx_pkt_num;
4054 /* free useless buffer */
4055 hns3_tx_free_useless_buffer(txq);
4057 tx_next_use = txq->next_to_use;
4058 tx_bd_max = txq->nb_tx_desc;
4059 tx_pkt_num = nb_pkts;
4060 tx_ring = txq->tx_ring;
4063 tx_bak_pkt = &txq->sw_ring[tx_next_use];
4064 for (nb_tx = 0; nb_tx < tx_pkt_num; nb_tx++) {
4065 tx_pkt = *tx_pkts++;
4067 nb_buf = tx_pkt->nb_segs;
4069 if (nb_buf > txq->tx_bd_ready) {
4070 txq->dfx_stats.queue_full_cnt++;
4078 * If packet length is less than minimum packet length supported
4079 * by hardware in Tx direction, driver need to pad it to avoid
4082 if (unlikely(rte_pktmbuf_pkt_len(tx_pkt) <
4083 txq->min_tx_pkt_len)) {
4087 add_len = txq->min_tx_pkt_len -
4088 rte_pktmbuf_pkt_len(tx_pkt);
4089 appended = rte_pktmbuf_append(tx_pkt, add_len);
4090 if (appended == NULL) {
4091 txq->dfx_stats.pkt_padding_fail_cnt++;
4095 memset(appended, 0, add_len);
4100 if (hns3_check_non_tso_pkt(nb_buf, &m_seg, tx_pkt, txq))
4103 if (hns3_parse_cksum(txq, tx_next_use, m_seg))
4107 desc = &tx_ring[tx_next_use];
4110 * If the packet is divided into multiple Tx Buffer Descriptors,
4111 * only need to fill vlan, paylen and tso into the first Tx
4112 * Buffer Descriptor.
4114 hns3_fill_first_desc(txq, desc, m_seg);
4117 desc = &tx_ring[tx_next_use];
4119 * Fill valid bits, DMA address and data length for each
4120 * Tx Buffer Descriptor.
4122 hns3_fill_per_desc(desc, m_seg);
4123 tx_bak_pkt->mbuf = m_seg;
4124 m_seg = m_seg->next;
4127 if (tx_next_use >= tx_bd_max) {
4129 tx_bak_pkt = txq->sw_ring;
4133 } while (m_seg != NULL);
4135 /* Add end flag for the last Tx Buffer Descriptor */
4136 desc->tx.tp_fe_sc_vld_ra_ri |=
4137 rte_cpu_to_le_16(BIT(HNS3_TXD_FE_B));
4139 /* Increment bytes counter */
4140 txq->basic_stats.bytes += tx_pkt->pkt_len;
4142 txq->next_to_use = tx_next_use;
4143 txq->tx_bd_ready -= i;
4149 hns3_write_reg_opt(txq->io_tail_reg, nb_hold);
4155 hns3_tx_check_vec_support(__rte_unused struct rte_eth_dev *dev)
4161 hns3_xmit_pkts_vec(__rte_unused void *tx_queue,
4162 __rte_unused struct rte_mbuf **tx_pkts,
4163 __rte_unused uint16_t nb_pkts)
4169 hns3_xmit_pkts_vec_sve(void __rte_unused * tx_queue,
4170 struct rte_mbuf __rte_unused **tx_pkts,
4171 uint16_t __rte_unused nb_pkts)
4177 hns3_tx_burst_mode_get(struct rte_eth_dev *dev, __rte_unused uint16_t queue_id,
4178 struct rte_eth_burst_mode *mode)
4180 eth_tx_burst_t pkt_burst = dev->tx_pkt_burst;
4181 const char *info = NULL;
4183 if (pkt_burst == hns3_xmit_pkts_simple)
4184 info = "Scalar Simple";
4185 else if (pkt_burst == hns3_xmit_pkts)
4187 else if (pkt_burst == hns3_xmit_pkts_vec)
4188 info = "Vector Neon";
4189 else if (pkt_burst == hns3_xmit_pkts_vec_sve)
4190 info = "Vector Sve";
4195 snprintf(mode->info, sizeof(mode->info), "%s", info);
4201 hns3_tx_check_simple_support(struct rte_eth_dev *dev)
4203 uint64_t offloads = dev->data->dev_conf.txmode.offloads;
4205 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4206 if (hns3_dev_ptp_supported(hw))
4209 return (offloads == (offloads & DEV_TX_OFFLOAD_MBUF_FAST_FREE));
4213 hns3_get_tx_prep_needed(struct rte_eth_dev *dev)
4215 #ifdef RTE_LIBRTE_ETHDEV_DEBUG
4217 /* always perform tx_prepare when debug */
4220 #define HNS3_DEV_TX_CSKUM_TSO_OFFLOAD_MASK (\
4221 DEV_TX_OFFLOAD_IPV4_CKSUM | \
4222 DEV_TX_OFFLOAD_TCP_CKSUM | \
4223 DEV_TX_OFFLOAD_UDP_CKSUM | \
4224 DEV_TX_OFFLOAD_SCTP_CKSUM | \
4225 DEV_TX_OFFLOAD_OUTER_IPV4_CKSUM | \
4226 DEV_TX_OFFLOAD_OUTER_UDP_CKSUM | \
4227 DEV_TX_OFFLOAD_TCP_TSO | \
4228 DEV_TX_OFFLOAD_VXLAN_TNL_TSO | \
4229 DEV_TX_OFFLOAD_GRE_TNL_TSO | \
4230 DEV_TX_OFFLOAD_GENEVE_TNL_TSO)
4232 uint64_t tx_offload = dev->data->dev_conf.txmode.offloads;
4233 if (tx_offload & HNS3_DEV_TX_CSKUM_TSO_OFFLOAD_MASK)
4240 static eth_tx_burst_t
4241 hns3_get_tx_function(struct rte_eth_dev *dev, eth_tx_prep_t *prep)
4243 struct hns3_adapter *hns = dev->data->dev_private;
4244 bool vec_allowed, sve_allowed, simple_allowed;
4245 bool vec_support, tx_prepare_needed;
4247 vec_support = hns3_tx_check_vec_support(dev) == 0;
4248 vec_allowed = vec_support && hns3_get_default_vec_support();
4249 sve_allowed = vec_support && hns3_get_sve_support();
4250 simple_allowed = hns3_tx_check_simple_support(dev);
4251 tx_prepare_needed = hns3_get_tx_prep_needed(dev);
4255 if (hns->tx_func_hint == HNS3_IO_FUNC_HINT_VEC && vec_allowed)
4256 return hns3_xmit_pkts_vec;
4257 if (hns->tx_func_hint == HNS3_IO_FUNC_HINT_SVE && sve_allowed)
4258 return hns3_xmit_pkts_vec_sve;
4259 if (hns->tx_func_hint == HNS3_IO_FUNC_HINT_SIMPLE && simple_allowed)
4260 return hns3_xmit_pkts_simple;
4261 if (hns->tx_func_hint == HNS3_IO_FUNC_HINT_COMMON) {
4262 if (tx_prepare_needed)
4263 *prep = hns3_prep_pkts;
4264 return hns3_xmit_pkts;
4268 return hns3_xmit_pkts_vec;
4270 return hns3_xmit_pkts_simple;
4272 if (tx_prepare_needed)
4273 *prep = hns3_prep_pkts;
4274 return hns3_xmit_pkts;
4278 hns3_dummy_rxtx_burst(void *dpdk_txq __rte_unused,
4279 struct rte_mbuf **pkts __rte_unused,
4280 uint16_t pkts_n __rte_unused)
4286 hns3_trace_rxtx_function(struct rte_eth_dev *dev)
4288 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4289 struct rte_eth_burst_mode rx_mode;
4290 struct rte_eth_burst_mode tx_mode;
4292 memset(&rx_mode, 0, sizeof(rx_mode));
4293 memset(&tx_mode, 0, sizeof(tx_mode));
4294 (void)hns3_rx_burst_mode_get(dev, 0, &rx_mode);
4295 (void)hns3_tx_burst_mode_get(dev, 0, &tx_mode);
4297 hns3_dbg(hw, "using rx_pkt_burst: %s, tx_pkt_burst: %s.",
4298 rx_mode.info, tx_mode.info);
4301 void hns3_set_rxtx_function(struct rte_eth_dev *eth_dev)
4303 struct hns3_adapter *hns = eth_dev->data->dev_private;
4304 eth_tx_prep_t prep = NULL;
4306 if (hns->hw.adapter_state == HNS3_NIC_STARTED &&
4307 __atomic_load_n(&hns->hw.reset.resetting, __ATOMIC_RELAXED) == 0) {
4308 eth_dev->rx_pkt_burst = hns3_get_rx_function(eth_dev);
4309 eth_dev->rx_descriptor_status = hns3_dev_rx_descriptor_status;
4310 eth_dev->tx_pkt_burst = hns3_get_tx_function(eth_dev, &prep);
4311 eth_dev->tx_pkt_prepare = prep;
4312 eth_dev->tx_descriptor_status = hns3_dev_tx_descriptor_status;
4313 hns3_trace_rxtx_function(eth_dev);
4315 eth_dev->rx_pkt_burst = hns3_dummy_rxtx_burst;
4316 eth_dev->tx_pkt_burst = hns3_dummy_rxtx_burst;
4317 eth_dev->tx_pkt_prepare = hns3_dummy_rxtx_burst;
4322 hns3_rxq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
4323 struct rte_eth_rxq_info *qinfo)
4325 struct hns3_rx_queue *rxq = dev->data->rx_queues[queue_id];
4327 qinfo->mp = rxq->mb_pool;
4328 qinfo->nb_desc = rxq->nb_rx_desc;
4329 qinfo->scattered_rx = dev->data->scattered_rx;
4330 /* Report the HW Rx buffer length to user */
4331 qinfo->rx_buf_size = rxq->rx_buf_len;
4334 * If there are no available Rx buffer descriptors, incoming packets
4335 * are always dropped by hardware based on hns3 network engine.
4337 qinfo->conf.rx_drop_en = 1;
4338 qinfo->conf.offloads = dev->data->dev_conf.rxmode.offloads;
4339 qinfo->conf.rx_free_thresh = rxq->rx_free_thresh;
4340 qinfo->conf.rx_deferred_start = rxq->rx_deferred_start;
4344 hns3_txq_info_get(struct rte_eth_dev *dev, uint16_t queue_id,
4345 struct rte_eth_txq_info *qinfo)
4347 struct hns3_tx_queue *txq = dev->data->tx_queues[queue_id];
4349 qinfo->nb_desc = txq->nb_tx_desc;
4350 qinfo->conf.offloads = dev->data->dev_conf.txmode.offloads;
4351 qinfo->conf.tx_rs_thresh = txq->tx_rs_thresh;
4352 qinfo->conf.tx_free_thresh = txq->tx_free_thresh;
4353 qinfo->conf.tx_deferred_start = txq->tx_deferred_start;
4357 hns3_dev_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
4359 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4360 struct hns3_rx_queue *rxq = dev->data->rx_queues[rx_queue_id];
4361 struct hns3_adapter *hns = HNS3_DEV_HW_TO_ADAPTER(hw);
4364 if (!hns3_dev_indep_txrx_supported(hw))
4367 rte_spinlock_lock(&hw->lock);
4368 ret = hns3_reset_queue(hw, rx_queue_id, HNS3_RING_TYPE_RX);
4370 hns3_err(hw, "fail to reset Rx queue %u, ret = %d.",
4372 rte_spinlock_unlock(&hw->lock);
4376 ret = hns3_init_rxq(hns, rx_queue_id);
4378 hns3_err(hw, "fail to init Rx queue %u, ret = %d.",
4380 rte_spinlock_unlock(&hw->lock);
4384 hns3_enable_rxq(rxq, true);
4385 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
4386 rte_spinlock_unlock(&hw->lock);
4392 hns3_reset_sw_rxq(struct hns3_rx_queue *rxq)
4394 rxq->next_to_use = 0;
4395 rxq->rx_rearm_start = 0;
4396 rxq->rx_free_hold = 0;
4397 rxq->rx_rearm_nb = 0;
4398 rxq->pkt_first_seg = NULL;
4399 rxq->pkt_last_seg = NULL;
4400 memset(&rxq->rx_ring[0], 0, rxq->nb_rx_desc * sizeof(struct hns3_desc));
4401 hns3_rxq_vec_setup(rxq);
4405 hns3_dev_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
4407 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4408 struct hns3_rx_queue *rxq = dev->data->rx_queues[rx_queue_id];
4410 if (!hns3_dev_indep_txrx_supported(hw))
4413 rte_spinlock_lock(&hw->lock);
4414 hns3_enable_rxq(rxq, false);
4416 hns3_rx_queue_release_mbufs(rxq);
4418 hns3_reset_sw_rxq(rxq);
4419 dev->data->rx_queue_state[rx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
4420 rte_spinlock_unlock(&hw->lock);
4426 hns3_dev_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
4428 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4429 struct hns3_tx_queue *txq = dev->data->tx_queues[tx_queue_id];
4432 if (!hns3_dev_indep_txrx_supported(hw))
4435 rte_spinlock_lock(&hw->lock);
4436 ret = hns3_reset_queue(hw, tx_queue_id, HNS3_RING_TYPE_TX);
4438 hns3_err(hw, "fail to reset Tx queue %u, ret = %d.",
4440 rte_spinlock_unlock(&hw->lock);
4445 hns3_enable_txq(txq, true);
4446 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STARTED;
4447 rte_spinlock_unlock(&hw->lock);
4453 hns3_dev_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
4455 struct hns3_hw *hw = HNS3_DEV_PRIVATE_TO_HW(dev->data->dev_private);
4456 struct hns3_tx_queue *txq = dev->data->tx_queues[tx_queue_id];
4458 if (!hns3_dev_indep_txrx_supported(hw))
4461 rte_spinlock_lock(&hw->lock);
4462 hns3_enable_txq(txq, false);
4463 hns3_tx_queue_release_mbufs(txq);
4465 * All the mbufs in sw_ring are released and all the pointers in sw_ring
4466 * are set to NULL. If this queue is still called by upper layer,
4467 * residual SW status of this txq may cause these pointers in sw_ring
4468 * which have been set to NULL to be released again. To avoid it,
4472 dev->data->tx_queue_state[tx_queue_id] = RTE_ETH_QUEUE_STATE_STOPPED;
4473 rte_spinlock_unlock(&hw->lock);
4479 hns3_tx_done_cleanup_full(struct hns3_tx_queue *txq, uint32_t free_cnt)
4481 uint16_t next_to_clean = txq->next_to_clean;
4482 uint16_t next_to_use = txq->next_to_use;
4483 uint16_t tx_bd_ready = txq->tx_bd_ready;
4484 struct hns3_entry *tx_pkt = &txq->sw_ring[next_to_clean];
4485 struct hns3_desc *desc = &txq->tx_ring[next_to_clean];
4488 if (free_cnt == 0 || free_cnt > txq->nb_tx_desc)
4489 free_cnt = txq->nb_tx_desc;
4491 for (idx = 0; idx < free_cnt; idx++) {
4492 if (next_to_clean == next_to_use)
4495 if (desc->tx.tp_fe_sc_vld_ra_ri &
4496 rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B)))
4499 if (tx_pkt->mbuf != NULL) {
4500 rte_pktmbuf_free_seg(tx_pkt->mbuf);
4501 tx_pkt->mbuf = NULL;
4508 if (next_to_clean == txq->nb_tx_desc) {
4509 tx_pkt = txq->sw_ring;
4510 desc = txq->tx_ring;
4516 txq->next_to_clean = next_to_clean;
4517 txq->tx_bd_ready = tx_bd_ready;
4524 hns3_tx_done_cleanup(void *txq, uint32_t free_cnt)
4526 struct hns3_tx_queue *q = (struct hns3_tx_queue *)txq;
4527 struct rte_eth_dev *dev = &rte_eth_devices[q->port_id];
4529 if (dev->tx_pkt_burst == hns3_xmit_pkts)
4530 return hns3_tx_done_cleanup_full(q, free_cnt);
4531 else if (dev->tx_pkt_burst == hns3_dummy_rxtx_burst)
4538 hns3_dev_rx_descriptor_status(void *rx_queue, uint16_t offset)
4540 volatile struct hns3_desc *rxdp;
4541 struct hns3_rx_queue *rxq;
4542 struct rte_eth_dev *dev;
4543 uint32_t bd_base_info;
4546 rxq = (struct hns3_rx_queue *)rx_queue;
4547 if (offset >= rxq->nb_rx_desc)
4550 desc_id = (rxq->next_to_use + offset) % rxq->nb_rx_desc;
4551 rxdp = &rxq->rx_ring[desc_id];
4552 bd_base_info = rte_le_to_cpu_32(rxdp->rx.bd_base_info);
4553 dev = &rte_eth_devices[rxq->port_id];
4554 if (dev->rx_pkt_burst == hns3_recv_pkts_simple ||
4555 dev->rx_pkt_burst == hns3_recv_scattered_pkts) {
4556 if (offset >= rxq->nb_rx_desc - rxq->rx_free_hold)
4557 return RTE_ETH_RX_DESC_UNAVAIL;
4558 } else if (dev->rx_pkt_burst == hns3_recv_pkts_vec ||
4559 dev->rx_pkt_burst == hns3_recv_pkts_vec_sve) {
4560 if (offset >= rxq->nb_rx_desc - rxq->rx_rearm_nb)
4561 return RTE_ETH_RX_DESC_UNAVAIL;
4563 return RTE_ETH_RX_DESC_UNAVAIL;
4566 if (!(bd_base_info & BIT(HNS3_RXD_VLD_B)))
4567 return RTE_ETH_RX_DESC_AVAIL;
4569 return RTE_ETH_RX_DESC_DONE;
4573 hns3_dev_tx_descriptor_status(void *tx_queue, uint16_t offset)
4575 volatile struct hns3_desc *txdp;
4576 struct hns3_tx_queue *txq;
4577 struct rte_eth_dev *dev;
4580 txq = (struct hns3_tx_queue *)tx_queue;
4581 if (offset >= txq->nb_tx_desc)
4584 dev = &rte_eth_devices[txq->port_id];
4585 if (dev->tx_pkt_burst != hns3_xmit_pkts_simple &&
4586 dev->tx_pkt_burst != hns3_xmit_pkts &&
4587 dev->tx_pkt_burst != hns3_xmit_pkts_vec_sve &&
4588 dev->tx_pkt_burst != hns3_xmit_pkts_vec)
4589 return RTE_ETH_TX_DESC_UNAVAIL;
4591 desc_id = (txq->next_to_use + offset) % txq->nb_tx_desc;
4592 txdp = &txq->tx_ring[desc_id];
4593 if (txdp->tx.tp_fe_sc_vld_ra_ri & rte_cpu_to_le_16(BIT(HNS3_TXD_VLD_B)))
4594 return RTE_ETH_TX_DESC_FULL;
4596 return RTE_ETH_TX_DESC_DONE;
4600 hns3_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
4603 * Number of BDs that have been processed by the driver
4604 * but have not been notified to the hardware.
4606 uint32_t driver_hold_bd_num;
4607 struct hns3_rx_queue *rxq;
4610 rxq = dev->data->rx_queues[rx_queue_id];
4611 fbd_num = hns3_read_dev(rxq, HNS3_RING_RX_FBDNUM_REG);
4612 if (dev->rx_pkt_burst == hns3_recv_pkts_vec ||
4613 dev->rx_pkt_burst == hns3_recv_pkts_vec_sve)
4614 driver_hold_bd_num = rxq->rx_rearm_nb;
4616 driver_hold_bd_num = rxq->rx_free_hold;
4618 if (fbd_num <= driver_hold_bd_num)
4621 return fbd_num - driver_hold_bd_num;
4625 hns3_enable_rxd_adv_layout(struct hns3_hw *hw)
4628 * If the hardware support rxd advanced layout, then driver enable it
4631 if (hns3_dev_rxd_adv_layout_supported(hw))
4632 hns3_write_dev(hw, HNS3_RXD_ADV_LAYOUT_EN_REG, 1);