public inbox for linux-ia64@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Altix system controller event handling
@ 2005-03-09 16:38 Greg Howard
  2005-03-09 22:31 ` Christoph Hellwig
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Greg Howard @ 2005-03-09 16:38 UTC (permalink / raw)
  To: linux-ia64

Hi Tony,

The following patch allows Altix and Altix-like systems to
handle environmental events generated by the system
controllers.  It should apply on top of Jack Steiner's patch of
3/1/05 ("New chipset support for SN platform") and Mark
Goodwin's patch of 3/8/05 ("Altix SN topology support for new
chipsets and pci topology").

Thanks - Greg

Signed-off-by: Greg Howard <ghoward@sgi.com>

Index: linux/drivers/char/Makefile
=================================--- linux.orig/drivers/char/Makefile	2005-03-07 17:39:28.630503333 -0600
+++ linux/drivers/char/Makefile	2005-03-08 11:48:54.099081064 -0600
@@ -43,7 +43,7 @@
 obj-$(CONFIG_RIO)		+= rio/ generic_serial.o
 obj-$(CONFIG_HVC_CONSOLE)	+= hvc_console.o hvsi.o
 obj-$(CONFIG_RAW_DRIVER)	+= raw.o
-obj-$(CONFIG_SGI_SNSC)		+= snsc.o
+obj-$(CONFIG_SGI_SNSC)		+= snsc.o snsc_event.o
 obj-$(CONFIG_MMTIMER)		+= mmtimer.o
 obj-$(CONFIG_VIOCONS) += viocons.o
 obj-$(CONFIG_VIOTAPE)		+= viotape.o
Index: linux/drivers/char/snsc.c
=================================--- linux.orig/drivers/char/snsc.c	2005-03-07 17:39:28.631479884 -0600
+++ linux/drivers/char/snsc.c	2005-03-08 11:48:54.120565212 -0600
@@ -366,6 +366,7 @@
 int __init
 scdrv_init(void)
 {
+	extern nasid_t master_nasid;
 	geoid_t geoid;
 	cnodeid_t cnode;
 	char devname[32];
@@ -441,6 +442,13 @@
 			ia64_sn_irtr_intr_enable(scd->scd_nasid,
 						 0 /*ignored */ ,
 						 SAL_IROUTER_INTR_RECV);
+
+                        /* on the master nasid, prepare to receive
+                         * system controller environmental events
+                         */
+                        if(scd->scd_nasid = master_nasid) {
+                                scdrv_event_init(scd);
+                        }
 	}
 	return 0;
 }
Index: linux/include/asm-ia64/sn/sn_sal.h
=================================--- linux.orig/include/asm-ia64/sn/sn_sal.h	2005-03-08 11:46:29.702169535 -0600
+++ linux/include/asm-ia64/sn/sn_sal.h	2005-03-08 11:48:54.218220431 -0600
@@ -64,6 +64,7 @@

 #define  SN_SAL_SYSCTL_IOBRICK_PCI_OP		   0x02000042	// reentrant
 #define	 SN_SAL_IROUTER_OP			   0x02000043
+#define  SN_SAL_SYSCTL_EVENT                       0x02000044
 #define  SN_SAL_IOIF_INTERRUPT			   0x0200004a
 #define  SN_SAL_HWPERF_OP			   0x02000050   // lock
 #define  SN_SAL_IOIF_ERROR_INTERRUPT		   0x02000051
@@ -850,6 +851,19 @@
 	return (int) rv.v0;
 }

+/*
+ * Set up a node as the point of contact for system controller
+ * environmental event delivery.
+ */
+static inline int
+ia64_sn_sysctl_event_init(nasid_t nasid)
+{
+        struct ia64_sal_retval rv;
+        SAL_CALL_NOLOCK(rv, SN_SAL_SYSCTL_EVENT, (u64) nasid,
+                        0, 0, 0, 0, 0, 0);
+        return (int) rv.v0;
+}
+
 /**
  * ia64_sn_get_fit_compt - read a FIT entry from the PROM header
  * @nasid: NASID of node to read
Index: linux/drivers/char/snsc.h
=================================--- linux.orig/drivers/char/snsc.h	2005-03-07 17:39:28.631479884 -0600
+++ linux/drivers/char/snsc.h	2005-03-08 11:48:54.240681131 -0600
@@ -46,5 +46,43 @@
 	struct cdev scd_cdev;	/* Character device info */
 	nasid_t scd_nasid;	/* Node on which subchannels are opened. */
 };
+/* argument types */
+#define IR_ARG_INT              0x00    /* 4-byte integer (big-endian)  */
+#define IR_ARG_ASCII            0x01    /* null-terminated ASCII string */
+#define IR_ARG_UNKNOWN          0x80    /* unknown data type.  The low
+                                         * 7 bits will contain the data
+                                         * length.                      */
+#define IR_ARG_UNKNOWN_LENGTH_MASK	0x7f
+
+
+/* system controller event codes */
+#define EV_CLASS_MASK		0xf000ul
+#define EV_SEVERITY_MASK	0x0f00ul
+#define EV_COMPONENT_MASK	0x00fful
+
+#define EV_CLASS_POWER		0x1000ul
+#define EV_CLASS_FAN		0x2000ul
+#define EV_CLASS_TEMP		0x3000ul
+#define EV_CLASS_ENV		0x4000ul
+#define EV_CLASS_TEST_FAULT	0x5000ul
+#define EV_CLASS_TEST_WARNING	0x6000ul
+#define EV_CLASS_PWRD_NOTIFY	0x8000ul
+
+#define EV_SEVERITY_POWER_STABLE	0x0000ul
+#define EV_SEVERITY_POWER_LOW_WARNING	0x0100ul
+#define EV_SEVERITY_POWER_HIGH_WARNING	0x0200ul
+#define EV_SEVERITY_POWER_HIGH_FAULT	0x0300ul
+#define EV_SEVERITY_POWER_LOW_FAULT	0x0400ul
+
+#define EV_SEVERITY_FAN_STABLE		0x0000ul
+#define EV_SEVERITY_FAN_WARNING		0x0100ul
+#define EV_SEVERITY_FAN_FAULT		0x0200ul
+
+#define EV_SEVERITY_TEMP_STABLE		0x0000ul
+#define EV_SEVERITY_TEMP_ADVISORY	0x0100ul
+#define EV_SEVERITY_TEMP_CRITICAL	0x0200ul
+#define EV_SEVERITY_TEMP_FAULT		0x0300ul
+
+void scdrv_event_init(struct sysctl_data_s *);

 #endif /* _SN_SYSCTL_H_ */
Index: linux/drivers/char/snsc_event.c
=================================--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux/drivers/char/snsc_event.c	2005-03-08 11:48:54.287555635 -0600
@@ -0,0 +1,322 @@
+/*
+ * SN Platform system controller communication support
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ * Copyright (C) 2004 Silicon Graphics, Inc. All rights reserved.
+ */
+
+/*
+ * System controller event handler
+ *
+ * These routines deal with environmental events arriving from the
+ * system controllers.
+ */
+
+#include "snsc.h"
+#include <asm/sn/sn_sal.h>
+#include <linux/interrupt.h>
+#include <linux/sched.h>
+
+static struct subch_data_s *event_sd;
+
+void scdrv_event(unsigned long);
+DECLARE_TASKLET(sn_sysctl_event, scdrv_event, 0);
+
+/*
+ * scdrv_event_interrupt
+ *
+ * Pull incoming environmental events off the physical link to the
+ * system controller and put them in a temporary holding area in SAL.
+ * Schedule scdrv_event() to move them along to their ultimate
+ * destination.
+ */
+static irqreturn_t
+scdrv_event_interrupt(int irq, void *subch_data, struct pt_regs *regs)
+{
+	struct subch_data_s *sd = (struct subch_data_s *) subch_data;
+	unsigned long flags;
+	int status;
+
+	spin_lock_irqsave(&sd->sd_rlock, flags);
+	status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch);
+
+	if ((status > 0) && (status & SAL_IROUTER_INTR_RECV)) {
+		tasklet_schedule(&sn_sysctl_event);
+	}
+	spin_unlock_irqrestore(&sd->sd_rlock, flags);
+	return IRQ_HANDLED;
+}
+
+
+/*
+ * scdrv_buffer_to_int
+ *
+ * Convert 4 bytes of big-endian integer into a little-endian integer.
+ */
+static unsigned int
+scdrv_buffer_to_int(char *buf)
+{
+	int i;
+	unsigned int n = 0;
+	for( i = 0; i < sizeof(n); i++ ) {
+		n |= ((unsigned)(*(unsigned char *)buf++)
+		      << (8 * ((sizeof(n) - i) - 1)));
+	}
+	return n;
+}
+
+
+/*
+ * scdrv_parse_event
+ *
+ * Break an event (as read from SAL) into useful pieces so we can decide
+ * what to do with it.
+ */
+static int
+scdrv_parse_event(char *event, int *src, int *code, int *esp_code, char *desc)
+{
+	char *desc_end;
+
+	/* record event source address */
+	*src = scdrv_buffer_to_int(event);
+	event += 4; 			/* move on to event code */
+
+	/* record the system controller's event code */
+	*code = scdrv_buffer_to_int(event);
+	event += 4;			/* move on to event arguments */
+
+	/* how many arguments are in the packet? */
+	if (*event++ != 2) {
+		/* if not 2, give up */
+		return -1;
+	}
+
+	/* parse out the ESP code */
+	if (*event++ != IR_ARG_INT) {
+		/* not an integer argument, so give up */
+		return -1;
+	}
+	*esp_code = scdrv_buffer_to_int(event);
+	event += 4;
+
+	/* parse out the event description */
+	if (*event++ != IR_ARG_ASCII) {
+		/* not an ASCII string, so give up */
+		return -1;
+	}
+	event[CHUNKSIZE-1] = '\0';	/* ensure this string ends! */
+	event += 2; 			/* skip leading CR/LF */
+	desc_end = desc + sprintf(desc, "%s", event);
+
+	/* strip trailing CR/LF (if any) */
+	for (desc_end--;
+	     (desc_end != desc) && ((*desc_end = 0xd) || (*desc_end = 0xa));
+	     desc_end--) {
+		*desc_end = '\0';
+	}
+
+	return 0;
+}
+
+
+/*
+ * scdrv_event_severity
+ *
+ * Figure out how urgent a message we should write to the console/syslog
+ * via printk.
+ */
+static char *
+scdrv_event_severity(int code)
+{
+	int ev_class = (code & EV_CLASS_MASK);
+	int ev_severity = (code & EV_SEVERITY_MASK);
+	char *pk_severity = KERN_NOTICE;
+
+	switch (ev_class) {
+	case EV_CLASS_POWER:
+		switch (ev_severity) {
+		case EV_SEVERITY_POWER_LOW_WARNING:
+		case EV_SEVERITY_POWER_HIGH_WARNING:
+			pk_severity = KERN_WARNING;
+			break;
+		case EV_SEVERITY_POWER_HIGH_FAULT:
+		case EV_SEVERITY_POWER_LOW_FAULT:
+			pk_severity = KERN_ALERT;
+			break;
+		}
+		break;
+	case EV_CLASS_FAN:
+		switch (ev_severity) {
+		case EV_SEVERITY_FAN_WARNING:
+			pk_severity = KERN_WARNING;
+			break;
+		case EV_SEVERITY_FAN_FAULT:
+			pk_severity = KERN_CRIT;
+			break;
+		}
+		break;
+	case EV_CLASS_TEMP:
+		switch (ev_severity) {
+		case EV_SEVERITY_TEMP_ADVISORY:
+			pk_severity = KERN_WARNING;
+			break;
+		case EV_SEVERITY_TEMP_CRITICAL:
+			pk_severity = KERN_CRIT;
+			break;
+		case EV_SEVERITY_TEMP_FAULT:
+			pk_severity = KERN_ALERT;
+			break;
+		}
+		break;
+	case EV_CLASS_ENV:
+		pk_severity = KERN_ALERT;
+		break;
+	case EV_CLASS_TEST_FAULT:
+		pk_severity = KERN_ALERT;
+		break;
+	case EV_CLASS_TEST_WARNING:
+		pk_severity = KERN_WARNING;
+		break;
+	case EV_CLASS_PWRD_NOTIFY:
+		pk_severity = KERN_ALERT;
+		break;
+	}
+
+	return pk_severity;
+}
+
+
+/*
+ * scdrv_dispatch_event
+ *
+ * Do the right thing with an incoming event.  That's often nothing
+ * more than printing it to the system log.  For power-down notifications
+ * we start a graceful shutdown.
+ */
+static void
+scdrv_dispatch_event(char *event, int len)
+{
+	int code, esp_code, src;
+	char desc[CHUNKSIZE];
+	char *severity;
+
+	if (scdrv_parse_event(event, &src, &code, &esp_code, desc) < 0) {
+		/* ignore uninterpretible event */
+		return;
+	}
+
+	/* how urgent is the message? */
+	severity = scdrv_event_severity(code);
+
+	if ((code & EV_CLASS_MASK) = EV_CLASS_PWRD_NOTIFY) {
+		struct task_struct *p;
+
+		/* give a SIGPWR signal to init proc */
+
+		/* first find init's task */
+		read_lock(&tasklist_lock);
+		for_each_process(p) {
+			if (p->pid = 1)
+				break;
+		}
+		if (p) { /* we found init's task */
+			printk(KERN_EMERG "Power off indication received. Initiating power fail sequence...\n");
+			force_sig(SIGPWR, p);
+		} else { /* failed to find init's task - just give message(s) */
+			printk(KERN_WARNING "Failed to find init proc to handle power off!\n");
+			printk("%s|$(0x%x)%s\n", severity, esp_code, desc);
+		}
+		read_unlock(&tasklist_lock);
+	} else {
+		/* print to system log */
+		printk("%s|$(0x%x)%s\n", severity, esp_code, desc);
+	}
+}
+
+
+/*
+ * scdrv_event
+ *
+ * Called as a tasklet when an event arrives from the L1.  Read the event
+ * from where it's temporarily stored in SAL and call scdrv_dispatch_event()
+ * to send it on its way.  Keep trying to read events until SAL indicates
+ * that there are no more immediately available.
+ */
+void
+scdrv_event(unsigned long dummy)
+{
+	int status;
+	int len;
+	unsigned long flags;
+	struct subch_data_s *sd = event_sd;
+
+	/* anything to read? */
+	len = CHUNKSIZE;
+	spin_lock_irqsave(&sd->sd_rlock, flags);
+	status = ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch,
+				   sd->sd_rb, &len);
+
+	while (!(status < 0)) {
+		spin_unlock_irqrestore(&sd->sd_rlock, flags);
+		scdrv_dispatch_event(sd->sd_rb, len);
+		len = CHUNKSIZE;
+		spin_lock_irqsave(&sd->sd_rlock, flags);
+		status = ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch,
+					   sd->sd_rb, &len);
+	}
+	spin_unlock_irqrestore(&sd->sd_rlock, flags);
+}
+
+
+/*
+ * scdrv_event_init
+ *
+ * Sets up a system controller subchannel to begin receiving event
+ * messages. This is sort of a specialized version of scdrv_open()
+ * in drivers/char/sn_sysctl.c.
+ */
+void
+scdrv_event_init(struct sysctl_data_s *scd)
+{
+	int rv;
+
+	event_sd = (struct subch_data_s *) kmalloc
+	  (sizeof (struct subch_data_s), GFP_KERNEL);
+	if (event_sd = NULL) {
+		printk(KERN_WARNING "%s: couldn't allocate subchannel info"
+		       " for event monitoring\n", __FUNCTION__);
+		return;
+	}
+
+	/* initialize subch_data_s fields */
+	memset(event_sd, 0, sizeof (struct subch_data_s));
+	event_sd->sd_nasid = scd->scd_nasid;
+	spin_lock_init(&event_sd->sd_rlock);
+
+	/* ask the system controllers to send events to this node */
+	event_sd->sd_subch = ia64_sn_sysctl_event_init(scd->scd_nasid);
+
+	if (event_sd->sd_subch < 0) {
+		kfree(event_sd);
+		printk(KERN_WARNING "%s: couldn't open event subchannel\n",
+		       __FUNCTION__);
+		return;
+	}
+
+	/* hook event subchannel up to the system controller interrupt */
+	rv = request_irq(SGI_UART_VECTOR, scdrv_event_interrupt,
+			 SA_SHIRQ | SA_INTERRUPT,
+			 "system controller events", event_sd);
+	if (rv) {
+		printk(KERN_WARNING "%s: irq request failed (%d)\n",
+		       __FUNCTION__, rv);
+		ia64_sn_irtr_close(event_sd->sd_nasid, event_sd->sd_subch);
+		kfree(event_sd);
+		return;
+	}
+}
+
+


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Altix system controller event handling
  2005-03-09 16:38 [PATCH] Altix system controller event handling Greg Howard
@ 2005-03-09 22:31 ` Christoph Hellwig
  2005-03-10 10:57 ` Andreas Schwab
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Christoph Hellwig @ 2005-03-09 22:31 UTC (permalink / raw)
  To: linux-ia64

> +	extern nasid_t master_nasid;

please put externs always in headers (that are included by the file
defining the blobal symbol, too)

> +#include "snsc.h"
> +#include <asm/sn/sn_sal.h>
> +#include <linux/interrupt.h>
> +#include <linux/sched.h>

header ordering is <linux/*.h> first, then <asm/*.h>, then local
headers.

> +scdrv_event_interrupt(int irq, void *subch_data, struct pt_regs *regs)
> +{
> +	struct subch_data_s *sd = (struct subch_data_s *) subch_data;

no need to cast.

> +static unsigned int
> +scdrv_buffer_to_int(char *buf)
> +{
> +	int i;
> +	unsigned int n = 0;
> +	for( i = 0; i < sizeof(n); i++ ) {
> +		n |= ((unsigned)(*(unsigned char *)buf++)
> +		      << (8 * ((sizeof(n) - i) - 1)));

urgg.  the (*(unsigned char *)buf++) should be just *(buf++), no?
address arithmetics on signed and unsigned char are the same.

So
	for (i = 0; i < sizeof(n); i++)
		n |= ((unsigned int)buf[i] << (8 * (sizeof(n) - i - 1)));

should do the same, no?

> +	event_sd = (struct subch_data_s *) kmalloc
> +	  (sizeof (struct subch_data_s), GFP_KERNEL);

no need to cast


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Altix system controller event handling
  2005-03-09 16:38 [PATCH] Altix system controller event handling Greg Howard
  2005-03-09 22:31 ` Christoph Hellwig
