199a2dd95SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
299a2dd95SBruce Richardson * Copyright(c) 2017 Brocade Communications Systems, Inc.
399a2dd95SBruce Richardson * Author: Jan Blunck <[email protected]>
499a2dd95SBruce Richardson */
599a2dd95SBruce Richardson
699a2dd95SBruce Richardson #ifndef _RTE_ETHDEV_PCI_H_
799a2dd95SBruce Richardson #define _RTE_ETHDEV_PCI_H_
899a2dd95SBruce Richardson
9dbf9fc1dSBrian Dooley #ifdef __cplusplus
10dbf9fc1dSBrian Dooley extern "C" {
11dbf9fc1dSBrian Dooley #endif
12dbf9fc1dSBrian Dooley
1399a2dd95SBruce Richardson #include <rte_malloc.h>
1499a2dd95SBruce Richardson #include <rte_pci.h>
1599a2dd95SBruce Richardson #include <rte_bus_pci.h>
1699a2dd95SBruce Richardson #include <rte_config.h>
1799a2dd95SBruce Richardson #include <ethdev_driver.h>
1899a2dd95SBruce Richardson
1999a2dd95SBruce Richardson /**
2099a2dd95SBruce Richardson * Copy pci device info to the Ethernet device data.
2199a2dd95SBruce Richardson * Shared memory (eth_dev->data) only updated by primary process, so it is safe
2299a2dd95SBruce Richardson * to call this function from both primary and secondary processes.
2399a2dd95SBruce Richardson *
2499a2dd95SBruce Richardson * @param eth_dev
2599a2dd95SBruce Richardson * The *eth_dev* pointer is the address of the *rte_eth_dev* structure.
2699a2dd95SBruce Richardson * @param pci_dev
2799a2dd95SBruce Richardson * The *pci_dev* pointer is the address of the *rte_pci_device* structure.
2899a2dd95SBruce Richardson */
2999a2dd95SBruce Richardson static inline void
rte_eth_copy_pci_info(struct rte_eth_dev * eth_dev,struct rte_pci_device * pci_dev)3099a2dd95SBruce Richardson rte_eth_copy_pci_info(struct rte_eth_dev *eth_dev,
3199a2dd95SBruce Richardson struct rte_pci_device *pci_dev)
3299a2dd95SBruce Richardson {
3399a2dd95SBruce Richardson if ((eth_dev == NULL) || (pci_dev == NULL)) {
3499a2dd95SBruce Richardson RTE_ETHDEV_LOG(ERR, "NULL pointer eth_dev=%p pci_dev=%p",
3599a2dd95SBruce Richardson (void *)eth_dev, (void *)pci_dev);
3699a2dd95SBruce Richardson return;
3799a2dd95SBruce Richardson }
3899a2dd95SBruce Richardson
39d61138d4SHarman Kalra eth_dev->intr_handle = pci_dev->intr_handle;
4099a2dd95SBruce Richardson
4199a2dd95SBruce Richardson if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
4299a2dd95SBruce Richardson eth_dev->data->dev_flags = 0;
4399a2dd95SBruce Richardson if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
4499a2dd95SBruce Richardson eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
4599a2dd95SBruce Richardson if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_RMV)
4699a2dd95SBruce Richardson eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_RMV;
4799a2dd95SBruce Richardson
4899a2dd95SBruce Richardson eth_dev->data->numa_node = pci_dev->device.numa_node;
4999a2dd95SBruce Richardson }
5099a2dd95SBruce Richardson }
5199a2dd95SBruce Richardson
5299a2dd95SBruce Richardson static inline int
eth_dev_pci_specific_init(struct rte_eth_dev * eth_dev,void * bus_device)53*5ed2a2d4SBruce Richardson eth_dev_pci_specific_init(struct rte_eth_dev *eth_dev, void *bus_device)
54*5ed2a2d4SBruce Richardson {
55*5ed2a2d4SBruce Richardson struct rte_pci_device *pci_dev = (struct rte_pci_device *)bus_device;
5699a2dd95SBruce Richardson
5799a2dd95SBruce Richardson if (!pci_dev)
5899a2dd95SBruce Richardson return -ENODEV;
5999a2dd95SBruce Richardson
6099a2dd95SBruce Richardson rte_eth_copy_pci_info(eth_dev, pci_dev);
6199a2dd95SBruce Richardson
6299a2dd95SBruce Richardson return 0;
6399a2dd95SBruce Richardson }
6499a2dd95SBruce Richardson
6599a2dd95SBruce Richardson /**
6699a2dd95SBruce Richardson * @internal
670d9f56a8SAndrew Rybchenko * Allocates a new ethdev slot for an Ethernet device and returns the pointer
6899a2dd95SBruce Richardson * to that slot for the driver to use.
6999a2dd95SBruce Richardson *
7099a2dd95SBruce Richardson * @param dev
7199a2dd95SBruce Richardson * Pointer to the PCI device
7299a2dd95SBruce Richardson *
7399a2dd95SBruce Richardson * @param private_data_size
7499a2dd95SBruce Richardson * Size of private data structure
7599a2dd95SBruce Richardson *
7699a2dd95SBruce Richardson * @return
7799a2dd95SBruce Richardson * A pointer to a rte_eth_dev or NULL if allocation failed.
7899a2dd95SBruce Richardson */
7999a2dd95SBruce Richardson static inline struct rte_eth_dev *
rte_eth_dev_pci_allocate(struct rte_pci_device * dev,size_t private_data_size)8099a2dd95SBruce Richardson rte_eth_dev_pci_allocate(struct rte_pci_device *dev, size_t private_data_size)
8199a2dd95SBruce Richardson {
8299a2dd95SBruce Richardson struct rte_eth_dev *eth_dev;
8399a2dd95SBruce Richardson const char *name;
8499a2dd95SBruce Richardson
8599a2dd95SBruce Richardson if (!dev)
8699a2dd95SBruce Richardson return NULL;
8799a2dd95SBruce Richardson
8899a2dd95SBruce Richardson name = dev->device.name;
8999a2dd95SBruce Richardson
9099a2dd95SBruce Richardson if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
9199a2dd95SBruce Richardson eth_dev = rte_eth_dev_allocate(name);
9299a2dd95SBruce Richardson if (!eth_dev)
9399a2dd95SBruce Richardson return NULL;
9499a2dd95SBruce Richardson
9599a2dd95SBruce Richardson if (private_data_size) {
9699a2dd95SBruce Richardson eth_dev->data->dev_private = rte_zmalloc_socket(name,
9799a2dd95SBruce Richardson private_data_size, RTE_CACHE_LINE_SIZE,
9899a2dd95SBruce Richardson dev->device.numa_node);
9999a2dd95SBruce Richardson if (!eth_dev->data->dev_private) {
10099a2dd95SBruce Richardson rte_eth_dev_release_port(eth_dev);
10199a2dd95SBruce Richardson return NULL;
10299a2dd95SBruce Richardson }
10399a2dd95SBruce Richardson }
10499a2dd95SBruce Richardson } else {
10599a2dd95SBruce Richardson eth_dev = rte_eth_dev_attach_secondary(name);
10699a2dd95SBruce Richardson if (!eth_dev)
10799a2dd95SBruce Richardson return NULL;
10899a2dd95SBruce Richardson }
10999a2dd95SBruce Richardson
11099a2dd95SBruce Richardson eth_dev->device = &dev->device;
11199a2dd95SBruce Richardson rte_eth_copy_pci_info(eth_dev, dev);
11299a2dd95SBruce Richardson return eth_dev;
11399a2dd95SBruce Richardson }
11499a2dd95SBruce Richardson
11599a2dd95SBruce Richardson typedef int (*eth_dev_pci_callback_t)(struct rte_eth_dev *eth_dev);
11699a2dd95SBruce Richardson
11799a2dd95SBruce Richardson /**
11899a2dd95SBruce Richardson * @internal
11999a2dd95SBruce Richardson * Wrapper for use by pci drivers in a .probe function to attach to a ethdev
12099a2dd95SBruce Richardson * interface.
12199a2dd95SBruce Richardson */
12299a2dd95SBruce Richardson static inline int
rte_eth_dev_pci_generic_probe(struct rte_pci_device * pci_dev,size_t private_data_size,eth_dev_pci_callback_t dev_init)12399a2dd95SBruce Richardson rte_eth_dev_pci_generic_probe(struct rte_pci_device *pci_dev,
12499a2dd95SBruce Richardson size_t private_data_size, eth_dev_pci_callback_t dev_init)
12599a2dd95SBruce Richardson {
12699a2dd95SBruce Richardson struct rte_eth_dev *eth_dev;
12799a2dd95SBruce Richardson int ret;
12899a2dd95SBruce Richardson
12999a2dd95SBruce Richardson eth_dev = rte_eth_dev_pci_allocate(pci_dev, private_data_size);
13099a2dd95SBruce Richardson if (!eth_dev)
13199a2dd95SBruce Richardson return -ENOMEM;
13299a2dd95SBruce Richardson
13399a2dd95SBruce Richardson RTE_FUNC_PTR_OR_ERR_RET(*dev_init, -EINVAL);
13499a2dd95SBruce Richardson ret = dev_init(eth_dev);
13599a2dd95SBruce Richardson if (ret)
13699a2dd95SBruce Richardson rte_eth_dev_release_port(eth_dev);
13799a2dd95SBruce Richardson else
13899a2dd95SBruce Richardson rte_eth_dev_probing_finish(eth_dev);
13999a2dd95SBruce Richardson
14099a2dd95SBruce Richardson return ret;
14199a2dd95SBruce Richardson }
14299a2dd95SBruce Richardson
14399a2dd95SBruce Richardson /**
14499a2dd95SBruce Richardson * @internal
14599a2dd95SBruce Richardson * Wrapper for use by pci drivers in a .remove function to detach a ethdev
14699a2dd95SBruce Richardson * interface.
14799a2dd95SBruce Richardson */
14899a2dd95SBruce Richardson static inline int
rte_eth_dev_pci_generic_remove(struct rte_pci_device * pci_dev,eth_dev_pci_callback_t dev_uninit)14999a2dd95SBruce Richardson rte_eth_dev_pci_generic_remove(struct rte_pci_device *pci_dev,
15099a2dd95SBruce Richardson eth_dev_pci_callback_t dev_uninit)
15199a2dd95SBruce Richardson {
15299a2dd95SBruce Richardson struct rte_eth_dev *eth_dev;
15399a2dd95SBruce Richardson int ret;
15499a2dd95SBruce Richardson
15599a2dd95SBruce Richardson eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
15699a2dd95SBruce Richardson if (!eth_dev)
15799a2dd95SBruce Richardson return 0;
15899a2dd95SBruce Richardson
15917faaed8SHuisong Li /*
16017faaed8SHuisong Li * In secondary process, a released eth device can be found by its name
16117faaed8SHuisong Li * in shared memory.
16217faaed8SHuisong Li * If the state of the eth device is RTE_ETH_DEV_UNUSED, it means the
16317faaed8SHuisong Li * eth device has been released.
16417faaed8SHuisong Li */
16517faaed8SHuisong Li if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
16617faaed8SHuisong Li eth_dev->state == RTE_ETH_DEV_UNUSED)
16717faaed8SHuisong Li return 0;
16817faaed8SHuisong Li
16999a2dd95SBruce Richardson if (dev_uninit) {
17099a2dd95SBruce Richardson ret = dev_uninit(eth_dev);
17199a2dd95SBruce Richardson if (ret)
17299a2dd95SBruce Richardson return ret;
17399a2dd95SBruce Richardson }
17499a2dd95SBruce Richardson
17599a2dd95SBruce Richardson rte_eth_dev_release_port(eth_dev);
17699a2dd95SBruce Richardson return 0;
17799a2dd95SBruce Richardson }
17899a2dd95SBruce Richardson
179dbf9fc1dSBrian Dooley #ifdef __cplusplus
180dbf9fc1dSBrian Dooley }
181dbf9fc1dSBrian Dooley #endif
182dbf9fc1dSBrian Dooley
18399a2dd95SBruce Richardson #endif /* _RTE_ETHDEV_PCI_H_ */
184