Merge branches 'acpica', 'acpidump', 'intel-idle', 'misc', 'module_acpi_driver-simpli...
[firefly-linux-kernel-4.4.55.git] / arch / powerpc / platforms / pseries / eeh_event.c
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15  *
16  * Copyright (c) 2005 Linas Vepstas <linas@linas.org>
17  */
18
19 #include <linux/delay.h>
20 #include <linux/list.h>
21 #include <linux/mutex.h>
22 #include <linux/sched.h>
23 #include <linux/pci.h>
24 #include <linux/slab.h>
25 #include <linux/workqueue.h>
26 #include <asm/eeh_event.h>
27 #include <asm/ppc-pci.h>
28
29 /** Overview:
30  *  EEH error states may be detected within exception handlers;
31  *  however, the recovery processing needs to occur asynchronously
32  *  in a normal kernel context and not an interrupt context.
33  *  This pair of routines creates an event and queues it onto a
34  *  work-queue, where a worker thread can drive recovery.
35  */
36
37 /* EEH event workqueue setup. */
38 static DEFINE_SPINLOCK(eeh_eventlist_lock);
39 LIST_HEAD(eeh_eventlist);
40 static void eeh_thread_launcher(struct work_struct *);
41 DECLARE_WORK(eeh_event_wq, eeh_thread_launcher);
42
43 /* Serialize reset sequences for a given pci device */
44 DEFINE_MUTEX(eeh_event_mutex);
45
46 /**
47  * eeh_event_handler - Dispatch EEH events.
48  * @dummy - unused
49  *
50  * The detection of a frozen slot can occur inside an interrupt,
51  * where it can be hard to do anything about it.  The goal of this
52  * routine is to pull these detection events out of the context
53  * of the interrupt handler, and re-dispatch them for processing
54  * at a later time in a normal context.
55  */
56 static int eeh_event_handler(void * dummy)
57 {
58         unsigned long flags;
59         struct eeh_event *event;
60         struct eeh_dev *edev;
61
62         set_task_comm(current, "eehd");
63
64         spin_lock_irqsave(&eeh_eventlist_lock, flags);
65         event = NULL;
66
67         /* Unqueue the event, get ready to process. */
68         if (!list_empty(&eeh_eventlist)) {
69                 event = list_entry(eeh_eventlist.next, struct eeh_event, list);
70                 list_del(&event->list);
71         }
72         spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
73
74         if (event == NULL)
75                 return 0;
76
77         /* Serialize processing of EEH events */
78         mutex_lock(&eeh_event_mutex);
79         edev = event->edev;
80         eeh_mark_slot(eeh_dev_to_of_node(edev), EEH_MODE_RECOVERING);
81
82         printk(KERN_INFO "EEH: Detected PCI bus error on device %s\n",
83                eeh_pci_name(edev->pdev));
84
85         set_current_state(TASK_INTERRUPTIBLE);  /* Don't add to load average */
86         edev = handle_eeh_events(event);
87
88         if (edev) {
89                 eeh_clear_slot(eeh_dev_to_of_node(edev), EEH_MODE_RECOVERING);
90                 pci_dev_put(edev->pdev);
91         }
92
93         kfree(event);
94         mutex_unlock(&eeh_event_mutex);
95
96         /* If there are no new errors after an hour, clear the counter. */
97         if (edev && edev->freeze_count>0) {
98                 msleep_interruptible(3600*1000);
99                 if (edev->freeze_count>0)
100                         edev->freeze_count--;
101
102         }
103
104         return 0;
105 }
106
107 /**
108  * eeh_thread_launcher - Start kernel thread to handle EEH events
109  * @dummy - unused
110  *
111  * This routine is called to start the kernel thread for processing
112  * EEH event.
113  */
114 static void eeh_thread_launcher(struct work_struct *dummy)
115 {
116         if (kernel_thread(eeh_event_handler, NULL, CLONE_KERNEL) < 0)
117                 printk(KERN_ERR "Failed to start EEH daemon\n");
118 }
119
120 /**
121  * eeh_send_failure_event - Generate a PCI error event
122  * @edev: EEH device
123  *
124  * This routine can be called within an interrupt context;
125  * the actual event will be delivered in a normal context
126  * (from a workqueue).
127  */
128 int eeh_send_failure_event(struct eeh_dev *edev)
129 {
130         unsigned long flags;
131         struct eeh_event *event;
132         struct device_node *dn = eeh_dev_to_of_node(edev);
133         const char *location;
134
135         if (!mem_init_done) {
136                 printk(KERN_ERR "EEH: event during early boot not handled\n");
137                 location = of_get_property(dn, "ibm,loc-code", NULL);
138                 printk(KERN_ERR "EEH: device node = %s\n", dn->full_name);
139                 printk(KERN_ERR "EEH: PCI location = %s\n", location);
140                 return 1;
141         }
142         event = kmalloc(sizeof(*event), GFP_ATOMIC);
143         if (event == NULL) {
144                 printk(KERN_ERR "EEH: out of memory, event not handled\n");
145                 return 1;
146         }
147
148         if (edev->pdev)
149                 pci_dev_get(edev->pdev);
150
151         event->edev = edev;
152
153         /* We may or may not be called in an interrupt context */
154         spin_lock_irqsave(&eeh_eventlist_lock, flags);
155         list_add(&event->list, &eeh_eventlist);
156         spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
157
158         schedule_work(&eeh_event_wq);
159
160         return 0;
161 }