search.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. if (tmp->dev_flags & PCI_DEV_FLAGS_PCI_BRIDGE_NO_ALIAS)
  78. continue;
  79. ret = fn(tmp,
  80. PCI_DEVID(tmp->subordinate->number,
  81. PCI_DEVFN(0, 0)), data);
  82. if (ret)
  83. return ret;
  84. continue;
  85. case PCI_EXP_TYPE_PCIE_BRIDGE:
  86. ret = fn(tmp, pci_dev_id(tmp), data);
  87. if (ret)
  88. return ret;
  89. continue;
  90. }
  91. } else {
  92. if (tmp->dev_flags & PCI_DEV_FLAG_PCIE_BRIDGE_ALIAS)
  93. ret = fn(tmp,
  94. PCI_DEVID(tmp->subordinate->number,
  95. PCI_DEVFN(0, 0)), data);
  96. else
  97. ret = fn(tmp, pci_dev_id(tmp), data);
  98. if (ret)
  99. return ret;
  100. }
  101. }
  102. return ret;
  103. }
  104. static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
  105. {
  106. struct pci_bus *child;
  107. struct pci_bus *tmp;
  108. if (bus->number == busnr)
  109. return bus;
  110. list_for_each_entry(tmp, &bus->children, node) {
  111. child = pci_do_find_bus(tmp, busnr);
  112. if (child)
  113. return child;
  114. }
  115. return NULL;
  116. }
  117. /**
  118. * pci_find_bus - locate PCI bus from a given domain and bus number
  119. * @domain: number of PCI domain to search
  120. * @busnr: number of desired PCI bus
  121. *
  122. * Given a PCI bus number and domain number, the desired PCI bus is located
  123. * in the global list of PCI buses. If the bus is found, a pointer to its
  124. * data structure is returned. If no bus is found, %NULL is returned.
  125. */
  126. struct pci_bus *pci_find_bus(int domain, int busnr)
  127. {
  128. struct pci_bus *bus = NULL;
  129. struct pci_bus *tmp_bus;
  130. while ((bus = pci_find_next_bus(bus)) != NULL) {
  131. if (pci_domain_nr(bus) != domain)
  132. continue;
  133. tmp_bus = pci_do_find_bus(bus, busnr);
  134. if (tmp_bus)
  135. return tmp_bus;
  136. }
  137. return NULL;
  138. }
  139. EXPORT_SYMBOL(pci_find_bus);
  140. /**
  141. * pci_find_next_bus - begin or continue searching for a PCI bus
  142. * @from: Previous PCI bus found, or %NULL for new search.
  143. *
  144. * Iterates through the list of known PCI buses. A new search is
  145. * initiated by passing %NULL as the @from argument. Otherwise if
  146. * @from is not %NULL, searches continue from next device on the
  147. * global list.
  148. */
  149. struct pci_bus *pci_find_next_bus(const struct pci_bus *from)
  150. {
  151. struct list_head *n;
  152. struct pci_bus *b = NULL;
  153. down_read(&pci_bus_sem);
  154. n = from ? from->node.next : pci_root_buses.next;
  155. if (n != &pci_root_buses)
  156. b = list_entry(n, struct pci_bus, node);
  157. up_read(&pci_bus_sem);
  158. return b;
  159. }
  160. EXPORT_SYMBOL(pci_find_next_bus);
  161. /**
  162. * pci_get_slot - locate PCI device for a given PCI slot
  163. * @bus: PCI bus on which desired PCI device resides
  164. * @devfn: encodes number of PCI slot in which the desired PCI
  165. * device resides and the logical device number within that slot
  166. * in case of multi-function devices.
  167. *
  168. * Given a PCI bus and slot/function number, the desired PCI device
  169. * is located in the list of PCI devices.
  170. * If the device is found, its reference count is increased and this
  171. * function returns a pointer to its data structure. The caller must
  172. * decrement the reference count by calling pci_dev_put().
  173. * If no device is found, %NULL is returned.
  174. */
  175. struct pci_dev *pci_get_slot(struct pci_bus *bus, unsigned int devfn)
  176. {
  177. struct pci_dev *dev;
  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. 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. static struct pci_dev *pci_get_dev_by_id_reverse(const struct pci_device_id *id,
  257. struct pci_dev *from)
  258. {
  259. struct device *dev;
  260. struct device *dev_start = NULL;
  261. struct pci_dev *pdev = NULL;
  262. if (from)
  263. dev_start = &from->dev;
  264. dev = bus_find_device_reverse(&pci_bus_type, dev_start, (void *)id,
  265. match_pci_dev_by_id);
  266. if (dev)
  267. pdev = to_pci_dev(dev);
  268. pci_dev_put(from);
  269. return pdev;
  270. }
  271. enum pci_search_direction {
  272. PCI_SEARCH_FORWARD,
  273. PCI_SEARCH_REVERSE,
  274. };
  275. static struct pci_dev *__pci_get_subsys(unsigned int vendor, unsigned int device,
  276. unsigned int ss_vendor, unsigned int ss_device,
  277. struct pci_dev *from, enum pci_search_direction dir)
  278. {
  279. struct pci_device_id id = {
  280. .vendor = vendor,
  281. .device = device,
  282. .subvendor = ss_vendor,
  283. .subdevice = ss_device,
  284. };
  285. if (dir == PCI_SEARCH_FORWARD)
  286. return pci_get_dev_by_id(&id, from);
  287. else
  288. return pci_get_dev_by_id_reverse(&id, from);
  289. }
  290. /**
  291. * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
  292. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  293. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  294. * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
  295. * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
  296. * @from: Previous PCI device found in search, or %NULL for new search.
  297. *
  298. * Iterates through the list of known PCI devices. If a PCI device is found
  299. * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
  300. * device structure is returned, and the reference count to the device is
  301. * incremented. Otherwise, %NULL is returned. A new search is initiated by
  302. * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
  303. * searches continue from next device on the global list.
  304. * The reference count for @from is always decremented if it is not %NULL.
  305. */
  306. struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
  307. unsigned int ss_vendor, unsigned int ss_device,
  308. struct pci_dev *from)
  309. {
  310. return __pci_get_subsys(vendor, device, ss_vendor, ss_device, from,
  311. PCI_SEARCH_FORWARD);
  312. }
  313. EXPORT_SYMBOL(pci_get_subsys);
  314. /**
  315. * pci_get_device - begin or continue searching for a PCI device by vendor/device id
  316. * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
  317. * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
  318. * @from: Previous PCI device found in search, or %NULL for new search.
  319. *
  320. * Iterates through the list of known PCI devices. If a PCI device is
  321. * found with a matching @vendor and @device, the reference count to the
  322. * device is incremented and a pointer to its device structure is returned.
  323. * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
  324. * as the @from argument. Otherwise if @from is not %NULL, searches continue
  325. * from next device on the global list. The reference count for @from is
  326. * always decremented if it is not %NULL.
  327. */
  328. struct pci_dev *pci_get_device(unsigned int vendor, unsigned int device,
  329. struct pci_dev *from)
  330. {
  331. return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
  332. }
  333. EXPORT_SYMBOL(pci_get_device);
  334. /*
  335. * Same semantics as pci_get_device(), except walks the PCI device list
  336. * in reverse discovery order.
  337. */
  338. struct pci_dev *pci_get_device_reverse(unsigned int vendor,
  339. unsigned int device,
  340. struct pci_dev *from)
  341. {
  342. return __pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from,
  343. PCI_SEARCH_REVERSE);
  344. }
  345. EXPORT_SYMBOL(pci_get_device_reverse);
  346. /**
  347. * pci_get_class - begin or continue searching for a PCI device by class
  348. * @class: search for a PCI device with this class designation
  349. * @from: Previous PCI device found in search, or %NULL for new search.
  350. *
  351. * Iterates through the list of known PCI devices. If a PCI device is
  352. * found with a matching @class, the reference count to the device is
  353. * incremented and a pointer to its device structure is returned.
  354. * Otherwise, %NULL is returned.
  355. * A new search is initiated by passing %NULL as the @from argument.
  356. * Otherwise if @from is not %NULL, searches continue from next device
  357. * on the global list. The reference count for @from is always decremented
  358. * if it is not %NULL.
  359. */
  360. struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
  361. {
  362. struct pci_device_id id = {
  363. .vendor = PCI_ANY_ID,
  364. .device = PCI_ANY_ID,
  365. .subvendor = PCI_ANY_ID,
  366. .subdevice = PCI_ANY_ID,
  367. .class_mask = PCI_ANY_ID,
  368. .class = class,
  369. };
  370. return pci_get_dev_by_id(&id, from);
  371. }
  372. EXPORT_SYMBOL(pci_get_class);
  373. /**
  374. * pci_get_base_class - searching for a PCI device by matching against the base class code only
  375. * @class: search for a PCI device with this base class code
  376. * @from: Previous PCI device found in search, or %NULL for new search.
  377. *
  378. * Iterates through the list of known PCI devices. If a PCI device is found
  379. * with a matching base class code, the reference count to the device is
  380. * incremented. See pci_match_one_device() to figure out how does this works.
  381. * A new search is initiated by passing %NULL as the @from argument.
  382. * Otherwise if @from is not %NULL, searches continue from next device on the
  383. * global list. The reference count for @from is always decremented if it is
  384. * not %NULL.
  385. *
  386. * Returns:
  387. * A pointer to a matched PCI device, %NULL Otherwise.
  388. */
  389. struct pci_dev *pci_get_base_class(unsigned int class, struct pci_dev *from)
  390. {
  391. struct pci_device_id id = {
  392. .vendor = PCI_ANY_ID,
  393. .device = PCI_ANY_ID,
  394. .subvendor = PCI_ANY_ID,
  395. .subdevice = PCI_ANY_ID,
  396. .class_mask = 0xFF0000,
  397. .class = class << 16,
  398. };
  399. return pci_get_dev_by_id(&id, from);
  400. }
  401. EXPORT_SYMBOL(pci_get_base_class);
  402. /**
  403. * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
  404. * @ids: A pointer to a null terminated list of struct pci_device_id structures
  405. * that describe the type of PCI device the caller is trying to find.
  406. *
  407. * Obvious fact: You do not have a reference to any device that might be found
  408. * by this function, so if that device is removed from the system right after
  409. * this function is finished, the value will be stale. Use this function to
  410. * find devices that are usually built into a system, or for a general hint as
  411. * to if another device happens to be present at this specific moment in time.
  412. */
  413. int pci_dev_present(const struct pci_device_id *ids)
  414. {
  415. struct pci_dev *found = NULL;
  416. while (ids->vendor || ids->subvendor || ids->class_mask) {
  417. found = pci_get_dev_by_id(ids, NULL);
  418. if (found) {
  419. pci_dev_put(found);
  420. return 1;
  421. }
  422. ids++;
  423. }
  424. return 0;
  425. }
  426. EXPORT_SYMBOL(pci_dev_present);