pciehp_hpc.c 23.9 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 20
#include <linux/signal.h>
#include <linux/jiffies.h>
#include <linux/timer.h>
L
Linus Torvalds 已提交
21
#include <linux/pci.h>
A
Andrew Morton 已提交
22
#include <linux/interrupt.h>
23
#include <linux/time.h>
24
#include <linux/slab.h>
A
Andrew Morton 已提交
25

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

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

34 35
static irqreturn_t pcie_isr(int irq, void *dev_id);
static void start_int_poll_timer(struct controller *ctrl, int sec);
L
Linus Torvalds 已提交
36 37

/* This is the interrupt polling timeout function. */
38
static void int_poll_timeout(struct timer_list *t)
L
Linus Torvalds 已提交
39
{
40
	struct controller *ctrl = from_timer(ctrl, t, poll_timer);
L
Linus Torvalds 已提交
41 42

	/* Poll for interrupt events.  regs == NULL => polling */
43
	pcie_isr(0, ctrl);
L
Linus Torvalds 已提交
44 45

	if (!pciehp_poll_time)
46
		pciehp_poll_time = 2; /* default polling interval is 2 sec */
L
Linus Torvalds 已提交
47

48
	start_int_poll_timer(ctrl, pciehp_poll_time);
L
Linus Torvalds 已提交
49 50 51
}

/* This function starts the interrupt polling timer. */
52
static void start_int_poll_timer(struct controller *ctrl, int sec)
L
Linus Torvalds 已提交
53
{
54 55
	/* Clamp to sane value */
	if ((sec <= 0) || (sec > 60))
56
		sec = 2;
57 58 59

	ctrl->poll_timer.expires = jiffies + sec * HZ;
	add_timer(&ctrl->poll_timer);
L
Linus Torvalds 已提交
60 61
}

K
Kenji Kaneshige 已提交
62 63
static inline int pciehp_request_irq(struct controller *ctrl)
{
64
	int retval, irq = ctrl->pcie->irq;
K
Kenji Kaneshige 已提交
65 66 67

	/* Install interrupt polling timer. Start with 10 sec delay */
	if (pciehp_poll_mode) {
68
		timer_setup(&ctrl->poll_timer, int_poll_timeout, 0);
K
Kenji Kaneshige 已提交
69 70 71 72 73 74 75
		start_int_poll_timer(ctrl, 10);
		return 0;
	}

	/* Installs the interrupt handler */
	retval = request_irq(irq, pcie_isr, IRQF_SHARED, MY_NAME, ctrl);
	if (retval)
76 77
		ctrl_err(ctrl, "Cannot get irq %d for the hotplug controller\n",
			 irq);
K
Kenji Kaneshige 已提交
78 79 80 81 82 83 84 85
	return retval;
}

static inline void pciehp_free_irq(struct controller *ctrl)
{
	if (pciehp_poll_mode)
		del_timer_sync(&ctrl->poll_timer);
	else
86
		free_irq(ctrl->pcie->irq, ctrl);
K
Kenji Kaneshige 已提交
87 88
}

89
static int pcie_poll_cmd(struct controller *ctrl, int timeout)
90
{
91
	struct pci_dev *pdev = ctrl_dev(ctrl);
92 93
	u16 slot_status;

94
	while (true) {
95
		pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
96 97 98 99 100 101
		if (slot_status == (u16) ~0) {
			ctrl_info(ctrl, "%s: no response from device\n",
				  __func__);
			return 0;
		}

102
		if (slot_status & PCI_EXP_SLTSTA_CC) {
103 104
			pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
						   PCI_EXP_SLTSTA_CC);
105
			return 1;
K
Kenji Kaneshige 已提交
106
		}
107 108 109 110
		if (timeout < 0)
			break;
		msleep(10);
		timeout -= 10;
111 112 113 114
	}
	return 0;	/* timeout */
}

