perf tools: Don't clone maps from parent when synthesizing forks
[linux/fpc-iii.git] / drivers / misc / ibmasm / ibmasmfs.c
blobfa840666bdd1aeb20cca67fc5df9556fc73135aa
1 /*
2 * IBM ASM Service Processor Device Driver
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 * Copyright (C) IBM Corporation, 2004
20 * Author: Max Asböck <amax@us.ibm.com>
25 * Parts of this code are based on an article by Jonathan Corbet
26 * that appeared in Linux Weekly News.
31 * The IBMASM file virtual filesystem. It creates the following hierarchy
32 * dynamically when mounted from user space:
34 * /ibmasm
35 * |-- 0
36 * | |-- command
37 * | |-- event
38 * | |-- reverse_heartbeat
39 * | `-- remote_video
40 * | |-- depth
41 * | |-- height
42 * | `-- width
43 * .
44 * .
45 * .
46 * `-- n
47 * |-- command
48 * |-- event
49 * |-- reverse_heartbeat
50 * `-- remote_video
51 * |-- depth
52 * |-- height
53 * `-- width
55 * For each service processor the following files are created:
57 * command: execute dot commands
58 * write: execute a dot command on the service processor
59 * read: return the result of a previously executed dot command
61 * events: listen for service processor events
62 * read: sleep (interruptible) until an event occurs
63 * write: wakeup sleeping event listener
65 * reverse_heartbeat: send a heartbeat to the service processor
66 * read: sleep (interruptible) until the reverse heartbeat fails
67 * write: wakeup sleeping heartbeat listener
69 * remote_video/width
70 * remote_video/height
71 * remote_video/width: control remote display settings
72 * write: set value
73 * read: read value
76 #include <linux/fs.h>
77 #include <linux/pagemap.h>
78 #include <linux/slab.h>
79 #include <linux/uaccess.h>
80 #include <asm/io.h>
81 #include "ibmasm.h"
82 #include "remote.h"
83 #include "dot_command.h"
85 #define IBMASMFS_MAGIC 0x66726f67
87 static LIST_HEAD(service_processors);
89 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode);
90 static void ibmasmfs_create_files (struct super_block *sb);
91 static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent);
94 static struct dentry *ibmasmfs_mount(struct file_system_type *fst,
95 int flags, const char *name, void *data)
97 return mount_single(fst, flags, data, ibmasmfs_fill_super);
100 static const struct super_operations ibmasmfs_s_ops = {
101 .statfs = simple_statfs,
102 .drop_inode = generic_delete_inode,
105 static const struct file_operations *ibmasmfs_dir_ops = &simple_dir_operations;
107 static struct file_system_type ibmasmfs_type = {
108 .owner = THIS_MODULE,
109 .name = "ibmasmfs",
110 .mount = ibmasmfs_mount,
111 .kill_sb = kill_litter_super,
113 MODULE_ALIAS_FS("ibmasmfs");
115 static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent)
117 struct inode *root;
119 sb->s_blocksize = PAGE_SIZE;
120 sb->s_blocksize_bits = PAGE_SHIFT;
121 sb->s_magic = IBMASMFS_MAGIC;
122 sb->s_op = &ibmasmfs_s_ops;
123 sb->s_time_gran = 1;
125 root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
126 if (!root)
127 return -ENOMEM;
129 root->i_op = &simple_dir_inode_operations;
130 root->i_fop = ibmasmfs_dir_ops;
132 sb->s_root = d_make_root(root);
133 if (!sb->s_root)
134 return -ENOMEM;
136 ibmasmfs_create_files(sb);
137 return 0;
140 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
142 struct inode *ret = new_inode(sb);
144 if (ret) {
145 ret->i_ino = get_next_ino();
146 ret->i_mode = mode;
147 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
149 return ret;
152 static struct dentry *ibmasmfs_create_file(struct dentry *parent,
153 const char *name,
154 const struct file_operations *fops,
155 void *data,
156 int mode)
158 struct dentry *dentry;
159 struct inode *inode;
161 dentry = d_alloc_name(parent, name);
162 if (!dentry)
163 return NULL;
165 inode = ibmasmfs_make_inode(parent->d_sb, S_IFREG | mode);
166 if (!inode) {
167 dput(dentry);
168 return NULL;
171 inode->i_fop = fops;
172 inode->i_private = data;
174 d_add(dentry, inode);
175 return dentry;
178 static struct dentry *ibmasmfs_create_dir(struct dentry *parent,
179 const char *name)
181 struct dentry *dentry;
182 struct inode *inode;
184 dentry = d_alloc_name(parent, name);
185 if (!dentry)
186 return NULL;
188 inode = ibmasmfs_make_inode(parent->d_sb, S_IFDIR | 0500);
189 if (!inode) {
190 dput(dentry);
191 return NULL;
194 inode->i_op = &simple_dir_inode_operations;
195 inode->i_fop = ibmasmfs_dir_ops;
197 d_add(dentry, inode);
198 return dentry;
201 int ibmasmfs_register(void)
203 return register_filesystem(&ibmasmfs_type);
206 void ibmasmfs_unregister(void)
208 unregister_filesystem(&ibmasmfs_type);
211 void ibmasmfs_add_sp(struct service_processor *sp)
213 list_add(&sp->node, &service_processors);
216 /* struct to save state between command file operations */
217 struct ibmasmfs_command_data {
218 struct service_processor *sp;
219 struct command *command;
222 /* struct to save state between event file operations */
223 struct ibmasmfs_event_data {
224 struct service_processor *sp;
225 struct event_reader reader;
226 int active;
229 /* struct to save state between reverse heartbeat file operations */
230 struct ibmasmfs_heartbeat_data {
231 struct service_processor *sp;
232 struct reverse_heartbeat heartbeat;
233 int active;
236 static int command_file_open(struct inode *inode, struct file *file)
238 struct ibmasmfs_command_data *command_data;
240 if (!inode->i_private)
241 return -ENODEV;
243 command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
244 if (!command_data)
245 return -ENOMEM;
247 command_data->command = NULL;
248 command_data->sp = inode->i_private;
249 file->private_data = command_data;
250 return 0;
253 static int command_file_close(struct inode *inode, struct file *file)
255 struct ibmasmfs_command_data *command_data = file->private_data;
257 if (command_data->command)
258 command_put(command_data->command);
260 kfree(command_data);
261 return 0;
264 static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
266 struct ibmasmfs_command_data *command_data = file->private_data;
267 struct command *cmd;
268 int len;
269 unsigned long flags;
271 if (*offset < 0)
272 return -EINVAL;
273 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
274 return 0;
275 if (*offset != 0)
276 return 0;
278 spin_lock_irqsave(&command_data->sp->lock, flags);
279 cmd = command_data->command;
280 if (cmd == NULL) {
281 spin_unlock_irqrestore(&command_data->sp->lock, flags);
282 return 0;
284 command_data->command = NULL;
285 spin_unlock_irqrestore(&command_data->sp->lock, flags);
287 if (cmd->status != IBMASM_CMD_COMPLETE) {
288 command_put(cmd);
289 return -EIO;
291 len = min(count, cmd->buffer_size);
292 if (copy_to_user(buf, cmd->buffer, len)) {
293 command_put(cmd);
294 return -EFAULT;
296 command_put(cmd);
298 return len;
301 static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
303 struct ibmasmfs_command_data *command_data = file->private_data;
304 struct command *cmd;
305 unsigned long flags;
307 if (*offset < 0)
308 return -EINVAL;
309 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
310 return 0;
311 if (*offset != 0)
312 return 0;
314 /* commands are executed sequentially, only one command at a time */
315 if (command_data->command)
316 return -EAGAIN;
318 cmd = ibmasm_new_command(command_data->sp, count);
319 if (!cmd)
320 return -ENOMEM;
322 if (copy_from_user(cmd->buffer, ubuff, count)) {
323 command_put(cmd);
324 return -EFAULT;
327 spin_lock_irqsave(&command_data->sp->lock, flags);
328 if (command_data->command) {
329 spin_unlock_irqrestore(&command_data->sp->lock, flags);
330 command_put(cmd);
331 return -EAGAIN;
333 command_data->command = cmd;
334 spin_unlock_irqrestore(&command_data->sp->lock, flags);
336 ibmasm_exec_command(command_data->sp, cmd);
337 ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
339 return count;
342 static int event_file_open(struct inode *inode, struct file *file)
344 struct ibmasmfs_event_data *event_data;
345 struct service_processor *sp;
347 if (!inode->i_private)
348 return -ENODEV;
350 sp = inode->i_private;
352 event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
353 if (!event_data)
354 return -ENOMEM;
356 ibmasm_event_reader_register(sp, &event_data->reader);
358 event_data->sp = sp;
359 event_data->active = 0;
360 file->private_data = event_data;
361 return 0;
364 static int event_file_close(struct inode *inode, struct file *file)
366 struct ibmasmfs_event_data *event_data = file->private_data;
368 ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
369 kfree(event_data);
370 return 0;
373 static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
375 struct ibmasmfs_event_data *event_data = file->private_data;
376 struct event_reader *reader = &event_data->reader;
377 struct service_processor *sp = event_data->sp;
378 int ret;
379 unsigned long flags;
381 if (*offset < 0)
382 return -EINVAL;
383 if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
384 return 0;
385 if (*offset != 0)
386 return 0;
388 spin_lock_irqsave(&sp->lock, flags);
389 if (event_data->active) {
390 spin_unlock_irqrestore(&sp->lock, flags);
391 return -EBUSY;
393 event_data->active = 1;
394 spin_unlock_irqrestore(&sp->lock, flags);
396 ret = ibmasm_get_next_event(sp, reader);
397 if (ret <= 0)
398 goto out;
400 if (count < reader->data_size) {
401 ret = -EINVAL;
402 goto out;
405 if (copy_to_user(buf, reader->data, reader->data_size)) {
406 ret = -EFAULT;
407 goto out;
409 ret = reader->data_size;
411 out:
412 event_data->active = 0;
413 return ret;
416 static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
418 struct ibmasmfs_event_data *event_data = file->private_data;
420 if (*offset < 0)
421 return -EINVAL;
422 if (count != 1)
423 return 0;
424 if (*offset != 0)
425 return 0;
427 ibmasm_cancel_next_event(&event_data->reader);
428 return 0;
431 static int r_heartbeat_file_open(struct inode *inode, struct file *file)
433 struct ibmasmfs_heartbeat_data *rhbeat;
435 if (!inode->i_private)
436 return -ENODEV;
438 rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
439 if (!rhbeat)
440 return -ENOMEM;
442 rhbeat->sp = inode->i_private;
443 rhbeat->active = 0;
444 ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
445 file->private_data = rhbeat;
446 return 0;
449 static int r_heartbeat_file_close(struct inode *inode, struct file *file)
451 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
453 kfree(rhbeat);
454 return 0;
457 static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
459 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
460 unsigned long flags;
461 int result;
463 if (*offset < 0)
464 return -EINVAL;
465 if (count == 0 || count > 1024)
466 return 0;
467 if (*offset != 0)
468 return 0;
470 /* allow only one reverse heartbeat per process */
471 spin_lock_irqsave(&rhbeat->sp->lock, flags);
472 if (rhbeat->active) {
473 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
474 return -EBUSY;
476 rhbeat->active = 1;
477 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
479 result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
480 rhbeat->active = 0;
482 return result;
485 static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
487 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
489 if (*offset < 0)
490 return -EINVAL;
491 if (count != 1)
492 return 0;
493 if (*offset != 0)
494 return 0;
496 if (rhbeat->active)
497 ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
499 return 1;
502 static int remote_settings_file_close(struct inode *inode, struct file *file)
504 return 0;
507 static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
509 void __iomem *address = (void __iomem *)file->private_data;
510 int len = 0;
511 unsigned int value;
512 char lbuf[20];
514 value = readl(address);
515 len = snprintf(lbuf, sizeof(lbuf), "%d\n", value);
517 return simple_read_from_buffer(buf, count, offset, lbuf, len);
520 static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
522 void __iomem *address = (void __iomem *)file->private_data;
523 char *buff;
524 unsigned int value;
526 if (*offset < 0)
527 return -EINVAL;
528 if (count == 0 || count > 1024)
529 return 0;
530 if (*offset != 0)
531 return 0;
533 buff = kzalloc (count + 1, GFP_KERNEL);
534 if (!buff)
535 return -ENOMEM;
538 if (copy_from_user(buff, ubuff, count)) {
539 kfree(buff);
540 return -EFAULT;
543 value = simple_strtoul(buff, NULL, 10);
544 writel(value, address);
545 kfree(buff);
547 return count;
550 static const struct file_operations command_fops = {
551 .open = command_file_open,
552 .release = command_file_close,
553 .read = command_file_read,
554 .write = command_file_write,
555 .llseek = generic_file_llseek,
558 static const struct file_operations event_fops = {
559 .open = event_file_open,
560 .release = event_file_close,
561 .read = event_file_read,
562 .write = event_file_write,
563 .llseek = generic_file_llseek,
566 static const struct file_operations r_heartbeat_fops = {
567 .open = r_heartbeat_file_open,
568 .release = r_heartbeat_file_close,
569 .read = r_heartbeat_file_read,
570 .write = r_heartbeat_file_write,
571 .llseek = generic_file_llseek,
574 static const struct file_operations remote_settings_fops = {
575 .open = simple_open,
576 .release = remote_settings_file_close,
577 .read = remote_settings_file_read,
578 .write = remote_settings_file_write,
579 .llseek = generic_file_llseek,
583 static void ibmasmfs_create_files (struct super_block *sb)
585 struct list_head *entry;
586 struct service_processor *sp;
588 list_for_each(entry, &service_processors) {
589 struct dentry *dir;
590 struct dentry *remote_dir;
591 sp = list_entry(entry, struct service_processor, node);
592 dir = ibmasmfs_create_dir(sb->s_root, sp->dirname);
593 if (!dir)
594 continue;
596 ibmasmfs_create_file(dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
597 ibmasmfs_create_file(dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
598 ibmasmfs_create_file(dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
600 remote_dir = ibmasmfs_create_dir(dir, "remote_video");
601 if (!remote_dir)
602 continue;
604 ibmasmfs_create_file(remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
605 ibmasmfs_create_file(remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
606 ibmasmfs_create_file(remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);