a0e6613e8be6151d3fdc00efc3005ad8c6f22ebd
[pandora-kernel.git] / drivers / infiniband / hw / qib / qib_fs.c
1 /*
2  * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
3  * Copyright (c) 2006 PathScale, Inc. All rights reserved.
4  *
5  * This software is available to you under a choice of one of two
6  * licenses.  You may choose to be licensed under the terms of the GNU
7  * General Public License (GPL) Version 2, available from the file
8  * COPYING in the main directory of this source tree, or the
9  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33
34 #include <linux/module.h>
35 #include <linux/fs.h>
36 #include <linux/mount.h>
37 #include <linux/pagemap.h>
38 #include <linux/init.h>
39 #include <linux/namei.h>
40
41 #include "qib.h"
42
43 #define QIBFS_MAGIC 0x726a77
44
45 static struct super_block *qib_super;
46
47 #define private2dd(file) ((file)->f_dentry->d_inode->i_private)
48
49 static int qibfs_mknod(struct inode *dir, struct dentry *dentry,
50                        int mode, const struct file_operations *fops,
51                        void *data)
52 {
53         int error;
54         struct inode *inode = new_inode(dir->i_sb);
55
56         if (!inode) {
57                 error = -EPERM;
58                 goto bail;
59         }
60
61         inode->i_mode = mode;
62         inode->i_uid = 0;
63         inode->i_gid = 0;
64         inode->i_blocks = 0;
65         inode->i_atime = CURRENT_TIME;
66         inode->i_mtime = inode->i_atime;
67         inode->i_ctime = inode->i_atime;
68         inode->i_private = data;
69         if ((mode & S_IFMT) == S_IFDIR) {
70                 inode->i_op = &simple_dir_inode_operations;
71                 inc_nlink(inode);
72                 inc_nlink(dir);
73         }
74
75         inode->i_fop = fops;
76
77         d_instantiate(dentry, inode);
78         error = 0;
79
80 bail:
81         return error;
82 }
83
84 static int create_file(const char *name, mode_t mode,
85                        struct dentry *parent, struct dentry **dentry,
86                        const struct file_operations *fops, void *data)
87 {
88         int error;
89
90         *dentry = NULL;
91         mutex_lock(&parent->d_inode->i_mutex);
92         *dentry = lookup_one_len(name, parent, strlen(name));
93         if (!IS_ERR(*dentry))
94                 error = qibfs_mknod(parent->d_inode, *dentry,
95                                     mode, fops, data);
96         else
97                 error = PTR_ERR(*dentry);
98         mutex_unlock(&parent->d_inode->i_mutex);
99
100         return error;
101 }
102
103 static ssize_t driver_stats_read(struct file *file, char __user *buf,
104                                  size_t count, loff_t *ppos)
105 {
106         return simple_read_from_buffer(buf, count, ppos, &qib_stats,
107                                        sizeof qib_stats);
108 }
109
110 /*
111  * driver stats field names, one line per stat, single string.  Used by
112  * programs like ipathstats to print the stats in a way which works for
113  * different versions of drivers, without changing program source.
114  * if qlogic_ib_stats changes, this needs to change.  Names need to be
115  * 12 chars or less (w/o newline), for proper display by ipathstats utility.
116  */
117 static const char qib_statnames[] =
118         "KernIntr\n"
119         "ErrorIntr\n"
120         "Tx_Errs\n"
121         "Rcv_Errs\n"
122         "H/W_Errs\n"
123         "NoPIOBufs\n"
124         "CtxtsOpen\n"
125         "RcvLen_Errs\n"
126         "EgrBufFull\n"
127         "EgrHdrFull\n"
128         ;
129
130 static ssize_t driver_names_read(struct file *file, char __user *buf,
131                                  size_t count, loff_t *ppos)
132 {
133         return simple_read_from_buffer(buf, count, ppos, qib_statnames,
134                 sizeof qib_statnames - 1); /* no null */
135 }
136
137 static const struct file_operations driver_ops[] = {
138         { .read = driver_stats_read, .llseek = generic_file_llseek, },
139         { .read = driver_names_read, .llseek = generic_file_llseek, },
140 };
141
142 /* read the per-device counters */
143 static ssize_t dev_counters_read(struct file *file, char __user *buf,
144                                  size_t count, loff_t *ppos)
145 {
146         u64 *counters;
147         size_t avail;
148         struct qib_devdata *dd = private2dd(file);
149
150         avail = dd->f_read_cntrs(dd, *ppos, NULL, &counters);
151         return simple_read_from_buffer(buf, count, ppos, counters, avail);
152 }
153
154 /* read the per-device counters */
155 static ssize_t dev_names_read(struct file *file, char __user *buf,
156                               size_t count, loff_t *ppos)
157 {
158         char *names;
159         size_t avail;
160         struct qib_devdata *dd = private2dd(file);
161
162         avail = dd->f_read_cntrs(dd, *ppos, &names, NULL);
163         return simple_read_from_buffer(buf, count, ppos, names, avail);
164 }
165
166 static const struct file_operations cntr_ops[] = {
167         { .read = dev_counters_read, .llseek = generic_file_llseek, },
168         { .read = dev_names_read, .llseek = generic_file_llseek, },
169 };
170
171 /*
172  * Could use file->f_dentry->d_inode->i_ino to figure out which file,
173  * instead of separate routine for each, but for now, this works...
174  */
175
176 /* read the per-port names (same for each port) */
177 static ssize_t portnames_read(struct file *file, char __user *buf,
178                               size_t count, loff_t *ppos)
179 {
180         char *names;
181         size_t avail;
182         struct qib_devdata *dd = private2dd(file);
183
184         avail = dd->f_read_portcntrs(dd, *ppos, 0, &names, NULL);
185         return simple_read_from_buffer(buf, count, ppos, names, avail);
186 }
187
188 /* read the per-port counters for port 1 (pidx 0) */
189 static ssize_t portcntrs_1_read(struct file *file, char __user *buf,
190                                 size_t count, loff_t *ppos)
191 {
192         u64 *counters;
193         size_t avail;
194         struct qib_devdata *dd = private2dd(file);
195
196         avail = dd->f_read_portcntrs(dd, *ppos, 0, NULL, &counters);
197         return simple_read_from_buffer(buf, count, ppos, counters, avail);
198 }
199
200 /* read the per-port counters for port 2 (pidx 1) */
201 static ssize_t portcntrs_2_read(struct file *file, char __user *buf,
202                                 size_t count, loff_t *ppos)
203 {
204         u64 *counters;
205         size_t avail;
206         struct qib_devdata *dd = private2dd(file);
207
208         avail = dd->f_read_portcntrs(dd, *ppos, 1, NULL, &counters);
209         return simple_read_from_buffer(buf, count, ppos, counters, avail);
210 }
211
212 static const struct file_operations portcntr_ops[] = {
213         { .read = portnames_read, .llseek = generic_file_llseek, },
214         { .read = portcntrs_1_read, .llseek = generic_file_llseek, },
215         { .read = portcntrs_2_read, .llseek = generic_file_llseek, },
216 };
217
218 /*
219  * read the per-port QSFP data for port 1 (pidx 0)
220  */
221 static ssize_t qsfp_1_read(struct file *file, char __user *buf,
222                            size_t count, loff_t *ppos)
223 {
224         struct qib_devdata *dd = private2dd(file);
225         char *tmp;
226         int ret;
227
228         tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
229         if (!tmp)
230                 return -ENOMEM;
231
232         ret = qib_qsfp_dump(dd->pport, tmp, PAGE_SIZE);
233         if (ret > 0)
234                 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
235         kfree(tmp);
236         return ret;
237 }
238
239 /*
240  * read the per-port QSFP data for port 2 (pidx 1)
241  */
242 static ssize_t qsfp_2_read(struct file *file, char __user *buf,
243                            size_t count, loff_t *ppos)
244 {
245         struct qib_devdata *dd = private2dd(file);
246         char *tmp;
247         int ret;
248
249         if (dd->num_pports < 2)
250                 return -ENODEV;
251
252         tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
253         if (!tmp)
254                 return -ENOMEM;
255
256         ret = qib_qsfp_dump(dd->pport + 1, tmp, PAGE_SIZE);
257         if (ret > 0)
258                 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
259         kfree(tmp);
260         return ret;
261 }
262
263 static const struct file_operations qsfp_ops[] = {
264         { .read = qsfp_1_read, .llseek = generic_file_llseek, },
265         { .read = qsfp_2_read, .llseek = generic_file_llseek, },
266 };
267
268 static ssize_t flash_read(struct file *file, char __user *buf,
269                           size_t count, loff_t *ppos)
270 {
271         struct qib_devdata *dd;
272         ssize_t ret;
273         loff_t pos;
274         char *tmp;
275
276         pos = *ppos;
277
278         if (pos < 0) {
279                 ret = -EINVAL;
280                 goto bail;
281         }
282
283         if (pos >= sizeof(struct qib_flash)) {
284                 ret = 0;
285                 goto bail;
286         }
287
288         if (count > sizeof(struct qib_flash) - pos)
289                 count = sizeof(struct qib_flash) - pos;
290
291         tmp = kmalloc(count, GFP_KERNEL);
292         if (!tmp) {
293                 ret = -ENOMEM;
294                 goto bail;
295         }
296
297         dd = private2dd(file);
298         if (qib_eeprom_read(dd, pos, tmp, count)) {
299                 qib_dev_err(dd, "failed to read from flash\n");
300                 ret = -ENXIO;
301                 goto bail_tmp;
302         }
303
304         if (copy_to_user(buf, tmp, count)) {
305                 ret = -EFAULT;
306                 goto bail_tmp;
307         }
308
309         *ppos = pos + count;
310         ret = count;
311
312 bail_tmp:
313         kfree(tmp);
314
315 bail:
316         return ret;
317 }
318
319 static ssize_t flash_write(struct file *file, const char __user *buf,
320                            size_t count, loff_t *ppos)
321 {
322         struct qib_devdata *dd;
323         ssize_t ret;
324         loff_t pos;
325         char *tmp;
326
327         pos = *ppos;
328
329         if (pos != 0) {
330                 ret = -EINVAL;
331                 goto bail;
332         }
333
334         if (count != sizeof(struct qib_flash)) {
335                 ret = -EINVAL;
336                 goto bail;
337         }
338
339         tmp = kmalloc(count, GFP_KERNEL);
340         if (!tmp) {
341                 ret = -ENOMEM;
342                 goto bail;
343         }
344
345         if (copy_from_user(tmp, buf, count)) {
346                 ret = -EFAULT;
347                 goto bail_tmp;
348         }
349
350         dd = private2dd(file);
351         if (qib_eeprom_write(dd, pos, tmp, count)) {
352                 ret = -ENXIO;
353                 qib_dev_err(dd, "failed to write to flash\n");
354                 goto bail_tmp;
355         }
356
357         *ppos = pos + count;
358         ret = count;
359
360 bail_tmp:
361         kfree(tmp);
362
363 bail:
364         return ret;
365 }
366
367 static const struct file_operations flash_ops = {
368         .read = flash_read,
369         .write = flash_write,
370         .llseek = default_llseek,
371 };
372
373 static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
374 {
375         struct dentry *dir, *tmp;
376         char unit[10];
377         int ret, i;
378
379         /* create the per-unit directory */
380         snprintf(unit, sizeof unit, "%u", dd->unit);
381         ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
382                           &simple_dir_operations, dd);
383         if (ret) {
384                 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
385                 goto bail;
386         }
387
388         /* create the files in the new directory */
389         ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
390                           &cntr_ops[0], dd);
391         if (ret) {
392                 printk(KERN_ERR "create_file(%s/counters) failed: %d\n",
393                        unit, ret);
394                 goto bail;
395         }
396         ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
397                           &cntr_ops[1], dd);
398         if (ret) {
399                 printk(KERN_ERR "create_file(%s/counter_names) failed: %d\n",
400                        unit, ret);
401                 goto bail;
402         }
403         ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
404                           &portcntr_ops[0], dd);
405         if (ret) {
406                 printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
407                        unit, "portcounter_names", ret);
408                 goto bail;
409         }
410         for (i = 1; i <= dd->num_pports; i++) {
411                 char fname[24];
412
413                 sprintf(fname, "port%dcounters", i);
414                 /* create the files in the new directory */
415                 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
416                                   &portcntr_ops[i], dd);
417                 if (ret) {
418                         printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
419                                 unit, fname, ret);
420                         goto bail;
421                 }
422                 if (!(dd->flags & QIB_HAS_QSFP))
423                         continue;
424                 sprintf(fname, "qsfp%d", i);
425                 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
426                                   &qsfp_ops[i - 1], dd);
427                 if (ret) {
428                         printk(KERN_ERR "create_file(%s/%s) failed: %d\n",
429                                 unit, fname, ret);
430                         goto bail;
431                 }
432         }
433
434         ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
435                           &flash_ops, dd);
436         if (ret)
437                 printk(KERN_ERR "create_file(%s/flash) failed: %d\n",
438                         unit, ret);
439 bail:
440         return ret;
441 }
442
443 static int remove_file(struct dentry *parent, char *name)
444 {
445         struct dentry *tmp;
446         int ret;
447
448         tmp = lookup_one_len(name, parent, strlen(name));
449
450         if (IS_ERR(tmp)) {
451                 ret = PTR_ERR(tmp);
452                 goto bail;
453         }
454
455         spin_lock(&dcache_lock);
456         spin_lock(&tmp->d_lock);
457         if (!(d_unhashed(tmp) && tmp->d_inode)) {
458                 dget_locked(tmp);
459                 __d_drop(tmp);
460                 spin_unlock(&tmp->d_lock);
461                 spin_unlock(&dcache_lock);
462                 simple_unlink(parent->d_inode, tmp);
463         } else {
464                 spin_unlock(&tmp->d_lock);
465                 spin_unlock(&dcache_lock);
466         }
467
468         ret = 0;
469 bail:
470         /*
471          * We don't expect clients to care about the return value, but
472          * it's there if they need it.
473          */
474         return ret;
475 }
476
477 static int remove_device_files(struct super_block *sb,
478                                struct qib_devdata *dd)
479 {
480         struct dentry *dir, *root;
481         char unit[10];
482         int ret, i;
483
484         root = dget(sb->s_root);
485         mutex_lock(&root->d_inode->i_mutex);
486         snprintf(unit, sizeof unit, "%u", dd->unit);
487         dir = lookup_one_len(unit, root, strlen(unit));
488
489         if (IS_ERR(dir)) {
490                 ret = PTR_ERR(dir);
491                 printk(KERN_ERR "Lookup of %s failed\n", unit);
492                 goto bail;
493         }
494
495         remove_file(dir, "counters");
496         remove_file(dir, "counter_names");
497         remove_file(dir, "portcounter_names");
498         for (i = 0; i < dd->num_pports; i++) {
499                 char fname[24];
500
501                 sprintf(fname, "port%dcounters", i + 1);
502                 remove_file(dir, fname);
503                 if (dd->flags & QIB_HAS_QSFP) {
504                         sprintf(fname, "qsfp%d", i + 1);
505                         remove_file(dir, fname);
506                 }
507         }
508         remove_file(dir, "flash");
509         d_delete(dir);
510         ret = simple_rmdir(root->d_inode, dir);
511
512 bail:
513         mutex_unlock(&root->d_inode->i_mutex);
514         dput(root);
515         return ret;
516 }
517
518 /*
519  * This fills everything in when the fs is mounted, to handle umount/mount
520  * after device init.  The direct add_cntr_files() call handles adding
521  * them from the init code, when the fs is already mounted.
522  */
523 static int qibfs_fill_super(struct super_block *sb, void *data, int silent)
524 {
525         struct qib_devdata *dd, *tmp;
526         unsigned long flags;
527         int ret;
528
529         static struct tree_descr files[] = {
530                 [2] = {"driver_stats", &driver_ops[0], S_IRUGO},
531                 [3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
532                 {""},
533         };
534
535         ret = simple_fill_super(sb, QIBFS_MAGIC, files);
536         if (ret) {
537                 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
538                 goto bail;
539         }
540
541         spin_lock_irqsave(&qib_devs_lock, flags);
542
543         list_for_each_entry_safe(dd, tmp, &qib_dev_list, list) {
544                 spin_unlock_irqrestore(&qib_devs_lock, flags);
545                 ret = add_cntr_files(sb, dd);
546                 if (ret)
547                         goto bail;
548                 spin_lock_irqsave(&qib_devs_lock, flags);
549         }
550
551         spin_unlock_irqrestore(&qib_devs_lock, flags);
552
553 bail:
554         return ret;
555 }
556
557 static int qibfs_get_sb(struct file_system_type *fs_type, int flags,
558                         const char *dev_name, void *data, struct vfsmount *mnt)
559 {
560         int ret = get_sb_single(fs_type, flags, data,
561                                 qibfs_fill_super, mnt);
562         if (ret >= 0)
563                 qib_super = mnt->mnt_sb;
564         return ret;
565 }
566
567 static void qibfs_kill_super(struct super_block *s)
568 {
569         kill_litter_super(s);
570         qib_super = NULL;
571 }
572
573 int qibfs_add(struct qib_devdata *dd)
574 {
575         int ret;
576
577         /*
578          * On first unit initialized, qib_super will not yet exist
579          * because nobody has yet tried to mount the filesystem, so
580          * we can't consider that to be an error; if an error occurs
581          * during the mount, that will get a complaint, so this is OK.
582          * add_cntr_files() for all units is done at mount from
583          * qibfs_fill_super(), so one way or another, everything works.
584          */
585         if (qib_super == NULL)
586                 ret = 0;
587         else
588                 ret = add_cntr_files(qib_super, dd);
589         return ret;
590 }
591
592 int qibfs_remove(struct qib_devdata *dd)
593 {
594         int ret = 0;
595
596         if (qib_super)
597                 ret = remove_device_files(qib_super, dd);
598
599         return ret;
600 }
601
602 static struct file_system_type qibfs_fs_type = {
603         .owner =        THIS_MODULE,
604         .name =         "ipathfs",
605         .get_sb =       qibfs_get_sb,
606         .kill_sb =      qibfs_kill_super,
607 };
608
609 int __init qib_init_qibfs(void)
610 {
611         return register_filesystem(&qibfs_fs_type);
612 }
613
614 int __exit qib_exit_qibfs(void)
615 {
616         return unregister_filesystem(&qibfs_fs_type);
617 }