pciehp_ctrl.c 13 KB

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