Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | */ | |
1da177e4 LT |
35 | #include <linux/kernel.h> |
36 | #include <linux/module.h> | |
37 | #include <linux/pci.h> | |
7a54f25c | 38 | #include <linux/pci_hotplug.h> |
1da177e4 | 39 | #include <linux/init.h> |
4e57b681 TS |
40 | #include <linux/string.h> |
41 | #include <linux/slab.h> | |
5c796ae7 | 42 | #include <linux/workqueue.h> |
1da177e4 LT |
43 | #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 | ||
63 | struct dummy_slot { | |
64 | struct list_head node; | |
65 | struct hotplug_slot *slot; | |
66 | struct pci_dev *dev; | |
5c796ae7 IA |
67 | struct work_struct remove_work; |
68 | unsigned long removed; | |
1da177e4 LT |
69 | }; |
70 | ||
71 | static int debug; | |
0b8b0dca | 72 | static int dup_slots; |
1da177e4 | 73 | static LIST_HEAD(slot_list); |
5c796ae7 IA |
74 | static struct workqueue_struct *dummyphp_wq; |
75 | ||
76 | static void pci_rescan_worker(struct work_struct *work); | |
77 | static DECLARE_WORK(pci_rescan_work, pci_rescan_worker); | |
1da177e4 LT |
78 | |
79 | static int enable_slot (struct hotplug_slot *slot); | |
80 | static int disable_slot (struct hotplug_slot *slot); | |
81 | ||
82 | static struct hotplug_slot_ops dummy_hotplug_slot_ops = { | |
83 | .owner = THIS_MODULE, | |
84 | .enable_slot = enable_slot, | |
85 | .disable_slot = disable_slot, | |
86 | }; | |
87 | ||
88 | static void dummy_release(struct hotplug_slot *slot) | |
89 | { | |
90 | struct dummy_slot *dslot = slot->private; | |
91 | ||
92 | list_del(&dslot->node); | |
93 | kfree(dslot->slot->info); | |
94 | kfree(dslot->slot); | |
95 | pci_dev_put(dslot->dev); | |
96 | kfree(dslot); | |
97 | } | |
98 | ||
43caae88 AC |
99 | #define SLOT_NAME_SIZE 8 |
100 | ||
1da177e4 LT |
101 | static int add_slot(struct pci_dev *dev) |
102 | { | |
103 | struct dummy_slot *dslot; | |
104 | struct hotplug_slot *slot; | |
43caae88 | 105 | char name[SLOT_NAME_SIZE]; |
1da177e4 | 106 | int retval = -ENOMEM; |
fe99740c | 107 | static int count = 1; |
1da177e4 | 108 | |
f5afe806 | 109 | slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL); |
1da177e4 LT |
110 | if (!slot) |
111 | goto error; | |
1da177e4 | 112 | |
f5afe806 | 113 | slot->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL); |
1da177e4 LT |
114 | if (!slot->info) |
115 | goto error_slot; | |
1da177e4 LT |
116 | |
117 | slot->info->power_status = 1; | |
118 | slot->info->max_bus_speed = PCI_SPEED_UNKNOWN; | |
119 | slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN; | |
120 | ||
5c796ae7 | 121 | dslot = kzalloc(sizeof(struct dummy_slot), GFP_KERNEL); |
1da177e4 LT |
122 | if (!dslot) |
123 | goto error_info; | |
124 | ||
0b8b0dca AC |
125 | if (dup_slots) |
126 | snprintf(name, SLOT_NAME_SIZE, "fake"); | |
127 | else | |
128 | snprintf(name, SLOT_NAME_SIZE, "fake%d", count++); | |
129 | dbg("slot->name = %s\n", name); | |
1da177e4 LT |
130 | slot->ops = &dummy_hotplug_slot_ops; |
131 | slot->release = &dummy_release; | |
132 | slot->private = dslot; | |
133 | ||
43caae88 | 134 | retval = pci_hp_register(slot, dev->bus, PCI_SLOT(dev->devfn), name); |
1da177e4 LT |
135 | if (retval) { |
136 | err("pci_hp_register failed with error %d\n", retval); | |
137 | goto error_dslot; | |
138 | } | |
139 | ||
43caae88 | 140 | dbg("slot->name = %s\n", hotplug_slot_name(slot)); |
1da177e4 LT |
141 | dslot->slot = slot; |
142 | dslot->dev = pci_dev_get(dev); | |
143 | list_add (&dslot->node, &slot_list); | |
144 | return retval; | |
145 | ||
146 | error_dslot: | |
147 | kfree(dslot); | |
148 | error_info: | |
149 | kfree(slot->info); | |
150 | error_slot: | |
151 | kfree(slot); | |
152 | error: | |
153 | return retval; | |
154 | } | |
155 | ||
156 | static int __init pci_scan_buses(void) | |
157 | { | |
158 | struct pci_dev *dev = NULL; | |
fe99740c | 159 | int lastslot = 0; |
1da177e4 LT |
160 | |
161 | while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { | |
fe99740c AC |
162 | if (PCI_FUNC(dev->devfn) > 0 && |
163 | lastslot == PCI_SLOT(dev->devfn)) | |
164 | continue; | |
165 | lastslot = PCI_SLOT(dev->devfn); | |
166 | add_slot(dev); | |
1da177e4 LT |
167 | } |
168 | ||
fe99740c | 169 | return 0; |
1da177e4 LT |
170 | } |
171 | ||
172 | static void remove_slot(struct dummy_slot *dslot) | |
173 | { | |
174 | int retval; | |
175 | ||
43caae88 | 176 | dbg("removing slot %s\n", hotplug_slot_name(dslot->slot)); |
1da177e4 LT |
177 | retval = pci_hp_deregister(dslot->slot); |
178 | if (retval) | |
43caae88 AC |
179 | err("Problem unregistering a slot %s\n", |
180 | hotplug_slot_name(dslot->slot)); | |
1da177e4 LT |
181 | } |
182 | ||
5c796ae7 IA |
183 | /* called from the single-threaded workqueue handler to remove a slot */ |
184 | static void remove_slot_worker(struct work_struct *work) | |
185 | { | |
186 | struct dummy_slot *dslot = | |
187 | container_of(work, struct dummy_slot, remove_work); | |
188 | remove_slot(dslot); | |
189 | } | |
190 | ||
1da177e4 | 191 | /** |
26e6c66e RD |
192 | * pci_rescan_slot - Rescan slot |
193 | * @temp: Device template. Should be set: bus and devfn. | |
1da177e4 | 194 | * |
26e6c66e RD |
195 | * Tries hard not to re-enable already existing devices; |
196 | * also handles scanning of subfunctions. | |
1da177e4 LT |
197 | */ |
198 | static void pci_rescan_slot(struct pci_dev *temp) | |
199 | { | |
200 | struct pci_bus *bus = temp->bus; | |
201 | struct pci_dev *dev; | |
202 | int func; | |
b19441af | 203 | int retval; |
1da177e4 | 204 | u8 hdr_type; |
b19441af | 205 | |
1da177e4 LT |
206 | if (!pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) { |
207 | temp->hdr_type = hdr_type & 0x7f; | |
094ed76e AC |
208 | if ((dev = pci_get_slot(bus, temp->devfn)) != NULL) |
209 | pci_dev_put(dev); | |
210 | else { | |
1da177e4 LT |
211 | dev = pci_scan_single_device(bus, temp->devfn); |
212 | if (dev) { | |
213 | dbg("New device on %s function %x:%x\n", | |
214 | bus->name, temp->devfn >> 3, | |
215 | temp->devfn & 7); | |
b19441af GKH |
216 | retval = pci_bus_add_device(dev); |
217 | if (retval) | |
218 | dev_err(&dev->dev, "error adding " | |
219 | "device, continuing.\n"); | |
220 | else | |
221 | add_slot(dev); | |
1da177e4 LT |
222 | } |
223 | } | |
224 | /* multifunction device? */ | |
225 | if (!(hdr_type & 0x80)) | |
226 | return; | |
227 | ||
228 | /* continue scanning for other functions */ | |
229 | for (func = 1, temp->devfn++; func < 8; func++, temp->devfn++) { | |
230 | if (pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) | |
231 | continue; | |
232 | temp->hdr_type = hdr_type & 0x7f; | |
233 | ||
094ed76e AC |
234 | if ((dev = pci_get_slot(bus, temp->devfn)) != NULL) |
235 | pci_dev_put(dev); | |
236 | else { | |
1da177e4 LT |
237 | dev = pci_scan_single_device(bus, temp->devfn); |
238 | if (dev) { | |
239 | dbg("New device on %s function %x:%x\n", | |
240 | bus->name, temp->devfn >> 3, | |
241 | temp->devfn & 7); | |
b19441af GKH |
242 | retval = pci_bus_add_device(dev); |
243 | if (retval) | |
244 | dev_err(&dev->dev, "error adding " | |
245 | "device, continuing.\n"); | |
246 | else | |
247 | add_slot(dev); | |
1da177e4 LT |
248 | } |
249 | } | |
250 | } | |
251 | } | |
252 | } | |
253 | ||
254 | ||
255 | /** | |
26e6c66e RD |
256 | * pci_rescan_bus - Rescan PCI bus |
257 | * @bus: the PCI bus to rescan | |
1da177e4 | 258 | * |
26e6c66e | 259 | * Call pci_rescan_slot for each possible function of the bus. |
1da177e4 LT |
260 | */ |
261 | static void pci_rescan_bus(const struct pci_bus *bus) | |
262 | { | |
263 | unsigned int devfn; | |
264 | struct pci_dev *dev; | |
bab41e9b | 265 | dev = alloc_pci_dev(); |
1da177e4 LT |
266 | if (!dev) |
267 | return; | |
268 | ||
1da177e4 LT |
269 | dev->bus = (struct pci_bus*)bus; |
270 | dev->sysdata = bus->sysdata; | |
271 | for (devfn = 0; devfn < 0x100; devfn += 8) { | |
272 | dev->devfn = devfn; | |
273 | pci_rescan_slot(dev); | |
274 | } | |
275 | kfree(dev); | |
276 | } | |
277 | ||
278 | /* recursively scan all buses */ | |
279 | static void pci_rescan_buses(const struct list_head *list) | |
280 | { | |
281 | const struct list_head *l; | |
282 | list_for_each(l,list) { | |
283 | const struct pci_bus *b = pci_bus_b(l); | |
284 | pci_rescan_bus(b); | |
285 | pci_rescan_buses(&b->children); | |
286 | } | |
287 | } | |
288 | ||
289 | /* initiate rescan of all pci buses */ | |
290 | static inline void pci_rescan(void) { | |
291 | pci_rescan_buses(&pci_root_buses); | |
292 | } | |
293 | ||
5c796ae7 IA |
294 | /* called from the single-threaded workqueue handler to rescan all pci buses */ |
295 | static void pci_rescan_worker(struct work_struct *work) | |
296 | { | |
297 | pci_rescan(); | |
298 | } | |
1da177e4 LT |
299 | |
300 | static int enable_slot(struct hotplug_slot *hotplug_slot) | |
301 | { | |
302 | /* mis-use enable_slot for rescanning of the pci bus */ | |
5c796ae7 IA |
303 | cancel_work_sync(&pci_rescan_work); |
304 | queue_work(dummyphp_wq, &pci_rescan_work); | |
ca99eb8c | 305 | return 0; |
1da177e4 LT |
306 | } |
307 | ||
1da177e4 LT |
308 | static int disable_slot(struct hotplug_slot *slot) |
309 | { | |
310 | struct dummy_slot *dslot; | |
1da177e4 LT |
311 | struct pci_dev *dev; |
312 | int func; | |
313 | ||
314 | if (!slot) | |
315 | return -ENODEV; | |
316 | dslot = slot->private; | |
317 | ||
43caae88 | 318 | dbg("%s - physical_slot = %s\n", __func__, hotplug_slot_name(slot)); |
1da177e4 | 319 | |
fe99740c AC |
320 | for (func = 7; func >= 0; func--) { |
321 | dev = pci_get_slot(dslot->dev->bus, dslot->dev->devfn + func); | |
322 | if (!dev) | |
323 | continue; | |
324 | ||
325 | if (test_and_set_bit(0, &dslot->removed)) { | |
326 | dbg("Slot already scheduled for removal\n"); | |
327 | return -ENODEV; | |
1da177e4 | 328 | } |
1da177e4 | 329 | |
48902025 AC |
330 | /* remove the device from the pci core */ |
331 | pci_remove_bus_device(dev); | |
332 | ||
fe99740c AC |
333 | /* queue work item to blow away this sysfs entry and other |
334 | * parts. | |
335 | */ | |
336 | INIT_WORK(&dslot->remove_work, remove_slot_worker); | |
337 | queue_work(dummyphp_wq, &dslot->remove_work); | |
1da177e4 | 338 | |
fe99740c AC |
339 | pci_dev_put(dev); |
340 | } | |
1da177e4 LT |
341 | return 0; |
342 | } | |
343 | ||
344 | static void cleanup_slots (void) | |
345 | { | |
346 | struct list_head *tmp; | |
347 | struct list_head *next; | |
348 | struct dummy_slot *dslot; | |
349 | ||
5c796ae7 | 350 | destroy_workqueue(dummyphp_wq); |
1da177e4 LT |
351 | list_for_each_safe (tmp, next, &slot_list) { |
352 | dslot = list_entry (tmp, struct dummy_slot, node); | |
353 | remove_slot(dslot); | |
354 | } | |
355 | ||
356 | } | |
357 | ||
358 | static int __init dummyphp_init(void) | |
359 | { | |
360 | info(DRIVER_DESC "\n"); | |
361 | ||
5c796ae7 IA |
362 | dummyphp_wq = create_singlethread_workqueue(MY_NAME); |
363 | if (!dummyphp_wq) | |
364 | return -ENOMEM; | |
365 | ||
1da177e4 LT |
366 | return pci_scan_buses(); |
367 | } | |
368 | ||
369 | ||
370 | static void __exit dummyphp_exit(void) | |
371 | { | |
372 | cleanup_slots(); | |
373 | } | |
374 | ||
375 | module_init(dummyphp_init); | |
376 | module_exit(dummyphp_exit); | |
377 | ||
378 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
379 | MODULE_DESCRIPTION(DRIVER_DESC); | |
380 | MODULE_LICENSE("GPL"); | |
381 | module_param(debug, bool, S_IRUGO | S_IWUSR); | |
382 | MODULE_PARM_DESC(debug, "Debugging mode enabled or not"); | |
0b8b0dca AC |
383 | module_param(dup_slots, bool, S_IRUGO | S_IWUSR); |
384 | MODULE_PARM_DESC(dup_slots, "Force duplicate slot names for debugging"); |