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
);
61 inode
->i_ino
= get_next_ino();
66 inode
->i_atime
= CURRENT_TIME
;
67 inode
->i_mtime
= inode
->i_atime
;
68 inode
->i_ctime
= inode
->i_atime
;
69 inode
->i_private
= data
;
70 if ((mode
& S_IFMT
) == S_IFDIR
) {
71 inode
->i_op
= &simple_dir_inode_operations
;
78 d_instantiate(dentry
, inode
);
85 static int create_file(const char *name
, mode_t mode
,
86 struct dentry
*parent
, struct dentry
**dentry
,
87 const struct file_operations
*fops
, void *data
)
92 mutex_lock(&parent
->d_inode
->i_mutex
);
93 *dentry
= lookup_one_len(name
, parent
, strlen(name
));
95 error
= qibfs_mknod(parent
->d_inode
, *dentry
,
98 error
= PTR_ERR(*dentry
);
99 mutex_unlock(&parent
->d_inode
->i_mutex
);
104 static ssize_t
driver_stats_read(struct file
*file
, char __user
*buf
,
105 size_t count
, loff_t
*ppos
)
107 return simple_read_from_buffer(buf
, count
, ppos
, &qib_stats
,
112 * driver stats field names, one line per stat, single string. Used by
113 * programs like ipathstats to print the stats in a way which works for
114 * different versions of drivers, without changing program source.
115 * if qlogic_ib_stats changes, this needs to change. Names need to be
116 * 12 chars or less (w/o newline), for proper display by ipathstats utility.
118 static const char qib_statnames
[] =
131 static ssize_t
driver_names_read(struct file
*file
, char __user
*buf
,
132 size_t count
, loff_t
*ppos
)
134 return simple_read_from_buffer(buf
, count
, ppos
, qib_statnames
,
135 sizeof qib_statnames
- 1); /* no null */
138 static const struct file_operations driver_ops
[] = {
139 { .read
= driver_stats_read
, .llseek
= generic_file_llseek
, },
140 { .read
= driver_names_read
, .llseek
= generic_file_llseek
, },
143 /* read the per-device counters */
144 static ssize_t
dev_counters_read(struct file
*file
, char __user
*buf
,
145 size_t count
, loff_t
*ppos
)
149 struct qib_devdata
*dd
= private2dd(file
);
151 avail
= dd
->f_read_cntrs(dd
, *ppos
, NULL
, &counters
);
152 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
155 /* read the per-device counters */
156 static ssize_t
dev_names_read(struct file
*file
, char __user
*buf
,
157 size_t count
, loff_t
*ppos
)
161 struct qib_devdata
*dd
= private2dd(file
);
163 avail
= dd
->f_read_cntrs(dd
, *ppos
, &names
, NULL
);
164 return simple_read_from_buffer(buf
, count
, ppos
, names
, avail
);
167 static const struct file_operations cntr_ops
[] = {
168 { .read
= dev_counters_read
, .llseek
= generic_file_llseek
, },
169 { .read
= dev_names_read
, .llseek
= generic_file_llseek
, },
173 * Could use file->f_dentry->d_inode->i_ino to figure out which file,
174 * instead of separate routine for each, but for now, this works...
177 /* read the per-port names (same for each port) */
178 static ssize_t
portnames_read(struct file
*file
, char __user
*buf
,
179 size_t count
, loff_t
*ppos
)
183 struct qib_devdata
*dd
= private2dd(file
);
185 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 0, &names
, NULL
);
186 return simple_read_from_buffer(buf
, count
, ppos
, names
, avail
);
189 /* read the per-port counters for port 1 (pidx 0) */
190 static ssize_t
portcntrs_1_read(struct file
*file
, char __user
*buf
,
191 size_t count
, loff_t
*ppos
)
195 struct qib_devdata
*dd
= private2dd(file
);
197 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 0, NULL
, &counters
);
198 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
201 /* read the per-port counters for port 2 (pidx 1) */
202 static ssize_t
portcntrs_2_read(struct file
*file
, char __user
*buf
,
203 size_t count
, loff_t
*ppos
)
207 struct qib_devdata
*dd
= private2dd(file
);
209 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 1, NULL
, &counters
);
210 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
213 static const struct file_operations portcntr_ops
[] = {
214 { .read
= portnames_read
, .llseek
= generic_file_llseek
, },
215 { .read
= portcntrs_1_read
, .llseek
= generic_file_llseek
, },
216 { .read
= portcntrs_2_read
, .llseek
= generic_file_llseek
, },
220 * read the per-port QSFP data for port 1 (pidx 0)
222 static ssize_t
qsfp_1_read(struct file
*file
, char __user
*buf
,
223 size_t count
, loff_t
*ppos
)
225 struct qib_devdata
*dd
= private2dd(file
);
229 tmp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
233 ret
= qib_qsfp_dump(dd
->pport
, tmp
, PAGE_SIZE
);
235 ret
= simple_read_from_buffer(buf
, count
, ppos
, tmp
, ret
);
241 * read the per-port QSFP data for port 2 (pidx 1)
243 static ssize_t
qsfp_2_read(struct file
*file
, char __user
*buf
,
244 size_t count
, loff_t
*ppos
)
246 struct qib_devdata
*dd
= private2dd(file
);
250 if (dd
->num_pports
< 2)
253 tmp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
257 ret
= qib_qsfp_dump(dd
->pport
+ 1, tmp
, PAGE_SIZE
);
259 ret
= simple_read_from_buffer(buf
, count
, ppos
, tmp
, ret
);
264 static const struct file_operations qsfp_ops
[] = {
265 { .read
= qsfp_1_read
, .llseek
= generic_file_llseek
, },
266 { .read
= qsfp_2_read
, .llseek
= generic_file_llseek
, },
269 static ssize_t
flash_read(struct file
*file
, char __user
*buf
,
270 size_t count
, loff_t
*ppos
)
272 struct qib_devdata
*dd
;
284 if (pos
>= sizeof(struct qib_flash
)) {
289 if (count
> sizeof(struct qib_flash
) - pos
)
290 count
= sizeof(struct qib_flash
) - pos
;
292 tmp
= kmalloc(count
, GFP_KERNEL
);
298 dd
= private2dd(file
);
299 if (qib_eeprom_read(dd
, pos
, tmp
, count
)) {
300 qib_dev_err(dd
, "failed to read from flash\n");
305 if (copy_to_user(buf
, tmp
, count
)) {
320 static ssize_t
flash_write(struct file
*file
, const char __user
*buf
,
321 size_t count
, loff_t
*ppos
)
323 struct qib_devdata
*dd
;
335 if (count
!= sizeof(struct qib_flash
)) {
340 tmp
= kmalloc(count
, GFP_KERNEL
);
346 if (copy_from_user(tmp
, buf
, count
)) {
351 dd
= private2dd(file
);
352 if (qib_eeprom_write(dd
, pos
, tmp
, count
)) {
354 qib_dev_err(dd
, "failed to write to flash\n");
368 static const struct file_operations flash_ops
= {
370 .write
= flash_write
,
371 .llseek
= default_llseek
,
374 static int add_cntr_files(struct super_block
*sb
, struct qib_devdata
*dd
)
376 struct dentry
*dir
, *tmp
;
380 /* create the per-unit directory */
381 snprintf(unit
, sizeof unit
, "%u", dd
->unit
);
382 ret
= create_file(unit
, S_IFDIR
|S_IRUGO
|S_IXUGO
, sb
->s_root
, &dir
,
383 &simple_dir_operations
, dd
);
385 printk(KERN_ERR
"create_file(%s) failed: %d\n", unit
, ret
);
389 /* create the files in the new directory */
390 ret
= create_file("counters", S_IFREG
|S_IRUGO
, dir
, &tmp
,
393 printk(KERN_ERR
"create_file(%s/counters) failed: %d\n",
397 ret
= create_file("counter_names", S_IFREG
|S_IRUGO
, dir
, &tmp
,
400 printk(KERN_ERR
"create_file(%s/counter_names) failed: %d\n",
404 ret
= create_file("portcounter_names", S_IFREG
|S_IRUGO
, dir
, &tmp
,
405 &portcntr_ops
[0], dd
);
407 printk(KERN_ERR
"create_file(%s/%s) failed: %d\n",
408 unit
, "portcounter_names", ret
);
411 for (i
= 1; i
<= dd
->num_pports
; i
++) {
414 sprintf(fname
, "port%dcounters", i
);
415 /* create the files in the new directory */
416 ret
= create_file(fname
, S_IFREG
|S_IRUGO
, dir
, &tmp
,
417 &portcntr_ops
[i
], dd
);
419 printk(KERN_ERR
"create_file(%s/%s) failed: %d\n",
423 if (!(dd
->flags
& QIB_HAS_QSFP
))
425 sprintf(fname
, "qsfp%d", i
);
426 ret
= create_file(fname
, S_IFREG
|S_IRUGO
, dir
, &tmp
,
427 &qsfp_ops
[i
- 1], dd
);
429 printk(KERN_ERR
"create_file(%s/%s) failed: %d\n",
435 ret
= create_file("flash", S_IFREG
|S_IWUSR
|S_IRUGO
, dir
, &tmp
,
438 printk(KERN_ERR
"create_file(%s/flash) failed: %d\n",
444 static int remove_file(struct dentry
*parent
, char *name
)
449 tmp
= lookup_one_len(name
, parent
, strlen(name
));
456 spin_lock(&tmp
->d_lock
);
457 if (!(d_unhashed(tmp
) && tmp
->d_inode
)) {
460 spin_unlock(&tmp
->d_lock
);
461 simple_unlink(parent
->d_inode
, tmp
);
463 spin_unlock(&tmp
->d_lock
);
469 * We don't expect clients to care about the return value, but
470 * it's there if they need it.
475 static int remove_device_files(struct super_block
*sb
,
476 struct qib_devdata
*dd
)
478 struct dentry
*dir
, *root
;
482 root
= dget(sb
->s_root
);
483 mutex_lock(&root
->d_inode
->i_mutex
);
484 snprintf(unit
, sizeof unit
, "%u", dd
->unit
);
485 dir
= lookup_one_len(unit
, root
, strlen(unit
));
489 printk(KERN_ERR
"Lookup of %s failed\n", unit
);
493 remove_file(dir
, "counters");
494 remove_file(dir
, "counter_names");
495 remove_file(dir
, "portcounter_names");
496 for (i
= 0; i
< dd
->num_pports
; i
++) {
499 sprintf(fname
, "port%dcounters", i
+ 1);
500 remove_file(dir
, fname
);
501 if (dd
->flags
& QIB_HAS_QSFP
) {
502 sprintf(fname
, "qsfp%d", i
+ 1);
503 remove_file(dir
, fname
);
506 remove_file(dir
, "flash");
508 ret
= simple_rmdir(root
->d_inode
, dir
);
511 mutex_unlock(&root
->d_inode
->i_mutex
);
517 * This fills everything in when the fs is mounted, to handle umount/mount
518 * after device init. The direct add_cntr_files() call handles adding
519 * them from the init code, when the fs is already mounted.
521 static int qibfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
523 struct qib_devdata
*dd
, *tmp
;
527 static struct tree_descr files
[] = {
528 [2] = {"driver_stats", &driver_ops
[0], S_IRUGO
},
529 [3] = {"driver_stats_names", &driver_ops
[1], S_IRUGO
},
533 ret
= simple_fill_super(sb
, QIBFS_MAGIC
, files
);
535 printk(KERN_ERR
"simple_fill_super failed: %d\n", ret
);
539 spin_lock_irqsave(&qib_devs_lock
, flags
);
541 list_for_each_entry_safe(dd
, tmp
, &qib_dev_list
, list
) {
542 spin_unlock_irqrestore(&qib_devs_lock
, flags
);
543 ret
= add_cntr_files(sb
, dd
);
546 spin_lock_irqsave(&qib_devs_lock
, flags
);
549 spin_unlock_irqrestore(&qib_devs_lock
, flags
);
555 static struct dentry
*qibfs_mount(struct file_system_type
*fs_type
, int flags
,
556 const char *dev_name
, void *data
)
559 ret
= mount_single(fs_type
, flags
, data
, qibfs_fill_super
);
561 qib_super
= ret
->d_sb
;
565 static void qibfs_kill_super(struct super_block
*s
)
567 kill_litter_super(s
);
571 int qibfs_add(struct qib_devdata
*dd
)
576 * On first unit initialized, qib_super will not yet exist
577 * because nobody has yet tried to mount the filesystem, so
578 * we can't consider that to be an error; if an error occurs
579 * during the mount, that will get a complaint, so this is OK.
580 * add_cntr_files() for all units is done at mount from
581 * qibfs_fill_super(), so one way or another, everything works.
583 if (qib_super
== NULL
)
586 ret
= add_cntr_files(qib_super
, dd
);
590 int qibfs_remove(struct qib_devdata
*dd
)
595 ret
= remove_device_files(qib_super
, dd
);
600 static struct file_system_type qibfs_fs_type
= {
601 .owner
= THIS_MODULE
,
603 .mount
= qibfs_mount
,
604 .kill_sb
= qibfs_kill_super
,
607 int __init
qib_init_qibfs(void)
609 return register_filesystem(&qibfs_fs_type
);
612 int __exit
qib_exit_qibfs(void)
614 return unregister_filesystem(&qibfs_fs_type
);