115
static void pcie_wait_cmd(struct controller *ctrl)
116
{
117
	unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
118 119 120
	unsigned long duration = msecs_to_jiffies(msecs);
	unsigned long cmd_timeout = ctrl->cmd_started + duration;
	unsigned long now, timeout;
121 122
	int rc;

123 124 125 126
	/*
	 * If the controller does not generate notifications for command
	 * completions, we never need to wait between writes.
	 */
127
	if (NO_CMD_CMPL(ctrl))
128 129 130 131 132
		return;

	if (!ctrl->cmd_busy)
		return;

133 134 135 136 137 138 139 140 141 142
	/*
	 * 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;

143 144
	if (ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE &&
	    ctrl->slot_ctrl & PCI_EXP_SLTCTL_CCIE)
145
		rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
146
	else
147
		rc = pcie_poll_cmd(ctrl, jiffies_to_msecs(timeout));
148

149
	if (!rc)
150
		ctrl_info(ctrl, "Timeout on hotplug command %#06x (issued %u msec ago)\n",
151
			  ctrl->slot_ctrl,
152
			  jiffies_to_msecs(jiffies - ctrl->cmd_started));
153 154
}

155 156 157 158 159
#define CC_ERRATUM_MASK		(PCI_EXP_SLTCTL_PCC |	\
				 PCI_EXP_SLTCTL_PIC |	\
				 PCI_EXP_SLTCTL_AIC |	\
				 PCI_EXP_SLTCTL_EIC)

160 161
static void pcie_do_write_cmd(struct controller *ctrl, u16 cmd,
			      u16 mask, bool wait)
L
Linus Torvalds 已提交
162
{
163
	struct pci_dev *pdev = ctrl_dev(ctrl);
164
	u16 slot_ctrl_orig, slot_ctrl;
L
Linus Torvalds 已提交
165

166 167
	mutex_lock(&ctrl->ctrl_lock);

168 169 170
	/*
	 * Always wait for any previous command that might still be in progress
	 */
171 172
	pcie_wait_cmd(ctrl);

173
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
174 175 176 177 178
	if (slot_ctrl == (u16) ~0) {
		ctrl_info(ctrl, "%s: no response from device\n", __func__);
		goto out;
	}

179
	slot_ctrl_orig = slot_ctrl;
180
	slot_ctrl &= ~mask;
K
Kenji Kaneshige 已提交
181
	slot_ctrl |= (cmd & mask);
182
	ctrl->cmd_busy = 1;
183
	smp_mb();
184
	pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, slot_ctrl);
185
	ctrl->cmd_started = jiffies;
186
	ctrl->slot_ctrl = slot_ctrl;
187

188 189 190 191 192 193 194 195 196 197 198
	/*
	 * 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;

199 200 201 202 203 204 205
	/*
	 * 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);

206
out:
207
	mutex_unlock(&ctrl->ctrl_lock);
L
Linus Torvalds 已提交
208 209
}

210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
/**
 * 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);
}

227
bool pciehp_check_link_active(struct controller *ctrl)
228
{
229
	struct pci_dev *pdev = ctrl_dev(ctrl);
230
	u16 lnk_status;
231
	bool ret;
232

233
	pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
234 235 236 237 238 239
	ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);

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

	return ret;
240 241
}

242 243
static void pcie_wait_link_active(struct controller *ctrl)
{
244 245 246
	struct pci_dev *pdev = ctrl_dev(ctrl);

	pcie_wait_for_link(pdev, true);
247 248
}

249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
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 已提交
275
int pciehp_check_link_status(struct controller *ctrl)
L
Linus Torvalds 已提交
276
{
277
	struct pci_dev *pdev = ctrl_dev(ctrl);
278
	bool found;
L
Linus Torvalds 已提交
279 280
	u16 lnk_status;

R
Ryan Desfosses 已提交
281 282 283 284 285 286 287 288 289
	/*
	 * Data Link Layer Link Active Reporting must be capable for
	 * hot-plug capable downstream port. But old controller might
	 * not implement it. In this case, we wait for 1000 ms.
	*/
	if (ctrl->link_active_reporting)
		pcie_wait_link_active(ctrl);
	else
		msleep(1000);
