blob: 5601cdb8bbb31677f5686f7845cdbdbcd96a784b [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
10#include <linux/init.h>
11#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090012#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/interrupt.h>
15#include "pci.h"
16
Zhang Yanmind71374d2006-06-02 12:35:43 +080017DECLARE_RWSEM(pci_bus_sem);
Amos Kongce29ca32012-05-23 10:20:35 -060018EXPORT_SYMBOL_GPL(pci_bus_sem);
19
Keshavamurthy, Anil S994a65e2007-10-21 16:41:46 -070020/*
Alex Williamsonc25dc822014-05-22 17:07:30 -060021 * pci_for_each_dma_alias - Iterate over DMA aliases for a device
22 * @pdev: starting downstream device
23 * @fn: function to call for each alias
24 * @data: opaque data to pass to @fn
25 *
26 * Starting @pdev, walk up the bus calling @fn for each possible alias
27 * of @pdev at the root bus.
28 */
29int pci_for_each_dma_alias(struct pci_dev *pdev,
30 int (*fn)(struct pci_dev *pdev,
31 u16 alias, void *data), void *data)
32{
33 struct pci_bus *bus;
34 int ret;
35
36 ret = fn(pdev, PCI_DEVID(pdev->bus->number, pdev->devfn), data);
37 if (ret)
38 return ret;
39
40 for (bus = pdev->bus; !pci_is_root_bus(bus); bus = bus->parent) {
41 struct pci_dev *tmp;
42
43 /* Skip virtual buses */
44 if (!bus->self)
45 continue;
46
47 tmp = bus->self;
48
49 /*
50 * PCIe-to-PCI/X bridges alias transactions from downstream
51 * devices using the subordinate bus number (PCI Express to
52 * PCI/PCI-X Bridge Spec, rev 1.0, sec 2.3). For all cases
53 * where the upstream bus is PCI/X we alias to the bridge
54 * (there are various conditions in the previous reference
55 * where the bridge may take ownership of transactions, even
56 * when the secondary interface is PCI-X).
57 */
58 if (pci_is_pcie(tmp)) {
59 switch (pci_pcie_type(tmp)) {
60 case PCI_EXP_TYPE_ROOT_PORT:
61 case PCI_EXP_TYPE_UPSTREAM:
62 case PCI_EXP_TYPE_DOWNSTREAM:
63 continue;
64 case PCI_EXP_TYPE_PCI_BRIDGE:
65 ret = fn(tmp,
66 PCI_DEVID(tmp->subordinate->number,
67 PCI_DEVFN(0, 0)), data);
68 if (ret)
69 return ret;
70 continue;
71 case PCI_EXP_TYPE_PCIE_BRIDGE:
72 ret = fn(tmp,
73 PCI_DEVID(tmp->bus->number,
74 tmp->devfn), data);
75 if (ret)
76 return ret;
77 continue;
78 }
79 } else {
80 ret = fn(tmp, PCI_DEVID(tmp->bus->number, tmp->devfn),
81 data);
82 if (ret)
83 return ret;
84 }
85 }
86
87 return ret;
88}
89
90/*
Stefan Assmann45e829e2009-12-03 06:49:24 -050091 * find the upstream PCIe-to-PCI bridge of a PCI device
Keshavamurthy, Anil S994a65e2007-10-21 16:41:46 -070092 * if the device is PCIE, return NULL
Stefan Assmann45e829e2009-12-03 06:49:24 -050093 * if the device isn't connected to a PCIe bridge (that is its parent is a
Keshavamurthy, Anil S994a65e2007-10-21 16:41:46 -070094 * legacy PCI bridge and the bridge is directly connected to bus 0), return its
95 * parent
96 */
97struct pci_dev *
98pci_find_upstream_pcie_bridge(struct pci_dev *pdev)
99{
100 struct pci_dev *tmp = NULL;
101
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900102 if (pci_is_pcie(pdev))
Keshavamurthy, Anil S994a65e2007-10-21 16:41:46 -0700103 return NULL;
104 while (1) {
Kenji Kaneshige6e3f36d2009-05-26 16:06:10 +0900105 if (pci_is_root_bus(pdev->bus))
Keshavamurthy, Anil S994a65e2007-10-21 16:41:46 -0700106 break;
107 pdev = pdev->bus->self;
108 /* a p2p bridge */
Kenji Kaneshige5f4d91a2009-11-11 14:36:17 +0900109 if (!pci_is_pcie(pdev)) {
Keshavamurthy, Anil S994a65e2007-10-21 16:41:46 -0700110 tmp = pdev;
111 continue;
112 }
Stefan Assmann45e829e2009-12-03 06:49:24 -0500113 /* PCI device should connect to a PCIe bridge */
Yijing Wang62f87c02012-07-24 17:20:03 +0800114 if (pci_pcie_type(pdev) != PCI_EXP_TYPE_PCI_BRIDGE) {
Keshavamurthy, Anil S994a65e2007-10-21 16:41:46 -0700115 /* Busted hardware? */
116 WARN_ON_ONCE(1);
117 return NULL;
118 }
119 return pdev;
120 }
121
122 return tmp;
123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Sam Ravnborg96bde062007-03-26 21:53:30 -0800125static struct pci_bus *pci_do_find_bus(struct pci_bus *bus, unsigned char busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800127 struct pci_bus *child;
128 struct pci_bus *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130 if(bus->number == busnr)
131 return bus;
132
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800133 list_for_each_entry(tmp, &bus->children, node) {
134 child = pci_do_find_bus(tmp, busnr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if(child)
136 return child;
137 }
138 return NULL;
139}
140
141/**
142 * pci_find_bus - locate PCI bus from a given domain and bus number
143 * @domain: number of PCI domain to search
144 * @busnr: number of desired PCI bus
145 *
146 * Given a PCI bus number and domain number, the desired PCI bus is located
147 * in the global list of PCI buses. If the bus is found, a pointer to its
148 * data structure is returned. If no bus is found, %NULL is returned.
149 */
Randy Dunlapc8439cfc2006-07-18 14:33:16 -0700150struct pci_bus * pci_find_bus(int domain, int busnr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 struct pci_bus *bus = NULL;
153 struct pci_bus *tmp_bus;
154
155 while ((bus = pci_find_next_bus(bus)) != NULL) {
156 if (pci_domain_nr(bus) != domain)
157 continue;
158 tmp_bus = pci_do_find_bus(bus, busnr);
159 if (tmp_bus)
160 return tmp_bus;
161 }
162 return NULL;
163}
164
165/**
166 * pci_find_next_bus - begin or continue searching for a PCI bus
167 * @from: Previous PCI bus found, or %NULL for new search.
168 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700169 * Iterates through the list of known PCI buses. A new search is
Randy Dunlapd75763d2006-07-30 03:03:41 -0700170 * initiated by passing %NULL as the @from argument. Otherwise if
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 * @from is not %NULL, searches continue from next device on the
172 * global list.
173 */
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700174struct pci_bus *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175pci_find_next_bus(const struct pci_bus *from)
176{
177 struct list_head *n;
178 struct pci_bus *b = NULL;
179
180 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800181 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 n = from ? from->node.next : pci_root_buses.next;
183 if (n != &pci_root_buses)
Yijing Wang94e6a9b2014-02-13 21:14:03 +0800184 b = list_entry(n, struct pci_bus, node);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800185 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return b;
187}
188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189/**
190 * pci_get_slot - locate PCI device for a given PCI slot
191 * @bus: PCI bus on which desired PCI device resides
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700192 * @devfn: encodes number of PCI slot in which the desired PCI
193 * device resides and the logical device number within that slot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 * in case of multi-function devices.
195 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700196 * Given a PCI bus and slot/function number, the desired PCI device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * is located in the list of PCI devices.
198 * If the device is found, its reference count is increased and this
199 * function returns a pointer to its data structure. The caller must
200 * decrement the reference count by calling pci_dev_put().
201 * If no device is found, %NULL is returned.
202 */
Bjorn Helgaas66455f52012-08-17 15:53:27 -0600203struct pci_dev *pci_get_slot(struct pci_bus *bus, unsigned int devfn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 struct pci_dev *dev;
206
207 WARN_ON(in_interrupt());
Zhang Yanmind71374d2006-06-02 12:35:43 +0800208 down_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Bjorn Helgaas66455f52012-08-17 15:53:27 -0600210 list_for_each_entry(dev, &bus->devices, bus_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (dev->devfn == devfn)
212 goto out;
213 }
214
215 dev = NULL;
216 out:
217 pci_dev_get(dev);
Zhang Yanmind71374d2006-06-02 12:35:43 +0800218 up_read(&pci_bus_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return dev;
220}
221
222/**
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600223 * pci_get_domain_bus_and_slot - locate PCI device for a given PCI domain (segment), bus, and slot
224 * @domain: PCI domain/segment on which the PCI device resides.
225 * @bus: PCI bus on which desired PCI device resides
226 * @devfn: encodes number of PCI slot in which the desired PCI device
227 * resides and the logical device number within that slot in case of
228 * multi-function devices.
Alan Cox29f3eb62006-10-16 16:20:21 -0700229 *
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600230 * Given a PCI domain, bus, and slot/function number, the desired PCI
231 * device is located in the list of PCI devices. If the device is
232 * found, its reference count is increased and this function returns a
233 * pointer to its data structure. The caller must decrement the
234 * reference count by calling pci_dev_put(). If no device is found,
235 * %NULL is returned.
Alan Cox29f3eb62006-10-16 16:20:21 -0700236 */
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600237struct pci_dev *pci_get_domain_bus_and_slot(int domain, unsigned int bus,
238 unsigned int devfn)
Alan Cox29f3eb62006-10-16 16:20:21 -0700239{
240 struct pci_dev *dev = NULL;
241
Kulikov Vasiliy4e344b12010-07-03 20:04:39 +0400242 for_each_pci_dev(dev) {
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600243 if (pci_domain_nr(dev->bus) == domain &&
244 (dev->bus->number == bus && dev->devfn == devfn))
Alan Cox29f3eb62006-10-16 16:20:21 -0700245 return dev;
246 }
247 return NULL;
248}
Andrew Patterson3c299dc2009-10-12 13:14:00 -0600249EXPORT_SYMBOL(pci_get_domain_bus_and_slot);
Alan Cox29f3eb62006-10-16 16:20:21 -0700250
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800251static int match_pci_dev_by_id(struct device *dev, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800253 struct pci_dev *pdev = to_pci_dev(dev);
254 struct pci_device_id *id = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800256 if (pci_match_one_device(id, pdev))
257 return 1;
258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800261/*
262 * pci_get_dev_by_id - begin or continue searching for a PCI device by id
263 * @id: pointer to struct pci_device_id to match for the device
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 * @from: Previous PCI device found in search, or %NULL for new search.
265 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700266 * Iterates through the list of known PCI devices. If a PCI device is found
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800267 * with a matching id a pointer to its device structure is returned, and the
268 * reference count to the device is incremented. Otherwise, %NULL is returned.
269 * A new search is initiated by passing %NULL as the @from argument. Otherwise
270 * if @from is not %NULL, searches continue from next device on the global
271 * list. The reference count for @from is always decremented if it is not
272 * %NULL.
273 *
274 * This is an internal function for use by the other search functions in
275 * this file.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 */
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800277static struct pci_dev *pci_get_dev_by_id(const struct pci_device_id *id,
Greg KHb08508c2008-08-26 08:20:34 -0700278 struct pci_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800280 struct device *dev;
281 struct device *dev_start = NULL;
282 struct pci_dev *pdev = NULL;
283
284 WARN_ON(in_interrupt());
Matthew Wilcoxc4ed02f2008-10-20 19:13:08 -0600285 if (from)
286 dev_start = &from->dev;
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800287 dev = bus_find_device(&pci_bus_type, dev_start, (void *)id,
288 match_pci_dev_by_id);
289 if (dev)
290 pdev = to_pci_dev(dev);
Greg KHebca4f12008-08-21 13:47:58 -0700291 if (from)
292 pci_dev_put(from);
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800293 return pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
296/**
297 * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id
298 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
299 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
300 * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids
301 * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids
302 * @from: Previous PCI device found in search, or %NULL for new search.
303 *
Randy Dunlapd75763d2006-07-30 03:03:41 -0700304 * Iterates through the list of known PCI devices. If a PCI device is found
305 * with a matching @vendor, @device, @ss_vendor and @ss_device, a pointer to its
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 * device structure is returned, and the reference count to the device is
307 * incremented. Otherwise, %NULL is returned. A new search is initiated by
Randy Dunlapd75763d2006-07-30 03:03:41 -0700308 * passing %NULL as the @from argument. Otherwise if @from is not %NULL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 * searches continue from next device on the global list.
310 * The reference count for @from is always decremented if it is not %NULL.
311 */
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800312struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device,
313 unsigned int ss_vendor, unsigned int ss_device,
Greg KHb08508c2008-08-26 08:20:34 -0700314 struct pci_dev *from)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315{
Feng Tangb9443f42012-08-23 15:45:03 +0800316 struct pci_device_id id = {
317 .vendor = vendor,
318 .device = device,
319 .subvendor = ss_vendor,
320 .subdevice = ss_device,
321 };
Ard van Breemen6ae4adf2007-01-05 16:36:21 -0800322
Feng Tangb9443f42012-08-23 15:45:03 +0800323 return pci_get_dev_by_id(&id, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324}
325
326/**
327 * pci_get_device - begin or continue searching for a PCI device by vendor/device id
328 * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids
329 * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids
330 * @from: Previous PCI device found in search, or %NULL for new search.
331 *
332 * Iterates through the list of known PCI devices. If a PCI device is
333 * found with a matching @vendor and @device, the reference count to the
334 * device is incremented and a pointer to its device structure is returned.
335 * Otherwise, %NULL is returned. A new search is initiated by passing %NULL
Randy Dunlapd75763d2006-07-30 03:03:41 -0700336 * as the @from argument. Otherwise if @from is not %NULL, searches continue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * from next device on the global list. The reference count for @from is
338 * always decremented if it is not %NULL.
339 */
340struct pci_dev *
341pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from)
342{
343 return pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from);
344}
345
Alan Cox29f3eb62006-10-16 16:20:21 -0700346/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * 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.
Randy Dunlapd75763d2006-07-30 03:03:41 -0700355 * A new search is initiated by passing %NULL as the @from argument.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 * 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 */
360struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from)
361{
Feng Tangb9443f42012-08-23 15:45:03 +0800362 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 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
Feng Tangb9443f42012-08-23 15:45:03 +0800371 return pci_get_dev_by_id(&id, from);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Greg Kroah-Hartman448432c2008-02-12 13:36:20 -0800374/**
375 * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not.
376 * @ids: A pointer to a null terminated list of struct pci_device_id structures
377 * that describe the type of PCI device the caller is trying to find.
378 *
379 * Obvious fact: You do not have a reference to any device that might be found
380 * by this function, so if that device is removed from the system right after
381 * this function is finished, the value will be stale. Use this function to
382 * find devices that are usually built into a system, or for a general hint as
383 * to if another device happens to be present at this specific moment in time.
384 */
385int pci_dev_present(const struct pci_device_id *ids)
Alan Coxd86f90f2006-12-04 15:14:44 -0800386{
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800387 struct pci_dev *found = NULL;
Alan Coxd86f90f2006-12-04 15:14:44 -0800388
389 WARN_ON(in_interrupt());
Alan Coxd86f90f2006-12-04 15:14:44 -0800390 while (ids->vendor || ids->subvendor || ids->class_mask) {
Greg Kroah-Hartman95247b52008-02-13 11:03:58 -0800391 found = pci_get_dev_by_id(ids, NULL);
Jiang Liud5af7d92013-01-21 13:20:45 -0800392 if (found) {
393 pci_dev_put(found);
394 return 1;
395 }
Alan Coxd86f90f2006-12-04 15:14:44 -0800396 ids++;
397 }
Jiang Liud5af7d92013-01-21 13:20:45 -0800398
Greg Kroah-Hartman448432c2008-02-12 13:36:20 -0800399 return 0;
Alan Coxd86f90f2006-12-04 15:14:44 -0800400}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401EXPORT_SYMBOL(pci_dev_present);
402
Alan Cox29f3eb62006-10-16 16:20:21 -0700403/* For boot time work */
404EXPORT_SYMBOL(pci_find_bus);
405EXPORT_SYMBOL(pci_find_next_bus);
406/* For everyone */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407EXPORT_SYMBOL(pci_get_device);
408EXPORT_SYMBOL(pci_get_subsys);
409EXPORT_SYMBOL(pci_get_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410EXPORT_SYMBOL(pci_get_class);