pciehp_ctrl.c 14.5 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
 *
 */

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

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

K
Kenji Kaneshige 已提交
41
static int queue_interrupt_event(struct slot *p_slot, u32 event_type)
42
{
K
Kenji Kaneshige 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55
	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;
56 57
}

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

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

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

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

	return 0;
}

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

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

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

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

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

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

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

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

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

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

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

	/* power fault */
145
	ctrl_dbg(ctrl, "Power fault interrupt received\n");
146 147 148
	ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
	event_type = INT_POWER_FAULT;
	ctrl_info(ctrl, "Power fault bit %x set\n", 0);
K
Kenji Kaneshige 已提交
149 150 151
	queue_interrupt_event(p_slot, event_type);

	return 1;
L
Linus Torvalds 已提交
152 153
}

154
/* The following routines constitute the bulk of the
L
Linus Torvalds 已提交
155 156 157 158 159 160
   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*/
161
	if (POWER_CTRL(ctrl)) {
K
Kenji Kaneshige 已提交
162
		if (pciehp_power_off_slot(pslot)) {
163
			ctrl_err(ctrl,
164
				 "Issue of Slot Power Off command failed\n");
L
Linus Torvalds 已提交
165 166
			return;
		}
167 168 169 170 171 172
		/*
		 * 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 已提交
173 174
	}

175
	if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
176
		pciehp_green_led_off(pslot);
L
Linus Torvalds 已提交
177

178
	if (ATTN_LED(ctrl)) {
K
Kenji Kaneshige 已提交
179
		if (pciehp_set_attention_status(pslot, 1)) {
180 181
			ctrl_err(ctrl,
				 "Issue of Set Attention Led command failed\n");
L
Linus Torvalds 已提交
182 183 184 185 186 187 188
			return;
		}
	}
}

/**
 * board_added - Called after a board has been added to the system.
R
Randy Dunlap 已提交
189
 * @p_slot: &slot where board is added
L
Linus Torvalds 已提交
190
 *
R
Randy Dunlap 已提交
191 192
 * Turns power on for the board.
 * Configures board.
L
Linus Torvalds 已提交
193
 */
194
static int board_added(struct slot *p_slot)
L
Linus Torvalds 已提交
195
{
196
	int retval = 0;
197
	struct controller *ctrl = p_slot->ctrl;
198
	struct pci_bus *parent = ctrl->pcie->port->subordinate;
L
Linus Torvalds 已提交
199

200
	if (POWER_CTRL(ctrl)) {
L
Linus Torvalds 已提交
201
		/* Power on slot */
K
Kenji Kaneshige 已提交
202
		retval = pciehp_power_on_slot(p_slot);
203 204
		if (retval)
			return retval;
L
Linus Torvalds 已提交
205
	}
206

207
	if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
208
		pciehp_green_led_blink(p_slot);
L
Linus Torvalds 已提交
209

210
	/* Check link training status */
K
Kenji Kaneshige 已提交
211
	retval = pciehp_check_link_status(ctrl);
212
	if (retval) {
213
		ctrl_err(ctrl, "Failed to check link status\n");
214
		goto err_exit;
L
Linus Torvalds 已提交
215 216 217
	}

	/* Check for a power fault */
218 219
	if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
		ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
220
		retval = -EIO;
221
		goto err_exit;
L
Linus Torvalds 已提交
222 223
	}

224 225
	retval = pciehp_configure_device(p_slot);
	if (retval) {
226 227
		ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
			 pci_domain_nr(parent), parent->number);
228 229
		goto err_exit;
	}
L
Linus Torvalds 已提交
230

231
	if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
232
		pciehp_green_led_on(p_slot);
233

L
Linus Torvalds 已提交
234
	return 0;
235 236 237

err_exit:
	set_slot_off(ctrl, p_slot);
238
	return retval;
L
Linus Torvalds 已提交
239 240 241
}

/**
R
Randy Dunlap 已提交
242 243
 * remove_board - Turns off slot and LEDs
 * @p_slot: slot where board is being removed
L
Linus Torvalds 已提交
244
 */
