pciehp_ctrl.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. */
  15. #include <linux/kernel.h>
  16. #include <linux/types.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/pci.h>
  19. #include "pciehp.h"
  20. /* The following routines constitute the bulk of the
  21. hotplug controller logic
  22. */
  23. #define SAFE_REMOVAL true
  24. #define SURPRISE_REMOVAL false
  25. static void set_slot_off(struct controller *ctrl)
  26. {
  27. /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
  28. if (POWER_CTRL(ctrl)) {
  29. pciehp_power_off_slot(ctrl);
  30. /*
  31. * After turning power off, we must wait for at least 1 second
  32. * before taking any action that relies on power having been
  33. * removed from the slot/adapter.
  34. */
  35. msleep(1000);
  36. }
  37. pciehp_green_led_off(ctrl);
  38. pciehp_set_attention_status(ctrl, 1);
  39. }
  40. /**
  41. * board_added - Called after a board has been added to the system.
  42. * @ctrl: PCIe hotplug controller where board is added
  43. *
  44. * Turns power on for the board.
  45. * Configures board.
  46. */
  47. static int board_added(struct controller *ctrl)
  48. {
  49. int retval = 0;
  50. struct pci_bus *parent = ctrl->pcie->port->subordinate;
  51. if (POWER_CTRL(ctrl)) {
  52. /* Power on slot */
  53. retval = pciehp_power_on_slot(ctrl);
  54. if (retval)
  55. return retval;
  56. }
  57. pciehp_green_led_blink(ctrl);
  58. /* Check link training status */
  59. retval = pciehp_check_link_status(ctrl);
  60. if (retval) {
  61. ctrl_err(ctrl, "Failed to check link status\n");
  62. goto err_exit;
  63. }
  64. /* Check for a power fault */
  65. if (ctrl->power_fault_detected || pciehp_query_power_fault(ctrl)) {
  66. ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(ctrl));
  67. retval = -EIO;
  68. goto err_exit;
  69. }
  70. retval = pciehp_configure_device(ctrl);
  71. if (retval) {
  72. if (retval != -EEXIST) {
  73. ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
  74. pci_domain_nr(parent), parent->number);
  75. goto err_exit;
  76. }
  77. }
  78. pciehp_green_led_on(ctrl);
  79. pciehp_set_attention_status(ctrl, 0);
  80. return 0;
  81. err_exit:
  82. set_slot_off(ctrl);
  83. return retval;
  84. }
  85. /**
  86. * remove_board - Turns off slot and LEDs
  87. * @ctrl: PCIe hotplug controller where board is being removed
  88. * @safe_removal: whether the board is safely removed (versus surprise removed)
  89. */
  90. static void remove_board(struct controller *ctrl, bool safe_removal)
  91. {
  92. pciehp_unconfigure_device(ctrl, safe_removal);
  93. if (POWER_CTRL(ctrl)) {
  94. pciehp_power_off_slot(ctrl);
  95. /*
  96. * After turning power off, we must wait for at least 1 second
  97. * before taking any action that relies on power having been
  98. * removed from the slot/adapter.
  99. */
  100. msleep(1000);
  101. }
  102. /* turn off Green LED */
  103. pciehp_green_led_off(ctrl);
  104. }
  105. static int pciehp_enable_slot(struct controller *ctrl);
  106. static int pciehp_disable_slot(struct controller *ctrl, bool safe_removal);
  107. void pciehp_request(struct controller *ctrl, int action)
  108. {
  109. atomic_or(action, &ctrl->pending_events);
  110. if (!pciehp_poll_mode)
  111. irq_wake_thread(ctrl->pcie->irq, ctrl);
  112. }
  113. void pciehp_queue_pushbutton_work(struct work_struct *work)
  114. {
  115. struct controller *ctrl = container_of(work, struct controller,
  116. button_work.work);
  117. mutex_lock(&ctrl->state_lock);
  118. switch (ctrl->state) {
  119. case BLINKINGOFF_STATE:
  120. pciehp_request(ctrl, DISABLE_SLOT);
  121. break;
  122. case BLINKINGON_STATE:
  123. pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
  124. break;
  125. default:
  126. break;
  127. }
  128. mutex_unlock(&ctrl->state_lock);
  129. }
  130. void pciehp_handle_button_press(struct controller *ctrl)
  131. {
  132. mutex_lock(&ctrl->state_lock);
  133. switch (ctrl->state) {
  134. case OFF_STATE:
  135. case ON_STATE:
  136. if (ctrl->state == ON_STATE) {
  137. ctrl->state = BLINKINGOFF_STATE;
  138. ctrl_info(ctrl, "Slot(%s): Powering off due to button press\n",
  139. slot_name(ctrl));
  140. } else {
  141. ctrl->state = BLINKINGON_STATE;
  142. ctrl_info(ctrl, "Slot(%s) Powering on due to button press\n",
  143. slot_name(ctrl));
  144. }
  145. /* blink green LED and turn off amber */
  146. pciehp_green_led_blink(ctrl);
  147. pciehp_set_attention_status(ctrl, 0);
  148. schedule_delayed_work(&ctrl->button_work, 5 * HZ);
  149. break;
  150. case BLINKINGOFF_STATE:
  151. case BLINKINGON_STATE:
  152. /*
  153. * Cancel if we are still blinking; this means that we
  154. * press the attention again before the 5 sec. limit
  155. * expires to cancel hot-add or hot-remove
  156. */
  157. ctrl_info(ctrl, "Slot(%s): Button cancel\n", slot_name(ctrl));
  158. cancel_delayed_work(&ctrl->button_work);
  159. if (ctrl->state == BLINKINGOFF_STATE) {
  160. ctrl->state = ON_STATE;
  161. pciehp_green_led_on(ctrl);
  162. } else {
  163. ctrl->state = OFF_STATE;
  164. pciehp_green_led_off(ctrl);
  165. }
  166. pciehp_set_attention_status(ctrl, 0);
  167. ctrl_info(ctrl, "Slot(%s): Action canceled due to button press\n",
  168. slot_name(ctrl));
  169. break;
  170. default:
  171. ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
  172. slot_name(ctrl), ctrl->state);
  173. break;
  174. }
  175. mutex_unlock(&ctrl->state_lock);
  176. }
  177. void pciehp_handle_disable_request(struct controller *ctrl)
  178. {
  179. mutex_lock(&ctrl->state_lock);
  180. switch (ctrl->state) {
  181. case BLINKINGON_STATE:
  182. case BLINKINGOFF_STATE:
  183. cancel_delayed_work(&ctrl->button_work);
  184. break;
  185. }
  186. ctrl->state = POWEROFF_STATE;
  187. mutex_unlock(&ctrl->state_lock);
  188. ctrl->request_result = pciehp_disable_slot(ctrl, SAFE_REMOVAL);
  189. }
  190. void pciehp_handle_presence_or_link_change(struct controller *ctrl, u32 events)
  191. {
  192. bool present, link_active;
  193. /*
  194. * If the slot is on and presence or link has changed, turn it off.
  195. * Even if it's occupied again, we cannot assume the card is the same.
  196. */
  197. mutex_lock(&ctrl->state_lock);
  198. switch (ctrl->state) {
  199. case BLINKINGOFF_STATE:
  200. cancel_delayed_work(&ctrl->button_work);
  201. /* fall through */
  202. case ON_STATE:
  203. ctrl->state = POWEROFF_STATE;
  204. mutex_unlock(&ctrl->state_lock);
  205. if (events & PCI_EXP_SLTSTA_DLLSC)
  206. ctrl_info(ctrl, "Slot(%s): Link Down\n",
  207. slot_name(ctrl));
  208. if (events & PCI_EXP_SLTSTA_PDC)
  209. ctrl_info(ctrl, "Slot(%s): Card not present\n",
  210. slot_name(ctrl));
  211. pciehp_disable_slot(ctrl, SURPRISE_REMOVAL);
  212. break;
  213. default:
  214. mutex_unlock(&ctrl->state_lock);
  215. break;
  216. }
  217. /* Turn the slot on if it's occupied or link is up */
  218. mutex_lock(&ctrl->state_lock);
  219. present = pciehp_card_present(ctrl);
  220. link_active = pciehp_check_link_active(ctrl);
  221. if (!present && !link_active) {
  222. mutex_unlock(&ctrl->state_lock);
  223. return;
  224. }
  225. switch (ctrl->state) {
  226. case BLINKINGON_STATE:
  227. cancel_delayed_work(&ctrl->button_work);
  228. /* fall through */
  229. case OFF_STATE:
  230. ctrl->state = POWERON_STATE;
  231. mutex_unlock(&ctrl->state_lock);
  232. if (present)
  233. ctrl_info(ctrl, "Slot(%s): Card present\n",
  234. slot_name(ctrl));
  235. if (link_active)
  236. ctrl_info(ctrl, "Slot(%s): Link Up\n",
  237. slot_name(ctrl));
  238. ctrl->request_result = pciehp_enable_slot(ctrl);
  239. break;
  240. default:
  241. mutex_unlock(&ctrl->state_lock);
  242. break;
  243. }
  244. }
  245. static int __pciehp_enable_slot(struct controller *ctrl)
  246. {
  247. u8 getstatus = 0;
  248. if (MRL_SENS(ctrl)) {
  249. pciehp_get_latch_status(ctrl, &getstatus);
  250. if (getstatus) {
  251. ctrl_info(ctrl, "Slot(%s): Latch open\n",
  252. slot_name(ctrl));
  253. return -ENODEV;
  254. }
  255. }
  256. if (POWER_CTRL(ctrl)) {
  257. pciehp_get_power_status(ctrl, &getstatus);
  258. if (getstatus) {
  259. ctrl_info(ctrl, "Slot(%s): Already enabled\n",
  260. slot_name(ctrl));
  261. return 0;
  262. }
  263. }
  264. return board_added(ctrl);
  265. }
  266. static int pciehp_enable_slot(struct controller *ctrl)
  267. {
  268. int ret;
  269. pm_runtime_get_sync(&ctrl->pcie->port->dev);
  270. ret = __pciehp_enable_slot(ctrl);
  271. if (ret && ATTN_BUTTN(ctrl))
  272. pciehp_green_led_off(ctrl); /* may be blinking */
  273. pm_runtime_put(&ctrl->pcie->port->dev);
  274. mutex_lock(&ctrl->state_lock);
  275. ctrl->state = ret ? OFF_STATE : ON_STATE;
  276. mutex_unlock(&ctrl->state_lock);
  277. return ret;
  278. }
  279. static int __pciehp_disable_slot(struct controller *ctrl, bool safe_removal)
  280. {
  281. u8 getstatus = 0;
  282. if (POWER_CTRL(ctrl)) {
  283. pciehp_get_power_status(ctrl, &getstatus);
  284. if (!getstatus) {
  285. ctrl_info(ctrl, "Slot(%s): Already disabled\n",
  286. slot_name(ctrl));
  287. return -EINVAL;
  288. }
  289. }
  290. remove_board(ctrl, safe_removal);
  291. return 0;
  292. }
  293. static int pciehp_disable_slot(struct controller *ctrl, bool safe_removal)
  294. {
  295. int ret;
  296. pm_runtime_get_sync(&ctrl->pcie->port->dev);
  297. ret = __pciehp_disable_slot(ctrl, safe_removal);
  298. pm_runtime_put(&ctrl->pcie->port->dev);
  299. mutex_lock(&ctrl->state_lock);
  300. ctrl->state = OFF_STATE;
  301. mutex_unlock(&ctrl->state_lock);
  302. return ret;
  303. }
  304. int pciehp_sysfs_enable_slot(struct hotplug_slot *hotplug_slot)
  305. {
  306. struct controller *ctrl = to_ctrl(hotplug_slot);
  307. mutex_lock(&ctrl->state_lock);
  308. switch (ctrl->state) {
  309. case BLINKINGON_STATE:
  310. case OFF_STATE:
  311. mutex_unlock(&ctrl->state_lock);
  312. /*
  313. * The IRQ thread becomes a no-op if the user pulls out the
  314. * card before the thread wakes up, so initialize to -ENODEV.
  315. */
  316. ctrl->request_result = -ENODEV;
  317. pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
  318. wait_event(ctrl->requester,
  319. !atomic_read(&ctrl->pending_events));
  320. return ctrl->request_result;
  321. case POWERON_STATE:
  322. ctrl_info(ctrl, "Slot(%s): Already in powering on state\n",
  323. slot_name(ctrl));
  324. break;
  325. case BLINKINGOFF_STATE:
  326. case ON_STATE:
  327. case POWEROFF_STATE:
  328. ctrl_info(ctrl, "Slot(%s): Already enabled\n",
  329. slot_name(ctrl));
  330. break;
  331. default:
  332. ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
  333. slot_name(ctrl), ctrl->state);
  334. break;
  335. }
  336. mutex_unlock(&ctrl->state_lock);
  337. return -ENODEV;
  338. }
  339. int pciehp_sysfs_disable_slot(struct hotplug_slot *hotplug_slot)
  340. {
  341. struct controller *ctrl = to_ctrl(hotplug_slot);
  342. mutex_lock(&ctrl->state_lock);
  343. switch (ctrl->state) {
  344. case BLINKINGOFF_STATE:
  345. case ON_STATE:
  346. mutex_unlock(&ctrl->state_lock);
  347. pciehp_request(ctrl, DISABLE_SLOT);
  348. wait_event(ctrl->requester,
  349. !atomic_read(&ctrl->pending_events));
  350. return ctrl->request_result;
  351. case POWEROFF_STATE:
  352. ctrl_info(ctrl, "Slot(%s): Already in powering off state\n",
  353. slot_name(ctrl));
  354. break;
  355. case BLINKINGON_STATE:
  356. case OFF_STATE:
  357. case POWERON_STATE:
  358. ctrl_info(ctrl, "Slot(%s): Already disabled\n",
  359. slot_name(ctrl));
  360. break;
  361. default:
  362. ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
  363. slot_name(ctrl), ctrl->state);
  364. break;
  365. }
  366. mutex_unlock(&ctrl->state_lock);
  367. return -ENODEV;
  368. }