search.c 16 KB

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