blob: 802c4c48d1862dad639d4ec69793352ac95eb27c [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
310 /* Wait for exclusive access to hardware */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100311 mutex_lock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 /* Power on slot without connecting to bus */
314 rc = p_slot->hpc_ops->power_on_slot(p_slot);
315 if (rc) {
316 err("%s: Failed to power on slot\n", __FUNCTION__);
317 /* Done with exclusive hardware access */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100318 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return -1;
320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if ((ctrl->pci_dev->vendor == 0x8086) && (ctrl->pci_dev->device == 0x0332)) {
323 if (slots_not_empty)
324 return WRONG_BUS_FREQUENCY;
325
326 if ((rc = p_slot->hpc_ops->set_bus_speed_mode(p_slot, PCI_SPEED_33MHz))) {
327 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100328 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return WRONG_BUS_FREQUENCY;
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 /* turn on board, blink green LED, turn off Amber LED */
333 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
334 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100335 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return rc;
337 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 rc = p_slot->hpc_ops->get_adapter_speed(p_slot, &adapter_speed);
341 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
342 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
343 /* 0xd = PCI-X 133 Mhz 533 */
344 /* This encoding is different from the one used in cur_bus_speed & */
345 /* max_bus_speed */
346
347 if (rc || adapter_speed == PCI_SPEED_UNKNOWN) {
348 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__);
349 /* Done with exclusive hardware access */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100350 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return WRONG_BUS_FREQUENCY;
352 }
353
354 rc = p_slot->hpc_ops->get_cur_bus_speed(p_slot, &bus_speed);
355 if (rc || bus_speed == PCI_SPEED_UNKNOWN) {
356 err("%s: Can't get bus operation speed\n", __FUNCTION__);
357 /* Done with exclusive hardware access */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100358 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return WRONG_BUS_FREQUENCY;
360 }
361
362 rc = p_slot->hpc_ops->get_max_bus_speed(p_slot, &max_bus_speed);
363 if (rc || max_bus_speed == PCI_SPEED_UNKNOWN) {
364 err("%s: Can't get max bus operation speed\n", __FUNCTION__);
365 max_bus_speed = bus_speed;
366 }
367
368 /* Done with exclusive hardware access */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100369 mutex_unlock(&ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370
371 if ((rc = p_slot->hpc_ops->get_prog_int(p_slot, &pi))) {
372 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__);
373 pi = 1;
374 }
375
376 /* Check if there are other slots or devices on the same bus */
377 if (!list_empty(&ctrl->pci_dev->subordinate->devices))
378 slots_not_empty = 1;
379
380 dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__,
381 slots_not_empty, pi);
382 dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
383 adapter_speed, bus_speed, max_bus_speed);
384
385 if (pi == 2) {
386 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
387 if ((rc = p_slot->hpc_ops->get_mode1_ECC_cap(p_slot, &mode))) {
388 err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__);
389 mode = 0;
390 }
391
392 switch (adapter_speed) {
393 case PCI_SPEED_133MHz_PCIX_533:
394 case PCI_SPEED_133MHz_PCIX_266:
395 if ((bus_speed != adapter_speed) &&
396 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
397 return rc;
398 break;
399 case PCI_SPEED_133MHz_PCIX_ECC:
400 case PCI_SPEED_133MHz_PCIX:
401 if (mode) { /* Bus - Mode 1 ECC */
402 if ((bus_speed != 0x7) &&
403 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
404 return rc;
405 } else {
406 if ((bus_speed != 0x4) &&
407 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
408 return rc;
409 }
410 break;
411 case PCI_SPEED_66MHz_PCIX_ECC:
412 case PCI_SPEED_66MHz_PCIX:
413 if (mode) { /* Bus - Mode 1 ECC */
414 if ((bus_speed != 0x5) &&
415 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
416 return rc;
417 } else {
418 if ((bus_speed != 0x2) &&
419 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
420 return rc;
421 }
422 break;
423 case PCI_SPEED_66MHz:
424 if ((bus_speed != 0x1) &&
425 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
426 return rc;
427 break;
428 case PCI_SPEED_33MHz:
429 if (bus_speed > 0x0) {
430 if (slots_not_empty == 0) {
431 if ((rc = change_bus_speed(ctrl, p_slot, adapter_speed)))
432 return rc;
433 } else {
434 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
435 return WRONG_BUS_FREQUENCY;
436 }
437 }
438 break;
439 default:
440 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__, bus_speed, adapter_speed);
441 return WRONG_BUS_FREQUENCY;
442 }
443 } else {
444 /* If adpater_speed == bus_speed, nothing to do here */
445 dbg("%s: In PI = %d\n", __FUNCTION__, pi);
446 if ((adapter_speed != bus_speed) &&
447 ((rc = fix_bus_speed(ctrl, p_slot, slots_not_empty, adapter_speed, bus_speed, max_bus_speed))))
448 return rc;
449 }
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /* turn on board, blink green LED, turn off Amber LED */
452 if ((rc = p_slot->hpc_ops->slot_enable(p_slot))) {
453 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return rc;
455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 /* Wait for ~1 second */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 wait_for_ctrl_irq (ctrl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700460 dbg("%s: slot status = %x\n", __FUNCTION__, p_slot->status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /* Check for a power fault */
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700462 if (p_slot->status == 0xFF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /* power fault occurred, but it was benign */
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700464 dbg("%s: power fault\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 rc = POWER_FAILURE;
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700466 p_slot->status = 0;
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700467 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700470 if (shpchp_configure_device(p_slot)) {
471 err("Cannot add device at 0x%x:0x%x\n", p_slot->bus,
472 p_slot->device);
473 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 }
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700475
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700476 p_slot->status = 0;
477 p_slot->is_a_board = 0x01;
478 p_slot->pwr_save = 1;
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700479
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700480 p_slot->hpc_ops->green_led_on(p_slot);
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 0;
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700483
484err_exit:
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700485 /* turn off slot, turn on Amber LED, turn off Green LED */
486 rc = p_slot->hpc_ops->slot_disable(p_slot);
487 if (rc) {
488 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700489 return rc;
490 }
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700491
rajesh.shah@intel.comdbd7a782005-10-13 12:05:36 -0700492 return(rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495
496/**
497 * remove_board - Turns off slot and LED's
498 *
499 */
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700500static int remove_board(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700502 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 u8 hp_slot;
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700504 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700506 if (shpchp_unconfigure_device(p_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 return(1);
508
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700509 hp_slot = p_slot->device - ctrl->slot_device_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
511
512 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 /* Change status to shutdown */
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700515 if (p_slot->is_a_board)
516 p_slot->status = 0x01;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 /* turn off slot, turn on Amber LED, turn off Green LED */
519 rc = p_slot->hpc_ops->slot_disable(p_slot);
520 if (rc) {
521 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return rc;
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 rc = p_slot->hpc_ops->set_attention_status(p_slot, 0);
526 if (rc) {
527 err("%s: Issue of Set Attention command failed\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 return rc;
529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700531 p_slot->pwr_save = 0;
532 p_slot->is_a_board = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 return 0;
535}
536
537
538static void pushbutton_helper_thread (unsigned long data)
539{
540 pushbutton_pending = data;
541
542 up(&event_semaphore);
543}
544
545
546/**
547 * shpchp_pushbutton_thread
548 *
549 * Scheduled procedure to handle blocking stuff for the pushbuttons
550 * Handles all pending events and exits.
551 *
552 */
553static void shpchp_pushbutton_thread (unsigned long slot)
554{
555 struct slot *p_slot = (struct slot *) slot;
556 u8 getstatus;
557
558 pushbutton_pending = 0;
559
560 if (!p_slot) {
561 dbg("%s: Error! slot NULL\n", __FUNCTION__);
562 return;
563 }
564
565 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
566 if (getstatus) {
567 p_slot->state = POWEROFF_STATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569 shpchp_disable_slot(p_slot);
570 p_slot->state = STATIC_STATE;
571 } else {
572 p_slot->state = POWERON_STATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Kenji Kaneshiged29aadd2006-01-26 09:59:24 +0900574 if (shpchp_enable_slot(p_slot))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 p_slot->hpc_ops->green_led_off(p_slot);
576
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 p_slot->state = STATIC_STATE;
578 }
579
580 return;
581}
582
583
584/* this is the main worker thread */
585static int event_thread(void* data)
586{
587 struct controller *ctrl;
588 lock_kernel();
589 daemonize("shpchpd_event");
590 unlock_kernel();
591
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
Kenji Kaneshigea4534562006-01-26 09:58:30 +0900602 list_for_each_entry(ctrl, &shpchp_ctrl_list, ctrl_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 interrupt_event_handler(ctrl);
604 }
605 dbg("event_thread signals exit\n");
606 up(&event_exit);
607 return 0;
608}
609
610int shpchp_event_start_thread (void)
611{
612 int pid;
613
614 /* initialize our semaphores */
615 init_MUTEX_LOCKED(&event_exit);
616 event_finished=0;
617
618 init_MUTEX_LOCKED(&event_semaphore);
619 pid = kernel_thread(event_thread, NULL, 0);
620
621 if (pid < 0) {
622 err ("Can't start up our event thread\n");
623 return -1;
624 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 return 0;
626}
627
628
629void shpchp_event_stop_thread (void)
630{
631 event_finished = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 up(&event_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 down(&event_exit);
634}
635
636
637static int update_slot_info (struct slot *slot)
638{
639 struct hotplug_slot_info *info;
640 int result;
641
642 info = kmalloc(sizeof(*info), GFP_KERNEL);
643 if (!info)
644 return -ENOMEM;
645
646 slot->hpc_ops->get_power_status(slot, &(info->power_status));
647 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
648 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
649 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
650
651 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
652 kfree (info);
653 return result;
654}
655
656static void interrupt_event_handler(struct controller *ctrl)
657{
658 int loop = 0;
659 int change = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 u8 hp_slot;
661 u8 getstatus;
662 struct slot *p_slot;
663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 while (change) {
665 change = 0;
666
667 for (loop = 0; loop < 10; loop++) {
668 if (ctrl->event_queue[loop].event_type != 0) {
669 dbg("%s:loop %x event_type %x\n", __FUNCTION__, loop,
670 ctrl->event_queue[loop].event_type);
671 hp_slot = ctrl->event_queue[loop].hp_slot;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 p_slot = shpchp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
676 dbg("%s: button cancel\n", __FUNCTION__);
677 del_timer(&p_slot->task_event);
678
679 switch (p_slot->state) {
680 case BLINKINGOFF_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 p_slot->hpc_ops->green_led_on(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 p_slot->hpc_ops->set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 break;
684 case BLINKINGON_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 p_slot->hpc_ops->set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 break;
688 default:
689 warn("Not a valid state\n");
690 return;
691 }
692 info(msg_button_cancel, p_slot->number);
693 p_slot->state = STATIC_STATE;
694 } else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
695 /* Button Pressed (No action on 1st press...) */
696 dbg("%s: Button pressed\n", __FUNCTION__);
697
698 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
699 if (getstatus) {
700 /* slot is on */
701 dbg("%s: slot is on\n", __FUNCTION__);
702 p_slot->state = BLINKINGOFF_STATE;
703 info(msg_button_off, p_slot->number);
704 } else {
705 /* slot is off */
706 dbg("%s: slot is off\n", __FUNCTION__);
707 p_slot->state = BLINKINGON_STATE;
708 info(msg_button_on, p_slot->number);
709 }
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* blink green LED and turn off amber */
712 p_slot->hpc_ops->green_led_blink(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 p_slot->hpc_ops->set_attention_status(p_slot, 0);
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 init_timer(&p_slot->task_event);
716 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
717 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
718 p_slot->task_event.data = (unsigned long) p_slot;
719
720 dbg("%s: add_timer p_slot = %p\n", __FUNCTION__,(void *) p_slot);
721 add_timer(&p_slot->task_event);
722 } else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
723 /***********POWER FAULT********************/
724 dbg("%s: power fault\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 p_slot->hpc_ops->set_attention_status(p_slot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 } else {
728 /* refresh notification */
729 if (p_slot)
730 update_slot_info(p_slot);
731 }
732
733 ctrl->event_queue[loop].event_type = 0;
734
735 change = 1;
736 }
737 } /* End of FOR loop */
738 }
739
740 return;
741}
742
743
744int shpchp_enable_slot (struct slot *p_slot)
745{
746 u8 getstatus = 0;
747 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 /* Check to see if (latch closed, card present, power off) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100750 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
752 if (rc || !getstatus) {
753 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100754 mutex_unlock(&p_slot->ctrl->crit_sect);
Dely Syee17fd92005-05-05 11:57:25 -0700755 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
757 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
758 if (rc || getstatus) {
759 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100760 mutex_unlock(&p_slot->ctrl->crit_sect);
Dely Syee17fd92005-05-05 11:57:25 -0700761 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 }
763 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
764 if (rc || getstatus) {
765 info("%s: already enabled on slot(%x)\n", __FUNCTION__, p_slot->number);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100766 mutex_unlock(&p_slot->ctrl->crit_sect);
Dely Syee17fd92005-05-05 11:57:25 -0700767 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100769 mutex_unlock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700771 p_slot->is_a_board = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773 /* We have to save the presence info for these slots */
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700774 p_slot->hpc_ops->get_adapter_status(p_slot, &(p_slot->presence_save));
775 p_slot->hpc_ops->get_power_status(p_slot, &(p_slot->pwr_save));
776 dbg("%s: p_slot->pwr_save %x\n", __FUNCTION__, p_slot->pwr_save);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
Keck, David53044f32006-01-16 15:22:36 -0600779 if(((p_slot->ctrl->pci_dev->vendor == PCI_VENDOR_ID_AMD) ||
780 (p_slot->ctrl->pci_dev->device == PCI_DEVICE_ID_AMD_POGO_7458))
781 && p_slot->ctrl->num_slots == 1) {
782 /* handle amd pogo errata; this must be done before enable */
783 amd_pogo_errata_save_misc_reg(p_slot);
784 rc = board_added(p_slot);
785 /* handle amd pogo errata; this must be done after enable */
786 amd_pogo_errata_restore_misc_reg(p_slot);
787 } else
788 rc = board_added(p_slot);
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (rc) {
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700791 p_slot->hpc_ops->get_adapter_status(p_slot,
792 &(p_slot->presence_save));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700796 update_slot_info(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 return rc;
798}
799
800
801int shpchp_disable_slot (struct slot *p_slot)
802{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 u8 getstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
806 if (!p_slot->ctrl)
Dely Syee17fd92005-05-05 11:57:25 -0700807 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 /* Check to see if (latch closed, card present, power on) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100810 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
813 if (ret || !getstatus) {
814 info("%s: no adapter on slot(%x)\n", __FUNCTION__, p_slot->number);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100815 mutex_unlock(&p_slot->ctrl->crit_sect);
Dely Syee17fd92005-05-05 11:57:25 -0700816 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 }
818 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
819 if (ret || getstatus) {
820 info("%s: latch open on slot(%x)\n", __FUNCTION__, p_slot->number);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100821 mutex_unlock(&p_slot->ctrl->crit_sect);
Dely Syee17fd92005-05-05 11:57:25 -0700822 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
825 if (ret || !getstatus) {
826 info("%s: already disabled slot(%x)\n", __FUNCTION__, p_slot->number);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100827 mutex_unlock(&p_slot->ctrl->crit_sect);
Dely Syee17fd92005-05-05 11:57:25 -0700828 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 }
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100830 mutex_unlock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700832 ret = remove_board(p_slot);
rajesh.shah@intel.com2178bfa2005-10-13 12:05:41 -0700833 update_slot_info(p_slot);
rajesh.shah@intel.comee138332005-10-13 12:05:42 -0700834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836