pciehp_hpc.c 24.8 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 PCI Hot Plug 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
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h>
18 19
#include <linux/signal.h>
#include <linux/jiffies.h>
20
#include <linux/kthread.h>
L
Linus Torvalds 已提交
21
#include <linux/pci.h>
22
#include <linux/pm_runtime.h>
A
Andrew Morton 已提交
23
#include <linux/interrupt.h>
24
#include <linux/time.h>
25
#include <linux/slab.h>
A
Andrew Morton 已提交
26

L
Linus Torvalds 已提交
27 28 29
#include "../pci.h"
#include "pciehp.h"

30
static inline struct pci_dev *ctrl_dev(struct controller *ctrl)
31
{
32
	return ctrl->pcie->port;
33
}
L
Linus Torvalds 已提交
34

35 36
static irqreturn_t pciehp_isr(int irq, void *dev_id);
static irqreturn_t pciehp_ist(int irq, void *dev_id);
37
static int pciehp_poll(void *data);
L
Linus Torvalds 已提交
38

K
Kenji Kaneshige 已提交
39 40
static inline int pciehp_request_irq(struct controller *ctrl)
{
41
	int retval, irq = ctrl->pcie->irq;
K
Kenji Kaneshige 已提交
42 43

	if (pciehp_poll_mode) {
44 45 46 47
		ctrl->poll_thread = kthread_run(&pciehp_poll, ctrl,
						"pciehp_poll-%s",
						slot_name(ctrl->slot));
		return PTR_ERR_OR_ZERO(ctrl->poll_thread);
K
Kenji Kaneshige 已提交
48 49 50
	}

	/* Installs the interrupt handler */
51 52
	retval = request_threaded_irq(irq, pciehp_isr, pciehp_ist,
				      IRQF_SHARED, MY_NAME, ctrl);
K
Kenji Kaneshige 已提交
53
	if (retval)
54 55
		ctrl_err(ctrl, "Cannot get irq %d for the hotplug controller\n",
			 irq);
K
Kenji Kaneshige 已提交
56 57 58 59 60 61
	return retval;
}

static inline void pciehp_free_irq(struct controller *ctrl)
{
	if (pciehp_poll_mode)
62
		kthread_stop(ctrl->poll_thread);
K
Kenji Kaneshige 已提交
63
	else
64
		free_irq(ctrl->pcie->irq, ctrl);
K
Kenji Kaneshige 已提交
65 66
}

67
static int pcie_poll_cmd(struct controller *ctrl, int timeout)
68
{
69
	struct pci_dev *pdev = ctrl_dev(ctrl);
70 71
	u16 slot_status;

72
	while (true) {
73
		pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
74 75 76 77 78 79
		if (slot_status == (u16) ~0) {
			ctrl_info(ctrl, "%s: no response from device\n",
				  __func__);
			return 0;
		}

80
		if (slot_status & PCI_EXP_SLTSTA_CC) {
81 82
			pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
						   PCI_EXP_SLTSTA_CC);
83
			return 1;
K
Kenji Kaneshige 已提交
84
		}
85 86 87 88
		if (timeout < 0)
			break;
		msleep(10);
		timeout -= 10;
89 90 91 92
	}
	return 0;	/* timeout */
}

93
static void pcie_wait_cmd(struct controller *ctrl)
94
{
95
	unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
96 97 98
	unsigned long duration = msecs_to_jiffies(msecs);
	unsigned long cmd_timeout = ctrl->cmd_started + duration;
	unsigned long now, timeout;
99 100
	int rc;

101 102 103 104
	/*
	 * If the controller does not generate notifications for command
	 * completions, we never need to wait between writes.
	 */
105
	if (NO_CMD_CMPL(ctrl))
106 107 108 109 110
		return;

	if (!ctrl->cmd_busy)
		return;

111 112 113 114 115 116 117 118 119 120
	/*
	 * Even if the command has already timed out, we want to call
	 * pcie_poll_cmd() so it can clear PCI_EXP_SLTSTA_CC.
	 */
	now = jiffies;
	if (time_before_eq(cmd_timeout, now))
		timeout = 1;
	else
		timeout = cmd_timeout - now;

121 122
	if (ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE &&
	    ctrl->slot_ctrl & PCI_EXP_SLTCTL_CCIE)
123
		rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
124
	else
125
		rc = pcie_poll_cmd(ctrl, jiffies_to_msecs(timeout));
126

127
	if (!rc)
128
		ctrl_info(ctrl, "Timeout on hotplug command %#06x (issued %u msec ago)\n",
129
			  ctrl->slot_ctrl,
130
			  jiffies_to_msecs(jiffies - ctrl->cmd_started));
131 132
}

