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
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:
38 * | |-- reverse_heartbeat
49 * |-- reverse_heartbeat
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
71 * remote_video/width: control remote display settings
77 #include <linux/pagemap.h>
78 #include <linux/slab.h>
79 #include <asm/uaccess.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,
110 .mount = ibmasmfs_mount,
111 .kill_sb = kill_litter_super,
114 static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent)
118 sb->s_blocksize = PAGE_CACHE_SIZE;
119 sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
120 sb->s_magic = IBMASMFS_MAGIC;
121 sb->s_op = &ibmasmfs_s_ops;
124 root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
128 root->i_op = &simple_dir_inode_operations;
129 root->i_fop = ibmasmfs_dir_ops;
131 sb->s_root = d_make_root(root);
135 ibmasmfs_create_files(sb);
139 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
141 struct inode *ret = new_inode(sb);
144 ret->i_ino = get_next_ino();
146 ret->i_atime = ret->i_mtime = ret->i_ctime = CURRENT_TIME;
151 static struct dentry *ibmasmfs_create_file (struct super_block *sb,
152 struct dentry *parent,
154 const struct file_operations *fops,
158 struct dentry *dentry;
161 dentry = d_alloc_name(parent, name);
165 inode = ibmasmfs_make_inode(sb, S_IFREG | mode);
172 inode->i_private = data;
174 d_add(dentry, inode);
178 static struct dentry *ibmasmfs_create_dir (struct super_block *sb,
179 struct dentry *parent,
182 struct dentry *dentry;
185 dentry = d_alloc_name(parent, name);
189 inode = ibmasmfs_make_inode(sb, S_IFDIR | 0500);
195 inode->i_op = &simple_dir_inode_operations;
196 inode->i_fop = ibmasmfs_dir_ops;
198 d_add(dentry, inode);
202 int ibmasmfs_register(void)
204 return register_filesystem(&ibmasmfs_type);
207 void ibmasmfs_unregister(void)
209 unregister_filesystem(&ibmasmfs_type);
212 void ibmasmfs_add_sp(struct service_processor *sp)
214 list_add(&sp->node, &service_processors);
217 /* struct to save state between command file operations */
218 struct ibmasmfs_command_data {
219 struct service_processor *sp;
220 struct command *command;
223 /* struct to save state between event file operations */
224 struct ibmasmfs_event_data {
225 struct service_processor *sp;
226 struct event_reader reader;
230 /* struct to save state between reverse heartbeat file operations */
231 struct ibmasmfs_heartbeat_data {
232 struct service_processor *sp;
233 struct reverse_heartbeat heartbeat;
237 static int command_file_open(struct inode *inode, struct file *file)
239 struct ibmasmfs_command_data *command_data;
241 if (!inode->i_private)
244 command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
248 command_data->command = NULL;
249 command_data->sp = inode->i_private;
250 file->private_data = command_data;
254 static int command_file_close(struct inode *inode, struct file *file)
256 struct ibmasmfs_command_data *command_data = file->private_data;
258 if (command_data->command)
259 command_put(command_data->command);
265 static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
267 struct ibmasmfs_command_data *command_data = file->private_data;
274 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
279 spin_lock_irqsave(&command_data->sp->lock, flags);
280 cmd = command_data->command;
282 spin_unlock_irqrestore(&command_data->sp->lock, flags);
285 command_data->command = NULL;
286 spin_unlock_irqrestore(&command_data->sp->lock, flags);
288 if (cmd->status != IBMASM_CMD_COMPLETE) {
292 len = min(count, cmd->buffer_size);
293 if (copy_to_user(buf, cmd->buffer, len)) {
302 static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
304 struct ibmasmfs_command_data *command_data = file->private_data;
310 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
315 /* commands are executed sequentially, only one command at a time */
316 if (command_data->command)
319 cmd = ibmasm_new_command(command_data->sp, count);
323 if (copy_from_user(cmd->buffer, ubuff, count)) {
328 spin_lock_irqsave(&command_data->sp->lock, flags);
329 if (command_data->command) {
330 spin_unlock_irqrestore(&command_data->sp->lock, flags);
334 command_data->command = cmd;
335 spin_unlock_irqrestore(&command_data->sp->lock, flags);
337 ibmasm_exec_command(command_data->sp, cmd);
338 ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
343 static int event_file_open(struct inode *inode, struct file *file)
345 struct ibmasmfs_event_data *event_data;
346 struct service_processor *sp;
348 if (!inode->i_private)
351 sp = inode->i_private;
353 event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
357 ibmasm_event_reader_register(sp, &event_data->reader);
360 event_data->active = 0;
361 file->private_data = event_data;
365 static int event_file_close(struct inode *inode, struct file *file)
367 struct ibmasmfs_event_data *event_data = file->private_data;
369 ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
374 static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
376 struct ibmasmfs_event_data *event_data = file->private_data;
377 struct event_reader *reader = &event_data->reader;
378 struct service_processor *sp = event_data->sp;
384 if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
389 spin_lock_irqsave(&sp->lock, flags);
390 if (event_data->active) {
391 spin_unlock_irqrestore(&sp->lock, flags);
394 event_data->active = 1;
395 spin_unlock_irqrestore(&sp->lock, flags);
397 ret = ibmasm_get_next_event(sp, reader);
401 if (count < reader->data_size) {
406 if (copy_to_user(buf, reader->data, reader->data_size)) {
410 ret = reader->data_size;
413 event_data->active = 0;
417 static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
419 struct ibmasmfs_event_data *event_data = file->private_data;
428 ibmasm_cancel_next_event(&event_data->reader);
432 static int r_heartbeat_file_open(struct inode *inode, struct file *file)
434 struct ibmasmfs_heartbeat_data *rhbeat;
436 if (!inode->i_private)
439 rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
443 rhbeat->sp = inode->i_private;
445 ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
446 file->private_data = rhbeat;
450 static int r_heartbeat_file_close(struct inode *inode, struct file *file)
452 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
458 static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
460 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
466 if (count == 0 || count > 1024)
471 /* allow only one reverse heartbeat per process */
472 spin_lock_irqsave(&rhbeat->sp->lock, flags);
473 if (rhbeat->active) {
474 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
478 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
480 result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
486 static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
488 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
498 ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
503 static int remote_settings_file_close(struct inode *inode, struct file *file)
508 static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
510 void __iomem *address = (void __iomem *)file->private_data;
518 if (count == 0 || count > 1024)
523 page = (unsigned char *)__get_free_page(GFP_KERNEL);
527 value = readl(address);
528 len = sprintf(page, "%d\n", value);
530 if (copy_to_user(buf, page, len)) {
538 free_page((unsigned long)page);
542 static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
544 void __iomem *address = (void __iomem *)file->private_data;
550 if (count == 0 || count > 1024)
555 buff = kzalloc (count + 1, GFP_KERNEL);
560 if (copy_from_user(buff, ubuff, count)) {
565 value = simple_strtoul(buff, NULL, 10);
566 writel(value, address);
572 static const struct file_operations command_fops = {
573 .open = command_file_open,
574 .release = command_file_close,
575 .read = command_file_read,
576 .write = command_file_write,
577 .llseek = generic_file_llseek,
580 static const struct file_operations event_fops = {
581 .open = event_file_open,
582 .release = event_file_close,
583 .read = event_file_read,
584 .write = event_file_write,
585 .llseek = generic_file_llseek,
588 static const struct file_operations r_heartbeat_fops = {
589 .open = r_heartbeat_file_open,
590 .release = r_heartbeat_file_close,
591 .read = r_heartbeat_file_read,
592 .write = r_heartbeat_file_write,
593 .llseek = generic_file_llseek,
596 static const struct file_operations remote_settings_fops = {
598 .release = remote_settings_file_close,
599 .read = remote_settings_file_read,
600 .write = remote_settings_file_write,
601 .llseek = generic_file_llseek,
605 static void ibmasmfs_create_files (struct super_block *sb)
607 struct list_head *entry;
608 struct service_processor *sp;
610 list_for_each(entry, &service_processors) {
612 struct dentry *remote_dir;
613 sp = list_entry(entry, struct service_processor, node);
614 dir = ibmasmfs_create_dir(sb, sb->s_root, sp->dirname);
618 ibmasmfs_create_file(sb, dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
619 ibmasmfs_create_file(sb, dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
620 ibmasmfs_create_file(sb, dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
622 remote_dir = ibmasmfs_create_dir(sb, dir, "remote_video");
626 ibmasmfs_create_file(sb, remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
627 ibmasmfs_create_file(sb, remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
628 ibmasmfs_create_file(sb, remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);