search.c 13 KB

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