pciehp_ctrl.c 11 KB

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