fakephp.c 8.1 KB

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