search.c 13 KB

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