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