treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / infiniband / hw / qib / qib_fs.c
blobe336d778e076eddb3d56c37b6fd33be0de87b050
1 /*
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
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
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
32 * SOFTWARE.
35 #include <linux/module.h>
36 #include <linux/fs.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>
43 #include "qib.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,
53 void *data)
55 int error;
56 struct inode *inode = new_inode(dir->i_sb);
58 if (!inode) {
59 error = -EPERM;
60 goto bail;
63 inode->i_ino = get_next_ino();
64 inode->i_mode = mode;
65 inode->i_uid = GLOBAL_ROOT_UID;
66 inode->i_gid = GLOBAL_ROOT_GID;
67 inode->i_blocks = 0;
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;
72 if (S_ISDIR(mode)) {
73 inode->i_op = &simple_dir_inode_operations;
74 inc_nlink(inode);
75 inc_nlink(dir);
78 inode->i_fop = fops;
80 d_instantiate(dentry, inode);
81 error = 0;
83 bail:
84 return error;
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)
91 int error;
93 inode_lock(d_inode(parent));
94 *dentry = lookup_one_len(name, parent, strlen(name));
95 if (!IS_ERR(*dentry))
96 error = qibfs_mknod(d_inode(parent), *dentry,
97 mode, fops, data);
98 else
99 error = PTR_ERR(*dentry);
100 inode_unlock(d_inode(parent));
102 return error;
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,
110 sizeof(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[] =
121 "KernIntr\n"
122 "ErrorIntr\n"
123 "Tx_Errs\n"
124 "Rcv_Errs\n"
125 "H/W_Errs\n"
126 "NoPIOBufs\n"
127 "CtxtsOpen\n"
128 "RcvLen_Errs\n"
129 "EgrBufFull\n"
130 "EgrHdrFull\n"
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)
149 u64 *counters;
150 size_t avail;
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)
161 char *names;
162 size_t avail;
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)
183 char *names;
184 size_t avail;
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)
195 u64 *counters;
196 size_t avail;
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)
207 u64 *counters;
208 size_t avail;
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);
228 char *tmp;
229 int ret;
231 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
232 if (!tmp)
233 return -ENOMEM;
235 ret = qib_qsfp_dump(dd->pport, tmp, PAGE_SIZE);
236 if (ret > 0)
237 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
238 kfree(tmp);
239 return 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);
249 char *tmp;
250 int ret;
252 if (dd->num_pports < 2)
253 return -ENODEV;
255 tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
256 if (!tmp)
257 return -ENOMEM;
259 ret = qib_qsfp_dump(dd->pport + 1, tmp, PAGE_SIZE);
260 if (ret > 0)
261 ret = simple_read_from_buffer(buf, count, ppos, tmp, ret);
262 kfree(tmp);
263 return 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;
275 ssize_t ret;
276 loff_t pos;
277 char *tmp;
279 pos = *ppos;
281 if (pos < 0) {
282 ret = -EINVAL;
283 goto bail;
286 if (pos >= sizeof(struct qib_flash)) {
287 ret = 0;
288 goto bail;
291 if (count > sizeof(struct qib_flash) - pos)
292 count = sizeof(struct qib_flash) - pos;
294 tmp = kmalloc(count, GFP_KERNEL);
295 if (!tmp) {
296 ret = -ENOMEM;
297 goto bail;
300 dd = private2dd(file);
301 if (qib_eeprom_read(dd, pos, tmp, count)) {
302 qib_dev_err(dd, "failed to read from flash\n");
303 ret = -ENXIO;
304 goto bail_tmp;
307 if (copy_to_user(buf, tmp, count)) {
308 ret = -EFAULT;
309 goto bail_tmp;
312 *ppos = pos + count;
313 ret = count;
315 bail_tmp:
316 kfree(tmp);
318 bail:
319 return ret;
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;
326 ssize_t ret;
327 loff_t pos;
328 char *tmp;
330 pos = *ppos;
332 if (pos != 0 || count != sizeof(struct qib_flash))
333 return -EINVAL;
335 tmp = memdup_user(buf, count);
336 if (IS_ERR(tmp))
337 return PTR_ERR(tmp);
339 dd = private2dd(file);
340 if (qib_eeprom_write(dd, pos, tmp, count)) {
341 ret = -ENXIO;
342 qib_dev_err(dd, "failed to write to flash\n");
343 goto bail_tmp;
346 *ppos = pos + count;
347 ret = count;
349 bail_tmp:
350 kfree(tmp);
351 return ret;
354 static const struct file_operations flash_ops = {
355 .read = flash_read,
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;
363 char unit[10];
364 int ret, i;
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);
370 if (ret) {
371 pr_err("create_file(%s) failed: %d\n", unit, ret);
372 goto bail;
375 /* create the files in the new directory */
376 ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
377 &cntr_ops[0], dd);
378 if (ret) {
379 pr_err("create_file(%s/counters) failed: %d\n",
380 unit, ret);
381 goto bail;
383 ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
384 &cntr_ops[1], dd);
385 if (ret) {
386 pr_err("create_file(%s/counter_names) failed: %d\n",
387 unit, ret);
388 goto bail;
390 ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
391 &portcntr_ops[0], dd);
392 if (ret) {
393 pr_err("create_file(%s/%s) failed: %d\n",
394 unit, "portcounter_names", ret);
395 goto bail;
397 for (i = 1; i <= dd->num_pports; i++) {
398 char fname[24];
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);
404 if (ret) {
405 pr_err("create_file(%s/%s) failed: %d\n",
406 unit, fname, ret);
407 goto bail;
409 if (!(dd->flags & QIB_HAS_QSFP))
410 continue;
411 sprintf(fname, "qsfp%d", i);
412 ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
413 &qsfp_ops[i - 1], dd);
414 if (ret) {
415 pr_err("create_file(%s/%s) failed: %d\n",
416 unit, fname, ret);
417 goto bail;
421 ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
422 &flash_ops, dd);
423 if (ret)
424 pr_err("create_file(%s/flash) failed: %d\n",
425 unit, ret);
426 bail:
427 return ret;
430 static int remove_file(struct dentry *parent, char *name)
432 struct dentry *tmp;
433 int ret;
435 tmp = lookup_one_len(name, parent, strlen(name));
437 if (IS_ERR(tmp)) {
438 ret = PTR_ERR(tmp);
439 goto bail;
442 spin_lock(&tmp->d_lock);
443 if (simple_positive(tmp)) {
444 __d_drop(tmp);
445 spin_unlock(&tmp->d_lock);
446 simple_unlink(d_inode(parent), tmp);
447 } else {
448 spin_unlock(&tmp->d_lock);
450 dput(tmp);
452 ret = 0;
453 bail:
455 * We don't expect clients to care about the return value, but
456 * it's there if they need it.
458 return ret;
461 static int remove_device_files(struct super_block *sb,
462 struct qib_devdata *dd)
464 struct dentry *dir, *root;
465 char unit[10];
466 int ret, i;
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));
473 if (IS_ERR(dir)) {
474 ret = PTR_ERR(dir);
475 pr_err("Lookup of %s failed\n", unit);
476 goto bail;
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++) {
484 char fname[24];
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);
496 d_drop(dir);
497 dput(dir);
499 bail:
500 inode_unlock(d_inode(root));
501 dput(root);
502 return ret;
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;
513 unsigned long index;
514 int ret;
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},
519 {""},
522 ret = simple_fill_super(sb, QIBFS_MAGIC, files);
523 if (ret) {
524 pr_err("simple_fill_super failed: %d\n", ret);
525 goto bail;
528 xa_for_each(&qib_dev_table, index, dd) {
529 ret = add_cntr_files(sb, dd);
530 if (ret)
531 goto bail;
534 bail:
535 return ret;
538 static int qibfs_get_tree(struct fs_context *fc)
540 int ret = get_tree_single(fc, qibfs_fill_super);
541 if (ret == 0)
542 qib_super = fc->root->d_sb;
543 return ret;
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;
553 return 0;
556 static void qibfs_kill_super(struct super_block *s)
558 kill_litter_super(s);
559 qib_super = NULL;
562 int qibfs_add(struct qib_devdata *dd)
564 int ret;
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)
575 ret = 0;
576 else
577 ret = add_cntr_files(qib_super, dd);
578 return ret;
581 int qibfs_remove(struct qib_devdata *dd)
583 int ret = 0;
585 if (qib_super)
586 ret = remove_device_files(qib_super, dd);
588 return ret;
591 static struct file_system_type qibfs_fs_type = {
592 .owner = THIS_MODULE,
593 .name = "ipathfs",
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);