search.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 *
  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. return NULL;
  184. down_read(&pci_bus_sem);
  185. n = from ? from->global_list.next : pci_devices.next;
  186. while (n && (n != &pci_devices)) {
  187. dev = pci_dev_g(n);
  188. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  189. (device == PCI_ANY_ID || dev->device == device) &&
  190. (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
  191. (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
  192. goto exit;
  193. n = n->next;
  194. }
  195. dev = NULL;
  196. exit:
  197. up_read(&pci_bus_sem);
  198. return dev;
  199. }
  200. /**
  201. * pci_find_device - begin or continue searching for a PCI device by vendor/device id
  202. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  203. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  204. * @from: Previous PCI device found in search, or %NULL for new search.
  205. *
  206. * Iterates through the list of known PCI devices. If a PCI device is found
  207. * with a matching @vendor and @device, a pointer to its device structure is
  208. * returned. Otherwise, %NULL is returned.
  209. * A new search is initiated by passing %NULL as the @from argument.
  210. * Otherwise if @from is not %NULL, searches continue from next device
  211. * on the global list.
  212. *
  213. * NOTE: Do not use this function any more; use pci_get_device() instead, as
  214. * the PCI device returned by this function can disappear at any moment in
  215. * time.
  216. */
  217. struct pci_dev *
  218. pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev *from)
  219. {
  220. return pci_find_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  221. }
  222. /**
  223. * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  224. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  225. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  226. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  227. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  228. * @from: Previous PCI device found in search, or %NULL for new search.
  229. *
  230. * Iterates through the list of known PCI devices. If a PCI device is found
  231. * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  232. * device structure is returned, and the reference count to the device is
  233. * incremented. Otherwise, %NULL is returned. A new search is initiated by
  234. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  235. * searches continue from next device on the global list.
  236. * The reference count for @from is always decremented if it is not %NULL.
  237. */
  238. struct pci_dev *
  239. pci_get_subsys(unsigned int vendor, unsigned int device,
  240. unsigned int ss_vendor, unsigned int ss_device,
  241. struct pci_dev *from)
  242. {
  243. struct list_head *n;
  244. struct pci_dev *dev;
  245. WARN_ON(in_interrupt());
  246. /*
  247. * pci_get_subsys() can potentially be called by drivers super-early
  248. * in boot. But the down_read() will enable local interrupts, which
  249. * can cause some machines to crash. So here we detect and flag that
  250. * situation and bail out early.
  251. */
  252. if (unlikely(list_empty(&pci_devices)))
  253. return NULL;
  254. down_read(&pci_bus_sem);
  255. n = from ? from->global_list.next : pci_devices.next;
  256. while (n && (n != &pci_devices)) {
  257. dev = pci_dev_g(n);
  258. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  259. (device == PCI_ANY_ID || dev->device == device) &&
  260. (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) &&
  261. (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device))
  262. goto exit;
  263. n = n->next;
  264. }
  265. dev = NULL;
  266. exit:
  267. dev = pci_dev_get(dev);
  268. up_read(&pci_bus_sem);
  269. pci_dev_put(from);
  270. return dev;
  271. }
  272. /**
  273. * pci_get_device - begin or continue searching for a PCI device by vendor/device id
  274. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  275. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  276. * @from: Previous PCI device found in search, or %NULL for new search.
  277. *
  278. * Iterates through the list of known PCI devices. If a PCI device is
  279. * found with a matching @vendor and @device, the reference count to the
  280. * device is incremented and a pointer to its device structure is returned.
  281. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  282. * as the @from argument. Otherwise if @from is not %NULL, searches continue
  283. * from next device on the global list. The reference count for @from is
  284. * always decremented if it is not %NULL.
  285. */
  286. struct pci_dev *
  287. pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
  288. {
  289. return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  290. }
  291. /**
  292. * pci_get_device_reverse - begin or continue searching for a PCI device by vendor/device id
  293. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  294. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  295. * @from: Previous PCI device found in search, or %NULL for new search.
  296. *
  297. * Iterates through the list of known PCI devices in the reverse order of
  298. * pci_get_device.
  299. * If a PCI device is found with a matching @vendor and @device, the reference
  300. * count to the device is incremented and a pointer to its device structure
  301. * is returned Otherwise, %NULL is returned. A new search is initiated by
  302. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  303. * searches continue from next device on the global list. The reference
  304. * count for @from is always decremented if it is not %NULL.
  305. */
  306. struct pci_dev *
  307. pci_get_device_reverse(unsigned int vendor, unsigned int device, struct pci_dev *from)
  308. {
  309. struct list_head *n;
  310. struct pci_dev *dev;
  311. WARN_ON(in_interrupt());
  312. down_read(&pci_bus_sem);
  313. n = from ? from->global_list.prev : pci_devices.prev;
  314. while (n && (n != &pci_devices)) {
  315. dev = pci_dev_g(n);
  316. if ((vendor == PCI_ANY_ID || dev->vendor == vendor) &&
  317. (device == PCI_ANY_ID || dev->device == device))
  318. goto exit;
  319. n = n->prev;
  320. }
  321. dev = NULL;
  322. exit:
  323. dev = pci_dev_get(dev);
  324. up_read(&pci_bus_sem);
  325. pci_dev_put(from);
  326. return dev;
  327. }
  328. /**
  329. * pci_get_class - begin or continue searching for a PCI device by class
  330. * @class: search for a PCI device with this class designation
  331. * @from: Previous PCI device found in search, or %NULL for new search.
  332. *
  333. * Iterates through the list of known PCI devices. If a PCI device is
  334. * found with a matching @class, the reference count to the device is
  335. * incremented and a pointer to its device structure is returned.
  336. * Otherwise, %NULL is returned.
  337. * A new search is initiated by passing %NULL as the @from argument.
  338. * Otherwise if @from is not %NULL, searches continue from next device
  339. * on the global list. The reference count for @from is always decremented
  340. * if it is not %NULL.
  341. */
  342. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  343. {
  344. struct list_head *n;
  345. struct pci_dev *dev;
  346. WARN_ON(in_interrupt());
  347. down_read(&pci_bus_sem);
  348. n = from ? from->global_list.next : pci_devices.next;
  349. while (n && (n != &pci_devices)) {
  350. dev = pci_dev_g(n);
  351. if (dev->class == class)
  352. goto exit;
  353. n = n->next;
  354. }
  355. dev = NULL;
  356. exit:
  357. dev = pci_dev_get(dev);
  358. up_read(&pci_bus_sem);
  359. pci_dev_put(from);
  360. return dev;
  361. }
  362. const struct pci_device_id *pci_find_present(const struct pci_device_id *ids)
  363. {
  364. struct pci_dev *dev;
  365. const struct pci_device_id *found = NULL;
  366. WARN_ON(in_interrupt());
  367. down_read(&pci_bus_sem);
  368. while (ids->vendor || ids->subvendor || ids->class_mask) {
  369. list_for_each_entry(dev, &pci_devices, global_list) {
  370. if ((found = pci_match_one_device(ids, dev)) != NULL)
  371. break;
  372. }
  373. ids++;
  374. }
  375. up_read(&pci_bus_sem);
  376. return found;
  377. }
  378. /**
  379. * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
  380. * @ids: A pointer to a null terminated list of struct pci_device_id structures
  381. * that describe the type of PCI device the caller is trying to find.
  382. *
  383. * Obvious fact: You do not have a reference to any device that might be found
  384. * by this function, so if that device is removed from the system right after
  385. * this function is finished, the value will be stale. Use this function to
  386. * find devices that are usually built into a system, or for a general hint as
  387. * to if another device happens to be present at this specific moment in time.
  388. */
  389. int pci_dev_present(const struct pci_device_id *ids)
  390. {
  391. return pci_find_present(ids) == NULL ? 0 : 1;
  392. }
  393. EXPORT_SYMBOL(pci_dev_present);
  394. EXPORT_SYMBOL(pci_find_present);
  395. EXPORT_SYMBOL(pci_find_device);
  396. EXPORT_SYMBOL(pci_find_slot);
  397. /* For boot time work */
  398. EXPORT_SYMBOL(pci_find_bus);
  399. EXPORT_SYMBOL(pci_find_next_bus);
  400. /* For everyone */
  401. EXPORT_SYMBOL(pci_get_device);
  402. EXPORT_SYMBOL(pci_get_device_reverse);
  403. EXPORT_SYMBOL(pci_get_subsys);
  404. EXPORT_SYMBOL(pci_get_slot);
  405. EXPORT_SYMBOL(pci_get_bus_and_slot);
  406. EXPORT_SYMBOL(pci_get_class);