inode numbering: change libfs sb creation routines to avoid collisions with their...
[linux-2.6-block.git] / drivers / infiniband / hw / ipath / ipath_fs.c
CommitLineData
3e9b4a5e 1/*
759d5768 2 * Copyright (c) 2006 QLogic, Inc. All rights reserved.
3e9b4a5e
BS
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>
3e9b4a5e
BS
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>
3e9b4a5e
BS
41
42#include "ipath_kernel.h"
43
44#define IPATHFS_MAGIC 0x726a77
45
46static struct super_block *ipath_super;
47
48static int ipathfs_mknod(struct inode *dir, struct dentry *dentry,
2b8693c0 49 int mode, const struct file_operations *fops,
3e9b4a5e
BS
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;
3e9b4a5e
BS
63 inode->i_blocks = 0;
64 inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
8e18e294 65 inode->i_private = data;
3e9b4a5e
BS
66 if ((mode & S_IFMT) == S_IFDIR) {
67 inode->i_op = &simple_dir_inode_operations;
d8c76e6f
DH
68 inc_nlink(inode);
69 inc_nlink(dir);
3e9b4a5e
BS
70 }
71
72 inode->i_fop = fops;
73
74 d_instantiate(dentry, inode);
75 error = 0;
76
77bail:
78 return error;
79}
80
81static int create_file(const char *name, mode_t mode,
82 struct dentry *parent, struct dentry **dentry,
2b8693c0 83 const struct file_operations *fops, void *data)
3e9b4a5e
BS
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
100static 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
2b8693c0 107static const struct file_operations atomic_stats_ops = {
3e9b4a5e
BS
108 .read = atomic_stats_read,
109};
110
111#define NUM_COUNTERS sizeof(struct infinipath_counters) / sizeof(u64)
112
113static 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
1cfd6e64 120 dd = file->f_path.dentry->d_inode->i_private;
3e9b4a5e
BS
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
2b8693c0 129static const struct file_operations atomic_counters_ops = {
3e9b4a5e
BS
130 .read = atomic_counters_read,
131};
132
133static 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
1cfd6e64 140 dd = file->f_path.dentry->d_inode->i_private;
3e9b4a5e
BS
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
2b8693c0 168static const struct file_operations atomic_node_info_ops = {
3e9b4a5e
BS
169 .read = atomic_node_info_read,
170};
171
172static 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
1cfd6e64 179 dd = file->f_path.dentry->d_inode->i_private;
3e9b4a5e
BS
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 /*
0fd41363
BS
192 * Notimpl yet SMLID.
193 * CapabilityMask is 0, we don't support any of these
3e9b4a5e
BS
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 portinfo[10] = /* VLArbitrationLowCap is 0 (only one VL) */
261 /* InitTypeReply is SMA decision */
262 (5 << 16) /* MTUCap 4096 */
263 | (7 << 13) /* VLStallCount */
264 | (0x1f << 8) /* HOQLife */
265 | (1 << 4)
266
267 /* OperationalVLs 0 */
268 /* PartitionEnforcementInbound */
269 /* PartitionEnforcementOutbound not enforced */
270 /* FilterRawinbound not enforced */
271 ; /* FilterRawOutbound not enforced */
272 /* M_KeyViolations are not counted by hardware, SMA can count */
273 tmp = ipath_read_creg32(dd, dd->ipath_cregs->cr_errpkey);
274 /* P_KeyViolations are counted by hardware. */
275 portinfo[11] = ((tmp & 0xffff) << 0);
276 portinfo[12] =
277 /* Q_KeyViolations are not counted by hardware */
278 (1 << 8)
279
280 /* GUIDCap */
281 /* SubnetTimeOut handled by SMA */
282 /* RespTimeValue handled by SMA */
283 ;
284 /* LocalPhyErrors are programmed to max */
285 portinfo[12] |= (0xf << 20)
286 | (0xf << 16) /* OverRunErrors are programmed to max */
287 ;
288
289 return simple_read_from_buffer(buf, count, ppos, portinfo,
290 sizeof portinfo);
291}
292
2b8693c0 293static const struct file_operations atomic_port_info_ops = {
3e9b4a5e
BS
294 .read = atomic_port_info_read,
295};
296
297static ssize_t flash_read(struct file *file, char __user *buf,
298 size_t count, loff_t *ppos)
299{
300 struct ipath_devdata *dd;
301 ssize_t ret;
302 loff_t pos;
303 char *tmp;
304
305 pos = *ppos;
306
307 if ( pos < 0) {
308 ret = -EINVAL;
309 goto bail;
310 }
311
312 if (pos >= sizeof(struct ipath_flash)) {
313 ret = 0;
314 goto bail;
315 }
316
317 if (count > sizeof(struct ipath_flash) - pos)
318 count = sizeof(struct ipath_flash) - pos;
319
320 tmp = kmalloc(count, GFP_KERNEL);
321 if (!tmp) {
322 ret = -ENOMEM;
323 goto bail;
324 }
325
1cfd6e64 326 dd = file->f_path.dentry->d_inode->i_private;
3e9b4a5e
BS
327 if (ipath_eeprom_read(dd, pos, tmp, count)) {
328 ipath_dev_err(dd, "failed to read from flash\n");
329 ret = -ENXIO;
330 goto bail_tmp;
331 }
332
333 if (copy_to_user(buf, tmp, count)) {
334 ret = -EFAULT;
335 goto bail_tmp;
336 }
337
338 *ppos = pos + count;
339 ret = count;
340
341bail_tmp:
342 kfree(tmp);
343
344bail:
345 return ret;
346}
347
348static ssize_t flash_write(struct file *file, const char __user *buf,
349 size_t count, loff_t *ppos)
350{
351 struct ipath_devdata *dd;
352 ssize_t ret;
353 loff_t pos;
354 char *tmp;
355
356 pos = *ppos;
357
7dae5bff 358 if (pos != 0) {
3e9b4a5e
BS
359 ret = -EINVAL;
360 goto bail;
361 }
362
7dae5bff
BS
363 if (count != sizeof(struct ipath_flash)) {
364 ret = -EINVAL;
3e9b4a5e
BS
365 goto bail;
366 }
367
3e9b4a5e
BS
368 tmp = kmalloc(count, GFP_KERNEL);
369 if (!tmp) {
370 ret = -ENOMEM;
371 goto bail;
372 }
373
374 if (copy_from_user(tmp, buf, count)) {
375 ret = -EFAULT;
376 goto bail_tmp;
377 }
378
1cfd6e64 379 dd = file->f_path.dentry->d_inode->i_private;
3e9b4a5e
BS
380 if (ipath_eeprom_write(dd, pos, tmp, count)) {
381 ret = -ENXIO;
382 ipath_dev_err(dd, "failed to write to flash\n");
383 goto bail_tmp;
384 }
385
386 *ppos = pos + count;
387 ret = count;
388
389bail_tmp:
390 kfree(tmp);
391
392bail:
393 return ret;
394}
395
2b8693c0 396static const struct file_operations flash_ops = {
3e9b4a5e
BS
397 .read = flash_read,
398 .write = flash_write,
399};
400
401static int create_device_files(struct super_block *sb,
402 struct ipath_devdata *dd)
403{
404 struct dentry *dir, *tmp;
405 char unit[10];
406 int ret;
407
408 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
409 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
410 (struct file_operations *) &simple_dir_operations,
411 dd);
412 if (ret) {
413 printk(KERN_ERR "create_file(%s) failed: %d\n", unit, ret);
414 goto bail;
415 }
416
417 ret = create_file("atomic_counters", S_IFREG|S_IRUGO, dir, &tmp,
418 &atomic_counters_ops, dd);
419 if (ret) {
420 printk(KERN_ERR "create_file(%s/atomic_counters) "
421 "failed: %d\n", unit, ret);
422 goto bail;
423 }
424
425 ret = create_file("node_info", S_IFREG|S_IRUGO, dir, &tmp,
426 &atomic_node_info_ops, dd);
427 if (ret) {
428 printk(KERN_ERR "create_file(%s/node_info) "
429 "failed: %d\n", unit, ret);
430 goto bail;
431 }
432
433 ret = create_file("port_info", S_IFREG|S_IRUGO, dir, &tmp,
434 &atomic_port_info_ops, dd);
435 if (ret) {
436 printk(KERN_ERR "create_file(%s/port_info) "
437 "failed: %d\n", unit, ret);
438 goto bail;
439 }
440
441 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
442 &flash_ops, dd);
443 if (ret) {
444 printk(KERN_ERR "create_file(%s/flash) "
445 "failed: %d\n", unit, ret);
446 goto bail;
447 }
448
449bail:
450 return ret;
451}
452
fae8773b 453static int remove_file(struct dentry *parent, char *name)
3e9b4a5e
BS
454{
455 struct dentry *tmp;
fae8773b 456 int ret;
3e9b4a5e
BS
457
458 tmp = lookup_one_len(name, parent, strlen(name));
459
fae8773b
BS
460 if (IS_ERR(tmp)) {
461 ret = PTR_ERR(tmp);
462 goto bail;
463 }
464
3e9b4a5e
BS
465 spin_lock(&dcache_lock);
466 spin_lock(&tmp->d_lock);
467 if (!(d_unhashed(tmp) && tmp->d_inode)) {
468 dget_locked(tmp);
469 __d_drop(tmp);
470 spin_unlock(&tmp->d_lock);
471 spin_unlock(&dcache_lock);
472 simple_unlink(parent->d_inode, tmp);
473 } else {
474 spin_unlock(&tmp->d_lock);
475 spin_unlock(&dcache_lock);
476 }
fae8773b
BS
477
478 ret = 0;
479bail:
480 /*
481 * We don't expect clients to care about the return value, but
482 * it's there if they need it.
483 */
484 return ret;
3e9b4a5e
BS
485}
486
487static int remove_device_files(struct super_block *sb,
488 struct ipath_devdata *dd)
489{
490 struct dentry *dir, *root;
491 char unit[10];
492 int ret;
493
494 root = dget(sb->s_root);
495 mutex_lock(&root->d_inode->i_mutex);
496 snprintf(unit, sizeof unit, "%02d", dd->ipath_unit);
497 dir = lookup_one_len(unit, root, strlen(unit));
498
499 if (IS_ERR(dir)) {
500 ret = PTR_ERR(dir);
501 printk(KERN_ERR "Lookup of %s failed\n", unit);
502 goto bail;
503 }
504
505 remove_file(dir, "flash");
506 remove_file(dir, "port_info");
507 remove_file(dir, "node_info");
508 remove_file(dir, "atomic_counters");
509 d_delete(dir);
510 ret = simple_rmdir(root->d_inode, dir);
511
512bail:
513 mutex_unlock(&root->d_inode->i_mutex);
514 dput(root);
515 return ret;
516}
517
518static int ipathfs_fill_super(struct super_block *sb, void *data,
519 int silent)
520{
521 struct ipath_devdata *dd, *tmp;
522 unsigned long flags;
523 int ret;
524
525 static struct tree_descr files[] = {
1a1c9bb4 526 [2] = {"atomic_stats", &atomic_stats_ops, S_IRUGO},
3e9b4a5e
BS
527 {""},
528 };
529
530 ret = simple_fill_super(sb, IPATHFS_MAGIC, files);
531 if (ret) {
532 printk(KERN_ERR "simple_fill_super failed: %d\n", ret);
533 goto bail;
534 }
535
536 spin_lock_irqsave(&ipath_devs_lock, flags);
537
538 list_for_each_entry_safe(dd, tmp, &ipath_dev_list, ipath_list) {
539 spin_unlock_irqrestore(&ipath_devs_lock, flags);
540 ret = create_device_files(sb, dd);
541 if (ret) {
542 deactivate_super(sb);
543 goto bail;
544 }
545 spin_lock_irqsave(&ipath_devs_lock, flags);
546 }
547
548 spin_unlock_irqrestore(&ipath_devs_lock, flags);
549
550bail:
551 return ret;
552}
553
454e2398
DH
554static int ipathfs_get_sb(struct file_system_type *fs_type, int flags,
555 const char *dev_name, void *data, struct vfsmount *mnt)
3e9b4a5e 556{
454e2398
DH
557 int ret = get_sb_single(fs_type, flags, data,
558 ipathfs_fill_super, mnt);
559 if (ret >= 0)
560 ipath_super = mnt->mnt_sb;
561 return ret;
3e9b4a5e
BS
562}
563
564static void ipathfs_kill_super(struct super_block *s)
565{
566 kill_litter_super(s);
567 ipath_super = NULL;
568}
569
570int ipathfs_add_device(struct ipath_devdata *dd)
571{
572 int ret;
573
574 if (ipath_super == NULL) {
575 ret = 0;
576 goto bail;
577 }
578
579 ret = create_device_files(ipath_super, dd);
580
581bail:
582 return ret;
583}
584
585int ipathfs_remove_device(struct ipath_devdata *dd)
586{
587 int ret;
588
589 if (ipath_super == NULL) {
590 ret = 0;
591 goto bail;
592 }
593
594 ret = remove_device_files(ipath_super, dd);
595
596bail:
597 return ret;
598}
599
600static struct file_system_type ipathfs_fs_type = {
601 .owner = THIS_MODULE,
602 .name = "ipathfs",
603 .get_sb = ipathfs_get_sb,
604 .kill_sb = ipathfs_kill_super,
605};
606
607int __init ipath_init_ipathfs(void)
608{
609 return register_filesystem(&ipathfs_fs_type);
610}
611
612void __exit ipath_exit_ipathfs(void)
613{
614 unregister_filesystem(&ipathfs_fs_type);
615}