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 * dymamically 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 <asm/uaccess.h>
82 #include "dot_command.h"
84 #define IBMASMFS_MAGIC 0x66726f67
86 static LIST_HEAD(service_processors
);
88 static struct inode
*ibmasmfs_make_inode(struct super_block
*sb
, int mode
);
89 static void ibmasmfs_create_files (struct super_block
*sb
, struct dentry
*root
);
90 static int ibmasmfs_fill_super (struct super_block
*sb
, void *data
, int silent
);
93 static int ibmasmfs_get_super(struct file_system_type
*fst
,
94 int flags
, const char *name
, void *data
,
97 return get_sb_single(fst
, flags
, data
, ibmasmfs_fill_super
, mnt
);
100 static 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 .get_sb
= ibmasmfs_get_super
,
111 .kill_sb
= kill_litter_super
,
114 static int ibmasmfs_fill_super (struct super_block
*sb
, void *data
, int silent
)
117 struct dentry
*root_dentry
;
119 sb
->s_blocksize
= PAGE_CACHE_SIZE
;
120 sb
->s_blocksize_bits
= PAGE_CACHE_SHIFT
;
121 sb
->s_magic
= IBMASMFS_MAGIC
;
122 sb
->s_op
= &ibmasmfs_s_ops
;
125 root
= ibmasmfs_make_inode (sb
, S_IFDIR
| 0500);
129 root
->i_op
= &simple_dir_inode_operations
;
130 root
->i_fop
= ibmasmfs_dir_ops
;
132 root_dentry
= d_alloc_root(root
);
137 sb
->s_root
= root_dentry
;
139 ibmasmfs_create_files(sb
, root_dentry
);
143 static struct inode
*ibmasmfs_make_inode(struct super_block
*sb
, int mode
)
145 struct inode
*ret
= new_inode(sb
);
149 ret
->i_atime
= ret
->i_mtime
= ret
->i_ctime
= CURRENT_TIME
;
154 static struct dentry
*ibmasmfs_create_file (struct super_block
*sb
,
155 struct dentry
*parent
,
157 const struct file_operations
*fops
,
161 struct dentry
*dentry
;
164 dentry
= d_alloc_name(parent
, name
);
168 inode
= ibmasmfs_make_inode(sb
, S_IFREG
| mode
);
175 inode
->i_private
= data
;
177 d_add(dentry
, inode
);
181 static struct dentry
*ibmasmfs_create_dir (struct super_block
*sb
,
182 struct dentry
*parent
,
185 struct dentry
*dentry
;
188 dentry
= d_alloc_name(parent
, name
);
192 inode
= ibmasmfs_make_inode(sb
, S_IFDIR
| 0500);
198 inode
->i_op
= &simple_dir_inode_operations
;
199 inode
->i_fop
= ibmasmfs_dir_ops
;
201 d_add(dentry
, inode
);
205 int ibmasmfs_register(void)
207 return register_filesystem(&ibmasmfs_type
);
210 void ibmasmfs_unregister(void)
212 unregister_filesystem(&ibmasmfs_type
);
215 void ibmasmfs_add_sp(struct service_processor
*sp
)
217 list_add(&sp
->node
, &service_processors
);
220 /* struct to save state between command file operations */
221 struct ibmasmfs_command_data
{
222 struct service_processor
*sp
;
223 struct command
*command
;
226 /* struct to save state between event file operations */
227 struct ibmasmfs_event_data
{
228 struct service_processor
*sp
;
229 struct event_reader reader
;
233 /* struct to save state between reverse heartbeat file operations */
234 struct ibmasmfs_heartbeat_data
{
235 struct service_processor
*sp
;
236 struct reverse_heartbeat heartbeat
;
240 static int command_file_open(struct inode
*inode
, struct file
*file
)
242 struct ibmasmfs_command_data
*command_data
;
244 if (!inode
->i_private
)
247 command_data
= kmalloc(sizeof(struct ibmasmfs_command_data
), GFP_KERNEL
);
251 command_data
->command
= NULL
;
252 command_data
->sp
= inode
->i_private
;
253 file
->private_data
= command_data
;
257 static int command_file_close(struct inode
*inode
, struct file
*file
)
259 struct ibmasmfs_command_data
*command_data
= file
->private_data
;
261 if (command_data
->command
)
262 command_put(command_data
->command
);
268 static ssize_t
command_file_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
270 struct ibmasmfs_command_data
*command_data
= file
->private_data
;
277 if (count
== 0 || count
> IBMASM_CMD_MAX_BUFFER_SIZE
)
282 spin_lock_irqsave(&command_data
->sp
->lock
, flags
);
283 cmd
= command_data
->command
;
285 spin_unlock_irqrestore(&command_data
->sp
->lock
, flags
);
288 command_data
->command
= NULL
;
289 spin_unlock_irqrestore(&command_data
->sp
->lock
, flags
);
291 if (cmd
->status
!= IBMASM_CMD_COMPLETE
) {
295 len
= min(count
, cmd
->buffer_size
);
296 if (copy_to_user(buf
, cmd
->buffer
, len
)) {
305 static ssize_t
command_file_write(struct file
*file
, const char __user
*ubuff
, size_t count
, loff_t
*offset
)
307 struct ibmasmfs_command_data
*command_data
= file
->private_data
;
313 if (count
== 0 || count
> IBMASM_CMD_MAX_BUFFER_SIZE
)
318 /* commands are executed sequentially, only one command at a time */
319 if (command_data
->command
)
322 cmd
= ibmasm_new_command(command_data
->sp
, count
);
326 if (copy_from_user(cmd
->buffer
, ubuff
, count
)) {
331 spin_lock_irqsave(&command_data
->sp
->lock
, flags
);
332 if (command_data
->command
) {
333 spin_unlock_irqrestore(&command_data
->sp
->lock
, flags
);
337 command_data
->command
= cmd
;
338 spin_unlock_irqrestore(&command_data
->sp
->lock
, flags
);
340 ibmasm_exec_command(command_data
->sp
, cmd
);
341 ibmasm_wait_for_response(cmd
, get_dot_command_timeout(cmd
->buffer
));
346 static int event_file_open(struct inode
*inode
, struct file
*file
)
348 struct ibmasmfs_event_data
*event_data
;
349 struct service_processor
*sp
;
351 if (!inode
->i_private
)
354 sp
= inode
->i_private
;
356 event_data
= kmalloc(sizeof(struct ibmasmfs_event_data
), GFP_KERNEL
);
360 ibmasm_event_reader_register(sp
, &event_data
->reader
);
363 event_data
->active
= 0;
364 file
->private_data
= event_data
;
368 static int event_file_close(struct inode
*inode
, struct file
*file
)
370 struct ibmasmfs_event_data
*event_data
= file
->private_data
;
372 ibmasm_event_reader_unregister(event_data
->sp
, &event_data
->reader
);
377 static ssize_t
event_file_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
379 struct ibmasmfs_event_data
*event_data
= file
->private_data
;
380 struct event_reader
*reader
= &event_data
->reader
;
381 struct service_processor
*sp
= event_data
->sp
;
387 if (count
== 0 || count
> IBMASM_EVENT_MAX_SIZE
)
392 spin_lock_irqsave(&sp
->lock
, flags
);
393 if (event_data
->active
) {
394 spin_unlock_irqrestore(&sp
->lock
, flags
);
397 event_data
->active
= 1;
398 spin_unlock_irqrestore(&sp
->lock
, flags
);
400 ret
= ibmasm_get_next_event(sp
, reader
);
404 if (count
< reader
->data_size
) {
409 if (copy_to_user(buf
, reader
->data
, reader
->data_size
)) {
413 ret
= reader
->data_size
;
416 event_data
->active
= 0;
420 static ssize_t
event_file_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
422 struct ibmasmfs_event_data
*event_data
= file
->private_data
;
431 ibmasm_cancel_next_event(&event_data
->reader
);
435 static int r_heartbeat_file_open(struct inode
*inode
, struct file
*file
)
437 struct ibmasmfs_heartbeat_data
*rhbeat
;
439 if (!inode
->i_private
)
442 rhbeat
= kmalloc(sizeof(struct ibmasmfs_heartbeat_data
), GFP_KERNEL
);
446 rhbeat
->sp
= inode
->i_private
;
448 ibmasm_init_reverse_heartbeat(rhbeat
->sp
, &rhbeat
->heartbeat
);
449 file
->private_data
= rhbeat
;
453 static int r_heartbeat_file_close(struct inode
*inode
, struct file
*file
)
455 struct ibmasmfs_heartbeat_data
*rhbeat
= file
->private_data
;
461 static ssize_t
r_heartbeat_file_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
463 struct ibmasmfs_heartbeat_data
*rhbeat
= file
->private_data
;
469 if (count
== 0 || count
> 1024)
474 /* allow only one reverse heartbeat per process */
475 spin_lock_irqsave(&rhbeat
->sp
->lock
, flags
);
476 if (rhbeat
->active
) {
477 spin_unlock_irqrestore(&rhbeat
->sp
->lock
, flags
);
481 spin_unlock_irqrestore(&rhbeat
->sp
->lock
, flags
);
483 result
= ibmasm_start_reverse_heartbeat(rhbeat
->sp
, &rhbeat
->heartbeat
);
489 static ssize_t
r_heartbeat_file_write(struct file
*file
, const char __user
*buf
, size_t count
, loff_t
*offset
)
491 struct ibmasmfs_heartbeat_data
*rhbeat
= file
->private_data
;
501 ibmasm_stop_reverse_heartbeat(&rhbeat
->heartbeat
);
506 static int remote_settings_file_open(struct inode
*inode
, struct file
*file
)
508 file
->private_data
= inode
->i_private
;
512 static int remote_settings_file_close(struct inode
*inode
, struct file
*file
)
517 static ssize_t
remote_settings_file_read(struct file
*file
, char __user
*buf
, size_t count
, loff_t
*offset
)
519 void __iomem
*address
= (void __iomem
*)file
->private_data
;
527 if (count
== 0 || count
> 1024)
532 page
= (unsigned char *)__get_free_page(GFP_KERNEL
);
536 value
= readl(address
);
537 len
= sprintf(page
, "%d\n", value
);
539 if (copy_to_user(buf
, page
, len
)) {
547 free_page((unsigned long)page
);
551 static ssize_t
remote_settings_file_write(struct file
*file
, const char __user
*ubuff
, size_t count
, loff_t
*offset
)
553 void __iomem
*address
= (void __iomem
*)file
->private_data
;
559 if (count
== 0 || count
> 1024)
564 buff
= kzalloc (count
+ 1, GFP_KERNEL
);
569 if (copy_from_user(buff
, ubuff
, count
)) {
574 value
= simple_strtoul(buff
, NULL
, 10);
575 writel(value
, address
);
581 static const struct file_operations command_fops
= {
582 .open
= command_file_open
,
583 .release
= command_file_close
,
584 .read
= command_file_read
,
585 .write
= command_file_write
,
588 static const struct file_operations event_fops
= {
589 .open
= event_file_open
,
590 .release
= event_file_close
,
591 .read
= event_file_read
,
592 .write
= event_file_write
,
595 static const struct file_operations r_heartbeat_fops
= {
596 .open
= r_heartbeat_file_open
,
597 .release
= r_heartbeat_file_close
,
598 .read
= r_heartbeat_file_read
,
599 .write
= r_heartbeat_file_write
,
602 static const struct file_operations remote_settings_fops
= {
603 .open
= remote_settings_file_open
,
604 .release
= remote_settings_file_close
,
605 .read
= remote_settings_file_read
,
606 .write
= remote_settings_file_write
,
610 static void ibmasmfs_create_files (struct super_block
*sb
, struct dentry
*root
)
612 struct list_head
*entry
;
613 struct service_processor
*sp
;
615 list_for_each(entry
, &service_processors
) {
617 struct dentry
*remote_dir
;
618 sp
= list_entry(entry
, struct service_processor
, node
);
619 dir
= ibmasmfs_create_dir(sb
, root
, sp
->dirname
);
623 ibmasmfs_create_file(sb
, dir
, "command", &command_fops
, sp
, S_IRUSR
|S_IWUSR
);
624 ibmasmfs_create_file(sb
, dir
, "event", &event_fops
, sp
, S_IRUSR
|S_IWUSR
);
625 ibmasmfs_create_file(sb
, dir
, "reverse_heartbeat", &r_heartbeat_fops
, sp
, S_IRUSR
|S_IWUSR
);
627 remote_dir
= ibmasmfs_create_dir(sb
, dir
, "remote_video");
631 ibmasmfs_create_file(sb
, remote_dir
, "width", &remote_settings_fops
, (void *)display_width(sp
), S_IRUSR
|S_IWUSR
);
632 ibmasmfs_create_file(sb
, remote_dir
, "height", &remote_settings_fops
, (void *)display_height(sp
), S_IRUSR
|S_IWUSR
);
633 ibmasmfs_create_file(sb
, remote_dir
, "depth", &remote_settings_fops
, (void *)display_depth(sp
), S_IRUSR
|S_IWUSR
);