133 134 135 136 137
#define CC_ERRATUM_MASK		(PCI_EXP_SLTCTL_PCC |	\
				 PCI_EXP_SLTCTL_PIC |	\
				 PCI_EXP_SLTCTL_AIC |	\
				 PCI_EXP_SLTCTL_EIC)

138 139
static void pcie_do_write_cmd(struct controller *ctrl, u16 cmd,
			      u16 mask, bool wait)
L
Linus Torvalds 已提交
140
{
141
	struct pci_dev *pdev = ctrl_dev(ctrl);
142
	u16 slot_ctrl_orig, slot_ctrl;
L
Linus Torvalds 已提交
143

144 145
	mutex_lock(&ctrl->ctrl_lock);

146 147 148
	/*
	 * Always wait for any previous command that might still be in progress
	 */
149 150
	pcie_wait_cmd(ctrl);

151
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
152 153 154 155 156
	if (slot_ctrl == (u16) ~0) {
		ctrl_info(ctrl, "%s: no response from device\n", __func__);
		goto out;
	}

157
	slot_ctrl_orig = slot_ctrl;
158
	slot_ctrl &= ~mask;
K
Kenji Kaneshige 已提交
159
	slot_ctrl |= (cmd & mask);
160
	ctrl->cmd_busy = 1;
161
	smp_mb();
162
	pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, slot_ctrl);
163
	ctrl->cmd_started = jiffies;
164
	ctrl->slot_ctrl = slot_ctrl;
165

166 167 168 169 170 171 172 173 174 175 176
	/*
	 * Controllers with the Intel CF118 and similar errata advertise
	 * Command Completed support, but they only set Command Completed
	 * if we change the "Control" bits for power, power indicator,
	 * attention indicator, or interlock.  If we only change the
	 * "Enable" bits, they never set the Command Completed bit.
	 */
	if (pdev->broken_cmd_compl &&
	    (slot_ctrl_orig & CC_ERRATUM_MASK) == (slot_ctrl & CC_ERRATUM_MASK))
		ctrl->cmd_busy = 0;

177 178 179 180 181 182 183
	/*
	 * Optionally wait for the hardware to be ready for a new command,
	 * indicating completion of the above issued command.
	 */
	if (wait)
		pcie_wait_cmd(ctrl);

184
out:
185
	mutex_unlock(&ctrl->ctrl_lock);
L
Linus Torvalds 已提交
186 187
}

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
/**
 * pcie_write_cmd - Issue controller command
 * @ctrl: controller to which the command is issued
 * @cmd:  command value written to slot control register
 * @mask: bitmask of slot control register to be modified
 */
static void pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
{
	pcie_do_write_cmd(ctrl, cmd, mask, true);
}

/* Same as above without waiting for the hardware to latch */
static void pcie_write_cmd_nowait(struct controller *ctrl, u16 cmd, u16 mask)
{
	pcie_do_write_cmd(ctrl, cmd, mask, false);
}

205
bool pciehp_check_link_active(struct controller *ctrl)
206
{
207
	struct pci_dev *pdev = ctrl_dev(ctrl);
208
	u16 lnk_status;
209
	bool ret;
210

211
	pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
212 213 214 215 216 217
	ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);

	if (ret)
		ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);

	return ret;
218 219
}

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
{
	u32 l;
	int count = 0;
	int delay = 1000, step = 20;
	bool found = false;

	do {
		found = pci_bus_read_dev_vendor_id(bus, devfn, &l, 0);
		count++;

		if (found)
			break;

		msleep(step);
		delay -= step;
	} while (delay > 0);

	if (count > 1 && pciehp_debug)
		printk(KERN_DEBUG "pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
			pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
			PCI_FUNC(devfn), count, step, l);

	return found;
}

K
Kenji Kaneshige 已提交
246
int pciehp_check_link_status(struct controller *ctrl)
L
Linus Torvalds 已提交
247
{
248
	struct pci_dev *pdev = ctrl_dev(ctrl);
249
	bool found;
L
Linus Torvalds 已提交
250 251
	u16 lnk_status;

252 253
	if (!pcie_wait_for_link(pdev, true))
		return -1;
254

255 256
	found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
					PCI_DEVFN(0, 0));
257

258 259 260 261 262
	/* ignore link or presence changes up to this point */
	if (found)
		atomic_and(~(PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC),
			   &ctrl->pending_events);

263
	pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
264
	ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
265 266
	if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
	    !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
267 268
		ctrl_err(ctrl, "link training error: status %#06x\n",
			 lnk_status);
269
		return -1;
L
Linus Torvalds 已提交
270 271
	}

272 273
	pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);

274 275
	if (!found)
		return -1;
276

277
	return 0;
L
Linus Torvalds 已提交
278 279
}

280 281
static int __pciehp_link_set(struct controller *ctrl, bool enable)
{
282
	struct pci_dev *pdev = ctrl_dev(ctrl);
283 284
	u16 lnk_ctrl;

285
	pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &lnk_ctrl);
286 287 288 289 290 291

	if (enable)
		lnk_ctrl &= ~PCI_EXP_LNKCTL_LD;
	else
		lnk_ctrl |= PCI_EXP_LNKCTL_LD;

292
	pcie_capability_write_word(pdev, PCI_EXP_LNKCTL, lnk_ctrl);
293
	ctrl_dbg(ctrl, "%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
294
	return 0;
295 296 297 298 299 300 301
}

static int pciehp_link_enable(struct controller *ctrl)
{
	return __pciehp_link_set(ctrl, true);
}

302 303 304 305 306 307 308
int pciehp_get_raw_indicator_status(struct hotplug_slot *hotplug_slot,
				    u8 *status)
{
	struct slot *slot = hotplug_slot->private;
	struct pci_dev *pdev = ctrl_dev(slot->ctrl);
	u16 slot_ctrl;

309
	pci_config_pm_runtime_get(pdev);
310
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
311
	pci_config_pm_runtime_put(pdev);
312 313 314 315
	*status = (slot_ctrl & (PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC)) >> 6;
	return 0;
}

316
void pciehp_get_attention_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
317
{
318
	struct controller *ctrl = slot->ctrl;
319
	struct pci_dev *pdev = ctrl_dev(ctrl);
L
Linus Torvalds 已提交
320 321
	u16 slot_ctrl;

322
	pci_config_pm_runtime_get(pdev);
323
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
324
	pci_config_pm_runtime_put(pdev);
K
Kenji Kaneshige 已提交
325 326
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
L
Linus Torvalds 已提交
327

328 329
	switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
	case PCI_EXP_SLTCTL_ATTN_IND_ON:
L
Linus Torvalds 已提交
330 331
		*status = 1;	/* On */
		break;
332
	case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
L
Linus Torvalds 已提交
333 334
		*status = 2;	/* Blink */
		break;
335
	case PCI_EXP_SLTCTL_ATTN_IND_OFF:
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343
		*status = 0;	/* Off */
		break;
	default:
		*status = 0xFF;
		break;
	}
}

344
void pciehp_get_power_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
345
{
346
	struct controller *ctrl = slot->ctrl;
347
	struct pci_dev *pdev = ctrl_dev(ctrl);
L
Linus Torvalds 已提交
348 349
	u16 slot_ctrl;

350
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
K
Kenji Kaneshige 已提交
351 352
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
L
Linus Torvalds 已提交
353

354 355 356
	switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
	case PCI_EXP_SLTCTL_PWR_ON:
		*status = 1;	/* On */
L
Linus Torvalds 已提交
357
		break;
358 359
	case PCI_EXP_SLTCTL_PWR_OFF:
		*status = 0;	/* Off */
L
Linus Torvalds 已提交
360 361 362 363 364 365 366
		break;
	default:
		*status = 0xFF;
		break;
	}
}

367
void pciehp_get_latch_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
368
{
369
	struct pci_dev *pdev = ctrl_dev(slot->ctrl);
L
Linus Torvalds 已提交
370 371
	u16 slot_status;

372
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
373
	*status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
L
Linus Torvalds 已提交
374 375
}

376
void pciehp_get_adapter_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
377
{
378
	struct pci_dev *pdev = ctrl_dev(slot->ctrl);
L
Linus Torvalds 已提交
379 380
	u16 slot_status;

381
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
382
	*status = !!(slot_status & PCI_EXP_SLTSTA_PDS);
L
Linus Torvalds 已提交
383 384
}

