blob: 0b799ac1b04957f501d4c50539f58e7ee28199cd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Jes Sorensen40953ed2006-03-23 03:00:49 -08008 * Copyright (C) 2004, 2006 Silicon Graphics, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
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>
Jonathan Corbetc43ef172008-05-15 16:44:14 -060024#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <asm/sn/io.h>
26#include <asm/sn/sn_sal.h>
27#include <asm/sn/module.h>
28#include <asm/sn/geo.h>
29#include <asm/sn/nodepda.h>
30#include "snsc.h"
31
32#define SYSCTL_BASENAME "snsc"
33
34#define SCDRV_BUFSZ 2048
35#define SCDRV_TIMEOUT 1000
36
37static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +010038scdrv_interrupt(int irq, void *subch_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
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 spin_lock(&sd->sd_wlock);
46 status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch);
47
48 if (status > 0) {
49 if (status & SAL_IROUTER_INTR_RECV) {
50 wake_up(&sd->sd_rq);
51 }
52 if (status & SAL_IROUTER_INTR_XMIT) {
53 ia64_sn_irtr_intr_disable
54 (sd->sd_nasid, sd->sd_subch,
55 SAL_IROUTER_INTR_XMIT);
56 wake_up(&sd->sd_wq);
57 }
58 }
59 spin_unlock(&sd->sd_wlock);
60 spin_unlock_irqrestore(&sd->sd_rlock, flags);
61 return IRQ_HANDLED;
62}
63
64/*
65 * scdrv_open
66 *
67 * Reserve a subchannel for system controller communication.
68 */
69
70static int
71scdrv_open(struct inode *inode, struct file *file)
72{
73 struct sysctl_data_s *scd;
74 struct subch_data_s *sd;
75 int rv;
76
77 /* look up device info for this device file */
78 scd = container_of(inode->i_cdev, struct sysctl_data_s, scd_cdev);
79
80 /* allocate memory for subchannel data */
Jes Sorensen40953ed2006-03-23 03:00:49 -080081 sd = kzalloc(sizeof (struct subch_data_s), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (sd == NULL) {
83 printk("%s: couldn't allocate subchannel data\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -070084 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 return -ENOMEM;
86 }
87
88 /* initialize subch_data_s fields */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 sd->sd_nasid = scd->scd_nasid;
90 sd->sd_subch = ia64_sn_irtr_open(scd->scd_nasid);
91
92 if (sd->sd_subch < 0) {
93 kfree(sd);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -070094 printk("%s: couldn't allocate subchannel\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 return -EBUSY;
96 }
97
98 spin_lock_init(&sd->sd_rlock);
99 spin_lock_init(&sd->sd_wlock);
100 init_waitqueue_head(&sd->sd_rq);
101 init_waitqueue_head(&sd->sd_wq);
102 sema_init(&sd->sd_rbs, 1);
103 sema_init(&sd->sd_wbs, 1);
104
105 file->private_data = sd;
106
107 /* hook this subchannel up to the system controller interrupt */
Jonathan Corbetc43ef172008-05-15 16:44:14 -0600108 lock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 rv = request_irq(SGI_UART_VECTOR, scdrv_interrupt,
Thomas Gleixner0f2ed4c2006-07-01 19:29:33 -0700110 IRQF_SHARED | IRQF_DISABLED,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 SYSCTL_BASENAME, sd);
112 if (rv) {
113 ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch);
114 kfree(sd);
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700115 printk("%s: irq request failed (%d)\n", __func__, rv);
Jonathan Corbetc43ef172008-05-15 16:44:14 -0600116 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return -EBUSY;
118 }
Jonathan Corbetc43ef172008-05-15 16:44:14 -0600119 unlock_kernel();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 return 0;
121}
122
123/*
124 * scdrv_release
125 *
126 * Release a previously-reserved subchannel.
127 */
128
129static int
130scdrv_release(struct inode *inode, struct file *file)
131{
132 struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
133 int rv;
134
135 /* free the interrupt */
136 free_irq(SGI_UART_VECTOR, sd);
137
138 /* ask SAL to close the subchannel */
139 rv = ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch);
140
141 kfree(sd);
142 return rv;
143}
144
145/*
146 * scdrv_read
147 *
148 * Called to read bytes from the open IRouter pipe.
149 *
150 */
151
152static inline int
153read_status_check(struct subch_data_s *sd, int *len)
154{
155 return ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch, sd->sd_rb, len);
156}
157
158static ssize_t
159scdrv_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos)
160{
161 int status;
162 int len;
163 unsigned long flags;
164 struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
165
166 /* try to get control of the read buffer */
167 if (down_trylock(&sd->sd_rbs)) {
168 /* somebody else has it now;
169 * if we're non-blocking, then exit...
170 */
171 if (file->f_flags & O_NONBLOCK) {
172 return -EAGAIN;
173 }
174 /* ...or if we want to block, then do so here */
175 if (down_interruptible(&sd->sd_rbs)) {
176 /* something went wrong with wait */
177 return -ERESTARTSYS;
178 }
179 }
180
181 /* anything to read? */
182 len = CHUNKSIZE;
183 spin_lock_irqsave(&sd->sd_rlock, flags);
184 status = read_status_check(sd, &len);
185
186 /* if not, and we're blocking I/O, loop */
187 while (status < 0) {
188 DECLARE_WAITQUEUE(wait, current);
189
190 if (file->f_flags & O_NONBLOCK) {
191 spin_unlock_irqrestore(&sd->sd_rlock, flags);
192 up(&sd->sd_rbs);
193 return -EAGAIN;
194 }
195
196 len = CHUNKSIZE;
197 set_current_state(TASK_INTERRUPTIBLE);
198 add_wait_queue(&sd->sd_rq, &wait);
199 spin_unlock_irqrestore(&sd->sd_rlock, flags);
200
201 schedule_timeout(SCDRV_TIMEOUT);
202
203 remove_wait_queue(&sd->sd_rq, &wait);
204 if (signal_pending(current)) {
205 /* wait was interrupted */
206 up(&sd->sd_rbs);
207 return -ERESTARTSYS;
208 }
209
210 spin_lock_irqsave(&sd->sd_rlock, flags);
211 status = read_status_check(sd, &len);
212 }
213 spin_unlock_irqrestore(&sd->sd_rlock, flags);
214
215 if (len > 0) {
216 /* we read something in the last read_status_check(); copy
217 * it out to user space
218 */
219 if (count < len) {
220 pr_debug("%s: only accepting %d of %d bytes\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700221 __func__, (int) count, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223 len = min((int) count, len);
224 if (copy_to_user(buf, sd->sd_rb, len))
225 len = -EFAULT;
226 }
227
228 /* release the read buffer and wake anyone who might be
229 * waiting for it
230 */
231 up(&sd->sd_rbs);
232
233 /* return the number of characters read in */
234 return len;
235}
236
237/*
238 * scdrv_write
239 *
240 * Writes a chunk of an IRouter packet (or other system controller data)
241 * to the system controller.
242 *
243 */
244static inline int
245write_status_check(struct subch_data_s *sd, int count)
246{
247 return ia64_sn_irtr_send(sd->sd_nasid, sd->sd_subch, sd->sd_wb, count);
248}
249
250static ssize_t
251scdrv_write(struct file *file, const char __user *buf,
252 size_t count, loff_t *f_pos)
253{
254 unsigned long flags;
255 int status;
256 struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
257
258 /* try to get control of the write buffer */
259 if (down_trylock(&sd->sd_wbs)) {
260 /* somebody else has it now;
261 * if we're non-blocking, then exit...
262 */
263 if (file->f_flags & O_NONBLOCK) {
264 return -EAGAIN;
265 }
266 /* ...or if we want to block, then do so here */
267 if (down_interruptible(&sd->sd_wbs)) {
268 /* something went wrong with wait */
269 return -ERESTARTSYS;
270 }
271 }
272
273 count = min((int) count, CHUNKSIZE);
274 if (copy_from_user(sd->sd_wb, buf, count)) {
275 up(&sd->sd_wbs);
276 return -EFAULT;
277 }
278
279 /* try to send the buffer */
280 spin_lock_irqsave(&sd->sd_wlock, flags);
281 status = write_status_check(sd, count);
282
283 /* if we failed, and we want to block, then loop */
284 while (status <= 0) {
285 DECLARE_WAITQUEUE(wait, current);
286
287 if (file->f_flags & O_NONBLOCK) {
288 spin_unlock(&sd->sd_wlock);
289 up(&sd->sd_wbs);
290 return -EAGAIN;
291 }
292
293 set_current_state(TASK_INTERRUPTIBLE);
294 add_wait_queue(&sd->sd_wq, &wait);
295 spin_unlock_irqrestore(&sd->sd_wlock, flags);
296
297 schedule_timeout(SCDRV_TIMEOUT);
298
299 remove_wait_queue(&sd->sd_wq, &wait);
300 if (signal_pending(current)) {
301 /* wait was interrupted */
302 up(&sd->sd_wbs);
303 return -ERESTARTSYS;
304 }
305
306 spin_lock_irqsave(&sd->sd_wlock, flags);
307 status = write_status_check(sd, count);
308 }
309 spin_unlock_irqrestore(&sd->sd_wlock, flags);
310
311 /* release the write buffer and wake anyone who's waiting for it */
312 up(&sd->sd_wbs);
313
314 /* return the number of characters accepted (should be the complete
315 * "chunk" as requested)
316 */
317 if ((status >= 0) && (status < count)) {
318 pr_debug("Didn't accept the full chunk; %d of %d\n",
319 status, (int) count);
320 }
321 return status;
322}
323
324static unsigned int
325scdrv_poll(struct file *file, struct poll_table_struct *wait)
326{
327 unsigned int mask = 0;
328 int status = 0;
329 struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
330 unsigned long flags;
331
332 poll_wait(file, &sd->sd_rq, wait);
333 poll_wait(file, &sd->sd_wq, wait);
334
335 spin_lock_irqsave(&sd->sd_rlock, flags);
336 spin_lock(&sd->sd_wlock);
337 status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch);
338 spin_unlock(&sd->sd_wlock);
339 spin_unlock_irqrestore(&sd->sd_rlock, flags);
340
341 if (status > 0) {
342 if (status & SAL_IROUTER_INTR_RECV) {
343 mask |= POLLIN | POLLRDNORM;
344 }
345 if (status & SAL_IROUTER_INTR_XMIT) {
346 mask |= POLLOUT | POLLWRNORM;
347 }
348 }
349
350 return mask;
351}
352
Arjan van de Ven62322d22006-07-03 00:24:21 -0700353static const struct file_operations scdrv_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 .owner = THIS_MODULE,
355 .read = scdrv_read,
356 .write = scdrv_write,
357 .poll = scdrv_poll,
358 .open = scdrv_open,
359 .release = scdrv_release,
360};
361
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800362static struct class *snsc_class;
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364/*
365 * scdrv_init
366 *
367 * Called at boot time to initialize the system controller communication
368 * facility.
369 */
370int __init
371scdrv_init(void)
372{
373 geoid_t geoid;
374 cnodeid_t cnode;
375 char devname[32];
376 char *devnamep;
377 struct sysctl_data_s *scd;
378 void *salbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 dev_t first_dev, dev;
Greg Edwardsc7c17422006-07-28 10:03:55 -0500380 nasid_t event_nasid;
381
382 if (!ia64_platform_is("sn2"))
383 return -ENODEV;
384
385 event_nasid = ia64_sn_get_console_nasid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Jack Steiner24ee0a62005-09-12 12:15:43 -0500387 if (alloc_chrdev_region(&first_dev, 0, num_cnodes,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 SYSCTL_BASENAME) < 0) {
389 printk("%s: failed to register SN system controller device\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700390 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 return -ENODEV;
392 }
gregkh@suse.deca8eca62005-03-23 09:53:09 -0800393 snsc_class = class_create(THIS_MODULE, SYSCTL_BASENAME);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Jack Steiner24ee0a62005-09-12 12:15:43 -0500395 for (cnode = 0; cnode < num_cnodes; cnode++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 geoid = cnodeid_get_geoid(cnode);
397 devnamep = devname;
398 format_module_id(devnamep, geo_module(geoid),
399 MODULE_FORMAT_BRIEF);
400 devnamep = devname + strlen(devname);
akpm@osdl.org8c4335a2006-04-22 02:36:15 -0700401 sprintf(devnamep, "^%d#%d", geo_slot(geoid),
402 geo_slab(geoid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* allocate sysctl device data */
Jes Sorensen40953ed2006-03-23 03:00:49 -0800405 scd = kzalloc(sizeof (struct sysctl_data_s),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 GFP_KERNEL);
407 if (!scd) {
408 printk("%s: failed to allocate device info"
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700409 "for %s/%s\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 SYSCTL_BASENAME, devname);
411 continue;
412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* initialize sysctl device data fields */
415 scd->scd_nasid = cnodeid_to_nasid(cnode);
416 if (!(salbuf = kmalloc(SCDRV_BUFSZ, GFP_KERNEL))) {
417 printk("%s: failed to allocate driver buffer"
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700418 "(%s%s)\n", __func__,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 SYSCTL_BASENAME, devname);
420 kfree(scd);
421 continue;
422 }
423
424 if (ia64_sn_irtr_init(scd->scd_nasid, salbuf,
425 SCDRV_BUFSZ) < 0) {
426 printk
427 ("%s: failed to initialize SAL for"
428 " system controller communication"
429 " (%s/%s): outdated PROM?\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700430 __func__, SYSCTL_BASENAME, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 kfree(scd);
432 kfree(salbuf);
433 continue;
434 }
435
436 dev = first_dev + cnode;
437 cdev_init(&scd->scd_cdev, &scdrv_fops);
438 if (cdev_add(&scd->scd_cdev, dev, 1)) {
439 printk("%s: failed to register system"
440 " controller device (%s%s)\n",
Harvey Harrisonbf9d8922008-04-30 00:55:10 -0700441 __func__, SYSCTL_BASENAME, devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 kfree(scd);
443 kfree(salbuf);
444 continue;
445 }
446
tonyj@suse.de07c015e2007-08-07 22:28:44 -0700447 device_create(snsc_class, NULL, dev, "%s", devname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 ia64_sn_irtr_intr_enable(scd->scd_nasid,
450 0 /*ignored */ ,
451 SAL_IROUTER_INTR_RECV);
Greg Howard67639de2005-04-25 13:28:52 -0700452
453 /* on the console nasid, prepare to receive
454 * system controller environmental events
455 */
456 if(scd->scd_nasid == event_nasid) {
457 scdrv_event_init(scd);
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460 return 0;
461}
462
463module_init(scdrv_init);