pciehp_ctrl.c 13.1 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 35 36 37
#include <linux/pci.h>
#include "../pci.h"
#include "pciehp.h"

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

40
void pciehp_queue_interrupt_event(struct slot *p_slot, u32 event_type)
41
{
K
Kenji Kaneshige 已提交
42 43 44
	struct event_info *info;

	info = kmalloc(sizeof(*info), GFP_ATOMIC);
45 46 47 48
	if (!info) {
		ctrl_err(p_slot->ctrl, "dropped event %d (ENOMEM)\n", event_type);
		return;
	}
K
Kenji Kaneshige 已提交
49

50
	INIT_WORK(&info->work, interrupt_event_handler);
K
Kenji Kaneshige 已提交
51 52
	info->event_type = event_type;
	info->p_slot = p_slot;
53
	queue_work(p_slot->wq, &info->work);
54 55
}

56
/* The following routines constitute the bulk of the
L
Linus Torvalds 已提交
57 58 59
   hotplug controller logic
 */

R
Ryan Desfosses 已提交
60
static void set_slot_off(struct controller *ctrl, struct slot *pslot)
L
Linus Torvalds 已提交
61 62
{
	/* turn off slot, turn on Amber LED, turn off Green LED if supported*/
63
	if (POWER_CTRL(ctrl)) {
64 65
		pciehp_power_off_slot(pslot);

66 67 68 69 70 71
		/*
		 * 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 已提交
72 73
	}

74 75
	pciehp_green_led_off(pslot);
	pciehp_set_attention_status(pslot, 1);
L
Linus Torvalds 已提交
76 77 78 79
}

/**
 * board_added - Called after a board has been added to the system.
R
Randy Dunlap 已提交
80
 * @p_slot: &slot where board is added
L
Linus Torvalds 已提交
81
 *
R
Randy Dunlap 已提交
82 83
 * Turns power on for the board.
 * Configures board.
L
Linus Torvalds 已提交
84
 */
85
static int board_added(struct slot *p_slot)
L
Linus Torvalds 已提交
86
{
87
	int retval = 0;
88
	struct controller *ctrl = p_slot->ctrl;
89
	struct pci_bus *parent = ctrl->pcie->port->subordinate;
L
Linus Torvalds 已提交
90

91
	if (POWER_CTRL(ctrl)) {
L
Linus Torvalds 已提交
92
		/* Power on slot */
K
Kenji Kaneshige 已提交
93
		retval = pciehp_power_on_slot(p_slot);
94 95
		if (retval)
			return retval;
L
Linus Torvalds 已提交
96
	}
97

98
	pciehp_green_led_blink(p_slot);
L
Linus Torvalds 已提交
99

100
	/* Check link training status */
K
Kenji Kaneshige 已提交
101
	retval = pciehp_check_link_status(ctrl);
102
	if (retval) {
103
		ctrl_err(ctrl, "Failed to check link status\n");
104
		goto err_exit;
L
Linus Torvalds 已提交
105 106 107
	}

	/* Check for a power fault */
108 109
	if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
		ctrl_err(ctrl, "Power fault on slot %s\n", slot_name(p_slot));
110
		retval = -EIO;
111
		goto err_exit;
L
Linus Torvalds 已提交
112 113
	}

114 115
	retval = pciehp_configure_device(p_slot);
	if (retval) {
116 117
		ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
			 pci_domain_nr(parent), parent->number);
118 119
		if (retval != -EEXIST)
			goto err_exit;
120
	}
L
Linus Torvalds 已提交
121

122
	pciehp_green_led_on(p_slot);
L
Linus Torvalds 已提交
123
	return 0;
124 125 126

err_exit:
	set_slot_off(ctrl, p_slot);
127
	return retval;
L
Linus Torvalds 已提交
128 129 130
}

/**
R
Randy Dunlap 已提交
131 132
 * remove_board - Turns off slot and LEDs
 * @p_slot: slot where board is being removed
L
Linus Torvalds 已提交
133
 */
