blob: 33e0f033a48e7e2f6097b13d4425d8d17b1cabb7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Bjorn Helgaasf7625982013-11-14 11:28:18 -07002 * PCI searching functions.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090011#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/interrupt.h>
14#include "pci.h"
15
Zhang Yanmind71374d2006-06-02 12:35:43 +080016DECLARE_RWSEM(pci_bus_sem);
Amos Kongce29ca32012-05-23 10:20:35 -060017EXPORT_SYMBOL_GPL(pci_bus_sem);
18
Keshavamurthy, Anil S994a65e2007-10-21 16:41:46 -070019/*
Alex Williamsonc25dc822014-05-22 17:07:30 -060020 * pci_for_each_dma_alias - Iterate over DMA aliases for a device
21 * @pdev: starting downstream device
22 * @fn: function to call for each alias
23 * @data: opaque data to pass to @fn
24 *
25 * Starting @pdev, walk up the bus calling @fn for each possible alias
26 * of @pdev at the root bus.
27 */
28int pci_for_each_dma_alias(struct pci_dev *pdev,
29 int (*fn)(struct pci_dev *pdev,
30 u16 alias, void *data), void *data)
31{
32 struct pci_bus *bus;
33 int ret;
34
35 ret = fn(pdev, PCI_DEVID(pdev->bus->number, pdev->devfn), data);
36 if (ret)
37 return ret;
38
Alex Williamson31c2b812014-05-22 17:07:43 -060039 /*
40 * If the device is broken and uses an alias requester ID for
41 * DMA, iterate over that too.
42 */
Jacek Lawrynowicz338c3142016-03-03 15:38:02 +010043 if (unlikely(pdev->dma_alias_mask)) {
44 u8 devfn;
45
46 for_each_set_bit(devfn, pdev->dma_alias_mask, U8_MAX) {
47 ret = fn(pdev, PCI_DEVID(pdev->bus->number, devfn),
48 data);
49 if (ret)
50 return ret;
51 }
Alex Williamson31c2b812014-05-22 17:07:43 -060052 }
53
Alex Williamsonc25dc822014-05-22 17:07:30 -060054 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
55 struct pci_dev *tmp;
56
57 /* Skip virtual buses */
58 if (!bus->self)
59 continue;
60
61 tmp = bus->self;
62
63 /*
64 * PCIe-to-PCI/X bridges alias transactions from downstream
65 * devices using the subordinate bus number (PCI Express to
66 * PCI/PCI-X Bridge Spec, rev 1.0, sec 2.3). For all cases
67 * where the upstream bus is PCI/X we alias to the bridge
68 * (there are various conditions in the previous reference
69 * where the bridge may take ownership of transactions, even
70 * when the secondary interface is PCI-X).
71 */
72 if (pci_is_pcie(tmp)) {
73 switch (pci_pcie_type(tmp)) {
74 case PCI_EXP_TYPE_ROOT_PORT:
75 case PCI_EXP_TYPE_UPSTREAM:
76 case PCI_EXP_TYPE_DOWNSTREAM:
77 continue;
78 case PCI_EXP_TYPE_PCI_BRIDGE:
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,
87 PCI_DEVID(tmp->bus->number,
88 tmp->devfn), data);
89 if (ret)
90 return ret;
91 continue;
92 }
93 } else {
Alex Williamsonc8fe16e2014-05-28 14:57:02 -060094 if (tmp->dev_flags & PCI_DEV_FLAG_PCIE_BRIDGE_ALIAS)
95 ret = fn(tmp,
96 PCI_DEVID(tmp->subordinate->number,
97 PCI_DEVFN(0, 0)), data);
98 else
99 ret = fn(tmp,
100 PCI_DEVID(tmp->bus->number,
101 tmp->devfn), data);
Alex Williamsonc25dc822014-05-22 17:07:30 -0600102 if (ret)
103 return ret;
104 }
105 }
106
107 return ret;
108}
109
Sam Ravnborg96bde062007-03-26 21:53:30 -0800110static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800112 struct pci_bus *child;
113 struct pci_bus *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400115 if (bus->number == busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return bus;
117
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800118 list_for_each_entry(tmp, &bus->children, node) {
119 child = pci_do_find_bus(tmp, busnr);
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400120 if (child)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 return child;
122 }
123 return NULL;
124}
125
126/**
127 * pci_find_bus - locate PCI bus from a given domain and bus number
128 * @domain: number of PCI domain to search
129 * @busnr: number of desired PCI bus
130 *
131 * Given a PCI bus number and domain number, the desired PCI bus is located
132 * in the global list of PCI buses. If the bus is found, a pointer to its
133 * data structure is returned. If no bus is found, %NULL is returned.
134 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400135struct pci_bus *pci_find_bus(int domain, int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct pci_bus *bus = NULL;
138 struct pci_bus *tmp_bus;
139
140 while ((bus = pci_find_next_bus(bus)) != NULL) {
141 if (pci_domain_nr(bus) != domain)
142 continue;
143 tmp_bus = pci_do_find_bus(bus, busnr);
144 if (tmp_bus)
145 return tmp_bus;
146 }
147 return NULL;
148}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600149EXPORT_SYMBOL(pci_find_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151/**
152 * pci_find_next_bus - begin or continue searching for a PCI bus
153 * @from: Previous PCI bus found, or %NULL for new search.
154 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700155 * Iterates through the list of known PCI buses. A new search is
Randy Dunlapd75763d2006-07-30 03:03:41 -0700156 * initiated by passing %NULL as the @from argument. Otherwise if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 * @from is not %NULL, searches continue from next device on the
158 * global list.
159 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400160struct pci_bus *pci_find_next_bus(const struct pci_bus *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
162 struct list_head *n;
163 struct pci_bus *b = NULL;
164
165 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800166 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 n = from ? from->node.next : pci_root_buses.next;
168 if (n != &pci_root_buses)
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800169 b = list_entry(n, struct pci_bus, node);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800170 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 return b;
172}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600173EXPORT_SYMBOL(pci_find_next_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175/**
176 * pci_get_slot - locate PCI device for a given PCI slot
177 * @bus: PCI bus on which desired PCI device resides
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700178 * @devfn: encodes number of PCI slot in which the desired PCI
179 * device resides and the logical device number within that slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * in case of multi-function devices.
181 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700182 * Given a PCI bus and slot/function number, the desired PCI device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * is located in the list of PCI devices.
184 * If the device is found, its reference count is increased and this
185 * function returns a pointer to its data structure. The caller must
186 * decrement the reference count by calling pci_dev_put().
187 * If no device is found, %NULL is returned.
188 */
Bjorn Helgaas66455f52012-08-17 15:53:27 -0600189struct pci_dev *pci_get_slot(struct pci_bus *bus, unsigned int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct pci_dev *dev;
192
193 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800194 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Bjorn Helgaas66455f52012-08-17 15:53:27 -0600196 list_for_each_entry(dev, &bus->devices, bus_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (dev->devfn == devfn)
198 goto out;
199 }
200
201 dev = NULL;
202 out:
203 pci_dev_get(dev);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800204 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return dev;
206}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600207EXPORT_SYMBOL(pci_get_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209/**
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600210 * pci_get_domain_bus_and_slot - locate PCI device for a given PCI domain (segment), bus, and slot
211 * @domain: PCI domain/segment on which the PCI device resides.
212 * @bus: PCI bus on which desired PCI device resides
213 * @devfn: encodes number of PCI slot in which the desired PCI device
214 * resides and the logical device number within that slot in case of
215 * multi-function devices.
Alan Cox29f3eb62006-10-16 16:20:21 -0700216 *
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600217 * Given a PCI domain, bus, and slot/function number, the desired PCI
218 * device is located in the list of PCI devices. If the device is
219 * found, its reference count is increased and this function returns a
220 * pointer to its data structure. The caller must decrement the
221 * reference count by calling pci_dev_put(). If no device is found,
222 * %NULL is returned.
Alan Cox29f3eb62006-10-16 16:20:21 -0700223 */
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600224struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
225 unsigned int devfn)
Alan Cox29f3eb62006-10-16 16:20:21 -0700226{
227 struct pci_dev *dev = NULL;
228
Kulikov Vasiliy4e344b12010-07-03 20:04:39 +0400229 for_each_pci_dev(dev) {
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600230 if (pci_domain_nr(dev->bus) == domain &&
231 (dev->bus->number == bus && dev->devfn == devfn))
Alan Cox29f3eb62006-10-16 16:20:21 -0700232 return dev;
233 }
234 return NULL;
235}
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600236EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
Alan Cox29f3eb62006-10-16 16:20:21 -0700237
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800238static int match_pci_dev_by_id(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800240 struct pci_dev *pdev = to_pci_dev(dev);
241 struct pci_device_id *id = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800243 if (pci_match_one_device(id, pdev))
244 return 1;
245 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800248/*
249 * pci_get_dev_by_id - begin or continue searching for a PCI device by id
250 * @id: pointer to struct pci_device_id to match for the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * @from: Previous PCI device found in search, or %NULL for new search.
252 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700253 * Iterates through the list of known PCI devices. If a PCI device is found
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800254 * with a matching id a pointer to its device structure is returned, and the
255 * reference count to the device is incremented. Otherwise, %NULL is returned.
256 * A new search is initiated by passing %NULL as the @from argument. Otherwise
257 * if @from is not %NULL, searches continue from next device on the global
258 * list. The reference count for @from is always decremented if it is not
259 * %NULL.
260 *
261 * This is an internal function for use by the other search functions in
262 * this file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 */
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800264static struct pci_dev *pci_get_dev_by_id(const struct pci_device_id *id,
Greg KHb08508c2008-08-26 08:20:34 -0700265 struct pci_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800267 struct device *dev;
268 struct device *dev_start = NULL;
269 struct pci_dev *pdev = NULL;
270
271 WARN_ON(in_interrupt());
Matthew Wilcoxc4ed02f2008-10-20 19:13:08 -0600272 if (from)
273 dev_start = &from->dev;
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800274 dev = bus_find_device(&pci_bus_type, dev_start, (void *)id,
275 match_pci_dev_by_id);
276 if (dev)
277 pdev = to_pci_dev(dev);
Markus Elfringff0387c2014-11-10 21:02:17 -0700278 pci_dev_put(from);
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800279 return pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/**
283 * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice 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 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
287 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
288 * @from: Previous PCI device found in search, or %NULL for new search.
289 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700290 * Iterates through the list of known PCI devices. If a PCI device is found
291 * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * device structure is returned, and the reference count to the device is
293 * incremented. Otherwise, %NULL is returned. A new search is initiated by
Randy Dunlapd75763d2006-07-30 03:03:41 -0700294 * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * searches continue from next device on the global list.
296 * The reference count for @from is always decremented if it is not %NULL.
297 */
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800298struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
299 unsigned int ss_vendor, unsigned int ss_device,
Greg KHb08508c2008-08-26 08:20:34 -0700300 struct pci_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Feng Tangb9443f42012-08-23 15:45:03 +0800302 struct pci_device_id id = {
303 .vendor = vendor,
304 .device = device,
305 .subvendor = ss_vendor,
306 .subdevice = ss_device,
307 };
Ard van Breemen6ae4adf2007-01-05 16:36:21 -0800308
Feng Tangb9443f42012-08-23 15:45:03 +0800309 return pci_get_dev_by_id(&id, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600311EXPORT_SYMBOL(pci_get_subsys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
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
Randy Dunlapd75763d2006-07-30 03:03:41 -0700323 * as the @from argument. Otherwise if @from is not %NULL, searches continue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 * from next device on the global list. The reference count for @from is
325 * always decremented if it is not %NULL.
326 */
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400327struct pci_dev *pci_get_device(unsigned int vendor, unsigned int device,
328 struct pci_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
331}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600332EXPORT_SYMBOL(pci_get_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Alan Cox29f3eb62006-10-16 16:20:21 -0700334/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * pci_get_class - begin or continue searching for a PCI device by class
336 * @class: search for a PCI device with this class designation
337 * @from: Previous PCI device found in search, or %NULL for new search.
338 *
339 * Iterates through the list of known PCI devices. If a PCI device is
340 * found with a matching @class, the reference count to the device is
341 * incremented and a pointer to its device structure is returned.
342 * Otherwise, %NULL is returned.
Randy Dunlapd75763d2006-07-30 03:03:41 -0700343 * A new search is initiated by passing %NULL as the @from argument.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * Otherwise if @from is not %NULL, searches continue from next device
345 * on the global list. The reference count for @from is always decremented
346 * if it is not %NULL.
347 */
348struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
349{
Feng Tangb9443f42012-08-23 15:45:03 +0800350 struct pci_device_id id = {
351 .vendor = PCI_ANY_ID,
352 .device = PCI_ANY_ID,
353 .subvendor = PCI_ANY_ID,
354 .subdevice = PCI_ANY_ID,
355 .class_mask = PCI_ANY_ID,
356 .class = class,
357 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Feng Tangb9443f42012-08-23 15:45:03 +0800359 return pci_get_dev_by_id(&id, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
Ryan Desfossesb7fe9432014-04-25 14:32:25 -0600361EXPORT_SYMBOL(pci_get_class);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Greg Kroah-Hartman448432c2008-02-12 13:36:20 -0800363/**
364 * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
365 * @ids: A pointer to a null terminated list of struct pci_device_id structures
366 * that describe the type of PCI device the caller is trying to find.
367 *
368 * Obvious fact: You do not have a reference to any device that might be found
369 * by this function, so if that device is removed from the system right after
370 * this function is finished, the value will be stale. Use this function to
371 * find devices that are usually built into a system, or for a general hint as
372 * to if another device happens to be present at this specific moment in time.
373 */
374int pci_dev_present(const struct pci_device_id *ids)
Alan Coxd86f90f2006-12-04 15:14:44 -0800375{
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800376 struct pci_dev *found = NULL;
Alan Coxd86f90f2006-12-04 15:14:44 -0800377
378 WARN_ON(in_interrupt());
Alan Coxd86f90f2006-12-04 15:14:44 -0800379 while (ids->vendor || ids->subvendor || ids->class_mask) {
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800380 found = pci_get_dev_by_id(ids, NULL);
Jiang Liud5af7d92013-01-21 13:20:45 -0800381 if (found) {
382 pci_dev_put(found);
383 return 1;
384 }
Alan Coxd86f90f2006-12-04 15:14:44 -0800385 ids++;
386 }
Jiang Liud5af7d92013-01-21 13:20:45 -0800387
Greg Kroah-Hartman448432c2008-02-12 13:36:20 -0800388 return 0;
Alan Coxd86f90f2006-12-04 15:14:44 -0800389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390EXPORT_SYMBOL(pci_dev_present);