• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * PCI Express PCI Hot Plug Driver
4  *
5  * Copyright (C) 1995,2001 Compaq Computer Corporation
6  * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
7  * Copyright (C) 2001 IBM Corp.
8  * Copyright (C) 2003-2004 Intel Corporation
9  *
10  * All rights reserved.
11  *
12  * Send feedback to <greg@kroah.com>,<kristen.c.accardi@intel.com>
13  */
14 
15 #define dev_fmt(fmt) "pciehp: " fmt
16 
17 #include <linux/kernel.h>
18 #include <linux/types.h>
19 #include <linux/jiffies.h>
20 #include <linux/kthread.h>
21 #include <linux/pci.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/interrupt.h>
24 #include <linux/slab.h>
25 
26 #include "../pci.h"
27 #include "pciehp.h"
28 
ctrl_dev(struct controller * ctrl)29 static inline struct pci_dev *ctrl_dev(struct controller *ctrl)
30 {
31 	return ctrl->pcie->port;
32 }
33 
34 static irqreturn_t pciehp_isr(int irq, void *dev_id);
35 static irqreturn_t pciehp_ist(int irq, void *dev_id);
36 static int pciehp_poll(void *data);
37 
pciehp_request_irq(struct controller * ctrl)38 static inline int pciehp_request_irq(struct controller *ctrl)
39 {
40 	int retval, irq = ctrl->pcie->irq;
41 
42 	if (pciehp_poll_mode) {
43 		ctrl->poll_thread = kthread_run(&pciehp_poll, ctrl,
44 						"pciehp_poll-%s",
45 						slot_name(ctrl));
46 		return PTR_ERR_OR_ZERO(ctrl->poll_thread);
47 	}
48 
49 	/* Installs the interrupt handler */
50 	retval = request_threaded_irq(irq, pciehp_isr, pciehp_ist,
51 				      IRQF_SHARED, "pciehp", ctrl);
52 	if (retval)
53 		ctrl_err(ctrl, "Cannot get irq %d for the hotplug controller\n",
54 			 irq);
55 	return retval;
56 }
57 
pciehp_free_irq(struct controller * ctrl)58 static inline void pciehp_free_irq(struct controller *ctrl)
59 {
60 	if (pciehp_poll_mode)
61 		kthread_stop(ctrl->poll_thread);
62 	else
63 		free_irq(ctrl->pcie->irq, ctrl);
64 }
65 
pcie_poll_cmd(struct controller * ctrl,int timeout)66 static int pcie_poll_cmd(struct controller *ctrl, int timeout)
67 {
68 	struct pci_dev *pdev = ctrl_dev(ctrl);
69 	u16 slot_status;
70 
71 	while (true) {
72 		pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
73 		if (slot_status == (u16) ~0) {
74 			ctrl_info(ctrl, "%s: no response from device\n",
75 				  __func__);
76 			return 0;
77 		}
78 
79 		if (slot_status & PCI_EXP_SLTSTA_CC) {
80 			pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
81 						   PCI_EXP_SLTSTA_CC);
82 			ctrl->cmd_busy = 0;
83 			smp_mb();
84 			return 1;
85 		}
86 		if (timeout < 0)
87 			break;
88 		msleep(10);
89 		timeout -= 10;
90 	}
91 	return 0;	/* timeout */
92 }
93 
pcie_wait_cmd(struct controller * ctrl)94 static void pcie_wait_cmd(struct controller *ctrl)
95 {
96 	unsigned int msecs = pciehp_poll_mode ? 2500 : 1000;
97 	unsigned long duration = msecs_to_jiffies(msecs);
98 	unsigned long cmd_timeout = ctrl->cmd_started + duration;
99 	unsigned long now, timeout;
100 	int rc;
101 
102 	/*
103 	 * If the controller does not generate notifications for command
104 	 * completions, we never need to wait between writes.
105 	 */
106 	if (NO_CMD_CMPL(ctrl))
107 		return;
108 
109 	if (!ctrl->cmd_busy)
110 		return;
111 
112 	/*
113 	 * Even if the command has already timed out, we want to call
114 	 * pcie_poll_cmd() so it can clear PCI_EXP_SLTSTA_CC.
115 	 */
116 	now = jiffies;
117 	if (time_before_eq(cmd_timeout, now))
118 		timeout = 1;
119 	else
120 		timeout = cmd_timeout - now;
121 
122 	if (ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE &&
123 	    ctrl->slot_ctrl & PCI_EXP_SLTCTL_CCIE)
124 		rc = wait_event_timeout(ctrl->queue, !ctrl->cmd_busy, timeout);
125 	else
126 		rc = pcie_poll_cmd(ctrl, jiffies_to_msecs(timeout));
127 
128 	if (!rc)
129 		ctrl_info(ctrl, "Timeout on hotplug command %#06x (issued %u msec ago)\n",
130 			  ctrl->slot_ctrl,
131 			  jiffies_to_msecs(jiffies - ctrl->cmd_started));
132 }
133 
134 #define CC_ERRATUM_MASK		(PCI_EXP_SLTCTL_PCC |	\
135 				 PCI_EXP_SLTCTL_PIC |	\
136 				 PCI_EXP_SLTCTL_AIC |	\
137 				 PCI_EXP_SLTCTL_EIC)
138 
pcie_do_write_cmd(struct controller * ctrl,u16 cmd,u16 mask,bool wait)139 static void pcie_do_write_cmd(struct controller *ctrl, u16 cmd,
140 			      u16 mask, bool wait)
141 {
142 	struct pci_dev *pdev = ctrl_dev(ctrl);
143 	u16 slot_ctrl_orig, slot_ctrl;
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 
152 	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
153 	if (slot_ctrl == (u16) ~0) {
154 		ctrl_info(ctrl, "%s: no response from device\n", __func__);
155 		goto out;
156 	}
157 
158 	slot_ctrl_orig = slot_ctrl;
159 	slot_ctrl &= ~mask;
160 	slot_ctrl |= (cmd & mask);
161 	ctrl->cmd_busy = 1;
162 	smp_mb();
163 	ctrl->slot_ctrl = slot_ctrl;
164 	pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, slot_ctrl);
165 	ctrl->cmd_started = jiffies;
166 
167 	/*
168 	 * Controllers with the Intel CF118 and similar errata advertise
169 	 * Command Completed support, but they only set Command Completed
170 	 * if we change the "Control" bits for power, power indicator,
171 	 * attention indicator, or interlock.  If we only change the
172 	 * "Enable" bits, they never set the Command Completed bit.
173 	 */
174 	if (pdev->broken_cmd_compl &&
175 	    (slot_ctrl_orig & CC_ERRATUM_MASK) == (slot_ctrl & CC_ERRATUM_MASK))
176 		ctrl->cmd_busy = 0;
177 
178 	/*
179 	 * Optionally wait for the hardware to be ready for a new command,
180 	 * indicating completion of the above issued command.
181 	 */
182 	if (wait)
183 		pcie_wait_cmd(ctrl);
184 
185 out:
186 	mutex_unlock(&ctrl->ctrl_lock);
187 }
188 
189 /**
190  * pcie_write_cmd - Issue controller command
191  * @ctrl: controller to which the command is issued
192  * @cmd:  command value written to slot control register
193  * @mask: bitmask of slot control register to be modified
194  */
pcie_write_cmd(struct controller * ctrl,u16 cmd,u16 mask)195 static void pcie_write_cmd(struct controller *ctrl, u16 cmd, u16 mask)
196 {
197 	pcie_do_write_cmd(ctrl, cmd, mask, true);
198 }
199 
200 /* Same as above without waiting for the hardware to latch */
pcie_write_cmd_nowait(struct controller * ctrl,u16 cmd,u16 mask)201 static void pcie_write_cmd_nowait(struct controller *ctrl, u16 cmd, u16 mask)
202 {
203 	pcie_do_write_cmd(ctrl, cmd, mask, false);
204 }
205 
206 /**
207  * pciehp_check_link_active() - Is the link active
208  * @ctrl: PCIe hotplug controller
209  *
210  * Check whether the downstream link is currently active. Note it is
211  * possible that the card is removed immediately after this so the
212  * caller may need to take it into account.
213  *
214  * If the hotplug controller itself is not available anymore returns
215  * %-ENODEV.
216  */
pciehp_check_link_active(struct controller * ctrl)217 int pciehp_check_link_active(struct controller *ctrl)
218 {
219 	struct pci_dev *pdev = ctrl_dev(ctrl);
220 	u16 lnk_status;
221 	int ret;
222 
223 	ret = pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
224 	if (ret == PCIBIOS_DEVICE_NOT_FOUND || lnk_status == (u16)~0)
225 		return -ENODEV;
226 
227 	ret = !!(lnk_status & PCI_EXP_LNKSTA_DLLLA);
228 	ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
229 
230 	return ret;
231 }
232 
pci_bus_check_dev(struct pci_bus * bus,int devfn)233 static bool pci_bus_check_dev(struct pci_bus *bus, int devfn)
234 {
235 	u32 l;
236 	int count = 0;
237 	int delay = 1000, step = 20;
238 	bool found = false;
239 
240 	do {
241 		found = pci_bus_read_dev_vendor_id(bus, devfn, &l, 0);
242 		count++;
243 
244 		if (found)
245 			break;
246 
247 		msleep(step);
248 		delay -= step;
249 	} while (delay > 0);
250 
251 	if (count > 1)
252 		pr_debug("pci %04x:%02x:%02x.%d id reading try %d times with interval %d ms to get %08x\n",
253 			pci_domain_nr(bus), bus->number, PCI_SLOT(devfn),
254 			PCI_FUNC(devfn), count, step, l);
255 
256 	return found;
257 }
258 
pciehp_check_link_status(struct controller * ctrl)259 int pciehp_check_link_status(struct controller *ctrl)
260 {
261 	struct pci_dev *pdev = ctrl_dev(ctrl);
262 	bool found;
263 	u16 lnk_status;
264 
265 	if (!pcie_wait_for_link(pdev, true))
266 		return -1;
267 
268 	found = pci_bus_check_dev(ctrl->pcie->port->subordinate,
269 					PCI_DEVFN(0, 0));
270 
271 	/* ignore link or presence changes up to this point */
272 	if (found)
273 		atomic_and(~(PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC),
274 			   &ctrl->pending_events);
275 
276 	pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lnk_status);
277 	ctrl_dbg(ctrl, "%s: lnk_status = %x\n", __func__, lnk_status);
278 	if ((lnk_status & PCI_EXP_LNKSTA_LT) ||
279 	    !(lnk_status & PCI_EXP_LNKSTA_NLW)) {
280 		ctrl_err(ctrl, "link training error: status %#06x\n",
281 			 lnk_status);
282 		return -1;
283 	}
284 
285 	pcie_update_link_speed(ctrl->pcie->port->subordinate, lnk_status);
286 
287 	if (!found)
288 		return -1;
289 
290 	return 0;
291 }
292 
__pciehp_link_set(struct controller * ctrl,bool enable)293 static int __pciehp_link_set(struct controller *ctrl, bool enable)
294 {
295 	struct pci_dev *pdev = ctrl_dev(ctrl);
296 
297 	pcie_capability_clear_and_set_word(pdev, PCI_EXP_LNKCTL,
298 					   PCI_EXP_LNKCTL_LD,
299 					   enable ? 0 : PCI_EXP_LNKCTL_LD);
300 
301 	return 0;
302 }
303 
pciehp_link_enable(struct controller * ctrl)304 static int pciehp_link_enable(struct controller *ctrl)
305 {
306 	return __pciehp_link_set(ctrl, true);
307 }
308 
pciehp_get_raw_indicator_status(struct hotplug_slot * hotplug_slot,u8 * status)309 int pciehp_get_raw_indicator_status(struct hotplug_slot *hotplug_slot,
310 				    u8 *status)
311 {
312 	struct controller *ctrl = to_ctrl(hotplug_slot);
313 	struct pci_dev *pdev = ctrl_dev(ctrl);
314 	u16 slot_ctrl;
315 
316 	pci_config_pm_runtime_get(pdev);
317 	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
318 	pci_config_pm_runtime_put(pdev);
319 	*status = (slot_ctrl & (PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC)) >> 6;
320 	return 0;
321 }
322 
pciehp_get_attention_status(struct hotplug_slot * hotplug_slot,u8 * status)323 int pciehp_get_attention_status(struct hotplug_slot *hotplug_slot, u8 *status)
324 {
325 	struct controller *ctrl = to_ctrl(hotplug_slot);
326 	struct pci_dev *pdev = ctrl_dev(ctrl);
327 	u16 slot_ctrl;
328 
329 	pci_config_pm_runtime_get(pdev);
330 	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
331 	pci_config_pm_runtime_put(pdev);
332 	ctrl_dbg(ctrl, "%s: SLOTCTRL %x, value read %x\n", __func__,
333 		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
334 
335 	switch (slot_ctrl & PCI_EXP_SLTCTL_AIC) {
336 	case PCI_EXP_SLTCTL_ATTN_IND_ON:
337 		*status = 1;	/* On */
338 		break;
339 	case PCI_EXP_SLTCTL_ATTN_IND_BLINK:
340 		*status = 2;	/* Blink */
341 		break;
342 	case PCI_EXP_SLTCTL_ATTN_IND_OFF:
343 		*status = 0;	/* Off */
344 		break;
345 	default:
346 		*status = 0xFF;
347 		break;
348 	}
349 
350 	return 0;
351 }
352 
pciehp_get_power_status(struct controller * ctrl,u8 * status)353 void pciehp_get_power_status(struct controller *ctrl, u8 *status)
354 {
355 	struct pci_dev *pdev = ctrl_dev(ctrl);
356 	u16 slot_ctrl;
357 
358 	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &slot_ctrl);
359 	ctrl_dbg(ctrl, "%s: SLOTCTRL %x value read %x\n", __func__,
360 		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, slot_ctrl);
361 
362 	switch (slot_ctrl & PCI_EXP_SLTCTL_PCC) {
363 	case PCI_EXP_SLTCTL_PWR_ON:
364 		*status = 1;	/* On */
365 		break;
366 	case PCI_EXP_SLTCTL_PWR_OFF:
367 		*status = 0;	/* Off */
368 		break;
369 	default:
370 		*status = 0xFF;
371 		break;
372 	}
373 }
374 
pciehp_get_latch_status(struct controller * ctrl,u8 * status)375 void pciehp_get_latch_status(struct controller *ctrl, u8 *status)
376 {
377 	struct pci_dev *pdev = ctrl_dev(ctrl);
378 	u16 slot_status;
379 
380 	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
381 	*status = !!(slot_status & PCI_EXP_SLTSTA_MRLSS);
382 }
383 
384 /**
385  * pciehp_card_present() - Is the card present
386  * @ctrl: PCIe hotplug controller
387  *
388  * Function checks whether the card is currently present in the slot and
389  * in that case returns true. Note it is possible that the card is
390  * removed immediately after the check so the caller may need to take
391  * this into account.
392  *
393  * It the hotplug controller itself is not available anymore returns
394  * %-ENODEV.
395  */
pciehp_card_present(struct controller * ctrl)396 int pciehp_card_present(struct controller *ctrl)
397 {
398 	struct pci_dev *pdev = ctrl_dev(ctrl);
399 	u16 slot_status;
400 	int ret;
401 
402 	ret = pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
403 	if (ret == PCIBIOS_DEVICE_NOT_FOUND || slot_status == (u16)~0)
404 		return -ENODEV;
405 
406 	return !!(slot_status & PCI_EXP_SLTSTA_PDS);
407 }
408 
409 /**
410  * pciehp_card_present_or_link_active() - whether given slot is occupied
411  * @ctrl: PCIe hotplug controller
412  *
413  * Unlike pciehp_card_present(), which determines presence solely from the
414  * Presence Detect State bit, this helper also returns true if the Link Active
415  * bit is set.  This is a concession to broken hotplug ports which hardwire
416  * Presence Detect State to zero, such as Wilocity's [1ae9:0200].
417  *
418  * Returns: %1 if the slot is occupied and %0 if it is not. If the hotplug
419  *	    port is not present anymore returns %-ENODEV.
420  */
pciehp_card_present_or_link_active(struct controller * ctrl)421 int pciehp_card_present_or_link_active(struct controller *ctrl)
422 {
423 	int ret;
424 
425 	ret = pciehp_card_present(ctrl);
426 	if (ret)
427 		return ret;
428 
429 	return pciehp_check_link_active(ctrl);
430 }
431 
pciehp_query_power_fault(struct controller * ctrl)432 int pciehp_query_power_fault(struct controller *ctrl)
433 {
434 	struct pci_dev *pdev = ctrl_dev(ctrl);
435 	u16 slot_status;
436 
437 	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
438 	return !!(slot_status & PCI_EXP_SLTSTA_PFD);
439 }
440 
pciehp_set_raw_indicator_status(struct hotplug_slot * hotplug_slot,u8 status)441 int pciehp_set_raw_indicator_status(struct hotplug_slot *hotplug_slot,
442 				    u8 status)
443 {
444 	struct controller *ctrl = to_ctrl(hotplug_slot);
445 	struct pci_dev *pdev = ctrl_dev(ctrl);
446 
447 	pci_config_pm_runtime_get(pdev);
448 	pcie_write_cmd_nowait(ctrl, status << 6,
449 			      PCI_EXP_SLTCTL_AIC | PCI_EXP_SLTCTL_PIC);
450 	pci_config_pm_runtime_put(pdev);
451 	return 0;
452 }
453 
454 /**
455  * pciehp_set_indicators() - set attention indicator, power indicator, or both
456  * @ctrl: PCIe hotplug controller
457  * @pwr: one of:
458  *	PCI_EXP_SLTCTL_PWR_IND_ON
459  *	PCI_EXP_SLTCTL_PWR_IND_BLINK
460  *	PCI_EXP_SLTCTL_PWR_IND_OFF
461  * @attn: one of:
462  *	PCI_EXP_SLTCTL_ATTN_IND_ON
463  *	PCI_EXP_SLTCTL_ATTN_IND_BLINK
464  *	PCI_EXP_SLTCTL_ATTN_IND_OFF
465  *
466  * Either @pwr or @attn can also be INDICATOR_NOOP to leave that indicator
467  * unchanged.
468  */
pciehp_set_indicators(struct controller * ctrl,int pwr,int attn)469 void pciehp_set_indicators(struct controller *ctrl, int pwr, int attn)
470 {
471 	u16 cmd = 0, mask = 0;
472 
473 	if (PWR_LED(ctrl) && pwr != INDICATOR_NOOP) {
474 		cmd |= (pwr & PCI_EXP_SLTCTL_PIC);
475 		mask |= PCI_EXP_SLTCTL_PIC;
476 	}
477 
478 	if (ATTN_LED(ctrl) && attn != INDICATOR_NOOP) {
479 		cmd |= (attn & PCI_EXP_SLTCTL_AIC);
480 		mask |= PCI_EXP_SLTCTL_AIC;
481 	}
482 
483 	if (cmd) {
484 		pcie_write_cmd_nowait(ctrl, cmd, mask);
485 		ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
486 			 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
487 	}
488 }
489 
pciehp_power_on_slot(struct controller * ctrl)490 int pciehp_power_on_slot(struct controller *ctrl)
491 {
492 	struct pci_dev *pdev = ctrl_dev(ctrl);
493 	u16 slot_status;
494 	int retval;
495 
496 	/* Clear power-fault bit from previous power failures */
497 	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &slot_status);
498 	if (slot_status & PCI_EXP_SLTSTA_PFD)
499 		pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
500 					   PCI_EXP_SLTSTA_PFD);
501 	ctrl->power_fault_detected = 0;
502 
503 	pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_ON, PCI_EXP_SLTCTL_PCC);
504 	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
505 		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
506 		 PCI_EXP_SLTCTL_PWR_ON);
507 
508 	retval = pciehp_link_enable(ctrl);
509 	if (retval)
510 		ctrl_err(ctrl, "%s: Can not enable the link!\n", __func__);
511 
512 	return retval;
513 }
514 
pciehp_power_off_slot(struct controller * ctrl)515 void pciehp_power_off_slot(struct controller *ctrl)
516 {
517 	pcie_write_cmd(ctrl, PCI_EXP_SLTCTL_PWR_OFF, PCI_EXP_SLTCTL_PCC);
518 	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
519 		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL,
520 		 PCI_EXP_SLTCTL_PWR_OFF);
521 }
522 
pciehp_isr(int irq,void * dev_id)523 static irqreturn_t pciehp_isr(int irq, void *dev_id)
524 {
525 	struct controller *ctrl = (struct controller *)dev_id;
526 	struct pci_dev *pdev = ctrl_dev(ctrl);
527 	struct device *parent = pdev->dev.parent;
528 	u16 status, events = 0;
529 
530 	/*
531 	 * Interrupts only occur in D3hot or shallower and only if enabled
532 	 * in the Slot Control register (PCIe r4.0, sec 6.7.3.4).
533 	 */
534 	if (pdev->current_state == PCI_D3cold ||
535 	    (!(ctrl->slot_ctrl & PCI_EXP_SLTCTL_HPIE) && !pciehp_poll_mode))
536 		return IRQ_NONE;
537 
538 	/*
539 	 * Keep the port accessible by holding a runtime PM ref on its parent.
540 	 * Defer resume of the parent to the IRQ thread if it's suspended.
541 	 * Mask the interrupt until then.
542 	 */
543 	if (parent) {
544 		pm_runtime_get_noresume(parent);
545 		if (!pm_runtime_active(parent)) {
546 			pm_runtime_put(parent);
547 			disable_irq_nosync(irq);
548 			atomic_or(RERUN_ISR, &ctrl->pending_events);
549 			return IRQ_WAKE_THREAD;
550 		}
551 	}
552 
553 read_status:
554 	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &status);
555 	if (status == (u16) ~0) {
556 		ctrl_info(ctrl, "%s: no response from device\n", __func__);
557 		if (parent)
558 			pm_runtime_put(parent);
559 		return IRQ_NONE;
560 	}
561 
562 	/*
563 	 * Slot Status contains plain status bits as well as event
564 	 * notification bits; right now we only want the event bits.
565 	 */
566 	status &= PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
567 		  PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_CC |
568 		  PCI_EXP_SLTSTA_DLLSC;
569 
570 	/*
571 	 * If we've already reported a power fault, don't report it again
572 	 * until we've done something to handle it.
573 	 */
574 	if (ctrl->power_fault_detected)
575 		status &= ~PCI_EXP_SLTSTA_PFD;
576 	else if (status & PCI_EXP_SLTSTA_PFD)
577 		ctrl->power_fault_detected = true;
578 
579 	events |= status;
580 	if (!events) {
581 		if (parent)
582 			pm_runtime_put(parent);
583 		return IRQ_NONE;
584 	}
585 
586 	if (status) {
587 		pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, status);
588 
589 		/*
590 		 * In MSI mode, all event bits must be zero before the port
591 		 * will send a new interrupt (PCIe Base Spec r5.0 sec 6.7.3.4).
592 		 * So re-read the Slot Status register in case a bit was set
593 		 * between read and write.
594 		 */
595 		if (pci_dev_msi_enabled(pdev) && !pciehp_poll_mode)
596 			goto read_status;
597 	}
598 
599 	ctrl_dbg(ctrl, "pending interrupts %#06x from Slot Status\n", events);
600 	if (parent)
601 		pm_runtime_put(parent);
602 
603 	/*
604 	 * Command Completed notifications are not deferred to the
605 	 * IRQ thread because it may be waiting for their arrival.
606 	 */
607 	if (events & PCI_EXP_SLTSTA_CC) {
608 		ctrl->cmd_busy = 0;
609 		smp_mb();
610 		wake_up(&ctrl->queue);
611 
612 		if (events == PCI_EXP_SLTSTA_CC)
613 			return IRQ_HANDLED;
614 
615 		events &= ~PCI_EXP_SLTSTA_CC;
616 	}
617 
618 	if (pdev->ignore_hotplug) {
619 		ctrl_dbg(ctrl, "ignoring hotplug event %#06x\n", events);
620 		return IRQ_HANDLED;
621 	}
622 
623 	/* Save pending events for consumption by IRQ thread. */
624 	atomic_or(events, &ctrl->pending_events);
625 	return IRQ_WAKE_THREAD;
626 }
627 
pciehp_ist(int irq,void * dev_id)628 static irqreturn_t pciehp_ist(int irq, void *dev_id)
629 {
630 	struct controller *ctrl = (struct controller *)dev_id;
631 	struct pci_dev *pdev = ctrl_dev(ctrl);
632 	irqreturn_t ret;
633 	u32 events;
634 
635 	ctrl->ist_running = true;
636 	pci_config_pm_runtime_get(pdev);
637 
638 	/* rerun pciehp_isr() if the port was inaccessible on interrupt */
639 	if (atomic_fetch_and(~RERUN_ISR, &ctrl->pending_events) & RERUN_ISR) {
640 		ret = pciehp_isr(irq, dev_id);
641 		enable_irq(irq);
642 		if (ret != IRQ_WAKE_THREAD)
643 			goto out;
644 	}
645 
646 	synchronize_hardirq(irq);
647 	events = atomic_xchg(&ctrl->pending_events, 0);
648 	if (!events) {
649 		ret = IRQ_NONE;
650 		goto out;
651 	}
652 
653 	/* Check Attention Button Pressed */
654 	if (events & PCI_EXP_SLTSTA_ABP) {
655 		ctrl_info(ctrl, "Slot(%s): Attention button pressed\n",
656 			  slot_name(ctrl));
657 		pciehp_handle_button_press(ctrl);
658 	}
659 
660 	/* Check Power Fault Detected */
661 	if (events & PCI_EXP_SLTSTA_PFD) {
662 		ctrl_err(ctrl, "Slot(%s): Power fault\n", slot_name(ctrl));
663 		pciehp_set_indicators(ctrl, PCI_EXP_SLTCTL_PWR_IND_OFF,
664 				      PCI_EXP_SLTCTL_ATTN_IND_ON);
665 	}
666 
667 	/*
668 	 * Disable requests have higher priority than Presence Detect Changed
669 	 * or Data Link Layer State Changed events.
670 	 */
671 	down_read_nested(&ctrl->reset_lock, ctrl->depth);
672 	if (events & DISABLE_SLOT)
673 		pciehp_handle_disable_request(ctrl);
674 	else if (events & (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC))
675 		pciehp_handle_presence_or_link_change(ctrl, events);
676 	up_read(&ctrl->reset_lock);
677 
678 	ret = IRQ_HANDLED;
679 out:
680 	pci_config_pm_runtime_put(pdev);
681 	ctrl->ist_running = false;
682 	wake_up(&ctrl->requester);
683 	return ret;
684 }
685 
pciehp_poll(void * data)686 static int pciehp_poll(void *data)
687 {
688 	struct controller *ctrl = data;
689 
690 	schedule_timeout_idle(10 * HZ); /* start with 10 sec delay */
691 
692 	while (!kthread_should_stop()) {
693 		/* poll for interrupt events or user requests */
694 		while (pciehp_isr(IRQ_NOTCONNECTED, ctrl) == IRQ_WAKE_THREAD ||
695 		       atomic_read(&ctrl->pending_events))
696 			pciehp_ist(IRQ_NOTCONNECTED, ctrl);
697 
698 		if (pciehp_poll_time <= 0 || pciehp_poll_time > 60)
699 			pciehp_poll_time = 2; /* clamp to sane value */
700 
701 		schedule_timeout_idle(pciehp_poll_time * HZ);
702 	}
703 
704 	return 0;
705 }
706 
pcie_enable_notification(struct controller * ctrl)707 static void pcie_enable_notification(struct controller *ctrl)
708 {
709 	u16 cmd, mask;
710 
711 	/*
712 	 * TBD: Power fault detected software notification support.
713 	 *
714 	 * Power fault detected software notification is not enabled
715 	 * now, because it caused power fault detected interrupt storm
716 	 * on some machines. On those machines, power fault detected
717 	 * bit in the slot status register was set again immediately
718 	 * when it is cleared in the interrupt service routine, and
719 	 * next power fault detected interrupt was notified again.
720 	 */
721 
722 	/*
723 	 * Always enable link events: thus link-up and link-down shall
724 	 * always be treated as hotplug and unplug respectively. Enable
725 	 * presence detect only if Attention Button is not present.
726 	 */
727 	cmd = PCI_EXP_SLTCTL_DLLSCE;
728 	if (ATTN_BUTTN(ctrl))
729 		cmd |= PCI_EXP_SLTCTL_ABPE;
730 	else
731 		cmd |= PCI_EXP_SLTCTL_PDCE;
732 	if (!pciehp_poll_mode)
733 		cmd |= PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE;
734 
735 	mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
736 		PCI_EXP_SLTCTL_PFDE |
737 		PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
738 		PCI_EXP_SLTCTL_DLLSCE);
739 
740 	pcie_write_cmd_nowait(ctrl, cmd, mask);
741 	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
742 		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, cmd);
743 }
744 
pcie_disable_notification(struct controller * ctrl)745 static void pcie_disable_notification(struct controller *ctrl)
746 {
747 	u16 mask;
748 
749 	mask = (PCI_EXP_SLTCTL_PDCE | PCI_EXP_SLTCTL_ABPE |
750 		PCI_EXP_SLTCTL_MRLSCE | PCI_EXP_SLTCTL_PFDE |
751 		PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_CCIE |
752 		PCI_EXP_SLTCTL_DLLSCE);
753 	pcie_write_cmd(ctrl, 0, mask);
754 	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
755 		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
756 }
757 
pcie_clear_hotplug_events(struct controller * ctrl)758 void pcie_clear_hotplug_events(struct controller *ctrl)
759 {
760 	pcie_capability_write_word(ctrl_dev(ctrl), PCI_EXP_SLTSTA,
761 				   PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
762 }
763 
pcie_enable_interrupt(struct controller * ctrl)764 void pcie_enable_interrupt(struct controller *ctrl)
765 {
766 	u16 mask;
767 
768 	mask = PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_DLLSCE;
769 	pcie_write_cmd(ctrl, mask, mask);
770 }
771 
pcie_disable_interrupt(struct controller * ctrl)772 void pcie_disable_interrupt(struct controller *ctrl)
773 {
774 	u16 mask;
775 
776 	/*
777 	 * Mask hot-plug interrupt to prevent it triggering immediately
778 	 * when the link goes inactive (we still get PME when any of the
779 	 * enabled events is detected). Same goes with Link Layer State
780 	 * changed event which generates PME immediately when the link goes
781 	 * inactive so mask it as well.
782 	 */
783 	mask = PCI_EXP_SLTCTL_HPIE | PCI_EXP_SLTCTL_DLLSCE;
784 	pcie_write_cmd(ctrl, 0, mask);
785 }
786 
787 /*
788  * pciehp has a 1:1 bus:slot relationship so we ultimately want a secondary
789  * bus reset of the bridge, but at the same time we want to ensure that it is
790  * not seen as a hot-unplug, followed by the hot-plug of the device. Thus,
791  * disable link state notification and presence detection change notification
792  * momentarily, if we see that they could interfere. Also, clear any spurious
793  * events after.
794  */
pciehp_reset_slot(struct hotplug_slot * hotplug_slot,int probe)795 int pciehp_reset_slot(struct hotplug_slot *hotplug_slot, int probe)
796 {
797 	struct controller *ctrl = to_ctrl(hotplug_slot);
798 	struct pci_dev *pdev = ctrl_dev(ctrl);
799 	u16 stat_mask = 0, ctrl_mask = 0;
800 	int rc;
801 
802 	if (probe)
803 		return 0;
804 
805 	down_write_nested(&ctrl->reset_lock, ctrl->depth);
806 
807 	if (!ATTN_BUTTN(ctrl)) {
808 		ctrl_mask |= PCI_EXP_SLTCTL_PDCE;
809 		stat_mask |= PCI_EXP_SLTSTA_PDC;
810 	}
811 	ctrl_mask |= PCI_EXP_SLTCTL_DLLSCE;
812 	stat_mask |= PCI_EXP_SLTSTA_DLLSC;
813 
814 	pcie_write_cmd(ctrl, 0, ctrl_mask);
815 	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
816 		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, 0);
817 
818 	rc = pci_bridge_secondary_bus_reset(ctrl->pcie->port);
819 
820 	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, stat_mask);
821 	pcie_write_cmd_nowait(ctrl, ctrl_mask, ctrl_mask);
822 	ctrl_dbg(ctrl, "%s: SLOTCTRL %x write cmd %x\n", __func__,
823 		 pci_pcie_cap(ctrl->pcie->port) + PCI_EXP_SLTCTL, ctrl_mask);
824 
825 	up_write(&ctrl->reset_lock);
826 	return rc;
827 }
828 
pcie_init_notification(struct controller * ctrl)829 int pcie_init_notification(struct controller *ctrl)
830 {
831 	if (pciehp_request_irq(ctrl))
832 		return -1;
833 	pcie_enable_notification(ctrl);
834 	ctrl->notification_enabled = 1;
835 	return 0;
836 }
837 
pcie_shutdown_notification(struct controller * ctrl)838 void pcie_shutdown_notification(struct controller *ctrl)
839 {
840 	if (ctrl->notification_enabled) {
841 		pcie_disable_notification(ctrl);
842 		pciehp_free_irq(ctrl);
843 		ctrl->notification_enabled = 0;
844 	}
845 }
846 
dbg_ctrl(struct controller * ctrl)847 static inline void dbg_ctrl(struct controller *ctrl)
848 {
849 	struct pci_dev *pdev = ctrl->pcie->port;
850 	u16 reg16;
851 
852 	ctrl_dbg(ctrl, "Slot Capabilities      : 0x%08x\n", ctrl->slot_cap);
853 	pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &reg16);
854 	ctrl_dbg(ctrl, "Slot Status            : 0x%04x\n", reg16);
855 	pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &reg16);
856 	ctrl_dbg(ctrl, "Slot Control           : 0x%04x\n", reg16);
857 }
858 
859 #define FLAG(x, y)	(((x) & (y)) ? '+' : '-')
860 
pcie_hotplug_depth(struct pci_dev * dev)861 static inline int pcie_hotplug_depth(struct pci_dev *dev)
862 {
863 	struct pci_bus *bus = dev->bus;
864 	int depth = 0;
865 
866 	while (bus->parent) {
867 		bus = bus->parent;
868 		if (bus->self && bus->self->is_hotplug_bridge)
869 			depth++;
870 	}
871 
872 	return depth;
873 }
874 
pcie_init(struct pcie_device * dev)875 struct controller *pcie_init(struct pcie_device *dev)
876 {
877 	struct controller *ctrl;
878 	u32 slot_cap, link_cap;
879 	u8 poweron;
880 	struct pci_dev *pdev = dev->port;
881 	struct pci_bus *subordinate = pdev->subordinate;
882 
883 	ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
884 	if (!ctrl)
885 		return NULL;
886 
887 	ctrl->pcie = dev;
888 	ctrl->depth = pcie_hotplug_depth(dev->port);
889 	pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
890 
891 	if (pdev->hotplug_user_indicators)
892 		slot_cap &= ~(PCI_EXP_SLTCAP_AIP | PCI_EXP_SLTCAP_PIP);
893 
894 	/*
895 	 * We assume no Thunderbolt controllers support Command Complete events,
896 	 * but some controllers falsely claim they do.
897 	 */
898 	if (pdev->is_thunderbolt)
899 		slot_cap |= PCI_EXP_SLTCAP_NCCS;
900 
901 	ctrl->slot_cap = slot_cap;
902 	mutex_init(&ctrl->ctrl_lock);
903 	mutex_init(&ctrl->state_lock);
904 	init_rwsem(&ctrl->reset_lock);
905 	init_waitqueue_head(&ctrl->requester);
906 	init_waitqueue_head(&ctrl->queue);
907 	INIT_DELAYED_WORK(&ctrl->button_work, pciehp_queue_pushbutton_work);
908 	dbg_ctrl(ctrl);
909 
910 	down_read(&pci_bus_sem);
911 	ctrl->state = list_empty(&subordinate->devices) ? OFF_STATE : ON_STATE;
912 	up_read(&pci_bus_sem);
913 
914 	/* Check if Data Link Layer Link Active Reporting is implemented */
915 	pcie_capability_read_dword(pdev, PCI_EXP_LNKCAP, &link_cap);
916 
917 	/* Clear all remaining event bits in Slot Status register. */
918 	pcie_capability_write_word(pdev, PCI_EXP_SLTSTA,
919 		PCI_EXP_SLTSTA_ABP | PCI_EXP_SLTSTA_PFD |
920 		PCI_EXP_SLTSTA_MRLSC | PCI_EXP_SLTSTA_CC |
921 		PCI_EXP_SLTSTA_DLLSC | PCI_EXP_SLTSTA_PDC);
922 
923 	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",
924 		(slot_cap & PCI_EXP_SLTCAP_PSN) >> 19,
925 		FLAG(slot_cap, PCI_EXP_SLTCAP_ABP),
926 		FLAG(slot_cap, PCI_EXP_SLTCAP_PCP),
927 		FLAG(slot_cap, PCI_EXP_SLTCAP_MRLSP),
928 		FLAG(slot_cap, PCI_EXP_SLTCAP_AIP),
929 		FLAG(slot_cap, PCI_EXP_SLTCAP_PIP),
930 		FLAG(slot_cap, PCI_EXP_SLTCAP_HPC),
931 		FLAG(slot_cap, PCI_EXP_SLTCAP_HPS),
932 		FLAG(slot_cap, PCI_EXP_SLTCAP_EIP),
933 		FLAG(slot_cap, PCI_EXP_SLTCAP_NCCS),
934 		FLAG(link_cap, PCI_EXP_LNKCAP_DLLLARC),
935 		pdev->broken_cmd_compl ? " (with Cmd Compl erratum)" : "");
936 
937 	/*
938 	 * If empty slot's power status is on, turn power off.  The IRQ isn't
939 	 * requested yet, so avoid triggering a notification with this command.
940 	 */
941 	if (POWER_CTRL(ctrl)) {
942 		pciehp_get_power_status(ctrl, &poweron);
943 		if (!pciehp_card_present_or_link_active(ctrl) && poweron) {
944 			pcie_disable_notification(ctrl);
945 			pciehp_power_off_slot(ctrl);
946 		}
947 	}
948 
949 	return ctrl;
950 }
951 
pciehp_release_ctrl(struct controller * ctrl)952 void pciehp_release_ctrl(struct controller *ctrl)
953 {
954 	cancel_delayed_work_sync(&ctrl->button_work);
955 	kfree(ctrl);
956 }
957 
quirk_cmd_compl(struct pci_dev * pdev)958 static void quirk_cmd_compl(struct pci_dev *pdev)
959 {
960 	u32 slot_cap;
961 
962 	if (pci_is_pcie(pdev)) {
963 		pcie_capability_read_dword(pdev, PCI_EXP_SLTCAP, &slot_cap);
964 		if (slot_cap & PCI_EXP_SLTCAP_HPC &&
965 		    !(slot_cap & PCI_EXP_SLTCAP_NCCS))
966 			pdev->broken_cmd_compl = 1;
967 	}
968 }
969 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_INTEL, PCI_ANY_ID,
970 			      PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
971 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0110,
972 			      PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
973 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0400,
974 			      PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
975 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_QCOM, 0x0401,
976 			      PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
977 DECLARE_PCI_FIXUP_CLASS_EARLY(PCI_VENDOR_ID_HXT, 0x0401,
978 			      PCI_CLASS_BRIDGE_PCI, 8, quirk_cmd_compl);
979