pciehp_ctrl.c 10.1 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 128 129 130 131
void pciehp_request(struct controller *ctrl, int action)
{
	atomic_or(action, &ctrl->pending_events);
	if (!pciehp_poll_mode)
		irq_wake_thread(ctrl->pcie->irq, ctrl);
}

132 133 134
void pciehp_queue_pushbutton_work(struct work_struct *work)
{
	struct slot *p_slot = container_of(work, struct slot, work.work);
135
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
136

K
Kenji Kaneshige 已提交
137 138 139
	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGOFF_STATE:
140 141
		pciehp_request(ctrl, DISABLE_SLOT);
		break;
K
Kenji Kaneshige 已提交
142
	case BLINKINGON_STATE:
143 144
		pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
		break;
K
Kenji Kaneshige 已提交
145
	default:
146
		break;
L
Linus Torvalds 已提交
147
	}
K
Kenji Kaneshige 已提交
148
	mutex_unlock(&p_slot->lock);
L
Linus Torvalds 已提交
149 150
}

151
void pciehp_handle_button_press(struct slot *p_slot)
L
Linus Torvalds 已提交
152
{
K
Kenji Kaneshige 已提交
153
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
154

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

201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
void pciehp_handle_disable_request(struct slot *slot)
{
	struct controller *ctrl = slot->ctrl;

	mutex_lock(&slot->lock);
	switch (slot->state) {
	case BLINKINGON_STATE:
	case BLINKINGOFF_STATE:
		cancel_delayed_work(&slot->work);
	}
	slot->state = POWEROFF_STATE;
	mutex_unlock(&slot->lock);

	ctrl->request_result = pciehp_disable_slot(slot);
}

217
void pciehp_handle_link_change(struct slot *p_slot)
218 219
{
	struct controller *ctrl = p_slot->ctrl;
220 221 222 223
	bool link_active;

	mutex_lock(&p_slot->lock);
	link_active = pciehp_check_link_active(ctrl);
224 225 226 227 228 229

	switch (p_slot->state) {
	case BLINKINGON_STATE:
	case BLINKINGOFF_STATE:
		cancel_delayed_work(&p_slot->work);
		/* Fall through */
230 231
	case ON_STATE:
	case OFF_STATE:
232 233 234 235 236 237 238 239 240 241 242 243
		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;
244 245
		break;
	default:
246 247
		ctrl_err(ctrl, "Slot(%s): Ignoring invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
248 249
		break;
	}
250
	mutex_unlock(&p_slot->lock);
251 252
}

253
void pciehp_handle_presence_change(struct slot *slot)
K
Kenji Kaneshige 已提交
254
{
255 256 257 258
	struct controller *ctrl = slot->ctrl;
	u8 present;

	mutex_lock(&slot->lock);
259 260 261 262 263 264
	switch (slot->state) {
	case BLINKINGON_STATE:
	case BLINKINGOFF_STATE:
		cancel_delayed_work(&slot->work);
	}

265 266 267 268 269 270 271
	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);
272
		ctrl->request_result = pciehp_enable_slot(slot);
273 274 275 276
	} else {
		slot->state = POWEROFF_STATE;
		mutex_unlock(&slot->lock);
		pciehp_disable_slot(slot);
K
Kenji Kaneshige 已提交
277 278 279
	}
}

280 281 282
/*
 * Note: This function must be called with slot->hotplug_lock held
 */
283
static int __pciehp_enable_slot(struct slot *p_slot)
L
Linus Torvalds 已提交
284 285
{
	u8 getstatus = 0;
286
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
287

288 289
	pciehp_get_adapter_status(p_slot, &getstatus);
	if (!getstatus) {
290
		ctrl_info(ctrl, "Slot(%s): No adapter\n", slot_name(p_slot));
291
		return -ENODEV;
L
Linus Torvalds 已提交
292
	}
293
	if (MRL_SENS(p_slot->ctrl)) {
294 295
		pciehp_get_latch_status(p_slot, &getstatus);
		if (getstatus) {
296
			ctrl_info(ctrl, "Slot(%s): Latch open\n",
297
				  slot_name(p_slot));
298
			return -ENODEV;
L
Linus Torvalds 已提交
299 300
		}
	}
301

302
	if (POWER_CTRL(p_slot->ctrl)) {
303 304
		pciehp_get_power_status(p_slot, &getstatus);
		if (getstatus) {
305
			ctrl_info(ctrl, "Slot(%s): Already enabled\n",
306
				  slot_name(p_slot));
307
			return 0;
L
Linus Torvalds 已提交
308 309 310
		}
	}

311
	return board_added(p_slot);
L
Linus Torvalds 已提交
312 313
}

