pciehp_ctrl.c 12.0 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0+
L
Linus Torvalds 已提交
2 3 4 5 6 7 8 9 10 11
/*
 * 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.
 *
12
 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
L
Linus Torvalds 已提交
13 14 15 16 17 18
 *
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/types.h>
19
#include <linux/slab.h>
L
Linus Torvalds 已提交
20 21 22 23
#include <linux/pci.h>
#include "../pci.h"
#include "pciehp.h"

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

26
void pciehp_queue_interrupt_event(struct slot *p_slot, u32 event_type)
27
{
K
Kenji Kaneshige 已提交
28 29 30
	struct event_info *info;

	info = kmalloc(sizeof(*info), GFP_ATOMIC);
31 32 33 34
	if (!info) {
		ctrl_err(p_slot->ctrl, "dropped event %d (ENOMEM)\n", event_type);
		return;
	}
K
Kenji Kaneshige 已提交
35

36
	INIT_WORK(&info->work, interrupt_event_handler);
K
Kenji Kaneshige 已提交
37 38
	info->event_type = event_type;
	info->p_slot = p_slot;
39
	queue_work(p_slot->wq, &info->work);
40 41
}

42
/* The following routines constitute the bulk of the
L
Linus Torvalds 已提交
43 44 45
   hotplug controller logic
 */

R
Ryan Desfosses 已提交
46
static void set_slot_off(struct controller *ctrl, struct slot *pslot)
L
Linus Torvalds 已提交
47 48
{
	/* turn off slot, turn on Amber LED, turn off Green LED if supported*/
49
	if (POWER_CTRL(ctrl)) {
50 51
		pciehp_power_off_slot(pslot);

52 53 54 55 56 57
		/*
		 * 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 已提交
58 59
	}

60 61
	pciehp_green_led_off(pslot);
	pciehp_set_attention_status(pslot, 1);
L
Linus Torvalds 已提交
62 63 64 65
}

/**
 * board_added - Called after a board has been added to the system.
R
Randy Dunlap 已提交
66
 * @p_slot: &slot where board is added
L
Linus Torvalds 已提交
67
 *
R
Randy Dunlap 已提交
68 69
 * Turns power on for the board.
 * Configures board.
L
Linus Torvalds 已提交
70
 */
71
static int board_added(struct slot *p_slot)
L
Linus Torvalds 已提交
72
{
73
	int retval = 0;
74
	struct controller *ctrl = p_slot->ctrl;
75
	struct pci_bus *parent = ctrl->pcie->port->subordinate;
L
Linus Torvalds 已提交
76

77
	if (POWER_CTRL(ctrl)) {
L
Linus Torvalds 已提交
78
		/* Power on slot */
K
Kenji Kaneshige 已提交
79
		retval = pciehp_power_on_slot(p_slot);
80 81
		if (retval)
			return retval;
L
Linus Torvalds 已提交
82
	}
83

84
	pciehp_green_led_blink(p_slot);
L
Linus Torvalds 已提交
85

86
	/* Check link training status */
K
Kenji Kaneshige 已提交
87
	retval = pciehp_check_link_status(ctrl);
88
	if (retval) {
89
		ctrl_err(ctrl, "Failed to check link status\n");
90
		goto err_exit;
L
Linus Torvalds 已提交
91 92 93
	}

	/* Check for a power fault */
94
	if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
95
		ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(p_slot));
96
		retval = -EIO;
97
		goto err_exit;
L
Linus Torvalds 已提交
98 99
	}

100 101
	retval = pciehp_configure_device(p_slot);
	if (retval) {
102 103 104
		if (retval != -EEXIST) {
			ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
				 pci_domain_nr(parent), parent->number);
105
			goto err_exit;
106
		}
107
	}
L
Linus Torvalds 已提交
108

109
	pciehp_green_led_on(p_slot);
110
	pciehp_set_attention_status(p_slot, 0);
L
Linus Torvalds 已提交
111
	return 0;
112 113 114

err_exit:
	set_slot_off(ctrl, p_slot);
115
	return retval;
L
Linus Torvalds 已提交
116 117 118
}

/**
R
Randy Dunlap 已提交
119 120
 * remove_board - Turns off slot and LEDs
 * @p_slot: slot where board is being removed
L
Linus Torvalds 已提交
121
 */
