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