pciehp_core.c 11 KB

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