search.c 12 KB

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