2 * Copyright (c) 2012 Intel Corporation. All rights reserved.
3 * Copyright (c) 2006 - 2012 QLogic Corporation. All rights reserved.
4 * Copyright (c) 2006 PathScale, Inc. All rights reserved.
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #include <linux/module.h>
37 #include <linux/fs_context.h>
38 #include <linux/mount.h>
39 #include <linux/pagemap.h>
40 #include <linux/init.h>
41 #include <linux/namei.h>
45 #define QIBFS_MAGIC 0x726a77
47 static struct super_block
*qib_super
;
49 #define private2dd(file) (file_inode(file)->i_private)
51 static int qibfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
52 umode_t mode
, const struct file_operations
*fops
,
56 struct inode
*inode
= new_inode(dir
->i_sb
);
63 inode
->i_ino
= get_next_ino();
65 inode
->i_uid
= GLOBAL_ROOT_UID
;
66 inode
->i_gid
= GLOBAL_ROOT_GID
;
68 inode
->i_atime
= current_time(inode
);
69 inode
->i_mtime
= inode
->i_atime
;
70 inode
->i_ctime
= inode
->i_atime
;
71 inode
->i_private
= data
;
73 inode
->i_op
= &simple_dir_inode_operations
;
80 d_instantiate(dentry
, inode
);
87 static int create_file(const char *name
, umode_t mode
,
88 struct dentry
*parent
, struct dentry
**dentry
,
89 const struct file_operations
*fops
, void *data
)
93 inode_lock(d_inode(parent
));
94 *dentry
= lookup_one_len(name
, parent
, strlen(name
));
96 error
= qibfs_mknod(d_inode(parent
), *dentry
,
99 error
= PTR_ERR(*dentry
);
100 inode_unlock(d_inode(parent
));
105 static ssize_t
driver_stats_read(struct file
*file
, char __user
*buf
,
106 size_t count
, loff_t
*ppos
)
108 qib_stats
.sps_ints
= qib_sps_ints();
109 return simple_read_from_buffer(buf
, count
, ppos
, &qib_stats
,
114 * driver stats field names, one line per stat, single string. Used by
115 * programs like ipathstats to print the stats in a way which works for
116 * different versions of drivers, without changing program source.
117 * if qlogic_ib_stats changes, this needs to change. Names need to be
118 * 12 chars or less (w/o newline), for proper display by ipathstats utility.
120 static const char qib_statnames
[] =
133 static ssize_t
driver_names_read(struct file
*file
, char __user
*buf
,
134 size_t count
, loff_t
*ppos
)
136 return simple_read_from_buffer(buf
, count
, ppos
, qib_statnames
,
137 sizeof(qib_statnames
) - 1); /* no null */
140 static const struct file_operations driver_ops
[] = {
141 { .read
= driver_stats_read
, .llseek
= generic_file_llseek
, },
142 { .read
= driver_names_read
, .llseek
= generic_file_llseek
, },
145 /* read the per-device counters */
146 static ssize_t
dev_counters_read(struct file
*file
, char __user
*buf
,
147 size_t count
, loff_t
*ppos
)
151 struct qib_devdata
*dd
= private2dd(file
);
153 avail
= dd
->f_read_cntrs(dd
, *ppos
, NULL
, &counters
);
154 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
157 /* read the per-device counters */
158 static ssize_t
dev_names_read(struct file
*file
, char __user
*buf
,
159 size_t count
, loff_t
*ppos
)
163 struct qib_devdata
*dd
= private2dd(file
);
165 avail
= dd
->f_read_cntrs(dd
, *ppos
, &names
, NULL
);
166 return simple_read_from_buffer(buf
, count
, ppos
, names
, avail
);
169 static const struct file_operations cntr_ops
[] = {
170 { .read
= dev_counters_read
, .llseek
= generic_file_llseek
, },
171 { .read
= dev_names_read
, .llseek
= generic_file_llseek
, },
175 * Could use file_inode(file)->i_ino to figure out which file,
176 * instead of separate routine for each, but for now, this works...
179 /* read the per-port names (same for each port) */
180 static ssize_t
portnames_read(struct file
*file
, char __user
*buf
,
181 size_t count
, loff_t
*ppos
)
185 struct qib_devdata
*dd
= private2dd(file
);
187 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 0, &names
, NULL
);
188 return simple_read_from_buffer(buf
, count
, ppos
, names
, avail
);
191 /* read the per-port counters for port 1 (pidx 0) */
192 static ssize_t
portcntrs_1_read(struct file
*file
, char __user
*buf
,
193 size_t count
, loff_t
*ppos
)
197 struct qib_devdata
*dd
= private2dd(file
);
199 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 0, NULL
, &counters
);
200 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
203 /* read the per-port counters for port 2 (pidx 1) */
204 static ssize_t
portcntrs_2_read(struct file
*file
, char __user
*buf
,
205 size_t count
, loff_t
*ppos
)
209 struct qib_devdata
*dd
= private2dd(file
);
211 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 1, NULL
, &counters
);
212 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
215 static const struct file_operations portcntr_ops
[] = {
216 { .read
= portnames_read
, .llseek
= generic_file_llseek
, },
217 { .read
= portcntrs_1_read
, .llseek
= generic_file_llseek
, },
218 { .read
= portcntrs_2_read
, .llseek
= generic_file_llseek
, },
222 * read the per-port QSFP data for port 1 (pidx 0)
224 static ssize_t
qsfp_1_read(struct file
*file
, char __user
*buf
,
225 size_t count
, loff_t
*ppos
)
227 struct qib_devdata
*dd
= private2dd(file
);
231 tmp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
235 ret
= qib_qsfp_dump(dd
->pport
, tmp
, PAGE_SIZE
);
237 ret
= simple_read_from_buffer(buf
, count
, ppos
, tmp
, ret
);
243 * read the per-port QSFP data for port 2 (pidx 1)
245 static ssize_t
qsfp_2_read(struct file
*file
, char __user
*buf
,
246 size_t count
, loff_t
*ppos
)
248 struct qib_devdata
*dd
= private2dd(file
);
252 if (dd
->num_pports
< 2)
255 tmp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
259 ret
= qib_qsfp_dump(dd
->pport
+ 1, tmp
, PAGE_SIZE
);
261 ret
= simple_read_from_buffer(buf
, count
, ppos
, tmp
, ret
);
266 static const struct file_operations qsfp_ops
[] = {
267 { .read
= qsfp_1_read
, .llseek
= generic_file_llseek
, },
268 { .read
= qsfp_2_read
, .llseek
= generic_file_llseek
, },
271 static ssize_t
flash_read(struct file
*file
, char __user
*buf
,
272 size_t count
, loff_t
*ppos
)
274 struct qib_devdata
*dd
;
286 if (pos
>= sizeof(struct qib_flash
)) {
291 if (count
> sizeof(struct qib_flash
) - pos
)
292 count
= sizeof(struct qib_flash
) - pos
;
294 tmp
= kmalloc(count
, GFP_KERNEL
);
300 dd
= private2dd(file
);
301 if (qib_eeprom_read(dd
, pos
, tmp
, count
)) {
302 qib_dev_err(dd
, "failed to read from flash\n");
307 if (copy_to_user(buf
, tmp
, count
)) {
322 static ssize_t
flash_write(struct file
*file
, const char __user
*buf
,
323 size_t count
, loff_t
*ppos
)
325 struct qib_devdata
*dd
;
332 if (pos
!= 0 || count
!= sizeof(struct qib_flash
))
335 tmp
= memdup_user(buf
, count
);
339 dd
= private2dd(file
);
340 if (qib_eeprom_write(dd
, pos
, tmp
, count
)) {
342 qib_dev_err(dd
, "failed to write to flash\n");
354 static const struct file_operations flash_ops
= {
356 .write
= flash_write
,
357 .llseek
= default_llseek
,
360 static int add_cntr_files(struct super_block
*sb
, struct qib_devdata
*dd
)
362 struct dentry
*dir
, *tmp
;
366 /* create the per-unit directory */
367 snprintf(unit
, sizeof(unit
), "%u", dd
->unit
);
368 ret
= create_file(unit
, S_IFDIR
|S_IRUGO
|S_IXUGO
, sb
->s_root
, &dir
,
369 &simple_dir_operations
, dd
);
371 pr_err("create_file(%s) failed: %d\n", unit
, ret
);
375 /* create the files in the new directory */
376 ret
= create_file("counters", S_IFREG
|S_IRUGO
, dir
, &tmp
,
379 pr_err("create_file(%s/counters) failed: %d\n",
383 ret
= create_file("counter_names", S_IFREG
|S_IRUGO
, dir
, &tmp
,
386 pr_err("create_file(%s/counter_names) failed: %d\n",
390 ret
= create_file("portcounter_names", S_IFREG
|S_IRUGO
, dir
, &tmp
,
391 &portcntr_ops
[0], dd
);
393 pr_err("create_file(%s/%s) failed: %d\n",
394 unit
, "portcounter_names", ret
);
397 for (i
= 1; i
<= dd
->num_pports
; i
++) {
400 sprintf(fname
, "port%dcounters", i
);
401 /* create the files in the new directory */
402 ret
= create_file(fname
, S_IFREG
|S_IRUGO
, dir
, &tmp
,
403 &portcntr_ops
[i
], dd
);
405 pr_err("create_file(%s/%s) failed: %d\n",
409 if (!(dd
->flags
& QIB_HAS_QSFP
))
411 sprintf(fname
, "qsfp%d", i
);
412 ret
= create_file(fname
, S_IFREG
|S_IRUGO
, dir
, &tmp
,
413 &qsfp_ops
[i
- 1], dd
);
415 pr_err("create_file(%s/%s) failed: %d\n",
421 ret
= create_file("flash", S_IFREG
|S_IWUSR
|S_IRUGO
, dir
, &tmp
,
424 pr_err("create_file(%s/flash) failed: %d\n",
430 static int remove_file(struct dentry
*parent
, char *name
)
435 tmp
= lookup_one_len(name
, parent
, strlen(name
));
442 spin_lock(&tmp
->d_lock
);
443 if (simple_positive(tmp
)) {
445 spin_unlock(&tmp
->d_lock
);
446 simple_unlink(d_inode(parent
), tmp
);
448 spin_unlock(&tmp
->d_lock
);
455 * We don't expect clients to care about the return value, but
456 * it's there if they need it.
461 static int remove_device_files(struct super_block
*sb
,
462 struct qib_devdata
*dd
)
464 struct dentry
*dir
, *root
;
468 root
= dget(sb
->s_root
);
469 inode_lock(d_inode(root
));
470 snprintf(unit
, sizeof(unit
), "%u", dd
->unit
);
471 dir
= lookup_one_len(unit
, root
, strlen(unit
));
475 pr_err("Lookup of %s failed\n", unit
);
479 inode_lock(d_inode(dir
));
480 remove_file(dir
, "counters");
481 remove_file(dir
, "counter_names");
482 remove_file(dir
, "portcounter_names");
483 for (i
= 0; i
< dd
->num_pports
; i
++) {
486 sprintf(fname
, "port%dcounters", i
+ 1);
487 remove_file(dir
, fname
);
488 if (dd
->flags
& QIB_HAS_QSFP
) {
489 sprintf(fname
, "qsfp%d", i
+ 1);
490 remove_file(dir
, fname
);
493 remove_file(dir
, "flash");
494 inode_unlock(d_inode(dir
));
495 ret
= simple_rmdir(d_inode(root
), dir
);
500 inode_unlock(d_inode(root
));
506 * This fills everything in when the fs is mounted, to handle umount/mount
507 * after device init. The direct add_cntr_files() call handles adding
508 * them from the init code, when the fs is already mounted.
510 static int qibfs_fill_super(struct super_block
*sb
, struct fs_context
*fc
)
512 struct qib_devdata
*dd
;
516 static const struct tree_descr files
[] = {
517 [2] = {"driver_stats", &driver_ops
[0], S_IRUGO
},
518 [3] = {"driver_stats_names", &driver_ops
[1], S_IRUGO
},
522 ret
= simple_fill_super(sb
, QIBFS_MAGIC
, files
);
524 pr_err("simple_fill_super failed: %d\n", ret
);
528 xa_for_each(&qib_dev_table
, index
, dd
) {
529 ret
= add_cntr_files(sb
, dd
);
538 static int qibfs_get_tree(struct fs_context
*fc
)
540 int ret
= get_tree_single(fc
, qibfs_fill_super
);
542 qib_super
= fc
->root
->d_sb
;
546 static const struct fs_context_operations qibfs_context_ops
= {
547 .get_tree
= qibfs_get_tree
,
550 static int qibfs_init_fs_context(struct fs_context
*fc
)
552 fc
->ops
= &qibfs_context_ops
;
556 static void qibfs_kill_super(struct super_block
*s
)
558 kill_litter_super(s
);
562 int qibfs_add(struct qib_devdata
*dd
)
567 * On first unit initialized, qib_super will not yet exist
568 * because nobody has yet tried to mount the filesystem, so
569 * we can't consider that to be an error; if an error occurs
570 * during the mount, that will get a complaint, so this is OK.
571 * add_cntr_files() for all units is done at mount from
572 * qibfs_fill_super(), so one way or another, everything works.
574 if (qib_super
== NULL
)
577 ret
= add_cntr_files(qib_super
, dd
);
581 int qibfs_remove(struct qib_devdata
*dd
)
586 ret
= remove_device_files(qib_super
, dd
);
591 static struct file_system_type qibfs_fs_type
= {
592 .owner
= THIS_MODULE
,
594 .init_fs_context
= qibfs_init_fs_context
,
595 .kill_sb
= qibfs_kill_super
,
597 MODULE_ALIAS_FS("ipathfs");
599 int __init
qib_init_qibfs(void)
601 return register_filesystem(&qibfs_fs_type
);
604 int __exit
qib_exit_qibfs(void)
606 return unregister_filesystem(&qibfs_fs_type
);