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
36 #include <linux/fs_context.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 simple_inode_init_ts(inode);
69 inode->i_private = data;
71 inode->i_op = &simple_dir_inode_operations;
78 d_instantiate(dentry, inode);
85 static int create_file(const char *name, umode_t mode,
86 struct dentry *parent, struct dentry **dentry,
87 const struct file_operations *fops, void *data)
91 inode_lock(d_inode(parent));
92 *dentry = lookup_one_len(name, parent, strlen(name));
94 error = qibfs_mknod(d_inode(parent), *dentry,
97 error = PTR_ERR(*dentry);
98 inode_unlock(d_inode(parent));
103 static ssize_t driver_stats_read(struct file *file, char __user *buf,
104 size_t count, loff_t *ppos)
106 qib_stats.sps_ints = qib_sps_ints();
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_inode(file)->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;
330 if (pos != 0 || count != sizeof(struct qib_flash))
333 tmp = memdup_user(buf, count);
337 dd = private2dd(file);
338 if (qib_eeprom_write(dd, pos, tmp, count)) {
340 qib_dev_err(dd, "failed to write to flash\n");
352 static const struct file_operations flash_ops = {
354 .write = flash_write,
355 .llseek = default_llseek,
358 static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
360 struct dentry *dir, *tmp;
364 /* create the per-unit directory */
365 snprintf(unit, sizeof(unit), "%u", dd->unit);
366 ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
367 &simple_dir_operations, dd);
369 pr_err("create_file(%s) failed: %d\n", unit, ret);
373 /* create the files in the new directory */
374 ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
377 pr_err("create_file(%s/counters) failed: %d\n",
381 ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
384 pr_err("create_file(%s/counter_names) failed: %d\n",
388 ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
389 &portcntr_ops[0], dd);
391 pr_err("create_file(%s/%s) failed: %d\n",
392 unit, "portcounter_names", ret);
395 for (i = 1; i <= dd->num_pports; i++) {
398 sprintf(fname, "port%dcounters", i);
399 /* create the files in the new directory */
400 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
401 &portcntr_ops[i], dd);
403 pr_err("create_file(%s/%s) failed: %d\n",
407 if (!(dd->flags & QIB_HAS_QSFP))
409 sprintf(fname, "qsfp%d", i);
410 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
411 &qsfp_ops[i - 1], dd);
413 pr_err("create_file(%s/%s) failed: %d\n",
419 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
422 pr_err("create_file(%s/flash) failed: %d\n",
428 static int remove_device_files(struct super_block *sb,
429 struct qib_devdata *dd)
434 snprintf(unit, sizeof(unit), "%u", dd->unit);
435 dir = lookup_one_len_unlocked(unit, sb->s_root, strlen(unit));
438 pr_err("Lookup of %s failed\n", unit);
441 simple_recursive_removal(dir, NULL);
447 * This fills everything in when the fs is mounted, to handle umount/mount
448 * after device init. The direct add_cntr_files() call handles adding
449 * them from the init code, when the fs is already mounted.
451 static int qibfs_fill_super(struct super_block *sb, struct fs_context *fc)
453 struct qib_devdata *dd;
457 static const struct tree_descr files[] = {
458 [2] = {"driver_stats", &driver_ops[0], S_IRUGO},
459 [3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
463 ret = simple_fill_super(sb, QIBFS_MAGIC, files);
465 pr_err("simple_fill_super failed: %d\n", ret);
469 xa_for_each(&qib_dev_table, index, dd) {
470 ret = add_cntr_files(sb, dd);
479 static int qibfs_get_tree(struct fs_context *fc)
481 int ret = get_tree_single(fc, qibfs_fill_super);
483 qib_super = fc->root->d_sb;
487 static const struct fs_context_operations qibfs_context_ops = {
488 .get_tree = qibfs_get_tree,
491 static int qibfs_init_fs_context(struct fs_context *fc)
493 fc->ops = &qibfs_context_ops;
497 static void qibfs_kill_super(struct super_block *s)
499 kill_litter_super(s);
503 int qibfs_add(struct qib_devdata *dd)
508 * On first unit initialized, qib_super will not yet exist
509 * because nobody has yet tried to mount the filesystem, so
510 * we can't consider that to be an error; if an error occurs
511 * during the mount, that will get a complaint, so this is OK.
512 * add_cntr_files() for all units is done at mount from
513 * qibfs_fill_super(), so one way or another, everything works.
515 if (qib_super == NULL)
518 ret = add_cntr_files(qib_super, dd);
522 int qibfs_remove(struct qib_devdata *dd)
527 ret = remove_device_files(qib_super, dd);
532 static struct file_system_type qibfs_fs_type = {
533 .owner = THIS_MODULE,
535 .init_fs_context = qibfs_init_fs_context,
536 .kill_sb = qibfs_kill_super,
538 MODULE_ALIAS_FS("ipathfs");
540 int __init qib_init_qibfs(void)
542 return register_filesystem(&qibfs_fs_type);
545 int __exit qib_exit_qibfs(void)
547 return unregister_filesystem(&qibfs_fs_type);