search.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/interrupt.h>
  14. #include "pci.h"
  15. DECLARE_RWSEM(pci_bus_sem);
  16. EXPORT_SYMBOL_GPL(pci_bus_sem);
  17. /*
  18. * find the upstream PCIe-to-PCI bridge of a PCI device
  19. * if the device is PCIE, return NULL
  20. * if the device isn't connected to a PCIe bridge (that is its parent is a
  21. * legacy PCI bridge and the bridge is directly connected to bus 0), return its
  22. * parent
  23. */
  24. struct pci_dev *
  25. pci_find_upstream_pcie_bridge(struct pci_dev *pdev)
  26. {
  27. struct pci_dev *tmp = NULL;
  28. if (pci_is_pcie(pdev))
  29. return NULL;
  30. while (1) {
  31. if (pci_is_root_bus(pdev->bus))
  32. break;
  33. pdev = pdev->bus->self;
  34. /* a p2p bridge */
  35. if (!pci_is_pcie(pdev)) {
  36. tmp = pdev;
  37. continue;
  38. }
  39. /* PCI device should connect to a PCIe bridge */
  40. if (pdev->pcie_type != PCI_EXP_TYPE_PCI_BRIDGE) {
  41. /* Busted hardware? */
  42. WARN_ON_ONCE(1);
  43. return NULL;
  44. }
  45. return pdev;
  46. }
  47. return tmp;
  48. }
  49. static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
  50. {
  51. struct pci_bus* child;
  52. struct list_head *tmp;
  53. if(bus->number == busnr)
  54. return bus;
  55. list_for_each(tmp, &bus->children) {
  56. child = pci_do_find_bus(pci_bus_b(tmp), busnr);
  57. if(child)
  58. return child;
  59. }
  60. return NULL;
  61. }
  62. /**
  63. * pci_find_bus - locate PCI bus from a given domain and bus number
  64. * @domain: number of PCI domain to search
  65. * @busnr: number of desired PCI bus
  66. *
  67. * Given a PCI bus number and domain number, the desired PCI bus is located
  68. * in the global list of PCI buses. If the bus is found, a pointer to its
  69. * data structure is returned. If no bus is found, %NULL is returned.
  70. */
  71. struct pci_bus * pci_find_bus(int domain, int busnr)
  72. {
  73. struct pci_bus *bus = NULL;
  74. struct pci_bus *tmp_bus;
  75. while ((bus = pci_find_next_bus(bus)) != NULL) {
  76. if (pci_domain_nr(bus) != domain)
  77. continue;
  78. tmp_bus = pci_do_find_bus(bus, busnr);
  79. if (tmp_bus)
  80. return tmp_bus;
  81. }
  82. return NULL;
  83. }
  84. /**
  85. * pci_find_next_bus - begin or continue searching for a PCI bus
  86. * @from: Previous PCI bus found, or %NULL for new search.
  87. *
  88. * Iterates through the list of known PCI busses. A new search is
  89. * initiated by passing %NULL as the @from argument. Otherwise if
  90. * @from is not %NULL, searches continue from next device on the
  91. * global list.
  92. */
  93. struct pci_bus *
  94. pci_find_next_bus(const struct pci_bus *from)
  95. {
  96. struct list_head *n;
  97. struct pci_bus *b = NULL;
  98. WARN_ON(in_interrupt());
  99. down_read(&pci_bus_sem);
  100. n = from ? from->node.next : pci_root_buses.next;
  101. if (n != &pci_root_buses)
  102. b = pci_bus_b(n);
  103. up_read(&pci_bus_sem);
  104. return b;
  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. */
  120. struct pci_dev * pci_get_slot(struct pci_bus *bus, unsigned int devfn)
  121. {
  122. struct list_head *tmp;
  123. struct pci_dev *dev;
  124. WARN_ON(in_interrupt());
  125. down_read(&pci_bus_sem);
  126. list_for_each(tmp, &bus->devices) {
  127. dev = pci_dev_b(tmp);
  128. if (dev->devfn == devfn)
  129. goto out;
  130. }
  131. dev = NULL;
  132. out:
  133. pci_dev_get(dev);
  134. up_read(&pci_bus_sem);
  135. return dev;
  136. }
  137. /**
  138. * pci_get_domain_bus_and_slot - locate PCI device for a given PCI domain (segment), bus, and slot
  139. * @domain: PCI domain/segment on which the PCI device resides.
  140. * @bus: PCI bus on which desired PCI device resides
  141. * @devfn: encodes number of PCI slot in which the desired PCI device
  142. * resides and the logical device number within that slot in case of
  143. * multi-function devices.
  144. *
  145. * Given a PCI domain, bus, and slot/function number, the desired PCI
  146. * device is located in the list of PCI devices. If the device is
  147. * found, its reference count is increased and this function returns a
  148. * pointer to its data structure. The caller must decrement the
  149. * reference count by calling pci_dev_put(). If no device is found,
  150. * %NULL is returned.
  151. */
  152. struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
  153. unsigned int devfn)
  154. {
  155. struct pci_dev *dev = NULL;
  156. for_each_pci_dev(dev) {
  157. if (pci_domain_nr(dev->bus) == domain &&
  158. (dev->bus->number == bus && dev->devfn == devfn))
  159. return dev;
  160. }
  161. return NULL;
  162. }
  163. EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
  164. static int match_pci_dev_by_id(struct device *dev, void *data)
  165. {
  166. struct pci_dev *pdev = to_pci_dev(dev);
  167. struct pci_device_id *id = data;
  168. if (pci_match_one_device(id, pdev))
  169. return 1;
  170. return 0;
  171. }
  172. /*
  173. * pci_get_dev_by_id - begin or continue searching for a PCI device by id
  174. * @id: pointer to struct pci_device_id to match for the device
  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 found
  178. * with a matching id a pointer to its device structure is returned, and the
  179. * reference count to the device is incremented. Otherwise, %NULL is returned.
  180. * A new search is initiated by passing %NULL as the @from argument. Otherwise
  181. * if @from is not %NULL, searches continue from next device on the global
  182. * list. The reference count for @from is always decremented if it is not
  183. * %NULL.
  184. *
  185. * This is an internal function for use by the other search functions in
  186. * this file.
  187. */
  188. static struct pci_dev *pci_get_dev_by_id(const struct pci_device_id *id,
  189. struct pci_dev *from)
  190. {
  191. struct device *dev;
  192. struct device *dev_start = NULL;
  193. struct pci_dev *pdev = NULL;
  194. WARN_ON(in_interrupt());
  195. if (from)
  196. dev_start = &from->dev;
  197. dev = bus_find_device(&pci_bus_type, dev_start, (void *)id,
  198. match_pci_dev_by_id);
  199. if (dev)
  200. pdev = to_pci_dev(dev);
  201. if (from)
  202. pci_dev_put(from);
  203. return pdev;
  204. }
  205. /**
  206. * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  207. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  208. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  209. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  210. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  211. * @from: Previous PCI device found in search, or %NULL for new search.
  212. *
  213. * Iterates through the list of known PCI devices. If a PCI device is found
  214. * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  215. * device structure is returned, and the reference count to the device is
  216. * incremented. Otherwise, %NULL is returned. A new search is initiated by
  217. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  218. * searches continue from next device on the global list.
  219. * The reference count for @from is always decremented if it is not %NULL.
  220. */
  221. struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
  222. unsigned int ss_vendor, unsigned int ss_device,
  223. struct pci_dev *from)
  224. {
  225. struct pci_dev *pdev;
  226. struct pci_device_id *id;
  227. /*
  228. * pci_find_subsys() can be called on the ide_setup() path,
  229. * super-early in boot. But the down_read() will enable local
  230. * interrupts, which can cause some machines to crash. So here we
  231. * detect and flag that situation and bail out early.
  232. */
  233. if (unlikely(no_pci_devices()))
  234. return NULL;
  235. id = kzalloc(sizeof(*id), GFP_KERNEL);
  236. if (!id)
  237. return NULL;
  238. id->vendor = vendor;
  239. id->device = device;
  240. id->subvendor = ss_vendor;
  241. id->subdevice = ss_device;
  242. pdev = pci_get_dev_by_id(id, from);
  243. kfree(id);
  244. return pdev;
  245. }
  246. /**
  247. * pci_get_device - begin or continue searching for a PCI device by vendor/device id
  248. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  249. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  250. * @from: Previous PCI device found in search, or %NULL for new search.
  251. *
  252. * Iterates through the list of known PCI devices. If a PCI device is
  253. * found with a matching @vendor and @device, the reference count to the
  254. * device is incremented and a pointer to its device structure is returned.
  255. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  256. * as the @from argument. Otherwise if @from is not %NULL, searches continue
  257. * from next device on the global list. The reference count for @from is
  258. * always decremented if it is not %NULL.
  259. */
  260. struct pci_dev *
  261. pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
  262. {
  263. return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  264. }
  265. /**
  266. * pci_get_class - begin or continue searching for a PCI device by class
  267. * @class: search for a PCI device with this class designation
  268. * @from: Previous PCI device found in search, or %NULL for new search.
  269. *
  270. * Iterates through the list of known PCI devices. If a PCI device is
  271. * found with a matching @class, the reference count to the device is
  272. * incremented and a pointer to its device structure is returned.
  273. * Otherwise, %NULL is returned.
  274. * A new search is initiated by passing %NULL as the @from argument.
  275. * Otherwise if @from is not %NULL, searches continue from next device
  276. * on the global list. The reference count for @from is always decremented
  277. * if it is not %NULL.
  278. */
  279. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  280. {
  281. struct pci_dev *dev;
  282. struct pci_device_id *id;
  283. id = kzalloc(sizeof(*id), GFP_KERNEL);
  284. if (!id)
  285. return NULL;
  286. id->vendor = id->device = id->subvendor = id->subdevice = PCI_ANY_ID;
  287. id->class_mask = PCI_ANY_ID;
  288. id->class = class;
  289. dev = pci_get_dev_by_id(id, from);
  290. kfree(id);
  291. return dev;
  292. }
  293. /**
  294. * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
  295. * @ids: A pointer to a null terminated list of struct pci_device_id structures
  296. * that describe the type of PCI device the caller is trying to find.
  297. *
  298. * Obvious fact: You do not have a reference to any device that might be found
  299. * by this function, so if that device is removed from the system right after
  300. * this function is finished, the value will be stale. Use this function to
  301. * find devices that are usually built into a system, or for a general hint as
  302. * to if another device happens to be present at this specific moment in time.
  303. */
  304. int pci_dev_present(const struct pci_device_id *ids)
  305. {
  306. struct pci_dev *found = NULL;
  307. WARN_ON(in_interrupt());
  308. while (ids->vendor || ids->subvendor || ids->class_mask) {
  309. found = pci_get_dev_by_id(ids, NULL);
  310. if (found)
  311. goto exit;
  312. ids++;
  313. }
  314. exit:
  315. if (found)
  316. return 1;
  317. return 0;
  318. }
  319. EXPORT_SYMBOL(pci_dev_present);
  320. /* For boot time work */
  321. EXPORT_SYMBOL(pci_find_bus);
  322. EXPORT_SYMBOL(pci_find_next_bus);
  323. /* For everyone */
  324. EXPORT_SYMBOL(pci_get_device);
  325. EXPORT_SYMBOL(pci_get_subsys);
  326. EXPORT_SYMBOL(pci_get_slot);
  327. EXPORT_SYMBOL(pci_get_class);