pciehp_hpc.c 23.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>
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 pciehp_isr(int irq, void *dev_id);
static irqreturn_t pciehp_ist(int irq, void *dev_id);
36
static int pciehp_poll(void *data);
L
Linus Torvalds 已提交
37

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

	if (pciehp_poll_mode) {
43 44 45 46
		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 已提交
47 48 49
	}

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

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

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

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

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

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

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

	if (!ctrl->cmd_busy)
		return;

110 111 112 113 114 115 116 117 118 119
	/*
	 * 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;

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

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

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

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

143 144
	mutex_lock(&ctrl->ctrl_lock);

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

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

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

165 166 167 168 169 170 171 172 173 174 175
	/*
	 * 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;

176 177 178 179 180 181 182
	/*
	 * 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);

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

187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/**
 * 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);
}

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

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

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

	return ret;
217 218
}

219 220
static void pcie_wait_link_active(struct controller *ctrl)
{
221 222 223
	struct pci_dev *pdev = ctrl_dev(ctrl);

	pcie_wait_for_link(pdev, true);
224 225
}

226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
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 已提交
252
int pciehp_check_link_status(struct controller *ctrl)
L
Linus Torvalds 已提交
253
{
254
	struct pci_dev *pdev = ctrl_dev(ctrl);
255
	bool found;
L
Linus Torvalds 已提交
256 257
	u16 lnk_status;

R
Ryan Desfosses 已提交
258 259 260 261 262 263 264 265 266
	/*
	 * 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);
267

268 269 270 271
	/* 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));
272

273 274 275 276 277
	/* ignore link or presence changes up to this point */
	if (found)
		atomic_and(~(PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC),
			   &ctrl->pending_events);

278
	pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
279
	ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
280 281
	if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
	    !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
282 283
		ctrl_err(ctrl, "link training error: status %#06x\n",
			 lnk_status);
284
		return -1;
L
Linus Torvalds 已提交
285 286
	}

287 288
	pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);

289 290
	if (!found)
		return -1;
291

292
	return 0;
L
Linus Torvalds 已提交
293 294
}

295 296
static int __pciehp_link_set(struct controller *ctrl, bool enable)
{
297
	struct pci_dev *pdev = ctrl_dev(ctrl);
298 299
	u16 lnk_ctrl;

300
	pcie_capability_read_word(pdev, PCI_EXP_LNKCTL, &lnk_ctrl);
301 302 303 304 305 306

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

307
	pcie_capability_write_word(pdev, PCI_EXP_LNKCTL, lnk_ctrl);
308
	ctrl_dbg(ctrl, "%s: lnk_ctrl = %x\n", __func__, lnk_ctrl);
309
	return 0;
310 311 312 313 314 315 316
}

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

317 318 319 320 321 322 323 324 325 326 327 328
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;
}

329
void pciehp_get_attention_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
330
{
331
	struct controller *ctrl = slot->ctrl;
332
	struct pci_dev *pdev = ctrl_dev(ctrl);
L
Linus Torvalds 已提交
333 334
	u16 slot_ctrl;

335
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
K
Kenji Kaneshige 已提交
336 337
	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 已提交
338

339 340
	switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
	case PCI_EXP_SLTCTL_ATTN_IND_ON:
L
Linus Torvalds 已提交
341 342
		*status = 1;	/* On */
		break;
343
	case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
L
Linus Torvalds 已提交
344 345
		*status = 2;	/* Blink */
		break;
346
	case PCI_EXP_SLTCTL_ATTN_IND_OFF:
L
Linus Torvalds 已提交
347 348 349 350 351 352 353 354
		*status = 0;	/* Off */
		break;
	default:
		*status = 0xFF;
		break;
	}
}

355
void pciehp_get_power_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
356
{
357
	struct controller *ctrl = slot->ctrl;
358
	struct pci_dev *pdev = ctrl_dev(ctrl);
L
Linus Torvalds 已提交
359 360
	u16 slot_ctrl;

361
	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
K
Kenji Kaneshige 已提交
362 363
	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 已提交
364

365 366 367
	switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
	case PCI_EXP_SLTCTL_PWR_ON:
		*status = 1;	/* On */
L
Linus Torvalds 已提交
368
		break;
369 370
	case PCI_EXP_SLTCTL_PWR_OFF:
		*status = 0;	/* Off */
L
Linus Torvalds 已提交
371 372 373 374 375 376 377
		break;
	default:
		*status = 0xFF;
		break;
	}
}

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