245
static int remove_board(struct slot *p_slot)
L
Linus Torvalds 已提交
246
{
247
	int retval = 0;
248
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
249

250 251 252
	retval = pciehp_unconfigure_device(p_slot);
	if (retval)
		return retval;
L
Linus Torvalds 已提交
253

254
	if (POWER_CTRL(ctrl)) {
L
Linus Torvalds 已提交
255
		/* power off slot */
K
Kenji Kaneshige 已提交
256
		retval = pciehp_power_off_slot(p_slot);
257
		if (retval) {
258 259
			ctrl_err(ctrl,
				 "Issue of Slot Disable command failed\n");
260
			return retval;
L
Linus Torvalds 已提交
261
		}
262 263 264 265 266 267
		/*
		 * 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 已提交
268 269
	}

K
Kenji Kaneshige 已提交
270
	/* turn off Green LED */
271
	if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
272
		pciehp_green_led_off(p_slot);
L
Linus Torvalds 已提交
273 274 275 276

	return 0;
}

K
Kenji Kaneshige 已提交
277 278 279 280
struct power_work_info {
	struct slot *p_slot;
	struct work_struct work;
};
L
Linus Torvalds 已提交
281 282

/**
R
Randy Dunlap 已提交
283 284
 * pciehp_power_thread - handle pushbutton events
 * @work: &struct work_struct describing work to be done
L
Linus Torvalds 已提交
285
 *
R
Randy Dunlap 已提交
286
 * Scheduled procedure to handle blocking stuff for the pushbuttons.
L
Linus Torvalds 已提交
287 288
 * Handles all pending events and exits.
 */
K
Kenji Kaneshige 已提交
289
static void pciehp_power_thread(struct work_struct *work)
L
Linus Torvalds 已提交
290
{
K
Kenji Kaneshige 已提交
291 292 293 294 295 296 297 298
	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);
299
		ctrl_dbg(p_slot->ctrl,
300
			 "Disabling domain:bus:device=%04x:%02x:00\n",
301 302
			 pci_domain_nr(p_slot->ctrl->pcie->port->subordinate),
			 p_slot->ctrl->pcie->port->subordinate->number);
L
Linus Torvalds 已提交
303
		pciehp_disable_slot(p_slot);
K
Kenji Kaneshige 已提交
304
		mutex_lock(&p_slot->lock);
L
Linus Torvalds 已提交
305
		p_slot->state = STATIC_STATE;
K
Kenji Kaneshige 已提交
306 307 308
		break;
	case POWERON_STATE:
		mutex_unlock(&p_slot->lock);
K
Kenji Kaneshige 已提交
309 310
		if (pciehp_enable_slot(p_slot) && PWR_LED(p_slot->ctrl))
			pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
311
		mutex_lock(&p_slot->lock);
L
Linus Torvalds 已提交
312
		p_slot->state = STATIC_STATE;
K
Kenji Kaneshige 已提交
313 314 315
		break;
	default:
		break;
L
Linus Torvalds 已提交
316
	}
K
Kenji Kaneshige 已提交
317
	mutex_unlock(&p_slot->lock);
L
Linus Torvalds 已提交
318

K
Kenji Kaneshige 已提交
319
	kfree(info);
L
Linus Torvalds 已提交
320 321
}

322
void pciehp_queue_pushbutton_work(struct work_struct *work)
L
Linus Torvalds 已提交
323
{
K
Kenji Kaneshige 已提交
324 325
	struct slot *p_slot = container_of(work, struct slot, work.work);
	struct power_work_info *info;
L
Linus Torvalds 已提交
326

K
Kenji Kaneshige 已提交
327 328
	info = kmalloc(sizeof(*info), GFP_KERNEL);
	if (!info) {
329 330
		ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
			 __func__);
L
Linus Torvalds 已提交
331 332
		return;
	}
K
Kenji Kaneshige 已提交
333 334
	info->p_slot = p_slot;
	INIT_WORK(&info->work, pciehp_power_thread);
L
Linus Torvalds 已提交
335

K
Kenji Kaneshige 已提交
336 337 338
	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGOFF_STATE:
L
Linus Torvalds 已提交
339
		p_slot->state = POWEROFF_STATE;
K
Kenji Kaneshige 已提交
340 341
		break;
	case BLINKINGON_STATE:
L
Linus Torvalds 已提交
342
		p_slot->state = POWERON_STATE;
K
Kenji Kaneshige 已提交
343 344
		break;
	default:
J
Jiri Slaby 已提交
345
		kfree(info);
K
Kenji Kaneshige 已提交
346
		goto out;
L
Linus Torvalds 已提交
347
	}
