pciehp_core.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. bool pciehp_force;
  43. struct workqueue_struct *pciehp_wq;
  44. #define DRIVER_VERSION "0.4"
  45. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  46. #define DRIVER_DESC "PCI Express Hot Plug Controller Driver"
  47. MODULE_AUTHOR(DRIVER_AUTHOR);
  48. MODULE_DESCRIPTION(DRIVER_DESC);
  49. MODULE_LICENSE("GPL");
  50. module_param(pciehp_debug, bool, 0644);
  51. module_param(pciehp_poll_mode, bool, 0644);
  52. module_param(pciehp_poll_time, int, 0644);
  53. module_param(pciehp_force, bool, 0644);
  54. MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
  55. MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
  56. MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
  57. MODULE_PARM_DESC(pciehp_force, "Force pciehp, even if OSHP is missing");
  58. #define PCIE_MODULE_NAME "pciehp"
  59. static int set_attention_status (struct hotplug_slot *slot, u8 value);
  60. static int enable_slot (struct hotplug_slot *slot);
  61. static int disable_slot (struct hotplug_slot *slot);
  62. static int get_power_status (struct hotplug_slot *slot, u8 *value);
  63. static int get_attention_status (struct hotplug_slot *slot, u8 *value);
  64. static int get_latch_status (struct hotplug_slot *slot, u8 *value);
  65. static int get_adapter_status (struct hotplug_slot *slot, u8 *value);
  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. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  74. __func__, hotplug_slot_name(hotplug_slot));
  75. kfree(hotplug_slot->ops);
  76. kfree(hotplug_slot->info);
  77. kfree(hotplug_slot);
  78. }
  79. static int init_slot(struct controller *ctrl)
  80. {
  81. struct slot *slot = ctrl->slot;
  82. struct hotplug_slot *hotplug = NULL;
  83. struct hotplug_slot_info *info = NULL;
  84. struct hotplug_slot_ops *ops = NULL;
  85. char name[SLOT_NAME_SIZE];
  86. int retval = -ENOMEM;
  87. hotplug = kzalloc(sizeof(*hotplug), GFP_KERNEL);
  88. if (!hotplug)
  89. goto out;
  90. info = kzalloc(sizeof(*info), GFP_KERNEL);
  91. if (!info)
  92. goto out;
  93. /* Setup hotplug slot ops */
  94. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  95. if (!ops)
  96. goto out;
  97. ops->enable_slot = enable_slot;
  98. ops->disable_slot = disable_slot;
  99. ops->get_power_status = get_power_status;
  100. ops->get_adapter_status = get_adapter_status;
  101. if (MRL_SENS(ctrl))
  102. ops->get_latch_status = get_latch_status;
  103. if (ATTN_LED(ctrl)) {
  104. ops->get_attention_status = get_attention_status;
  105. ops->set_attention_status = set_attention_status;
  106. }
  107. /* register this slot with the hotplug pci core */
  108. hotplug->info = info;
  109. hotplug->private = slot;
  110. hotplug->release = &release_slot;
  111. hotplug->ops = ops;
  112. slot->hotplug_slot = hotplug;
  113. snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
  114. ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:00 sun=%x\n",
  115. pci_domain_nr(ctrl->pcie->port->subordinate),
  116. ctrl->pcie->port->subordinate->number, PSN(ctrl));
  117. retval = pci_hp_register(hotplug,
  118. ctrl->pcie->port->subordinate, 0, name);
  119. if (retval)
  120. ctrl_err(ctrl,
  121. "pci_hp_register failed with error %d\n", retval);
  122. out:
  123. if (retval) {
  124. kfree(ops);
  125. kfree(info);
  126. kfree(hotplug);
  127. }
  128. return retval;
  129. }
  130. static void cleanup_slot(struct controller *ctrl)
  131. {
  132. pci_hp_deregister(ctrl->slot->hotplug_slot);
  133. }
  134. /*
  135. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  136. */
  137. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  138. {
  139. struct slot *slot = hotplug_slot->private;
  140. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  141. __func__, slot_name(slot));
  142. return pciehp_set_attention_status(slot, status);
  143. }
  144. static int enable_slot(struct hotplug_slot *hotplug_slot)
  145. {
  146. struct slot *slot = hotplug_slot->private;
  147. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  148. __func__, slot_name(slot));
  149. return pciehp_sysfs_enable_slot(slot);
  150. }
  151. static int disable_slot(struct hotplug_slot *hotplug_slot)
  152. {
  153. struct slot *slot = hotplug_slot->private;
  154. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  155. __func__, slot_name(slot));
  156. return pciehp_sysfs_disable_slot(slot);
  157. }
  158. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  159. {
  160. struct slot *slot = hotplug_slot->private;
  161. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  162. __func__, slot_name(slot));
  163. return pciehp_get_power_status(slot, value);
  164. }
  165. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  166. {
  167. struct slot *slot = hotplug_slot->private;
  168. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  169. __func__, slot_name(slot));
  170. return pciehp_get_attention_status(slot, value);
  171. }
  172. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  173. {
  174. struct slot *slot = hotplug_slot->private;
  175. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  176. __func__, slot_name(slot));
  177. return pciehp_get_latch_status(slot, value);
  178. }
  179. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  180. {
  181. struct slot *slot = hotplug_slot->private;
  182. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  183. __func__, slot_name(slot));
  184. return pciehp_get_adapter_status(slot, value);
  185. }
  186. static int pciehp_probe(struct pcie_device *dev)
  187. {
  188. int rc;
  189. struct controller *ctrl;
  190. struct slot *slot;
  191. u8 occupied, poweron;
  192. if (pciehp_force)
  193. dev_info(&dev->device,
  194. "Bypassing BIOS check for pciehp use on %s\n",
  195. pci_name(dev->port));
  196. else if (pciehp_acpi_slot_detection_check(dev->port))
  197. goto err_out_none;
  198. ctrl = pcie_init(dev);
  199. if (!ctrl) {
  200. dev_err(&dev->device, "Controller initialization failed\n");
  201. goto err_out_none;
  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 "
  209. "hotplug driver\n");
  210. else
  211. ctrl_err(ctrl, "Slot initialization failed\n");
  212. goto err_out_release_ctlr;
  213. }
  214. /* Enable events after we have setup the data structures */
  215. rc = pcie_init_notification(ctrl);
  216. if (rc) {
  217. ctrl_err(ctrl, "Notification initialization failed\n");
  218. goto err_out_free_ctrl_slot;
  219. }
  220. /* Check if slot is occupied */
  221. slot = ctrl->slot;
  222. pciehp_get_adapter_status(slot, &occupied);
  223. pciehp_get_power_status(slot, &poweron);
  224. if (occupied && pciehp_force)
  225. pciehp_enable_slot(slot);
  226. /* If empty slot's power status is on, turn power off */
  227. if (!occupied && poweron && POWER_CTRL(ctrl))
  228. pciehp_power_off_slot(slot);
  229. return 0;
  230. err_out_free_ctrl_slot:
  231. cleanup_slot(ctrl);
  232. err_out_release_ctlr:
  233. pciehp_release_ctrl(ctrl);
  234. err_out_none:
  235. return -ENODEV;
  236. }
  237. static void pciehp_remove(struct pcie_device *dev)
  238. {
  239. struct controller *ctrl = get_service_data(dev);
  240. cleanup_slot(ctrl);
  241. pciehp_release_ctrl(ctrl);
  242. }
  243. #ifdef CONFIG_PM
  244. static int pciehp_suspend (struct pcie_device *dev)
  245. {
  246. dev_info(&dev->device, "%s ENTRY\n", __func__);
  247. return 0;
  248. }
  249. static int pciehp_resume (struct pcie_device *dev)
  250. {
  251. dev_info(&dev->device, "%s ENTRY\n", __func__);
  252. if (pciehp_force) {
  253. struct controller *ctrl = get_service_data(dev);
  254. struct slot *slot;
  255. u8 status;
  256. /* reinitialize the chipset's event detection logic */
  257. pcie_enable_notification(ctrl);
  258. slot = ctrl->slot;
  259. /* Check if slot is occupied */
  260. pciehp_get_adapter_status(slot, &status);
  261. if (status)
  262. pciehp_enable_slot(slot);
  263. else
  264. pciehp_disable_slot(slot);
  265. }
  266. return 0;
  267. }
  268. #endif /* PM */
  269. static struct pcie_port_service_driver hpdriver_portdrv = {
  270. .name = PCIE_MODULE_NAME,
  271. .port_type = PCIE_ANY_PORT,
  272. .service = PCIE_PORT_SERVICE_HP,
  273. .probe = pciehp_probe,
  274. .remove = pciehp_remove,
  275. #ifdef CONFIG_PM
  276. .suspend = pciehp_suspend,
  277. .resume = pciehp_resume,
  278. #endif /* PM */
  279. };
  280. static int __init pcied_init(void)
  281. {
  282. int retval = 0;
  283. pciehp_wq = alloc_workqueue("pciehp", 0, 0);
  284. if (!pciehp_wq)
  285. return -ENOMEM;
  286. pciehp_firmware_init();
  287. retval = pcie_port_service_register(&hpdriver_portdrv);
  288. dbg("pcie_port_service_register = %d\n", retval);
  289. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  290. if (retval) {
  291. destroy_workqueue(pciehp_wq);
  292. dbg("Failure to register service\n");
  293. }
  294. return retval;
  295. }
  296. static void __exit pcied_cleanup(void)
  297. {
  298. dbg("unload_pciehpd()\n");
  299. pcie_port_service_unregister(&hpdriver_portdrv);
  300. destroy_workqueue(pciehp_wq);
  301. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  302. }
  303. module_init(pcied_init);
  304. module_exit(pcied_cleanup);