Automatic merge of /spare/repo/netdev-2.6 branch sis900
[linux-2.6] / drivers / char / snsc_event.c
1 /*
2  * SN Platform system controller communication support
3  *
4  * This file is subject to the terms and conditions of the GNU General Public
5  * License.  See the file "COPYING" in the main directory of this archive
6  * for more details.
7  *
8  * Copyright (C) 2004 Silicon Graphics, Inc. All rights reserved.
9  */
10
11 /*
12  * System controller event handler
13  *
14  * These routines deal with environmental events arriving from the
15  * system controllers.
16  */
17
18 #include <linux/interrupt.h>
19 #include <linux/sched.h>
20 #include <linux/byteorder/generic.h>
21 #include <asm/sn/sn_sal.h>
22 #include "snsc.h"
23
24 static struct subch_data_s *event_sd;
25
26 void scdrv_event(unsigned long);
27 DECLARE_TASKLET(sn_sysctl_event, scdrv_event, 0);
28
29 /*
30  * scdrv_event_interrupt
31  *
32  * Pull incoming environmental events off the physical link to the
33  * system controller and put them in a temporary holding area in SAL.
34  * Schedule scdrv_event() to move them along to their ultimate
35  * destination.
36  */
37 static irqreturn_t
38 scdrv_event_interrupt(int irq, void *subch_data, struct pt_regs *regs)
39 {
40         struct subch_data_s *sd = subch_data;
41         unsigned long flags;
42         int status;
43
44         spin_lock_irqsave(&sd->sd_rlock, flags);
45         status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch);
46
47         if ((status > 0) && (status & SAL_IROUTER_INTR_RECV)) {
48                 tasklet_schedule(&sn_sysctl_event);
49         }
50         spin_unlock_irqrestore(&sd->sd_rlock, flags);
51         return IRQ_HANDLED;
52 }
53
54
55 /*
56  * scdrv_parse_event
57  *
58  * Break an event (as read from SAL) into useful pieces so we can decide
59  * what to do with it.
60  */
61 static int
62 scdrv_parse_event(char *event, int *src, int *code, int *esp_code, char *desc)
63 {
64         char *desc_end;
65
66         /* record event source address */
67         *src = be32_to_cpup((__be32 *)event);
68         event += 4;                     /* move on to event code */
69
70         /* record the system controller's event code */
71         *code = be32_to_cpup((__be32 *)event);
72         event += 4;                     /* move on to event arguments */
73
74         /* how many arguments are in the packet? */
75         if (*event++ != 2) {
76                 /* if not 2, give up */
77                 return -1;
78         }
79
80         /* parse out the ESP code */
81         if (*event++ != IR_ARG_INT) {
82                 /* not an integer argument, so give up */
83                 return -1;
84         }
85         *esp_code = be32_to_cpup((__be32 *)event);
86         event += 4;
87
88         /* parse out the event description */
89         if (*event++ != IR_ARG_ASCII) {
90                 /* not an ASCII string, so give up */
91                 return -1;
92         }
93         event[CHUNKSIZE-1] = '\0';      /* ensure this string ends! */
94         event += 2;                     /* skip leading CR/LF */
95         desc_end = desc + sprintf(desc, "%s", event);
96
97         /* strip trailing CR/LF (if any) */
98         for (desc_end--;
99              (desc_end != desc) && ((*desc_end == 0xd) || (*desc_end == 0xa));
100              desc_end--) {
101                 *desc_end = '\0';
102         }
103
104         return 0;
105 }
106
107
108 /*
109  * scdrv_event_severity
110  *
111  * Figure out how urgent a message we should write to the console/syslog
112  * via printk.
113  */
114 static char *
115 scdrv_event_severity(int code)
116 {
117         int ev_class = (code & EV_CLASS_MASK);
118         int ev_severity = (code & EV_SEVERITY_MASK);
119         char *pk_severity = KERN_NOTICE;
120
121         switch (ev_class) {
122         case EV_CLASS_POWER:
123                 switch (ev_severity) {
124                 case EV_SEVERITY_POWER_LOW_WARNING:
125                 case EV_SEVERITY_POWER_HIGH_WARNING:
126                         pk_severity = KERN_WARNING;
127                         break;
128                 case EV_SEVERITY_POWER_HIGH_FAULT:
129                 case EV_SEVERITY_POWER_LOW_FAULT:
130                         pk_severity = KERN_ALERT;
131                         break;
132                 }
133                 break;
134         case EV_CLASS_FAN:
135                 switch (ev_severity) {
136                 case EV_SEVERITY_FAN_WARNING:
137                         pk_severity = KERN_WARNING;
138                         break;
139                 case EV_SEVERITY_FAN_FAULT:
140                         pk_severity = KERN_CRIT;
141                         break;
142                 }
143                 break;
144         case EV_CLASS_TEMP:
145                 switch (ev_severity) {
146                 case EV_SEVERITY_TEMP_ADVISORY:
147                         pk_severity = KERN_WARNING;
148                         break;
149                 case EV_SEVERITY_TEMP_CRITICAL:
150                         pk_severity = KERN_CRIT;
151                         break;
152                 case EV_SEVERITY_TEMP_FAULT:
153                         pk_severity = KERN_ALERT;
154                         break;
155                 }
156                 break;
157         case EV_CLASS_ENV:
158                 pk_severity = KERN_ALERT;
159                 break;
160         case EV_CLASS_TEST_FAULT:
161                 pk_severity = KERN_ALERT;
162                 break;
163         case EV_CLASS_TEST_WARNING:
164                 pk_severity = KERN_WARNING;
165                 break;
166         case EV_CLASS_PWRD_NOTIFY:
167                 pk_severity = KERN_ALERT;
168                 break;
169         }
170
171         return pk_severity;
172 }
173
174
175 /*
176  * scdrv_dispatch_event
177  *
178  * Do the right thing with an incoming event.  That's often nothing
179  * more than printing it to the system log.  For power-down notifications
180  * we start a graceful shutdown.
181  */
182 static void
183 scdrv_dispatch_event(char *event, int len)
184 {
185         int code, esp_code, src;
186         char desc[CHUNKSIZE];
187         char *severity;
188
189         if (scdrv_parse_event(event, &src, &code, &esp_code, desc) < 0) {
190                 /* ignore uninterpretible event */
191                 return;
192         }
193
194         /* how urgent is the message? */
195         severity = scdrv_event_severity(code);
196
197         if ((code & EV_CLASS_MASK) == EV_CLASS_PWRD_NOTIFY) {
198                 struct task_struct *p;
199
200                 /* give a SIGPWR signal to init proc */
201
202                 /* first find init's task */
203                 read_lock(&tasklist_lock);
204                 for_each_process(p) {
205                         if (p->pid == 1)
206                                 break;
207                 }
208                 if (p) { /* we found init's task */
209                         printk(KERN_EMERG "Power off indication received. Initiating power fail sequence...\n");
210                         force_sig(SIGPWR, p);
211                 } else { /* failed to find init's task - just give message(s) */
212                         printk(KERN_WARNING "Failed to find init proc to handle power off!\n");
213                         printk("%s|$(0x%x)%s\n", severity, esp_code, desc);
214                 }
215                 read_unlock(&tasklist_lock);
216         } else {
217                 /* print to system log */
218                 printk("%s|$(0x%x)%s\n", severity, esp_code, desc);
219         }
220 }
221
222
223 /*
224  * scdrv_event
225  *
226  * Called as a tasklet when an event arrives from the L1.  Read the event
227  * from where it's temporarily stored in SAL and call scdrv_dispatch_event()
228  * to send it on its way.  Keep trying to read events until SAL indicates
229  * that there are no more immediately available.
230  */
231 void
232 scdrv_event(unsigned long dummy)
233 {
234         int status;
235         int len;
236         unsigned long flags;
237         struct subch_data_s *sd = event_sd;
238
239         /* anything to read? */
240         len = CHUNKSIZE;
241         spin_lock_irqsave(&sd->sd_rlock, flags);
242         status = ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch,
243                                    sd->sd_rb, &len);
244
245         while (!(status < 0)) {
246                 spin_unlock_irqrestore(&sd->sd_rlock, flags);
247                 scdrv_dispatch_event(sd->sd_rb, len);
248                 len = CHUNKSIZE;
249                 spin_lock_irqsave(&sd->sd_rlock, flags);
250                 status = ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch,
251                                            sd->sd_rb, &len);
252         }
253         spin_unlock_irqrestore(&sd->sd_rlock, flags);
254 }
255
256
257 /*
258  * scdrv_event_init
259  *
260  * Sets up a system controller subchannel to begin receiving event
261  * messages. This is sort of a specialized version of scdrv_open()
262  * in drivers/char/sn_sysctl.c.
263  */
264 void
265 scdrv_event_init(struct sysctl_data_s *scd)
266 {
267         int rv;
268
269         event_sd = kmalloc(sizeof (struct subch_data_s), GFP_KERNEL);
270         if (event_sd == NULL) {
271                 printk(KERN_WARNING "%s: couldn't allocate subchannel info"
272                        " for event monitoring\n", __FUNCTION__);
273                 return;
274         }
275
276         /* initialize subch_data_s fields */
277         memset(event_sd, 0, sizeof (struct subch_data_s));
278         event_sd->sd_nasid = scd->scd_nasid;
279         spin_lock_init(&event_sd->sd_rlock);
280
281         /* ask the system controllers to send events to this node */
282         event_sd->sd_subch = ia64_sn_sysctl_event_init(scd->scd_nasid);
283
284         if (event_sd->sd_subch < 0) {
285                 kfree(event_sd);
286                 printk(KERN_WARNING "%s: couldn't open event subchannel\n",
287                        __FUNCTION__);
288                 return;
289         }
290
291         /* hook event subchannel up to the system controller interrupt */
292         rv = request_irq(SGI_UART_VECTOR, scdrv_event_interrupt,
293                          SA_SHIRQ | SA_INTERRUPT,
294                          "system controller events", event_sd);
295         if (rv) {
296                 printk(KERN_WARNING "%s: irq request failed (%d)\n",
297                        __FUNCTION__, rv);
298                 ia64_sn_irtr_close(event_sd->sd_nasid, event_sd->sd_subch);
299                 kfree(event_sd);
300                 return;
301         }
302 }
303
304