xref: /openbmc/linux/drivers/misc/ibmasm/ibmasmfs.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  * IBM ASM Service Processor Device Driver
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  * Copyright (C) IBM Corporation, 2004
61da177e4SLinus Torvalds  *
7d36b6910SAl Viro  * Author: Max Asböck <amax@us.ibm.com>
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*
111da177e4SLinus Torvalds  * Parts of this code are based on an article by Jonathan Corbet
121da177e4SLinus Torvalds  * that appeared in Linux Weekly News.
131da177e4SLinus Torvalds  */
141da177e4SLinus Torvalds 
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds /*
171da177e4SLinus Torvalds  * The IBMASM file virtual filesystem. It creates the following hierarchy
185ecd602eSJustin P. Mattock  * dynamically when mounted from user space:
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  *    /ibmasm
211da177e4SLinus Torvalds  *    |-- 0
221da177e4SLinus Torvalds  *    |   |-- command
231da177e4SLinus Torvalds  *    |   |-- event
241da177e4SLinus Torvalds  *    |   |-- reverse_heartbeat
251da177e4SLinus Torvalds  *    |   `-- remote_video
261da177e4SLinus Torvalds  *    |       |-- depth
271da177e4SLinus Torvalds  *    |       |-- height
281da177e4SLinus Torvalds  *    |       `-- width
291da177e4SLinus Torvalds  *    .
301da177e4SLinus Torvalds  *    .
311da177e4SLinus Torvalds  *    .
321da177e4SLinus Torvalds  *    `-- n
331da177e4SLinus Torvalds  *        |-- command
341da177e4SLinus Torvalds  *        |-- event
351da177e4SLinus Torvalds  *        |-- reverse_heartbeat
361da177e4SLinus Torvalds  *        `-- remote_video
371da177e4SLinus Torvalds  *            |-- depth
381da177e4SLinus Torvalds  *            |-- height
391da177e4SLinus Torvalds  *            `-- width
401da177e4SLinus Torvalds  *
411da177e4SLinus Torvalds  * For each service processor the following files are created:
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * command: execute dot commands
441da177e4SLinus Torvalds  *	write: execute a dot command on the service processor
451da177e4SLinus Torvalds  *	read: return the result of a previously executed dot command
461da177e4SLinus Torvalds  *
471da177e4SLinus Torvalds  * events: listen for service processor events
481da177e4SLinus Torvalds  *	read: sleep (interruptible) until an event occurs
491da177e4SLinus Torvalds  *      write: wakeup sleeping event listener
501da177e4SLinus Torvalds  *
511da177e4SLinus Torvalds  * reverse_heartbeat: send a heartbeat to the service processor
521da177e4SLinus Torvalds  *	read: sleep (interruptible) until the reverse heartbeat fails
531da177e4SLinus Torvalds  *      write: wakeup sleeping heartbeat listener
541da177e4SLinus Torvalds  *
551da177e4SLinus Torvalds  * remote_video/width
561da177e4SLinus Torvalds  * remote_video/height
571da177e4SLinus Torvalds  * remote_video/width: control remote display settings
581da177e4SLinus Torvalds  *	write: set value
591da177e4SLinus Torvalds  *	read: read value
601da177e4SLinus Torvalds  */
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #include <linux/fs.h>
63129f809dSDavid Howells #include <linux/fs_context.h>
641da177e4SLinus Torvalds #include <linux/pagemap.h>
655a0e3ad6STejun Heo #include <linux/slab.h>
667c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
671da177e4SLinus Torvalds #include <asm/io.h>
681da177e4SLinus Torvalds #include "ibmasm.h"
691da177e4SLinus Torvalds #include "remote.h"
701da177e4SLinus Torvalds #include "dot_command.h"
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds #define IBMASMFS_MAGIC 0x66726f67
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds static LIST_HEAD(service_processors);
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode);
77318ceed0SAl Viro static void ibmasmfs_create_files (struct super_block *sb);
78129f809dSDavid Howells static int ibmasmfs_fill_super(struct super_block *sb, struct fs_context *fc);
791da177e4SLinus Torvalds 
ibmasmfs_get_tree(struct fs_context * fc)80129f809dSDavid Howells static int ibmasmfs_get_tree(struct fs_context *fc)
811da177e4SLinus Torvalds {
82129f809dSDavid Howells 	return get_tree_single(fc, ibmasmfs_fill_super);
83129f809dSDavid Howells }
84129f809dSDavid Howells 
85129f809dSDavid Howells static const struct fs_context_operations ibmasmfs_context_ops = {
86129f809dSDavid Howells 	.get_tree	= ibmasmfs_get_tree,
87129f809dSDavid Howells };
88129f809dSDavid Howells 
ibmasmfs_init_fs_context(struct fs_context * fc)89129f809dSDavid Howells static int ibmasmfs_init_fs_context(struct fs_context *fc)
90129f809dSDavid Howells {
91129f809dSDavid Howells 	fc->ops = &ibmasmfs_context_ops;
92129f809dSDavid Howells 	return 0;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
95b87221deSAlexey Dobriyan static const struct super_operations ibmasmfs_s_ops = {
961da177e4SLinus Torvalds 	.statfs		= simple_statfs,
971da177e4SLinus Torvalds 	.drop_inode	= generic_delete_inode,
981da177e4SLinus Torvalds };
991da177e4SLinus Torvalds 
1004b6f5d20SArjan van de Ven static const struct file_operations *ibmasmfs_dir_ops = &simple_dir_operations;
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds static struct file_system_type ibmasmfs_type = {
1031da177e4SLinus Torvalds 	.owner          = THIS_MODULE,
1041da177e4SLinus Torvalds 	.name           = "ibmasmfs",
105129f809dSDavid Howells 	.init_fs_context = ibmasmfs_init_fs_context,
1061da177e4SLinus Torvalds 	.kill_sb        = kill_litter_super,
1071da177e4SLinus Torvalds };
1087f78e035SEric W. Biederman MODULE_ALIAS_FS("ibmasmfs");
1091da177e4SLinus Torvalds 
ibmasmfs_fill_super(struct super_block * sb,struct fs_context * fc)110129f809dSDavid Howells static int ibmasmfs_fill_super(struct super_block *sb, struct fs_context *fc)
1111da177e4SLinus Torvalds {
1121da177e4SLinus Torvalds 	struct inode *root;
1131da177e4SLinus Torvalds 
11409cbfeafSKirill A. Shutemov 	sb->s_blocksize = PAGE_SIZE;
11509cbfeafSKirill A. Shutemov 	sb->s_blocksize_bits = PAGE_SHIFT;
1161da177e4SLinus Torvalds 	sb->s_magic = IBMASMFS_MAGIC;
1171da177e4SLinus Torvalds 	sb->s_op = &ibmasmfs_s_ops;
1181da177e4SLinus Torvalds 	sb->s_time_gran = 1;
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
1211da177e4SLinus Torvalds 	if (!root)
1221da177e4SLinus Torvalds 		return -ENOMEM;
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds 	root->i_op = &simple_dir_inode_operations;
1251da177e4SLinus Torvalds 	root->i_fop = ibmasmfs_dir_ops;
1261da177e4SLinus Torvalds 
127318ceed0SAl Viro 	sb->s_root = d_make_root(root);
128318ceed0SAl Viro 	if (!sb->s_root)
1291da177e4SLinus Torvalds 		return -ENOMEM;
1301da177e4SLinus Torvalds 
131318ceed0SAl Viro 	ibmasmfs_create_files(sb);
1321da177e4SLinus Torvalds 	return 0;
1331da177e4SLinus Torvalds }
1341da177e4SLinus Torvalds 
ibmasmfs_make_inode(struct super_block * sb,int mode)1351da177e4SLinus Torvalds static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
1361da177e4SLinus Torvalds {
1371da177e4SLinus Torvalds 	struct inode *ret = new_inode(sb);
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 	if (ret) {
14085fe4025SChristoph Hellwig 		ret->i_ino = get_next_ino();
1411da177e4SLinus Torvalds 		ret->i_mode = mode;
142*41441cecSJeff Layton 		ret->i_atime = ret->i_mtime = inode_set_ctime_current(ret);
1431da177e4SLinus Torvalds 	}
1441da177e4SLinus Torvalds 	return ret;
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds 
ibmasmfs_create_file(struct dentry * parent,const char * name,const struct file_operations * fops,void * data,int mode)1470507c78aSAl Viro static struct dentry *ibmasmfs_create_file(struct dentry *parent,
1481da177e4SLinus Torvalds 			const char *name,
149d54b1fdbSArjan van de Ven 			const struct file_operations *fops,
1501da177e4SLinus Torvalds 			void *data,
1511da177e4SLinus Torvalds 			int mode)
1521da177e4SLinus Torvalds {
1531da177e4SLinus Torvalds 	struct dentry *dentry;
1541da177e4SLinus Torvalds 	struct inode *inode;
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	dentry = d_alloc_name(parent, name);
1571da177e4SLinus Torvalds 	if (!dentry)
1581da177e4SLinus Torvalds 		return NULL;
1591da177e4SLinus Torvalds 
1600507c78aSAl Viro 	inode = ibmasmfs_make_inode(parent->d_sb, S_IFREG | mode);
1611da177e4SLinus Torvalds 	if (!inode) {
1621da177e4SLinus Torvalds 		dput(dentry);
1631da177e4SLinus Torvalds 		return NULL;
1641da177e4SLinus Torvalds 	}
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	inode->i_fop = fops;
1678e18e294STheodore Ts'o 	inode->i_private = data;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	d_add(dentry, inode);
1701da177e4SLinus Torvalds 	return dentry;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
ibmasmfs_create_dir(struct dentry * parent,const char * name)1730507c78aSAl Viro static struct dentry *ibmasmfs_create_dir(struct dentry *parent,
1741da177e4SLinus Torvalds 				const char *name)
1751da177e4SLinus Torvalds {
1761da177e4SLinus Torvalds 	struct dentry *dentry;
1771da177e4SLinus Torvalds 	struct inode *inode;
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	dentry = d_alloc_name(parent, name);
1801da177e4SLinus Torvalds 	if (!dentry)
1811da177e4SLinus Torvalds 		return NULL;
1821da177e4SLinus Torvalds 
1830507c78aSAl Viro 	inode = ibmasmfs_make_inode(parent->d_sb, S_IFDIR | 0500);
1841da177e4SLinus Torvalds 	if (!inode) {
1851da177e4SLinus Torvalds 		dput(dentry);
1861da177e4SLinus Torvalds 		return NULL;
1871da177e4SLinus Torvalds 	}
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	inode->i_op = &simple_dir_inode_operations;
1901da177e4SLinus Torvalds 	inode->i_fop = ibmasmfs_dir_ops;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	d_add(dentry, inode);
1931da177e4SLinus Torvalds 	return dentry;
1941da177e4SLinus Torvalds }
1951da177e4SLinus Torvalds 
ibmasmfs_register(void)1961da177e4SLinus Torvalds int ibmasmfs_register(void)
1971da177e4SLinus Torvalds {
1981da177e4SLinus Torvalds 	return register_filesystem(&ibmasmfs_type);
1991da177e4SLinus Torvalds }
2001da177e4SLinus Torvalds 
ibmasmfs_unregister(void)2011da177e4SLinus Torvalds void ibmasmfs_unregister(void)
2021da177e4SLinus Torvalds {
2031da177e4SLinus Torvalds 	unregister_filesystem(&ibmasmfs_type);
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds 
ibmasmfs_add_sp(struct service_processor * sp)2061da177e4SLinus Torvalds void ibmasmfs_add_sp(struct service_processor *sp)
2071da177e4SLinus Torvalds {
2081da177e4SLinus Torvalds 	list_add(&sp->node, &service_processors);
2091da177e4SLinus Torvalds }
2101da177e4SLinus Torvalds 
2111da177e4SLinus Torvalds /* struct to save state between command file operations */
2121da177e4SLinus Torvalds struct ibmasmfs_command_data {
2131da177e4SLinus Torvalds 	struct service_processor	*sp;
2141da177e4SLinus Torvalds 	struct command			*command;
2151da177e4SLinus Torvalds };
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds /* struct to save state between event file operations */
2181da177e4SLinus Torvalds struct ibmasmfs_event_data {
2191da177e4SLinus Torvalds 	struct service_processor	*sp;
2201da177e4SLinus Torvalds 	struct event_reader		reader;
2211da177e4SLinus Torvalds 	int				active;
2221da177e4SLinus Torvalds };
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds /* struct to save state between reverse heartbeat file operations */
2251da177e4SLinus Torvalds struct ibmasmfs_heartbeat_data {
2261da177e4SLinus Torvalds 	struct service_processor	*sp;
2271da177e4SLinus Torvalds 	struct reverse_heartbeat	heartbeat;
2281da177e4SLinus Torvalds 	int				active;
2291da177e4SLinus Torvalds };
2301da177e4SLinus Torvalds 
command_file_open(struct inode * inode,struct file * file)2311da177e4SLinus Torvalds static int command_file_open(struct inode *inode, struct file *file)
2321da177e4SLinus Torvalds {
2331da177e4SLinus Torvalds 	struct ibmasmfs_command_data *command_data;
2341da177e4SLinus Torvalds 
2358e18e294STheodore Ts'o 	if (!inode->i_private)
2361da177e4SLinus Torvalds 		return -ENODEV;
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	command_data = kmalloc(sizeof(struct ibmasmfs_command_data), GFP_KERNEL);
2391da177e4SLinus Torvalds 	if (!command_data)
2401da177e4SLinus Torvalds 		return -ENOMEM;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	command_data->command = NULL;
2438e18e294STheodore Ts'o 	command_data->sp = inode->i_private;
2441da177e4SLinus Torvalds 	file->private_data = command_data;
2451da177e4SLinus Torvalds 	return 0;
2461da177e4SLinus Torvalds }
2471da177e4SLinus Torvalds 
command_file_close(struct inode * inode,struct file * file)2481da177e4SLinus Torvalds static int command_file_close(struct inode *inode, struct file *file)
2491da177e4SLinus Torvalds {
2501da177e4SLinus Torvalds 	struct ibmasmfs_command_data *command_data = file->private_data;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	if (command_data->command)
2531da177e4SLinus Torvalds 		command_put(command_data->command);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	kfree(command_data);
2561da177e4SLinus Torvalds 	return 0;
2571da177e4SLinus Torvalds }
2581da177e4SLinus Torvalds 
command_file_read(struct file * file,char __user * buf,size_t count,loff_t * offset)2591da177e4SLinus Torvalds static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
2601da177e4SLinus Torvalds {
2611da177e4SLinus Torvalds 	struct ibmasmfs_command_data *command_data = file->private_data;
2621da177e4SLinus Torvalds 	struct command *cmd;
2631da177e4SLinus Torvalds 	int len;
2641da177e4SLinus Torvalds 	unsigned long flags;
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	if (*offset < 0)
2671da177e4SLinus Torvalds 		return -EINVAL;
2681da177e4SLinus Torvalds 	if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
2691da177e4SLinus Torvalds 		return 0;
2701da177e4SLinus Torvalds 	if (*offset != 0)
2711da177e4SLinus Torvalds 		return 0;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds 	spin_lock_irqsave(&command_data->sp->lock, flags);
2741da177e4SLinus Torvalds 	cmd = command_data->command;
2751da177e4SLinus Torvalds 	if (cmd == NULL) {
2761da177e4SLinus Torvalds 		spin_unlock_irqrestore(&command_data->sp->lock, flags);
2771da177e4SLinus Torvalds 		return 0;
2781da177e4SLinus Torvalds 	}
2791da177e4SLinus Torvalds 	command_data->command = NULL;
2801da177e4SLinus Torvalds 	spin_unlock_irqrestore(&command_data->sp->lock, flags);
2811da177e4SLinus Torvalds 
2821da177e4SLinus Torvalds 	if (cmd->status != IBMASM_CMD_COMPLETE) {
2831da177e4SLinus Torvalds 		command_put(cmd);
2841da177e4SLinus Torvalds 		return -EIO;
2851da177e4SLinus Torvalds 	}
2861da177e4SLinus Torvalds 	len = min(count, cmd->buffer_size);
2871da177e4SLinus Torvalds 	if (copy_to_user(buf, cmd->buffer, len)) {
2881da177e4SLinus Torvalds 		command_put(cmd);
2891da177e4SLinus Torvalds 		return -EFAULT;
2901da177e4SLinus Torvalds 	}
2911da177e4SLinus Torvalds 	command_put(cmd);
2921da177e4SLinus Torvalds 
2931da177e4SLinus Torvalds 	return len;
2941da177e4SLinus Torvalds }
2951da177e4SLinus Torvalds 
command_file_write(struct file * file,const char __user * ubuff,size_t count,loff_t * offset)2961da177e4SLinus Torvalds static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
2971da177e4SLinus Torvalds {
2981da177e4SLinus Torvalds 	struct ibmasmfs_command_data *command_data = file->private_data;
2991da177e4SLinus Torvalds 	struct command *cmd;
3001da177e4SLinus Torvalds 	unsigned long flags;
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	if (*offset < 0)
3031da177e4SLinus Torvalds 		return -EINVAL;
3041da177e4SLinus Torvalds 	if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
3051da177e4SLinus Torvalds 		return 0;
3061da177e4SLinus Torvalds 	if (*offset != 0)
3071da177e4SLinus Torvalds 		return 0;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* commands are executed sequentially, only one command at a time */
3101da177e4SLinus Torvalds 	if (command_data->command)
3111da177e4SLinus Torvalds 		return -EAGAIN;
3121da177e4SLinus Torvalds 
31388187605SMax Asbock 	cmd = ibmasm_new_command(command_data->sp, count);
3141da177e4SLinus Torvalds 	if (!cmd)
3151da177e4SLinus Torvalds 		return -ENOMEM;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	if (copy_from_user(cmd->buffer, ubuff, count)) {
3181da177e4SLinus Torvalds 		command_put(cmd);
3191da177e4SLinus Torvalds 		return -EFAULT;
3201da177e4SLinus Torvalds 	}
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	spin_lock_irqsave(&command_data->sp->lock, flags);
3231da177e4SLinus Torvalds 	if (command_data->command) {
3241da177e4SLinus Torvalds 		spin_unlock_irqrestore(&command_data->sp->lock, flags);
3251da177e4SLinus Torvalds 		command_put(cmd);
3261da177e4SLinus Torvalds 		return -EAGAIN;
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds 	command_data->command = cmd;
3291da177e4SLinus Torvalds 	spin_unlock_irqrestore(&command_data->sp->lock, flags);
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds 	ibmasm_exec_command(command_data->sp, cmd);
3321da177e4SLinus Torvalds 	ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
3331da177e4SLinus Torvalds 
3341da177e4SLinus Torvalds 	return count;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
event_file_open(struct inode * inode,struct file * file)3371da177e4SLinus Torvalds static int event_file_open(struct inode *inode, struct file *file)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	struct ibmasmfs_event_data *event_data;
3401da177e4SLinus Torvalds 	struct service_processor *sp;
3411da177e4SLinus Torvalds 
3428e18e294STheodore Ts'o 	if (!inode->i_private)
3431da177e4SLinus Torvalds 		return -ENODEV;
3441da177e4SLinus Torvalds 
3458e18e294STheodore Ts'o 	sp = inode->i_private;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	event_data = kmalloc(sizeof(struct ibmasmfs_event_data), GFP_KERNEL);
3481da177e4SLinus Torvalds 	if (!event_data)
3491da177e4SLinus Torvalds 		return -ENOMEM;
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds 	ibmasm_event_reader_register(sp, &event_data->reader);
3521da177e4SLinus Torvalds 
3531da177e4SLinus Torvalds 	event_data->sp = sp;
354b8acb808SMax Asbock 	event_data->active = 0;
3551da177e4SLinus Torvalds 	file->private_data = event_data;
3561da177e4SLinus Torvalds 	return 0;
3571da177e4SLinus Torvalds }
3581da177e4SLinus Torvalds 
event_file_close(struct inode * inode,struct file * file)3591da177e4SLinus Torvalds static int event_file_close(struct inode *inode, struct file *file)
3601da177e4SLinus Torvalds {
3611da177e4SLinus Torvalds 	struct ibmasmfs_event_data *event_data = file->private_data;
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
3641da177e4SLinus Torvalds 	kfree(event_data);
3651da177e4SLinus Torvalds 	return 0;
3661da177e4SLinus Torvalds }
3671da177e4SLinus Torvalds 
event_file_read(struct file * file,char __user * buf,size_t count,loff_t * offset)3681da177e4SLinus Torvalds static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
3691da177e4SLinus Torvalds {
3701da177e4SLinus Torvalds 	struct ibmasmfs_event_data *event_data = file->private_data;
3711da177e4SLinus Torvalds 	struct event_reader *reader = &event_data->reader;
372b8acb808SMax Asbock 	struct service_processor *sp = event_data->sp;
3731da177e4SLinus Torvalds 	int ret;
374b8acb808SMax Asbock 	unsigned long flags;
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	if (*offset < 0)
3771da177e4SLinus Torvalds 		return -EINVAL;
3781da177e4SLinus Torvalds 	if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
3791da177e4SLinus Torvalds 		return 0;
3801da177e4SLinus Torvalds 	if (*offset != 0)
3811da177e4SLinus Torvalds 		return 0;
3821da177e4SLinus Torvalds 
383b8acb808SMax Asbock 	spin_lock_irqsave(&sp->lock, flags);
384b8acb808SMax Asbock 	if (event_data->active) {
385b8acb808SMax Asbock 		spin_unlock_irqrestore(&sp->lock, flags);
386b8acb808SMax Asbock 		return -EBUSY;
387b8acb808SMax Asbock 	}
388b8acb808SMax Asbock 	event_data->active = 1;
389b8acb808SMax Asbock 	spin_unlock_irqrestore(&sp->lock, flags);
390b8acb808SMax Asbock 
391b8acb808SMax Asbock 	ret = ibmasm_get_next_event(sp, reader);
3921da177e4SLinus Torvalds 	if (ret <= 0)
393b8acb808SMax Asbock 		goto out;
394b8acb808SMax Asbock 
395b8acb808SMax Asbock 	if (count < reader->data_size) {
396b8acb808SMax Asbock 		ret = -EINVAL;
397b8acb808SMax Asbock 		goto out;
398b8acb808SMax Asbock 	}
399b8acb808SMax Asbock 
400b8acb808SMax Asbock         if (copy_to_user(buf, reader->data, reader->data_size)) {
401b8acb808SMax Asbock 		ret = -EFAULT;
402b8acb808SMax Asbock 		goto out;
403b8acb808SMax Asbock 	}
404b8acb808SMax Asbock 	ret = reader->data_size;
405b8acb808SMax Asbock 
406b8acb808SMax Asbock out:
407b8acb808SMax Asbock 	event_data->active = 0;
4081da177e4SLinus Torvalds 	return ret;
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
event_file_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)4111da177e4SLinus Torvalds static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
4121da177e4SLinus Torvalds {
4131da177e4SLinus Torvalds 	struct ibmasmfs_event_data *event_data = file->private_data;
4141da177e4SLinus Torvalds 
4151da177e4SLinus Torvalds 	if (*offset < 0)
4161da177e4SLinus Torvalds 		return -EINVAL;
4171da177e4SLinus Torvalds 	if (count != 1)
4181da177e4SLinus Torvalds 		return 0;
4191da177e4SLinus Torvalds 	if (*offset != 0)
4201da177e4SLinus Torvalds 		return 0;
4211da177e4SLinus Torvalds 
422b8acb808SMax Asbock 	ibmasm_cancel_next_event(&event_data->reader);
4231da177e4SLinus Torvalds 	return 0;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
r_heartbeat_file_open(struct inode * inode,struct file * file)4261da177e4SLinus Torvalds static int r_heartbeat_file_open(struct inode *inode, struct file *file)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	struct ibmasmfs_heartbeat_data *rhbeat;
4291da177e4SLinus Torvalds 
4308e18e294STheodore Ts'o 	if (!inode->i_private)
4311da177e4SLinus Torvalds 		return -ENODEV;
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds 	rhbeat = kmalloc(sizeof(struct ibmasmfs_heartbeat_data), GFP_KERNEL);
4341da177e4SLinus Torvalds 	if (!rhbeat)
4351da177e4SLinus Torvalds 		return -ENOMEM;
4361da177e4SLinus Torvalds 
4378e18e294STheodore Ts'o 	rhbeat->sp = inode->i_private;
4381da177e4SLinus Torvalds 	rhbeat->active = 0;
4391da177e4SLinus Torvalds 	ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
4401da177e4SLinus Torvalds 	file->private_data = rhbeat;
4411da177e4SLinus Torvalds 	return 0;
4421da177e4SLinus Torvalds }
4431da177e4SLinus Torvalds 
r_heartbeat_file_close(struct inode * inode,struct file * file)4441da177e4SLinus Torvalds static int r_heartbeat_file_close(struct inode *inode, struct file *file)
4451da177e4SLinus Torvalds {
4461da177e4SLinus Torvalds 	struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
4471da177e4SLinus Torvalds 
4481da177e4SLinus Torvalds 	kfree(rhbeat);
4491da177e4SLinus Torvalds 	return 0;
4501da177e4SLinus Torvalds }
4511da177e4SLinus Torvalds 
r_heartbeat_file_read(struct file * file,char __user * buf,size_t count,loff_t * offset)4521da177e4SLinus Torvalds static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
4531da177e4SLinus Torvalds {
4541da177e4SLinus Torvalds 	struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
4551da177e4SLinus Torvalds 	unsigned long flags;
4561da177e4SLinus Torvalds 	int result;
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	if (*offset < 0)
4591da177e4SLinus Torvalds 		return -EINVAL;
4601da177e4SLinus Torvalds 	if (count == 0 || count > 1024)
4611da177e4SLinus Torvalds 		return 0;
4621da177e4SLinus Torvalds 	if (*offset != 0)
4631da177e4SLinus Torvalds 		return 0;
4641da177e4SLinus Torvalds 
4651da177e4SLinus Torvalds 	/* allow only one reverse heartbeat per process */
4661da177e4SLinus Torvalds 	spin_lock_irqsave(&rhbeat->sp->lock, flags);
4671da177e4SLinus Torvalds 	if (rhbeat->active) {
4681da177e4SLinus Torvalds 		spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
4691da177e4SLinus Torvalds 		return -EBUSY;
4701da177e4SLinus Torvalds 	}
4711da177e4SLinus Torvalds 	rhbeat->active = 1;
4721da177e4SLinus Torvalds 	spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
4731da177e4SLinus Torvalds 
4741da177e4SLinus Torvalds 	result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
4751da177e4SLinus Torvalds 	rhbeat->active = 0;
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	return result;
4781da177e4SLinus Torvalds }
4791da177e4SLinus Torvalds 
r_heartbeat_file_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)4801da177e4SLinus Torvalds static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
4811da177e4SLinus Torvalds {
4821da177e4SLinus Torvalds 	struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
4831da177e4SLinus Torvalds 
4841da177e4SLinus Torvalds 	if (*offset < 0)
4851da177e4SLinus Torvalds 		return -EINVAL;
4861da177e4SLinus Torvalds 	if (count != 1)
4871da177e4SLinus Torvalds 		return 0;
4881da177e4SLinus Torvalds 	if (*offset != 0)
4891da177e4SLinus Torvalds 		return 0;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	if (rhbeat->active)
4921da177e4SLinus Torvalds 		ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	return 1;
4951da177e4SLinus Torvalds }
4961da177e4SLinus Torvalds 
remote_settings_file_close(struct inode * inode,struct file * file)4971da177e4SLinus Torvalds static int remote_settings_file_close(struct inode *inode, struct file *file)
4981da177e4SLinus Torvalds {
4991da177e4SLinus Torvalds 	return 0;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds 
remote_settings_file_read(struct file * file,char __user * buf,size_t count,loff_t * offset)5021da177e4SLinus Torvalds static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
5031da177e4SLinus Torvalds {
5041da177e4SLinus Torvalds 	void __iomem *address = (void __iomem *)file->private_data;
5051da177e4SLinus Torvalds 	int len = 0;
5061da177e4SLinus Torvalds 	unsigned int value;
507a0341fc1SJann Horn 	char lbuf[20];
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	value = readl(address);
510a0341fc1SJann Horn 	len = snprintf(lbuf, sizeof(lbuf), "%d\n", value);
5111da177e4SLinus Torvalds 
512a0341fc1SJann Horn 	return simple_read_from_buffer(buf, count, offset, lbuf, len);
5131da177e4SLinus Torvalds }
5141da177e4SLinus Torvalds 
remote_settings_file_write(struct file * file,const char __user * ubuff,size_t count,loff_t * offset)5151da177e4SLinus Torvalds static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
5161da177e4SLinus Torvalds {
5171da177e4SLinus Torvalds 	void __iomem *address = (void __iomem *)file->private_data;
5181da177e4SLinus Torvalds 	char *buff;
5191da177e4SLinus Torvalds 	unsigned int value;
5201da177e4SLinus Torvalds 
5211da177e4SLinus Torvalds 	if (*offset < 0)
5221da177e4SLinus Torvalds 		return -EINVAL;
5231da177e4SLinus Torvalds 	if (count == 0 || count > 1024)
5241da177e4SLinus Torvalds 		return 0;
5251da177e4SLinus Torvalds 	if (*offset != 0)
5261da177e4SLinus Torvalds 		return 0;
5271da177e4SLinus Torvalds 
528dd00cc48SYoann Padioleau 	buff = kzalloc (count + 1, GFP_KERNEL);
5291da177e4SLinus Torvalds 	if (!buff)
5301da177e4SLinus Torvalds 		return -ENOMEM;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds 	if (copy_from_user(buff, ubuff, count)) {
5341da177e4SLinus Torvalds 		kfree(buff);
5351da177e4SLinus Torvalds 		return -EFAULT;
5361da177e4SLinus Torvalds 	}
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	value = simple_strtoul(buff, NULL, 10);
5391da177e4SLinus Torvalds 	writel(value, address);
5401da177e4SLinus Torvalds 	kfree(buff);
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	return count;
5431da177e4SLinus Torvalds }
5441da177e4SLinus Torvalds 
545d54b1fdbSArjan van de Ven static const struct file_operations command_fops = {
5461da177e4SLinus Torvalds 	.open =		command_file_open,
5471da177e4SLinus Torvalds 	.release =	command_file_close,
5481da177e4SLinus Torvalds 	.read =		command_file_read,
5491da177e4SLinus Torvalds 	.write =	command_file_write,
550275bd41aSArnd Bergmann 	.llseek =	generic_file_llseek,
5511da177e4SLinus Torvalds };
5521da177e4SLinus Torvalds 
553d54b1fdbSArjan van de Ven static const struct file_operations event_fops = {
5541da177e4SLinus Torvalds 	.open =		event_file_open,
5551da177e4SLinus Torvalds 	.release =	event_file_close,
5561da177e4SLinus Torvalds 	.read =		event_file_read,
5571da177e4SLinus Torvalds 	.write =	event_file_write,
558275bd41aSArnd Bergmann 	.llseek =	generic_file_llseek,
5591da177e4SLinus Torvalds };
5601da177e4SLinus Torvalds 
561d54b1fdbSArjan van de Ven static const struct file_operations r_heartbeat_fops = {
5621da177e4SLinus Torvalds 	.open =		r_heartbeat_file_open,
5631da177e4SLinus Torvalds 	.release =	r_heartbeat_file_close,
5641da177e4SLinus Torvalds 	.read =		r_heartbeat_file_read,
5651da177e4SLinus Torvalds 	.write =	r_heartbeat_file_write,
566275bd41aSArnd Bergmann 	.llseek =	generic_file_llseek,
5671da177e4SLinus Torvalds };
5681da177e4SLinus Torvalds 
569d54b1fdbSArjan van de Ven static const struct file_operations remote_settings_fops = {
570234e3405SStephen Boyd 	.open =		simple_open,
5711da177e4SLinus Torvalds 	.release =	remote_settings_file_close,
5721da177e4SLinus Torvalds 	.read =		remote_settings_file_read,
5731da177e4SLinus Torvalds 	.write =	remote_settings_file_write,
574275bd41aSArnd Bergmann 	.llseek =	generic_file_llseek,
5751da177e4SLinus Torvalds };
5761da177e4SLinus Torvalds 
5771da177e4SLinus Torvalds 
ibmasmfs_create_files(struct super_block * sb)578318ceed0SAl Viro static void ibmasmfs_create_files (struct super_block *sb)
5791da177e4SLinus Torvalds {
5801da177e4SLinus Torvalds 	struct list_head *entry;
5811da177e4SLinus Torvalds 	struct service_processor *sp;
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds 	list_for_each(entry, &service_processors) {
5841da177e4SLinus Torvalds 		struct dentry *dir;
5851da177e4SLinus Torvalds 		struct dentry *remote_dir;
5861da177e4SLinus Torvalds 		sp = list_entry(entry, struct service_processor, node);
5870507c78aSAl Viro 		dir = ibmasmfs_create_dir(sb->s_root, sp->dirname);
5881da177e4SLinus Torvalds 		if (!dir)
5891da177e4SLinus Torvalds 			continue;
5901da177e4SLinus Torvalds 
5910507c78aSAl Viro 		ibmasmfs_create_file(dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
5920507c78aSAl Viro 		ibmasmfs_create_file(dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
5930507c78aSAl Viro 		ibmasmfs_create_file(dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
5941da177e4SLinus Torvalds 
5950507c78aSAl Viro 		remote_dir = ibmasmfs_create_dir(dir, "remote_video");
5961da177e4SLinus Torvalds 		if (!remote_dir)
5971da177e4SLinus Torvalds 			continue;
5981da177e4SLinus Torvalds 
5990507c78aSAl Viro 		ibmasmfs_create_file(remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
6000507c78aSAl Viro 		ibmasmfs_create_file(remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
6010507c78aSAl Viro 		ibmasmfs_create_file(remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);
6021da177e4SLinus Torvalds 	}
6031da177e4SLinus Torvalds }
604