pciehp_ctrl.c 12 KB

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