pciehp_core.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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/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. int pciehp_debug;
  39. int pciehp_poll_mode;
  40. int pciehp_poll_time;
  41. int pciehp_force;
  42. struct workqueue_struct *pciehp_wq;
  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 _OSC and OSHP are 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 get_max_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  66. static int get_cur_bus_speed (struct hotplug_slot *slot, enum pci_bus_speed *value);
  67. /**
  68. * release_slot - free up the memory used by a slot
  69. * @hotplug_slot: slot to free
  70. */
  71. static void release_slot(struct hotplug_slot *hotplug_slot)
  72. {
  73. struct slot *slot = hotplug_slot->private;
  74. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  75. __func__, hotplug_slot_name(hotplug_slot));
  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->get_max_bus_speed = get_max_bus_speed;
  103. ops->get_cur_bus_speed = get_cur_bus_speed;
  104. if (MRL_SENS(ctrl))
  105. ops->get_latch_status = get_latch_status;
  106. if (ATTN_LED(ctrl)) {
  107. ops->get_attention_status = get_attention_status;
  108. ops->set_attention_status = set_attention_status;
  109. }
  110. /* register this slot with the hotplug pci core */
  111. hotplug->info = info;
  112. hotplug->private = slot;
  113. hotplug->release = &release_slot;
  114. hotplug->ops = ops;
  115. slot->hotplug_slot = hotplug;
  116. snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
  117. ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:00 sun=%x\n",
  118. pci_domain_nr(ctrl->pcie->port->subordinate),
  119. ctrl->pcie->port->subordinate->number, PSN(ctrl));
  120. retval = pci_hp_register(hotplug,
  121. ctrl->pcie->port->subordinate, 0, name);
  122. if (retval) {
  123. ctrl_err(ctrl,
  124. "pci_hp_register failed with error %d\n", retval);
  125. goto out;
  126. }
  127. get_power_status(hotplug, &info->power_status);
  128. get_attention_status(hotplug, &info->attention_status);
  129. get_latch_status(hotplug, &info->latch_status);
  130. get_adapter_status(hotplug, &info->adapter_status);
  131. out:
  132. if (retval) {
  133. kfree(ops);
  134. kfree(info);
  135. kfree(hotplug);
  136. }
  137. return retval;
  138. }
  139. static void cleanup_slot(struct controller *ctrl)
  140. {
  141. pci_hp_deregister(ctrl->slot->hotplug_slot);
  142. }
  143. /*
  144. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  145. */
  146. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  147. {
  148. struct slot *slot = hotplug_slot->private;
  149. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  150. __func__, slot_name(slot));
  151. hotplug_slot->info->attention_status = status;
  152. pciehp_set_attention_status(slot, status);
  153. return 0;
  154. }
  155. static int enable_slot(struct hotplug_slot *hotplug_slot)
  156. {
  157. struct slot *slot = hotplug_slot->private;
  158. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  159. __func__, slot_name(slot));
  160. return pciehp_sysfs_enable_slot(slot);
  161. }
  162. static int disable_slot(struct hotplug_slot *hotplug_slot)
  163. {
  164. struct slot *slot = hotplug_slot->private;
  165. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  166. __func__, slot_name(slot));
  167. return pciehp_sysfs_disable_slot(slot);
  168. }
  169. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  170. {
  171. struct slot *slot = hotplug_slot->private;
  172. int retval;
  173. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  174. __func__, slot_name(slot));
  175. retval = pciehp_get_power_status(slot, value);
  176. if (retval < 0)
  177. *value = hotplug_slot->info->power_status;
  178. return 0;
  179. }
  180. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  181. {
  182. struct slot *slot = hotplug_slot->private;
  183. int retval;
  184. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  185. __func__, slot_name(slot));
  186. retval = pciehp_get_attention_status(slot, value);
  187. if (retval < 0)
  188. *value = hotplug_slot->info->attention_status;
  189. return 0;
  190. }
  191. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  192. {
  193. struct slot *slot = hotplug_slot->private;
  194. int retval;
  195. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  196. __func__, slot_name(slot));
  197. retval = pciehp_get_latch_status(slot, value);
  198. if (retval < 0)
  199. *value = hotplug_slot->info->latch_status;
  200. return 0;
  201. }
  202. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  203. {
  204. struct slot *slot = hotplug_slot->private;
  205. int retval;
  206. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  207. __func__, slot_name(slot));
  208. retval = pciehp_get_adapter_status(slot, value);
  209. if (retval < 0)
  210. *value = hotplug_slot->info->adapter_status;
  211. return 0;
  212. }
  213. static int get_max_bus_speed(struct hotplug_slot *hotplug_slot,
  214. enum pci_bus_speed *value)
  215. {
  216. struct slot *slot = hotplug_slot->private;
  217. int retval;
  218. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  219. __func__, slot_name(slot));
  220. retval = pciehp_get_max_link_speed(slot, value);
  221. if (retval < 0)
  222. *value = PCI_SPEED_UNKNOWN;
  223. return 0;
  224. }
  225. static int get_cur_bus_speed(struct hotplug_slot *hotplug_slot, enum pci_bus_speed *value)
  226. {
  227. struct slot *slot = hotplug_slot->private;
  228. int retval;
  229. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  230. __func__, slot_name(slot));
  231. retval = pciehp_get_cur_link_speed(slot, value);
  232. if (retval < 0)
  233. *value = PCI_SPEED_UNKNOWN;
  234. return 0;
  235. }
  236. static int pciehp_probe(struct pcie_device *dev)
  237. {
  238. int rc;
  239. struct controller *ctrl;
  240. struct slot *slot;
  241. u8 value;
  242. struct pci_dev *pdev = dev->port;
  243. if (pciehp_force)
  244. dev_info(&dev->device,
  245. "Bypassing BIOS check for pciehp use on %s\n",
  246. pci_name(pdev));
  247. else if (pciehp_get_hp_hw_control_from_firmware(pdev))
  248. goto err_out_none;
  249. ctrl = pcie_init(dev);
  250. if (!ctrl) {
  251. dev_err(&dev->device, "Controller initialization failed\n");
  252. goto err_out_none;
  253. }
  254. set_service_data(dev, ctrl);
  255. /* Setup the slot information structures */
  256. rc = init_slot(ctrl);
  257. if (rc) {
  258. if (rc == -EBUSY)
  259. ctrl_warn(ctrl, "Slot already registered by another "
  260. "hotplug driver\n");
  261. else
  262. ctrl_err(ctrl, "Slot initialization failed\n");
  263. goto err_out_release_ctlr;
  264. }
  265. /* Enable events after we have setup the data structures */
  266. rc = pcie_init_notification(ctrl);
  267. if (rc) {
  268. ctrl_err(ctrl, "Notification initialization failed\n");
  269. goto err_out_release_ctlr;
  270. }
  271. /* Check if slot is occupied */
  272. slot = ctrl->slot;
  273. pciehp_get_adapter_status(slot, &value);
  274. if (value) {
  275. if (pciehp_force)
  276. pciehp_enable_slot(slot);
  277. } else {
  278. /* Power off slot if not occupied */
  279. if (POWER_CTRL(ctrl)) {
  280. rc = pciehp_power_off_slot(slot);
  281. if (rc)
  282. goto err_out_free_ctrl_slot;
  283. }
  284. }
  285. return 0;
  286. err_out_free_ctrl_slot:
  287. cleanup_slot(ctrl);
  288. err_out_release_ctlr:
  289. pciehp_release_ctrl(ctrl);
  290. err_out_none:
  291. return -ENODEV;
  292. }
  293. static void pciehp_remove(struct pcie_device *dev)
  294. {
  295. struct controller *ctrl = get_service_data(dev);
  296. cleanup_slot(ctrl);
  297. pciehp_release_ctrl(ctrl);
  298. }
  299. #ifdef CONFIG_PM
  300. static int pciehp_suspend (struct pcie_device *dev)
  301. {
  302. dev_info(&dev->device, "%s ENTRY\n", __func__);
  303. return 0;
  304. }
  305. static int pciehp_resume (struct pcie_device *dev)
  306. {
  307. dev_info(&dev->device, "%s ENTRY\n", __func__);
  308. if (pciehp_force) {
  309. struct controller *ctrl = get_service_data(dev);
  310. struct slot *slot;
  311. u8 status;
  312. /* reinitialize the chipset's event detection logic */
  313. pcie_enable_notification(ctrl);
  314. slot = ctrl->slot;
  315. /* Check if slot is occupied */
  316. pciehp_get_adapter_status(slot, &status);
  317. if (status)
  318. pciehp_enable_slot(slot);
  319. else
  320. pciehp_disable_slot(slot);
  321. }
  322. return 0;
  323. }
  324. #endif /* PM */
  325. static struct pcie_port_service_driver hpdriver_portdrv = {
  326. .name = PCIE_MODULE_NAME,
  327. .port_type = PCIE_ANY_PORT,
  328. .service = PCIE_PORT_SERVICE_HP,
  329. .probe = pciehp_probe,
  330. .remove = pciehp_remove,
  331. #ifdef CONFIG_PM
  332. .suspend = pciehp_suspend,
  333. .resume = pciehp_resume,
  334. #endif /* PM */
  335. };
  336. static int __init pcied_init(void)
  337. {
  338. int retval = 0;
  339. pciehp_firmware_init();
  340. retval = pcie_port_service_register(&hpdriver_portdrv);
  341. dbg("pcie_port_service_register = %d\n", retval);
  342. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  343. if (retval)
  344. dbg("Failure to register service\n");
  345. return retval;
  346. }
  347. static void __exit pcied_cleanup(void)
  348. {
  349. dbg("unload_pciehpd()\n");
  350. pcie_port_service_unregister(&hpdriver_portdrv);
  351. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  352. }
  353. module_init(pcied_init);
  354. module_exit(pcied_cleanup);