pciehp_ctrl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. #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): Button press: will power off in 5 sec\n",
  145. slot_name(ctrl));
  146. } else {
  147. ctrl->state = BLINKINGON_STATE;
  148. ctrl_info(ctrl, "Slot(%s): Button press: will power on in 5 sec\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. cancel_delayed_work(&ctrl->button_work);
  164. if (ctrl->state == BLINKINGOFF_STATE) {
  165. ctrl->state = ON_STATE;
  166. pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON,
  167. PCI_EXP_SLTCTL_ATTN_IND_OFF);
  168. ctrl_info(ctrl, "Slot(%s): Button press: canceling request to power off\n",
  169. slot_name(ctrl));
  170. } else {
  171. ctrl->state = OFF_STATE;
  172. pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
  173. PCI_EXP_SLTCTL_ATTN_IND_OFF);
  174. ctrl_info(ctrl, "Slot(%s): Button press: canceling request to power on\n",
  175. slot_name(ctrl));
  176. }
  177. break;
  178. default:
  179. ctrl_err(ctrl, "Slot(%s): Button press: ignoring invalid state %#x\n",
  180. slot_name(ctrl), ctrl->state);
  181. break;
  182. }
  183. mutex_unlock(&ctrl->state_lock);
  184. }
  185. void pciehp_handle_disable_request(struct controller *ctrl)
  186. {
  187. mutex_lock(&ctrl->state_lock);
  188. switch (ctrl->state) {
  189. case BLINKINGON_STATE:
  190. case BLINKINGOFF_STATE:
  191. cancel_delayed_work(&ctrl->button_work);
  192. break;
  193. }
  194. ctrl->state = POWEROFF_STATE;
  195. mutex_unlock(&ctrl->state_lock);
  196. ctrl->request_result = pciehp_disable_slot(ctrl, SAFE_REMOVAL);
  197. }
  198. void pciehp_handle_presence_or_link_change(struct controller *ctrl, u32 events)
  199. {
  200. int present, link_active;
  201. /*
  202. * If the slot is on and presence or link has changed, turn it off.
  203. * Even if it's occupied again, we cannot assume the card is the same.
  204. */
  205. mutex_lock(&ctrl->state_lock);
  206. switch (ctrl->state) {
  207. case BLINKINGOFF_STATE:
  208. cancel_delayed_work(&ctrl->button_work);
  209. fallthrough;
  210. case ON_STATE:
  211. ctrl->state = POWEROFF_STATE;
  212. mutex_unlock(&ctrl->state_lock);
  213. if (events & PCI_EXP_SLTSTA_DLLSC)
  214. ctrl_info(ctrl, "Slot(%s): Link Down\n",
  215. slot_name(ctrl));
  216. if (events & PCI_EXP_SLTSTA_PDC)
  217. ctrl_info(ctrl, "Slot(%s): Card not present\n",
  218. slot_name(ctrl));
  219. pciehp_disable_slot(ctrl, SURPRISE_REMOVAL);
  220. break;
  221. default:
  222. mutex_unlock(&ctrl->state_lock);
  223. break;
  224. }
  225. /* Turn the slot on if it's occupied or link is up */
  226. mutex_lock(&ctrl->state_lock);
  227. present = pciehp_card_present(ctrl);
  228. link_active = pciehp_check_link_active(ctrl);
  229. if (present <= 0 && link_active <= 0) {
  230. if (ctrl->state == BLINKINGON_STATE) {
  231. ctrl->state = OFF_STATE;
  232. cancel_delayed_work(&ctrl->button_work);
  233. pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
  234. INDICATOR_NOOP);
  235. ctrl_info(ctrl, "Slot(%s): Card not present\n",
  236. slot_name(ctrl));
  237. }
  238. mutex_unlock(&ctrl->state_lock);
  239. return;
  240. }
  241. switch (ctrl->state) {
  242. case BLINKINGON_STATE:
  243. cancel_delayed_work(&ctrl->button_work);
  244. fallthrough;
  245. case OFF_STATE:
  246. ctrl->state = POWERON_STATE;
  247. mutex_unlock(&ctrl->state_lock);
  248. if (present)
  249. ctrl_info(ctrl, "Slot(%s): Card present\n",
  250. slot_name(ctrl));
  251. if (link_active)
  252. ctrl_info(ctrl, "Slot(%s): Link Up\n",
  253. slot_name(ctrl));
  254. ctrl->request_result = pciehp_enable_slot(ctrl);
  255. break;
  256. default:
  257. mutex_unlock(&ctrl->state_lock);
  258. break;
  259. }
  260. }
  261. static int __pciehp_enable_slot(struct controller *ctrl)
  262. {
  263. u8 getstatus = 0;
  264. if (MRL_SENS(ctrl)) {
  265. pciehp_get_latch_status(ctrl, &getstatus);
  266. if (getstatus) {
  267. ctrl_info(ctrl, "Slot(%s): Latch open\n",
  268. slot_name(ctrl));
  269. return -ENODEV;
  270. }
  271. }
  272. if (POWER_CTRL(ctrl)) {
  273. pciehp_get_power_status(ctrl, &getstatus);
  274. if (getstatus) {
  275. ctrl_info(ctrl, "Slot(%s): Already enabled\n",
  276. slot_name(ctrl));
  277. return 0;
  278. }
  279. }
  280. return board_added(ctrl);
  281. }
  282. static int pciehp_enable_slot(struct controller *ctrl)
  283. {
  284. int ret;
  285. pm_runtime_get_sync(&ctrl->pcie->port->dev);
  286. ret = __pciehp_enable_slot(ctrl);
  287. if (ret && ATTN_BUTTN(ctrl))
  288. /* may be blinking */
  289. pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
  290. INDICATOR_NOOP);
  291. pm_runtime_put(&ctrl->pcie->port->dev);
  292. mutex_lock(&ctrl->state_lock);
  293. ctrl->state = ret ? OFF_STATE : ON_STATE;
  294. mutex_unlock(&ctrl->state_lock);
  295. return ret;
  296. }
  297. static int __pciehp_disable_slot(struct controller *ctrl, bool safe_removal)
  298. {
  299. u8 getstatus = 0;
  300. if (POWER_CTRL(ctrl)) {
  301. pciehp_get_power_status(ctrl, &getstatus);
  302. if (!getstatus) {
  303. ctrl_info(ctrl, "Slot(%s): Already disabled\n",
  304. slot_name(ctrl));
  305. return -EINVAL;
  306. }
  307. }
  308. remove_board(ctrl, safe_removal);
  309. return 0;
  310. }
  311. static int pciehp_disable_slot(struct controller *ctrl, bool safe_removal)
  312. {
  313. int ret;
  314. pm_runtime_get_sync(&ctrl->pcie->port->dev);
  315. ret = __pciehp_disable_slot(ctrl, safe_removal);
  316. pm_runtime_put(&ctrl->pcie->port->dev);
  317. mutex_lock(&ctrl->state_lock);
  318. ctrl->state = OFF_STATE;
  319. mutex_unlock(&ctrl->state_lock);
  320. return ret;
  321. }
  322. int pciehp_sysfs_enable_slot(struct hotplug_slot *hotplug_slot)
  323. {
  324. struct controller *ctrl = to_ctrl(hotplug_slot);
  325. mutex_lock(&ctrl->state_lock);
  326. switch (ctrl->state) {
  327. case BLINKINGON_STATE:
  328. case OFF_STATE:
  329. mutex_unlock(&ctrl->state_lock);
  330. /*
  331. * The IRQ thread becomes a no-op if the user pulls out the
  332. * card before the thread wakes up, so initialize to -ENODEV.
  333. */
  334. ctrl->request_result = -ENODEV;
  335. pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
  336. wait_event(ctrl->requester,
  337. !atomic_read(&ctrl->pending_events) &&
  338. !ctrl->ist_running);
  339. return ctrl->request_result;
  340. case POWERON_STATE:
  341. ctrl_info(ctrl, "Slot(%s): Already in powering on state\n",
  342. slot_name(ctrl));
  343. break;
  344. case BLINKINGOFF_STATE:
  345. case ON_STATE:
  346. case POWEROFF_STATE:
  347. ctrl_info(ctrl, "Slot(%s): Already enabled\n",
  348. slot_name(ctrl));
  349. break;
  350. default:
  351. ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
  352. slot_name(ctrl), ctrl->state);
  353. break;
  354. }
  355. mutex_unlock(&ctrl->state_lock);
  356. return -ENODEV;
  357. }
  358. int pciehp_sysfs_disable_slot(struct hotplug_slot *hotplug_slot)
  359. {
  360. struct controller *ctrl = to_ctrl(hotplug_slot);
  361. mutex_lock(&ctrl->state_lock);
  362. switch (ctrl->state) {
  363. case BLINKINGOFF_STATE:
  364. case ON_STATE:
  365. mutex_unlock(&ctrl->state_lock);
  366. pciehp_request(ctrl, DISABLE_SLOT);
  367. wait_event(ctrl->requester,
  368. !atomic_read(&ctrl->pending_events) &&
  369. !ctrl->ist_running);
  370. return ctrl->request_result;
  371. case POWEROFF_STATE:
  372. ctrl_info(ctrl, "Slot(%s): Already in powering off state\n",
  373. slot_name(ctrl));
  374. break;
  375. case BLINKINGON_STATE:
  376. case OFF_STATE:
  377. case POWERON_STATE:
  378. ctrl_info(ctrl, "Slot(%s): Already disabled\n",
  379. slot_name(ctrl));
  380. break;
  381. default:
  382. ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
  383. slot_name(ctrl), ctrl->state);
  384. break;
  385. }
  386. mutex_unlock(&ctrl->state_lock);
  387. return -ENODEV;
  388. }