314 315 316 317 318 319 320 321 322 323 324 325 326
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);
327
	slot->state = ret ? OFF_STATE : ON_STATE;
328 329 330 331 332
	mutex_unlock(&slot->lock);

	return ret;
}

333 334 335
/*
 * Note: This function must be called with slot->hotplug_lock held
 */
336
static int __pciehp_disable_slot(struct slot *p_slot)
L
Linus Torvalds 已提交
337 338
{
	u8 getstatus = 0;
339
	struct controller *ctrl = p_slot->ctrl;
L
Linus Torvalds 已提交
340

341
	if (POWER_CTRL(p_slot->ctrl)) {
342 343
		pciehp_get_power_status(p_slot, &getstatus);
		if (!getstatus) {
344
			ctrl_info(ctrl, "Slot(%s): Already disabled\n",
345
				  slot_name(p_slot));
346
			return -EINVAL;
L
Linus Torvalds 已提交
347 348 349
		}
	}

350 351
	remove_board(p_slot);
	return 0;
L
Linus Torvalds 已提交
352 353
}

354 355 356 357 358 359 360 361 362
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);
363
	slot->state = OFF_STATE;
364 365 366 367 368
	mutex_unlock(&slot->lock);

	return ret;
}

K
Kenji Kaneshige 已提交
369 370
int pciehp_sysfs_enable_slot(struct slot *p_slot)
{
371
	struct controller *ctrl = p_slot->ctrl;
K
Kenji Kaneshige 已提交
372 373 374 375

	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGON_STATE:
376
	case OFF_STATE:
K
Kenji Kaneshige 已提交
377
		mutex_unlock(&p_slot->lock);
378 379 380 381 382 383 384 385 386
		/*
		 * The IRQ thread becomes a no-op if the user pulls out the
		 * card before the thread wakes up, so initialize to -ENODEV.
		 */
		ctrl->request_result = -ENODEV;
		pciehp_request(ctrl, PCI_EXP_SLTSTA_PDC);
		wait_event(ctrl->requester,
			   !atomic_read(&ctrl->pending_events));
		return ctrl->request_result;
K
Kenji Kaneshige 已提交
387
	case POWERON_STATE:
388
		ctrl_info(ctrl, "Slot(%s): Already in powering on state\n",
A
Alex Chiang 已提交
389
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
390 391
		break;
	case BLINKINGOFF_STATE:
392
	case ON_STATE:
K
Kenji Kaneshige 已提交
393
	case POWEROFF_STATE:
394
		ctrl_info(ctrl, "Slot(%s): Already enabled\n",
A
Alex Chiang 已提交
395
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
396 397
		break;
	default:
398 399
		ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
K
Kenji Kaneshige 已提交
400 401 402 403
		break;
	}
	mutex_unlock(&p_slot->lock);

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

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

	mutex_lock(&p_slot->lock);
	switch (p_slot->state) {
	case BLINKINGOFF_STATE:
414
	case ON_STATE:
K
Kenji Kaneshige 已提交
415
		mutex_unlock(&p_slot->lock);
416 417 418 419
		pciehp_request(ctrl, DISABLE_SLOT);
		wait_event(ctrl->requester,
			   !atomic_read(&ctrl->pending_events));
		return ctrl->request_result;
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
		break;
	case BLINKINGON_STATE:
425
	case OFF_STATE:
K
Kenji Kaneshige 已提交
426
	case POWERON_STATE:
427
		ctrl_info(ctrl, "Slot(%s): Already disabled\n",
A
Alex Chiang 已提交
428
			  slot_name(p_slot));
K
Kenji Kaneshige 已提交
429 430
		break;
	default:
431 432
		ctrl_err(ctrl, "Slot(%s): Invalid state %#x\n",
			 slot_name(p_slot), p_slot->state);
K
Kenji Kaneshige 已提交
433 434 435 436
		break;
	}
	mutex_unlock(&p_slot->lock);

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