blob: 8f58148be044ae3a53e1b7b0419f222698a76558 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#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;
Taku Izumi7f2feec2008-09-05 12:11:26 +090061 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63 /* Attention Button Change */
Taku Izumi18b341b2008-10-23 11:47:32 +090064 ctrl_dbg(ctrl, "Attention button interrupt received\n");
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080065
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 /*
67 * Button pressed - See if need to TAKE ACTION!!!
68 */
Alex Chiange1acb242008-10-20 17:41:38 -060069 ctrl_info(ctrl, "Button pressed on Slot(%s)\n", slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080070 event_type = INT_BUTTON_PRESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080072 queue_interrupt_event(p_slot, event_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075}
76
Kenji Kaneshigedbd79ae2008-05-27 19:03:16 +090077u8 pciehp_handle_switch_change(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 u8 getstatus;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080080 u32 event_type;
Taku Izumi7f2feec2008-09-05 12:11:26 +090081 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /* Switch Change */
Taku Izumi18b341b2008-10-23 11:47:32 +090084 ctrl_dbg(ctrl, "Switch interrupt received\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Kenji Kaneshige82a9e792009-09-15 17:30:48 +090086 pciehp_get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 if (getstatus) {
88 /*
89 * Switch opened
90 */
Alex Chiange1acb242008-10-20 17:41:38 -060091 ctrl_info(ctrl, "Latch open on Slot(%s)\n", slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080092 event_type = INT_SWITCH_OPEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 } else {
94 /*
95 * Switch closed
96 */
Alex Chiange1acb242008-10-20 17:41:38 -060097 ctrl_info(ctrl, "Latch close on Slot(%s)\n", slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -080098 event_type = INT_SWITCH_CLOSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 }
100
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800101 queue_interrupt_event(p_slot, event_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800103 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
Kenji Kaneshigedbd79ae2008-05-27 19:03:16 +0900106u8 pciehp_handle_presence_change(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800108 u32 event_type;
109 u8 presence_save;
Taku Izumi7f2feec2008-09-05 12:11:26 +0900110 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /* Presence Change */
Taku Izumi18b341b2008-10-23 11:47:32 +0900113 ctrl_dbg(ctrl, "Presence/Notify input change\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 /* Switch is open, assume a presence change
116 * Save the presence state
117 */
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900118 pciehp_get_adapter_status(p_slot, &presence_save);
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800119 if (presence_save) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 /*
121 * Card Present
122 */
Alex Chiange1acb242008-10-20 17:41:38 -0600123 ctrl_info(ctrl, "Card present on Slot(%s)\n", slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800124 event_type = INT_PRESENCE_ON;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 } else {
126 /*
127 * Not Present
128 */
Alex Chiange1acb242008-10-20 17:41:38 -0600129 ctrl_info(ctrl, "Card not present on Slot(%s)\n",
130 slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800131 event_type = INT_PRESENCE_OFF;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800134 queue_interrupt_event(p_slot, event_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800136 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Kenji Kaneshigedbd79ae2008-05-27 19:03:16 +0900139u8 pciehp_handle_power_fault(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800141 u32 event_type;
Taku Izumi7f2feec2008-09-05 12:11:26 +0900142 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
144 /* power fault */
Taku Izumi18b341b2008-10-23 11:47:32 +0900145 ctrl_dbg(ctrl, "Power fault interrupt received\n");
Kenji Kaneshige5651c482009-11-13 15:14:10 +0900146 ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
147 event_type = INT_POWER_FAULT;
148 ctrl_info(ctrl, "Power fault bit %x set\n", 0);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800149 queue_interrupt_event(p_slot, event_type);
150
151 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700154/* The following routines constitute the bulk of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 hotplug controller logic
156 */
157
158static void set_slot_off(struct controller *ctrl, struct slot * pslot)
159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* turn off slot, turn on Amber LED, turn off Green LED if supported*/
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700161 if (POWER_CTRL(ctrl)) {
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900162 if (pciehp_power_off_slot(pslot)) {
Taku Izumi7f2feec2008-09-05 12:11:26 +0900163 ctrl_err(ctrl,
Taku Izumi18b341b2008-10-23 11:47:32 +0900164 "Issue of Slot Power Off command failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 return;
166 }
Kenji Kaneshigec7b4fee2008-10-24 14:26:35 +0900167 /*
168 * After turning power off, we must wait for at least 1 second
169 * before taking any action that relies on power having been
170 * removed from the slot/adapter.
171 */
172 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700175 if (PWR_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900176 pciehp_green_led_off(pslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700178 if (ATTN_LED(ctrl)) {
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900179 if (pciehp_set_attention_status(pslot, 1)) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900180 ctrl_err(ctrl,
181 "Issue of Set Attention Led command failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return;
183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/**
188 * board_added - Called after a board has been added to the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -0800189 * @p_slot: &slot where board is added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800191 * Turns power on for the board.
192 * Configures board.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800194static int board_added(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800196 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800197 struct controller *ctrl = p_slot->ctrl;
Kenji Kaneshige385e2492009-09-15 17:30:14 +0900198 struct pci_bus *parent = ctrl->pcie->port->subordinate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700200 if (POWER_CTRL(ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 /* Power on slot */
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900202 retval = pciehp_power_on_slot(p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800203 if (retval)
204 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700206
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700207 if (PWR_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900208 pciehp_green_led_blink(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800210 /* Check link training status */
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900211 retval = pciehp_check_link_status(ctrl);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800212 if (retval) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900213 ctrl_err(ctrl, "Failed to check link status\n");
Kenji Kaneshige5651c482009-11-13 15:14:10 +0900214 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* Check for a power fault */
Kenji Kaneshige5651c482009-11-13 15:14:10 +0900218 if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
219 ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
Kenji Kaneshiged9fb42a82009-09-15 17:33:30 +0900220 retval = -EIO;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800221 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800224 retval = pciehp_configure_device(p_slot);
225 if (retval) {
Kenji Kaneshiged689f7e2009-09-15 17:26:56 +0900226 ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
227 pci_domain_nr(parent), parent->number);
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800228 goto err_exit;
229 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700231 if (PWR_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900232 pciehp_green_led_on(p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return 0;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800235
236err_exit:
237 set_slot_off(ctrl, p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800238 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239}
240
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800242 * remove_board - Turns off slot and LEDs
243 * @p_slot: slot where board is being removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800245static int remove_board(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246{
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800247 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800248 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800250 retval = pciehp_unconfigure_device(p_slot);
251 if (retval)
252 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700254 if (POWER_CTRL(ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 /* power off slot */
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900256 retval = pciehp_power_off_slot(p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800257 if (retval) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900258 ctrl_err(ctrl,
259 "Issue of Slot Disable command failed\n");
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800260 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Kenji Kaneshigec7b4fee2008-10-24 14:26:35 +0900262 /*
263 * After turning power off, we must wait for at least 1 second
264 * before taking any action that relies on power having been
265 * removed from the slot/adapter.
266 */
267 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 }
269
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900270 /* turn off Green LED */
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700271 if (PWR_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900272 pciehp_green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return 0;
275}
276
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800277struct power_work_info {
278 struct slot *p_slot;
279 struct work_struct work;
280};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
282/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800283 * pciehp_power_thread - handle pushbutton events
284 * @work: &struct work_struct describing work to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800286 * Scheduled procedure to handle blocking stuff for the pushbuttons.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 * Handles all pending events and exits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800289static void pciehp_power_thread(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800291 struct power_work_info *info =
292 container_of(work, struct power_work_info, work);
293 struct slot *p_slot = info->p_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800295 mutex_lock(&p_slot->lock);
296 switch (p_slot->state) {
297 case POWEROFF_STATE:
298 mutex_unlock(&p_slot->lock);
Taku Izumi18b341b2008-10-23 11:47:32 +0900299 ctrl_dbg(p_slot->ctrl,
Kenji Kaneshiged689f7e2009-09-15 17:26:56 +0900300 "Disabling domain:bus:device=%04x:%02x:00\n",
Kenji Kaneshige385e2492009-09-15 17:30:14 +0900301 pci_domain_nr(p_slot->ctrl->pcie->port->subordinate),
302 p_slot->ctrl->pcie->port->subordinate->number);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 pciehp_disable_slot(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800304 mutex_lock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 p_slot->state = STATIC_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800306 break;
307 case POWERON_STATE:
308 mutex_unlock(&p_slot->lock);
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900309 if (pciehp_enable_slot(p_slot) && PWR_LED(p_slot->ctrl))
310 pciehp_green_led_off(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800311 mutex_lock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 p_slot->state = STATIC_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800313 break;
314 default:
315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800317 mutex_unlock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800319 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Kristen Carlson Accardie325e1f2007-03-21 11:45:31 -0700322void pciehp_queue_pushbutton_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800324 struct slot *p_slot = container_of(work, struct slot, work.work);
325 struct power_work_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800327 info = kmalloc(sizeof(*info), GFP_KERNEL);
328 if (!info) {
Taku Izumi7f2feec2008-09-05 12:11:26 +0900329 ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
330 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return;
332 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800333 info->p_slot = p_slot;
334 INIT_WORK(&info->work, pciehp_power_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800336 mutex_lock(&p_slot->lock);
337 switch (p_slot->state) {
338 case BLINKINGOFF_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 p_slot->state = POWEROFF_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800340 break;
341 case BLINKINGON_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 p_slot->state = POWERON_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800343 break;
344 default:
Jiri Slaby6fcaf172010-01-06 17:47:56 +0100345 kfree(info);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800346 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800348 queue_work(pciehp_wq, &info->work);
349 out:
350 mutex_unlock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800353/*
354 * Note: This function must be called with slot->lock held
355 */
356static void handle_button_press_event(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800358 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 u8 getstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800361 switch (p_slot->state) {
362 case STATIC_STATE:
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900363 pciehp_get_power_status(p_slot, &getstatus);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800364 if (getstatus) {
365 p_slot->state = BLINKINGOFF_STATE;
Taku Izumi7f2feec2008-09-05 12:11:26 +0900366 ctrl_info(ctrl,
367 "PCI slot #%s - powering off due to button "
Alex Chiange1acb242008-10-20 17:41:38 -0600368 "press.\n", slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800369 } else {
370 p_slot->state = BLINKINGON_STATE;
Taku Izumi7f2feec2008-09-05 12:11:26 +0900371 ctrl_info(ctrl,
372 "PCI slot #%s - powering on due to button "
Alex Chiange1acb242008-10-20 17:41:38 -0600373 "press.\n", slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800374 }
375 /* blink green LED and turn off amber */
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700376 if (PWR_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900377 pciehp_green_led_blink(p_slot);
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700378 if (ATTN_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900379 pciehp_set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800381 schedule_delayed_work(&p_slot->work, 5*HZ);
382 break;
383 case BLINKINGOFF_STATE:
384 case BLINKINGON_STATE:
385 /*
386 * Cancel if we are still blinking; this means that we
387 * press the attention again before the 5 sec. limit
388 * expires to cancel hot-add or hot-remove
389 */
Alex Chiange1acb242008-10-20 17:41:38 -0600390 ctrl_info(ctrl, "Button cancel on Slot(%s)\n", slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800391 cancel_delayed_work(&p_slot->work);
392 if (p_slot->state == BLINKINGOFF_STATE) {
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700393 if (PWR_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900394 pciehp_green_led_on(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800395 } else {
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700396 if (PWR_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900397 pciehp_green_led_off(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800398 }
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700399 if (ATTN_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900400 pciehp_set_attention_status(p_slot, 0);
Taku Izumi7f2feec2008-09-05 12:11:26 +0900401 ctrl_info(ctrl, "PCI slot #%s - action canceled "
Alex Chiange1acb242008-10-20 17:41:38 -0600402 "due to button press\n", slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800403 p_slot->state = STATIC_STATE;
404 break;
405 case POWEROFF_STATE:
406 case POWERON_STATE:
407 /*
408 * Ignore if the slot is on power-on or power-off state;
409 * this means that the previous attention button action
410 * to hot-add or hot-remove is undergoing
411 */
Alex Chiange1acb242008-10-20 17:41:38 -0600412 ctrl_info(ctrl, "Button ignore on Slot(%s)\n", slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800413 break;
414 default:
Taku Izumi7f2feec2008-09-05 12:11:26 +0900415 ctrl_warn(ctrl, "Not a valid state\n");
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418}
419
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800420/*
421 * Note: This function must be called with slot->lock held
422 */
423static void handle_surprise_event(struct slot *p_slot)
424{
425 u8 getstatus;
426 struct power_work_info *info;
427
428 info = kmalloc(sizeof(*info), GFP_KERNEL);
429 if (!info) {
Taku Izumi7f2feec2008-09-05 12:11:26 +0900430 ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
431 __func__);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800432 return;
433 }
434 info->p_slot = p_slot;
435 INIT_WORK(&info->work, pciehp_power_thread);
436
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900437 pciehp_get_adapter_status(p_slot, &getstatus);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800438 if (!getstatus)
439 p_slot->state = POWEROFF_STATE;
440 else
441 p_slot->state = POWERON_STATE;
442
443 queue_work(pciehp_wq, &info->work);
444}
445
446static void interrupt_event_handler(struct work_struct *work)
447{
448 struct event_info *info = container_of(work, struct event_info, work);
449 struct slot *p_slot = info->p_slot;
450 struct controller *ctrl = p_slot->ctrl;
451
452 mutex_lock(&p_slot->lock);
453 switch (info->event_type) {
454 case INT_BUTTON_PRESS:
455 handle_button_press_event(p_slot);
456 break;
457 case INT_POWER_FAULT:
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700458 if (!POWER_CTRL(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800459 break;
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700460 if (ATTN_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900461 pciehp_set_attention_status(p_slot, 1);
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700462 if (PWR_LED(ctrl))
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900463 pciehp_green_led_off(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800464 break;
465 case INT_PRESENCE_ON:
466 case INT_PRESENCE_OFF:
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700467 if (!HP_SUPR_RM(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800468 break;
Taku Izumi7f2feec2008-09-05 12:11:26 +0900469 ctrl_dbg(ctrl, "Surprise Removal\n");
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800470 handle_surprise_event(p_slot);
471 break;
472 default:
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800473 break;
474 }
475 mutex_unlock(&p_slot->lock);
476
477 kfree(info);
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480int pciehp_enable_slot(struct slot *p_slot)
481{
482 u8 getstatus = 0;
483 int rc;
Taku Izumi7f2feec2008-09-05 12:11:26 +0900484 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900486 rc = pciehp_get_adapter_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 if (rc || !getstatus) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900488 ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700489 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700491 if (MRL_SENS(p_slot->ctrl)) {
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900492 rc = pciehp_get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if (rc || getstatus) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900494 ctrl_info(ctrl, "Latch open on slot(%s)\n",
495 slot_name(p_slot));
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700496 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 }
498 }
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700499
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700500 if (POWER_CTRL(p_slot->ctrl)) {
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900501 rc = pciehp_get_power_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 if (rc || getstatus) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900503 ctrl_info(ctrl, "Already enabled on slot(%s)\n",
504 slot_name(p_slot));
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700505 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900509 pciehp_get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800511 rc = board_added(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (rc) {
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900513 pciehp_get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 return rc;
516}
517
518
519int pciehp_disable_slot(struct slot *p_slot)
520{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 u8 getstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 int ret = 0;
Taku Izumi7f2feec2008-09-05 12:11:26 +0900523 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 if (!p_slot->ctrl)
526 return 1;
527
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700528 if (!HP_SUPR_RM(p_slot->ctrl)) {
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900529 ret = pciehp_get_adapter_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (ret || !getstatus) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900531 ctrl_info(ctrl, "No adapter on slot(%s)\n",
532 slot_name(p_slot));
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700533 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535 }
536
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700537 if (MRL_SENS(p_slot->ctrl)) {
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900538 ret = pciehp_get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 if (ret || getstatus) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900540 ctrl_info(ctrl, "Latch open on slot(%s)\n",
541 slot_name(p_slot));
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700542 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
544 }
545
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700546 if (POWER_CTRL(p_slot->ctrl)) {
Kenji Kaneshige82a9e792009-09-15 17:30:48 +0900547 ret = pciehp_get_power_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 if (ret || !getstatus) {
Taku Izumi18b341b2008-10-23 11:47:32 +0900549 ctrl_info(ctrl, "Already disabled on slot(%s)\n",
550 slot_name(p_slot));
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700551 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 }
554
Kenji Kaneshige445f7982009-10-05 17:42:59 +0900555 return remove_board(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800558int pciehp_sysfs_enable_slot(struct slot *p_slot)
559{
560 int retval = -ENODEV;
Taku Izumi7f2feec2008-09-05 12:11:26 +0900561 struct controller *ctrl = p_slot->ctrl;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800562
563 mutex_lock(&p_slot->lock);
564 switch (p_slot->state) {
565 case BLINKINGON_STATE:
566 cancel_delayed_work(&p_slot->work);
567 case STATIC_STATE:
568 p_slot->state = POWERON_STATE;
569 mutex_unlock(&p_slot->lock);
570 retval = pciehp_enable_slot(p_slot);
571 mutex_lock(&p_slot->lock);
572 p_slot->state = STATIC_STATE;
573 break;
574 case POWERON_STATE:
Taku Izumi7f2feec2008-09-05 12:11:26 +0900575 ctrl_info(ctrl, "Slot %s is already in powering on state\n",
Alex Chiange1acb242008-10-20 17:41:38 -0600576 slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800577 break;
578 case BLINKINGOFF_STATE:
579 case POWEROFF_STATE:
Alex Chiange1acb242008-10-20 17:41:38 -0600580 ctrl_info(ctrl, "Already enabled on slot %s\n",
581 slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800582 break;
583 default:
Alex Chiange1acb242008-10-20 17:41:38 -0600584 ctrl_err(ctrl, "Not a valid state on slot %s\n",
585 slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800586 break;
587 }
588 mutex_unlock(&p_slot->lock);
589
590 return retval;
591}
592
593int pciehp_sysfs_disable_slot(struct slot *p_slot)
594{
595 int retval = -ENODEV;
Taku Izumi7f2feec2008-09-05 12:11:26 +0900596 struct controller *ctrl = p_slot->ctrl;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800597
598 mutex_lock(&p_slot->lock);
599 switch (p_slot->state) {
600 case BLINKINGOFF_STATE:
601 cancel_delayed_work(&p_slot->work);
602 case STATIC_STATE:
603 p_slot->state = POWEROFF_STATE;
604 mutex_unlock(&p_slot->lock);
605 retval = pciehp_disable_slot(p_slot);
606 mutex_lock(&p_slot->lock);
607 p_slot->state = STATIC_STATE;
608 break;
609 case POWEROFF_STATE:
Taku Izumi7f2feec2008-09-05 12:11:26 +0900610 ctrl_info(ctrl, "Slot %s is already in powering off state\n",
Alex Chiange1acb242008-10-20 17:41:38 -0600611 slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800612 break;
613 case BLINKINGON_STATE:
614 case POWERON_STATE:
Alex Chiange1acb242008-10-20 17:41:38 -0600615 ctrl_info(ctrl, "Already disabled on slot %s\n",
616 slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800617 break;
618 default:
Alex Chiange1acb242008-10-20 17:41:38 -0600619 ctrl_err(ctrl, "Not a valid state on slot %s\n",
620 slot_name(p_slot));
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800621 break;
622 }
623 mutex_unlock(&p_slot->lock);
624
625 return retval;
626}