pciehp_ctrl.c 15.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * PCI Express Hot Plug Controller Driver
 *
 * Copyright (C) 1995,2001 Compaq Computer Corporation
 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
 * Copyright (C) 2001 IBM Corp.
 * Copyright (C) 2003-2004 Intel Corporation
 *
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
 * NON INFRINGEMENT.  See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
26
 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
L
Linus Torvalds 已提交
27 28 29 30 31 32 33
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/pci.h>
K
Kenji Kaneshige 已提交
34
#include <linux/workqueue.h>
L
Linus Torvalds 已提交
35 36 37
#include "../pci.h"
#include "pciehp.h"

K
Kenji Kaneshige 已提交
38
static void interrupt_event_handler(struct work_struct *work);
L
Linus Torvalds 已提交
39

K
Kenji Kaneshige 已提交
40
static int queue_interrupt_event(struct slot *p_slot, u32 event_type)
41
{
K
Kenji Kaneshige 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54
	struct event_info *info;

	info = kmalloc(sizeof(*info), GFP_ATOMIC);
	if (!info)
		return -ENOMEM;

	info->event_type = event_type;
	info->p_slot = p_slot;
	INIT_WORK(&info->work, interrupt_event_handler);

	schedule_work(&info->work);

	return 0;
55 56
}

