blob: 40020c65c89e4da0e2365fac47316593ee712c12 [file] [log] [blame]
Linas Vepstas172ca922005-11-03 18:50:04 -06001/*
2 * eeh_event.c
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Copyright (c) 2005 Linas Vepstas <linas@linas.org>
19 */
20
21#include <linux/list.h>
Linas Vepstas8c33fd112006-03-29 15:29:18 -060022#include <linux/mutex.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060023#include <linux/pci.h>
Linas Vepstas8c33fd112006-03-29 15:29:18 -060024#include <linux/workqueue.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060025#include <asm/eeh_event.h>
Linas Vepstas77bd7412005-11-03 18:52:49 -060026#include <asm/ppc-pci.h>
Linas Vepstas172ca922005-11-03 18:50:04 -060027
28/** Overview:
29 * EEH error states may be detected within exception handlers;
30 * however, the recovery processing needs to occur asynchronously
31 * in a normal kernel context and not an interrupt context.
32 * This pair of routines creates an event and queues it onto a
33 * work-queue, where a worker thread can drive recovery.
34 */
35
36/* EEH event workqueue setup. */
37static spinlock_t eeh_eventlist_lock = SPIN_LOCK_UNLOCKED;
38LIST_HEAD(eeh_eventlist);
39static void eeh_thread_launcher(void *);
40DECLARE_WORK(eeh_event_wq, eeh_thread_launcher, NULL);
41
Linas Vepstas8c33fd112006-03-29 15:29:18 -060042/* Serialize reset sequences for a given pci device */
43DEFINE_MUTEX(eeh_event_mutex);
44
Linas Vepstas172ca922005-11-03 18:50:04 -060045/**
Linas Vepstas8c33fd112006-03-29 15:29:18 -060046 * eeh_event_handler - dispatch EEH events.
Linas Vepstas172ca922005-11-03 18:50:04 -060047 * @dummy - unused
Linas Vepstas8c33fd112006-03-29 15:29:18 -060048 *
49 * The detection of a frozen slot can occur inside an interrupt,
50 * where it can be hard to do anything about it. The goal of this
51 * routine is to pull these detection events out of the context
52 * of the interrupt handler, and re-dispatch them for processing
53 * at a later time in a normal context.
Linas Vepstas172ca922005-11-03 18:50:04 -060054 */
55static int eeh_event_handler(void * dummy)
56{
57 unsigned long flags;
58 struct eeh_event *event;
59
60 daemonize ("eehd");
61
62 while (1) {
63 set_current_state(TASK_INTERRUPTIBLE);
64
65 spin_lock_irqsave(&eeh_eventlist_lock, flags);
66 event = NULL;
Linas Vepstas77bd7412005-11-03 18:52:49 -060067
68 /* Unqueue the event, get ready to process. */
Linas Vepstas172ca922005-11-03 18:50:04 -060069 if (!list_empty(&eeh_eventlist)) {
70 event = list_entry(eeh_eventlist.next, struct eeh_event, list);
71 list_del(&event->list);
72 }
73 spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
Linas Vepstas8c33fd112006-03-29 15:29:18 -060074
Linas Vepstas172ca922005-11-03 18:50:04 -060075 if (event == NULL)
76 break;
77
Linas Vepstas8c33fd112006-03-29 15:29:18 -060078 /* Serialize processing of EEH events */
79 mutex_lock(&eeh_event_mutex);
80 eeh_mark_slot(event->dn, EEH_MODE_RECOVERING);
81
Linas Vepstas172ca922005-11-03 18:50:04 -060082 printk(KERN_INFO "EEH: Detected PCI bus error on device %s\n",
83 pci_name(event->dev));
84
Linas Vepstas77bd7412005-11-03 18:52:49 -060085 handle_eeh_events(event);
Linas Vepstas172ca922005-11-03 18:50:04 -060086
Linas Vepstas77bd7412005-11-03 18:52:49 -060087 eeh_clear_slot(event->dn, EEH_MODE_RECOVERING);
Linas Vepstas77bd7412005-11-03 18:52:49 -060088 pci_dev_put(event->dev);
Linas Vepstas172ca922005-11-03 18:50:04 -060089 kfree(event);
Linas Vepstas8c33fd112006-03-29 15:29:18 -060090 mutex_unlock(&eeh_event_mutex);
Linas Vepstas172ca922005-11-03 18:50:04 -060091 }
92
93 return 0;
94}
95
96/**
97 * eeh_thread_launcher
Linas Vepstas172ca922005-11-03 18:50:04 -060098 * @dummy - unused
99 */
100static void eeh_thread_launcher(void *dummy)
101{
102 if (kernel_thread(eeh_event_handler, NULL, CLONE_KERNEL) < 0)
103 printk(KERN_ERR "Failed to start EEH daemon\n");
104}
105
106/**
107 * eeh_send_failure_event - generate a PCI error event
108 * @dev pci device
109 *
110 * This routine can be called within an interrupt context;
111 * the actual event will be delivered in a normal context
112 * (from a workqueue).
113 */
114int eeh_send_failure_event (struct device_node *dn,
115 struct pci_dev *dev,
Linas Vepstas77bd7412005-11-03 18:52:49 -0600116 enum pci_channel_state state,
Linas Vepstas172ca922005-11-03 18:50:04 -0600117 int time_unavail)
118{
119 unsigned long flags;
120 struct eeh_event *event;
Linas Vepstas054d8ff2006-04-27 02:31:20 -0700121 char *location;
Linas Vepstas172ca922005-11-03 18:50:04 -0600122
Linas Vepstas054d8ff2006-04-27 02:31:20 -0700123 if (!mem_init_done) {
124 printk(KERN_ERR "EEH: event during early boot not handled\n");
125 location = (char *) get_property(dn, "ibm,loc-code", NULL);
126 printk(KERN_ERR "EEH: device node = %s\n", dn->full_name);
127 printk(KERN_ERR "EEH: PCI location = %s\n", location);
128 return 1;
129 }
Linas Vepstas172ca922005-11-03 18:50:04 -0600130 event = kmalloc(sizeof(*event), GFP_ATOMIC);
131 if (event == NULL) {
132 printk (KERN_ERR "EEH: out of memory, event not handled\n");
133 return 1;
134 }
135
136 if (dev)
137 pci_dev_get(dev);
138
139 event->dn = dn;
140 event->dev = dev;
141 event->state = state;
142 event->time_unavail = time_unavail;
143
144 /* We may or may not be called in an interrupt context */
145 spin_lock_irqsave(&eeh_eventlist_lock, flags);
146 list_add(&event->list, &eeh_eventlist);
147 spin_unlock_irqrestore(&eeh_eventlist_lock, flags);
148
149 schedule_work(&eeh_event_wq);
150
151 return 0;
152}
153
154/********************** END OF FILE ******************************/