pciehp_core.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * PCI Express Hot Plug Controller Driver
  3. *
  4. * Copyright (C) 1995,2001 Compaq Computer Corporation
  5. * Copyright (C) 2001 Greg Kroah-Hartman ([email protected])
  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 <[email protected]>, <[email protected]>
  27. *
  28. */
  29. #include <linux/moduleparam.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/types.h>
  33. #include <linux/pci.h>
  34. #include "pciehp.h"
  35. #include <linux/interrupt.h>
  36. #include <linux/time.h>
  37. /* Global variables */
  38. bool pciehp_debug;
  39. bool pciehp_poll_mode;
  40. int pciehp_poll_time;
  41. static bool pciehp_force;
  42. #define DRIVER_VERSION "0.4"
  43. #define DRIVER_AUTHOR "Dan Zink <[email protected]>, Greg Kroah-Hartman <[email protected]>, Dely Sy <[email protected]>"
  44. #define DRIVER_DESC "PCI Express Hot Plug Controller Driver"
  45. /*
  46. * not really modular, but the easiest way to keep compat with existing
  47. * bootargs behaviour is to continue using module_param here.
  48. */
  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. } else if (ctrl->pcie->port->hotplug_user_indicators) {
  109. ops->get_attention_status = pciehp_get_raw_indicator_status;
  110. ops->set_attention_status = pciehp_set_raw_indicator_status;
  111. }
  112. /* register this slot with the hotplug pci core */
  113. hotplug->info = info;
  114. hotplug->private = slot;
  115. hotplug->release = &release_slot;
  116. hotplug->ops = ops;
  117. slot->hotplug_slot = hotplug;
  118. snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
  119. retval = pci_hp_register(hotplug,
  120. ctrl->pcie->port->subordinate, 0, name);
  121. if (retval)
  122. ctrl_err(ctrl, "pci_hp_register failed: error %d\n", retval);
  123. out:
  124. if (retval) {
  125. kfree(ops);
  126. kfree(info);
  127. kfree(hotplug);
  128. }
  129. return retval;
  130. }
  131. static void cleanup_slot(struct controller *ctrl)
  132. {
  133. pci_hp_deregister(ctrl->slot->hotplug_slot);
  134. }
  135. /*
  136. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  137. */
  138. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  139. {
  140. struct slot *slot = hotplug_slot->private;
  141. pciehp_set_attention_status(slot, status);
  142. return 0;
  143. }
  144. static int enable_slot(struct hotplug_slot *hotplug_slot)
  145. {
  146. struct slot *slot = hotplug_slot->private;
  147. return pciehp_sysfs_enable_slot(slot);
  148. }
  149. static int disable_slot(struct hotplug_slot *hotplug_slot)
  150. {
  151. struct slot *slot = hotplug_slot->private;
  152. return pciehp_sysfs_disable_slot(slot);
  153. }
  154. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  155. {
  156. struct slot *slot = hotplug_slot->private;
  157. pciehp_get_power_status(slot, value);
  158. return 0;
  159. }
  160. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  161. {
  162. struct slot *slot = hotplug_slot->private;
  163. pciehp_get_attention_status(slot, value);
  164. return 0;
  165. }
  166. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  167. {
  168. struct slot *slot = hotplug_slot->private;
  169. pciehp_get_latch_status(slot, value);
  170. return 0;
  171. }
  172. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  173. {
  174. struct slot *slot = hotplug_slot->private;
  175. pciehp_get_adapter_status(slot, value);
  176. return 0;
  177. }
  178. static int reset_slot(struct hotplug_slot *hotplug_slot, int probe)
  179. {
  180. struct slot *slot = hotplug_slot->private;
  181. return pciehp_reset_slot(slot, probe);
  182. }
  183. static int pciehp_probe(struct pcie_device *dev)
  184. {
  185. int rc;
  186. struct controller *ctrl;
  187. struct slot *slot;
  188. u8 occupied, poweron;
  189. /* If this is not a "hotplug" service, we have no business here. */
  190. if (dev->service != PCIE_PORT_SERVICE_HP)
  191. return -ENODEV;
  192. if (!dev->port->subordinate) {
  193. /* Can happen if we run out of bus numbers during probe */
  194. dev_err(&dev->device,
  195. "Hotplug bridge without secondary bus, ignoring\n");
  196. return -ENODEV;
  197. }
  198. ctrl = pcie_init(dev);
  199. if (!ctrl) {
  200. dev_err(&dev->device, "Controller initialization failed\n");
  201. return -ENODEV;
  202. }
  203. set_service_data(dev, ctrl);
  204. /* Setup the slot information structures */
  205. rc = init_slot(ctrl);
  206. if (rc) {
  207. if (rc == -EBUSY)
  208. ctrl_warn(ctrl, "Slot already registered by another hotplug driver\n");
  209. else
  210. ctrl_err(ctrl, "Slot initialization failed (%d)\n", rc);
  211. goto err_out_release_ctlr;
  212. }
  213. /* Enable events after we have setup the data structures */
  214. rc = pcie_init_notification(ctrl);
  215. if (rc) {
  216. ctrl_err(ctrl, "Notification initialization failed (%d)\n", rc);
  217. goto err_out_free_ctrl_slot;
  218. }
  219. /* Check if slot is occupied */
  220. slot = ctrl->slot;
  221. pciehp_get_adapter_status(slot, &occupied);
  222. pciehp_get_power_status(slot, &poweron);
  223. if (occupied && pciehp_force) {
  224. mutex_lock(&slot->hotplug_lock);
  225. pciehp_enable_slot(slot);
  226. mutex_unlock(&slot->hotplug_lock);
  227. }
  228. /* If empty slot's power status is on, turn power off */
  229. if (!occupied && poweron && POWER_CTRL(ctrl))
  230. pciehp_power_off_slot(slot);
  231. return 0;
  232. err_out_free_ctrl_slot:
  233. cleanup_slot(ctrl);
  234. err_out_release_ctlr:
  235. pciehp_release_ctrl(ctrl);
  236. return -ENODEV;
  237. }
  238. static void pciehp_remove(struct pcie_device *dev)
  239. {
  240. struct controller *ctrl = get_service_data(dev);
  241. pcie_shutdown_notification(ctrl);
  242. cleanup_slot(ctrl);
  243. pciehp_release_ctrl(ctrl);
  244. }
  245. #ifdef CONFIG_PM
  246. static int pciehp_suspend(struct pcie_device *dev)
  247. {
  248. return 0;
  249. }
  250. static int pciehp_resume(struct pcie_device *dev)
  251. {
  252. struct controller *ctrl;
  253. struct slot *slot;
  254. u8 status;
  255. ctrl = get_service_data(dev);
  256. /* reinitialize the chipset's event detection logic */
  257. pcie_reenable_notification(ctrl);
  258. slot = ctrl->slot;
  259. /* Check if slot is occupied */
  260. pciehp_get_adapter_status(slot, &status);
  261. mutex_lock(&slot->hotplug_lock);
  262. if (status)
  263. pciehp_enable_slot(slot);
  264. else
  265. pciehp_disable_slot(slot);
  266. mutex_unlock(&slot->hotplug_lock);
  267. return 0;
  268. }
  269. #endif /* PM */
  270. static struct pcie_port_service_driver hpdriver_portdrv = {
  271. .name = PCIE_MODULE_NAME,
  272. .port_type = PCIE_ANY_PORT,
  273. .service = PCIE_PORT_SERVICE_HP,
  274. .probe = pciehp_probe,
  275. .remove = pciehp_remove,
  276. #ifdef CONFIG_PM
  277. .suspend = pciehp_suspend,
  278. .resume = pciehp_resume,
  279. #endif /* PM */
  280. };
  281. static int __init pcied_init(void)
  282. {
  283. int retval = 0;
  284. retval = pcie_port_service_register(&hpdriver_portdrv);
  285. dbg("pcie_port_service_register = %d\n", retval);
  286. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  287. if (retval)
  288. dbg("Failure to register service\n");
  289. return retval;
  290. }
  291. device_initcall(pcied_init);