134
static int remove_board(struct slot *p_slot)
L
Linus Torvalds 已提交
135
{
136
	int retval;
137
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
138

139 140 141
	retval = pciehp_unconfigure_device(p_slot);
	if (retval)
		return retval;
L
Linus Torvalds 已提交
142

143
	if (POWER_CTRL(ctrl)) {
144 145
		pciehp_power_off_slot(p_slot);

146 147 148 149 150 151
		/*
		 * 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 已提交
152 153
	}

K
Kenji Kaneshige 已提交
154
	/* turn off Green LED */
155
	pciehp_green_led_off(p_slot);
L
Linus Torvalds 已提交
156 157 158
	return 0;
}

K
Kenji Kaneshige 已提交
159 160 161
struct power_work_info {
	struct slot *p_slot;
	struct work_struct work;
162 163 164
	unsigned int req;
#define DISABLE_REQ 0
#define ENABLE_REQ  1
K
Kenji Kaneshige 已提交
165
};
L
Linus Torvalds 已提交
166 167

/**
R
Randy Dunlap 已提交
168 169
 * pciehp_power_thread - handle pushbutton events
 * @work: &struct work_struct describing work to be done
L
Linus Torvalds 已提交
170
 *
R
Randy Dunlap 已提交
171
 * Scheduled procedure to handle blocking stuff for the pushbuttons.
L
Linus Torvalds 已提交
172 173
 * Handles all pending events and exits.
 */
K
Kenji Kaneshige 已提交
174
static void pciehp_power_thread(struct work_struct *work)
L
Linus Torvalds 已提交
175
{
K
Kenji Kaneshige 已提交
176 177 178
	struct power_work_info *info =
		container_of(work, struct power_work_info, work);
	struct slot *p_slot = info->p_slot;
179
	int ret;
K
Kenji Kaneshige 已提交
180

181 182
	switch (info->req) {
	case DISABLE_REQ:
183
		mutex_lock(&p_slot->hotplug_lock);
L
Linus Torvalds 已提交
184
		pciehp_disable_slot(p_slot);
185
		mutex_unlock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
186
		mutex_lock(&p_slot->lock);
L
Linus Torvalds 已提交
187
		p_slot->state = STATIC_STATE;
K
Kenji Kaneshige 已提交
188
		mutex_unlock(&p_slot->lock);
189 190
		break;
	case ENABLE_REQ:
191 192 193 194
		mutex_lock(&p_slot->hotplug_lock);
		ret = pciehp_enable_slot(p_slot);
		mutex_unlock(&p_slot->hotplug_lock);
		if (ret)
K
Kenji Kaneshige 已提交
195
			pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
196
		mutex_lock(&p_slot->lock);
L
Linus Torvalds 已提交
197
		p_slot->state = STATIC_STATE;
198
		mutex_unlock(&p_slot->lock);
K
Kenji Kaneshige 已提交
199 200 201
		break;
	default:
		break;
L
Linus Torvalds 已提交
202 203
	}

K
Kenji Kaneshige 已提交
204
	kfree(info);
L
Linus Torvalds 已提交
205 206
}

207
static void pciehp_queue_power_work(struct slot *p_slot, int req)
L
Linus Torvalds 已提交
208
{
K
Kenji Kaneshige 已提交
209
	struct power_work_info *info;
L
Linus Torvalds 已提交
210

211 212
	p_slot->state = (req == ENABLE_REQ) ? POWERON_STATE : POWEROFF_STATE;

K
Kenji Kaneshige 已提交
213 214
	info = kmalloc(sizeof(*info), GFP_KERNEL);
	if (!info) {
215 216
		ctrl_err(p_slot->ctrl, "no memory to queue %s request\n",
			 (req == ENABLE_REQ) ? "poweron" : "poweroff");
L
Linus Torvalds 已提交
217 218
		return;
	}
K
Kenji Kaneshige 已提交
219 220
	info->p_slot = p_slot;
	INIT_WORK(&info->work, pciehp_power_thread);
221 222 223 224 225 226 227
	info->req = req;
	queue_work(p_slot->wq, &info->work);
}

void pciehp_queue_pushbutton_work(struct work_struct *work)
{
	struct slot *p_slot = container_of(work, struct slot, work.work);
L
Linus Torvalds 已提交
228

K
Kenji Kaneshige 已提交
229 230 231
	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGOFF_STATE:
232
		pciehp_queue_power_work(p_slot, DISABLE_REQ);
K
Kenji Kaneshige 已提交
233 234
		break;
	case BLINKINGON_STATE:
235
		pciehp_queue_power_work(p_slot, ENABLE_REQ);
K
Kenji Kaneshige 已提交
236 237
		break;
	default:
238
		break;
L
Linus Torvalds 已提交
239
	}
K
Kenji Kaneshige 已提交
240
	mutex_unlock(&p_slot->lock);
L
Linus Torvalds 已提交
241 242
}

