pciehp_core.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * PCI Express Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  6. * Copyright (C) 2001 IBM Corp.
  7. * Copyright (C) 2003-2004 Intel Corporation
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  19. * NON INFRINGEMENT. See the GNU General Public License for more
  20. * details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
  27. *
  28. */
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/kernel.h>
  32. #include <linux/slab.h>
  33. #include <linux/types.h>
  34. #include <linux/pci.h>
  35. #include "pciehp.h"
  36. #include <linux/interrupt.h>
  37. #include <linux/time.h>
  38. /* Global variables */
  39. bool pciehp_debug;
  40. bool pciehp_poll_mode;
  41. int pciehp_poll_time;
  42. static bool pciehp_force;
  43. #define DRIVER_VERSION "0.4"
  44. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  45. #define DRIVER_DESC "PCI Express Hot Plug Controller Driver"
  46. MODULE_AUTHOR(DRIVER_AUTHOR);
  47. MODULE_DESCRIPTION(DRIVER_DESC);
  48. MODULE_LICENSE("GPL");
  49. module_param(pciehp_debug, bool, 0644);
  50. module_param(pciehp_poll_mode, bool, 0644);
  51. module_param(pciehp_poll_time, int, 0644);
  52. module_param(pciehp_force, bool, 0644);
  53. MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
  54. MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
  55. MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
  56. MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if OSHP is missing");
  57. #define PCIE_MODULE_NAME "pciehp"
  58. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  59. static int enable_slot (struct hotplug_slot *slot);
  60. static int disable_slot (struct hotplug_slot *slot);
  61. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  62. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  63. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  64. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  65. static int reset_slot (struct hotplug_slot *slot, int probe);
  66. /**
  67. * release_slot - free up the memory used by a slot
  68. * @hotplug_slot: slot to free
  69. */
  70. static void release_slot(struct hotplug_slot *hotplug_slot)
  71. {
  72. struct slot *slot = hotplug_slot->private;
  73. /* queued work needs hotplug_slot name */
  74. cancel_delayed_work(&slot->work);
  75. drain_workqueue(slot->wq);
  76. kfree(hotplug_slot->ops);
  77. kfree(hotplug_slot->info);
  78. kfree(hotplug_slot);
  79. }
  80. static int init_slot(struct controller *ctrl)
  81. {
  82. struct slot *slot = ctrl->slot;
  83. struct hotplug_slot *hotplug = NULL;
  84. struct hotplug_slot_info *info = NULL;
  85. struct hotplug_slot_ops *ops = NULL;
  86. char name[SLOT_NAME_SIZE];
  87. int retval = -ENOMEM;
  88. hotplug = kzalloc(sizeof(*hotplug), GFP_KERNEL);
  89. if (!hotplug)
  90. goto out;
  91. info = kzalloc(sizeof(*info), GFP_KERNEL);
  92. if (!info)
  93. goto out;
  94. /* Setup hotplug slot ops */
  95. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  96. if (!ops)
  97. goto out;
  98. ops->enable_slot = enable_slot;
  99. ops->disable_slot = disable_slot;
  100. ops->get_power_status = get_power_status;
  101. ops->get_adapter_status = get_adapter_status;
  102. ops->reset_slot = reset_slot;
  103. if (MRL_SENS(ctrl))
  104. ops->get_latch_status = get_latch_status;
  105. if (ATTN_LED(ctrl)) {
  106. ops->get_attention_status = get_attention_status;
  107. ops->set_attention_status = set_attention_status;
  108. }
  109. /* register this slot with the hotplug pci core */
  110. hotplug->info = info;
  111. hotplug->private = slot;
  112. hotplug->release = &release_slot;
  113. hotplug->ops = ops;
  114. slot->hotplug_slot = hotplug;
  115. snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
  116. retval = pci_hp_register(hotplug,
  117. ctrl->pcie->port->subordinate, 0, name);
  118. if (retval)
  119. ctrl_err(ctrl, "pci_hp_register failed: error %d\n", retval);
  120. out:
  121. if (retval) {
  122. kfree(ops);
  123. kfree(info);
  124. kfree(hotplug);
  125. }
  126. return retval;
  127. }
  128. static void cleanup_slot(struct controller *ctrl)
  129. {
  130. pci_hp_deregister(ctrl->slot->hotplug_slot);
  131. }
  132. /*
  133. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  134. */
  135. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  136. {
  137. struct slot *slot = hotplug_slot->private;
  138. pciehp_set_attention_status(slot, status);
  139. return 0;
  140. }
  141. static int enable_slot(struct hotplug_slot *hotplug_slot)
  142. {
  143. struct slot *slot = hotplug_slot->private;
  144. return pciehp_sysfs_enable_slot(slot);
  145. }
  146. static int disable_slot(struct hotplug_slot *hotplug_slot)
  147. {
  148. struct slot *slot = hotplug_slot->private;
  149. return pciehp_sysfs_disable_slot(slot);
  150. }
  151. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  152. {
  153. struct slot *slot = hotplug_slot->private;
  154. pciehp_get_power_status(slot, value);
  155. return 0;
  156. }
  157. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  158. {
  159. struct slot *slot = hotplug_slot->private;
  160. pciehp_get_attention_status(slot, value);
  161. return 0;
  162. }
  163. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  164. {
  165. struct slot *slot = hotplug_slot->private;
  166. pciehp_get_latch_status(slot, value);
  167. return 0;
  168. }
  169. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  170. {
  171. struct slot *slot = hotplug_slot->private;
  172. pciehp_get_adapter_status(slot, value);
  173. return 0;
  174. }
  175. static int reset_slot(struct hotplug_slot *hotplug_slot, int probe)
  176. {
  177. struct slot *slot = hotplug_slot->private;
  178. return pciehp_reset_slot(slot, probe);
  179. }
  180. static int pciehp_probe(struct pcie_device *dev)
  181. {
  182. int rc;
  183. struct controller *ctrl;
  184. struct slot *slot;
  185. u8 occupied, poweron;
  186. /* If this is not a "hotplug" service, we have no business here. */
  187. if (dev->service != PCIE_PORT_SERVICE_HP)
  188. return -ENODEV;
  189. if (!dev->port->subordinate) {
  190. /* Can happen if we run out of bus numbers during probe */
  191. dev_err(&dev->device,
  192. "Hotplug bridge without secondary bus, ignoring\n");
  193. return -ENODEV;
  194. }
  195. ctrl = pcie_init(dev);
  196. if (!ctrl) {
  197. dev_err(&dev->device, "Controller initialization failed\n");
  198. return -ENODEV;
  199. }
  200. set_service_data(dev, ctrl);
  201. /* Setup the slot information structures */
  202. rc = init_slot(ctrl);
  203. if (rc) {
  204. if (rc == -EBUSY)
  205. ctrl_warn(ctrl, "Slot already registered by another hotplug driver\n");
  206. else
  207. ctrl_err(ctrl, "Slot initialization failed (%d)\n", rc);
  208. goto err_out_release_ctlr;
  209. }
  210. /* Enable events after we have setup the data structures */
  211. rc = pcie_init_notification(ctrl);
  212. if (rc) {
  213. ctrl_err(ctrl, "Notification initialization failed (%d)\n", rc);
  214. goto err_out_free_ctrl_slot;
  215. }
  216. /* Check if slot is occupied */
  217. slot = ctrl->slot;
  218. pciehp_get_adapter_status(slot, &occupied);
  219. pciehp_get_power_status(slot, &poweron);
  220. if (occupied && pciehp_force) {
  221. mutex_lock(&slot->hotplug_lock);
  222. pciehp_enable_slot(slot);
  223. mutex_unlock(&slot->hotplug_lock);
  224. }
  225. /* If empty slot's power status is on, turn power off */
  226. if (!occupied && poweron && POWER_CTRL(ctrl))
  227. pciehp_power_off_slot(slot);
  228. return 0;
  229. err_out_free_ctrl_slot:
  230. cleanup_slot(ctrl);
  231. err_out_release_ctlr:
  232. pciehp_release_ctrl(ctrl);
  233. return -ENODEV;
  234. }
  235. static void pciehp_remove(struct pcie_device *dev)
  236. {
  237. struct controller *ctrl = get_service_data(dev);
  238. pcie_shutdown_notification(ctrl);
  239. cleanup_slot(ctrl);
  240. pciehp_release_ctrl(ctrl);
  241. }
  242. #ifdef CONFIG_PM
  243. static int pciehp_suspend(struct pcie_device *dev)
  244. {
  245. return 0;
  246. }
  247. static int pciehp_resume(struct pcie_device *dev)
  248. {
  249. struct controller *ctrl;
  250. struct slot *slot;
  251. u8 status;
  252. ctrl = get_service_data(dev);
  253. /* reinitialize the chipset's event detection logic */
  254. pcie_reenable_notification(ctrl);
  255. slot = ctrl->slot;
  256. /* Check if slot is occupied */
  257. pciehp_get_adapter_status(slot, &status);
  258. mutex_lock(&slot->hotplug_lock);
  259. if (status)
  260. pciehp_enable_slot(slot);
  261. else
  262. pciehp_disable_slot(slot);
  263. mutex_unlock(&slot->hotplug_lock);
  264. return 0;
  265. }
  266. #endif /* PM */
  267. static struct pcie_port_service_driver hpdriver_portdrv = {
  268. .name = PCIE_MODULE_NAME,
  269. .port_type = PCIE_ANY_PORT,
  270. .service = PCIE_PORT_SERVICE_HP,
  271. .probe = pciehp_probe,
  272. .remove = pciehp_remove,
  273. #ifdef CONFIG_PM
  274. .suspend = pciehp_suspend,
  275. .resume = pciehp_resume,
  276. #endif /* PM */
  277. };
  278. static int __init pcied_init(void)
  279. {
  280. int retval = 0;
  281. retval = pcie_port_service_register(&hpdriver_portdrv);
  282. dbg("pcie_port_service_register = %d\n", retval);
  283. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  284. if (retval)
  285. dbg("Failure to register service\n");
  286. return retval;
  287. }
  288. static void __exit pcied_cleanup(void)
  289. {
  290. dbg("unload_pciehpd()\n");
  291. pcie_port_service_unregister(&hpdriver_portdrv);
  292. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  293. }
  294. module_init(pcied_init);
  295. module_exit(pcied_cleanup);