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/mount.h>
38 #include <linux/pagemap.h>
39 #include <linux/init.h>
40 #include <linux/namei.h>
44 #define QIBFS_MAGIC 0x726a77
46 static struct super_block
*qib_super
;
48 #define private2dd(file) (file_inode(file)->i_private)
50 static int qibfs_mknod(struct inode
*dir
, struct dentry
*dentry
,
51 umode_t mode
, const struct file_operations
*fops
,
55 struct inode
*inode
= new_inode(dir
->i_sb
);
62 inode
->i_ino
= get_next_ino();
64 inode
->i_uid
= GLOBAL_ROOT_UID
;
65 inode
->i_gid
= GLOBAL_ROOT_GID
;
67 inode
->i_atime
= current_time(inode
);
68 inode
->i_mtime
= inode
->i_atime
;
69 inode
->i_ctime
= inode
->i_atime
;
70 inode
->i_private
= data
;
72 inode
->i_op
= &simple_dir_inode_operations
;
79 d_instantiate(dentry
, inode
);
86 static int create_file(const char *name
, umode_t mode
,
87 struct dentry
*parent
, struct dentry
**dentry
,
88 const struct file_operations
*fops
, void *data
)
92 inode_lock(d_inode(parent
));
93 *dentry
= lookup_one_len(name
, parent
, strlen(name
));
95 error
= qibfs_mknod(d_inode(parent
), *dentry
,
98 error
= PTR_ERR(*dentry
);
99 inode_unlock(d_inode(parent
));
104 static ssize_t
driver_stats_read(struct file
*file
, char __user
*buf
,
105 size_t count
, loff_t
*ppos
)
107 qib_stats
.sps_ints
= qib_sps_ints();
108 return simple_read_from_buffer(buf
, count
, ppos
, &qib_stats
,
113 * driver stats field names, one line per stat, single string. Used by
114 * programs like ipathstats to print the stats in a way which works for
115 * different versions of drivers, without changing program source.
116 * if qlogic_ib_stats changes, this needs to change. Names need to be
117 * 12 chars or less (w/o newline), for proper display by ipathstats utility.
119 static const char qib_statnames
[] =
132 static ssize_t
driver_names_read(struct file
*file
, char __user
*buf
,
133 size_t count
, loff_t
*ppos
)
135 return simple_read_from_buffer(buf
, count
, ppos
, qib_statnames
,
136 sizeof(qib_statnames
) - 1); /* no null */
139 static const struct file_operations driver_ops
[] = {
140 { .read
= driver_stats_read
, .llseek
= generic_file_llseek
, },
141 { .read
= driver_names_read
, .llseek
= generic_file_llseek
, },
144 /* read the per-device counters */
145 static ssize_t
dev_counters_read(struct file
*file
, char __user
*buf
,
146 size_t count
, loff_t
*ppos
)
150 struct qib_devdata
*dd
= private2dd(file
);
152 avail
= dd
->f_read_cntrs(dd
, *ppos
, NULL
, &counters
);
153 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
156 /* read the per-device counters */
157 static ssize_t
dev_names_read(struct file
*file
, char __user
*buf
,
158 size_t count
, loff_t
*ppos
)
162 struct qib_devdata
*dd
= private2dd(file
);
164 avail
= dd
->f_read_cntrs(dd
, *ppos
, &names
, NULL
);
165 return simple_read_from_buffer(buf
, count
, ppos
, names
, avail
);
168 static const struct file_operations cntr_ops
[] = {
169 { .read
= dev_counters_read
, .llseek
= generic_file_llseek
, },
170 { .read
= dev_names_read
, .llseek
= generic_file_llseek
, },
174 * Could use file_inode(file)->i_ino to figure out which file,
175 * instead of separate routine for each, but for now, this works...
178 /* read the per-port names (same for each port) */
179 static ssize_t
portnames_read(struct file
*file
, char __user
*buf
,
180 size_t count
, loff_t
*ppos
)
184 struct qib_devdata
*dd
= private2dd(file
);
186 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 0, &names
, NULL
);
187 return simple_read_from_buffer(buf
, count
, ppos
, names
, avail
);
190 /* read the per-port counters for port 1 (pidx 0) */
191 static ssize_t
portcntrs_1_read(struct file
*file
, char __user
*buf
,
192 size_t count
, loff_t
*ppos
)
196 struct qib_devdata
*dd
= private2dd(file
);
198 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 0, NULL
, &counters
);
199 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
202 /* read the per-port counters for port 2 (pidx 1) */
203 static ssize_t
portcntrs_2_read(struct file
*file
, char __user
*buf
,
204 size_t count
, loff_t
*ppos
)
208 struct qib_devdata
*dd
= private2dd(file
);
210 avail
= dd
->f_read_portcntrs(dd
, *ppos
, 1, NULL
, &counters
);
211 return simple_read_from_buffer(buf
, count
, ppos
, counters
, avail
);
214 static const struct file_operations portcntr_ops
[] = {
215 { .read
= portnames_read
, .llseek
= generic_file_llseek
, },
216 { .read
= portcntrs_1_read
, .llseek
= generic_file_llseek
, },
217 { .read
= portcntrs_2_read
, .llseek
= generic_file_llseek
, },
221 * read the per-port QSFP data for port 1 (pidx 0)
223 static ssize_t
qsfp_1_read(struct file
*file
, char __user
*buf
,
224 size_t count
, loff_t
*ppos
)
226 struct qib_devdata
*dd
= private2dd(file
);
230 tmp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
234 ret
= qib_qsfp_dump(dd
->pport
, tmp
, PAGE_SIZE
);
236 ret
= simple_read_from_buffer(buf
, count
, ppos
, tmp
, ret
);
242 * read the per-port QSFP data for port 2 (pidx 1)
244 static ssize_t
qsfp_2_read(struct file
*file
, char __user
*buf
,
245 size_t count
, loff_t
*ppos
)
247 struct qib_devdata
*dd
= private2dd(file
);
251 if (dd
->num_pports
< 2)
254 tmp
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
258 ret
= qib_qsfp_dump(dd
->pport
+ 1, tmp
, PAGE_SIZE
);
260 ret
= simple_read_from_buffer(buf
, count
, ppos
, tmp
, ret
);
265 static const struct file_operations qsfp_ops
[] = {
266 { .read
= qsfp_1_read
, .llseek
= generic_file_llseek
, },
267 { .read
= qsfp_2_read
, .llseek
= generic_file_llseek
, },
270 static ssize_t
flash_read(struct file
*file
, char __user
*buf
,
271 size_t count
, loff_t
*ppos
)
273 struct qib_devdata
*dd
;
285 if (pos
>= sizeof(struct qib_flash
)) {
290 if (count
> sizeof(struct qib_flash
) - pos
)
291 count
= sizeof(struct qib_flash
) - pos
;
293 tmp
= kmalloc(count
, GFP_KERNEL
);
299 dd
= private2dd(file
);
300 if (qib_eeprom_read(dd
, pos
, tmp
, count
)) {
301 qib_dev_err(dd
, "failed to read from flash\n");
306 if (copy_to_user(buf
, tmp
, count
)) {
321 static ssize_t
flash_write(struct file
*file
, const char __user
*buf
,
322 size_t count
, loff_t
*ppos
)
324 struct qib_devdata
*dd
;
331 if (pos
!= 0 || count
!= sizeof(struct qib_flash
))
334 tmp
= memdup_user(buf
, count
);
338 dd
= private2dd(file
);
339 if (qib_eeprom_write(dd
, pos
, tmp
, count
)) {
341 qib_dev_err(dd
, "failed to write to flash\n");
353 static const struct file_operations flash_ops
= {
355 .write
= flash_write
,
356 .llseek
= default_llseek
,
359 static int add_cntr_files(struct super_block
*sb
, struct qib_devdata
*dd
)
361 struct dentry
*dir
, *tmp
;
365 /* create the per-unit directory */
366 snprintf(unit
, sizeof(unit
), "%u", dd
->unit
);
367 ret
= create_file(unit
, S_IFDIR
|S_IRUGO
|S_IXUGO
, sb
->s_root
, &dir
,
368 &simple_dir_operations
, dd
);
370 pr_err("create_file(%s) failed: %d\n", unit
, ret
);
374 /* create the files in the new directory */
375 ret
= create_file("counters", S_IFREG
|S_IRUGO
, dir
, &tmp
,
378 pr_err("create_file(%s/counters) failed: %d\n",
382 ret
= create_file("counter_names", S_IFREG
|S_IRUGO
, dir
, &tmp
,
385 pr_err("create_file(%s/counter_names) failed: %d\n",
389 ret
= create_file("portcounter_names", S_IFREG
|S_IRUGO
, dir
, &tmp
,
390 &portcntr_ops
[0], dd
);
392 pr_err("create_file(%s/%s) failed: %d\n",
393 unit
, "portcounter_names", ret
);
396 for (i
= 1; i
<= dd
->num_pports
; i
++) {
399 sprintf(fname
, "port%dcounters", i
);
400 /* create the files in the new directory */
401 ret
= create_file(fname
, S_IFREG
|S_IRUGO
, dir
, &tmp
,
402 &portcntr_ops
[i
], dd
);
404 pr_err("create_file(%s/%s) failed: %d\n",
408 if (!(dd
->flags
& QIB_HAS_QSFP
))
410 sprintf(fname
, "qsfp%d", i
);
411 ret
= create_file(fname
, S_IFREG
|S_IRUGO
, dir
, &tmp
,
412 &qsfp_ops
[i
- 1], dd
);
414 pr_err("create_file(%s/%s) failed: %d\n",
420 ret
= create_file("flash", S_IFREG
|S_IWUSR
|S_IRUGO
, dir
, &tmp
,
423 pr_err("create_file(%s/flash) failed: %d\n",
429 static int remove_file(struct dentry
*parent
, char *name
)
434 tmp
= lookup_one_len(name
, parent
, strlen(name
));
441 spin_lock(&tmp
->d_lock
);
442 if (simple_positive(tmp
)) {
444 spin_unlock(&tmp
->d_lock
);
445 simple_unlink(d_inode(parent
), tmp
);
447 spin_unlock(&tmp
->d_lock
);
454 * We don't expect clients to care about the return value, but
455 * it's there if they need it.
460 static int remove_device_files(struct super_block
*sb
,
461 struct qib_devdata
*dd
)
463 struct dentry
*dir
, *root
;
467 root
= dget(sb
->s_root
);
468 inode_lock(d_inode(root
));
469 snprintf(unit
, sizeof(unit
), "%u", dd
->unit
);
470 dir
= lookup_one_len(unit
, root
, strlen(unit
));
474 pr_err("Lookup of %s failed\n", unit
);
478 inode_lock(d_inode(dir
));
479 remove_file(dir
, "counters");
480 remove_file(dir
, "counter_names");
481 remove_file(dir
, "portcounter_names");
482 for (i
= 0; i
< dd
->num_pports
; i
++) {
485 sprintf(fname
, "port%dcounters", i
+ 1);
486 remove_file(dir
, fname
);
487 if (dd
->flags
& QIB_HAS_QSFP
) {
488 sprintf(fname
, "qsfp%d", i
+ 1);
489 remove_file(dir
, fname
);
492 remove_file(dir
, "flash");
493 inode_unlock(d_inode(dir
));
494 ret
= simple_rmdir(d_inode(root
), dir
);
499 inode_unlock(d_inode(root
));
505 * This fills everything in when the fs is mounted, to handle umount/mount
506 * after device init. The direct add_cntr_files() call handles adding
507 * them from the init code, when the fs is already mounted.
509 static int qibfs_fill_super(struct super_block
*sb
, void *data
, int silent
)
511 struct qib_devdata
*dd
, *tmp
;
515 static const struct tree_descr files
[] = {
516 [2] = {"driver_stats", &driver_ops
[0], S_IRUGO
},
517 [3] = {"driver_stats_names", &driver_ops
[1], S_IRUGO
},
521 ret
= simple_fill_super(sb
, QIBFS_MAGIC
, files
);
523 pr_err("simple_fill_super failed: %d\n", ret
);
527 spin_lock_irqsave(&qib_devs_lock
, flags
);
529 list_for_each_entry_safe(dd
, tmp
, &qib_dev_list
, list
) {
530 spin_unlock_irqrestore(&qib_devs_lock
, flags
);
531 ret
= add_cntr_files(sb
, dd
);
534 spin_lock_irqsave(&qib_devs_lock
, flags
);
537 spin_unlock_irqrestore(&qib_devs_lock
, flags
);
543 static struct dentry
*qibfs_mount(struct file_system_type
*fs_type
, int flags
,
544 const char *dev_name
, void *data
)
548 ret
= mount_single(fs_type
, flags
, data
, qibfs_fill_super
);
550 qib_super
= ret
->d_sb
;
554 static void qibfs_kill_super(struct super_block
*s
)
556 kill_litter_super(s
);
560 int qibfs_add(struct qib_devdata
*dd
)
565 * On first unit initialized, qib_super will not yet exist
566 * because nobody has yet tried to mount the filesystem, so
567 * we can't consider that to be an error; if an error occurs
568 * during the mount, that will get a complaint, so this is OK.
569 * add_cntr_files() for all units is done at mount from
570 * qibfs_fill_super(), so one way or another, everything works.
572 if (qib_super
== NULL
)
575 ret
= add_cntr_files(qib_super
, dd
);
579 int qibfs_remove(struct qib_devdata
*dd
)
584 ret
= remove_device_files(qib_super
, dd
);
589 static struct file_system_type qibfs_fs_type
= {
590 .owner
= THIS_MODULE
,
592 .mount
= qibfs_mount
,
593 .kill_sb
= qibfs_kill_super
,
595 MODULE_ALIAS_FS("ipathfs");
597 int __init
qib_init_qibfs(void)
599 return register_filesystem(&qibfs_fs_type
);
602 int __exit
qib_exit_qibfs(void)
604 return unregister_filesystem(&qibfs_fs_type
);