xref: /openbmc/linux/drivers/infiniband/hw/qib/qib_fs.c (revision 588b48ca)
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.
5  *
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:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
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.
24  *
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.
33  */
34 
35 #include <linux/module.h>
36 #include <linux/fs.h>
37 #include <linux/mount.h>
38 #include <linux/pagemap.h>
39 #include <linux/init.h>
40 #include <linux/namei.h>
41 
42 #include "qib.h"
43 
44 #define QIBFS_MAGIC 0x726a77
45 
46 static struct super_block *qib_super;
47 
48 #define private2dd(file) (file_inode(file)->i_private)
49 
50 static int qibfs_mknod(struct inode *dir, struct dentry *dentry,
51 		       umode_t mode, const struct file_operations *fops,
52 		       void *data)
53 {
54 	int error;
55 	struct inode *inode = new_inode(dir->i_sb);
56 
57 	if (!inode) {
58 		error = -EPERM;
59 		goto bail;
60 	}
61 
62 	inode->i_ino = get_next_ino();
63 	inode->i_mode = mode;
64 	inode->i_uid = GLOBAL_ROOT_UID;
65 	inode->i_gid = GLOBAL_ROOT_GID;
66 	inode->i_blocks = 0;
67 	inode->i_atime = CURRENT_TIME;
68 	inode->i_mtime = inode->i_atime;
69 	inode->i_ctime = inode->i_atime;
70 	inode->i_private = data;
71 	if (S_ISDIR(mode)) {
72 		inode->i_op = &simple_dir_inode_operations;
73 		inc_nlink(inode);
74 		inc_nlink(dir);
75 	}
76 
77 	inode->i_fop = fops;
78 
79 	d_instantiate(dentry, inode);
80 	error = 0;
81 
82 bail:
83 	return error;
84 }
85 
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)
89 {
90 	int error;
91 
92 	*dentry = NULL;
93 	mutex_lock(&parent->d_inode->i_mutex);
94 	*dentry = lookup_one_len(name, parent, strlen(name));
95 	if (!IS_ERR(*dentry))
96 		error = qibfs_mknod(parent->d_inode, *dentry,
97 				    mode, fops, data);
98 	else
99 		error = PTR_ERR(*dentry);
100 	mutex_unlock(&parent->d_inode->i_mutex);
101 
102 	return error;
103 }
104 
105 static ssize_t driver_stats_read(struct file *file, char __user *buf,
106 				 size_t count, loff_t *ppos)
107 {
108 	qib_stats.sps_ints = qib_sps_ints();
109 	return simple_read_from_buffer(buf, count, ppos, &qib_stats,
110 				       sizeof qib_stats);
111 }
112 
113 /*
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.
119  */
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"
131 	;
132 
133 static ssize_t driver_names_read(struct file *file, char __user *buf,
134 				 size_t count, loff_t *ppos)
135 {
136 	return simple_read_from_buffer(buf, count, ppos, qib_statnames,
137 		sizeof qib_statnames - 1); /* no null */
138 }
139 
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, },
143 };
144 
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)
148 {
149 	u64 *counters;
150 	size_t avail;
151 	struct qib_devdata *dd = private2dd(file);
152 
153 	avail = dd->f_read_cntrs(dd, *ppos, NULL, &counters);
154 	return simple_read_from_buffer(buf, count, ppos, counters, avail);
155 }
156 
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)
160 {
161 	char *names;
162 	size_t avail;
163 	struct qib_devdata *dd = private2dd(file);
164 
165 	avail = dd->f_read_cntrs(dd, *ppos, &names, NULL);
166 	return simple_read_from_buffer(buf, count, ppos, names, avail);
167 }
168 
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, },
172 };
173 
174 /*
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...
177  */
178 
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)
182 {
183 	char *names;
184 	size_t avail;
185 	struct qib_devdata *dd = private2dd(file);
186 
187 	avail = dd->f_read_portcntrs(dd, *ppos, 0, &names, NULL);
188 	return simple_read_from_buffer(buf, count, ppos, names, avail);
189 }
190 
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)
194 {
195 	u64 *counters;
196 	size_t avail;
197 	struct qib_devdata *dd = private2dd(file);
198 
199 	avail = dd->f_read_portcntrs(dd, *ppos, 0, NULL, &counters);
200 	return simple_read_from_buffer(buf, count, ppos, counters, avail);
201 }
202 
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)
206 {
207 	u64 *counters;
208 	size_t avail;
209 	struct qib_devdata *dd = private2dd(file);
210 
211 	avail = dd->f_read_portcntrs(dd, *ppos, 1, NULL, &counters);
212 	return simple_read_from_buffer(buf, count, ppos, counters, avail);
213 }
214 
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, },
219 };
220 
221 /*
222  * read the per-port QSFP data for port 1 (pidx 0)
223  */
224 static ssize_t qsfp_1_read(struct file *file, char __user *buf,
225 			   size_t count, loff_t *ppos)
226 {
227 	struct qib_devdata *dd = private2dd(file);
228 	char *tmp;
229 	int ret;
230 
231 	tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
232 	if (!tmp)
233 		return -ENOMEM;
234 
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;
240 }
241 
242 /*
243  * read the per-port QSFP data for port 2 (pidx 1)
244  */
245 static ssize_t qsfp_2_read(struct file *file, char __user *buf,
246 			   size_t count, loff_t *ppos)
247 {
248 	struct qib_devdata *dd = private2dd(file);
249 	char *tmp;
250 	int ret;
251 
252 	if (dd->num_pports < 2)
253 		return -ENODEV;
254 
255 	tmp = kmalloc(PAGE_SIZE, GFP_KERNEL);
256 	if (!tmp)
257 		return -ENOMEM;
258 
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;
264 }
265 
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, },
269 };
270 
271 static ssize_t flash_read(struct file *file, char __user *buf,
272 			  size_t count, loff_t *ppos)
273 {
274 	struct qib_devdata *dd;
275 	ssize_t ret;
276 	loff_t pos;
277 	char *tmp;
278 
279 	pos = *ppos;
280 
281 	if (pos < 0) {
282 		ret = -EINVAL;
283 		goto bail;
284 	}
285 
286 	if (pos >= sizeof(struct qib_flash)) {
287 		ret = 0;
288 		goto bail;
289 	}
290 
291 	if (count > sizeof(struct qib_flash) - pos)
292 		count = sizeof(struct qib_flash) - pos;
293 
294 	tmp = kmalloc(count, GFP_KERNEL);
295 	if (!tmp) {
296 		ret = -ENOMEM;
297 		goto bail;
298 	}
299 
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;
305 	}
306 
307 	if (copy_to_user(buf, tmp, count)) {
308 		ret = -EFAULT;
309 		goto bail_tmp;
310 	}
311 
312 	*ppos = pos + count;
313 	ret = count;
314 
315 bail_tmp:
316 	kfree(tmp);
317 
318 bail:
319 	return ret;
320 }
321 
322 static ssize_t flash_write(struct file *file, const char __user *buf,
323 			   size_t count, loff_t *ppos)
324 {
325 	struct qib_devdata *dd;
326 	ssize_t ret;
327 	loff_t pos;
328 	char *tmp;
329 
330 	pos = *ppos;
331 
332 	if (pos != 0) {
333 		ret = -EINVAL;
334 		goto bail;
335 	}
336 
337 	if (count != sizeof(struct qib_flash)) {
338 		ret = -EINVAL;
339 		goto bail;
340 	}
341 
342 	tmp = kmalloc(count, GFP_KERNEL);
343 	if (!tmp) {
344 		ret = -ENOMEM;
345 		goto bail;
346 	}
347 
348 	if (copy_from_user(tmp, buf, count)) {
349 		ret = -EFAULT;
350 		goto bail_tmp;
351 	}
352 
353 	dd = private2dd(file);
354 	if (qib_eeprom_write(dd, pos, tmp, count)) {
355 		ret = -ENXIO;
356 		qib_dev_err(dd, "failed to write to flash\n");
357 		goto bail_tmp;
358 	}
359 
360 	*ppos = pos + count;
361 	ret = count;
362 
363 bail_tmp:
364 	kfree(tmp);
365 
366 bail:
367 	return ret;
368 }
369 
370 static const struct file_operations flash_ops = {
371 	.read = flash_read,
372 	.write = flash_write,
373 	.llseek = default_llseek,
374 };
375 
376 static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
377 {
378 	struct dentry *dir, *tmp;
379 	char unit[10];
380 	int ret, i;
381 
382 	/* create the per-unit directory */
383 	snprintf(unit, sizeof unit, "%u", dd->unit);
384 	ret = create_file(unit, S_IFDIR|S_IRUGO|S_IXUGO, sb->s_root, &dir,
385 			  &simple_dir_operations, dd);
386 	if (ret) {
387 		pr_err("create_file(%s) failed: %d\n", unit, ret);
388 		goto bail;
389 	}
390 
391 	/* create the files in the new directory */
392 	ret = create_file("counters", S_IFREG|S_IRUGO, dir, &tmp,
393 			  &cntr_ops[0], dd);
394 	if (ret) {
395 		pr_err("create_file(%s/counters) failed: %d\n",
396 		       unit, ret);
397 		goto bail;
398 	}
399 	ret = create_file("counter_names", S_IFREG|S_IRUGO, dir, &tmp,
400 			  &cntr_ops[1], dd);
401 	if (ret) {
402 		pr_err("create_file(%s/counter_names) failed: %d\n",
403 		       unit, ret);
404 		goto bail;
405 	}
406 	ret = create_file("portcounter_names", S_IFREG|S_IRUGO, dir, &tmp,
407 			  &portcntr_ops[0], dd);
408 	if (ret) {
409 		pr_err("create_file(%s/%s) failed: %d\n",
410 		       unit, "portcounter_names", ret);
411 		goto bail;
412 	}
413 	for (i = 1; i <= dd->num_pports; i++) {
414 		char fname[24];
415 
416 		sprintf(fname, "port%dcounters", i);
417 		/* create the files in the new directory */
418 		ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
419 				  &portcntr_ops[i], dd);
420 		if (ret) {
421 			pr_err("create_file(%s/%s) failed: %d\n",
422 				unit, fname, ret);
423 			goto bail;
424 		}
425 		if (!(dd->flags & QIB_HAS_QSFP))
426 			continue;
427 		sprintf(fname, "qsfp%d", i);
428 		ret = create_file(fname, S_IFREG|S_IRUGO, dir, &tmp,
429 				  &qsfp_ops[i - 1], dd);
430 		if (ret) {
431 			pr_err("create_file(%s/%s) failed: %d\n",
432 				unit, fname, ret);
433 			goto bail;
434 		}
435 	}
436 
437 	ret = create_file("flash", S_IFREG|S_IWUSR|S_IRUGO, dir, &tmp,
438 			  &flash_ops, dd);
439 	if (ret)
440 		pr_err("create_file(%s/flash) failed: %d\n",
441 			unit, ret);
442 bail:
443 	return ret;
444 }
445 
446 static int remove_file(struct dentry *parent, char *name)
447 {
448 	struct dentry *tmp;
449 	int ret;
450 
451 	tmp = lookup_one_len(name, parent, strlen(name));
452 
453 	if (IS_ERR(tmp)) {
454 		ret = PTR_ERR(tmp);
455 		goto bail;
456 	}
457 
458 	spin_lock(&tmp->d_lock);
459 	if (!(d_unhashed(tmp) && tmp->d_inode)) {
460 		__d_drop(tmp);
461 		spin_unlock(&tmp->d_lock);
462 		simple_unlink(parent->d_inode, tmp);
463 	} else {
464 		spin_unlock(&tmp->d_lock);
465 	}
466 	dput(tmp);
467 
468 	ret = 0;
469 bail:
470 	/*
471 	 * We don't expect clients to care about the return value, but
472 	 * it's there if they need it.
473 	 */
474 	return ret;
475 }
476 
477 static int remove_device_files(struct super_block *sb,
478 			       struct qib_devdata *dd)
479 {
480 	struct dentry *dir, *root;
481 	char unit[10];
482 	int ret, i;
483 
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));
488 
489 	if (IS_ERR(dir)) {
490 		ret = PTR_ERR(dir);
491 		pr_err("Lookup of %s failed\n", unit);
492 		goto bail;
493 	}
494 
495 	mutex_lock(&dir->d_inode->i_mutex);
496 	remove_file(dir, "counters");
497 	remove_file(dir, "counter_names");
498 	remove_file(dir, "portcounter_names");
499 	for (i = 0; i < dd->num_pports; i++) {
500 		char fname[24];
501 
502 		sprintf(fname, "port%dcounters", i + 1);
503 		remove_file(dir, fname);
504 		if (dd->flags & QIB_HAS_QSFP) {
505 			sprintf(fname, "qsfp%d", i + 1);
506 			remove_file(dir, fname);
507 		}
508 	}
509 	remove_file(dir, "flash");
510 	mutex_unlock(&dir->d_inode->i_mutex);
511 	ret = simple_rmdir(root->d_inode, dir);
512 	d_delete(dir);
513 	dput(dir);
514 
515 bail:
516 	mutex_unlock(&root->d_inode->i_mutex);
517 	dput(root);
518 	return ret;
519 }
520 
521 /*
522  * This fills everything in when the fs is mounted, to handle umount/mount
523  * after device init.  The direct add_cntr_files() call handles adding
524  * them from the init code, when the fs is already mounted.
525  */
526 static int qibfs_fill_super(struct super_block *sb, void *data, int silent)
527 {
528 	struct qib_devdata *dd, *tmp;
529 	unsigned long flags;
530 	int ret;
531 
532 	static struct tree_descr files[] = {
533 		[2] = {"driver_stats", &driver_ops[0], S_IRUGO},
534 		[3] = {"driver_stats_names", &driver_ops[1], S_IRUGO},
535 		{""},
536 	};
537 
538 	ret = simple_fill_super(sb, QIBFS_MAGIC, files);
539 	if (ret) {
540 		pr_err("simple_fill_super failed: %d\n", ret);
541 		goto bail;
542 	}
543 
544 	spin_lock_irqsave(&qib_devs_lock, flags);
545 
546 	list_for_each_entry_safe(dd, tmp, &qib_dev_list, list) {
547 		spin_unlock_irqrestore(&qib_devs_lock, flags);
548 		ret = add_cntr_files(sb, dd);
549 		if (ret)
550 			goto bail;
551 		spin_lock_irqsave(&qib_devs_lock, flags);
552 	}
553 
554 	spin_unlock_irqrestore(&qib_devs_lock, flags);
555 
556 bail:
557 	return ret;
558 }
559 
560 static struct dentry *qibfs_mount(struct file_system_type *fs_type, int flags,
561 			const char *dev_name, void *data)
562 {
563 	struct dentry *ret;
564 	ret = mount_single(fs_type, flags, data, qibfs_fill_super);
565 	if (!IS_ERR(ret))
566 		qib_super = ret->d_sb;
567 	return ret;
568 }
569 
570 static void qibfs_kill_super(struct super_block *s)
571 {
572 	kill_litter_super(s);
573 	qib_super = NULL;
574 }
575 
576 int qibfs_add(struct qib_devdata *dd)
577 {
578 	int ret;
579 
580 	/*
581 	 * On first unit initialized, qib_super will not yet exist
582 	 * because nobody has yet tried to mount the filesystem, so
583 	 * we can't consider that to be an error; if an error occurs
584 	 * during the mount, that will get a complaint, so this is OK.
585 	 * add_cntr_files() for all units is done at mount from
586 	 * qibfs_fill_super(), so one way or another, everything works.
587 	 */
588 	if (qib_super == NULL)
589 		ret = 0;
590 	else
591 		ret = add_cntr_files(qib_super, dd);
592 	return ret;
593 }
594 
595 int qibfs_remove(struct qib_devdata *dd)
596 {
597 	int ret = 0;
598 
599 	if (qib_super)
600 		ret = remove_device_files(qib_super, dd);
601 
602 	return ret;
603 }
604 
605 static struct file_system_type qibfs_fs_type = {
606 	.owner =        THIS_MODULE,
607 	.name =         "ipathfs",
608 	.mount =        qibfs_mount,
609 	.kill_sb =      qibfs_kill_super,
610 };
611 MODULE_ALIAS_FS("ipathfs");
612 
613 int __init qib_init_qibfs(void)
614 {
615 	return register_filesystem(&qibfs_fs_type);
616 }
617 
618 int __exit qib_exit_qibfs(void)
619 {
620 	return unregister_filesystem(&qibfs_fs_type);
621 }
622