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