383
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
384
	*status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
L
Linus Torvalds 已提交
385 386
}

387
void pciehp_get_adapter_status(struct slot *slot, u8 *status)
L
Linus Torvalds 已提交
388
{
389
	struct pci_dev *pdev = ctrl_dev(slot->ctrl);
L
Linus Torvalds 已提交
390 391
	u16 slot_status;

392
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
393
	*status = !!(slot_status & PCI_EXP_SLTSTA_PDS);
L
Linus Torvalds 已提交
394 395
}

K
Kenji Kaneshige 已提交
396
int pciehp_query_power_fault(struct slot *slot)
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
	return !!(slot_status & PCI_EXP_SLTSTA_PFD);
L
Linus Torvalds 已提交
403 404
}

405 406 407 408 409 410 411 412 413 414 415
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;
}

416
void pciehp_set_attention_status(struct slot *slot, u8 value)
L
Linus Torvalds 已提交
417
{
418
	struct controller *ctrl = slot->ctrl;
419
	u16 slot_cmd;
L
Linus Torvalds 已提交
420

421 422 423
	if (!ATTN_LED(ctrl))
		return;

L
Linus Torvalds 已提交
424
	switch (value) {
R
Ryan Desfosses 已提交
425
	case 0:		/* turn off */
426
		slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_OFF;
427 428
		break;
	case 1:		/* turn on */
429
		slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_ON;
430 431
		break;
	case 2:		/* turn blink */
432
		slot_cmd = PCI_EXP_SLTCTL_ATTN_IND_BLINK;
433 434
		break;
	default:
435
		return;
L
Linus Torvalds 已提交
436
	}
437
	pcie_write_cmd_nowait(ctrl, slot_cmd, PCI_EXP_SLTCTL_AIC);
K
Kenji Kaneshige 已提交
438 439
	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 已提交
440 441
}

K
Kenji Kaneshige 已提交
442
void pciehp_green_led_on(struct slot *slot)
L
Linus Torvalds 已提交
443
{
444
	struct controller *ctrl = slot->ctrl;
445

446 447 448
	if (!PWR_LED(ctrl))
		return;

449 450
	pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_ON,
			      PCI_EXP_SLTCTL_PIC);
K
Kenji Kaneshige 已提交
451
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
452 453
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_IND_ON);
L
Linus Torvalds 已提交
454 455
}

K
Kenji Kaneshige 已提交
456
void pciehp_green_led_off(struct slot *slot)
L
Linus Torvalds 已提交
457
{
458
	struct controller *ctrl = slot->ctrl;
L
Linus Torvalds 已提交
459

460 461 462
	if (!PWR_LED(ctrl))
		return;

463 464
	pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
			      PCI_EXP_SLTCTL_PIC);
K
Kenji Kaneshige 已提交
465
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
466 467
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_IND_OFF);
L
Linus Torvalds 已提交
468 469
}

K
Kenji Kaneshige 已提交
470
void pciehp_green_led_blink(struct slot *slot)
L
Linus Torvalds 已提交
471
{
472
	struct controller *ctrl = slot->ctrl;
473

474 475 476
	if (!PWR_LED(ctrl))
		return;

477 478
	pcie_write_cmd_nowait(ctrl, PCI_EXP_SLTCTL_PWR_IND_BLINK,
			      PCI_EXP_SLTCTL_PIC);
K
Kenji Kaneshige 已提交
479
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
480 481
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_IND_BLINK);
L
Linus Torvalds 已提交
482 483
}

R
Ryan Desfosses 已提交
484
int pciehp_power_on_slot(struct slot *slot)
L
Linus Torvalds 已提交
485
{
486
	struct controller *ctrl = slot->ctrl;
487
	struct pci_dev *pdev = ctrl_dev(ctrl);
488
	u16 slot_status;
489
	int retval;
L
Linus Torvalds 已提交
490

491
	/* Clear sticky power-fault bit from previous power failures */
492
	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
493 494 495
	if (slot_status & PCI_EXP_SLTSTA_PFD)
		pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
					   PCI_EXP_SLTSTA_PFD);
