shpchp_ctrl.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  1. /*
  2. * Standard 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 "../pci.h"
  35. #include "shpchp.h"
  36. static void interrupt_event_handler(struct controller *ctrl);
  37. static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
  38. static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
  39. static int event_finished;
  40. static unsigned long pushbutton_pending; /* = 0 */
  41. u8 shpchp_handle_attention_button(u8 hp_slot, void *inst_id)
  42. {
  43. struct controller *ctrl = (struct controller *) inst_id;
  44. struct slot *p_slot;
  45. u8 rc = 0;
  46. u8 getstatus;
  47. struct event_info *taskInfo;
  48. /* Attention Button Change */
  49. dbg("shpchp: Attention button interrupt received.\n");
  50. /* This is the structure that tells the worker thread what to do */
  51. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  52. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  53. p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
  54. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  55. ctrl->next_event = (ctrl->next_event + 1) % 10;
  56. taskInfo->hp_slot = hp_slot;
  57. rc++;
  58. /*
  59. * Button pressed - See if need to TAKE ACTION!!!
  60. */
  61. info("Button pressed on Slot(%d)\n", ctrl->first_slot + hp_slot);
  62. taskInfo->event_type = INT_BUTTON_PRESS;
  63. if ((p_slot->state == BLINKINGON_STATE)
  64. || (p_slot->state == BLINKINGOFF_STATE)) {
  65. /* Cancel if we are still blinking; this means that we press the
  66. * attention again before the 5 sec. limit expires to cancel hot-add
  67. * or hot-remove
  68. */
  69. taskInfo->event_type = INT_BUTTON_CANCEL;
  70. info("Button cancel on Slot(%d)\n", ctrl->first_slot + hp_slot);
  71. } else if ((p_slot->state == POWERON_STATE)
  72. || (p_slot->state == POWEROFF_STATE)) {
  73. /* Ignore if the slot is on power-on or power-off state; this
  74. * means that the previous attention button action to hot-add or
  75. * hot-remove is undergoing
  76. */
  77. taskInfo->event_type = INT_BUTTON_IGNORE;
  78. info("Button ignore on Slot(%d)\n", ctrl->first_slot + hp_slot);
  79. }
  80. if (rc)
  81. up(&event_semaphore); /* signal event thread that new event is posted */
  82. return 0;
  83. }
  84. u8 shpchp_handle_switch_change(u8 hp_slot, void *inst_id)
  85. {
  86. struct controller *ctrl = (struct controller *) inst_id;
  87. struct slot *p_slot;
  88. u8 rc = 0;
  89. u8 getstatus;
  90. struct event_info *taskInfo;
  91. /* Switch Change */
  92. dbg("shpchp: Switch interrupt received.\n");
  93. /* This is the structure that tells the worker thread
  94. * what to do
  95. */
  96. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  97. ctrl->next_event = (ctrl->next_event + 1) % 10;
  98. taskInfo->hp_slot = hp_slot;
  99. rc++;
  100. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  101. p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
  102. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  103. dbg("%s: Card present %x Power status %x\n", __FUNCTION__,
  104. p_slot->presence_save, p_slot->pwr_save);
  105. if (getstatus) {
  106. /*
  107. * Switch opened
  108. */
  109. info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
  110. taskInfo->event_type = INT_SWITCH_OPEN;
  111. if (p_slot->pwr_save && p_slot->presence_save) {
  112. taskInfo->event_type = INT_POWER_FAULT;
  113. err("Surprise Removal of card\n");
  114. }
  115. } else {
  116. /*
  117. * Switch closed
  118. */
  119. info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
  120. taskInfo->event_type = INT_SWITCH_CLOSE;
  121. }
  122. if (rc)
  123. up(&event_semaphore); /* signal event thread that new event is posted */
  124. return rc;
  125. }
  126. u8 shpchp_handle_presence_change(u8 hp_slot, void *inst_id)
  127. {
  128. struct controller *ctrl = (struct controller *) inst_id;
  129. struct slot *p_slot;
  130. u8 rc = 0;
  131. /*u8 temp_byte;*/
  132. struct event_info *taskInfo;
  133. /* Presence Change */
  134. dbg("shpchp: Presence/Notify input change.\n");
  135. /* This is the structure that tells the worker thread
  136. * what to do
  137. */
  138. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  139. ctrl->next_event = (ctrl->next_event + 1) % 10;
  140. taskInfo->hp_slot = hp_slot;
  141. rc++;
  142. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  143. /*
  144. * Save the presence state
  145. */
  146. p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
  147. if (p_slot->presence_save) {
  148. /*
  149. * Card Present
  150. */
  151. info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
  152. taskInfo->event_type = INT_PRESENCE_ON;
  153. } else {
  154. /*
  155. * Not Present
  156. */
  157. info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
  158. taskInfo->event_type = INT_PRESENCE_OFF;
  159. }
  160. if (rc)
  161. up(&event_semaphore); /* signal event thread that new event is posted */
  162. return rc;
  163. }
  164. u8 shpchp_handle_power_fault(u8 hp_slot, void *inst_id)
  165. {
  166. struct controller *ctrl = (struct controller *) inst_id;
  167. struct slot *p_slot;
  168. u8 rc = 0;
  169. struct event_info *taskInfo;
  170. /* Power fault */
  171. dbg("shpchp: Power fault interrupt received.\n");
  172. /* This is the structure that tells the worker thread
  173. * what to do
  174. */
  175. taskInfo = &(ctrl->event_queue[ctrl->next_event]);
  176. ctrl->next_event = (ctrl->next_event + 1) % 10;
  177. taskInfo->hp_slot = hp_slot;
  178. rc++;
  179. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  180. if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
  181. /*
  182. * Power fault Cleared
  183. */
  184. info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
  185. p_slot->status = 0x00;
  186. taskInfo->event_type = INT_POWER_FAULT_CLEAR;
  187. } else {
  188. /*
  189. * Power fault
  190. */
  191. info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
  192. taskInfo->event_type = INT_POWER_FAULT;
  193. /* set power fault status for this board */
  194. p_slot->status = 0xFF;
  195. info("power fault bit %x set\n", hp_slot);
  196. }
  197. if (rc)
  198. up(&event_semaphore); /* signal event thread that new event is posted */
  199. return rc;
  200. }
  201. /* The following routines constitute the bulk of the
  202. hotplug controller logic
  203. */
  204. static int change_bus_speed(struct controller *ctrl, struct slot *p_slot,
  205. enum pci_bus_speed speed)
  206. {
  207. int rc = 0;
  208. dbg("%s: change to speed %d\n", __FUNCTION__, speed);
  209. down(&ctrl->crit_sect);
  210. if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, speed))) {
  211. err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
  212. up(&ctrl->crit_sect);
  213. return WRONG_BUS_FREQUENCY;
  214. }
  215. wait_for_ctrl_irq (ctrl);
  216. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  217. err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
  218. __FUNCTION__);
  219. err("%s: Error code (%d)\n", __FUNCTION__, rc);
  220. up(&ctrl->crit_sect);
  221. return WRONG_BUS_FREQUENCY;
  222. }
  223. up(&ctrl->crit_sect);
  224. return rc;
  225. }
  226. static int fix_bus_speed(struct controller *ctrl, struct slot *pslot,
  227. u8 flag, enum pci_bus_speed asp, enum pci_bus_speed bsp,
  228. enum pci_bus_speed msp)
  229. {
  230. int rc = 0;
  231. if (flag != 0) { /* Other slots on the same bus are occupied */
  232. if ( asp < bsp ) {
  233. err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bsp, asp);
  234. return WRONG_BUS_FREQUENCY;
  235. }
  236. } else {
  237. /* Other slots on the same bus are empty */
  238. if (msp == bsp) {
  239. /* if adapter_speed >= bus_speed, do nothing */
  240. if (asp < bsp) {
  241. /*
  242. * Try to lower bus speed to accommodate the adapter if other slots
  243. * on the same controller are empty
  244. */
  245. if ((rc = change_bus_speed(ctrl, pslot, asp)))
  246. return rc;
  247. }
  248. } else {
  249. if (asp < msp) {
  250. if ((rc = change_bus_speed(ctrl, pslot, asp)))
  251. return rc;
  252. } else {
  253. if ((rc = change_bus_speed(ctrl, pslot, msp)))
  254. return rc;
  255. }
  256. }
  257. }
  258. return rc;
  259. }
  260. /**
  261. * board_added - Called after a board has been added to the system.
  262. *
  263. * Turns power on for the board
  264. * Configures board
  265. *
  266. */
  267. static int board_added(struct slot *p_slot)
  268. {
  269. u8 hp_slot;
  270. u8 slots_not_empty = 0;
  271. int rc = 0;
  272. enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
  273. u8 pi, mode;
  274. struct controller *ctrl = p_slot->ctrl;
  275. hp_slot = p_slot->device - ctrl->slot_device_offset;
  276. dbg("%s: p_slot->device, slot_offset, hp_slot = %d, %d ,%d\n",
  277. __FUNCTION__, p_slot->device,
  278. ctrl->slot_device_offset, hp_slot);
  279. /* Wait for exclusive access to hardware */
  280. down(&ctrl->crit_sect);
  281. /* Power on slot without connecting to bus */
  282. rc = p_slot->hpc_ops->power_on_slot(p_slot);
  283. if (rc) {
  284. err("%s: Failed to power on slot\n", __FUNCTION__);
  285. /* Done with exclusive hardware access */
  286. up(&ctrl->crit_sect);
  287. return -1;
  288. }
  289. /* Wait for the command to complete */
  290. wait_for_ctrl_irq (ctrl);
  291. rc = p_slot->hpc_ops->check_cmd_status(ctrl);
  292. if (rc) {
  293. err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
  294. /* Done with exclusive hardware access */
  295. up(&ctrl->crit_sect);
  296. return -1;
  297. }
  298. if ((ctrl->pci_dev->vendor == 0x8086) && (ctrl->pci_dev->device == 0x0332)) {
  299. if (slots_not_empty)
  300. return WRONG_BUS_FREQUENCY;
  301. if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, PCI_SPEED_33MHz))) {
  302. err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
  303. up(&ctrl->crit_sect);
  304. return WRONG_BUS_FREQUENCY;
  305. }
  306. wait_for_ctrl_irq (ctrl);
  307. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  308. err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
  309. __FUNCTION__);
  310. err("%s: Error code (%d)\n", __FUNCTION__, rc);
  311. up(&ctrl->crit_sect);
  312. return WRONG_BUS_FREQUENCY;
  313. }
  314. /* turn on board, blink green LED, turn off Amber LED */
  315. if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
  316. err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
  317. up(&ctrl->crit_sect);
  318. return rc;
  319. }
  320. wait_for_ctrl_irq (ctrl);
  321. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  322. err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
  323. up(&ctrl->crit_sect);
  324. return rc;
  325. }
  326. }
  327. rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
  328. /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
  329. /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
  330. /* 0xd = PCI-X 133 Mhz 533 */
  331. /* This encoding is different from the one used in cur_bus_speed & */
  332. /* max_bus_speed */
  333. if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
  334. err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
  335. /* Done with exclusive hardware access */
  336. up(&ctrl->crit_sect);
  337. return WRONG_BUS_FREQUENCY;
  338. }
  339. rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
  340. if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
  341. err("%s: Can't get bus operation speed\n", __FUNCTION__);
  342. /* Done with exclusive hardware access */
  343. up(&ctrl->crit_sect);
  344. return WRONG_BUS_FREQUENCY;
  345. }
  346. rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
  347. if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
  348. err("%s: Can't get max bus operation speed\n", __FUNCTION__);
  349. max_bus_speed = bus_speed;
  350. }
  351. /* Done with exclusive hardware access */
  352. up(&ctrl->crit_sect);
  353. if ((rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi))) {
  354. err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
  355. pi = 1;
  356. }
  357. /* Check if there are other slots or devices on the same bus */
  358. if (!list_empty(&ctrl->pci_dev->subordinate->devices))
  359. slots_not_empty = 1;
  360. dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__,
  361. slots_not_empty, pi);
  362. dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
  363. adapter_speed, bus_speed, max_bus_speed);
  364. if (pi == 2) {
  365. dbg("%s: In PI = %d\n", __FUNCTION__, pi);
  366. if ((rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode))) {
  367. err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__);
  368. mode = 0;
  369. }
  370. switch (adapter_speed) {
  371. case PCI_SPEED_133MHz_PCIX_533:
  372. case PCI_SPEED_133MHz_PCIX_266:
  373. if ((bus_speed != adapter_speed) &&
  374. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  375. return rc;
  376. break;
  377. case PCI_SPEED_133MHz_PCIX_ECC:
  378. case PCI_SPEED_133MHz_PCIX:
  379. if (mode) { /* Bus - Mode 1 ECC */
  380. if ((bus_speed != 0x7) &&
  381. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  382. return rc;
  383. } else {
  384. if ((bus_speed != 0x4) &&
  385. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  386. return rc;
  387. }
  388. break;
  389. case PCI_SPEED_66MHz_PCIX_ECC:
  390. case PCI_SPEED_66MHz_PCIX:
  391. if (mode) { /* Bus - Mode 1 ECC */
  392. if ((bus_speed != 0x5) &&
  393. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  394. return rc;
  395. } else {
  396. if ((bus_speed != 0x2) &&
  397. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  398. return rc;
  399. }
  400. break;
  401. case PCI_SPEED_66MHz:
  402. if ((bus_speed != 0x1) &&
  403. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  404. return rc;
  405. break;
  406. case PCI_SPEED_33MHz:
  407. if (bus_speed > 0x0) {
  408. if (slots_not_empty == 0) {
  409. if ((rc = change_bus_speed(ctrl, p_slot, adapter_speed)))
  410. return rc;
  411. } else {
  412. err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
  413. return WRONG_BUS_FREQUENCY;
  414. }
  415. }
  416. break;
  417. default:
  418. err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
  419. return WRONG_BUS_FREQUENCY;
  420. }
  421. } else {
  422. /* If adpater_speed == bus_speed, nothing to do here */
  423. dbg("%s: In PI = %d\n", __FUNCTION__, pi);
  424. if ((adapter_speed != bus_speed) &&
  425. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  426. return rc;
  427. }
  428. down(&ctrl->crit_sect);
  429. /* turn on board, blink green LED, turn off Amber LED */
  430. if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
  431. err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
  432. up(&ctrl->crit_sect);
  433. return rc;
  434. }
  435. wait_for_ctrl_irq (ctrl);
  436. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  437. err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
  438. up(&ctrl->crit_sect);
  439. return rc;
  440. }
  441. up(&ctrl->crit_sect);
  442. /* Wait for ~1 second */
  443. wait_for_ctrl_irq (ctrl);
  444. dbg("%s: slot status = %x\n", __FUNCTION__, p_slot->status);
  445. /* Check for a power fault */
  446. if (p_slot->status == 0xFF) {
  447. /* power fault occurred, but it was benign */
  448. dbg("%s: power fault\n", __FUNCTION__);
  449. rc = POWER_FAILURE;
  450. p_slot->status = 0;
  451. goto err_exit;
  452. }
  453. if (shpchp_configure_device(p_slot)) {
  454. err("Cannot add device at 0x%x:0x%x\n", p_slot->bus,
  455. p_slot->device);
  456. goto err_exit;
  457. }
  458. p_slot->status = 0;
  459. p_slot->is_a_board = 0x01;
  460. p_slot->pwr_save = 1;
  461. /* Wait for exclusive access to hardware */
  462. down(&ctrl->crit_sect);
  463. p_slot->hpc_ops->green_led_on(p_slot);
  464. /* Wait for the command to complete */
  465. wait_for_ctrl_irq (ctrl);
  466. /* Done with exclusive hardware access */
  467. up(&ctrl->crit_sect);
  468. return 0;
  469. err_exit:
  470. /* Wait for exclusive access to hardware */
  471. down(&ctrl->crit_sect);
  472. /* turn off slot, turn on Amber LED, turn off Green LED */
  473. rc = p_slot->hpc_ops->slot_disable(p_slot);
  474. if (rc) {
  475. err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
  476. /* Done with exclusive hardware access */
  477. up(&ctrl->crit_sect);
  478. return rc;
  479. }
  480. /* Wait for the command to complete */
  481. wait_for_ctrl_irq (ctrl);
  482. rc = p_slot->hpc_ops->check_cmd_status(ctrl);
  483. if (rc) {
  484. err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
  485. /* Done with exclusive hardware access */
  486. up(&ctrl->crit_sect);
  487. return rc;
  488. }
  489. /* Done with exclusive hardware access */
  490. up(&ctrl->crit_sect);
  491. return(rc);
  492. }
  493. /**
  494. * remove_board - Turns off slot and LED's
  495. *
  496. */
  497. static int remove_board(struct slot *p_slot)
  498. {
  499. struct controller *ctrl = p_slot->ctrl;
  500. u8 hp_slot;
  501. int rc;
  502. if (shpchp_unconfigure_device(p_slot))
  503. return(1);
  504. hp_slot = p_slot->device - ctrl->slot_device_offset;
  505. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  506. dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
  507. /* Change status to shutdown */
  508. if (p_slot->is_a_board)
  509. p_slot->status = 0x01;
  510. /* Wait for exclusive access to hardware */
  511. down(&ctrl->crit_sect);
  512. /* turn off slot, turn on Amber LED, turn off Green LED */
  513. rc = p_slot->hpc_ops->slot_disable(p_slot);
  514. if (rc) {
  515. err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
  516. /* Done with exclusive hardware access */
  517. up(&ctrl->crit_sect);
  518. return rc;
  519. }
  520. /* Wait for the command to complete */
  521. wait_for_ctrl_irq (ctrl);
  522. rc = p_slot->hpc_ops->check_cmd_status(ctrl);
  523. if (rc) {
  524. err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
  525. /* Done with exclusive hardware access */
  526. up(&ctrl->crit_sect);
  527. return rc;
  528. }
  529. rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
  530. if (rc) {
  531. err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
  532. /* Done with exclusive hardware access */
  533. up(&ctrl->crit_sect);
  534. return rc;
  535. }
  536. /* Wait for the command to complete */
  537. wait_for_ctrl_irq (ctrl);
  538. /* Done with exclusive hardware access */
  539. up(&ctrl->crit_sect);
  540. p_slot->pwr_save = 0;
  541. p_slot->is_a_board = 0;
  542. return 0;
  543. }
  544. static void pushbutton_helper_thread (unsigned long data)
  545. {
  546. pushbutton_pending = data;
  547. up(&event_semaphore);
  548. }
  549. /**
  550. * shpchp_pushbutton_thread
  551. *
  552. * Scheduled procedure to handle blocking stuff for the pushbuttons
  553. * Handles all pending events and exits.
  554. *
  555. */
  556. static void shpchp_pushbutton_thread (unsigned long slot)
  557. {
  558. struct slot *p_slot = (struct slot *) slot;
  559. u8 getstatus;
  560. pushbutton_pending = 0;
  561. if (!p_slot) {
  562. dbg("%s: Error! slot NULL\n", __FUNCTION__);
  563. return;
  564. }
  565. p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  566. if (getstatus) {
  567. p_slot->state = POWEROFF_STATE;
  568. shpchp_disable_slot(p_slot);
  569. p_slot->state = STATIC_STATE;
  570. } else {
  571. p_slot->state = POWERON_STATE;
  572. if (shpchp_enable_slot(p_slot)) {
  573. /* Wait for exclusive access to hardware */
  574. down(&p_slot->ctrl->crit_sect);
  575. p_slot->hpc_ops->green_led_off(p_slot);
  576. /* Wait for the command to complete */
  577. wait_for_ctrl_irq (p_slot->ctrl);
  578. /* Done with exclusive hardware access */
  579. up(&p_slot->ctrl->crit_sect);
  580. }
  581. p_slot->state = STATIC_STATE;
  582. }
  583. return;
  584. }
  585. /* this is the main worker thread */
  586. static int event_thread(void* data)
  587. {
  588. struct controller *ctrl;
  589. lock_kernel();
  590. daemonize("shpchpd_event");
  591. unlock_kernel();
  592. while (1) {
  593. dbg("!!!!event_thread sleeping\n");
  594. down_interruptible (&event_semaphore);
  595. dbg("event_thread woken finished = %d\n", event_finished);
  596. if (event_finished || signal_pending(current))
  597. break;
  598. /* Do stuff here */
  599. if (pushbutton_pending)
  600. shpchp_pushbutton_thread(pushbutton_pending);
  601. else
  602. for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
  603. interrupt_event_handler(ctrl);
  604. }
  605. dbg("event_thread signals exit\n");
  606. up(&event_exit);
  607. return 0;
  608. }
  609. int shpchp_event_start_thread (void)
  610. {
  611. int pid;
  612. /* initialize our semaphores */
  613. init_MUTEX_LOCKED(&event_exit);
  614. event_finished=0;
  615. init_MUTEX_LOCKED(&event_semaphore);
  616. pid = kernel_thread(event_thread, NULL, 0);
  617. if (pid < 0) {
  618. err ("Can't start up our event thread\n");
  619. return -1;
  620. }
  621. return 0;
  622. }
  623. void shpchp_event_stop_thread (void)
  624. {
  625. event_finished = 1;
  626. up(&event_semaphore);
  627. down(&event_exit);
  628. }
  629. static int update_slot_info (struct slot *slot)
  630. {
  631. struct hotplug_slot_info *info;
  632. int result;
  633. info = kmalloc(sizeof(*info), GFP_KERNEL);
  634. if (!info)
  635. return -ENOMEM;
  636. slot->hpc_ops->get_power_status(slot, &(info->power_status));
  637. slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
  638. slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
  639. slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
  640. result = pci_hp_change_slot_info(slot->hotplug_slot, info);
  641. kfree (info);
  642. return result;
  643. }
  644. static void interrupt_event_handler(struct controller *ctrl)
  645. {
  646. int loop = 0;
  647. int change = 1;
  648. u8 hp_slot;
  649. u8 getstatus;
  650. struct slot *p_slot;
  651. while (change) {
  652. change = 0;
  653. for (loop = 0; loop < 10; loop++) {
  654. if (ctrl->event_queue[loop].event_type != 0) {
  655. dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
  656. ctrl->event_queue[loop].event_type);
  657. hp_slot = ctrl->event_queue[loop].hp_slot;
  658. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  659. if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
  660. dbg("%s: button cancel\n", __FUNCTION__);
  661. del_timer(&p_slot->task_event);
  662. switch (p_slot->state) {
  663. case BLINKINGOFF_STATE:
  664. /* Wait for exclusive access to hardware */
  665. down(&ctrl->crit_sect);
  666. p_slot->hpc_ops->green_led_on(p_slot);
  667. /* Wait for the command to complete */
  668. wait_for_ctrl_irq (ctrl);
  669. p_slot->hpc_ops->set_attention_status(p_slot, 0);
  670. /* Wait for the command to complete */
  671. wait_for_ctrl_irq (ctrl);
  672. /* Done with exclusive hardware access */
  673. up(&ctrl->crit_sect);
  674. break;
  675. case BLINKINGON_STATE:
  676. /* Wait for exclusive access to hardware */
  677. down(&ctrl->crit_sect);
  678. p_slot->hpc_ops->green_led_off(p_slot);
  679. /* Wait for the command to complete */
  680. wait_for_ctrl_irq (ctrl);
  681. p_slot->hpc_ops->set_attention_status(p_slot, 0);
  682. /* Wait for the command to complete */
  683. wait_for_ctrl_irq (ctrl);
  684. /* Done with exclusive hardware access */
  685. up(&ctrl->crit_sect);
  686. break;
  687. default:
  688. warn("Not a valid state\n");
  689. return;
  690. }
  691. info(msg_button_cancel, p_slot->number);
  692. p_slot->state = STATIC_STATE;
  693. } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
  694. /* Button Pressed (No action on 1st press...) */
  695. dbg("%s: Button pressed\n", __FUNCTION__);
  696. p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  697. if (getstatus) {
  698. /* slot is on */
  699. dbg("%s: slot is on\n", __FUNCTION__);
  700. p_slot->state = BLINKINGOFF_STATE;
  701. info(msg_button_off, p_slot->number);
  702. } else {
  703. /* slot is off */
  704. dbg("%s: slot is off\n", __FUNCTION__);
  705. p_slot->state = BLINKINGON_STATE;
  706. info(msg_button_on, p_slot->number);
  707. }
  708. /* Wait for exclusive access to hardware */
  709. down(&ctrl->crit_sect);
  710. /* blink green LED and turn off amber */
  711. p_slot->hpc_ops->green_led_blink(p_slot);
  712. /* Wait for the command to complete */
  713. wait_for_ctrl_irq (ctrl);
  714. p_slot->hpc_ops->set_attention_status(p_slot, 0);
  715. /* Wait for the command to complete */
  716. wait_for_ctrl_irq (ctrl);
  717. /* Done with exclusive hardware access */
  718. up(&ctrl->crit_sect);
  719. init_timer(&p_slot->task_event);
  720. p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
  721. p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
  722. p_slot->task_event.data = (unsigned long) p_slot;
  723. dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
  724. add_timer(&p_slot->task_event);
  725. } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
  726. /***********POWER FAULT********************/
  727. dbg("%s: power fault\n", __FUNCTION__);
  728. /* Wait for exclusive access to hardware */
  729. down(&ctrl->crit_sect);
  730. p_slot->hpc_ops->set_attention_status(p_slot, 1);
  731. /* Wait for the command to complete */
  732. wait_for_ctrl_irq (ctrl);
  733. p_slot->hpc_ops->green_led_off(p_slot);
  734. /* Wait for the command to complete */
  735. wait_for_ctrl_irq (ctrl);
  736. /* Done with exclusive hardware access */
  737. up(&ctrl->crit_sect);
  738. } else {
  739. /* refresh notification */
  740. if (p_slot)
  741. update_slot_info(p_slot);
  742. }
  743. ctrl->event_queue[loop].event_type = 0;
  744. change = 1;
  745. }
  746. } /* End of FOR loop */
  747. }
  748. return;
  749. }
  750. int shpchp_enable_slot (struct slot *p_slot)
  751. {
  752. u8 getstatus = 0;
  753. int rc;
  754. /* Check to see if (latch closed, card present, power off) */
  755. down(&p_slot->ctrl->crit_sect);
  756. rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
  757. if (rc || !getstatus) {
  758. info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
  759. up(&p_slot->ctrl->crit_sect);
  760. return -ENODEV;
  761. }
  762. rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  763. if (rc || getstatus) {
  764. info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
  765. up(&p_slot->ctrl->crit_sect);
  766. return -ENODEV;
  767. }
  768. rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  769. if (rc || getstatus) {
  770. info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
  771. up(&p_slot->ctrl->crit_sect);
  772. return -ENODEV;
  773. }
  774. up(&p_slot->ctrl->crit_sect);
  775. p_slot->is_a_board = 1;
  776. /* We have to save the presence info for these slots */
  777. p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
  778. p_slot->hpc_ops->get_power_status(p_slot, &(p_slot->pwr_save));
  779. dbg("%s: p_slot->pwr_save %x\n", __FUNCTION__, p_slot->pwr_save);
  780. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  781. rc = board_added(p_slot);
  782. if (rc) {
  783. p_slot->hpc_ops->get_adapter_status(p_slot,
  784. &(p_slot->presence_save));
  785. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  786. }
  787. update_slot_info(p_slot);
  788. return rc;
  789. }
  790. int shpchp_disable_slot (struct slot *p_slot)
  791. {
  792. u8 getstatus = 0;
  793. int ret = 0;
  794. if (!p_slot->ctrl)
  795. return -ENODEV;
  796. /* Check to see if (latch closed, card present, power on) */
  797. down(&p_slot->ctrl->crit_sect);
  798. ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
  799. if (ret || !getstatus) {
  800. info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
  801. up(&p_slot->ctrl->crit_sect);
  802. return -ENODEV;
  803. }
  804. ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  805. if (ret || getstatus) {
  806. info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
  807. up(&p_slot->ctrl->crit_sect);
  808. return -ENODEV;
  809. }
  810. ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  811. if (ret || !getstatus) {
  812. info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
  813. up(&p_slot->ctrl->crit_sect);
  814. return -ENODEV;
  815. }
  816. up(&p_slot->ctrl->crit_sect);
  817. ret = remove_board(p_slot);
  818. update_slot_info(p_slot);
  819. return ret;
  820. }