shpchp_core.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Standard Hot Plug Controller Driver
  4. *
  5. * Copyright (C) 1995,2001 Compaq Computer Corporation
  6. * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
  7. * Copyright (C) 2001 IBM Corp.
  8. * Copyright (C) 2003-2004 Intel Corporation
  9. *
  10. * All rights reserved.
  11. *
  12. * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
  13. *
  14. */
  15. #include <linux/module.h>
  16. #include <linux/moduleparam.h>
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/slab.h>
  20. #include <linux/pci.h>
  21. #include "shpchp.h"
  22. /* Global variables */
  23. bool shpchp_debug;
  24. bool shpchp_poll_mode;
  25. int shpchp_poll_time;
  26. #define DRIVER_VERSION "0.4"
  27. #define DRIVER_AUTHOR "Dan Zink <dan.zink@compaq.com>, Greg Kroah-Hartman <greg@kroah.com>, Dely Sy <dely.l.sy@intel.com>"
  28. #define DRIVER_DESC "Standard Hot Plug PCI Controller Driver"
  29. MODULE_AUTHOR(DRIVER_AUTHOR);
  30. MODULE_DESCRIPTION(DRIVER_DESC);
  31. module_param(shpchp_debug, bool, 0644);
  32. module_param(shpchp_poll_mode, bool, 0644);
  33. module_param(shpchp_poll_time, int, 0644);
  34. MODULE_PARM_DESC(shpchp_debug, "Debugging mode enabled or not");
  35. MODULE_PARM_DESC(shpchp_poll_mode, "Using polling mechanism for hot-plug events or not");
  36. MODULE_PARM_DESC(shpchp_poll_time, "Polling mechanism frequency, in seconds");
  37. #define SHPC_MODULE_NAME "shpchp"
  38. static int set_attention_status(struct hotplug_slot *slot, u8 value);
  39. static int enable_slot(struct hotplug_slot *slot);
  40. static int disable_slot(struct hotplug_slot *slot);
  41. static int get_power_status(struct hotplug_slot *slot, u8 *value);
  42. static int get_attention_status(struct hotplug_slot *slot, u8 *value);
  43. static int get_latch_status(struct hotplug_slot *slot, u8 *value);
  44. static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
  45. static const struct hotplug_slot_ops shpchp_hotplug_slot_ops = {
  46. .set_attention_status = set_attention_status,
  47. .enable_slot = enable_slot,
  48. .disable_slot = disable_slot,
  49. .get_power_status = get_power_status,
  50. .get_attention_status = get_attention_status,
  51. .get_latch_status = get_latch_status,
  52. .get_adapter_status = get_adapter_status,
  53. };
  54. static int init_slots(struct controller *ctrl)
  55. {
  56. struct slot *slot;
  57. struct hotplug_slot *hotplug_slot;
  58. char name[SLOT_NAME_SIZE];
  59. int retval;
  60. int i;
  61. for (i = 0; i < ctrl->num_slots; i++) {
  62. slot = kzalloc(sizeof(*slot), GFP_KERNEL);
  63. if (!slot) {
  64. retval = -ENOMEM;
  65. goto error;
  66. }
  67. hotplug_slot = &slot->hotplug_slot;
  68. slot->hp_slot = i;
  69. slot->ctrl = ctrl;
  70. slot->bus = ctrl->pci_dev->subordinate->number;
  71. slot->device = ctrl->slot_device_offset + i;
  72. slot->number = ctrl->first_slot + (ctrl->slot_num_inc * i);
  73. slot->wq = alloc_workqueue("shpchp-%d", 0, 0, slot->number);
  74. if (!slot->wq) {
  75. retval = -ENOMEM;
  76. goto error_slot;
  77. }
  78. mutex_init(&slot->lock);
  79. INIT_DELAYED_WORK(&slot->work, shpchp_queue_pushbutton_work);
  80. /* register this slot with the hotplug pci core */
  81. snprintf(name, SLOT_NAME_SIZE, "%d", slot->number);
  82. hotplug_slot->ops = &shpchp_hotplug_slot_ops;
  83. ctrl_dbg(ctrl, "Registering domain:bus:dev=%04x:%02x:%02x hp_slot=%x sun=%x slot_device_offset=%x\n",
  84. pci_domain_nr(ctrl->pci_dev->subordinate),
  85. slot->bus, slot->device, slot->hp_slot, slot->number,
  86. ctrl->slot_device_offset);
  87. retval = pci_hp_register(hotplug_slot,
  88. ctrl->pci_dev->subordinate, slot->device, name);
  89. if (retval) {
  90. ctrl_err(ctrl, "pci_hp_register failed with error %d\n",
  91. retval);
  92. goto error_slotwq;
  93. }
  94. get_power_status(hotplug_slot, &slot->pwr_save);
  95. get_attention_status(hotplug_slot, &slot->attention_save);
  96. get_latch_status(hotplug_slot, &slot->latch_save);
  97. get_adapter_status(hotplug_slot, &slot->presence_save);
  98. list_add(&slot->slot_list, &ctrl->slot_list);
  99. }
  100. return 0;
  101. error_slotwq:
  102. destroy_workqueue(slot->wq);
  103. error_slot:
  104. kfree(slot);
  105. error:
  106. return retval;
  107. }
  108. void cleanup_slots(struct controller *ctrl)
  109. {
  110. struct slot *slot, *next;
  111. list_for_each_entry_safe(slot, next, &ctrl->slot_list, slot_list) {
  112. list_del(&slot->slot_list);
  113. cancel_delayed_work(&slot->work);
  114. destroy_workqueue(slot->wq);
  115. pci_hp_deregister(&slot->hotplug_slot);
  116. kfree(slot);
  117. }
  118. }
  119. /*
  120. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  121. */
  122. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  123. {
  124. struct slot *slot = get_slot(hotplug_slot);
  125. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  126. __func__, slot_name(slot));
  127. slot->attention_save = status;
  128. shpchp_set_attention_status(slot, status);
  129. return 0;
  130. }
  131. static int enable_slot(struct hotplug_slot *hotplug_slot)
  132. {
  133. struct slot *slot = get_slot(hotplug_slot);
  134. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  135. __func__, slot_name(slot));
  136. return shpchp_sysfs_enable_slot(slot);
  137. }
  138. static int disable_slot(struct hotplug_slot *hotplug_slot)
  139. {
  140. struct slot *slot = get_slot(hotplug_slot);
  141. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  142. __func__, slot_name(slot));
  143. return shpchp_sysfs_disable_slot(slot);
  144. }
  145. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  146. {
  147. struct slot *slot = get_slot(hotplug_slot);
  148. int retval;
  149. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  150. __func__, slot_name(slot));
  151. retval = shpchp_get_power_status(slot, value);
  152. if (retval < 0)
  153. *value = slot->pwr_save;
  154. return 0;
  155. }
  156. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  157. {
  158. struct slot *slot = get_slot(hotplug_slot);
  159. int retval;
  160. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  161. __func__, slot_name(slot));
  162. retval = shpchp_get_attention_status(slot, value);
  163. if (retval < 0)
  164. *value = slot->attention_save;
  165. return 0;
  166. }
  167. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  168. {
  169. struct slot *slot = get_slot(hotplug_slot);
  170. int retval;
  171. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  172. __func__, slot_name(slot));
  173. retval = shpchp_get_latch_status(slot, value);
  174. if (retval < 0)
  175. *value = slot->latch_save;
  176. return 0;
  177. }
  178. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  179. {
  180. struct slot *slot = get_slot(hotplug_slot);
  181. int retval;
  182. ctrl_dbg(slot->ctrl, "%s: physical_slot = %s\n",
  183. __func__, slot_name(slot));
  184. retval = shpchp_get_adapter_status(slot, value);
  185. if (retval < 0)
  186. *value = slot->presence_save;
  187. return 0;
  188. }
  189. static bool shpc_capable(struct pci_dev *bridge)
  190. {
  191. /*
  192. * It is assumed that AMD GOLAM chips support SHPC but they do not
  193. * have SHPC capability.
  194. */
  195. if (bridge->vendor == PCI_VENDOR_ID_AMD &&
  196. bridge->device == PCI_DEVICE_ID_AMD_GOLAM_7450)
  197. return true;
  198. if (pci_find_capability(bridge, PCI_CAP_ID_SHPC))
  199. return true;
  200. return false;
  201. }
  202. static int shpc_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  203. {
  204. int rc;
  205. struct controller *ctrl;
  206. if (!shpc_capable(pdev))
  207. return -ENODEV;
  208. if (acpi_get_hp_hw_control_from_firmware(pdev))
  209. return -ENODEV;
  210. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  211. if (!ctrl)
  212. goto err_out_none;
  213. INIT_LIST_HEAD(&ctrl->slot_list);
  214. rc = shpc_init(ctrl, pdev);
  215. if (rc) {
  216. ctrl_dbg(ctrl, "Controller initialization failed\n");
  217. goto err_out_free_ctrl;
  218. }
  219. pci_set_drvdata(pdev, ctrl);
  220. /* Setup the slot information structures */
  221. rc = init_slots(ctrl);
  222. if (rc) {
  223. ctrl_err(ctrl, "Slot initialization failed\n");
  224. goto err_out_release_ctlr;
  225. }
  226. rc = shpchp_create_ctrl_files(ctrl);
  227. if (rc)
  228. goto err_cleanup_slots;
  229. pdev->shpc_managed = 1;
  230. return 0;
  231. err_cleanup_slots:
  232. cleanup_slots(ctrl);
  233. err_out_release_ctlr:
  234. shpchp_release_ctlr(ctrl);
  235. err_out_free_ctrl:
  236. kfree(ctrl);
  237. err_out_none:
  238. return -ENODEV;
  239. }
  240. static void shpc_remove(struct pci_dev *dev)
  241. {
  242. struct controller *ctrl = pci_get_drvdata(dev);
  243. dev->shpc_managed = 0;
  244. shpchp_remove_ctrl_files(ctrl);
  245. shpchp_release_ctlr(ctrl);
  246. kfree(ctrl);
  247. }
  248. static const struct pci_device_id shpcd_pci_tbl[] = {
  249. {PCI_DEVICE_CLASS(PCI_CLASS_BRIDGE_PCI_NORMAL, ~0)},
  250. { /* end: all zeroes */ }
  251. };
  252. MODULE_DEVICE_TABLE(pci, shpcd_pci_tbl);
  253. static struct pci_driver shpc_driver = {
  254. .name = SHPC_MODULE_NAME,
  255. .id_table = shpcd_pci_tbl,
  256. .probe = shpc_probe,
  257. .remove = shpc_remove,
  258. };
  259. static int __init shpcd_init(void)
  260. {
  261. int retval;
  262. retval = pci_register_driver(&shpc_driver);
  263. dbg("%s: pci_register_driver = %d\n", __func__, retval);
  264. info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
  265. return retval;
  266. }
  267. static void __exit shpcd_cleanup(void)
  268. {
  269. dbg("unload_shpchpd()\n");
  270. pci_unregister_driver(&shpc_driver);
  271. info(DRIVER_DESC " version: " DRIVER_VERSION " unloaded\n");
  272. }
  273. module_init(shpcd_init);
  274. module_exit(shpcd_cleanup);