pciehp_ctrl.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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, "Power fault on slot %s\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. return 0;
  108. err_exit:
  109. set_slot_off(ctrl, p_slot);
  110. return retval;
  111. }
  112. /**
  113. * remove_board - Turns off slot and LEDs
  114. * @p_slot: slot where board is being removed
  115. */
  116. static int remove_board(struct slot *p_slot)
  117. {
  118. int retval;
  119. struct controller *ctrl = p_slot->ctrl;
  120. retval = pciehp_unconfigure_device(p_slot);
  121. if (retval)
  122. return retval;
  123. if (POWER_CTRL(ctrl)) {
  124. pciehp_power_off_slot(p_slot);
  125. /*
  126. * After turning power off, we must wait for at least 1 second
  127. * before taking any action that relies on power having been
  128. * removed from the slot/adapter.
  129. */
  130. msleep(1000);
  131. }
  132. /* turn off Green LED */
  133. pciehp_green_led_off(p_slot);
  134. return 0;
  135. }
  136. struct power_work_info {
  137. struct slot *p_slot;
  138. struct work_struct work;
  139. unsigned int req;
  140. #define DISABLE_REQ 0
  141. #define ENABLE_REQ 1
  142. };
  143. /**
  144. * pciehp_power_thread - handle pushbutton events
  145. * @work: &struct work_struct describing work to be done
  146. *
  147. * Scheduled procedure to handle blocking stuff for the pushbuttons.
  148. * Handles all pending events and exits.
  149. */
  150. static void pciehp_power_thread(struct work_struct *work)
  151. {
  152. struct power_work_info *info =
  153. container_of(work, struct power_work_info, work);
  154. struct slot *p_slot = info->p_slot;
  155. int ret;
  156. switch (info->req) {
  157. case DISABLE_REQ:
  158. mutex_lock(&p_slot->hotplug_lock);
  159. pciehp_disable_slot(p_slot);
  160. mutex_unlock(&p_slot->hotplug_lock);
  161. mutex_lock(&p_slot->lock);
  162. p_slot->state = STATIC_STATE;
  163. mutex_unlock(&p_slot->lock);
  164. break;
  165. case ENABLE_REQ:
  166. mutex_lock(&p_slot->hotplug_lock);
  167. ret = pciehp_enable_slot(p_slot);
  168. mutex_unlock(&p_slot->hotplug_lock);
  169. if (ret)
  170. pciehp_green_led_off(p_slot);
  171. mutex_lock(&p_slot->lock);
  172. p_slot->state = STATIC_STATE;
  173. mutex_unlock(&p_slot->lock);
  174. break;
  175. default:
  176. break;
  177. }
  178. kfree(info);
  179. }
  180. static void pciehp_queue_power_work(struct slot *p_slot, int req)
  181. {
  182. struct power_work_info *info;
  183. p_slot->state = (req == ENABLE_REQ) ? POWERON_STATE : POWEROFF_STATE;
  184. info = kmalloc(sizeof(*info), GFP_KERNEL);
  185. if (!info) {
  186. ctrl_err(p_slot->ctrl, "no memory to queue %s request\n",
  187. (req == ENABLE_REQ) ? "poweron" : "poweroff");
  188. return;
  189. }
  190. info->p_slot = p_slot;
  191. INIT_WORK(&info->work, pciehp_power_thread);
  192. info->req = req;
  193. queue_work(p_slot->wq, &info->work);
  194. }
  195. void pciehp_queue_pushbutton_work(struct work_struct *work)
  196. {
  197. struct slot *p_slot = container_of(work, struct slot, work.work);
  198. mutex_lock(&p_slot->lock);
  199. switch (p_slot->state) {
  200. case BLINKINGOFF_STATE:
  201. pciehp_queue_power_work(p_slot, DISABLE_REQ);
  202. break;
  203. case BLINKINGON_STATE:
  204. pciehp_queue_power_work(p_slot, ENABLE_REQ);
  205. break;
  206. default:
  207. break;
  208. }
  209. mutex_unlock(&p_slot->lock);
  210. }
  211. /*
  212. * Note: This function must be called with slot->lock held
  213. */
  214. static void handle_button_press_event(struct slot *p_slot)
  215. {
  216. struct controller *ctrl = p_slot->ctrl;
  217. u8 getstatus;
  218. switch (p_slot->state) {
  219. case STATIC_STATE:
  220. pciehp_get_power_status(p_slot, &getstatus);
  221. if (getstatus) {
  222. p_slot->state = BLINKINGOFF_STATE;
  223. ctrl_info(ctrl, "PCI slot #%s - powering off due to button press\n",
  224. slot_name(p_slot));
  225. } else {
  226. p_slot->state = BLINKINGON_STATE;
  227. ctrl_info(ctrl, "PCI slot #%s - powering on due to button press\n",
  228. slot_name(p_slot));
  229. }
  230. /* blink green LED and turn off amber */
  231. pciehp_green_led_blink(p_slot);
  232. pciehp_set_attention_status(p_slot, 0);
  233. queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
  234. break;
  235. case BLINKINGOFF_STATE:
  236. case BLINKINGON_STATE:
  237. /*
  238. * Cancel if we are still blinking; this means that we
  239. * press the attention again before the 5 sec. limit
  240. * expires to cancel hot-add or hot-remove
  241. */
  242. ctrl_info(ctrl, "Button cancel on Slot(%s)\n", slot_name(p_slot));
  243. cancel_delayed_work(&p_slot->work);
  244. if (p_slot->state == BLINKINGOFF_STATE)
  245. pciehp_green_led_on(p_slot);
  246. else
  247. pciehp_green_led_off(p_slot);
  248. pciehp_set_attention_status(p_slot, 0);
  249. ctrl_info(ctrl, "PCI slot #%s - action canceled due to button press\n",
  250. slot_name(p_slot));
  251. p_slot->state = STATIC_STATE;
  252. break;
  253. case POWEROFF_STATE:
  254. case POWERON_STATE:
  255. /*
  256. * Ignore if the slot is on power-on or power-off state;
  257. * this means that the previous attention button action
  258. * to hot-add or hot-remove is undergoing
  259. */
  260. ctrl_info(ctrl, "Button ignore on Slot(%s)\n", slot_name(p_slot));
  261. break;
  262. default:
  263. ctrl_warn(ctrl, "ignoring invalid state %#x\n", p_slot->state);
  264. break;
  265. }
  266. }
  267. /*
  268. * Note: This function must be called with slot->lock held
  269. */
  270. static void handle_surprise_event(struct slot *p_slot)
  271. {
  272. u8 getstatus;
  273. pciehp_get_adapter_status(p_slot, &getstatus);
  274. if (!getstatus)
  275. pciehp_queue_power_work(p_slot, DISABLE_REQ);
  276. else
  277. pciehp_queue_power_work(p_slot, ENABLE_REQ);
  278. }
  279. /*
  280. * Note: This function must be called with slot->lock held
  281. */
  282. static void handle_link_event(struct slot *p_slot, u32 event)
  283. {
  284. struct controller *ctrl = p_slot->ctrl;
  285. switch (p_slot->state) {
  286. case BLINKINGON_STATE:
  287. case BLINKINGOFF_STATE:
  288. cancel_delayed_work(&p_slot->work);
  289. /* Fall through */
  290. case STATIC_STATE:
  291. pciehp_queue_power_work(p_slot, event == INT_LINK_UP ?
  292. ENABLE_REQ : DISABLE_REQ);
  293. break;
  294. case POWERON_STATE:
  295. if (event == INT_LINK_UP) {
  296. ctrl_info(ctrl,
  297. "Link Up event ignored on slot(%s): already powering on\n",
  298. slot_name(p_slot));
  299. } else {
  300. ctrl_info(ctrl,
  301. "Link Down event queued on slot(%s): currently getting powered on\n",
  302. slot_name(p_slot));
  303. pciehp_queue_power_work(p_slot, DISABLE_REQ);
  304. }
  305. break;
  306. case POWEROFF_STATE:
  307. if (event == INT_LINK_UP) {
  308. ctrl_info(ctrl,
  309. "Link Up event queued on slot(%s): currently getting powered off\n",
  310. slot_name(p_slot));
  311. pciehp_queue_power_work(p_slot, ENABLE_REQ);
  312. } else {
  313. ctrl_info(ctrl,
  314. "Link Down event ignored on slot(%s): already powering off\n",
  315. slot_name(p_slot));
  316. }
  317. break;
  318. default:
  319. ctrl_err(ctrl, "ignoring invalid state %#x on slot(%s)\n",
  320. p_slot->state, slot_name(p_slot));
  321. break;
  322. }
  323. }
  324. static void interrupt_event_handler(struct work_struct *work)
  325. {
  326. struct event_info *info = container_of(work, struct event_info, work);
  327. struct slot *p_slot = info->p_slot;
  328. struct controller *ctrl = p_slot->ctrl;
  329. mutex_lock(&p_slot->lock);
  330. switch (info->event_type) {
  331. case INT_BUTTON_PRESS:
  332. handle_button_press_event(p_slot);
  333. break;
  334. case INT_POWER_FAULT:
  335. if (!POWER_CTRL(ctrl))
  336. break;
  337. pciehp_set_attention_status(p_slot, 1);
  338. pciehp_green_led_off(p_slot);
  339. break;
  340. case INT_PRESENCE_ON:
  341. handle_surprise_event(p_slot);
  342. break;
  343. case INT_PRESENCE_OFF:
  344. /*
  345. * Regardless of surprise capability, we need to
  346. * definitely remove a card that has been pulled out!
  347. */
  348. handle_surprise_event(p_slot);
  349. break;
  350. case INT_LINK_UP:
  351. case INT_LINK_DOWN:
  352. handle_link_event(p_slot, info->event_type);
  353. break;
  354. default:
  355. break;
  356. }
  357. mutex_unlock(&p_slot->lock);
  358. kfree(info);
  359. }
  360. /*
  361. * Note: This function must be called with slot->hotplug_lock held
  362. */
  363. int pciehp_enable_slot(struct slot *p_slot)
  364. {
  365. u8 getstatus = 0;
  366. int rc;
  367. struct controller *ctrl = p_slot->ctrl;
  368. pciehp_get_adapter_status(p_slot, &getstatus);
  369. if (!getstatus) {
  370. ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
  371. return -ENODEV;
  372. }
  373. if (MRL_SENS(p_slot->ctrl)) {
  374. pciehp_get_latch_status(p_slot, &getstatus);
  375. if (getstatus) {
  376. ctrl_info(ctrl, "Latch open on slot(%s)\n",
  377. slot_name(p_slot));
  378. return -ENODEV;
  379. }
  380. }
  381. if (POWER_CTRL(p_slot->ctrl)) {
  382. pciehp_get_power_status(p_slot, &getstatus);
  383. if (getstatus) {
  384. ctrl_info(ctrl, "Already enabled on slot(%s)\n",
  385. slot_name(p_slot));
  386. return -EINVAL;
  387. }
  388. }
  389. pciehp_get_latch_status(p_slot, &getstatus);
  390. rc = board_added(p_slot);
  391. if (rc)
  392. pciehp_get_latch_status(p_slot, &getstatus);
  393. return rc;
  394. }
  395. /*
  396. * Note: This function must be called with slot->hotplug_lock held
  397. */
  398. int pciehp_disable_slot(struct slot *p_slot)
  399. {
  400. u8 getstatus = 0;
  401. struct controller *ctrl = p_slot->ctrl;
  402. if (!p_slot->ctrl)
  403. return 1;
  404. if (POWER_CTRL(p_slot->ctrl)) {
  405. pciehp_get_power_status(p_slot, &getstatus);
  406. if (!getstatus) {
  407. ctrl_info(ctrl, "Already disabled on slot(%s)\n",
  408. slot_name(p_slot));
  409. return -EINVAL;
  410. }
  411. }
  412. return remove_board(p_slot);
  413. }
  414. int pciehp_sysfs_enable_slot(struct slot *p_slot)
  415. {
  416. int retval = -ENODEV;
  417. struct controller *ctrl = p_slot->ctrl;
  418. mutex_lock(&p_slot->lock);
  419. switch (p_slot->state) {
  420. case BLINKINGON_STATE:
  421. cancel_delayed_work(&p_slot->work);
  422. case STATIC_STATE:
  423. p_slot->state = POWERON_STATE;
  424. mutex_unlock(&p_slot->lock);
  425. mutex_lock(&p_slot->hotplug_lock);
  426. retval = pciehp_enable_slot(p_slot);
  427. mutex_unlock(&p_slot->hotplug_lock);
  428. mutex_lock(&p_slot->lock);
  429. p_slot->state = STATIC_STATE;
  430. break;
  431. case POWERON_STATE:
  432. ctrl_info(ctrl, "Slot %s is already in powering on state\n",
  433. slot_name(p_slot));
  434. break;
  435. case BLINKINGOFF_STATE:
  436. case POWEROFF_STATE:
  437. ctrl_info(ctrl, "Already enabled on slot %s\n",
  438. slot_name(p_slot));
  439. break;
  440. default:
  441. ctrl_err(ctrl, "invalid state %#x on slot %s\n",
  442. p_slot->state, slot_name(p_slot));
  443. break;
  444. }
  445. mutex_unlock(&p_slot->lock);
  446. return retval;
  447. }
  448. int pciehp_sysfs_disable_slot(struct slot *p_slot)
  449. {
  450. int retval = -ENODEV;
  451. struct controller *ctrl = p_slot->ctrl;
  452. mutex_lock(&p_slot->lock);
  453. switch (p_slot->state) {
  454. case BLINKINGOFF_STATE:
  455. cancel_delayed_work(&p_slot->work);
  456. case STATIC_STATE:
  457. p_slot->state = POWEROFF_STATE;
  458. mutex_unlock(&p_slot->lock);
  459. retval = pciehp_disable_slot(p_slot);
  460. mutex_lock(&p_slot->lock);
  461. p_slot->state = STATIC_STATE;
  462. break;
  463. case POWEROFF_STATE:
  464. ctrl_info(ctrl, "Slot %s is already in powering off state\n",
  465. slot_name(p_slot));
  466. break;
  467. case BLINKINGON_STATE:
  468. case POWERON_STATE:
  469. ctrl_info(ctrl, "Already disabled on slot %s\n",
  470. slot_name(p_slot));
  471. break;
  472. default:
  473. ctrl_err(ctrl, "invalid state %#x on slot %s\n",
  474. p_slot->state, slot_name(p_slot));
  475. break;
  476. }
  477. mutex_unlock(&p_slot->lock);
  478. return retval;
  479. }