blob: c6e79d01ce3d1a8e4f747a762f8e052712320102 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Zhang Yanmind71374d2006-06-02 12:35:43 +080016DECLARE_RWSEM(pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Sam Ravnborg96bde062007-03-26 21:53:30 -080018static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070019{
20 struct pci_bus* child;
21 struct list_head *tmp;
22
23 if(bus->number == busnr)
24 return bus;
25
26 list_for_each(tmp, &bus->children) {
27 child = pci_do_find_bus(pci_bus_b(tmp), busnr);
28 if(child)
29 return child;
30 }
31 return NULL;
32}
33
34/**
35 * pci_find_bus - locate PCI bus from a given domain and bus number
36 * @domain: number of PCI domain to search
37 * @busnr: number of desired PCI bus
38 *
39 * Given a PCI bus number and domain number, the desired PCI bus is located
40 * in the global list of PCI buses. If the bus is found, a pointer to its
41 * data structure is returned. If no bus is found, %NULL is returned.
42 */
Randy Dunlapc8439cfc2006-07-18 14:33:16 -070043struct pci_bus * pci_find_bus(int domain, int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 struct pci_bus *bus = NULL;
46 struct pci_bus *tmp_bus;
47
48 while ((bus = pci_find_next_bus(bus)) != NULL) {
49 if (pci_domain_nr(bus) != domain)
50 continue;
51 tmp_bus = pci_do_find_bus(bus, busnr);
52 if (tmp_bus)
53 return tmp_bus;
54 }
55 return NULL;
56}
57
58/**
59 * pci_find_next_bus - begin or continue searching for a PCI bus
60 * @from: Previous PCI bus found, or %NULL for new search.
61 *
62 * Iterates through the list of known PCI busses. A new search is
Randy Dunlapd75763d2006-07-30 03:03:41 -070063 * initiated by passing %NULL as the @from argument. Otherwise if
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 * @from is not %NULL, searches continue from next device on the
65 * global list.
66 */
67struct pci_bus *
68pci_find_next_bus(const struct pci_bus *from)
69{
70 struct list_head *n;
71 struct pci_bus *b = NULL;
72
73 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +080074 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 n = from ? from->node.next : pci_root_buses.next;
76 if (n != &pci_root_buses)
77 b = pci_bus_b(n);
Zhang Yanmind71374d2006-06-02 12:35:43 +080078 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 return b;
80}
81
82/**
83 * pci_find_slot - locate PCI device from a given PCI slot
84 * @bus: number of PCI bus on which desired PCI device resides
85 * @devfn: encodes number of PCI slot in which the desired PCI
86 * device resides and the logical device number within that slot
87 * in case of multi-function devices.
88 *
89 * Given a PCI bus and slot/function number, the desired PCI device
90 * is located in system global list of PCI devices. If the device
91 * is found, a pointer to its data structure is returned. If no
92 * device is found, %NULL is returned.
93 */
94struct pci_dev *
95pci_find_slot(unsigned int bus, unsigned int devfn)
96{
97 struct pci_dev *dev = NULL;
98
99 while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
100 if (dev->bus->number == bus && dev->devfn == devfn)
101 return dev;
102 }
103 return NULL;
104}
105
106/**
107 * pci_get_slot - locate PCI device for a given PCI slot
108 * @bus: PCI bus on which desired PCI device resides
109 * @devfn: encodes number of PCI slot in which the desired PCI
110 * device resides and the logical device number within that slot
111 * in case of multi-function devices.
112 *
113 * Given a PCI bus and slot/function number, the desired PCI device
114 * is located in the list of PCI devices.
115 * If the device is found, its reference count is increased and this
116 * function returns a pointer to its data structure. The caller must
117 * decrement the reference count by calling pci_dev_put().
118 * If no device is found, %NULL is returned.
119 */
120struct pci_dev * pci_get_slot(struct pci_bus *bus, unsigned int devfn)
121{
122 struct list_head *tmp;
123 struct pci_dev *dev;
124
125 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800126 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 list_for_each(tmp, &bus->devices) {
129 dev = pci_dev_b(tmp);
130 if (dev->devfn == devfn)
131 goto out;
132 }
133
134 dev = NULL;
135 out:
136 pci_dev_get(dev);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800137 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 return dev;
139}
140
141/**
Randy Dunlap5463d9f2007-06-28 16:04:21 -0700142 * pci_get_bus_and_slot - locate PCI device from a given PCI bus & slot
Alan Cox29f3eb62006-10-16 16:20:21 -0700143 * @bus: number of PCI bus on which desired PCI device resides
144 * @devfn: encodes number of PCI slot in which the desired PCI
145 * device resides and the logical device number within that slot
146 * in case of multi-function devices.
147 *
Randy Dunlap5463d9f2007-06-28 16:04:21 -0700148 * Note: the bus/slot search is limited to PCI domain (segment) 0.
149 *
Alan Cox29f3eb62006-10-16 16:20:21 -0700150 * Given a PCI bus and slot/function number, the desired PCI device
151 * is located in system global list of PCI devices. If the device
152 * is found, a pointer to its data structure is returned. If no
153 * device is found, %NULL is returned. The returned device has its
154 * reference count bumped by one.
155 */
156
157struct pci_dev * pci_get_bus_and_slot(unsigned int bus, unsigned int devfn)
158{
159 struct pci_dev *dev = NULL;
160
161 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
Randy Dunlap5463d9f2007-06-28 16:04:21 -0700162 if (pci_domain_nr(dev->bus) == 0 &&
163 (dev->bus->number == bus && dev->devfn == devfn))
Alan Cox29f3eb62006-10-16 16:20:21 -0700164 return dev;
165 }
166 return NULL;
167}
168
169/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * pci_find_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
171 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
172 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
173 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
174 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
175 * @from: Previous PCI device found in search, or %NULL for new search.
176 *
177 * Iterates through the list of known PCI devices. If a PCI device is
Randy Dunlapd75763d2006-07-30 03:03:41 -0700178 * found with a matching @vendor, @device, @ss_vendor and @ss_device, a
179 * pointer to its device structure is returned. Otherwise, %NULL is returned.
180 * A new search is initiated by passing %NULL as the @from argument.
181 * Otherwise if @from is not %NULL, searches continue from next device
182 * on the global list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700184 * NOTE: Do not use this function any more; use pci_get_subsys() instead, as
185 * the PCI device returned by this function can disappear at any moment in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 * time.
187 */
188static struct pci_dev * pci_find_subsys(unsigned int vendor,
189 unsigned int device,
190 unsigned int ss_vendor,
191 unsigned int ss_device,
192 const struct pci_dev *from)
193{
194 struct list_head *n;
195 struct pci_dev *dev;
196
197 WARN_ON(in_interrupt());
Ard van Breemen6ae4adf2007-01-05 16:36:21 -0800198
199 /*
200 * pci_find_subsys() can be called on the ide_setup() path, super-early
201 * in boot. But the down_read() will enable local interrupts, which
202 * can cause some machines to crash. So here we detect and flag that
203 * situation and bail out early.
204 */
Andrew Morton35ef63f2007-07-15 23:39:40 -0700205 if (unlikely(no_pci_devices()))
Ard van Breemen6ae4adf2007-01-05 16:36:21 -0800206 return NULL;
Zhang Yanmind71374d2006-06-02 12:35:43 +0800207 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 n = from ? from->global_list.next : pci_devices.next;
209
210 while (n && (n != &pci_devices)) {
211 dev = pci_dev_g(n);
212 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
213 (device == PCI_ANY_ID || dev->device == device) &&
214 (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
215 (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
216 goto exit;
217 n = n->next;
218 }
219 dev = NULL;
220exit:
Zhang Yanmind71374d2006-06-02 12:35:43 +0800221 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return dev;
223}
224
225/**
226 * pci_find_device - begin or continue searching for a PCI device by vendor/device id
227 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
228 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
229 * @from: Previous PCI device found in search, or %NULL for new search.
230 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700231 * Iterates through the list of known PCI devices. If a PCI device is found
232 * with a matching @vendor and @device, a pointer to its device structure is
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 * returned. Otherwise, %NULL is returned.
Randy Dunlapd75763d2006-07-30 03:03:41 -0700234 * A new search is initiated by passing %NULL as the @from argument.
235 * Otherwise if @from is not %NULL, searches continue from next device
236 * on the global list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700238 * NOTE: Do not use this function any more; use pci_get_device() instead, as
239 * the PCI device returned by this function can disappear at any moment in
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 * time.
241 */
242struct pci_dev *
243pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev *from)
244{
245 return pci_find_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
246}
247
248/**
249 * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
250 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
251 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
252 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
253 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
254 * @from: Previous PCI device found in search, or %NULL for new search.
255 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700256 * Iterates through the list of known PCI devices. If a PCI device is found
257 * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 * device structure is returned, and the reference count to the device is
259 * incremented. Otherwise, %NULL is returned. A new search is initiated by
Randy Dunlapd75763d2006-07-30 03:03:41 -0700260 * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 * searches continue from next device on the global list.
262 * The reference count for @from is always decremented if it is not %NULL.
263 */
264struct pci_dev *
265pci_get_subsys(unsigned int vendor, unsigned int device,
266 unsigned int ss_vendor, unsigned int ss_device,
267 struct pci_dev *from)
268{
269 struct list_head *n;
270 struct pci_dev *dev;
271
272 WARN_ON(in_interrupt());
Ard van Breemen6ae4adf2007-01-05 16:36:21 -0800273
274 /*
275 * pci_get_subsys() can potentially be called by drivers super-early
276 * in boot. But the down_read() will enable local interrupts, which
277 * can cause some machines to crash. So here we detect and flag that
278 * situation and bail out early.
279 */
Andrew Morton35ef63f2007-07-15 23:39:40 -0700280 if (unlikely(no_pci_devices()))
Ard van Breemen6ae4adf2007-01-05 16:36:21 -0800281 return NULL;
Zhang Yanmind71374d2006-06-02 12:35:43 +0800282 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 n = from ? from->global_list.next : pci_devices.next;
284
285 while (n && (n != &pci_devices)) {
286 dev = pci_dev_g(n);
287 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
288 (device == PCI_ANY_ID || dev->device == device) &&
289 (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
290 (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
291 goto exit;
292 n = n->next;
293 }
294 dev = NULL;
295exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 dev = pci_dev_get(dev);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800297 up_read(&pci_bus_sem);
Alan Sternb89b7ea2006-02-23 17:12:51 -0500298 pci_dev_put(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return dev;
300}
301
302/**
303 * pci_get_device - begin or continue searching for a PCI device by vendor/device id
304 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
305 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
306 * @from: Previous PCI device found in search, or %NULL for new search.
307 *
308 * Iterates through the list of known PCI devices. If a PCI device is
309 * found with a matching @vendor and @device, the reference count to the
310 * device is incremented and a pointer to its device structure is returned.
311 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
Randy Dunlapd75763d2006-07-30 03:03:41 -0700312 * as the @from argument. Otherwise if @from is not %NULL, searches continue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * from next device on the global list. The reference count for @from is
314 * always decremented if it is not %NULL.
315 */
316struct pci_dev *
317pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
318{
319 return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
320}
321
Alan Cox29f3eb62006-10-16 16:20:21 -0700322/**
323 * pci_get_device_reverse - begin or continue searching for a PCI device by vendor/device id
324 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
325 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
326 * @from: Previous PCI device found in search, or %NULL for new search.
327 *
328 * Iterates through the list of known PCI devices in the reverse order of
329 * pci_get_device.
330 * If a PCI device is found with a matching @vendor and @device, the reference
331 * count to the device is incremented and a pointer to its device structure
332 * is returned Otherwise, %NULL is returned. A new search is initiated by
333 * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
334 * searches continue from next device on the global list. The reference
335 * count for @from is always decremented if it is not %NULL.
336 */
337struct pci_dev *
338pci_get_device_reverse(unsigned int vendor, unsigned int device, struct pci_dev *from)
339{
340 struct list_head *n;
341 struct pci_dev *dev;
342
343 WARN_ON(in_interrupt());
344 down_read(&pci_bus_sem);
345 n = from ? from->global_list.prev : pci_devices.prev;
346
347 while (n && (n != &pci_devices)) {
348 dev = pci_dev_g(n);
349 if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
350 (device == PCI_ANY_ID || dev->device == device))
351 goto exit;
352 n = n->prev;
353 }
354 dev = NULL;
355exit:
356 dev = pci_dev_get(dev);
357 up_read(&pci_bus_sem);
358 pci_dev_put(from);
359 return dev;
360}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 * pci_get_class - begin or continue searching for a PCI device by class
364 * @class: search for a PCI device with this class designation
365 * @from: Previous PCI device found in search, or %NULL for new search.
366 *
367 * Iterates through the list of known PCI devices. If a PCI device is
368 * found with a matching @class, the reference count to the device is
369 * incremented and a pointer to its device structure is returned.
370 * Otherwise, %NULL is returned.
Randy Dunlapd75763d2006-07-30 03:03:41 -0700371 * A new search is initiated by passing %NULL as the @from argument.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * Otherwise if @from is not %NULL, searches continue from next device
373 * on the global list. The reference count for @from is always decremented
374 * if it is not %NULL.
375 */
376struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
377{
378 struct list_head *n;
379 struct pci_dev *dev;
380
381 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800382 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 n = from ? from->global_list.next : pci_devices.next;
384
385 while (n && (n != &pci_devices)) {
386 dev = pci_dev_g(n);
387 if (dev->class == class)
388 goto exit;
389 n = n->next;
390 }
391 dev = NULL;
392exit:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 dev = pci_dev_get(dev);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800394 up_read(&pci_bus_sem);
Alan Sternb89b7ea2006-02-23 17:12:51 -0500395 pci_dev_put(from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return dev;
397}
398
Alan Coxd86f90f2006-12-04 15:14:44 -0800399const struct pci_device_id *pci_find_present(const struct pci_device_id *ids)
400{
401 struct pci_dev *dev;
Alan Cox1597cac2006-12-04 15:14:45 -0800402 const struct pci_device_id *found = NULL;
Alan Coxd86f90f2006-12-04 15:14:44 -0800403
404 WARN_ON(in_interrupt());
405 down_read(&pci_bus_sem);
406 while (ids->vendor || ids->subvendor || ids->class_mask) {
407 list_for_each_entry(dev, &pci_devices, global_list) {
408 if ((found = pci_match_one_device(ids, dev)) != NULL)
Ben Gardner3c92c572007-05-10 22:58:58 -0700409 goto exit;
Alan Coxd86f90f2006-12-04 15:14:44 -0800410 }
411 ids++;
412 }
Ben Gardner3c92c572007-05-10 22:58:58 -0700413exit:
Alan Coxd86f90f2006-12-04 15:14:44 -0800414 up_read(&pci_bus_sem);
415 return found;
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/**
419 * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
420 * @ids: A pointer to a null terminated list of struct pci_device_id structures
421 * that describe the type of PCI device the caller is trying to find.
422 *
423 * Obvious fact: You do not have a reference to any device that might be found
424 * by this function, so if that device is removed from the system right after
425 * this function is finished, the value will be stale. Use this function to
426 * find devices that are usually built into a system, or for a general hint as
427 * to if another device happens to be present at this specific moment in time.
428 */
429int pci_dev_present(const struct pci_device_id *ids)
430{
Alan Coxd86f90f2006-12-04 15:14:44 -0800431 return pci_find_present(ids) == NULL ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
Alan Coxd86f90f2006-12-04 15:14:44 -0800433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434EXPORT_SYMBOL(pci_dev_present);
Alan Coxd86f90f2006-12-04 15:14:44 -0800435EXPORT_SYMBOL(pci_find_present);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437EXPORT_SYMBOL(pci_find_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438EXPORT_SYMBOL(pci_find_slot);
Alan Cox29f3eb62006-10-16 16:20:21 -0700439/* For boot time work */
440EXPORT_SYMBOL(pci_find_bus);
441EXPORT_SYMBOL(pci_find_next_bus);
442/* For everyone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443EXPORT_SYMBOL(pci_get_device);
Alan Cox29f3eb62006-10-16 16:20:21 -0700444EXPORT_SYMBOL(pci_get_device_reverse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445EXPORT_SYMBOL(pci_get_subsys);
446EXPORT_SYMBOL(pci_get_slot);
Alan Cox29f3eb62006-10-16 16:20:21 -0700447EXPORT_SYMBOL(pci_get_bus_and_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448EXPORT_SYMBOL(pci_get_class);