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