search.c 16 KB

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