162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci * Copyright (c) 2004 Topspin Communications.  All rights reserved.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * This software is available to you under a choice of one of two
562306a36Sopenharmony_ci * licenses.  You may choose to be licensed under the terms of the GNU
662306a36Sopenharmony_ci * General Public License (GPL) Version 2, available from the file
762306a36Sopenharmony_ci * COPYING in the main directory of this source tree, or the
862306a36Sopenharmony_ci * OpenIB.org BSD license below:
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci *     Redistribution and use in source and binary forms, with or
1162306a36Sopenharmony_ci *     without modification, are permitted provided that the following
1262306a36Sopenharmony_ci *     conditions are met:
1362306a36Sopenharmony_ci *
1462306a36Sopenharmony_ci *      - Redistributions of source code must retain the above
1562306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
1662306a36Sopenharmony_ci *        disclaimer.
1762306a36Sopenharmony_ci *
1862306a36Sopenharmony_ci *      - Redistributions in binary form must reproduce the above
1962306a36Sopenharmony_ci *        copyright notice, this list of conditions and the following
2062306a36Sopenharmony_ci *        disclaimer in the documentation and/or other materials
2162306a36Sopenharmony_ci *        provided with the distribution.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2462306a36Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2562306a36Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2662306a36Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
2762306a36Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
2862306a36Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
2962306a36Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3062306a36Sopenharmony_ci * SOFTWARE.
3162306a36Sopenharmony_ci */
3262306a36Sopenharmony_ci
3362306a36Sopenharmony_ci#include <linux/err.h>
3462306a36Sopenharmony_ci#include <linux/seq_file.h>
3562306a36Sopenharmony_ci#include <linux/slab.h>
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_cistruct file_operations;
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#include <linux/debugfs.h>
4062306a36Sopenharmony_ci#include <linux/export.h>
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci#include "ipoib.h"
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_cistatic struct dentry *ipoib_root;
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic void format_gid(union ib_gid *gid, char *buf)
4762306a36Sopenharmony_ci{
4862306a36Sopenharmony_ci	int i, n;
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci	for (n = 0, i = 0; i < 8; ++i) {
5162306a36Sopenharmony_ci		n += sprintf(buf + n, "%x",
5262306a36Sopenharmony_ci			     be16_to_cpu(((__be16 *) gid->raw)[i]));
5362306a36Sopenharmony_ci		if (i < 7)
5462306a36Sopenharmony_ci			buf[n++] = ':';
5562306a36Sopenharmony_ci	}
5662306a36Sopenharmony_ci}
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_cistatic void *ipoib_mcg_seq_start(struct seq_file *file, loff_t *pos)
5962306a36Sopenharmony_ci{
6062306a36Sopenharmony_ci	struct ipoib_mcast_iter *iter;
6162306a36Sopenharmony_ci	loff_t n = *pos;
6262306a36Sopenharmony_ci
6362306a36Sopenharmony_ci	iter = ipoib_mcast_iter_init(file->private);
6462306a36Sopenharmony_ci	if (!iter)
6562306a36Sopenharmony_ci		return NULL;
6662306a36Sopenharmony_ci
6762306a36Sopenharmony_ci	while (n--) {
6862306a36Sopenharmony_ci		if (ipoib_mcast_iter_next(iter)) {
6962306a36Sopenharmony_ci			kfree(iter);
7062306a36Sopenharmony_ci			return NULL;
7162306a36Sopenharmony_ci		}
7262306a36Sopenharmony_ci	}
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci	return iter;
7562306a36Sopenharmony_ci}
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_cistatic void *ipoib_mcg_seq_next(struct seq_file *file, void *iter_ptr,
7862306a36Sopenharmony_ci				   loff_t *pos)
7962306a36Sopenharmony_ci{
8062306a36Sopenharmony_ci	struct ipoib_mcast_iter *iter = iter_ptr;
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci	(*pos)++;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	if (ipoib_mcast_iter_next(iter)) {
8562306a36Sopenharmony_ci		kfree(iter);
8662306a36Sopenharmony_ci		return NULL;
8762306a36Sopenharmony_ci	}
8862306a36Sopenharmony_ci
8962306a36Sopenharmony_ci	return iter;
9062306a36Sopenharmony_ci}
9162306a36Sopenharmony_ci
9262306a36Sopenharmony_cistatic void ipoib_mcg_seq_stop(struct seq_file *file, void *iter_ptr)
9362306a36Sopenharmony_ci{
9462306a36Sopenharmony_ci	/* nothing for now */
9562306a36Sopenharmony_ci}
9662306a36Sopenharmony_ci
9762306a36Sopenharmony_cistatic int ipoib_mcg_seq_show(struct seq_file *file, void *iter_ptr)
9862306a36Sopenharmony_ci{
9962306a36Sopenharmony_ci	struct ipoib_mcast_iter *iter = iter_ptr;
10062306a36Sopenharmony_ci	char gid_buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
10162306a36Sopenharmony_ci	union ib_gid mgid;
10262306a36Sopenharmony_ci	unsigned long created;
10362306a36Sopenharmony_ci	unsigned int queuelen, complete, send_only;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	if (!iter)
10662306a36Sopenharmony_ci		return 0;
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci	ipoib_mcast_iter_read(iter, &mgid, &created, &queuelen,
10962306a36Sopenharmony_ci			      &complete, &send_only);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci	format_gid(&mgid, gid_buf);
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci	seq_printf(file,
11462306a36Sopenharmony_ci		   "GID: %s\n"
11562306a36Sopenharmony_ci		   "  created: %10ld\n"
11662306a36Sopenharmony_ci		   "  queuelen: %9d\n"
11762306a36Sopenharmony_ci		   "  complete: %9s\n"
11862306a36Sopenharmony_ci		   "  send_only: %8s\n"
11962306a36Sopenharmony_ci		   "\n",
12062306a36Sopenharmony_ci		   gid_buf, created, queuelen,
12162306a36Sopenharmony_ci		   complete ? "yes" : "no",
12262306a36Sopenharmony_ci		   send_only ? "yes" : "no");
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_ci	return 0;
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_cistatic const struct seq_operations ipoib_mcg_sops = {
12862306a36Sopenharmony_ci	.start = ipoib_mcg_seq_start,
12962306a36Sopenharmony_ci	.next  = ipoib_mcg_seq_next,
13062306a36Sopenharmony_ci	.stop  = ipoib_mcg_seq_stop,
13162306a36Sopenharmony_ci	.show  = ipoib_mcg_seq_show,
13262306a36Sopenharmony_ci};
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ciDEFINE_SEQ_ATTRIBUTE(ipoib_mcg);
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_cistatic void *ipoib_path_seq_start(struct seq_file *file, loff_t *pos)
13762306a36Sopenharmony_ci{
13862306a36Sopenharmony_ci	struct ipoib_path_iter *iter;
13962306a36Sopenharmony_ci	loff_t n = *pos;
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci	iter = ipoib_path_iter_init(file->private);
14262306a36Sopenharmony_ci	if (!iter)
14362306a36Sopenharmony_ci		return NULL;
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	while (n--) {
14662306a36Sopenharmony_ci		if (ipoib_path_iter_next(iter)) {
14762306a36Sopenharmony_ci			kfree(iter);
14862306a36Sopenharmony_ci			return NULL;
14962306a36Sopenharmony_ci		}
15062306a36Sopenharmony_ci	}
15162306a36Sopenharmony_ci
15262306a36Sopenharmony_ci	return iter;
15362306a36Sopenharmony_ci}
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_cistatic void *ipoib_path_seq_next(struct seq_file *file, void *iter_ptr,
15662306a36Sopenharmony_ci				   loff_t *pos)
15762306a36Sopenharmony_ci{
15862306a36Sopenharmony_ci	struct ipoib_path_iter *iter = iter_ptr;
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci	(*pos)++;
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_ci	if (ipoib_path_iter_next(iter)) {
16362306a36Sopenharmony_ci		kfree(iter);
16462306a36Sopenharmony_ci		return NULL;
16562306a36Sopenharmony_ci	}
16662306a36Sopenharmony_ci
16762306a36Sopenharmony_ci	return iter;
16862306a36Sopenharmony_ci}
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_cistatic void ipoib_path_seq_stop(struct seq_file *file, void *iter_ptr)
17162306a36Sopenharmony_ci{
17262306a36Sopenharmony_ci	/* nothing for now */
17362306a36Sopenharmony_ci}
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_cistatic int ipoib_path_seq_show(struct seq_file *file, void *iter_ptr)
17662306a36Sopenharmony_ci{
17762306a36Sopenharmony_ci	struct ipoib_path_iter *iter = iter_ptr;
17862306a36Sopenharmony_ci	char gid_buf[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"];
17962306a36Sopenharmony_ci	struct ipoib_path path;
18062306a36Sopenharmony_ci	int rate;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	if (!iter)
18362306a36Sopenharmony_ci		return 0;
18462306a36Sopenharmony_ci
18562306a36Sopenharmony_ci	ipoib_path_iter_read(iter, &path);
18662306a36Sopenharmony_ci
18762306a36Sopenharmony_ci	format_gid(&path.pathrec.dgid, gid_buf);
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	seq_printf(file,
19062306a36Sopenharmony_ci		   "GID: %s\n"
19162306a36Sopenharmony_ci		   "  complete: %6s\n",
19262306a36Sopenharmony_ci		   gid_buf, sa_path_get_dlid(&path.pathrec) ? "yes" : "no");
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	if (sa_path_get_dlid(&path.pathrec)) {
19562306a36Sopenharmony_ci		rate = ib_rate_to_mbps(path.pathrec.rate);
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci		seq_printf(file,
19862306a36Sopenharmony_ci			   "  DLID:     0x%04x\n"
19962306a36Sopenharmony_ci			   "  SL: %12d\n"
20062306a36Sopenharmony_ci			   "  rate: %8d.%d Gb/sec\n",
20162306a36Sopenharmony_ci			   be32_to_cpu(sa_path_get_dlid(&path.pathrec)),
20262306a36Sopenharmony_ci			   path.pathrec.sl,
20362306a36Sopenharmony_ci			   rate / 1000, rate % 1000);
20462306a36Sopenharmony_ci	}
20562306a36Sopenharmony_ci
20662306a36Sopenharmony_ci	seq_putc(file, '\n');
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	return 0;
20962306a36Sopenharmony_ci}
21062306a36Sopenharmony_ci
21162306a36Sopenharmony_cistatic const struct seq_operations ipoib_path_sops = {
21262306a36Sopenharmony_ci	.start = ipoib_path_seq_start,
21362306a36Sopenharmony_ci	.next  = ipoib_path_seq_next,
21462306a36Sopenharmony_ci	.stop  = ipoib_path_seq_stop,
21562306a36Sopenharmony_ci	.show  = ipoib_path_seq_show,
21662306a36Sopenharmony_ci};
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ciDEFINE_SEQ_ATTRIBUTE(ipoib_path);
21962306a36Sopenharmony_ci
22062306a36Sopenharmony_civoid ipoib_create_debug_files(struct net_device *dev)
22162306a36Sopenharmony_ci{
22262306a36Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
22362306a36Sopenharmony_ci	char name[IFNAMSIZ + sizeof("_path")];
22462306a36Sopenharmony_ci
22562306a36Sopenharmony_ci	snprintf(name, sizeof(name), "%s_mcg", dev->name);
22662306a36Sopenharmony_ci	priv->mcg_dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
22762306a36Sopenharmony_ci					       ipoib_root, dev, &ipoib_mcg_fops);
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci	snprintf(name, sizeof(name), "%s_path", dev->name);
23062306a36Sopenharmony_ci	priv->path_dentry = debugfs_create_file(name, S_IFREG | S_IRUGO,
23162306a36Sopenharmony_ci						ipoib_root, dev, &ipoib_path_fops);
23262306a36Sopenharmony_ci}
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_civoid ipoib_delete_debug_files(struct net_device *dev)
23562306a36Sopenharmony_ci{
23662306a36Sopenharmony_ci	struct ipoib_dev_priv *priv = ipoib_priv(dev);
23762306a36Sopenharmony_ci
23862306a36Sopenharmony_ci	debugfs_remove(priv->mcg_dentry);
23962306a36Sopenharmony_ci	debugfs_remove(priv->path_dentry);
24062306a36Sopenharmony_ci	priv->mcg_dentry = priv->path_dentry = NULL;
24162306a36Sopenharmony_ci}
24262306a36Sopenharmony_ci
24362306a36Sopenharmony_civoid ipoib_register_debugfs(void)
24462306a36Sopenharmony_ci{
24562306a36Sopenharmony_ci	ipoib_root = debugfs_create_dir("ipoib", NULL);
24662306a36Sopenharmony_ci}
24762306a36Sopenharmony_ci
24862306a36Sopenharmony_civoid ipoib_unregister_debugfs(void)
24962306a36Sopenharmony_ci{
25062306a36Sopenharmony_ci	debugfs_remove(ipoib_root);
25162306a36Sopenharmony_ci}
252