DPDK  20.11.10
rte_ethdev_pci.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2017 Brocade Communications Systems, Inc.
3  * Author: Jan Blunck <jblunck@infradead.org>
4  */
5 
6 #ifndef _RTE_ETHDEV_PCI_H_
7 #define _RTE_ETHDEV_PCI_H_
8 
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 
13 #include <rte_malloc.h>
14 #include <rte_pci.h>
15 #include <rte_bus_pci.h>
16 #include <rte_config.h>
17 #include <rte_ethdev_driver.h>
18 
29 static inline void
30 rte_eth_copy_pci_info(struct rte_eth_dev *eth_dev,
31  struct rte_pci_device *pci_dev)
32 {
33  if ((eth_dev == NULL) || (pci_dev == NULL)) {
34  RTE_ETHDEV_LOG(ERR, "NULL pointer eth_dev=%p pci_dev=%p",
35  (void *)eth_dev, (void *)pci_dev);
36  return;
37  }
38 
39  eth_dev->intr_handle = &pci_dev->intr_handle;
40 
41  if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
42  eth_dev->data->dev_flags = 0;
43  if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_LSC)
44  eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_LSC;
45  if (pci_dev->driver->drv_flags & RTE_PCI_DRV_INTR_RMV)
46  eth_dev->data->dev_flags |= RTE_ETH_DEV_INTR_RMV;
47 
48  eth_dev->data->numa_node = pci_dev->device.numa_node;
49  }
50 }
51 
52 static inline int
53 eth_dev_pci_specific_init(struct rte_eth_dev *eth_dev, void *bus_device)
54 {
55  struct rte_pci_device *pci_dev = (struct rte_pci_device *)bus_device;
56 
57  if (!pci_dev)
58  return -ENODEV;
59 
60  rte_eth_copy_pci_info(eth_dev, pci_dev);
61 
62  return 0;
63 }
64 
79 static inline struct rte_eth_dev *
80 rte_eth_dev_pci_allocate(struct rte_pci_device *dev, size_t private_data_size)
81 {
82  struct rte_eth_dev *eth_dev;
83  const char *name;
84 
85  if (!dev)
86  return NULL;
87 
88  name = dev->device.name;
89 
90  if (rte_eal_process_type() == RTE_PROC_PRIMARY) {
91  eth_dev = rte_eth_dev_allocate(name);
92  if (!eth_dev)
93  return NULL;
94 
95  if (private_data_size) {
96  eth_dev->data->dev_private = rte_zmalloc_socket(name,
97  private_data_size, RTE_CACHE_LINE_SIZE,
98  dev->device.numa_node);
99  if (!eth_dev->data->dev_private) {
100  rte_eth_dev_release_port(eth_dev);
101  return NULL;
102  }
103  }
104  } else {
105  eth_dev = rte_eth_dev_attach_secondary(name);
106  if (!eth_dev)
107  return NULL;
108  }
109 
110  eth_dev->device = &dev->device;
111  rte_eth_copy_pci_info(eth_dev, dev);
112  return eth_dev;
113 }
114 
115 typedef int (*eth_dev_pci_callback_t)(struct rte_eth_dev *eth_dev);
116 
122 static inline int
123 rte_eth_dev_pci_generic_probe(struct rte_pci_device *pci_dev,
124  size_t private_data_size, eth_dev_pci_callback_t dev_init)
125 {
126  struct rte_eth_dev *eth_dev;
127  int ret;
128 
129  if (*dev_init == NULL)
130  return -EINVAL;
131 
132  eth_dev = rte_eth_dev_pci_allocate(pci_dev, private_data_size);
133  if (!eth_dev)
134  return -ENOMEM;
135 
136  RTE_FUNC_PTR_OR_ERR_RET(*dev_init, -EINVAL);
137  ret = dev_init(eth_dev);
138  if (ret)
139  rte_eth_dev_release_port(eth_dev);
140  else
141  rte_eth_dev_probing_finish(eth_dev);
142 
143  return ret;
144 }
145 
151 static inline int
152 rte_eth_dev_pci_generic_remove(struct rte_pci_device *pci_dev,
153  eth_dev_pci_callback_t dev_uninit)
154 {
155  struct rte_eth_dev *eth_dev;
156  int ret;
157 
158  eth_dev = rte_eth_dev_allocated(pci_dev->device.name);
159  if (!eth_dev)
160  return 0;
161 
162  /*
163  * In secondary process, a released eth device can be found by its name
164  * in shared memory.
165  * If the state of the eth device is RTE_ETH_DEV_UNUSED, it means the
166  * eth device has been released.
167  */
168  if (rte_eal_process_type() == RTE_PROC_SECONDARY &&
169  eth_dev->state == RTE_ETH_DEV_UNUSED)
170  return 0;
171 
172  if (dev_uninit) {
173  ret = dev_uninit(eth_dev);
174  if (ret)
175  return ret;
176  }
177 
178  rte_eth_dev_release_port(eth_dev);
179  return 0;
180 }
181 
182 #ifdef __cplusplus
183 }
184 #endif
185 
186 #endif /* _RTE_ETHDEV_PCI_H_ */
#define RTE_ETH_DEV_INTR_LSC
Definition: rte_ethdev.h:1815
enum rte_proc_type_t rte_eal_process_type(void)
#define RTE_ETH_DEV_INTR_RMV
Definition: rte_ethdev.h:1819
void * rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) __rte_alloc_size(2)