K
Kenji Kaneshige 已提交
385
int pciehp_query_power_fault(struct slot *slot)
L
Linus Torvalds 已提交
386
{
387
	struct pci_dev *pdev = ctrl_dev(slot->ctrl);
L
Linus Torvalds 已提交
388 389
	u16 slot_status;

390
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
391
	return !!(slot_status & PCI_EXP_SLTSTA_PFD);
L
Linus Torvalds 已提交
392 393
}

394 395 396 397 398
int pciehp_set_raw_indicator_status(struct hotplug_slot *hotplug_slot,
				    u8 status)
{
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;
399
	struct pci_dev *pdev = ctrl_dev(ctrl);
400

401
	pci_config_pm_runtime_get(pdev);
402 403
	pcie_write_cmd_nowait(ctrl, status << 6,
			      PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC);
404
	pci_config_pm_runtime_put(pdev);
405 406 407
	return 0;
}

408
void pciehp_set_attention_status(struct slot *slot, u8 value)
L
Linus Torvalds 已提交
409
{
410
	struct controller *ctrl = slot->ctrl;
411
	u16 slot_cmd;
L
Linus Torvalds 已提交
412

413 414 415
	if (!ATTN_LED(ctrl))
		return;

L
Linus Torvalds 已提交
416
	switch (value) {
R
Ryan Desfosses 已提交
417
	case 0:		/* turn off */
418
		slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_OFF;
419 420
		break;
	case 1:		/* turn on */
421
		slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_ON;
422 423
		break;
	case 2:		/* turn blink */
424
		slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_BLINK;
425 426
		break;
	default:
427
		return;
L
Linus Torvalds 已提交
428
	}
429
	pcie_write_cmd_nowait(ctrl, slot_cmd, PCI_EXP_SLTCTL_AIC);
K
Kenji Kaneshige 已提交
430 431
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_cmd);
L
Linus Torvalds 已提交
432 433
}

K
Kenji Kaneshige 已提交
434
void pciehp_green_led_on(struct slot *slot)
L
Linus Torvalds 已提交
435
{
436
	struct controller *ctrl = slot->ctrl;
437

438 439 440
	if (!PWR_LED(ctrl))
		return;

441 442
	pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON,
			      PCI_EXP_SLTCTL_PIC);
K
Kenji Kaneshige 已提交
443
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
444 445
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_IND_ON);
L
Linus Torvalds 已提交
446 447
}

K
Kenji Kaneshige 已提交
448
void pciehp_green_led_off(struct slot *slot)
L
Linus Torvalds 已提交
449
{
450
	struct controller *ctrl = slot->ctrl;
L
Linus Torvalds 已提交
451

452 453 454
	if (!PWR_LED(ctrl))
		return;

455 456
	pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
			      PCI_EXP_SLTCTL_PIC);
K
Kenji Kaneshige 已提交
457
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
458 459
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_IND_OFF);
L
Linus Torvalds 已提交
460 461
}

K
Kenji Kaneshige 已提交
462
void pciehp_green_led_blink(struct slot *slot)
L
Linus Torvalds 已提交
463
{
464
	struct controller *ctrl = slot->ctrl;
465

466 467 468
	if (!PWR_LED(ctrl))
		return;

469 470
	pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK,
			      PCI_EXP_SLTCTL_PIC);
K
Kenji Kaneshige 已提交
471
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
472 473
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_IND_BLINK);
L
Linus Torvalds 已提交
474 475
}

R
Ryan Desfosses 已提交
476
int pciehp_power_on_slot(struct slot *slot)
L
Linus Torvalds 已提交
477
{
478
	struct controller *ctrl = slot->ctrl;
479
	struct pci_dev *pdev = ctrl_dev(ctrl);
480
	u16 slot_status;
481
	int retval;
L
Linus Torvalds 已提交
482

483
	/* Clear power-fault bit from previous power failures */
484
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
485 486 487
	if (slot_status & PCI_EXP_SLTSTA_PFD)
		pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
					   PCI_EXP_SLTSTA_PFD);
488
	ctrl->power_fault_detected = 0;
L
Linus Torvalds 已提交
489

490
	pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
K
Kenji Kaneshige 已提交
491
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
492 493
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_ON);
L
Linus Torvalds 已提交
494

495 496 497 498
	retval = pciehp_link_enable(ctrl);
	if (retval)
		ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);

