pciehp_ctrl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. static int 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. return -ENOMEM;
  43. info->event_type = event_type;
  44. info->p_slot = p_slot;
  45. INIT_WORK(&info->work, interrupt_event_handler);
  46. queue_work(p_slot->wq, &info->work);
  47. return 0;
  48. }
  49. u8 pciehp_handle_attention_button(struct slot *p_slot)
  50. {
  51. u32 event_type;
  52. struct controller *ctrl = p_slot->ctrl;
  53. /* Attention Button Change */
  54. ctrl_dbg(ctrl, "Attention button interrupt received\n");
  55. /*
  56. * Button pressed - See if need to TAKE ACTION!!!
  57. */
  58. ctrl_info(ctrl, "Button pressed on Slot(%s)\n", slot_name(p_slot));
  59. event_type = INT_BUTTON_PRESS;
  60. queue_interrupt_event(p_slot, event_type);
  61. return 0;
  62. }
  63. u8 pciehp_handle_switch_change(struct slot *p_slot)
  64. {
  65. u8 getstatus;
  66. u32 event_type;
  67. struct controller *ctrl = p_slot->ctrl;
  68. /* Switch Change */
  69. ctrl_dbg(ctrl, "Switch interrupt received\n");
  70. pciehp_get_latch_status(p_slot, &getstatus);
  71. if (getstatus) {
  72. /*
  73. * Switch opened
  74. */
  75. ctrl_info(ctrl, "Latch open on Slot(%s)\n", slot_name(p_slot));
  76. event_type = INT_SWITCH_OPEN;
  77. } else {
  78. /*
  79. * Switch closed
  80. */
  81. ctrl_info(ctrl, "Latch close on Slot(%s)\n", slot_name(p_slot));
  82. event_type = INT_SWITCH_CLOSE;
  83. }
  84. queue_interrupt_event(p_slot, event_type);
  85. return 1;
  86. }
  87. u8 pciehp_handle_presence_change(struct slot *p_slot)
  88. {
  89. u32 event_type;
  90. u8 presence_save;
  91. struct controller *ctrl = p_slot->ctrl;
  92. /* Presence Change */
  93. ctrl_dbg(ctrl, "Presence/Notify input change\n");
  94. /* Switch is open, assume a presence change
  95. * Save the presence state
  96. */
  97. pciehp_get_adapter_status(p_slot, &presence_save);
  98. if (presence_save) {
  99. /*
  100. * Card Present
  101. */
  102. ctrl_info(ctrl, "Card present on Slot(%s)\n", slot_name(p_slot));
  103. event_type = INT_PRESENCE_ON;
  104. } else {
  105. /*
  106. * Not Present
  107. */
  108. ctrl_info(ctrl, "Card not present on Slot(%s)\n",
  109. slot_name(p_slot));
  110. event_type = INT_PRESENCE_OFF;
  111. }
  112. queue_interrupt_event(p_slot, event_type);
  113. return 1;
  114. }
  115. u8 pciehp_handle_power_fault(struct slot *p_slot)
  116. {
  117. u32 event_type;
  118. struct controller *ctrl = p_slot->ctrl;
  119. /* power fault */
  120. ctrl_dbg(ctrl, "Power fault interrupt received\n");
  121. ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
  122. event_type = INT_POWER_FAULT;
  123. ctrl_info(ctrl, "Power fault bit %x set\n", 0);
  124. queue_interrupt_event(p_slot, event_type);
  125. return 1;
  126. }
  127. /* The following routines constitute the bulk of the
  128. hotplug controller logic
  129. */
  130. static void set_slot_off(struct controller *ctrl, struct slot * pslot)
  131. {
  132. /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
  133. if (POWER_CTRL(ctrl)) {
  134. if (pciehp_power_off_slot(pslot)) {
  135. ctrl_err(ctrl,
  136. "Issue of Slot Power Off command failed\n");
  137. return;
  138. }
  139. /*
  140. * After turning power off, we must wait for at least 1 second
  141. * before taking any action that relies on power having been
  142. * removed from the slot/adapter.
  143. */
  144. msleep(1000);
  145. }
  146. if (PWR_LED(ctrl))
  147. pciehp_green_led_off(pslot);
  148. if (ATTN_LED(ctrl)) {
  149. if (pciehp_set_attention_status(pslot, 1)) {
  150. ctrl_err(ctrl,
  151. "Issue of Set Attention Led command failed\n");
  152. return;
  153. }
  154. }
  155. }
  156. /**
  157. * board_added - Called after a board has been added to the system.
  158. * @p_slot: &slot where board is added
  159. *
  160. * Turns power on for the board.
  161. * Configures board.
  162. */
  163. static int board_added(struct slot *p_slot)
  164. {
  165. int retval = 0;
  166. struct controller *ctrl = p_slot->ctrl;
  167. struct pci_bus *parent = ctrl->pcie->port->subordinate;
  168. if (POWER_CTRL(ctrl)) {
  169. /* Power on slot */
  170. retval = pciehp_power_on_slot(p_slot);
  171. if (retval)
  172. return retval;
  173. }
  174. if (PWR_LED(ctrl))
  175. pciehp_green_led_blink(p_slot);
  176. /* Check link training status */
  177. retval = pciehp_check_link_status(ctrl);
  178. if (retval) {
  179. ctrl_err(ctrl, "Failed to check link status\n");
  180. goto err_exit;
  181. }
  182. /* Check for a power fault */
  183. if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
  184. ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
  185. retval = -EIO;
  186. goto err_exit;
  187. }
  188. retval = pciehp_configure_device(p_slot);
  189. if (retval) {
  190. ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
  191. pci_domain_nr(parent), parent->number);
  192. goto err_exit;
  193. }
  194. if (PWR_LED(ctrl))
  195. pciehp_green_led_on(p_slot);
  196. return 0;
  197. err_exit:
  198. set_slot_off(ctrl, p_slot);
  199. return retval;
  200. }
  201. /**
  202. * remove_board - Turns off slot and LEDs
  203. * @p_slot: slot where board is being removed
  204. */
  205. static int remove_board(struct slot *p_slot)
  206. {
  207. int retval = 0;
  208. struct controller *ctrl = p_slot->ctrl;
  209. retval = pciehp_unconfigure_device(p_slot);
  210. if (retval)
  211. return retval;
  212. if (POWER_CTRL(ctrl)) {
  213. /* power off slot */
  214. retval = pciehp_power_off_slot(p_slot);
  215. if (retval) {
  216. ctrl_err(ctrl,
  217. "Issue of Slot Disable command failed\n");
  218. return retval;
  219. }
  220. /*
  221. * After turning power off, we must wait for at least 1 second
  222. * before taking any action that relies on power having been
  223. * removed from the slot/adapter.
  224. */
  225. msleep(1000);
  226. }
  227. /* turn off Green LED */
  228. if (PWR_LED(ctrl))
  229. pciehp_green_led_off(p_slot);
  230. return 0;
  231. }
  232. struct power_work_info {
  233. struct slot *p_slot;
  234. struct work_struct work;
  235. };
  236. /**
  237. * pciehp_power_thread - handle pushbutton events
  238. * @work: &struct work_struct describing work to be done
  239. *
  240. * Scheduled procedure to handle blocking stuff for the pushbuttons.
  241. * Handles all pending events and exits.
  242. */
  243. static void pciehp_power_thread(struct work_struct *work)
  244. {
  245. struct power_work_info *info =
  246. container_of(work, struct power_work_info, work);
  247. struct slot *p_slot = info->p_slot;
  248. mutex_lock(&p_slot->lock);
  249. switch (p_slot->state) {
  250. case POWEROFF_STATE:
  251. mutex_unlock(&p_slot->lock);
  252. ctrl_dbg(p_slot->ctrl,
  253. "Disabling domain:bus:device=%04x:%02x:00\n",
  254. pci_domain_nr(p_slot->ctrl->pcie->port->subordinate),
  255. p_slot->ctrl->pcie->port->subordinate->number);
  256. pciehp_disable_slot(p_slot);
  257. mutex_lock(&p_slot->lock);
  258. p_slot->state = STATIC_STATE;
  259. break;
  260. case POWERON_STATE:
  261. mutex_unlock(&p_slot->lock);
  262. if (pciehp_enable_slot(p_slot) && PWR_LED(p_slot->ctrl))
  263. pciehp_green_led_off(p_slot);
  264. mutex_lock(&p_slot->lock);
  265. p_slot->state = STATIC_STATE;
  266. break;
  267. default:
  268. break;
  269. }
  270. mutex_unlock(&p_slot->lock);
  271. kfree(info);
  272. }
  273. void pciehp_queue_pushbutton_work(struct work_struct *work)
  274. {
  275. struct slot *p_slot = container_of(work, struct slot, work.work);
  276. struct power_work_info *info;
  277. info = kmalloc(sizeof(*info), GFP_KERNEL);
  278. if (!info) {
  279. ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
  280. __func__);
  281. return;
  282. }
  283. info->p_slot = p_slot;
  284. INIT_WORK(&info->work, pciehp_power_thread);
  285. mutex_lock(&p_slot->lock);
  286. switch (p_slot->state) {
  287. case BLINKINGOFF_STATE:
  288. p_slot->state = POWEROFF_STATE;
  289. break;
  290. case BLINKINGON_STATE:
  291. p_slot->state = POWERON_STATE;
  292. break;
  293. default:
  294. kfree(info);
  295. goto out;
  296. }
  297. queue_work(p_slot->wq, &info->work);
  298. out:
  299. mutex_unlock(&p_slot->lock);
  300. }
  301. /*
  302. * Note: This function must be called with slot->lock held
  303. */
  304. static void handle_button_press_event(struct slot *p_slot)
  305. {
  306. struct controller *ctrl = p_slot->ctrl;
  307. u8 getstatus;
  308. switch (p_slot->state) {
  309. case STATIC_STATE:
  310. pciehp_get_power_status(p_slot, &getstatus);
  311. if (getstatus) {
  312. p_slot->state = BLINKINGOFF_STATE;
  313. ctrl_info(ctrl,
  314. "PCI slot #%s - powering off due to button "
  315. "press.\n", slot_name(p_slot));
  316. } else {
  317. p_slot->state = BLINKINGON_STATE;
  318. ctrl_info(ctrl,
  319. "PCI slot #%s - powering on due to button "
  320. "press.\n", slot_name(p_slot));
  321. }
  322. /* blink green LED and turn off amber */
  323. if (PWR_LED(ctrl))
  324. pciehp_green_led_blink(p_slot);
  325. if (ATTN_LED(ctrl))
  326. pciehp_set_attention_status(p_slot, 0);
  327. queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
  328. break;
  329. case BLINKINGOFF_STATE:
  330. case BLINKINGON_STATE:
  331. /*
  332. * Cancel if we are still blinking; this means that we
  333. * press the attention again before the 5 sec. limit
  334. * expires to cancel hot-add or hot-remove
  335. */
  336. ctrl_info(ctrl, "Button cancel on Slot(%s)\n", slot_name(p_slot));
  337. cancel_delayed_work(&p_slot->work);
  338. if (p_slot->state == BLINKINGOFF_STATE) {
  339. if (PWR_LED(ctrl))
  340. pciehp_green_led_on(p_slot);
  341. } else {
  342. if (PWR_LED(ctrl))
  343. pciehp_green_led_off(p_slot);
  344. }
  345. if (ATTN_LED(ctrl))
  346. pciehp_set_attention_status(p_slot, 0);
  347. ctrl_info(ctrl, "PCI slot #%s - action canceled "
  348. "due to button press\n", slot_name(p_slot));
  349. p_slot->state = STATIC_STATE;
  350. break;
  351. case POWEROFF_STATE:
  352. case POWERON_STATE:
  353. /*
  354. * Ignore if the slot is on power-on or power-off state;
  355. * this means that the previous attention button action
  356. * to hot-add or hot-remove is undergoing
  357. */
  358. ctrl_info(ctrl, "Button ignore on Slot(%s)\n", slot_name(p_slot));
  359. break;
  360. default:
  361. ctrl_warn(ctrl, "Not a valid state\n");
  362. break;
  363. }
  364. }
  365. /*
  366. * Note: This function must be called with slot->lock held
  367. */
  368. static void handle_surprise_event(struct slot *p_slot)
  369. {
  370. u8 getstatus;
  371. struct power_work_info *info;
  372. info = kmalloc(sizeof(*info), GFP_KERNEL);
  373. if (!info) {
  374. ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
  375. __func__);
  376. return;
  377. }
  378. info->p_slot = p_slot;
  379. INIT_WORK(&info->work, pciehp_power_thread);
  380. pciehp_get_adapter_status(p_slot, &getstatus);
  381. if (!getstatus)
  382. p_slot->state = POWEROFF_STATE;
  383. else
  384. p_slot->state = POWERON_STATE;
  385. queue_work(p_slot->wq, &info->work);
  386. }
  387. static void interrupt_event_handler(struct work_struct *work)
  388. {
  389. struct event_info *info = container_of(work, struct event_info, work);
  390. struct slot *p_slot = info->p_slot;
  391. struct controller *ctrl = p_slot->ctrl;
  392. mutex_lock(&p_slot->lock);
  393. switch (info->event_type) {
  394. case INT_BUTTON_PRESS:
  395. handle_button_press_event(p_slot);
  396. break;
  397. case INT_POWER_FAULT:
  398. if (!POWER_CTRL(ctrl))
  399. break;
  400. if (ATTN_LED(ctrl))
  401. pciehp_set_attention_status(p_slot, 1);
  402. if (PWR_LED(ctrl))
  403. pciehp_green_led_off(p_slot);
  404. break;
  405. case INT_PRESENCE_ON:
  406. case INT_PRESENCE_OFF:
  407. if (!HP_SUPR_RM(ctrl))
  408. break;
  409. ctrl_dbg(ctrl, "Surprise Removal\n");
  410. handle_surprise_event(p_slot);
  411. break;
  412. default:
  413. break;
  414. }
  415. mutex_unlock(&p_slot->lock);
  416. kfree(info);
  417. }
  418. int pciehp_enable_slot(struct slot *p_slot)
  419. {
  420. u8 getstatus = 0;
  421. int rc;
  422. struct controller *ctrl = p_slot->ctrl;
  423. rc = pciehp_get_adapter_status(p_slot, &getstatus);
  424. if (rc || !getstatus) {
  425. ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
  426. return -ENODEV;
  427. }
  428. if (MRL_SENS(p_slot->ctrl)) {
  429. rc = pciehp_get_latch_status(p_slot, &getstatus);
  430. if (rc || getstatus) {
  431. ctrl_info(ctrl, "Latch open on slot(%s)\n",
  432. slot_name(p_slot));
  433. return -ENODEV;
  434. }
  435. }
  436. if (POWER_CTRL(p_slot->ctrl)) {
  437. rc = pciehp_get_power_status(p_slot, &getstatus);
  438. if (rc || getstatus) {
  439. ctrl_info(ctrl, "Already enabled on slot(%s)\n",
  440. slot_name(p_slot));
  441. return -EINVAL;
  442. }
  443. }
  444. pciehp_get_latch_status(p_slot, &getstatus);
  445. rc = board_added(p_slot);
  446. if (rc) {
  447. pciehp_get_latch_status(p_slot, &getstatus);
  448. }
  449. return rc;
  450. }
  451. int pciehp_disable_slot(struct slot *p_slot)
  452. {
  453. u8 getstatus = 0;
  454. int ret = 0;
  455. struct controller *ctrl = p_slot->ctrl;
  456. if (!p_slot->ctrl)
  457. return 1;
  458. if (!HP_SUPR_RM(p_slot->ctrl)) {
  459. ret = pciehp_get_adapter_status(p_slot, &getstatus);
  460. if (ret || !getstatus) {
  461. ctrl_info(ctrl, "No adapter on slot(%s)\n",
  462. slot_name(p_slot));
  463. return -ENODEV;
  464. }
  465. }
  466. if (MRL_SENS(p_slot->ctrl)) {
  467. ret = pciehp_get_latch_status(p_slot, &getstatus);
  468. if (ret || getstatus) {
  469. ctrl_info(ctrl, "Latch open on slot(%s)\n",
  470. slot_name(p_slot));
  471. return -ENODEV;
  472. }
  473. }
  474. if (POWER_CTRL(p_slot->ctrl)) {
  475. ret = pciehp_get_power_status(p_slot, &getstatus);
  476. if (ret || !getstatus) {
  477. ctrl_info(ctrl, "Already disabled on slot(%s)\n",
  478. slot_name(p_slot));
  479. return -EINVAL;
  480. }
  481. }
  482. return remove_board(p_slot);
  483. }
  484. int pciehp_sysfs_enable_slot(struct slot *p_slot)
  485. {
  486. int retval = -ENODEV;
  487. struct controller *ctrl = p_slot->ctrl;
  488. mutex_lock(&p_slot->lock);
  489. switch (p_slot->state) {
  490. case BLINKINGON_STATE:
  491. cancel_delayed_work(&p_slot->work);
  492. case STATIC_STATE:
  493. p_slot->state = POWERON_STATE;
  494. mutex_unlock(&p_slot->lock);
  495. retval = pciehp_enable_slot(p_slot);
  496. mutex_lock(&p_slot->lock);
  497. p_slot->state = STATIC_STATE;
  498. break;
  499. case POWERON_STATE:
  500. ctrl_info(ctrl, "Slot %s is already in powering on state\n",
  501. slot_name(p_slot));
  502. break;
  503. case BLINKINGOFF_STATE:
  504. case POWEROFF_STATE:
  505. ctrl_info(ctrl, "Already enabled on slot %s\n",
  506. slot_name(p_slot));
  507. break;
  508. default:
  509. ctrl_err(ctrl, "Not a valid state on slot %s\n",
  510. slot_name(p_slot));
  511. break;
  512. }
  513. mutex_unlock(&p_slot->lock);
  514. return retval;
  515. }
  516. int pciehp_sysfs_disable_slot(struct slot *p_slot)
  517. {
  518. int retval = -ENODEV;
  519. struct controller *ctrl = p_slot->ctrl;
  520. mutex_lock(&p_slot->lock);
  521. switch (p_slot->state) {
  522. case BLINKINGOFF_STATE:
  523. cancel_delayed_work(&p_slot->work);
  524. case STATIC_STATE:
  525. p_slot->state = POWEROFF_STATE;
  526. mutex_unlock(&p_slot->lock);
  527. retval = pciehp_disable_slot(p_slot);
  528. mutex_lock(&p_slot->lock);
  529. p_slot->state = STATIC_STATE;
  530. break;
  531. case POWEROFF_STATE:
  532. ctrl_info(ctrl, "Slot %s is already in powering off state\n",
  533. slot_name(p_slot));
  534. break;
  535. case BLINKINGON_STATE:
  536. case POWERON_STATE:
  537. ctrl_info(ctrl, "Already disabled on slot %s\n",
  538. slot_name(p_slot));
  539. break;
  540. default:
  541. ctrl_err(ctrl, "Not a valid state on slot %s\n",
  542. slot_name(p_slot));
  543. break;
  544. }
  545. mutex_unlock(&p_slot->lock);
  546. return retval;
  547. }