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/slab.h>
34 #include <linux/pci.h>
38 static void interrupt_event_handler(struct work_struct *work);
40 static int queue_interrupt_event(struct slot *p_slot, u32 event_type)
42 struct event_info *info;
44 info = kmalloc(sizeof(*info), GFP_ATOMIC);
48 info->event_type = event_type;
49 info->p_slot = p_slot;
50 INIT_WORK(&info->work, interrupt_event_handler);
52 queue_work(p_slot->wq, &info->work);
57 u8 pciehp_handle_attention_button(struct slot *p_slot)
60 struct controller *ctrl = p_slot->ctrl;
62 /* Attention Button Change */
63 ctrl_dbg(ctrl, "Attention button interrupt received\n");
66 * Button pressed - See if need to TAKE ACTION!!!
68 ctrl_info(ctrl, "Button pressed on Slot(%s)\n", slot_name(p_slot));
69 event_type = INT_BUTTON_PRESS;
71 queue_interrupt_event(p_slot, event_type);
76 u8 pciehp_handle_switch_change(struct slot *p_slot)
80 struct controller *ctrl = p_slot->ctrl;
83 ctrl_dbg(ctrl, "Switch interrupt received\n");
85 pciehp_get_latch_status(p_slot, &getstatus);
90 ctrl_info(ctrl, "Latch open on Slot(%s)\n", slot_name(p_slot));
91 event_type = INT_SWITCH_OPEN;
96 ctrl_info(ctrl, "Latch close on Slot(%s)\n", slot_name(p_slot));
97 event_type = INT_SWITCH_CLOSE;
100 queue_interrupt_event(p_slot, event_type);
105 u8 pciehp_handle_presence_change(struct slot *p_slot)
109 struct controller *ctrl = p_slot->ctrl;
111 /* Presence Change */
112 ctrl_dbg(ctrl, "Presence/Notify input change\n");
114 /* Switch is open, assume a presence change
115 * Save the presence state
117 pciehp_get_adapter_status(p_slot, &presence_save);
122 ctrl_info(ctrl, "Card present on Slot(%s)\n", slot_name(p_slot));
123 event_type = INT_PRESENCE_ON;
128 ctrl_info(ctrl, "Card not present on Slot(%s)\n",
130 event_type = INT_PRESENCE_OFF;
133 queue_interrupt_event(p_slot, event_type);
138 u8 pciehp_handle_power_fault(struct slot *p_slot)
141 struct controller *ctrl = p_slot->ctrl;
144 ctrl_dbg(ctrl, "Power fault interrupt received\n");
145 ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
146 event_type = INT_POWER_FAULT;
147 ctrl_info(ctrl, "Power fault bit %x set\n", 0);
148 queue_interrupt_event(p_slot, event_type);
153 /* The following routines constitute the bulk of the
154 hotplug controller logic
157 static void set_slot_off(struct controller *ctrl, struct slot * pslot)
159 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
160 if (POWER_CTRL(ctrl)) {
161 pciehp_power_off_slot(pslot);
164 * After turning power off, we must wait for at least 1 second
165 * before taking any action that relies on power having been
166 * removed from the slot/adapter.
171 pciehp_green_led_off(pslot);
172 pciehp_set_attention_status(pslot, 1);
176 * board_added - Called after a board has been added to the system.
177 * @p_slot: &slot where board is added
179 * Turns power on for the board.
182 static int board_added(struct slot *p_slot)
185 struct controller *ctrl = p_slot->ctrl;
186 struct pci_bus *parent = ctrl->pcie->port->subordinate;
188 if (POWER_CTRL(ctrl)) {
190 retval = pciehp_power_on_slot(p_slot);
195 pciehp_green_led_blink(p_slot);
197 /* Check link training status */
198 retval = pciehp_check_link_status(ctrl);
200 ctrl_err(ctrl, "Failed to check link status\n");
204 /* Check for a power fault */
205 if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
206 ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
211 retval = pciehp_configure_device(p_slot);
213 ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
214 pci_domain_nr(parent), parent->number);
218 pciehp_green_led_on(p_slot);
222 set_slot_off(ctrl, p_slot);
227 * remove_board - Turns off slot and LEDs
228 * @p_slot: slot where board is being removed
230 static int remove_board(struct slot *p_slot)
233 struct controller *ctrl = p_slot->ctrl;
235 retval = pciehp_unconfigure_device(p_slot);
239 if (POWER_CTRL(ctrl)) {
240 pciehp_power_off_slot(p_slot);
243 * After turning power off, we must wait for at least 1 second
244 * before taking any action that relies on power having been
245 * removed from the slot/adapter.
250 /* turn off Green LED */
251 pciehp_green_led_off(p_slot);
255 struct power_work_info {
257 struct work_struct work;
261 * pciehp_power_thread - handle pushbutton events
262 * @work: &struct work_struct describing work to be done
264 * Scheduled procedure to handle blocking stuff for the pushbuttons.
265 * Handles all pending events and exits.
267 static void pciehp_power_thread(struct work_struct *work)
269 struct power_work_info *info =
270 container_of(work, struct power_work_info, work);
271 struct slot *p_slot = info->p_slot;
273 mutex_lock(&p_slot->lock);
274 switch (p_slot->state) {
276 mutex_unlock(&p_slot->lock);
277 ctrl_dbg(p_slot->ctrl,
278 "Disabling domain:bus:device=%04x:%02x:00\n",
279 pci_domain_nr(p_slot->ctrl->pcie->port->subordinate),
280 p_slot->ctrl->pcie->port->subordinate->number);
281 pciehp_disable_slot(p_slot);
282 mutex_lock(&p_slot->lock);
283 p_slot->state = STATIC_STATE;
286 mutex_unlock(&p_slot->lock);
287 if (pciehp_enable_slot(p_slot))
288 pciehp_green_led_off(p_slot);
289 mutex_lock(&p_slot->lock);
290 p_slot->state = STATIC_STATE;
295 mutex_unlock(&p_slot->lock);
300 void pciehp_queue_pushbutton_work(struct work_struct *work)
302 struct slot *p_slot = container_of(work, struct slot, work.work);
303 struct power_work_info *info;
305 info = kmalloc(sizeof(*info), GFP_KERNEL);
307 ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
311 info->p_slot = p_slot;
312 INIT_WORK(&info->work, pciehp_power_thread);
314 mutex_lock(&p_slot->lock);
315 switch (p_slot->state) {
316 case BLINKINGOFF_STATE:
317 p_slot->state = POWEROFF_STATE;
319 case BLINKINGON_STATE:
320 p_slot->state = POWERON_STATE;
326 queue_work(p_slot->wq, &info->work);
328 mutex_unlock(&p_slot->lock);
332 * Note: This function must be called with slot->lock held
334 static void handle_button_press_event(struct slot *p_slot)
336 struct controller *ctrl = p_slot->ctrl;
339 switch (p_slot->state) {
341 pciehp_get_power_status(p_slot, &getstatus);
343 p_slot->state = BLINKINGOFF_STATE;
345 "PCI slot #%s - powering off due to button "
346 "press.\n", slot_name(p_slot));
348 p_slot->state = BLINKINGON_STATE;
350 "PCI slot #%s - powering on due to button "
351 "press.\n", slot_name(p_slot));
353 /* blink green LED and turn off amber */
354 pciehp_green_led_blink(p_slot);
355 pciehp_set_attention_status(p_slot, 0);
356 queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
358 case BLINKINGOFF_STATE:
359 case BLINKINGON_STATE:
361 * Cancel if we are still blinking; this means that we
362 * press the attention again before the 5 sec. limit
363 * expires to cancel hot-add or hot-remove
365 ctrl_info(ctrl, "Button cancel on Slot(%s)\n", slot_name(p_slot));
366 cancel_delayed_work(&p_slot->work);
367 if (p_slot->state == BLINKINGOFF_STATE) {
368 pciehp_green_led_on(p_slot);
370 pciehp_green_led_off(p_slot);
372 pciehp_set_attention_status(p_slot, 0);
373 ctrl_info(ctrl, "PCI slot #%s - action canceled "
374 "due to button press\n", slot_name(p_slot));
375 p_slot->state = STATIC_STATE;
380 * Ignore if the slot is on power-on or power-off state;
381 * this means that the previous attention button action
382 * to hot-add or hot-remove is undergoing
384 ctrl_info(ctrl, "Button ignore on Slot(%s)\n", slot_name(p_slot));
387 ctrl_warn(ctrl, "Not a valid state\n");
393 * Note: This function must be called with slot->lock held
395 static void handle_surprise_event(struct slot *p_slot)
398 struct power_work_info *info;
400 info = kmalloc(sizeof(*info), GFP_KERNEL);
402 ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
406 info->p_slot = p_slot;
407 INIT_WORK(&info->work, pciehp_power_thread);
409 pciehp_get_adapter_status(p_slot, &getstatus);
411 p_slot->state = POWEROFF_STATE;
413 p_slot->state = POWERON_STATE;
415 queue_work(p_slot->wq, &info->work);
418 static void interrupt_event_handler(struct work_struct *work)
420 struct event_info *info = container_of(work, struct event_info, work);
421 struct slot *p_slot = info->p_slot;
422 struct controller *ctrl = p_slot->ctrl;
424 mutex_lock(&p_slot->lock);
425 switch (info->event_type) {
426 case INT_BUTTON_PRESS:
427 handle_button_press_event(p_slot);
429 case INT_POWER_FAULT:
430 if (!POWER_CTRL(ctrl))
432 pciehp_set_attention_status(p_slot, 1);
433 pciehp_green_led_off(p_slot);
435 case INT_PRESENCE_ON:
436 case INT_PRESENCE_OFF:
437 if (!HP_SUPR_RM(ctrl))
439 ctrl_dbg(ctrl, "Surprise Removal\n");
440 handle_surprise_event(p_slot);
445 mutex_unlock(&p_slot->lock);
450 int pciehp_enable_slot(struct slot *p_slot)
454 struct controller *ctrl = p_slot->ctrl;
456 pciehp_get_adapter_status(p_slot, &getstatus);
458 ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
461 if (MRL_SENS(p_slot->ctrl)) {
462 pciehp_get_latch_status(p_slot, &getstatus);
464 ctrl_info(ctrl, "Latch open on slot(%s)\n",
470 if (POWER_CTRL(p_slot->ctrl)) {
471 pciehp_get_power_status(p_slot, &getstatus);
473 ctrl_info(ctrl, "Already enabled on slot(%s)\n",
479 pciehp_get_latch_status(p_slot, &getstatus);
481 rc = board_added(p_slot);
483 pciehp_get_latch_status(p_slot, &getstatus);
489 int pciehp_disable_slot(struct slot *p_slot)
492 struct controller *ctrl = p_slot->ctrl;
497 if (!HP_SUPR_RM(p_slot->ctrl)) {
498 pciehp_get_adapter_status(p_slot, &getstatus);
500 ctrl_info(ctrl, "No adapter on slot(%s)\n",
506 if (MRL_SENS(p_slot->ctrl)) {
507 pciehp_get_latch_status(p_slot, &getstatus);
509 ctrl_info(ctrl, "Latch open on slot(%s)\n",
515 if (POWER_CTRL(p_slot->ctrl)) {
516 pciehp_get_power_status(p_slot, &getstatus);
518 ctrl_info(ctrl, "Already disabled on slot(%s)\n",
524 return remove_board(p_slot);
527 int pciehp_sysfs_enable_slot(struct slot *p_slot)
529 int retval = -ENODEV;
530 struct controller *ctrl = p_slot->ctrl;
532 mutex_lock(&p_slot->lock);
533 switch (p_slot->state) {
534 case BLINKINGON_STATE:
535 cancel_delayed_work(&p_slot->work);
537 p_slot->state = POWERON_STATE;
538 mutex_unlock(&p_slot->lock);
539 retval = pciehp_enable_slot(p_slot);
540 mutex_lock(&p_slot->lock);
541 p_slot->state = STATIC_STATE;
544 ctrl_info(ctrl, "Slot %s is already in powering on state\n",
547 case BLINKINGOFF_STATE:
549 ctrl_info(ctrl, "Already enabled on slot %s\n",
553 ctrl_err(ctrl, "Not a valid state on slot %s\n",
557 mutex_unlock(&p_slot->lock);
562 int pciehp_sysfs_disable_slot(struct slot *p_slot)
564 int retval = -ENODEV;
565 struct controller *ctrl = p_slot->ctrl;
567 mutex_lock(&p_slot->lock);
568 switch (p_slot->state) {
569 case BLINKINGOFF_STATE:
570 cancel_delayed_work(&p_slot->work);
572 p_slot->state = POWEROFF_STATE;
573 mutex_unlock(&p_slot->lock);
574 retval = pciehp_disable_slot(p_slot);
575 mutex_lock(&p_slot->lock);
576 p_slot->state = STATIC_STATE;
579 ctrl_info(ctrl, "Slot %s is already in powering off state\n",
582 case BLINKINGON_STATE:
584 ctrl_info(ctrl, "Already disabled on slot %s\n",
588 ctrl_err(ctrl, "Not a valid state on slot %s\n",
592 mutex_unlock(&p_slot->lock);