L
Linus Torvalds 已提交
499 500 501
	return retval;
}

R
Ryan Desfosses 已提交
502
void pciehp_power_off_slot(struct slot *slot)
L
Linus Torvalds 已提交
503
{
504
	struct controller *ctrl = slot->ctrl;
505

506
	pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_OFF, PCI_EXP_SLTCTL_PCC);
K
Kenji Kaneshige 已提交
507
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
508 509
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_OFF);
L
Linus Torvalds 已提交
510 511
}

512
static irqreturn_t pciehp_isr(int irq, void *dev_id)
L
Linus Torvalds 已提交
513
{
514
	struct controller *ctrl = (struct controller *)dev_id;
515
	struct pci_dev *pdev = ctrl_dev(ctrl);
516
	struct device *parent = pdev->dev.parent;
517
	u16 status, events;
L
Linus Torvalds 已提交
518

519 520 521
	/*
	 * Interrupts only occur in D3hot or shallower (PCIe r4.0, sec 6.7.3.4).
	 */
522 523 524
	if (pdev->current_state == PCI_D3cold)
		return IRQ_NONE;

525 526 527 528 529 530 531 532 533 534 535 536 537 538 539
	/*
	 * Keep the port accessible by holding a runtime PM ref on its parent.
	 * Defer resume of the parent to the IRQ thread if it's suspended.
	 * Mask the interrupt until then.
	 */
	if (parent) {
		pm_runtime_get_noresume(parent);
		if (!pm_runtime_active(parent)) {
			pm_runtime_put(parent);
			disable_irq_nosync(irq);
			atomic_or(RERUN_ISR, &ctrl->pending_events);
			return IRQ_WAKE_THREAD;
		}
	}

540 541 542
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &status);
	if (status == (u16) ~0) {
		ctrl_info(ctrl, "%s: no response from device\n", __func__);
543 544
		if (parent)
			pm_runtime_put(parent);
545 546 547
		return IRQ_NONE;
	}

548
	/*
549 550
	 * Slot Status contains plain status bits as well as event
	 * notification bits; right now we only want the event bits.
551
	 */
552
	events = status & (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
553 554
			   PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_CC |
			   PCI_EXP_SLTSTA_DLLSC);
555 556 557 558 559 560 561 562

	/*
	 * If we've already reported a power fault, don't report it again
	 * until we've done something to handle it.
	 */
	if (ctrl->power_fault_detected)
		events &= ~PCI_EXP_SLTSTA_PFD;

563 564 565
	if (!events) {
		if (parent)
			pm_runtime_put(parent);
566
		return IRQ_NONE;
567
	}
568

569
	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, events);
570
	ctrl_dbg(ctrl, "pending interrupts %#06x from Slot Status\n", events);
571 572
	if (parent)
		pm_runtime_put(parent);
573

574 575 576 577
	/*
	 * Command Completed notifications are not deferred to the
	 * IRQ thread because it may be waiting for their arrival.
	 */
578
	if (events & PCI_EXP_SLTSTA_CC) {
579
		ctrl->cmd_busy = 0;
580
		smp_mb();
581
		wake_up(&ctrl->queue);
582 583 584 585 586

		if (events == PCI_EXP_SLTSTA_CC)
			return IRQ_HANDLED;

		events &= ~PCI_EXP_SLTSTA_CC;
L
Linus Torvalds 已提交
587 588
	}

589 590 591
	if (pdev->ignore_hotplug) {
		ctrl_dbg(ctrl, "ignoring hotplug event %#06x\n", events);
		return IRQ_HANDLED;
592 593
	}

594 595 596 597 598 599 600 601
	/* Save pending events for consumption by IRQ thread. */
	atomic_or(events, &ctrl->pending_events);
	return IRQ_WAKE_THREAD;
}

static irqreturn_t pciehp_ist(int irq, void *dev_id)
{
	struct controller *ctrl = (struct controller *)dev_id;
602
	struct pci_dev *pdev = ctrl_dev(ctrl);
603
	struct slot *slot = ctrl->slot;
604
	irqreturn_t ret;
605 606
	u32 events;

607
	ctrl->ist_running = true;
608 609 610 611 612 613 614 615 616 617 618 619
	pci_config_pm_runtime_get(pdev);

	/* rerun pciehp_isr() if the port was inaccessible on interrupt */
	if (atomic_fetch_and(~RERUN_ISR, &ctrl->pending_events) & RERUN_ISR) {
		ret = pciehp_isr(irq, dev_id);
		enable_irq(irq);
		if (ret != IRQ_WAKE_THREAD) {
			pci_config_pm_runtime_put(pdev);
			return ret;
		}
	}

620 621
	synchronize_hardirq(irq);
	events = atomic_xchg(&ctrl->pending_events, 0);
622 623
	if (!events) {
		pci_config_pm_runtime_put(pdev);
624
		return IRQ_NONE;
625
	}
626

627
	/* Check Attention Button Pressed */
628
	if (events & PCI_EXP_SLTSTA_ABP) {
629
		ctrl_info(ctrl, "Slot(%s): Attention button pressed\n",
630
			  slot_name(slot));
631
		pciehp_handle_button_press(slot);
632
	}
633

634 635 636 637 638 639 640 641
	/* Check Power Fault Detected */
	if ((events & PCI_EXP_SLTSTA_PFD) && !ctrl->power_fault_detected) {
		ctrl->power_fault_detected = 1;
		ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(slot));
		pciehp_set_attention_status(slot, 1);
		pciehp_green_led_off(slot);
	}

642
	/*
643 644
	 * Disable requests have higher priority than Presence Detect Changed
	 * or Data Link Layer State Changed events.
645
	 */
646
	down_read(&ctrl->reset_lock);
647 648
	if (events & DISABLE_SLOT)
		pciehp_handle_disable_request(slot);
649 650
	else if (events & (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC))
		pciehp_handle_presence_or_link_change(slot, events);
651
	up_read(&ctrl->reset_lock);
652

653
	pci_config_pm_runtime_put(pdev);
654
	ctrl->ist_running = false;
655
	wake_up(&ctrl->requester);
L
Linus Torvalds 已提交
656 657 658
	return IRQ_HANDLED;
}

659 660 661 662 663 664 665
static int pciehp_poll(void *data)
{
	struct controller *ctrl = data;

	schedule_timeout_idle(10 * HZ); /* start with 10 sec delay */

	while (!kthread_should_stop()) {
666 667 668
		/* poll for interrupt events or user requests */
		while (pciehp_isr(IRQ_NOTCONNECTED, ctrl) == IRQ_WAKE_THREAD ||
		       atomic_read(&ctrl->pending_events))
669 670 671 672 673 674 675 676 677 678 679
			pciehp_ist(IRQ_NOTCONNECTED, ctrl);

		if (pciehp_poll_time <= 0 || pciehp_poll_time > 60)
			pciehp_poll_time = 2; /* clamp to sane value */

		schedule_timeout_idle(pciehp_poll_time * HZ);
	}

	return 0;
}

680
static void pcie_enable_notification(struct controller *ctrl)
M
Mark Lord 已提交
681
{
682
	u16 cmd, mask;
L
Linus Torvalds 已提交
683

684 685 686 687 688 689 690 691 692 693
	/*
	 * TBD: Power fault detected software notification support.
	 *
	 * Power fault detected software notification is not enabled
	 * now, because it caused power fault detected interrupt storm
	 * on some machines. On those machines, power fault detected
	 * bit in the slot status register was set again immediately
	 * when it is cleared in the interrupt service routine, and
	 * next power fault detected interrupt was notified again.
	 */
694 695 696 697 698 699 700

	/*
	 * Always enable link events: thus link-up and link-down shall
	 * always be treated as hotplug and unplug respectively. Enable
	 * presence detect only if Attention Button is not present.
	 */
	cmd = PCI_EXP_SLTCTL_DLLSCE;
701
	if (ATTN_BUTTN(ctrl))
702
		cmd |= PCI_EXP_SLTCTL_ABPE;
703 704
	else
		cmd |= PCI_EXP_SLTCTL_PDCE;
705
	if (!pciehp_poll_mode)
706
		cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
707

708
	mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
709
		PCI_EXP_SLTCTL_PFDE |
710 711
		PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
		PCI_EXP_SLTCTL_DLLSCE);
712

713
	pcie_write_cmd_nowait(ctrl, cmd, mask);
714 715
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
716 717 718 719 720
}

static void pcie_disable_notification(struct controller *ctrl)
{
	u16 mask;
721

722 723
	mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
		PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
724 725
		PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
		PCI_EXP_SLTCTL_DLLSCE);
726
	pcie_write_cmd(ctrl, 0, mask);
