blob: 342cfc6c1de87afb7b0604906b2b17b79a8c597e [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>
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080035#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include "../pci.h"
37#include "pciehp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080039static void interrupt_event_handler(struct work_struct *work);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080040static int pciehp_disable_slot(struct slot *p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080042static int queue_interrupt_event(struct slot *p_slot, u32 event_type)
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -070043{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080044 struct event_info *info;
45
46 info = kmalloc(sizeof(*info), GFP_ATOMIC);
47 if (!info)
48 return -ENOMEM;
49
50 info->event_type = event_type;
51 info->p_slot = p_slot;
52 INIT_WORK(&info->work, interrupt_event_handler);
53
54 schedule_work(&info->work);
55
56 return 0;
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -070057}
58
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080059u8 pciehp_handle_attention_button(u8 hp_slot, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct slot *p_slot;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080062 u32 event_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
64 /* Attention Button Change */
65 dbg("pciehp: Attention button interrupt received.\n");
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 /*
70 * Button pressed - See if need to TAKE ACTION!!!
71 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080072 info("Button pressed on Slot(%s)\n", p_slot->name);
73 event_type = INT_BUTTON_PRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080075 queue_interrupt_event(p_slot, event_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
77 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
Kenji Kaneshige48fe3912006-12-21 17:01:04 -080080u8 pciehp_handle_switch_change(u8 hp_slot, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct slot *p_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 u8 getstatus;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080084 u32 event_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* Switch Change */
87 dbg("pciehp: Switch interrupt received.\n");
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
91
92 if (getstatus) {
93 /*
94 * Switch opened
95 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080096 info("Latch open on Slot(%s)\n", p_slot->name);
97 event_type = INT_SWITCH_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 } else {
99 /*
100 * Switch closed
101 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800102 info("Latch close on Slot(%s)\n", p_slot->name);
103 event_type = INT_SWITCH_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800106 queue_interrupt_event(p_slot, event_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800108 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800111u8 pciehp_handle_presence_change(u8 hp_slot, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 struct slot *p_slot;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800114 u32 event_type;
115 u8 presence_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 /* Presence Change */
118 dbg("pciehp: Presence/Notify input change.\n");
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
121
122 /* Switch is open, assume a presence change
123 * Save the presence state
124 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800125 p_slot->hpc_ops->get_adapter_status(p_slot, &presence_save);
126 if (presence_save) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 /*
128 * Card Present
129 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800130 info("Card present on Slot(%s)\n", p_slot->name);
131 event_type = INT_PRESENCE_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } else {
133 /*
134 * Not Present
135 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800136 info("Card not present on Slot(%s)\n", p_slot->name);
137 event_type = INT_PRESENCE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800140 queue_interrupt_event(p_slot, event_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Kenji Kaneshige48fe3912006-12-21 17:01:04 -0800145u8 pciehp_handle_power_fault(u8 hp_slot, struct controller *ctrl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 struct slot *p_slot;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800148 u32 event_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 /* power fault */
151 dbg("pciehp: Power fault interrupt received.\n");
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
154
155 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
156 /*
157 * power fault Cleared
158 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800159 info("Power fault cleared on Slot(%s)\n", p_slot->name);
160 event_type = INT_POWER_FAULT_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 } else {
162 /*
163 * power fault
164 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800165 info("Power fault on Slot(%s)\n", p_slot->name);
166 event_type = INT_POWER_FAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 info("power fault bit %x set\n", hp_slot);
168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800170 queue_interrupt_event(p_slot, event_type);
171
172 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700175/* The following routines constitute the bulk of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 hotplug controller logic
177 */
178
179static void set_slot_off(struct controller *ctrl, struct slot * pslot)
180{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
182 if (POWER_CTRL(ctrl->ctrlcap)) {
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700183 if (pslot->hpc_ops->power_off_slot(pslot)) {
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800184 err("%s: Issue of Slot Power Off command failed\n",
185 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800190 if (PWR_LED(ctrl->ctrlcap))
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700191 pslot->hpc_ops->green_led_off(pslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800193 if (ATTN_LED(ctrl->ctrlcap)) {
194 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
195 err("%s: Issue of Set Attention Led command failed\n",
196 __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return;
198 }
Kenji Kaneshige5b57a6c2007-07-05 11:10:45 -0700199 /*
200 * After turning power off, we must wait for at least
201 * 1 second before taking any action that relies on
202 * power having been removed from the slot/adapter.
203 */
204 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208/**
209 * board_added - Called after a board has been added to the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -0800210 * @p_slot: &slot where board is added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800212 * Turns power on for the board.
213 * Configures board.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800215static int board_added(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
217 u8 hp_slot;
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800218 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800219 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800221 hp_slot = p_slot->device - ctrl->slot_device_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800223 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
224 __FUNCTION__, p_slot->device,
225 ctrl->slot_device_offset, hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 if (POWER_CTRL(ctrl->ctrlcap)) {
228 /* Power on slot */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800229 retval = p_slot->hpc_ops->power_on_slot(p_slot);
230 if (retval)
231 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700233
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800234 if (PWR_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 p_slot->hpc_ops->green_led_blink(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 /* Wait for ~1 second */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800238 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800240 /* Check link training status */
241 retval = p_slot->hpc_ops->check_lnk_status(ctrl);
242 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 err("%s: Failed to check link status\n", __FUNCTION__);
244 set_slot_off(ctrl, p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800245 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 /* Check for a power fault */
Rajesh Shah5a49f202005-11-23 15:44:54 -0800249 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
250 dbg("%s: power fault detected\n", __FUNCTION__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800251 retval = POWER_FAILURE;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800252 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 }
254
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800255 retval = pciehp_configure_device(p_slot);
256 if (retval) {
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800257 err("Cannot add device 0x%x:%x\n", p_slot->bus,
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800258 p_slot->device);
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800259 goto err_exit;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800262 /*
263 * Some PCI Express root ports require fixup after hot-plug operation.
264 */
265 if (pcie_mch_quirk)
266 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800267 if (PWR_LED(ctrl->ctrlcap))
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800268 p_slot->hpc_ops->green_led_on(p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800269
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return 0;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800271
272err_exit:
273 set_slot_off(ctrl, p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800274 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800278 * remove_board - Turns off slot and LEDs
279 * @p_slot: slot where board is being removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800281static int remove_board(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 u8 device;
284 u8 hp_slot;
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800285 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800286 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800288 retval = pciehp_unconfigure_device(p_slot);
289 if (retval)
290 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800292 device = p_slot->device;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800293 hp_slot = p_slot->device - ctrl->slot_device_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 p_slot = pciehp_find_slot(ctrl, hp_slot + ctrl->slot_device_offset);
295
296 dbg("In %s, hp_slot = %d\n", __FUNCTION__, hp_slot);
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (POWER_CTRL(ctrl->ctrlcap)) {
299 /* power off slot */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800300 retval = p_slot->hpc_ops->power_off_slot(p_slot);
301 if (retval) {
302 err("%s: Issue of Slot Disable command failed\n",
303 __FUNCTION__);
304 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800308 if (PWR_LED(ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 /* turn off Green LED */
310 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 return 0;
313}
314
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800315struct power_work_info {
316 struct slot *p_slot;
317 struct work_struct work;
318};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800321 * pciehp_power_thread - handle pushbutton events
322 * @work: &struct work_struct describing work to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800324 * Scheduled procedure to handle blocking stuff for the pushbuttons.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 * Handles all pending events and exits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800327static void pciehp_power_thread(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800329 struct power_work_info *info =
330 container_of(work, struct power_work_info, work);
331 struct slot *p_slot = info->p_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800333 mutex_lock(&p_slot->lock);
334 switch (p_slot->state) {
335 case POWEROFF_STATE:
336 mutex_unlock(&p_slot->lock);
337 dbg("%s: disabling bus:device(%x:%x)\n",
338 __FUNCTION__, p_slot->bus, p_slot->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 pciehp_disable_slot(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800340 mutex_lock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 p_slot->state = STATIC_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800342 break;
343 case POWERON_STATE:
344 mutex_unlock(&p_slot->lock);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800345 if (pciehp_enable_slot(p_slot) &&
346 PWR_LED(p_slot->ctrl->ctrlcap))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 p_slot->hpc_ops->green_led_off(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800348 mutex_lock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 p_slot->state = STATIC_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800350 break;
351 default:
352 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800354 mutex_unlock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800356 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Kristen Carlson Accardie325e1f2007-03-21 11:45:31 -0700359void pciehp_queue_pushbutton_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800361 struct slot *p_slot = container_of(work, struct slot, work.work);
362 struct power_work_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800364 info = kmalloc(sizeof(*info), GFP_KERNEL);
365 if (!info) {
366 err("%s: Cannot allocate memory\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 return;
368 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800369 info->p_slot = p_slot;
370 INIT_WORK(&info->work, pciehp_power_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800372 mutex_lock(&p_slot->lock);
373 switch (p_slot->state) {
374 case BLINKINGOFF_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 p_slot->state = POWEROFF_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800376 break;
377 case BLINKINGON_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 p_slot->state = POWERON_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800379 break;
380 default:
381 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800383 queue_work(pciehp_wq, &info->work);
384 out:
385 mutex_unlock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388static int update_slot_info(struct slot *slot)
389{
390 struct hotplug_slot_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 int result;
392
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800393 info = kmalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!info)
395 return -ENOMEM;
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 slot->hpc_ops->get_power_status(slot, &(info->power_status));
398 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
399 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
400 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
403 kfree (info);
404 return result;
405}
406
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800407/*
408 * Note: This function must be called with slot->lock held
409 */
410static void handle_button_press_event(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800412 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 u8 getstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800415 switch (p_slot->state) {
416 case STATIC_STATE:
417 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
418 if (getstatus) {
419 p_slot->state = BLINKINGOFF_STATE;
420 info("PCI slot #%s - powering off due to button "
421 "press.\n", p_slot->name);
422 } else {
423 p_slot->state = BLINKINGON_STATE;
424 info("PCI slot #%s - powering on due to button "
425 "press.\n", p_slot->name);
426 }
427 /* blink green LED and turn off amber */
428 if (PWR_LED(ctrl->ctrlcap))
429 p_slot->hpc_ops->green_led_blink(p_slot);
430 if (ATTN_LED(ctrl->ctrlcap))
431 p_slot->hpc_ops->set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800433 schedule_delayed_work(&p_slot->work, 5*HZ);
434 break;
435 case BLINKINGOFF_STATE:
436 case BLINKINGON_STATE:
437 /*
438 * Cancel if we are still blinking; this means that we
439 * press the attention again before the 5 sec. limit
440 * expires to cancel hot-add or hot-remove
441 */
442 info("Button cancel on Slot(%s)\n", p_slot->name);
443 dbg("%s: button cancel\n", __FUNCTION__);
444 cancel_delayed_work(&p_slot->work);
445 if (p_slot->state == BLINKINGOFF_STATE) {
446 if (PWR_LED(ctrl->ctrlcap))
447 p_slot->hpc_ops->green_led_on(p_slot);
448 } else {
449 if (PWR_LED(ctrl->ctrlcap))
450 p_slot->hpc_ops->green_led_off(p_slot);
451 }
452 if (ATTN_LED(ctrl->ctrlcap))
453 p_slot->hpc_ops->set_attention_status(p_slot, 0);
454 info("PCI slot #%s - action canceled due to button press\n",
455 p_slot->name);
456 p_slot->state = STATIC_STATE;
457 break;
458 case POWEROFF_STATE:
459 case POWERON_STATE:
460 /*
461 * Ignore if the slot is on power-on or power-off state;
462 * this means that the previous attention button action
463 * to hot-add or hot-remove is undergoing
464 */
465 info("Button ignore on Slot(%s)\n", p_slot->name);
466 update_slot_info(p_slot);
467 break;
468 default:
469 warn("Not a valid state\n");
470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472}
473
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800474/*
475 * Note: This function must be called with slot->lock held
476 */
477static void handle_surprise_event(struct slot *p_slot)
478{
479 u8 getstatus;
480 struct power_work_info *info;
481
482 info = kmalloc(sizeof(*info), GFP_KERNEL);
483 if (!info) {
484 err("%s: Cannot allocate memory\n", __FUNCTION__);
485 return;
486 }
487 info->p_slot = p_slot;
488 INIT_WORK(&info->work, pciehp_power_thread);
489
490 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
491 if (!getstatus)
492 p_slot->state = POWEROFF_STATE;
493 else
494 p_slot->state = POWERON_STATE;
495
496 queue_work(pciehp_wq, &info->work);
497}
498
499static void interrupt_event_handler(struct work_struct *work)
500{
501 struct event_info *info = container_of(work, struct event_info, work);
502 struct slot *p_slot = info->p_slot;
503 struct controller *ctrl = p_slot->ctrl;
504
505 mutex_lock(&p_slot->lock);
506 switch (info->event_type) {
507 case INT_BUTTON_PRESS:
508 handle_button_press_event(p_slot);
509 break;
510 case INT_POWER_FAULT:
511 if (!POWER_CTRL(ctrl->ctrlcap))
512 break;
513 if (ATTN_LED(ctrl->ctrlcap))
514 p_slot->hpc_ops->set_attention_status(p_slot, 1);
515 if (PWR_LED(ctrl->ctrlcap))
516 p_slot->hpc_ops->green_led_off(p_slot);
517 break;
518 case INT_PRESENCE_ON:
519 case INT_PRESENCE_OFF:
520 if (!HP_SUPR_RM(ctrl->ctrlcap))
521 break;
522 dbg("Surprise Removal\n");
523 update_slot_info(p_slot);
524 handle_surprise_event(p_slot);
525 break;
526 default:
527 update_slot_info(p_slot);
528 break;
529 }
530 mutex_unlock(&p_slot->lock);
531
532 kfree(info);
533}
534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535int pciehp_enable_slot(struct slot *p_slot)
536{
537 u8 getstatus = 0;
538 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540 /* Check to see if (latch closed, card present, power off) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100541 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
544 if (rc || !getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700545 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800546 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100547 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700548 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700550 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
552 if (rc || getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700553 info("%s: latch open on slot(%s)\n", __FUNCTION__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800554 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100555 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700556 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 }
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700559
560 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
562 if (rc || getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700563 info("%s: already enabled on slot(%s)\n", __FUNCTION__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800564 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100565 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700566 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 }
568 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800572 rc = board_added(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 }
576
Eric Sesterhenn9ef99772006-09-25 00:56:53 +0200577 update_slot_info(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -0700579 mutex_unlock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return rc;
581}
582
583
584int pciehp_disable_slot(struct slot *p_slot)
585{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 u8 getstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589 if (!p_slot->ctrl)
590 return 1;
591
592 /* Check to see if (latch closed, card present, power on) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100593 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700595 if (!HP_SUPR_RM(p_slot->ctrl->ctrlcap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
597 if (ret || !getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700598 info("%s: no adapter on slot(%s)\n", __FUNCTION__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800599 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100600 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700601 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603 }
604
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700605 if (MRL_SENS(p_slot->ctrl->ctrlcap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
607 if (ret || getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700608 info("%s: latch open on slot(%s)\n", __FUNCTION__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800609 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100610 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700611 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613 }
614
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700615 if (POWER_CTRL(p_slot->ctrl->ctrlcap)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
617 if (ret || !getstatus) {
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -0700618 info("%s: already disabled slot(%s)\n", __FUNCTION__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800619 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100620 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700621 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Kenji Kaneshige5b57a6c2007-07-05 11:10:45 -0700623 /*
624 * After turning power off, we must wait for at least
625 * 1 second before taking any action that relies on
626 * power having been removed from the slot/adapter.
627 */
628 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800631 ret = remove_board(p_slot);
632 update_slot_info(p_slot);
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -0700633
634 mutex_unlock(&p_slot->ctrl->crit_sect);
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800635 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800638int pciehp_sysfs_enable_slot(struct slot *p_slot)
639{
640 int retval = -ENODEV;
641
642 mutex_lock(&p_slot->lock);
643 switch (p_slot->state) {
644 case BLINKINGON_STATE:
645 cancel_delayed_work(&p_slot->work);
646 case STATIC_STATE:
647 p_slot->state = POWERON_STATE;
648 mutex_unlock(&p_slot->lock);
649 retval = pciehp_enable_slot(p_slot);
650 mutex_lock(&p_slot->lock);
651 p_slot->state = STATIC_STATE;
652 break;
653 case POWERON_STATE:
654 info("Slot %s is already in powering on state\n",
655 p_slot->name);
656 break;
657 case BLINKINGOFF_STATE:
658 case POWEROFF_STATE:
659 info("Already enabled on slot %s\n", p_slot->name);
660 break;
661 default:
662 err("Not a valid state on slot %s\n", p_slot->name);
663 break;
664 }
665 mutex_unlock(&p_slot->lock);
666
667 return retval;
668}
669
670int pciehp_sysfs_disable_slot(struct slot *p_slot)
671{
672 int retval = -ENODEV;
673
674 mutex_lock(&p_slot->lock);
675 switch (p_slot->state) {
676 case BLINKINGOFF_STATE:
677 cancel_delayed_work(&p_slot->work);
678 case STATIC_STATE:
679 p_slot->state = POWEROFF_STATE;
680 mutex_unlock(&p_slot->lock);
681 retval = pciehp_disable_slot(p_slot);
682 mutex_lock(&p_slot->lock);
683 p_slot->state = STATIC_STATE;
684 break;
685 case POWEROFF_STATE:
686 info("Slot %s is already in powering off state\n",
687 p_slot->name);
688 break;
689 case BLINKINGON_STATE:
690 case POWERON_STATE:
691 info("Already disabled on slot %s\n", p_slot->name);
692 break;
693 default:
694 err("Not a valid state on slot %s\n", p_slot->name);
695 break;
696 }
697 mutex_unlock(&p_slot->lock);
698
699 return retval;
700}