pciehp_core.c 8.9 KB

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