122
static void remove_board(struct slot *p_slot)
L
Linus Torvalds 已提交
123
{
124
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
125

126
	pciehp_unconfigure_device(p_slot);
L
Linus Torvalds 已提交
127

128
	if (POWER_CTRL(ctrl)) {
129 130
		pciehp_power_off_slot(p_slot);

131 132 133 134 135 136
		/*
		 * 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 已提交
137 138
	}

K
Kenji Kaneshige 已提交
139
	/* turn off Green LED */
140
	pciehp_green_led_off(p_slot);
L
Linus Torvalds 已提交
141 142
}

K
Kenji Kaneshige 已提交
143 144 145
struct power_work_info {
	struct slot *p_slot;
	struct work_struct work;
146 147 148
	unsigned int req;
#define DISABLE_REQ 0
#define ENABLE_REQ  1
K
Kenji Kaneshige 已提交
149
};
L
Linus Torvalds 已提交
150 151

/**
R
Randy Dunlap 已提交
152 153
 * pciehp_power_thread - handle pushbutton events
 * @work: &struct work_struct describing work to be done
L
Linus Torvalds 已提交
154
 *
R
Randy Dunlap 已提交
155
 * Scheduled procedure to handle blocking stuff for the pushbuttons.
L
Linus Torvalds 已提交
156 157
 * Handles all pending events and exits.
 */
K
Kenji Kaneshige 已提交
158
static void pciehp_power_thread(struct work_struct *work)
L
Linus Torvalds 已提交
159
{
K
Kenji Kaneshige 已提交
160 161 162
	struct power_work_info *info =
		container_of(work, struct power_work_info, work);
	struct slot *p_slot = info->p_slot;
163
	int ret;
K
Kenji Kaneshige 已提交
164

165 166
	switch (info->req) {
	case DISABLE_REQ:
167
		mutex_lock(&p_slot->hotplug_lock);
L
Linus Torvalds 已提交
168
		pciehp_disable_slot(p_slot);
169
		mutex_unlock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
170
		mutex_lock(&p_slot->lock);
L
Linus Torvalds 已提交
171
		p_slot->state = STATIC_STATE;
K
Kenji Kaneshige 已提交
172
		mutex_unlock(&p_slot->lock);
173 174
		break;
	case ENABLE_REQ:
175 176 177 178
		mutex_lock(&p_slot->hotplug_lock);
		ret = pciehp_enable_slot(p_slot);
		mutex_unlock(&p_slot->hotplug_lock);
		if (ret)
K
Kenji Kaneshige 已提交
179
			pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
180
		mutex_lock(&p_slot->lock);
L
Linus Torvalds 已提交
181
		p_slot->state = STATIC_STATE;
182
		mutex_unlock(&p_slot->lock);
K
Kenji Kaneshige 已提交
183 184 185
		break;
	default:
		break;
L
Linus Torvalds 已提交
186 187
	}

K
Kenji Kaneshige 已提交
188
	kfree(info);
L
Linus Torvalds 已提交
189 190
}

191
static void pciehp_queue_power_work(struct slot *p_slot, int req)
L
Linus Torvalds 已提交
192
{
K
Kenji Kaneshige 已提交
193
	struct power_work_info *info;
L
Linus Torvalds 已提交
194

195 196
	p_slot->state = (req == ENABLE_REQ) ? POWERON_STATE : POWEROFF_STATE;

K
Kenji Kaneshige 已提交
197 198
	info = kmalloc(sizeof(*info), GFP_KERNEL);
	if (!info) {
199 200
		ctrl_err(p_slot->ctrl, "no memory to queue %s request\n",
			 (req == ENABLE_REQ) ? "poweron" : "poweroff");
L
Linus Torvalds 已提交
201 202
		return;
	}
K
Kenji Kaneshige 已提交
203 204
	info->p_slot = p_slot;
	INIT_WORK(&info->work, pciehp_power_thread);
205 206 207 208 209 210 211
	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 已提交
212

K
Kenji Kaneshige 已提交
213 214 215
	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGOFF_STATE:
216
		pciehp_queue_power_work(p_slot, DISABLE_REQ);
K
Kenji Kaneshige 已提交
217 218
		break;
	case BLINKINGON_STATE:
219
		pciehp_queue_power_work(p_slot, ENABLE_REQ);
K
Kenji Kaneshige 已提交
220 221
		break;
	default:
222
		break;
L
Linus Torvalds 已提交
223
	}
K
Kenji Kaneshige 已提交
224
	mutex_unlock(&p_slot->lock);
L
Linus Torvalds 已提交
225 226
}

