blob: 24dcbf13e98be124fb867d56de68c972e3647721 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Fake PCI Hot Plug Controller Driver
3 *
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>
7 *
8 * Based on ideas and code from:
9 * Vladimir Kondratiev <vladimir.kondratiev@intel.com>
10 * Rolf Eike Beer <eike-kernel@sf-tec.de>
11 *
12 * All rights reserved.
13 *
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.
17 *
18 * Send feedback to <greg@kroah.com>
19 */
20
21/*
22 *
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.
26 *
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.
29 *
30 * Use this module at your own risk, you have been warned!
31 *
32 * Enabling PCI devices is left as an exercise for the reader...
33 *
34 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070038#include <linux/pci_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/init.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080040#include <linux/string.h>
41#include <linux/slab.h>
Ian Abbott5c796ae2008-01-25 16:23:56 +000042#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include "../pci.h"
44
45#if !defined(MODULE)
46 #define MY_NAME "fakephp"
47#else
48 #define MY_NAME THIS_MODULE->name
49#endif
50
51#define dbg(format, arg...) \
52 do { \
53 if (debug) \
54 printk(KERN_DEBUG "%s: " format, \
55 MY_NAME , ## arg); \
56 } while (0)
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)
59
60#define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
61#define DRIVER_DESC "Fake PCI Hot Plug Controller Driver"
62
63struct dummy_slot {
64 struct list_head node;
65 struct hotplug_slot *slot;
66 struct pci_dev *dev;
Ian Abbott5c796ae2008-01-25 16:23:56 +000067 struct work_struct remove_work;
68 unsigned long removed;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
71static int debug;
72static LIST_HEAD(slot_list);
Ian Abbott5c796ae2008-01-25 16:23:56 +000073static struct workqueue_struct *dummyphp_wq;
74
75static void pci_rescan_worker(struct work_struct *work);
76static DECLARE_WORK(pci_rescan_work, pci_rescan_worker);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78static int enable_slot (struct hotplug_slot *slot);
79static int disable_slot (struct hotplug_slot *slot);
80
81static struct hotplug_slot_ops dummy_hotplug_slot_ops = {
82 .owner = THIS_MODULE,
83 .enable_slot = enable_slot,
84 .disable_slot = disable_slot,
85};
86
87static void dummy_release(struct hotplug_slot *slot)
88{
89 struct dummy_slot *dslot = slot->private;
90
91 list_del(&dslot->node);
92 kfree(dslot->slot->info);
93 kfree(dslot->slot);
94 pci_dev_put(dslot->dev);
95 kfree(dslot);
96}
97
Alex Chiang43caae82008-10-20 17:41:28 -060098#define SLOT_NAME_SIZE 8
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static int add_slot(struct pci_dev *dev)
101{
102 struct dummy_slot *dslot;
103 struct hotplug_slot *slot;
Alex Chiang43caae82008-10-20 17:41:28 -0600104 char name[SLOT_NAME_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 int retval = -ENOMEM;
Alex Chiangfe997402008-06-10 15:27:37 -0600106 static int count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100108 slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 if (!slot)
110 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Eric Sesterhennf5afe802006-02-28 15:34:49 +0100112 slot->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (!slot->info)
114 goto error_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116 slot->info->power_status = 1;
117 slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
118 slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
119
Ian Abbott5c796ae2008-01-25 16:23:56 +0000120 dslot = kzalloc(sizeof(struct dummy_slot), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (!dslot)
122 goto error_info;
123
Alex Chiang43caae82008-10-20 17:41:28 -0600124 snprintf(name, SLOT_NAME_SIZE, "fake%d", count++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 slot->ops = &dummy_hotplug_slot_ops;
126 slot->release = &dummy_release;
127 slot->private = dslot;
128
Alex Chiang43caae82008-10-20 17:41:28 -0600129 retval = pci_hp_register(slot, dev->bus, PCI_SLOT(dev->devfn), name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (retval) {
131 err("pci_hp_register failed with error %d\n", retval);
132 goto error_dslot;
133 }
134
Alex Chiang43caae82008-10-20 17:41:28 -0600135 dbg("slot->name = %s\n", hotplug_slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 dslot->slot = slot;
137 dslot->dev = pci_dev_get(dev);
138 list_add (&dslot->node, &slot_list);
139 return retval;
140
141error_dslot:
142 kfree(dslot);
143error_info:
144 kfree(slot->info);
145error_slot:
146 kfree(slot);
147error:
148 return retval;
149}
150
151static int __init pci_scan_buses(void)
152{
153 struct pci_dev *dev = NULL;
Alex Chiangfe997402008-06-10 15:27:37 -0600154 int lastslot = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
Alex Chiangfe997402008-06-10 15:27:37 -0600157 if (PCI_FUNC(dev->devfn) > 0 &&
158 lastslot == PCI_SLOT(dev->devfn))
159 continue;
160 lastslot = PCI_SLOT(dev->devfn);
161 add_slot(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 }
163
Alex Chiangfe997402008-06-10 15:27:37 -0600164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
167static void remove_slot(struct dummy_slot *dslot)
168{
169 int retval;
170
Alex Chiang43caae82008-10-20 17:41:28 -0600171 dbg("removing slot %s\n", hotplug_slot_name(dslot->slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 retval = pci_hp_deregister(dslot->slot);
173 if (retval)
Alex Chiang43caae82008-10-20 17:41:28 -0600174 err("Problem unregistering a slot %s\n",
175 hotplug_slot_name(dslot->slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Ian Abbott5c796ae2008-01-25 16:23:56 +0000178/* called from the single-threaded workqueue handler to remove a slot */
179static void remove_slot_worker(struct work_struct *work)
180{
181 struct dummy_slot *dslot =
182 container_of(work, struct dummy_slot, remove_work);
183 remove_slot(dslot);
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800187 * pci_rescan_slot - Rescan slot
188 * @temp: Device template. Should be set: bus and devfn.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800190 * Tries hard not to re-enable already existing devices;
191 * also handles scanning of subfunctions.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 */
193static void pci_rescan_slot(struct pci_dev *temp)
194{
195 struct pci_bus *bus = temp->bus;
196 struct pci_dev *dev;
197 int func;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700198 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 u8 hdr_type;
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700200
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (!pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) {
202 temp->hdr_type = hdr_type & 0x7f;
Alan Cox094ed762006-09-29 18:36:15 +0100203 if ((dev = pci_get_slot(bus, temp->devfn)) != NULL)
204 pci_dev_put(dev);
205 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 dev = pci_scan_single_device(bus, temp->devfn);
207 if (dev) {
208 dbg("New device on %s function %x:%x\n",
209 bus->name, temp->devfn >> 3,
210 temp->devfn & 7);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700211 retval = pci_bus_add_device(dev);
212 if (retval)
213 dev_err(&dev->dev, "error adding "
214 "device, continuing.\n");
215 else
216 add_slot(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218 }
219 /* multifunction device? */
220 if (!(hdr_type & 0x80))
221 return;
222
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))
226 continue;
227 temp->hdr_type = hdr_type & 0x7f;
228
Alan Cox094ed762006-09-29 18:36:15 +0100229 if ((dev = pci_get_slot(bus, temp->devfn)) != NULL)
230 pci_dev_put(dev);
231 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 dev = pci_scan_single_device(bus, temp->devfn);
233 if (dev) {
234 dbg("New device on %s function %x:%x\n",
235 bus->name, temp->devfn >> 3,
236 temp->devfn & 7);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700237 retval = pci_bus_add_device(dev);
238 if (retval)
239 dev_err(&dev->dev, "error adding "
240 "device, continuing.\n");
241 else
242 add_slot(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244 }
245 }
246 }
247}
248
249
250/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800251 * pci_rescan_bus - Rescan PCI bus
252 * @bus: the PCI bus to rescan
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800254 * Call pci_rescan_slot for each possible function of the bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 */
256static void pci_rescan_bus(const struct pci_bus *bus)
257{
258 unsigned int devfn;
259 struct pci_dev *dev;
Michael Ellermanbab41e92007-04-05 17:19:09 +1000260 dev = alloc_pci_dev();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (!dev)
262 return;
263
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 dev->bus = (struct pci_bus*)bus;
265 dev->sysdata = bus->sysdata;
266 for (devfn = 0; devfn < 0x100; devfn += 8) {
267 dev->devfn = devfn;
268 pci_rescan_slot(dev);
269 }
270 kfree(dev);
271}
272
273/* recursively scan all buses */
274static void pci_rescan_buses(const struct list_head *list)
275{
276 const struct list_head *l;
277 list_for_each(l,list) {
278 const struct pci_bus *b = pci_bus_b(l);
279 pci_rescan_bus(b);
280 pci_rescan_buses(&b->children);
281 }
282}
283
284/* initiate rescan of all pci buses */
285static inline void pci_rescan(void) {
286 pci_rescan_buses(&pci_root_buses);
287}
288
Ian Abbott5c796ae2008-01-25 16:23:56 +0000289/* called from the single-threaded workqueue handler to rescan all pci buses */
290static void pci_rescan_worker(struct work_struct *work)
291{
292 pci_rescan();
293}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295static int enable_slot(struct hotplug_slot *hotplug_slot)
296{
297 /* mis-use enable_slot for rescanning of the pci bus */
Ian Abbott5c796ae2008-01-25 16:23:56 +0000298 cancel_work_sync(&pci_rescan_work);
299 queue_work(dummyphp_wq, &pci_rescan_work);
Trent Piephoca99eb82008-04-07 16:04:16 -0700300 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static int disable_slot(struct hotplug_slot *slot)
304{
305 struct dummy_slot *dslot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct pci_dev *dev;
307 int func;
308
309 if (!slot)
310 return -ENODEV;
311 dslot = slot->private;
312
Alex Chiang43caae82008-10-20 17:41:28 -0600313 dbg("%s - physical_slot = %s\n", __func__, hotplug_slot_name(slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Alex Chiangfe997402008-06-10 15:27:37 -0600315 for (func = 7; func >= 0; func--) {
316 dev = pci_get_slot(dslot->dev->bus, dslot->dev->devfn + func);
317 if (!dev)
318 continue;
319
320 if (test_and_set_bit(0, &dslot->removed)) {
321 dbg("Slot already scheduled for removal\n");
322 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 }
Alex Chiangfe997402008-06-10 15:27:37 -0600324
Alex Chiang48902022008-09-05 14:05:03 -0700325 /* remove the device from the pci core */
326 pci_remove_bus_device(dev);
327
Alex Chiangfe997402008-06-10 15:27:37 -0600328 /* queue work item to blow away this sysfs entry and other
329 * parts.
330 */
331 INIT_WORK(&dslot->remove_work, remove_slot_worker);
332 queue_work(dummyphp_wq, &dslot->remove_work);
333
Alex Chiangfe997402008-06-10 15:27:37 -0600334 pci_dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return 0;
337}
338
339static void cleanup_slots (void)
340{
341 struct list_head *tmp;
342 struct list_head *next;
343 struct dummy_slot *dslot;
344
Ian Abbott5c796ae2008-01-25 16:23:56 +0000345 destroy_workqueue(dummyphp_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 list_for_each_safe (tmp, next, &slot_list) {
347 dslot = list_entry (tmp, struct dummy_slot, node);
348 remove_slot(dslot);
349 }
350
351}
352
353static int __init dummyphp_init(void)
354{
355 info(DRIVER_DESC "\n");
356
Ian Abbott5c796ae2008-01-25 16:23:56 +0000357 dummyphp_wq = create_singlethread_workqueue(MY_NAME);
358 if (!dummyphp_wq)
359 return -ENOMEM;
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 return pci_scan_buses();
362}
363
364
365static void __exit dummyphp_exit(void)
366{
367 cleanup_slots();
368}
369
370module_init(dummyphp_init);
371module_exit(dummyphp_exit);
372
373MODULE_AUTHOR(DRIVER_AUTHOR);
374MODULE_DESCRIPTION(DRIVER_DESC);
375MODULE_LICENSE("GPL");
376module_param(debug, bool, S_IRUGO | S_IWUSR);
377MODULE_PARM_DESC(debug, "Debugging mode enabled or not");
378