shpchp_core.c 8.0 KB

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