K
Kenji Kaneshige 已提交
227 228 229 230
/*
 * Note: This function must be called with slot->lock held
 */
static void handle_button_press_event(struct slot *p_slot)
L
Linus Torvalds 已提交
231
{
K
Kenji Kaneshige 已提交
232
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
233 234
	u8 getstatus;

K
Kenji Kaneshige 已提交
235 236
	switch (p_slot->state) {
	case STATIC_STATE:
K
Kenji Kaneshige 已提交
237
		pciehp_get_power_status(p_slot, &getstatus);
K
Kenji Kaneshige 已提交
238 239
		if (getstatus) {
			p_slot->state = BLINKINGOFF_STATE;
240
			ctrl_info(ctrl, "Slot(%s): Powering off due to button press\n",
241
				  slot_name(p_slot));
K
Kenji Kaneshige 已提交
242 243
		} else {
			p_slot->state = BLINKINGON_STATE;
244
			ctrl_info(ctrl, "Slot(%s) Powering on due to button press\n",
245
				  slot_name(p_slot));
K
Kenji Kaneshige 已提交
246 247
		}
		/* blink green LED and turn off amber */
248 249
		pciehp_green_led_blink(p_slot);
		pciehp_set_attention_status(p_slot, 0);
250
		queue_delayed_work(p_slot->wq, &p_slot->work, 5*HZ);
K
Kenji Kaneshige 已提交
251 252 253 254 255 256 257 258
		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
		 */
259
		ctrl_info(ctrl, "Slot(%s): Button cancel\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
260
		cancel_delayed_work(&p_slot->work);
B
Bjorn Helgaas 已提交
261
		if (p_slot->state == BLINKINGOFF_STATE)
262
			pciehp_green_led_on(p_slot);
B
Bjorn Helgaas 已提交
263
		else
264 265
			pciehp_green_led_off(p_slot);
		pciehp_set_attention_status(p_slot, 0);
266
		ctrl_info(ctrl, "Slot(%s): Action canceled due to button press\n",
267
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
268 269 270 271 272 273 274 275 276
		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
		 */
277 278
		ctrl_info(ctrl, "Slot(%s): Button ignored\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
279 280
		break;
	default:
281 282
		ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
K
Kenji Kaneshige 已提交
283
		break;
L
Linus Torvalds 已提交
284 285 286
	}
}

287 288 289 290 291 292 293 294 295 296 297 298 299
/*
 * 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:
300 301
		pciehp_queue_power_work(p_slot, event == INT_LINK_UP ?
					ENABLE_REQ : DISABLE_REQ);
302 303 304
		break;
	case POWERON_STATE:
		if (event == INT_LINK_UP) {
305
			ctrl_info(ctrl, "Slot(%s): Link Up event ignored; already powering on\n",
306 307
				  slot_name(p_slot));
		} else {
308
			ctrl_info(ctrl, "Slot(%s): Link Down event queued; currently getting powered on\n",
309
				  slot_name(p_slot));
310
			pciehp_queue_power_work(p_slot, DISABLE_REQ);
311 312 313 314
		}
		break;
	case POWEROFF_STATE:
		if (event == INT_LINK_UP) {
315
			ctrl_info(ctrl, "Slot(%s): Link Up event queued; currently getting powered off\n",
316
				  slot_name(p_slot));
317
			pciehp_queue_power_work(p_slot, ENABLE_REQ);
318
		} else {
319
			ctrl_info(ctrl, "Slot(%s): Link Down event ignored; already powering off\n",
320 321 322 323
				  slot_name(p_slot));
		}
		break;
	default:
324 325
		ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
326 327 328 329
		break;
	}
}

K
Kenji Kaneshige 已提交
330 331 332 333 334 335 336 337 338 339 340 341
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:
342
		if (!POWER_CTRL(ctrl))
K
Kenji Kaneshige 已提交
343
			break;
344 345
		pciehp_set_attention_status(p_slot, 1);
		pciehp_green_led_off(p_slot);
K
Kenji Kaneshige 已提交
346 347
		break;
	case INT_PRESENCE_ON:
348
		pciehp_queue_power_work(p_slot, ENABLE_REQ);
349 350 351 352 353 354
		break;
	case INT_PRESENCE_OFF:
		/*
		 * Regardless of surprise capability, we need to
		 * definitely remove a card that has been pulled out!
		 */
355
		pciehp_queue_power_work(p_slot, DISABLE_REQ);
K
Kenji Kaneshige 已提交
356
		break;
357 358 359 360
	case INT_LINK_UP:
	case INT_LINK_DOWN:
		handle_link_event(p_slot, info->event_type);
		break;
K
Kenji Kaneshige 已提交
361 362 363 364 365 366 367 368
	default:
		break;
	}
	mutex_unlock(&p_slot->lock);

	kfree(info);
}

369 370 371
/*
 * Note: This function must be called with slot->hotplug_lock held
 */
L
Linus Torvalds 已提交
372 373 374
int pciehp_enable_slot(struct slot *p_slot)
{
	u8 getstatus = 0;
375
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
376

377 378
	pciehp_get_adapter_status(p_slot, &getstatus);
	if (!getstatus) {
379
		ctrl_info(ctrl, "Slot(%s): No adapter\n", slot_name(p_slot));
380
		return -ENODEV;
L
Linus Torvalds 已提交
381
	}
382
	if (MRL_SENS(p_slot->ctrl)) {
383 384
		pciehp_get_latch_status(p_slot, &getstatus);
		if (getstatus) {
385
			ctrl_info(ctrl, "Slot(%s): Latch open\n",
386
				  slot_name(p_slot));
387
			return -ENODEV;
L
Linus Torvalds 已提交
388 389
		}
	}
390

391
	if (POWER_CTRL(p_slot->ctrl)) {
392 393
		pciehp_get_power_status(p_slot, &getstatus);
		if (getstatus) {
394
			ctrl_info(ctrl, "Slot(%s): Already enabled\n",
395
				  slot_name(p_slot));
396
			return 0;
L
Linus Torvalds 已提交
397 398 399
		}
	}

400
	return board_added(p_slot);
L
Linus Torvalds 已提交
401 402
}

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

411
	if (POWER_CTRL(p_slot->ctrl)) {
412 413
		pciehp_get_power_status(p_slot, &getstatus);
		if (!getstatus) {
414
			ctrl_info(ctrl, "Slot(%s): Already disabled\n",
415
				  slot_name(p_slot));
416
			return -EINVAL;
L
Linus Torvalds 已提交
417 418 419
		}
	}

420 421
	remove_board(p_slot);
	return 0;
L
Linus Torvalds 已提交
422 423
}

