search.c 13 KB

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