K
Kenji Kaneshige 已提交
243 244 245 246
/*
 * Note: This function must be called with slot->lock held
 */
static void handle_button_press_event(struct slot *p_slot)
L
Linus Torvalds 已提交
247
{
K
Kenji Kaneshige 已提交
248
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
249 250
	u8 getstatus;

K
Kenji Kaneshige 已提交
251 252
	switch (p_slot->state) {
	case STATIC_STATE:
K
Kenji Kaneshige 已提交
253
		pciehp_get_power_status(p_slot, &getstatus);
K
Kenji Kaneshige 已提交
254 255
		if (getstatus) {
			p_slot->state = BLINKINGOFF_STATE;
256 257
			ctrl_info(ctrl, "PCI slot #%s - powering off due to button press\n",
				  slot_name(p_slot));
K
Kenji Kaneshige 已提交
258 259
		} else {
			p_slot->state = BLINKINGON_STATE;
260 261
			ctrl_info(ctrl, "PCI slot #%s - powering on due to button press\n",
				  slot_name(p_slot));
K
Kenji Kaneshige 已提交
262 263
		}
		/* blink green LED and turn off amber */
264 265
		pciehp_green_led_blink(p_slot);
		pciehp_set_attention_status(p_slot, 0);
266
		queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
K
Kenji Kaneshige 已提交
267 268 269 270 271 272 273 274
		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 已提交
275
		ctrl_info(ctrl, "Button cancel on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
276
		cancel_delayed_work(&p_slot->work);
B
Bjorn Helgaas 已提交
277
		if (p_slot->state == BLINKINGOFF_STATE)
278
			pciehp_green_led_on(p_slot);
B
Bjorn Helgaas 已提交
279
		else
280 281
			pciehp_green_led_off(p_slot);
		pciehp_set_attention_status(p_slot, 0);
282 283
		ctrl_info(ctrl, "PCI slot #%s - action canceled due to button press\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
284 285 286 287 288 289 290 291 292
		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 已提交
293
		ctrl_info(ctrl, "Button ignore on Slot(%s)\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
294 295
		break;
	default:
296
		ctrl_warn(ctrl, "ignoring invalid state %#x\n", p_slot->state);
K
Kenji Kaneshige 已提交
297
		break;
L
Linus Torvalds 已提交
298 299 300
	}
}

K
Kenji Kaneshige 已提交
301 302 303 304 305 306 307
/*
 * Note: This function must be called with slot->lock held
 */
static void handle_surprise_event(struct slot *p_slot)
{
	u8 getstatus;

K
Kenji Kaneshige 已提交
308
	pciehp_get_adapter_status(p_slot, &getstatus);
309 310 311 312
	if (!getstatus)
		pciehp_queue_power_work(p_slot, DISABLE_REQ);
	else
		pciehp_queue_power_work(p_slot, ENABLE_REQ);
K
Kenji Kaneshige 已提交
313 314
}

315 316 317 318 319 320 321 322 323 324 325 326 327
/*
 * Note: This function must be called with slot->lock held
 */
static void handle_link_event(struct slot *p_slot, u32 event)
{
	struct controller *ctrl = p_slot->ctrl;

	switch (p_slot->state) {
	case BLINKINGON_STATE:
	case BLINKINGOFF_STATE:
		cancel_delayed_work(&p_slot->work);
		/* Fall through */
	case STATIC_STATE:
328 329
		pciehp_queue_power_work(p_slot, event == INT_LINK_UP ?
					ENABLE_REQ : DISABLE_REQ);
330 331 332 333 334 335 336 337 338 339
		break;
	case POWERON_STATE:
		if (event == INT_LINK_UP) {
			ctrl_info(ctrl,
				  "Link Up event ignored on slot(%s): already powering on\n",
				  slot_name(p_slot));
		} else {
			ctrl_info(ctrl,
				  "Link Down event queued on slot(%s): currently getting powered on\n",
				  slot_name(p_slot));
340
			pciehp_queue_power_work(p_slot, DISABLE_REQ);
341 342 343 344 345 346 347
		}
		break;
	case POWEROFF_STATE:
		if (event == INT_LINK_UP) {
			ctrl_info(ctrl,
				  "Link Up event queued on slot(%s): currently getting powered off\n",
				  slot_name(p_slot));
348
			pciehp_queue_power_work(p_slot, ENABLE_REQ);
349 350 351 352 353 354 355
		} else {
			ctrl_info(ctrl,
				  "Link Down event ignored on slot(%s): already powering off\n",
				  slot_name(p_slot));
		}
		break;
	default:
356 357
		ctrl_err(ctrl, "ignoring invalid state %#x on slot(%s)\n",
			 p_slot->state, slot_name(p_slot));
358 359 360 361
		break;
	}
}

K
Kenji Kaneshige 已提交
362 363 364 365 366 367 368 369 370 371 372 373
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:
374
		if (!POWER_CTRL(ctrl))
K
Kenji Kaneshige 已提交
375
			break;
376 377
		pciehp_set_attention_status(p_slot, 1);
		pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
378 379
		break;
	case INT_PRESENCE_ON:
380 381 382 383 384 385 386
		handle_surprise_event(p_slot);
		break;
	case INT_PRESENCE_OFF:
		/*
		 * Regardless of surprise capability, we need to
		 * definitely remove a card that has been pulled out!
		 */
K
Kenji Kaneshige 已提交
387 388
		handle_surprise_event(p_slot);
		break;
389 390 391 392
	case INT_LINK_UP:
	case INT_LINK_DOWN:
		handle_link_event(p_slot, info->event_type);
		break;
K
Kenji Kaneshige 已提交
393 394 395 396 397 398 399 400
	default:
		break;
	}
	mutex_unlock(&p_slot->lock);

	kfree(info);
}

401 402 403
/*
 * Note: This function must be called with slot->hotplug_lock held
 */
L
Linus Torvalds 已提交
404 405 406 407
int pciehp_enable_slot(struct slot *p_slot)
{
	u8 getstatus = 0;
	int rc;
408
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
409

410 411
	pciehp_get_adapter_status(p_slot, &getstatus);
	if (!getstatus) {
412
		ctrl_info(ctrl, "No adapter on slot(%s)\n", slot_name(p_slot));
413
		return -ENODEV;
L
Linus Torvalds 已提交
414
	}
415
	if (MRL_SENS(p_slot->ctrl)) {
416 417
		pciehp_get_latch_status(p_slot, &getstatus);
		if (getstatus) {
418 419
			ctrl_info(ctrl, "Latch open on slot(%s)\n",
				  slot_name(p_slot));
420
			return -ENODEV;
L
Linus Torvalds 已提交
421 422
		}
	}
423

424
	if (POWER_CTRL(p_slot->ctrl)) {
425 426
		pciehp_get_power_status(p_slot, &getstatus);
		if (getstatus) {
427 428
			ctrl_info(ctrl, "Already enabled on slot(%s)\n",
				  slot_name(p_slot));
429
			return -EINVAL;
L
Linus Torvalds 已提交
430 431 432
		}
	}

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

435
	rc = board_added(p_slot);
B
Bjorn Helgaas 已提交
436
	if (rc)
K
Kenji Kaneshige 已提交
437
		pciehp_get_latch_status(p_slot, &getstatus);
B
Bjorn Helgaas 已提交
438

L
Linus Torvalds 已提交
439 440 441
	return rc;
}

442 443 444
/*
 * Note: This function must be called with slot->hotplug_lock held
 */
L
Linus Torvalds 已提交
445 446 447
int pciehp_disable_slot(struct slot *p_slot)
{
	u8 getstatus = 0;
448
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
449 450 451 452

	if (!p_slot->ctrl)
		return 1;

453
	if (POWER_CTRL(p_slot->ctrl)) {
454 455
		pciehp_get_power_status(p_slot, &getstatus);
		if (!getstatus) {
456 457
			ctrl_info(ctrl, "Already disabled on slot(%s)\n",
				  slot_name(p_slot));
458
			return -EINVAL;
L
Linus Torvalds 已提交
459 460 461
		}
	}

462
	return remove_board(p_slot);
L
Linus Torvalds 已提交
463 464
}

K
Kenji Kaneshige 已提交
465 466 467
int pciehp_sysfs_enable_slot(struct slot *p_slot)
{
	int retval = -ENODEV;
468
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
469 470 471 472 473 474 475 476

	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);
477
		mutex_lock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
478
		retval = pciehp_enable_slot(p_slot);
479
		mutex_unlock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
480 481 482 483
		mutex_lock(&p_slot->lock);
		p_slot->state = STATIC_STATE;
		break;
	case POWERON_STATE:
484
		ctrl_info(ctrl, "Slot %s is already in powering on state\n",
A
Alex Chiang 已提交
485
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
486 487 488
		break;
	case BLINKINGOFF_STATE:
	case POWEROFF_STATE:
A
Alex Chiang 已提交
489 490
		ctrl_info(ctrl, "Already enabled on slot %s\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
491 492
		break;
	default:
493 494
		ctrl_err(ctrl, "invalid state %#x on slot %s\n",
			 p_slot->state, slot_name(p_slot));
K
Kenji Kaneshige 已提交
495 496 497 498 499 500 501 502 503 504
		break;
	}
	mutex_unlock(&p_slot->lock);

	return retval;
}

int pciehp_sysfs_disable_slot(struct slot *p_slot)
{
	int retval = -ENODEV;
505
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
506 507 508 509 510 511 512 513

	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);
514
		mutex_lock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
515
		retval = pciehp_disable_slot(p_slot);
516
		mutex_unlock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
517 518 519 520
		mutex_lock(&p_slot->lock);
		p_slot->state = STATIC_STATE;
		break;
	case POWEROFF_STATE:
521
		ctrl_info(ctrl, "Slot %s is already in powering off state\n",
A
Alex Chiang 已提交
522
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
523 524 525
		break;
	case BLINKINGON_STATE:
	case POWERON_STATE:
A
Alex Chiang 已提交
526 527
		ctrl_info(ctrl, "Already disabled on slot %s\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
528 529
		break;
	default:
530 531
		ctrl_err(ctrl, "invalid state %#x on slot %s\n",
			 p_slot->state, slot_name(p_slot));
K
Kenji Kaneshige 已提交
532 533 534 535 536 537
		break;
	}
	mutex_unlock(&p_slot->lock);

	return retval;
}