2 * Fake PCI Hot Plug Controller Driver
4 * Copyright (C) 2003 Greg Kroah-Hartman <greg@kroah.com>
5 * Copyright (C) 2003 IBM Corp.
6 * Copyright (C) 2003 Rolf Eike Beer <eike-kernel@sf-tec.de>
8 * Based on ideas and code from:
9 * Vladimir Kondratiev <vladimir.kondratiev@intel.com>
10 * Rolf Eike Beer <eike-kernel@sf-tec.de>
12 * All rights reserved.
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, version 2 of the License.
18 * Send feedback to <greg@kroah.com>
23 * This driver will "emulate" removing PCI devices from the system. If
24 * the "power" file is written to with "0" then the specified PCI device
25 * will be completely removed from the kernel.
27 * WARNING, this does NOT turn off the power to the PCI device. This is
28 * a "logical" removal, not a physical or electrical removal.
30 * Use this module at your own risk, you have been warned!
32 * Enabling PCI devices is left as an exercise for the reader...
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/pci_hotplug.h>
39 #include <linux/init.h>
40 #include <linux/string.h>
41 #include <linux/slab.h>
42 #include <linux/workqueue.h>
46 #define MY_NAME "fakephp"
48 #define MY_NAME THIS_MODULE->name
51 #define dbg(format, arg...) \
54 printk(KERN_DEBUG "%s: " format, \
57 #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME , ## arg)
58 #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME , ## arg)
60 #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
61 #define DRIVER_DESC "Fake PCI Hot Plug Controller Driver"
64 struct list_head node;
65 struct hotplug_slot *slot;
67 struct work_struct remove_work;
68 unsigned long removed;
73 static LIST_HEAD(slot_list);
74 static struct workqueue_struct *dummyphp_wq;
76 static void pci_rescan_worker(struct work_struct *work);
77 static DECLARE_WORK(pci_rescan_work, pci_rescan_worker);
79 static int enable_slot (struct hotplug_slot *slot);
80 static int disable_slot (struct hotplug_slot *slot);
82 static struct hotplug_slot_ops dummy_hotplug_slot_ops = {
84 .enable_slot = enable_slot,
85 .disable_slot = disable_slot,
88 static void dummy_release(struct hotplug_slot *slot)
90 struct dummy_slot *dslot = slot->private;
92 list_del(&dslot->node);
93 kfree(dslot->slot->info);
95 pci_dev_put(dslot->dev);
99 #define SLOT_NAME_SIZE 8
101 static int add_slot(struct pci_dev *dev)
103 struct dummy_slot *dslot;
104 struct hotplug_slot *slot;
105 char name[SLOT_NAME_SIZE];
106 int retval = -ENOMEM;
107 static int count = 1;
109 slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
113 slot->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
117 slot->info->power_status = 1;
118 slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
119 slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
121 dslot = kzalloc(sizeof(struct dummy_slot), GFP_KERNEL);
126 snprintf(name, SLOT_NAME_SIZE, "fake");
128 snprintf(name, SLOT_NAME_SIZE, "fake%d", count++);
129 dbg("slot->name = %s\n", name);
130 slot->ops = &dummy_hotplug_slot_ops;
131 slot->release = &dummy_release;
132 slot->private = dslot;
134 retval = pci_hp_register(slot, dev->bus, PCI_SLOT(dev->devfn), name);
136 err("pci_hp_register failed with error %d\n", retval);
140 dbg("slot->name = %s\n", hotplug_slot_name(slot));
142 dslot->dev = pci_dev_get(dev);
143 list_add (&dslot->node, &slot_list);
156 static int __init pci_scan_buses(void)
158 struct pci_dev *dev = NULL;
161 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
162 if (PCI_FUNC(dev->devfn) > 0 &&
163 lastslot == PCI_SLOT(dev->devfn))
165 lastslot = PCI_SLOT(dev->devfn);
172 static void remove_slot(struct dummy_slot *dslot)
176 dbg("removing slot %s\n", hotplug_slot_name(dslot->slot));
177 retval = pci_hp_deregister(dslot->slot);
179 err("Problem unregistering a slot %s\n",
180 hotplug_slot_name(dslot->slot));
183 /* called from the single-threaded workqueue handler to remove a slot */
184 static void remove_slot_worker(struct work_struct *work)
186 struct dummy_slot *dslot =
187 container_of(work, struct dummy_slot, remove_work);
192 * pci_rescan_slot - Rescan slot
193 * @temp: Device template. Should be set: bus and devfn.
195 * Tries hard not to re-enable already existing devices;
196 * also handles scanning of subfunctions.
198 static int pci_rescan_slot(struct pci_dev *temp)
200 struct pci_bus *bus = temp->bus;
206 if (!pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) {
207 temp->hdr_type = hdr_type & 0x7f;
208 if ((dev = pci_get_slot(bus, temp->devfn)) != NULL)
211 dev = pci_scan_single_device(bus, temp->devfn);
213 dbg("New device on %s function %x:%x\n",
214 bus->name, temp->devfn >> 3,
219 /* multifunction device? */
220 if (!(hdr_type & 0x80))
223 /* continue scanning for other functions */
224 for (func = 1, temp->devfn++; func < 8; func++, temp->devfn++) {
225 if (pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type))
227 temp->hdr_type = hdr_type & 0x7f;
229 if ((dev = pci_get_slot(bus, temp->devfn)) != NULL)
232 dev = pci_scan_single_device(bus, temp->devfn);
234 dbg("New device on %s function %x:%x\n",
235 bus->name, temp->devfn >> 3,
248 * pci_rescan_bus - Rescan PCI bus
249 * @bus: the PCI bus to rescan
251 * Call pci_rescan_slot for each possible function of the bus.
253 static void pci_rescan_bus(const struct pci_bus *bus)
259 dev = alloc_pci_dev();
263 dev->bus = (struct pci_bus*)bus;
264 dev->sysdata = bus->sysdata;
265 for (devfn = 0; devfn < 0x100; devfn += 8) {
267 found += pci_rescan_slot(dev);
271 pci_bus_assign_resources(bus);
272 list_for_each_entry(dev, &bus->devices, bus_list) {
273 /* Skip already-added devices */
276 retval = pci_bus_add_device(dev);
279 "Error adding device, continuing\n");
283 pci_bus_add_devices(bus);
288 /* recursively scan all buses */
289 static void pci_rescan_buses(const struct list_head *list)
291 const struct list_head *l;
292 list_for_each(l,list) {
293 const struct pci_bus *b = pci_bus_b(l);
295 pci_rescan_buses(&b->children);
299 /* initiate rescan of all pci buses */
300 static inline void pci_rescan(void) {
301 pci_rescan_buses(&pci_root_buses);
304 /* called from the single-threaded workqueue handler to rescan all pci buses */
305 static void pci_rescan_worker(struct work_struct *work)
310 static int enable_slot(struct hotplug_slot *hotplug_slot)
312 /* mis-use enable_slot for rescanning of the pci bus */
313 cancel_work_sync(&pci_rescan_work);
314 queue_work(dummyphp_wq, &pci_rescan_work);
318 static int disable_slot(struct hotplug_slot *slot)
320 struct dummy_slot *dslot;
326 dslot = slot->private;
328 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot_name(slot));
330 for (func = 7; func >= 0; func--) {
331 dev = pci_get_slot(dslot->dev->bus, dslot->dev->devfn + func);
335 if (test_and_set_bit(0, &dslot->removed)) {
336 dbg("Slot already scheduled for removal\n");
341 /* remove the device from the pci core */
342 pci_remove_bus_device(dev);
344 /* queue work item to blow away this sysfs entry and other
347 INIT_WORK(&dslot->remove_work, remove_slot_worker);
348 queue_work(dummyphp_wq, &dslot->remove_work);
355 static void cleanup_slots (void)
357 struct list_head *tmp;
358 struct list_head *next;
359 struct dummy_slot *dslot;
361 destroy_workqueue(dummyphp_wq);
362 list_for_each_safe (tmp, next, &slot_list) {
363 dslot = list_entry (tmp, struct dummy_slot, node);
369 static int __init dummyphp_init(void)
371 info(DRIVER_DESC "\n");
373 dummyphp_wq = create_singlethread_workqueue(MY_NAME);
377 return pci_scan_buses();
381 static void __exit dummyphp_exit(void)
386 module_init(dummyphp_init);
387 module_exit(dummyphp_exit);
389 MODULE_AUTHOR(DRIVER_AUTHOR);
390 MODULE_DESCRIPTION(DRIVER_DESC);
391 MODULE_LICENSE("GPL");
392 module_param(debug, bool, S_IRUGO | S_IWUSR);
393 MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
394 module_param(dup_slots, bool, S_IRUGO | S_IWUSR);
395 MODULE_PARM_DESC(dup_slots, "Force duplicate slot names for debugging");