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 void pciehp_queue_interrupt_event(struct slot *p_slot, u32 event_type)
42 struct event_info *info;
44 info = kmalloc(sizeof(*info), GFP_ATOMIC);
46 ctrl_err(p_slot->ctrl, "dropped event %d (ENOMEM)\n", event_type);
50 INIT_WORK(&info->work, interrupt_event_handler);
51 info->event_type = event_type;
52 info->p_slot = p_slot;
53 queue_work(p_slot->wq, &info->work);
56 /* The following routines constitute the bulk of the
57 hotplug controller logic
60 static void set_slot_off(struct controller *ctrl, struct slot *pslot)
62 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
63 if (POWER_CTRL(ctrl)) {
64 pciehp_power_off_slot(pslot);
67 * After turning power off, we must wait for at least 1 second
68 * before taking any action that relies on power having been
69 * removed from the slot/adapter.
74 pciehp_green_led_off(pslot);
75 pciehp_set_attention_status(pslot, 1);
79 * board_added - Called after a board has been added to the system.
80 * @p_slot: &slot where board is added
82 * Turns power on for the board.
85 static int board_added(struct slot *p_slot)
88 struct controller *ctrl = p_slot->ctrl;
89 struct pci_bus *parent = ctrl->pcie->port->subordinate;
91 if (POWER_CTRL(ctrl)) {
93 retval = pciehp_power_on_slot(p_slot);
98 pciehp_green_led_blink(p_slot);
100 /* Check link training status */
101 retval = pciehp_check_link_status(ctrl);
103 ctrl_err(ctrl, "Failed to check link status\n");
107 /* Check for a power fault */
108 if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
109 ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(p_slot));
114 retval = pciehp_configure_device(p_slot);
116 ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
117 pci_domain_nr(parent), parent->number);
118 if (retval != -EEXIST)
122 pciehp_green_led_on(p_slot);
123 pciehp_set_attention_status(p_slot, 0);
127 set_slot_off(ctrl, p_slot);
132 * remove_board - Turns off slot and LEDs
133 * @p_slot: slot where board is being removed
135 static int remove_board(struct slot *p_slot)
138 struct controller *ctrl = p_slot->ctrl;
140 retval = pciehp_unconfigure_device(p_slot);
144 if (POWER_CTRL(ctrl)) {
145 pciehp_power_off_slot(p_slot);
148 * After turning power off, we must wait for at least 1 second
149 * before taking any action that relies on power having been
150 * removed from the slot/adapter.
155 /* turn off Green LED */
156 pciehp_green_led_off(p_slot);
160 struct power_work_info {
162 struct work_struct work;
164 #define DISABLE_REQ 0
169 * pciehp_power_thread - handle pushbutton events
170 * @work: &struct work_struct describing work to be done
172 * Scheduled procedure to handle blocking stuff for the pushbuttons.
173 * Handles all pending events and exits.
175 static void pciehp_power_thread(struct work_struct *work)
177 struct power_work_info *info =
178 container_of(work, struct power_work_info, work);
179 struct slot *p_slot = info->p_slot;
184 mutex_lock(&p_slot->hotplug_lock);
185 pciehp_disable_slot(p_slot);
186 mutex_unlock(&p_slot->hotplug_lock);
187 mutex_lock(&p_slot->lock);
188 p_slot->state = STATIC_STATE;
189 mutex_unlock(&p_slot->lock);
192 mutex_lock(&p_slot->hotplug_lock);
193 ret = pciehp_enable_slot(p_slot);
194 mutex_unlock(&p_slot->hotplug_lock);
196 pciehp_green_led_off(p_slot);
197 mutex_lock(&p_slot->lock);
198 p_slot->state = STATIC_STATE;
199 mutex_unlock(&p_slot->lock);
208 static void pciehp_queue_power_work(struct slot *p_slot, int req)
210 struct power_work_info *info;
212 p_slot->state = (req == ENABLE_REQ) ? POWERON_STATE : POWEROFF_STATE;
214 info = kmalloc(sizeof(*info), GFP_KERNEL);
216 ctrl_err(p_slot->ctrl, "no memory to queue %s request\n",
217 (req == ENABLE_REQ) ? "poweron" : "poweroff");
220 info->p_slot = p_slot;
221 INIT_WORK(&info->work, pciehp_power_thread);
223 queue_work(p_slot->wq, &info->work);
226 void pciehp_queue_pushbutton_work(struct work_struct *work)
228 struct slot *p_slot = container_of(work, struct slot, work.work);
230 mutex_lock(&p_slot->lock);
231 switch (p_slot->state) {
232 case BLINKINGOFF_STATE:
233 pciehp_queue_power_work(p_slot, DISABLE_REQ);
235 case BLINKINGON_STATE:
236 pciehp_queue_power_work(p_slot, ENABLE_REQ);
241 mutex_unlock(&p_slot->lock);
245 * Note: This function must be called with slot->lock held
247 static void handle_button_press_event(struct slot *p_slot)
249 struct controller *ctrl = p_slot->ctrl;
252 switch (p_slot->state) {
254 pciehp_get_power_status(p_slot, &getstatus);
256 p_slot->state = BLINKINGOFF_STATE;
257 ctrl_info(ctrl, "Slot(%s): Powering off due to button press\n",
260 p_slot->state = BLINKINGON_STATE;
261 ctrl_info(ctrl, "Slot(%s) Powering on due to button press\n",
264 /* blink green LED and turn off amber */
265 pciehp_green_led_blink(p_slot);
266 pciehp_set_attention_status(p_slot, 0);
267 queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
269 case BLINKINGOFF_STATE:
270 case BLINKINGON_STATE:
272 * Cancel if we are still blinking; this means that we
273 * press the attention again before the 5 sec. limit
274 * expires to cancel hot-add or hot-remove
276 ctrl_info(ctrl, "Slot(%s): Button cancel\n", slot_name(p_slot));
277 cancel_delayed_work(&p_slot->work);
278 if (p_slot->state == BLINKINGOFF_STATE)
279 pciehp_green_led_on(p_slot);
281 pciehp_green_led_off(p_slot);
282 pciehp_set_attention_status(p_slot, 0);
283 ctrl_info(ctrl, "Slot(%s): Action canceled due to button press\n",
285 p_slot->state = STATIC_STATE;
290 * Ignore if the slot is on power-on or power-off state;
291 * this means that the previous attention button action
292 * to hot-add or hot-remove is undergoing
294 ctrl_info(ctrl, "Slot(%s): Button ignored\n",
298 ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
299 slot_name(p_slot), p_slot->state);
305 * Note: This function must be called with slot->lock held
307 static void handle_link_event(struct slot *p_slot, u32 event)
309 struct controller *ctrl = p_slot->ctrl;
311 switch (p_slot->state) {
312 case BLINKINGON_STATE:
313 case BLINKINGOFF_STATE:
314 cancel_delayed_work(&p_slot->work);
317 pciehp_queue_power_work(p_slot, event == INT_LINK_UP ?
318 ENABLE_REQ : DISABLE_REQ);
321 if (event == INT_LINK_UP) {
322 ctrl_info(ctrl, "Slot(%s): Link Up event ignored; already powering on\n",
325 ctrl_info(ctrl, "Slot(%s): Link Down event queued; currently getting powered on\n",
327 pciehp_queue_power_work(p_slot, DISABLE_REQ);
331 if (event == INT_LINK_UP) {
332 ctrl_info(ctrl, "Slot(%s): Link Up event queued; currently getting powered off\n",
334 pciehp_queue_power_work(p_slot, ENABLE_REQ);
336 ctrl_info(ctrl, "Slot(%s): Link Down event ignored; already powering off\n",
341 ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
342 slot_name(p_slot), p_slot->state);
347 static void interrupt_event_handler(struct work_struct *work)
349 struct event_info *info = container_of(work, struct event_info, work);
350 struct slot *p_slot = info->p_slot;
351 struct controller *ctrl = p_slot->ctrl;
353 mutex_lock(&p_slot->lock);
354 switch (info->event_type) {
355 case INT_BUTTON_PRESS:
356 handle_button_press_event(p_slot);
358 case INT_POWER_FAULT:
359 if (!POWER_CTRL(ctrl))
361 pciehp_set_attention_status(p_slot, 1);
362 pciehp_green_led_off(p_slot);
364 case INT_PRESENCE_ON:
365 pciehp_queue_power_work(p_slot, ENABLE_REQ);
367 case INT_PRESENCE_OFF:
369 * Regardless of surprise capability, we need to
370 * definitely remove a card that has been pulled out!
372 pciehp_queue_power_work(p_slot, DISABLE_REQ);
376 handle_link_event(p_slot, info->event_type);
381 mutex_unlock(&p_slot->lock);
387 * Note: This function must be called with slot->hotplug_lock held
389 int pciehp_enable_slot(struct slot *p_slot)
392 struct controller *ctrl = p_slot->ctrl;
394 pciehp_get_adapter_status(p_slot, &getstatus);
396 ctrl_info(ctrl, "Slot(%s): No adapter\n", slot_name(p_slot));
399 if (MRL_SENS(p_slot->ctrl)) {
400 pciehp_get_latch_status(p_slot, &getstatus);
402 ctrl_info(ctrl, "Slot(%s): Latch open\n",
408 if (POWER_CTRL(p_slot->ctrl)) {
409 pciehp_get_power_status(p_slot, &getstatus);
411 ctrl_info(ctrl, "Slot(%s): Already enabled\n",
417 return board_added(p_slot);
421 * Note: This function must be called with slot->hotplug_lock held
423 int pciehp_disable_slot(struct slot *p_slot)
426 struct controller *ctrl = p_slot->ctrl;
431 if (POWER_CTRL(p_slot->ctrl)) {
432 pciehp_get_power_status(p_slot, &getstatus);
434 ctrl_info(ctrl, "Slot(%s): Already disabled\n",
440 return remove_board(p_slot);
443 int pciehp_sysfs_enable_slot(struct slot *p_slot)
445 int retval = -ENODEV;
446 struct controller *ctrl = p_slot->ctrl;
448 mutex_lock(&p_slot->lock);
449 switch (p_slot->state) {
450 case BLINKINGON_STATE:
451 cancel_delayed_work(&p_slot->work);
453 p_slot->state = POWERON_STATE;
454 mutex_unlock(&p_slot->lock);
455 mutex_lock(&p_slot->hotplug_lock);
456 retval = pciehp_enable_slot(p_slot);
457 mutex_unlock(&p_slot->hotplug_lock);
458 mutex_lock(&p_slot->lock);
459 p_slot->state = STATIC_STATE;
462 ctrl_info(ctrl, "Slot(%s): Already in powering on state\n",
465 case BLINKINGOFF_STATE:
467 ctrl_info(ctrl, "Slot(%s): Already enabled\n",
471 ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
472 slot_name(p_slot), p_slot->state);
475 mutex_unlock(&p_slot->lock);
480 int pciehp_sysfs_disable_slot(struct slot *p_slot)
482 int retval = -ENODEV;
483 struct controller *ctrl = p_slot->ctrl;
485 mutex_lock(&p_slot->lock);
486 switch (p_slot->state) {
487 case BLINKINGOFF_STATE:
488 cancel_delayed_work(&p_slot->work);
490 p_slot->state = POWEROFF_STATE;
491 mutex_unlock(&p_slot->lock);
492 mutex_lock(&p_slot->hotplug_lock);
493 retval = pciehp_disable_slot(p_slot);
494 mutex_unlock(&p_slot->hotplug_lock);
495 mutex_lock(&p_slot->lock);
496 p_slot->state = STATIC_STATE;
499 ctrl_info(ctrl, "Slot(%s): Already in powering off state\n",
502 case BLINKINGON_STATE:
504 ctrl_info(ctrl, "Slot(%s): Already disabled\n",
508 ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
509 slot_name(p_slot), p_slot->state);
512 mutex_unlock(&p_slot->lock);