Blame view

drivers/char/snsc.c 11 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
2
3
4
5
6
7
  /*
   * 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.
   *
40953ed87   Jes Sorensen   [PATCH] snsc kmal...
8
   * Copyright (C) 2004, 2006 Silicon Graphics, Inc. All rights reserved.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
   */
  
  /*
   * System controller communication driver
   *
   * This driver allows a user process to communicate with the system
   * controller (a.k.a. "IRouter") network in an SGI SN system.
   */
  
  #include <linux/interrupt.h>
  #include <linux/sched.h>
  #include <linux/device.h>
  #include <linux/poll.h>
  #include <linux/module.h>
  #include <linux/slab.h>
613655fa3   Arnd Bergmann   drivers: autoconv...
24
  #include <linux/mutex.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
25
26
27
28
29
30
31
32
33
34
35
  #include <asm/sn/io.h>
  #include <asm/sn/sn_sal.h>
  #include <asm/sn/module.h>
  #include <asm/sn/geo.h>
  #include <asm/sn/nodepda.h>
  #include "snsc.h"
  
  #define SYSCTL_BASENAME	"snsc"
  
  #define SCDRV_BUFSZ	2048
  #define SCDRV_TIMEOUT	1000
613655fa3   Arnd Bergmann   drivers: autoconv...
36
  static DEFINE_MUTEX(scdrv_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
37
  static irqreturn_t
7d12e780e   David Howells   IRQ: Maintain reg...
38
  scdrv_interrupt(int irq, void *subch_data)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
  {
  	struct subch_data_s *sd = subch_data;
  	unsigned long flags;
  	int status;
  
  	spin_lock_irqsave(&sd->sd_rlock, flags);
  	spin_lock(&sd->sd_wlock);
  	status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch);
  
  	if (status > 0) {
  		if (status & SAL_IROUTER_INTR_RECV) {
  			wake_up(&sd->sd_rq);
  		}
  		if (status & SAL_IROUTER_INTR_XMIT) {
  			ia64_sn_irtr_intr_disable
  			    (sd->sd_nasid, sd->sd_subch,
  			     SAL_IROUTER_INTR_XMIT);
  			wake_up(&sd->sd_wq);
  		}
  	}
  	spin_unlock(&sd->sd_wlock);
  	spin_unlock_irqrestore(&sd->sd_rlock, flags);
  	return IRQ_HANDLED;
  }
  
  /*
   * scdrv_open
   *
   * Reserve a subchannel for system controller communication.
   */
  
  static int
  scdrv_open(struct inode *inode, struct file *file)
  {
  	struct sysctl_data_s *scd;
  	struct subch_data_s *sd;
  	int rv;
  
  	/* look up device info for this device file */
  	scd = container_of(inode->i_cdev, struct sysctl_data_s, scd_cdev);
  
  	/* allocate memory for subchannel data */
40953ed87   Jes Sorensen   [PATCH] snsc kmal...
81
  	sd = kzalloc(sizeof (struct subch_data_s), GFP_KERNEL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
  	if (sd == NULL) {
  		printk("%s: couldn't allocate subchannel data
  ",
bf9d89295   Harvey Harrison   drivers/char: rep...
85
  		       __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
86
87
88
89
  		return -ENOMEM;
  	}
  
  	/* initialize subch_data_s fields */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
90
91
92
93
94
  	sd->sd_nasid = scd->scd_nasid;
  	sd->sd_subch = ia64_sn_irtr_open(scd->scd_nasid);
  
  	if (sd->sd_subch < 0) {
  		kfree(sd);
bf9d89295   Harvey Harrison   drivers/char: rep...
95
96
  		printk("%s: couldn't allocate subchannel
  ", __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
98
99
100
101
102
103
104
105
106
107
108
109
  		return -EBUSY;
  	}
  
  	spin_lock_init(&sd->sd_rlock);
  	spin_lock_init(&sd->sd_wlock);
  	init_waitqueue_head(&sd->sd_rq);
  	init_waitqueue_head(&sd->sd_wq);
  	sema_init(&sd->sd_rbs, 1);
  	sema_init(&sd->sd_wbs, 1);
  
  	file->private_data = sd;
  
  	/* hook this subchannel up to the system controller interrupt */
613655fa3   Arnd Bergmann   drivers: autoconv...
110
  	mutex_lock(&scdrv_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
111
  	rv = request_irq(SGI_UART_VECTOR, scdrv_interrupt,
0f2ed4c6b   Thomas Gleixner   [PATCH] irq-flags...
112
  			 IRQF_SHARED | IRQF_DISABLED,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
113
114
115
116
  			 SYSCTL_BASENAME, sd);
  	if (rv) {
  		ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch);
  		kfree(sd);
bf9d89295   Harvey Harrison   drivers/char: rep...
117
118
  		printk("%s: irq request failed (%d)
  ", __func__, rv);
613655fa3   Arnd Bergmann   drivers: autoconv...
119
  		mutex_unlock(&scdrv_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
  		return -EBUSY;
  	}
613655fa3   Arnd Bergmann   drivers: autoconv...
122
  	mutex_unlock(&scdrv_mutex);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  	return 0;
  }
  
  /*
   * scdrv_release
   *
   * Release a previously-reserved subchannel.
   */
  
  static int
  scdrv_release(struct inode *inode, struct file *file)
  {
  	struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
  	int rv;
  
  	/* free the interrupt */
  	free_irq(SGI_UART_VECTOR, sd);
  
  	/* ask SAL to close the subchannel */
  	rv = ia64_sn_irtr_close(sd->sd_nasid, sd->sd_subch);
  
  	kfree(sd);
  	return rv;
  }
  
  /*
   * scdrv_read
   *
   * Called to read bytes from the open IRouter pipe.
   *
   */
  
  static inline int
  read_status_check(struct subch_data_s *sd, int *len)
  {
  	return ia64_sn_irtr_recv(sd->sd_nasid, sd->sd_subch, sd->sd_rb, len);
  }
  
  static ssize_t
  scdrv_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos)
  {
  	int status;
  	int len;
  	unsigned long flags;
  	struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
  
  	/* try to get control of the read buffer */
  	if (down_trylock(&sd->sd_rbs)) {
  		/* somebody else has it now;
  		 * if we're non-blocking, then exit...
  		 */
  		if (file->f_flags & O_NONBLOCK) {
  			return -EAGAIN;
  		}
  		/* ...or if we want to block, then do so here */
  		if (down_interruptible(&sd->sd_rbs)) {
  			/* something went wrong with wait */
  			return -ERESTARTSYS;
  		}
  	}
  
  	/* anything to read? */
  	len = CHUNKSIZE;
  	spin_lock_irqsave(&sd->sd_rlock, flags);
  	status = read_status_check(sd, &len);
  
  	/* if not, and we're blocking I/O, loop */
  	while (status < 0) {
  		DECLARE_WAITQUEUE(wait, current);
  
  		if (file->f_flags & O_NONBLOCK) {
  			spin_unlock_irqrestore(&sd->sd_rlock, flags);
  			up(&sd->sd_rbs);
  			return -EAGAIN;
  		}
  
  		len = CHUNKSIZE;
  		set_current_state(TASK_INTERRUPTIBLE);
  		add_wait_queue(&sd->sd_rq, &wait);
  		spin_unlock_irqrestore(&sd->sd_rlock, flags);
  
  		schedule_timeout(SCDRV_TIMEOUT);
  
  		remove_wait_queue(&sd->sd_rq, &wait);
  		if (signal_pending(current)) {
  			/* wait was interrupted */
  			up(&sd->sd_rbs);
  			return -ERESTARTSYS;
  		}
  
  		spin_lock_irqsave(&sd->sd_rlock, flags);
  		status = read_status_check(sd, &len);
  	}
  	spin_unlock_irqrestore(&sd->sd_rlock, flags);
  
  	if (len > 0) {
  		/* we read something in the last read_status_check(); copy
  		 * it out to user space
  		 */
  		if (count < len) {
  			pr_debug("%s: only accepting %d of %d bytes
  ",
bf9d89295   Harvey Harrison   drivers/char: rep...
225
  				 __func__, (int) count, len);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
  		}
  		len = min((int) count, len);
  		if (copy_to_user(buf, sd->sd_rb, len))
  			len = -EFAULT;
  	}
  
  	/* release the read buffer and wake anyone who might be
  	 * waiting for it
  	 */
  	up(&sd->sd_rbs);
  
  	/* return the number of characters read in */
  	return len;
  }
  
  /*
   * scdrv_write
   *
   * Writes a chunk of an IRouter packet (or other system controller data)
   * to the system controller.
   *
   */
  static inline int
  write_status_check(struct subch_data_s *sd, int count)
  {
  	return ia64_sn_irtr_send(sd->sd_nasid, sd->sd_subch, sd->sd_wb, count);
  }
  
  static ssize_t
  scdrv_write(struct file *file, const char __user *buf,
  	    size_t count, loff_t *f_pos)
  {
  	unsigned long flags;
  	int status;
  	struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
  
  	/* try to get control of the write buffer */
  	if (down_trylock(&sd->sd_wbs)) {
  		/* somebody else has it now;
  		 * if we're non-blocking, then exit...
  		 */
  		if (file->f_flags & O_NONBLOCK) {
  			return -EAGAIN;
  		}
  		/* ...or if we want to block, then do so here */
  		if (down_interruptible(&sd->sd_wbs)) {
  			/* something went wrong with wait */
  			return -ERESTARTSYS;
  		}
  	}
  
  	count = min((int) count, CHUNKSIZE);
  	if (copy_from_user(sd->sd_wb, buf, count)) {
  		up(&sd->sd_wbs);
  		return -EFAULT;
  	}
  
  	/* try to send the buffer */
  	spin_lock_irqsave(&sd->sd_wlock, flags);
  	status = write_status_check(sd, count);
  
  	/* if we failed, and we want to block, then loop */
  	while (status <= 0) {
  		DECLARE_WAITQUEUE(wait, current);
  
  		if (file->f_flags & O_NONBLOCK) {
  			spin_unlock(&sd->sd_wlock);
  			up(&sd->sd_wbs);
  			return -EAGAIN;
  		}
  
  		set_current_state(TASK_INTERRUPTIBLE);
  		add_wait_queue(&sd->sd_wq, &wait);
  		spin_unlock_irqrestore(&sd->sd_wlock, flags);
  
  		schedule_timeout(SCDRV_TIMEOUT);
  
  		remove_wait_queue(&sd->sd_wq, &wait);
  		if (signal_pending(current)) {
  			/* wait was interrupted */
  			up(&sd->sd_wbs);
  			return -ERESTARTSYS;
  		}
  
  		spin_lock_irqsave(&sd->sd_wlock, flags);
  		status = write_status_check(sd, count);
  	}
  	spin_unlock_irqrestore(&sd->sd_wlock, flags);
  
  	/* release the write buffer and wake anyone who's waiting for it */
  	up(&sd->sd_wbs);
  
  	/* return the number of characters accepted (should be the complete
  	 * "chunk" as requested)
  	 */
  	if ((status >= 0) && (status < count)) {
  		pr_debug("Didn't accept the full chunk; %d of %d
  ",
  			 status, (int) count);
  	}
  	return status;
  }
  
  static unsigned int
  scdrv_poll(struct file *file, struct poll_table_struct *wait)
  {
  	unsigned int mask = 0;
  	int status = 0;
  	struct subch_data_s *sd = (struct subch_data_s *) file->private_data;
  	unsigned long flags;
  
  	poll_wait(file, &sd->sd_rq, wait);
  	poll_wait(file, &sd->sd_wq, wait);
  
  	spin_lock_irqsave(&sd->sd_rlock, flags);
  	spin_lock(&sd->sd_wlock);
  	status = ia64_sn_irtr_intr(sd->sd_nasid, sd->sd_subch);
  	spin_unlock(&sd->sd_wlock);
  	spin_unlock_irqrestore(&sd->sd_rlock, flags);
  
  	if (status > 0) {
  		if (status & SAL_IROUTER_INTR_RECV) {
  			mask |= POLLIN | POLLRDNORM;
  		}
  		if (status & SAL_IROUTER_INTR_XMIT) {
  			mask |= POLLOUT | POLLWRNORM;
  		}
  	}
  
  	return mask;
  }
62322d255   Arjan van de Ven   [PATCH] make more...
357
  static const struct file_operations scdrv_fops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
358
359
360
361
362
363
  	.owner =	THIS_MODULE,
  	.read =		scdrv_read,
  	.write =	scdrv_write,
  	.poll =		scdrv_poll,
  	.open =		scdrv_open,
  	.release =	scdrv_release,
6038f373a   Arnd Bergmann   llseek: automatic...
364
  	.llseek =	noop_llseek,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
  };
ca8eca688   Greg Kroah-Hartman   [PATCH] class: co...
366
  static struct class *snsc_class;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
  /*
   * scdrv_init
   *
   * Called at boot time to initialize the system controller communication
   * facility.
   */
  int __init
  scdrv_init(void)
  {
  	geoid_t geoid;
  	cnodeid_t cnode;
  	char devname[32];
  	char *devnamep;
  	struct sysctl_data_s *scd;
  	void *salbuf;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
382
  	dev_t first_dev, dev;
c7c17423b   Greg Edwards   [IA64] add platfo...
383
384
385
386
387
388
  	nasid_t event_nasid;
  
  	if (!ia64_platform_is("sn2"))
  		return -ENODEV;
  
  	event_nasid = ia64_sn_get_console_nasid();
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
389

24ee0a6d7   Jack Steiner   [IA64] Cleanup us...
390
  	if (alloc_chrdev_region(&first_dev, 0, num_cnodes,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
391
392
393
  				SYSCTL_BASENAME) < 0) {
  		printk("%s: failed to register SN system controller device
  ",
bf9d89295   Harvey Harrison   drivers/char: rep...
394
  		       __func__);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
395
396
  		return -ENODEV;
  	}
ca8eca688   Greg Kroah-Hartman   [PATCH] class: co...
397
  	snsc_class = class_create(THIS_MODULE, SYSCTL_BASENAME);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
398

24ee0a6d7   Jack Steiner   [IA64] Cleanup us...
399
  	for (cnode = 0; cnode < num_cnodes; cnode++) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
400
401
402
403
404
  			geoid = cnodeid_get_geoid(cnode);
  			devnamep = devname;
  			format_module_id(devnamep, geo_module(geoid),
  					 MODULE_FORMAT_BRIEF);
  			devnamep = devname + strlen(devname);
8c4335a87   Andrew Morton   [PATCH] Altix sns...
405
406
  			sprintf(devnamep, "^%d#%d", geo_slot(geoid),
  				geo_slab(geoid));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
407
408
  
  			/* allocate sysctl device data */
40953ed87   Jes Sorensen   [PATCH] snsc kmal...
409
  			scd = kzalloc(sizeof (struct sysctl_data_s),
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
410
411
412
  				      GFP_KERNEL);
  			if (!scd) {
  				printk("%s: failed to allocate device info"
bf9d89295   Harvey Harrison   drivers/char: rep...
413
414
  				       "for %s/%s
  ", __func__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
415
416
417
  				       SYSCTL_BASENAME, devname);
  				continue;
  			}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
418
419
420
421
422
  
  			/* initialize sysctl device data fields */
  			scd->scd_nasid = cnodeid_to_nasid(cnode);
  			if (!(salbuf = kmalloc(SCDRV_BUFSZ, GFP_KERNEL))) {
  				printk("%s: failed to allocate driver buffer"
bf9d89295   Harvey Harrison   drivers/char: rep...
423
424
  				       "(%s%s)
  ", __func__,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
425
426
427
428
429
430
431
432
433
434
435
436
  				       SYSCTL_BASENAME, devname);
  				kfree(scd);
  				continue;
  			}
  
  			if (ia64_sn_irtr_init(scd->scd_nasid, salbuf,
  					      SCDRV_BUFSZ) < 0) {
  				printk
  				    ("%s: failed to initialize SAL for"
  				     " system controller communication"
  				     " (%s/%s): outdated PROM?
  ",
bf9d89295   Harvey Harrison   drivers/char: rep...
437
  				     __func__, SYSCTL_BASENAME, devname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
438
439
440
441
442
443
444
445
446
447
448
  				kfree(scd);
  				kfree(salbuf);
  				continue;
  			}
  
  			dev = first_dev + cnode;
  			cdev_init(&scd->scd_cdev, &scdrv_fops);
  			if (cdev_add(&scd->scd_cdev, dev, 1)) {
  				printk("%s: failed to register system"
  				       " controller device (%s%s)
  ",
bf9d89295   Harvey Harrison   drivers/char: rep...
449
  				       __func__, SYSCTL_BASENAME, devname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
450
451
452
453
  				kfree(scd);
  				kfree(salbuf);
  				continue;
  			}
03457cd45   Greg Kroah-Hartman   device create: ch...
454
455
  			device_create(snsc_class, NULL, dev, NULL,
  				      "%s", devname);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
456
457
458
459
  
  			ia64_sn_irtr_intr_enable(scd->scd_nasid,
  						 0 /*ignored */ ,
  						 SAL_IROUTER_INTR_RECV);
67639deb0   Greg Howard   [IA64] Altix syst...
460
461
462
463
464
465
466
  
                          /* on the console nasid, prepare to receive
                           * system controller environmental events
                           */
                          if(scd->scd_nasid == event_nasid) {
                                  scdrv_event_init(scd);
                          }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
467
468
469
470
471
  	}
  	return 0;
  }
  
  module_init(scdrv_init);