496
	ctrl->power_fault_detected = 0;
L
Linus Torvalds 已提交
497

498
	pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
K
Kenji Kaneshige 已提交
499
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
500 501
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
		 PCI_EXP_SLTCTL_PWR_ON);
L
Linus Torvalds 已提交
502

503 504 505 506
	retval = pciehp_link_enable(ctrl);
	if (retval)
		ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);

L
Linus Torvalds 已提交
507 508 509
	return retval;
}

R
Ryan Desfosses 已提交
510
void pciehp_power_off_slot(struct slot *slot)
L
Linus Torvalds 已提交
511
{
512
	struct controller *ctrl = slot->ctrl;
513

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

520
static irqreturn_t pciehp_isr(int irq, void *dev_id)
L
Linus Torvalds 已提交
521
{
522
	struct controller *ctrl = (struct controller *)dev_id;
523
	struct pci_dev *pdev = ctrl_dev(ctrl);
524
	u16 status, events;
L
Linus Torvalds 已提交
525

526 527 528
	/*
	 * Interrupts only occur in D3hot or shallower (PCIe r4.0, sec 6.7.3.4).
	 */
529 530 531
	if (pdev->current_state == PCI_D3cold)
		return IRQ_NONE;

532 533 534 535 536 537
	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;
	}

538
	/*
539 540
	 * Slot Status contains plain status bits as well as event
	 * notification bits; right now we only want the event bits.
541
	 */
542
	events = status & (PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
543 544
			   PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_CC |
			   PCI_EXP_SLTSTA_DLLSC);
545 546 547 548 549 550 551 552

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

553 554
	if (!events)
		return IRQ_NONE;
555

556
	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, events);
557
	ctrl_dbg(ctrl, "pending interrupts %#06x from Slot Status\n", events);
558

559 560 561 562
	/*
	 * Command Completed notifications are not deferred to the
	 * IRQ thread because it may be waiting for their arrival.
	 */
563
	if (events & PCI_EXP_SLTSTA_CC) {
564
		ctrl->cmd_busy = 0;
565
		smp_mb();
566
		wake_up(&ctrl->queue);
567 568 569 570 571

		if (events == PCI_EXP_SLTSTA_CC)
			return IRQ_HANDLED;

		events &= ~PCI_EXP_SLTSTA_CC;
L
Linus Torvalds 已提交
572 573
	}

574 575 576
	if (pdev->ignore_hotplug) {
		ctrl_dbg(ctrl, "ignoring hotplug event %#06x\n", events);
		return IRQ_HANDLED;
577 578
	}

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594
	/* 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;
	struct slot *slot = ctrl->slot;
	u32 events;

	synchronize_hardirq(irq);
	events = atomic_xchg(&ctrl->pending_events, 0);
	if (!events)
		return IRQ_NONE;

595
	/* Check Attention Button Pressed */
596
	if (events & PCI_EXP_SLTSTA_ABP) {
597
		ctrl_info(ctrl, "Slot(%s): Attention button pressed\n",
598
			  slot_name(slot));
599
		pciehp_handle_button_press(slot);
600
	}
601

602
	/*
603 604 605
	 * Disable requests have higher priority than Presence Detect Changed
	 * or Data Link Layer State Changed events.
	 *
606 607
	 * Check Link Status Changed at higher precedence than Presence
	 * Detect Changed.  The PDS value may be set to "card present" from
608
	 * out-of-band detection, which may be in conflict with a Link Down.
609
	 */
610 611 612
	if (events & DISABLE_SLOT)
		pciehp_handle_disable_request(slot);
	else if (events & PCI_EXP_SLTSTA_DLLSC)
613 614 615
		pciehp_handle_link_change(slot);
	else if (events & PCI_EXP_SLTSTA_PDC)
		pciehp_handle_presence_change(slot);
616

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

625
	wake_up(&ctrl->requester);
L
Linus Torvalds 已提交
626 627 628
	return IRQ_HANDLED;
}