K
Kenji Kaneshige 已提交
348 349 350
	queue_work(pciehp_wq, &info->work);
 out:
	mutex_unlock(&p_slot->lock);
L
Linus Torvalds 已提交
351 352
}

K
Kenji Kaneshige 已提交
353 354 355 356
/*
 * Note: This function must be called with slot->lock held
 */
static void handle_button_press_event(struct slot *p_slot)
L
Linus Torvalds 已提交
357
{
K
Kenji Kaneshige 已提交
358
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
359 360
	u8 getstatus;

K
Kenji Kaneshige 已提交
361 362
	switch (p_slot->state) {
	case STATIC_STATE:
K
Kenji Kaneshige 已提交
363
		pciehp_get_power_status(p_slot, &getstatus);
K
Kenji Kaneshige 已提交
364 365
		if (getstatus) {
			p_slot->state = BLINKINGOFF_STATE;
366 367
			ctrl_info(ctrl,
				  "PCI slot #%s - powering off due to button "
A
Alex Chiang 已提交
368
				  "press.\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
369 370
		} else {
			p_slot->state = BLINKINGON_STATE;
371 372
			ctrl_info(ctrl,
				  "PCI slot #%s - powering on due to button "
A
Alex Chiang 已提交
373
				  "press.\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
374 375
		}
		/* blink green LED and turn off amber */
376
		if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
377
			pciehp_green_led_blink(p_slot);
378
		if (ATTN_LED(ctrl))
K
Kenji Kaneshige 已提交
379
			pciehp_set_attention_status(p_slot, 0);
K
Kenji Kaneshige 已提交
380 381 382 383 384 385 386 387 388 389

		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 已提交
390
		ctrl_info(ctrl, "Button cancel on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
391 392
		cancel_delayed_work(&p_slot->work);
		if (p_slot->state == BLINKINGOFF_STATE) {
393
			if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
394
				pciehp_green_led_on(p_slot);
K
Kenji Kaneshige 已提交
395
		} else {
396
			if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
397
				pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
398
		}
399
		if (ATTN_LED(ctrl))
K
Kenji Kaneshige 已提交
400
			pciehp_set_attention_status(p_slot, 0);
401
		ctrl_info(ctrl, "PCI slot #%s - action canceled "
A
Alex Chiang 已提交
402
			  "due to button press\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
403 404 405 406 407 408 409 410 411
		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 已提交
412
		ctrl_info(ctrl, "Button ignore on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
413 414
		break;
	default:
415
		ctrl_warn(ctrl, "Not a valid state\n");
K
Kenji Kaneshige 已提交
416
		break;
L
Linus Torvalds 已提交
417 418 419
	}
}

K
Kenji Kaneshige 已提交
420 421 422 423 424 425 426 427 428 429
/*
 * 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) {
430 431
		ctrl_err(p_slot->ctrl, "%s: Cannot allocate memory\n",
			 __func__);
K
Kenji Kaneshige 已提交
432 433 434 435 436
		return;
	}
	info->p_slot = p_slot;
	INIT_WORK(&info->work, pciehp_power_thread);

K
Kenji Kaneshige 已提交
437
	pciehp_get_adapter_status(p_slot, &getstatus);
K
Kenji Kaneshige 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
	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:
458
		if (!POWER_CTRL(ctrl))
K
Kenji Kaneshige 已提交
459
			break;
460
		if (ATTN_LED(ctrl))
K
Kenji Kaneshige 已提交
461
			pciehp_set_attention_status(p_slot, 1);
462
		if (PWR_LED(ctrl))
K
Kenji Kaneshige 已提交
463
			pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
464 465 466
		break;
	case INT_PRESENCE_ON:
	case INT_PRESENCE_OFF:
467
		if (!HP_SUPR_RM(ctrl))
K
Kenji Kaneshige 已提交
468
			break;
469
		ctrl_dbg(ctrl, "Surprise Removal\n");
K
Kenji Kaneshige 已提交
470 471 472 473 474 475 476 477 478 479
		handle_surprise_event(p_slot);
		break;
	default:
		break;
	}
	mutex_unlock(&p_slot->lock);

	kfree(info);
}

L
Linus Torvalds 已提交
480 481 482 483
int pciehp_enable_slot(struct slot *p_slot)
{
	u8 getstatus = 0;
	int rc;
484
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
485

K
Kenji Kaneshige 已提交
486
	rc = pciehp_get_adapter_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
487
	if (rc || !getstatus) {
488
		ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
489
		return -ENODEV;
L
Linus Torvalds 已提交
490
	}
491
	if (MRL_SENS(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
492
		rc = pciehp_get_latch_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
493
		if (rc || getstatus) {
494 495
			ctrl_info(ctrl, "Latch open on slot(%s)\n",
				  slot_name(p_slot));
496
			return -ENODEV;
L
Linus Torvalds 已提交
497 498
		}
	}
499

500
	if (POWER_CTRL(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
501
		rc = pciehp_get_power_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
502
		if (rc || getstatus) {
503 504
			ctrl_info(ctrl, "Already enabled on slot(%s)\n",
				  slot_name(p_slot));
505
			return -EINVAL;
L
Linus Torvalds 已提交
506 507 508
		}
	}

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

511
	rc = board_added(p_slot);
L
Linus Torvalds 已提交
512
	if (rc) {
K
Kenji Kaneshige 已提交
513
		pciehp_get_latch_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
514 515 516 517 518 519 520 521 522
	}
	return rc;
}


int pciehp_disable_slot(struct slot *p_slot)
{
	u8 getstatus = 0;
	int ret = 0;
523
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
524 525 526 527

	if (!p_slot->ctrl)
		return 1;

528
	if (!HP_SUPR_RM(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
529
		ret = pciehp_get_adapter_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
530
		if (ret || !getstatus) {
531 532
			ctrl_info(ctrl, "No adapter on slot(%s)\n",
				  slot_name(p_slot));
533
			return -ENODEV;
L
Linus Torvalds 已提交
534 535 536
		}
	}

537
	if (MRL_SENS(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
538
		ret = pciehp_get_latch_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
539
		if (ret || getstatus) {
540 541
			ctrl_info(ctrl, "Latch open on slot(%s)\n",
				  slot_name(p_slot));
542
			return -ENODEV;
L
Linus Torvalds 已提交
543 544 545
		}
	}

546
	if (POWER_CTRL(p_slot->ctrl)) {
K
Kenji Kaneshige 已提交
547
		ret = pciehp_get_power_status(p_slot, &getstatus);
L
Linus Torvalds 已提交
548
		if (ret || !getstatus) {
549 550
			ctrl_info(ctrl, "Already disabled on slot(%s)\n",
				  slot_name(p_slot));
551
			return -EINVAL;
L
Linus Torvalds 已提交
552 553 554
		}
	}

555
	return remove_board(p_slot);
L
Linus Torvalds 已提交
556 557
}

K
Kenji Kaneshige 已提交
558 559 560
int pciehp_sysfs_enable_slot(struct slot *p_slot)
{
	int retval = -ENODEV;
561
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
562 563 564 565 566 567 568 569 570 571 572 573 574

	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:
575
		ctrl_info(ctrl, "Slot %s is already in powering on state\n",
A
Alex Chiang 已提交
576
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
577 578 579
		break;
	case BLINKINGOFF_STATE:
	case POWEROFF_STATE:
A
Alex Chiang 已提交
580 581
		ctrl_info(ctrl, "Already enabled on slot %s\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
582 583
		break;
	default:
A
Alex Chiang 已提交
584 585
		ctrl_err(ctrl, "Not a valid state on slot %s\n",
			 slot_name(p_slot));
K
Kenji Kaneshige 已提交
586 587 588 589 590 591 592 593 594 595
		break;
	}
	mutex_unlock(&p_slot->lock);

	return retval;
}

int pciehp_sysfs_disable_slot(struct slot *p_slot)
{
	int retval = -ENODEV;
596
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
597 598 599 600 601 602 603 604 605 606 607 608 609

	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:
610
		ctrl_info(ctrl, "Slot %s is already in powering off state\n",
A
Alex Chiang 已提交
611
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
612 613 614
		break;
	case BLINKINGON_STATE:
	case POWERON_STATE:
A
Alex Chiang 已提交
615 616
		ctrl_info(ctrl, "Already disabled on slot %s\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
617 618
		break;
	default:
A
Alex Chiang 已提交
619 620
		ctrl_err(ctrl, "Not a valid state on slot %s\n",
			 slot_name(p_slot));
K
Kenji Kaneshige 已提交
621 622 623 624 625 626
		break;
	}
	mutex_unlock(&p_slot->lock);

	return retval;
}