blob: 4283ef56dbd977509ba6ab8567d91f1b7bbdc712 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * PCI Express Hot Plug Controller Driver
3 *
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
8 *
9 * All rights reserved.
10 *
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.
15 *
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
20 * details.
21 *
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.
25 *
Kristen Accardi8cf4c192005-08-16 15:16:10 -070026 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 *
28 */
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/module.h>
31#include <linux/kernel.h>
32#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/smp_lock.h>
34#include <linux/pci.h>
35#include "../pci.h"
36#include "pciehp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038static void interrupt_event_handler(struct controller *ctrl);
39
40static struct semaphore event_semaphore; /* mutex for process loop (up if something to process) */
41static struct semaphore event_exit; /* guard ensure thread has exited before calling it quits */
42static int event_finished;
43static unsigned long pushbutton_pending; /* = 0 */
44static unsigned long surprise_rm_pending; /* = 0 */
45
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -070046static inline char *slot_name(struct slot *p_slot)
47{
48 return p_slot->hotplug_slot->name;
49}
50
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080051u8 pciehp_handle_attention_button(u8 hp_slot, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 struct slot *p_slot;
54 u8 rc = 0;
55 u8 getstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 struct event_info *taskInfo;
57
58 /* Attention Button Change */
59 dbg("pciehp: Attention button interrupt received.\n");
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* This is the structure that tells the worker thread what to do */
62 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
63 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
64
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
66
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -080067 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 taskInfo->hp_slot = hp_slot;
69
70 rc++;
71
72 /*
73 * Button pressed - See if need to TAKE ACTION!!!
74 */
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -070075 info("Button pressed on Slot(%s)\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 taskInfo->event_type = INT_BUTTON_PRESS;
77
78 if ((p_slot->state == BLINKINGON_STATE)
79 || (p_slot->state == BLINKINGOFF_STATE)) {
80 /* Cancel if we are still blinking; this means that we press the
81 * attention again before the 5 sec. limit expires to cancel hot-add
82 * or hot-remove
83 */
84 taskInfo->event_type = INT_BUTTON_CANCEL;
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -070085 info("Button cancel on Slot(%s)\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 } else if ((p_slot->state == POWERON_STATE)
87 || (p_slot->state == POWEROFF_STATE)) {
88 /* Ignore if the slot is on power-on or power-off state; this
89 * means that the previous attention button action to hot-add or
90 * hot-remove is undergoing
91 */
92 taskInfo->event_type = INT_BUTTON_IGNORE;
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -070093 info("Button ignore on Slot(%s)\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
96 if (rc)
97 up(&event_semaphore); /* signal event thread that new event is posted */
98
99 return 0;
100
101}
102
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800103u8 pciehp_handle_switch_change(u8 hp_slot, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 struct slot *p_slot;
106 u8 rc = 0;
107 u8 getstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 struct event_info *taskInfo;
109
110 /* Switch Change */
111 dbg("pciehp: Switch interrupt received.\n");
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 /* This is the structure that tells the worker thread
114 * what to do
115 */
116 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800117 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 taskInfo->hp_slot = hp_slot;
119
120 rc++;
121 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
123
124 if (getstatus) {
125 /*
126 * Switch opened
127 */
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700128 info("Latch open on Slot(%s)\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 taskInfo->event_type = INT_SWITCH_OPEN;
130 } else {
131 /*
132 * Switch closed
133 */
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700134 info("Latch close on Slot(%s)\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 taskInfo->event_type = INT_SWITCH_CLOSE;
136 }
137
138 if (rc)
139 up(&event_semaphore); /* signal event thread that new event is posted */
140
141 return rc;
142}
143
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800144u8 pciehp_handle_presence_change(u8 hp_slot, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 struct slot *p_slot;
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800147 u8 presence_save, rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 struct event_info *taskInfo;
149
150 /* Presence Change */
151 dbg("pciehp: Presence/Notify input change.\n");
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 /* This is the structure that tells the worker thread
154 * what to do
155 */
156 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800157 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 taskInfo->hp_slot = hp_slot;
159
160 rc++;
161 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
162
163 /* Switch is open, assume a presence change
164 * Save the presence state
165 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800166 p_slot->hpc_ops->get_adapter_status(p_slot, &presence_save);
167 if (presence_save) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /*
169 * Card Present
170 */
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700171 info("Card present on Slot(%s)\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 taskInfo->event_type = INT_PRESENCE_ON;
173 } else {
174 /*
175 * Not Present
176 */
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700177 info("Card not present on Slot(%s)\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 taskInfo->event_type = INT_PRESENCE_OFF;
179 }
180
181 if (rc)
182 up(&event_semaphore); /* signal event thread that new event is posted */
183
184 return rc;
185}
186
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800187u8 pciehp_handle_power_fault(u8 hp_slot, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 struct slot *p_slot;
190 u8 rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 struct event_info *taskInfo;
192
193 /* power fault */
194 dbg("pciehp: Power fault interrupt received.\n");
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 /* this is the structure that tells the worker thread
197 * what to do
198 */
199 taskInfo = &(ctrl->event_queue[ctrl->next_event]);
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800200 ctrl->next_event = (ctrl->next_event + 1) % MAX_EVENTS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 taskInfo->hp_slot = hp_slot;
202
203 rc++;
204 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
205
206 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
207 /*
208 * power fault Cleared
209 */
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700210 info("Power fault cleared on Slot(%s)\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 taskInfo->event_type = INT_POWER_FAULT_CLEAR;
212 } else {
213 /*
214 * power fault
215 */
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700216 info("Power fault on Slot(%s)\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 taskInfo->event_type = INT_POWER_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 info("power fault bit %x set\n", hp_slot);
219 }
220 if (rc)
221 up(&event_semaphore); /* signal event thread that new event is posted */
222
223 return rc;
224}
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226/* The following routines constitute the bulk of the
227 hotplug controller logic
228 */
229
230static void set_slot_off(struct controller *ctrl, struct slot * pslot)
231{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
233 if (POWER_CTRL(ctrl->ctrlcap)) {
234 if (pslot->hpc_ops->power_off_slot(pslot)) {
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800235 err("%s: Issue of Slot Power Off command failed\n",
236 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800241 if (PWR_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 pslot->hpc_ops->green_led_off(pslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800244 if (ATTN_LED(ctrl->ctrlcap)) {
245 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
246 err("%s: Issue of Set Attention Led command failed\n",
247 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return;
249 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253/**
254 * board_added - Called after a board has been added to the system.
255 *
256 * Turns power on for the board
257 * Configures board
258 *
259 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800260static int board_added(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 u8 hp_slot;
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800263 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800264 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800266 hp_slot = p_slot->device - ctrl->slot_device_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800268 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
269 __FUNCTION__, p_slot->device,
270 ctrl->slot_device_offset, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 if (POWER_CTRL(ctrl->ctrlcap)) {
273 /* Power on slot */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800274 retval = p_slot->hpc_ops->power_on_slot(p_slot);
275 if (retval)
276 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800279 if (PWR_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 p_slot->hpc_ops->green_led_blink(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282 /* Wait for ~1 second */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800283 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800285 /* Check link training status */
286 retval = p_slot->hpc_ops->check_lnk_status(ctrl);
287 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 err("%s: Failed to check link status\n", __FUNCTION__);
289 set_slot_off(ctrl, p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800290 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* Check for a power fault */
Rajesh Shah5a49f202005-11-23 15:44:54 -0800294 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
295 dbg("%s: power fault detected\n", __FUNCTION__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800296 retval = POWER_FAILURE;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800297 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 }
299
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800300 retval = pciehp_configure_device(p_slot);
301 if (retval) {
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800302 err("Cannot add device 0x%x:%x\n", p_slot->bus,
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800303 p_slot->device);
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800304 goto err_exit;
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800307 /*
308 * Some PCI Express root ports require fixup after hot-plug operation.
309 */
310 if (pcie_mch_quirk)
311 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800312 if (PWR_LED(ctrl->ctrlcap))
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800313 p_slot->hpc_ops->green_led_on(p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 return 0;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800316
317err_exit:
318 set_slot_off(ctrl, p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800319 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/**
323 * remove_board - Turns off slot and LED's
324 *
325 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800326static int remove_board(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 u8 device;
329 u8 hp_slot;
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800330 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800331 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800333 retval = pciehp_unconfigure_device(p_slot);
334 if (retval)
335 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800337 device = p_slot->device;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800338 hp_slot = p_slot->device - ctrl->slot_device_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
340
341 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (POWER_CTRL(ctrl->ctrlcap)) {
344 /* power off slot */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800345 retval = p_slot->hpc_ops->power_off_slot(p_slot);
346 if (retval) {
347 err("%s: Issue of Slot Disable command failed\n",
348 __FUNCTION__);
349 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 }
352
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800353 if (PWR_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 /* turn off Green LED */
355 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 return 0;
358}
359
360
361static void pushbutton_helper_thread(unsigned long data)
362{
363 pushbutton_pending = data;
364
365 up(&event_semaphore);
366}
367
368/**
369 * pciehp_pushbutton_thread
370 *
371 * Scheduled procedure to handle blocking stuff for the pushbuttons
372 * Handles all pending events and exits.
373 *
374 */
375static void pciehp_pushbutton_thread(unsigned long slot)
376{
377 struct slot *p_slot = (struct slot *) slot;
378 u8 getstatus;
379
380 pushbutton_pending = 0;
381
382 if (!p_slot) {
383 dbg("%s: Error! slot NULL\n", __FUNCTION__);
384 return;
385 }
386
387 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
388 if (getstatus) {
389 p_slot->state = POWEROFF_STATE;
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800390 dbg("%s: disabling bus:device(%x:%x)\n", __FUNCTION__,
391 p_slot->bus, p_slot->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 pciehp_disable_slot(p_slot);
394 p_slot->state = STATIC_STATE;
395 } else {
396 p_slot->state = POWERON_STATE;
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800397 dbg("%s: adding bus:device(%x:%x)\n", __FUNCTION__,
398 p_slot->bus, p_slot->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800400 if (pciehp_enable_slot(p_slot) &&
401 PWR_LED(p_slot->ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 p_slot->hpc_ops->green_led_off(p_slot);
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 p_slot->state = STATIC_STATE;
405 }
406
407 return;
408}
409
410/**
411 * pciehp_surprise_rm_thread
412 *
413 * Scheduled procedure to handle blocking stuff for the surprise removal
414 * Handles all pending events and exits.
415 *
416 */
417static void pciehp_surprise_rm_thread(unsigned long slot)
418{
419 struct slot *p_slot = (struct slot *) slot;
420 u8 getstatus;
421
422 surprise_rm_pending = 0;
423
424 if (!p_slot) {
425 dbg("%s: Error! slot NULL\n", __FUNCTION__);
426 return;
427 }
428
429 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
430 if (!getstatus) {
431 p_slot->state = POWEROFF_STATE;
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800432 dbg("%s: removing bus:device(%x:%x)\n",
433 __FUNCTION__, p_slot->bus, p_slot->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 pciehp_disable_slot(p_slot);
436 p_slot->state = STATIC_STATE;
437 } else {
438 p_slot->state = POWERON_STATE;
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800439 dbg("%s: adding bus:device(%x:%x)\n",
440 __FUNCTION__, p_slot->bus, p_slot->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800442 if (pciehp_enable_slot(p_slot) &&
443 PWR_LED(p_slot->ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 p_slot->hpc_ops->green_led_off(p_slot);
445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 p_slot->state = STATIC_STATE;
447 }
448
449 return;
450}
451
452
453
454/* this is the main worker thread */
455static int event_thread(void* data)
456{
457 struct controller *ctrl;
458 lock_kernel();
459 daemonize("pciehpd_event");
460
461 unlock_kernel();
462
463 while (1) {
464 dbg("!!!!event_thread sleeping\n");
465 down_interruptible (&event_semaphore);
466 dbg("event_thread woken finished = %d\n", event_finished);
467 if (event_finished || signal_pending(current))
468 break;
469 /* Do stuff here */
470 if (pushbutton_pending)
471 pciehp_pushbutton_thread(pushbutton_pending);
472 else if (surprise_rm_pending)
473 pciehp_surprise_rm_thread(surprise_rm_pending);
474 else
475 for (ctrl = pciehp_ctrl_list; ctrl; ctrl=ctrl->next)
476 interrupt_event_handler(ctrl);
477 }
478 dbg("event_thread signals exit\n");
479 up(&event_exit);
480 return 0;
481}
482
483int pciehp_event_start_thread(void)
484{
485 int pid;
486
487 /* initialize our semaphores */
488 init_MUTEX_LOCKED(&event_exit);
489 event_finished=0;
490
491 init_MUTEX_LOCKED(&event_semaphore);
492 pid = kernel_thread(event_thread, NULL, 0);
493
494 if (pid < 0) {
495 err ("Can't start up our event thread\n");
496 return -1;
497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return 0;
499}
500
501
502void pciehp_event_stop_thread(void)
503{
504 event_finished = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 up(&event_semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 down(&event_exit);
507}
508
509
510static int update_slot_info(struct slot *slot)
511{
512 struct hotplug_slot_info *info;
513 /* char buffer[SLOT_NAME_SIZE]; */
514 int result;
515
516 info = kmalloc(sizeof(struct hotplug_slot_info), GFP_KERNEL);
517 if (!info)
518 return -ENOMEM;
519
520 /* make_slot_name (&buffer[0], SLOT_NAME_SIZE, slot); */
521
522 slot->hpc_ops->get_power_status(slot, &(info->power_status));
523 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
524 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
525 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
526
527 /* result = pci_hp_change_slot_info(buffer, info); */
528 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
529 kfree (info);
530 return result;
531}
532
533static void interrupt_event_handler(struct controller *ctrl)
534{
535 int loop = 0;
536 int change = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 u8 hp_slot;
538 u8 getstatus;
539 struct slot *p_slot;
540
541 while (change) {
542 change = 0;
543
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800544 for (loop = 0; loop < MAX_EVENTS; loop++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (ctrl->event_queue[loop].event_type != 0) {
546 hp_slot = ctrl->event_queue[loop].hp_slot;
547
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (ctrl->event_queue[loop].event_type == INT_BUTTON_CANCEL) {
551 dbg("button cancel\n");
552 del_timer(&p_slot->task_event);
553
554 switch (p_slot->state) {
555 case BLINKINGOFF_STATE:
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800556 if (PWR_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 p_slot->hpc_ops->green_led_on(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800559 if (ATTN_LED(ctrl->ctrlcap))
560 p_slot->hpc_ops->set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 break;
562 case BLINKINGON_STATE:
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800563 if (PWR_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800566 if (ATTN_LED(ctrl->ctrlcap))
567 p_slot->hpc_ops->set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 break;
569 default:
570 warn("Not a valid state\n");
571 return;
572 }
Kenji Kaneshige15232ec2006-12-21 17:01:07 -0800573 info("PCI slot #%s - action canceled due to button press.\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 p_slot->state = STATIC_STATE;
575 }
576 /* ***********Button Pressed (No action on 1st press...) */
577 else if (ctrl->event_queue[loop].event_type == INT_BUTTON_PRESS) {
578
579 if (ATTN_BUTTN(ctrl->ctrlcap)) {
580 dbg("Button pressed\n");
581 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
582 if (getstatus) {
583 /* slot is on */
584 dbg("slot is on\n");
585 p_slot->state = BLINKINGOFF_STATE;
Kenji Kaneshige15232ec2006-12-21 17:01:07 -0800586 info("PCI slot #%s - powering off due to button press.\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 } else {
588 /* slot is off */
589 dbg("slot is off\n");
590 p_slot->state = BLINKINGON_STATE;
Kenji Kaneshige15232ec2006-12-21 17:01:07 -0800591 info("PCI slot #%s - powering on due to button press.\n", slot_name(p_slot));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 /* blink green LED and turn off amber */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800595 if (PWR_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 p_slot->hpc_ops->green_led_blink(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800598 if (ATTN_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 p_slot->hpc_ops->set_attention_status(p_slot, 0);
600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 init_timer(&p_slot->task_event);
602 p_slot->task_event.expires = jiffies + 5 * HZ; /* 5 second delay */
603 p_slot->task_event.function = (void (*)(unsigned long)) pushbutton_helper_thread;
604 p_slot->task_event.data = (unsigned long) p_slot;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 add_timer(&p_slot->task_event);
607 }
608 }
609 /***********POWER FAULT********************/
610 else if (ctrl->event_queue[loop].event_type == INT_POWER_FAULT) {
611 if (POWER_CTRL(ctrl->ctrlcap)) {
612 dbg("power fault\n");
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800613 if (ATTN_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 p_slot->hpc_ops->set_attention_status(p_slot, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800616 if (PWR_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 }
619 }
620 /***********SURPRISE REMOVAL********************/
621 else if ((ctrl->event_queue[loop].event_type == INT_PRESENCE_ON) ||
622 (ctrl->event_queue[loop].event_type == INT_PRESENCE_OFF)) {
623 if (HP_SUPR_RM(ctrl->ctrlcap)) {
624 dbg("Surprise Removal\n");
625 if (p_slot) {
626 surprise_rm_pending = (unsigned long) p_slot;
627 up(&event_semaphore);
628 update_slot_info(p_slot);
629 }
630 }
631 } else {
632 /* refresh notification */
633 if (p_slot)
634 update_slot_info(p_slot);
635 }
636
637 ctrl->event_queue[loop].event_type = 0;
638
639 change = 1;
640 }
641 } /* End of FOR loop */
642 }
643}
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645int pciehp_enable_slot(struct slot *p_slot)
646{
647 u8 getstatus = 0;
648 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* Check to see if (latch closed, card present, power off) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100651 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
653 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
654 if (rc || !getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700655 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
656 slot_name(p_slot));
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100657 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700658 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 }
660 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
661 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
662 if (rc || getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700663 info("%s: latch open on slot(%s)\n", __FUNCTION__,
664 slot_name(p_slot));
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100665 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700666 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668 }
669
670 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
671 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
672 if (rc || getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700673 info("%s: already enabled on slot(%s)\n", __FUNCTION__,
674 slot_name(p_slot));
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100675 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700676 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800682 rc = board_added(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 }
686
Eric Sesterhenn9ef99772006-09-25 00:56:53 +0200687 update_slot_info(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -0700689 mutex_unlock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 return rc;
691}
692
693
694int pciehp_disable_slot(struct slot *p_slot)
695{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 u8 getstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 if (!p_slot->ctrl)
700 return 1;
701
702 /* Check to see if (latch closed, card present, power on) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100703 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (!HP_SUPR_RM(p_slot->ctrl->ctrlcap)) {
706 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
707 if (ret || !getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700708 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
709 slot_name(p_slot));
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100710 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700711 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 }
713 }
714
715 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
716 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
717 if (ret || getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700718 info("%s: latch open on slot(%s)\n", __FUNCTION__,
719 slot_name(p_slot));
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100720 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700721 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 }
723 }
724
725 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
726 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
727 if (ret || !getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700728 info("%s: already disabled slot(%s)\n", __FUNCTION__,
729 slot_name(p_slot));
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100730 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700731 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
733 }
734
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800735 ret = remove_board(p_slot);
736 update_slot_info(p_slot);
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -0700737
738 mutex_unlock(&p_slot->ctrl->crit_sect);
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800739 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741