pciehp_ctrl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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. pciehp_power_off_slot(pslot);
  135. /*
  136. * After turning power off, we must wait for at least 1 second
  137. * before taking any action that relies on power having been
  138. * removed from the slot/adapter.
  139. */
  140. msleep(1000);
  141. }
  142. pciehp_green_led_off(pslot);
  143. pciehp_set_attention_status(pslot, 1);
  144. }
  145. /**
  146. * board_added - Called after a board has been added to the system.
  147. * @p_slot: &slot where board is added
  148. *
  149. * Turns power on for the board.
  150. * Configures board.
  151. */
  152. static int board_added(struct slot *p_slot)
  153. {
  154. int retval = 0;
  155. struct controller *ctrl = p_slot->ctrl;
  156. struct pci_bus *parent = ctrl->pcie->port->subordinate;
  157. if (POWER_CTRL(ctrl)) {
  158. /* Power on slot */
  159. retval = pciehp_power_on_slot(p_slot);
  160. if (retval)
  161. return retval;
  162. }
  163. pciehp_green_led_blink(p_slot);
  164. /* Check link training status */
  165. retval = pciehp_check_link_status(ctrl);
  166. if (retval) {
  167. ctrl_err(ctrl, "Failed to check link status\n");
  168. goto err_exit;
  169. }
  170. /* Check for a power fault */
  171. if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
  172. ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
  173. retval = -EIO;
  174. goto err_exit;
  175. }
  176. retval = pciehp_configure_device(p_slot);
  177. if (retval) {
  178. ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
  179. pci_domain_nr(parent), parent->number);
  180. goto err_exit;
  181. }
  182. pciehp_green_led_on(p_slot);
  183. return 0;
  184. err_exit:
  185. set_slot_off(ctrl, p_slot);
  186. return retval;
  187. }
  188. /**
  189. * remove_board - Turns off slot and LEDs
  190. * @p_slot: slot where board is being removed
  191. */
  192. static int remove_board(struct slot *p_slot)
  193. {
  194. int retval;
  195. struct controller *ctrl = p_slot->ctrl;
  196. retval = pciehp_unconfigure_device(p_slot);
  197. if (retval)
  198. return retval;
  199. if (POWER_CTRL(ctrl)) {
  200. pciehp_power_off_slot(p_slot);
  201. /*
  202. * After turning power off, we must wait for at least 1 second
  203. * before taking any action that relies on power having been
  204. * removed from the slot/adapter.
  205. */
  206. msleep(1000);
  207. }
  208. /* turn off Green LED */
  209. pciehp_green_led_off(p_slot);
  210. return 0;
  211. }
  212. struct power_work_info {
  213. struct slot *p_slot;
  214. struct work_struct work;
  215. };
  216. /**
  217. * pciehp_power_thread - handle pushbutton events
  218. * @work: &struct work_struct describing work to be done
  219. *
  220. * Scheduled procedure to handle blocking stuff for the pushbuttons.
  221. * Handles all pending events and exits.
  222. */
  223. static void pciehp_power_thread(struct work_struct *work)
  224. {
  225. struct power_work_info *info =
  226. container_of(work, struct power_work_info, work);
  227. struct slot *p_slot = info->p_slot;
  228. mutex_lock(&p_slot->lock);
  229. switch (p_slot->state) {
  230. case POWEROFF_STATE:
  231. mutex_unlock(&p_slot->lock);
  232. ctrl_dbg(p_slot->ctrl,
  233. "Disabling domain:bus:device=%04x:%02x:00\n",
  234. pci_domain_nr(p_slot->ctrl->pcie->port->subordinate),
  235. p_slot->ctrl->pcie->port->subordinate->number);
  236. pciehp_disable_slot(p_slot);
  237. mutex_lock(&p_slot->lock);
  238. p_slot->state = STATIC_STATE;
  239. break;
  240. case POWERON_STATE:
  241. mutex_unlock(&p_slot->lock);
  242. if (pciehp_enable_slot(p_slot))
  243. pciehp_green_led_off(p_slot);
  244. mutex_lock(&p_slot->lock);
  245. p_slot->state = STATIC_STATE;
  246. break;
  247. default:
  248. break;
  249. }
  250. mutex_unlock(&p_slot->lock);
  251. kfree(info);
  252. }
  253. void pciehp_queue_pushbutton_work(struct work_struct *work)
  254. {
  255. struct slot *p_slot = container_of(work, struct slot, work.work);
  256. struct power_work_info *info;
  257. info = kmalloc(sizeof(*info), GFP_KERNEL);
  258. if (!info) {
  259. ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
  260. __func__);
  261. return;
  262. }
  263. info->p_slot = p_slot;
  264. INIT_WORK(&info->work, pciehp_power_thread);
  265. mutex_lock(&p_slot->lock);
  266. switch (p_slot->state) {
  267. case BLINKINGOFF_STATE:
  268. p_slot->state = POWEROFF_STATE;
  269. break;
  270. case BLINKINGON_STATE:
  271. p_slot->state = POWERON_STATE;
  272. break;
  273. default:
  274. kfree(info);
  275. goto out;
  276. }
  277. queue_work(p_slot->wq, &info->work);
  278. out:
  279. mutex_unlock(&p_slot->lock);
  280. }
  281. /*
  282. * Note: This function must be called with slot->lock held
  283. */
  284. static void handle_button_press_event(struct slot *p_slot)
  285. {
  286. struct controller *ctrl = p_slot->ctrl;
  287. u8 getstatus;
  288. switch (p_slot->state) {
  289. case STATIC_STATE:
  290. pciehp_get_power_status(p_slot, &getstatus);
  291. if (getstatus) {
  292. p_slot->state = BLINKINGOFF_STATE;
  293. ctrl_info(ctrl,
  294. "PCI slot #%s - powering off due to button "
  295. "press.\n", slot_name(p_slot));
  296. } else {
  297. p_slot->state = BLINKINGON_STATE;
  298. ctrl_info(ctrl,
  299. "PCI slot #%s - powering on due to button "
  300. "press.\n", slot_name(p_slot));
  301. }
  302. /* blink green LED and turn off amber */
  303. pciehp_green_led_blink(p_slot);
  304. pciehp_set_attention_status(p_slot, 0);
  305. queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
  306. break;
  307. case BLINKINGOFF_STATE:
  308. case BLINKINGON_STATE:
  309. /*
  310. * Cancel if we are still blinking; this means that we
  311. * press the attention again before the 5 sec. limit
  312. * expires to cancel hot-add or hot-remove
  313. */
  314. ctrl_info(ctrl, "Button cancel on Slot(%s)\n", slot_name(p_slot));
  315. cancel_delayed_work(&p_slot->work);
  316. if (p_slot->state == BLINKINGOFF_STATE) {
  317. pciehp_green_led_on(p_slot);
  318. } else {
  319. pciehp_green_led_off(p_slot);
  320. }
  321. pciehp_set_attention_status(p_slot, 0);
  322. ctrl_info(ctrl, "PCI slot #%s - action canceled "
  323. "due to button press\n", slot_name(p_slot));
  324. p_slot->state = STATIC_STATE;
  325. break;
  326. case POWEROFF_STATE:
  327. case POWERON_STATE:
  328. /*
  329. * Ignore if the slot is on power-on or power-off state;
  330. * this means that the previous attention button action
  331. * to hot-add or hot-remove is undergoing
  332. */
  333. ctrl_info(ctrl, "Button ignore on Slot(%s)\n", slot_name(p_slot));
  334. break;
  335. default:
  336. ctrl_warn(ctrl, "Not a valid state\n");
  337. break;
  338. }
  339. }
  340. /*
  341. * Note: This function must be called with slot->lock held
  342. */
  343. static void handle_surprise_event(struct slot *p_slot)
  344. {
  345. u8 getstatus;
  346. struct power_work_info *info;
  347. info = kmalloc(sizeof(*info), GFP_KERNEL);
  348. if (!info) {
  349. ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
  350. __func__);
  351. return;
  352. }
  353. info->p_slot = p_slot;
  354. INIT_WORK(&info->work, pciehp_power_thread);
  355. pciehp_get_adapter_status(p_slot, &getstatus);
  356. if (!getstatus)
  357. p_slot->state = POWEROFF_STATE;
  358. else
  359. p_slot->state = POWERON_STATE;
  360. queue_work(p_slot->wq, &info->work);
  361. }
  362. static void interrupt_event_handler(struct work_struct *work)
  363. {
  364. struct event_info *info = container_of(work, struct event_info, work);
  365. struct slot *p_slot = info->p_slot;
  366. struct controller *ctrl = p_slot->ctrl;
  367. mutex_lock(&p_slot->lock);
  368. switch (info->event_type) {
  369. case INT_BUTTON_PRESS:
  370. handle_button_press_event(p_slot);
  371. break;
  372. case INT_POWER_FAULT:
  373. if (!POWER_CTRL(ctrl))
  374. break;
  375. pciehp_set_attention_status(p_slot, 1);
  376. pciehp_green_led_off(p_slot);
  377. break;
  378. case INT_PRESENCE_ON:
  379. case INT_PRESENCE_OFF:
  380. if (!HP_SUPR_RM(ctrl))
  381. break;
  382. ctrl_dbg(ctrl, "Surprise Removal\n");
  383. handle_surprise_event(p_slot);
  384. break;
  385. default:
  386. break;
  387. }
  388. mutex_unlock(&p_slot->lock);
  389. kfree(info);
  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. }
  422. return rc;
  423. }
  424. int pciehp_disable_slot(struct slot *p_slot)
  425. {
  426. u8 getstatus = 0;
  427. struct controller *ctrl = p_slot->ctrl;
  428. if (!p_slot->ctrl)
  429. return 1;
  430. if (!HP_SUPR_RM(p_slot->ctrl)) {
  431. pciehp_get_adapter_status(p_slot, &getstatus);
  432. if (!getstatus) {
  433. ctrl_info(ctrl, "No adapter on slot(%s)\n",
  434. slot_name(p_slot));
  435. return -ENODEV;
  436. }
  437. }
  438. if (MRL_SENS(p_slot->ctrl)) {
  439. pciehp_get_latch_status(p_slot, &getstatus);
  440. if (getstatus) {
  441. ctrl_info(ctrl, "Latch open on slot(%s)\n",
  442. slot_name(p_slot));
  443. return -ENODEV;
  444. }
  445. }
  446. if (POWER_CTRL(p_slot->ctrl)) {
  447. pciehp_get_power_status(p_slot, &getstatus);
  448. if (!getstatus) {
  449. ctrl_info(ctrl, "Already disabled on slot(%s)\n",
  450. slot_name(p_slot));
  451. return -EINVAL;
  452. }
  453. }
  454. return remove_board(p_slot);
  455. }
  456. int pciehp_sysfs_enable_slot(struct slot *p_slot)
  457. {
  458. int retval = -ENODEV;
  459. struct controller *ctrl = p_slot->ctrl;
  460. mutex_lock(&p_slot->lock);
  461. switch (p_slot->state) {
  462. case BLINKINGON_STATE:
  463. cancel_delayed_work(&p_slot->work);
  464. case STATIC_STATE:
  465. p_slot->state = POWERON_STATE;
  466. mutex_unlock(&p_slot->lock);
  467. retval = pciehp_enable_slot(p_slot);
  468. mutex_lock(&p_slot->lock);
  469. p_slot->state = STATIC_STATE;
  470. break;
  471. case POWERON_STATE:
  472. ctrl_info(ctrl, "Slot %s is already in powering on state\n",
  473. slot_name(p_slot));
  474. break;
  475. case BLINKINGOFF_STATE:
  476. case POWEROFF_STATE:
  477. ctrl_info(ctrl, "Already enabled on slot %s\n",
  478. slot_name(p_slot));
  479. break;
  480. default:
  481. ctrl_err(ctrl, "Not a valid state on slot %s\n",
  482. slot_name(p_slot));
  483. break;
  484. }
  485. mutex_unlock(&p_slot->lock);
  486. return retval;
  487. }
  488. int pciehp_sysfs_disable_slot(struct slot *p_slot)
  489. {
  490. int retval = -ENODEV;
  491. struct controller *ctrl = p_slot->ctrl;
  492. mutex_lock(&p_slot->lock);
  493. switch (p_slot->state) {
  494. case BLINKINGOFF_STATE:
  495. cancel_delayed_work(&p_slot->work);
  496. case STATIC_STATE:
  497. p_slot->state = POWEROFF_STATE;
  498. mutex_unlock(&p_slot->lock);
  499. retval = pciehp_disable_slot(p_slot);
  500. mutex_lock(&p_slot->lock);
  501. p_slot->state = STATIC_STATE;
  502. break;
  503. case POWEROFF_STATE:
  504. ctrl_info(ctrl, "Slot %s is already in powering off state\n",
  505. slot_name(p_slot));
  506. break;
  507. case BLINKINGON_STATE:
  508. case POWERON_STATE:
  509. ctrl_info(ctrl, "Already disabled on slot %s\n",
  510. slot_name(p_slot));
  511. break;
  512. default:
  513. ctrl_err(ctrl, "Not a valid state on slot %s\n",
  514. slot_name(p_slot));
  515. break;
  516. }
  517. mutex_unlock(&p_slot->lock);
  518. return retval;
  519. }