blob: 96a5d55a49835e43caadb3b2b0cd52210fd2fce9 [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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080041static int queue_interrupt_event(struct slot *p_slot, u32 event_type)
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -070042{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080043 struct event_info *info;
44
45 info = kmalloc(sizeof(*info), GFP_ATOMIC);
46 if (!info)
47 return -ENOMEM;
48
49 info->event_type = event_type;
50 info->p_slot = p_slot;
51 INIT_WORK(&info->work, interrupt_event_handler);
52
53 schedule_work(&info->work);
54
55 return 0;
Kenji Kaneshige49ed2b42006-09-22 10:17:10 -070056}
57
Kenji Kaneshigedbd79ae2008-05-27 19:03:16 +090058u8 pciehp_handle_attention_button(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080060 u32 event_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /* Attention Button Change */
63 dbg("pciehp: Attention button interrupt received.\n");
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080064
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 /*
66 * Button pressed - See if need to TAKE ACTION!!!
67 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080068 info("Button pressed on Slot(%s)\n", p_slot->name);
69 event_type = INT_BUTTON_PRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080071 queue_interrupt_event(p_slot, event_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
Kenji Kaneshigedbd79ae2008-05-27 19:03:16 +090076u8 pciehp_handle_switch_change(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 u8 getstatus;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080079 u32 event_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 /* Switch Change */
82 dbg("pciehp: Switch interrupt received.\n");
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 if (getstatus) {
86 /*
87 * Switch opened
88 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080089 info("Latch open on Slot(%s)\n", p_slot->name);
90 event_type = INT_SWITCH_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 } else {
92 /*
93 * Switch closed
94 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080095 info("Latch close on Slot(%s)\n", p_slot->name);
96 event_type = INT_SWITCH_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080099 queue_interrupt_event(p_slot, event_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800101 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Kenji Kaneshigedbd79ae2008-05-27 19:03:16 +0900104u8 pciehp_handle_presence_change(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800106 u32 event_type;
107 u8 presence_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 /* Presence Change */
110 dbg("pciehp: Presence/Notify input change.\n");
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 /* Switch is open, assume a presence change
113 * Save the presence state
114 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800115 p_slot->hpc_ops->get_adapter_status(p_slot, &presence_save);
116 if (presence_save) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /*
118 * Card Present
119 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800120 info("Card present on Slot(%s)\n", p_slot->name);
121 event_type = INT_PRESENCE_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 } else {
123 /*
124 * Not Present
125 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800126 info("Card not present on Slot(%s)\n", p_slot->name);
127 event_type = INT_PRESENCE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800130 queue_interrupt_event(p_slot, event_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800132 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Kenji Kaneshigedbd79ae2008-05-27 19:03:16 +0900135u8 pciehp_handle_power_fault(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800137 u32 event_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 /* power fault */
140 dbg("pciehp: Power fault interrupt received.\n");
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if ( !(p_slot->hpc_ops->query_power_fault(p_slot))) {
143 /*
144 * power fault Cleared
145 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800146 info("Power fault cleared on Slot(%s)\n", p_slot->name);
147 event_type = INT_POWER_FAULT_CLEAR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 } else {
149 /*
150 * power fault
151 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800152 info("Power fault on Slot(%s)\n", p_slot->name);
153 event_type = INT_POWER_FAULT;
Kenji Kaneshigedbd79ae2008-05-27 19:03:16 +0900154 info("power fault bit %x set\n", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800157 queue_interrupt_event(p_slot, event_type);
158
159 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700162/* The following routines constitute the bulk of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 hotplug controller logic
164 */
165
166static void set_slot_off(struct controller *ctrl, struct slot * pslot)
167{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700169 if (POWER_CTRL(ctrl)) {
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700170 if (pslot->hpc_ops->power_off_slot(pslot)) {
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800171 err("%s: Issue of Slot Power Off command failed\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800172 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return;
174 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
Kenji Kaneshige0711c702008-05-27 19:06:22 +0900177 /*
178 * After turning power off, we must wait for at least 1 second
179 * before taking any action that relies on power having been
180 * removed from the slot/adapter.
181 */
182 msleep(1000);
183
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700184 if (PWR_LED(ctrl))
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700185 pslot->hpc_ops->green_led_off(pslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700187 if (ATTN_LED(ctrl)) {
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800188 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
189 err("%s: Issue of Set Attention Led command failed\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800190 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return;
192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
196/**
197 * board_added - Called after a board has been added to the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -0800198 * @p_slot: &slot where board is added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800200 * Turns power on for the board.
201 * Configures board.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800203static int board_added(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800205 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800206 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800208 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800209 __func__, p_slot->device,
Kenji Kaneshige941f10e2007-11-09 17:29:42 +0900210 ctrl->slot_device_offset, p_slot->hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700212 if (POWER_CTRL(ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Power on slot */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800214 retval = p_slot->hpc_ops->power_on_slot(p_slot);
215 if (retval)
216 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700218
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700219 if (PWR_LED(ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 p_slot->hpc_ops->green_led_blink(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /* Wait for ~1 second */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800223 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800225 /* Check link training status */
226 retval = p_slot->hpc_ops->check_lnk_status(ctrl);
227 if (retval) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800228 err("%s: Failed to check link status\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 set_slot_off(ctrl, p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800230 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /* Check for a power fault */
Rajesh Shah5a49f202005-11-23 15:44:54 -0800234 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800235 dbg("%s: power fault detected\n", __func__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800236 retval = POWER_FAILURE;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800237 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 }
239
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800240 retval = pciehp_configure_device(p_slot);
241 if (retval) {
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800242 err("Cannot add device 0x%x:%x\n", p_slot->bus,
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800243 p_slot->device);
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800244 goto err_exit;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800247 /*
248 * Some PCI Express root ports require fixup after hot-plug operation.
249 */
250 if (pcie_mch_quirk)
251 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700252 if (PWR_LED(ctrl))
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800253 p_slot->hpc_ops->green_led_on(p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return 0;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800256
257err_exit:
258 set_slot_off(ctrl, p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800259 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800263 * remove_board - Turns off slot and LEDs
264 * @p_slot: slot where board is being removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800266static int remove_board(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800268 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800269 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800271 retval = pciehp_unconfigure_device(p_slot);
272 if (retval)
273 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800275 dbg("In %s, hp_slot = %d\n", __func__, p_slot->hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700277 if (POWER_CTRL(ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* power off slot */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800279 retval = p_slot->hpc_ops->power_off_slot(p_slot);
280 if (retval) {
281 err("%s: Issue of Slot Disable command failed\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800282 __func__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800283 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
Kenji Kaneshige0711c702008-05-27 19:06:22 +0900287 /*
288 * After turning power off, we must wait for at least 1 second
289 * before taking any action that relies on power having been
290 * removed from the slot/adapter.
291 */
292 msleep(1000);
293
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700294 if (PWR_LED(ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /* turn off Green LED */
296 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 return 0;
299}
300
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800301struct power_work_info {
302 struct slot *p_slot;
303 struct work_struct work;
304};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
306/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800307 * pciehp_power_thread - handle pushbutton events
308 * @work: &struct work_struct describing work to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800310 * Scheduled procedure to handle blocking stuff for the pushbuttons.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 * Handles all pending events and exits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800313static void pciehp_power_thread(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800315 struct power_work_info *info =
316 container_of(work, struct power_work_info, work);
317 struct slot *p_slot = info->p_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800319 mutex_lock(&p_slot->lock);
320 switch (p_slot->state) {
321 case POWEROFF_STATE:
322 mutex_unlock(&p_slot->lock);
323 dbg("%s: disabling bus:device(%x:%x)\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800324 __func__, p_slot->bus, p_slot->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 pciehp_disable_slot(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800326 mutex_lock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 p_slot->state = STATIC_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800328 break;
329 case POWERON_STATE:
330 mutex_unlock(&p_slot->lock);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800331 if (pciehp_enable_slot(p_slot) &&
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700332 PWR_LED(p_slot->ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 p_slot->hpc_ops->green_led_off(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800334 mutex_lock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 p_slot->state = STATIC_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800336 break;
337 default:
338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800340 mutex_unlock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800342 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343}
344
Kristen Carlson Accardie325e1f2007-03-21 11:45:31 -0700345void pciehp_queue_pushbutton_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800347 struct slot *p_slot = container_of(work, struct slot, work.work);
348 struct power_work_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800350 info = kmalloc(sizeof(*info), GFP_KERNEL);
351 if (!info) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800352 err("%s: Cannot allocate memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return;
354 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800355 info->p_slot = p_slot;
356 INIT_WORK(&info->work, pciehp_power_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800358 mutex_lock(&p_slot->lock);
359 switch (p_slot->state) {
360 case BLINKINGOFF_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 p_slot->state = POWEROFF_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800362 break;
363 case BLINKINGON_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 p_slot->state = POWERON_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800365 break;
366 default:
367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800369 queue_work(pciehp_wq, &info->work);
370 out:
371 mutex_unlock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372}
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374static int update_slot_info(struct slot *slot)
375{
376 struct hotplug_slot_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 int result;
378
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800379 info = kmalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 if (!info)
381 return -ENOMEM;
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 slot->hpc_ops->get_power_status(slot, &(info->power_status));
384 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
385 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
386 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
389 kfree (info);
390 return result;
391}
392
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800393/*
394 * Note: This function must be called with slot->lock held
395 */
396static void handle_button_press_event(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800398 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 u8 getstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800401 switch (p_slot->state) {
402 case STATIC_STATE:
403 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
404 if (getstatus) {
405 p_slot->state = BLINKINGOFF_STATE;
406 info("PCI slot #%s - powering off due to button "
407 "press.\n", p_slot->name);
408 } else {
409 p_slot->state = BLINKINGON_STATE;
410 info("PCI slot #%s - powering on due to button "
411 "press.\n", p_slot->name);
412 }
413 /* blink green LED and turn off amber */
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700414 if (PWR_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800415 p_slot->hpc_ops->green_led_blink(p_slot);
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700416 if (ATTN_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800417 p_slot->hpc_ops->set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800419 schedule_delayed_work(&p_slot->work, 5*HZ);
420 break;
421 case BLINKINGOFF_STATE:
422 case BLINKINGON_STATE:
423 /*
424 * Cancel if we are still blinking; this means that we
425 * press the attention again before the 5 sec. limit
426 * expires to cancel hot-add or hot-remove
427 */
428 info("Button cancel on Slot(%s)\n", p_slot->name);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800429 dbg("%s: button cancel\n", __func__);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800430 cancel_delayed_work(&p_slot->work);
431 if (p_slot->state == BLINKINGOFF_STATE) {
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700432 if (PWR_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800433 p_slot->hpc_ops->green_led_on(p_slot);
434 } else {
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700435 if (PWR_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800436 p_slot->hpc_ops->green_led_off(p_slot);
437 }
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700438 if (ATTN_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800439 p_slot->hpc_ops->set_attention_status(p_slot, 0);
440 info("PCI slot #%s - action canceled due to button press\n",
441 p_slot->name);
442 p_slot->state = STATIC_STATE;
443 break;
444 case POWEROFF_STATE:
445 case POWERON_STATE:
446 /*
447 * Ignore if the slot is on power-on or power-off state;
448 * this means that the previous attention button action
449 * to hot-add or hot-remove is undergoing
450 */
451 info("Button ignore on Slot(%s)\n", p_slot->name);
452 update_slot_info(p_slot);
453 break;
454 default:
455 warn("Not a valid state\n");
456 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458}
459
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800460/*
461 * Note: This function must be called with slot->lock held
462 */
463static void handle_surprise_event(struct slot *p_slot)
464{
465 u8 getstatus;
466 struct power_work_info *info;
467
468 info = kmalloc(sizeof(*info), GFP_KERNEL);
469 if (!info) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800470 err("%s: Cannot allocate memory\n", __func__);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800471 return;
472 }
473 info->p_slot = p_slot;
474 INIT_WORK(&info->work, pciehp_power_thread);
475
476 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
477 if (!getstatus)
478 p_slot->state = POWEROFF_STATE;
479 else
480 p_slot->state = POWERON_STATE;
481
482 queue_work(pciehp_wq, &info->work);
483}
484
485static void interrupt_event_handler(struct work_struct *work)
486{
487 struct event_info *info = container_of(work, struct event_info, work);
488 struct slot *p_slot = info->p_slot;
489 struct controller *ctrl = p_slot->ctrl;
490
491 mutex_lock(&p_slot->lock);
492 switch (info->event_type) {
493 case INT_BUTTON_PRESS:
494 handle_button_press_event(p_slot);
495 break;
496 case INT_POWER_FAULT:
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700497 if (!POWER_CTRL(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800498 break;
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700499 if (ATTN_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800500 p_slot->hpc_ops->set_attention_status(p_slot, 1);
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700501 if (PWR_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800502 p_slot->hpc_ops->green_led_off(p_slot);
503 break;
504 case INT_PRESENCE_ON:
505 case INT_PRESENCE_OFF:
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700506 if (!HP_SUPR_RM(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800507 break;
508 dbg("Surprise Removal\n");
509 update_slot_info(p_slot);
510 handle_surprise_event(p_slot);
511 break;
512 default:
513 update_slot_info(p_slot);
514 break;
515 }
516 mutex_unlock(&p_slot->lock);
517
518 kfree(info);
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521int pciehp_enable_slot(struct slot *p_slot)
522{
523 u8 getstatus = 0;
524 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526 /* Check to see if (latch closed, card present, power off) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100527 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528
529 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
530 if (rc || !getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800531 info("%s: no adapter on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800532 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100533 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700534 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 }
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700536 if (MRL_SENS(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
538 if (rc || getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800539 info("%s: latch open on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800540 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100541 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700542 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 }
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700545
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700546 if (POWER_CTRL(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
548 if (rc || getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800549 info("%s: already enabled on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800550 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100551 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700552 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
554 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800558 rc = board_added(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 }
562
Eric Sesterhenn9ef99772006-09-25 00:56:53 +0200563 update_slot_info(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -0700565 mutex_unlock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 return rc;
567}
568
569
570int pciehp_disable_slot(struct slot *p_slot)
571{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 u8 getstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (!p_slot->ctrl)
576 return 1;
577
578 /* Check to see if (latch closed, card present, power on) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100579 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700581 if (!HP_SUPR_RM(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
583 if (ret || !getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800584 info("%s: no adapter on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800585 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100586 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700587 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589 }
590
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700591 if (MRL_SENS(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
593 if (ret || getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800594 info("%s: latch open on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800595 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100596 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700597 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
599 }
600
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700601 if (POWER_CTRL(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
603 if (ret || !getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800604 info("%s: already disabled slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800605 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100606 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700607 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609 }
610
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800611 ret = remove_board(p_slot);
612 update_slot_info(p_slot);
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -0700613
614 mutex_unlock(&p_slot->ctrl->crit_sect);
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800615 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800618int pciehp_sysfs_enable_slot(struct slot *p_slot)
619{
620 int retval = -ENODEV;
621
622 mutex_lock(&p_slot->lock);
623 switch (p_slot->state) {
624 case BLINKINGON_STATE:
625 cancel_delayed_work(&p_slot->work);
626 case STATIC_STATE:
627 p_slot->state = POWERON_STATE;
628 mutex_unlock(&p_slot->lock);
629 retval = pciehp_enable_slot(p_slot);
630 mutex_lock(&p_slot->lock);
631 p_slot->state = STATIC_STATE;
632 break;
633 case POWERON_STATE:
634 info("Slot %s is already in powering on state\n",
635 p_slot->name);
636 break;
637 case BLINKINGOFF_STATE:
638 case POWEROFF_STATE:
639 info("Already enabled on slot %s\n", p_slot->name);
640 break;
641 default:
642 err("Not a valid state on slot %s\n", p_slot->name);
643 break;
644 }
645 mutex_unlock(&p_slot->lock);
646
647 return retval;
648}
649
650int pciehp_sysfs_disable_slot(struct slot *p_slot)
651{
652 int retval = -ENODEV;
653
654 mutex_lock(&p_slot->lock);
655 switch (p_slot->state) {
656 case BLINKINGOFF_STATE:
657 cancel_delayed_work(&p_slot->work);
658 case STATIC_STATE:
659 p_slot->state = POWEROFF_STATE;
660 mutex_unlock(&p_slot->lock);
661 retval = pciehp_disable_slot(p_slot);
662 mutex_lock(&p_slot->lock);
663 p_slot->state = STATIC_STATE;
664 break;
665 case POWEROFF_STATE:
666 info("Slot %s is already in powering off state\n",
667 p_slot->name);
668 break;
669 case BLINKINGON_STATE:
670 case POWERON_STATE:
671 info("Already disabled on slot %s\n", p_slot->name);
672 break;
673 default:
674 err("Not a valid state on slot %s\n", p_slot->name);
675 break;
676 }
677 mutex_unlock(&p_slot->lock);
678
679 return retval;
680}