2 * PCI Express 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(struct work_struct
*work
);
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
);
53 schedule_work(&info
->work
);
58 u8
pciehp_handle_attention_button(u8 hp_slot
, struct controller
*ctrl
)
63 /* Attention Button Change */
64 dbg("pciehp: Attention button interrupt received.\n");
66 p_slot
= pciehp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
69 * Button pressed - See if need to TAKE ACTION!!!
71 info("Button pressed on Slot(%s)\n", p_slot
->name
);
72 event_type
= INT_BUTTON_PRESS
;
74 queue_interrupt_event(p_slot
, event_type
);
79 u8
pciehp_handle_switch_change(u8 hp_slot
, struct controller
*ctrl
)
86 dbg("pciehp: Switch interrupt received.\n");
88 p_slot
= pciehp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
89 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
95 info("Latch open on Slot(%s)\n", p_slot
->name
);
96 event_type
= INT_SWITCH_OPEN
;
101 info("Latch close on Slot(%s)\n", p_slot
->name
);
102 event_type
= INT_SWITCH_CLOSE
;
105 queue_interrupt_event(p_slot
, event_type
);
110 u8
pciehp_handle_presence_change(u8 hp_slot
, struct controller
*ctrl
)
116 /* Presence Change */
117 dbg("pciehp: Presence/Notify input change.\n");
119 p_slot
= pciehp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
121 /* Switch is open, assume a presence change
122 * Save the presence state
124 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &presence_save
);
129 info("Card present on Slot(%s)\n", p_slot
->name
);
130 event_type
= INT_PRESENCE_ON
;
135 info("Card not present on Slot(%s)\n", p_slot
->name
);
136 event_type
= INT_PRESENCE_OFF
;
139 queue_interrupt_event(p_slot
, event_type
);
144 u8
pciehp_handle_power_fault(u8 hp_slot
, struct controller
*ctrl
)
150 dbg("pciehp: Power fault interrupt received.\n");
152 p_slot
= pciehp_find_slot(ctrl
, hp_slot
+ ctrl
->slot_device_offset
);
154 if ( !(p_slot
->hpc_ops
->query_power_fault(p_slot
))) {
156 * power fault Cleared
158 info("Power fault cleared on Slot(%s)\n", p_slot
->name
);
159 event_type
= INT_POWER_FAULT_CLEAR
;
164 info("Power fault on Slot(%s)\n", p_slot
->name
);
165 event_type
= INT_POWER_FAULT
;
166 info("power fault bit %x set\n", hp_slot
);
169 queue_interrupt_event(p_slot
, event_type
);
174 /* The following routines constitute the bulk of the
175 hotplug controller logic
178 static void set_slot_off(struct controller
*ctrl
, struct slot
* pslot
)
180 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
181 if (POWER_CTRL(ctrl
)) {
182 if (pslot
->hpc_ops
->power_off_slot(pslot
)) {
183 err("%s: Issue of Slot Power Off command failed\n",
190 pslot
->hpc_ops
->green_led_off(pslot
);
192 if (ATTN_LED(ctrl
)) {
193 if (pslot
->hpc_ops
->set_attention_status(pslot
, 1)) {
194 err("%s: Issue of Set Attention Led command failed\n",
202 * board_added - Called after a board has been added to the system.
203 * @p_slot: &slot where board is added
205 * Turns power on for the board.
208 static int board_added(struct slot
*p_slot
)
211 struct controller
*ctrl
= p_slot
->ctrl
;
213 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
214 __func__
, p_slot
->device
,
215 ctrl
->slot_device_offset
, p_slot
->hp_slot
);
217 if (POWER_CTRL(ctrl
)) {
219 retval
= p_slot
->hpc_ops
->power_on_slot(p_slot
);
225 p_slot
->hpc_ops
->green_led_blink(p_slot
);
227 /* Wait for ~1 second */
230 /* Check link training status */
231 retval
= p_slot
->hpc_ops
->check_lnk_status(ctrl
);
233 err("%s: Failed to check link status\n", __func__
);
234 set_slot_off(ctrl
, p_slot
);
238 /* Check for a power fault */
239 if (p_slot
->hpc_ops
->query_power_fault(p_slot
)) {
240 dbg("%s: power fault detected\n", __func__
);
241 retval
= POWER_FAILURE
;
245 retval
= pciehp_configure_device(p_slot
);
247 err("Cannot add device 0x%x:%x\n", p_slot
->bus
,
253 * Some PCI Express root ports require fixup after hot-plug operation.
256 pci_fixup_device(pci_fixup_final
, ctrl
->pci_dev
);
258 p_slot
->hpc_ops
->green_led_on(p_slot
);
263 set_slot_off(ctrl
, p_slot
);
268 * remove_board - Turns off slot and LEDs
269 * @p_slot: slot where board is being removed
271 static int remove_board(struct slot
*p_slot
)
274 struct controller
*ctrl
= p_slot
->ctrl
;
276 retval
= pciehp_unconfigure_device(p_slot
);
280 dbg("In %s, hp_slot = %d\n", __func__
, p_slot
->hp_slot
);
282 if (POWER_CTRL(ctrl
)) {
284 retval
= p_slot
->hpc_ops
->power_off_slot(p_slot
);
286 err("%s: Issue of Slot Disable command failed\n",
293 /* turn off Green LED */
294 p_slot
->hpc_ops
->green_led_off(p_slot
);
299 struct power_work_info
{
301 struct work_struct work
;
305 * pciehp_power_thread - handle pushbutton events
306 * @work: &struct work_struct describing work to be done
308 * Scheduled procedure to handle blocking stuff for the pushbuttons.
309 * Handles all pending events and exits.
311 static void pciehp_power_thread(struct work_struct
*work
)
313 struct power_work_info
*info
=
314 container_of(work
, struct power_work_info
, work
);
315 struct slot
*p_slot
= info
->p_slot
;
317 mutex_lock(&p_slot
->lock
);
318 switch (p_slot
->state
) {
320 mutex_unlock(&p_slot
->lock
);
321 dbg("%s: disabling bus:device(%x:%x)\n",
322 __func__
, p_slot
->bus
, p_slot
->device
);
323 pciehp_disable_slot(p_slot
);
324 mutex_lock(&p_slot
->lock
);
325 p_slot
->state
= STATIC_STATE
;
328 mutex_unlock(&p_slot
->lock
);
329 if (pciehp_enable_slot(p_slot
) &&
330 PWR_LED(p_slot
->ctrl
))
331 p_slot
->hpc_ops
->green_led_off(p_slot
);
332 mutex_lock(&p_slot
->lock
);
333 p_slot
->state
= STATIC_STATE
;
338 mutex_unlock(&p_slot
->lock
);
343 void pciehp_queue_pushbutton_work(struct work_struct
*work
)
345 struct slot
*p_slot
= container_of(work
, struct slot
, work
.work
);
346 struct power_work_info
*info
;
348 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
350 err("%s: Cannot allocate memory\n", __func__
);
353 info
->p_slot
= p_slot
;
354 INIT_WORK(&info
->work
, pciehp_power_thread
);
356 mutex_lock(&p_slot
->lock
);
357 switch (p_slot
->state
) {
358 case BLINKINGOFF_STATE
:
359 p_slot
->state
= POWEROFF_STATE
;
361 case BLINKINGON_STATE
:
362 p_slot
->state
= POWERON_STATE
;
367 queue_work(pciehp_wq
, &info
->work
);
369 mutex_unlock(&p_slot
->lock
);
372 static int update_slot_info(struct slot
*slot
)
374 struct hotplug_slot_info
*info
;
377 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
381 slot
->hpc_ops
->get_power_status(slot
, &(info
->power_status
));
382 slot
->hpc_ops
->get_attention_status(slot
, &(info
->attention_status
));
383 slot
->hpc_ops
->get_latch_status(slot
, &(info
->latch_status
));
384 slot
->hpc_ops
->get_adapter_status(slot
, &(info
->adapter_status
));
386 result
= pci_hp_change_slot_info(slot
->hotplug_slot
, info
);
392 * Note: This function must be called with slot->lock held
394 static void handle_button_press_event(struct slot
*p_slot
)
396 struct controller
*ctrl
= p_slot
->ctrl
;
399 switch (p_slot
->state
) {
401 p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
403 p_slot
->state
= BLINKINGOFF_STATE
;
404 info("PCI slot #%s - powering off due to button "
405 "press.\n", p_slot
->name
);
407 p_slot
->state
= BLINKINGON_STATE
;
408 info("PCI slot #%s - powering on due to button "
409 "press.\n", p_slot
->name
);
411 /* blink green LED and turn off amber */
413 p_slot
->hpc_ops
->green_led_blink(p_slot
);
415 p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
417 schedule_delayed_work(&p_slot
->work
, 5*HZ
);
419 case BLINKINGOFF_STATE
:
420 case BLINKINGON_STATE
:
422 * Cancel if we are still blinking; this means that we
423 * press the attention again before the 5 sec. limit
424 * expires to cancel hot-add or hot-remove
426 info("Button cancel on Slot(%s)\n", p_slot
->name
);
427 dbg("%s: button cancel\n", __func__
);
428 cancel_delayed_work(&p_slot
->work
);
429 if (p_slot
->state
== BLINKINGOFF_STATE
) {
431 p_slot
->hpc_ops
->green_led_on(p_slot
);
434 p_slot
->hpc_ops
->green_led_off(p_slot
);
437 p_slot
->hpc_ops
->set_attention_status(p_slot
, 0);
438 info("PCI slot #%s - action canceled due to button press\n",
440 p_slot
->state
= STATIC_STATE
;
445 * Ignore if the slot is on power-on or power-off state;
446 * this means that the previous attention button action
447 * to hot-add or hot-remove is undergoing
449 info("Button ignore on Slot(%s)\n", p_slot
->name
);
450 update_slot_info(p_slot
);
453 warn("Not a valid state\n");
459 * Note: This function must be called with slot->lock held
461 static void handle_surprise_event(struct slot
*p_slot
)
464 struct power_work_info
*info
;
466 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
468 err("%s: Cannot allocate memory\n", __func__
);
471 info
->p_slot
= p_slot
;
472 INIT_WORK(&info
->work
, pciehp_power_thread
);
474 p_slot
->hpc_ops
->get_adapter_status(p_slot
, &getstatus
);
476 p_slot
->state
= POWEROFF_STATE
;
478 p_slot
->state
= POWERON_STATE
;
480 queue_work(pciehp_wq
, &info
->work
);
483 static void interrupt_event_handler(struct work_struct
*work
)
485 struct event_info
*info
= container_of(work
, struct event_info
, work
);
486 struct slot
*p_slot
= info
->p_slot
;
487 struct controller
*ctrl
= p_slot
->ctrl
;
489 mutex_lock(&p_slot
->lock
);
490 switch (info
->event_type
) {
491 case INT_BUTTON_PRESS
:
492 handle_button_press_event(p_slot
);
494 case INT_POWER_FAULT
:
495 if (!POWER_CTRL(ctrl
))
498 p_slot
->hpc_ops
->set_attention_status(p_slot
, 1);
500 p_slot
->hpc_ops
->green_led_off(p_slot
);
502 case INT_PRESENCE_ON
:
503 case INT_PRESENCE_OFF
:
504 if (!HP_SUPR_RM(ctrl
))
506 dbg("Surprise Removal\n");
507 update_slot_info(p_slot
);
508 handle_surprise_event(p_slot
);
511 update_slot_info(p_slot
);
514 mutex_unlock(&p_slot
->lock
);
519 int pciehp_enable_slot(struct slot
*p_slot
)
524 /* Check to see if (latch closed, card present, power off) */
525 mutex_lock(&p_slot
->ctrl
->crit_sect
);
527 rc
= p_slot
->hpc_ops
->get_adapter_status(p_slot
, &getstatus
);
528 if (rc
|| !getstatus
) {
529 info("%s: no adapter on slot(%s)\n", __func__
,
531 mutex_unlock(&p_slot
->ctrl
->crit_sect
);
534 if (MRL_SENS(p_slot
->ctrl
)) {
535 rc
= p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
536 if (rc
|| getstatus
) {
537 info("%s: latch open on slot(%s)\n", __func__
,
539 mutex_unlock(&p_slot
->ctrl
->crit_sect
);
544 if (POWER_CTRL(p_slot
->ctrl
)) {
545 rc
= p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
546 if (rc
|| getstatus
) {
547 info("%s: already enabled on slot(%s)\n", __func__
,
549 mutex_unlock(&p_slot
->ctrl
->crit_sect
);
554 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
556 rc
= board_added(p_slot
);
558 p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
561 update_slot_info(p_slot
);
563 mutex_unlock(&p_slot
->ctrl
->crit_sect
);
568 int pciehp_disable_slot(struct slot
*p_slot
)
576 /* Check to see if (latch closed, card present, power on) */
577 mutex_lock(&p_slot
->ctrl
->crit_sect
);
579 if (!HP_SUPR_RM(p_slot
->ctrl
)) {
580 ret
= p_slot
->hpc_ops
->get_adapter_status(p_slot
, &getstatus
);
581 if (ret
|| !getstatus
) {
582 info("%s: no adapter on slot(%s)\n", __func__
,
584 mutex_unlock(&p_slot
->ctrl
->crit_sect
);
589 if (MRL_SENS(p_slot
->ctrl
)) {
590 ret
= p_slot
->hpc_ops
->get_latch_status(p_slot
, &getstatus
);
591 if (ret
|| getstatus
) {
592 info("%s: latch open on slot(%s)\n", __func__
,
594 mutex_unlock(&p_slot
->ctrl
->crit_sect
);
599 if (POWER_CTRL(p_slot
->ctrl
)) {
600 ret
= p_slot
->hpc_ops
->get_power_status(p_slot
, &getstatus
);
601 if (ret
|| !getstatus
) {
602 info("%s: already disabled slot(%s)\n", __func__
,
604 mutex_unlock(&p_slot
->ctrl
->crit_sect
);
609 ret
= remove_board(p_slot
);
610 update_slot_info(p_slot
);
612 mutex_unlock(&p_slot
->ctrl
->crit_sect
);
616 int pciehp_sysfs_enable_slot(struct slot
*p_slot
)
618 int retval
= -ENODEV
;
620 mutex_lock(&p_slot
->lock
);
621 switch (p_slot
->state
) {
622 case BLINKINGON_STATE
:
623 cancel_delayed_work(&p_slot
->work
);
625 p_slot
->state
= POWERON_STATE
;
626 mutex_unlock(&p_slot
->lock
);
627 retval
= pciehp_enable_slot(p_slot
);
628 mutex_lock(&p_slot
->lock
);
629 p_slot
->state
= STATIC_STATE
;
632 info("Slot %s is already in powering on state\n",
635 case BLINKINGOFF_STATE
:
637 info("Already enabled on slot %s\n", p_slot
->name
);
640 err("Not a valid state on slot %s\n", p_slot
->name
);
643 mutex_unlock(&p_slot
->lock
);
648 int pciehp_sysfs_disable_slot(struct slot
*p_slot
)
650 int retval
= -ENODEV
;
652 mutex_lock(&p_slot
->lock
);
653 switch (p_slot
->state
) {
654 case BLINKINGOFF_STATE
:
655 cancel_delayed_work(&p_slot
->work
);
657 p_slot
->state
= POWEROFF_STATE
;
658 mutex_unlock(&p_slot
->lock
);
659 retval
= pciehp_disable_slot(p_slot
);
660 mutex_lock(&p_slot
->lock
);
661 p_slot
->state
= STATIC_STATE
;
664 info("Slot %s is already in powering off state\n",
667 case BLINKINGON_STATE
:
669 info("Already disabled on slot %s\n", p_slot
->name
);
672 err("Not a valid state on slot %s\n", p_slot
->name
);
675 mutex_unlock(&p_slot
->lock
);