1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IBM ASM Service Processor Device Driver
5 * Copyright (C) IBM Corporation, 2004
11 * Parts of this code are based on an article by Jonathan Corbet
12 * that appeared in Linux Weekly News.
17 * The IBMASM file virtual filesystem. It creates the following hierarchy
18 * dynamically when mounted from user space:
24 * | |-- reverse_heartbeat
35 * |-- reverse_heartbeat
41 * For each service processor the following files are created:
43 * command: execute dot commands
44 * write: execute a dot command on the service processor
45 * read: return the result of a previously executed dot command
47 * events: listen for service processor events
48 * read: sleep (interruptible) until an event occurs
49 * write: wakeup sleeping event listener
51 * reverse_heartbeat: send a heartbeat to the service processor
52 * read: sleep (interruptible) until the reverse heartbeat fails
53 * write: wakeup sleeping heartbeat listener
57 * remote_video/width: control remote display settings
63 #include <linux/pagemap.h>
64 #include <linux/slab.h>
65 #include <linux/uaccess.h>
69 #include "dot_command.h"
71 #define IBMASMFS_MAGIC 0x66726f67
73 static LIST_HEAD(service_processors);
75 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode);
76 static void ibmasmfs_create_files (struct super_block *sb);
77 static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent);
80 static struct dentry *ibmasmfs_mount(struct file_system_type *fst,
81 int flags, const char *name, void *data)
83 return mount_single(fst, flags, data, ibmasmfs_fill_super);
86 static const struct super_operations ibmasmfs_s_ops = {
87 .statfs = simple_statfs,
88 .drop_inode = generic_delete_inode,
91 static const struct file_operations *ibmasmfs_dir_ops = &simple_dir_operations;
93 static struct file_system_type ibmasmfs_type = {
96 .mount = ibmasmfs_mount,
97 .kill_sb = kill_litter_super,
99 MODULE_ALIAS_FS("ibmasmfs");
101 static int ibmasmfs_fill_super (struct super_block *sb, void *data, int silent)
105 sb->s_blocksize = PAGE_SIZE;
106 sb->s_blocksize_bits = PAGE_SHIFT;
107 sb->s_magic = IBMASMFS_MAGIC;
108 sb->s_op = &ibmasmfs_s_ops;
111 root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
115 root->i_op = &simple_dir_inode_operations;
116 root->i_fop = ibmasmfs_dir_ops;
118 sb->s_root = d_make_root(root);
122 ibmasmfs_create_files(sb);
126 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
128 struct inode *ret = new_inode(sb);
131 ret->i_ino = get_next_ino();
133 ret->i_atime = ret->i_mtime = ret->i_ctime = current_time(ret);
138 static struct dentry *ibmasmfs_create_file(struct dentry *parent,
140 const struct file_operations *fops,
144 struct dentry *dentry;
147 dentry = d_alloc_name(parent, name);
151 inode = ibmasmfs_make_inode(parent->d_sb, S_IFREG | mode);
158 inode->i_private = data;
160 d_add(dentry, inode);
164 static struct dentry *ibmasmfs_create_dir(struct dentry *parent,
167 struct dentry *dentry;
170 dentry = d_alloc_name(parent, name);
174 inode = ibmasmfs_make_inode(parent->d_sb, S_IFDIR | 0500);
180 inode->i_op = &simple_dir_inode_operations;
181 inode->i_fop = ibmasmfs_dir_ops;
183 d_add(dentry, inode);
187 int ibmasmfs_register(void)
189 return register_filesystem(&ibmasmfs_type);
192 void ibmasmfs_unregister(void)
194 unregister_filesystem(&ibmasmfs_type);
197 void ibmasmfs_add_sp(struct service_processor *sp)
199 list_add(&sp->node, &service_processors);
202 /* struct to save state between command file operations */
203 struct ibmasmfs_command_data {
204 struct service_processor *sp;
205 struct command *command;
208 /* struct to save state between event file operations */
209 struct ibmasmfs_event_data {
210 struct service_processor *sp;
211 struct event_reader reader;
215 /* struct to save state between reverse heartbeat file operations */
216 struct ibmasmfs_heartbeat_data {
217 struct service_processor *sp;
218 struct reverse_heartbeat heartbeat;
222 static int command_file_open(struct inode *inode, struct file *file)
224 struct ibmasmfs_command_data *command_data;
226 if (!inode->i_private)
229 command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
233 command_data->command = NULL;
234 command_data->sp = inode->i_private;
235 file->private_data = command_data;
239 static int command_file_close(struct inode *inode, struct file *file)
241 struct ibmasmfs_command_data *command_data = file->private_data;
243 if (command_data->command)
244 command_put(command_data->command);
250 static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
252 struct ibmasmfs_command_data *command_data = file->private_data;
259 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
264 spin_lock_irqsave(&command_data->sp->lock, flags);
265 cmd = command_data->command;
267 spin_unlock_irqrestore(&command_data->sp->lock, flags);
270 command_data->command = NULL;
271 spin_unlock_irqrestore(&command_data->sp->lock, flags);
273 if (cmd->status != IBMASM_CMD_COMPLETE) {
277 len = min(count, cmd->buffer_size);
278 if (copy_to_user(buf, cmd->buffer, len)) {
287 static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
289 struct ibmasmfs_command_data *command_data = file->private_data;
295 if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
300 /* commands are executed sequentially, only one command at a time */
301 if (command_data->command)
304 cmd = ibmasm_new_command(command_data->sp, count);
308 if (copy_from_user(cmd->buffer, ubuff, count)) {
313 spin_lock_irqsave(&command_data->sp->lock, flags);
314 if (command_data->command) {
315 spin_unlock_irqrestore(&command_data->sp->lock, flags);
319 command_data->command = cmd;
320 spin_unlock_irqrestore(&command_data->sp->lock, flags);
322 ibmasm_exec_command(command_data->sp, cmd);
323 ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
328 static int event_file_open(struct inode *inode, struct file *file)
330 struct ibmasmfs_event_data *event_data;
331 struct service_processor *sp;
333 if (!inode->i_private)
336 sp = inode->i_private;
338 event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
342 ibmasm_event_reader_register(sp, &event_data->reader);
345 event_data->active = 0;
346 file->private_data = event_data;
350 static int event_file_close(struct inode *inode, struct file *file)
352 struct ibmasmfs_event_data *event_data = file->private_data;
354 ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
359 static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
361 struct ibmasmfs_event_data *event_data = file->private_data;
362 struct event_reader *reader = &event_data->reader;
363 struct service_processor *sp = event_data->sp;
369 if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
374 spin_lock_irqsave(&sp->lock, flags);
375 if (event_data->active) {
376 spin_unlock_irqrestore(&sp->lock, flags);
379 event_data->active = 1;
380 spin_unlock_irqrestore(&sp->lock, flags);
382 ret = ibmasm_get_next_event(sp, reader);
386 if (count < reader->data_size) {
391 if (copy_to_user(buf, reader->data, reader->data_size)) {
395 ret = reader->data_size;
398 event_data->active = 0;
402 static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
404 struct ibmasmfs_event_data *event_data = file->private_data;
413 ibmasm_cancel_next_event(&event_data->reader);
417 static int r_heartbeat_file_open(struct inode *inode, struct file *file)
419 struct ibmasmfs_heartbeat_data *rhbeat;
421 if (!inode->i_private)
424 rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
428 rhbeat->sp = inode->i_private;
430 ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
431 file->private_data = rhbeat;
435 static int r_heartbeat_file_close(struct inode *inode, struct file *file)
437 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
443 static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
445 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
451 if (count == 0 || count > 1024)
456 /* allow only one reverse heartbeat per process */
457 spin_lock_irqsave(&rhbeat->sp->lock, flags);
458 if (rhbeat->active) {
459 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
463 spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
465 result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
471 static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
473 struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
483 ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
488 static int remote_settings_file_close(struct inode *inode, struct file *file)
493 static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
495 void __iomem *address = (void __iomem *)file->private_data;
500 value = readl(address);
501 len = snprintf(lbuf, sizeof(lbuf), "%d\n", value);
503 return simple_read_from_buffer(buf, count, offset, lbuf, len);
506 static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
508 void __iomem *address = (void __iomem *)file->private_data;
514 if (count == 0 || count > 1024)
519 buff = kzalloc (count + 1, GFP_KERNEL);
524 if (copy_from_user(buff, ubuff, count)) {
529 value = simple_strtoul(buff, NULL, 10);
530 writel(value, address);
536 static const struct file_operations command_fops = {
537 .open = command_file_open,
538 .release = command_file_close,
539 .read = command_file_read,
540 .write = command_file_write,
541 .llseek = generic_file_llseek,
544 static const struct file_operations event_fops = {
545 .open = event_file_open,
546 .release = event_file_close,
547 .read = event_file_read,
548 .write = event_file_write,
549 .llseek = generic_file_llseek,
552 static const struct file_operations r_heartbeat_fops = {
553 .open = r_heartbeat_file_open,
554 .release = r_heartbeat_file_close,
555 .read = r_heartbeat_file_read,
556 .write = r_heartbeat_file_write,
557 .llseek = generic_file_llseek,
560 static const struct file_operations remote_settings_fops = {
562 .release = remote_settings_file_close,
563 .read = remote_settings_file_read,
564 .write = remote_settings_file_write,
565 .llseek = generic_file_llseek,
569 static void ibmasmfs_create_files (struct super_block *sb)
571 struct list_head *entry;
572 struct service_processor *sp;
574 list_for_each(entry, &service_processors) {
576 struct dentry *remote_dir;
577 sp = list_entry(entry, struct service_processor, node);
578 dir = ibmasmfs_create_dir(sb->s_root, sp->dirname);
582 ibmasmfs_create_file(dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
583 ibmasmfs_create_file(dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
584 ibmasmfs_create_file(dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
586 remote_dir = ibmasmfs_create_dir(dir, "remote_video");
590 ibmasmfs_create_file(remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
591 ibmasmfs_create_file(remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
592 ibmasmfs_create_file(remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);