pciehp_ctrl.c 10.3 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"

24
/* The following routines constitute the bulk of the
L
Linus Torvalds 已提交
25 26 27
   hotplug controller logic
 */

R
Ryan Desfosses 已提交
28
static void set_slot_off(struct controller *ctrl, struct slot *pslot)
L
Linus Torvalds 已提交
29 30
{
	/* turn off slot, turn on Amber LED, turn off Green LED if supported*/
31
	if (POWER_CTRL(ctrl)) {
32 33
		pciehp_power_off_slot(pslot);

34 35 36 37 38 39
		/*
		 * 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 已提交
40 41
	}

42 43
	pciehp_green_led_off(pslot);
	pciehp_set_attention_status(pslot, 1);
L
Linus Torvalds 已提交
44 45 46 47
}

/**
 * board_added - Called after a board has been added to the system.
R
Randy Dunlap 已提交
48
 * @p_slot: &slot where board is added
L
Linus Torvalds 已提交
49
 *
R
Randy Dunlap 已提交
50 51
 * Turns power on for the board.
 * Configures board.
L
Linus Torvalds 已提交
52
 */
53
static int board_added(struct slot *p_slot)
L
Linus Torvalds 已提交
54
{
55
	int retval = 0;
56
	struct controller *ctrl = p_slot->ctrl;
57
	struct pci_bus *parent = ctrl->pcie->port->subordinate;
L
Linus Torvalds 已提交
58

59
	if (POWER_CTRL(ctrl)) {
L
Linus Torvalds 已提交
60
		/* Power on slot */
K
Kenji Kaneshige 已提交
61
		retval = pciehp_power_on_slot(p_slot);
62 63
		if (retval)
			return retval;
L
Linus Torvalds 已提交
64
	}
65

66
	pciehp_green_led_blink(p_slot);
L
Linus Torvalds 已提交
67

68
	/* Check link training status */
K
Kenji Kaneshige 已提交
69
	retval = pciehp_check_link_status(ctrl);
70
	if (retval) {
71
		ctrl_err(ctrl, "Failed to check link status\n");
72
		goto err_exit;
L
Linus Torvalds 已提交
73 74 75
	}

	/* Check for a power fault */
76
	if (ctrl->power_fault_detected || pciehp_query_power_fault(p_slot)) {
77
		ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(p_slot));
78
		retval = -EIO;
79
		goto err_exit;
L
Linus Torvalds 已提交
80 81
	}

82 83
	retval = pciehp_configure_device(p_slot);
	if (retval) {
84 85 86
		if (retval != -EEXIST) {
			ctrl_err(ctrl, "Cannot add device at %04x:%02x:00\n",
				 pci_domain_nr(parent), parent->number);
87
			goto err_exit;
88
		}
89
	}
L
Linus Torvalds 已提交
90

91
	pciehp_green_led_on(p_slot);
92
	pciehp_set_attention_status(p_slot, 0);
L
Linus Torvalds 已提交
93
	return 0;
94 95 96

err_exit:
	set_slot_off(ctrl, p_slot);
97
	return retval;
L
Linus Torvalds 已提交
98 99 100
}

/**
R
Randy Dunlap 已提交
101 102
 * remove_board - Turns off slot and LEDs
 * @p_slot: slot where board is being removed
L
Linus Torvalds 已提交
103
 */
104
static void remove_board(struct slot *p_slot)
L
Linus Torvalds 已提交
105
{
106
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
107

108
	pciehp_unconfigure_device(p_slot);
L
Linus Torvalds 已提交
109

110
	if (POWER_CTRL(ctrl)) {
111 112
		pciehp_power_off_slot(p_slot);

113 114 115 116 117 118
		/*
		 * 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 已提交
119 120
	}

K
Kenji Kaneshige 已提交
121
	/* turn off Green LED */
122
	pciehp_green_led_off(p_slot);
L
Linus Torvalds 已提交
123 124
}

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

K
Kenji Kaneshige 已提交
129 130 131
	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGOFF_STATE:
132 133 134 135
		p_slot->state = POWEROFF_STATE;
		mutex_unlock(&p_slot->lock);
		pciehp_disable_slot(p_slot);
		return;
K
Kenji Kaneshige 已提交
136
	case BLINKINGON_STATE:
137 138 139 140
		p_slot->state = POWERON_STATE;
		mutex_unlock(&p_slot->lock);
		pciehp_enable_slot(p_slot);
		return;
K
Kenji Kaneshige 已提交
141
	default:
142
		break;
L
Linus Torvalds 已提交
143
	}
K
Kenji Kaneshige 已提交
144
	mutex_unlock(&p_slot->lock);
L
Linus Torvalds 已提交
145 146
}

