search.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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/pci.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/interrupt.h>
  13. #include "pci.h"
  14. DECLARE_RWSEM(pci_bus_sem);
  15. EXPORT_SYMBOL_GPL(pci_bus_sem);
  16. /*
  17. * pci_for_each_dma_alias - Iterate over DMA aliases for a device
  18. * @pdev: starting downstream device
  19. * @fn: function to call for each alias
  20. * @data: opaque data to pass to @fn
  21. *
  22. * Starting @pdev, walk up the bus calling @fn for each possible alias
  23. * of @pdev at the root bus.
  24. */
  25. int pci_for_each_dma_alias(struct pci_dev *pdev,
  26. int (*fn)(struct pci_dev *pdev,
  27. u16 alias, void *data), void *data)
  28. {
  29. struct pci_bus *bus;
  30. int ret;
  31. ret = fn(pdev, PCI_DEVID(pdev->bus->number, pdev->devfn), data);
  32. if (ret)
  33. return ret;
  34. /*
  35. * If the device is broken and uses an alias requester ID for
  36. * DMA, iterate over that too.
  37. */
  38. if (unlikely(pdev->dev_flags & PCI_DEV_FLAGS_DMA_ALIAS_DEVFN)) {
  39. ret = fn(pdev, PCI_DEVID(pdev->bus->number,
  40. pdev->dma_alias_devfn), data);
  41. if (ret)
  42. return ret;
  43. }
  44. for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
  45. struct pci_dev *tmp;
  46. /* Skip virtual buses */
  47. if (!bus->self)
  48. continue;
  49. tmp = bus->self;
  50. /*
  51. * PCIe-to-PCI/X bridges alias transactions from downstream
  52. * devices using the subordinate bus number (PCI Express to
  53. * PCI/PCI-X Bridge Spec, rev 1.0, sec 2.3). For all cases
  54. * where the upstream bus is PCI/X we alias to the bridge
  55. * (there are various conditions in the previous reference
  56. * where the bridge may take ownership of transactions, even
  57. * when the secondary interface is PCI-X).
  58. */
  59. if (pci_is_pcie(tmp)) {
  60. switch (pci_pcie_type(tmp)) {
  61. case PCI_EXP_TYPE_ROOT_PORT:
  62. case PCI_EXP_TYPE_UPSTREAM:
  63. case PCI_EXP_TYPE_DOWNSTREAM:
  64. continue;
  65. case PCI_EXP_TYPE_PCI_BRIDGE:
  66. ret = fn(tmp,
  67. PCI_DEVID(tmp->subordinate->number,
  68. PCI_DEVFN(0, 0)), data);
  69. if (ret)
  70. return ret;
  71. continue;
  72. case PCI_EXP_TYPE_PCIE_BRIDGE:
  73. ret = fn(tmp,
  74. PCI_DEVID(tmp->bus->number,
  75. tmp->devfn), data);
  76. if (ret)
  77. return ret;
  78. continue;
  79. }
  80. } else {
  81. if (tmp->dev_flags & PCI_DEV_FLAG_PCIE_BRIDGE_ALIAS)
  82. ret = fn(tmp,
  83. PCI_DEVID(tmp->subordinate->number,
  84. PCI_DEVFN(0, 0)), data);
  85. else
  86. ret = fn(tmp,
  87. PCI_DEVID(tmp->bus->number,
  88. tmp->devfn), data);
  89. if (ret)
  90. return ret;
  91. }
  92. }
  93. return ret;
  94. }
  95. /*
  96. * find the upstream PCIe-to-PCI bridge of a PCI device
  97. * if the device is PCIE, return NULL
  98. * if the device isn't connected to a PCIe bridge (that is its parent is a
  99. * legacy PCI bridge and the bridge is directly connected to bus 0), return its
  100. * parent
  101. */
  102. struct pci_dev *pci_find_upstream_pcie_bridge(struct pci_dev *pdev)
  103. {
  104. struct pci_dev *tmp = NULL;
  105. if (pci_is_pcie(pdev))
  106. return NULL;
  107. while (1) {
  108. if (pci_is_root_bus(pdev->bus))
  109. break;
  110. pdev = pdev->bus->self;
  111. /* a p2p bridge */
  112. if (!pci_is_pcie(pdev)) {
  113. tmp = pdev;
  114. continue;
  115. }
  116. /* PCI device should connect to a PCIe bridge */
  117. if (pci_pcie_type(pdev) != PCI_EXP_TYPE_PCI_BRIDGE) {
  118. /* Busted hardware? */
  119. WARN_ON_ONCE(1);
  120. return NULL;
  121. }
  122. return pdev;
  123. }
  124. return tmp;
  125. }
  126. static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
  127. {
  128. struct pci_bus *child;
  129. struct pci_bus *tmp;
  130. if (bus->number == busnr)
  131. return bus;
  132. list_for_each_entry(tmp, &bus->children, node) {
  133. child = pci_do_find_bus(tmp, busnr);
  134. if (child)
  135. return child;
  136. }
  137. return NULL;
  138. }
  139. /**
  140. * pci_find_bus - locate PCI bus from a given domain and bus number
  141. * @domain: number of PCI domain to search
  142. * @busnr: number of desired PCI bus
  143. *
  144. * Given a PCI bus number and domain number, the desired PCI bus is located
  145. * in the global list of PCI buses. If the bus is found, a pointer to its
  146. * data structure is returned. If no bus is found, %NULL is returned.
  147. */
  148. struct pci_bus *pci_find_bus(int domain, int busnr)
  149. {
  150. struct pci_bus *bus = NULL;
  151. struct pci_bus *tmp_bus;
  152. while ((bus = pci_find_next_bus(bus)) != NULL) {
  153. if (pci_domain_nr(bus) != domain)
  154. continue;
  155. tmp_bus = pci_do_find_bus(bus, busnr);
  156. if (tmp_bus)
  157. return tmp_bus;
  158. }
  159. return NULL;
  160. }
  161. EXPORT_SYMBOL(pci_find_bus);
  162. /**
  163. * pci_find_next_bus - begin or continue searching for a PCI bus
  164. * @from: Previous PCI bus found, or %NULL for new search.
  165. *
  166. * Iterates through the list of known PCI buses. A new search is
  167. * initiated by passing %NULL as the @from argument. Otherwise if
  168. * @from is not %NULL, searches continue from next device on the
  169. * global list.
  170. */
  171. struct pci_bus *pci_find_next_bus(const struct pci_bus *from)
  172. {
  173. struct list_head *n;
  174. struct pci_bus *b = NULL;
  175. WARN_ON(in_interrupt());
  176. down_read(&pci_bus_sem);
  177. n = from ? from->node.next : pci_root_buses.next;
  178. if (n != &pci_root_buses)
  179. b = list_entry(n, struct pci_bus, node);
  180. up_read(&pci_bus_sem);
  181. return b;
  182. }
  183. EXPORT_SYMBOL(pci_find_next_bus);
  184. /**
  185. * pci_get_slot - locate PCI device for a given PCI slot
  186. * @bus: PCI bus on which desired PCI device resides
  187. * @devfn: encodes number of PCI slot in which the desired PCI
  188. * device resides and the logical device number within that slot
  189. * in case of multi-function devices.
  190. *
  191. * Given a PCI bus and slot/function number, the desired PCI device
  192. * is located in the list of PCI devices.
  193. * If the device is found, its reference count is increased and this
  194. * function returns a pointer to its data structure. The caller must
  195. * decrement the reference count by calling pci_dev_put().
  196. * If no device is found, %NULL is returned.
  197. */
  198. struct pci_dev *pci_get_slot(struct pci_bus *bus, unsigned int devfn)
  199. {
  200. struct pci_dev *dev;
  201. WARN_ON(in_interrupt());
  202. down_read(&pci_bus_sem);
  203. list_for_each_entry(dev, &bus->devices, bus_list) {
  204. if (dev->devfn == devfn)
  205. goto out;
  206. }
  207. dev = NULL;
  208. out:
  209. pci_dev_get(dev);
  210. up_read(&pci_bus_sem);
  211. return dev;
  212. }
  213. EXPORT_SYMBOL(pci_get_slot);
  214. /**
  215. * pci_get_domain_bus_and_slot - locate PCI device for a given PCI domain (segment), bus, and slot
  216. * @domain: PCI domain/segment on which the PCI device resides.
  217. * @bus: PCI bus on which desired PCI device resides
  218. * @devfn: encodes number of PCI slot in which the desired PCI device
  219. * resides and the logical device number within that slot in case of
  220. * multi-function devices.
  221. *
  222. * Given a PCI domain, bus, and slot/function number, the desired PCI
  223. * device is located in the list of PCI devices. If the device is
  224. * found, its reference count is increased and this function returns a
  225. * pointer to its data structure. The caller must decrement the
  226. * reference count by calling pci_dev_put(). If no device is found,
  227. * %NULL is returned.
  228. */
  229. struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
  230. unsigned int devfn)
  231. {
  232. struct pci_dev *dev = NULL;
  233. for_each_pci_dev(dev) {
  234. if (pci_domain_nr(dev->bus) == domain &&
  235. (dev->bus->number == bus && dev->devfn == devfn))
  236. return dev;
  237. }
  238. return NULL;
  239. }
  240. EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
  241. static int match_pci_dev_by_id(struct device *dev, void *data)
  242. {
  243. struct pci_dev *pdev = to_pci_dev(dev);
  244. struct pci_device_id *id = data;
  245. if (pci_match_one_device(id, pdev))
  246. return 1;
  247. return 0;
  248. }
  249. /*
  250. * pci_get_dev_by_id - begin or continue searching for a PCI device by id
  251. * @id: pointer to struct pci_device_id to match for the device
  252. * @from: Previous PCI device found in search, or %NULL for new search.
  253. *
  254. * Iterates through the list of known PCI devices. If a PCI device is found
  255. * with a matching id a pointer to its device structure is returned, and the
  256. * reference count to the device is incremented. Otherwise, %NULL is returned.
  257. * A new search is initiated by passing %NULL as the @from argument. Otherwise
  258. * if @from is not %NULL, searches continue from next device on the global
  259. * list. The reference count for @from is always decremented if it is not
  260. * %NULL.
  261. *
  262. * This is an internal function for use by the other search functions in
  263. * this file.
  264. */
  265. static struct pci_dev *pci_get_dev_by_id(const struct pci_device_id *id,
  266. struct pci_dev *from)
  267. {
  268. struct device *dev;
  269. struct device *dev_start = NULL;
  270. struct pci_dev *pdev = NULL;
  271. WARN_ON(in_interrupt());
  272. if (from)
  273. dev_start = &from->dev;
  274. dev = bus_find_device(&pci_bus_type, dev_start, (void *)id,
  275. match_pci_dev_by_id);
  276. if (dev)
  277. pdev = to_pci_dev(dev);
  278. if (from)
  279. pci_dev_put(from);
  280. return pdev;
  281. }
  282. /**
  283. * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  284. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  285. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  286. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  287. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  288. * @from: Previous PCI device found in search, or %NULL for new search.
  289. *
  290. * Iterates through the list of known PCI devices. If a PCI device is found
  291. * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  292. * device structure is returned, and the reference count to the device is
  293. * incremented. Otherwise, %NULL is returned. A new search is initiated by
  294. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  295. * searches continue from next device on the global list.
  296. * The reference count for @from is always decremented if it is not %NULL.
  297. */
  298. struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
  299. unsigned int ss_vendor, unsigned int ss_device,
  300. struct pci_dev *from)
  301. {
  302. struct pci_device_id id = {
  303. .vendor = vendor,
  304. .device = device,
  305. .subvendor = ss_vendor,
  306. .subdevice = ss_device,
  307. };
  308. return pci_get_dev_by_id(&id, from);
  309. }
  310. EXPORT_SYMBOL(pci_get_subsys);
  311. /**
  312. * pci_get_device - begin or continue searching for a PCI device by vendor/device id
  313. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  314. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  315. * @from: Previous PCI device found in search, or %NULL for new search.
  316. *
  317. * Iterates through the list of known PCI devices. If a PCI device is
  318. * found with a matching @vendor and @device, the reference count to the
  319. * device is incremented and a pointer to its device structure is returned.
  320. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  321. * as the @from argument. Otherwise if @from is not %NULL, searches continue
  322. * from next device on the global list. The reference count for @from is
  323. * always decremented if it is not %NULL.
  324. */
  325. struct pci_dev *pci_get_device(unsigned int vendor, unsigned int device,
  326. struct pci_dev *from)
  327. {
  328. return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  329. }
  330. EXPORT_SYMBOL(pci_get_device);
  331. /**
  332. * pci_get_class - begin or continue searching for a PCI device by class
  333. * @class: search for a PCI device with this class designation
  334. * @from: Previous PCI device found in search, or %NULL for new search.
  335. *
  336. * Iterates through the list of known PCI devices. If a PCI device is
  337. * found with a matching @class, the reference count to the device is
  338. * incremented and a pointer to its device structure is returned.
  339. * Otherwise, %NULL is returned.
  340. * A new search is initiated by passing %NULL as the @from argument.
  341. * Otherwise if @from is not %NULL, searches continue from next device
  342. * on the global list. The reference count for @from is always decremented
  343. * if it is not %NULL.
  344. */
  345. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  346. {
  347. struct pci_device_id id = {
  348. .vendor = PCI_ANY_ID,
  349. .device = PCI_ANY_ID,
  350. .subvendor = PCI_ANY_ID,
  351. .subdevice = PCI_ANY_ID,
  352. .class_mask = PCI_ANY_ID,
  353. .class = class,
  354. };
  355. return pci_get_dev_by_id(&id, from);
  356. }
  357. EXPORT_SYMBOL(pci_get_class);
  358. /**
  359. * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
  360. * @ids: A pointer to a null terminated list of struct pci_device_id structures
  361. * that describe the type of PCI device the caller is trying to find.
  362. *
  363. * Obvious fact: You do not have a reference to any device that might be found
  364. * by this function, so if that device is removed from the system right after
  365. * this function is finished, the value will be stale. Use this function to
  366. * find devices that are usually built into a system, or for a general hint as
  367. * to if another device happens to be present at this specific moment in time.
  368. */
  369. int pci_dev_present(const struct pci_device_id *ids)
  370. {
  371. struct pci_dev *found = NULL;
  372. WARN_ON(in_interrupt());
  373. while (ids->vendor || ids->subvendor || ids->class_mask) {
  374. found = pci_get_dev_by_id(ids, NULL);
  375. if (found) {
  376. pci_dev_put(found);
  377. return 1;
  378. }
  379. ids++;
  380. }
  381. return 0;
  382. }
  383. EXPORT_SYMBOL(pci_dev_present);