pciehp_ctrl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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. void pciehp_queue_pushbutton_work(struct work_struct *work)
  181. {
  182. struct slot *p_slot = container_of(work, struct slot, work.work);
  183. struct power_work_info *info;
  184. info = kmalloc(sizeof(*info), GFP_KERNEL);
  185. if (!info) {
  186. ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
  187. __func__);
  188. return;
  189. }
  190. info->p_slot = p_slot;
  191. INIT_WORK(&info->work, pciehp_power_thread);
  192. mutex_lock(&p_slot->lock);
  193. switch (p_slot->state) {
  194. case BLINKINGOFF_STATE:
  195. p_slot->state = POWEROFF_STATE;
  196. info->req = DISABLE_REQ;
  197. break;
  198. case BLINKINGON_STATE:
  199. p_slot->state = POWERON_STATE;
  200. info->req = ENABLE_REQ;
  201. break;
  202. default:
  203. kfree(info);
  204. goto out;
  205. }
  206. queue_work(p_slot->wq, &info->work);
  207. out:
  208. mutex_unlock(&p_slot->lock);
  209. }
  210. /*
  211. * Note: This function must be called with slot->lock held
  212. */
  213. static void handle_button_press_event(struct slot *p_slot)
  214. {
  215. struct controller *ctrl = p_slot->ctrl;
  216. u8 getstatus;
  217. switch (p_slot->state) {
  218. case STATIC_STATE:
  219. pciehp_get_power_status(p_slot, &getstatus);
  220. if (getstatus) {
  221. p_slot->state = BLINKINGOFF_STATE;
  222. ctrl_info(ctrl, "PCI slot #%s - powering off due to button press\n",
  223. slot_name(p_slot));
  224. } else {
  225. p_slot->state = BLINKINGON_STATE;
  226. ctrl_info(ctrl, "PCI slot #%s - powering on due to button press\n",
  227. slot_name(p_slot));
  228. }
  229. /* blink green LED and turn off amber */
  230. pciehp_green_led_blink(p_slot);
  231. pciehp_set_attention_status(p_slot, 0);
  232. queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
  233. break;
  234. case BLINKINGOFF_STATE:
  235. case BLINKINGON_STATE:
  236. /*
  237. * Cancel if we are still blinking; this means that we
  238. * press the attention again before the 5 sec. limit
  239. * expires to cancel hot-add or hot-remove
  240. */
  241. ctrl_info(ctrl, "Button cancel on Slot(%s)\n", slot_name(p_slot));
  242. cancel_delayed_work(&p_slot->work);
  243. if (p_slot->state == BLINKINGOFF_STATE)
  244. pciehp_green_led_on(p_slot);
  245. else
  246. pciehp_green_led_off(p_slot);
  247. pciehp_set_attention_status(p_slot, 0);
  248. ctrl_info(ctrl, "PCI slot #%s - action canceled due to button press\n",
  249. slot_name(p_slot));
  250. p_slot->state = STATIC_STATE;
  251. break;
  252. case POWEROFF_STATE:
  253. case POWERON_STATE:
  254. /*
  255. * Ignore if the slot is on power-on or power-off state;
  256. * this means that the previous attention button action
  257. * to hot-add or hot-remove is undergoing
  258. */
  259. ctrl_info(ctrl, "Button ignore on Slot(%s)\n", slot_name(p_slot));
  260. break;
  261. default:
  262. ctrl_warn(ctrl, "ignoring invalid state %#x\n", p_slot->state);
  263. break;
  264. }
  265. }
  266. /*
  267. * Note: This function must be called with slot->lock held
  268. */
  269. static void handle_surprise_event(struct slot *p_slot)
  270. {
  271. u8 getstatus;
  272. struct power_work_info *info;
  273. info = kmalloc(sizeof(*info), GFP_KERNEL);
  274. if (!info) {
  275. ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
  276. __func__);
  277. return;
  278. }
  279. info->p_slot = p_slot;
  280. INIT_WORK(&info->work, pciehp_power_thread);
  281. pciehp_get_adapter_status(p_slot, &getstatus);
  282. if (!getstatus) {
  283. p_slot->state = POWEROFF_STATE;
  284. info->req = DISABLE_REQ;
  285. } else {
  286. p_slot->state = POWERON_STATE;
  287. info->req = ENABLE_REQ;
  288. }
  289. queue_work(p_slot->wq, &info->work);
  290. }
  291. /*
  292. * Note: This function must be called with slot->lock held
  293. */
  294. static void handle_link_event(struct slot *p_slot, u32 event)
  295. {
  296. struct controller *ctrl = p_slot->ctrl;
  297. struct power_work_info *info;
  298. info = kmalloc(sizeof(*info), GFP_KERNEL);
  299. if (!info) {
  300. ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
  301. __func__);
  302. return;
  303. }
  304. info->p_slot = p_slot;
  305. info->req = event == INT_LINK_UP ? ENABLE_REQ : DISABLE_REQ;
  306. INIT_WORK(&info->work, pciehp_power_thread);
  307. switch (p_slot->state) {
  308. case BLINKINGON_STATE:
  309. case BLINKINGOFF_STATE:
  310. cancel_delayed_work(&p_slot->work);
  311. /* Fall through */
  312. case STATIC_STATE:
  313. p_slot->state = event == INT_LINK_UP ?
  314. POWERON_STATE : POWEROFF_STATE;
  315. queue_work(p_slot->wq, &info->work);
  316. break;
  317. case POWERON_STATE:
  318. if (event == INT_LINK_UP) {
  319. ctrl_info(ctrl,
  320. "Link Up event ignored on slot(%s): already powering on\n",
  321. slot_name(p_slot));
  322. kfree(info);
  323. } else {
  324. ctrl_info(ctrl,
  325. "Link Down event queued on slot(%s): currently getting powered on\n",
  326. slot_name(p_slot));
  327. p_slot->state = POWEROFF_STATE;
  328. queue_work(p_slot->wq, &info->work);
  329. }
  330. break;
  331. case POWEROFF_STATE:
  332. if (event == INT_LINK_UP) {
  333. ctrl_info(ctrl,
  334. "Link Up event queued on slot(%s): currently getting powered off\n",
  335. slot_name(p_slot));
  336. p_slot->state = POWERON_STATE;
  337. queue_work(p_slot->wq, &info->work);
  338. } else {
  339. ctrl_info(ctrl,
  340. "Link Down event ignored on slot(%s): already powering off\n",
  341. slot_name(p_slot));
  342. kfree(info);
  343. }
  344. break;
  345. default:
  346. ctrl_err(ctrl, "ignoring invalid state %#x on slot(%s)\n",
  347. p_slot->state, slot_name(p_slot));
  348. kfree(info);
  349. break;
  350. }
  351. }
  352. static void interrupt_event_handler(struct work_struct *work)
  353. {
  354. struct event_info *info = container_of(work, struct event_info, work);
  355. struct slot *p_slot = info->p_slot;
  356. struct controller *ctrl = p_slot->ctrl;
  357. mutex_lock(&p_slot->lock);
  358. switch (info->event_type) {
  359. case INT_BUTTON_PRESS:
  360. handle_button_press_event(p_slot);
  361. break;
  362. case INT_POWER_FAULT:
  363. if (!POWER_CTRL(ctrl))
  364. break;
  365. pciehp_set_attention_status(p_slot, 1);
  366. pciehp_green_led_off(p_slot);
  367. break;
  368. case INT_PRESENCE_ON:
  369. handle_surprise_event(p_slot);
  370. break;
  371. case INT_PRESENCE_OFF:
  372. /*
  373. * Regardless of surprise capability, we need to
  374. * definitely remove a card that has been pulled out!
  375. */
  376. handle_surprise_event(p_slot);
  377. break;
  378. case INT_LINK_UP:
  379. case INT_LINK_DOWN:
  380. handle_link_event(p_slot, info->event_type);
  381. break;
  382. default:
  383. break;
  384. }
  385. mutex_unlock(&p_slot->lock);
  386. kfree(info);
  387. }
  388. /*
  389. * Note: This function must be called with slot->hotplug_lock held
  390. */
  391. int pciehp_enable_slot(struct slot *p_slot)
  392. {
  393. u8 getstatus = 0;
  394. int rc;
  395. struct controller *ctrl = p_slot->ctrl;
  396. pciehp_get_adapter_status(p_slot, &getstatus);
  397. if (!getstatus) {
  398. ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
  399. return -ENODEV;
  400. }
  401. if (MRL_SENS(p_slot->ctrl)) {
  402. pciehp_get_latch_status(p_slot, &getstatus);
  403. if (getstatus) {
  404. ctrl_info(ctrl, "Latch open on slot(%s)\n",
  405. slot_name(p_slot));
  406. return -ENODEV;
  407. }
  408. }
  409. if (POWER_CTRL(p_slot->ctrl)) {
  410. pciehp_get_power_status(p_slot, &getstatus);
  411. if (getstatus) {
  412. ctrl_info(ctrl, "Already enabled on slot(%s)\n",
  413. slot_name(p_slot));
  414. return -EINVAL;
  415. }
  416. }
  417. pciehp_get_latch_status(p_slot, &getstatus);
  418. rc = board_added(p_slot);
  419. if (rc)
  420. pciehp_get_latch_status(p_slot, &getstatus);
  421. return rc;
  422. }
  423. /*
  424. * Note: This function must be called with slot->hotplug_lock held
  425. */
  426. int pciehp_disable_slot(struct slot *p_slot)
  427. {
  428. u8 getstatus = 0;
  429. struct controller *ctrl = p_slot->ctrl;
  430. if (!p_slot->ctrl)
  431. return 1;
  432. if (POWER_CTRL(p_slot->ctrl)) {
  433. pciehp_get_power_status(p_slot, &getstatus);
  434. if (!getstatus) {
  435. ctrl_info(ctrl, "Already disabled on slot(%s)\n",
  436. slot_name(p_slot));
  437. return -EINVAL;
  438. }
  439. }
  440. return remove_board(p_slot);
  441. }
  442. int pciehp_sysfs_enable_slot(struct slot *p_slot)
  443. {
  444. int retval = -ENODEV;
  445. struct controller *ctrl = p_slot->ctrl;
  446. mutex_lock(&p_slot->lock);
  447. switch (p_slot->state) {
  448. case BLINKINGON_STATE:
  449. cancel_delayed_work(&p_slot->work);
  450. case STATIC_STATE:
  451. p_slot->state = POWERON_STATE;
  452. mutex_unlock(&p_slot->lock);
  453. mutex_lock(&p_slot->hotplug_lock);
  454. retval = pciehp_enable_slot(p_slot);
  455. mutex_unlock(&p_slot->hotplug_lock);
  456. mutex_lock(&p_slot->lock);
  457. p_slot->state = STATIC_STATE;
  458. break;
  459. case POWERON_STATE:
  460. ctrl_info(ctrl, "Slot %s is already in powering on state\n",
  461. slot_name(p_slot));
  462. break;
  463. case BLINKINGOFF_STATE:
  464. case POWEROFF_STATE:
  465. ctrl_info(ctrl, "Already enabled on slot %s\n",
  466. slot_name(p_slot));
  467. break;
  468. default:
  469. ctrl_err(ctrl, "invalid state %#x on slot %s\n",
  470. p_slot->state, slot_name(p_slot));
  471. break;
  472. }
  473. mutex_unlock(&p_slot->lock);
  474. return retval;
  475. }
  476. int pciehp_sysfs_disable_slot(struct slot *p_slot)
  477. {
  478. int retval = -ENODEV;
  479. struct controller *ctrl = p_slot->ctrl;
  480. mutex_lock(&p_slot->lock);
  481. switch (p_slot->state) {
  482. case BLINKINGOFF_STATE:
  483. cancel_delayed_work(&p_slot->work);
  484. case STATIC_STATE:
  485. p_slot->state = POWEROFF_STATE;
  486. mutex_unlock(&p_slot->lock);
  487. retval = pciehp_disable_slot(p_slot);
  488. mutex_lock(&p_slot->lock);
  489. p_slot->state = STATIC_STATE;
  490. break;
  491. case POWEROFF_STATE:
  492. ctrl_info(ctrl, "Slot %s is already in powering off state\n",
  493. slot_name(p_slot));
  494. break;
  495. case BLINKINGON_STATE:
  496. case POWERON_STATE:
  497. ctrl_info(ctrl, "Already disabled on slot %s\n",
  498. slot_name(p_slot));
  499. break;
  500. default:
  501. ctrl_err(ctrl, "invalid state %#x on slot %s\n",
  502. p_slot->state, slot_name(p_slot));
  503. break;
  504. }
  505. mutex_unlock(&p_slot->lock);
  506. return retval;
  507. }