blob: b709328a41451845a5b3c57c4cd0a8b483d81eac [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Kristen Accardi8cf4c192005-08-16 15:16:10 -070026 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/smp_lock.h>
34#include <linux/pci.h>
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -070035#include "../pci.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "shpchp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static void interrupt_event_handler(struct controller *ctrl);
39
40static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
41static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
42static int event_finished;
43static unsigned long pushbutton_pending; /* = 0 */
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045u8 shpchp_handle_attention_button(u8 hp_slot, void *inst_id)
46{
47 struct controller *ctrl = (struct controller *) inst_id;
48 struct slot *p_slot;
49 u8 rc = 0;
50 u8 getstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct event_info *taskInfo;
52
53 /* Attention Button Change */
54 dbg("shpchp: Attention button interrupt received.\n");
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 /* This is the structure that tells the worker thread what to do */
57 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
58 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
59
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -070060 p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
62
63 ctrl->next_event = (ctrl->next_event + 1) % 10;
64 taskInfo->hp_slot = hp_slot;
65
66 rc++;
67
68 /*
69 * Button pressed - See if need to TAKE ACTION!!!
70 */
71 info("Button pressed on Slot(%d)\n", ctrl->first_slot + hp_slot);
72 taskInfo->event_type = INT_BUTTON_PRESS;
73
74 if ((p_slot->state == BLINKINGON_STATE)
75 || (p_slot->state == BLINKINGOFF_STATE)) {
76 /* Cancel if we are still blinking; this means that we press the
77 * attention again before the 5 sec. limit expires to cancel hot-add
78 * or hot-remove
79 */
80 taskInfo->event_type = INT_BUTTON_CANCEL;
81 info("Button cancel on Slot(%d)\n", ctrl->first_slot + hp_slot);
82 } else if ((p_slot->state == POWERON_STATE)
83 || (p_slot->state == POWEROFF_STATE)) {
84 /* Ignore if the slot is on power-on or power-off state; this
85 * means that the previous attention button action to hot-add or
86 * hot-remove is undergoing
87 */
88 taskInfo->event_type = INT_BUTTON_IGNORE;
89 info("Button ignore on Slot(%d)\n", ctrl->first_slot + hp_slot);
90 }
91
92 if (rc)
93 up(&event_semaphore); /* signal event thread that new event is posted */
94
95 return 0;
96
97}
98
99u8 shpchp_handle_switch_change(u8 hp_slot, void *inst_id)
100{
101 struct controller *ctrl = (struct controller *) inst_id;
102 struct slot *p_slot;
103 u8 rc = 0;
104 u8 getstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct event_info *taskInfo;
106
107 /* Switch Change */
108 dbg("shpchp: Switch interrupt received.\n");
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 /* This is the structure that tells the worker thread
111 * what to do
112 */
113 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
114 ctrl->next_event = (ctrl->next_event + 1) % 10;
115 taskInfo->hp_slot = hp_slot;
116
117 rc++;
118 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700119 p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
121 dbg("%s: Card present %x Power status %x\n", __FUNCTION__,
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700122 p_slot->presence_save, p_slot->pwr_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if (getstatus) {
125 /*
126 * Switch opened
127 */
128 info("Latch open on Slot(%d)\n", ctrl->first_slot + hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 taskInfo->event_type = INT_SWITCH_OPEN;
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700130 if (p_slot->pwr_save && p_slot->presence_save) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 taskInfo->event_type = INT_POWER_FAULT;
132 err("Surprise Removal of card\n");
133 }
134 } else {
135 /*
136 * Switch closed
137 */
138 info("Latch close on Slot(%d)\n", ctrl->first_slot + hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 taskInfo->event_type = INT_SWITCH_CLOSE;
140 }
141
142 if (rc)
143 up(&event_semaphore); /* signal event thread that new event is posted */
144
145 return rc;
146}
147
148u8 shpchp_handle_presence_change(u8 hp_slot, void *inst_id)
149{
150 struct controller *ctrl = (struct controller *) inst_id;
151 struct slot *p_slot;
152 u8 rc = 0;
153 /*u8 temp_byte;*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 struct event_info *taskInfo;
155
156 /* Presence Change */
157 dbg("shpchp: Presence/Notify input change.\n");
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /* This is the structure that tells the worker thread
160 * what to do
161 */
162 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
163 ctrl->next_event = (ctrl->next_event + 1) % 10;
164 taskInfo->hp_slot = hp_slot;
165
166 rc++;
167 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
168
169 /*
170 * Save the presence state
171 */
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700172 p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
173 if (p_slot->presence_save) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 /*
175 * Card Present
176 */
177 info("Card present on Slot(%d)\n", ctrl->first_slot + hp_slot);
178 taskInfo->event_type = INT_PRESENCE_ON;
179 } else {
180 /*
181 * Not Present
182 */
183 info("Card not present on Slot(%d)\n", ctrl->first_slot + hp_slot);
184 taskInfo->event_type = INT_PRESENCE_OFF;
185 }
186
187 if (rc)
188 up(&event_semaphore); /* signal event thread that new event is posted */
189
190 return rc;
191}
192
193u8 shpchp_handle_power_fault(u8 hp_slot, void *inst_id)
194{
195 struct controller *ctrl = (struct controller *) inst_id;
196 struct slot *p_slot;
197 u8 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct event_info *taskInfo;
199
200 /* Power fault */
201 dbg("shpchp: Power fault interrupt received.\n");
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /* This is the structure that tells the worker thread
204 * what to do
205 */
206 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
207 ctrl->next_event = (ctrl->next_event + 1) % 10;
208 taskInfo->hp_slot = hp_slot;
209
210 rc++;
211 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
212
213 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
214 /*
215 * Power fault Cleared
216 */
217 info("Power fault cleared on Slot(%d)\n", ctrl->first_slot + hp_slot);
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700218 p_slot->status = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
220 } else {
221 /*
222 * Power fault
223 */
224 info("Power fault on Slot(%d)\n", ctrl->first_slot + hp_slot);
225 taskInfo->event_type = INT_POWER_FAULT;
226 /* set power fault status for this board */
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700227 p_slot->status = 0xFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 info("power fault bit %x set\n", hp_slot);
229 }
230 if (rc)
231 up(&event_semaphore); /* signal event thread that new event is posted */
232
233 return rc;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/* The following routines constitute the bulk of the
237 hotplug controller logic
238 */
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700239static int change_bus_speed(struct controller *ctrl, struct slot *p_slot,
240 enum pci_bus_speed speed)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700242 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 dbg("%s: change to speed %d\n", __FUNCTION__, speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, speed))) {
246 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return WRONG_BUS_FREQUENCY;
248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return rc;
250}
251
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700252static int fix_bus_speed(struct controller *ctrl, struct slot *pslot,
253 u8 flag, enum pci_bus_speed asp, enum pci_bus_speed bsp,
254 enum pci_bus_speed msp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700256 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (flag != 0) { /* Other slots on the same bus are occupied */
259 if ( asp < bsp ) {
260 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bsp, asp);
261 return WRONG_BUS_FREQUENCY;
262 }
263 } else {
264 /* Other slots on the same bus are empty */
265 if (msp == bsp) {
266 /* if adapter_speed >= bus_speed, do nothing */
267 if (asp < bsp) {
268 /*
269 * Try to lower bus speed to accommodate the adapter if other slots
270 * on the same controller are empty
271 */
272 if ((rc = change_bus_speed(ctrl, pslot, asp)))
273 return rc;
274 }
275 } else {
276 if (asp < msp) {
277 if ((rc = change_bus_speed(ctrl, pslot, asp)))
278 return rc;
279 } else {
280 if ((rc = change_bus_speed(ctrl, pslot, msp)))
281 return rc;
282 }
283 }
284 }
285 return rc;
286}
287
288/**
289 * board_added - Called after a board has been added to the system.
290 *
291 * Turns power on for the board
292 * Configures board
293 *
294 */
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700295static int board_added(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296{
297 u8 hp_slot;
298 u8 slots_not_empty = 0;
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700299 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 enum pci_bus_speed adapter_speed, bus_speed, max_bus_speed;
301 u8 pi, mode;
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700302 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700304 hp_slot = p_slot->device - ctrl->slot_device_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700306 dbg("%s: p_slot->device, slot_offset, hp_slot = %d, %d ,%d\n",
307 __FUNCTION__, p_slot->device,
308 ctrl->slot_device_offset, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /* Power on slot without connecting to bus */
311 rc = p_slot->hpc_ops->power_on_slot(p_slot);
312 if (rc) {
313 err("%s: Failed to power on slot\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return -1;
315 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if ((ctrl->pci_dev->vendor == 0x8086) && (ctrl->pci_dev->device == 0x0332)) {
318 if (slots_not_empty)
319 return WRONG_BUS_FREQUENCY;
320
321 if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, PCI_SPEED_33MHz))) {
322 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return WRONG_BUS_FREQUENCY;
324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* turn on board, blink green LED, turn off Amber LED */
327 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
328 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return rc;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 }
332
333 rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
334 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
335 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
336 /* 0xd = PCI-X 133 Mhz 533 */
337 /* This encoding is different from the one used in cur_bus_speed & */
338 /* max_bus_speed */
339
340 if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
341 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return WRONG_BUS_FREQUENCY;
343 }
344
345 rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
346 if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
347 err("%s: Can't get bus operation speed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return WRONG_BUS_FREQUENCY;
349 }
350
351 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
352 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
353 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
354 max_bus_speed = bus_speed;
355 }
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if ((rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi))) {
358 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
359 pi = 1;
360 }
361
362 /* Check if there are other slots or devices on the same bus */
363 if (!list_empty(&ctrl->pci_dev->subordinate->devices))
364 slots_not_empty = 1;
365
366 dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__,
367 slots_not_empty, pi);
368 dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
369 adapter_speed, bus_speed, max_bus_speed);
370
371 if (pi == 2) {
372 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
373 if ((rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode))) {
374 err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__);
375 mode = 0;
376 }
377
378 switch (adapter_speed) {
379 case PCI_SPEED_133MHz_PCIX_533:
380 case PCI_SPEED_133MHz_PCIX_266:
381 if ((bus_speed != adapter_speed) &&
382 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
383 return rc;
384 break;
385 case PCI_SPEED_133MHz_PCIX_ECC:
386 case PCI_SPEED_133MHz_PCIX:
387 if (mode) { /* Bus - Mode 1 ECC */
388 if ((bus_speed != 0x7) &&
389 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
390 return rc;
391 } else {
392 if ((bus_speed != 0x4) &&
393 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
394 return rc;
395 }
396 break;
397 case PCI_SPEED_66MHz_PCIX_ECC:
398 case PCI_SPEED_66MHz_PCIX:
399 if (mode) { /* Bus - Mode 1 ECC */
400 if ((bus_speed != 0x5) &&
401 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
402 return rc;
403 } else {
404 if ((bus_speed != 0x2) &&
405 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
406 return rc;
407 }
408 break;
409 case PCI_SPEED_66MHz:
410 if ((bus_speed != 0x1) &&
411 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
412 return rc;
413 break;
414 case PCI_SPEED_33MHz:
415 if (bus_speed > 0x0) {
416 if (slots_not_empty == 0) {
417 if ((rc = change_bus_speed(ctrl, p_slot, adapter_speed)))
418 return rc;
419 } else {
420 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
421 return WRONG_BUS_FREQUENCY;
422 }
423 }
424 break;
425 default:
426 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
427 return WRONG_BUS_FREQUENCY;
428 }
429 } else {
430 /* If adpater_speed == bus_speed, nothing to do here */
431 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
432 if ((adapter_speed != bus_speed) &&
433 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
434 return rc;
435 }
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 /* turn on board, blink green LED, turn off Amber LED */
438 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
439 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return rc;
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 /* Wait for ~1 second */
Kenji Kaneshige68c0b672006-02-21 15:45:42 -0800444 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700446 dbg("%s: slot status = %x\n", __FUNCTION__, p_slot->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 /* Check for a power fault */
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700448 if (p_slot->status == 0xFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 /* power fault occurred, but it was benign */
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700450 dbg("%s: power fault\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 rc = POWER_FAILURE;
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700452 p_slot->status = 0;
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700453 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700456 if (shpchp_configure_device(p_slot)) {
457 err("Cannot add device at 0x%x:0x%x\n", p_slot->bus,
458 p_slot->device);
459 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700461
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700462 p_slot->status = 0;
463 p_slot->is_a_board = 0x01;
464 p_slot->pwr_save = 1;
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700465
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700466 p_slot->hpc_ops->green_led_on(p_slot);
467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return 0;
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700469
470err_exit:
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700471 /* turn off slot, turn on Amber LED, turn off Green LED */
472 rc = p_slot->hpc_ops->slot_disable(p_slot);
473 if (rc) {
474 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700475 return rc;
476 }
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700477
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700478 return(rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479}
480
481
482/**
483 * remove_board - Turns off slot and LED's
484 *
485 */
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700486static int remove_board(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700488 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 u8 hp_slot;
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700490 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700492 if (shpchp_unconfigure_device(p_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 return(1);
494
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700495 hp_slot = p_slot->device - ctrl->slot_device_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
497
498 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /* Change status to shutdown */
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700501 if (p_slot->is_a_board)
502 p_slot->status = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 /* turn off slot, turn on Amber LED, turn off Green LED */
505 rc = p_slot->hpc_ops->slot_disable(p_slot);
506 if (rc) {
507 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 return rc;
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
512 if (rc) {
513 err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return rc;
515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700517 p_slot->pwr_save = 0;
518 p_slot->is_a_board = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
520 return 0;
521}
522
523
524static void pushbutton_helper_thread (unsigned long data)
525{
526 pushbutton_pending = data;
527
528 up(&event_semaphore);
529}
530
531
532/**
533 * shpchp_pushbutton_thread
534 *
535 * Scheduled procedure to handle blocking stuff for the pushbuttons
536 * Handles all pending events and exits.
537 *
538 */
539static void shpchp_pushbutton_thread (unsigned long slot)
540{
541 struct slot *p_slot = (struct slot *) slot;
542 u8 getstatus;
543
544 pushbutton_pending = 0;
545
546 if (!p_slot) {
547 dbg("%s: Error! slot NULL\n", __FUNCTION__);
548 return;
549 }
550
551 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
552 if (getstatus) {
553 p_slot->state = POWEROFF_STATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
555 shpchp_disable_slot(p_slot);
556 p_slot->state = STATIC_STATE;
557 } else {
558 p_slot->state = POWERON_STATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Kenji Kaneshiged29aadd2006-01-26 09:59:24 +0900560 if (shpchp_enable_slot(p_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 p_slot->hpc_ops->green_led_off(p_slot);
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 p_slot->state = STATIC_STATE;
564 }
565
566 return;
567}
568
569
570/* this is the main worker thread */
571static int event_thread(void* data)
572{
573 struct controller *ctrl;
574 lock_kernel();
575 daemonize("shpchpd_event");
576 unlock_kernel();
577
578 while (1) {
579 dbg("!!!!event_thread sleeping\n");
580 down_interruptible (&event_semaphore);
581 dbg("event_thread woken finished = %d\n", event_finished);
582 if (event_finished || signal_pending(current))
583 break;
584 /* Do stuff here */
585 if (pushbutton_pending)
586 shpchp_pushbutton_thread(pushbutton_pending);
587 else
Kenji Kaneshigea4534562006-01-26 09:58:30 +0900588 list_for_each_entry(ctrl, &shpchp_ctrl_list, ctrl_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 interrupt_event_handler(ctrl);
590 }
591 dbg("event_thread signals exit\n");
592 up(&event_exit);
593 return 0;
594}
595
596int shpchp_event_start_thread (void)
597{
598 int pid;
599
600 /* initialize our semaphores */
601 init_MUTEX_LOCKED(&event_exit);
602 event_finished=0;
603
604 init_MUTEX_LOCKED(&event_semaphore);
605 pid = kernel_thread(event_thread, NULL, 0);
606
607 if (pid < 0) {
608 err ("Can't start up our event thread\n");
609 return -1;
610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return 0;
612}
613
614
615void shpchp_event_stop_thread (void)
616{
617 event_finished = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 up(&event_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 down(&event_exit);
620}
621
622
623static int update_slot_info (struct slot *slot)
624{
625 struct hotplug_slot_info *info;
626 int result;
627
628 info = kmalloc(sizeof(*info), GFP_KERNEL);
629 if (!info)
630 return -ENOMEM;
631
632 slot->hpc_ops->get_power_status(slot, &(info->power_status));
633 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
634 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
635 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
636
637 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
638 kfree (info);
639 return result;
640}
641
642static void interrupt_event_handler(struct controller *ctrl)
643{
644 int loop = 0;
645 int change = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 u8 hp_slot;
647 u8 getstatus;
648 struct slot *p_slot;
649
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 while (change) {
651 change = 0;
652
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
662 dbg("%s: button cancel\n", __FUNCTION__);
663 del_timer(&p_slot->task_event);
664
665 switch (p_slot->state) {
666 case BLINKINGOFF_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 p_slot->hpc_ops->green_led_on(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 p_slot->hpc_ops->set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 break;
670 case BLINKINGON_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 p_slot->hpc_ops->set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 break;
674 default:
675 warn("Not a valid state\n");
676 return;
677 }
678 info(msg_button_cancel, p_slot->number);
679 p_slot->state = STATIC_STATE;
680 } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
681 /* Button Pressed (No action on 1st press...) */
682 dbg("%s: Button pressed\n", __FUNCTION__);
683
684 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
685 if (getstatus) {
686 /* slot is on */
687 dbg("%s: slot is on\n", __FUNCTION__);
688 p_slot->state = BLINKINGOFF_STATE;
689 info(msg_button_off, p_slot->number);
690 } else {
691 /* slot is off */
692 dbg("%s: slot is off\n", __FUNCTION__);
693 p_slot->state = BLINKINGON_STATE;
694 info(msg_button_on, p_slot->number);
695 }
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 /* blink green LED and turn off amber */
698 p_slot->hpc_ops->green_led_blink(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 p_slot->hpc_ops->set_attention_status(p_slot, 0);
700
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 init_timer(&p_slot->task_event);
702 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
703 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
704 p_slot->task_event.data = (unsigned long) p_slot;
705
706 dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
707 add_timer(&p_slot->task_event);
708 } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
709 /***********POWER FAULT********************/
710 dbg("%s: power fault\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 p_slot->hpc_ops->set_attention_status(p_slot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 } else {
714 /* refresh notification */
715 if (p_slot)
716 update_slot_info(p_slot);
717 }
718
719 ctrl->event_queue[loop].event_type = 0;
720
721 change = 1;
722 }
723 } /* End of FOR loop */
724 }
725
726 return;
727}
728
729
730int shpchp_enable_slot (struct slot *p_slot)
731{
732 u8 getstatus = 0;
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900733 int rc, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
735 /* Check to see if (latch closed, card present, power off) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100736 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
738 if (rc || !getstatus) {
739 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900740 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
743 if (rc || getstatus) {
744 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900745 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 }
747 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
748 if (rc || getstatus) {
749 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900750 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700753 p_slot->is_a_board = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
755 /* We have to save the presence info for these slots */
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700756 p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
757 p_slot->hpc_ops->get_power_status(p_slot, &(p_slot->pwr_save));
758 dbg("%s: p_slot->pwr_save %x\n", __FUNCTION__, p_slot->pwr_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Keck, David53044f32006-01-16 15:22:36 -0600761 if(((p_slot->ctrl->pci_dev->vendor == PCI_VENDOR_ID_AMD) ||
762 (p_slot->ctrl->pci_dev->device == PCI_DEVICE_ID_AMD_POGO_7458))
763 && p_slot->ctrl->num_slots == 1) {
764 /* handle amd pogo errata; this must be done before enable */
765 amd_pogo_errata_save_misc_reg(p_slot);
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900766 retval = board_added(p_slot);
Keck, David53044f32006-01-16 15:22:36 -0600767 /* handle amd pogo errata; this must be done after enable */
768 amd_pogo_errata_restore_misc_reg(p_slot);
769 } else
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900770 retval = board_added(p_slot);
Keck, David53044f32006-01-16 15:22:36 -0600771
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900772 if (retval) {
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700773 p_slot->hpc_ops->get_adapter_status(p_slot,
774 &(p_slot->presence_save));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700778 update_slot_info(p_slot);
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900779 out:
780 mutex_unlock(&p_slot->ctrl->crit_sect);
781 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784
785int shpchp_disable_slot (struct slot *p_slot)
786{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 u8 getstatus = 0;
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900788 int rc, retval = -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 if (!p_slot->ctrl)
Dely Syee17fd92005-05-05 11:57:25 -0700791 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 /* Check to see if (latch closed, card present, power on) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100794 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900796 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
797 if (rc || !getstatus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900799 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900801 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
802 if (rc || getstatus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900804 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900806 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
807 if (rc || !getstatus) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900809 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900812 retval = remove_board(p_slot);
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700813 update_slot_info(p_slot);
Kenji Kaneshigeef3be542006-01-26 10:00:33 +0900814 out:
815 mutex_unlock(&p_slot->ctrl->crit_sect);
816 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
818