pciehp_ctrl.c 13 KB

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