xref: /linux/drivers/misc/ibmasm/ibmasmfs.c (revision cb4eb6771c0f8fd1c52a8f6fdec7762fb087380a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * IBM ASM Service Processor Device Driver
4  *
5  * Copyright (C) IBM Corporation, 2004
6  *
7  * Author: Max Asböck <amax@us.ibm.com>
8  */
9 
10 /*
11  * Parts of this code are based on an article by Jonathan Corbet
12  * that appeared in Linux Weekly News.
13  */
14 
15 
16 /*
17  * The IBMASM file virtual filesystem. It creates the following hierarchy
18  * dynamically when mounted from user space:
19  *
20  *    /ibmasm
21  *    |-- 0
22  *    |   |-- command
23  *    |   |-- event
24  *    |   |-- reverse_heartbeat
25  *    |   `-- remote_video
26  *    |       |-- depth
27  *    |       |-- height
28  *    |       `-- width
29  *    .
30  *    .
31  *    .
32  *    `-- n
33  *        |-- command
34  *        |-- event
35  *        |-- reverse_heartbeat
36  *        `-- remote_video
37  *            |-- depth
38  *            |-- height
39  *            `-- width
40  *
41  * For each service processor the following files are created:
42  *
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
46  *
47  * events: listen for service processor events
48  *	read: sleep (interruptible) until an event occurs
49  *      write: wakeup sleeping event listener
50  *
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
54  *
55  * remote_video/width
56  * remote_video/height
57  * remote_video/width: control remote display settings
58  *	write: set value
59  *	read: read value
60  */
61 
62 #include <linux/fs.h>
63 #include <linux/fs_context.h>
64 #include <linux/pagemap.h>
65 #include <linux/slab.h>
66 #include <linux/uaccess.h>
67 #include <asm/io.h>
68 #include "ibmasm.h"
69 #include "remote.h"
70 #include "dot_command.h"
71 
72 #define IBMASMFS_MAGIC 0x66726f67
73 
74 static LIST_HEAD(service_processors);
75 
76 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode);
77 static void ibmasmfs_create_files (struct super_block *sb);
78 static int ibmasmfs_fill_super(struct super_block *sb, struct fs_context *fc);
79 
ibmasmfs_get_tree(struct fs_context * fc)80 static int ibmasmfs_get_tree(struct fs_context *fc)
81 {
82 	return get_tree_single(fc, ibmasmfs_fill_super);
83 }
84 
85 static const struct fs_context_operations ibmasmfs_context_ops = {
86 	.get_tree	= ibmasmfs_get_tree,
87 };
88 
ibmasmfs_init_fs_context(struct fs_context * fc)89 static int ibmasmfs_init_fs_context(struct fs_context *fc)
90 {
91 	fc->ops = &ibmasmfs_context_ops;
92 	return 0;
93 }
94 
95 static const struct super_operations ibmasmfs_s_ops = {
96 	.statfs		= simple_statfs,
97 	.drop_inode	= inode_just_drop,
98 };
99 
100 static struct file_system_type ibmasmfs_type = {
101 	.owner          = THIS_MODULE,
102 	.name           = "ibmasmfs",
103 	.init_fs_context = ibmasmfs_init_fs_context,
104 	.kill_sb        = kill_anon_super,
105 };
106 MODULE_ALIAS_FS("ibmasmfs");
107 
ibmasmfs_fill_super(struct super_block * sb,struct fs_context * fc)108 static int ibmasmfs_fill_super(struct super_block *sb, struct fs_context *fc)
109 {
110 	struct inode *root;
111 
112 	sb->s_blocksize = PAGE_SIZE;
113 	sb->s_blocksize_bits = PAGE_SHIFT;
114 	sb->s_magic = IBMASMFS_MAGIC;
115 	sb->s_op = &ibmasmfs_s_ops;
116 	sb->s_time_gran = 1;
117 
118 	root = ibmasmfs_make_inode (sb, S_IFDIR | 0500);
119 	if (!root)
120 		return -ENOMEM;
121 
122 	root->i_op = &simple_dir_inode_operations;
123 	root->i_fop = &simple_dir_operations;
124 
125 	sb->s_root = d_make_root(root);
126 	if (!sb->s_root)
127 		return -ENOMEM;
128 
129 	ibmasmfs_create_files(sb);
130 	return 0;
131 }
132 
ibmasmfs_make_inode(struct super_block * sb,int mode)133 static struct inode *ibmasmfs_make_inode(struct super_block *sb, int mode)
134 {
135 	struct inode *ret = new_inode(sb);
136 
137 	if (ret) {
138 		ret->i_ino = get_next_ino();
139 		ret->i_mode = mode;
140 		simple_inode_init_ts(ret);
141 	}
142 	return ret;
143 }
144 
ibmasmfs_create_file(struct dentry * parent,const char * name,const struct file_operations * fops,void * data,int mode)145 static int ibmasmfs_create_file(struct dentry *parent,
146 			const char *name,
147 			const struct file_operations *fops,
148 			void *data,
149 			int mode)
150 {
151 	struct dentry *dentry;
152 	struct inode *inode;
153 
154 	dentry = d_alloc_name(parent, name);
155 	if (!dentry)
156 		return -ENOMEM;
157 
158 	inode = ibmasmfs_make_inode(parent->d_sb, S_IFREG | mode);
159 	if (!inode) {
160 		dput(dentry);
161 		return -ENOMEM;
162 	}
163 
164 	inode->i_fop = fops;
165 	inode->i_private = data;
166 
167 	d_make_persistent(dentry, inode);
168 	dput(dentry);
169 	return 0;
170 }
171 
ibmasmfs_create_dir(struct dentry * parent,const char * name)172 static struct dentry *ibmasmfs_create_dir(struct dentry *parent,
173 				const char *name)
174 {
175 	struct dentry *dentry;
176 	struct inode *inode;
177 
178 	dentry = d_alloc_name(parent, name);
179 	if (!dentry)
180 		return NULL;
181 
182 	inode = ibmasmfs_make_inode(parent->d_sb, S_IFDIR | 0500);
183 	if (!inode) {
184 		dput(dentry);
185 		return NULL;
186 	}
187 
188 	inode->i_op = &simple_dir_inode_operations;
189 	inode->i_fop = &simple_dir_operations;
190 
191 	d_make_persistent(dentry, inode);
192 	dput(dentry);
193 	return dentry; // borrowed
194 }
195 
ibmasmfs_register(void)196 int ibmasmfs_register(void)
197 {
198 	return register_filesystem(&ibmasmfs_type);
199 }
200 
ibmasmfs_unregister(void)201 void ibmasmfs_unregister(void)
202 {
203 	unregister_filesystem(&ibmasmfs_type);
204 }
205 
ibmasmfs_add_sp(struct service_processor * sp)206 void ibmasmfs_add_sp(struct service_processor *sp)
207 {
208 	list_add(&sp->node, &service_processors);
209 }
210 
211 /* struct to save state between command file operations */
212 struct ibmasmfs_command_data {
213 	struct service_processor	*sp;
214 	struct command			*command;
215 };
216 
217 /* struct to save state between event file operations */
218 struct ibmasmfs_event_data {
219 	struct service_processor	*sp;
220 	struct event_reader		reader;
221 	int				active;
222 };
223 
224 /* struct to save state between reverse heartbeat file operations */
225 struct ibmasmfs_heartbeat_data {
226 	struct service_processor	*sp;
227 	struct reverse_heartbeat	heartbeat;
228 	int				active;
229 };
230 
command_file_open(struct inode * inode,struct file * file)231 static int command_file_open(struct inode *inode, struct file *file)
232 {
233 	struct ibmasmfs_command_data *command_data;
234 
235 	if (!inode->i_private)
236 		return -ENODEV;
237 
238 	command_data = kmalloc_obj(struct ibmasmfs_command_data);
239 	if (!command_data)
240 		return -ENOMEM;
241 
242 	command_data->command = NULL;
243 	command_data->sp = inode->i_private;
244 	file->private_data = command_data;
245 	return 0;
246 }
247 
command_file_close(struct inode * inode,struct file * file)248 static int command_file_close(struct inode *inode, struct file *file)
249 {
250 	struct ibmasmfs_command_data *command_data = file->private_data;
251 
252 	if (command_data->command)
253 		command_put(command_data->command);
254 
255 	kfree(command_data);
256 	return 0;
257 }
258 
command_file_read(struct file * file,char __user * buf,size_t count,loff_t * offset)259 static ssize_t command_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
260 {
261 	struct ibmasmfs_command_data *command_data = file->private_data;
262 	struct command *cmd;
263 	int len;
264 	unsigned long flags;
265 
266 	if (*offset < 0)
267 		return -EINVAL;
268 	if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
269 		return 0;
270 	if (*offset != 0)
271 		return 0;
272 
273 	spin_lock_irqsave(&command_data->sp->lock, flags);
274 	cmd = command_data->command;
275 	if (cmd == NULL) {
276 		spin_unlock_irqrestore(&command_data->sp->lock, flags);
277 		return 0;
278 	}
279 	command_data->command = NULL;
280 	spin_unlock_irqrestore(&command_data->sp->lock, flags);
281 
282 	if (cmd->status != IBMASM_CMD_COMPLETE) {
283 		command_put(cmd);
284 		return -EIO;
285 	}
286 	len = min(count, cmd->buffer_size);
287 	if (copy_to_user(buf, cmd->buffer, len)) {
288 		command_put(cmd);
289 		return -EFAULT;
290 	}
291 	command_put(cmd);
292 
293 	return len;
294 }
295 
command_file_write(struct file * file,const char __user * ubuff,size_t count,loff_t * offset)296 static ssize_t command_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
297 {
298 	struct ibmasmfs_command_data *command_data = file->private_data;
299 	struct command *cmd;
300 	unsigned long flags;
301 
302 	if (*offset < 0)
303 		return -EINVAL;
304 	if (count == 0 || count > IBMASM_CMD_MAX_BUFFER_SIZE)
305 		return 0;
306 	if (count < sizeof(struct dot_command_header))
307 		return -EINVAL;
308 	if (*offset != 0)
309 		return 0;
310 
311 	/* commands are executed sequentially, only one command at a time */
312 	if (command_data->command)
313 		return -EAGAIN;
314 
315 	cmd = ibmasm_new_command(command_data->sp, count);
316 	if (!cmd)
317 		return -ENOMEM;
318 
319 	if (copy_from_user(cmd->buffer, ubuff, count)) {
320 		command_put(cmd);
321 		return -EFAULT;
322 	}
323 
324 	if (count < get_dot_command_size(cmd->buffer)) {
325 		command_put(cmd);
326 		return -EINVAL;
327 	}
328 
329 	spin_lock_irqsave(&command_data->sp->lock, flags);
330 	if (command_data->command) {
331 		spin_unlock_irqrestore(&command_data->sp->lock, flags);
332 		command_put(cmd);
333 		return -EAGAIN;
334 	}
335 	command_data->command = cmd;
336 	spin_unlock_irqrestore(&command_data->sp->lock, flags);
337 
338 	ibmasm_exec_command(command_data->sp, cmd);
339 	ibmasm_wait_for_response(cmd, get_dot_command_timeout(cmd->buffer));
340 
341 	return count;
342 }
343 
event_file_open(struct inode * inode,struct file * file)344 static int event_file_open(struct inode *inode, struct file *file)
345 {
346 	struct ibmasmfs_event_data *event_data;
347 	struct service_processor *sp;
348 
349 	if (!inode->i_private)
350 		return -ENODEV;
351 
352 	sp = inode->i_private;
353 
354 	event_data = kmalloc_obj(struct ibmasmfs_event_data);
355 	if (!event_data)
356 		return -ENOMEM;
357 
358 	ibmasm_event_reader_register(sp, &event_data->reader);
359 
360 	event_data->sp = sp;
361 	event_data->active = 0;
362 	file->private_data = event_data;
363 	return 0;
364 }
365 
event_file_close(struct inode * inode,struct file * file)366 static int event_file_close(struct inode *inode, struct file *file)
367 {
368 	struct ibmasmfs_event_data *event_data = file->private_data;
369 
370 	ibmasm_event_reader_unregister(event_data->sp, &event_data->reader);
371 	kfree(event_data);
372 	return 0;
373 }
374 
event_file_read(struct file * file,char __user * buf,size_t count,loff_t * offset)375 static ssize_t event_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
376 {
377 	struct ibmasmfs_event_data *event_data = file->private_data;
378 	struct event_reader *reader = &event_data->reader;
379 	struct service_processor *sp = event_data->sp;
380 	int ret;
381 	unsigned long flags;
382 
383 	if (*offset < 0)
384 		return -EINVAL;
385 	if (count == 0 || count > IBMASM_EVENT_MAX_SIZE)
386 		return 0;
387 	if (*offset != 0)
388 		return 0;
389 
390 	spin_lock_irqsave(&sp->lock, flags);
391 	if (event_data->active) {
392 		spin_unlock_irqrestore(&sp->lock, flags);
393 		return -EBUSY;
394 	}
395 	event_data->active = 1;
396 	spin_unlock_irqrestore(&sp->lock, flags);
397 
398 	ret = ibmasm_get_next_event(sp, reader);
399 	if (ret <= 0)
400 		goto out;
401 
402 	if (count < reader->data_size) {
403 		ret = -EINVAL;
404 		goto out;
405 	}
406 
407         if (copy_to_user(buf, reader->data, reader->data_size)) {
408 		ret = -EFAULT;
409 		goto out;
410 	}
411 	ret = reader->data_size;
412 
413 out:
414 	event_data->active = 0;
415 	return ret;
416 }
417 
event_file_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)418 static ssize_t event_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
419 {
420 	struct ibmasmfs_event_data *event_data = file->private_data;
421 
422 	if (*offset < 0)
423 		return -EINVAL;
424 	if (count != 1)
425 		return 0;
426 	if (*offset != 0)
427 		return 0;
428 
429 	ibmasm_cancel_next_event(&event_data->reader);
430 	return 0;
431 }
432 
r_heartbeat_file_open(struct inode * inode,struct file * file)433 static int r_heartbeat_file_open(struct inode *inode, struct file *file)
434 {
435 	struct ibmasmfs_heartbeat_data *rhbeat;
436 
437 	if (!inode->i_private)
438 		return -ENODEV;
439 
440 	rhbeat = kmalloc_obj(struct ibmasmfs_heartbeat_data);
441 	if (!rhbeat)
442 		return -ENOMEM;
443 
444 	rhbeat->sp = inode->i_private;
445 	rhbeat->active = 0;
446 	ibmasm_init_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
447 	file->private_data = rhbeat;
448 	return 0;
449 }
450 
r_heartbeat_file_close(struct inode * inode,struct file * file)451 static int r_heartbeat_file_close(struct inode *inode, struct file *file)
452 {
453 	struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
454 
455 	kfree(rhbeat);
456 	return 0;
457 }
458 
r_heartbeat_file_read(struct file * file,char __user * buf,size_t count,loff_t * offset)459 static ssize_t r_heartbeat_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
460 {
461 	struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
462 	unsigned long flags;
463 	int result;
464 
465 	if (*offset < 0)
466 		return -EINVAL;
467 	if (count == 0 || count > 1024)
468 		return 0;
469 	if (*offset != 0)
470 		return 0;
471 
472 	/* allow only one reverse heartbeat per process */
473 	spin_lock_irqsave(&rhbeat->sp->lock, flags);
474 	if (rhbeat->active) {
475 		spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
476 		return -EBUSY;
477 	}
478 	rhbeat->active = 1;
479 	spin_unlock_irqrestore(&rhbeat->sp->lock, flags);
480 
481 	result = ibmasm_start_reverse_heartbeat(rhbeat->sp, &rhbeat->heartbeat);
482 	rhbeat->active = 0;
483 
484 	return result;
485 }
486 
r_heartbeat_file_write(struct file * file,const char __user * buf,size_t count,loff_t * offset)487 static ssize_t r_heartbeat_file_write(struct file *file, const char __user *buf, size_t count, loff_t *offset)
488 {
489 	struct ibmasmfs_heartbeat_data *rhbeat = file->private_data;
490 
491 	if (*offset < 0)
492 		return -EINVAL;
493 	if (count != 1)
494 		return 0;
495 	if (*offset != 0)
496 		return 0;
497 
498 	if (rhbeat->active)
499 		ibmasm_stop_reverse_heartbeat(&rhbeat->heartbeat);
500 
501 	return 1;
502 }
503 
remote_settings_file_close(struct inode * inode,struct file * file)504 static int remote_settings_file_close(struct inode *inode, struct file *file)
505 {
506 	return 0;
507 }
508 
remote_settings_file_read(struct file * file,char __user * buf,size_t count,loff_t * offset)509 static ssize_t remote_settings_file_read(struct file *file, char __user *buf, size_t count, loff_t *offset)
510 {
511 	void __iomem *address = (void __iomem *)file->private_data;
512 	int len = 0;
513 	unsigned int value;
514 	char lbuf[20];
515 
516 	value = readl(address);
517 	len = snprintf(lbuf, sizeof(lbuf), "%d\n", value);
518 
519 	return simple_read_from_buffer(buf, count, offset, lbuf, len);
520 }
521 
remote_settings_file_write(struct file * file,const char __user * ubuff,size_t count,loff_t * offset)522 static ssize_t remote_settings_file_write(struct file *file, const char __user *ubuff, size_t count, loff_t *offset)
523 {
524 	void __iomem *address = (void __iomem *)file->private_data;
525 	char *buff;
526 	unsigned int value;
527 
528 	if (*offset < 0)
529 		return -EINVAL;
530 	if (count == 0 || count > 1024)
531 		return 0;
532 	if (*offset != 0)
533 		return 0;
534 
535 	buff = memdup_user_nul(ubuff, count);
536 	if (IS_ERR(buff))
537 		return PTR_ERR(buff);
538 
539 	value = simple_strtoul(buff, NULL, 10);
540 	writel(value, address);
541 	kfree(buff);
542 
543 	return count;
544 }
545 
546 static const struct file_operations command_fops = {
547 	.open =		command_file_open,
548 	.release =	command_file_close,
549 	.read =		command_file_read,
550 	.write =	command_file_write,
551 	.llseek =	generic_file_llseek,
552 };
553 
554 static const struct file_operations event_fops = {
555 	.open =		event_file_open,
556 	.release =	event_file_close,
557 	.read =		event_file_read,
558 	.write =	event_file_write,
559 	.llseek =	generic_file_llseek,
560 };
561 
562 static const struct file_operations r_heartbeat_fops = {
563 	.open =		r_heartbeat_file_open,
564 	.release =	r_heartbeat_file_close,
565 	.read =		r_heartbeat_file_read,
566 	.write =	r_heartbeat_file_write,
567 	.llseek =	generic_file_llseek,
568 };
569 
570 static const struct file_operations remote_settings_fops = {
571 	.open =		simple_open,
572 	.release =	remote_settings_file_close,
573 	.read =		remote_settings_file_read,
574 	.write =	remote_settings_file_write,
575 	.llseek =	generic_file_llseek,
576 };
577 
578 
ibmasmfs_create_files(struct super_block * sb)579 static void ibmasmfs_create_files (struct super_block *sb)
580 {
581 	struct list_head *entry;
582 	struct service_processor *sp;
583 
584 	list_for_each(entry, &service_processors) {
585 		struct dentry *dir;
586 		struct dentry *remote_dir;
587 		sp = list_entry(entry, struct service_processor, node);
588 		dir = ibmasmfs_create_dir(sb->s_root, sp->dirname);
589 		if (!dir)
590 			continue;
591 
592 		ibmasmfs_create_file(dir, "command", &command_fops, sp, S_IRUSR|S_IWUSR);
593 		ibmasmfs_create_file(dir, "event", &event_fops, sp, S_IRUSR|S_IWUSR);
594 		ibmasmfs_create_file(dir, "reverse_heartbeat", &r_heartbeat_fops, sp, S_IRUSR|S_IWUSR);
595 
596 		remote_dir = ibmasmfs_create_dir(dir, "remote_video");
597 		if (!remote_dir)
598 			continue;
599 
600 		ibmasmfs_create_file(remote_dir, "width", &remote_settings_fops, (void *)display_width(sp), S_IRUSR|S_IWUSR);
601 		ibmasmfs_create_file(remote_dir, "height", &remote_settings_fops, (void *)display_height(sp), S_IRUSR|S_IWUSR);
602 		ibmasmfs_create_file(remote_dir, "depth", &remote_settings_fops, (void *)display_depth(sp), S_IRUSR|S_IWUSR);
603 	}
604 }
605