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/pm_runtime.h>
35 #include <linux/pci.h>
39 static void interrupt_event_handler(struct work_struct
*work
);
41 void pciehp_queue_interrupt_event(struct slot
*p_slot
, u32 event_type
)
43 struct event_info
*info
;
45 info
= kmalloc(sizeof(*info
), GFP_ATOMIC
);
47 ctrl_err(p_slot
->ctrl
, "dropped event %d (ENOMEM)\n", event_type
);
51 INIT_WORK(&info
->work
, interrupt_event_handler
);
52 info
->event_type
= event_type
;
53 info
->p_slot
= p_slot
;
54 queue_work(p_slot
->wq
, &info
->work
);
57 /* The following routines constitute the bulk of the
58 hotplug controller logic
61 static void set_slot_off(struct controller
*ctrl
, struct slot
*pslot
)
63 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
64 if (POWER_CTRL(ctrl
)) {
65 pciehp_power_off_slot(pslot
);
68 * After turning power off, we must wait for at least 1 second
69 * before taking any action that relies on power having been
70 * removed from the slot/adapter.
75 pciehp_green_led_off(pslot
);
76 pciehp_set_attention_status(pslot
, 1);
80 * board_added - Called after a board has been added to the system.
81 * @p_slot: &slot where board is added
83 * Turns power on for the board.
86 static int board_added(struct slot
*p_slot
)
89 struct controller
*ctrl
= p_slot
->ctrl
;
90 struct pci_bus
*parent
= ctrl
->pcie
->port
->subordinate
;
92 if (POWER_CTRL(ctrl
)) {
94 retval
= pciehp_power_on_slot(p_slot
);
99 pciehp_green_led_blink(p_slot
);
101 /* Check link training status */
102 pm_runtime_get_sync(&ctrl
->pcie
->port
->dev
);
103 retval
= pciehp_check_link_status(ctrl
);
105 ctrl_err(ctrl
, "Failed to check link status\n");
109 /* Check for a power fault */
110 if (ctrl
->power_fault_detected
|| pciehp_query_power_fault(p_slot
)) {
111 ctrl_err(ctrl
, "Slot(%s): Power fault\n", slot_name(p_slot
));
116 retval
= pciehp_configure_device(p_slot
);
118 ctrl_err(ctrl
, "Cannot add device at %04x:%02x:00\n",
119 pci_domain_nr(parent
), parent
->number
);
120 if (retval
!= -EEXIST
)
123 pm_runtime_put(&ctrl
->pcie
->port
->dev
);
125 pciehp_green_led_on(p_slot
);
126 pciehp_set_attention_status(p_slot
, 0);
130 pm_runtime_put(&ctrl
->pcie
->port
->dev
);
131 set_slot_off(ctrl
, p_slot
);
136 * remove_board - Turns off slot and LEDs
137 * @p_slot: slot where board is being removed
139 static int remove_board(struct slot
*p_slot
)
142 struct controller
*ctrl
= p_slot
->ctrl
;
144 pm_runtime_get_sync(&ctrl
->pcie
->port
->dev
);
145 retval
= pciehp_unconfigure_device(p_slot
);
146 pm_runtime_put(&ctrl
->pcie
->port
->dev
);
150 if (POWER_CTRL(ctrl
)) {
151 pciehp_power_off_slot(p_slot
);
154 * After turning power off, we must wait for at least 1 second
155 * before taking any action that relies on power having been
156 * removed from the slot/adapter.
161 /* turn off Green LED */
162 pciehp_green_led_off(p_slot
);
166 struct power_work_info
{
168 struct work_struct work
;
170 #define DISABLE_REQ 0
175 * pciehp_power_thread - handle pushbutton events
176 * @work: &struct work_struct describing work to be done
178 * Scheduled procedure to handle blocking stuff for the pushbuttons.
179 * Handles all pending events and exits.
181 static void pciehp_power_thread(struct work_struct
*work
)
183 struct power_work_info
*info
=
184 container_of(work
, struct power_work_info
, work
);
185 struct slot
*p_slot
= info
->p_slot
;
190 mutex_lock(&p_slot
->hotplug_lock
);
191 pciehp_disable_slot(p_slot
);
192 mutex_unlock(&p_slot
->hotplug_lock
);
193 mutex_lock(&p_slot
->lock
);
194 p_slot
->state
= STATIC_STATE
;
195 mutex_unlock(&p_slot
->lock
);
198 mutex_lock(&p_slot
->hotplug_lock
);
199 ret
= pciehp_enable_slot(p_slot
);
200 mutex_unlock(&p_slot
->hotplug_lock
);
202 pciehp_green_led_off(p_slot
);
203 mutex_lock(&p_slot
->lock
);
204 p_slot
->state
= STATIC_STATE
;
205 mutex_unlock(&p_slot
->lock
);
214 static void pciehp_queue_power_work(struct slot
*p_slot
, int req
)
216 struct power_work_info
*info
;
218 p_slot
->state
= (req
== ENABLE_REQ
) ? POWERON_STATE
: POWEROFF_STATE
;
220 info
= kmalloc(sizeof(*info
), GFP_KERNEL
);
222 ctrl_err(p_slot
->ctrl
, "no memory to queue %s request\n",
223 (req
== ENABLE_REQ
) ? "poweron" : "poweroff");
226 info
->p_slot
= p_slot
;
227 INIT_WORK(&info
->work
, pciehp_power_thread
);
229 queue_work(p_slot
->wq
, &info
->work
);
232 void pciehp_queue_pushbutton_work(struct work_struct
*work
)
234 struct slot
*p_slot
= container_of(work
, struct slot
, work
.work
);
236 mutex_lock(&p_slot
->lock
);
237 switch (p_slot
->state
) {
238 case BLINKINGOFF_STATE
:
239 pciehp_queue_power_work(p_slot
, DISABLE_REQ
);
241 case BLINKINGON_STATE
:
242 pciehp_queue_power_work(p_slot
, ENABLE_REQ
);
247 mutex_unlock(&p_slot
->lock
);
251 * Note: This function must be called with slot->lock held
253 static void handle_button_press_event(struct slot
*p_slot
)
255 struct controller
*ctrl
= p_slot
->ctrl
;
258 switch (p_slot
->state
) {
260 pciehp_get_power_status(p_slot
, &getstatus
);
262 p_slot
->state
= BLINKINGOFF_STATE
;
263 ctrl_info(ctrl
, "Slot(%s): Powering off due to button press\n",
266 p_slot
->state
= BLINKINGON_STATE
;
267 ctrl_info(ctrl
, "Slot(%s) Powering on due to button press\n",
270 /* blink green LED and turn off amber */
271 pciehp_green_led_blink(p_slot
);
272 pciehp_set_attention_status(p_slot
, 0);
273 queue_delayed_work(p_slot
->wq
, &p_slot
->work
, 5*HZ
);
275 case BLINKINGOFF_STATE
:
276 case BLINKINGON_STATE
:
278 * Cancel if we are still blinking; this means that we
279 * press the attention again before the 5 sec. limit
280 * expires to cancel hot-add or hot-remove
282 ctrl_info(ctrl
, "Slot(%s): Button cancel\n", slot_name(p_slot
));
283 cancel_delayed_work(&p_slot
->work
);
284 if (p_slot
->state
== BLINKINGOFF_STATE
)
285 pciehp_green_led_on(p_slot
);
287 pciehp_green_led_off(p_slot
);
288 pciehp_set_attention_status(p_slot
, 0);
289 ctrl_info(ctrl
, "Slot(%s): Action canceled due to button press\n",
291 p_slot
->state
= STATIC_STATE
;
296 * Ignore if the slot is on power-on or power-off state;
297 * this means that the previous attention button action
298 * to hot-add or hot-remove is undergoing
300 ctrl_info(ctrl
, "Slot(%s): Button ignored\n",
304 ctrl_err(ctrl
, "Slot(%s): Ignoring invalid state %#x\n",
305 slot_name(p_slot
), p_slot
->state
);
311 * Note: This function must be called with slot->lock held
313 static void handle_link_event(struct slot
*p_slot
, u32 event
)
315 struct controller
*ctrl
= p_slot
->ctrl
;
317 switch (p_slot
->state
) {
318 case BLINKINGON_STATE
:
319 case BLINKINGOFF_STATE
:
320 cancel_delayed_work(&p_slot
->work
);
323 pciehp_queue_power_work(p_slot
, event
== INT_LINK_UP
?
324 ENABLE_REQ
: DISABLE_REQ
);
327 if (event
== INT_LINK_UP
) {
328 ctrl_info(ctrl
, "Slot(%s): Link Up event ignored; already powering on\n",
331 ctrl_info(ctrl
, "Slot(%s): Link Down event queued; currently getting powered on\n",
333 pciehp_queue_power_work(p_slot
, DISABLE_REQ
);
337 if (event
== INT_LINK_UP
) {
338 ctrl_info(ctrl
, "Slot(%s): Link Up event queued; currently getting powered off\n",
340 pciehp_queue_power_work(p_slot
, ENABLE_REQ
);
342 ctrl_info(ctrl
, "Slot(%s): Link Down event ignored; already powering off\n",
347 ctrl_err(ctrl
, "Slot(%s): Ignoring invalid state %#x\n",
348 slot_name(p_slot
), p_slot
->state
);
353 static void interrupt_event_handler(struct work_struct
*work
)
355 struct event_info
*info
= container_of(work
, struct event_info
, work
);
356 struct slot
*p_slot
= info
->p_slot
;
357 struct controller
*ctrl
= p_slot
->ctrl
;
359 mutex_lock(&p_slot
->lock
);
360 switch (info
->event_type
) {
361 case INT_BUTTON_PRESS
:
362 handle_button_press_event(p_slot
);
364 case INT_POWER_FAULT
:
365 if (!POWER_CTRL(ctrl
))
367 pciehp_set_attention_status(p_slot
, 1);
368 pciehp_green_led_off(p_slot
);
370 case INT_PRESENCE_ON
:
371 pciehp_queue_power_work(p_slot
, ENABLE_REQ
);
373 case INT_PRESENCE_OFF
:
375 * Regardless of surprise capability, we need to
376 * definitely remove a card that has been pulled out!
378 pciehp_queue_power_work(p_slot
, DISABLE_REQ
);
382 handle_link_event(p_slot
, info
->event_type
);
387 mutex_unlock(&p_slot
->lock
);
393 * Note: This function must be called with slot->hotplug_lock held
395 int pciehp_enable_slot(struct slot
*p_slot
)
398 struct controller
*ctrl
= p_slot
->ctrl
;
400 pciehp_get_adapter_status(p_slot
, &getstatus
);
402 ctrl_info(ctrl
, "Slot(%s): No adapter\n", slot_name(p_slot
));
405 if (MRL_SENS(p_slot
->ctrl
)) {
406 pciehp_get_latch_status(p_slot
, &getstatus
);
408 ctrl_info(ctrl
, "Slot(%s): Latch open\n",
414 if (POWER_CTRL(p_slot
->ctrl
)) {
415 pciehp_get_power_status(p_slot
, &getstatus
);
417 ctrl_info(ctrl
, "Slot(%s): Already enabled\n",
423 return board_added(p_slot
);
427 * Note: This function must be called with slot->hotplug_lock held
429 int pciehp_disable_slot(struct slot
*p_slot
)
432 struct controller
*ctrl
= p_slot
->ctrl
;
437 if (POWER_CTRL(p_slot
->ctrl
)) {
438 pciehp_get_power_status(p_slot
, &getstatus
);
440 ctrl_info(ctrl
, "Slot(%s): Already disabled\n",
446 return remove_board(p_slot
);
449 int pciehp_sysfs_enable_slot(struct slot
*p_slot
)
451 int retval
= -ENODEV
;
452 struct controller
*ctrl
= p_slot
->ctrl
;
454 mutex_lock(&p_slot
->lock
);
455 switch (p_slot
->state
) {
456 case BLINKINGON_STATE
:
457 cancel_delayed_work(&p_slot
->work
);
459 p_slot
->state
= POWERON_STATE
;
460 mutex_unlock(&p_slot
->lock
);
461 mutex_lock(&p_slot
->hotplug_lock
);
462 retval
= pciehp_enable_slot(p_slot
);
463 mutex_unlock(&p_slot
->hotplug_lock
);
464 mutex_lock(&p_slot
->lock
);
465 p_slot
->state
= STATIC_STATE
;
468 ctrl_info(ctrl
, "Slot(%s): Already in powering on state\n",
471 case BLINKINGOFF_STATE
:
473 ctrl_info(ctrl
, "Slot(%s): Already enabled\n",
477 ctrl_err(ctrl
, "Slot(%s): Invalid state %#x\n",
478 slot_name(p_slot
), p_slot
->state
);
481 mutex_unlock(&p_slot
->lock
);
486 int pciehp_sysfs_disable_slot(struct slot
*p_slot
)
488 int retval
= -ENODEV
;
489 struct controller
*ctrl
= p_slot
->ctrl
;
491 mutex_lock(&p_slot
->lock
);
492 switch (p_slot
->state
) {
493 case BLINKINGOFF_STATE
:
494 cancel_delayed_work(&p_slot
->work
);
496 p_slot
->state
= POWEROFF_STATE
;
497 mutex_unlock(&p_slot
->lock
);
498 mutex_lock(&p_slot
->hotplug_lock
);
499 retval
= pciehp_disable_slot(p_slot
);
500 mutex_unlock(&p_slot
->hotplug_lock
);
501 mutex_lock(&p_slot
->lock
);
502 p_slot
->state
= STATIC_STATE
;
505 ctrl_info(ctrl
, "Slot(%s): Already in powering off state\n",
508 case BLINKINGON_STATE
:
510 ctrl_info(ctrl
, "Slot(%s): Already disabled\n",
514 ctrl_err(ctrl
, "Slot(%s): Invalid state %#x\n",
515 slot_name(p_slot
), p_slot
->state
);
518 mutex_unlock(&p_slot
->lock
);