Pull acpi_bus_register_driver into release branch
[pandora-kernel.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/config.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 #include <linux/pci.h>
42
43 #include "ipath_kernel.h"
44
45 #define IPATHFS_MAGIC 0x726a77
46
47 static struct super_block *ipath_super;
48
49 static int ipathfs_mknod(struct inode *dir, struct dentry *dentry,
50                          int mode, 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_blksize = PAGE_CACHE_SIZE;
65         inode->i_blocks = 0;
66         inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
67         inode->u.generic_ip = data;
68         if ((mode & S_IFMT) == S_IFDIR) {
69                 inode->i_op = &simple_dir_inode_operations;
70                 inode->i_nlink++;
71                 dir->i_nlink++;
72         }
73
74         inode->i_fop = fops;
75
76         d_instantiate(dentry, inode);
77         error = 0;
78
79 bail:
80         return error;
81 }
82
83 static int create_file(const char *name, mode_t mode,
84                        struct dentry *parent, struct dentry **dentry,
85                        struct file_operations *fops, void *data)
86 {
87         int error;
88
89         *dentry = NULL;
90         mutex_lock(&parent->d_inode->i_mutex);
91         *dentry = lookup_one_len(name, parent, strlen(name));
92         if (!IS_ERR(dentry))
93                 error = ipathfs_mknod(parent->d_inode, *dentry,
94                                       mode, fops, data);
95         else
96                 error = PTR_ERR(dentry);
97         mutex_unlock(&parent->d_inode->i_mutex);
98
99         return error;
100 }
101
102 static ssize_t atomic_stats_read(struct file *file, char __user *buf,
103                                  size_t count, loff_t *ppos)
104 {
105         return simple_read_from_buffer(buf, count, ppos, &ipath_stats,
106                                        sizeof ipath_stats);
107 }
108
109 static struct file_operations atomic_stats_ops = {
110         .read = atomic_stats_read,
111 };
112
113 #define NUM_COUNTERS sizeof(struct infinipath_counters) / sizeof(u64)
114
115 static ssize_t atomic_counters_read(struct file *file, char __user *buf,
116                                     size_t count, loff_t *ppos)
117 {
118         u64 counters[NUM_COUNTERS];
119         u16 i;
120         struct ipath_devdata *dd;
121
122         dd = file->f_dentry->d_inode->u.generic_ip;
123
124         for (i = 0; i < NUM_COUNTERS; i++)
125                 counters[i] = ipath_snap_cntr(dd, i);
126
127         return simple_read_from_buffer(buf, count, ppos, counters,
128                                        sizeof counters);
129 }
130
131 static struct file_operations atomic_counters_ops = {
132         .read = atomic_counters_read,
133 };
134
135 static ssize_t atomic_node_info_read(struct file *file, char __user *buf,
136                                      size_t count, loff_t *ppos)
137 {
138         u32 nodeinfo[10];
139         struct ipath_devdata *dd;
140         u64 guid;
141
142         dd = file->f_dentry->d_inode->u.generic_ip;
143
144         guid = be64_to_cpu(dd->ipath_guid);
145
146         nodeinfo[0] =                   /* BaseVersion is SMA */
147                 /* ClassVersion is SMA */
148                 (1 << 8)                /* NodeType  */
149                 | (1 << 0);             /* NumPorts */
150         nodeinfo[1] = (u32) (guid >> 32);
151         nodeinfo[2] = (u32) (guid & 0xffffffff);
152         /* PortGUID == SystemImageGUID for us */
153         nodeinfo[3] = nodeinfo[1];
154         /* PortGUID == SystemImageGUID for us */
155         nodeinfo[4] = nodeinfo[2];
156         /* PortGUID == NodeGUID for us */
157         nodeinfo[5] = nodeinfo[3];
158         /* PortGUID == NodeGUID for us */
159         nodeinfo[6] = nodeinfo[4];
160         nodeinfo[7] = (4 << 16) /* we support 4 pkeys */
161                 | (dd->ipath_deviceid << 0);
162         /* our chip version as 16 bits major, 16 bits minor */
163         nodeinfo[8] = dd->ipath_minrev | (dd->ipath_majrev << 16);
164         nodeinfo[9] = (dd->ipath_unit << 24) | (dd->ipath_vendorid << 0);
165
166         return simple_read_from_buffer(buf, count, ppos, nodeinfo,
167                                        sizeof nodeinfo);
168 }
169
170 static struct file_operations atomic_node_info_ops = {
171         .read = atomic_node_info_read,
172 };
173
174 static ssize_t atomic_port_info_read(struct file *file, char __user *buf,
175                                      size_t count, loff_t *ppos)
176 {
177         u32 portinfo[13];
178         u32 tmp, tmp2;
179         struct ipath_devdata *dd;
180
181         dd = file->f_dentry->d_inode->u.generic_ip;
182
183         /* so we only initialize non-zero fields. */
184         memset(portinfo, 0, sizeof portinfo);
185
186         /*
187          * Notimpl yet M_Key (64)
188          * Notimpl yet GID (64)
189          */
190
191         portinfo[4] = (dd->ipath_lid << 16);
192
193         /*
194          * Notimpl yet SMLID (should we store this in the driver, in case
195          * SMA dies?)  CapabilityMask is 0, we don't support any of these
196          * DiagCode is 0; we don't store any diag info for now Notimpl yet
197          * M_KeyLeasePeriod (we don't support M_Key)
198          */
199
200         /* LocalPortNum is whichever port number they ask for */
201         portinfo[7] = (dd->ipath_unit << 24)
202                 /* LinkWidthEnabled */
203                 | (2 << 16)
204                 /* LinkWidthSupported (really 2, but not IB valid) */
205                 | (3 << 8)
206                 /* LinkWidthActive */
207                 | (2 << 0);
208         tmp = dd->ipath_lastibcstat & IPATH_IBSTATE_MASK;
209         tmp2 = 5;
210         if (tmp == IPATH_IBSTATE_INIT)
211                 tmp = 2;
212         else if (tmp == IPATH_IBSTATE_ARM)
213                 tmp = 3;
214         else if (tmp == IPATH_IBSTATE_ACTIVE)
215                 tmp = 4;
216         else {
217                 tmp = 0;        /* down */
218                 tmp2 = tmp & 0xf;
219         }
220
221         portinfo[8] = (1 << 28) /* LinkSpeedSupported */
222                 | (tmp << 24)   /* PortState */
223                 | (tmp2 << 20)  /* PortPhysicalState */
224                 | (2 << 16)
225
226                 /* LinkDownDefaultState */
227                 /* M_KeyProtectBits == 0 */
228                 /* NotImpl yet LMC == 0 (we can support all values) */
229                 | (1 << 4)      /* LinkSpeedActive */
230                 | (1 << 0);     /* LinkSpeedEnabled */
231         switch (dd->ipath_ibmtu) {
232         case 4096:
233                 tmp = 5;
234                 break;
235         case 2048:
236                 tmp = 4;
237                 break;
238         case 1024:
239                 tmp = 3;
240                 break;
241         case 512:
242                 tmp = 2;
243                 break;
244         case 256:
245                 tmp = 1;
246                 break;
247         default:                /* oops, something is wrong */
248                 ipath_dbg("Problem, ipath_ibmtu 0x%x not a valid IB MTU, "
249                           "treat as 2048\n", dd->ipath_ibmtu);
250                 tmp = 4;
251                 break;
252         }
253         portinfo[9] = (tmp << 28)
254                 /* NeighborMTU */
255                 /* Notimpl MasterSMSL */
256                 | (1 << 20)
257
258                 /* VLCap */
259                 /* Notimpl InitType (actually, an SMA decision) */
260                 /* VLHighLimit is 0 (only one VL) */
261                 ; /* VLArbitrationHighCap is 0 (only one VL) */
262         portinfo[10] =  /* VLArbitrationLowCap is 0 (only one VL) */
263                 /* InitTypeReply is SMA decision */
264                 (5 << 16)       /* MTUCap 4096 */
265                 | (7 << 13)     /* VLStallCount */
266                 | (0x1f << 8)   /* HOQLife */
267                 | (1 << 4)
268
269                 /* OperationalVLs 0 */
270                 /* PartitionEnforcementInbound */
271                 /* PartitionEnforcementOutbound not enforced */
272                 /* FilterRawinbound not enforced */
273                 ;               /* FilterRawOutbound not enforced */
274         /* M_KeyViolations are not counted by hardware, SMA can count */
275         tmp = ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
276         /* P_KeyViolations are counted by hardware. */
277         portinfo[11] = ((tmp & 0xffff) << 0);
278         portinfo[12] =
279                 /* Q_KeyViolations are not counted by hardware */
280                 (1 << 8)
281
282                 /* GUIDCap */
283                 /* SubnetTimeOut handled by SMA */
284                 /* RespTimeValue handled by SMA */
285                 ;
286         /* LocalPhyErrors are programmed to max */
287         portinfo[12] |= (0xf << 20)
288                 | (0xf << 16)   /* OverRunErrors are programmed to max */
289                 ;
290
291         return simple_read_from_buffer(buf, count, ppos, portinfo,
292                                        sizeof portinfo);
293 }
294
295 static struct file_operations atomic_port_info_ops = {
296         .read = atomic_port_info_read,
297 };
298
299 static ssize_t flash_read(struct file *file, char __user *buf,
300                           size_t count, loff_t *ppos)
301 {
302         struct ipath_devdata *dd;
303         ssize_t ret;
304         loff_t pos;
305         char *tmp;
306
307         pos = *ppos;
308
309         if ( pos < 0) {
310                 ret = -EINVAL;
311                 goto bail;
312         }
313
314         if (pos >= sizeof(struct ipath_flash)) {
315                 ret = 0;
316                 goto bail;
317         }
318
319         if (count > sizeof(struct ipath_flash) - pos)
320                 count = sizeof(struct ipath_flash) - pos;
321
322         tmp = kmalloc(count, GFP_KERNEL);
323         if (!tmp) {
324                 ret = -ENOMEM;
325                 goto bail;
326         }
327
328         dd = file->f_dentry->d_inode->u.generic_ip;
329         if (ipath_eeprom_read(dd, pos, tmp, count)) {
330                 ipath_dev_err(dd, "failed to read from flash\n");
331                 ret = -ENXIO;
332                 goto bail_tmp;
333         }
334
335         if (copy_to_user(buf, tmp, count)) {
336                 ret = -EFAULT;
337                 goto bail_tmp;
338         }
339
340         *ppos = pos + count;
341         ret = count;
342
343 bail_tmp:
344         kfree(tmp);
345
346 bail:
347         return ret;
348 }
349
350 static ssize_t flash_write(struct file *file, const char __user *buf,
351                            size_t count, loff_t *ppos)
352 {
353         struct ipath_devdata *dd;
354         ssize_t ret;
355         loff_t pos;
356         char *tmp;
357
358         pos = *ppos;
359
360         if ( pos < 0) {
361                 ret = -EINVAL;
362                 goto bail;
363         }
364
365         if (pos >= sizeof(struct ipath_flash)) {
366                 ret = 0;
367                 goto bail;
368         }
369
370         if (count > sizeof(struct ipath_flash) - pos)
371                 count = sizeof(struct ipath_flash) - pos;
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_dentry->d_inode->u.generic_ip;
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 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 void remove_file(struct dentry *parent, char *name)
459 {
460         struct dentry *tmp;
461
462         tmp = lookup_one_len(name, parent, strlen(name));
463
464         spin_lock(&dcache_lock);
465         spin_lock(&tmp->d_lock);
466         if (!(d_unhashed(tmp) && tmp->d_inode)) {
467                 dget_locked(tmp);
468                 __d_drop(tmp);
469                 spin_unlock(&tmp->d_lock);
470                 spin_unlock(&dcache_lock);
471                 simple_unlink(parent->d_inode, tmp);
472         } else {
473                 spin_unlock(&tmp->d_lock);
474                 spin_unlock(&dcache_lock);
475         }
476 }
477
478 static int remove_device_files(struct super_block *sb,
479                                struct ipath_devdata *dd)
480 {
481         struct dentry *dir, *root;
482         char unit[10];
483         int ret;
484
485         root = dget(sb->s_root);
486         mutex_lock(&root->d_inode->i_mutex);
487         snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
488         dir = lookup_one_len(unit, root, strlen(unit));
489
490         if (IS_ERR(dir)) {
491                 ret = PTR_ERR(dir);
492                 printk(KERN_ERR "Lookup of %s failed\n", unit);
493                 goto bail;
494         }
495
496         remove_file(dir, "flash");
497         remove_file(dir, "port_info");
498         remove_file(dir, "node_info");
499         remove_file(dir, "atomic_counters");
500         d_delete(dir);
501         ret = simple_rmdir(root->d_inode, dir);
502
503 bail:
504         mutex_unlock(&root->d_inode->i_mutex);
505         dput(root);
506         return ret;
507 }
508
509 static int ipathfs_fill_super(struct super_block *sb, void *data,
510                               int silent)
511 {
512         struct ipath_devdata *dd, *tmp;
513         unsigned long flags;
514         int ret;
515
516         static struct tree_descr files[] = {
517                 [1] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
518                 {""},
519         };
520
521         ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
522         if (ret) {
523                 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
524                 goto bail;
525         }
526
527         spin_lock_irqsave(&ipath_devs_lock, flags);
528
529         list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
530                 spin_unlock_irqrestore(&ipath_devs_lock, flags);
531                 ret = create_device_files(sb, dd);
532                 if (ret) {
533                         deactivate_super(sb);
534                         goto bail;
535                 }
536                 spin_lock_irqsave(&ipath_devs_lock, flags);
537         }
538
539         spin_unlock_irqrestore(&ipath_devs_lock, flags);
540
541 bail:
542         return ret;
543 }
544
545 static struct super_block *ipathfs_get_sb(struct file_system_type *fs_type,
546                                         int flags, const char *dev_name,
547                                         void *data)
548 {
549         ipath_super = get_sb_single(fs_type, flags, data,
550                                     ipathfs_fill_super);
551         return ipath_super;
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 }