727 728
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
729 730
}

731 732 733 734 735 736
void pcie_clear_hotplug_events(struct controller *ctrl)
{
	pcie_capability_write_word(ctrl_dev(ctrl), PCI_EXP_SLTSTA,
				   PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
}

737 738
/*
 * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
739 740 741 742
 * bus reset of the bridge, but at the same time we want to ensure that it is
 * not seen as a hot-unplug, followed by the hot-plug of the device. Thus,
 * disable link state notification and presence detection change notification
 * momentarily, if we see that they could interfere. Also, clear any spurious
743 744 745 746 747
 * events after.
 */
int pciehp_reset_slot(struct slot *slot, int probe)
{
	struct controller *ctrl = slot->ctrl;
748
	struct pci_dev *pdev = ctrl_dev(ctrl);
749
	u16 stat_mask = 0, ctrl_mask = 0;
750
	int rc;
751 752 753 754

	if (probe)
		return 0;

755 756
	down_write(&ctrl->reset_lock);

757
	if (!ATTN_BUTTN(ctrl)) {
758 759
		ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
		stat_mask |= PCI_EXP_SLTSTA_PDC;
760
	}
761 762 763 764
	ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
	stat_mask |= PCI_EXP_SLTSTA_DLLSC;

	pcie_write_cmd(ctrl, 0, ctrl_mask);
765 766
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
767

768
	rc = pci_bridge_secondary_bus_reset(ctrl->pcie->port);
769

770
	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
771
	pcie_write_cmd_nowait(ctrl, ctrl_mask, ctrl_mask);
772 773
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, ctrl_mask);
774 775

	up_write(&ctrl->reset_lock);
776
	return rc;
777 778
}

779
int pcie_init_notification(struct controller *ctrl)
780 781 782
{
	if (pciehp_request_irq(ctrl))
		return -1;
783
	pcie_enable_notification(ctrl);
784
	ctrl->notification_enabled = 1;
785 786 787
	return 0;
}

788
void pcie_shutdown_notification(struct controller *ctrl)
789
{
790 791 792 793 794
	if (ctrl->notification_enabled) {
		pcie_disable_notification(ctrl);
		pciehp_free_irq(ctrl);
		ctrl->notification_enabled = 0;
	}
795 796 797 798
}

static int pcie_init_slot(struct controller *ctrl)
{
799
	struct pci_bus *subordinate = ctrl_dev(ctrl)->subordinate;
800 801 802 803 804 805
	struct slot *slot;

	slot = kzalloc(sizeof(*slot), GFP_KERNEL);
	if (!slot)
		return -ENOMEM;

806 807 808 809
	down_read(&pci_bus_sem);
	slot->state = list_empty(&subordinate->devices) ? OFF_STATE : ON_STATE;
	up_read(&pci_bus_sem);

810 811 812
	slot->ctrl = ctrl;
	mutex_init(&slot->lock);
	INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
813
	ctrl->slot = slot;
L
Linus Torvalds 已提交
814 815
	return 0;
}
816

817 818
static void pcie_cleanup_slot(struct controller *ctrl)
{
819
	struct slot *slot = ctrl->slot;
820

L
Lukas Wunner 已提交
821
	cancel_delayed_work_sync(&slot->work);
822 823 824
	kfree(slot);
}

K
Kenji Kaneshige 已提交
825
static inline void dbg_ctrl(struct controller *ctrl)
826
{
827
	struct pci_dev *pdev = ctrl->pcie->port;
828
	u16 reg16;
829

K
Kenji Kaneshige 已提交
830 831
	if (!pciehp_debug)
		return;
832

833
	ctrl_info(ctrl, "Slot Capabilities      : 0x%08x\n", ctrl->slot_cap);
834
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &reg16);
835
	ctrl_info(ctrl, "Slot Status            : 0x%04x\n", reg16);
836
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &reg16);
837
	ctrl_info(ctrl, "Slot Control           : 0x%04x\n", reg16);
K
Kenji Kaneshige 已提交
838
}
839

R
Ryan Desfosses 已提交
840
#define FLAG(x, y)	(((x) & (y)) ? '+' : '-')
841

