search.c 11 KB

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