290

291 292 293 294
	/* wait 100ms before read pci conf, and try in 1s */
	msleep(100);
	found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
					PCI_DEVFN(0, 0));
295

296
	pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
297
	ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
298 299
	if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
	    !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
300 301
		ctrl_err(ctrl, "link training error: status %#06x\n",
			 lnk_status);
302
		return -1;
L
Linus Torvalds 已提交
303 304
	}

305 306
	pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);

307 308
	if (!found)
		return -1;
309

310
	return 0;
L
Linus Torvalds 已提交
311 312
}

313 314
static int __pciehp_link_set(struct controller *ctrl, bool enable)
{
315
	struct pci_dev *pdev = ctrl_dev(ctrl);
316 317
	u16 lnk_ctrl;

318
	pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &lnk_ctrl);
319 320 321 322 323 324

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

325
	pcie_capability_write_word(pdev, PCI_EXP_LNKCTL, lnk_ctrl);
326
	ctrl_dbg(ctrl, "%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
327
	return 0;
328 329 330 331 332 333 334
}

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

335 336 337 338 339 340 341 342 343 344 345 346
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;

	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
	*status = (slot_ctrl & (PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC)) >> 6;
	return 0;
}

347
void pciehp_get_attention_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
348
{
349
	struct controller *ctrl = slot->ctrl;
350
	struct pci_dev *pdev = ctrl_dev(ctrl);
L
Linus Torvalds 已提交
351 352
	u16 slot_ctrl;

353
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
K
Kenji Kaneshige 已提交
354 355
	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 已提交
356

357 358
	switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
	case PCI_EXP_SLTCTL_ATTN_IND_ON:
L
Linus Torvalds 已提交
359 360
		*status = 1;	/* On */
		break;
361
	case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
L
Linus Torvalds 已提交
362 363
		*status = 2;	/* Blink */
		break;
364
	case PCI_EXP_SLTCTL_ATTN_IND_OFF:
L
Linus Torvalds 已提交
365 366 367 368 369 370 371 372
		*status = 0;	/* Off */
		break;
	default:
		*status = 0xFF;
		break;
	}
}

373
void pciehp_get_power_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
374
{
375
	struct controller *ctrl = slot->ctrl;
376
	struct pci_dev *pdev = ctrl_dev(ctrl);
L
Linus Torvalds 已提交
377 378
	u16 slot_ctrl;

379
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
K
Kenji Kaneshige 已提交
380 381
	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 已提交
382

383 384 385
	switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
	case PCI_EXP_SLTCTL_PWR_ON:
		*status = 1;	/* On */
L
Linus Torvalds 已提交
386
		break;
387 388
	case PCI_EXP_SLTCTL_PWR_OFF:
		*status = 0;	/* Off */
L
Linus Torvalds 已提交
389 390 391 392 393 394 395
		break;
	default:
		*status = 0xFF;
		break;
	}
}

396
void pciehp_get_latch_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
397
{
398
	struct pci_dev *pdev = ctrl_dev(slot->ctrl);
L
Linus Torvalds 已提交
399 400
	u16 slot_status;

401
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
402
	*status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
L
Linus Torvalds 已提交
403 404
}

405
void pciehp_get_adapter_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
406
{
407
	struct pci_dev *pdev = ctrl_dev(slot->ctrl);
L
Linus Torvalds 已提交
408 409
	u16 slot_status;

410
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
411
	*status = !!(slot_status & PCI_EXP_SLTSTA_PDS);
L
Linus Torvalds 已提交
412 413
}

