sysfs.c 36.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3 4
 * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "core_priv.h"

37
#include <linux/slab.h>
38
#include <linux/stat.h>
39
#include <linux/string.h>
M
Matan Barak 已提交
40
#include <linux/netdevice.h>
41
#include <linux/ethtool.h>
42

43
#include <rdma/ib_mad.h>
44
#include <rdma/ib_pma.h>
45
#include <rdma/ib_cache.h>
L
Linus Torvalds 已提交
46

M
Matan Barak 已提交
47 48 49 50 51 52 53 54
struct ib_port;

struct gid_attr_group {
	struct ib_port		*port;
	struct kobject		kobj;
	struct attribute_group	ndev;
	struct attribute_group	type;
};
L
Linus Torvalds 已提交
55 56 57
struct ib_port {
	struct kobject         kobj;
	struct ib_device      *ibdev;
M
Matan Barak 已提交
58
	struct gid_attr_group *gid_attr_group;
L
Linus Torvalds 已提交
59 60
	struct attribute_group gid_group;
	struct attribute_group pkey_group;
61
	struct attribute_group *pma_table;
62 63 64
	struct attribute_group *hw_stats_ag;
	struct rdma_hw_stats   *hw_stats;
	u8                     port_num;
L
Linus Torvalds 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
};

struct port_attribute {
	struct attribute attr;
	ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
	ssize_t (*store)(struct ib_port *, struct port_attribute *,
			 const char *buf, size_t count);
};

#define PORT_ATTR(_name, _mode, _show, _store) \
struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)

#define PORT_ATTR_RO(_name) \
struct port_attribute port_attr_##_name = __ATTR_RO(_name)

struct port_table_attribute {
81 82 83
	struct port_attribute	attr;
	char			name[8];
	int			index;
84
	__be16			attr_id;
L
Linus Torvalds 已提交
85 86
};

87 88 89 90 91 92 93 94 95 96 97 98
struct hw_stats_attribute {
	struct attribute	attr;
	ssize_t			(*show)(struct kobject *kobj,
					struct attribute *attr, char *buf);
	ssize_t			(*store)(struct kobject *kobj,
					 struct attribute *attr,
					 const char *buf,
					 size_t count);
	int			index;
	u8			port_num;
};

L
Linus Torvalds 已提交
99 100 101 102 103 104 105 106
static ssize_t port_attr_show(struct kobject *kobj,
			      struct attribute *attr, char *buf)
{
	struct port_attribute *port_attr =
		container_of(attr, struct port_attribute, attr);
	struct ib_port *p = container_of(kobj, struct ib_port, kobj);

	if (!port_attr->show)
107
		return -EIO;
L
Linus Torvalds 已提交
108 109 110 111

	return port_attr->show(p, port_attr, buf);
}

112 113 114 115 116 117 118 119 120 121 122 123 124
static ssize_t port_attr_store(struct kobject *kobj,
			       struct attribute *attr,
			       const char *buf, size_t count)
{
	struct port_attribute *port_attr =
		container_of(attr, struct port_attribute, attr);
	struct ib_port *p = container_of(kobj, struct ib_port, kobj);

	if (!port_attr->store)
		return -EIO;
	return port_attr->store(p, port_attr, buf, count);
}

125
static const struct sysfs_ops port_sysfs_ops = {
126 127
	.show	= port_attr_show,
	.store	= port_attr_store
L
Linus Torvalds 已提交
128 129
};

M
Matan Barak 已提交
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
static ssize_t gid_attr_show(struct kobject *kobj,
			     struct attribute *attr, char *buf)
{
	struct port_attribute *port_attr =
		container_of(attr, struct port_attribute, attr);
	struct ib_port *p = container_of(kobj, struct gid_attr_group,
					 kobj)->port;

	if (!port_attr->show)
		return -EIO;

	return port_attr->show(p, port_attr, buf);
}

static const struct sysfs_ops gid_attr_sysfs_ops = {
	.show = gid_attr_show
};

L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
			  char *buf)
{
	struct ib_port_attr attr;
	ssize_t ret;

	static const char *state_name[] = {
		[IB_PORT_NOP]		= "NOP",
		[IB_PORT_DOWN]		= "DOWN",
		[IB_PORT_INIT]		= "INIT",
		[IB_PORT_ARMED]		= "ARMED",
		[IB_PORT_ACTIVE]	= "ACTIVE",
		[IB_PORT_ACTIVE_DEFER]	= "ACTIVE_DEFER"
	};

	ret = ib_query_port(p->ibdev, p->port_num, &attr);
	if (ret)
		return ret;

	return sprintf(buf, "%d: %s\n", attr.state,
R
Roland Dreier 已提交
168
		       attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
L
Linus Torvalds 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
		       state_name[attr.state] : "UNKNOWN");
}

static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
			char *buf)
{
	struct ib_port_attr attr;
	ssize_t ret;

	ret = ib_query_port(p->ibdev, p->port_num, &attr);
	if (ret)
		return ret;

	return sprintf(buf, "0x%x\n", attr.lid);
}

static ssize_t lid_mask_count_show(struct ib_port *p,
				   struct port_attribute *unused,
				   char *buf)
{
	struct ib_port_attr attr;
	ssize_t ret;

	ret = ib_query_port(p->ibdev, p->port_num, &attr);
	if (ret)
		return ret;

	return sprintf(buf, "%d\n", attr.lmc);
}

static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
			   char *buf)
{
	struct ib_port_attr attr;
	ssize_t ret;

	ret = ib_query_port(p->ibdev, p->port_num, &attr);
	if (ret)
		return ret;

	return sprintf(buf, "0x%x\n", attr.sm_lid);
}

static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
			  char *buf)
{
	struct ib_port_attr attr;
	ssize_t ret;

	ret = ib_query_port(p->ibdev, p->port_num, &attr);
	if (ret)
		return ret;

	return sprintf(buf, "%d\n", attr.sm_sl);
}

static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
			     char *buf)
{
	struct ib_port_attr attr;
	ssize_t ret;

	ret = ib_query_port(p->ibdev, p->port_num, &attr);
	if (ret)
		return ret;

	return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
}

