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