search.c 12 KB

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