pciehp_core.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * PCI Express 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. * Authors:
  15. * Dan Zink <dan.zink@compaq.com>
  16. * Greg Kroah-Hartman <greg@kroah.com>
  17. * Dely Sy <dely.l.sy@intel.com>"
  18. */
  19. #include <linux/moduleparam.h>
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/types.h>
  23. #include <linux/pci.h>
  24. #include "pciehp.h"
  25. #include <linux/interrupt.h>
  26. #include <linux/time.h>
  27. #include "../pci.h"
  28. /* Global variables */
  29. bool pciehp_debug;
  30. bool pciehp_poll_mode;
  31. int pciehp_poll_time;
  32. /*
  33. * not really modular, but the easiest way to keep compat with existing
  34. * bootargs behaviour is to continue using module_param here.
  35. */
  36. module_param(pciehp_debug, bool, 0644);
  37. module_param(pciehp_poll_mode, bool, 0644);
  38. module_param(pciehp_poll_time, int, 0644);
  39. MODULE_PARM_DESC(pciehp_debug, "Debugging mode enabled or not");
  40. MODULE_PARM_DESC(pciehp_poll_mode, "Using polling mechanism for hot-plug events or not");
  41. MODULE_PARM_DESC(pciehp_poll_time, "Polling mechanism frequency, in seconds");
  42. #define PCIE_MODULE_NAME "pciehp"
  43. static int set_attention_status(struct hotplug_slot *slot, u8 value);
  44. static int enable_slot(struct hotplug_slot *slot);
  45. static int disable_slot(struct hotplug_slot *slot);
  46. static int get_power_status(struct hotplug_slot *slot, u8 *value);
  47. static int get_attention_status(struct hotplug_slot *slot, u8 *value);
  48. static int get_latch_status(struct hotplug_slot *slot, u8 *value);
  49. static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
  50. static int reset_slot(struct hotplug_slot *slot, int probe);
  51. static int init_slot(struct controller *ctrl)
  52. {
  53. struct slot *slot = ctrl->slot;
  54. struct hotplug_slot *hotplug = NULL;
  55. struct hotplug_slot_info *info = NULL;
  56. struct hotplug_slot_ops *ops = NULL;
  57. char name[SLOT_NAME_SIZE];
  58. int retval = -ENOMEM;
  59. hotplug = kzalloc(sizeof(*hotplug), GFP_KERNEL);
  60. if (!hotplug)
  61. goto out;
  62. info = kzalloc(sizeof(*info), GFP_KERNEL);
  63. if (!info)
  64. goto out;
  65. /* Setup hotplug slot ops */
  66. ops = kzalloc(sizeof(*ops), GFP_KERNEL);
  67. if (!ops)
  68. goto out;
  69. ops->enable_slot = enable_slot;
  70. ops->disable_slot = disable_slot;
  71. ops->get_power_status = get_power_status;
  72. ops->get_adapter_status = get_adapter_status;
  73. ops->reset_slot = reset_slot;
  74. if (MRL_SENS(ctrl))
  75. ops->get_latch_status = get_latch_status;
  76. if (ATTN_LED(ctrl)) {
  77. ops->get_attention_status = get_attention_status;
  78. ops->set_attention_status = set_attention_status;
  79. } else if (ctrl->pcie->port->hotplug_user_indicators) {
  80. ops->get_attention_status = pciehp_get_raw_indicator_status;
  81. ops->set_attention_status = pciehp_set_raw_indicator_status;
  82. }
  83. /* register this slot with the hotplug pci core */
  84. hotplug->info = info;
  85. hotplug->private = slot;
  86. hotplug->ops = ops;
  87. slot->hotplug_slot = hotplug;
  88. snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
  89. retval = pci_hp_initialize(hotplug,
  90. ctrl->pcie->port->subordinate, 0, name);
  91. if (retval)
  92. ctrl_err(ctrl, "pci_hp_initialize failed: error %d\n", retval);
  93. out:
  94. if (retval) {
  95. kfree(ops);
  96. kfree(info);
  97. kfree(hotplug);
  98. }
  99. return retval;
  100. }
  101. static void cleanup_slot(struct controller *ctrl)
  102. {
  103. struct hotplug_slot *hotplug_slot = ctrl->slot->hotplug_slot;
  104. pci_hp_destroy(hotplug_slot);
  105. kfree(hotplug_slot->ops);
  106. kfree(hotplug_slot->info);
  107. kfree(hotplug_slot);
  108. }
  109. /*
  110. * set_attention_status - Turns the Amber LED for a slot on, off or blink
  111. */
  112. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  113. {
  114. struct slot *slot = hotplug_slot->private;
  115. struct pci_dev *pdev = slot->ctrl->pcie->port;
  116. pci_config_pm_runtime_get(pdev);
  117. pciehp_set_attention_status(slot, status);
  118. pci_config_pm_runtime_put(pdev);
  119. return 0;
  120. }
  121. static int enable_slot(struct hotplug_slot *hotplug_slot)
  122. {
  123. struct slot *slot = hotplug_slot->private;
  124. return pciehp_sysfs_enable_slot(slot);
  125. }
  126. static int disable_slot(struct hotplug_slot *hotplug_slot)
  127. {
  128. struct slot *slot = hotplug_slot->private;
  129. return pciehp_sysfs_disable_slot(slot);
  130. }
  131. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  132. {
  133. struct slot *slot = hotplug_slot->private;
  134. struct pci_dev *pdev = slot->ctrl->pcie->port;
  135. pci_config_pm_runtime_get(pdev);
  136. pciehp_get_power_status(slot, value);
  137. pci_config_pm_runtime_put(pdev);
  138. return 0;
  139. }
  140. static int get_attention_status(struct hotplug_slot *hotplug_slot, u8 *value)
  141. {
  142. struct slot *slot = hotplug_slot->private;
  143. pciehp_get_attention_status(slot, value);
  144. return 0;
  145. }
  146. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  147. {
  148. struct slot *slot = hotplug_slot->private;
  149. struct pci_dev *pdev = slot->ctrl->pcie->port;
  150. pci_config_pm_runtime_get(pdev);
  151. pciehp_get_latch_status(slot, value);
  152. pci_config_pm_runtime_put(pdev);
  153. return 0;
  154. }
  155. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  156. {
  157. struct slot *slot = hotplug_slot->private;
  158. struct pci_dev *pdev = slot->ctrl->pcie->port;
  159. pci_config_pm_runtime_get(pdev);
  160. pciehp_get_adapter_status(slot, value);
  161. pci_config_pm_runtime_put(pdev);
  162. return 0;
  163. }
  164. static int reset_slot(struct hotplug_slot *hotplug_slot, int probe)
  165. {
  166. struct slot *slot = hotplug_slot->private;
  167. return pciehp_reset_slot(slot, probe);
  168. }
  169. /**
  170. * pciehp_check_presence() - synthesize event if presence has changed
  171. *
  172. * On probe and resume, an explicit presence check is necessary to bring up an
  173. * occupied slot or bring down an unoccupied slot. This can't be triggered by
  174. * events in the Slot Status register, they may be stale and are therefore
  175. * cleared. Secondly, sending an interrupt for "events that occur while
  176. * interrupt generation is disabled [when] interrupt generation is subsequently
  177. * enabled" is optional per PCIe r4.0, sec 6.7.3.4.
  178. */
  179. static void pciehp_check_presence(struct controller *ctrl)
  180. {
  181. struct slot *slot = ctrl->slot;
  182. u8 occupied;
  183. down_read(&ctrl->reset_lock);
  184. mutex_lock(&slot->lock);
  185. pciehp_get_adapter_status(slot, &occupied);
  186. if ((occupied && (slot->state == OFF_STATE ||
  187. slot->state == BLINKINGON_STATE)) ||
  188. (!occupied && (slot->state == ON_STATE ||
  189. slot->state == BLINKINGOFF_STATE)))
  190. pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
  191. mutex_unlock(&slot->lock);
  192. up_read(&ctrl->reset_lock);
  193. }
  194. static int pciehp_probe(struct pcie_device *dev)
  195. {
  196. int rc;
  197. struct controller *ctrl;
  198. struct slot *slot;
  199. /* If this is not a "hotplug" service, we have no business here. */
  200. if (dev->service != PCIE_PORT_SERVICE_HP)
  201. return -ENODEV;
  202. if (!dev->port->subordinate) {
  203. /* Can happen if we run out of bus numbers during probe */
  204. dev_err(&dev->device,
  205. "Hotplug bridge without secondary bus, ignoring\n");
  206. return -ENODEV;
  207. }
  208. ctrl = pcie_init(dev);
  209. if (!ctrl) {
  210. dev_err(&dev->device, "Controller initialization failed\n");
  211. return -ENODEV;
  212. }
  213. set_service_data(dev, ctrl);
  214. /* Setup the slot information structures */
  215. rc = init_slot(ctrl);
  216. if (rc) {
  217. if (rc == -EBUSY)
  218. ctrl_warn(ctrl, "Slot already registered by another hotplug driver\n");
  219. else
  220. ctrl_err(ctrl, "Slot initialization failed (%d)\n", rc);
  221. goto err_out_release_ctlr;
  222. }
  223. /* Enable events after we have setup the data structures */
  224. rc = pcie_init_notification(ctrl);
  225. if (rc) {
  226. ctrl_err(ctrl, "Notification initialization failed (%d)\n", rc);
  227. goto err_out_free_ctrl_slot;
  228. }
  229. /* Publish to user space */
  230. slot = ctrl->slot;
  231. rc = pci_hp_add(slot->hotplug_slot);
  232. if (rc) {
  233. ctrl_err(ctrl, "Publication to user space failed (%d)\n", rc);
  234. goto err_out_shutdown_notification;
  235. }
  236. pciehp_check_presence(ctrl);
  237. return 0;
  238. err_out_shutdown_notification:
  239. pcie_shutdown_notification(ctrl);
  240. err_out_free_ctrl_slot:
  241. cleanup_slot(ctrl);
  242. err_out_release_ctlr:
  243. pciehp_release_ctrl(ctrl);
  244. return -ENODEV;
  245. }
  246. static void pciehp_remove(struct pcie_device *dev)
  247. {
  248. struct controller *ctrl = get_service_data(dev);
  249. pci_hp_del(ctrl->slot->hotplug_slot);
  250. pcie_shutdown_notification(ctrl);
  251. cleanup_slot(ctrl);
  252. pciehp_release_ctrl(ctrl);
  253. }
  254. #ifdef CONFIG_PM
  255. static int pciehp_suspend(struct pcie_device *dev)
  256. {
  257. return 0;
  258. }
  259. static int pciehp_resume_noirq(struct pcie_device *dev)
  260. {
  261. struct controller *ctrl = get_service_data(dev);
  262. struct slot *slot = ctrl->slot;
  263. /* pci_restore_state() just wrote to the Slot Control register */
  264. ctrl->cmd_started = jiffies;
  265. ctrl->cmd_busy = true;
  266. /* clear spurious events from rediscovery of inserted card */
  267. if (slot->state == ON_STATE || slot->state == BLINKINGOFF_STATE)
  268. pcie_clear_hotplug_events(ctrl);
  269. return 0;
  270. }
  271. static int pciehp_resume(struct pcie_device *dev)
  272. {
  273. struct controller *ctrl = get_service_data(dev);
  274. pciehp_check_presence(ctrl);
  275. return 0;
  276. }
  277. #endif /* PM */
  278. static struct pcie_port_service_driver hpdriver_portdrv = {
  279. .name = PCIE_MODULE_NAME,
  280. .port_type = PCIE_ANY_PORT,
  281. .service = PCIE_PORT_SERVICE_HP,
  282. .probe = pciehp_probe,
  283. .remove = pciehp_remove,
  284. #ifdef CONFIG_PM
  285. .suspend = pciehp_suspend,
  286. .resume_noirq = pciehp_resume_noirq,
  287. .resume = pciehp_resume,
  288. #endif /* PM */
  289. };
  290. int __init pcie_hp_init(void)
  291. {
  292. int retval = 0;
  293. retval = pcie_port_service_register(&hpdriver_portdrv);
  294. dbg("pcie_port_service_register = %d\n", retval);
  295. if (retval)
  296. dbg("Failure to register service\n");
  297. return retval;
  298. }