This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / drivers / char / snsc.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 communication driver
13  *
14  * This driver allows a user process to communicate with the system
15  * controller (a.k.a. "IRouter") network in an SGI SN system.
16  */
17
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>
26 #include "snsc.h"
27
28 #define SYSCTL_BASENAME "snsc"
29
30 #define SCDRV_BUFSZ     2048
31 #define SCDRV_TIMEOUT   1000
32
33 static irqreturn_t
34 scdrv_interrupt(int irq, void *subch_data, struct pt_regs *regs)
35 {
36         struct subch_data_s *sd = subch_data;
37         unsigned long flags;
38         int status;
39
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);
43
44         if (status > 0) {
45                 if (status & SAL_IROUTER_INTR_RECV) {
46                         wake_up(&sd->sd_rq);
47                 }
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);
52                         wake_up(&sd->sd_wq);
53                 }
54         }
55         spin_unlock(&sd->sd_wlock);
56         spin_unlock_irqrestore(&sd->sd_rlock, flags);
57         return IRQ_HANDLED;
58 }
59
60 /*
61  * scdrv_open
62  *
63  * Reserve a subchannel for system controller communication.
64  */
65
66 static int
67 scdrv_open(struct inode *inode, struct file *file)
68 {
69         struct sysctl_data_s *scd;
70         struct subch_data_s *sd;
71         int rv;
72
73         /* look up device info for this device file */
74         scd = container_of(inode->i_cdev, struct sysctl_data_s, scd_cdev);
75
76         /* allocate memory for subchannel data */
77         sd = kmalloc(sizeof (struct subch_data_s), GFP_KERNEL);
78         if (sd == NULL) {
79                 printk("%s: couldn't allocate subchannel data\n",
80                        __FUNCTION__);
81                 return -ENOMEM;
82         }
83
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);
88
89         if (sd->sd_subch < 0) {
90                 kfree(sd);
91                 printk("%s: couldn't allocate subchannel\n", __FUNCTION__);
92                 return -EBUSY;
93         }
94
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);
101
102         file->private_data = sd;
103
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);
108         if (rv) {
109                 ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch);
110                 kfree(sd);
111                 printk("%s: irq request failed (%d)\n", __FUNCTION__, rv);
112                 return -EBUSY;
113         }
114
115         return 0;
116 }
117
118 /*
119  * scdrv_release
120  *
121  * Release a previously-reserved subchannel.
122  */
123
124 static int
125 scdrv_release(struct inode *inode, struct file *file)
126 {
127         struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
128         int rv;
129
130         /* free the interrupt */
131         free_irq(SGI_UART_VECTOR, sd);
132
133         /* ask SAL to close the subchannel */
134         rv = ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch);
135
136         kfree(sd);
137         return rv;
138 }
139
140 /*
141  * scdrv_read
142  *
143  * Called to read bytes from the open IRouter pipe.
144  *
145  */
146
147 static inline int
148 read_status_check(struct subch_data_s *sd, int *len)
149 {
150         return ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch, sd->sd_rb, len);
151 }
152
153 static ssize_t
154 scdrv_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos)
155 {
156         int status;
157         int len;
158         unsigned long flags;
159         struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
160
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...
165                  */
166                 if (file->f_flags & O_NONBLOCK) {
167                         return -EAGAIN;
168                 }
169                 /* ...or if we want to block, then do so here */
170                 if (down_interruptible(&sd->sd_rbs)) {
171                         /* something went wrong with wait */
172                         return -ERESTARTSYS;
173                 }
174         }
175
176         /* anything to read? */
177         len = CHUNKSIZE;
178         spin_lock_irqsave(&sd->sd_rlock, flags);
179         status = read_status_check(sd, &len);
180
181         /* if not, and we're blocking I/O, loop */
182         while (status < 0) {
183                 DECLARE_WAITQUEUE(wait, current);
184
185                 if (file->f_flags & O_NONBLOCK) {
186                         spin_unlock_irqrestore(&sd->sd_rlock, flags);
187                         up(&sd->sd_rbs);
188                         return -EAGAIN;
189                 }
190
191                 len = CHUNKSIZE;
192                 add_wait_queue(&sd->sd_rq, &wait);
193                 set_current_state(TASK_INTERRUPTIBLE);
194                 spin_unlock_irqrestore(&sd->sd_rlock, flags);
195
196                 schedule_timeout(SCDRV_TIMEOUT);
197
198                 remove_wait_queue(&sd->sd_rq, &wait);
199                 if (signal_pending(current)) {
200                         /* wait was interrupted */
201                         up(&sd->sd_rbs);
202                         return -ERESTARTSYS;
203                 }
204
205                 spin_lock_irqsave(&sd->sd_rlock, flags);
206                 status = read_status_check(sd, &len);
207         }
208         spin_unlock_irqrestore(&sd->sd_rlock, flags);
209
210         if (len > 0) {
211                 /* we read something in the last read_status_check(); copy
212                  * it out to user space
213                  */
214                 if (count < len) {
215                         pr_debug("%s: only accepting %d of %d bytes\n",
216                                  __FUNCTION__, (int) count, len);
217                 }
218                 len = min((int) count, len);
219                 if (copy_to_user(buf, sd->sd_rb, len))
220                         len = -EFAULT;
221         }
222
223         /* release the read buffer and wake anyone who might be
224          * waiting for it
225          */
226         up(&sd->sd_rbs);
227
228         /* return the number of characters read in */
229         return len;
230 }
231
232 /*
233  * scdrv_write
234  *
235  * Writes a chunk of an IRouter packet (or other system controller data)
236  * to the system controller.
237  *
238  */
239 static inline int
240 write_status_check(struct subch_data_s *sd, int count)
241 {
242         return ia64_sn_irtr_send(sd->sd_nasid, sd->sd_subch, sd->sd_wb, count);
243 }
244
245 static ssize_t
246 scdrv_write(struct file *file, const char __user *buf,
247             size_t count, loff_t *f_pos)
248 {
249         unsigned long flags;
250         int status;
251         struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
252
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...
257                  */
258                 if (file->f_flags & O_NONBLOCK) {
259                         return -EAGAIN;
260                 }
261                 /* ...or if we want to block, then do so here */
262                 if (down_interruptible(&sd->sd_wbs)) {
263                         /* something went wrong with wait */
264                         return -ERESTARTSYS;
265                 }
266         }
267
268         count = min((int) count, CHUNKSIZE);
269         if (copy_from_user(sd->sd_wb, buf, count)) {
270                 up(&sd->sd_wbs);
271                 return -EFAULT;
272         }
273
274         /* try to send the buffer */
275         spin_lock_irqsave(&sd->sd_wlock, flags);
276         status = write_status_check(sd, count);
277
278         /* if we failed, and we want to block, then loop */
279         while (status <= 0) {
280                 DECLARE_WAITQUEUE(wait, current);
281
282                 if (file->f_flags & O_NONBLOCK) {
283                         spin_unlock(&sd->sd_wlock);
284                         up(&sd->sd_wbs);
285                         return -EAGAIN;
286                 }
287
288                 add_wait_queue(&sd->sd_wq, &wait);
289                 set_current_state(TASK_INTERRUPTIBLE);
290                 spin_unlock_irqrestore(&sd->sd_wlock, flags);
291
292                 schedule_timeout(SCDRV_TIMEOUT);
293
294                 remove_wait_queue(&sd->sd_wq, &wait);
295                 if (signal_pending(current)) {
296                         /* wait was interrupted */
297                         up(&sd->sd_wbs);
298                         return -ERESTARTSYS;
299                 }
300
301                 spin_lock_irqsave(&sd->sd_wlock, flags);
302                 status = write_status_check(sd, count);
303         }
304         spin_unlock_irqrestore(&sd->sd_wlock, flags);
305
306         /* release the write buffer and wake anyone who's waiting for it */
307         up(&sd->sd_wbs);
308
309         /* return the number of characters accepted (should be the complete
310          * "chunk" as requested)
311          */
312         if ((status >= 0) && (status < count)) {
313                 pr_debug("Didn't accept the full chunk; %d of %d\n",
314                          status, (int) count);
315         }
316         return status;
317 }
318
319 static unsigned int
320 scdrv_poll(struct file *file, struct poll_table_struct *wait)
321 {
322         unsigned int mask = 0;
323         int status = 0;
324         struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
325         unsigned long flags;
326
327         poll_wait(file, &sd->sd_rq, wait);
328         poll_wait(file, &sd->sd_wq, wait);
329
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);
335
336         if (status > 0) {
337                 if (status & SAL_IROUTER_INTR_RECV) {
338                         mask |= POLLIN | POLLRDNORM;
339                 }
340                 if (status & SAL_IROUTER_INTR_XMIT) {
341                         mask |= POLLOUT | POLLWRNORM;
342                 }
343         }
344
345         return mask;
346 }
347
348 static struct file_operations scdrv_fops = {
349         .owner =        THIS_MODULE,
350         .read =         scdrv_read,
351         .write =        scdrv_write,
352         .poll =         scdrv_poll,
353         .open =         scdrv_open,
354         .release =      scdrv_release,
355 };
356
357 /*
358  * scdrv_init
359  *
360  * Called at boot time to initialize the system controller communication
361  * facility.
362  */
363 int __init
364 scdrv_init(void)
365 {
366         geoid_t geoid;
367         cmoduleid_t cmod;
368         int i;
369         char devname[32];
370         char *devnamep;
371         module_t *m;
372         struct sysctl_data_s *scd;
373         void *salbuf;
374         struct class_simple *snsc_class;
375         dev_t first_dev, dev;
376
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",
380                        __FUNCTION__);
381                 return -ENODEV;
382         }
383         snsc_class = class_simple_create(THIS_MODULE, SYSCTL_BASENAME);
384
385         for (cmod = 0; cmod < nummodules; cmod++) {
386                 m = sn_modules[cmod];
387                 for (i = 0; i <= MAX_SLABS; i++) {
388
389                         if (m->nodes[i] == -1) {
390                                 /* node is not alive in module */
391                                 continue;
392                         }
393
394                         geoid = m->geoid[i];
395                         devnamep = devname;
396                         format_module_id(devnamep, geo_module(geoid),
397                                          MODULE_FORMAT_BRIEF);
398                         devnamep = devname + strlen(devname);
399                         sprintf(devnamep, "#%d", geo_slab(geoid));
400
401                         /* allocate sysctl device data */
402                         scd = kmalloc(sizeof (struct sysctl_data_s),
403                                       GFP_KERNEL);
404                         if (!scd) {
405                                 printk("%s: failed to allocate device info"
406                                        "for %s/%s\n", __FUNCTION__,
407                                        SYSCTL_BASENAME, devname);
408                                 continue;
409                         }
410                         memset(scd, 0, sizeof (struct sysctl_data_s));
411
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);
418                                 kfree(scd);
419                                 continue;
420                         }
421
422                         if (ia64_sn_irtr_init(scd->scd_nasid, salbuf,
423                                               SCDRV_BUFSZ) < 0) {
424                                 printk
425                                     ("%s: failed to initialize SAL for"
426                                      " system controller communication"
427                                      " (%s/%s): outdated PROM?\n",
428                                      __FUNCTION__, SYSCTL_BASENAME, devname);
429                                 kfree(scd);
430                                 kfree(salbuf);
431                                 continue;
432                         }
433
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);
440                                 kfree(scd);
441                                 kfree(salbuf);
442                                 continue;
443                         }
444
445                         class_simple_device_add(snsc_class, dev, NULL,
446                                                 "%s", devname);
447
448                         ia64_sn_irtr_intr_enable(scd->scd_nasid,
449                                                  0 /*ignored */ ,
450                                                  SAL_IROUTER_INTR_RECV);
451                 }
452         }
453         return 0;
454 }
455
456 module_init(scdrv_init);