search.c 12 KB

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