147
void pciehp_handle_button_press(struct slot *p_slot)
L
Linus Torvalds 已提交
148
{
K
Kenji Kaneshige 已提交
149
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
150 151
	u8 getstatus;

152
	mutex_lock(&p_slot->lock);
K
Kenji Kaneshige 已提交
153 154
	switch (p_slot->state) {
	case STATIC_STATE:
K
Kenji Kaneshige 已提交
155
		pciehp_get_power_status(p_slot, &getstatus);
K
Kenji Kaneshige 已提交
156 157
		if (getstatus) {
			p_slot->state = BLINKINGOFF_STATE;
158
			ctrl_info(ctrl, "Slot(%s): Powering off due to button press\n",
159
				  slot_name(p_slot));
K
Kenji Kaneshige 已提交
160 161
		} else {
			p_slot->state = BLINKINGON_STATE;
162
			ctrl_info(ctrl, "Slot(%s) Powering on due to button press\n",
163
				  slot_name(p_slot));
K
Kenji Kaneshige 已提交
164 165
		}
		/* blink green LED and turn off amber */
166 167
		pciehp_green_led_blink(p_slot);
		pciehp_set_attention_status(p_slot, 0);
L
Lukas Wunner 已提交
168
		schedule_delayed_work(&p_slot->work, 5 * HZ);
K
Kenji Kaneshige 已提交
169 170 171 172 173 174 175 176
		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
		 */
177
		ctrl_info(ctrl, "Slot(%s): Button cancel\n", slot_name(p_slot));
K
Kenji Kaneshige 已提交
178
		cancel_delayed_work(&p_slot->work);
B
Bjorn Helgaas 已提交
179
		if (p_slot->state == BLINKINGOFF_STATE)
180
			pciehp_green_led_on(p_slot);
B
Bjorn Helgaas 已提交
181
		else
182 183
			pciehp_green_led_off(p_slot);
		pciehp_set_attention_status(p_slot, 0);
184
		ctrl_info(ctrl, "Slot(%s): Action canceled due to button press\n",
185
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
186 187 188 189 190 191 192 193 194
		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
		 */
195 196
		ctrl_info(ctrl, "Slot(%s): Button ignored\n",
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
197 198
		break;
	default:
199 200
		ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
K
Kenji Kaneshige 已提交
201
		break;
L
Linus Torvalds 已提交
202
	}
203
	mutex_unlock(&p_slot->lock);
L
Linus Torvalds 已提交
204 205
}

206
void pciehp_handle_link_change(struct slot *p_slot)
207 208
{
	struct controller *ctrl = p_slot->ctrl;
209 210 211 212
	bool link_active;

	mutex_lock(&p_slot->lock);
	link_active = pciehp_check_link_active(ctrl);
213 214 215 216 217 218 219

	switch (p_slot->state) {
	case BLINKINGON_STATE:
	case BLINKINGOFF_STATE:
		cancel_delayed_work(&p_slot->work);
		/* Fall through */
	case STATIC_STATE:
220 221 222 223 224 225 226 227 228 229 230 231
		if (link_active) {
			p_slot->state = POWERON_STATE;
			mutex_unlock(&p_slot->lock);
			ctrl_info(ctrl, "Slot(%s): Link Up\n", slot_name(p_slot));
			pciehp_enable_slot(p_slot);
		} else {
			p_slot->state = POWEROFF_STATE;
			mutex_unlock(&p_slot->lock);
			ctrl_info(ctrl, "Slot(%s): Link Down\n", slot_name(p_slot));
			pciehp_disable_slot(p_slot);
		}
		return;
232 233
		break;
	case POWERON_STATE:
234
		if (link_active) {
235
			ctrl_info(ctrl, "Slot(%s): Link Up event ignored; already powering on\n",
236 237
				  slot_name(p_slot));
		} else {
238 239
			p_slot->state = POWEROFF_STATE;
			mutex_unlock(&p_slot->lock);
240
			ctrl_info(ctrl, "Slot(%s): Link Down event queued; currently getting powered on\n",
241
				  slot_name(p_slot));
242 243
			pciehp_disable_slot(p_slot);
			return;
244 245 246
		}
		break;
	case POWEROFF_STATE:
247 248 249
		if (link_active) {
			p_slot->state = POWERON_STATE;
			mutex_unlock(&p_slot->lock);
250
			ctrl_info(ctrl, "Slot(%s): Link Up event queued; currently getting powered off\n",
251
				  slot_name(p_slot));
252 253
			pciehp_enable_slot(p_slot);
			return;
254
		} else {
255
			ctrl_info(ctrl, "Slot(%s): Link Down event ignored; already powering off\n",
256 257 258 259
				  slot_name(p_slot));
		}
		break;
	default:
260 261
		ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
262 263
		break;
	}
264
	mutex_unlock(&p_slot->lock);
265 266
}

267
void pciehp_handle_presence_change(struct slot *slot)
K
Kenji Kaneshige 已提交
268
{
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	struct controller *ctrl = slot->ctrl;
	u8 present;

	mutex_lock(&slot->lock);
	pciehp_get_adapter_status(slot, &present);
	ctrl_info(ctrl, "Slot(%s): Card %spresent\n", slot_name(slot),
		  present ? "" : "not ");

	if (present) {
		slot->state = POWERON_STATE;
		mutex_unlock(&slot->lock);
		pciehp_enable_slot(slot);
	} else {
		slot->state = POWEROFF_STATE;
		mutex_unlock(&slot->lock);
		pciehp_disable_slot(slot);
K
Kenji Kaneshige 已提交
285 286 287
	}
}

288 289 290
/*
 * Note: This function must be called with slot->hotplug_lock held
 */
291
static int __pciehp_enable_slot(struct slot *p_slot)
L
Linus Torvalds 已提交
292 293
{
	u8 getstatus = 0;
294
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
295

296 297
	pciehp_get_adapter_status(p_slot, &getstatus);
	if (!getstatus) {
298
		ctrl_info(ctrl, "Slot(%s): No adapter\n", slot_name(p_slot));
299
		return -ENODEV;
L
Linus Torvalds 已提交
300
	}
301
	if (MRL_SENS(p_slot->ctrl)) {
302 303
		pciehp_get_latch_status(p_slot, &getstatus);
		if (getstatus) {
304
			ctrl_info(ctrl, "Slot(%s): Latch open\n",
305
				  slot_name(p_slot));
306
			return -ENODEV;
L
Linus Torvalds 已提交
307 308
		}
	}
309

310
	if (POWER_CTRL(p_slot->ctrl)) {
311 312
		pciehp_get_power_status(p_slot, &getstatus);
		if (getstatus) {
313
			ctrl_info(ctrl, "Slot(%s): Already enabled\n",
314
				  slot_name(p_slot));
315
			return 0;
L
Linus Torvalds 已提交
316 317 318
		}
	}

319
	return board_added(p_slot);
L
Linus Torvalds 已提交
320 321
}

322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
int pciehp_enable_slot(struct slot *slot)
{
	struct controller *ctrl = slot->ctrl;
	int ret;

	mutex_lock(&slot->hotplug_lock);
	ret = __pciehp_enable_slot(slot);
	mutex_unlock(&slot->hotplug_lock);

	if (ret && ATTN_BUTTN(ctrl))
		pciehp_green_led_off(slot); /* may be blinking */

	mutex_lock(&slot->lock);
	slot->state = STATIC_STATE;
	mutex_unlock(&slot->lock);

	return ret;
}

341 342 343
/*
 * Note: This function must be called with slot->hotplug_lock held
 */
344
static int __pciehp_disable_slot(struct slot *p_slot)
L
Linus Torvalds 已提交
345 346
{
	u8 getstatus = 0;
347
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
348

349
	if (POWER_CTRL(p_slot->ctrl)) {
350 351
		pciehp_get_power_status(p_slot, &getstatus);
		if (!getstatus) {
352
			ctrl_info(ctrl, "Slot(%s): Already disabled\n",
353
				  slot_name(p_slot));
354
			return -EINVAL;
L
Linus Torvalds 已提交
355 356 357
		}
	}

358 359
	remove_board(p_slot);
	return 0;
L
Linus Torvalds 已提交
360 361
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
int pciehp_disable_slot(struct slot *slot)
{
	int ret;

	mutex_lock(&slot->hotplug_lock);
	ret = __pciehp_disable_slot(slot);
	mutex_unlock(&slot->hotplug_lock);

	mutex_lock(&slot->lock);
	slot->state = STATIC_STATE;
	mutex_unlock(&slot->lock);

	return ret;
}

K
Kenji Kaneshige 已提交
377 378
int pciehp_sysfs_enable_slot(struct slot *p_slot)
{
379
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
380 381 382 383 384 385 386 387

	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);
388
		return pciehp_enable_slot(p_slot);
K
Kenji Kaneshige 已提交
389
	case POWERON_STATE:
390
		ctrl_info(ctrl, "Slot(%s): Already in powering on state\n",
A
Alex Chiang 已提交
391
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
392 393 394
		break;
	case BLINKINGOFF_STATE:
	case POWEROFF_STATE:
395
		ctrl_info(ctrl, "Slot(%s): Already enabled\n",
A
Alex Chiang 已提交
396
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
397 398
		break;
	default:
399 400
		ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
K
Kenji Kaneshige 已提交
401 402 403 404
		break;
	}
	mutex_unlock(&p_slot->lock);

405
	return -ENODEV;
K
Kenji Kaneshige 已提交
406 407 408 409
}

int pciehp_sysfs_disable_slot(struct slot *p_slot)
{
410
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
411 412 413 414 415 416 417 418

	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);
419
		return pciehp_disable_slot(p_slot);
K
Kenji Kaneshige 已提交
420
	case POWEROFF_STATE:
421
		ctrl_info(ctrl, "Slot(%s): Already in powering off state\n",
A
Alex Chiang 已提交
422
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
423 424 425
		break;
	case BLINKINGON_STATE:
	case POWERON_STATE:
426
		ctrl_info(ctrl, "Slot(%s): Already disabled\n",
A
Alex Chiang 已提交
427
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
428 429
		break;
	default:
430 431
		ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
K
Kenji Kaneshige 已提交
432 433 434 435
		break;
	}
	mutex_unlock(&p_slot->lock);

436
	return -ENODEV;
K
Kenji Kaneshige 已提交
437
}