shpchp_core.c 9.0 KB

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