Remove obsolete #include <linux/config.h>
[linux-3.10.git] / drivers / infiniband / hw / ipath / ipath_fs.c
1 /*
2  * Copyright (c) 2006 PathScale, Inc. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <linux/version.h>
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 #include <linux/pci.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, 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_blksize = PAGE_CACHE_SIZE;
64         inode->i_blocks = 0;
65         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
66         inode->u.generic_ip = data;
67         if ((mode & S_IFMT) == S_IFDIR) {
68                 inode->i_op = &simple_dir_inode_operations;
69                 inode->i_nlink++;
70                 dir->i_nlink++;
71         }
72
73         inode->i_fop = fops;
74
75         d_instantiate(dentry, inode);
76         error = 0;
77
78 bail:
79         return error;
80 }
81
82 static int create_file(const char *name, mode_t mode,
83                        struct dentry *parent, struct dentry **dentry,
84                        struct file_operations *fops, void *data)
85 {
86         int error;
87
88         *dentry = NULL;
89         mutex_lock(&parent->d_inode->i_mutex);
90         *dentry = lookup_one_len(name, parent, strlen(name));
91         if (!IS_ERR(dentry))
92                 error = ipathfs_mknod(parent->d_inode, *dentry,
93                                       mode, fops, data);
94         else
95                 error = PTR_ERR(dentry);
96         mutex_unlock(&parent->d_inode->i_mutex);
97
98         return error;
99 }
100
101 static ssize_t atomic_stats_read(struct file *file, char __user *buf,
102                                  size_t count, loff_t *ppos)
103 {
104         return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
105                                        sizeof ipath_stats);
106 }
107
108 static struct file_operations atomic_stats_ops = {
109         .read = atomic_stats_read,
110 };
111
112 #define NUM_COUNTERS sizeof(struct infinipath_counters) / sizeof(u64)
113
114 static ssize_t atomic_counters_read(struct file *file, char __user *buf,
115                                     size_t count, loff_t *ppos)
116 {
117         u64 counters[NUM_COUNTERS];
118         u16 i;
119         struct ipath_devdata *dd;
120
121         dd = file->f_dentry->d_inode->u.generic_ip;
122
123         for (i = 0; i < NUM_COUNTERS; i++)
124                 counters[i] = ipath_snap_cntr(dd, i);
125
126         return simple_read_from_buffer(buf, count, ppos, counters,
127                                        sizeof counters);
128 }
129
130 static struct file_operations atomic_counters_ops = {
131         .read = atomic_counters_read,
132 };
133
134 static ssize_t atomic_node_info_read(struct file *file, char __user *buf,
135                                      size_t count, loff_t *ppos)
136 {
137         u32 nodeinfo[10];
138         struct ipath_devdata *dd;
139         u64 guid;
140
141         dd = file->f_dentry->d_inode->u.generic_ip;
142
143         guid = be64_to_cpu(dd->ipath_guid);
144
145         nodeinfo[0] =                   /* BaseVersion is SMA */
146                 /* ClassVersion is SMA */
147                 (1 << 8)                /* NodeType  */
148                 | (1 << 0);             /* NumPorts */
149         nodeinfo[1] = (u32) (guid >> 32);
150         nodeinfo[2] = (u32) (guid & 0xffffffff);
151         /* PortGUID == SystemImageGUID for us */
152         nodeinfo[3] = nodeinfo[1];
153         /* PortGUID == SystemImageGUID for us */
154         nodeinfo[4] = nodeinfo[2];
155         /* PortGUID == NodeGUID for us */
156         nodeinfo[5] = nodeinfo[3];
157         /* PortGUID == NodeGUID for us */
158         nodeinfo[6] = nodeinfo[4];
159         nodeinfo[7] = (4 << 16) /* we support 4 pkeys */
160                 | (dd->ipath_deviceid << 0);
161         /* our chip version as 16 bits major, 16 bits minor */
162         nodeinfo[8] = dd->ipath_minrev | (dd->ipath_majrev << 16);
163         nodeinfo[9] = (dd->ipath_unit << 24) | (dd->ipath_vendorid << 0);
164
165         return simple_read_from_buffer(buf, count, ppos, nodeinfo,
166                                        sizeof nodeinfo);
167 }
168
169 static struct file_operations atomic_node_info_ops = {
170         .read = atomic_node_info_read,
171 };
172
173 static ssize_t atomic_port_info_read(struct file *file, char __user *buf,
174                                      size_t count, loff_t *ppos)
175 {
176         u32 portinfo[13];
177         u32 tmp, tmp2;
178         struct ipath_devdata *dd;
179
180         dd = file->f_dentry->d_inode->u.generic_ip;
181
182         /* so we only initialize non-zero fields. */
183         memset(portinfo, 0, sizeof portinfo);
184
185         /*
186          * Notimpl yet M_Key (64)
187          * Notimpl yet GID (64)
188          */
189
190         portinfo[4] = (dd->ipath_lid << 16);
191
192         /*
193          * Notimpl yet SMLID (should we store this in the driver, in case
194          * SMA dies?)  CapabilityMask is 0, we don't support any of these
195          * DiagCode is 0; we don't store any diag info for now Notimpl yet
196          * M_KeyLeasePeriod (we don't support M_Key)
197          */
198
199         /* LocalPortNum is whichever port number they ask for */
200         portinfo[7] = (dd->ipath_unit << 24)
201                 /* LinkWidthEnabled */
202                 | (2 << 16)
203                 /* LinkWidthSupported (really 2, but not IB valid) */
204                 | (3 << 8)
205                 /* LinkWidthActive */
206                 | (2 << 0);
207         tmp = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
208         tmp2 = 5;
209         if (tmp == IPATH_IBSTATE_INIT)
210                 tmp = 2;
211         else if (tmp == IPATH_IBSTATE_ARM)
212                 tmp = 3;
213         else if (tmp == IPATH_IBSTATE_ACTIVE)
214                 tmp = 4;
215         else {
216                 tmp = 0;        /* down */
217                 tmp2 = tmp & 0xf;
218         }
219
220         portinfo[8] = (1 << 28) /* LinkSpeedSupported */
221                 | (tmp << 24)   /* PortState */
222                 | (tmp2 << 20)  /* PortPhysicalState */
223                 | (2 << 16)
224
225                 /* LinkDownDefaultState */
226                 /* M_KeyProtectBits == 0 */
227                 /* NotImpl yet LMC == 0 (we can support all values) */
228                 | (1 << 4)      /* LinkSpeedActive */
229                 | (1 << 0);     /* LinkSpeedEnabled */
230         switch (dd->ipath_ibmtu) {
231         case 4096:
232                 tmp = 5;
233                 break;
234         case 2048:
235                 tmp = 4;
236                 break;
237         case 1024:
238                 tmp = 3;
239                 break;
240         case 512:
241                 tmp = 2;
242                 break;
243         case 256:
244                 tmp = 1;
245                 break;
246         default:                /* oops, something is wrong */
247                 ipath_dbg("Problem, ipath_ibmtu 0x%x not a valid IB MTU, "
248                           "treat as 2048\n", dd->ipath_ibmtu);
249                 tmp = 4;
250                 break;
251         }
252         portinfo[9] = (tmp << 28)
253                 /* NeighborMTU */
254                 /* Notimpl MasterSMSL */
255                 | (1 << 20)
256
257                 /* VLCap */
258                 /* Notimpl InitType (actually, an SMA decision) */
259                 /* VLHighLimit is 0 (only one VL) */
260                 ; /* VLArbitrationHighCap is 0 (only one VL) */
261         portinfo[10] =  /* VLArbitrationLowCap is 0 (only one VL) */
262                 /* InitTypeReply is SMA decision */
263                 (5 << 16)       /* MTUCap 4096 */
264                 | (7 << 13)     /* VLStallCount */
265                 | (0x1f << 8)   /* HOQLife */
266                 | (1 << 4)
267
268                 /* OperationalVLs 0 */
269                 /* PartitionEnforcementInbound */
270                 /* PartitionEnforcementOutbound not enforced */
271                 /* FilterRawinbound not enforced */
272                 ;               /* FilterRawOutbound not enforced */
273         /* M_KeyViolations are not counted by hardware, SMA can count */
274         tmp = ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
275         /* P_KeyViolations are counted by hardware. */
276         portinfo[11] = ((tmp & 0xffff) << 0);
277         portinfo[12] =
278                 /* Q_KeyViolations are not counted by hardware */
279                 (1 << 8)
280
281                 /* GUIDCap */
282                 /* SubnetTimeOut handled by SMA */
283                 /* RespTimeValue handled by SMA */
284                 ;
285         /* LocalPhyErrors are programmed to max */
286         portinfo[12] |= (0xf << 20)
287                 | (0xf << 16)   /* OverRunErrors are programmed to max */
288                 ;
289
290         return simple_read_from_buffer(buf, count, ppos, portinfo,
291                                        sizeof portinfo);
292 }
293
294 static struct file_operations atomic_port_info_ops = {
295         .read = atomic_port_info_read,
296 };
297
298 static ssize_t flash_read(struct file *file, char __user *buf,
299                           size_t count, loff_t *ppos)
300 {
301         struct ipath_devdata *dd;
302         ssize_t ret;
303         loff_t pos;
304         char *tmp;
305
306         pos = *ppos;
307
308         if ( pos < 0) {
309                 ret = -EINVAL;
310                 goto bail;
311         }
312
313         if (pos >= sizeof(struct ipath_flash)) {
314                 ret = 0;
315                 goto bail;
316         }
317
318         if (count > sizeof(struct ipath_flash) - pos)
319                 count = sizeof(struct ipath_flash) - pos;
320
321         tmp = kmalloc(count, GFP_KERNEL);
322         if (!tmp) {
323                 ret = -ENOMEM;
324                 goto bail;
325         }
326
327         dd = file->f_dentry->d_inode->u.generic_ip;
328         if (ipath_eeprom_read(dd, pos, tmp, count)) {
329                 ipath_dev_err(dd, "failed to read from flash\n");
330                 ret = -ENXIO;
331                 goto bail_tmp;
332         }
333
334         if (copy_to_user(buf, tmp, count)) {
335                 ret = -EFAULT;
336                 goto bail_tmp;
337         }
338
339         *ppos = pos + count;
340         ret = count;
341
342 bail_tmp:
343         kfree(tmp);
344
345 bail:
346         return ret;
347 }
348
349 static ssize_t flash_write(struct file *file, const char __user *buf,
350                            size_t count, loff_t *ppos)
351 {
352         struct ipath_devdata *dd;
353         ssize_t ret;
354         loff_t pos;
355         char *tmp;
356
357         pos = *ppos;
358
359         if ( pos < 0) {
360                 ret = -EINVAL;
361                 goto bail;
362         }
363
364         if (pos >= sizeof(struct ipath_flash)) {
365                 ret = 0;
366                 goto bail;
367         }
368
369         if (count > sizeof(struct ipath_flash) - pos)
370                 count = sizeof(struct ipath_flash) - pos;
371
372         tmp = kmalloc(count, GFP_KERNEL);
373         if (!tmp) {
374                 ret = -ENOMEM;
375                 goto bail;
376         }
377
378         if (copy_from_user(tmp, buf, count)) {
379                 ret = -EFAULT;
380                 goto bail_tmp;
381         }
382
383         dd = file->f_dentry->d_inode->u.generic_ip;
384         if (ipath_eeprom_write(dd, pos, tmp, count)) {
385                 ret = -ENXIO;
386                 ipath_dev_err(dd, "failed to write to flash\n");
387                 goto bail_tmp;
388         }
389
390         *ppos = pos + count;
391         ret = count;
392
393 bail_tmp:
394         kfree(tmp);
395
396 bail:
397         return ret;
398 }
399
400 static struct file_operations flash_ops = {
401         .read = flash_read,
402         .write = flash_write,
403 };
404
405 static int create_device_files(struct super_block *sb,
406                                struct ipath_devdata *dd)
407 {
408         struct dentry *dir, *tmp;
409         char unit[10];
410         int ret;
411
412         snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
413         ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
414                           (struct file_operations *) &simple_dir_operations,
415                           dd);
416         if (ret) {
417                 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
418                 goto bail;
419         }
420
421         ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
422                           &atomic_counters_ops, dd);
423         if (ret) {
424                 printk(KERN_ERR "create_file(%s/atomic_counters) "
425                        "failed: %d\n", unit, ret);
426                 goto bail;
427         }
428
429         ret = create_file("node_info", S_IFREG|S_IRUGO, dir, &tmp,
430                           &atomic_node_info_ops, dd);
431         if (ret) {
432                 printk(KERN_ERR "create_file(%s/node_info) "
433                        "failed: %d\n", unit, ret);
434                 goto bail;
435         }
436
437         ret = create_file("port_info", S_IFREG|S_IRUGO, dir, &tmp,
438                           &atomic_port_info_ops, dd);
439         if (ret) {
440                 printk(KERN_ERR "create_file(%s/port_info) "
441                        "failed: %d\n", unit, ret);
442                 goto bail;
443         }
444
445         ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
446                           &flash_ops, dd);
447         if (ret) {
448                 printk(KERN_ERR "create_file(%s/flash) "
449                        "failed: %d\n", unit, ret);
450                 goto bail;
451         }
452
453 bail:
454         return ret;
455 }
456
457 static void remove_file(struct dentry *parent, char *name)
458 {
459         struct dentry *tmp;
460
461         tmp = lookup_one_len(name, parent, strlen(name));
462
463         spin_lock(&dcache_lock);
464         spin_lock(&tmp->d_lock);
465         if (!(d_unhashed(tmp) && tmp->d_inode)) {
466                 dget_locked(tmp);
467                 __d_drop(tmp);
468                 spin_unlock(&tmp->d_lock);
469                 spin_unlock(&dcache_lock);
470                 simple_unlink(parent->d_inode, tmp);
471         } else {
472                 spin_unlock(&tmp->d_lock);
473                 spin_unlock(&dcache_lock);
474         }
475 }
476
477 static int remove_device_files(struct super_block *sb,
478                                struct ipath_devdata *dd)
479 {
480         struct dentry *dir, *root;
481         char unit[10];
482         int ret;
483
484         root = dget(sb->s_root);
485         mutex_lock(&root->d_inode->i_mutex);
486         snprintf(unit, sizeof unit, "%02d", dd->ipath_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, "flash");
496         remove_file(dir, "port_info");
497         remove_file(dir, "node_info");
498         remove_file(dir, "atomic_counters");
499         d_delete(dir);
500         ret = simple_rmdir(root->d_inode, dir);
501
502 bail:
503         mutex_unlock(&root->d_inode->i_mutex);
504         dput(root);
505         return ret;
506 }
507
508 static int ipathfs_fill_super(struct super_block *sb, void *data,
509                               int silent)
510 {
511         struct ipath_devdata *dd, *tmp;
512         unsigned long flags;
513         int ret;
514
515         static struct tree_descr files[] = {
516                 [1] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
517                 {""},
518         };
519
520         ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
521         if (ret) {
522                 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
523                 goto bail;
524         }
525
526         spin_lock_irqsave(&ipath_devs_lock, flags);
527
528         list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
529                 spin_unlock_irqrestore(&ipath_devs_lock, flags);
530                 ret = create_device_files(sb, dd);
531                 if (ret) {
532                         deactivate_super(sb);
533                         goto bail;
534                 }
535                 spin_lock_irqsave(&ipath_devs_lock, flags);
536         }
537
538         spin_unlock_irqrestore(&ipath_devs_lock, flags);
539
540 bail:
541         return ret;
542 }
543
544 static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
545                         const char *dev_name, void *data, struct vfsmount *mnt)
546 {
547         int ret = get_sb_single(fs_type, flags, data,
548                                     ipathfs_fill_super, mnt);
549         if (ret >= 0)
550                 ipath_super = mnt->mnt_sb;
551         return ret;
552 }
553
554 static void ipathfs_kill_super(struct super_block *s)
555 {
556         kill_litter_super(s);
557         ipath_super = NULL;
558 }
559
560 int ipathfs_add_device(struct ipath_devdata *dd)
561 {
562         int ret;
563
564         if (ipath_super == NULL) {
565                 ret = 0;
566                 goto bail;
567         }
568
569         ret = create_device_files(ipath_super, dd);
570
571 bail:
572         return ret;
573 }
574
575 int ipathfs_remove_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 = remove_device_files(ipath_super, dd);
585
586 bail:
587         return ret;
588 }
589
590 static struct file_system_type ipathfs_fs_type = {
591         .owner =        THIS_MODULE,
592         .name =         "ipathfs",
593         .get_sb =       ipathfs_get_sb,
594         .kill_sb =      ipathfs_kill_super,
595 };
596
597 int __init ipath_init_ipathfs(void)
598 {
599         return register_filesystem(&ipathfs_fs_type);
600 }
601
602 void __exit ipath_exit_ipathfs(void)
603 {
604         unregister_filesystem(&ipathfs_fs_type);
605 }