fakephp.c 9.2 KB

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