pciehp_core.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. #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. /**
  66. * release_slot - free up the memory used by a slot
  67. * @hotplug_slot: slot to free
  68. */
  69. static void release_slot(struct hotplug_slot *hotplug_slot)
  70. {
  71. struct slot *slot = hotplug_slot->private;
  72. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  73. __func__, hotplug_slot_name(hotplug_slot));
  74. kfree(hotplug_slot->ops);
  75. kfree(hotplug_slot->info);
  76. kfree(hotplug_slot);
  77. }
  78. static int init_slot(struct controller *ctrl)
  79. {
  80. struct slot *slot = ctrl->slot;
  81. struct hotplug_slot *hotplug = NULL;
  82. struct hotplug_slot_info *info = NULL;
  83. struct hotplug_slot_ops *ops = NULL;
  84. char name[SLOT_NAME_SIZE];
  85. int retval = -ENOMEM;
  86. hotplug = kzalloc(sizeof(*hotplug), GFP_KERNEL);
  87. if (!hotplug)
  88. goto out;
  89. info = kzalloc(sizeof(*info), GFP_KERNEL);
  90. if (!info)
  91. goto out;
  92. /* Setup hotplug slot ops */
  93. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  94. if (!ops)
  95. goto out;
  96. ops->enable_slot = enable_slot;
  97. ops->disable_slot = disable_slot;
  98. ops->get_power_status = get_power_status;
  99. ops->get_adapter_status = get_adapter_status;
  100. if (MRL_SENS(ctrl))
  101. ops->get_latch_status = get_latch_status;
  102. if (ATTN_LED(ctrl)) {
  103. ops->get_attention_status = get_attention_status;
  104. ops->set_attention_status = set_attention_status;
  105. }
  106. /* register this slot with the hotplug pci core */
  107. hotplug->info = info;
  108. hotplug->private = slot;
  109. hotplug->release = &release_slot;
  110. hotplug->ops = ops;
  111. slot->hotplug_slot = hotplug;
  112. snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
  113. ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:00 sun=%x\n",
  114. pci_domain_nr(ctrl->pcie->port->subordinate),
  115. ctrl->pcie->port->subordinate->number, PSN(ctrl));
  116. retval = pci_hp_register(hotplug,
  117. ctrl->pcie->port->subordinate, 0, name);
  118. if (retval)
  119. ctrl_err(ctrl,
  120. "pci_hp_register failed with error %d\n", retval);
  121. out:
  122. if (retval) {
  123. kfree(ops);
  124. kfree(info);
  125. kfree(hotplug);
  126. }
  127. return retval;
  128. }
  129. static void cleanup_slot(struct controller *ctrl)
  130. {
  131. pci_hp_deregister(ctrl->slot->hotplug_slot);
  132. }
  133. /*
  134. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  135. */
  136. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  137. {
  138. struct slot *slot = hotplug_slot->private;
  139. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  140. __func__, slot_name(slot));
  141. return pciehp_set_attention_status(slot, status);
  142. }
  143. static int enable_slot(struct hotplug_slot *hotplug_slot)
  144. {
  145. struct slot *slot = hotplug_slot->private;
  146. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  147. __func__, slot_name(slot));
  148. return pciehp_sysfs_enable_slot(slot);
  149. }
  150. static int disable_slot(struct hotplug_slot *hotplug_slot)
  151. {
  152. struct slot *slot = hotplug_slot->private;
  153. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  154. __func__, slot_name(slot));
  155. return pciehp_sysfs_disable_slot(slot);
  156. }
  157. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  158. {
  159. struct slot *slot = hotplug_slot->private;
  160. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  161. __func__, slot_name(slot));
  162. return pciehp_get_power_status(slot, value);
  163. }
  164. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  165. {
  166. struct slot *slot = hotplug_slot->private;
  167. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  168. __func__, slot_name(slot));
  169. return pciehp_get_attention_status(slot, value);
  170. }
  171. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  172. {
  173. struct slot *slot = hotplug_slot->private;
  174. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  175. __func__, slot_name(slot));
  176. return pciehp_get_latch_status(slot, value);
  177. }
  178. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  179. {
  180. struct slot *slot = hotplug_slot->private;
  181. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  182. __func__, slot_name(slot));
  183. return pciehp_get_adapter_status(slot, value);
  184. }
  185. static int pciehp_probe(struct pcie_device *dev)
  186. {
  187. int rc;
  188. struct controller *ctrl;
  189. struct slot *slot;
  190. u8 occupied, poweron;
  191. if (pciehp_force)
  192. dev_info(&dev->device,
  193. "Bypassing BIOS check for pciehp use on %s\n",
  194. pci_name(dev->port));
  195. else if (pciehp_acpi_slot_detection_check(dev->port))
  196. goto err_out_none;
  197. if (!dev->port->subordinate) {
  198. /* Can happen if we run out of bus numbers during probe */
  199. dev_err(&dev->device,
  200. "Hotplug bridge without secondary bus, ignoring\n");
  201. goto err_out_none;
  202. }
  203. ctrl = pcie_init(dev);
  204. if (!ctrl) {
  205. dev_err(&dev->device, "Controller initialization failed\n");
  206. goto err_out_none;
  207. }
  208. set_service_data(dev, ctrl);
  209. /* Setup the slot information structures */
  210. rc = init_slot(ctrl);
  211. if (rc) {
  212. if (rc == -EBUSY)
  213. ctrl_warn(ctrl, "Slot already registered by another "
  214. "hotplug driver\n");
  215. else
  216. ctrl_err(ctrl, "Slot initialization failed\n");
  217. goto err_out_release_ctlr;
  218. }
  219. /* Enable events after we have setup the data structures */
  220. rc = pcie_init_notification(ctrl);
  221. if (rc) {
  222. ctrl_err(ctrl, "Notification initialization failed\n");
  223. goto err_out_free_ctrl_slot;
  224. }
  225. /* Check if slot is occupied */
  226. slot = ctrl->slot;
  227. pciehp_get_adapter_status(slot, &occupied);
  228. pciehp_get_power_status(slot, &poweron);
  229. if (occupied && pciehp_force)
  230. pciehp_enable_slot(slot);
  231. /* If empty slot's power status is on, turn power off */
  232. if (!occupied && poweron && POWER_CTRL(ctrl))
  233. pciehp_power_off_slot(slot);
  234. return 0;
  235. err_out_free_ctrl_slot:
  236. cleanup_slot(ctrl);
  237. err_out_release_ctlr:
  238. pciehp_release_ctrl(ctrl);
  239. err_out_none:
  240. return -ENODEV;
  241. }
  242. static void pciehp_remove(struct pcie_device *dev)
  243. {
  244. struct controller *ctrl = get_service_data(dev);
  245. cleanup_slot(ctrl);
  246. pciehp_release_ctrl(ctrl);
  247. }
  248. #ifdef CONFIG_PM
  249. static int pciehp_suspend (struct pcie_device *dev)
  250. {
  251. dev_info(&dev->device, "%s ENTRY\n", __func__);
  252. return 0;
  253. }
  254. static int pciehp_resume (struct pcie_device *dev)
  255. {
  256. dev_info(&dev->device, "%s ENTRY\n", __func__);
  257. if (pciehp_force) {
  258. struct controller *ctrl = get_service_data(dev);
  259. struct slot *slot;
  260. u8 status;
  261. /* reinitialize the chipset's event detection logic */
  262. pcie_enable_notification(ctrl);
  263. slot = ctrl->slot;
  264. /* Check if slot is occupied */
  265. pciehp_get_adapter_status(slot, &status);
  266. if (status)
  267. pciehp_enable_slot(slot);
  268. else
  269. pciehp_disable_slot(slot);
  270. }
  271. return 0;
  272. }
  273. #endif /* PM */
  274. static struct pcie_port_service_driver hpdriver_portdrv = {
  275. .name = PCIE_MODULE_NAME,
  276. .port_type = PCIE_ANY_PORT,
  277. .service = PCIE_PORT_SERVICE_HP,
  278. .probe = pciehp_probe,
  279. .remove = pciehp_remove,
  280. #ifdef CONFIG_PM
  281. .suspend = pciehp_suspend,
  282. .resume = pciehp_resume,
  283. #endif /* PM */
  284. };
  285. static int __init pcied_init(void)
  286. {
  287. int retval = 0;
  288. pciehp_firmware_init();
  289. retval = pcie_port_service_register(&hpdriver_portdrv);
  290. dbg("pcie_port_service_register = %d\n", retval);
  291. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  292. if (retval)
  293. dbg("Failure to register service\n");
  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. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  301. }
  302. module_init(pcied_init);
  303. module_exit(pcied_cleanup);