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