static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
			 char *buf)
{
	struct ib_port_attr attr;
	char *speed = "";
243
	int rate;		/* in deci-Gb/sec */
L
Linus Torvalds 已提交
244 245 246 247 248 249 250
	ssize_t ret;

	ret = ib_query_port(p->ibdev, p->port_num, &attr);
	if (ret)
		return ret;

	switch (attr.active_speed) {
251
	case IB_SPEED_DDR:
252
		speed = " DDR";
R
Roland Dreier 已提交
253
		rate = 50;
254
		break;
255
	case IB_SPEED_QDR:
256
		speed = " QDR";
R
Roland Dreier 已提交
257
		rate = 100;
258
		break;
259
	case IB_SPEED_FDR10:
260
		speed = " FDR10";
R
Roland Dreier 已提交
261
		rate = 100;
262
		break;
263
	case IB_SPEED_FDR:
264
		speed = " FDR";
R
Roland Dreier 已提交
265
		rate = 140;
266
		break;
267
	case IB_SPEED_EDR:
268
		speed = " EDR";
R
Roland Dreier 已提交
269
		rate = 250;
270
		break;
N
Noa Osherovich 已提交
271 272 273 274
	case IB_SPEED_HDR:
		speed = " HDR";
		rate = 500;
		break;
275 276
	case IB_SPEED_SDR:
	default:		/* default to SDR for invalid rates */
277
		speed = " SDR";
278 279
		rate = 25;
		break;
L
Linus Torvalds 已提交
280 281
	}

282
	rate *= ib_width_enum_to_int(attr.active_width);
L
Linus Torvalds 已提交
283 284 285 286
	if (rate < 0)
		return -EINVAL;

	return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
R
Roland Dreier 已提交
287
		       rate / 10, rate % 10 ? ".5" : "",
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
		       ib_width_enum_to_int(attr.active_width), speed);
}

static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
			       char *buf)
{
	struct ib_port_attr attr;

	ssize_t ret;

	ret = ib_query_port(p->ibdev, p->port_num, &attr);
	if (ret)
		return ret;

	switch (attr.phys_state) {
	case 1:  return sprintf(buf, "1: Sleep\n");
	case 2:  return sprintf(buf, "2: Polling\n");
	case 3:  return sprintf(buf, "3: Disabled\n");
	case 4:  return sprintf(buf, "4: PortConfigurationTraining\n");
	case 5:  return sprintf(buf, "5: LinkUp\n");
	case 6:  return sprintf(buf, "6: LinkErrorRecovery\n");
	case 7:  return sprintf(buf, "7: Phy Test\n");
	default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
	}
}

314 315 316 317 318 319 320 321 322 323 324 325 326
static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
			       char *buf)
{
	switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
	case IB_LINK_LAYER_INFINIBAND:
		return sprintf(buf, "%s\n", "InfiniBand");
	case IB_LINK_LAYER_ETHERNET:
		return sprintf(buf, "%s\n", "Ethernet");
	default:
		return sprintf(buf, "%s\n", "Unknown");
	}
}

L
Linus Torvalds 已提交
327 328 329 330 331 332 333 334
static PORT_ATTR_RO(state);
static PORT_ATTR_RO(lid);
static PORT_ATTR_RO(lid_mask_count);
static PORT_ATTR_RO(sm_lid);
static PORT_ATTR_RO(sm_sl);
static PORT_ATTR_RO(cap_mask);
static PORT_ATTR_RO(rate);
static PORT_ATTR_RO(phys_state);
335
static PORT_ATTR_RO(link_layer);
L
Linus Torvalds 已提交
336 337 338 339 340 341 342 343 344 345

static struct attribute *port_default_attrs[] = {
	&port_attr_state.attr,
	&port_attr_lid.attr,
	&port_attr_lid_mask_count.attr,
	&port_attr_sm_lid.attr,
	&port_attr_sm_sl.attr,
	&port_attr_cap_mask.attr,
	&port_attr_rate.attr,
	&port_attr_phys_state.attr,
346
	&port_attr_link_layer.attr,
L
Linus Torvalds 已提交
347 348 349
	NULL
};

350
static size_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
M
Matan Barak 已提交
351
{
352 353 354 355 356 357 358 359 360
	struct net_device *ndev;
	size_t ret = -EINVAL;

	rcu_read_lock();
	ndev = rcu_dereference(gid_attr->ndev);
	if (ndev)
		ret = sprintf(buf, "%s\n", ndev->name);
	rcu_read_unlock();
	return ret;
M
Matan Barak 已提交
361 362
}

363
static size_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
M
Matan Barak 已提交
364 365 366 367
{
	return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
}

368 369 370
static ssize_t _show_port_gid_attr(
	struct ib_port *p, struct port_attribute *attr, char *buf,
	size_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
M
Matan Barak 已提交
371 372 373
{
	struct port_table_attribute *tab_attr =
		container_of(attr, struct port_table_attribute, attr);
374
	const struct ib_gid_attr *gid_attr;
M
Matan Barak 已提交
375 376
	ssize_t ret;

377 378 379
	gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
	if (IS_ERR(gid_attr))
		return PTR_ERR(gid_attr);
M
Matan Barak 已提交
380

381 382
	ret = print(gid_attr, buf);
	rdma_put_gid_attr(gid_attr);
M
Matan Barak 已提交
383 384 385
	return ret;
}

L
Linus Torvalds 已提交
386 387 388 389 390
static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
			     char *buf)
{
	struct port_table_attribute *tab_attr =
		container_of(attr, struct port_table_attribute, attr);
391
	const struct ib_gid_attr *gid_attr;
L
Linus Torvalds 已提交
392 393
	ssize_t ret;

394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
	gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
	if (IS_ERR(gid_attr)) {
		const union ib_gid zgid = {};

		/* If reading GID fails, it is likely due to GID entry being
		 * empty (invalid) or reserved GID in the table.  User space
		 * expects to read GID table entries as long as it given index
		 * is within GID table size.  Administrative/debugging tool
		 * fails to query rest of the GID entries if it hits error
		 * while querying a GID of the given index.  To avoid user
		 * space throwing such error on fail to read gid, return zero
		 * GID as before. This maintains backward compatibility.
		 */
		return sprintf(buf, "%pI6\n", zgid.raw);
	}

	ret = sprintf(buf, "%pI6\n", gid_attr->gid.raw);
	rdma_put_gid_attr(gid_attr);
	return ret;
L
Linus Torvalds 已提交
413 414
}

