2 * PCI HotPlug Utility functions
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * Send feedback to <greg@kroah.com>
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/slab.h>
34 #include <linux/pci.h>
35 #include "pci_hotplug.h"
38 #if !defined(CONFIG_HOTPLUG_PCI_MODULE)
39 #define MY_NAME "pci_hotplug"
41 #define MY_NAME THIS_MODULE->name
44 #define dbg(fmt, arg...) do { if (debug) printk(KERN_DEBUG "%s: %s: " fmt , MY_NAME , __FUNCTION__ , ## arg); } while (0)
45 #define err(format, arg...) printk(KERN_ERR "%s: " format , MY_NAME , ## arg)
46 #define info(format, arg...) printk(KERN_INFO "%s: " format , MY_NAME , ## arg)
47 #define warn(format, arg...) printk(KERN_WARNING "%s: " format , MY_NAME , ## arg)
53 static int build_dev (struct pci_bus *bus, unsigned int devfn, struct pci_dev **pci_dev)
55 struct pci_dev *my_dev;
57 my_dev = kmalloc (sizeof (struct pci_dev), GFP_KERNEL);
61 memset(my_dev, 0, sizeof(struct pci_dev));
63 my_dev->devfn = devfn;
65 my_dev->sysdata = bus->sysdata;
71 * pci_bus_read_config_byte - read a byte from a pci device
72 * @bus: pointer to the parent bus of the pci device to read from
73 * @devfn: the device / function of the pci device to read from
74 * @where: the location in the pci address space to read from
75 * @value: pointer to where to place the data read
77 * Like pci_read_config_byte() but works for pci devices that do not have a
78 * pci_dev structure set up yet.
79 * Returns 0 on success.
81 int pci_bus_read_config_byte (struct pci_bus *bus, unsigned int devfn, int where, u8 *value)
83 struct pci_dev *dev = NULL;
86 dbg("%p, %d, %d, %p\n", bus, devfn, where, value);
87 dev = pci_find_slot(bus->number, devfn);
89 dbg("using native pci_dev\n");
90 return pci_read_config_byte (dev, where, value);
93 result = build_dev(bus, devfn, &dev);
96 result = pci_read_config_byte(dev, where, value);
102 * pci_bus_read_config_word - read a word from a pci device
103 * @bus: pointer to the parent bus of the pci device to read from
104 * @devfn: the device / function of the pci device to read from
105 * @where: the location on the pci address space to read from
106 * @value: pointer to where to place the data read
108 * Like pci_read_config_word() but works for pci devices that do not have a
109 * pci_dev structure set up yet.
110 * Returns 0 on success.
112 int pci_bus_read_config_word (struct pci_bus *bus, unsigned int devfn, int where, u16 *value)
114 struct pci_dev *dev = NULL;
117 dbg("%p, %d, %d, %p\n", bus, devfn, where, value);
118 dev = pci_find_slot(bus->number, devfn);
120 dbg("using native pci_dev\n");
121 return pci_read_config_word (dev, where, value);
124 result = build_dev(bus, devfn, &dev);
127 result = pci_read_config_word(dev, where, value);
133 * pci_bus_read_config_dword - read a dword from a pci device
134 * @bus: pointer to the parent bus of the pci device to read from
135 * @devfn: the device / function of the pci device to read from
136 * @where: the location on the pci address space to read from
137 * @value: pointer to where to place the data read
139 * Like pci_read_config_dword() but works for pci devices that do not have a
140 * pci_dev structure set up yet.
141 * Returns 0 on success.
143 int pci_bus_read_config_dword (struct pci_bus *bus, unsigned int devfn, int where, u32 *value)
145 struct pci_dev *dev = NULL;
148 dbg("%p, %d, %d, %p\n", bus, devfn, where, value);
149 dev = pci_find_slot(bus->number, devfn);
151 dbg("using native pci_dev\n");
152 return pci_read_config_dword (dev, where, value);
155 result = build_dev(bus, devfn, &dev);
158 result = pci_read_config_dword(dev, where, value);
164 * pci_bus_write_config_byte - write a byte to a pci device
165 * @bus: pointer to the parent bus of the pci device to read from
166 * @devfn: the device / function of the pci device to read from
167 * @where: the location on the pci address space to write to
168 * @value: the value to write to the pci device
170 * Like pci_write_config_byte() but works for pci devices that do not have a
171 * pci_dev structure set up yet.
172 * Returns 0 on success.
174 int pci_bus_write_config_byte (struct pci_bus *bus, unsigned int devfn, int where, u8 value)
176 struct pci_dev *dev = NULL;
179 dbg("%p, %d, %d, %d\n", bus, devfn, where, value);
180 dev = pci_find_slot(bus->number, devfn);
182 dbg("using native pci_dev\n");
183 return pci_write_config_byte (dev, where, value);
186 result = build_dev(bus, devfn, &dev);
189 result = pci_write_config_byte(dev, where, value);
195 * pci_bus_write_config_word - write a word to a pci device
196 * @bus: pointer to the parent bus of the pci device to read from
197 * @devfn: the device / function of the pci device to read from
198 * @where: the location on the pci address space to write to
199 * @value: the value to write to the pci device
201 * Like pci_write_config_word() but works for pci devices that do not have a
202 * pci_dev structure set up yet.
203 * Returns 0 on success.
205 int pci_bus_write_config_word (struct pci_bus *bus, unsigned int devfn, int where, u16 value)
207 struct pci_dev *dev = NULL;
210 dbg("%p, %d, %d, %d\n", bus, devfn, where, value);
211 dev = pci_find_slot(bus->number, devfn);
213 dbg("using native pci_dev\n");
214 return pci_write_config_word (dev, where, value);
217 result = build_dev(bus, devfn, &dev);
220 result = pci_write_config_word(dev, where, value);
226 * pci_bus_write_config_dword - write a dword to a pci device
227 * @bus: pointer to the parent bus of the pci device to read from
228 * @devfn: the device / function of the pci device to read from
229 * @where: the location on the pci address space to write to
230 * @value: the value to write to the pci device
232 * Like pci_write_config_dword() but works for pci devices that do not have a
233 * pci_dev structure set up yet.
234 * Returns 0 on success.
236 int pci_bus_write_config_dword (struct pci_bus *bus, unsigned int devfn, int where, u32 value)
238 struct pci_dev *dev = NULL;
241 dbg("%p, %d, %d, %d\n", bus, devfn, where, value);
242 dev = pci_find_slot(bus->number, devfn);
244 dbg("using native pci_dev\n");
245 return pci_write_config_dword (dev, where, value);
248 result = build_dev(bus, devfn, &dev);
251 result = pci_write_config_dword(dev, where, value);
257 * This is code that scans the pci buses.
258 * Every bus and every function is presented to a custom
259 * function that can act upon it.
262 static int pci_visit_bus (struct pci_visit * fn, struct pci_bus_wrapped *wrapped_bus, struct pci_dev_wrapped *wrapped_parent)
264 struct list_head *ln;
266 struct pci_dev_wrapped wrapped_dev;
269 dbg("scanning bus %02x\n", wrapped_bus->bus->number);
271 if (fn->pre_visit_pci_bus) {
272 result = fn->pre_visit_pci_bus(wrapped_bus, wrapped_parent);
277 ln = wrapped_bus->bus->devices.next;
278 while (ln != &wrapped_bus->bus->devices) {
282 memset(&wrapped_dev, 0, sizeof(struct pci_dev_wrapped));
283 wrapped_dev.dev = dev;
285 result = pci_visit_dev(fn, &wrapped_dev, wrapped_bus);
290 if (fn->post_visit_pci_bus)
291 result = fn->post_visit_pci_bus(wrapped_bus, wrapped_parent);
297 static int pci_visit_bridge (struct pci_visit * fn, struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_parent)
299 struct pci_bus *bus = wrapped_dev->dev->subordinate;
300 struct pci_bus_wrapped wrapped_bus;
303 memset(&wrapped_bus, 0, sizeof(struct pci_bus_wrapped));
304 wrapped_bus.bus = bus;
306 dbg("scanning bridge %02x, %02x\n", wrapped_dev->dev->devfn >> 3,
307 wrapped_dev->dev->devfn & 0x7);
309 if (fn->visit_pci_dev) {
310 result = fn->visit_pci_dev(wrapped_dev, wrapped_parent);
315 result = pci_visit_bus(fn, &wrapped_bus, wrapped_dev);
320 int pci_visit_dev (struct pci_visit *fn, struct pci_dev_wrapped *wrapped_dev, struct pci_bus_wrapped *wrapped_parent)
322 struct pci_dev* dev = wrapped_dev ? wrapped_dev->dev : NULL;
328 if (fn->pre_visit_pci_dev) {
329 result = fn->pre_visit_pci_dev(wrapped_dev, wrapped_parent);
334 switch (dev->class >> 8) {
335 case PCI_CLASS_BRIDGE_PCI:
336 result = pci_visit_bridge(fn, wrapped_dev,
342 dbg("scanning device %02x, %02x\n",
343 PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn));
344 if (fn->visit_pci_dev) {
345 result = fn->visit_pci_dev (wrapped_dev,
352 if (fn->post_visit_pci_dev)
353 result = fn->post_visit_pci_dev(wrapped_dev, wrapped_parent);
358 /* Compatibility wrapper functions */
360 static struct pci_bus *alloc_bus(struct pci_ops *ops, u8 bus_nr)
362 struct pci_bus *bus = kmalloc(sizeof(struct pci_bus), GFP_KERNEL);
365 memset(bus, 0, sizeof(struct pci_bus));
366 bus->number = bus_nr;
371 int pci_read_config_byte_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u8 *value)
374 struct pci_bus *bus = alloc_bus(ops, bus_nr);
377 result = pci_bus_read_config_byte(bus, PCI_DEVFN(slot, function),
383 int pci_read_config_word_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u16 *value)
386 struct pci_bus *bus = alloc_bus(ops, bus_nr);
389 result = pci_bus_read_config_word(bus, PCI_DEVFN(slot, function),
395 int pci_read_config_dword_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u32 *value)
398 struct pci_bus *bus = alloc_bus(ops, bus_nr);
401 result = pci_bus_read_config_dword(bus, PCI_DEVFN(slot, function),
407 int pci_write_config_byte_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u8 value)
410 struct pci_bus *bus = alloc_bus(ops, bus_nr);
413 result = pci_bus_write_config_byte(bus, PCI_DEVFN(slot, function),
419 int pci_write_config_word_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u16 value)
422 struct pci_bus *bus = alloc_bus(ops, bus_nr);
425 result = pci_bus_write_config_word(bus, PCI_DEVFN(slot, function),
431 int pci_write_config_dword_nodev(struct pci_ops *ops, u8 bus_nr, u8 slot, u8 function, int where, u32 value)
434 struct pci_bus *bus = alloc_bus(ops, bus_nr);
437 result = pci_bus_write_config_dword(bus, PCI_DEVFN(slot, function),
443 EXPORT_SYMBOL(pci_visit_dev);
444 EXPORT_SYMBOL(pci_bus_read_config_byte);
445 EXPORT_SYMBOL(pci_bus_read_config_word);
446 EXPORT_SYMBOL(pci_bus_read_config_dword);
447 EXPORT_SYMBOL(pci_bus_write_config_byte);
448 EXPORT_SYMBOL(pci_bus_write_config_word);
449 EXPORT_SYMBOL(pci_bus_write_config_dword);
450 EXPORT_SYMBOL(pci_read_config_byte_nodev);
451 EXPORT_SYMBOL(pci_read_config_word_nodev);
452 EXPORT_SYMBOL(pci_read_config_dword_nodev);
453 EXPORT_SYMBOL(pci_write_config_byte_nodev);
454 EXPORT_SYMBOL(pci_write_config_word_nodev);
455 EXPORT_SYMBOL(pci_write_config_dword_nodev);