search.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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 (pci_is_pcie(pdev))
  27. return NULL;
  28. while (1) {
  29. if (pci_is_root_bus(pdev->bus))
  30. break;
  31. pdev = pdev->bus->self;
  32. /* a p2p bridge */
  33. if (!pci_is_pcie(pdev)) {
  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. /**
  105. * pci_get_slot - locate PCI device for a given PCI slot
  106. * @bus: PCI bus on which desired PCI device resides
  107. * @devfn: encodes number of PCI slot in which the desired PCI
  108. * device resides and the logical device number within that slot
  109. * in case of multi-function devices.
  110. *
  111. * Given a PCI bus and slot/function number, the desired PCI device
  112. * is located in the list of PCI devices.
  113. * If the device is found, its reference count is increased and this
  114. * function returns a pointer to its data structure. The caller must
  115. * decrement the reference count by calling pci_dev_put().
  116. * If no device is found, %NULL is returned.
  117. */
  118. struct pci_dev * pci_get_slot(struct pci_bus *bus, unsigned int devfn)
  119. {
  120. struct list_head *tmp;
  121. struct pci_dev *dev;
  122. WARN_ON(in_interrupt());
  123. down_read(&pci_bus_sem);
  124. list_for_each(tmp, &bus->devices) {
  125. dev = pci_dev_b(tmp);
  126. if (dev->devfn == devfn)
  127. goto out;
  128. }
  129. dev = NULL;
  130. out:
  131. pci_dev_get(dev);
  132. up_read(&pci_bus_sem);
  133. return dev;
  134. }
  135. /**
  136. * pci_get_domain_bus_and_slot - locate PCI device for a given PCI domain (segment), bus, and slot
  137. * @domain: PCI domain/segment on which the PCI device resides.
  138. * @bus: PCI bus on which desired PCI device resides
  139. * @devfn: encodes number of PCI slot in which the desired PCI device
  140. * resides and the logical device number within that slot in case of
  141. * multi-function devices.
  142. *
  143. * Given a PCI domain, bus, and slot/function number, the desired PCI
  144. * device is located in the list of PCI devices. If the device is
  145. * found, its reference count is increased and this function returns a
  146. * pointer to its data structure. The caller must decrement the
  147. * reference count by calling pci_dev_put(). If no device is found,
  148. * %NULL is returned.
  149. */
  150. struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
  151. unsigned int devfn)
  152. {
  153. struct pci_dev *dev = NULL;
  154. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  155. if (pci_domain_nr(dev->bus) == domain &&
  156. (dev->bus->number == bus && dev->devfn == devfn))
  157. return dev;
  158. }
  159. return NULL;
  160. }
  161. EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
  162. static int match_pci_dev_by_id(struct device *dev, void *data)
  163. {
  164. struct pci_dev *pdev = to_pci_dev(dev);
  165. struct pci_device_id *id = data;
  166. if (pci_match_one_device(id, pdev))
  167. return 1;
  168. return 0;
  169. }
  170. /*
  171. * pci_get_dev_by_id - begin or continue searching for a PCI device by id
  172. * @id: pointer to struct pci_device_id to match for the device
  173. * @from: Previous PCI device found in search, or %NULL for new search.
  174. *
  175. * Iterates through the list of known PCI devices. If a PCI device is found
  176. * with a matching id a pointer to its device structure is returned, and the
  177. * reference count to the device is incremented. Otherwise, %NULL is returned.
  178. * A new search is initiated by passing %NULL as the @from argument. Otherwise
  179. * if @from is not %NULL, searches continue from next device on the global
  180. * list. The reference count for @from is always decremented if it is not
  181. * %NULL.
  182. *
  183. * This is an internal function for use by the other search functions in
  184. * this file.
  185. */
  186. static struct pci_dev *pci_get_dev_by_id(const struct pci_device_id *id,
  187. struct pci_dev *from)
  188. {
  189. struct device *dev;
  190. struct device *dev_start = NULL;
  191. struct pci_dev *pdev = NULL;
  192. WARN_ON(in_interrupt());
  193. if (from)
  194. dev_start = &from->dev;
  195. dev = bus_find_device(&pci_bus_type, dev_start, (void *)id,
  196. match_pci_dev_by_id);
  197. if (dev)
  198. pdev = to_pci_dev(dev);
  199. if (from)
  200. pci_dev_put(from);
  201. return pdev;
  202. }
  203. /**
  204. * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice 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. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  208. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  209. * @from: Previous PCI device found in search, or %NULL for new search.
  210. *
  211. * Iterates through the list of known PCI devices. If a PCI device is found
  212. * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  213. * device structure is returned, and the reference count to the device is
  214. * incremented. Otherwise, %NULL is returned. A new search is initiated by
  215. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  216. * searches continue from next device on the global list.
  217. * The reference count for @from is always decremented if it is not %NULL.
  218. */
  219. struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
  220. unsigned int ss_vendor, unsigned int ss_device,
  221. struct pci_dev *from)
  222. {
  223. struct pci_dev *pdev;
  224. struct pci_device_id *id;
  225. /*
  226. * pci_find_subsys() can be called on the ide_setup() path,
  227. * super-early in boot. But the down_read() will enable local
  228. * interrupts, which can cause some machines to crash. So here we
  229. * detect and flag that situation and bail out early.
  230. */
  231. if (unlikely(no_pci_devices()))
  232. return NULL;
  233. id = kzalloc(sizeof(*id), GFP_KERNEL);
  234. if (!id)
  235. return NULL;
  236. id->vendor = vendor;
  237. id->device = device;
  238. id->subvendor = ss_vendor;
  239. id->subdevice = ss_device;
  240. pdev = pci_get_dev_by_id(id, from);
  241. kfree(id);
  242. return pdev;
  243. }
  244. /**
  245. * pci_get_device - begin or continue searching for a PCI device by vendor/device id
  246. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  247. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  248. * @from: Previous PCI device found in search, or %NULL for new search.
  249. *
  250. * Iterates through the list of known PCI devices. If a PCI device is
  251. * found with a matching @vendor and @device, the reference count to the
  252. * device is incremented and a pointer to its device structure is returned.
  253. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  254. * as the @from argument. Otherwise if @from is not %NULL, searches continue
  255. * from next device on the global list. The reference count for @from is
  256. * always decremented if it is not %NULL.
  257. */
  258. struct pci_dev *
  259. pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
  260. {
  261. return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  262. }
  263. /**
  264. * pci_get_class - begin or continue searching for a PCI device by class
  265. * @class: search for a PCI device with this class designation
  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
  269. * found with a matching @class, the reference count to the device is
  270. * incremented and a pointer to its device structure is returned.
  271. * Otherwise, %NULL is returned.
  272. * A new search is initiated by passing %NULL as the @from argument.
  273. * Otherwise if @from is not %NULL, searches continue from next device
  274. * on the global list. The reference count for @from is always decremented
  275. * if it is not %NULL.
  276. */
  277. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  278. {
  279. struct pci_dev *dev;
  280. struct pci_device_id *id;
  281. id = kzalloc(sizeof(*id), GFP_KERNEL);
  282. if (!id)
  283. return NULL;
  284. id->vendor = id->device = id->subvendor = id->subdevice = PCI_ANY_ID;
  285. id->class_mask = PCI_ANY_ID;
  286. id->class = class;
  287. dev = pci_get_dev_by_id(id, from);
  288. kfree(id);
  289. return dev;
  290. }
  291. /**
  292. * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
  293. * @ids: A pointer to a null terminated list of struct pci_device_id structures
  294. * that describe the type of PCI device the caller is trying to find.
  295. *
  296. * Obvious fact: You do not have a reference to any device that might be found
  297. * by this function, so if that device is removed from the system right after
  298. * this function is finished, the value will be stale. Use this function to
  299. * find devices that are usually built into a system, or for a general hint as
  300. * to if another device happens to be present at this specific moment in time.
  301. */
  302. int pci_dev_present(const struct pci_device_id *ids)
  303. {
  304. struct pci_dev *found = NULL;
  305. WARN_ON(in_interrupt());
  306. while (ids->vendor || ids->subvendor || ids->class_mask) {
  307. found = pci_get_dev_by_id(ids, NULL);
  308. if (found)
  309. goto exit;
  310. ids++;
  311. }
  312. exit:
  313. if (found)
  314. return 1;
  315. return 0;
  316. }
  317. EXPORT_SYMBOL(pci_dev_present);
  318. /* For boot time work */
  319. EXPORT_SYMBOL(pci_find_bus);
  320. EXPORT_SYMBOL(pci_find_next_bus);
  321. /* For everyone */
  322. EXPORT_SYMBOL(pci_get_device);
  323. EXPORT_SYMBOL(pci_get_subsys);
  324. EXPORT_SYMBOL(pci_get_slot);
  325. EXPORT_SYMBOL(pci_get_class);