@ 2005-03-10 10:57 ` Andreas Schwab
  2005-03-10 16:48 ` Greg Howard
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Andreas Schwab @ 2005-03-10 10:57 UTC (permalink / raw)
  To: linux-ia64

Christoph Hellwig <hch@infradead.org> writes:

>> +static unsigned int
>> +scdrv_buffer_to_int(char *buf)
>> +{
>> +	int i;
>> +	unsigned int n = 0;
>> +	for( i = 0; i < sizeof(n); i++ ) {
>> +		n |= ((unsigned)(*(unsigned char *)buf++)
>> +		      << (8 * ((sizeof(n) - i) - 1)));
>
> urgg.  the (*(unsigned char *)buf++) should be just *(buf++), no?
> address arithmetics on signed and unsigned char are the same.

But not value promotion.  The latter gives you a different value when *buf
is negative.  For example if *buf = -1 then (unsigned)*buf = 0xffffffff,
but (unsigned char)*buf = 0xff.

> So
> 	for (i = 0; i < sizeof(n); i++)
> 		n |= ((unsigned int)buf[i] << (8 * (sizeof(n) - i - 1)));
>
> should do the same, no?

This should be better:

 	for (i = 0; i < sizeof(n); i++)
 		n |= ((unsigned char)buf[i] << (8 * (sizeof(n) - i - 1)));

Or even better: replace scdrv_buffer_to_int by be32_to_cpup.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Altix system controller event handling
  2005-03-09 16:38 [PATCH] Altix system controller event handling Greg Howard
  2005-03-09 22:31 ` Christoph Hellwig
  2005-03-10 10:57 ` Andreas Schwab
@ 2005-03-10 16:48 ` Greg Howard
  2005-03-10 17:02 ` Andreas Schwab
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Greg Howard @ 2005-03-10 16:48 UTC (permalink / raw)
  To: linux-ia64


On Thu, 10 Mar 2005, Andreas Schwab wrote:

>
> Or even better: replace scdrv_buffer_to_int by be32_to_cpup.

Sounds like a good idea.  Will be32_to_cpup work if the
argument isn't word-aligned?

Thanks - Greg


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Altix system controller event handling
  2005-03-09 16:38 [PATCH] Altix system controller event handling Greg Howard
                   ` (2 preceding siblings ...)
  2005-03-10 16:48 ` Greg Howard
@ 2005-03-10 17:02 ` Andreas Schwab
  2005-03-10 18:02 ` Greg Howard
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Andreas Schwab @ 2005-03-10 17:02 UTC (permalink / raw)
  To: linux-ia64

Greg Howard <ghoward@sgi.com> writes:

> On Thu, 10 Mar 2005, Andreas Schwab wrote:
>
>>
>> Or even better: replace scdrv_buffer_to_int by be32_to_cpup.
>
> Sounds like a good idea.  Will be32_to_cpup work if the
> argument isn't word-aligned?

Probably not, ie. it will result in an unaligned access.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux Products GmbH, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] Altix system controller event handling
  2005-03-09 16:38 [PATCH] Altix system controller event handling Greg Howard
                   ` (3 preceding siblings ...)
  2005-03-10 17:02 ` Andreas Schwab
@ 2005-03-10 18:02 ` Greg Howard
  2005-03-10 22:19 ` Christoph Hellwig
  2005-03-10 22:55 ` Greg Howard
  6 siblings, 0 replies; 8+ messages in thread
From: Greg Howard @ 2005-03-10 18:02 UTC (permalink / raw)
  To: linux-ia64

Hello again,

The following is an update of the patch I sent yesterday
(3/9/05) incorporating suggestions from Christoph Hellwig and
Andreas Schwab.  It allows Altix and Altix-like systems to
handle environmental events generated by the system controllers,
and should apply on top of Jack Steiner's patch of 3/1/05 ("New
chipset support for SN platform") and Mark Goodwin's patch of
3/8/05 ("Altix SN topology support for new chipsets and pci
topology").

Thanks - Greg

Signed-off-by: Greg Howard <ghoward@sgi.com>

Index: linux/drivers/char/Makefile
=================================--- linux.orig/drivers/char/Makefile	2005-03-10 10:21:22.000000000 -0600
+++ linux/drivers/char/Makefile	2005-03-10 10:21:24.000000000 -0600
@@ -43,7 +43,7 @@
 obj-$(CONFIG_RIO)		+= rio/ generic_serial.o
 obj-$(CONFIG_HVC_CONSOLE)	+= hvc_console.o hvsi.o
 obj-$(CONFIG_RAW_DRIVER)	+= raw.o
-obj-$(CONFIG_SGI_SNSC)		+= snsc.o
+obj-$(CONFIG_SGI_SNSC)		+= snsc.o snsc_event.o
 obj-$(CONFIG_MMTIMER)		+= mmtimer.o
 obj-$(CONFIG_VIOCONS) += viocons.o
 obj-$(CONFIG_VIOTAPE)		+= viotape.o
Index: linux/drivers/char/snsc.c
=================================--- linux.orig/drivers/char/snsc.c	2005-03-10 10:21:22.000000000 -0600
+++ linux/drivers/char/snsc.c	2005-03-10 10:21:24.000000000 -0600
@@ -374,6 +374,7 @@
 	void *salbuf;
 	struct class_simple *snsc_class;
 	dev_t first_dev, dev;
+	nasid_t event_nasid = ia64_sn_get_console_nasid();

 	if (alloc_chrdev_region(&first_dev, 0, numionodes,
 				SYSCTL_BASENAME) < 0) {
@@ -441,6 +442,13 @@
 			ia64_sn_irtr_intr_enable(scd->scd_nasid,
 						 0 /*ignored */ ,
 						 SAL_IROUTER_INTR_RECV);
+
+                        /* on the console nasid, prepare to receive
+                         * system controller environmental events
+                         */
+                        if(scd->scd_nasid = event_nasid) {
+                                scdrv_event_init(scd);
+                        }
 	}
 	return 0;
 }
Index: linux/include/asm-ia64/sn/sn_sal.h
=================================--- linux.orig/include/asm-ia64/sn/sn_sal.h	2005-03-10 10:21:22.000000000 -0600
+++ linux/include/asm-ia64/sn/sn_sal.h	2005-03-10 11:54:02.000000000 -0600
@@ -64,6 +64,7 @@

 #define  SN_SAL_SYSCTL_IOBRICK_PCI_OP		   0x02000042	// reentrant
 #define	 SN_SAL_IROUTER_OP			   0x02000043
+#define  SN_SAL_SYSCTL_EVENT                       0x02000044
 #define  SN_SAL_IOIF_INTERRUPT			   0x0200004a
 #define  SN_SAL_HWPERF_OP			   0x02000050   // lock
 #define  SN_SAL_IOIF_ERROR_INTERRUPT		   0x02000051
@@ -850,6 +851,19 @@
 	return (int) rv.v0;
 }

+/*
+ * Set up a node as the point of contact for system controller
+ * environmental event delivery.
+ */
+static inline int
+ia64_sn_sysctl_event_init(nasid_t nasid)
+{
+        struct ia64_sal_retval rv;
+        SAL_CALL_REENTRANT(rv, SN_SAL_SYSCTL_EVENT, (u64) nasid,
+			   0, 0, 0, 0, 0, 0);
+        return (int) rv.v0;
+}
+
 /**
  * ia64_sn_get_fit_compt - read a FIT entry from the PROM header
  * @nasid: NASID of node to read
Index: linux/drivers/char/snsc.h
=================================--- linux.orig/drivers/char/snsc.h	2005-03-10 10:21:22.000000000 -0600
+++ linux/drivers/char/snsc.h	2005-03-10 11:42:34.000000000 -0600
@@ -47,4 +47,44 @@
 	nasid_t scd_nasid;	/* Node on which subchannels are opened. */
 };

+
+/* argument types */
+#define IR_ARG_INT              0x00    /* 4-byte integer (big-endian)  */
+#define IR_ARG_ASCII            0x01    /* null-terminated ASCII string */
+#define IR_ARG_UNKNOWN          0x80    /* unknown data type.  The low
+                                         * 7 bits will contain the data
+                                         * length.                      */
+#define IR_ARG_UNKNOWN_LENGTH_MASK	0x7f
+
+
+/* system controller event codes */
+#define EV_CLASS_MASK		0xf000ul
+#define EV_SEVERITY_MASK	0x0f00ul
+#define EV_COMPONENT_MASK	0x00fful
+
+#define EV_CLASS_POWER		0x1000ul
+#define EV_CLASS_FAN		0x2000ul
+#define EV_CLASS_TEMP		0x3000ul
+#define EV_CLASS_ENV		0x4000ul
+#define EV_CLASS_TEST_FAULT	0x5000ul
+#define EV_CLASS_TEST_WARNING	0x6000ul
+#define EV_CLASS_PWRD_NOTIFY	0x8000ul
+
+#define EV_SEVERITY_POWER_STABLE	0x0000ul
+#define EV_SEVERITY_POWER_LOW_WARNING	0x0100ul
+#define EV_SEVERITY_POWER_HIGH_WARNING	0x0200ul
+#define EV_SEVERITY_POWER_HIGH_FAULT	0x0300ul
+#define EV_SEVERITY_POWER_LOW_FAULT	0x0400ul
+
+#define EV_SEVERITY_FAN_STABLE		0x0000ul
+#define EV_SEVERITY_FAN_WARNING		0x0100ul
+#define EV_SEVERITY_FAN_FAULT		0x0200ul
+
+#define EV_SEVERITY_TEMP_STABLE		0x0000ul
+#define EV_SEVERITY_TEMP_ADVISORY	0x0100ul
+#define EV_SEVERITY_TEMP_CRITICAL	0x0200ul
+#define EV_SEVERITY_TEMP_FAULT		0x0300ul
+
+void scdrv_event_init(struct sysctl_data_s *);
+
 #endif /* _SN_SYSCTL_H_ */
Index: linux/drivers/char/snsc_event.c
=================================--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ linux/drivers/char/snsc_event.c	2005-03-10 10:40:08.000000000 -0600
@@ -0,0 +1,304 @@
+/*
+ * SN Platform system controller communication support
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License.  See the file "COPYING" in the main directory of this archive
+ * for more details.
+ *
+ * Copyright (C) 2004 Silicon Graphics, Inc. All rights reserved.
+ */
+
+/*
+ * System controller event handler
+ *
+ * These routines deal with environmental events arriving from the
+ * system controllers.
+ */
+
+#include <linux/interrupt.h>
+#include <linux/sched.h>
+#include <linux/byteorder/generic.h>
+#include <asm/sn/sn_sal.h>
+#include "snsc.h"
+
+static struct subch_data_s *event_sd;
+
+void scdrv_event(unsigned long);
+DECLARE_TASKLET(sn_sysctl_event, scdrv_event, 0);
+
+/*
+ * scdrv_event_interrupt
+ *
+ * Pull incoming environmental events off the physical link to the
+ * system controller and put them in a temporary holding area in SAL.
+ * Schedule scdrv_event() to move them along to their ultimate
+ * destination.
+ */
+static irqreturn_t
+scdrv_event_interrupt(int irq, void *subch_data, struct pt_regs *regs)
+{
+	struct subch_data_s *sd = subch_data;
+	unsigned long flags;
+	int status;
+
+	spin_lock_irqsave(&sd->sd_rlock, flags);
+	status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch);
+
+	if ((status > 0) && (status & SAL_IROUTER_INTR_RECV)) {
+		tasklet_schedule(&sn_sysctl_event);
+	}
+	spin_unlock_irqrestore(&sd->sd_rlock, flags);
+	return IRQ_HANDLED;
+}
+
+
+/*
+ * scdrv_parse_event
+ *
+ * Break an event (as read from SAL) into useful pieces so we can decide
+ * what to do with it.
+ */
+static int
+scdrv_parse_event(char *event, int *src, int *code, int *esp_code, char *desc)
+{
+	char *desc_end;
+
+	/* record event source address */
+	*src = be32_to_cpup((__be32 *)event);
+	event += 4; 			/* move on to event code */
+
+	/* record the system controller's event code */
+	*code = be32_to_cpup((__be32 *)event);
+	event += 4;			/* move on to event arguments */
+
+	/* how many arguments are in the packet? */
+	if (*event++ != 2) {
+		/* if not 2, give up */
+		return -1;
+	}
+
+	/* parse out the ESP code */
+	if (*event++ != IR_ARG_INT) {
+		/* not an integer argument, so give up */
+		return -1;
+	}
+	*esp_code = be32_to_cpup((__be32 *)event);
+	event += 4;
+
+	/* parse out the event description */
+	if (*event++ != IR_ARG_ASCII) {
+		/* not an ASCII string, so give up */
+		return -1;
+	}
+	event[CHUNKSIZE-1] = '\0';	/* ensure this string ends! */
+	event += 2; 			/* skip leading CR/LF */
+	desc_end = desc + sprintf(desc, "%s", event);
+
+	/* strip trailing CR/LF (if any) */
+	for (desc_end--;
+	     (desc_end != desc) && ((*desc_end = 0xd) || (*desc_end = 0xa));
+	     desc_end--) {
+		*desc_end = '\0';
+	}
+
+	return 0;
+}
+
+
+/*
+ * scdrv_event_severity
+ *
+ * Figure out how urgent a message we should write to the console/syslog
+ * via printk.
+ */
+static char *
+scdrv_event_severity(int code)
+{
+	int ev_class = (code & EV_CLASS_MASK);
+	int ev_severity = (code & EV_SEVERITY_MASK);
+	char *pk_severity = KERN_NOTICE;
+
+	switch (ev_class) {
+	case EV_CLASS_POWER:
+		switch (ev_severity) {
+		case EV_SEVERITY_POWER_LOW_WARNING:
+		case EV_SEVERITY_POWER_HIGH_WARNING:
+			pk_severity = KERN_WARNING;
+			break;
+		case EV_SEVERITY_POWER_HIGH_FAULT:
+		case EV_SEVERITY_POWER_LOW_FAULT:
+			pk_severity = KERN_ALERT;
+			break;
+		}
+		break;
+	case EV_CLASS_FAN:
+		switch (ev_severity) {
+		case EV_SEVERITY_FAN_WARNING:
+			pk_severity = KERN_WARNING;
+			break;
+		case EV_SEVERITY_FAN_FAULT:
+			pk_severity = KERN_CRIT;
+			break;
+		}
+		break;
+	case EV_CLASS_TEMP:
+		switch (ev_severity) {
+		case EV_SEVERITY_TEMP_ADVISORY:
+			pk_severity = KERN_WARNING;
+			break;
+		case EV_SEVERITY_TEMP_CRITICAL:
+			pk_severity = KERN_CRIT;
+			break;
+		case EV_SEVERITY_TEMP_FAULT:
+			pk_severity = KERN_ALERT;
+			break;
+		}
+		break;
+	case EV_CLASS_ENV:
+		pk_severity = KERN_ALERT;
+		break;
+	case EV_CLASS_TEST_FAULT:
+		pk_severity = KERN_ALERT;
+		break;
+	case EV_CLASS_TEST_WARNING:
+		pk_severity = KERN_WARNING;
+		break;
+	case EV_CLASS_PWRD_NOTIFY:
+		pk_severity = KERN_ALERT;
+		break;
+	}
+
+	return pk_severity;
+}
+
+
+/*
+ * scdrv_dispatch_event
+ *
+ * Do the right thing with an incoming event.  That's often nothing
+ * more than printing it to the system log.  For power-down notifications
+ * we start a graceful shutdown.
+ */
+static void
+scdrv_dispatch_event(char *event, int len)
+{
+	int code, esp_code, src;
+	char desc[CHUNKSIZE];
+	char *severity;
+
+	if (scdrv_parse_event(event, &src, &code, &esp_code, desc) < 0) {
+		/* ignore uninterpretible event */
+		return;
+	}
+
+	/* how urgent is the message? */
+	severity = scdrv_event_severity(code);
+
+	if ((code & EV_CLASS_MASK) = EV_CLASS_PWRD_NOTIFY) {
+		struct task_struct *p;
+
+		/* give a SIGPWR signal to init proc */
+
+		/* first find init's task */
+		read_lock(&tasklist_lock);
+		for_each_process(p) {
+			if (p->pid = 1)
+				break;
+		}
+		if (p) { /* we found init's task */
+			printk(KERN_EMERG "Power off indication received. Initiating power fail sequence...\n");
+			force_sig(SIGPWR, p);
+		} else { /* failed to find init's task - just give message(s) */
+			printk(KERN_WARNING "Failed to find init proc to handle power off!\n");
+			printk("%s|$(0x%x)%s\n", severity, esp_code, desc);
+		}
+		read_unlock(&tasklist_lock);
+	} else {
+		/* print to system log */
+		printk("%s|$(0x%x)%s\n", severity, esp_code, desc);
+	}
+}
+
+
+/*
+ * scdrv_event
+ *
+ * Called as a tasklet when an event arrives from the L1.  Read the event
+ * from where it's temporarily stored in SAL and call scdrv_dispatch_event()
+ * to send it on its way.  Keep trying to read events until SAL indicates
+ * that there are no more immediately available.
+ */
+void
+scdrv_event(unsigned long dummy)
+{
+	int status;
+	int len;
+	unsigned long flags;
+	struct subch_data_s *sd = event_sd;
+
+	/* anything to read? */
+	len = CHUNKSIZE;
+	spin_lock_irqsave(&sd->sd_rlock, flags);
+	status = ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch,
+				   sd->sd_rb, &len);
+
+	while (!(status < 0)) {
+		spin_unlock_irqrestore(&sd->sd_rlock, flags);
+		scdrv_dispatch_event(sd->sd_rb, len);
+		len = CHUNKSIZE;
+		spin_lock_irqsave(&sd->sd_rlock, flags);
+		status = ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch,
+					   sd->sd_rb, &len);
+	}
+	spin_unlock_irqrestore(&sd->sd_rlock, flags);
+}
+
+
+/*
+ * scdrv_event_init
+ *
+ * Sets up a system controller subchannel to begin receiving event
+ * messages. This is sort of a specialized version of scdrv_open()
+ * in drivers/char/sn_sysctl.c.
+ */
+void
+scdrv_event_init(struct sysctl_data_s *scd)
+{
+	int rv;
+
+	event_sd = kmalloc(sizeof (struct subch_data_s), GFP_KERNEL);
+	if (event_sd = NULL) {
+		printk(KERN_WARNING "%s: couldn't allocate subchannel info"
+		       " for event monitoring\n", __FUNCTION__);
+		return;
+	}
+
+	/* initialize subch_data_s fields */
+	memset(event_sd, 0, sizeof (struct subch_data_s));
+	event_sd->sd_nasid = scd->scd_nasid;
+	spin_lock_init(&event_sd->sd_rlock);
+
+	/* ask the system controllers to send events to this node */
+	event_sd->sd_subch = ia64_sn_sysctl_event_init(scd->scd_nasid);
+
+	if (event_sd->sd_subch < 0) {
+		kfree(event_sd);
+		printk(KERN_WARNING "%s: couldn't open event subchannel\n",
+		       __FUNCTION__);
+		return;
+	}
+
+	/* hook event subchannel up to the system controller interrupt */
+	rv = request_irq(SGI_UART_VECTOR, scdrv_event_interrupt,
+			 SA_SHIRQ | SA_INTERRUPT,
+			 "system controller events", event_sd);
+	if (rv) {
+		printk(KERN_WARNING "%s: irq request failed (%d)\n",
+		       __FUNCTION__, rv);
+		ia64_sn_irtr_close(event_sd->sd_nasid, event_sd->sd_subch);
+		kfree(event_sd);
+		return;
+	}
+}
+
+

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Altix system controller event handling
  2005-03-09 16:38 [PATCH] Altix system controller event handling Greg Howard
                   ` (4 preceding siblings ...)
  2005-03-10 18:02 ` Greg Howard
@ 2005-03-10 22:19 ` Christoph Hellwig
  2005-03-10 22:55 ` Greg Howard
  6 siblings, 0 replies; 8+ messages in thread
From: Christoph Hellwig @ 2005-03-10 22:19 UTC (permalink / raw)
  To: linux-ia64

On Thu, Mar 10, 2005 at 06:02:33PM +0100, Andreas Schwab wrote:
> Greg Howard <ghoward@sgi.com> writes:
> 
> > On Thu, 10 Mar 2005, Andreas Schwab wrote:
> >
> >>
> >> Or even better: replace scdrv_buffer_to_int by be32_to_cpup.
> >
> > Sounds like a good idea.  Will be32_to_cpup work if the
> > argument isn't word-aligned?
> 
> Probably not, ie. it will result in an unaligned access.

but we have get_unaligned() for that..

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Altix system controller event handling
  2005-03-09 16:38 [PATCH] Altix system controller event handling Greg Howard
                   ` (5 preceding siblings ...)
  2005-03-10 22:19 ` Christoph Hellwig
@ 2005-03-10 22:55 ` Greg Howard
  6 siblings, 0 replies; 8+ messages in thread
From: Greg Howard @ 2005-03-10 22:55 UTC (permalink / raw)
  To: linux-ia64


On Thu, 10 Mar 2005, Andreas Schwab wrote:

> Greg Howard <ghoward@sgi.com> writes:
>
> > On Thu, 10 Mar 2005, Andreas Schwab wrote:
> >
> >>
> >> Or even better: replace scdrv_buffer_to_int by be32_to_cpup.
> >
> > Sounds like a good idea.  Will be32_to_cpup work if the
> > argument isn't word-aligned?
>
> Probably not, ie. it will result in an unaligned access.

Hmmm...  Well, for what it's worth I went ahead and tried it,
and it worked.  Looking at the code and how my test behaved (it
worked correctly and interpreted an entire event buffer), I
don't see anyway I could have *avoided* passing a
non-word-aligned pointer to be32_to_cpup at some point in the
byte stream-- but I didn't see any hiccups, still less an
unaligned access error.  Maybe the compiler is doing me a favor
behind the scenes...?  Anyhow, it seems to work, so unless
you or others have misgivings I'm inclined to let the revised
patch (as I sent it earlier today) stand.

Thanks all for your help.
- Greg

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2005-03-10 22:55 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-03-09 16:38 [PATCH] Altix system controller event handling Greg Howard
2005-03-09 22:31 ` Christoph Hellwig
2005-03-10 10:57 ` Andreas Schwab
2005-03-10 16:48 ` Greg Howard
2005-03-10 17:02 ` Andreas Schwab
2005-03-10 18:02 ` Greg Howard
2005-03-10 22:19 ` Christoph Hellwig
2005-03-10 22:55 ` Greg Howard

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox