pciehp_core.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. #define pr_fmt(fmt) "pciehp: " fmt
  20. #define dev_fmt pr_fmt
  21. #include <linux/bitfield.h>
  22. #include <linux/moduleparam.h>
  23. #include <linux/kernel.h>
  24. #include <linux/slab.h>
  25. #include <linux/types.h>
  26. #include <linux/pci.h>
  27. #include "pciehp.h"
  28. #include "../pci.h"
  29. /* Global variables */
  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_poll_mode, bool, 0644);
  37. module_param(pciehp_poll_time, int, 0644);
  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. static int set_attention_status(struct hotplug_slot *slot, u8 value);
  41. static int get_power_status(struct hotplug_slot *slot, u8 *value);
  42. static int get_latch_status(struct hotplug_slot *slot, u8 *value);
  43. static int get_adapter_status(struct hotplug_slot *slot, u8 *value);
  44. static int init_slot(struct controller *ctrl)
  45. {
  46. struct hotplug_slot_ops *ops;
  47. char name[SLOT_NAME_SIZE];
  48. int retval;
  49. /* Setup hotplug slot ops */
  50. ops = kzalloc_obj(*ops);
  51. if (!ops)
  52. return -ENOMEM;
  53. ops->enable_slot = pciehp_sysfs_enable_slot;
  54. ops->disable_slot = pciehp_sysfs_disable_slot;
  55. ops->get_power_status = get_power_status;
  56. ops->get_adapter_status = get_adapter_status;
  57. ops->reset_slot = pciehp_reset_slot;
  58. if (MRL_SENS(ctrl))
  59. ops->get_latch_status = get_latch_status;
  60. if (ATTN_LED(ctrl)) {
  61. ops->get_attention_status = pciehp_get_attention_status;
  62. ops->set_attention_status = set_attention_status;
  63. } else if (ctrl->pcie->port->hotplug_user_indicators) {
  64. ops->get_attention_status = pciehp_get_raw_indicator_status;
  65. ops->set_attention_status = pciehp_set_raw_indicator_status;
  66. }
  67. /* register this slot with the hotplug pci core */
  68. ctrl->hotplug_slot.ops = ops;
  69. snprintf(name, SLOT_NAME_SIZE, "%u", PSN(ctrl));
  70. retval = pci_hp_initialize(&ctrl->hotplug_slot,
  71. ctrl->pcie->port->subordinate, 0, name);
  72. if (retval) {
  73. ctrl_err(ctrl, "pci_hp_initialize failed: error %d\n", retval);
  74. kfree(ops);
  75. }
  76. return retval;
  77. }
  78. static void cleanup_slot(struct controller *ctrl)
  79. {
  80. struct hotplug_slot *hotplug_slot = &ctrl->hotplug_slot;
  81. pci_hp_destroy(hotplug_slot);
  82. kfree(hotplug_slot->ops);
  83. }
  84. /*
  85. * set_attention_status - Turns the Attention Indicator on, off or blinking
  86. */
  87. static int set_attention_status(struct hotplug_slot *hotplug_slot, u8 status)
  88. {
  89. struct controller *ctrl = to_ctrl(hotplug_slot);
  90. struct pci_dev *pdev = ctrl->pcie->port;
  91. if (status)
  92. status = FIELD_PREP(PCI_EXP_SLTCTL_AIC, status);
  93. else
  94. status = PCI_EXP_SLTCTL_ATTN_IND_OFF;
  95. pci_config_pm_runtime_get(pdev);
  96. pciehp_set_indicators(ctrl, INDICATOR_NOOP, status);
  97. pci_config_pm_runtime_put(pdev);
  98. return 0;
  99. }
  100. static int get_power_status(struct hotplug_slot *hotplug_slot, u8 *value)
  101. {
  102. struct controller *ctrl = to_ctrl(hotplug_slot);
  103. struct pci_dev *pdev = ctrl->pcie->port;
  104. pci_config_pm_runtime_get(pdev);
  105. pciehp_get_power_status(ctrl, value);
  106. pci_config_pm_runtime_put(pdev);
  107. return 0;
  108. }
  109. static int get_latch_status(struct hotplug_slot *hotplug_slot, u8 *value)
  110. {
  111. struct controller *ctrl = to_ctrl(hotplug_slot);
  112. struct pci_dev *pdev = ctrl->pcie->port;
  113. pci_config_pm_runtime_get(pdev);
  114. pciehp_get_latch_status(ctrl, value);
  115. pci_config_pm_runtime_put(pdev);
  116. return 0;
  117. }
  118. static int get_adapter_status(struct hotplug_slot *hotplug_slot, u8 *value)
  119. {
  120. struct controller *ctrl = to_ctrl(hotplug_slot);
  121. struct pci_dev *pdev = ctrl->pcie->port;
  122. int ret;
  123. pci_config_pm_runtime_get(pdev);
  124. ret = pciehp_card_present_or_link_active(ctrl);
  125. pci_config_pm_runtime_put(pdev);
  126. if (ret < 0)
  127. return ret;
  128. *value = ret;
  129. return 0;
  130. }
  131. /**
  132. * pciehp_check_presence() - synthesize event if presence has changed
  133. * @ctrl: controller to check
  134. *
  135. * On probe and resume, an explicit presence check is necessary to bring up an
  136. * occupied slot or bring down an unoccupied slot. This can't be triggered by
  137. * events in the Slot Status register, they may be stale and are therefore
  138. * cleared. Secondly, sending an interrupt for "events that occur while
  139. * interrupt generation is disabled [when] interrupt generation is subsequently
  140. * enabled" is optional per PCIe r4.0, sec 6.7.3.4.
  141. */
  142. static void pciehp_check_presence(struct controller *ctrl)
  143. {
  144. int occupied;
  145. down_read_nested(&ctrl->reset_lock, ctrl->depth);
  146. mutex_lock(&ctrl->state_lock);
  147. occupied = pciehp_card_present_or_link_active(ctrl);
  148. if ((occupied > 0 && (ctrl->state == OFF_STATE ||
  149. ctrl->state == BLINKINGON_STATE)) ||
  150. (!occupied && (ctrl->state == ON_STATE ||
  151. ctrl->state == BLINKINGOFF_STATE)))
  152. pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
  153. mutex_unlock(&ctrl->state_lock);
  154. up_read(&ctrl->reset_lock);
  155. }
  156. static int pciehp_probe(struct pcie_device *dev)
  157. {
  158. int rc;
  159. struct controller *ctrl;
  160. /* If this is not a "hotplug" service, we have no business here. */
  161. if (dev->service != PCIE_PORT_SERVICE_HP)
  162. return -ENODEV;
  163. if (!dev->port->subordinate) {
  164. /* Can happen if we run out of bus numbers during probe */
  165. pci_err(dev->port,
  166. "Hotplug bridge without secondary bus, ignoring\n");
  167. return -ENODEV;
  168. }
  169. ctrl = pcie_init(dev);
  170. if (!ctrl) {
  171. pci_err(dev->port, "Controller initialization failed\n");
  172. return -ENODEV;
  173. }
  174. set_service_data(dev, ctrl);
  175. /* Setup the slot information structures */
  176. rc = init_slot(ctrl);
  177. if (rc) {
  178. if (rc == -EBUSY)
  179. ctrl_warn(ctrl, "Slot already registered by another hotplug driver\n");
  180. else
  181. ctrl_err(ctrl, "Slot initialization failed (%d)\n", rc);
  182. goto err_out_release_ctlr;
  183. }
  184. /* Enable events after we have setup the data structures */
  185. rc = pcie_init_notification(ctrl);
  186. if (rc) {
  187. ctrl_err(ctrl, "Notification initialization failed (%d)\n", rc);
  188. goto err_out_free_ctrl_slot;
  189. }
  190. /* Publish to user space */
  191. rc = pci_hp_add(&ctrl->hotplug_slot);
  192. if (rc) {
  193. ctrl_err(ctrl, "Publication to user space failed (%d)\n", rc);
  194. goto err_out_shutdown_notification;
  195. }
  196. pciehp_check_presence(ctrl);
  197. return 0;
  198. err_out_shutdown_notification:
  199. pcie_shutdown_notification(ctrl);
  200. err_out_free_ctrl_slot:
  201. cleanup_slot(ctrl);
  202. err_out_release_ctlr:
  203. pciehp_release_ctrl(ctrl);
  204. return -ENODEV;
  205. }
  206. static void pciehp_remove(struct pcie_device *dev)
  207. {
  208. struct controller *ctrl = get_service_data(dev);
  209. pci_hp_del(&ctrl->hotplug_slot);
  210. pcie_shutdown_notification(ctrl);
  211. cleanup_slot(ctrl);
  212. pciehp_release_ctrl(ctrl);
  213. }
  214. #ifdef CONFIG_PM
  215. static bool pme_is_native(struct pcie_device *dev)
  216. {
  217. const struct pci_host_bridge *host;
  218. host = pci_find_host_bridge(dev->port->bus);
  219. return pcie_ports_native || host->native_pme;
  220. }
  221. static void pciehp_disable_interrupt(struct pcie_device *dev)
  222. {
  223. /*
  224. * Disable hotplug interrupt so that it does not trigger
  225. * immediately when the downstream link goes down.
  226. */
  227. if (pme_is_native(dev))
  228. pcie_disable_interrupt(get_service_data(dev));
  229. }
  230. #ifdef CONFIG_PM_SLEEP
  231. static int pciehp_suspend(struct pcie_device *dev)
  232. {
  233. /*
  234. * If the port is already runtime suspended we can keep it that
  235. * way.
  236. */
  237. if (dev_pm_skip_suspend(&dev->port->dev))
  238. return 0;
  239. pciehp_disable_interrupt(dev);
  240. return 0;
  241. }
  242. static int pciehp_resume_noirq(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. pcie_clear_hotplug_events(ctrl);
  251. /*
  252. * If hotplugged device was replaced with a different one
  253. * during system sleep, mark the old device disconnected
  254. * (to prevent its driver from accessing the new device)
  255. * and synthesize a Presence Detect Changed event.
  256. */
  257. if (pciehp_device_replaced(ctrl)) {
  258. ctrl_dbg(ctrl, "device replaced during system sleep\n");
  259. pci_walk_bus(ctrl->pcie->port->subordinate,
  260. pci_dev_set_disconnected, NULL);
  261. pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
  262. }
  263. }
  264. return 0;
  265. }
  266. #endif
  267. static int pciehp_resume(struct pcie_device *dev)
  268. {
  269. struct controller *ctrl = get_service_data(dev);
  270. if (pme_is_native(dev))
  271. pcie_enable_interrupt(ctrl);
  272. pciehp_check_presence(ctrl);
  273. return 0;
  274. }
  275. static int pciehp_runtime_suspend(struct pcie_device *dev)
  276. {
  277. pciehp_disable_interrupt(dev);
  278. return 0;
  279. }
  280. static int pciehp_runtime_resume(struct pcie_device *dev)
  281. {
  282. struct controller *ctrl = get_service_data(dev);
  283. /* pci_restore_state() just wrote to the Slot Control register */
  284. ctrl->cmd_started = jiffies;
  285. ctrl->cmd_busy = true;
  286. /* clear spurious events from rediscovery of inserted card */
  287. if ((ctrl->state == ON_STATE || ctrl->state == BLINKINGOFF_STATE) &&
  288. pme_is_native(dev))
  289. pcie_clear_hotplug_events(ctrl);
  290. return pciehp_resume(dev);
  291. }
  292. #endif /* PM */
  293. static struct pcie_port_service_driver hpdriver_portdrv = {
  294. .name = "pciehp",
  295. .port_type = PCIE_ANY_PORT,
  296. .service = PCIE_PORT_SERVICE_HP,
  297. .probe = pciehp_probe,
  298. .remove = pciehp_remove,
  299. #ifdef CONFIG_PM
  300. #ifdef CONFIG_PM_SLEEP
  301. .suspend = pciehp_suspend,
  302. .resume_noirq = pciehp_resume_noirq,
  303. .resume = pciehp_resume,
  304. #endif
  305. .runtime_suspend = pciehp_runtime_suspend,
  306. .runtime_resume = pciehp_runtime_resume,
  307. #endif /* PM */
  308. .slot_reset = pciehp_slot_reset,
  309. };
  310. int __init pcie_hp_init(void)
  311. {
  312. int retval = 0;
  313. retval = pcie_port_service_register(&hpdriver_portdrv);
  314. pr_debug("pcie_port_service_register = %d\n", retval);
  315. if (retval)
  316. pr_debug("Failure to register service\n");
  317. return retval;
  318. }