search.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 <[email protected]>
  8. * Copyright (C) 2003 -- 2004 Greg Kroah-Hartman <[email protected]>
  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. 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. down_read(&pci_bus_sem);
  177. list_for_each_entry(dev, &bus->devices, bus_list) {
  178. if (dev->devfn == devfn)
  179. goto out;
  180. }
  181. dev = NULL;
  182. out:
  183. pci_dev_get(dev);
  184. up_read(&pci_bus_sem);
  185. return dev;
  186. }
  187. EXPORT_SYMBOL(pci_get_slot);
  188. /**
  189. * pci_get_domain_bus_and_slot - locate PCI device for a given PCI domain (segment), bus, and slot
  190. * @domain: PCI domain/segment on which the PCI device resides.
  191. * @bus: PCI bus on which desired PCI device resides
  192. * @devfn: encodes number of PCI slot in which the desired PCI device
  193. * resides and the logical device number within that slot in case of
  194. * multi-function devices.
  195. *
  196. * Given a PCI domain, bus, and slot/function number, the desired PCI
  197. * device is located in the list of PCI devices. If the device is
  198. * found, its reference count is increased and this function returns a
  199. * pointer to its data structure. The caller must decrement the
  200. * reference count by calling pci_dev_put(). If no device is found,
  201. * %NULL is returned.
  202. */
  203. struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
  204. unsigned int devfn)
  205. {
  206. struct pci_dev *dev = NULL;
  207. for_each_pci_dev(dev) {
  208. if (pci_domain_nr(dev->bus) == domain &&
  209. (dev->bus->number == bus && dev->devfn == devfn))
  210. return dev;
  211. }
  212. return NULL;
  213. }
  214. EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
  215. static int match_pci_dev_by_id(struct device *dev, const void *data)
  216. {
  217. struct pci_dev *pdev = to_pci_dev(dev);
  218. const struct pci_device_id *id = data;
  219. if (pci_match_one_device(id, pdev))
  220. return 1;
  221. return 0;
  222. }
  223. /*
  224. * pci_get_dev_by_id - begin or continue searching for a PCI device by id
  225. * @id: pointer to struct pci_device_id to match for the device
  226. * @from: Previous PCI device found in search, or %NULL for new search.
  227. *
  228. * Iterates through the list of known PCI devices. If a PCI device is found
  229. * with a matching id a pointer to its device structure is returned, and the
  230. * reference count to the device is incremented. Otherwise, %NULL is returned.
  231. * A new search is initiated by passing %NULL as the @from argument. Otherwise
  232. * if @from is not %NULL, searches continue from next device on the global
  233. * list. The reference count for @from is always decremented if it is not
  234. * %NULL.
  235. *
  236. * This is an internal function for use by the other search functions in
  237. * this file.
  238. */
  239. static struct pci_dev *pci_get_dev_by_id(const struct pci_device_id *id,
  240. struct pci_dev *from)
  241. {
  242. struct device *dev;
  243. struct device *dev_start = NULL;
  244. struct pci_dev *pdev = NULL;
  245. if (from)
  246. dev_start = &from->dev;
  247. dev = bus_find_device(&pci_bus_type, dev_start, (void *)id,
  248. match_pci_dev_by_id);
  249. if (dev)
  250. pdev = to_pci_dev(dev);
  251. pci_dev_put(from);
  252. return pdev;
  253. }
  254. /**
  255. * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  256. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  257. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  258. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  259. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  260. * @from: Previous PCI device found in search, or %NULL for new search.
  261. *
  262. * Iterates through the list of known PCI devices. If a PCI device is found
  263. * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  264. * device structure is returned, and the reference count to the device is
  265. * incremented. Otherwise, %NULL is returned. A new search is initiated by
  266. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  267. * searches continue from next device on the global list.
  268. * The reference count for @from is always decremented if it is not %NULL.
  269. */
  270. struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
  271. unsigned int ss_vendor, unsigned int ss_device,
  272. struct pci_dev *from)
  273. {
  274. struct pci_device_id id = {
  275. .vendor = vendor,
  276. .device = device,
  277. .subvendor = ss_vendor,
  278. .subdevice = ss_device,
  279. };
  280. return pci_get_dev_by_id(&id, from);
  281. }
  282. EXPORT_SYMBOL(pci_get_subsys);
  283. /**
  284. * pci_get_device - begin or continue searching for a PCI device by vendor/device id
  285. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  286. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  287. * @from: Previous PCI device found in search, or %NULL for new search.
  288. *
  289. * Iterates through the list of known PCI devices. If a PCI device is
  290. * found with a matching @vendor and @device, the reference count to the
  291. * device is incremented and a pointer to its device structure is returned.
  292. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  293. * as the @from argument. Otherwise if @from is not %NULL, searches continue
  294. * from next device on the global list. The reference count for @from is
  295. * always decremented if it is not %NULL.
  296. */
  297. struct pci_dev *pci_get_device(unsigned int vendor, unsigned int device,
  298. struct pci_dev *from)
  299. {
  300. return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  301. }
  302. EXPORT_SYMBOL(pci_get_device);
  303. /**
  304. * pci_get_class - begin or continue searching for a PCI device by class
  305. * @class: search for a PCI device with this class designation
  306. * @from: Previous PCI device found in search, or %NULL for new search.
  307. *
  308. * Iterates through the list of known PCI devices. If a PCI device is
  309. * found with a matching @class, the reference count to the device is
  310. * incremented and a pointer to its device structure is returned.
  311. * Otherwise, %NULL is returned.
  312. * A new search is initiated by passing %NULL as the @from argument.
  313. * Otherwise if @from is not %NULL, searches continue from next device
  314. * on the global list. The reference count for @from is always decremented
  315. * if it is not %NULL.
  316. */
  317. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  318. {
  319. struct pci_device_id id = {
  320. .vendor = PCI_ANY_ID,
  321. .device = PCI_ANY_ID,
  322. .subvendor = PCI_ANY_ID,
  323. .subdevice = PCI_ANY_ID,
  324. .class_mask = PCI_ANY_ID,
  325. .class = class,
  326. };
  327. return pci_get_dev_by_id(&id, from);
  328. }
  329. EXPORT_SYMBOL(pci_get_class);
  330. /**
  331. * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
  332. * @ids: A pointer to a null terminated list of struct pci_device_id structures
  333. * that describe the type of PCI device the caller is trying to find.
  334. *
  335. * Obvious fact: You do not have a reference to any device that might be found
  336. * by this function, so if that device is removed from the system right after
  337. * this function is finished, the value will be stale. Use this function to
  338. * find devices that are usually built into a system, or for a general hint as
  339. * to if another device happens to be present at this specific moment in time.
  340. */
  341. int pci_dev_present(const struct pci_device_id *ids)
  342. {
  343. struct pci_dev *found = NULL;
  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);