2 * Standard Hot Plug Controller Driver
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
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.
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
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.
26 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/types.h>
33 #include <linux/smp_lock.h>
34 #include <linux/pci.h>
35 #include <linux/workqueue.h>
39 static void interrupt_event_handler(void *data
);
41 static int queue_interrupt_event(struct slot
*p_slot
, u32 event_type
)
43 struct event_info
*info
;
45 info
= kmalloc(sizeof(*info
), GFP_ATOMIC
);
49 info
->event_type
= event_type
;
50 info
->p_slot
= p_slot
;
51 INIT_WORK(&info
->work
, interrupt_event_handler
, info
);
53 queue_work(shpchp_wq
, &info
->work
);
58 u8
shpchp_handle_attention_button(u8 hp_slot
, void *inst_id
)
60 struct controller
*ctrl
= (struct controller
*) inst_id
;
64 /* Attention Button Change */
65 dbg("shpchp: Attention button interrupt received.\n");
67 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
68 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &(p_slot
->presence_save
));
71 * Button pressed - See if need to TAKE ACTION!!!
73 info("Button pressed on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
74 event_type
= INT_BUTTON_PRESS
;
76 if ((p_slot
->state
== BLINKINGON_STATE
)
77 || (p_slot
->state
== BLINKINGOFF_STATE
)) {
78 /* Cancel if we are still blinking; this means that we press the
79 * attention again before the 5 sec. limit expires to cancel hot-add
82 event_type
= INT_BUTTON_CANCEL
;
83 info("Button cancel on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
84 } else if ((p_slot
->state
== POWERON_STATE
)
85 || (p_slot
->state
== POWEROFF_STATE
)) {
86 /* Ignore if the slot is on power-on or power-off state; this
87 * means that the previous attention button action to hot-add or
88 * hot-remove is undergoing
90 event_type
= INT_BUTTON_IGNORE
;
91 info("Button ignore on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
94 queue_interrupt_event(p_slot
, event_type
);
100 u8
shpchp_handle_switch_change(u8 hp_slot
, void *inst_id
)
102 struct controller
*ctrl
= (struct controller
*) inst_id
;
108 dbg("shpchp: Switch interrupt received.\n");
110 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
111 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &(p_slot
->presence_save
));
112 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
113 dbg("%s: Card present %x Power status %x\n", __FUNCTION__
,
114 p_slot
->presence_save
, p_slot
->pwr_save
);
120 info("Latch open on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
121 event_type
= INT_SWITCH_OPEN
;
122 if (p_slot
->pwr_save
&& p_slot
->presence_save
) {
123 event_type
= INT_POWER_FAULT
;
124 err("Surprise Removal of card\n");
130 info("Latch close on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
131 event_type
= INT_SWITCH_CLOSE
;
134 queue_interrupt_event(p_slot
, event_type
);
139 u8
shpchp_handle_presence_change(u8 hp_slot
, void *inst_id
)
141 struct controller
*ctrl
= (struct controller
*) inst_id
;
145 /* Presence Change */
146 dbg("shpchp: Presence/Notify input change.\n");
148 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
151 * Save the presence state
153 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &(p_slot
->presence_save
));
154 if (p_slot
->presence_save
) {
158 info("Card present on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
159 event_type
= INT_PRESENCE_ON
;
164 info("Card not present on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
165 event_type
= INT_PRESENCE_OFF
;
168 queue_interrupt_event(p_slot
, event_type
);
173 u8
shpchp_handle_power_fault(u8 hp_slot
, void *inst_id
)
175 struct controller
*ctrl
= (struct controller
*) inst_id
;
180 dbg("shpchp: Power fault interrupt received.\n");
182 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
184 if ( !(p_slot
->hpc_ops
->query_power_fault(p_slot
))) {
186 * Power fault Cleared
188 info("Power fault cleared on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
189 p_slot
->status
= 0x00;
190 event_type
= INT_POWER_FAULT_CLEAR
;
195 info("Power fault on Slot(%d)\n", ctrl
->first_slot
+ hp_slot
);
196 event_type
= INT_POWER_FAULT
;
197 /* set power fault status for this board */
198 p_slot
->status
= 0xFF;
199 info("power fault bit %x set\n", hp_slot
);
202 queue_interrupt_event(p_slot
, event_type
);
207 /* The following routines constitute the bulk of the
208 hotplug controller logic
210 static int change_bus_speed(struct controller
*ctrl
, struct slot
*p_slot
,
211 enum pci_bus_speed speed
)
215 dbg("%s: change to speed %d\n", __FUNCTION__
, speed
);
216 if ((rc
= p_slot
->hpc_ops
->set_bus_speed_mode(p_slot
, speed
))) {
217 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__
);
218 return WRONG_BUS_FREQUENCY
;
223 static int fix_bus_speed(struct controller
*ctrl
, struct slot
*pslot
,
224 u8 flag
, enum pci_bus_speed asp
, enum pci_bus_speed bsp
,
225 enum pci_bus_speed msp
)
229 if (flag
!= 0) { /* Other slots on the same bus are occupied */
231 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__
, bsp
, asp
);
232 return WRONG_BUS_FREQUENCY
;
235 /* Other slots on the same bus are empty */
237 /* if adapter_speed >= bus_speed, do nothing */
240 * Try to lower bus speed to accommodate the adapter if other slots
241 * on the same controller are empty
243 if ((rc
= change_bus_speed(ctrl
, pslot
, asp
)))
248 if ((rc
= change_bus_speed(ctrl
, pslot
, asp
)))
251 if ((rc
= change_bus_speed(ctrl
, pslot
, msp
)))
260 * board_added - Called after a board has been added to the system.
262 * Turns power on for the board
266 static int board_added(struct slot
*p_slot
)
269 u8 slots_not_empty
= 0;
271 enum pci_bus_speed adapter_speed
, bus_speed
, max_bus_speed
;
273 struct controller
*ctrl
= p_slot
->ctrl
;
275 hp_slot
= p_slot
->device
- ctrl
->slot_device_offset
;
277 dbg("%s: p_slot->device, slot_offset, hp_slot = %d, %d ,%d\n",
278 __FUNCTION__
, p_slot
->device
,
279 ctrl
->slot_device_offset
, hp_slot
);
281 /* Power on slot without connecting to bus */
282 rc
= p_slot
->hpc_ops
->power_on_slot(p_slot
);
284 err("%s: Failed to power on slot\n", __FUNCTION__
);
288 if ((ctrl
->pci_dev
->vendor
== 0x8086) && (ctrl
->pci_dev
->device
== 0x0332)) {
290 return WRONG_BUS_FREQUENCY
;
292 if ((rc
= p_slot
->hpc_ops
->set_bus_speed_mode(p_slot
, PCI_SPEED_33MHz
))) {
293 err("%s: Issue of set bus speed mode command failed\n", __FUNCTION__
);
294 return WRONG_BUS_FREQUENCY
;
297 /* turn on board, blink green LED, turn off Amber LED */
298 if ((rc
= p_slot
->hpc_ops
->slot_enable(p_slot
))) {
299 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__
);
304 rc
= p_slot
->hpc_ops
->get_adapter_speed(p_slot
, &adapter_speed
);
305 /* 0 = PCI 33Mhz, 1 = PCI 66 Mhz, 2 = PCI-X 66 PA, 4 = PCI-X 66 ECC, */
306 /* 5 = PCI-X 133 PA, 7 = PCI-X 133 ECC, 0xa = PCI-X 133 Mhz 266, */
307 /* 0xd = PCI-X 133 Mhz 533 */
308 /* This encoding is different from the one used in cur_bus_speed & */
311 if (rc
|| adapter_speed
== PCI_SPEED_UNKNOWN
) {
312 err("%s: Can't get adapter speed or bus mode mismatch\n", __FUNCTION__
);
313 return WRONG_BUS_FREQUENCY
;
316 rc
= p_slot
->hpc_ops
->get_cur_bus_speed(p_slot
, &bus_speed
);
317 if (rc
|| bus_speed
== PCI_SPEED_UNKNOWN
) {
318 err("%s: Can't get bus operation speed\n", __FUNCTION__
);
319 return WRONG_BUS_FREQUENCY
;
322 rc
= p_slot
->hpc_ops
->get_max_bus_speed(p_slot
, &max_bus_speed
);
323 if (rc
|| max_bus_speed
== PCI_SPEED_UNKNOWN
) {
324 err("%s: Can't get max bus operation speed\n", __FUNCTION__
);
325 max_bus_speed
= bus_speed
;
328 if ((rc
= p_slot
->hpc_ops
->get_prog_int(p_slot
, &pi
))) {
329 err("%s: Can't get controller programming interface, set it to 1\n", __FUNCTION__
);
333 /* Check if there are other slots or devices on the same bus */
334 if (!list_empty(&ctrl
->pci_dev
->subordinate
->devices
))
337 dbg("%s: slots_not_empty %d, pi %d\n", __FUNCTION__
,
338 slots_not_empty
, pi
);
339 dbg("adapter_speed %d, bus_speed %d, max_bus_speed %d\n",
340 adapter_speed
, bus_speed
, max_bus_speed
);
343 dbg("%s: In PI = %d\n", __FUNCTION__
, pi
);
344 if ((rc
= p_slot
->hpc_ops
->get_mode1_ECC_cap(p_slot
, &mode
))) {
345 err("%s: Can't get Mode1_ECC, set mode to 0\n", __FUNCTION__
);
349 switch (adapter_speed
) {
350 case PCI_SPEED_133MHz_PCIX_533
:
351 case PCI_SPEED_133MHz_PCIX_266
:
352 if ((bus_speed
!= adapter_speed
) &&
353 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
356 case PCI_SPEED_133MHz_PCIX_ECC
:
357 case PCI_SPEED_133MHz_PCIX
:
358 if (mode
) { /* Bus - Mode 1 ECC */
359 if ((bus_speed
!= 0x7) &&
360 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
363 if ((bus_speed
!= 0x4) &&
364 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
368 case PCI_SPEED_66MHz_PCIX_ECC
:
369 case PCI_SPEED_66MHz_PCIX
:
370 if (mode
) { /* Bus - Mode 1 ECC */
371 if ((bus_speed
!= 0x5) &&
372 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
375 if ((bus_speed
!= 0x2) &&
376 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
380 case PCI_SPEED_66MHz
:
381 if ((bus_speed
!= 0x1) &&
382 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
385 case PCI_SPEED_33MHz
:
386 if (bus_speed
> 0x0) {
387 if (slots_not_empty
== 0) {
388 if ((rc
= change_bus_speed(ctrl
, p_slot
, adapter_speed
)))
391 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__
, bus_speed
, adapter_speed
);
392 return WRONG_BUS_FREQUENCY
;
397 err("%s: speed of bus %x and adapter %x mismatch\n", __FUNCTION__
, bus_speed
, adapter_speed
);
398 return WRONG_BUS_FREQUENCY
;
401 /* If adpater_speed == bus_speed, nothing to do here */
402 dbg("%s: In PI = %d\n", __FUNCTION__
, pi
);
403 if ((adapter_speed
!= bus_speed
) &&
404 ((rc
= fix_bus_speed(ctrl
, p_slot
, slots_not_empty
, adapter_speed
, bus_speed
, max_bus_speed
))))
408 /* turn on board, blink green LED, turn off Amber LED */
409 if ((rc
= p_slot
->hpc_ops
->slot_enable(p_slot
))) {
410 err("%s: Issue of Slot Enable command failed\n", __FUNCTION__
);
414 /* Wait for ~1 second */
417 dbg("%s: slot status = %x\n", __FUNCTION__
, p_slot
->status
);
418 /* Check for a power fault */
419 if (p_slot
->status
== 0xFF) {
420 /* power fault occurred, but it was benign */
421 dbg("%s: power fault\n", __FUNCTION__
);
427 if (shpchp_configure_device(p_slot
)) {
428 err("Cannot add device at 0x%x:0x%x\n", p_slot
->bus
,
434 p_slot
->is_a_board
= 0x01;
435 p_slot
->pwr_save
= 1;
437 p_slot
->hpc_ops
->green_led_on(p_slot
);
442 /* turn off slot, turn on Amber LED, turn off Green LED */
443 rc
= p_slot
->hpc_ops
->slot_disable(p_slot
);
445 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__
);
454 * remove_board - Turns off slot and LED's
457 static int remove_board(struct slot
*p_slot
)
459 struct controller
*ctrl
= p_slot
->ctrl
;
463 if (shpchp_unconfigure_device(p_slot
))
466 hp_slot
= p_slot
->device
- ctrl
->slot_device_offset
;
467 p_slot
= shpchp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
469 dbg("In %s, hp_slot = %d\n", __FUNCTION__
, hp_slot
);
471 /* Change status to shutdown */
472 if (p_slot
->is_a_board
)
473 p_slot
->status
= 0x01;
475 /* turn off slot, turn on Amber LED, turn off Green LED */
476 rc
= p_slot
->hpc_ops
->slot_disable(p_slot
);
478 err("%s: Issue of Slot Disable command failed\n", __FUNCTION__
);
482 rc
= p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
484 err("%s: Issue of Set Attention command failed\n", __FUNCTION__
);
488 p_slot
->pwr_save
= 0;
489 p_slot
->is_a_board
= 0;
496 * shpchp_pushbutton_thread
498 * Scheduled procedure to handle blocking stuff for the pushbuttons
499 * Handles all pending events and exits.
502 void shpchp_pushbutton_thread(void *data
)
504 struct slot
*p_slot
= data
;
507 p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
509 p_slot
->state
= POWEROFF_STATE
;
510 shpchp_disable_slot(p_slot
);
511 p_slot
->state
= STATIC_STATE
;
513 p_slot
->state
= POWERON_STATE
;
514 if (shpchp_enable_slot(p_slot
))
515 p_slot
->hpc_ops
->green_led_off(p_slot
);
516 p_slot
->state
= STATIC_STATE
;
520 static int update_slot_info (struct slot
*slot
)
522 struct hotplug_slot_info
*info
;
525 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
529 slot
->hpc_ops
->get_power_status(slot
, &(info
->power_status
));
530 slot
->hpc_ops
->get_attention_status(slot
, &(info
->attention_status
));
531 slot
->hpc_ops
->get_latch_status(slot
, &(info
->latch_status
));
532 slot
->hpc_ops
->get_adapter_status(slot
, &(info
->adapter_status
));
534 result
= pci_hp_change_slot_info(slot
->hotplug_slot
, info
);
539 static void interrupt_event_handler(void *data
)
541 struct event_info
*info
= data
;
542 struct slot
*p_slot
= info
->p_slot
;
545 switch (info
->event_type
) {
546 case INT_BUTTON_CANCEL
:
547 dbg("%s: button cancel\n", __FUNCTION__
);
548 cancel_delayed_work(&p_slot
->work
);
549 switch (p_slot
->state
) {
550 case BLINKINGOFF_STATE
:
551 p_slot
->hpc_ops
->green_led_on(p_slot
);
552 p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
554 case BLINKINGON_STATE
:
555 p_slot
->hpc_ops
->green_led_off(p_slot
);
556 p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
559 warn("Not a valid state\n");
562 info(msg_button_cancel
, p_slot
->number
);
563 p_slot
->state
= STATIC_STATE
;
565 case INT_BUTTON_PRESS
:
566 dbg("%s: Button pressed\n", __FUNCTION__
);
567 p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
569 p_slot
->state
= BLINKINGOFF_STATE
;
570 info(msg_button_off
, p_slot
->number
);
572 p_slot
->state
= BLINKINGON_STATE
;
573 info(msg_button_on
, p_slot
->number
);
575 /* blink green LED and turn off amber */
576 p_slot
->hpc_ops
->green_led_blink(p_slot
);
577 p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
579 queue_delayed_work(shpchp_wq
, &p_slot
->work
, 5*HZ
);
581 case INT_POWER_FAULT
:
582 dbg("%s: power fault\n", __FUNCTION__
);
583 p_slot
->hpc_ops
->set_attention_status(p_slot
, 1);
584 p_slot
->hpc_ops
->green_led_off(p_slot
);
587 update_slot_info(p_slot
);
595 int shpchp_enable_slot (struct slot
*p_slot
)
598 int rc
, retval
= -ENODEV
;
600 /* Check to see if (latch closed, card present, power off) */
601 mutex_lock(&p_slot
->ctrl
->crit_sect
);
602 rc
= p_slot
->hpc_ops
->get_adapter_status(p_slot
, &getstatus
);
603 if (rc
|| !getstatus
) {
604 info("%s: no adapter on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
607 rc
= p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
608 if (rc
|| getstatus
) {
609 info("%s: latch open on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
612 rc
= p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
613 if (rc
|| getstatus
) {
614 info("%s: already enabled on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
618 p_slot
->is_a_board
= 1;
620 /* We have to save the presence info for these slots */
621 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &(p_slot
->presence_save
));
622 p_slot
->hpc_ops
->get_power_status(p_slot
, &(p_slot
->pwr_save
));
623 dbg("%s: p_slot->pwr_save %x\n", __FUNCTION__
, p_slot
->pwr_save
);
624 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
626 if(((p_slot
->ctrl
->pci_dev
->vendor
== PCI_VENDOR_ID_AMD
) ||
627 (p_slot
->ctrl
->pci_dev
->device
== PCI_DEVICE_ID_AMD_POGO_7458
))
628 && p_slot
->ctrl
->num_slots
== 1) {
629 /* handle amd pogo errata; this must be done before enable */
630 amd_pogo_errata_save_misc_reg(p_slot
);
631 retval
= board_added(p_slot
);
632 /* handle amd pogo errata; this must be done after enable */
633 amd_pogo_errata_restore_misc_reg(p_slot
);
635 retval
= board_added(p_slot
);
638 p_slot
->hpc_ops
->get_adapter_status(p_slot
,
639 &(p_slot
->presence_save
));
640 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
643 update_slot_info(p_slot
);
645 mutex_unlock(&p_slot
->ctrl
->crit_sect
);
650 int shpchp_disable_slot (struct slot
*p_slot
)
653 int rc
, retval
= -ENODEV
;
658 /* Check to see if (latch closed, card present, power on) */
659 mutex_lock(&p_slot
->ctrl
->crit_sect
);
661 rc
= p_slot
->hpc_ops
->get_adapter_status(p_slot
, &getstatus
);
662 if (rc
|| !getstatus
) {
663 info("%s: no adapter on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
666 rc
= p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
667 if (rc
|| getstatus
) {
668 info("%s: latch open on slot(%x)\n", __FUNCTION__
, p_slot
->number
);
671 rc
= p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
672 if (rc
|| !getstatus
) {
673 info("%s: already disabled slot(%x)\n", __FUNCTION__
, p_slot
->number
);
677 retval
= remove_board(p_slot
);
678 update_slot_info(p_slot
);
680 mutex_unlock(&p_slot
->ctrl
->crit_sect
);