xref: /openbmc/linux/drivers/pci/pci-driver.c (revision a1e58bbd)
1 /*
2  * drivers/pci/pci-driver.c
3  *
4  * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
5  * (C) Copyright 2007 Novell Inc.
6  *
7  * Released under the GPL v2 only.
8  *
9  */
10 
11 #include <linux/pci.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/device.h>
15 #include <linux/mempolicy.h>
16 #include <linux/string.h>
17 #include <linux/slab.h>
18 #include <linux/sched.h>
19 #include "pci.h"
20 
21 /*
22  * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
23  */
24 
25 struct pci_dynid {
26 	struct list_head node;
27 	struct pci_device_id id;
28 };
29 
30 #ifdef CONFIG_HOTPLUG
31 
32 /**
33  * store_new_id - add a new PCI device ID to this driver and re-probe devices
34  * @driver: target device driver
35  * @buf: buffer for scanning device ID data
36  * @count: input size
37  *
38  * Adds a new dynamic pci device ID to this driver,
39  * and causes the driver to probe for all devices again.
40  */
41 static ssize_t
42 store_new_id(struct device_driver *driver, const char *buf, size_t count)
43 {
44 	struct pci_dynid *dynid;
45 	struct pci_driver *pdrv = to_pci_driver(driver);
46 	__u32 vendor, device, subvendor=PCI_ANY_ID,
47 		subdevice=PCI_ANY_ID, class=0, class_mask=0;
48 	unsigned long driver_data=0;
49 	int fields=0;
50 	int retval = 0;
51 
52 	fields = sscanf(buf, "%x %x %x %x %x %x %lux",
53 			&vendor, &device, &subvendor, &subdevice,
54 			&class, &class_mask, &driver_data);
55 	if (fields < 2)
56 		return -EINVAL;
57 
58 	dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
59 	if (!dynid)
60 		return -ENOMEM;
61 
62 	dynid->id.vendor = vendor;
63 	dynid->id.device = device;
64 	dynid->id.subvendor = subvendor;
65 	dynid->id.subdevice = subdevice;
66 	dynid->id.class = class;
67 	dynid->id.class_mask = class_mask;
68 	dynid->id.driver_data = pdrv->dynids.use_driver_data ?
69 		driver_data : 0UL;
70 
71 	spin_lock(&pdrv->dynids.lock);
72 	list_add_tail(&dynid->node, &pdrv->dynids.list);
73 	spin_unlock(&pdrv->dynids.lock);
74 
75 	if (get_driver(&pdrv->driver)) {
76 		retval = driver_attach(&pdrv->driver);
77 		put_driver(&pdrv->driver);
78 	}
79 
80 	if (retval)
81 		return retval;
82 	return count;
83 }
84 static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
85 
86 static void
87 pci_free_dynids(struct pci_driver *drv)
88 {
89 	struct pci_dynid *dynid, *n;
90 
91 	spin_lock(&drv->dynids.lock);
92 	list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
93 		list_del(&dynid->node);
94 		kfree(dynid);
95 	}
96 	spin_unlock(&drv->dynids.lock);
97 }
98 
99 static int
100 pci_create_newid_file(struct pci_driver *drv)
101 {
102 	int error = 0;
103 	if (drv->probe != NULL)
104 		error = driver_create_file(&drv->driver, &driver_attr_new_id);
105 	return error;
106 }
107 
108 static void pci_remove_newid_file(struct pci_driver *drv)
109 {
110 	driver_remove_file(&drv->driver, &driver_attr_new_id);
111 }
112 #else /* !CONFIG_HOTPLUG */
113 static inline void pci_free_dynids(struct pci_driver *drv) {}
114 static inline int pci_create_newid_file(struct pci_driver *drv)
115 {
116 	return 0;
117 }
118 static inline void pci_remove_newid_file(struct pci_driver *drv) {}
119 #endif
120 
121 /**
122  * pci_match_id - See if a pci device matches a given pci_id table
123  * @ids: array of PCI device id structures to search in
124  * @dev: the PCI device structure to match against.
125  *
126  * Used by a driver to check whether a PCI device present in the
127  * system is in its list of supported devices.  Returns the matching
128  * pci_device_id structure or %NULL if there is no match.
129  *
130  * Deprecated, don't use this as it will not catch any dynamic ids
131  * that a driver might want to check for.
132  */
133 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
134 					 struct pci_dev *dev)
135 {
136 	if (ids) {
137 		while (ids->vendor || ids->subvendor || ids->class_mask) {
138 			if (pci_match_one_device(ids, dev))
139 				return ids;
140 			ids++;
141 		}
142 	}
143 	return NULL;
144 }
145 
146 /**
147  * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
148  * @drv: the PCI driver to match against
149  * @dev: the PCI device structure to match against
150  *
151  * Used by a driver to check whether a PCI device present in the
152  * system is in its list of supported devices.  Returns the matching
153  * pci_device_id structure or %NULL if there is no match.
154  */
155 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
156 						    struct pci_dev *dev)
157 {
158 	struct pci_dynid *dynid;
159 
160 	/* Look at the dynamic ids first, before the static ones */
161 	spin_lock(&drv->dynids.lock);
162 	list_for_each_entry(dynid, &drv->dynids.list, node) {
163 		if (pci_match_one_device(&dynid->id, dev)) {
164 			spin_unlock(&drv->dynids.lock);
165 			return &dynid->id;
166 		}
167 	}
168 	spin_unlock(&drv->dynids.lock);
169 
170 	return pci_match_id(drv->id_table, dev);
171 }
172 
173 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
174 			  const struct pci_device_id *id)
175 {
176 	int error;
177 #ifdef CONFIG_NUMA
178 	/* Execute driver initialization on node where the
179 	   device's bus is attached to.  This way the driver likely
180 	   allocates its local memory on the right node without
181 	   any need to change it. */
182 	struct mempolicy *oldpol;
183 	cpumask_t oldmask = current->cpus_allowed;
184 	int node = pcibus_to_node(dev->bus);
185 	if (node >= 0 && node_online(node))
186 	    set_cpus_allowed(current, node_to_cpumask(node));
187 	/* And set default memory allocation policy */
188 	oldpol = current->mempolicy;
189 	current->mempolicy = NULL;	/* fall back to system default policy */
190 #endif
191 	error = drv->probe(dev, id);
192 #ifdef CONFIG_NUMA
193 	set_cpus_allowed(current, oldmask);
194 	current->mempolicy = oldpol;
195 #endif
196 	return error;
197 }
198 
199 /**
200  * __pci_device_probe()
201  * @drv: driver to call to check if it wants the PCI device
202  * @pci_dev: PCI device being probed
203  *
204  * returns 0 on success, else error.
205  * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
206  */
207 static int
208 __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
209 {
210 	const struct pci_device_id *id;
211 	int error = 0;
212 
213 	if (!pci_dev->driver && drv->probe) {
214 		error = -ENODEV;
215 
216 		id = pci_match_device(drv, pci_dev);
217 		if (id)
218 			error = pci_call_probe(drv, pci_dev, id);
219 		if (error >= 0) {
220 			pci_dev->driver = drv;
221 			error = 0;
222 		}
223 	}
224 	return error;
225 }
226 
227 static int pci_device_probe(struct device * dev)
228 {
229 	int error = 0;
230 	struct pci_driver *drv;
231 	struct pci_dev *pci_dev;
232 
233 	drv = to_pci_driver(dev->driver);
234 	pci_dev = to_pci_dev(dev);
235 	pci_dev_get(pci_dev);
236 	error = __pci_device_probe(drv, pci_dev);
237 	if (error)
238 		pci_dev_put(pci_dev);
239 
240 	return error;
241 }
242 
243 static int pci_device_remove(struct device * dev)
244 {
245 	struct pci_dev * pci_dev = to_pci_dev(dev);
246 	struct pci_driver * drv = pci_dev->driver;
247 
248 	if (drv) {
249 		if (drv->remove)
250 			drv->remove(pci_dev);
251 		pci_dev->driver = NULL;
252 	}
253 
254 	/*
255 	 * If the device is still on, set the power state as "unknown",
256 	 * since it might change by the next time we load the driver.
257 	 */
258 	if (pci_dev->current_state == PCI_D0)
259 		pci_dev->current_state = PCI_UNKNOWN;
260 
261 	/*
262 	 * We would love to complain here if pci_dev->is_enabled is set, that
263 	 * the driver should have called pci_disable_device(), but the
264 	 * unfortunate fact is there are too many odd BIOS and bridge setups
265 	 * that don't like drivers doing that all of the time.
266 	 * Oh well, we can dream of sane hardware when we sleep, no matter how
267 	 * horrible the crap we have to deal with is when we are awake...
268 	 */
269 
270 	pci_dev_put(pci_dev);
271 	return 0;
272 }
273 
274 static int pci_device_suspend(struct device * dev, pm_message_t state)
275 {
276 	struct pci_dev * pci_dev = to_pci_dev(dev);
277 	struct pci_driver * drv = pci_dev->driver;
278 	int i = 0;
279 
280 	if (drv && drv->suspend) {
281 		i = drv->suspend(pci_dev, state);
282 		suspend_report_result(drv->suspend, i);
283 	} else {
284 		pci_save_state(pci_dev);
285 		/*
286 		 * mark its power state as "unknown", since we don't know if
287 		 * e.g. the BIOS will change its device state when we suspend.
288 		 */
289 		if (pci_dev->current_state == PCI_D0)
290 			pci_dev->current_state = PCI_UNKNOWN;
291 	}
292 	return i;
293 }
294 
295 static int pci_device_suspend_late(struct device * dev, pm_message_t state)
296 {
297 	struct pci_dev * pci_dev = to_pci_dev(dev);
298 	struct pci_driver * drv = pci_dev->driver;
299 	int i = 0;
300 
301 	if (drv && drv->suspend_late) {
302 		i = drv->suspend_late(pci_dev, state);
303 		suspend_report_result(drv->suspend_late, i);
304 	}
305 	return i;
306 }
307 
308 /*
309  * Default resume method for devices that have no driver provided resume,
310  * or not even a driver at all.
311  */
312 static int pci_default_resume(struct pci_dev *pci_dev)
313 {
314 	int retval = 0;
315 
316 	/* restore the PCI config space */
317 	pci_restore_state(pci_dev);
318 	/* if the device was enabled before suspend, reenable */
319 	retval = pci_reenable_device(pci_dev);
320 	/* if the device was busmaster before the suspend, make it busmaster again */
321 	if (pci_dev->is_busmaster)
322 		pci_set_master(pci_dev);
323 
324 	return retval;
325 }
326 
327 static int pci_device_resume(struct device * dev)
328 {
329 	int error;
330 	struct pci_dev * pci_dev = to_pci_dev(dev);
331 	struct pci_driver * drv = pci_dev->driver;
332 
333 	if (drv && drv->resume)
334 		error = drv->resume(pci_dev);
335 	else
336 		error = pci_default_resume(pci_dev);
337 	return error;
338 }
339 
340 static int pci_device_resume_early(struct device * dev)
341 {
342 	int error = 0;
343 	struct pci_dev * pci_dev = to_pci_dev(dev);
344 	struct pci_driver * drv = pci_dev->driver;
345 
346 	pci_fixup_device(pci_fixup_resume, pci_dev);
347 
348 	if (drv && drv->resume_early)
349 		error = drv->resume_early(pci_dev);
350 	return error;
351 }
352 
353 static void pci_device_shutdown(struct device *dev)
354 {
355 	struct pci_dev *pci_dev = to_pci_dev(dev);
356 	struct pci_driver *drv = pci_dev->driver;
357 
358 	if (drv && drv->shutdown)
359 		drv->shutdown(pci_dev);
360 }
361 
362 /**
363  * __pci_register_driver - register a new pci driver
364  * @drv: the driver structure to register
365  * @owner: owner module of drv
366  * @mod_name: module name string
367  *
368  * Adds the driver structure to the list of registered drivers.
369  * Returns a negative value on error, otherwise 0.
370  * If no error occurred, the driver remains registered even if
371  * no device was claimed during registration.
372  */
373 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
374 			  const char *mod_name)
375 {
376 	int error;
377 
378 	/* initialize common driver fields */
379 	drv->driver.name = drv->name;
380 	drv->driver.bus = &pci_bus_type;
381 	drv->driver.owner = owner;
382 	drv->driver.mod_name = mod_name;
383 
384 	spin_lock_init(&drv->dynids.lock);
385 	INIT_LIST_HEAD(&drv->dynids.list);
386 
387 	/* register with core */
388 	error = driver_register(&drv->driver);
389 	if (error)
390 		return error;
391 
392 	error = pci_create_newid_file(drv);
393 	if (error)
394 		driver_unregister(&drv->driver);
395 
396 	return error;
397 }
398 
399 /**
400  * pci_unregister_driver - unregister a pci driver
401  * @drv: the driver structure to unregister
402  *
403  * Deletes the driver structure from the list of registered PCI drivers,
404  * gives it a chance to clean up by calling its remove() function for
405  * each device it was responsible for, and marks those devices as
406  * driverless.
407  */
408 
409 void
410 pci_unregister_driver(struct pci_driver *drv)
411 {
412 	pci_remove_newid_file(drv);
413 	driver_unregister(&drv->driver);
414 	pci_free_dynids(drv);
415 }
416 
417 static struct pci_driver pci_compat_driver = {
418 	.name = "compat"
419 };
420 
421 /**
422  * pci_dev_driver - get the pci_driver of a device
423  * @dev: the device to query
424  *
425  * Returns the appropriate pci_driver structure or %NULL if there is no
426  * registered driver for the device.
427  */
428 struct pci_driver *
429 pci_dev_driver(const struct pci_dev *dev)
430 {
431 	if (dev->driver)
432 		return dev->driver;
433 	else {
434 		int i;
435 		for(i=0; i<=PCI_ROM_RESOURCE; i++)
436 			if (dev->resource[i].flags & IORESOURCE_BUSY)
437 				return &pci_compat_driver;
438 	}
439 	return NULL;
440 }
441 
442 /**
443  * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
444  * @dev: the PCI device structure to match against
445  * @drv: the device driver to search for matching PCI device id structures
446  *
447  * Used by a driver to check whether a PCI device present in the
448  * system is in its list of supported devices. Returns the matching
449  * pci_device_id structure or %NULL if there is no match.
450  */
451 static int pci_bus_match(struct device *dev, struct device_driver *drv)
452 {
453 	struct pci_dev *pci_dev = to_pci_dev(dev);
454 	struct pci_driver *pci_drv = to_pci_driver(drv);
455 	const struct pci_device_id *found_id;
456 
457 	found_id = pci_match_device(pci_drv, pci_dev);
458 	if (found_id)
459 		return 1;
460 
461 	return 0;
462 }
463 
464 /**
465  * pci_dev_get - increments the reference count of the pci device structure
466  * @dev: the device being referenced
467  *
468  * Each live reference to a device should be refcounted.
469  *
470  * Drivers for PCI devices should normally record such references in
471  * their probe() methods, when they bind to a device, and release
472  * them by calling pci_dev_put(), in their disconnect() methods.
473  *
474  * A pointer to the device with the incremented reference counter is returned.
475  */
476 struct pci_dev *pci_dev_get(struct pci_dev *dev)
477 {
478 	if (dev)
479 		get_device(&dev->dev);
480 	return dev;
481 }
482 
483 /**
484  * pci_dev_put - release a use of the pci device structure
485  * @dev: device that's been disconnected
486  *
487  * Must be called when a user of a device is finished with it.  When the last
488  * user of the device calls this function, the memory of the device is freed.
489  */
490 void pci_dev_put(struct pci_dev *dev)
491 {
492 	if (dev)
493 		put_device(&dev->dev);
494 }
495 
496 #ifndef CONFIG_HOTPLUG
497 int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
498 {
499 	return -ENODEV;
500 }
501 #endif
502 
503 struct bus_type pci_bus_type = {
504 	.name		= "pci",
505 	.match		= pci_bus_match,
506 	.uevent		= pci_uevent,
507 	.probe		= pci_device_probe,
508 	.remove		= pci_device_remove,
509 	.suspend	= pci_device_suspend,
510 	.suspend_late	= pci_device_suspend_late,
511 	.resume_early	= pci_device_resume_early,
512 	.resume		= pci_device_resume,
513 	.shutdown	= pci_device_shutdown,
514 	.dev_attrs	= pci_dev_attrs,
515 };
516 
517 static int __init pci_driver_init(void)
518 {
519 	return bus_register(&pci_bus_type);
520 }
521 
522 postcore_initcall(pci_driver_init);
523 
524 EXPORT_SYMBOL(pci_match_id);
525 EXPORT_SYMBOL(__pci_register_driver);
526 EXPORT_SYMBOL(pci_unregister_driver);
527 EXPORT_SYMBOL(pci_dev_driver);
528 EXPORT_SYMBOL(pci_bus_type);
529 EXPORT_SYMBOL(pci_dev_get);
530 EXPORT_SYMBOL(pci_dev_put);
531