search.c 12 KB

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