diff options
Diffstat (limited to 'drivers/pci/search.c')
-rw-r--r-- | drivers/pci/search.c | 313 |
1 files changed, 121 insertions, 192 deletions
diff --git a/drivers/pci/search.c b/drivers/pci/search.c index 8541034021f0..217814fef4ef 100644 --- a/drivers/pci/search.c +++ b/drivers/pci/search.c | |||
@@ -114,31 +114,63 @@ pci_find_next_bus(const struct pci_bus *from) | |||
114 | } | 114 | } |
115 | 115 | ||
116 | #ifdef CONFIG_PCI_LEGACY | 116 | #ifdef CONFIG_PCI_LEGACY |
117 | |||
118 | /** | 117 | /** |
119 | * pci_find_slot - locate PCI device from a given PCI slot | 118 | * pci_find_slot - locate PCI device from a given PCI slot |
120 | * @bus: number of PCI bus on which desired PCI device resides | 119 | * @bus: number of PCI bus on which desired PCI device resides |
121 | * @devfn: encodes number of PCI slot in which the desired PCI | 120 | * @devfn: encodes number of PCI slot in which the desired PCI |
122 | * device resides and the logical device number within that slot | 121 | * device resides and the logical device number within that slot |
123 | * in case of multi-function devices. | 122 | * in case of multi-function devices. |
124 | * | 123 | * |
125 | * Given a PCI bus and slot/function number, the desired PCI device | 124 | * Given a PCI bus and slot/function number, the desired PCI device |
126 | * is located in system global list of PCI devices. If the device | 125 | * is located in system global list of PCI devices. If the device |
127 | * is found, a pointer to its data structure is returned. If no | 126 | * is found, a pointer to its data structure is returned. If no |
128 | * device is found, %NULL is returned. | 127 | * device is found, %NULL is returned. |
128 | * | ||
129 | * NOTE: Do not use this function any more; use pci_get_slot() instead, as | ||
130 | * the PCI device returned by this function can disappear at any moment in | ||
131 | * time. | ||
129 | */ | 132 | */ |
130 | struct pci_dev * | 133 | struct pci_dev *pci_find_slot(unsigned int bus, unsigned int devfn) |
131 | pci_find_slot(unsigned int bus, unsigned int devfn) | ||
132 | { | 134 | { |
133 | struct pci_dev *dev = NULL; | 135 | struct pci_dev *dev = NULL; |
134 | 136 | ||
135 | while ((dev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { | 137 | while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { |
136 | if (dev->bus->number == bus && dev->devfn == devfn) | 138 | if (dev->bus->number == bus && dev->devfn == devfn) { |
139 | pci_dev_put(dev); | ||
137 | return dev; | 140 | return dev; |
141 | } | ||
138 | } | 142 | } |
139 | return NULL; | 143 | return NULL; |
140 | } | 144 | } |
145 | EXPORT_SYMBOL(pci_find_slot); | ||
146 | |||
147 | /** | ||
148 | * pci_find_device - begin or continue searching for a PCI device by vendor/device id | ||
149 | * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids | ||
150 | * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids | ||
151 | * @from: Previous PCI device found in search, or %NULL for new search. | ||
152 | * | ||
153 | * Iterates through the list of known PCI devices. If a PCI device is found | ||
154 | * with a matching @vendor and @device, a pointer to its device structure is | ||
155 | * returned. Otherwise, %NULL is returned. | ||
156 | * A new search is initiated by passing %NULL as the @from argument. | ||
157 | * Otherwise if @from is not %NULL, searches continue from next device | ||
158 | * on the global list. | ||
159 | * | ||
160 | * NOTE: Do not use this function any more; use pci_get_device() instead, as | ||
161 | * the PCI device returned by this function can disappear at any moment in | ||
162 | * time. | ||
163 | */ | ||
164 | struct pci_dev *pci_find_device(unsigned int vendor, unsigned int device, | ||
165 | const struct pci_dev *from) | ||
166 | { | ||
167 | struct pci_dev *pdev; | ||
141 | 168 | ||
169 | pdev = pci_get_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from); | ||
170 | pci_dev_put(pdev); | ||
171 | return pdev; | ||
172 | } | ||
173 | EXPORT_SYMBOL(pci_find_device); | ||
142 | #endif /* CONFIG_PCI_LEGACY */ | 174 | #endif /* CONFIG_PCI_LEGACY */ |
143 | 175 | ||
144 | /** | 176 | /** |
@@ -204,86 +236,52 @@ struct pci_dev * pci_get_bus_and_slot(unsigned int bus, unsigned int devfn) | |||
204 | return NULL; | 236 | return NULL; |
205 | } | 237 | } |
206 | 238 | ||
207 | #ifdef CONFIG_PCI_LEGACY | 239 | static int match_pci_dev_by_id(struct device *dev, void *data) |
208 | /** | ||
209 | * pci_find_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id | ||
210 | * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids | ||
211 | * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids | ||
212 | * @ss_vendor: PCI subsystem vendor id to match, or %PCI_ANY_ID to match all vendor ids | ||
213 | * @ss_device: PCI subsystem device id to match, or %PCI_ANY_ID to match all device ids | ||
214 | * @from: Previous PCI device found in search, or %NULL for new search. | ||
215 | * | ||
216 | * Iterates through the list of known PCI devices. If a PCI device is | ||
217 | * found with a matching @vendor, @device, @ss_vendor and @ss_device, a | ||
218 | * pointer to its device structure is returned. Otherwise, %NULL is returned. | ||
219 | * A new search is initiated by passing %NULL as the @from argument. | ||
220 | * Otherwise if @from is not %NULL, searches continue from next device | ||
221 | * on the global list. | ||
222 | * | ||
223 | * NOTE: Do not use this function any more; use pci_get_subsys() instead, as | ||
224 | * the PCI device returned by this function can disappear at any moment in | ||
225 | * time. | ||
226 | */ | ||
227 | static struct pci_dev * pci_find_subsys(unsigned int vendor, | ||
228 | unsigned int device, | ||
229 | unsigned int ss_vendor, | ||
230 | unsigned int ss_device, | ||
231 | const struct pci_dev *from) | ||
232 | { | 240 | { |
233 | struct list_head *n; | 241 | struct pci_dev *pdev = to_pci_dev(dev); |
234 | struct pci_dev *dev; | 242 | struct pci_device_id *id = data; |
235 | |||
236 | WARN_ON(in_interrupt()); | ||
237 | 243 | ||
238 | /* | 244 | if (pci_match_one_device(id, pdev)) |
239 | * pci_find_subsys() can be called on the ide_setup() path, super-early | 245 | return 1; |
240 | * in boot. But the down_read() will enable local interrupts, which | 246 | return 0; |
241 | * can cause some machines to crash. So here we detect and flag that | ||
242 | * situation and bail out early. | ||
243 | */ | ||
244 | if (unlikely(no_pci_devices())) | ||
245 | return NULL; | ||
246 | down_read(&pci_bus_sem); | ||
247 | n = from ? from->global_list.next : pci_devices.next; | ||
248 | |||
249 | while (n && (n != &pci_devices)) { | ||
250 | dev = pci_dev_g(n); | ||
251 | if ((vendor == PCI_ANY_ID || dev->vendor == vendor) && | ||
252 | (device == PCI_ANY_ID || dev->device == device) && | ||
253 | (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) && | ||
254 | (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device)) | ||
255 | goto exit; | ||
256 | n = n->next; | ||
257 | } | ||
258 | dev = NULL; | ||
259 | exit: | ||
260 | up_read(&pci_bus_sem); | ||
261 | return dev; | ||
262 | } | 247 | } |
263 | 248 | ||
264 | /** | 249 | /* |
265 | * pci_find_device - begin or continue searching for a PCI device by vendor/device id | 250 | * pci_get_dev_by_id - begin or continue searching for a PCI device by id |
266 | * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids | 251 | * @id: pointer to struct pci_device_id to match for the device |
267 | * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids | ||
268 | * @from: Previous PCI device found in search, or %NULL for new search. | 252 | * @from: Previous PCI device found in search, or %NULL for new search. |
269 | * | 253 | * |
270 | * Iterates through the list of known PCI devices. If a PCI device is found | 254 | * Iterates through the list of known PCI devices. If a PCI device is found |
271 | * with a matching @vendor and @device, a pointer to its device structure is | 255 | * with a matching id a pointer to its device structure is returned, and the |
272 | * returned. Otherwise, %NULL is returned. | 256 | * reference count to the device is incremented. Otherwise, %NULL is returned. |
273 | * A new search is initiated by passing %NULL as the @from argument. | 257 | * A new search is initiated by passing %NULL as the @from argument. Otherwise |
274 | * Otherwise if @from is not %NULL, searches continue from next device | 258 | * if @from is not %NULL, searches continue from next device on the global |
275 | * on the global list. | 259 | * list. The reference count for @from is always decremented if it is not |
276 | * | 260 | * %NULL. |
277 | * NOTE: Do not use this function any more; use pci_get_device() instead, as | 261 | * |
278 | * the PCI device returned by this function can disappear at any moment in | 262 | * This is an internal function for use by the other search functions in |
279 | * time. | 263 | * this file. |
280 | */ | 264 | */ |
281 | struct pci_dev * | 265 | static struct pci_dev *pci_get_dev_by_id(const struct pci_device_id *id, |
282 | pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev *from) | 266 | const struct pci_dev *from) |
283 | { | 267 | { |
284 | return pci_find_subsys(vendor, device, PCI_ANY_ID, PCI_ANY_ID, from); | 268 | struct device *dev; |
269 | struct device *dev_start = NULL; | ||
270 | struct pci_dev *pdev = NULL; | ||
271 | |||
272 | WARN_ON(in_interrupt()); | ||
273 | if (from) { | ||
274 | /* FIXME | ||
275 | * take the cast off, when bus_find_device is made const. | ||
276 | */ | ||
277 | dev_start = (struct device *)&from->dev; | ||
278 | } | ||
279 | dev = bus_find_device(&pci_bus_type, dev_start, (void *)id, | ||
280 | match_pci_dev_by_id); | ||
281 | if (dev) | ||
282 | pdev = to_pci_dev(dev); | ||
283 | return pdev; | ||
285 | } | 284 | } |
286 | #endif /* CONFIG_PCI_LEGACY */ | ||
287 | 285 | ||
288 | /** | 286 | /** |
289 | * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id | 287 | * pci_get_subsys - begin or continue searching for a PCI device by vendor/subvendor/device/subdevice id |
@@ -301,42 +299,34 @@ pci_find_device(unsigned int vendor, unsigned int device, const struct pci_dev * | |||
301 | * searches continue from next device on the global list. | 299 | * searches continue from next device on the global list. |
302 | * The reference count for @from is always decremented if it is not %NULL. | 300 | * The reference count for @from is always decremented if it is not %NULL. |
303 | */ | 301 | */ |
304 | struct pci_dev * | 302 | struct pci_dev *pci_get_subsys(unsigned int vendor, unsigned int device, |
305 | pci_get_subsys(unsigned int vendor, unsigned int device, | 303 | unsigned int ss_vendor, unsigned int ss_device, |
306 | unsigned int ss_vendor, unsigned int ss_device, | 304 | const struct pci_dev *from) |
307 | struct pci_dev *from) | ||
308 | { | 305 | { |
309 | struct list_head *n; | 306 | struct pci_dev *pdev; |
310 | struct pci_dev *dev; | 307 | struct pci_device_id *id; |
311 | |||
312 | WARN_ON(in_interrupt()); | ||
313 | 308 | ||
314 | /* | 309 | /* |
315 | * pci_get_subsys() can potentially be called by drivers super-early | 310 | * pci_find_subsys() can be called on the ide_setup() path, |
316 | * in boot. But the down_read() will enable local interrupts, which | 311 | * super-early in boot. But the down_read() will enable local |
317 | * can cause some machines to crash. So here we detect and flag that | 312 | * interrupts, which can cause some machines to crash. So here we |
318 | * situation and bail out early. | 313 | * detect and flag that situation and bail out early. |
319 | */ | 314 | */ |
320 | if (unlikely(no_pci_devices())) | 315 | if (unlikely(no_pci_devices())) |
321 | return NULL; | 316 | return NULL; |
322 | down_read(&pci_bus_sem); | 317 | |
323 | n = from ? from->global_list.next : pci_devices.next; | 318 | id = kzalloc(sizeof(*id), GFP_KERNEL); |
324 | 319 | if (!id) | |
325 | while (n && (n != &pci_devices)) { | 320 | return NULL; |
326 | dev = pci_dev_g(n); | 321 | id->vendor = vendor; |
327 | if ((vendor == PCI_ANY_ID || dev->vendor == vendor) && | 322 | id->device = device; |
328 | (device == PCI_ANY_ID || dev->device == device) && | 323 | id->subvendor = ss_vendor; |
329 | (ss_vendor == PCI_ANY_ID || dev->subsystem_vendor == ss_vendor) && | 324 | id->subdevice = ss_device; |
330 | (ss_device == PCI_ANY_ID || dev->subsystem_device == ss_device)) | 325 | |
331 | goto exit; | 326 | pdev = pci_get_dev_by_id(id, from); |
332 | n = n->next; | 327 | kfree(id); |
333 | } | 328 | |
334 | dev = NULL; | 329 | return pdev; |
335 | exit: | ||
336 | dev = pci_dev_get(dev); | ||
337 | up_read(&pci_bus_sem); | ||
338 | pci_dev_put(from); | ||
339 | return dev; | ||
340 | } | 330 | } |
341 | 331 | ||
342 | /** | 332 | /** |
@@ -360,46 +350,6 @@ pci_get_device(unsigned int vendor, unsigned int device, struct pci_dev *from) | |||
360 | } | 350 | } |
361 | 351 | ||
362 | /** | 352 | /** |
363 | * pci_get_device_reverse - begin or continue searching for a PCI device by vendor/device id | ||
364 | * @vendor: PCI vendor id to match, or %PCI_ANY_ID to match all vendor ids | ||
365 | * @device: PCI device id to match, or %PCI_ANY_ID to match all device ids | ||
366 | * @from: Previous PCI device found in search, or %NULL for new search. | ||
367 | * | ||
368 | * Iterates through the list of known PCI devices in the reverse order of | ||
369 | * pci_get_device. | ||
370 | * If a PCI device is found with a matching @vendor and @device, the reference | ||
371 | * count to the device is incremented and a pointer to its device structure | ||
372 | * is returned Otherwise, %NULL is returned. A new search is initiated by | ||
373 | * passing %NULL as the @from argument. Otherwise if @from is not %NULL, | ||
374 | * searches continue from next device on the global list. The reference | ||
375 | * count for @from is always decremented if it is not %NULL. | ||
376 | */ | ||
377 | struct pci_dev * | ||
378 | pci_get_device_reverse(unsigned int vendor, unsigned int device, struct pci_dev *from) | ||
379 | { | ||
380 | struct list_head *n; | ||
381 | struct pci_dev *dev; | ||
382 | |||
383 | WARN_ON(in_interrupt()); | ||
384 | down_read(&pci_bus_sem); | ||
385 | n = from ? from->global_list.prev : pci_devices.prev; | ||
386 | |||
387 | while (n && (n != &pci_devices)) { | ||
388 | dev = pci_dev_g(n); | ||
389 | if ((vendor == PCI_ANY_ID || dev->vendor == vendor) && | ||
390 | (device == PCI_ANY_ID || dev->device == device)) | ||
391 | goto exit; | ||
392 | n = n->prev; | ||
393 | } | ||
394 | dev = NULL; | ||
395 | exit: | ||
396 | dev = pci_dev_get(dev); | ||
397 | up_read(&pci_bus_sem); | ||
398 | pci_dev_put(from); | ||
399 | return dev; | ||
400 | } | ||
401 | |||
402 | /** | ||
403 | * pci_get_class - begin or continue searching for a PCI device by class | 353 | * pci_get_class - begin or continue searching for a PCI device by class |
404 | * @class: search for a PCI device with this class designation | 354 | * @class: search for a PCI device with this class designation |
405 | * @from: Previous PCI device found in search, or %NULL for new search. | 355 | * @from: Previous PCI device found in search, or %NULL for new search. |
@@ -415,46 +365,21 @@ exit: | |||
415 | */ | 365 | */ |
416 | struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from) | 366 | struct pci_dev *pci_get_class(unsigned int class, struct pci_dev *from) |
417 | { | 367 | { |
418 | struct list_head *n; | ||
419 | struct pci_dev *dev; | 368 | struct pci_dev *dev; |
369 | struct pci_device_id *id; | ||
420 | 370 | ||
421 | WARN_ON(in_interrupt()); | 371 | id = kzalloc(sizeof(*id), GFP_KERNEL); |
422 | down_read(&pci_bus_sem); | 372 | if (!id) |
423 | n = from ? from->global_list.next : pci_devices.next; | 373 | return NULL; |
374 | id->vendor = id->device = id->subvendor = id->subdevice = PCI_ANY_ID; | ||
375 | id->class_mask = PCI_ANY_ID; | ||
376 | id->class = class; | ||
424 | 377 | ||
425 | while (n && (n != &pci_devices)) { | 378 | dev = pci_get_dev_by_id(id, from); |
426 | dev = pci_dev_g(n); | 379 | kfree(id); |
427 | if (dev->class == class) | ||
428 | goto exit; | ||
429 | n = n->next; | ||
430 | } | ||
431 | dev = NULL; | ||
432 | exit: | ||
433 | dev = pci_dev_get(dev); | ||
434 | up_read(&pci_bus_sem); | ||
435 | pci_dev_put(from); | ||
436 | return dev; | 380 | return dev; |
437 | } | 381 | } |
438 | 382 | ||
439 | const struct pci_device_id *pci_find_present(const struct pci_device_id *ids) | ||
440 | { | ||
441 | struct pci_dev *dev; | ||
442 | const struct pci_device_id *found = NULL; | ||
443 | |||
444 | WARN_ON(in_interrupt()); | ||
445 | down_read(&pci_bus_sem); | ||
446 | while (ids->vendor || ids->subvendor || ids->class_mask) { | ||
447 | list_for_each_entry(dev, &pci_devices, global_list) { | ||
448 | if ((found = pci_match_one_device(ids, dev)) != NULL) | ||
449 | goto exit; | ||
450 | } | ||
451 | ids++; | ||
452 | } | ||
453 | exit: | ||
454 | up_read(&pci_bus_sem); | ||
455 | return found; | ||
456 | } | ||
457 | |||
458 | /** | 383 | /** |
459 | * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not. | 384 | * pci_dev_present - Returns 1 if device matching the device list is present, 0 if not. |
460 | * @ids: A pointer to a null terminated list of struct pci_device_id structures | 385 | * @ids: A pointer to a null terminated list of struct pci_device_id structures |
@@ -468,23 +393,27 @@ exit: | |||
468 | */ | 393 | */ |
469 | int pci_dev_present(const struct pci_device_id *ids) | 394 | int pci_dev_present(const struct pci_device_id *ids) |
470 | { | 395 | { |
471 | return pci_find_present(ids) == NULL ? 0 : 1; | 396 | struct pci_dev *found = NULL; |
472 | } | ||
473 | 397 | ||
398 | WARN_ON(in_interrupt()); | ||
399 | while (ids->vendor || ids->subvendor || ids->class_mask) { | ||
400 | found = pci_get_dev_by_id(ids, NULL); | ||
401 | if (found) | ||
402 | goto exit; | ||
403 | ids++; | ||
404 | } | ||
405 | exit: | ||
406 | if (found) | ||
407 | return 1; | ||
408 | return 0; | ||
409 | } | ||
474 | EXPORT_SYMBOL(pci_dev_present); | 410 | EXPORT_SYMBOL(pci_dev_present); |
475 | EXPORT_SYMBOL(pci_find_present); | ||
476 | |||
477 | #ifdef CONFIG_PCI_LEGACY | ||
478 | EXPORT_SYMBOL(pci_find_device); | ||
479 | EXPORT_SYMBOL(pci_find_slot); | ||
480 | #endif /* CONFIG_PCI_LEGACY */ | ||
481 | 411 | ||
482 | /* For boot time work */ | 412 | /* For boot time work */ |
483 | EXPORT_SYMBOL(pci_find_bus); | 413 | EXPORT_SYMBOL(pci_find_bus); |
484 | EXPORT_SYMBOL(pci_find_next_bus); | 414 | EXPORT_SYMBOL(pci_find_next_bus); |
485 | /* For everyone */ | 415 | /* For everyone */ |
486 | EXPORT_SYMBOL(pci_get_device); | 416 | EXPORT_SYMBOL(pci_get_device); |
487 | EXPORT_SYMBOL(pci_get_device_reverse); | ||
488 | EXPORT_SYMBOL(pci_get_subsys); | 417 | EXPORT_SYMBOL(pci_get_subsys); |
489 | EXPORT_SYMBOL(pci_get_slot); | 418 | EXPORT_SYMBOL(pci_get_slot); |
490 | EXPORT_SYMBOL(pci_get_bus_and_slot); | 419 | EXPORT_SYMBOL(pci_get_bus_and_slot); |