M
Matan Barak 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427
static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
				       struct port_attribute *attr, char *buf)
{
	return _show_port_gid_attr(p, attr, buf, print_ndev);
}

static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
					   struct port_attribute *attr,
					   char *buf)
{
	return _show_port_gid_attr(p, attr, buf, print_gid_type);
}

L
Linus Torvalds 已提交
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
			      char *buf)
{
	struct port_table_attribute *tab_attr =
		container_of(attr, struct port_table_attribute, attr);
	u16 pkey;
	ssize_t ret;

	ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
	if (ret)
		return ret;

	return sprintf(buf, "0x%04x\n", pkey);
}

#define PORT_PMA_ATTR(_name, _counter, _width, _offset)			\
struct port_table_attribute port_pma_attr_##_name = {			\
	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
446 447
	.index = (_offset) | ((_width) << 16) | ((_counter) << 24),	\
	.attr_id = IB_PMA_PORT_COUNTERS ,				\
L
Linus Torvalds 已提交
448 449
}

450 451 452 453 454 455 456
#define PORT_PMA_ATTR_EXT(_name, _width, _offset)			\
struct port_table_attribute port_pma_attr_ext_##_name = {		\
	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
	.index = (_offset) | ((_width) << 16),				\
	.attr_id = IB_PMA_PORT_COUNTERS_EXT ,				\
}

457 458 459 460
/*
 * Get a Perfmgmt MAD block of data.
 * Returns error code or the number of bytes retrieved.
 */
461
static int get_perf_mad(struct ib_device *dev, int port_num, __be16 attr,
462
		void *data, int offset, size_t size)
