Pull ec into release branch
[linux-2.6] / drivers / infiniband / hw / ipath / ipath_fs.c
1 /*
2  * Copyright (c) 2006, 2007 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/version.h>
35 #include <linux/module.h>
36 #include <linux/fs.h>
37 #include <linux/mount.h>
38 #include <linux/pagemap.h>
39 #include <linux/init.h>
40 #include <linux/namei.h>
41
42 #include "ipath_kernel.h"
43
44 #define IPATHFS_MAGIC 0x726a77
45
46 static struct super_block *ipath_super;
47
48 static int ipathfs_mknod(struct inode *dir, struct dentry *dentry,
49                          int mode, const struct file_operations *fops,
50                          void *data)
51 {
52         int error;
53         struct inode *inode = new_inode(dir->i_sb);
54
55         if (!inode) {
56                 error = -EPERM;
57                 goto bail;
58         }
59
60         inode->i_mode = mode;
61         inode->i_uid = 0;
62         inode->i_gid = 0;
63         inode->i_blocks = 0;
64         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
65         inode->i_private = data;
66         if ((mode & S_IFMT) == S_IFDIR) {
67                 inode->i_op = &simple_dir_inode_operations;
68                 inc_nlink(inode);
69                 inc_nlink(dir);
70         }
71
72         inode->i_fop = fops;
73
74         d_instantiate(dentry, inode);
75         error = 0;
76
77 bail:
78         return error;
79 }
80
81 static int create_file(const char *name, mode_t mode,
82                        struct dentry *parent, struct dentry **dentry,
83                        const struct file_operations *fops, void *data)
84 {
85         int error;
86
87         *dentry = NULL;
88         mutex_lock(&parent->d_inode->i_mutex);
89         *dentry = lookup_one_len(name, parent, strlen(name));
90         if (!IS_ERR(dentry))
91                 error = ipathfs_mknod(parent->d_inode, *dentry,
92                                       mode, fops, data);
93         else
94                 error = PTR_ERR(dentry);
95         mutex_unlock(&parent->d_inode->i_mutex);
96
97         return error;
98 }
99
100 static ssize_t atomic_stats_read(struct file *file, char __user *buf,
101                                  size_t count, loff_t *ppos)
102 {
103         return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
104                                        sizeof ipath_stats);
105 }
106
107 static const struct file_operations atomic_stats_ops = {
108         .read = atomic_stats_read,
109 };
110
111 #define NUM_COUNTERS sizeof(struct infinipath_counters) / sizeof(u64)
112
113 static ssize_t atomic_counters_read(struct file *file, char __user *buf,
114                                     size_t count, loff_t *ppos)
115 {
116         u64 counters[NUM_COUNTERS];
117         u16 i;
118         struct ipath_devdata *dd;
119
120         dd = file->f_path.dentry->d_inode->i_private;
121
122         for (i = 0; i < NUM_COUNTERS; i++)
123                 counters[i] = ipath_snap_cntr(dd, i);
124
125         return simple_read_from_buffer(buf, count, ppos, counters,
126                                        sizeof counters);
127 }
128
129 static const struct file_operations atomic_counters_ops = {
130         .read = atomic_counters_read,
131 };
132
133 static ssize_t atomic_node_info_read(struct file *file, char __user *buf,
134                                      size_t count, loff_t *ppos)
135 {
136         u32 nodeinfo[10];
137         struct ipath_devdata *dd;
138         u64 guid;
139
140         dd = file->f_path.dentry->d_inode->i_private;
141
142         guid = be64_to_cpu(dd->ipath_guid);
143
144         nodeinfo[0] =                   /* BaseVersion is SMA */
145                 /* ClassVersion is SMA */
146                 (1 << 8)                /* NodeType  */
147                 | (1 << 0);             /* NumPorts */
148         nodeinfo[1] = (u32) (guid >> 32);
149         nodeinfo[2] = (u32) (guid & 0xffffffff);
150         /* PortGUID == SystemImageGUID for us */
151         nodeinfo[3] = nodeinfo[1];
152         /* PortGUID == SystemImageGUID for us */
153         nodeinfo[4] = nodeinfo[2];
154         /* PortGUID == NodeGUID for us */
155         nodeinfo[5] = nodeinfo[3];
156         /* PortGUID == NodeGUID for us */
157         nodeinfo[6] = nodeinfo[4];
158         nodeinfo[7] = (4 << 16) /* we support 4 pkeys */
159                 | (dd->ipath_deviceid << 0);
160         /* our chip version as 16 bits major, 16 bits minor */
161         nodeinfo[8] = dd->ipath_minrev | (dd->ipath_majrev << 16);
162         nodeinfo[9] = (dd->ipath_unit << 24) | (dd->ipath_vendorid << 0);
163
164         return simple_read_from_buffer(buf, count, ppos, nodeinfo,
165                                        sizeof nodeinfo);
166 }
167
168 static const struct file_operations atomic_node_info_ops = {
169         .read = atomic_node_info_read,
170 };
171
172 static ssize_t atomic_port_info_read(struct file *file, char __user *buf,
173                                      size_t count, loff_t *ppos)
174 {
175         u32 portinfo[13];
176         u32 tmp, tmp2;
177         struct ipath_devdata *dd;
178
179         dd = file->f_path.dentry->d_inode->i_private;
180
181         /* so we only initialize non-zero fields. */
182         memset(portinfo, 0, sizeof portinfo);
183
184         /*
185          * Notimpl yet M_Key (64)
186          * Notimpl yet GID (64)
187          */
188
189         portinfo[4] = (dd->ipath_lid << 16);
190
191         /*
192          * Notimpl yet SMLID.
193          * CapabilityMask is 0, we don't support any of these
194          * DiagCode is 0; we don't store any diag info for now Notimpl yet
195          * M_KeyLeasePeriod (we don't support M_Key)
196          */
197
198         /* LocalPortNum is whichever port number they ask for */
199         portinfo[7] = (dd->ipath_unit << 24)
200                 /* LinkWidthEnabled */
201                 | (2 << 16)
202                 /* LinkWidthSupported (really 2, but not IB valid) */
203                 | (3 << 8)
204                 /* LinkWidthActive */
205                 | (2 << 0);
206         tmp = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
207         tmp2 = 5;
208         if (tmp == IPATH_IBSTATE_INIT)
209                 tmp = 2;
210         else if (tmp == IPATH_IBSTATE_ARM)
211                 tmp = 3;
212         else if (tmp == IPATH_IBSTATE_ACTIVE)
213                 tmp = 4;
214         else {
215                 tmp = 0;        /* down */
216                 tmp2 = tmp & 0xf;
217         }
218
219         portinfo[8] = (1 << 28) /* LinkSpeedSupported */
220                 | (tmp << 24)   /* PortState */
221                 | (tmp2 << 20)  /* PortPhysicalState */
222                 | (2 << 16)
223
224                 /* LinkDownDefaultState */
225                 /* M_KeyProtectBits == 0 */
226                 /* NotImpl yet LMC == 0 (we can support all values) */
227                 | (1 << 4)      /* LinkSpeedActive */
228                 | (1 << 0);     /* LinkSpeedEnabled */
229         switch (dd->ipath_ibmtu) {
230         case 4096:
231                 tmp = 5;
232                 break;
233         case 2048:
234                 tmp = 4;
235                 break;
236         case 1024:
237                 tmp = 3;
238                 break;
239         case 512:
240                 tmp = 2;
241                 break;
242         case 256:
243                 tmp = 1;
244                 break;
245         default:                /* oops, something is wrong */
246                 ipath_dbg("Problem, ipath_ibmtu 0x%x not a valid IB MTU, "
247                           "treat as 2048\n", dd->ipath_ibmtu);
248                 tmp = 4;
249                 break;
250         }
251         portinfo[9] = (tmp << 28)
252                 /* NeighborMTU */
253                 /* Notimpl MasterSMSL */
254                 | (1 << 20)
255
256                 /* VLCap */
257                 /* Notimpl InitType (actually, an SMA decision) */
258                 /* VLHighLimit is 0 (only one VL) */
259                 ; /* VLArbitrationHighCap is 0 (only one VL) */
260         /*
261          * Note: the chips support a maximum MTU of 4096, but the driver
262          * hasn't implemented this feature yet, so set the maximum
263          * to 2048.
264          */
265         portinfo[10] =  /* VLArbitrationLowCap is 0 (only one VL) */
266                 /* InitTypeReply is SMA decision */
267                 (4 << 16)       /* MTUCap 2048 */
268                 | (7 << 13)     /* VLStallCount */
269                 | (0x1f << 8)   /* HOQLife */
270                 | (1 << 4)
271
272                 /* OperationalVLs 0 */
273                 /* PartitionEnforcementInbound */
274                 /* PartitionEnforcementOutbound not enforced */
275                 /* FilterRawinbound not enforced */
276                 ;               /* FilterRawOutbound not enforced */
277         /* M_KeyViolations are not counted by hardware, SMA can count */
278         tmp = ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
279         /* P_KeyViolations are counted by hardware. */
280         portinfo[11] = ((tmp & 0xffff) << 0);
281         portinfo[12] =
282                 /* Q_KeyViolations are not counted by hardware */
283                 (1 << 8)
284
285                 /* GUIDCap */
286                 /* SubnetTimeOut handled by SMA */
287                 /* RespTimeValue handled by SMA */
288                 ;
289         /* LocalPhyErrors are programmed to max */
290         portinfo[12] |= (0xf << 20)
291                 | (0xf << 16)   /* OverRunErrors are programmed to max */
292                 ;
293
294         return simple_read_from_buffer(buf, count, ppos, portinfo,
295                                        sizeof portinfo);
296 }
297
298 static const struct file_operations atomic_port_info_ops = {
299         .read = atomic_port_info_read,
300 };
301
302 static ssize_t flash_read(struct file *file, char __user *buf,
303                           size_t count, loff_t *ppos)
304 {
305         struct ipath_devdata *dd;
306         ssize_t ret;
307         loff_t pos;
308         char *tmp;
309
310         pos = *ppos;
311
312         if ( pos < 0) {
313                 ret = -EINVAL;
314                 goto bail;
315         }
316
317         if (pos >= sizeof(struct ipath_flash)) {
318                 ret = 0;
319                 goto bail;
320         }
321
322         if (count > sizeof(struct ipath_flash) - pos)
323                 count = sizeof(struct ipath_flash) - pos;
324
325         tmp = kmalloc(count, GFP_KERNEL);
326         if (!tmp) {
327                 ret = -ENOMEM;
328                 goto bail;
329         }
330
331         dd = file->f_path.dentry->d_inode->i_private;
332         if (ipath_eeprom_read(dd, pos, tmp, count)) {
333                 ipath_dev_err(dd, "failed to read from flash\n");
334                 ret = -ENXIO;
335                 goto bail_tmp;
336         }
337
338         if (copy_to_user(buf, tmp, count)) {
339                 ret = -EFAULT;
340                 goto bail_tmp;
341         }
342
343         *ppos = pos + count;
344         ret = count;
345
346 bail_tmp:
347         kfree(tmp);
348
349 bail:
350         return ret;
351 }
352
353 static ssize_t flash_write(struct file *file, const char __user *buf,
354                            size_t count, loff_t *ppos)
355 {
356         struct ipath_devdata *dd;
357         ssize_t ret;
358         loff_t pos;
359         char *tmp;
360
361         pos = *ppos;
362
363         if (pos != 0) {
364                 ret = -EINVAL;
365                 goto bail;
366         }
367
368         if (count != sizeof(struct ipath_flash)) {
369                 ret = -EINVAL;
370                 goto bail;
371         }
372
373         tmp = kmalloc(count, GFP_KERNEL);
374         if (!tmp) {
375                 ret = -ENOMEM;
376                 goto bail;
377         }
378
379         if (copy_from_user(tmp, buf, count)) {
380                 ret = -EFAULT;
381                 goto bail_tmp;
382         }
383
384         dd = file->f_path.dentry->d_inode->i_private;
385         if (ipath_eeprom_write(dd, pos, tmp, count)) {
386                 ret = -ENXIO;
387                 ipath_dev_err(dd, "failed to write to flash\n");
388                 goto bail_tmp;
389         }
390
391         *ppos = pos + count;
392         ret = count;
393
394 bail_tmp:
395         kfree(tmp);
396
397 bail:
398         return ret;
399 }
400
401 static const struct file_operations flash_ops = {
402         .read = flash_read,
403         .write = flash_write,
404 };
405
406 static int create_device_files(struct super_block *sb,
407                                struct ipath_devdata *dd)
408 {
409         struct dentry *dir, *tmp;
410         char unit[10];
411         int ret;
412
413         snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
414         ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
415                           (struct file_operations *) &simple_dir_operations,
416                           dd);
417         if (ret) {
418                 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
419                 goto bail;
420         }
421
422         ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
423                           &atomic_counters_ops, dd);
424         if (ret) {
425                 printk(KERN_ERR "create_file(%s/atomic_counters) "
426                        "failed: %d\n", unit, ret);
427                 goto bail;
428         }
429
430         ret = create_file("node_info", S_IFREG|S_IRUGO, dir, &tmp,
431                           &atomic_node_info_ops, dd);
432         if (ret) {
433                 printk(KERN_ERR "create_file(%s/node_info) "
434                        "failed: %d\n", unit, ret);
435                 goto bail;
436         }
437
438         ret = create_file("port_info", S_IFREG|S_IRUGO, dir, &tmp,
439                           &atomic_port_info_ops, dd);
440         if (ret) {
441                 printk(KERN_ERR "create_file(%s/port_info) "
442                        "failed: %d\n", unit, ret);
443                 goto bail;
444         }
445
446         ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
447                           &flash_ops, dd);
448         if (ret) {
449                 printk(KERN_ERR "create_file(%s/flash) "
450                        "failed: %d\n", unit, ret);
451                 goto bail;
452         }
453
454 bail:
455         return ret;
456 }
457
458 static int remove_file(struct dentry *parent, char *name)
459 {
460         struct dentry *tmp;
461         int ret;
462
463         tmp = lookup_one_len(name, parent, strlen(name));
464
465         if (IS_ERR(tmp)) {
466                 ret = PTR_ERR(tmp);
467                 goto bail;
468         }
469
470         spin_lock(&dcache_lock);
471         spin_lock(&tmp->d_lock);
472         if (!(d_unhashed(tmp) && tmp->d_inode)) {
473                 dget_locked(tmp);
474                 __d_drop(tmp);
475                 spin_unlock(&tmp->d_lock);
476                 spin_unlock(&dcache_lock);
477                 simple_unlink(parent->d_inode, tmp);
478         } else {
479                 spin_unlock(&tmp->d_lock);
480                 spin_unlock(&dcache_lock);
481         }
482
483         ret = 0;
484 bail:
485         /*
486          * We don't expect clients to care about the return value, but
487          * it's there if they need it.
488          */
489         return ret;
490 }
491
492 static int remove_device_files(struct super_block *sb,
493                                struct ipath_devdata *dd)
494 {
495         struct dentry *dir, *root;
496         char unit[10];
497         int ret;
498
499         root = dget(sb->s_root);
500         mutex_lock(&root->d_inode->i_mutex);
501         snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
502         dir = lookup_one_len(unit, root, strlen(unit));
503
504         if (IS_ERR(dir)) {
505                 ret = PTR_ERR(dir);
506                 printk(KERN_ERR "Lookup of %s failed\n", unit);
507                 goto bail;
508         }
509
510         remove_file(dir, "flash");
511         remove_file(dir, "port_info");
512         remove_file(dir, "node_info");
513         remove_file(dir, "atomic_counters");
514         d_delete(dir);
515         ret = simple_rmdir(root->d_inode, dir);
516
517 bail:
518         mutex_unlock(&root->d_inode->i_mutex);
519         dput(root);
520         return ret;
521 }
522
523 static int ipathfs_fill_super(struct super_block *sb, void *data,
524                               int silent)
525 {
526         struct ipath_devdata *dd, *tmp;
527         unsigned long flags;
528         int ret;
529
530         static struct tree_descr files[] = {
531                 [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
532                 {""},
533         };
534
535         ret = simple_fill_super(sb, IPATHFS_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(&ipath_devs_lock, flags);
542
543         list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
544                 spin_unlock_irqrestore(&ipath_devs_lock, flags);
545                 ret = create_device_files(sb, dd);
546                 if (ret) {
547                         deactivate_super(sb);
548                         goto bail;
549                 }
550                 spin_lock_irqsave(&ipath_devs_lock, flags);
551         }
552
553         spin_unlock_irqrestore(&ipath_devs_lock, flags);
554
555 bail:
556         return ret;
557 }
558
559 static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
560                         const char *dev_name, void *data, struct vfsmount *mnt)
561 {
562         int ret = get_sb_single(fs_type, flags, data,
563                                     ipathfs_fill_super, mnt);
564         if (ret >= 0)
565                 ipath_super = mnt->mnt_sb;
566         return ret;
567 }
568
569 static void ipathfs_kill_super(struct super_block *s)
570 {
571         kill_litter_super(s);
572         ipath_super = NULL;
573 }
574
575 int ipathfs_add_device(struct ipath_devdata *dd)
576 {
577         int ret;
578
579         if (ipath_super == NULL) {
580                 ret = 0;
581                 goto bail;
582         }
583
584         ret = create_device_files(ipath_super, dd);
585
586 bail:
587         return ret;
588 }
589
590 int ipathfs_remove_device(struct ipath_devdata *dd)
591 {
592         int ret;
593
594         if (ipath_super == NULL) {
595                 ret = 0;
596                 goto bail;
597         }
598
599         ret = remove_device_files(ipath_super, dd);
600
601 bail:
602         return ret;
603 }
604
605 static struct file_system_type ipathfs_fs_type = {
606         .owner =        THIS_MODULE,
607         .name =         "ipathfs",
608         .get_sb =       ipathfs_get_sb,
609         .kill_sb =      ipathfs_kill_super,
610 };
611
612 int __init ipath_init_ipathfs(void)
613 {
614         return register_filesystem(&ipathfs_fs_type);
615 }
616
617 void __exit ipath_exit_ipathfs(void)
618 {
619         unregister_filesystem(&ipathfs_fs_type);
620 }