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, 2006 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 <linux/mutex.h>
25 #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>
32 #define SYSCTL_BASENAME "snsc"
34 #define SCDRV_BUFSZ 2048
35 #define SCDRV_TIMEOUT 1000
37 static DEFINE_MUTEX(scdrv_mutex
);
39 scdrv_interrupt(int irq
, void *subch_data
)
41 struct subch_data_s
*sd
= subch_data
;
45 spin_lock_irqsave(&sd
->sd_rlock
, flags
);
46 spin_lock(&sd
->sd_wlock
);
47 status
= ia64_sn_irtr_intr(sd
->sd_nasid
, sd
->sd_subch
);
50 if (status
& SAL_IROUTER_INTR_RECV
) {
53 if (status
& SAL_IROUTER_INTR_XMIT
) {
54 ia64_sn_irtr_intr_disable
55 (sd
->sd_nasid
, sd
->sd_subch
,
56 SAL_IROUTER_INTR_XMIT
);
60 spin_unlock(&sd
->sd_wlock
);
61 spin_unlock_irqrestore(&sd
->sd_rlock
, flags
);
68 * Reserve a subchannel for system controller communication.
72 scdrv_open(struct inode
*inode
, struct file
*file
)
74 struct sysctl_data_s
*scd
;
75 struct subch_data_s
*sd
;
78 /* look up device info for this device file */
79 scd
= container_of(inode
->i_cdev
, struct sysctl_data_s
, scd_cdev
);
81 /* allocate memory for subchannel data */
82 sd
= kzalloc(sizeof (struct subch_data_s
), GFP_KERNEL
);
84 printk("%s: couldn't allocate subchannel data\n",
89 /* initialize subch_data_s fields */
90 sd
->sd_nasid
= scd
->scd_nasid
;
91 sd
->sd_subch
= ia64_sn_irtr_open(scd
->scd_nasid
);
93 if (sd
->sd_subch
< 0) {
95 printk("%s: couldn't allocate subchannel\n", __func__
);
99 spin_lock_init(&sd
->sd_rlock
);
100 spin_lock_init(&sd
->sd_wlock
);
101 init_waitqueue_head(&sd
->sd_rq
);
102 init_waitqueue_head(&sd
->sd_wq
);
103 sema_init(&sd
->sd_rbs
, 1);
104 sema_init(&sd
->sd_wbs
, 1);
106 file
->private_data
= sd
;
108 /* hook this subchannel up to the system controller interrupt */
109 mutex_lock(&scdrv_mutex
);
110 rv
= request_irq(SGI_UART_VECTOR
, scdrv_interrupt
,
111 IRQF_SHARED
| IRQF_DISABLED
,
112 SYSCTL_BASENAME
, sd
);
114 ia64_sn_irtr_close(sd
->sd_nasid
, sd
->sd_subch
);
116 printk("%s: irq request failed (%d)\n", __func__
, rv
);
117 mutex_unlock(&scdrv_mutex
);
120 mutex_unlock(&scdrv_mutex
);
127 * Release a previously-reserved subchannel.
131 scdrv_release(struct inode
*inode
, struct file
*file
)
133 struct subch_data_s
*sd
= (struct subch_data_s
*) file
->private_data
;
136 /* free the interrupt */
137 free_irq(SGI_UART_VECTOR
, sd
);
139 /* ask SAL to close the subchannel */
140 rv
= ia64_sn_irtr_close(sd
->sd_nasid
, sd
->sd_subch
);
149 * Called to read bytes from the open IRouter pipe.
154 read_status_check(struct subch_data_s
*sd
, int *len
)
156 return ia64_sn_irtr_recv(sd
->sd_nasid
, sd
->sd_subch
, sd
->sd_rb
, len
);
160 scdrv_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*f_pos
)
165 struct subch_data_s
*sd
= (struct subch_data_s
*) file
->private_data
;
167 /* try to get control of the read buffer */
168 if (down_trylock(&sd
->sd_rbs
)) {
169 /* somebody else has it now;
170 * if we're non-blocking, then exit...
172 if (file
->f_flags
& O_NONBLOCK
) {
175 /* ...or if we want to block, then do so here */
176 if (down_interruptible(&sd
->sd_rbs
)) {
177 /* something went wrong with wait */
182 /* anything to read? */
184 spin_lock_irqsave(&sd
->sd_rlock
, flags
);
185 status
= read_status_check(sd
, &len
);
187 /* if not, and we're blocking I/O, loop */
189 DECLARE_WAITQUEUE(wait
, current
);
191 if (file
->f_flags
& O_NONBLOCK
) {
192 spin_unlock_irqrestore(&sd
->sd_rlock
, flags
);
198 set_current_state(TASK_INTERRUPTIBLE
);
199 add_wait_queue(&sd
->sd_rq
, &wait
);
200 spin_unlock_irqrestore(&sd
->sd_rlock
, flags
);
202 schedule_timeout(SCDRV_TIMEOUT
);
204 remove_wait_queue(&sd
->sd_rq
, &wait
);
205 if (signal_pending(current
)) {
206 /* wait was interrupted */
211 spin_lock_irqsave(&sd
->sd_rlock
, flags
);
212 status
= read_status_check(sd
, &len
);
214 spin_unlock_irqrestore(&sd
->sd_rlock
, flags
);
217 /* we read something in the last read_status_check(); copy
218 * it out to user space
221 pr_debug("%s: only accepting %d of %d bytes\n",
222 __func__
, (int) count
, len
);
224 len
= min((int) count
, len
);
225 if (copy_to_user(buf
, sd
->sd_rb
, len
))
229 /* release the read buffer and wake anyone who might be
234 /* return the number of characters read in */
241 * Writes a chunk of an IRouter packet (or other system controller data)
242 * to the system controller.
246 write_status_check(struct subch_data_s
*sd
, int count
)
248 return ia64_sn_irtr_send(sd
->sd_nasid
, sd
->sd_subch
, sd
->sd_wb
, count
);
252 scdrv_write(struct file
*file
, const char __user
*buf
,
253 size_t count
, loff_t
*f_pos
)
257 struct subch_data_s
*sd
= (struct subch_data_s
*) file
->private_data
;
259 /* try to get control of the write buffer */
260 if (down_trylock(&sd
->sd_wbs
)) {
261 /* somebody else has it now;
262 * if we're non-blocking, then exit...
264 if (file
->f_flags
& O_NONBLOCK
) {
267 /* ...or if we want to block, then do so here */
268 if (down_interruptible(&sd
->sd_wbs
)) {
269 /* something went wrong with wait */
274 count
= min((int) count
, CHUNKSIZE
);
275 if (copy_from_user(sd
->sd_wb
, buf
, count
)) {
280 /* try to send the buffer */
281 spin_lock_irqsave(&sd
->sd_wlock
, flags
);
282 status
= write_status_check(sd
, count
);
284 /* if we failed, and we want to block, then loop */
285 while (status
<= 0) {
286 DECLARE_WAITQUEUE(wait
, current
);
288 if (file
->f_flags
& O_NONBLOCK
) {
289 spin_unlock(&sd
->sd_wlock
);
294 set_current_state(TASK_INTERRUPTIBLE
);
295 add_wait_queue(&sd
->sd_wq
, &wait
);
296 spin_unlock_irqrestore(&sd
->sd_wlock
, flags
);
298 schedule_timeout(SCDRV_TIMEOUT
);
300 remove_wait_queue(&sd
->sd_wq
, &wait
);
301 if (signal_pending(current
)) {
302 /* wait was interrupted */
307 spin_lock_irqsave(&sd
->sd_wlock
, flags
);
308 status
= write_status_check(sd
, count
);
310 spin_unlock_irqrestore(&sd
->sd_wlock
, flags
);
312 /* release the write buffer and wake anyone who's waiting for it */
315 /* return the number of characters accepted (should be the complete
316 * "chunk" as requested)
318 if ((status
>= 0) && (status
< count
)) {
319 pr_debug("Didn't accept the full chunk; %d of %d\n",
320 status
, (int) count
);
326 scdrv_poll(struct file
*file
, struct poll_table_struct
*wait
)
328 unsigned int mask
= 0;
330 struct subch_data_s
*sd
= (struct subch_data_s
*) file
->private_data
;
333 poll_wait(file
, &sd
->sd_rq
, wait
);
334 poll_wait(file
, &sd
->sd_wq
, wait
);
336 spin_lock_irqsave(&sd
->sd_rlock
, flags
);
337 spin_lock(&sd
->sd_wlock
);
338 status
= ia64_sn_irtr_intr(sd
->sd_nasid
, sd
->sd_subch
);
339 spin_unlock(&sd
->sd_wlock
);
340 spin_unlock_irqrestore(&sd
->sd_rlock
, flags
);
343 if (status
& SAL_IROUTER_INTR_RECV
) {
344 mask
|= POLLIN
| POLLRDNORM
;
346 if (status
& SAL_IROUTER_INTR_XMIT
) {
347 mask
|= POLLOUT
| POLLWRNORM
;
354 static const struct file_operations scdrv_fops
= {
355 .owner
= THIS_MODULE
,
357 .write
= scdrv_write
,
360 .release
= scdrv_release
,
361 .llseek
= noop_llseek
,
364 static struct class *snsc_class
;
369 * Called at boot time to initialize the system controller communication
379 struct sysctl_data_s
*scd
;
381 dev_t first_dev
, dev
;
384 if (!ia64_platform_is("sn2"))
387 event_nasid
= ia64_sn_get_console_nasid();
389 if (alloc_chrdev_region(&first_dev
, 0, num_cnodes
,
390 SYSCTL_BASENAME
) < 0) {
391 printk("%s: failed to register SN system controller device\n",
395 snsc_class
= class_create(THIS_MODULE
, SYSCTL_BASENAME
);
397 for (cnode
= 0; cnode
< num_cnodes
; cnode
++) {
398 geoid
= cnodeid_get_geoid(cnode
);
400 format_module_id(devnamep
, geo_module(geoid
),
401 MODULE_FORMAT_BRIEF
);
402 devnamep
= devname
+ strlen(devname
);
403 sprintf(devnamep
, "^%d#%d", geo_slot(geoid
),
406 /* allocate sysctl device data */
407 scd
= kzalloc(sizeof (struct sysctl_data_s
),
410 printk("%s: failed to allocate device info"
411 "for %s/%s\n", __func__
,
412 SYSCTL_BASENAME
, devname
);
416 /* initialize sysctl device data fields */
417 scd
->scd_nasid
= cnodeid_to_nasid(cnode
);
418 if (!(salbuf
= kmalloc(SCDRV_BUFSZ
, GFP_KERNEL
))) {
419 printk("%s: failed to allocate driver buffer"
420 "(%s%s)\n", __func__
,
421 SYSCTL_BASENAME
, devname
);
426 if (ia64_sn_irtr_init(scd
->scd_nasid
, salbuf
,
429 ("%s: failed to initialize SAL for"
430 " system controller communication"
431 " (%s/%s): outdated PROM?\n",
432 __func__
, SYSCTL_BASENAME
, devname
);
438 dev
= first_dev
+ cnode
;
439 cdev_init(&scd
->scd_cdev
, &scdrv_fops
);
440 if (cdev_add(&scd
->scd_cdev
, dev
, 1)) {
441 printk("%s: failed to register system"
442 " controller device (%s%s)\n",
443 __func__
, SYSCTL_BASENAME
, devname
);
449 device_create(snsc_class
, NULL
, dev
, NULL
,
452 ia64_sn_irtr_intr_enable(scd
->scd_nasid
,
454 SAL_IROUTER_INTR_RECV
);
456 /* on the console nasid, prepare to receive
457 * system controller environmental events
459 if(scd
->scd_nasid
== event_nasid
) {
460 scdrv_event_init(scd
);
466 module_init(scdrv_init
);