xref: /openbmc/linux/arch/powerpc/kernel/eeh_event.c (revision 68701780712f7ddb2fa81032aa1b4a949949ddf8)
1317f06deSGavin Shan /*
2317f06deSGavin Shan  * This program is free software; you can redistribute it and/or modify
3317f06deSGavin Shan  * it under the terms of the GNU General Public License as published by
4317f06deSGavin Shan  * the Free Software Foundation; either version 2 of the License, or
5317f06deSGavin Shan  * (at your option) any later version.
6317f06deSGavin Shan  *
7317f06deSGavin Shan  * This program is distributed in the hope that it will be useful,
8317f06deSGavin Shan  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9317f06deSGavin Shan  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10317f06deSGavin Shan  * GNU General Public License for more details.
11317f06deSGavin Shan  *
12317f06deSGavin Shan  * You should have received a copy of the GNU General Public License
13317f06deSGavin Shan  * along with this program; if not, write to the Free Software
14317f06deSGavin Shan  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15317f06deSGavin Shan  *
16317f06deSGavin Shan  * Copyright (c) 2005 Linas Vepstas <linas@linas.org>
17317f06deSGavin Shan  */
18317f06deSGavin Shan 
19317f06deSGavin Shan #include <linux/delay.h>
20317f06deSGavin Shan #include <linux/list.h>
21317f06deSGavin Shan #include <linux/sched.h>
22c8608558SGavin Shan #include <linux/semaphore.h>
23317f06deSGavin Shan #include <linux/pci.h>
24317f06deSGavin Shan #include <linux/slab.h>
25317f06deSGavin Shan #include <linux/kthread.h>
26317f06deSGavin Shan #include <asm/eeh_event.h>
27317f06deSGavin Shan #include <asm/ppc-pci.h>
28317f06deSGavin Shan 
29317f06deSGavin Shan /** Overview:
30317f06deSGavin Shan  *  EEH error states may be detected within exception handlers;
31317f06deSGavin Shan  *  however, the recovery processing needs to occur asynchronously
32317f06deSGavin Shan  *  in a normal kernel context and not an interrupt context.
33317f06deSGavin Shan  *  This pair of routines creates an event and queues it onto a
34317f06deSGavin Shan  *  work-queue, where a worker thread can drive recovery.
35317f06deSGavin Shan  */
36317f06deSGavin Shan 
37317f06deSGavin Shan static DEFINE_SPINLOCK(eeh_eventlist_lock);
38c8608558SGavin Shan static struct semaphore eeh_eventlist_sem;
39635218c7SDaniel Axtens static LIST_HEAD(eeh_eventlist);
40317f06deSGavin Shan 
41317f06deSGavin Shan /**
42317f06deSGavin Shan  * eeh_event_handler - Dispatch EEH events.
43317f06deSGavin Shan  * @dummy - unused
44317f06deSGavin Shan  *
45317f06deSGavin Shan  * The detection of a frozen slot can occur inside an interrupt,
46317f06deSGavin Shan  * where it can be hard to do anything about it.  The goal of this
47317f06deSGavin Shan  * routine is to pull these detection events out of the context
48317f06deSGavin Shan  * of the interrupt handler, and re-dispatch them for processing
49317f06deSGavin Shan  * at a later time in a normal context.
50317f06deSGavin Shan  */
51317f06deSGavin Shan static int eeh_event_handler(void * dummy)
52317f06deSGavin Shan {
53317f06deSGavin Shan 	unsigned long flags;
54317f06deSGavin Shan 	struct eeh_event *event;
55317f06deSGavin Shan 	struct eeh_pe *pe;
56317f06deSGavin Shan 
57c8608558SGavin Shan 	while (!kthread_should_stop()) {
585459ae14SGavin Shan 		if (down_interruptible(&eeh_eventlist_sem))
595459ae14SGavin Shan 			break;
60c8608558SGavin Shan 
61c8608558SGavin Shan 		/* Fetch EEH event from the queue */
62317f06deSGavin Shan 		spin_lock_irqsave(&eeh_eventlist_lock, flags);
63317f06deSGavin Shan 		event = NULL;
64317f06deSGavin Shan 		if (!list_empty(&eeh_eventlist)) {
65c8608558SGavin Shan 			event = list_entry(eeh_eventlist.next,
66c8608558SGavin Shan 					   struct eeh_event, list);
67317f06deSGavin Shan 			list_del(&event->list);
68317f06deSGavin Shan 		}
69317f06deSGavin Shan 		spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
70c8608558SGavin Shan 		if (!event)
71c8608558SGavin Shan 			continue;
72317f06deSGavin Shan 
73c8608558SGavin Shan 		/* We might have event without binding PE */
74317f06deSGavin Shan 		pe = event->pe;
75c8608558SGavin Shan 		if (pe) {
76317f06deSGavin Shan 			eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
770b5381a6SGavin Shan 			if (pe->type & EEH_PE_PHB)
781f52f176SRussell Currey 				pr_info("EEH: Detected error on PHB#%x\n",
790b5381a6SGavin Shan 					 pe->phb->global_number);
800b5381a6SGavin Shan 			else
810b5381a6SGavin Shan 				pr_info("EEH: Detected PCI bus error on "
821f52f176SRussell Currey 					"PHB#%x-PE#%x\n",
83317f06deSGavin Shan 					pe->phb->global_number, pe->addr);
84*68701780SSam Bobroff 			eeh_handle_normal_event(pe);
85317f06deSGavin Shan 			eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
86c8608558SGavin Shan 		} else {
87*68701780SSam Bobroff 			eeh_handle_special_event();
88c8608558SGavin Shan 		}
89317f06deSGavin Shan 
90317f06deSGavin Shan 		kfree(event);
91317f06deSGavin Shan 	}
92317f06deSGavin Shan 
93317f06deSGavin Shan 	return 0;
94317f06deSGavin Shan }
95317f06deSGavin Shan 
96317f06deSGavin Shan /**
97c8608558SGavin Shan  * eeh_event_init - Start kernel thread to handle EEH events
98317f06deSGavin Shan  *
99317f06deSGavin Shan  * This routine is called to start the kernel thread for processing
100317f06deSGavin Shan  * EEH event.
101317f06deSGavin Shan  */
102c8608558SGavin Shan int eeh_event_init(void)
103317f06deSGavin Shan {
104c8608558SGavin Shan 	struct task_struct *t;
105c8608558SGavin Shan 	int ret = 0;
106c8608558SGavin Shan 
107c8608558SGavin Shan 	/* Initialize semaphore */
108c8608558SGavin Shan 	sema_init(&eeh_eventlist_sem, 0);
109c8608558SGavin Shan 
110c8608558SGavin Shan 	t = kthread_run(eeh_event_handler, NULL, "eehd");
111c8608558SGavin Shan 	if (IS_ERR(t)) {
112c8608558SGavin Shan 		ret = PTR_ERR(t);
113c8608558SGavin Shan 		pr_err("%s: Failed to start EEH daemon (%d)\n",
114c8608558SGavin Shan 			__func__, ret);
115c8608558SGavin Shan 		return ret;
116c8608558SGavin Shan 	}
117c8608558SGavin Shan 
118c8608558SGavin Shan 	return 0;
119317f06deSGavin Shan }
120317f06deSGavin Shan 
121317f06deSGavin Shan /**
122317f06deSGavin Shan  * eeh_send_failure_event - Generate a PCI error event
123317f06deSGavin Shan  * @pe: EEH PE
124317f06deSGavin Shan  *
125317f06deSGavin Shan  * This routine can be called within an interrupt context;
126317f06deSGavin Shan  * the actual event will be delivered in a normal context
127317f06deSGavin Shan  * (from a workqueue).
128317f06deSGavin Shan  */
129317f06deSGavin Shan int eeh_send_failure_event(struct eeh_pe *pe)
130317f06deSGavin Shan {
131317f06deSGavin Shan 	unsigned long flags;
132317f06deSGavin Shan 	struct eeh_event *event;
133317f06deSGavin Shan 
134317f06deSGavin Shan 	event = kzalloc(sizeof(*event), GFP_ATOMIC);
135317f06deSGavin Shan 	if (!event) {
136317f06deSGavin Shan 		pr_err("EEH: out of memory, event not handled\n");
137317f06deSGavin Shan 		return -ENOMEM;
138317f06deSGavin Shan 	}
139317f06deSGavin Shan 	event->pe = pe;
140317f06deSGavin Shan 
141317f06deSGavin Shan 	/* We may or may not be called in an interrupt context */
142317f06deSGavin Shan 	spin_lock_irqsave(&eeh_eventlist_lock, flags);
143317f06deSGavin Shan 	list_add(&event->list, &eeh_eventlist);
144317f06deSGavin Shan 	spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
145317f06deSGavin Shan 
146c8608558SGavin Shan 	/* For EEH deamon to knick in */
147c8608558SGavin Shan 	up(&eeh_eventlist_sem);
148317f06deSGavin Shan 
149317f06deSGavin Shan 	return 0;
150317f06deSGavin Shan }
15199866595SGavin Shan 
15299866595SGavin Shan /**
15399866595SGavin Shan  * eeh_remove_event - Remove EEH event from the queue
15499866595SGavin Shan  * @pe: Event binding to the PE
1555c7a35e3SGavin Shan  * @force: Event will be removed unconditionally
15699866595SGavin Shan  *
15799866595SGavin Shan  * On PowerNV platform, we might have subsequent coming events
15899866595SGavin Shan  * is part of the former one. For that case, those subsequent
15999866595SGavin Shan  * coming events are totally duplicated and unnecessary, thus
16099866595SGavin Shan  * they should be removed.
16199866595SGavin Shan  */
1625c7a35e3SGavin Shan void eeh_remove_event(struct eeh_pe *pe, bool force)
16399866595SGavin Shan {
16499866595SGavin Shan 	unsigned long flags;
16599866595SGavin Shan 	struct eeh_event *event, *tmp;
16699866595SGavin Shan 
1675c7a35e3SGavin Shan 	/*
1685c7a35e3SGavin Shan 	 * If we have NULL PE passed in, we have dead IOC
1695c7a35e3SGavin Shan 	 * or we're sure we can report all existing errors
1705c7a35e3SGavin Shan 	 * by the caller.
1715c7a35e3SGavin Shan 	 *
1725c7a35e3SGavin Shan 	 * With "force", the event with associated PE that
1735c7a35e3SGavin Shan 	 * have been isolated, the event won't be removed
1745c7a35e3SGavin Shan 	 * to avoid event lost.
1755c7a35e3SGavin Shan 	 */
17699866595SGavin Shan 	spin_lock_irqsave(&eeh_eventlist_lock, flags);
17799866595SGavin Shan 	list_for_each_entry_safe(event, tmp, &eeh_eventlist, list) {
1785c7a35e3SGavin Shan 		if (!force && event->pe &&
1795c7a35e3SGavin Shan 		    (event->pe->state & EEH_PE_ISOLATED))
1805c7a35e3SGavin Shan 			continue;
1815c7a35e3SGavin Shan 
18299866595SGavin Shan 		if (!pe) {
18399866595SGavin Shan 			list_del(&event->list);
18499866595SGavin Shan 			kfree(event);
18599866595SGavin Shan 		} else if (pe->type & EEH_PE_PHB) {
18699866595SGavin Shan 			if (event->pe && event->pe->phb == pe->phb) {
18799866595SGavin Shan 				list_del(&event->list);
18899866595SGavin Shan 				kfree(event);
18999866595SGavin Shan 			}
19099866595SGavin Shan 		} else if (event->pe == pe) {
19199866595SGavin Shan 			list_del(&event->list);
19299866595SGavin Shan 			kfree(event);
19399866595SGavin Shan 		}
19499866595SGavin Shan 	}
19599866595SGavin Shan 	spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
19699866595SGavin Shan }
197