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/fs_context.h>
38 #include <linux/mount.h>
39 #include <linux/pagemap.h>
40 #include <linux/init.h>
41 #include <linux/namei.h>
42 
43 #include "qib.h"
44 
45 #define QIBFS_MAGIC 0x726a77
46 
47 static struct super_block *qib_super;
48 
49 #define private2dd(file) (file_inode(file)->i_private)
50 
qibfs_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,const struct file_operations * fops,void * data)51 static int qibfs_mknod(struct inode *dir, struct dentry *dentry,
52 		       umode_t mode, const struct file_operations *fops,
53 		       void *data)
54 {
55 	int error;
56 	struct inode *inode = new_inode(dir->i_sb);
57 
58 	if (!inode) {
59 		error = -EPERM;
60 		goto bail;
61 	}
62 
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);
76 	}
77 
78 	inode->i_fop = fops;
79 
80 	d_instantiate(dentry, inode);
81 	error = 0;
82 
83 bail:
84 	return error;
85 }
86 
create_file(const char * name,umode_t mode,struct dentry * parent,struct dentry ** dentry,const struct file_operations * fops,void * data)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)
90 {
91 	int error;
92 
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));
101 
102 	return error;
103 }
104 
driver_stats_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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 
driver_names_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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 */
dev_counters_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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 */
dev_names_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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) */
portnames_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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) */
portcntrs_1_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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) */
portcntrs_2_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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  */
qsfp_1_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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  */
qsfp_2_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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 
flash_read(struct file * file,char __user * buf,size_t count,loff_t * ppos)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 
flash_write(struct file * file,const char __user * buf,size_t count,loff_t * ppos)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 || count != sizeof(struct qib_flash))
333 		return -EINVAL;
334 
335 	tmp = memdup_user(buf, count);
336 	if (IS_ERR(tmp))
337 		return PTR_ERR(tmp);
338 
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;
344 	}
345 
346 	*ppos = pos + count;
347 	ret = count;
348 
349 bail_tmp:
350 	kfree(tmp);
351 	return ret;
352 }
353 
354 static const struct file_operations flash_ops = {
355 	.read = flash_read,
356 	.write = flash_write,
357 	.llseek = default_llseek,
358 };
359 
add_cntr_files(struct super_block * sb,struct qib_devdata * dd)360 static int add_cntr_files(struct super_block *sb, struct qib_devdata *dd)
361 {
362 	struct dentry *dir, *tmp;
363 	char unit[10];
364 	int ret, i;
365 
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;
373 	}
374 
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;
382 	}
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;
389 	}
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;
396 	}
397 	for (i = 1; i <= dd->num_pports; i++) {
398 		char fname[24];
399 
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;
408 		}
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;
418 		}
419 	}
420 
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;
428 }
429 
remove_file(struct dentry * parent,char * name)430 static int remove_file(struct dentry *parent, char *name)
431 {
432 	struct dentry *tmp;
433 	int ret;
434 
435 	tmp = lookup_one_len(name, parent, strlen(name));
436 
437 	if (IS_ERR(tmp)) {
438 		ret = PTR_ERR(tmp);
439 		goto bail;
440 	}
441 
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);
449 	}
450 	dput(tmp);
451 
452 	ret = 0;
453 bail:
454 	/*
455 	 * We don't expect clients to care about the return value, but
456 	 * it's there if they need it.
457 	 */
458 	return ret;
459 }
460 
remove_device_files(struct super_block * sb,struct qib_devdata * dd)461 static int remove_device_files(struct super_block *sb,
462 			       struct qib_devdata *dd)
463 {
464 	struct dentry *dir, *root;
465 	char unit[10];
466 	int ret, i;
467 
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));
472 
473 	if (IS_ERR(dir)) {
474 		ret = PTR_ERR(dir);
475 		pr_err("Lookup of %s failed\n", unit);
476 		goto bail;
477 	}
478 
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];
485 
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);
491 		}
492 	}
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);
498 
499 bail:
500 	inode_unlock(d_inode(root));
501 	dput(root);
502 	return ret;
503 }
504 
505 /*
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.
509  */
qibfs_fill_super(struct super_block * sb,struct fs_context * fc)510 static int qibfs_fill_super(struct super_block *sb, struct fs_context *fc)
511 {
512 	struct qib_devdata *dd;
513 	unsigned long index;
514 	int ret;
515 
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 		{""},
520 	};
521 
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;
526 	}
527 
528 	xa_for_each(&qib_dev_table, index, dd) {
529 		ret = add_cntr_files(sb, dd);
530 		if (ret)
531 			goto bail;
532 	}
533 
534 bail:
535 	return ret;
536 }
537 
qibfs_get_tree(struct fs_context * fc)538 static int qibfs_get_tree(struct fs_context *fc)
539 {
540 	int ret = get_tree_single(fc, qibfs_fill_super);
541 	if (ret == 0)
542 		qib_super = fc->root->d_sb;
543 	return ret;
544 }
545 
546 static const struct fs_context_operations qibfs_context_ops = {
547 	.get_tree	= qibfs_get_tree,
548 };
549 
qibfs_init_fs_context(struct fs_context * fc)550 static int qibfs_init_fs_context(struct fs_context *fc)
551 {
552 	fc->ops = &qibfs_context_ops;
553 	return 0;
554 }
555 
qibfs_kill_super(struct super_block * s)556 static void qibfs_kill_super(struct super_block *s)
557 {
558 	kill_litter_super(s);
559 	qib_super = NULL;
560 }
561 
qibfs_add(struct qib_devdata * dd)562 int qibfs_add(struct qib_devdata *dd)
563 {
564 	int ret;
565 
566 	/*
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.
573 	 */
574 	if (qib_super == NULL)
575 		ret = 0;
576 	else
577 		ret = add_cntr_files(qib_super, dd);
578 	return ret;
579 }
580 
qibfs_remove(struct qib_devdata * dd)581 int qibfs_remove(struct qib_devdata *dd)
582 {
583 	int ret = 0;
584 
585 	if (qib_super)
586 		ret = remove_device_files(qib_super, dd);
587 
588 	return ret;
589 }
590 
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,
596 };
597 MODULE_ALIAS_FS("ipathfs");
598 
qib_init_qibfs(void)599 int __init qib_init_qibfs(void)
600 {
601 	return register_filesystem(&qibfs_fs_type);
602 }
603 
qib_exit_qibfs(void)604 int __exit qib_exit_qibfs(void)
605 {
606 	return unregister_filesystem(&qibfs_fs_type);
607 }
608