[PATCH] PCI: prevent down_read when pci_devices is empty
[deliverable/linux.git] / drivers / pci / search.c
CommitLineData
1da177e4
LT
1/*
2 * PCI searching functions.
3 *
4 * Copyright (C) 1993 -- 1997 Drew Eckhardt, Frederic Potter,
5 * David Mosberger-Tang
6 * Copyright (C) 1997 -- 2000 Martin Mares <mj@ucw.cz>
7 * Copyright (C) 2003 -- 2004 Greg Kroah-Hartman <greg@kroah.com>
8 */
9
10#include <linux/init.h>
11#include <linux/pci.h>
12#include <linux/module.h>
13#include <linux/interrupt.h>
14#include "pci.h"
15
d71374da 16DECLARE_RWSEM(pci_bus_sem);
1da177e4
LT
17
18static struct pci_bus * __devinit
19pci_do_find_bus(struct pci_bus* bus, unsigned char busnr)
20{
21 struct pci_bus* child;
22 struct list_head *tmp;
23
24 if(bus->number == busnr)
25 return bus;
26
27 list_for_each(tmp, &bus->children) {
28 child = pci_do_find_bus(pci_bus_b(tmp), busnr);
29 if(child)
30 return child;
31 }
32 return NULL;
33}
34
35/**
36 * pci_find_bus - locate PCI bus from a given domain and bus number
37 * @domain: number of PCI domain to search
38 * @busnr: number of desired PCI bus
39 *
40 * Given a PCI bus number and domain number, the desired PCI bus is located
41 * in the global list of PCI buses. If the bus is found, a pointer to its
42 * data structure is returned. If no bus is found, %NULL is returned.
43 */
c8439cfc 44struct pci_bus * pci_find_bus(int domain, int busnr)
1da177e4
LT
45{
46 struct pci_bus *bus = NULL;
47 struct pci_bus *tmp_bus;
48
49 while ((bus = pci_find_next_bus(bus)) != NULL) {
50 if (pci_domain_nr(bus) != domain)
51 continue;
52 tmp_bus = pci_do_find_bus(bus, busnr);
53 if (tmp_bus)
54 return tmp_bus;
55 }
56 return NULL;
57}
58
59/**
60 * pci_find_next_bus - begin or continue searching for a PCI bus
61 * @from: Previous PCI bus found, or %NULL for new search.
62 *
63 * Iterates through the list of known PCI busses. A new search is
d75763d2 64 * initiated by passing %NULL as the @from argument. Otherwise if
1da177e4
LT
65 * @from is not %NULL, searches continue from next device on the
66 * global list.
67 */
68struct pci_bus *
69pci_find_next_bus(const struct pci_bus *from)
70{
71 struct list_head *n;
72 struct pci_bus *b = NULL;
73
74 WARN_ON(in_interrupt());
d71374da 75 down_read(&pci_bus_sem);
1da177e4
LT
76 n = from ? from->node.next : pci_root_buses.next;
77 if (n != &pci_root_buses)
78 b = pci_bus_b(n);
d71374da 79 up_read(&pci_bus_sem);
1da177e4
LT
80 return b;
81}
82
83/**
84 * pci_find_slot - locate PCI device from a given PCI slot
85 * @bus: number of PCI bus on which desired PCI device resides
86 * @devfn: encodes number of PCI slot in which the desired PCI
87 * device resides and the logical device number within that slot
88 * in case of multi-function devices.
89 *
90 * Given a PCI bus and slot/function number, the desired PCI device
91 * is located in system global list of PCI devices. If the device
92 * is found, a pointer to its data structure is returned. If no
93 * device is found, %NULL is returned.
94 */
95struct pci_dev *
96pci_find_slot(unsigned int bus, unsigned int devfn)
97{
98 struct pci_dev *dev = NULL;
99
100 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
101 if (dev->bus->number == bus && dev->devfn == devfn)
102 return dev;
103 }
104 return NULL;
105}
106
107/**
108 * pci_get_slot - locate PCI device for a given PCI slot
109 * @bus: PCI bus on which desired PCI device resides
110 * @devfn: encodes number of PCI slot in which the desired PCI
111 * device resides and the logical device number within that slot
112 * in case of multi-function devices.
113 *
114 * Given a PCI bus and slot/function number, the desired PCI device
115 * is located in the list of PCI devices.
116 * If the device is found, its reference count is increased and this
117 * function returns a pointer to its data structure. The caller must
118 * decrement the reference count by calling pci_dev_put().
119 * If no device is found, %NULL is returned.
120 */
121struct pci_dev * pci_get_slot(struct pci_bus *bus, unsigned int devfn)
122{
123 struct list_head *tmp;
124 struct pci_dev *dev;
125
126 WARN_ON(in_interrupt());
d71374da 127 down_read(&pci_bus_sem);
1da177e4
LT
128
129 list_for_each(tmp, &bus->devices) {
130 dev = pci_dev_b(tmp);
131 if (dev->devfn == devfn)
132 goto out;
133 }
134
135 dev = NULL;
136 out:
137 pci_dev_get(dev);
d71374da 138 up_read(&pci_bus_sem);
1da177e4
LT
139 return dev;
140}
141
29f3eb64
AC
142/**
143 * pci_get_bus_and_slot - locate PCI device from a given PCI slot
144 * @bus: number of PCI bus on which desired PCI device resides
145 * @devfn: encodes number of PCI slot in which the desired PCI
146 * device resides and the logical device number within that slot
147 * in case of multi-function devices.
148 *
149 * Given a PCI bus and slot/function number, the desired PCI device
150 * is located in system global list of PCI devices. If the device
151 * is found, a pointer to its data structure is returned. If no
152 * device is found, %NULL is returned. The returned device has its
153 * reference count bumped by one.
154 */
155
156struct pci_dev * pci_get_bus_and_slot(unsigned int bus, unsigned int devfn)
157{
158 struct pci_dev *dev = NULL;
159
160 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
161 if (dev->bus->number == bus && dev->devfn == devfn)
162 return dev;
163 }
164 return NULL;
165}
166
1da177e4
LT
167/**
168 * pci_find_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
169 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
170 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
171 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
172 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
173 * @from: Previous PCI device found in search, or %NULL for new search.
174 *
175 * Iterates through the list of known PCI devices. If a PCI device is
d75763d2
RD
176 * found with a matching @vendor, @device, @ss_vendor and @ss_device, a
177 * pointer to its device structure is returned. Otherwise, %NULL is returned.
178 * A new search is initiated by passing %NULL as the @from argument.
179 * Otherwise if @from is not %NULL, searches continue from next device
180 * on the global list.
1da177e4 181 *
d75763d2
RD
182 * NOTE: Do not use this function any more; use pci_get_subsys() instead, as
183 * the PCI device returned by this function can disappear at any moment in
1da177e4
LT
184 * time.
185 */
186static struct pci_dev * pci_find_subsys(unsigned int vendor,
187 unsigned int device,
188 unsigned int ss_vendor,
189 unsigned int ss_device,
190 const struct pci_dev *from)
191{
192 struct list_head *n;
193 struct pci_dev *dev;
194
195 WARN_ON(in_interrupt());
6ae4adf5
AB
196
197 /*
198 * pci_find_subsys() can be called on the ide_setup() path, super-early
199 * in boot. But the down_read() will enable local interrupts, which
200 * can cause some machines to crash. So here we detect and flag that
201 * situation and bail out early.
202 */
203 if (unlikely(list_empty(&pci_devices))) {
204 printk(KERN_INFO "pci_find_subsys() called while pci_devices "
205 "is still empty\n");
206 return NULL;
207 }
d71374da 208 down_read(&pci_bus_sem);
1da177e4
LT
209 n = from ? from->global_list.next : pci_devices.next;
210
211 while (n && (n != &pci_devices)) {
212 dev = pci_dev_g(n);
213 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
214 (device == PCI_ANY_ID || dev->device == device) &&
215 (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
216 (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
217 goto exit;
218 n = n->next;
219 }
220 dev = NULL;
221exit:
d71374da 222 up_read(&pci_bus_sem);
1da177e4
LT
223 return dev;
224}
225
226/**
227 * pci_find_device - begin or continue searching for a PCI device by vendor/device id
228 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
229 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
230 * @from: Previous PCI device found in search, or %NULL for new search.
231 *
d75763d2
RD
232 * Iterates through the list of known PCI devices. If a PCI device is found
233 * with a matching @vendor and @device, a pointer to its device structure is
1da177e4 234 * returned. Otherwise, %NULL is returned.
d75763d2
RD
235 * A new search is initiated by passing %NULL as the @from argument.
236 * Otherwise if @from is not %NULL, searches continue from next device
237 * on the global list.
1da177e4 238 *
d75763d2
RD
239 * NOTE: Do not use this function any more; use pci_get_device() instead, as
240 * the PCI device returned by this function can disappear at any moment in
1da177e4
LT
241 * time.
242 */
243struct pci_dev *
244pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev *from)
245{
246 return pci_find_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
247}
248
249/**
250 * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
251 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
252 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
253 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
254 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
255 * @from: Previous PCI device found in search, or %NULL for new search.
256 *
d75763d2
RD
257 * Iterates through the list of known PCI devices. If a PCI device is found
258 * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
1da177e4
LT
259 * device structure is returned, and the reference count to the device is
260 * incremented. Otherwise, %NULL is returned. A new search is initiated by
d75763d2 261 * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
1da177e4
LT
262 * searches continue from next device on the global list.
263 * The reference count for @from is always decremented if it is not %NULL.
264 */
265struct pci_dev *
266pci_get_subsys(unsigned int vendor, unsigned int device,
267 unsigned int ss_vendor, unsigned int ss_device,
268 struct pci_dev *from)
269{
270 struct list_head *n;
271 struct pci_dev *dev;
272
273 WARN_ON(in_interrupt());
6ae4adf5
AB
274
275 /*
276 * pci_get_subsys() can potentially be called by drivers super-early
277 * in boot. But the down_read() will enable local interrupts, which
278 * can cause some machines to crash. So here we detect and flag that
279 * situation and bail out early.
280 */
281 if (unlikely(list_empty(&pci_devices))) {
282 printk(KERN_NOTICE "pci_get_subsys() called while pci_devices "
283 "is still empty\n");
284 return NULL;
285 }
d71374da 286 down_read(&pci_bus_sem);
1da177e4
LT
287 n = from ? from->global_list.next : pci_devices.next;
288
289 while (n && (n != &pci_devices)) {
290 dev = pci_dev_g(n);
291 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
292 (device == PCI_ANY_ID || dev->device == device) &&
293 (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
294 (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
295 goto exit;
296 n = n->next;
297 }
298 dev = NULL;
299exit:
1da177e4 300 dev = pci_dev_get(dev);
d71374da 301 up_read(&pci_bus_sem);
b89b7ea0 302 pci_dev_put(from);
1da177e4
LT
303 return dev;
304}
305
306/**
307 * pci_get_device - begin or continue searching for a PCI device by vendor/device id
308 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
309 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
310 * @from: Previous PCI device found in search, or %NULL for new search.
311 *
312 * Iterates through the list of known PCI devices. If a PCI device is
313 * found with a matching @vendor and @device, the reference count to the
314 * device is incremented and a pointer to its device structure is returned.
315 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
d75763d2 316 * as the @from argument. Otherwise if @from is not %NULL, searches continue
1da177e4
LT
317 * from next device on the global list. The reference count for @from is
318 * always decremented if it is not %NULL.
319 */
320struct pci_dev *
321pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
322{
323 return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
324}
325
29f3eb64
AC
326/**
327 * pci_get_device_reverse - begin or continue searching for a PCI device by vendor/device id
328 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
329 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
330 * @from: Previous PCI device found in search, or %NULL for new search.
331 *
332 * Iterates through the list of known PCI devices in the reverse order of
333 * pci_get_device.
334 * If a PCI device is found with a matching @vendor and @device, the reference
335 * count to the device is incremented and a pointer to its device structure
336 * is returned Otherwise, %NULL is returned. A new search is initiated by
337 * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
338 * searches continue from next device on the global list. The reference
339 * count for @from is always decremented if it is not %NULL.
340 */
341struct pci_dev *
342pci_get_device_reverse(unsigned int vendor, unsigned int device, struct pci_dev *from)
343{
344 struct list_head *n;
345 struct pci_dev *dev;
346
347 WARN_ON(in_interrupt());
348 down_read(&pci_bus_sem);
349 n = from ? from->global_list.prev : pci_devices.prev;
350
351 while (n && (n != &pci_devices)) {
352 dev = pci_dev_g(n);
353 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
354 (device == PCI_ANY_ID || dev->device == device))
355 goto exit;
356 n = n->prev;
357 }
358 dev = NULL;
359exit:
360 dev = pci_dev_get(dev);
361 up_read(&pci_bus_sem);
362 pci_dev_put(from);
363 return dev;
364}
1da177e4
LT
365
366/**
367 * pci_find_device_reverse - begin or continue searching for a PCI device by vendor/device id
368 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
369 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
370 * @from: Previous PCI device found in search, or %NULL for new search.
371 *
d75763d2
RD
372 * Iterates through the list of known PCI devices in the reverse order of
373 * pci_find_device().
1da177e4
LT
374 * If a PCI device is found with a matching @vendor and @device, a pointer to
375 * its device structure is returned. Otherwise, %NULL is returned.
d75763d2
RD
376 * A new search is initiated by passing %NULL as the @from argument.
377 * Otherwise if @from is not %NULL, searches continue from previous device
378 * on the global list.
1da177e4
LT
379 */
380struct pci_dev *
381pci_find_device_reverse(unsigned int vendor, unsigned int device, const struct pci_dev *from)
382{
383 struct list_head *n;
384 struct pci_dev *dev;
385
386 WARN_ON(in_interrupt());
d71374da 387 down_read(&pci_bus_sem);
1da177e4
LT
388 n = from ? from->global_list.prev : pci_devices.prev;
389
390 while (n && (n != &pci_devices)) {
391 dev = pci_dev_g(n);
392 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
393 (device == PCI_ANY_ID || dev->device == device))
394 goto exit;
395 n = n->prev;
396 }
397 dev = NULL;
398exit:
d71374da 399 up_read(&pci_bus_sem);
1da177e4
LT
400 return dev;
401}
402
403/**
404 * pci_get_class - begin or continue searching for a PCI device by class
405 * @class: search for a PCI device with this class designation
406 * @from: Previous PCI device found in search, or %NULL for new search.
407 *
408 * Iterates through the list of known PCI devices. If a PCI device is
409 * found with a matching @class, the reference count to the device is
410 * incremented and a pointer to its device structure is returned.
411 * Otherwise, %NULL is returned.
d75763d2 412 * A new search is initiated by passing %NULL as the @from argument.
1da177e4
LT
413 * Otherwise if @from is not %NULL, searches continue from next device
414 * on the global list. The reference count for @from is always decremented
415 * if it is not %NULL.
416 */
417struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
418{
419 struct list_head *n;
420 struct pci_dev *dev;
421
422 WARN_ON(in_interrupt());
d71374da 423 down_read(&pci_bus_sem);
1da177e4
LT
424 n = from ? from->global_list.next : pci_devices.next;
425
426 while (n && (n != &pci_devices)) {
427 dev = pci_dev_g(n);
428 if (dev->class == class)
429 goto exit;
430 n = n->next;
431 }
432 dev = NULL;
433exit:
1da177e4 434 dev = pci_dev_get(dev);
d71374da 435 up_read(&pci_bus_sem);
b89b7ea0 436 pci_dev_put(from);
1da177e4
LT
437 return dev;
438}
439
d86f90f9
AC
440const struct pci_device_id *pci_find_present(const struct pci_device_id *ids)
441{
442 struct pci_dev *dev;
1597cacb 443 const struct pci_device_id *found = NULL;
d86f90f9
AC
444
445 WARN_ON(in_interrupt());
446 down_read(&pci_bus_sem);
447 while (ids->vendor || ids->subvendor || ids->class_mask) {
448 list_for_each_entry(dev, &pci_devices, global_list) {
449 if ((found = pci_match_one_device(ids, dev)) != NULL)
450 break;
451 }
452 ids++;
453 }
454 up_read(&pci_bus_sem);
455 return found;
456}
457
1da177e4
LT
458/**
459 * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
460 * @ids: A pointer to a null terminated list of struct pci_device_id structures
461 * that describe the type of PCI device the caller is trying to find.
462 *
463 * Obvious fact: You do not have a reference to any device that might be found
464 * by this function, so if that device is removed from the system right after
465 * this function is finished, the value will be stale. Use this function to
466 * find devices that are usually built into a system, or for a general hint as
467 * to if another device happens to be present at this specific moment in time.
468 */
469int pci_dev_present(const struct pci_device_id *ids)
470{
d86f90f9 471 return pci_find_present(ids) == NULL ? 0 : 1;
1da177e4 472}
d86f90f9 473
1da177e4 474EXPORT_SYMBOL(pci_dev_present);
d86f90f9 475EXPORT_SYMBOL(pci_find_present);
1da177e4 476
1da177e4
LT
477EXPORT_SYMBOL(pci_find_device);
478EXPORT_SYMBOL(pci_find_device_reverse);
479EXPORT_SYMBOL(pci_find_slot);
29f3eb64
AC
480/* For boot time work */
481EXPORT_SYMBOL(pci_find_bus);
482EXPORT_SYMBOL(pci_find_next_bus);
483/* For everyone */
1da177e4 484EXPORT_SYMBOL(pci_get_device);
29f3eb64 485EXPORT_SYMBOL(pci_get_device_reverse);
1da177e4
LT
486EXPORT_SYMBOL(pci_get_subsys);
487EXPORT_SYMBOL(pci_get_slot);
29f3eb64 488EXPORT_SYMBOL(pci_get_bus_and_slot);
1da177e4 489EXPORT_SYMBOL(pci_get_class);
This page took 0.20476 seconds and 5 git commands to generate.