shpchp_core.c 8.5 KB

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