L
Linus Torvalds 已提交
463
{
464 465
	struct ib_mad *in_mad;
	struct ib_mad *out_mad;
466 467
	size_t mad_size = sizeof(*out_mad);
	u16 out_mad_pkey_index = 0;
L
Linus Torvalds 已提交
468 469
	ssize_t ret;

K
Kamal Heib 已提交
470
	if (!dev->ops.process_mad)
471
		return -ENOSYS;
L
Linus Torvalds 已提交
472

R
Roland Dreier 已提交
473
	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
474
	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
L
Linus Torvalds 已提交
475 476 477 478 479 480 481 482 483
	if (!in_mad || !out_mad) {
		ret = -ENOMEM;
		goto out;
	}

	in_mad->mad_hdr.base_version  = 1;
	in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
	in_mad->mad_hdr.class_version = 1;
	in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
484
	in_mad->mad_hdr.attr_id       = attr;
L
Linus Torvalds 已提交
485

486 487
	if (attr != IB_PMA_CLASS_PORT_INFO)
		in_mad->data[41] = port_num;	/* PortSelect field */
L
Linus Torvalds 已提交
488

K
Kamal Heib 已提交
489 490 491 492 493
	if ((dev->ops.process_mad(dev, IB_MAD_IGNORE_MKEY,
				  port_num, NULL, NULL,
				  (const struct ib_mad_hdr *)in_mad, mad_size,
				  (struct ib_mad_hdr *)out_mad, &mad_size,
				  &out_mad_pkey_index) &
L
Linus Torvalds 已提交
494 495 496 497 498
	     (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
	    (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
		ret = -EINVAL;
		goto out;
	}
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
	memcpy(data, out_mad->data + offset, size);
	ret = size;
out:
	kfree(in_mad);
	kfree(out_mad);
	return ret;
}

static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
				char *buf)
{
	struct port_table_attribute *tab_attr =
		container_of(attr, struct port_table_attribute, attr);
	int offset = tab_attr->index & 0xffff;
	int width  = (tab_attr->index >> 16) & 0xff;
	ssize_t ret;
	u8 data[8];

517
	ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
518 519
			40 + offset / 8, sizeof(data));
	if (ret < 0)
520
		return ret;
L
Linus Torvalds 已提交
521 522 523

	switch (width) {
	case 4:
524
		ret = sprintf(buf, "%u\n", (*data >>
525
					    (4 - (offset % 8))) & 0xf);
L
Linus Torvalds 已提交
526 527
		break;
	case 8:
528
		ret = sprintf(buf, "%u\n", *data);
L
Linus Torvalds 已提交
529 530 531
		break;
	case 16:
		ret = sprintf(buf, "%u\n",
532
			      be16_to_cpup((__be16 *)data));
L
Linus Torvalds 已提交
533 534 535
		break;
	case 32:
		ret = sprintf(buf, "%u\n",
536
			      be32_to_cpup((__be32 *)data));
L
Linus Torvalds 已提交
537
		break;
538 539 540 541 542
	case 64:
		ret = sprintf(buf, "%llu\n",
				be64_to_cpup((__be64 *)data));
		break;

L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
	default:
		ret = 0;
	}

	return ret;
}

static PORT_PMA_ATTR(symbol_error		    ,  0, 16,  32);
static PORT_PMA_ATTR(link_error_recovery	    ,  1,  8,  48);
static PORT_PMA_ATTR(link_downed		    ,  2,  8,  56);
static PORT_PMA_ATTR(port_rcv_errors		    ,  3, 16,  64);
static PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
static PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
static PORT_PMA_ATTR(port_xmit_discards		    ,  6, 16, 112);
static PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
static PORT_PMA_ATTR(port_rcv_constraint_errors	    ,  8,  8, 136);
static PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
static PORT_PMA_ATTR(VL15_dropped		    , 11, 16, 176);
static PORT_PMA_ATTR(port_xmit_data		    , 12, 32, 192);
static PORT_PMA_ATTR(port_rcv_data		    , 13, 32, 224);
static PORT_PMA_ATTR(port_xmit_packets		    , 14, 32, 256);
static PORT_PMA_ATTR(port_rcv_packets		    , 15, 32, 288);
566
static PORT_PMA_ATTR(port_xmit_wait		    ,  0, 32, 320);
L
Linus Torvalds 已提交
567

568 569 570 571 572 573 574 575 576 577 578 579
/*
 * Counters added by extended set
 */
static PORT_PMA_ATTR_EXT(port_xmit_data		    , 64,  64);
static PORT_PMA_ATTR_EXT(port_rcv_data		    , 64, 128);
static PORT_PMA_ATTR_EXT(port_xmit_packets	    , 64, 192);
static PORT_PMA_ATTR_EXT(port_rcv_packets	    , 64, 256);
static PORT_PMA_ATTR_EXT(unicast_xmit_packets	    , 64, 320);
static PORT_PMA_ATTR_EXT(unicast_rcv_packets	    , 64, 384);
static PORT_PMA_ATTR_EXT(multicast_xmit_packets	    , 64, 448);
static PORT_PMA_ATTR_EXT(multicast_rcv_packets	    , 64, 512);

L
Linus Torvalds 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
static struct attribute *pma_attrs[] = {
	&port_pma_attr_symbol_error.attr.attr,
	&port_pma_attr_link_error_recovery.attr.attr,
	&port_pma_attr_link_downed.attr.attr,
	&port_pma_attr_port_rcv_errors.attr.attr,
	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
	&port_pma_attr_port_xmit_discards.attr.attr,
	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
	&port_pma_attr_local_link_integrity_errors.attr.attr,
	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
	&port_pma_attr_VL15_dropped.attr.attr,
	&port_pma_attr_port_xmit_data.attr.attr,
	&port_pma_attr_port_rcv_data.attr.attr,
	&port_pma_attr_port_xmit_packets.attr.attr,
	&port_pma_attr_port_rcv_packets.attr.attr,
597
	&port_pma_attr_port_xmit_wait.attr.attr,
L
Linus Torvalds 已提交
598 599 600
	NULL
};

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
static struct attribute *pma_attrs_ext[] = {
	&port_pma_attr_symbol_error.attr.attr,
	&port_pma_attr_link_error_recovery.attr.attr,
	&port_pma_attr_link_downed.attr.attr,
	&port_pma_attr_port_rcv_errors.attr.attr,
	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
	&port_pma_attr_port_xmit_discards.attr.attr,
	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
	&port_pma_attr_local_link_integrity_errors.attr.attr,
	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
	&port_pma_attr_VL15_dropped.attr.attr,
	&port_pma_attr_ext_port_xmit_data.attr.attr,
	&port_pma_attr_ext_port_rcv_data.attr.attr,
	&port_pma_attr_ext_port_xmit_packets.attr.attr,
617
	&port_pma_attr_port_xmit_wait.attr.attr,
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
	&port_pma_attr_ext_port_rcv_packets.attr.attr,
	&port_pma_attr_ext_unicast_rcv_packets.attr.attr,
	&port_pma_attr_ext_unicast_xmit_packets.attr.attr,
	&port_pma_attr_ext_multicast_rcv_packets.attr.attr,
	&port_pma_attr_ext_multicast_xmit_packets.attr.attr,
	NULL
};

static struct attribute *pma_attrs_noietf[] = {
	&port_pma_attr_symbol_error.attr.attr,
	&port_pma_attr_link_error_recovery.attr.attr,
	&port_pma_attr_link_downed.attr.attr,
	&port_pma_attr_port_rcv_errors.attr.attr,
	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
	&port_pma_attr_port_xmit_discards.attr.attr,
	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
	&port_pma_attr_local_link_integrity_errors.attr.attr,
	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
	&port_pma_attr_VL15_dropped.attr.attr,
	&port_pma_attr_ext_port_xmit_data.attr.attr,
	&port_pma_attr_ext_port_rcv_data.attr.attr,
	&port_pma_attr_ext_port_xmit_packets.attr.attr,
	&port_pma_attr_ext_port_rcv_packets.attr.attr,
643
	&port_pma_attr_port_xmit_wait.attr.attr,
644 645 646
	NULL
};

L
Linus Torvalds 已提交
647 648 649 650 651
static struct attribute_group pma_group = {
	.name  = "counters",
	.attrs  = pma_attrs
};

652 653 654 655 656 657 658 659 660 661
static struct attribute_group pma_group_ext = {
	.name  = "counters",
	.attrs  = pma_attrs_ext
};

static struct attribute_group pma_group_noietf = {
	.name  = "counters",
	.attrs  = pma_attrs_noietf
};

L
Linus Torvalds 已提交
662 663 664 665 666 667
static void ib_port_release(struct kobject *kobj)
{
	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
	struct attribute *a;
	int i;

668 669 670
	if (p->gid_group.attrs) {
		for (i = 0; (a = p->gid_group.attrs[i]); ++i)
			kfree(a);
L
Linus Torvalds 已提交
671

672 673
		kfree(p->gid_group.attrs);
	}
674

675 676 677
	if (p->pkey_group.attrs) {
		for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
			kfree(a);
L
Linus Torvalds 已提交
678

679 680
		kfree(p->pkey_group.attrs);
	}
681

L
Linus Torvalds 已提交
682 683 684
	kfree(p);
}

M
Matan Barak 已提交
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
static void ib_port_gid_attr_release(struct kobject *kobj)
{
	struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
						kobj);
	struct attribute *a;
	int i;

	if (g->ndev.attrs) {
		for (i = 0; (a = g->ndev.attrs[i]); ++i)
			kfree(a);

		kfree(g->ndev.attrs);
	}

	if (g->type.attrs) {
		for (i = 0; (a = g->type.attrs[i]); ++i)
			kfree(a);

		kfree(g->type.attrs);
	}

	kfree(g);
}

L
Linus Torvalds 已提交
709 710 711 712 713 714
static struct kobj_type port_type = {
	.release       = ib_port_release,
	.sysfs_ops     = &port_sysfs_ops,
	.default_attrs = port_default_attrs
};

M
Matan Barak 已提交
715 716 717 718 719
static struct kobj_type gid_attr_type = {
	.sysfs_ops      = &gid_attr_sysfs_ops,
	.release        = ib_port_gid_attr_release
};

720 721 722 723
static struct attribute **
alloc_group_attrs(ssize_t (*show)(struct ib_port *,
				  struct port_attribute *, char *buf),
		  int len)
