shpchp_core.c 9.2 KB

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