1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018-2019 NXP
6 #include <rte_ethdev_pci.h>
8 #include "enetc_logs.h"
11 int enetc_logtype_pmd;
14 enetc_dev_configure(struct rte_eth_dev *dev __rte_unused)
16 PMD_INIT_FUNC_TRACE();
21 enetc_dev_start(struct rte_eth_dev *dev)
23 struct enetc_eth_hw *hw =
24 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
25 struct enetc_hw *enetc_hw = &hw->hw;
28 PMD_INIT_FUNC_TRACE();
29 val = enetc_port_rd(enetc_hw, ENETC_PM0_CMD_CFG);
30 enetc_port_wr(enetc_hw, ENETC_PM0_CMD_CFG,
31 val | ENETC_PM0_TX_EN | ENETC_PM0_RX_EN);
34 val = enetc_port_rd(enetc_hw, ENETC_PMR);
35 enetc_port_wr(enetc_hw, ENETC_PMR, val | ENETC_PMR_EN);
37 /* set auto-speed for RGMII */
38 if (enetc_port_rd(enetc_hw, ENETC_PM0_IF_MODE) & ENETC_PMO_IFM_RG) {
39 enetc_port_wr(enetc_hw, ENETC_PM0_IF_MODE,
40 ENETC_PM0_IFM_RGAUTO);
41 enetc_port_wr(enetc_hw, ENETC_PM1_IF_MODE,
42 ENETC_PM0_IFM_RGAUTO);
44 if (enetc_global_rd(enetc_hw,
45 ENETC_G_EPFBLPR(1)) == ENETC_G_EPFBLPR1_XGMII) {
46 enetc_port_wr(enetc_hw, ENETC_PM0_IF_MODE,
48 enetc_port_wr(enetc_hw, ENETC_PM1_IF_MODE,
56 enetc_dev_stop(struct rte_eth_dev *dev)
58 struct enetc_eth_hw *hw =
59 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
60 struct enetc_hw *enetc_hw = &hw->hw;
63 PMD_INIT_FUNC_TRACE();
65 val = enetc_port_rd(enetc_hw, ENETC_PMR);
66 enetc_port_wr(enetc_hw, ENETC_PMR, val & (~ENETC_PMR_EN));
68 val = enetc_port_rd(enetc_hw, ENETC_PM0_CMD_CFG);
69 enetc_port_wr(enetc_hw, ENETC_PM0_CMD_CFG,
70 val & (~(ENETC_PM0_TX_EN | ENETC_PM0_RX_EN)));
73 static const uint32_t *
74 enetc_supported_ptypes_get(struct rte_eth_dev *dev __rte_unused)
76 static const uint32_t ptypes[] = {
90 /* return 0 means link status changed, -1 means not changed */
92 enetc_link_update(struct rte_eth_dev *dev, int wait_to_complete __rte_unused)
94 struct enetc_eth_hw *hw =
95 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
96 struct enetc_hw *enetc_hw = &hw->hw;
97 struct rte_eth_link link;
100 PMD_INIT_FUNC_TRACE();
102 memset(&link, 0, sizeof(link));
104 status = enetc_port_rd(enetc_hw, ENETC_PM0_STATUS);
106 if (status & ENETC_LINK_MODE)
107 link.link_duplex = ETH_LINK_FULL_DUPLEX;
109 link.link_duplex = ETH_LINK_HALF_DUPLEX;
111 if (status & ENETC_LINK_STATUS)
112 link.link_status = ETH_LINK_UP;
114 link.link_status = ETH_LINK_DOWN;
116 switch (status & ENETC_LINK_SPEED_MASK) {
117 case ENETC_LINK_SPEED_1G:
118 link.link_speed = ETH_SPEED_NUM_1G;
121 case ENETC_LINK_SPEED_100M:
122 link.link_speed = ETH_SPEED_NUM_100M;
126 case ENETC_LINK_SPEED_10M:
127 link.link_speed = ETH_SPEED_NUM_10M;
130 return rte_eth_linkstatus_set(dev, &link);
134 enetc_hardware_init(struct enetc_eth_hw *hw)
137 struct enetc_hw *enetc_hw = &hw->hw;
139 PMD_INIT_FUNC_TRACE();
140 /* Calculating and storing the base HW addresses */
141 hw->hw.port = (void *)((size_t)hw->hw.reg + ENETC_PORT_BASE);
142 hw->hw.global = (void *)((size_t)hw->hw.reg + ENETC_GLOBAL_BASE);
144 /* Enabling Station Interface */
145 enetc_wr(enetc_hw, ENETC_SIMR, ENETC_SIMR_EN);
147 /* Setting to accept broadcast packets for each inetrface */
148 psipmr |= ENETC_PSIPMR_SET_UP(0) | ENETC_PSIPMR_SET_MP(0) |
149 ENETC_PSIPMR_SET_VLAN_MP(0);
150 psipmr |= ENETC_PSIPMR_SET_UP(1) | ENETC_PSIPMR_SET_MP(1) |
151 ENETC_PSIPMR_SET_VLAN_MP(1);
152 psipmr |= ENETC_PSIPMR_SET_UP(2) | ENETC_PSIPMR_SET_MP(2) |
153 ENETC_PSIPMR_SET_VLAN_MP(2);
155 enetc_port_wr(enetc_hw, ENETC_PSIPMR, psipmr);
157 /* Enabling broadcast address */
158 enetc_port_wr(enetc_hw, ENETC_PSIPMAR0(0), 0xFFFFFFFF);
159 enetc_port_wr(enetc_hw, ENETC_PSIPMAR1(0), 0xFFFF << 16);
165 enetc_dev_infos_get(struct rte_eth_dev *dev __rte_unused,
166 struct rte_eth_dev_info *dev_info)
168 PMD_INIT_FUNC_TRACE();
169 dev_info->rx_desc_lim = (struct rte_eth_desc_lim) {
170 .nb_max = MAX_BD_COUNT,
171 .nb_min = MIN_BD_COUNT,
172 .nb_align = BD_ALIGN,
174 dev_info->tx_desc_lim = (struct rte_eth_desc_lim) {
175 .nb_max = MAX_BD_COUNT,
176 .nb_min = MIN_BD_COUNT,
177 .nb_align = BD_ALIGN,
179 dev_info->max_rx_queues = MAX_RX_RINGS;
180 dev_info->max_tx_queues = MAX_TX_RINGS;
181 dev_info->max_rx_pktlen = 1500;
185 enetc_alloc_txbdr(struct enetc_bdr *txr, uint16_t nb_desc)
189 size = nb_desc * sizeof(struct enetc_swbd);
190 txr->q_swbd = rte_malloc(NULL, size, RTE_CACHE_LINE_SIZE);
191 if (txr->q_swbd == NULL)
194 size = nb_desc * sizeof(struct enetc_tx_bd);
195 txr->bd_base = rte_malloc(NULL, size, RTE_CACHE_LINE_SIZE);
196 if (txr->bd_base == NULL) {
197 rte_free(txr->q_swbd);
202 txr->bd_count = nb_desc;
203 txr->next_to_clean = 0;
204 txr->next_to_use = 0;
210 enetc_free_bdr(struct enetc_bdr *rxr)
212 rte_free(rxr->q_swbd);
213 rte_free(rxr->bd_base);
219 enetc_setup_txbdr(struct enetc_hw *hw, struct enetc_bdr *tx_ring)
221 int idx = tx_ring->index;
223 phys_addr_t bd_address;
225 bd_address = (phys_addr_t)
226 rte_mem_virt2iova((const void *)tx_ring->bd_base);
227 enetc_txbdr_wr(hw, idx, ENETC_TBBAR0,
228 lower_32_bits((uint64_t)bd_address));
229 enetc_txbdr_wr(hw, idx, ENETC_TBBAR1,
230 upper_32_bits((uint64_t)bd_address));
231 enetc_txbdr_wr(hw, idx, ENETC_TBLENR,
232 ENETC_RTBLENR_LEN(tx_ring->bd_count));
234 tbmr = ENETC_TBMR_EN;
236 enetc_txbdr_wr(hw, idx, ENETC_TBMR, tbmr);
237 enetc_txbdr_wr(hw, idx, ENETC_TBCIR, 0);
238 enetc_txbdr_wr(hw, idx, ENETC_TBCISR, 0);
239 tx_ring->tcir = (void *)((size_t)hw->reg +
240 ENETC_BDR(TX, idx, ENETC_TBCIR));
241 tx_ring->tcisr = (void *)((size_t)hw->reg +
242 ENETC_BDR(TX, idx, ENETC_TBCISR));
246 enetc_alloc_tx_resources(struct rte_eth_dev *dev,
251 struct enetc_bdr *tx_ring;
252 struct rte_eth_dev_data *data = dev->data;
253 struct enetc_eth_adapter *priv =
254 ENETC_DEV_PRIVATE(data->dev_private);
256 tx_ring = rte_zmalloc(NULL, sizeof(struct enetc_bdr), 0);
257 if (tx_ring == NULL) {
258 ENETC_PMD_ERR("Failed to allocate TX ring memory");
263 err = enetc_alloc_txbdr(tx_ring, nb_desc);
267 tx_ring->index = queue_idx;
269 enetc_setup_txbdr(&priv->hw.hw, tx_ring);
270 data->tx_queues[queue_idx] = tx_ring;
280 enetc_tx_queue_setup(struct rte_eth_dev *dev,
283 unsigned int socket_id __rte_unused,
284 const struct rte_eth_txconf *tx_conf __rte_unused)
288 PMD_INIT_FUNC_TRACE();
289 if (nb_desc > MAX_BD_COUNT)
292 err = enetc_alloc_tx_resources(dev, queue_idx, nb_desc);
298 enetc_tx_queue_release(void *txq)
303 struct enetc_bdr *tx_ring = (struct enetc_bdr *)txq;
304 struct enetc_eth_hw *eth_hw =
305 ENETC_DEV_PRIVATE_TO_HW(tx_ring->ndev->data->dev_private);
307 struct enetc_swbd *tx_swbd;
311 /* Disable the ring */
313 val = enetc_txbdr_rd(hw, tx_ring->index, ENETC_TBMR);
314 val &= (~ENETC_TBMR_EN);
315 enetc_txbdr_wr(hw, tx_ring->index, ENETC_TBMR, val);
318 i = tx_ring->next_to_clean;
319 tx_swbd = &tx_ring->q_swbd[i];
320 while (tx_swbd->buffer_addr != NULL) {
321 rte_pktmbuf_free(tx_swbd->buffer_addr);
322 tx_swbd->buffer_addr = NULL;
325 if (unlikely(i == tx_ring->bd_count)) {
327 tx_swbd = &tx_ring->q_swbd[i];
331 enetc_free_bdr(tx_ring);
336 enetc_alloc_rxbdr(struct enetc_bdr *rxr,
341 size = nb_rx_desc * sizeof(struct enetc_swbd);
342 rxr->q_swbd = rte_malloc(NULL, size, RTE_CACHE_LINE_SIZE);
343 if (rxr->q_swbd == NULL)
346 size = nb_rx_desc * sizeof(union enetc_rx_bd);
347 rxr->bd_base = rte_malloc(NULL, size, RTE_CACHE_LINE_SIZE);
348 if (rxr->bd_base == NULL) {
349 rte_free(rxr->q_swbd);
354 rxr->bd_count = nb_rx_desc;
355 rxr->next_to_clean = 0;
356 rxr->next_to_use = 0;
357 rxr->next_to_alloc = 0;
363 enetc_setup_rxbdr(struct enetc_hw *hw, struct enetc_bdr *rx_ring,
364 struct rte_mempool *mb_pool)
366 int idx = rx_ring->index;
368 phys_addr_t bd_address;
370 bd_address = (phys_addr_t)
371 rte_mem_virt2iova((const void *)rx_ring->bd_base);
372 enetc_rxbdr_wr(hw, idx, ENETC_RBBAR0,
373 lower_32_bits((uint64_t)bd_address));
374 enetc_rxbdr_wr(hw, idx, ENETC_RBBAR1,
375 upper_32_bits((uint64_t)bd_address));
376 enetc_rxbdr_wr(hw, idx, ENETC_RBLENR,
377 ENETC_RTBLENR_LEN(rx_ring->bd_count));
379 rx_ring->mb_pool = mb_pool;
380 rx_ring->rcir = (void *)((size_t)hw->reg +
381 ENETC_BDR(RX, idx, ENETC_RBCIR));
382 enetc_refill_rx_ring(rx_ring, (enetc_bd_unused(rx_ring)));
383 buf_size = (uint16_t)(rte_pktmbuf_data_room_size(rx_ring->mb_pool) -
384 RTE_PKTMBUF_HEADROOM);
385 enetc_rxbdr_wr(hw, idx, ENETC_RBBSR, buf_size);
387 enetc_rxbdr_wr(hw, idx, ENETC_RBMR, ENETC_RBMR_EN);
388 enetc_rxbdr_wr(hw, idx, ENETC_RBPIR, 0);
392 enetc_alloc_rx_resources(struct rte_eth_dev *dev,
393 uint16_t rx_queue_id,
395 struct rte_mempool *mb_pool)
398 struct enetc_bdr *rx_ring;
399 struct rte_eth_dev_data *data = dev->data;
400 struct enetc_eth_adapter *adapter =
401 ENETC_DEV_PRIVATE(data->dev_private);
403 rx_ring = rte_zmalloc(NULL, sizeof(struct enetc_bdr), 0);
404 if (rx_ring == NULL) {
405 ENETC_PMD_ERR("Failed to allocate RX ring memory");
410 err = enetc_alloc_rxbdr(rx_ring, nb_rx_desc);
414 rx_ring->index = rx_queue_id;
416 enetc_setup_rxbdr(&adapter->hw.hw, rx_ring, mb_pool);
417 data->rx_queues[rx_queue_id] = rx_ring;
427 enetc_rx_queue_setup(struct rte_eth_dev *dev,
428 uint16_t rx_queue_id,
430 unsigned int socket_id __rte_unused,
431 const struct rte_eth_rxconf *rx_conf __rte_unused,
432 struct rte_mempool *mb_pool)
436 PMD_INIT_FUNC_TRACE();
437 if (nb_rx_desc > MAX_BD_COUNT)
440 err = enetc_alloc_rx_resources(dev, rx_queue_id,
448 enetc_rx_queue_release(void *rxq)
453 struct enetc_bdr *rx_ring = (struct enetc_bdr *)rxq;
454 struct enetc_eth_hw *eth_hw =
455 ENETC_DEV_PRIVATE_TO_HW(rx_ring->ndev->data->dev_private);
456 struct enetc_swbd *q_swbd;
461 /* Disable the ring */
463 val = enetc_rxbdr_rd(hw, rx_ring->index, ENETC_RBMR);
464 val &= (~ENETC_RBMR_EN);
465 enetc_rxbdr_wr(hw, rx_ring->index, ENETC_RBMR, val);
468 i = rx_ring->next_to_clean;
469 q_swbd = &rx_ring->q_swbd[i];
470 while (i != rx_ring->next_to_use) {
471 rte_pktmbuf_free(q_swbd->buffer_addr);
472 q_swbd->buffer_addr = NULL;
475 if (unlikely(i == rx_ring->bd_count)) {
477 q_swbd = &rx_ring->q_swbd[i];
481 enetc_free_bdr(rx_ring);
486 int enetc_stats_get(struct rte_eth_dev *dev,
487 struct rte_eth_stats *stats)
489 struct enetc_eth_hw *hw =
490 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
491 struct enetc_hw *enetc_hw = &hw->hw;
493 /* Total received packets, bad + good, if we want to get counters of
494 * only good received packets then use ENETC_PM0_RFRM,
495 * ENETC_PM0_TFRM registers.
497 stats->ipackets = enetc_port_rd(enetc_hw, ENETC_PM0_RPKT);
498 stats->opackets = enetc_port_rd(enetc_hw, ENETC_PM0_TPKT);
499 stats->ibytes = enetc_port_rd(enetc_hw, ENETC_PM0_REOCT);
500 stats->obytes = enetc_port_rd(enetc_hw, ENETC_PM0_TEOCT);
501 /* Dropped + Truncated packets, use ENETC_PM0_RDRNTP for without
504 stats->imissed = enetc_port_rd(enetc_hw, ENETC_PM0_RDRP);
505 stats->ierrors = enetc_port_rd(enetc_hw, ENETC_PM0_RERR);
506 stats->oerrors = enetc_port_rd(enetc_hw, ENETC_PM0_TERR);
512 enetc_stats_reset(struct rte_eth_dev *dev)
514 struct enetc_eth_hw *hw =
515 ENETC_DEV_PRIVATE_TO_HW(dev->data->dev_private);
516 struct enetc_hw *enetc_hw = &hw->hw;
518 enetc_port_wr(enetc_hw, ENETC_PM0_STAT_CONFIG, ENETC_CLEAR_STATS);
522 enetc_dev_close(struct rte_eth_dev *dev)
526 PMD_INIT_FUNC_TRACE();
529 for (i = 0; i < dev->data->nb_rx_queues; i++) {
530 enetc_rx_queue_release(dev->data->rx_queues[i]);
531 dev->data->rx_queues[i] = NULL;
533 dev->data->nb_rx_queues = 0;
535 for (i = 0; i < dev->data->nb_tx_queues; i++) {
536 enetc_tx_queue_release(dev->data->tx_queues[i]);
537 dev->data->tx_queues[i] = NULL;
539 dev->data->nb_tx_queues = 0;
543 * The set of PCI devices this driver supports
545 static const struct rte_pci_id pci_id_enetc_map[] = {
546 { RTE_PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID) },
547 { RTE_PCI_DEVICE(PCI_VENDOR_ID_FREESCALE, ENETC_DEV_ID_VF) },
548 { .vendor_id = 0, /* sentinel */ },
551 /* Features supported by this driver */
552 static const struct eth_dev_ops enetc_ops = {
553 .dev_configure = enetc_dev_configure,
554 .dev_start = enetc_dev_start,
555 .dev_stop = enetc_dev_stop,
556 .dev_close = enetc_dev_close,
557 .link_update = enetc_link_update,
558 .stats_get = enetc_stats_get,
559 .stats_reset = enetc_stats_reset,
560 .dev_infos_get = enetc_dev_infos_get,
561 .rx_queue_setup = enetc_rx_queue_setup,
562 .rx_queue_release = enetc_rx_queue_release,
563 .tx_queue_setup = enetc_tx_queue_setup,
564 .tx_queue_release = enetc_tx_queue_release,
565 .dev_supported_ptypes_get = enetc_supported_ptypes_get,
569 * Initialisation of the enetc device
572 * - Pointer to the structure rte_eth_dev
575 * - On success, zero.
576 * - On failure, negative value.
579 enetc_dev_init(struct rte_eth_dev *eth_dev)
582 struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
583 struct enetc_eth_hw *hw =
584 ENETC_DEV_PRIVATE_TO_HW(eth_dev->data->dev_private);
586 PMD_INIT_FUNC_TRACE();
587 eth_dev->dev_ops = &enetc_ops;
588 eth_dev->rx_pkt_burst = &enetc_recv_pkts;
589 eth_dev->tx_pkt_burst = &enetc_xmit_pkts;
591 /* Retrieving and storing the HW base address of device */
592 hw->hw.reg = (void *)pci_dev->mem_resource[0].addr;
593 hw->device_id = pci_dev->id.device_id;
595 error = enetc_hardware_init(hw);
597 ENETC_PMD_ERR("Hardware initialization failed");
601 /* Allocate memory for storing MAC addresses */
602 eth_dev->data->mac_addrs = rte_zmalloc("enetc_eth", ETHER_ADDR_LEN, 0);
603 if (!eth_dev->data->mac_addrs) {
604 ENETC_PMD_ERR("Failed to allocate %d bytes needed to "
605 "store MAC addresses",
611 /* Copy the permanent MAC address */
612 ether_addr_copy((struct ether_addr *)hw->mac.addr,
613 ð_dev->data->mac_addrs[0]);
615 ENETC_PMD_DEBUG("port_id %d vendorID=0x%x deviceID=0x%x",
616 eth_dev->data->port_id, pci_dev->id.vendor_id,
617 pci_dev->id.device_id);
622 enetc_dev_uninit(struct rte_eth_dev *eth_dev __rte_unused)
624 PMD_INIT_FUNC_TRACE();
629 enetc_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
630 struct rte_pci_device *pci_dev)
632 return rte_eth_dev_pci_generic_probe(pci_dev,
633 sizeof(struct enetc_eth_adapter),
638 enetc_pci_remove(struct rte_pci_device *pci_dev)
640 return rte_eth_dev_pci_generic_remove(pci_dev, enetc_dev_uninit);
643 static struct rte_pci_driver rte_enetc_pmd = {
644 .id_table = pci_id_enetc_map,
645 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
646 .probe = enetc_pci_probe,
647 .remove = enetc_pci_remove,
650 RTE_PMD_REGISTER_PCI(net_enetc, rte_enetc_pmd);
651 RTE_PMD_REGISTER_PCI_TABLE(net_enetc, pci_id_enetc_map);
652 RTE_PMD_REGISTER_KMOD_DEP(net_enetc, "* vfio-pci");
654 RTE_INIT(enetc_pmd_init_log)
656 enetc_logtype_pmd = rte_log_register("pmd.net.enetc");
657 if (enetc_logtype_pmd >= 0)
658 rte_log_set_level(enetc_logtype_pmd, RTE_LOG_NOTICE);