629 630 631 632 633 634 635 636 637 638
static int pciehp_poll(void *data)
{
	struct controller *ctrl = data;

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

	while (!kthread_should_stop()) {
		if (kthread_should_park())
			kthread_parkme();

639 640 641
		/* poll for interrupt events or user requests */
		while (pciehp_isr(IRQ_NOTCONNECTED, ctrl) == IRQ_WAKE_THREAD ||
		       atomic_read(&ctrl->pending_events))
642 643 644 645 646 647 648 649 650 651 652
			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;
}

653
static void pcie_enable_notification(struct controller *ctrl)
M
Mark Lord 已提交
654
{
655
	u16 cmd, mask;
L
Linus Torvalds 已提交
656

657 658 659 660 661 662 663 664 665 666
	/*
	 * 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.
	 */
667 668 669 670 671 672 673

	/*
	 * 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;
674
	if (ATTN_BUTTN(ctrl))
675
		cmd |= PCI_EXP_SLTCTL_ABPE;
676 677
	else
		cmd |= PCI_EXP_SLTCTL_PDCE;
678
	if (!pciehp_poll_mode)
679
		cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
680

681
	mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
682
		PCI_EXP_SLTCTL_PFDE |
683 684
		PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
		PCI_EXP_SLTCTL_DLLSCE);
685

686
	pcie_write_cmd_nowait(ctrl, cmd, mask);
687 688
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
689 690
}

691 692 693 694 695 696 697 698 699 700 701
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);
}

702 703 704
static void pcie_disable_notification(struct controller *ctrl)
{
	u16 mask;
705

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

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

	if (probe)
		return 0;

732
	if (!ATTN_BUTTN(ctrl)) {
733 734
		ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
		stat_mask |= PCI_EXP_SLTSTA_PDC;
735
	}
736 737 738 739
	ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
	stat_mask |= PCI_EXP_SLTSTA_DLLSC;

	pcie_write_cmd(ctrl, 0, ctrl_mask);
740 741
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
742
	if (pciehp_poll_mode)
743
		kthread_park(ctrl->poll_thread);
744 745 746

	pci_reset_bridge_secondary_bus(ctrl->pcie->port);

747
	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
748
	pcie_write_cmd_nowait(ctrl, ctrl_mask, ctrl_mask);
749 750
	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, ctrl_mask);
751
	if (pciehp_poll_mode)
752
		kthread_unpark(ctrl->poll_thread);
753 754 755
	return 0;
}

756
int pcie_init_notification(struct controller *ctrl)
757 758 759
{
	if (pciehp_request_irq(ctrl))
		return -1;
760
	pcie_enable_notification(ctrl);
761
	ctrl->notification_enabled = 1;
762 763 764
	return 0;
}

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

static int pcie_init_slot(struct controller *ctrl)
{
776
	struct pci_bus *subordinate = ctrl_dev(ctrl)->subordinate;
777 778 779 780 781 782
	struct slot *slot;

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

783 784 785 786
	down_read(&pci_bus_sem);
	slot->state = list_empty(&subordinate->devices) ? OFF_STATE : ON_STATE;
	up_read(&pci_bus_sem);

787 788 789
	slot->ctrl = ctrl;
	mutex_init(&slot->lock);
	INIT_DELAYED_WORK(&slot->work, pciehp_queue_pushbutton_work);
790
	ctrl->slot = slot;
L
Linus Torvalds 已提交
791 792
	return 0;
}
793

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

L
Lukas Wunner 已提交
798
	cancel_delayed_work_sync(&slot->work);
799 800 801
	kfree(slot);
}

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

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

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

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

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

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

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

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

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

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

853 854 855 856 857 858
	/*
	 * 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.
	 */
859 860
	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
		PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
861 862
		PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_CC |
		PCI_EXP_SLTSTA_DLLSC);
863

864
	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",
865 866 867 868
		(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),
869 870 871 872
		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),
873 874
		FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
		FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
875 876
		FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC),
		pdev->broken_cmd_compl ? " (with Cmd Compl erratum)" : "");
877 878 879

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

881 882 883 884
	return ctrl;

abort_ctrl:
	kfree(ctrl);
885
abort:
886 887 888
	return NULL;
}

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

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