2 * Copyright (c) 2006, 2007, 2008, 2009 QLogic Corporation. All rights reserved.
3 * Copyright (c) 2006 PathScale, Inc. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/module.h>
36 #include <linux/mount.h>
37 #include <linux/pagemap.h>
38 #include <linux/init.h>
39 #include <linux/namei.h>
43 #define QIBFS_MAGIC 0x726a77
45 static struct super_block
*qib_super
;
47 #define private2dd(file) ((file)->f_dentry->d_inode->i_private)
49 static int qibfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
50 int mode
, const struct file_operations
*fops
,
54 struct inode
*inode
= new_inode(dir
->i_sb
);
65 inode
->i_atime
= CURRENT_TIME
;
66 inode
->i_mtime
= inode
->i_atime
;
67 inode
->i_ctime
= inode
->i_atime
;
68 inode
->i_private
= data
;
69 if ((mode
& S_IFMT
) == S_IFDIR
) {
70 inode
->i_op
= &simple_dir_inode_operations
;
77 d_instantiate(dentry
, inode
);
84 static int create_file(const char *name
, mode_t mode
,
85 struct dentry
*parent
, struct dentry
**dentry
,
86 const struct file_operations
*fops
, void *data
)
91 mutex_lock(&parent
->d_inode
->i_mutex
);
92 *dentry
= lookup_one_len(name
, parent
, strlen(name
));
94 error
= qibfs_mknod(parent
->d_inode
, *dentry
,
97 error
= PTR_ERR(*dentry
);
98 mutex_unlock(&parent
->d_inode
->i_mutex
);
103 static ssize_t
driver_stats_read(struct file
*file
, char __user
*buf
,
104 size_t count
, loff_t
*ppos
)
106 return simple_read_from_buffer(buf
, count
, ppos
, &qib_stats
,
111 * driver stats field names, one line per stat, single string. Used by
112 * programs like ipathstats to print the stats in a way which works for
113 * different versions of drivers, without changing program source.
114 * if qlogic_ib_stats changes, this needs to change. Names need to be
115 * 12 chars or less (w/o newline), for proper display by ipathstats utility.
117 static const char qib_statnames
[] =
130 static ssize_t
driver_names_read(struct file
*file
, char __user
*buf
,
131 size_t count
, loff_t
*ppos
)
133 return simple_read_from_buffer(buf
, count
, ppos
, qib_statnames
,
134 sizeof qib_statnames
- 1); /* no null */
137 static const struct file_operations driver_ops
[] = {
138 { .read
= driver_stats_read
, .llseek
= generic_file_llseek
, },
139 { .read
= driver_names_read
, .llseek
= generic_file_llseek
, },
142 /* read the per-device counters */
143 static ssize_t
dev_counters_read(struct file
*file
, char __user
*buf
,
144 size_t count
, loff_t
*ppos
)
148 struct qib_devdata
*dd
= private2dd(file
);
150 avail
= dd
->f_read_cntrs(dd
, *ppos
, NULL
, &counters
);
151 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
154 /* read the per-device counters */
155 static ssize_t
dev_names_read(struct file
*file
, char __user
*buf
,
156 size_t count
, loff_t
*ppos
)
160 struct qib_devdata
*dd
= private2dd(file
);
162 avail
= dd
->f_read_cntrs(dd
, *ppos
, &names
, NULL
);
163 return simple_read_from_buffer(buf
, count
, ppos
, names
, avail
);
166 static const struct file_operations cntr_ops
[] = {
167 { .read
= dev_counters_read
, .llseek
= generic_file_llseek
, },
168 { .read
= dev_names_read
, .llseek
= generic_file_llseek
, },
172 * Could use file->f_dentry->d_inode->i_ino to figure out which file,
173 * instead of separate routine for each, but for now, this works...
176 /* read the per-port names (same for each port) */
177 static ssize_t
portnames_read(struct file
*file
, char __user
*buf
,
178 size_t count
, loff_t
*ppos
)
182 struct qib_devdata
*dd
= private2dd(file
);
184 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 0, &names
, NULL
);
185 return simple_read_from_buffer(buf
, count
, ppos
, names
, avail
);
188 /* read the per-port counters for port 1 (pidx 0) */
189 static ssize_t
portcntrs_1_read(struct file
*file
, char __user
*buf
,
190 size_t count
, loff_t
*ppos
)
194 struct qib_devdata
*dd
= private2dd(file
);
196 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 0, NULL
, &counters
);
197 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
200 /* read the per-port counters for port 2 (pidx 1) */
201 static ssize_t
portcntrs_2_read(struct file
*file
, char __user
*buf
,
202 size_t count
, loff_t
*ppos
)
206 struct qib_devdata
*dd
= private2dd(file
);
208 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 1, NULL
, &counters
);
209 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
212 static const struct file_operations portcntr_ops
[] = {
213 { .read
= portnames_read
, .llseek
= generic_file_llseek
, },
214 { .read
= portcntrs_1_read
, .llseek
= generic_file_llseek
, },
215 { .read
= portcntrs_2_read
, .llseek
= generic_file_llseek
, },
219 * read the per-port QSFP data for port 1 (pidx 0)
221 static ssize_t
qsfp_1_read(struct file
*file
, char __user
*buf
,
222 size_t count
, loff_t
*ppos
)
224 struct qib_devdata
*dd
= private2dd(file
);
228 tmp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
232 ret
= qib_qsfp_dump(dd
->pport
, tmp
, PAGE_SIZE
);
234 ret
= simple_read_from_buffer(buf
, count
, ppos
, tmp
, ret
);
240 * read the per-port QSFP data for port 2 (pidx 1)
242 static ssize_t
qsfp_2_read(struct file
*file
, char __user
*buf
,
243 size_t count
, loff_t
*ppos
)
245 struct qib_devdata
*dd
= private2dd(file
);
249 if (dd
->num_pports
< 2)
252 tmp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
256 ret
= qib_qsfp_dump(dd
->pport
+ 1, tmp
, PAGE_SIZE
);
258 ret
= simple_read_from_buffer(buf
, count
, ppos
, tmp
, ret
);
263 static const struct file_operations qsfp_ops
[] = {
264 { .read
= qsfp_1_read
, .llseek
= generic_file_llseek
, },
265 { .read
= qsfp_2_read
, .llseek
= generic_file_llseek
, },
268 static ssize_t
flash_read(struct file
*file
, char __user
*buf
,
269 size_t count
, loff_t
*ppos
)
271 struct qib_devdata
*dd
;
283 if (pos
>= sizeof(struct qib_flash
)) {
288 if (count
> sizeof(struct qib_flash
) - pos
)
289 count
= sizeof(struct qib_flash
) - pos
;
291 tmp
= kmalloc(count
, GFP_KERNEL
);
297 dd
= private2dd(file
);
298 if (qib_eeprom_read(dd
, pos
, tmp
, count
)) {
299 qib_dev_err(dd
, "failed to read from flash\n");
304 if (copy_to_user(buf
, tmp
, count
)) {
319 static ssize_t
flash_write(struct file
*file
, const char __user
*buf
,
320 size_t count
, loff_t
*ppos
)
322 struct qib_devdata
*dd
;
334 if (count
!= sizeof(struct qib_flash
)) {
339 tmp
= kmalloc(count
, GFP_KERNEL
);
345 if (copy_from_user(tmp
, buf
, count
)) {
350 dd
= private2dd(file
);
351 if (qib_eeprom_write(dd
, pos
, tmp
, count
)) {
353 qib_dev_err(dd
, "failed to write to flash\n");
367 static const struct file_operations flash_ops
= {
369 .write
= flash_write
,
370 .llseek
= default_llseek
,
373 static int add_cntr_files(struct super_block
*sb
, struct qib_devdata
*dd
)
375 struct dentry
*dir
, *tmp
;
379 /* create the per-unit directory */
380 snprintf(unit
, sizeof unit
, "%u", dd
->unit
);
381 ret
= create_file(unit
, S_IFDIR
|S_IRUGO
|S_IXUGO
, sb
->s_root
, &dir
,
382 &simple_dir_operations
, dd
);
384 printk(KERN_ERR
"create_file(%s) failed: %d\n", unit
, ret
);
388 /* create the files in the new directory */
389 ret
= create_file("counters", S_IFREG
|S_IRUGO
, dir
, &tmp
,
392 printk(KERN_ERR
"create_file(%s/counters) failed: %d\n",
396 ret
= create_file("counter_names", S_IFREG
|S_IRUGO
, dir
, &tmp
,
399 printk(KERN_ERR
"create_file(%s/counter_names) failed: %d\n",
403 ret
= create_file("portcounter_names", S_IFREG
|S_IRUGO
, dir
, &tmp
,
404 &portcntr_ops
[0], dd
);
406 printk(KERN_ERR
"create_file(%s/%s) failed: %d\n",
407 unit
, "portcounter_names", ret
);
410 for (i
= 1; i
<= dd
->num_pports
; i
++) {
413 sprintf(fname
, "port%dcounters", i
);
414 /* create the files in the new directory */
415 ret
= create_file(fname
, S_IFREG
|S_IRUGO
, dir
, &tmp
,
416 &portcntr_ops
[i
], dd
);
418 printk(KERN_ERR
"create_file(%s/%s) failed: %d\n",
422 if (!(dd
->flags
& QIB_HAS_QSFP
))
424 sprintf(fname
, "qsfp%d", i
);
425 ret
= create_file(fname
, S_IFREG
|S_IRUGO
, dir
, &tmp
,
426 &qsfp_ops
[i
- 1], dd
);
428 printk(KERN_ERR
"create_file(%s/%s) failed: %d\n",
434 ret
= create_file("flash", S_IFREG
|S_IWUSR
|S_IRUGO
, dir
, &tmp
,
437 printk(KERN_ERR
"create_file(%s/flash) failed: %d\n",
443 static int remove_file(struct dentry
*parent
, char *name
)
448 tmp
= lookup_one_len(name
, parent
, strlen(name
));
455 spin_lock(&dcache_lock
);
456 spin_lock(&tmp
->d_lock
);
457 if (!(d_unhashed(tmp
) && tmp
->d_inode
)) {
460 spin_unlock(&tmp
->d_lock
);
461 spin_unlock(&dcache_lock
);
462 simple_unlink(parent
->d_inode
, tmp
);
464 spin_unlock(&tmp
->d_lock
);
465 spin_unlock(&dcache_lock
);
471 * We don't expect clients to care about the return value, but
472 * it's there if they need it.
477 static int remove_device_files(struct super_block
*sb
,
478 struct qib_devdata
*dd
)
480 struct dentry
*dir
, *root
;
484 root
= dget(sb
->s_root
);
485 mutex_lock(&root
->d_inode
->i_mutex
);
486 snprintf(unit
, sizeof unit
, "%u", dd
->unit
);
487 dir
= lookup_one_len(unit
, root
, strlen(unit
));
491 printk(KERN_ERR
"Lookup of %s failed\n", unit
);
495 remove_file(dir
, "counters");
496 remove_file(dir
, "counter_names");
497 remove_file(dir
, "portcounter_names");
498 for (i
= 0; i
< dd
->num_pports
; i
++) {
501 sprintf(fname
, "port%dcounters", i
+ 1);
502 remove_file(dir
, fname
);
503 if (dd
->flags
& QIB_HAS_QSFP
) {
504 sprintf(fname
, "qsfp%d", i
+ 1);
505 remove_file(dir
, fname
);
508 remove_file(dir
, "flash");
510 ret
= simple_rmdir(root
->d_inode
, dir
);
513 mutex_unlock(&root
->d_inode
->i_mutex
);
519 * This fills everything in when the fs is mounted, to handle umount/mount
520 * after device init. The direct add_cntr_files() call handles adding
521 * them from the init code, when the fs is already mounted.
523 static int qibfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
525 struct qib_devdata
*dd
, *tmp
;
529 static struct tree_descr files
[] = {
530 [2] = {"driver_stats", &driver_ops
[0], S_IRUGO
},
531 [3] = {"driver_stats_names", &driver_ops
[1], S_IRUGO
},
535 ret
= simple_fill_super(sb
, QIBFS_MAGIC
, files
);
537 printk(KERN_ERR
"simple_fill_super failed: %d\n", ret
);
541 spin_lock_irqsave(&qib_devs_lock
, flags
);
543 list_for_each_entry_safe(dd
, tmp
, &qib_dev_list
, list
) {
544 spin_unlock_irqrestore(&qib_devs_lock
, flags
);
545 ret
= add_cntr_files(sb
, dd
);
548 spin_lock_irqsave(&qib_devs_lock
, flags
);
551 spin_unlock_irqrestore(&qib_devs_lock
, flags
);
557 static int qibfs_get_sb(struct file_system_type
*fs_type
, int flags
,
558 const char *dev_name
, void *data
, struct vfsmount
*mnt
)
560 int ret
= get_sb_single(fs_type
, flags
, data
,
561 qibfs_fill_super
, mnt
);
563 qib_super
= mnt
->mnt_sb
;
567 static void qibfs_kill_super(struct super_block
*s
)
569 kill_litter_super(s
);
573 int qibfs_add(struct qib_devdata
*dd
)
578 * On first unit initialized, qib_super will not yet exist
579 * because nobody has yet tried to mount the filesystem, so
580 * we can't consider that to be an error; if an error occurs
581 * during the mount, that will get a complaint, so this is OK.
582 * add_cntr_files() for all units is done at mount from
583 * qibfs_fill_super(), so one way or another, everything works.
585 if (qib_super
== NULL
)
588 ret
= add_cntr_files(qib_super
, dd
);
592 int qibfs_remove(struct qib_devdata
*dd
)
597 ret
= remove_device_files(qib_super
, dd
);
602 static struct file_system_type qibfs_fs_type
= {
603 .owner
= THIS_MODULE
,
605 .get_sb
= qibfs_get_sb
,
606 .kill_sb
= qibfs_kill_super
,
609 int __init
qib_init_qibfs(void)
611 return register_filesystem(&qibfs_fs_type
);
614 int __exit
qib_exit_qibfs(void)
616 return unregister_filesystem(&qibfs_fs_type
);