L
Linus Torvalds 已提交
724
{
725 726
	struct attribute **tab_attr;
	struct port_table_attribute *element;
L
Linus Torvalds 已提交
727 728
	int i;

729 730 731
	tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
	if (!tab_attr)
		return NULL;
L
Linus Torvalds 已提交
732

733
	for (i = 0; i < len; i++) {
734
		element = kzalloc(sizeof(struct port_table_attribute),
735 736
				  GFP_KERNEL);
		if (!element)
L
Linus Torvalds 已提交
737 738
			goto err;

739
		if (snprintf(element->name, sizeof(element->name),
R
Roland Dreier 已提交
740 741
			     "%d", i) >= sizeof(element->name)) {
			kfree(element);
L
Linus Torvalds 已提交
742
			goto err;
R
Roland Dreier 已提交
743
		}
L
Linus Torvalds 已提交
744

745 746 747 748
		element->attr.attr.name  = element->name;
		element->attr.attr.mode  = S_IRUGO;
		element->attr.show       = show;
		element->index		 = i;
749
		sysfs_attr_init(&element->attr.attr);
L
Linus Torvalds 已提交
750

751
		tab_attr[i] = &element->attr.attr;
L
Linus Torvalds 已提交
752 753
	}

754
	return tab_attr;
L
Linus Torvalds 已提交
755

756 757 758 759 760
err:
	while (--i >= 0)
		kfree(tab_attr[i]);
	kfree(tab_attr);
	return NULL;
L
Linus Torvalds 已提交
761 762
}

763 764 765 766
/*
 * Figure out which counter table to use depending on
 * the device capabilities.
 */
767 768
static struct attribute_group *get_counter_table(struct ib_device *dev,
						 int port_num)
769 770 771
{
	struct ib_class_port_info cpi;

772
	if (get_perf_mad(dev, port_num, IB_PMA_CLASS_PORT_INFO,
773
				&cpi, 40, sizeof(cpi)) >= 0) {
774
		if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH)
775 776 777
			/* We have extended counters */
			return &pma_group_ext;

778
		if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH_NOIETF)
779 780 781 782 783 784 785 786
			/* But not the IETF ones */
			return &pma_group_noietf;
	}

	/* Fall back to normal counters */
	return &pma_group;
}

787 788 789 790 791 792 793
static int update_hw_stats(struct ib_device *dev, struct rdma_hw_stats *stats,
			   u8 port_num, int index)
{
	int ret;

	if (time_is_after_eq_jiffies(stats->timestamp + stats->lifespan))
		return 0;
K
Kamal Heib 已提交
794
	ret = dev->ops.get_hw_stats(dev, stats, port_num, index);
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826
	if (ret < 0)
		return ret;
	if (ret == stats->num_counters)
		stats->timestamp = jiffies;

	return 0;
}

static ssize_t print_hw_stat(struct rdma_hw_stats *stats, int index, char *buf)
{
	return sprintf(buf, "%llu\n", stats->value[index]);
}

static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr,
			     char *buf)
{
	struct ib_device *dev;
	struct ib_port *port;
	struct hw_stats_attribute *hsa;
	struct rdma_hw_stats *stats;
	int ret;

	hsa = container_of(attr, struct hw_stats_attribute, attr);
	if (!hsa->port_num) {
		dev = container_of((struct device *)kobj,
				   struct ib_device, dev);
		stats = dev->hw_stats;
	} else {
		port = container_of(kobj, struct ib_port, kobj);
		dev = port->ibdev;
		stats = port->hw_stats;
	}
827
	mutex_lock(&stats->lock);
828 829
	ret = update_hw_stats(dev, stats, hsa->port_num, hsa->index);
	if (ret)
830 831 832 833 834 835
		goto unlock;
	ret = print_hw_stat(stats, hsa->index, buf);
unlock:
	mutex_unlock(&stats->lock);

	return ret;
836 837 838 839 840 841 842
}

static ssize_t show_stats_lifespan(struct kobject *kobj,
				   struct attribute *attr,
				   char *buf)
{
	struct hw_stats_attribute *hsa;
843
	struct rdma_hw_stats *stats;
844 845 846 847 848 849
	int msecs;

	hsa = container_of(attr, struct hw_stats_attribute, attr);
	if (!hsa->port_num) {
		struct ib_device *dev = container_of((struct device *)kobj,
						     struct ib_device, dev);
850 851

		stats = dev->hw_stats;
852 853
	} else {
		struct ib_port *p = container_of(kobj, struct ib_port, kobj);
854 855

		stats = p->hw_stats;
856
	}
857 858 859 860 861

	mutex_lock(&stats->lock);
	msecs = jiffies_to_msecs(stats->lifespan);
	mutex_unlock(&stats->lock);

862 863 864 865 866 867 868 869
	return sprintf(buf, "%d\n", msecs);
}

static ssize_t set_stats_lifespan(struct kobject *kobj,
				  struct attribute *attr,
				  const char *buf, size_t count)
{
	struct hw_stats_attribute *hsa;
870
	struct rdma_hw_stats *stats;
871 872 873 874 875 876 877 878 879 880 881 882 883 884
	int msecs;
	int jiffies;
	int ret;

	ret = kstrtoint(buf, 10, &msecs);
	if (ret)
		return ret;
	if (msecs < 0 || msecs > 10000)
		return -EINVAL;
	jiffies = msecs_to_jiffies(msecs);
	hsa = container_of(attr, struct hw_stats_attribute, attr);
	if (!hsa->port_num) {
		struct ib_device *dev = container_of((struct device *)kobj,
						     struct ib_device, dev);
885 886

		stats = dev->hw_stats;
887 888
	} else {
		struct ib_port *p = container_of(kobj, struct ib_port, kobj);
889 890

		stats = p->hw_stats;
891
	}
892 893 894 895 896

	mutex_lock(&stats->lock);
	stats->lifespan = jiffies;
	mutex_unlock(&stats->lock);

897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
	return count;
}

