pciehp_ctrl.c 15 KB

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