xref: /dpdk/drivers/bus/pci/pci_common.c (revision 0c00abfd85448449d73acc09cc6fe445dba37b50)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation.
3  * Copyright 2013-2014 6WIND S.A.
4  */
5 
6 #include <string.h>
7 #include <inttypes.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <sys/queue.h>
12 #include <sys/mman.h>
13 
14 #include <rte_errno.h>
15 #include <rte_interrupts.h>
16 #include <rte_log.h>
17 #include <rte_bus.h>
18 #include <rte_pci.h>
19 #include <rte_bus_pci.h>
20 #include <rte_per_lcore.h>
21 #include <rte_memory.h>
22 #include <rte_eal.h>
23 #include <rte_string_fns.h>
24 #include <rte_common.h>
25 #include <rte_devargs.h>
26 
27 #include "private.h"
28 
29 
30 static void rte_pci_remove_device(struct rte_pci_device *pci_device);
31 
32 extern struct rte_pci_bus rte_pci_bus;
33 
34 #define SYSFS_PCI_DEVICES "/sys/bus/pci/devices"
35 
36 const char *rte_pci_get_sysfs_path(void)
37 {
38 	const char *path = NULL;
39 
40 	path = getenv("SYSFS_PCI_DEVICES");
41 	if (path == NULL)
42 		return SYSFS_PCI_DEVICES;
43 
44 	return path;
45 }
46 
47 static struct rte_devargs *pci_devargs_lookup(struct rte_pci_device *dev)
48 {
49 	struct rte_devargs *devargs;
50 	struct rte_pci_addr addr;
51 
52 	RTE_EAL_DEVARGS_FOREACH("pci", devargs) {
53 		devargs->bus->parse(devargs->name, &addr);
54 		if (!rte_pci_addr_cmp(&dev->addr, &addr))
55 			return devargs;
56 	}
57 	return NULL;
58 }
59 
60 void
61 pci_name_set(struct rte_pci_device *dev)
62 {
63 	struct rte_devargs *devargs;
64 
65 	/* Each device has its internal, canonical name set. */
66 	rte_pci_device_name(&dev->addr,
67 			dev->name, sizeof(dev->name));
68 	devargs = pci_devargs_lookup(dev);
69 	dev->device.devargs = devargs;
70 	/* In blacklist mode, if the device is not blacklisted, no
71 	 * rte_devargs exists for it.
72 	 */
73 	if (devargs != NULL)
74 		/* If an rte_devargs exists, the generic rte_device uses the
75 		 * given name as its name.
76 		 */
77 		dev->device.name = dev->device.devargs->name;
78 	else
79 		/* Otherwise, it uses the internal, canonical form. */
80 		dev->device.name = dev->name;
81 }
82 
83 /*
84  * Match the PCI Driver and Device using the ID Table
85  */
86 int
87 rte_pci_match(const struct rte_pci_driver *pci_drv,
88 	      const struct rte_pci_device *pci_dev)
89 {
90 	const struct rte_pci_id *id_table;
91 
92 	for (id_table = pci_drv->id_table; id_table->vendor_id != 0;
93 	     id_table++) {
94 		/* check if device's identifiers match the driver's ones */
95 		if (id_table->vendor_id != pci_dev->id.vendor_id &&
96 				id_table->vendor_id != PCI_ANY_ID)
97 			continue;
98 		if (id_table->device_id != pci_dev->id.device_id &&
99 				id_table->device_id != PCI_ANY_ID)
100 			continue;
101 		if (id_table->subsystem_vendor_id !=
102 		    pci_dev->id.subsystem_vendor_id &&
103 		    id_table->subsystem_vendor_id != PCI_ANY_ID)
104 			continue;
105 		if (id_table->subsystem_device_id !=
106 		    pci_dev->id.subsystem_device_id &&
107 		    id_table->subsystem_device_id != PCI_ANY_ID)
108 			continue;
109 		if (id_table->class_id != pci_dev->id.class_id &&
110 				id_table->class_id != RTE_CLASS_ANY_ID)
111 			continue;
112 
113 		return 1;
114 	}
115 
116 	return 0;
117 }
118 
119 /*
120  * If vendor/device ID match, call the probe() function of the
121  * driver.
122  */
123 static int
124 rte_pci_probe_one_driver(struct rte_pci_driver *dr,
125 			 struct rte_pci_device *dev)
126 {
127 	int ret;
128 	struct rte_pci_addr *loc;
129 
130 	if ((dr == NULL) || (dev == NULL))
131 		return -EINVAL;
132 
133 	loc = &dev->addr;
134 
135 	/* The device is not blacklisted; Check if driver supports it */
136 	if (!rte_pci_match(dr, dev))
137 		/* Match of device and driver failed */
138 		return 1;
139 
140 	RTE_LOG(INFO, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
141 			loc->domain, loc->bus, loc->devid, loc->function,
142 			dev->device.numa_node);
143 
144 	/* no initialization when blacklisted, return without error */
145 	if (dev->device.devargs != NULL &&
146 		dev->device.devargs->policy ==
147 			RTE_DEV_BLACKLISTED) {
148 		RTE_LOG(INFO, EAL, "  Device is blacklisted, not"
149 			" initializing\n");
150 		return 1;
151 	}
152 
153 	if (dev->device.numa_node < 0) {
154 		RTE_LOG(WARNING, EAL, "  Invalid NUMA socket, default to 0\n");
155 		dev->device.numa_node = 0;
156 	}
157 
158 	RTE_LOG(INFO, EAL, "  probe driver: %x:%x %s\n", dev->id.vendor_id,
159 		dev->id.device_id, dr->driver.name);
160 
161 	/*
162 	 * reference driver structure
163 	 * This needs to be before rte_pci_map_device(), as it enables to use
164 	 * driver flags for adjusting configuration.
165 	 */
166 	dev->driver = dr;
167 	dev->device.driver = &dr->driver;
168 
169 	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) {
170 		/* map resources for devices that use igb_uio */
171 		ret = rte_pci_map_device(dev);
172 		if (ret != 0) {
173 			dev->driver = NULL;
174 			dev->device.driver = NULL;
175 			return ret;
176 		}
177 	}
178 
179 	/* call the driver probe() function */
180 	ret = dr->probe(dr, dev);
181 	if (ret) {
182 		dev->driver = NULL;
183 		dev->device.driver = NULL;
184 		if ((dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING) &&
185 			/* Don't unmap if device is unsupported and
186 			 * driver needs mapped resources.
187 			 */
188 			!(ret > 0 &&
189 				(dr->drv_flags & RTE_PCI_DRV_KEEP_MAPPED_RES)))
190 			rte_pci_unmap_device(dev);
191 	}
192 
193 	return ret;
194 }
195 
196 /*
197  * If vendor/device ID match, call the remove() function of the
198  * driver.
199  */
200 static int
201 rte_pci_detach_dev(struct rte_pci_device *dev)
202 {
203 	struct rte_pci_addr *loc;
204 	struct rte_pci_driver *dr;
205 	int ret = 0;
206 
207 	if (dev == NULL)
208 		return -EINVAL;
209 
210 	dr = dev->driver;
211 	loc = &dev->addr;
212 
213 	RTE_LOG(DEBUG, EAL, "PCI device "PCI_PRI_FMT" on NUMA socket %i\n",
214 			loc->domain, loc->bus, loc->devid,
215 			loc->function, dev->device.numa_node);
216 
217 	RTE_LOG(DEBUG, EAL, "  remove driver: %x:%x %s\n", dev->id.vendor_id,
218 			dev->id.device_id, dr->driver.name);
219 
220 	if (dr->remove) {
221 		ret = dr->remove(dev);
222 		if (ret < 0)
223 			return ret;
224 	}
225 
226 	/* clear driver structure */
227 	dev->driver = NULL;
228 
229 	if (dr->drv_flags & RTE_PCI_DRV_NEED_MAPPING)
230 		/* unmap resources for devices that use igb_uio */
231 		rte_pci_unmap_device(dev);
232 
233 	return 0;
234 }
235 
236 /*
237  * If vendor/device ID match, call the probe() function of all
238  * registered driver for the given device. Return -1 if initialization
239  * failed, return 1 if no driver is found for this device.
240  */
241 static int
242 pci_probe_all_drivers(struct rte_pci_device *dev)
243 {
244 	struct rte_pci_driver *dr = NULL;
245 	int rc = 0;
246 
247 	if (dev == NULL)
248 		return -1;
249 
250 	/* Check if a driver is already loaded */
251 	if (dev->driver != NULL)
252 		return 0;
253 
254 	FOREACH_DRIVER_ON_PCIBUS(dr) {
255 		rc = rte_pci_probe_one_driver(dr, dev);
256 		if (rc < 0)
257 			/* negative value is an error */
258 			return -1;
259 		if (rc > 0)
260 			/* positive value means driver doesn't support it */
261 			continue;
262 		return 0;
263 	}
264 	return 1;
265 }
266 
267 /*
268  * Scan the content of the PCI bus, and call the probe() function for
269  * all registered drivers that have a matching entry in its id_table
270  * for discovered devices.
271  */
272 int
273 rte_pci_probe(void)
274 {
275 	struct rte_pci_device *dev = NULL;
276 	size_t probed = 0, failed = 0;
277 	struct rte_devargs *devargs;
278 	int probe_all = 0;
279 	int ret = 0;
280 
281 	if (rte_pci_bus.bus.conf.scan_mode != RTE_BUS_SCAN_WHITELIST)
282 		probe_all = 1;
283 
284 	FOREACH_DEVICE_ON_PCIBUS(dev) {
285 		probed++;
286 
287 		devargs = dev->device.devargs;
288 		/* probe all or only whitelisted devices */
289 		if (probe_all)
290 			ret = pci_probe_all_drivers(dev);
291 		else if (devargs != NULL &&
292 			devargs->policy == RTE_DEV_WHITELISTED)
293 			ret = pci_probe_all_drivers(dev);
294 		if (ret < 0) {
295 			RTE_LOG(ERR, EAL, "Requested device " PCI_PRI_FMT
296 				 " cannot be used\n", dev->addr.domain, dev->addr.bus,
297 				 dev->addr.devid, dev->addr.function);
298 			rte_errno = errno;
299 			failed++;
300 			ret = 0;
301 		}
302 	}
303 
304 	return (probed && probed == failed) ? -1 : 0;
305 }
306 
307 /* dump one device */
308 static int
309 pci_dump_one_device(FILE *f, struct rte_pci_device *dev)
310 {
311 	int i;
312 
313 	fprintf(f, PCI_PRI_FMT, dev->addr.domain, dev->addr.bus,
314 	       dev->addr.devid, dev->addr.function);
315 	fprintf(f, " - vendor:%x device:%x\n", dev->id.vendor_id,
316 	       dev->id.device_id);
317 
318 	for (i = 0; i != sizeof(dev->mem_resource) /
319 		sizeof(dev->mem_resource[0]); i++) {
320 		fprintf(f, "   %16.16"PRIx64" %16.16"PRIx64"\n",
321 			dev->mem_resource[i].phys_addr,
322 			dev->mem_resource[i].len);
323 	}
324 	return 0;
325 }
326 
327 /* dump devices on the bus */
328 void
329 rte_pci_dump(FILE *f)
330 {
331 	struct rte_pci_device *dev = NULL;
332 
333 	FOREACH_DEVICE_ON_PCIBUS(dev) {
334 		pci_dump_one_device(f, dev);
335 	}
336 }
337 
338 static int
339 pci_parse(const char *name, void *addr)
340 {
341 	struct rte_pci_addr *out = addr;
342 	struct rte_pci_addr pci_addr;
343 	bool parse;
344 
345 	parse = (rte_pci_addr_parse(name, &pci_addr) == 0);
346 	if (parse && addr != NULL)
347 		*out = pci_addr;
348 	return parse == false;
349 }
350 
351 /* register a driver */
352 void
353 rte_pci_register(struct rte_pci_driver *driver)
354 {
355 	TAILQ_INSERT_TAIL(&rte_pci_bus.driver_list, driver, next);
356 	driver->bus = &rte_pci_bus;
357 }
358 
359 /* unregister a driver */
360 void
361 rte_pci_unregister(struct rte_pci_driver *driver)
362 {
363 	TAILQ_REMOVE(&rte_pci_bus.driver_list, driver, next);
364 	driver->bus = NULL;
365 }
366 
367 /* Add a device to PCI bus */
368 void
369 rte_pci_add_device(struct rte_pci_device *pci_dev)
370 {
371 	TAILQ_INSERT_TAIL(&rte_pci_bus.device_list, pci_dev, next);
372 }
373 
374 /* Insert a device into a predefined position in PCI bus */
375 void
376 rte_pci_insert_device(struct rte_pci_device *exist_pci_dev,
377 		      struct rte_pci_device *new_pci_dev)
378 {
379 	TAILQ_INSERT_BEFORE(exist_pci_dev, new_pci_dev, next);
380 }
381 
382 /* Remove a device from PCI bus */
383 static void
384 rte_pci_remove_device(struct rte_pci_device *pci_dev)
385 {
386 	TAILQ_REMOVE(&rte_pci_bus.device_list, pci_dev, next);
387 }
388 
389 static struct rte_device *
390 pci_find_device(const struct rte_device *start, rte_dev_cmp_t cmp,
391 		const void *data)
392 {
393 	const struct rte_pci_device *pstart;
394 	struct rte_pci_device *pdev;
395 
396 	if (start != NULL) {
397 		pstart = RTE_DEV_TO_PCI_CONST(start);
398 		pdev = TAILQ_NEXT(pstart, next);
399 	} else {
400 		pdev = TAILQ_FIRST(&rte_pci_bus.device_list);
401 	}
402 	while (pdev != NULL) {
403 		if (cmp(&pdev->device, data) == 0)
404 			return &pdev->device;
405 		pdev = TAILQ_NEXT(pdev, next);
406 	}
407 	return NULL;
408 }
409 
410 static int
411 pci_plug(struct rte_device *dev)
412 {
413 	return pci_probe_all_drivers(RTE_DEV_TO_PCI(dev));
414 }
415 
416 static int
417 pci_unplug(struct rte_device *dev)
418 {
419 	struct rte_pci_device *pdev;
420 	int ret;
421 
422 	pdev = RTE_DEV_TO_PCI(dev);
423 	ret = rte_pci_detach_dev(pdev);
424 	if (ret == 0) {
425 		rte_pci_remove_device(pdev);
426 		free(pdev);
427 	}
428 	return ret;
429 }
430 
431 struct rte_pci_bus rte_pci_bus = {
432 	.bus = {
433 		.scan = rte_pci_scan,
434 		.probe = rte_pci_probe,
435 		.find_device = pci_find_device,
436 		.plug = pci_plug,
437 		.unplug = pci_unplug,
438 		.parse = pci_parse,
439 		.get_iommu_class = rte_pci_get_iommu_class,
440 	},
441 	.device_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.device_list),
442 	.driver_list = TAILQ_HEAD_INITIALIZER(rte_pci_bus.driver_list),
443 };
444 
445 RTE_REGISTER_BUS(pci, rte_pci_bus.bus);
446