fakephp.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. * This driver will "emulate" removing PCI devices from the system. If
  23. * the "power" file is written to with "0" then the specified PCI device
  24. * will be completely removed from the kernel.
  25. *
  26. * WARNING, this does NOT turn off the power to the PCI device. This is
  27. * a "logical" removal, not a physical or electrical removal.
  28. *
  29. * Use this module at your own risk, you have been warned!
  30. *
  31. * Enabling PCI devices is left as an exercise for the reader...
  32. *
  33. */
  34. #include <linux/kernel.h>
  35. #include <linux/module.h>
  36. #include <linux/pci.h>
  37. #include <linux/init.h>
  38. #include <linux/string.h>
  39. #include <linux/slab.h>
  40. #include "pci_hotplug.h"
  41. #include "../pci.h"
  42. #if !defined(MODULE)
  43. #define MY_NAME "fakephp"
  44. #else
  45. #define MY_NAME THIS_MODULE->name
  46. #endif
  47. #define dbg(format, arg...) \
  48. do { \
  49. if (debug) \
  50. printk(KERN_DEBUG "%s: " format, \
  51. MY_NAME , ## arg); \
  52. } while (0)
  53. #define err(format, arg...) printk(KERN_ERR "%s: " format, MY_NAME , ## arg)
  54. #define info(format, arg...) printk(KERN_INFO "%s: " format, MY_NAME , ## arg)
  55. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  56. #define DRIVER_DESC "Fake PCI Hot Plug Controller Driver"
  57. struct dummy_slot {
  58. struct list_head node;
  59. struct hotplug_slot *slot;
  60. struct pci_dev *dev;
  61. };
  62. static int debug;
  63. static LIST_HEAD(slot_list);
  64. static int enable_slot (struct hotplug_slot *slot);
  65. static int disable_slot (struct hotplug_slot *slot);
  66. static struct hotplug_slot_ops dummy_hotplug_slot_ops = {
  67. .owner = THIS_MODULE,
  68. .enable_slot = enable_slot,
  69. .disable_slot = disable_slot,
  70. };
  71. static void dummy_release(struct hotplug_slot *slot)
  72. {
  73. struct dummy_slot *dslot = slot->private;
  74. list_del(&dslot->node);
  75. kfree(dslot->slot->info);
  76. kfree(dslot->slot);
  77. pci_dev_put(dslot->dev);
  78. kfree(dslot);
  79. }
  80. static int add_slot(struct pci_dev *dev)
  81. {
  82. struct dummy_slot *dslot;
  83. struct hotplug_slot *slot;
  84. int retval = -ENOMEM;
  85. slot = kzalloc(sizeof(struct hotplug_slot), GFP_KERNEL);
  86. if (!slot)
  87. goto error;
  88. slot->info = kzalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
  89. if (!slot->info)
  90. goto error_slot;
  91. slot->info->power_status = 1;
  92. slot->info->max_bus_speed = PCI_SPEED_UNKNOWN;
  93. slot->info->cur_bus_speed = PCI_SPEED_UNKNOWN;
  94. slot->name = &dev->dev.bus_id[0];
  95. dbg("slot->name = %s\n", slot->name);
  96. dslot = kmalloc(sizeof(struct dummy_slot), GFP_KERNEL);
  97. if (!dslot)
  98. goto error_info;
  99. slot->ops = &dummy_hotplug_slot_ops;
  100. slot->release = &dummy_release;
  101. slot->private = dslot;
  102. retval = pci_hp_register(slot);
  103. if (retval) {
  104. err("pci_hp_register failed with error %d\n", retval);
  105. goto error_dslot;
  106. }
  107. dslot->slot = slot;
  108. dslot->dev = pci_dev_get(dev);
  109. list_add (&dslot->node, &slot_list);
  110. return retval;
  111. error_dslot:
  112. kfree(dslot);
  113. error_info:
  114. kfree(slot->info);
  115. error_slot:
  116. kfree(slot);
  117. error:
  118. return retval;
  119. }
  120. static int __init pci_scan_buses(void)
  121. {
  122. struct pci_dev *dev = NULL;
  123. int retval = 0;
  124. while ((dev = pci_get_device(PCI_ANY_ID, PCI_ANY_ID, dev)) != NULL) {
  125. retval = add_slot(dev);
  126. if (retval) {
  127. pci_dev_put(dev);
  128. break;
  129. }
  130. }
  131. return retval;
  132. }
  133. static void remove_slot(struct dummy_slot *dslot)
  134. {
  135. int retval;
  136. dbg("removing slot %s\n", dslot->slot->name);
  137. retval = pci_hp_deregister(dslot->slot);
  138. if (retval)
  139. err("Problem unregistering a slot %s\n", dslot->slot->name);
  140. }
  141. /**
  142. * Rescan slot.
  143. * Tries hard not to re-enable already existing devices
  144. * also handles scanning of subfunctions
  145. *
  146. * @param temp Device template. Should be set: bus and devfn.
  147. */
  148. static void pci_rescan_slot(struct pci_dev *temp)
  149. {
  150. struct pci_bus *bus = temp->bus;
  151. struct pci_dev *dev;
  152. int func;
  153. int retval;
  154. u8 hdr_type;
  155. if (!pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type)) {
  156. temp->hdr_type = hdr_type & 0x7f;
  157. if (!pci_find_slot(bus->number, temp->devfn)) {
  158. dev = pci_scan_single_device(bus, temp->devfn);
  159. if (dev) {
  160. dbg("New device on %s function %x:%x\n",
  161. bus->name, temp->devfn >> 3,
  162. temp->devfn & 7);
  163. retval = pci_bus_add_device(dev);
  164. if (retval)
  165. dev_err(&dev->dev, "error adding "
  166. "device, continuing.\n");
  167. else
  168. add_slot(dev);
  169. }
  170. }
  171. /* multifunction device? */
  172. if (!(hdr_type & 0x80))
  173. return;
  174. /* continue scanning for other functions */
  175. for (func = 1, temp->devfn++; func < 8; func++, temp->devfn++) {
  176. if (pci_read_config_byte(temp, PCI_HEADER_TYPE, &hdr_type))
  177. continue;
  178. temp->hdr_type = hdr_type & 0x7f;
  179. if (!pci_find_slot(bus->number, temp->devfn)) {
  180. dev = pci_scan_single_device(bus, temp->devfn);
  181. if (dev) {
  182. dbg("New device on %s function %x:%x\n",
  183. bus->name, temp->devfn >> 3,
  184. temp->devfn & 7);
  185. retval = pci_bus_add_device(dev);
  186. if (retval)
  187. dev_err(&dev->dev, "error adding "
  188. "device, continuing.\n");
  189. else
  190. add_slot(dev);
  191. }
  192. }
  193. }
  194. }
  195. }
  196. /**
  197. * Rescan PCI bus.
  198. * call pci_rescan_slot for each possible function of the bus
  199. *
  200. * @param bus
  201. */
  202. static void pci_rescan_bus(const struct pci_bus *bus)
  203. {
  204. unsigned int devfn;
  205. struct pci_dev *dev;
  206. dev = kzalloc(sizeof(struct pci_dev), GFP_KERNEL);
  207. if (!dev)
  208. return;
  209. dev->bus = (struct pci_bus*)bus;
  210. dev->sysdata = bus->sysdata;
  211. for (devfn = 0; devfn < 0x100; devfn += 8) {
  212. dev->devfn = devfn;
  213. pci_rescan_slot(dev);
  214. }
  215. kfree(dev);
  216. }
  217. /* recursively scan all buses */
  218. static void pci_rescan_buses(const struct list_head *list)
  219. {
  220. const struct list_head *l;
  221. list_for_each(l,list) {
  222. const struct pci_bus *b = pci_bus_b(l);
  223. pci_rescan_bus(b);
  224. pci_rescan_buses(&b->children);
  225. }
  226. }
  227. /* initiate rescan of all pci buses */
  228. static inline void pci_rescan(void) {
  229. pci_rescan_buses(&pci_root_buses);
  230. }
  231. static int enable_slot(struct hotplug_slot *hotplug_slot)
  232. {
  233. /* mis-use enable_slot for rescanning of the pci bus */
  234. pci_rescan();
  235. return -ENODEV;
  236. }
  237. /* find the hotplug_slot for the pci_dev */
  238. static struct hotplug_slot *get_slot_from_dev(struct pci_dev *dev)
  239. {
  240. struct dummy_slot *dslot;
  241. list_for_each_entry(dslot, &slot_list, node) {
  242. if (dslot->dev == dev)
  243. return dslot->slot;
  244. }
  245. return NULL;
  246. }
  247. static int disable_slot(struct hotplug_slot *slot)
  248. {
  249. struct dummy_slot *dslot;
  250. struct hotplug_slot *hslot;
  251. struct pci_dev *dev;
  252. int func;
  253. if (!slot)
  254. return -ENODEV;
  255. dslot = slot->private;
  256. dbg("%s - physical_slot = %s\n", __FUNCTION__, slot->name);
  257. /* don't disable bridged devices just yet, we can't handle them easily... */
  258. if (dslot->dev->subordinate) {
  259. err("Can't remove PCI devices with other PCI devices behind it yet.\n");
  260. return -ENODEV;
  261. }
  262. /* search for subfunctions and disable them first */
  263. if (!(dslot->dev->devfn & 7)) {
  264. for (func = 1; func < 8; func++) {
  265. dev = pci_find_slot(dslot->dev->bus->number,
  266. dslot->dev->devfn + func);
  267. if (dev) {
  268. hslot = get_slot_from_dev(dev);
  269. if (hslot)
  270. disable_slot(hslot);
  271. else {
  272. err("Hotplug slot not found for subfunction of PCI device\n");
  273. return -ENODEV;
  274. }
  275. } else
  276. dbg("No device in slot found\n");
  277. }
  278. }
  279. /* remove the device from the pci core */
  280. pci_remove_bus_device(dslot->dev);
  281. /* blow away this sysfs entry and other parts. */
  282. remove_slot(dslot);
  283. return 0;
  284. }
  285. static void cleanup_slots (void)
  286. {
  287. struct list_head *tmp;
  288. struct list_head *next;
  289. struct dummy_slot *dslot;
  290. list_for_each_safe (tmp, next, &slot_list) {
  291. dslot = list_entry (tmp, struct dummy_slot, node);
  292. remove_slot(dslot);
  293. }
  294. }
  295. static int __init dummyphp_init(void)
  296. {
  297. info(DRIVER_DESC "\n");
  298. return pci_scan_buses();
  299. }
  300. static void __exit dummyphp_exit(void)
  301. {
  302. cleanup_slots();
  303. }
  304. module_init(dummyphp_init);
  305. module_exit(dummyphp_exit);
  306. MODULE_AUTHOR(DRIVER_AUTHOR);
  307. MODULE_DESCRIPTION(DRIVER_DESC);
  308. MODULE_LICENSE("GPL");
  309. module_param(debug, bool, S_IRUGO | S_IWUSR);
  310. MODULE_PARM_DESC(debug, "Debugging mode enabled or not");