K
Kenji Kaneshige 已提交
424 425 426
int pciehp_sysfs_enable_slot(struct slot *p_slot)
{
	int retval = -ENODEV;
427
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
428 429 430 431 432 433 434 435

	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);
436
		mutex_lock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
437
		retval = pciehp_enable_slot(p_slot);
438
		mutex_unlock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
439 440 441 442
		mutex_lock(&p_slot->lock);
		p_slot->state = STATIC_STATE;
		break;
	case POWERON_STATE:
443
		ctrl_info(ctrl, "Slot(%s): Already in powering on state\n",
A
Alex Chiang 已提交
444
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
445 446 447
		break;
	case BLINKINGOFF_STATE:
	case POWEROFF_STATE:
448
		ctrl_info(ctrl, "Slot(%s): Already enabled\n",
A
Alex Chiang 已提交
449
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
450 451
		break;
	default:
452 453
		ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
K
Kenji Kaneshige 已提交
454 455 456 457 458 459 460 461 462 463
		break;
	}
	mutex_unlock(&p_slot->lock);

	return retval;
}

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

	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);
473
		mutex_lock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
474
		retval = pciehp_disable_slot(p_slot);
475
		mutex_unlock(&p_slot->hotplug_lock);
K
Kenji Kaneshige 已提交
476 477 478 479
		mutex_lock(&p_slot->lock);
		p_slot->state = STATIC_STATE;
		break;
	case POWEROFF_STATE:
480
		ctrl_info(ctrl, "Slot(%s): Already in powering off state\n",
A
Alex Chiang 已提交
481
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
482 483 484
		break;
	case BLINKINGON_STATE:
	case POWERON_STATE:
485
		ctrl_info(ctrl, "Slot(%s): Already disabled\n",
A
Alex Chiang 已提交
486
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
487 488
		break;
	default:
489 490
		ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
K
Kenji Kaneshige 已提交
491 492 493 494 495 496
		break;
	}
	mutex_unlock(&p_slot->lock);

	return retval;
}