search.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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->dma_alias_mask)) {
  39. u8 devfn;
  40. for_each_set_bit(devfn, pdev->dma_alias_mask, U8_MAX) {
  41. ret = fn(pdev, PCI_DEVID(pdev->bus->number, devfn),
  42. data);
  43. if (ret)
  44. return ret;
  45. }
  46. }
  47. for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
  48. struct pci_dev *tmp;
  49. /* Skip virtual buses */
  50. if (!bus->self)
  51. continue;
  52. tmp = bus->self;
  53. /* stop at bridge where translation unit is associated */
  54. if (tmp->dev_flags & PCI_DEV_FLAGS_BRIDGE_XLATE_ROOT)
  55. return ret;
  56. /*
  57. * PCIe-to-PCI/X bridges alias transactions from downstream
  58. * devices using the subordinate bus number (PCI Express to
  59. * PCI/PCI-X Bridge Spec, rev 1.0, sec 2.3). For all cases
  60. * where the upstream bus is PCI/X we alias to the bridge
  61. * (there are various conditions in the previous reference
  62. * where the bridge may take ownership of transactions, even
  63. * when the secondary interface is PCI-X).
  64. */
  65. if (pci_is_pcie(tmp)) {
  66. switch (pci_pcie_type(tmp)) {
  67. case PCI_EXP_TYPE_ROOT_PORT:
  68. case PCI_EXP_TYPE_UPSTREAM:
  69. case PCI_EXP_TYPE_DOWNSTREAM:
  70. continue;
  71. case PCI_EXP_TYPE_PCI_BRIDGE:
  72. ret = fn(tmp,
  73. PCI_DEVID(tmp->subordinate->number,
  74. PCI_DEVFN(0, 0)), data);
  75. if (ret)
  76. return ret;
  77. continue;
  78. case PCI_EXP_TYPE_PCIE_BRIDGE:
  79. ret = fn(tmp,
  80. PCI_DEVID(tmp->bus->number,
  81. tmp->devfn), data);
  82. if (ret)
  83. return ret;
  84. continue;
  85. }
  86. } else {
  87. if (tmp->dev_flags & PCI_DEV_FLAG_PCIE_BRIDGE_ALIAS)
  88. ret = fn(tmp,
  89. PCI_DEVID(tmp->subordinate->number,
  90. PCI_DEVFN(0, 0)), data);
  91. else
  92. ret = fn(tmp,
  93. PCI_DEVID(tmp->bus->number,
  94. tmp->devfn), data);
  95. if (ret)
  96. return ret;
  97. }
  98. }
  99. return ret;
  100. }
  101. static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
  102. {
  103. struct pci_bus *child;
  104. struct pci_bus *tmp;
  105. if (bus->number == busnr)
  106. return bus;
  107. list_for_each_entry(tmp, &bus->children, node) {
  108. child = pci_do_find_bus(tmp, busnr);
  109. if (child)
  110. return child;
  111. }
  112. return NULL;
  113. }
  114. /**
  115. * pci_find_bus - locate PCI bus from a given domain and bus number
  116. * @domain: number of PCI domain to search
  117. * @busnr: number of desired PCI bus
  118. *
  119. * Given a PCI bus number and domain number, the desired PCI bus is located
  120. * in the global list of PCI buses. If the bus is found, a pointer to its
  121. * data structure is returned. If no bus is found, %NULL is returned.
  122. */
  123. struct pci_bus *pci_find_bus(int domain, int busnr)
  124. {
  125. struct pci_bus *bus = NULL;
  126. struct pci_bus *tmp_bus;
  127. while ((bus = pci_find_next_bus(bus)) != NULL) {
  128. if (pci_domain_nr(bus) != domain)
  129. continue;
  130. tmp_bus = pci_do_find_bus(bus, busnr);
  131. if (tmp_bus)
  132. return tmp_bus;
  133. }
  134. return NULL;
  135. }
  136. EXPORT_SYMBOL(pci_find_bus);
  137. /**
  138. * pci_find_next_bus - begin or continue searching for a PCI bus
  139. * @from: Previous PCI bus found, or %NULL for new search.
  140. *
  141. * Iterates through the list of known PCI buses. A new search is
  142. * initiated by passing %NULL as the @from argument. Otherwise if
  143. * @from is not %NULL, searches continue from next device on the
  144. * global list.
  145. */
  146. struct pci_bus *pci_find_next_bus(const struct pci_bus *from)
  147. {
  148. struct list_head *n;
  149. struct pci_bus *b = NULL;
  150. WARN_ON(in_interrupt());
  151. down_read(&pci_bus_sem);
  152. n = from ? from->node.next : pci_root_buses.next;
  153. if (n != &pci_root_buses)
  154. b = list_entry(n, struct pci_bus, node);
  155. up_read(&pci_bus_sem);
  156. return b;
  157. }
  158. EXPORT_SYMBOL(pci_find_next_bus);
  159. /**
  160. * pci_get_slot - locate PCI device for a given PCI slot
  161. * @bus: PCI bus on which desired PCI device resides
  162. * @devfn: encodes number of PCI slot in which the desired PCI
  163. * device resides and the logical device number within that slot
  164. * in case of multi-function devices.
  165. *
  166. * Given a PCI bus and slot/function number, the desired PCI device
  167. * is located in the list of PCI devices.
  168. * If the device is found, its reference count is increased and this
  169. * function returns a pointer to its data structure. The caller must
  170. * decrement the reference count by calling pci_dev_put().
  171. * If no device is found, %NULL is returned.
  172. */
  173. struct pci_dev *pci_get_slot(struct pci_bus *bus, unsigned int devfn)
  174. {
  175. struct pci_dev *dev;
  176. WARN_ON(in_interrupt());
  177. down_read(&pci_bus_sem);
  178. list_for_each_entry(dev, &bus->devices, bus_list) {
  179. if (dev->devfn == devfn)
  180. goto out;
  181. }
  182. dev = NULL;
  183. out:
  184. pci_dev_get(dev);
  185. up_read(&pci_bus_sem);
  186. return dev;
  187. }
  188. EXPORT_SYMBOL(pci_get_slot);
  189. /**
  190. * pci_get_domain_bus_and_slot - locate PCI device for a given PCI domain (segment), bus, and slot
  191. * @domain: PCI domain/segment on which the PCI device resides.
  192. * @bus: PCI bus on which desired PCI device resides
  193. * @devfn: encodes number of PCI slot in which the desired PCI device
  194. * resides and the logical device number within that slot in case of
  195. * multi-function devices.
  196. *
  197. * Given a PCI domain, bus, and slot/function number, the desired PCI
  198. * device is located in the list of PCI devices. If the device is
  199. * found, its reference count is increased and this function returns a
  200. * pointer to its data structure. The caller must decrement the
  201. * reference count by calling pci_dev_put(). If no device is found,
  202. * %NULL is returned.
  203. */
  204. struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
  205. unsigned int devfn)
  206. {
  207. struct pci_dev *dev = NULL;
  208. for_each_pci_dev(dev) {
  209. if (pci_domain_nr(dev->bus) == domain &&
  210. (dev->bus->number == bus && dev->devfn == devfn))
  211. return dev;
  212. }
  213. return NULL;
  214. }
  215. EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
  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. 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. dev_start = &from->dev;
  249. dev = bus_find_device(&pci_bus_type, dev_start, (void *)id,
  250. match_pci_dev_by_id);
  251. if (dev)
  252. pdev = to_pci_dev(dev);
  253. pci_dev_put(from);
  254. return pdev;
  255. }
  256. /**
  257. * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  258. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  259. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  260. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  261. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  262. * @from: Previous PCI device found in search, or %NULL for new search.
  263. *
  264. * Iterates through the list of known PCI devices. If a PCI device is found
  265. * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  266. * device structure is returned, and the reference count to the device is
  267. * incremented. Otherwise, %NULL is returned. A new search is initiated by
  268. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  269. * searches continue from next device on the global list.
  270. * The reference count for @from is always decremented if it is not %NULL.
  271. */
  272. struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
  273. unsigned int ss_vendor, unsigned int ss_device,
  274. struct pci_dev *from)
  275. {
  276. struct pci_device_id id = {
  277. .vendor = vendor,
  278. .device = device,
  279. .subvendor = ss_vendor,
  280. .subdevice = ss_device,
  281. };
  282. return pci_get_dev_by_id(&id, from);
  283. }
  284. EXPORT_SYMBOL(pci_get_subsys);
  285. /**
  286. * pci_get_device - begin or continue searching for a PCI device by vendor/device id
  287. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  288. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  289. * @from: Previous PCI device found in search, or %NULL for new search.
  290. *
  291. * Iterates through the list of known PCI devices. If a PCI device is
  292. * found with a matching @vendor and @device, the reference count to the
  293. * device is incremented and a pointer to its device structure is returned.
  294. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  295. * as the @from argument. Otherwise if @from is not %NULL, searches continue
  296. * from next device on the global list. The reference count for @from is
  297. * always decremented if it is not %NULL.
  298. */
  299. struct pci_dev *pci_get_device(unsigned int vendor, unsigned int device,
  300. struct pci_dev *from)
  301. {
  302. return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  303. }
  304. EXPORT_SYMBOL(pci_get_device);
  305. /**
  306. * pci_get_class - begin or continue searching for a PCI device by class
  307. * @class: search for a PCI device with this class designation
  308. * @from: Previous PCI device found in search, or %NULL for new search.
  309. *
  310. * Iterates through the list of known PCI devices. If a PCI device is
  311. * found with a matching @class, the reference count to the device is
  312. * incremented and a pointer to its device structure is returned.
  313. * Otherwise, %NULL is returned.
  314. * A new search is initiated by passing %NULL as the @from argument.
  315. * Otherwise if @from is not %NULL, searches continue from next device
  316. * on the global list. The reference count for @from is always decremented
  317. * if it is not %NULL.
  318. */
  319. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  320. {
  321. struct pci_device_id id = {
  322. .vendor = PCI_ANY_ID,
  323. .device = PCI_ANY_ID,
  324. .subvendor = PCI_ANY_ID,
  325. .subdevice = PCI_ANY_ID,
  326. .class_mask = PCI_ANY_ID,
  327. .class = class,
  328. };
  329. return pci_get_dev_by_id(&id, from);
  330. }
  331. EXPORT_SYMBOL(pci_get_class);
  332. /**
  333. * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
  334. * @ids: A pointer to a null terminated list of struct pci_device_id structures
  335. * that describe the type of PCI device the caller is trying to find.
  336. *
  337. * Obvious fact: You do not have a reference to any device that might be found
  338. * by this function, so if that device is removed from the system right after
  339. * this function is finished, the value will be stale. Use this function to
  340. * find devices that are usually built into a system, or for a general hint as
  341. * to if another device happens to be present at this specific moment in time.
  342. */
  343. int pci_dev_present(const struct pci_device_id *ids)
  344. {
  345. struct pci_dev *found = NULL;
  346. WARN_ON(in_interrupt());
  347. while (ids->vendor || ids->subvendor || ids->class_mask) {
  348. found = pci_get_dev_by_id(ids, NULL);
  349. if (found) {
  350. pci_dev_put(found);
  351. return 1;
  352. }
  353. ids++;
  354. }
  355. return 0;
  356. }
  357. EXPORT_SYMBOL(pci_dev_present);