static void free_hsag(struct kobject *kobj, struct attribute_group *attr_group)
{
	struct attribute **attr;

	sysfs_remove_group(kobj, attr_group);

	for (attr = attr_group->attrs; *attr; attr++)
		kfree(*attr);
	kfree(attr_group);
}

static struct attribute *alloc_hsa(int index, u8 port_num, const char *name)
{
	struct hw_stats_attribute *hsa;

	hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
	if (!hsa)
		return NULL;

	hsa->attr.name = (char *)name;
	hsa->attr.mode = S_IRUGO;
	hsa->show = show_hw_stats;
	hsa->store = NULL;
	hsa->index = index;
	hsa->port_num = port_num;

	return &hsa->attr;
}

static struct attribute *alloc_hsa_lifespan(char *name, u8 port_num)
{
	struct hw_stats_attribute *hsa;

	hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
	if (!hsa)
		return NULL;

	hsa->attr.name = name;
	hsa->attr.mode = S_IWUSR | S_IRUGO;
	hsa->show = show_stats_lifespan;
	hsa->store = set_stats_lifespan;
	hsa->index = 0;
	hsa->port_num = port_num;

	return &hsa->attr;
}

static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
			   u8 port_num)
{
950
	struct attribute_group *hsag;
951
	struct rdma_hw_stats *stats;
952
	int i, ret;
953

K
Kamal Heib 已提交
954
	stats = device->ops.alloc_hw_stats(device, port_num);
955 956 957 958 959

	if (!stats)
		return;

	if (!stats->names || stats->num_counters <= 0)
960
		goto err_free_stats;
961

962 963 964 965
	/*
	 * Two extra attribue elements here, one for the lifespan entry and
	 * one to NULL terminate the list for the sysfs core code
	 */
966
	hsag = kzalloc(sizeof(*hsag) +
967
		       sizeof(void *) * (stats->num_counters + 2),
968 969
		       GFP_KERNEL);
	if (!hsag)
970
		goto err_free_stats;
971

K
Kamal Heib 已提交
972 973
	ret = device->ops.get_hw_stats(device, stats, port_num,
				       stats->num_counters);
974
	if (ret != stats->num_counters)
975
		goto err_free_hsag;
976 977 978 979 980 981 982 983 984 985

	stats->timestamp = jiffies;

	hsag->name = "hw_counters";
	hsag->attrs = (void *)hsag + sizeof(*hsag);

	for (i = 0; i < stats->num_counters; i++) {
		hsag->attrs[i] = alloc_hsa(i, port_num, stats->names[i]);
		if (!hsag->attrs[i])
			goto err;
986
		sysfs_attr_init(hsag->attrs[i]);
987 988
	}

989
	mutex_init(&stats->lock);
990 991
	/* treat an error here as non-fatal */
	hsag->attrs[i] = alloc_hsa_lifespan("lifespan", port_num);
992 993
	if (hsag->attrs[i])
		sysfs_attr_init(hsag->attrs[i]);
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015

	if (port) {
		struct kobject *kobj = &port->kobj;
		ret = sysfs_create_group(kobj, hsag);
		if (ret)
			goto err;
		port->hw_stats_ag = hsag;
		port->hw_stats = stats;
	} else {
		struct kobject *kobj = &device->dev.kobj;
		ret = sysfs_create_group(kobj, hsag);
		if (ret)
			goto err;
		device->hw_stats_ag = hsag;
		device->hw_stats = stats;
	}

	return;

err:
	for (; i >= 0; i--)
		kfree(hsag->attrs[i]);
1016
err_free_hsag:
1017
	kfree(hsag);
1018 1019
err_free_stats:
	kfree(stats);
1020 1021 1022
	return;
}

1023
static int add_port(struct ib_core_device *coredev, int port_num)
L
Linus Torvalds 已提交
1024
{
1025
	struct ib_device *device = rdma_device_to_ibdev(&coredev->dev);
1026
	bool is_full_dev = &device->coredev == coredev;
L
Linus Torvalds 已提交
1027 1028 1029 1030 1031 1032 1033 1034 1035
	struct ib_port *p;
	struct ib_port_attr attr;
	int i;
	int ret;

	ret = ib_query_port(device, port_num, &attr);
	if (ret)
		return ret;

R
Roland Dreier 已提交
1036
	p = kzalloc(sizeof *p, GFP_KERNEL);
L
Linus Torvalds 已提交
1037 1038 1039 1040 1041 1042
	if (!p)
		return -ENOMEM;

	p->ibdev      = device;
	p->port_num   = port_num;

1043
	ret = kobject_init_and_add(&p->kobj, &port_type,
1044
				   coredev->ports_kobj,
1045
				   "%d", port_num);
1046 1047 1048 1049
	if (ret) {
		kfree(p);
		return ret;
	}
L
Linus Torvalds 已提交
1050

M
Matan Barak 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
	p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
	if (!p->gid_attr_group) {
		ret = -ENOMEM;
		goto err_put;
	}

	p->gid_attr_group->port = p;
	ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
				   &p->kobj, "gid_attrs");
	if (ret) {
		kfree(p->gid_attr_group);
		goto err_put;
	}

1065
	if (device->ops.process_mad && is_full_dev) {
1066 1067 1068 1069 1070
		p->pma_table = get_counter_table(device, port_num);
		ret = sysfs_create_group(&p->kobj, p->pma_table);
		if (ret)
			goto err_put_gid_attrs;
	}
L
Linus Torvalds 已提交
1071 1072

	p->gid_group.name  = "gids";
1073
	p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
1074 1075
	if (!p->gid_group.attrs) {
		ret = -ENOMEM;
1076
		goto err_remove_pma;
1077
	}
L
Linus Torvalds 已提交
1078 1079 1080 1081 1082

	ret = sysfs_create_group(&p->kobj, &p->gid_group);
	if (ret)
		goto err_free_gid;

M
Matan Barak 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
	p->gid_attr_group->ndev.name = "ndevs";
	p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
							  attr.gid_tbl_len);
	if (!p->gid_attr_group->ndev.attrs) {
		ret = -ENOMEM;
		goto err_remove_gid;
	}

	ret = sysfs_create_group(&p->gid_attr_group->kobj,
				 &p->gid_attr_group->ndev);
	if (ret)
		goto err_free_gid_ndev;

	p->gid_attr_group->type.name = "types";
	p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
							  attr.gid_tbl_len);
	if (!p->gid_attr_group->type.attrs) {
		ret = -ENOMEM;
		goto err_remove_gid_ndev;
	}

	ret = sysfs_create_group(&p->gid_attr_group->kobj,
				 &p->gid_attr_group->type);
	if (ret)
		goto err_free_gid_type;

