blob: 39bcd81e7f5d694ccb817f820ec8a57bb8c3aa93 [file] [log] [blame]
Linas Vepstas172ca922005-11-03 18:50:04 -06001/*
Linas Vepstas172ca922005-11-03 18:50:04 -06002 * 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
Linas Vepstasac325ac2006-04-18 21:05:21 -070019#include <linux/delay.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060020#include <linux/list.h>
Paul Gortmaker62fe91b2011-05-27 14:25:11 -040021#include <linux/sched.h>
Gavin Shanc8608552013-06-20 13:21:00 +080022#include <linux/semaphore.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060023#include <linux/pci.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Al Viroecf89e52012-10-02 15:32:10 -040025#include <linux/kthread.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060026#include <asm/eeh_event.h>
Linas Vepstas77bd7412005-11-03 18:52:49 -060027#include <asm/ppc-pci.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060028
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
Ingo Molnar34af9462006-06-27 02:53:55 -070037static DEFINE_SPINLOCK(eeh_eventlist_lock);
Gavin Shanc8608552013-06-20 13:21:00 +080038static struct semaphore eeh_eventlist_sem;
Linas Vepstas172ca922005-11-03 18:50:04 -060039LIST_HEAD(eeh_eventlist);
Linas Vepstas8c33fd12006-03-29 15:29:18 -060040
Linas Vepstas172ca922005-11-03 18:50:04 -060041/**
Gavin Shan29f8bf12012-02-27 20:04:02 +000042 * eeh_event_handler - Dispatch EEH events.
Linas Vepstas172ca922005-11-03 18:50:04 -060043 * @dummy - unused
Linas Vepstas8c33fd12006-03-29 15:29:18 -060044 *
45 * The detection of a frozen slot can occur inside an interrupt,
46 * where it can be hard to do anything about it. The goal of this
47 * routine is to pull these detection events out of the context
48 * of the interrupt handler, and re-dispatch them for processing
49 * at a later time in a normal context.
Linas Vepstas172ca922005-11-03 18:50:04 -060050 */
51static int eeh_event_handler(void * dummy)
52{
53 unsigned long flags;
Gavin Shan40a7cd92012-02-27 20:04:08 +000054 struct eeh_event *event;
Gavin Shan120dc492012-09-07 22:44:18 +000055 struct eeh_pe *pe;
Linas Vepstas172ca922005-11-03 18:50:04 -060056
Gavin Shanc8608552013-06-20 13:21:00 +080057 while (!kthread_should_stop()) {
58 down(&eeh_eventlist_sem);
Linas Vepstas172ca922005-11-03 18:50:04 -060059
Gavin Shanc8608552013-06-20 13:21:00 +080060 /* Fetch EEH event from the queue */
61 spin_lock_irqsave(&eeh_eventlist_lock, flags);
62 event = NULL;
63 if (!list_empty(&eeh_eventlist)) {
64 event = list_entry(eeh_eventlist.next,
65 struct eeh_event, list);
66 list_del(&event->list);
67 }
68 spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
69 if (!event)
70 continue;
Linas Vepstas77bd7412005-11-03 18:52:49 -060071
Gavin Shanc8608552013-06-20 13:21:00 +080072 /* We might have event without binding PE */
73 pe = event->pe;
74 if (pe) {
75 eeh_pe_state_mark(pe, EEH_PE_RECOVERING);
76 pr_info("EEH: Detected PCI bus error on PHB#%d-PE#%x\n",
77 pe->phb->global_number, pe->addr);
78 eeh_handle_event(pe);
79 eeh_pe_state_clear(pe, EEH_PE_RECOVERING);
80 } else {
81 eeh_handle_event(NULL);
82 }
Linas Vepstas8c33fd12006-03-29 15:29:18 -060083
Gavin Shanc8608552013-06-20 13:21:00 +080084 kfree(event);
Linas Vepstas172ca922005-11-03 18:50:04 -060085 }
86
87 return 0;
88}
89
90/**
Gavin Shanc8608552013-06-20 13:21:00 +080091 * eeh_event_init - Start kernel thread to handle EEH events
Gavin Shan29f8bf12012-02-27 20:04:02 +000092 *
93 * This routine is called to start the kernel thread for processing
94 * EEH event.
Linas Vepstas172ca922005-11-03 18:50:04 -060095 */
Gavin Shanc8608552013-06-20 13:21:00 +080096int eeh_event_init(void)
Linas Vepstas172ca922005-11-03 18:50:04 -060097{
Gavin Shanc8608552013-06-20 13:21:00 +080098 struct task_struct *t;
99 int ret = 0;
100
101 /* Initialize semaphore */
102 sema_init(&eeh_eventlist_sem, 0);
103
104 t = kthread_run(eeh_event_handler, NULL, "eehd");
105 if (IS_ERR(t)) {
106 ret = PTR_ERR(t);
107 pr_err("%s: Failed to start EEH daemon (%d)\n",
108 __func__, ret);
109 return ret;
110 }
111
112 return 0;
Linas Vepstas172ca922005-11-03 18:50:04 -0600113}
114
115/**
Gavin Shan29f8bf12012-02-27 20:04:02 +0000116 * eeh_send_failure_event - Generate a PCI error event
Gavin Shanc533b462012-09-07 22:44:11 +0000117 * @pe: EEH PE
Linas Vepstas172ca922005-11-03 18:50:04 -0600118 *
119 * This routine can be called within an interrupt context;
120 * the actual event will be delivered in a normal context
121 * (from a workqueue).
122 */
Gavin Shanc533b462012-09-07 22:44:11 +0000123int eeh_send_failure_event(struct eeh_pe *pe)
Linas Vepstas172ca922005-11-03 18:50:04 -0600124{
125 unsigned long flags;
126 struct eeh_event *event;
127
Gavin Shan7e4bbaf2012-09-07 22:44:03 +0000128 event = kzalloc(sizeof(*event), GFP_ATOMIC);
Gavin Shanc533b462012-09-07 22:44:11 +0000129 if (!event) {
130 pr_err("EEH: out of memory, event not handled\n");
131 return -ENOMEM;
132 }
133 event->pe = pe;
Linas Vepstas172ca922005-11-03 18:50:04 -0600134
135 /* We may or may not be called in an interrupt context */
136 spin_lock_irqsave(&eeh_eventlist_lock, flags);
137 list_add(&event->list, &eeh_eventlist);
138 spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
139
Gavin Shanc8608552013-06-20 13:21:00 +0800140 /* For EEH deamon to knick in */
141 up(&eeh_eventlist_sem);
Linas Vepstas172ca922005-11-03 18:50:04 -0600142
143 return 0;
144}
Gavin Shan99866592013-06-20 13:21:02 +0800145
146/**
147 * eeh_remove_event - Remove EEH event from the queue
148 * @pe: Event binding to the PE
149 *
150 * On PowerNV platform, we might have subsequent coming events
151 * is part of the former one. For that case, those subsequent
152 * coming events are totally duplicated and unnecessary, thus
153 * they should be removed.
154 */
155void eeh_remove_event(struct eeh_pe *pe)
156{
157 unsigned long flags;
158 struct eeh_event *event, *tmp;
159
160 spin_lock_irqsave(&eeh_eventlist_lock, flags);
161 list_for_each_entry_safe(event, tmp, &eeh_eventlist, list) {
162 /*
163 * If we don't have valid PE passed in, that means
164 * we already have event corresponding to dead IOC
165 * and all events should be purged.
166 */
167 if (!pe) {
168 list_del(&event->list);
169 kfree(event);
170 } else if (pe->type & EEH_PE_PHB) {
171 if (event->pe && event->pe->phb == pe->phb) {
172 list_del(&event->list);
173 kfree(event);
174 }
175 } else if (event->pe == pe) {
176 list_del(&event->list);
177 kfree(event);
178 }
179 }
180 spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
181}