Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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/config.h> |
| 36 | #include <linux/kernel.h> |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/pci.h> |
| 39 | #include <linux/init.h> |
Tim Schmielau | 4e57b68 | 2005-10-30 15:03:48 -0800 | [diff] [blame^] | 40 | #include <linux/string.h> |
| 41 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "pci_hotplug.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 | }; |
| 68 | |
| 69 | static int debug; |
| 70 | static LIST_HEAD(slot_list); |
| 71 | |
| 72 | static int enable_slot (struct hotplug_slot *slot); |
| 73 | static int disable_slot (struct hotplug_slot *slot); |
| 74 | |
| 75 | static struct hotplug_slot_ops dummy_hotplug_slot_ops = { |
| 76 | .owner = THIS_MODULE, |
| 77 | .enable_slot = enable_slot, |
| 78 | .disable_slot = disable_slot, |
| 79 | }; |
| 80 | |
| 81 | static void dummy_release(struct hotplug_slot *slot) |
| 82 | { |
| 83 | struct dummy_slot *dslot = slot->private; |
| 84 | |
| 85 | list_del(&dslot->node); |
| 86 | kfree(dslot->slot->info); |
| 87 | kfree(dslot->slot); |
| 88 | pci_dev_put(dslot->dev); |
| 89 | kfree(dslot); |
| 90 | } |
| 91 | |
| 92 | static int add_slot(struct pci_dev *dev) |
| 93 | { |
| 94 | struct dummy_slot *dslot; |
| 95 | struct hotplug_slot *slot; |
| 96 | int retval = -ENOMEM; |
| 97 | |
| 98 | slot = kmalloc(sizeof(struct hotplug_slot), GFP_KERNEL); |
| 99 | if (!slot) |
| 100 | goto error; |
| 101 | memset(slot, 0, sizeof(*slot)); |
| 102 | |
| 103 | slot->info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL); |
| 104 | if (!slot->info) |
| 105 | goto error_slot; |
| 106 | memset(slot->info, 0, sizeof(struct hotplug_slot_info)); |
| 107 | |
| 108 | slot->info->power_status = 1; |
| 109 | slot->info->max_bus_speed = PCI_SPEED_UNKNOWN; |
| 110 | slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN; |
| 111 | |
| 112 | slot->name = &dev->dev.bus_id[0]; |
| 113 | dbg("slot->name = %s\n", slot->name); |
| 114 | |
| 115 | dslot = kmalloc(sizeof(struct dummy_slot), GFP_KERNEL); |
| 116 | if (!dslot) |
| 117 | goto error_info; |
| 118 | |
| 119 | slot->ops = &dummy_hotplug_slot_ops; |
| 120 | slot->release = &dummy_release; |
| 121 | slot->private = dslot; |
| 122 | |
| 123 | retval = pci_hp_register(slot); |
| 124 | if (retval) { |
| 125 | err("pci_hp_register failed with error %d\n", retval); |
| 126 | goto error_dslot; |
| 127 | } |
| 128 | |
| 129 | dslot->slot = slot; |
| 130 | dslot->dev = pci_dev_get(dev); |
| 131 | list_add (&dslot->node, &slot_list); |
| 132 | return retval; |
| 133 | |
| 134 | error_dslot: |
| 135 | kfree(dslot); |
| 136 | error_info: |
| 137 | kfree(slot->info); |
| 138 | error_slot: |
| 139 | kfree(slot); |
| 140 | error: |
| 141 | return retval; |
| 142 | } |
| 143 | |
| 144 | static int __init pci_scan_buses(void) |
| 145 | { |
| 146 | struct pci_dev *dev = NULL; |
| 147 | int retval = 0; |
| 148 | |
| 149 | while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) { |
| 150 | retval = add_slot(dev); |
| 151 | if (retval) { |
| 152 | pci_dev_put(dev); |
| 153 | break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | return retval; |
| 158 | } |
| 159 | |
| 160 | static void remove_slot(struct dummy_slot *dslot) |
| 161 | { |
| 162 | int retval; |
| 163 | |
| 164 | dbg("removing slot %s\n", dslot->slot->name); |
| 165 | retval = pci_hp_deregister(dslot->slot); |
| 166 | if (retval) |
| 167 | err("Problem unregistering a slot %s\n", dslot->slot->name); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Rescan slot. |
| 172 | * Tries hard not to re-enable already existing devices |
| 173 | * also handles scanning of subfunctions |
| 174 | * |
| 175 | * @param temp Device template. Should be set: bus and devfn. |
| 176 | */ |
| 177 | static void pci_rescan_slot(struct pci_dev *temp) |
| 178 | { |
| 179 | struct pci_bus *bus = temp->bus; |
| 180 | struct pci_dev *dev; |
| 181 | int func; |
| 182 | u8 hdr_type; |
| 183 | if (!pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) { |
| 184 | temp->hdr_type = hdr_type & 0x7f; |
| 185 | if (!pci_find_slot(bus->number, temp->devfn)) { |
| 186 | dev = pci_scan_single_device(bus, temp->devfn); |
| 187 | if (dev) { |
| 188 | dbg("New device on %s function %x:%x\n", |
| 189 | bus->name, temp->devfn >> 3, |
| 190 | temp->devfn & 7); |
| 191 | pci_bus_add_device(dev); |
| 192 | add_slot(dev); |
| 193 | } |
| 194 | } |
| 195 | /* multifunction device? */ |
| 196 | if (!(hdr_type & 0x80)) |
| 197 | return; |
| 198 | |
| 199 | /* continue scanning for other functions */ |
| 200 | for (func = 1, temp->devfn++; func < 8; func++, temp->devfn++) { |
| 201 | if (pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) |
| 202 | continue; |
| 203 | temp->hdr_type = hdr_type & 0x7f; |
| 204 | |
| 205 | if (!pci_find_slot(bus->number, temp->devfn)) { |
| 206 | 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); |
| 211 | pci_bus_add_device(dev); |
| 212 | add_slot(dev); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | |
| 220 | /** |
| 221 | * Rescan PCI bus. |
| 222 | * call pci_rescan_slot for each possible function of the bus |
| 223 | * |
| 224 | * @param bus |
| 225 | */ |
| 226 | static void pci_rescan_bus(const struct pci_bus *bus) |
| 227 | { |
| 228 | unsigned int devfn; |
| 229 | struct pci_dev *dev; |
| 230 | dev = kmalloc(sizeof(struct pci_dev), GFP_KERNEL); |
| 231 | if (!dev) |
| 232 | return; |
| 233 | |
| 234 | memset(dev, 0, sizeof(dev)); |
| 235 | dev->bus = (struct pci_bus*)bus; |
| 236 | dev->sysdata = bus->sysdata; |
| 237 | for (devfn = 0; devfn < 0x100; devfn += 8) { |
| 238 | dev->devfn = devfn; |
| 239 | pci_rescan_slot(dev); |
| 240 | } |
| 241 | kfree(dev); |
| 242 | } |
| 243 | |
| 244 | /* recursively scan all buses */ |
| 245 | static void pci_rescan_buses(const struct list_head *list) |
| 246 | { |
| 247 | const struct list_head *l; |
| 248 | list_for_each(l,list) { |
| 249 | const struct pci_bus *b = pci_bus_b(l); |
| 250 | pci_rescan_bus(b); |
| 251 | pci_rescan_buses(&b->children); |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /* initiate rescan of all pci buses */ |
| 256 | static inline void pci_rescan(void) { |
| 257 | pci_rescan_buses(&pci_root_buses); |
| 258 | } |
| 259 | |
| 260 | |
| 261 | static int enable_slot(struct hotplug_slot *hotplug_slot) |
| 262 | { |
| 263 | /* mis-use enable_slot for rescanning of the pci bus */ |
| 264 | pci_rescan(); |
| 265 | return -ENODEV; |
| 266 | } |
| 267 | |
| 268 | /* find the hotplug_slot for the pci_dev */ |
| 269 | static struct hotplug_slot *get_slot_from_dev(struct pci_dev *dev) |
| 270 | { |
| 271 | struct dummy_slot *dslot; |
| 272 | |
| 273 | list_for_each_entry(dslot, &slot_list, node) { |
| 274 | if (dslot->dev == dev) |
| 275 | return dslot->slot; |
| 276 | } |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | |
| 281 | static int disable_slot(struct hotplug_slot *slot) |
| 282 | { |
| 283 | struct dummy_slot *dslot; |
| 284 | struct hotplug_slot *hslot; |
| 285 | struct pci_dev *dev; |
| 286 | int func; |
| 287 | |
| 288 | if (!slot) |
| 289 | return -ENODEV; |
| 290 | dslot = slot->private; |
| 291 | |
| 292 | dbg("%s - physical_slot = %s\n", __FUNCTION__, slot->name); |
| 293 | |
| 294 | /* don't disable bridged devices just yet, we can't handle them easily... */ |
| 295 | if (dslot->dev->subordinate) { |
| 296 | err("Can't remove PCI devices with other PCI devices behind it yet.\n"); |
| 297 | return -ENODEV; |
| 298 | } |
| 299 | /* search for subfunctions and disable them first */ |
| 300 | if (!(dslot->dev->devfn & 7)) { |
| 301 | for (func = 1; func < 8; func++) { |
| 302 | dev = pci_find_slot(dslot->dev->bus->number, |
| 303 | dslot->dev->devfn + func); |
| 304 | if (dev) { |
| 305 | hslot = get_slot_from_dev(dev); |
| 306 | if (hslot) |
| 307 | disable_slot(hslot); |
| 308 | else { |
| 309 | err("Hotplug slot not found for subfunction of PCI device\n"); |
| 310 | return -ENODEV; |
| 311 | } |
| 312 | } else |
| 313 | dbg("No device in slot found\n"); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /* remove the device from the pci core */ |
| 318 | pci_remove_bus_device(dslot->dev); |
| 319 | |
| 320 | /* blow away this sysfs entry and other parts. */ |
| 321 | remove_slot(dslot); |
| 322 | |
| 323 | return 0; |
| 324 | } |
| 325 | |
| 326 | static void cleanup_slots (void) |
| 327 | { |
| 328 | struct list_head *tmp; |
| 329 | struct list_head *next; |
| 330 | struct dummy_slot *dslot; |
| 331 | |
| 332 | list_for_each_safe (tmp, next, &slot_list) { |
| 333 | dslot = list_entry (tmp, struct dummy_slot, node); |
| 334 | remove_slot(dslot); |
| 335 | } |
| 336 | |
| 337 | } |
| 338 | |
| 339 | static int __init dummyphp_init(void) |
| 340 | { |
| 341 | info(DRIVER_DESC "\n"); |
| 342 | |
| 343 | return pci_scan_buses(); |
| 344 | } |
| 345 | |
| 346 | |
| 347 | static void __exit dummyphp_exit(void) |
| 348 | { |
| 349 | cleanup_slots(); |
| 350 | } |
| 351 | |
| 352 | module_init(dummyphp_init); |
| 353 | module_exit(dummyphp_exit); |
| 354 | |
| 355 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 356 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 357 | MODULE_LICENSE("GPL"); |
| 358 | module_param(debug, bool, S_IRUGO | S_IWUSR); |
| 359 | MODULE_PARM_DESC(debug, "Debugging mode enabled or not"); |
| 360 | |