57
u8 pciehp_handle_attention_button(struct slot *p_slot)
L
Linus Torvalds 已提交
58
{
K
Kenji Kaneshige 已提交
59
	u32 event_type;
60
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
61 62

	/* Attention Button Change */
63
	ctrl_dbg(ctrl, "Attention button interrupt received\n");
L
Linus Torvalds 已提交
64 65 66 67

	/*
	 *  Button pressed - See if need to TAKE ACTION!!!
	 */
A
Alex Chiang 已提交
68
	ctrl_info(ctrl, "Button pressed on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
69
	event_type = INT_BUTTON_PRESS;
L
Linus Torvalds 已提交
70

K
Kenji Kaneshige 已提交
71
	queue_interrupt_event(p_slot, event_type);
L
Linus Torvalds 已提交
72 73 74 75

	return 0;
}

76
u8 pciehp_handle_switch_change(struct slot *p_slot)
L
Linus Torvalds 已提交
77 78
{
	u8 getstatus;
K
Kenji Kaneshige 已提交
79
	u32 event_type;
80
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
81 82

	/* Switch Change */
83
	ctrl_dbg(ctrl, "Switch interrupt received\n");
L
Linus Torvalds 已提交
84

K
Kenji Kaneshige 已提交
85
	pciehp_get_latch_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
86 87 88 89
	if (getstatus) {
		/*
		 * Switch opened
		 */
A
Alex Chiang 已提交
90
		ctrl_info(ctrl, "Latch open on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
91
		event_type = INT_SWITCH_OPEN;
L
Linus Torvalds 已提交
92 93 94 95
	} else {
		/*
		 *  Switch closed
		 */
A
Alex Chiang 已提交
96
		ctrl_info(ctrl, "Latch close on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
97
		event_type = INT_SWITCH_CLOSE;
L
Linus Torvalds 已提交
98 99
	}

K
Kenji Kaneshige 已提交
100
	queue_interrupt_event(p_slot, event_type);
L
Linus Torvalds 已提交
101

K
Kenji Kaneshige 已提交
102
	return 1;
L
Linus Torvalds 已提交
103 104
}

105
u8 pciehp_handle_presence_change(struct slot *p_slot)
L
Linus Torvalds 已提交
106
{
K
Kenji Kaneshige 已提交
107 108
	u32 event_type;
	u8 presence_save;
109
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
110 111

	/* Presence Change */
112
	ctrl_dbg(ctrl, "Presence/Notify input change\n");
L
Linus Torvalds 已提交
113 114 115 116

	/* Switch is open, assume a presence change
	 * Save the presence state
	 */
K
Kenji Kaneshige 已提交
117
	pciehp_get_adapter_status(p_slot, &presence_save);
118
	if (presence_save) {
L
Linus Torvalds 已提交
119 120 121
		/*
		 * Card Present
		 */
A
Alex Chiang 已提交
122
		ctrl_info(ctrl, "Card present on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
123
		event_type = INT_PRESENCE_ON;
L
Linus Torvalds 已提交
124 125 126 127
	} else {
		/*
		 * Not Present
		 */
A
Alex Chiang 已提交
128 129
		ctrl_info(ctrl, "Card not present on Slot(%s)\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
130
		event_type = INT_PRESENCE_OFF;
L
Linus Torvalds 已提交
131 132
	}

K
Kenji Kaneshige 已提交
133
	queue_interrupt_event(p_slot, event_type);
L
Linus Torvalds 已提交
134

K
Kenji Kaneshige 已提交
135
	return 1;
L
Linus Torvalds 已提交
136 137
}

138
u8 pciehp_handle_power_fault(struct slot *p_slot)
L
Linus Torvalds 已提交
139
{
K
Kenji Kaneshige 已提交
140
	u32 event_type;
141
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
142 143

	/* power fault */
144
	ctrl_dbg(ctrl, "Power fault interrupt received\n");
L
Linus Torvalds 已提交
145

K
Kenji Kaneshige 已提交
146
	if (!pciehp_query_power_fault(p_slot)) {
L
Linus Torvalds 已提交
147 148 149
		/*
		 * power fault Cleared
		 */
150
		ctrl_info(ctrl, "Power fault cleared on Slot(%s)\n",
A
Alex Chiang 已提交
151
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
152
		event_type = INT_POWER_FAULT_CLEAR;
L
Linus Torvalds 已提交
153 154 155 156
	} else {
		/*
		 *   power fault
		 */
A
Alex Chiang 已提交
157
		ctrl_info(ctrl, "Power fault on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
158
		event_type = INT_POWER_FAULT;
159
		ctrl_info(ctrl, "Power fault bit %x set\n", 0);
L
Linus Torvalds 已提交
160 161
	}

K
Kenji Kaneshige 已提交
162 163 164
	queue_interrupt_event(p_slot, event_type);

	return 1;
L
Linus Torvalds 已提交
165 166
}

167
/* The following routines constitute the bulk of the
L
Linus Torvalds 已提交
168 169 170 171 172 173
   hotplug controller logic
 */

static void set_slot_off(struct controller *ctrl, struct slot * pslot)
{
	/* turn off slot, turn on Amber LED, turn off Green LED if supported*/
174
	if (POWER_CTRL(ctrl)) {
K
Kenji Kaneshige 已提交
175
		if (pciehp_power_off_slot(pslot)) {
176
			ctrl_err(ctrl,
177
				 "Issue of Slot Power Off command failed\n");
L
Linus Torvalds 已提交
178 179
			return;
		}
180 181 182 183 184 185
		/*
		 * After turning power off, we must wait for at least 1 second
		 * before taking any action that relies on power having been
		 * removed from the slot/adapter.
		 */
		msleep(1000);
L
Linus Torvalds 已提交
186 187
	}

188
	if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
189
		pciehp_green_led_off(pslot);
L
Linus Torvalds 已提交
190

191
	if (ATTN_LED(ctrl)) {
K
Kenji Kaneshige 已提交
192
		if (pciehp_set_attention_status(pslot, 1)) {
193 194
			ctrl_err(ctrl,
				 "Issue of Set Attention Led command failed\n");
L
Linus Torvalds 已提交
195 196 197 198 199 200 201
			return;
		}
	}
}

/**
 * board_added - Called after a board has been added to the system.
R
Randy Dunlap 已提交
202
 * @p_slot: &slot where board is added
L
Linus Torvalds 已提交
203
 *
R
Randy Dunlap 已提交
204 205
 * Turns power on for the board.
 * Configures board.
L
Linus Torvalds 已提交
206
 */
207
static int board_added(struct slot *p_slot)
L
Linus Torvalds 已提交
208
{
209
	int retval = 0;
210
	struct controller *ctrl = p_slot->ctrl;
211
	struct pci_bus *parent = ctrl->pcie->port->subordinate;
L
Linus Torvalds 已提交
212

213
	if (POWER_CTRL(ctrl)) {
L
Linus Torvalds 已提交
214
		/* Power on slot */
K
Kenji Kaneshige 已提交
215
		retval = pciehp_power_on_slot(p_slot);
216 217
		if (retval)
			return retval;
L
Linus Torvalds 已提交
218
	}
219

220
	if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
221
		pciehp_green_led_blink(p_slot);
L
Linus Torvalds 已提交
222

223
	/* Check link training status */
K
Kenji Kaneshige 已提交
224
	retval = pciehp_check_link_status(ctrl);
225
	if (retval) {
226
		ctrl_err(ctrl, "Failed to check link status\n");
L
Linus Torvalds 已提交
227
		set_slot_off(ctrl, p_slot);
228
		return retval;
L
Linus Torvalds 已提交
229 230 231
	}

	/* Check for a power fault */
K
Kenji Kaneshige 已提交
232
	if (pciehp_query_power_fault(p_slot)) {
233
		ctrl_dbg(ctrl, "Power fault detected\n");
234
		retval = -EIO;
235
		goto err_exit;
L
Linus Torvalds 已提交
236 237
	}

238 239
	retval = pciehp_configure_device(p_slot);
	if (retval) {
240 241
		ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
			 pci_domain_nr(parent), parent->number);
242 243
		goto err_exit;
	}
L
Linus Torvalds 已提交
244

245
	if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
246
		pciehp_green_led_on(p_slot);
247

L
Linus Torvalds 已提交
248
	return 0;
249 250 251

err_exit:
	set_slot_off(ctrl, p_slot);
252
	return retval;
L
Linus Torvalds 已提交
253 254 255
}

/**
R
Randy Dunlap 已提交
256 257
 * remove_board - Turns off slot and LEDs
 * @p_slot: slot where board is being removed
L
Linus Torvalds 已提交
258
 */
259
static int remove_board(struct slot *p_slot)
L
Linus Torvalds 已提交
260
{
261
	int retval = 0;
262
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
263

264 265 266
	retval = pciehp_unconfigure_device(p_slot);
	if (retval)
		return retval;
L
Linus Torvalds 已提交
267

268
	if (POWER_CTRL(ctrl)) {
L
Linus Torvalds 已提交
269
		/* power off slot */
K
Kenji Kaneshige 已提交
270
		retval = pciehp_power_off_slot(p_slot);
271
		if (retval) {
272 273
			ctrl_err(ctrl,
				 "Issue of Slot Disable command failed\n");
274
			return retval;
L
Linus Torvalds 已提交
275
		}
276 277 278 279 280 281
		/*
		 * After turning power off, we must wait for at least 1 second
		 * before taking any action that relies on power having been
		 * removed from the slot/adapter.
		 */
		msleep(1000);
L
Linus Torvalds 已提交
282 283
	}

K
Kenji Kaneshige 已提交
284
	/* turn off Green LED */
285
	if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
286
		pciehp_green_led_off(p_slot);
L
Linus Torvalds 已提交
287 288 289 290

	return 0;
}

K
Kenji Kaneshige 已提交
291 292 293 294
struct power_work_info {
	struct slot *p_slot;
	struct work_struct work;
};
L
Linus Torvalds 已提交
295 296

/**
R
Randy Dunlap 已提交
297 298
 * pciehp_power_thread - handle pushbutton events
 * @work: &struct work_struct describing work to be done
L
Linus Torvalds 已提交
299
 *
R
Randy Dunlap 已提交
300
 * Scheduled procedure to handle blocking stuff for the pushbuttons.
L
Linus Torvalds 已提交
301 302
 * Handles all pending events and exits.
 */
K
Kenji Kaneshige 已提交
303
static void pciehp_power_thread(struct work_struct *work)
L
Linus Torvalds 已提交
304
{
K
Kenji Kaneshige 已提交
305 306 307 308 309 310 311 312
	struct power_work_info *info =
		container_of(work, struct power_work_info, work);
	struct slot *p_slot = info->p_slot;

	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case POWEROFF_STATE:
		mutex_unlock(&p_slot->lock);
313
		ctrl_dbg(p_slot->ctrl,
314
			 "Disabling domain:bus:device=%04x:%02x:00\n",
315 316
			 pci_domain_nr(p_slot->ctrl->pcie->port->subordinate),
			 p_slot->ctrl->pcie->port->subordinate->number);
L
Linus Torvalds 已提交
317
		pciehp_disable_slot(p_slot);
K
Kenji Kaneshige 已提交
318
		mutex_lock(&p_slot->lock);
L
Linus Torvalds 已提交
319
		p_slot->state = STATIC_STATE;
K
Kenji Kaneshige 已提交
320 321 322
		break;
	case POWERON_STATE:
		mutex_unlock(&p_slot->lock);
K
Kenji Kaneshige 已提交
323 324
		if (pciehp_enable_slot(p_slot) && PWR_LED(p_slot->ctrl))
			pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
325
		mutex_lock(&p_slot->lock);
L
Linus Torvalds 已提交
326
		p_slot->state = STATIC_STATE;
K
Kenji Kaneshige 已提交
327 328 329
		break;
	default:
		break;
L
Linus Torvalds 已提交
330
	}
K
Kenji Kaneshige 已提交
331
	mutex_unlock(&p_slot->lock);
L
Linus Torvalds 已提交
332

K
Kenji Kaneshige 已提交
333
	kfree(info);
L
Linus Torvalds 已提交
334 335
}

336
void pciehp_queue_pushbutton_work(struct work_struct *work)
L
Linus Torvalds 已提交
337
{
K
Kenji Kaneshige 已提交
338 339
	struct slot *p_slot = container_of(work, struct slot, work.work);
	struct power_work_info *info;
L
Linus Torvalds 已提交
340

K
Kenji Kaneshige 已提交
341 342
	info = kmalloc(sizeof(*info), GFP_KERNEL);
	if (!info) {
343 344
		ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
			 __func__);
L
Linus Torvalds 已提交
345 346
		return;
	}
K
Kenji Kaneshige 已提交
347 348
	info->p_slot = p_slot;
	INIT_WORK(&info->work, pciehp_power_thread);
L
Linus Torvalds 已提交
349

K
Kenji Kaneshige 已提交
350 351 352
	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGOFF_STATE:
L
Linus Torvalds 已提交
353
		p_slot->state = POWEROFF_STATE;
K
Kenji Kaneshige 已提交
354 355
		break;
	case BLINKINGON_STATE:
L
Linus Torvalds 已提交
356
		p_slot->state = POWERON_STATE;
K
Kenji Kaneshige 已提交
357 358 359
		break;
	default:
		goto out;
L
Linus Torvalds 已提交
360
	}
K
Kenji Kaneshige 已提交
361 362 363
	queue_work(pciehp_wq, &info->work);
 out:
	mutex_unlock(&p_slot->lock);
L
Linus Torvalds 已提交
364 365 366 367 368 369 370
}

static int update_slot_info(struct slot *slot)
{
	struct hotplug_slot_info *info;
	int result;

K
Kenji Kaneshige 已提交
371
	info = kmalloc(sizeof(*info), GFP_KERNEL);
L
Linus Torvalds 已提交
372 373 374
	if (!info)
		return -ENOMEM;

K
Kenji Kaneshige 已提交
375 376 377 378
	pciehp_get_power_status(slot, &info->power_status);
	pciehp_get_attention_status(slot, &info->attention_status);
	pciehp_get_latch_status(slot, &info->latch_status);
	pciehp_get_adapter_status(slot, &info->adapter_status);
L
Linus Torvalds 已提交
379 380 381 382 383 384

	result = pci_hp_change_slot_info(slot->hotplug_slot, info);
	kfree (info);
	return result;
}

K
Kenji Kaneshige 已提交
385 386 387 388
/*
 * Note: This function must be called with slot->lock held
 */
static void handle_button_press_event(struct slot *p_slot)
L
Linus Torvalds 已提交
389
{
K
Kenji Kaneshige 已提交
390
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
391 392
	u8 getstatus;

K
Kenji Kaneshige 已提交
393 394
	switch (p_slot->state) {
	case STATIC_STATE:
K
Kenji Kaneshige 已提交
395
		pciehp_get_power_status(p_slot, &getstatus);
K
Kenji Kaneshige 已提交
396 397
		if (getstatus) {
			p_slot->state = BLINKINGOFF_STATE;
398 399
			ctrl_info(ctrl,
				  "PCI slot #%s - powering off due to button "
A
Alex Chiang 已提交
400
				  "press.\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
401 402
		} else {
			p_slot->state = BLINKINGON_STATE;
403 404
			ctrl_info(ctrl,
				  "PCI slot #%s - powering on due to button "
A
Alex Chiang 已提交
405
				  "press.\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
406 407
		}
		/* blink green LED and turn off amber */
408
		if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
409
			pciehp_green_led_blink(p_slot);
410
		if (ATTN_LED(ctrl))
K
Kenji Kaneshige 已提交
411
			pciehp_set_attention_status(p_slot, 0);
K
Kenji Kaneshige 已提交
412 413 414 415 416 417 418 419 420 421

		schedule_delayed_work(&p_slot->work, 5*HZ);
		break;
	case BLINKINGOFF_STATE:
	case BLINKINGON_STATE:
		/*
		 * Cancel if we are still blinking; this means that we
		 * press the attention again before the 5 sec. limit
		 * expires to cancel hot-add or hot-remove
		 */
A
Alex Chiang 已提交
422
		ctrl_info(ctrl, "Button cancel on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
423 424
		cancel_delayed_work(&p_slot->work);
		if (p_slot->state == BLINKINGOFF_STATE) {
425
			if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
426
				pciehp_green_led_on(p_slot);
K
Kenji Kaneshige 已提交
427
		} else {
428
			if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
429
				pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
430
		}
431
		if (ATTN_LED(ctrl))
K
Kenji Kaneshige 已提交
432
			pciehp_set_attention_status(p_slot, 0);
433
		ctrl_info(ctrl, "PCI slot #%s - action canceled "
A
Alex Chiang 已提交
434
			  "due to button press\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
435 436 437 438 439 440 441 442 443
		p_slot->state = STATIC_STATE;
		break;
	case POWEROFF_STATE:
	case POWERON_STATE:
		/*
		 * Ignore if the slot is on power-on or power-off state;
		 * this means that the previous attention button action
		 * to hot-add or hot-remove is undergoing
		 */
A
Alex Chiang 已提交
444
		ctrl_info(ctrl, "Button ignore on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
445 446 447
		update_slot_info(p_slot);
		break;
	default:
448
		ctrl_warn(ctrl, "Not a valid state\n");
K
Kenji Kaneshige 已提交
449
		break;
L
Linus Torvalds 已提交
450 451 452
	}
}

K
Kenji Kaneshige 已提交
453 454 455 456 457 458 459 460 461 462
/*
 * Note: This function must be called with slot->lock held
 */
static void handle_surprise_event(struct slot *p_slot)
{
	u8 getstatus;
	struct power_work_info *info;

	info = kmalloc(sizeof(*info), GFP_KERNEL);
	if (!info) {
463 464
		ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
			 __func__);
K
Kenji Kaneshige 已提交
465 466 467 468 469
		return;
	}
	info->p_slot = p_slot;
	INIT_WORK(&info->work, pciehp_power_thread);

K
Kenji Kaneshige 已提交
470
	pciehp_get_adapter_status(p_slot, &getstatus);
K
Kenji Kaneshige 已提交
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
	if (!getstatus)
		p_slot->state = POWEROFF_STATE;
	else
		p_slot->state = POWERON_STATE;

	queue_work(pciehp_wq, &info->work);
}

static void interrupt_event_handler(struct work_struct *work)
{
	struct event_info *info = container_of(work, struct event_info, work);
	struct slot *p_slot = info->p_slot;
	struct controller *ctrl = p_slot->ctrl;

	mutex_lock(&p_slot->lock);
	switch (info->event_type) {
	case INT_BUTTON_PRESS:
		handle_button_press_event(p_slot);
		break;
	case INT_POWER_FAULT:
491
		if (!POWER_CTRL(ctrl))
K
Kenji Kaneshige 已提交
492
			break;
493
		if (ATTN_LED(ctrl))
K
Kenji Kaneshige 已提交
494
			pciehp_set_attention_status(p_slot, 1);
495
		if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
496
			pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
497 498 499
		break;
	case INT_PRESENCE_ON:
	case INT_PRESENCE_OFF:
500
		if (!HP_SUPR_RM(ctrl))
K
Kenji Kaneshige 已提交
501
			break;
502
		ctrl_dbg(ctrl, "Surprise Removal\n");
K
Kenji Kaneshige 已提交
503 504 505 506 507 508 509 510 511 512 513 514
		update_slot_info(p_slot);
		handle_surprise_event(p_slot);
		break;
	default:
		update_slot_info(p_slot);
		break;
	}
	mutex_unlock(&p_slot->lock);

	kfree(info);
}

L
Linus Torvalds 已提交
515 516 517 518
int pciehp_enable_slot(struct slot *p_slot)
{
	u8 getstatus = 0;
	int rc;
519
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
520

K
Kenji Kaneshige 已提交
521
	rc = pciehp_get_adapter_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
522
	if (rc || !getstatus) {
523
		ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
524
		return -ENODEV;
L
Linus Torvalds 已提交
525
	}
526
	if (MRL_SENS(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
527
		rc = pciehp_get_latch_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
528
		if (rc || getstatus) {
529 530
			ctrl_info(ctrl, "Latch open on slot(%s)\n",
				  slot_name(p_slot));
531
			return -ENODEV;
L
Linus Torvalds 已提交
532 533
		}
	}
534

535
	if (POWER_CTRL(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
536
		rc = pciehp_get_power_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
537
		if (rc || getstatus) {
538 539
			ctrl_info(ctrl, "Already enabled on slot(%s)\n",
				  slot_name(p_slot));
540
			return -EINVAL;
L
Linus Torvalds 已提交
541 542 543
		}
	}

K
Kenji Kaneshige 已提交
544
	pciehp_get_latch_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
545

546
	rc = board_added(p_slot);
L
Linus Torvalds 已提交
547
	if (rc) {
K
Kenji Kaneshige 已提交
548
		pciehp_get_latch_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
549 550
	}

551
	update_slot_info(p_slot);
L
Linus Torvalds 已提交
552 553 554 555 556 557 558 559 560

	return rc;
}


int pciehp_disable_slot(struct slot *p_slot)
{
	u8 getstatus = 0;
	int ret = 0;
561
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
562 563 564 565

	if (!p_slot->ctrl)
		return 1;

566
	if (!HP_SUPR_RM(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
567
		ret = pciehp_get_adapter_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
568
		if (ret || !getstatus) {
569 570
			ctrl_info(ctrl, "No adapter on slot(%s)\n",
				  slot_name(p_slot));
571
			return -ENODEV;
L
Linus Torvalds 已提交
572 573 574
		}
	}

575
	if (MRL_SENS(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
576
		ret = pciehp_get_latch_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
577
		if (ret || getstatus) {
578 579
			ctrl_info(ctrl, "Latch open on slot(%s)\n",
				  slot_name(p_slot));
580
			return -ENODEV;
L
Linus Torvalds 已提交
581 582 583
		}
	}

584
	if (POWER_CTRL(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
585
		ret = pciehp_get_power_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
586
		if (ret || !getstatus) {
587 588
			ctrl_info(ctrl, "Already disabled on slot(%s)\n",
				  slot_name(p_slot));
589
			return -EINVAL;
L
Linus Torvalds 已提交
590 591 592
		}
	}

593 594
	ret = remove_board(p_slot);
	update_slot_info(p_slot);
K
Kenji Kaneshige 已提交
595

596
	return ret;
L
Linus Torvalds 已提交
597 598
}

K
Kenji Kaneshige 已提交
599 600 601
int pciehp_sysfs_enable_slot(struct slot *p_slot)
{
	int retval = -ENODEV;
602
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615

	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGON_STATE:
		cancel_delayed_work(&p_slot->work);
	case STATIC_STATE:
		p_slot->state = POWERON_STATE;
		mutex_unlock(&p_slot->lock);
		retval = pciehp_enable_slot(p_slot);
		mutex_lock(&p_slot->lock);
		p_slot->state = STATIC_STATE;
		break;
	case POWERON_STATE:
616
		ctrl_info(ctrl, "Slot %s is already in powering on state\n",
A
Alex Chiang 已提交
617
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
618 619 620
		break;
	case BLINKINGOFF_STATE:
	case POWEROFF_STATE:
A
Alex Chiang 已提交
621 622
		ctrl_info(ctrl, "Already enabled on slot %s\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
623 624
		break;
	default:
A
Alex Chiang 已提交
625 626
		ctrl_err(ctrl, "Not a valid state on slot %s\n",
			 slot_name(p_slot));
K
Kenji Kaneshige 已提交
627 628 629 630 631 632 633 634 635 636
		break;
	}
	mutex_unlock(&p_slot->lock);

	return retval;
}

int pciehp_sysfs_disable_slot(struct slot *p_slot)
{
	int retval = -ENODEV;
637
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650

	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGOFF_STATE:
		cancel_delayed_work(&p_slot->work);
	case STATIC_STATE:
		p_slot->state = POWEROFF_STATE;
		mutex_unlock(&p_slot->lock);
		retval = pciehp_disable_slot(p_slot);
		mutex_lock(&p_slot->lock);
		p_slot->state = STATIC_STATE;
		break;
	case POWEROFF_STATE:
651
		ctrl_info(ctrl, "Slot %s is already in powering off state\n",
A
Alex Chiang 已提交
652
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
653 654 655
		break;
	case BLINKINGON_STATE:
	case POWERON_STATE:
A
Alex Chiang 已提交
656 657
		ctrl_info(ctrl, "Already disabled on slot %s\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
658 659
		break;
	default:
A
Alex Chiang 已提交
660 661
		ctrl_err(ctrl, "Not a valid state on slot %s\n",
			 slot_name(p_slot));
K
Kenji Kaneshige 已提交
662 663 664 665 666 667
		break;
	}
	mutex_unlock(&p_slot->lock);

	return retval;
}