blob: 7ad8a7dbc1a48108833ab97a972ce1cbcb9cb10b [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 Kaneshigeae416e62008-04-25 14:39:06 -0700177 if (PWR_LED(ctrl))
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700178 pslot->hpc_ops->green_led_off(pslot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700180 if (ATTN_LED(ctrl)) {
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800181 if (pslot->hpc_ops->set_attention_status(pslot, 1)) {
182 err("%s: Issue of Set Attention Led command failed\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800183 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 return;
185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189/**
190 * board_added - Called after a board has been added to the system.
Randy Dunlap26e6c662007-11-28 09:04:30 -0800191 * @p_slot: &slot where board is added
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800193 * Turns power on for the board.
194 * Configures board.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800196static int board_added(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800198 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800199 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
rajesh.shah@intel.com1a9ed1b2005-10-31 16:20:10 -0800201 dbg("%s: slot device, slot offset, hp slot = %d, %d ,%d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800202 __func__, p_slot->device,
Kenji Kaneshige941f10e2007-11-09 17:29:42 +0900203 ctrl->slot_device_offset, p_slot->hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700205 if (POWER_CTRL(ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 /* Power on slot */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800207 retval = p_slot->hpc_ops->power_on_slot(p_slot);
208 if (retval)
209 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700211
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700212 if (PWR_LED(ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 p_slot->hpc_ops->green_led_blink(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Wait for ~1 second */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800216 msleep(1000);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800218 /* Check link training status */
219 retval = p_slot->hpc_ops->check_lnk_status(ctrl);
220 if (retval) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800221 err("%s: Failed to check link status\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 set_slot_off(ctrl, p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800223 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 /* Check for a power fault */
Rajesh Shah5a49f202005-11-23 15:44:54 -0800227 if (p_slot->hpc_ops->query_power_fault(p_slot)) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800228 dbg("%s: power fault detected\n", __func__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800229 retval = POWER_FAILURE;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800230 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800233 retval = pciehp_configure_device(p_slot);
234 if (retval) {
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800235 err("Cannot add device 0x%x:%x\n", p_slot->bus,
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800236 p_slot->device);
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800237 goto err_exit;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800240 /*
241 * Some PCI Express root ports require fixup after hot-plug operation.
242 */
243 if (pcie_mch_quirk)
244 pci_fixup_device(pci_fixup_final, ctrl->pci_dev);
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700245 if (PWR_LED(ctrl))
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800246 p_slot->hpc_ops->green_led_on(p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 return 0;
rajesh.shah@intel.com71b720c2005-10-31 16:20:06 -0800249
250err_exit:
251 set_slot_off(ctrl, p_slot);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800252 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800256 * remove_board - Turns off slot and LEDs
257 * @p_slot: slot where board is being removed
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 */
rajesh.shah@intel.comed6cbcf2005-10-31 16:20:09 -0800259static int remove_board(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800261 int retval = 0;
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800262 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800264 retval = pciehp_unconfigure_device(p_slot);
265 if (retval)
266 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800268 dbg("In %s, hp_slot = %d\n", __func__, p_slot->hp_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700270 if (POWER_CTRL(ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* power off slot */
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800272 retval = p_slot->hpc_ops->power_off_slot(p_slot);
273 if (retval) {
274 err("%s: Issue of Slot Disable command failed\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800275 __func__);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800276 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700280 if (PWR_LED(ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 /* turn off Green LED */
282 p_slot->hpc_ops->green_led_off(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 return 0;
285}
286
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800287struct power_work_info {
288 struct slot *p_slot;
289 struct work_struct work;
290};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
292/**
Randy Dunlap26e6c662007-11-28 09:04:30 -0800293 * pciehp_power_thread - handle pushbutton events
294 * @work: &struct work_struct describing work to be done
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 *
Randy Dunlap26e6c662007-11-28 09:04:30 -0800296 * Scheduled procedure to handle blocking stuff for the pushbuttons.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 * Handles all pending events and exits.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 */
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800299static void pciehp_power_thread(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800301 struct power_work_info *info =
302 container_of(work, struct power_work_info, work);
303 struct slot *p_slot = info->p_slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800305 mutex_lock(&p_slot->lock);
306 switch (p_slot->state) {
307 case POWEROFF_STATE:
308 mutex_unlock(&p_slot->lock);
309 dbg("%s: disabling bus:device(%x:%x)\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800310 __func__, p_slot->bus, p_slot->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 pciehp_disable_slot(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800312 mutex_lock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 p_slot->state = STATIC_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800314 break;
315 case POWERON_STATE:
316 mutex_unlock(&p_slot->lock);
Kenji Kaneshige44ef4ce2006-12-21 17:01:09 -0800317 if (pciehp_enable_slot(p_slot) &&
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700318 PWR_LED(p_slot->ctrl))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 p_slot->hpc_ops->green_led_off(p_slot);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800320 mutex_lock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 p_slot->state = STATIC_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800322 break;
323 default:
324 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800326 mutex_unlock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800328 kfree(info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Kristen Carlson Accardie325e1f2007-03-21 11:45:31 -0700331void pciehp_queue_pushbutton_work(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800333 struct slot *p_slot = container_of(work, struct slot, work.work);
334 struct power_work_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800336 info = kmalloc(sizeof(*info), GFP_KERNEL);
337 if (!info) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800338 err("%s: Cannot allocate memory\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 return;
340 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800341 info->p_slot = p_slot;
342 INIT_WORK(&info->work, pciehp_power_thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800344 mutex_lock(&p_slot->lock);
345 switch (p_slot->state) {
346 case BLINKINGOFF_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 p_slot->state = POWEROFF_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800348 break;
349 case BLINKINGON_STATE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 p_slot->state = POWERON_STATE;
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800351 break;
352 default:
353 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800355 queue_work(pciehp_wq, &info->work);
356 out:
357 mutex_unlock(&p_slot->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360static int update_slot_info(struct slot *slot)
361{
362 struct hotplug_slot_info *info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int result;
364
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800365 info = kmalloc(sizeof(*info), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (!info)
367 return -ENOMEM;
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 slot->hpc_ops->get_power_status(slot, &(info->power_status));
370 slot->hpc_ops->get_attention_status(slot, &(info->attention_status));
371 slot->hpc_ops->get_latch_status(slot, &(info->latch_status));
372 slot->hpc_ops->get_adapter_status(slot, &(info->adapter_status));
373
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 result = pci_hp_change_slot_info(slot->hotplug_slot, info);
375 kfree (info);
376 return result;
377}
378
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800379/*
380 * Note: This function must be called with slot->lock held
381 */
382static void handle_button_press_event(struct slot *p_slot)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800384 struct controller *ctrl = p_slot->ctrl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 u8 getstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800387 switch (p_slot->state) {
388 case STATIC_STATE:
389 p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
390 if (getstatus) {
391 p_slot->state = BLINKINGOFF_STATE;
392 info("PCI slot #%s - powering off due to button "
393 "press.\n", p_slot->name);
394 } else {
395 p_slot->state = BLINKINGON_STATE;
396 info("PCI slot #%s - powering on due to button "
397 "press.\n", p_slot->name);
398 }
399 /* blink green LED and turn off amber */
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700400 if (PWR_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800401 p_slot->hpc_ops->green_led_blink(p_slot);
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700402 if (ATTN_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800403 p_slot->hpc_ops->set_attention_status(p_slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800405 schedule_delayed_work(&p_slot->work, 5*HZ);
406 break;
407 case BLINKINGOFF_STATE:
408 case BLINKINGON_STATE:
409 /*
410 * Cancel if we are still blinking; this means that we
411 * press the attention again before the 5 sec. limit
412 * expires to cancel hot-add or hot-remove
413 */
414 info("Button cancel on Slot(%s)\n", p_slot->name);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800415 dbg("%s: button cancel\n", __func__);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800416 cancel_delayed_work(&p_slot->work);
417 if (p_slot->state == BLINKINGOFF_STATE) {
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700418 if (PWR_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800419 p_slot->hpc_ops->green_led_on(p_slot);
420 } else {
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700421 if (PWR_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800422 p_slot->hpc_ops->green_led_off(p_slot);
423 }
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700424 if (ATTN_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800425 p_slot->hpc_ops->set_attention_status(p_slot, 0);
426 info("PCI slot #%s - action canceled due to button press\n",
427 p_slot->name);
428 p_slot->state = STATIC_STATE;
429 break;
430 case POWEROFF_STATE:
431 case POWERON_STATE:
432 /*
433 * Ignore if the slot is on power-on or power-off state;
434 * this means that the previous attention button action
435 * to hot-add or hot-remove is undergoing
436 */
437 info("Button ignore on Slot(%s)\n", p_slot->name);
438 update_slot_info(p_slot);
439 break;
440 default:
441 warn("Not a valid state\n");
442 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444}
445
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800446/*
447 * Note: This function must be called with slot->lock held
448 */
449static void handle_surprise_event(struct slot *p_slot)
450{
451 u8 getstatus;
452 struct power_work_info *info;
453
454 info = kmalloc(sizeof(*info), GFP_KERNEL);
455 if (!info) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800456 err("%s: Cannot allocate memory\n", __func__);
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800457 return;
458 }
459 info->p_slot = p_slot;
460 INIT_WORK(&info->work, pciehp_power_thread);
461
462 p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
463 if (!getstatus)
464 p_slot->state = POWEROFF_STATE;
465 else
466 p_slot->state = POWERON_STATE;
467
468 queue_work(pciehp_wq, &info->work);
469}
470
471static void interrupt_event_handler(struct work_struct *work)
472{
473 struct event_info *info = container_of(work, struct event_info, work);
474 struct slot *p_slot = info->p_slot;
475 struct controller *ctrl = p_slot->ctrl;
476
477 mutex_lock(&p_slot->lock);
478 switch (info->event_type) {
479 case INT_BUTTON_PRESS:
480 handle_button_press_event(p_slot);
481 break;
482 case INT_POWER_FAULT:
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700483 if (!POWER_CTRL(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800484 break;
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700485 if (ATTN_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800486 p_slot->hpc_ops->set_attention_status(p_slot, 1);
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700487 if (PWR_LED(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800488 p_slot->hpc_ops->green_led_off(p_slot);
489 break;
490 case INT_PRESENCE_ON:
491 case INT_PRESENCE_OFF:
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700492 if (!HP_SUPR_RM(ctrl))
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800493 break;
494 dbg("Surprise Removal\n");
495 update_slot_info(p_slot);
496 handle_surprise_event(p_slot);
497 break;
498 default:
499 update_slot_info(p_slot);
500 break;
501 }
502 mutex_unlock(&p_slot->lock);
503
504 kfree(info);
505}
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507int pciehp_enable_slot(struct slot *p_slot)
508{
509 u8 getstatus = 0;
510 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
512 /* Check to see if (latch closed, card present, power off) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100513 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 rc = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
516 if (rc || !getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800517 info("%s: no adapter on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800518 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100519 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700520 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700522 if (MRL_SENS(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 rc = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
524 if (rc || getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800525 info("%s: latch open on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800526 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100527 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700528 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530 }
Kenji Kaneshige36ed27b2007-08-09 16:09:36 -0700531
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700532 if (POWER_CTRL(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 rc = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
534 if (rc || getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800535 info("%s: already enabled on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800536 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100537 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700538 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800544 rc = board_added(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
548
Eric Sesterhenn9ef99772006-09-25 00:56:53 +0200549 update_slot_info(p_slot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -0700551 mutex_unlock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 return rc;
553}
554
555
556int pciehp_disable_slot(struct slot *p_slot)
557{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 u8 getstatus = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 if (!p_slot->ctrl)
562 return 1;
563
564 /* Check to see if (latch closed, card present, power on) */
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100565 mutex_lock(&p_slot->ctrl->crit_sect);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700567 if (!HP_SUPR_RM(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 ret = p_slot->hpc_ops->get_adapter_status(p_slot, &getstatus);
569 if (ret || !getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800570 info("%s: no adapter on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800571 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100572 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700573 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
575 }
576
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700577 if (MRL_SENS(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 ret = p_slot->hpc_ops->get_latch_status(p_slot, &getstatus);
579 if (ret || getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800580 info("%s: latch open on slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800581 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100582 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700583 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 }
585 }
586
Kenji Kaneshigeae416e62008-04-25 14:39:06 -0700587 if (POWER_CTRL(p_slot->ctrl)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 ret = p_slot->hpc_ops->get_power_status(p_slot, &getstatus);
589 if (ret || !getstatus) {
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800590 info("%s: already disabled slot(%s)\n", __func__,
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800591 p_slot->name);
Ingo Molnar6aa4cdd2006-01-13 16:02:15 +0100592 mutex_unlock(&p_slot->ctrl->crit_sect);
Kenji Kaneshigec9d86d72006-09-19 17:04:33 -0700593 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595 }
596
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800597 ret = remove_board(p_slot);
598 update_slot_info(p_slot);
Kenji Kaneshigedd5619c2006-09-22 10:17:29 -0700599
600 mutex_unlock(&p_slot->ctrl->crit_sect);
rajesh.shah@intel.comca22a5e2005-10-31 16:20:08 -0800601 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602}
603
Kenji Kaneshige5d386e12007-03-06 15:02:26 -0800604int pciehp_sysfs_enable_slot(struct slot *p_slot)
605{
606 int retval = -ENODEV;
607
608 mutex_lock(&p_slot->lock);
609 switch (p_slot->state) {
610 case BLINKINGON_STATE:
611 cancel_delayed_work(&p_slot->work);
612 case STATIC_STATE:
613 p_slot->state = POWERON_STATE;
614 mutex_unlock(&p_slot->lock);
615 retval = pciehp_enable_slot(p_slot);
616 mutex_lock(&p_slot->lock);
617 p_slot->state = STATIC_STATE;
618 break;
619 case POWERON_STATE:
620 info("Slot %s is already in powering on state\n",
621 p_slot->name);
622 break;
623 case BLINKINGOFF_STATE:
624 case POWEROFF_STATE:
625 info("Already enabled on slot %s\n", p_slot->name);
626 break;
627 default:
628 err("Not a valid state on slot %s\n", p_slot->name);
629 break;
630 }
631 mutex_unlock(&p_slot->lock);
632
633 return retval;
634}
635
636int pciehp_sysfs_disable_slot(struct slot *p_slot)
637{
638 int retval = -ENODEV;
639
640 mutex_lock(&p_slot->lock);
641 switch (p_slot->state) {
642 case BLINKINGOFF_STATE:
643 cancel_delayed_work(&p_slot->work);
644 case STATIC_STATE:
645 p_slot->state = POWEROFF_STATE;
646 mutex_unlock(&p_slot->lock);
647 retval = pciehp_disable_slot(p_slot);
648 mutex_lock(&p_slot->lock);
649 p_slot->state = STATIC_STATE;
650 break;
651 case POWEROFF_STATE:
652 info("Slot %s is already in powering off state\n",
653 p_slot->name);
654 break;
655 case BLINKINGON_STATE:
656 case POWERON_STATE:
657 info("Already disabled on slot %s\n", p_slot->name);
658 break;
659 default:
660 err("Not a valid state on slot %s\n", p_slot->name);
661 break;
662 }
663 mutex_unlock(&p_slot->lock);
664
665 return retval;
666}