L
Linus Torvalds 已提交
1109
	p->pkey_group.name  = "pkeys";
1110 1111
	p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
						attr.pkey_tbl_len);
1112 1113
	if (!p->pkey_group.attrs) {
		ret = -ENOMEM;
M
Matan Barak 已提交
1114
		goto err_remove_gid_type;
1115
	}
L
Linus Torvalds 已提交
1116 1117 1118 1119 1120

	ret = sysfs_create_group(&p->kobj, &p->pkey_group);
	if (ret)
		goto err_free_pkey;

1121
	if (device->ops.init_port && is_full_dev) {
1122
		ret = device->ops.init_port(device, port_num, &p->kobj);
1123 1124 1125 1126
		if (ret)
			goto err_remove_pkey;
	}

1127
	/*
1128 1129 1130
	 * If port == 0, it means hw_counters are per device and not per
	 * port, so holder should be device. Therefore skip per port conunter
	 * initialization.
1131
	 */
1132
	if (device->ops.alloc_hw_stats && port_num && is_full_dev)
1133 1134
		setup_hw_stats(device, p, port_num);

1135
	list_add_tail(&p->kobj.entry, &coredev->port_list);
L
Linus Torvalds 已提交
1136

1137
	kobject_uevent(&p->kobj, KOBJ_ADD);
L
Linus Torvalds 已提交
1138 1139
	return 0;

1140 1141 1142
err_remove_pkey:
	sysfs_remove_group(&p->kobj, &p->pkey_group);

L
Linus Torvalds 已提交
1143
err_free_pkey:
1144 1145
	for (i = 0; i < attr.pkey_tbl_len; ++i)
		kfree(p->pkey_group.attrs[i]);
L
Linus Torvalds 已提交
1146

1147
	kfree(p->pkey_group.attrs);
1148
	p->pkey_group.attrs = NULL;
L
Linus Torvalds 已提交
1149

M
Matan Barak 已提交
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
err_remove_gid_type:
	sysfs_remove_group(&p->gid_attr_group->kobj,
			   &p->gid_attr_group->type);

err_free_gid_type:
	for (i = 0; i < attr.gid_tbl_len; ++i)
		kfree(p->gid_attr_group->type.attrs[i]);

	kfree(p->gid_attr_group->type.attrs);
	p->gid_attr_group->type.attrs = NULL;

err_remove_gid_ndev:
	sysfs_remove_group(&p->gid_attr_group->kobj,
			   &p->gid_attr_group->ndev);

err_free_gid_ndev:
	for (i = 0; i < attr.gid_tbl_len; ++i)
		kfree(p->gid_attr_group->ndev.attrs[i]);

	kfree(p->gid_attr_group->ndev.attrs);
	p->gid_attr_group->ndev.attrs = NULL;

L
Linus Torvalds 已提交
1172 1173 1174 1175
err_remove_gid:
	sysfs_remove_group(&p->kobj, &p->gid_group);

err_free_gid:
1176 1177
	for (i = 0; i < attr.gid_tbl_len; ++i)
		kfree(p->gid_group.attrs[i]);
L
Linus Torvalds 已提交
1178

1179
	kfree(p->gid_group.attrs);
1180
	p->gid_group.attrs = NULL;
L
Linus Torvalds 已提交
1181 1182

err_remove_pma:
1183 1184
	if (p->pma_table)
		sysfs_remove_group(&p->kobj, p->pma_table);
L
Linus Torvalds 已提交
1185

M
Matan Barak 已提交
1186 1187 1188
err_put_gid_attrs:
	kobject_put(&p->gid_attr_group->kobj);

L
Linus Torvalds 已提交
1189
err_put:
1190
	kobject_put(&p->kobj);
L
Linus Torvalds 已提交
1191 1192 1193
	return ret;
}

