2 * SN Platform system controller communication support
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
8 * Copyright (C) 2004 Silicon Graphics, Inc. All rights reserved.
12 * System controller communication driver
14 * This driver allows a user process to communicate with the system
15 * controller (a.k.a. "IRouter") network in an SGI SN system.
18 #include <linux/interrupt.h>
19 #include <linux/sched.h>
20 #include <linux/device.h>
21 #include <linux/poll.h>
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <asm/sn/sn_sal.h>
25 #include <asm/sn/nodepda.h>
28 #define SYSCTL_BASENAME "snsc"
30 #define SCDRV_BUFSZ 2048
31 #define SCDRV_TIMEOUT 1000
34 scdrv_interrupt(int irq, void *subch_data, struct pt_regs *regs)
36 struct subch_data_s *sd = subch_data;
40 spin_lock_irqsave(&sd->sd_rlock, flags);
41 spin_lock(&sd->sd_wlock);
42 status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch);
45 if (status & SAL_IROUTER_INTR_RECV) {
48 if (status & SAL_IROUTER_INTR_XMIT) {
49 ia64_sn_irtr_intr_disable
50 (sd->sd_nasid, sd->sd_subch,
51 SAL_IROUTER_INTR_XMIT);
55 spin_unlock(&sd->sd_wlock);
56 spin_unlock_irqrestore(&sd->sd_rlock, flags);
63 * Reserve a subchannel for system controller communication.
67 scdrv_open(struct inode *inode, struct file *file)
69 struct sysctl_data_s *scd;
70 struct subch_data_s *sd;
73 /* look up device info for this device file */
74 scd = container_of(inode->i_cdev, struct sysctl_data_s, scd_cdev);
76 /* allocate memory for subchannel data */
77 sd = kmalloc(sizeof (struct subch_data_s), GFP_KERNEL);
79 printk("%s: couldn't allocate subchannel data\n",
84 /* initialize subch_data_s fields */
85 memset(sd, 0, sizeof (struct subch_data_s));
86 sd->sd_nasid = scd->scd_nasid;
87 sd->sd_subch = ia64_sn_irtr_open(scd->scd_nasid);
89 if (sd->sd_subch < 0) {
91 printk("%s: couldn't allocate subchannel\n", __FUNCTION__);
95 spin_lock_init(&sd->sd_rlock);
96 spin_lock_init(&sd->sd_wlock);
97 init_waitqueue_head(&sd->sd_rq);
98 init_waitqueue_head(&sd->sd_wq);
99 sema_init(&sd->sd_rbs, 1);
100 sema_init(&sd->sd_wbs, 1);
102 file->private_data = sd;
104 /* hook this subchannel up to the system controller interrupt */
105 rv = request_irq(SGI_UART_VECTOR, scdrv_interrupt,
106 SA_SHIRQ | SA_INTERRUPT,
107 SYSCTL_BASENAME, sd);
109 ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch);
111 printk("%s: irq request failed (%d)\n", __FUNCTION__, rv);
121 * Release a previously-reserved subchannel.
125 scdrv_release(struct inode *inode, struct file *file)
127 struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
130 /* free the interrupt */
131 free_irq(SGI_UART_VECTOR, sd);
133 /* ask SAL to close the subchannel */
134 rv = ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch);
143 * Called to read bytes from the open IRouter pipe.
148 read_status_check(struct subch_data_s *sd, int *len)
150 return ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch, sd->sd_rb, len);
154 scdrv_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos)
159 struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
161 /* try to get control of the read buffer */
162 if (down_trylock(&sd->sd_rbs)) {
163 /* somebody else has it now;
164 * if we're non-blocking, then exit...
166 if (file->f_flags & O_NONBLOCK) {
169 /* ...or if we want to block, then do so here */
170 if (down_interruptible(&sd->sd_rbs)) {
171 /* something went wrong with wait */
176 /* anything to read? */
178 spin_lock_irqsave(&sd->sd_rlock, flags);
179 status = read_status_check(sd, &len);
181 /* if not, and we're blocking I/O, loop */
183 DECLARE_WAITQUEUE(wait, current);
185 if (file->f_flags & O_NONBLOCK) {
186 spin_unlock_irqrestore(&sd->sd_rlock, flags);
192 add_wait_queue(&sd->sd_rq, &wait);
193 set_current_state(TASK_INTERRUPTIBLE);
194 spin_unlock_irqrestore(&sd->sd_rlock, flags);
196 schedule_timeout(SCDRV_TIMEOUT);
198 remove_wait_queue(&sd->sd_rq, &wait);
199 if (signal_pending(current)) {
200 /* wait was interrupted */
205 spin_lock_irqsave(&sd->sd_rlock, flags);
206 status = read_status_check(sd, &len);
208 spin_unlock_irqrestore(&sd->sd_rlock, flags);
211 /* we read something in the last read_status_check(); copy
212 * it out to user space
215 pr_debug("%s: only accepting %d of %d bytes\n",
216 __FUNCTION__, (int) count, len);
218 len = min((int) count, len);
219 if (copy_to_user(buf, sd->sd_rb, len))
223 /* release the read buffer and wake anyone who might be
228 /* return the number of characters read in */
235 * Writes a chunk of an IRouter packet (or other system controller data)
236 * to the system controller.
240 write_status_check(struct subch_data_s *sd, int count)
242 return ia64_sn_irtr_send(sd->sd_nasid, sd->sd_subch, sd->sd_wb, count);
246 scdrv_write(struct file *file, const char __user *buf,
247 size_t count, loff_t *f_pos)
251 struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
253 /* try to get control of the write buffer */
254 if (down_trylock(&sd->sd_wbs)) {
255 /* somebody else has it now;
256 * if we're non-blocking, then exit...
258 if (file->f_flags & O_NONBLOCK) {
261 /* ...or if we want to block, then do so here */
262 if (down_interruptible(&sd->sd_wbs)) {
263 /* something went wrong with wait */
268 count = min((int) count, CHUNKSIZE);
269 if (copy_from_user(sd->sd_wb, buf, count)) {
274 /* try to send the buffer */
275 spin_lock_irqsave(&sd->sd_wlock, flags);
276 status = write_status_check(sd, count);
278 /* if we failed, and we want to block, then loop */
279 while (status <= 0) {
280 DECLARE_WAITQUEUE(wait, current);
282 if (file->f_flags & O_NONBLOCK) {
283 spin_unlock(&sd->sd_wlock);
288 add_wait_queue(&sd->sd_wq, &wait);
289 set_current_state(TASK_INTERRUPTIBLE);
290 spin_unlock_irqrestore(&sd->sd_wlock, flags);
292 schedule_timeout(SCDRV_TIMEOUT);
294 remove_wait_queue(&sd->sd_wq, &wait);
295 if (signal_pending(current)) {
296 /* wait was interrupted */
301 spin_lock_irqsave(&sd->sd_wlock, flags);
302 status = write_status_check(sd, count);
304 spin_unlock_irqrestore(&sd->sd_wlock, flags);
306 /* release the write buffer and wake anyone who's waiting for it */
309 /* return the number of characters accepted (should be the complete
310 * "chunk" as requested)
312 if ((status >= 0) && (status < count)) {
313 pr_debug("Didn't accept the full chunk; %d of %d\n",
314 status, (int) count);
320 scdrv_poll(struct file *file, struct poll_table_struct *wait)
322 unsigned int mask = 0;
324 struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
327 poll_wait(file, &sd->sd_rq, wait);
328 poll_wait(file, &sd->sd_wq, wait);
330 spin_lock_irqsave(&sd->sd_rlock, flags);
331 spin_lock(&sd->sd_wlock);
332 status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch);
333 spin_unlock(&sd->sd_wlock);
334 spin_unlock_irqrestore(&sd->sd_rlock, flags);
337 if (status & SAL_IROUTER_INTR_RECV) {
338 mask |= POLLIN | POLLRDNORM;
340 if (status & SAL_IROUTER_INTR_XMIT) {
341 mask |= POLLOUT | POLLWRNORM;
348 static struct file_operations scdrv_fops = {
349 .owner = THIS_MODULE,
351 .write = scdrv_write,
354 .release = scdrv_release,
360 * Called at boot time to initialize the system controller communication
372 struct sysctl_data_s *scd;
374 struct class_simple *snsc_class;
375 dev_t first_dev, dev;
377 if (alloc_chrdev_region(&first_dev, 0, (MAX_SLABS*nummodules),
378 SYSCTL_BASENAME) < 0) {
379 printk("%s: failed to register SN system controller device\n",
383 snsc_class = class_simple_create(THIS_MODULE, SYSCTL_BASENAME);
385 for (cmod = 0; cmod < nummodules; cmod++) {
386 m = sn_modules[cmod];
387 for (i = 0; i <= MAX_SLABS; i++) {
389 if (m->nodes[i] == -1) {
390 /* node is not alive in module */
396 format_module_id(devnamep, geo_module(geoid),
397 MODULE_FORMAT_BRIEF);
398 devnamep = devname + strlen(devname);
399 sprintf(devnamep, "#%d", geo_slab(geoid));
401 /* allocate sysctl device data */
402 scd = kmalloc(sizeof (struct sysctl_data_s),
405 printk("%s: failed to allocate device info"
406 "for %s/%s\n", __FUNCTION__,
407 SYSCTL_BASENAME, devname);
410 memset(scd, 0, sizeof (struct sysctl_data_s));
412 /* initialize sysctl device data fields */
413 scd->scd_nasid = cnodeid_to_nasid(m->nodes[i]);
414 if (!(salbuf = kmalloc(SCDRV_BUFSZ, GFP_KERNEL))) {
415 printk("%s: failed to allocate driver buffer"
416 "(%s%s)\n", __FUNCTION__,
417 SYSCTL_BASENAME, devname);
422 if (ia64_sn_irtr_init(scd->scd_nasid, salbuf,
425 ("%s: failed to initialize SAL for"
426 " system controller communication"
427 " (%s/%s): outdated PROM?\n",
428 __FUNCTION__, SYSCTL_BASENAME, devname);
434 dev = first_dev + m->nodes[i];
435 cdev_init(&scd->scd_cdev, &scdrv_fops);
436 if (cdev_add(&scd->scd_cdev, dev, 1)) {
437 printk("%s: failed to register system"
438 " controller device (%s%s)\n",
439 __FUNCTION__, SYSCTL_BASENAME, devname);
445 class_simple_device_add(snsc_class, dev, NULL,
448 ia64_sn_irtr_intr_enable(scd->scd_nasid,
450 SAL_IROUTER_INTR_RECV);
456 module_init(scdrv_init);