K
Kenji Kaneshige 已提交
414
int pciehp_query_power_fault(struct slot *slot)
L
Linus Torvalds 已提交
415
{
416
	struct pci_dev *pdev = ctrl_dev(slot->ctrl);
L
Linus Torvalds 已提交
417 418
	u16 slot_status;

419
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
420
	return !!(slot_status & PCI_EXP_SLTSTA_PFD);
L
Linus Torvalds 已提交
421 422
}

423 424 425 426 427 428 429 430 431 432 433
int pciehp_set_raw_indicator_status(struct hotplug_slot *hotplug_slot,
				    u8 status)
{
	struct slot *slot = hotplug_slot->private;
	struct controller *ctrl = slot->ctrl;

	pcie_write_cmd_nowait(ctrl, status << 6,
			      PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC);
	return 0;
}

434
void pciehp_set_attention_status(struct slot *slot, u8 value)
L
Linus Torvalds 已提交
435
{
436
	struct controller *ctrl = slot->ctrl;
437
	u16 slot_cmd;
L
Linus Torvalds 已提交
438

439 440 441
	if (!ATTN_LED(ctrl))
		return;

L
Linus Torvalds 已提交
442
	switch (value) {
R
Ryan Desfosses 已提交
443
	case 0:		/* turn off */
444
		slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_OFF;
445 446
		break;
	case 1:		/* turn on */
447
		slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_ON;
448 449
		break;
	case 2:		/* turn blink */
450
		slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_BLINK;
451 452
		break;
	default:
453
		return;
L
Linus Torvalds 已提交
454
	}
455
	pcie_write_cmd_nowait(ctrl, slot_cmd, PCI_EXP_SLTCTL_AIC);
K
Kenji Kaneshige 已提交
456 457
	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 已提交
458 459
}

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

464 465 466
	if (!PWR_LED(ctrl))
		return;

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

K
Kenji Kaneshige 已提交
474
void pciehp_green_led_off(struct slot *slot)
L
Linus Torvalds 已提交
475
{
476
	struct controller *ctrl = slot->ctrl;
L
Linus Torvalds 已提交
477

478 479 480
	if (!PWR_LED(ctrl))
		return;

481 482
	pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
			      PCI_EXP_SLTCTL_PIC);
K
Kenji Kaneshige 已提交
483
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
484 485
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_IND_OFF);
L
Linus Torvalds 已提交
486 487
}

K
Kenji Kaneshige 已提交
488
void pciehp_green_led_blink(struct slot *slot)
L
Linus Torvalds 已提交
489
{
490
	struct controller *ctrl = slot->ctrl;
491

492 493 494
	if (!PWR_LED(ctrl))
		return;

495 496
	pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK,
			      PCI_EXP_SLTCTL_PIC);
K
Kenji Kaneshige 已提交
497
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
498 499
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_IND_BLINK);
L
Linus Torvalds 已提交
500 501
}

R
Ryan Desfosses 已提交
502
int pciehp_power_on_slot(struct slot *slot)
L
Linus Torvalds 已提交
503
{
504
	struct controller *ctrl = slot->ctrl;
505
	struct pci_dev *pdev = ctrl_dev(ctrl);
506
	u16 slot_status;
507
	int retval;
L
Linus Torvalds 已提交
508

509
	/* Clear sticky power-fault bit from previous power failures */
510
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
511 512 513
	if (slot_status & PCI_EXP_SLTSTA_PFD)
		pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
					   PCI_EXP_SLTSTA_PFD);
514
	ctrl->power_fault_detected = 0;
L
Linus Torvalds 已提交
515

516
	pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
K
Kenji Kaneshige 已提交
517
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
518 519
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_ON);
L
Linus Torvalds 已提交
520

521 522 523 524
	retval = pciehp_link_enable(ctrl);
	if (retval)
		ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);

L
Linus Torvalds 已提交
525 526 527
	return retval;
}

R
Ryan Desfosses 已提交
528
void pciehp_power_off_slot(struct slot *slot)
L
Linus Torvalds 已提交
529
{
530
	struct controller *ctrl = slot->ctrl;
531

532
	pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_OFF, PCI_EXP_SLTCTL_PCC);
K
Kenji Kaneshige 已提交
533
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
534 535
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_OFF);
L
Linus Torvalds 已提交
536 537
}

538
static irqreturn_t pciehp_isr(int irq, void *dev_id)
L
Linus Torvalds 已提交
539
{
540
	struct controller *ctrl = (struct controller *)dev_id;
541
	struct pci_dev *pdev = ctrl_dev(ctrl);
542
	struct slot *slot = ctrl->slot;
543
	u16 status, events;
544
	u8 present;
545
	bool link;
L
Linus Torvalds 已提交
546

547 548 549 550
	/* Interrupts cannot originate from a controller that's asleep */
	if (pdev->current_state == PCI_D3cold)
		return IRQ_NONE;

551 552 553 554 555 556
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &status);
	if (status == (u16) ~0) {
		ctrl_info(ctrl, "%s: no response from device\n", __func__);
		return IRQ_NONE;
	}

557
	/*
558 559
	 * Slot Status contains plain status bits as well as event
	 * notification bits; right now we only want the event bits.
560
	 */
561
	events = status & (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
562 563
			   PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_CC |
			   PCI_EXP_SLTSTA_DLLSC);
564 565 566 567 568 569 570 571

	/*
	 * 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;

572 573
	if (!events)
		return IRQ_NONE;
574

575 576 577 578
	/* Capture link status before clearing interrupts */
	if (events & PCI_EXP_SLTSTA_DLLSC)
		link = pciehp_check_link_active(ctrl);

579
	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, events);
580
	ctrl_dbg(ctrl, "pending interrupts %#06x from Slot Status\n", events);
581

582
	/* Check Command Complete Interrupt Pending */
583
	if (events & PCI_EXP_SLTSTA_CC) {
584
		ctrl->cmd_busy = 0;
585
		smp_mb();
586
		wake_up(&ctrl->queue);
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
	/* Check Attention Button Pressed */
595
	if (events & PCI_EXP_SLTSTA_ABP) {
596
		ctrl_info(ctrl, "Slot(%s): Attention button pressed\n",
597 598 599
			  slot_name(slot));
		pciehp_queue_interrupt_event(slot, INT_BUTTON_PRESS);
	}
600

601 602 603 604 605 606 607 608 609 610 611 612
	/*
	 * Check Link Status Changed at higher precedence than Presence
	 * Detect Changed.  The PDS value may be set to "card present" from
	 * out-of-band detection, which may be in conflict with a Link Down
	 * and cause the wrong event to queue.
	 */
	if (events & PCI_EXP_SLTSTA_DLLSC) {
		ctrl_info(ctrl, "Slot(%s): Link %s\n", slot_name(slot),
			  link ? "Up" : "Down");
		pciehp_queue_interrupt_event(slot, link ? INT_LINK_UP :
					     INT_LINK_DOWN);
	} else if (events & PCI_EXP_SLTSTA_PDC) {
613
		present = !!(status & PCI_EXP_SLTSTA_PDS);
614 615
		ctrl_info(ctrl, "Slot(%s): Card %spresent\n", slot_name(slot),
			  present ? "" : "not ");
616 617 618
		pciehp_queue_interrupt_event(slot, present ? INT_PRESENCE_ON :
					     INT_PRESENCE_OFF);
	}
619

620
	/* Check Power Fault Detected */
621
	if ((events & PCI_EXP_SLTSTA_PFD) && !ctrl->power_fault_detected) {
622
		ctrl->power_fault_detected = 1;
623
		ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(slot));
624
		pciehp_queue_interrupt_event(slot, INT_POWER_FAULT);
625
	}
626

L
Linus Torvalds 已提交
627 628 629
	return IRQ_HANDLED;
}

630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
static irqreturn_t pcie_isr(int irq, void *dev_id)
{
	irqreturn_t rc, handled = IRQ_NONE;

	/*
	 * To guarantee that all interrupt events are serviced, we need to
	 * re-inspect Slot Status register after clearing what is presumed
	 * to be the last pending interrupt.
	 */
	do {
		rc = pciehp_isr(irq, dev_id);
		if (rc == IRQ_HANDLED)
			handled = IRQ_HANDLED;
	} while (rc == IRQ_HANDLED);

	/* Return IRQ_HANDLED if we handled one or more events */
	return handled;
}

649
static void pcie_enable_notification(struct controller *ctrl)
M
Mark Lord 已提交
650
{
651
	u16 cmd, mask;
L
Linus Torvalds 已提交
652

653 654 655 656 657 658 659 660 661 662
	/*
	 * 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.
	 */
663 664 665 666 667 668 669

	/*
	 * 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;
670
	if (ATTN_BUTTN(ctrl))
671
		cmd |= PCI_EXP_SLTCTL_ABPE;
672 673
	else
		cmd |= PCI_EXP_SLTCTL_PDCE;
674
	if (!pciehp_poll_mode)
675
		cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
676

677
	mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
678
		PCI_EXP_SLTCTL_PFDE |
679 680
		PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
		PCI_EXP_SLTCTL_DLLSCE);
681

682
	pcie_write_cmd_nowait(ctrl, cmd, mask);
683 684
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
685 686
}

687 688 689 690 691 692 693 694 695 696 697
void pcie_reenable_notification(struct controller *ctrl)
{
	/*
	 * Clear both Presence and Data Link Layer Changed to make sure
	 * those events still fire after we have re-enabled them.
	 */
	pcie_capability_write_word(ctrl->pcie->port, PCI_EXP_SLTSTA,
				   PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
	pcie_enable_notification(ctrl);
}

698 699 700
static void pcie_disable_notification(struct controller *ctrl)
{
	u16 mask;
701

702 703
	mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
		PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
704 705
		PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
		PCI_EXP_SLTCTL_DLLSCE);
706
	pcie_write_cmd(ctrl, 0, mask);
707 708
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
709 710
}

711 712
/*
 * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
713 714 715 716
 * 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
717 718 719 720 721
 * events after.
 */
int pciehp_reset_slot(struct slot *slot, int probe)
{
	struct controller *ctrl = slot->ctrl;
722
	struct pci_dev *pdev = ctrl_dev(ctrl);
723
	u16 stat_mask = 0, ctrl_mask = 0;
724 725 726 727

	if (probe)
		return 0;

728
	if (!ATTN_BUTTN(ctrl)) {
729 730
		ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
		stat_mask |= PCI_EXP_SLTSTA_PDC;
731
	}
732 733 734 735
	ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
	stat_mask |= PCI_EXP_SLTSTA_DLLSC;

	pcie_write_cmd(ctrl, 0, ctrl_mask);
736 737
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
738 739
	if (pciehp_poll_mode)
		del_timer_sync(&ctrl->poll_timer);
740 741 742

	pci_reset_bridge_secondary_bus(ctrl->pcie->port);

743
	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
744
	pcie_write_cmd_nowait(ctrl, ctrl_mask, ctrl_mask);
745 746
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, ctrl_mask);
747
	if (pciehp_poll_mode)
748
		int_poll_timeout(&ctrl->poll_timer);
749 750 751
	return 0;
}

752
int pcie_init_notification(struct controller *ctrl)
753 754 755
{
	if (pciehp_request_irq(ctrl))
		return -1;
756
	pcie_enable_notification(ctrl);
757
	ctrl->notification_enabled = 1;
758 759 760
	return 0;
}

761
void pcie_shutdown_notification(struct controller *ctrl)
762
{
763 764 765 766 767
	if (ctrl->notification_enabled) {
		pcie_disable_notification(ctrl);
		pciehp_free_irq(ctrl);
		ctrl->notification_enabled = 0;
	}
768 769 770 771 772 773 774 775 776 777
}