1194
static ssize_t node_type_show(struct device *device,
1195
			      struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1196
{
1197
	struct ib_device *dev = rdma_device_to_ibdev(device);
L
Linus Torvalds 已提交
1198 1199

	switch (dev->node_type) {
T
Tom Tucker 已提交
1200 1201
	case RDMA_NODE_IB_CA:	  return sprintf(buf, "%d: CA\n", dev->node_type);
	case RDMA_NODE_RNIC:	  return sprintf(buf, "%d: RNIC\n", dev->node_type);
1202
	case RDMA_NODE_USNIC:	  return sprintf(buf, "%d: usNIC\n", dev->node_type);
1203
	case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
G
Gal Pressman 已提交
1204
	case RDMA_NODE_UNSPECIFIED: return sprintf(buf, "%d: unspecified\n", dev->node_type);
T
Tom Tucker 已提交
1205 1206 1207
	case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
	case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
	default:		  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
L
Linus Torvalds 已提交
1208 1209
	}
}
1210
static DEVICE_ATTR_RO(node_type);
L
Linus Torvalds 已提交
1211

1212
static ssize_t sys_image_guid_show(struct device *device,
1213
				   struct device_attribute *dev_attr, char *buf)
L
Linus Torvalds 已提交
1214
{
1215
	struct ib_device *dev = rdma_device_to_ibdev(device);
L
Linus Torvalds 已提交
1216 1217

	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
1218 1219 1220 1221
		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
L
Linus Torvalds 已提交
1222
}
1223
static DEVICE_ATTR_RO(sys_image_guid);
L
Linus Torvalds 已提交
1224

1225
static ssize_t node_guid_show(struct device *device,
1226
			      struct device_attribute *attr, char *buf)
L
Linus Torvalds 已提交
1227
{
1228
	struct ib_device *dev = rdma_device_to_ibdev(device);
L
Linus Torvalds 已提交
1229 1230

	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
1231 1232 1233 1234
		       be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
		       be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
		       be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
		       be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
L
Linus Torvalds 已提交
1235
}
1236
static DEVICE_ATTR_RO(node_guid);
L
Linus Torvalds 已提交
1237

1238
static ssize_t node_desc_show(struct device *device,
1239
			      struct device_attribute *attr, char *buf)
1240
{
1241
	struct ib_device *dev = rdma_device_to_ibdev(device);
1242 1243 1244 1245

	return sprintf(buf, "%.64s\n", dev->node_desc);
}

1246 1247 1248
static ssize_t node_desc_store(struct device *device,
			       struct device_attribute *attr,
			       const char *buf, size_t count)
1249
{
1250
	struct ib_device *dev = rdma_device_to_ibdev(device);
1251 1252 1253
	struct ib_device_modify desc = {};
	int ret;

K
Kamal Heib 已提交
1254
	if (!dev->ops.modify_device)
1255 1256
		return -EIO;

1257
	memcpy(desc.node_desc, buf, min_t(int, count, IB_DEVICE_NODE_DESC_MAX));
1258 1259 1260 1261 1262 1263
	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
	if (ret)
		return ret;

	return count;
}
1264
static DEVICE_ATTR_RW(node_desc);
1265

1266
static ssize_t fw_ver_show(struct device *device, struct device_attribute *attr,
1267 1268
			   char *buf)
{
1269
	struct ib_device *dev = rdma_device_to_ibdev(device);
1270

1271 1272
	ib_get_device_fw_str(dev, buf);
	strlcat(buf, "\n", IB_FW_VERSION_NAME_MAX);
1273 1274
	return strlen(buf);
}
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284
static DEVICE_ATTR_RO(fw_ver);

static struct attribute *ib_dev_attrs[] = {
	&dev_attr_node_type.attr,
	&dev_attr_node_guid.attr,
	&dev_attr_sys_image_guid.attr,
	&dev_attr_fw_ver.attr,
	&dev_attr_node_desc.attr,
	NULL,
};
1285

1286
const struct attribute_group ib_dev_attr_group = {
1287
	.attrs = ib_dev_attrs,
L
Linus Torvalds 已提交
1288 1289
};

1290
void ib_free_port_attrs(struct ib_core_device *coredev)
1291 1292 1293
{
	struct kobject *p, *t;

1294
	list_for_each_entry_safe(p, t, &coredev->port_list, entry) {
1295
		struct ib_port *port = container_of(p, struct ib_port, kobj);
1296

1297
		list_del(&p->entry);
1298
		if (port->hw_stats_ag)
1299
			free_hsag(&port->kobj, port->hw_stats_ag);
1300
		kfree(port->hw_stats);
1301 1302 1303

		if (port->pma_table)
			sysfs_remove_group(p, port->pma_table);
1304 1305
		sysfs_remove_group(p, &port->pkey_group);
		sysfs_remove_group(p, &port->gid_group);
M
Matan Barak 已提交
1306 1307 1308 1309 1310
		sysfs_remove_group(&port->gid_attr_group->kobj,
				   &port->gid_attr_group->ndev);
		sysfs_remove_group(&port->gid_attr_group->kobj,
				   &port->gid_attr_group->type);
		kobject_put(&port->gid_attr_group->kobj);
1311 1312 1313
		kobject_put(p);
	}

1314
	kobject_put(coredev->ports_kobj);
1315 1316
}

1317
int ib_setup_port_attrs(struct ib_core_device *coredev)
L
Linus Torvalds 已提交
1318
{
1319
	struct ib_device *device = rdma_device_to_ibdev(&coredev->dev);
1320
	unsigned int port;
L
Linus Torvalds 已提交
1321 1322
	int ret;

1323 1324 1325
	coredev->ports_kobj = kobject_create_and_add("ports",
						     &coredev->dev.kobj);
	if (!coredev->ports_kobj)
1326
		return -ENOMEM;
L
Linus Torvalds 已提交
1327

1328
	rdma_for_each_port (device, port) {
1329
		ret = add_port(coredev, port);
L
Linus Torvalds 已提交
1330 1331 1332 1333 1334 1335 1336
		if (ret)
			goto err_put;
	}

	return 0;

err_put:
1337
	ib_free_port_attrs(coredev);
L
Linus Torvalds 已提交
1338 1339 1340
	return ret;
}

1341
int ib_device_register_sysfs(struct ib_device *device)
L
Linus Torvalds 已提交
1342
{
1343
	int ret;
1344

1345
	ret = ib_setup_port_attrs(&device->coredev);
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
	if (ret)
		return ret;

	if (device->ops.alloc_hw_stats)
		setup_hw_stats(device, NULL, 0);

	return 0;
}

void ib_device_unregister_sysfs(struct ib_device *device)
{
	if (device->hw_stats_ag)
		free_hsag(&device->dev.kobj, device->hw_stats_ag);
	kfree(device->hw_stats);

1361
	ib_free_port_attrs(&device->coredev);
L
Linus Torvalds 已提交
1362
}
1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405

/**
 * ib_port_register_module_stat - add module counters under relevant port
 *  of IB device.
 *
 * @device: IB device to add counters
 * @port_num: valid port number
 * @kobj: pointer to the kobject to initialize
 * @ktype: pointer to the ktype for this kobject.
 * @name: the name of the kobject
 */
int ib_port_register_module_stat(struct ib_device *device, u8 port_num,
				 struct kobject *kobj, struct kobj_type *ktype,
				 const char *name)
{
	struct kobject *p, *t;
	int ret;

	list_for_each_entry_safe(p, t, &device->coredev.port_list, entry) {
		struct ib_port *port = container_of(p, struct ib_port, kobj);

		if (port->port_num != port_num)
			continue;

		ret = kobject_init_and_add(kobj, ktype, &port->kobj, "%s",
					   name);
		if (ret)
			return ret;
	}

	return 0;
}
EXPORT_SYMBOL(ib_port_register_module_stat);

/**
 * ib_port_unregister_module_stat - release module counters
 * @kobj: pointer to the kobject to release
 */
void ib_port_unregister_module_stat(struct kobject *kobj)
{
	kobject_put(kobj);
}
EXPORT_SYMBOL(ib_port_unregister_module_stat);