Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 1 | /* |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 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 | |
Linas Vepstas | ac325ac | 2006-04-18 21:05:21 -0700 | [diff] [blame] | 19 | #include <linux/delay.h> |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 20 | #include <linux/list.h> |
Paul Gortmaker | 62fe91b | 2011-05-27 14:25:11 -0400 | [diff] [blame] | 21 | #include <linux/sched.h> |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 22 | #include <linux/semaphore.h> |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 23 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 24 | #include <linux/slab.h> |
Al Viro | ecf89e5 | 2012-10-02 15:32:10 -0400 | [diff] [blame] | 25 | #include <linux/kthread.h> |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 26 | #include <asm/eeh_event.h> |
Linas Vepstas | 77bd741 | 2005-11-03 18:52:49 -0600 | [diff] [blame] | 27 | #include <asm/ppc-pci.h> |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 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 | |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 37 | static DEFINE_SPINLOCK(eeh_eventlist_lock); |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 38 | static struct semaphore eeh_eventlist_sem; |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 39 | LIST_HEAD(eeh_eventlist); |
Linas Vepstas | 8c33fd1 | 2006-03-29 15:29:18 -0600 | [diff] [blame] | 40 | |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 41 | /** |
Gavin Shan | 29f8bf1 | 2012-02-27 20:04:02 +0000 | [diff] [blame] | 42 | * eeh_event_handler - Dispatch EEH events. |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 43 | * @dummy - unused |
Linas Vepstas | 8c33fd1 | 2006-03-29 15:29:18 -0600 | [diff] [blame] | 44 | * |
| 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 Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 50 | */ |
| 51 | static int eeh_event_handler(void * dummy) |
| 52 | { |
| 53 | unsigned long flags; |
Gavin Shan | 40a7cd9 | 2012-02-27 20:04:08 +0000 | [diff] [blame] | 54 | struct eeh_event *event; |
Gavin Shan | 120dc49 | 2012-09-07 22:44:18 +0000 | [diff] [blame] | 55 | struct eeh_pe *pe; |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 56 | |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 57 | while (!kthread_should_stop()) { |
Gavin Shan | 5459ae1 | 2013-06-25 14:35:28 +0800 | [diff] [blame] | 58 | if (down_interruptible(&eeh_eventlist_sem)) |
| 59 | break; |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 60 | |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 61 | /* Fetch EEH event from the queue */ |
| 62 | spin_lock_irqsave(&eeh_eventlist_lock, flags); |
| 63 | event = NULL; |
| 64 | if (!list_empty(&eeh_eventlist)) { |
| 65 | event = list_entry(eeh_eventlist.next, |
| 66 | struct eeh_event, list); |
| 67 | list_del(&event->list); |
| 68 | } |
| 69 | spin_unlock_irqrestore(&eeh_eventlist_lock, flags); |
| 70 | if (!event) |
| 71 | continue; |
Linas Vepstas | 77bd741 | 2005-11-03 18:52:49 -0600 | [diff] [blame] | 72 | |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 73 | /* We might have event without binding PE */ |
| 74 | pe = event->pe; |
| 75 | if (pe) { |
| 76 | eeh_pe_state_mark(pe, EEH_PE_RECOVERING); |
| 77 | pr_info("EEH: Detected PCI bus error on PHB#%d-PE#%x\n", |
| 78 | pe->phb->global_number, pe->addr); |
| 79 | eeh_handle_event(pe); |
| 80 | eeh_pe_state_clear(pe, EEH_PE_RECOVERING); |
| 81 | } else { |
| 82 | eeh_handle_event(NULL); |
| 83 | } |
Linas Vepstas | 8c33fd1 | 2006-03-29 15:29:18 -0600 | [diff] [blame] | 84 | |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 85 | kfree(event); |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | /** |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 92 | * eeh_event_init - Start kernel thread to handle EEH events |
Gavin Shan | 29f8bf1 | 2012-02-27 20:04:02 +0000 | [diff] [blame] | 93 | * |
| 94 | * This routine is called to start the kernel thread for processing |
| 95 | * EEH event. |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 96 | */ |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 97 | int eeh_event_init(void) |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 98 | { |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 99 | struct task_struct *t; |
| 100 | int ret = 0; |
| 101 | |
| 102 | /* Initialize semaphore */ |
| 103 | sema_init(&eeh_eventlist_sem, 0); |
| 104 | |
| 105 | t = kthread_run(eeh_event_handler, NULL, "eehd"); |
| 106 | if (IS_ERR(t)) { |
| 107 | ret = PTR_ERR(t); |
| 108 | pr_err("%s: Failed to start EEH daemon (%d)\n", |
| 109 | __func__, ret); |
| 110 | return ret; |
| 111 | } |
| 112 | |
| 113 | return 0; |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
Gavin Shan | 29f8bf1 | 2012-02-27 20:04:02 +0000 | [diff] [blame] | 117 | * eeh_send_failure_event - Generate a PCI error event |
Gavin Shan | c533b46 | 2012-09-07 22:44:11 +0000 | [diff] [blame] | 118 | * @pe: EEH PE |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 119 | * |
| 120 | * This routine can be called within an interrupt context; |
| 121 | * the actual event will be delivered in a normal context |
| 122 | * (from a workqueue). |
| 123 | */ |
Gavin Shan | c533b46 | 2012-09-07 22:44:11 +0000 | [diff] [blame] | 124 | int eeh_send_failure_event(struct eeh_pe *pe) |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 125 | { |
| 126 | unsigned long flags; |
| 127 | struct eeh_event *event; |
| 128 | |
Gavin Shan | 7e4bbaf | 2012-09-07 22:44:03 +0000 | [diff] [blame] | 129 | event = kzalloc(sizeof(*event), GFP_ATOMIC); |
Gavin Shan | c533b46 | 2012-09-07 22:44:11 +0000 | [diff] [blame] | 130 | if (!event) { |
| 131 | pr_err("EEH: out of memory, event not handled\n"); |
| 132 | return -ENOMEM; |
| 133 | } |
| 134 | event->pe = pe; |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 135 | |
| 136 | /* We may or may not be called in an interrupt context */ |
| 137 | spin_lock_irqsave(&eeh_eventlist_lock, flags); |
| 138 | list_add(&event->list, &eeh_eventlist); |
| 139 | spin_unlock_irqrestore(&eeh_eventlist_lock, flags); |
| 140 | |
Gavin Shan | c860855 | 2013-06-20 13:21:00 +0800 | [diff] [blame] | 141 | /* For EEH deamon to knick in */ |
| 142 | up(&eeh_eventlist_sem); |
Linas Vepstas | 172ca92 | 2005-11-03 18:50:04 -0600 | [diff] [blame] | 143 | |
| 144 | return 0; |
| 145 | } |
Gavin Shan | 9986659 | 2013-06-20 13:21:02 +0800 | [diff] [blame] | 146 | |
| 147 | /** |
| 148 | * eeh_remove_event - Remove EEH event from the queue |
| 149 | * @pe: Event binding to the PE |
| 150 | * |
| 151 | * On PowerNV platform, we might have subsequent coming events |
| 152 | * is part of the former one. For that case, those subsequent |
| 153 | * coming events are totally duplicated and unnecessary, thus |
| 154 | * they should be removed. |
| 155 | */ |
| 156 | void eeh_remove_event(struct eeh_pe *pe) |
| 157 | { |
| 158 | unsigned long flags; |
| 159 | struct eeh_event *event, *tmp; |
| 160 | |
| 161 | spin_lock_irqsave(&eeh_eventlist_lock, flags); |
| 162 | list_for_each_entry_safe(event, tmp, &eeh_eventlist, list) { |
| 163 | /* |
| 164 | * If we don't have valid PE passed in, that means |
| 165 | * we already have event corresponding to dead IOC |
| 166 | * and all events should be purged. |
| 167 | */ |
| 168 | if (!pe) { |
| 169 | list_del(&event->list); |
| 170 | kfree(event); |
| 171 | } else if (pe->type & EEH_PE_PHB) { |
| 172 | if (event->pe && event->pe->phb == pe->phb) { |
| 173 | list_del(&event->list); |
| 174 | kfree(event); |
| 175 | } |
| 176 | } else if (event->pe == pe) { |
| 177 | list_del(&event->list); |
| 178 | kfree(event); |
| 179 | } |
| 180 | } |
| 181 | spin_unlock_irqrestore(&eeh_eventlist_lock, flags); |
| 182 | } |