shpchp_ctrl.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  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. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  216. err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
  217. __FUNCTION__);
  218. err("%s: Error code (%d)\n", __FUNCTION__, rc);
  219. up(&ctrl->crit_sect);
  220. return WRONG_BUS_FREQUENCY;
  221. }
  222. up(&ctrl->crit_sect);
  223. return rc;
  224. }
  225. static int fix_bus_speed(struct controller *ctrl, struct slot *pslot,
  226. u8 flag, enum pci_bus_speed asp, enum pci_bus_speed bsp,
  227. enum pci_bus_speed msp)
  228. {
  229. int rc = 0;
  230. if (flag != 0) { /* Other slots on the same bus are occupied */
  231. if ( asp < bsp ) {
  232. err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bsp, asp);
  233. return WRONG_BUS_FREQUENCY;
  234. }
  235. } else {
  236. /* Other slots on the same bus are empty */
  237. if (msp == bsp) {
  238. /* if adapter_speed >= bus_speed, do nothing */
  239. if (asp < bsp) {
  240. /*
  241. * Try to lower bus speed to accommodate the adapter if other slots
  242. * on the same controller are empty
  243. */
  244. if ((rc = change_bus_speed(ctrl, pslot, asp)))
  245. return rc;
  246. }
  247. } else {
  248. if (asp < msp) {
  249. if ((rc = change_bus_speed(ctrl, pslot, asp)))
  250. return rc;
  251. } else {
  252. if ((rc = change_bus_speed(ctrl, pslot, msp)))
  253. return rc;
  254. }
  255. }
  256. }
  257. return rc;
  258. }
  259. /**
  260. * board_added - Called after a board has been added to the system.
  261. *
  262. * Turns power on for the board
  263. * Configures board
  264. *
  265. */
  266. static int board_added(struct slot *p_slot)
  267. {
  268. u8 hp_slot;
  269. u8 slots_not_empty = 0;
  270. int rc = 0;
  271. enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
  272. u8 pi, mode;
  273. struct controller *ctrl = p_slot->ctrl;
  274. hp_slot = p_slot->device - ctrl->slot_device_offset;
  275. dbg("%s: p_slot->device, slot_offset, hp_slot = %d, %d ,%d\n",
  276. __FUNCTION__, p_slot->device,
  277. ctrl->slot_device_offset, hp_slot);
  278. /* Wait for exclusive access to hardware */
  279. down(&ctrl->crit_sect);
  280. /* Power on slot without connecting to bus */
  281. rc = p_slot->hpc_ops->power_on_slot(p_slot);
  282. if (rc) {
  283. err("%s: Failed to power on slot\n", __FUNCTION__);
  284. /* Done with exclusive hardware access */
  285. up(&ctrl->crit_sect);
  286. return -1;
  287. }
  288. rc = p_slot->hpc_ops->check_cmd_status(ctrl);
  289. if (rc) {
  290. err("%s: Failed to power on slot, error code(%d)\n", __FUNCTION__, rc);
  291. /* Done with exclusive hardware access */
  292. up(&ctrl->crit_sect);
  293. return -1;
  294. }
  295. if ((ctrl->pci_dev->vendor == 0x8086) && (ctrl->pci_dev->device == 0x0332)) {
  296. if (slots_not_empty)
  297. return WRONG_BUS_FREQUENCY;
  298. if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, PCI_SPEED_33MHz))) {
  299. err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
  300. up(&ctrl->crit_sect);
  301. return WRONG_BUS_FREQUENCY;
  302. }
  303. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  304. err("%s: Can't set bus speed/mode in the case of adapter & bus mismatch\n",
  305. __FUNCTION__);
  306. err("%s: Error code (%d)\n", __FUNCTION__, rc);
  307. up(&ctrl->crit_sect);
  308. return WRONG_BUS_FREQUENCY;
  309. }
  310. /* turn on board, blink green LED, turn off Amber LED */
  311. if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
  312. err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
  313. up(&ctrl->crit_sect);
  314. return rc;
  315. }
  316. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  317. err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
  318. up(&ctrl->crit_sect);
  319. return rc;
  320. }
  321. }
  322. rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
  323. /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
  324. /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
  325. /* 0xd = PCI-X 133 Mhz 533 */
  326. /* This encoding is different from the one used in cur_bus_speed & */
  327. /* max_bus_speed */
  328. if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
  329. err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
  330. /* Done with exclusive hardware access */
  331. up(&ctrl->crit_sect);
  332. return WRONG_BUS_FREQUENCY;
  333. }
  334. rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
  335. if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
  336. err("%s: Can't get bus operation speed\n", __FUNCTION__);
  337. /* Done with exclusive hardware access */
  338. up(&ctrl->crit_sect);
  339. return WRONG_BUS_FREQUENCY;
  340. }
  341. rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
  342. if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
  343. err("%s: Can't get max bus operation speed\n", __FUNCTION__);
  344. max_bus_speed = bus_speed;
  345. }
  346. /* Done with exclusive hardware access */
  347. up(&ctrl->crit_sect);
  348. if ((rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi))) {
  349. err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
  350. pi = 1;
  351. }
  352. /* Check if there are other slots or devices on the same bus */
  353. if (!list_empty(&ctrl->pci_dev->subordinate->devices))
  354. slots_not_empty = 1;
  355. dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__,
  356. slots_not_empty, pi);
  357. dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
  358. adapter_speed, bus_speed, max_bus_speed);
  359. if (pi == 2) {
  360. dbg("%s: In PI = %d\n", __FUNCTION__, pi);
  361. if ((rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode))) {
  362. err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__);
  363. mode = 0;
  364. }
  365. switch (adapter_speed) {
  366. case PCI_SPEED_133MHz_PCIX_533:
  367. case PCI_SPEED_133MHz_PCIX_266:
  368. if ((bus_speed != adapter_speed) &&
  369. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  370. return rc;
  371. break;
  372. case PCI_SPEED_133MHz_PCIX_ECC:
  373. case PCI_SPEED_133MHz_PCIX:
  374. if (mode) { /* Bus - Mode 1 ECC */
  375. if ((bus_speed != 0x7) &&
  376. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  377. return rc;
  378. } else {
  379. if ((bus_speed != 0x4) &&
  380. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  381. return rc;
  382. }
  383. break;
  384. case PCI_SPEED_66MHz_PCIX_ECC:
  385. case PCI_SPEED_66MHz_PCIX:
  386. if (mode) { /* Bus - Mode 1 ECC */
  387. if ((bus_speed != 0x5) &&
  388. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  389. return rc;
  390. } else {
  391. if ((bus_speed != 0x2) &&
  392. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  393. return rc;
  394. }
  395. break;
  396. case PCI_SPEED_66MHz:
  397. if ((bus_speed != 0x1) &&
  398. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  399. return rc;
  400. break;
  401. case PCI_SPEED_33MHz:
  402. if (bus_speed > 0x0) {
  403. if (slots_not_empty == 0) {
  404. if ((rc = change_bus_speed(ctrl, p_slot, adapter_speed)))
  405. return rc;
  406. } else {
  407. err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
  408. return WRONG_BUS_FREQUENCY;
  409. }
  410. }
  411. break;
  412. default:
  413. err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
  414. return WRONG_BUS_FREQUENCY;
  415. }
  416. } else {
  417. /* If adpater_speed == bus_speed, nothing to do here */
  418. dbg("%s: In PI = %d\n", __FUNCTION__, pi);
  419. if ((adapter_speed != bus_speed) &&
  420. ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
  421. return rc;
  422. }
  423. down(&ctrl->crit_sect);
  424. /* turn on board, blink green LED, turn off Amber LED */
  425. if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
  426. err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
  427. up(&ctrl->crit_sect);
  428. return rc;
  429. }
  430. if ((rc = p_slot->hpc_ops->check_cmd_status(ctrl))) {
  431. err("%s: Failed to enable slot, error code(%d)\n", __FUNCTION__, rc);
  432. up(&ctrl->crit_sect);
  433. return rc;
  434. }
  435. up(&ctrl->crit_sect);
  436. /* Wait for ~1 second */
  437. wait_for_ctrl_irq (ctrl);
  438. dbg("%s: slot status = %x\n", __FUNCTION__, p_slot->status);
  439. /* Check for a power fault */
  440. if (p_slot->status == 0xFF) {
  441. /* power fault occurred, but it was benign */
  442. dbg("%s: power fault\n", __FUNCTION__);
  443. rc = POWER_FAILURE;
  444. p_slot->status = 0;
  445. goto err_exit;
  446. }
  447. if (shpchp_configure_device(p_slot)) {
  448. err("Cannot add device at 0x%x:0x%x\n", p_slot->bus,
  449. p_slot->device);
  450. goto err_exit;
  451. }
  452. p_slot->status = 0;
  453. p_slot->is_a_board = 0x01;
  454. p_slot->pwr_save = 1;
  455. /* Wait for exclusive access to hardware */
  456. down(&ctrl->crit_sect);
  457. p_slot->hpc_ops->green_led_on(p_slot);
  458. /* Done with exclusive hardware access */
  459. up(&ctrl->crit_sect);
  460. return 0;
  461. err_exit:
  462. /* Wait for exclusive access to hardware */
  463. down(&ctrl->crit_sect);
  464. /* turn off slot, turn on Amber LED, turn off Green LED */
  465. rc = p_slot->hpc_ops->slot_disable(p_slot);
  466. if (rc) {
  467. err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
  468. /* Done with exclusive hardware access */
  469. up(&ctrl->crit_sect);
  470. return rc;
  471. }
  472. rc = p_slot->hpc_ops->check_cmd_status(ctrl);
  473. if (rc) {
  474. err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
  475. /* Done with exclusive hardware access */
  476. up(&ctrl->crit_sect);
  477. return rc;
  478. }
  479. /* Done with exclusive hardware access */
  480. up(&ctrl->crit_sect);
  481. return(rc);
  482. }
  483. /**
  484. * remove_board - Turns off slot and LED's
  485. *
  486. */
  487. static int remove_board(struct slot *p_slot)
  488. {
  489. struct controller *ctrl = p_slot->ctrl;
  490. u8 hp_slot;
  491. int rc;
  492. if (shpchp_unconfigure_device(p_slot))
  493. return(1);
  494. hp_slot = p_slot->device - ctrl->slot_device_offset;
  495. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  496. dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
  497. /* Change status to shutdown */
  498. if (p_slot->is_a_board)
  499. p_slot->status = 0x01;
  500. /* Wait for exclusive access to hardware */
  501. down(&ctrl->crit_sect);
  502. /* turn off slot, turn on Amber LED, turn off Green LED */
  503. rc = p_slot->hpc_ops->slot_disable(p_slot);
  504. if (rc) {
  505. err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
  506. /* Done with exclusive hardware access */
  507. up(&ctrl->crit_sect);
  508. return rc;
  509. }
  510. rc = p_slot->hpc_ops->check_cmd_status(ctrl);
  511. if (rc) {
  512. err("%s: Failed to disable slot, error code(%d)\n", __FUNCTION__, rc);
  513. /* Done with exclusive hardware access */
  514. up(&ctrl->crit_sect);
  515. return rc;
  516. }
  517. rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
  518. if (rc) {
  519. err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
  520. /* Done with exclusive hardware access */
  521. up(&ctrl->crit_sect);
  522. return rc;
  523. }
  524. /* Done with exclusive hardware access */
  525. up(&ctrl->crit_sect);
  526. p_slot->pwr_save = 0;
  527. p_slot->is_a_board = 0;
  528. return 0;
  529. }
  530. static void pushbutton_helper_thread (unsigned long data)
  531. {
  532. pushbutton_pending = data;
  533. up(&event_semaphore);
  534. }
  535. /**
  536. * shpchp_pushbutton_thread
  537. *
  538. * Scheduled procedure to handle blocking stuff for the pushbuttons
  539. * Handles all pending events and exits.
  540. *
  541. */
  542. static void shpchp_pushbutton_thread (unsigned long slot)
  543. {
  544. struct slot *p_slot = (struct slot *) slot;
  545. u8 getstatus;
  546. pushbutton_pending = 0;
  547. if (!p_slot) {
  548. dbg("%s: Error! slot NULL\n", __FUNCTION__);
  549. return;
  550. }
  551. p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  552. if (getstatus) {
  553. p_slot->state = POWEROFF_STATE;
  554. shpchp_disable_slot(p_slot);
  555. p_slot->state = STATIC_STATE;
  556. } else {
  557. p_slot->state = POWERON_STATE;
  558. if (shpchp_enable_slot(p_slot)) {
  559. /* Wait for exclusive access to hardware */
  560. down(&p_slot->ctrl->crit_sect);
  561. p_slot->hpc_ops->green_led_off(p_slot);
  562. /* Done with exclusive hardware access */
  563. up(&p_slot->ctrl->crit_sect);
  564. }
  565. p_slot->state = STATIC_STATE;
  566. }
  567. return;
  568. }
  569. /* this is the main worker thread */
  570. static int event_thread(void* data)
  571. {
  572. struct controller *ctrl;
  573. lock_kernel();
  574. daemonize("shpchpd_event");
  575. unlock_kernel();
  576. while (1) {
  577. dbg("!!!!event_thread sleeping\n");
  578. down_interruptible (&event_semaphore);
  579. dbg("event_thread woken finished = %d\n", event_finished);
  580. if (event_finished || signal_pending(current))
  581. break;
  582. /* Do stuff here */
  583. if (pushbutton_pending)
  584. shpchp_pushbutton_thread(pushbutton_pending);
  585. else
  586. for (ctrl = shpchp_ctrl_list; ctrl; ctrl=ctrl->next)
  587. interrupt_event_handler(ctrl);
  588. }
  589. dbg("event_thread signals exit\n");
  590. up(&event_exit);
  591. return 0;
  592. }
  593. int shpchp_event_start_thread (void)
  594. {
  595. int pid;
  596. /* initialize our semaphores */
  597. init_MUTEX_LOCKED(&event_exit);
  598. event_finished=0;
  599. init_MUTEX_LOCKED(&event_semaphore);
  600. pid = kernel_thread(event_thread, NULL, 0);
  601. if (pid < 0) {
  602. err ("Can't start up our event thread\n");
  603. return -1;
  604. }
  605. return 0;
  606. }
  607. void shpchp_event_stop_thread (void)
  608. {
  609. event_finished = 1;
  610. up(&event_semaphore);
  611. down(&event_exit);
  612. }
  613. static int update_slot_info (struct slot *slot)
  614. {
  615. struct hotplug_slot_info *info;
  616. int result;
  617. info = kmalloc(sizeof(*info), GFP_KERNEL);
  618. if (!info)
  619. return -ENOMEM;
  620. slot->hpc_ops->get_power_status(slot, &(info->power_status));
  621. slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
  622. slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
  623. slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
  624. result = pci_hp_change_slot_info(slot->hotplug_slot, info);
  625. kfree (info);
  626. return result;
  627. }
  628. static void interrupt_event_handler(struct controller *ctrl)
  629. {
  630. int loop = 0;
  631. int change = 1;
  632. u8 hp_slot;
  633. u8 getstatus;
  634. struct slot *p_slot;
  635. while (change) {
  636. change = 0;
  637. for (loop = 0; loop < 10; loop++) {
  638. if (ctrl->event_queue[loop].event_type != 0) {
  639. dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
  640. ctrl->event_queue[loop].event_type);
  641. hp_slot = ctrl->event_queue[loop].hp_slot;
  642. p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
  643. if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
  644. dbg("%s: button cancel\n", __FUNCTION__);
  645. del_timer(&p_slot->task_event);
  646. switch (p_slot->state) {
  647. case BLINKINGOFF_STATE:
  648. /* Wait for exclusive access to hardware */
  649. down(&ctrl->crit_sect);
  650. p_slot->hpc_ops->green_led_on(p_slot);
  651. p_slot->hpc_ops->set_attention_status(p_slot, 0);
  652. /* Done with exclusive hardware access */
  653. up(&ctrl->crit_sect);
  654. break;
  655. case BLINKINGON_STATE:
  656. /* Wait for exclusive access to hardware */
  657. down(&ctrl->crit_sect);
  658. p_slot->hpc_ops->green_led_off(p_slot);
  659. p_slot->hpc_ops->set_attention_status(p_slot, 0);
  660. /* Done with exclusive hardware access */
  661. up(&ctrl->crit_sect);
  662. break;
  663. default:
  664. warn("Not a valid state\n");
  665. return;
  666. }
  667. info(msg_button_cancel, p_slot->number);
  668. p_slot->state = STATIC_STATE;
  669. } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
  670. /* Button Pressed (No action on 1st press...) */
  671. dbg("%s: Button pressed\n", __FUNCTION__);
  672. p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  673. if (getstatus) {
  674. /* slot is on */
  675. dbg("%s: slot is on\n", __FUNCTION__);
  676. p_slot->state = BLINKINGOFF_STATE;
  677. info(msg_button_off, p_slot->number);
  678. } else {
  679. /* slot is off */
  680. dbg("%s: slot is off\n", __FUNCTION__);
  681. p_slot->state = BLINKINGON_STATE;
  682. info(msg_button_on, p_slot->number);
  683. }
  684. /* Wait for exclusive access to hardware */
  685. down(&ctrl->crit_sect);
  686. /* blink green LED and turn off amber */
  687. p_slot->hpc_ops->green_led_blink(p_slot);
  688. p_slot->hpc_ops->set_attention_status(p_slot, 0);
  689. /* Done with exclusive hardware access */
  690. up(&ctrl->crit_sect);
  691. init_timer(&p_slot->task_event);
  692. p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
  693. p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
  694. p_slot->task_event.data = (unsigned long) p_slot;
  695. dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
  696. add_timer(&p_slot->task_event);
  697. } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
  698. /***********POWER FAULT********************/
  699. dbg("%s: power fault\n", __FUNCTION__);
  700. /* Wait for exclusive access to hardware */
  701. down(&ctrl->crit_sect);
  702. p_slot->hpc_ops->set_attention_status(p_slot, 1);
  703. p_slot->hpc_ops->green_led_off(p_slot);
  704. /* Done with exclusive hardware access */
  705. up(&ctrl->crit_sect);
  706. } else {
  707. /* refresh notification */
  708. if (p_slot)
  709. update_slot_info(p_slot);
  710. }
  711. ctrl->event_queue[loop].event_type = 0;
  712. change = 1;
  713. }
  714. } /* End of FOR loop */
  715. }
  716. return;
  717. }
  718. int shpchp_enable_slot (struct slot *p_slot)
  719. {
  720. u8 getstatus = 0;
  721. int rc;
  722. /* Check to see if (latch closed, card present, power off) */
  723. down(&p_slot->ctrl->crit_sect);
  724. rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
  725. if (rc || !getstatus) {
  726. info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
  727. up(&p_slot->ctrl->crit_sect);
  728. return -ENODEV;
  729. }
  730. rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  731. if (rc || getstatus) {
  732. info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
  733. up(&p_slot->ctrl->crit_sect);
  734. return -ENODEV;
  735. }
  736. rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  737. if (rc || getstatus) {
  738. info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
  739. up(&p_slot->ctrl->crit_sect);
  740. return -ENODEV;
  741. }
  742. up(&p_slot->ctrl->crit_sect);
  743. p_slot->is_a_board = 1;
  744. /* We have to save the presence info for these slots */
  745. p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
  746. p_slot->hpc_ops->get_power_status(p_slot, &(p_slot->pwr_save));
  747. dbg("%s: p_slot->pwr_save %x\n", __FUNCTION__, p_slot->pwr_save);
  748. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  749. if(((p_slot->ctrl->pci_dev->vendor == PCI_VENDOR_ID_AMD) ||
  750. (p_slot->ctrl->pci_dev->device == PCI_DEVICE_ID_AMD_POGO_7458))
  751. && p_slot->ctrl->num_slots == 1) {
  752. /* handle amd pogo errata; this must be done before enable */
  753. amd_pogo_errata_save_misc_reg(p_slot);
  754. rc = board_added(p_slot);
  755. /* handle amd pogo errata; this must be done after enable */
  756. amd_pogo_errata_restore_misc_reg(p_slot);
  757. } else
  758. rc = board_added(p_slot);
  759. if (rc) {
  760. p_slot->hpc_ops->get_adapter_status(p_slot,
  761. &(p_slot->presence_save));
  762. p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  763. }
  764. update_slot_info(p_slot);
  765. return rc;
  766. }
  767. int shpchp_disable_slot (struct slot *p_slot)
  768. {
  769. u8 getstatus = 0;
  770. int ret = 0;
  771. if (!p_slot->ctrl)
  772. return -ENODEV;
  773. /* Check to see if (latch closed, card present, power on) */
  774. down(&p_slot->ctrl->crit_sect);
  775. ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
  776. if (ret || !getstatus) {
  777. info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
  778. up(&p_slot->ctrl->crit_sect);
  779. return -ENODEV;
  780. }
  781. ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
  782. if (ret || getstatus) {
  783. info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
  784. up(&p_slot->ctrl->crit_sect);
  785. return -ENODEV;
  786. }
  787. ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
  788. if (ret || !getstatus) {
  789. info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
  790. up(&p_slot->ctrl->crit_sect);
  791. return -ENODEV;
  792. }
  793. up(&p_slot->ctrl->crit_sect);
  794. ret = remove_board(p_slot);
  795. update_slot_info(p_slot);
  796. return ret;
  797. }