pciehp_ctrl.c 15 KB

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