static int pcie_init_slot(struct controller *ctrl)
{
	struct slot *slot;

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

778
	slot->wq = alloc_ordered_workqueue("pciehp-%u", 0, PSN(ctrl));
779 780 781
	if (!slot->wq)
		goto abort;

782 783
	slot->ctrl = ctrl;
	mutex_init(&slot->lock);
784
	mutex_init(&slot->hotplug_lock);
785
	INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
786
	ctrl->slot = slot;
L
Linus Torvalds 已提交
787
	return 0;
788 789 790
abort:
	kfree(slot);
	return -ENOMEM;
L
Linus Torvalds 已提交
791
}
792

793 794
static void pcie_cleanup_slot(struct controller *ctrl)
{
795
	struct slot *slot = ctrl->slot;
796

797
	destroy_workqueue(slot->wq);
798 799 800
	kfree(slot);
}

K
Kenji Kaneshige 已提交
801
static inline void dbg_ctrl(struct controller *ctrl)
802
{
803
	struct pci_dev *pdev = ctrl->pcie->port;
804
	u16 reg16;
805

K
Kenji Kaneshige 已提交
806 807
	if (!pciehp_debug)
		return;
808

809
	ctrl_info(ctrl, "Slot Capabilities      : 0x%08x\n", ctrl->slot_cap);
810
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &reg16);
811
	ctrl_info(ctrl, "Slot Status            : 0x%04x\n", reg16);
812
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &reg16);
813
	ctrl_info(ctrl, "Slot Control           : 0x%04x\n", reg16);
K
Kenji Kaneshige 已提交
814
}
815

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

818
struct controller *pcie_init(struct pcie_device *dev)
K
Kenji Kaneshige 已提交
819
{
820
	struct controller *ctrl;
821
	u32 slot_cap, link_cap;
K
Kenji Kaneshige 已提交
822
	struct pci_dev *pdev = dev->port;
823

824
	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
825
	if (!ctrl)
826
		goto abort;
827

828
	ctrl->pcie = dev;
829
	pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
830 831 832 833

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

834 835 836 837 838 839 840
	/*
	 * 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 已提交
841
	ctrl->slot_cap = slot_cap;
842 843
	mutex_init(&ctrl->ctrl_lock);
	init_waitqueue_head(&ctrl->queue);
K
Kenji Kaneshige 已提交
844
	dbg_ctrl(ctrl);
845

R
Ryan Desfosses 已提交
846 847
	/* Check if Data Link Layer Link Active Reporting is implemented */
	pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
848
	if (link_cap & PCI_EXP_LNKCAP_DLLLARC)
R
Ryan Desfosses 已提交
849
		ctrl->link_active_reporting = 1;
850

851 852 853 854 855 856
	/*
	 * Clear all remaining event bits in Slot Status register except
	 * Presence Detect Changed. We want to make sure possible
	 * hotplug event is triggered when the interrupt is unmasked so
	 * that we don't lose that event.
	 */
857 858
	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
		PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
859 860
		PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_CC |
		PCI_EXP_SLTSTA_DLLSC);
861

862
	ctrl_info(ctrl, "Slot #%d AttnBtn%c PwrCtrl%c MRL%c AttnInd%c PwrInd%c HotPlug%c Surprise%c Interlock%c NoCompl%c LLActRep%c%s\n",
863 864 865 866
		(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),
867 868 869 870
		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),
871 872
		FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
		FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
873 874
		FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC),
		pdev->broken_cmd_compl ? " (with Cmd Compl erratum)" : "");
875 876 877

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

879 880 881 882
	return ctrl;

abort_ctrl:
	kfree(ctrl);
883
abort:
884 885 886
	return NULL;
}

K
Kenji Kaneshige 已提交
887
void pciehp_release_ctrl(struct controller *ctrl)
888 889 890
{
	pcie_cleanup_slot(ctrl);
	kfree(ctrl);
891
}
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

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);