pciehp_ctrl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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/module.h>
  16. #include <linux/kernel.h>
  17. #include <linux/types.h>
  18. #include <linux/slab.h>
  19. #include <linux/pci.h>
  20. #include "../pci.h"
  21. #include "pciehp.h"
  22. static void interrupt_event_handler(struct work_struct *work);
  23. void pciehp_queue_interrupt_event(struct slot *p_slot, u32 event_type)
  24. {
  25. struct event_info *info;
  26. info = kmalloc(sizeof(*info), GFP_ATOMIC);
  27. if (!info) {
  28. ctrl_err(p_slot->ctrl, "dropped event %d (ENOMEM)\n", event_type);
  29. return;
  30. }
  31. INIT_WORK(&info->work, interrupt_event_handler);
  32. info->event_type = event_type;
  33. info->p_slot = p_slot;
  34. queue_work(p_slot->wq, &info->work);
  35. }
  36. /* The following routines constitute the bulk of the
  37. hotplug controller logic
  38. */
  39. static void set_slot_off(struct controller *ctrl, struct slot *pslot)
  40. {
  41. /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
  42. if (POWER_CTRL(ctrl)) {
  43. pciehp_power_off_slot(pslot);
  44. /*
  45. * After turning power off, we must wait for at least 1 second
  46. * before taking any action that relies on power having been
  47. * removed from the slot/adapter.
  48. */
  49. msleep(1000);
  50. }
  51. pciehp_green_led_off(pslot);
  52. pciehp_set_attention_status(pslot, 1);
  53. }
  54. /**
  55. * board_added - Called after a board has been added to the system.
  56. * @p_slot: &slot where board is added
  57. *
  58. * Turns power on for the board.
  59. * Configures board.
  60. */
  61. static int board_added(struct slot *p_slot)
  62. {
  63. int retval = 0;
  64. struct controller *ctrl = p_slot->ctrl;
  65. struct pci_bus *parent = ctrl->pcie->port->subordinate;
  66. if (POWER_CTRL(ctrl)) {
  67. /* Power on slot */
  68. retval = pciehp_power_on_slot(p_slot);
  69. if (retval)
  70. return retval;
  71. }
  72. pciehp_green_led_blink(p_slot);
  73. /* Check link training status */
  74. retval = pciehp_check_link_status(ctrl);
  75. if (retval) {
  76. ctrl_err(ctrl, "Failed to check link status\n");
  77. goto err_exit;
  78. }
  79. /* Check for a power fault */
  80. if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
  81. ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(p_slot));
  82. retval = -EIO;
  83. goto err_exit;
  84. }
  85. retval = pciehp_configure_device(p_slot);
  86. if (retval) {
  87. if (retval != -EEXIST) {
  88. ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
  89. pci_domain_nr(parent), parent->number);
  90. goto err_exit;
  91. }
  92. }
  93. pciehp_green_led_on(p_slot);
  94. pciehp_set_attention_status(p_slot, 0);
  95. return 0;
  96. err_exit:
  97. set_slot_off(ctrl, p_slot);
  98. return retval;
  99. }
  100. /**
  101. * remove_board - Turns off slot and LEDs
  102. * @p_slot: slot where board is being removed
  103. */
  104. static int remove_board(struct slot *p_slot)
  105. {
  106. int retval;
  107. struct controller *ctrl = p_slot->ctrl;
  108. retval = pciehp_unconfigure_device(p_slot);
  109. if (retval)
  110. return retval;
  111. if (POWER_CTRL(ctrl)) {
  112. pciehp_power_off_slot(p_slot);
  113. /*
  114. * After turning power off, we must wait for at least 1 second
  115. * before taking any action that relies on power having been
  116. * removed from the slot/adapter.
  117. */
  118. msleep(1000);
  119. }
  120. /* turn off Green LED */
  121. pciehp_green_led_off(p_slot);
  122. return 0;
  123. }
  124. struct power_work_info {
  125. struct slot *p_slot;
  126. struct work_struct work;
  127. unsigned int req;
  128. #define DISABLE_REQ 0
  129. #define ENABLE_REQ 1
  130. };
  131. /**
  132. * pciehp_power_thread - handle pushbutton events
  133. * @work: &struct work_struct describing work to be done
  134. *
  135. * Scheduled procedure to handle blocking stuff for the pushbuttons.
  136. * Handles all pending events and exits.
  137. */
  138. static void pciehp_power_thread(struct work_struct *work)
  139. {
  140. struct power_work_info *info =
  141. container_of(work, struct power_work_info, work);
  142. struct slot *p_slot = info->p_slot;
  143. int ret;
  144. switch (info->req) {
  145. case DISABLE_REQ:
  146. mutex_lock(&p_slot->hotplug_lock);
  147. pciehp_disable_slot(p_slot);
  148. mutex_unlock(&p_slot->hotplug_lock);
  149. mutex_lock(&p_slot->lock);
  150. p_slot->state = STATIC_STATE;
  151. mutex_unlock(&p_slot->lock);
  152. break;
  153. case ENABLE_REQ:
  154. mutex_lock(&p_slot->hotplug_lock);
  155. ret = pciehp_enable_slot(p_slot);
  156. mutex_unlock(&p_slot->hotplug_lock);
  157. if (ret)
  158. pciehp_green_led_off(p_slot);
  159. mutex_lock(&p_slot->lock);
  160. p_slot->state = STATIC_STATE;
  161. mutex_unlock(&p_slot->lock);
  162. break;
  163. default:
  164. break;
  165. }
  166. kfree(info);
  167. }
  168. static void pciehp_queue_power_work(struct slot *p_slot, int req)
  169. {
  170. struct power_work_info *info;
  171. p_slot->state = (req == ENABLE_REQ) ? POWERON_STATE : POWEROFF_STATE;
  172. info = kmalloc(sizeof(*info), GFP_KERNEL);
  173. if (!info) {
  174. ctrl_err(p_slot->ctrl, "no memory to queue %s request\n",
  175. (req == ENABLE_REQ) ? "poweron" : "poweroff");
  176. return;
  177. }
  178. info->p_slot = p_slot;
  179. INIT_WORK(&info->work, pciehp_power_thread);
  180. info->req = req;
  181. queue_work(p_slot->wq, &info->work);
  182. }
  183. void pciehp_queue_pushbutton_work(struct work_struct *work)
  184. {
  185. struct slot *p_slot = container_of(work, struct slot, work.work);
  186. mutex_lock(&p_slot->lock);
  187. switch (p_slot->state) {
  188. case BLINKINGOFF_STATE:
  189. pciehp_queue_power_work(p_slot, DISABLE_REQ);
  190. break;
  191. case BLINKINGON_STATE:
  192. pciehp_queue_power_work(p_slot, ENABLE_REQ);
  193. break;
  194. default:
  195. break;
  196. }
  197. mutex_unlock(&p_slot->lock);
  198. }
  199. /*
  200. * Note: This function must be called with slot->lock held
  201. */
  202. static void handle_button_press_event(struct slot *p_slot)
  203. {
  204. struct controller *ctrl = p_slot->ctrl;
  205. u8 getstatus;
  206. switch (p_slot->state) {
  207. case STATIC_STATE:
  208. pciehp_get_power_status(p_slot, &getstatus);
  209. if (getstatus) {
  210. p_slot->state = BLINKINGOFF_STATE;
  211. ctrl_info(ctrl, "Slot(%s): Powering off due to button press\n",
  212. slot_name(p_slot));
  213. } else {
  214. p_slot->state = BLINKINGON_STATE;
  215. ctrl_info(ctrl, "Slot(%s) Powering on due to button press\n",
  216. slot_name(p_slot));
  217. }
  218. /* blink green LED and turn off amber */
  219. pciehp_green_led_blink(p_slot);
  220. pciehp_set_attention_status(p_slot, 0);
  221. queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
  222. break;
  223. case BLINKINGOFF_STATE:
  224. case BLINKINGON_STATE:
  225. /*
  226. * Cancel if we are still blinking; this means that we
  227. * press the attention again before the 5 sec. limit
  228. * expires to cancel hot-add or hot-remove
  229. */
  230. ctrl_info(ctrl, "Slot(%s): Button cancel\n", slot_name(p_slot));
  231. cancel_delayed_work(&p_slot->work);
  232. if (p_slot->state == BLINKINGOFF_STATE)
  233. pciehp_green_led_on(p_slot);
  234. else
  235. pciehp_green_led_off(p_slot);
  236. pciehp_set_attention_status(p_slot, 0);
  237. ctrl_info(ctrl, "Slot(%s): Action canceled due to button press\n",
  238. slot_name(p_slot));
  239. p_slot->state = STATIC_STATE;
  240. break;
  241. case POWEROFF_STATE:
  242. case POWERON_STATE:
  243. /*
  244. * Ignore if the slot is on power-on or power-off state;
  245. * this means that the previous attention button action
  246. * to hot-add or hot-remove is undergoing
  247. */
  248. ctrl_info(ctrl, "Slot(%s): Button ignored\n",
  249. slot_name(p_slot));
  250. break;
  251. default:
  252. ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
  253. slot_name(p_slot), p_slot->state);
  254. break;
  255. }
  256. }
  257. /*
  258. * Note: This function must be called with slot->lock held
  259. */
  260. static void handle_link_event(struct slot *p_slot, u32 event)
  261. {
  262. struct controller *ctrl = p_slot->ctrl;
  263. switch (p_slot->state) {
  264. case BLINKINGON_STATE:
  265. case BLINKINGOFF_STATE:
  266. cancel_delayed_work(&p_slot->work);
  267. /* Fall through */
  268. case STATIC_STATE:
  269. pciehp_queue_power_work(p_slot, event == INT_LINK_UP ?
  270. ENABLE_REQ : DISABLE_REQ);
  271. break;
  272. case POWERON_STATE:
  273. if (event == INT_LINK_UP) {
  274. ctrl_info(ctrl, "Slot(%s): Link Up event ignored; already powering on\n",
  275. slot_name(p_slot));
  276. } else {
  277. ctrl_info(ctrl, "Slot(%s): Link Down event queued; currently getting powered on\n",
  278. slot_name(p_slot));
  279. pciehp_queue_power_work(p_slot, DISABLE_REQ);
  280. }
  281. break;
  282. case POWEROFF_STATE:
  283. if (event == INT_LINK_UP) {
  284. ctrl_info(ctrl, "Slot(%s): Link Up event queued; currently getting powered off\n",
  285. slot_name(p_slot));
  286. pciehp_queue_power_work(p_slot, ENABLE_REQ);
  287. } else {
  288. ctrl_info(ctrl, "Slot(%s): Link Down event ignored; already powering off\n",
  289. slot_name(p_slot));
  290. }
  291. break;
  292. default:
  293. ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
  294. slot_name(p_slot), p_slot->state);
  295. break;
  296. }
  297. }
  298. static void interrupt_event_handler(struct work_struct *work)
  299. {
  300. struct event_info *info = container_of(work, struct event_info, work);
  301. struct slot *p_slot = info->p_slot;
  302. struct controller *ctrl = p_slot->ctrl;
  303. mutex_lock(&p_slot->lock);
  304. switch (info->event_type) {
  305. case INT_BUTTON_PRESS:
  306. handle_button_press_event(p_slot);
  307. break;
  308. case INT_POWER_FAULT:
  309. if (!POWER_CTRL(ctrl))
  310. break;
  311. pciehp_set_attention_status(p_slot, 1);
  312. pciehp_green_led_off(p_slot);
  313. break;
  314. case INT_PRESENCE_ON:
  315. pciehp_queue_power_work(p_slot, ENABLE_REQ);
  316. break;
  317. case INT_PRESENCE_OFF:
  318. /*
  319. * Regardless of surprise capability, we need to
  320. * definitely remove a card that has been pulled out!
  321. */
  322. pciehp_queue_power_work(p_slot, DISABLE_REQ);
  323. break;
  324. case INT_LINK_UP:
  325. case INT_LINK_DOWN:
  326. handle_link_event(p_slot, info->event_type);
  327. break;
  328. default:
  329. break;
  330. }
  331. mutex_unlock(&p_slot->lock);
  332. kfree(info);
  333. }
  334. /*
  335. * Note: This function must be called with slot->hotplug_lock held
  336. */
  337. int pciehp_enable_slot(struct slot *p_slot)
  338. {
  339. u8 getstatus = 0;
  340. struct controller *ctrl = p_slot->ctrl;
  341. pciehp_get_adapter_status(p_slot, &getstatus);
  342. if (!getstatus) {
  343. ctrl_info(ctrl, "Slot(%s): No adapter\n", slot_name(p_slot));
  344. return -ENODEV;
  345. }
  346. if (MRL_SENS(p_slot->ctrl)) {
  347. pciehp_get_latch_status(p_slot, &getstatus);
  348. if (getstatus) {
  349. ctrl_info(ctrl, "Slot(%s): Latch open\n",
  350. slot_name(p_slot));
  351. return -ENODEV;
  352. }
  353. }
  354. if (POWER_CTRL(p_slot->ctrl)) {
  355. pciehp_get_power_status(p_slot, &getstatus);
  356. if (getstatus) {
  357. ctrl_info(ctrl, "Slot(%s): Already enabled\n",
  358. slot_name(p_slot));
  359. return 0;
  360. }
  361. }
  362. return board_added(p_slot);
  363. }
  364. /*
  365. * Note: This function must be called with slot->hotplug_lock held
  366. */
  367. int pciehp_disable_slot(struct slot *p_slot)
  368. {
  369. u8 getstatus = 0;
  370. struct controller *ctrl = p_slot->ctrl;
  371. if (!p_slot->ctrl)
  372. return 1;
  373. if (POWER_CTRL(p_slot->ctrl)) {
  374. pciehp_get_power_status(p_slot, &getstatus);
  375. if (!getstatus) {
  376. ctrl_info(ctrl, "Slot(%s): Already disabled\n",
  377. slot_name(p_slot));
  378. return -EINVAL;
  379. }
  380. }
  381. return remove_board(p_slot);
  382. }
  383. int pciehp_sysfs_enable_slot(struct slot *p_slot)
  384. {
  385. int retval = -ENODEV;
  386. struct controller *ctrl = p_slot->ctrl;
  387. mutex_lock(&p_slot->lock);
  388. switch (p_slot->state) {
  389. case BLINKINGON_STATE:
  390. cancel_delayed_work(&p_slot->work);
  391. case STATIC_STATE:
  392. p_slot->state = POWERON_STATE;
  393. mutex_unlock(&p_slot->lock);
  394. mutex_lock(&p_slot->hotplug_lock);
  395. retval = pciehp_enable_slot(p_slot);
  396. mutex_unlock(&p_slot->hotplug_lock);
  397. mutex_lock(&p_slot->lock);
  398. p_slot->state = STATIC_STATE;
  399. break;
  400. case POWERON_STATE:
  401. ctrl_info(ctrl, "Slot(%s): Already in powering on state\n",
  402. slot_name(p_slot));
  403. break;
  404. case BLINKINGOFF_STATE:
  405. case POWEROFF_STATE:
  406. ctrl_info(ctrl, "Slot(%s): Already enabled\n",
  407. slot_name(p_slot));
  408. break;
  409. default:
  410. ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
  411. slot_name(p_slot), p_slot->state);
  412. break;
  413. }
  414. mutex_unlock(&p_slot->lock);
  415. return retval;
  416. }
  417. int pciehp_sysfs_disable_slot(struct slot *p_slot)
  418. {
  419. int retval = -ENODEV;
  420. struct controller *ctrl = p_slot->ctrl;
  421. mutex_lock(&p_slot->lock);
  422. switch (p_slot->state) {
  423. case BLINKINGOFF_STATE:
  424. cancel_delayed_work(&p_slot->work);
  425. case STATIC_STATE:
  426. p_slot->state = POWEROFF_STATE;
  427. mutex_unlock(&p_slot->lock);
  428. mutex_lock(&p_slot->hotplug_lock);
  429. retval = pciehp_disable_slot(p_slot);
  430. mutex_unlock(&p_slot->hotplug_lock);
  431. mutex_lock(&p_slot->lock);
  432. p_slot->state = STATIC_STATE;
  433. break;
  434. case POWEROFF_STATE:
  435. ctrl_info(ctrl, "Slot(%s): Already in powering off state\n",
  436. slot_name(p_slot));
  437. break;
  438. case BLINKINGON_STATE:
  439. case POWERON_STATE:
  440. ctrl_info(ctrl, "Slot(%s): Already disabled\n",
  441. slot_name(p_slot));
  442. break;
  443. default:
  444. ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
  445. slot_name(p_slot), p_slot->state);
  446. break;
  447. }
  448. mutex_unlock(&p_slot->lock);
  449. return retval;
  450. }