842
struct controller *pcie_init(struct pcie_device *dev)
K
Kenji Kaneshige 已提交
843
{
844
	struct controller *ctrl;
845
	u8 occupied, poweron;
846
	u32 slot_cap, slot_cap2, link_cap;
K
Kenji Kaneshige 已提交
847
	struct pci_dev *pdev = dev->port;
848

849
	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
850
	if (!ctrl)
851
		goto abort;
852

853
	ctrl->pcie = dev;
854
	pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
855 856 857 858

	if (pdev->hotplug_user_indicators)
		slot_cap &= ~(PCI_EXP_SLTCAP_AIP | PCI_EXP_SLTCAP_PIP);

859 860 861 862 863 864 865
	/*
	 * We assume no Thunderbolt controllers support Command Complete events,
	 * but some controllers falsely claim they do.
	 */
	if (pdev->is_thunderbolt)
		slot_cap |= PCI_EXP_SLTCAP_NCCS;

K
Kenji Kaneshige 已提交
866
	ctrl->slot_cap = slot_cap;
867
	mutex_init(&ctrl->ctrl_lock);
868
	init_rwsem(&ctrl->reset_lock);
869
	init_waitqueue_head(&ctrl->requester);
870
	init_waitqueue_head(&ctrl->queue);
K
Kenji Kaneshige 已提交
871
	dbg_ctrl(ctrl);
872

873 874 875 876 877 878 879
	pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP2, &slot_cap2);
	if (slot_cap2 & PCI_EXP_SLTCAP2_IBPD) {
		pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_IBPD_DISABLE,
				      PCI_EXP_SLTCTL_IBPD_DISABLE);
		ctrl->inband_presence_disabled = 1;
	}

R
Ryan Desfosses 已提交
880 881
	/* Check if Data Link Layer Link Active Reporting is implemented */
	pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
882

883
	/* Clear all remaining event bits in Slot Status register. */
884 885
	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
		PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
886
		PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_CC |
887
		PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC);
888

889
	ctrl_info(ctrl, "Slot #%d AttnBtn%c PwrCtrl%c MRL%c AttnInd%c PwrInd%c HotPlug%c Surprise%c Interlock%c NoCompl%c IbPresDis%c LLActRep%c%s\n",
890 891 892 893
		(slot_cap & PCI_EXP_SLTCAP_PSN) >> 19,
		FLAG(slot_cap, PCI_EXP_SLTCAP_ABP),
		FLAG(slot_cap, PCI_EXP_SLTCAP_PCP),
		FLAG(slot_cap, PCI_EXP_SLTCAP_MRLSP),
894 895 896 897
		FLAG(slot_cap, PCI_EXP_SLTCAP_AIP),
		FLAG(slot_cap, PCI_EXP_SLTCAP_PIP),
		FLAG(slot_cap, PCI_EXP_SLTCAP_HPC),
		FLAG(slot_cap, PCI_EXP_SLTCAP_HPS),
898 899
		FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
		FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
900
		FLAG(slot_cap2, PCI_EXP_SLTCAP2_IBPD),
901 902
		FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC),
		pdev->broken_cmd_compl ? " (with Cmd Compl erratum)" : "");
903 904 905

	if (pcie_init_slot(ctrl))
		goto abort_ctrl;
K
Kenji Kaneshige 已提交
906

907 908 909 910 911 912 913 914 915 916 917 918 919
	/*
	 * If empty slot's power status is on, turn power off.  The IRQ isn't
	 * requested yet, so avoid triggering a notification with this command.
	 */
	if (POWER_CTRL(ctrl)) {
		pciehp_get_adapter_status(ctrl->slot, &occupied);
		pciehp_get_power_status(ctrl->slot, &poweron);
		if (!occupied && poweron) {
			pcie_disable_notification(ctrl);
			pciehp_power_off_slot(ctrl->slot);
		}
	}

920 921 922 923
	return ctrl;

abort_ctrl:
	kfree(ctrl);
924
abort:
925 926 927
	return NULL;
}

K
Kenji Kaneshige 已提交
928
void pciehp_release_ctrl(struct controller *ctrl)
929 930 931
{
	pcie_cleanup_slot(ctrl);
	kfree(ctrl);
932
}
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950

static void quirk_cmd_compl(struct pci_dev *pdev)
{
	u32 slot_cap;

	if (pci_is_pcie(pdev)) {
		pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
		if (slot_cap & PCI_EXP_SLTCAP_HPC &&
		    !(slot_cap & PCI_EXP_SLTCAP_NCCS))
			pdev->broken_cmd_compl = 1;
	}
}
DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
			      PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0400,
			      PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0401,
			      PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);