sysfs.c 23.5 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
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.
Linus Torvalds's avatar
Linus Torvalds committed
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 40
#include <linux/string.h>

41
#include <rdma/ib_mad.h>
Linus Torvalds's avatar
Linus Torvalds committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

struct ib_port {
	struct kobject         kobj;
	struct ib_device      *ibdev;
	struct attribute_group gid_group;
	struct attribute_group pkey_group;
	u8                     port_num;
};

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 {
65 66 67
	struct port_attribute	attr;
	char			name[8];
	int			index;
Linus Torvalds's avatar
Linus Torvalds committed
68 69 70 71 72 73 74 75 76 77
};

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)
78
		return -EIO;
Linus Torvalds's avatar
Linus Torvalds committed
79 80 81 82

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

83
static const struct sysfs_ops port_sysfs_ops = {
Linus Torvalds's avatar
Linus Torvalds committed
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
	.show = port_attr_show
};

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,
Roland Dreier's avatar
Roland Dreier committed
107
		       attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
Linus Torvalds's avatar
Linus Torvalds committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
		       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 = "";
182
	int rate;		/* in deci-Gb/sec */
Linus Torvalds's avatar
Linus Torvalds committed
183 184 185 186 187 188 189
	ssize_t ret;

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

	switch (attr.active_speed) {
190
	case IB_SPEED_DDR:
191
		speed = " DDR";
192
		rate = 50;
193
		break;
194
	case IB_SPEED_QDR:
195
		speed = " QDR";
196
		rate = 100;
197
		break;
198
	case IB_SPEED_FDR10:
199
		speed = " FDR10";
200
		rate = 100;
201
		break;
202
	case IB_SPEED_FDR:
203
		speed = " FDR";
204
		rate = 140;
205
		break;
206
	case IB_SPEED_EDR:
207
		speed = " EDR";
208
		rate = 250;
209
		break;
210 211 212 213
	case IB_SPEED_SDR:
	default:		/* default to SDR for invalid rates */
		rate = 25;
		break;
Linus Torvalds's avatar
Linus Torvalds committed
214 215
	}

216
	rate *= ib_width_enum_to_int(attr.active_width);
Linus Torvalds's avatar
Linus Torvalds committed
217 218 219 220
	if (rate < 0)
		return -EINVAL;

	return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
221
		       rate / 10, rate % 10 ? ".5" : "",
Linus Torvalds's avatar
Linus Torvalds committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
		       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);
	}
}

248 249 250 251 252 253 254 255 256 257 258 259 260
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");
	}
}

Linus Torvalds's avatar
Linus Torvalds committed
261 262 263 264 265 266 267 268
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);
269
static PORT_ATTR_RO(link_layer);
Linus Torvalds's avatar
Linus Torvalds committed
270 271 272 273 274 275 276 277 278 279

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,
280
	&port_attr_link_layer.attr,
Linus Torvalds's avatar
Linus Torvalds committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
	NULL
};

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);
	union ib_gid gid;
	ssize_t ret;

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

Harvey Harrison's avatar
Harvey Harrison committed
296
	return sprintf(buf, "%pI6\n", gid.raw);
Linus Torvalds's avatar
Linus Torvalds committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
}

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),	\
	.index = (_offset) | ((_width) << 16) | ((_counter) << 24)	\
}

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;
	struct ib_mad *in_mad  = NULL;
	struct ib_mad *out_mad = NULL;
	ssize_t ret;

	if (!p->ibdev->process_mad)
		return sprintf(buf, "N/A (no PMA)\n");

Roland Dreier's avatar
Roland Dreier committed
334
	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
335
	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
	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;
	in_mad->mad_hdr.attr_id       = cpu_to_be16(0x12); /* PortCounters */

	in_mad->data[41] = p->port_num;	/* PortSelect field */

	if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
		 p->port_num, NULL, NULL, in_mad, out_mad) &
	     (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
	    (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
		ret = -EINVAL;
		goto out;
	}

	switch (width) {
	case 4:
		ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
360
					    (4 - (offset % 8))) & 0xf);
Linus Torvalds's avatar
Linus Torvalds committed
361 362 363 364 365 366
		break;
	case 8:
		ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
		break;
	case 16:
		ret = sprintf(buf, "%u\n",
367
			      be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8)));
Linus Torvalds's avatar
Linus Torvalds committed
368 369 370
		break;
	case 32:
		ret = sprintf(buf, "%u\n",
371
			      be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8)));
Linus Torvalds's avatar
Linus Torvalds committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
		break;
	default:
		ret = 0;
	}

out:
	kfree(in_mad);
	kfree(out_mad);

	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);

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,
	NULL
};

static struct attribute_group pma_group = {
	.name  = "counters",
	.attrs  = pma_attrs
};

static void ib_port_release(struct kobject *kobj)
{
	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
	struct attribute *a;
	int i;

432
	for (i = 0; (a = p->gid_group.attrs[i]); ++i)
Linus Torvalds's avatar
Linus Torvalds committed
433 434
		kfree(a);

435 436 437
	kfree(p->gid_group.attrs);

	for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
Linus Torvalds's avatar
Linus Torvalds committed
438 439
		kfree(a);

440 441
	kfree(p->pkey_group.attrs);

Linus Torvalds's avatar
Linus Torvalds committed
442 443 444 445 446 447 448 449 450
	kfree(p);
}

static struct kobj_type port_type = {
	.release       = ib_port_release,
	.sysfs_ops     = &port_sysfs_ops,
	.default_attrs = port_default_attrs
};

451
static void ib_device_release(struct device *device)
Linus Torvalds's avatar
Linus Torvalds committed
452
{
453
	struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds's avatar
Linus Torvalds committed
454 455 456 457

	kfree(dev);
}

458
static int ib_device_uevent(struct device *device,
459
			    struct kobj_uevent_env *env)
Linus Torvalds's avatar
Linus Torvalds committed
460
{
461
	struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds's avatar
Linus Torvalds committed
462

463
	if (add_uevent_var(env, "NAME=%s", dev->name))
Linus Torvalds's avatar
Linus Torvalds committed
464 465 466
		return -ENOMEM;

	/*
467
	 * It would be nice to pass the node GUID with the event...
Linus Torvalds's avatar
Linus Torvalds committed
468 469 470 471 472
	 */

	return 0;
}

473 474 475 476
static struct attribute **
alloc_group_attrs(ssize_t (*show)(struct ib_port *,
				  struct port_attribute *, char *buf),
		  int len)
Linus Torvalds's avatar
Linus Torvalds committed
477
{
478 479
	struct attribute **tab_attr;
	struct port_table_attribute *element;
Linus Torvalds's avatar
Linus Torvalds committed
480 481
	int i;

482 483 484
	tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
	if (!tab_attr)
		return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
485

486
	for (i = 0; i < len; i++) {
487
		element = kzalloc(sizeof(struct port_table_attribute),
488 489
				  GFP_KERNEL);
		if (!element)
Linus Torvalds's avatar
Linus Torvalds committed
490 491
			goto err;

492
		if (snprintf(element->name, sizeof(element->name),
Roland Dreier's avatar
Roland Dreier committed
493 494
			     "%d", i) >= sizeof(element->name)) {
			kfree(element);
Linus Torvalds's avatar
Linus Torvalds committed
495
			goto err;
Roland Dreier's avatar
Roland Dreier committed
496
		}
Linus Torvalds's avatar
Linus Torvalds committed
497

498 499 500 501
		element->attr.attr.name  = element->name;
		element->attr.attr.mode  = S_IRUGO;
		element->attr.show       = show;
		element->index		 = i;
502
		sysfs_attr_init(&element->attr.attr);
Linus Torvalds's avatar
Linus Torvalds committed
503

504
		tab_attr[i] = &element->attr.attr;
Linus Torvalds's avatar
Linus Torvalds committed
505 506
	}

507
	return tab_attr;
Linus Torvalds's avatar
Linus Torvalds committed
508

509 510 511 512 513
err:
	while (--i >= 0)
		kfree(tab_attr[i]);
	kfree(tab_attr);
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
514 515
}

516 517 518
static int add_port(struct ib_device *device, int port_num,
		    int (*port_callback)(struct ib_device *,
					 u8, struct kobject *))
Linus Torvalds's avatar
Linus Torvalds committed
519 520 521 522 523 524 525 526 527 528
{
	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;

Roland Dreier's avatar
Roland Dreier committed
529
	p = kzalloc(sizeof *p, GFP_KERNEL);
Linus Torvalds's avatar
Linus Torvalds committed
530 531 532 533 534 535
	if (!p)
		return -ENOMEM;

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

536 537 538
	ret = kobject_init_and_add(&p->kobj, &port_type,
				   kobject_get(device->ports_parent),
				   "%d", port_num);
Linus Torvalds's avatar
Linus Torvalds committed
539 540 541 542 543 544 545 546
	if (ret)
		goto err_put;

	ret = sysfs_create_group(&p->kobj, &pma_group);
	if (ret)
		goto err_put;

	p->gid_group.name  = "gids";
547 548 549
	p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
	if (!p->gid_group.attrs)
		goto err_remove_pma;
Linus Torvalds's avatar
Linus Torvalds committed
550 551 552 553 554 555

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

	p->pkey_group.name  = "pkeys";
556 557 558 559
	p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
						attr.pkey_tbl_len);
	if (!p->pkey_group.attrs)
		goto err_remove_gid;
Linus Torvalds's avatar
Linus Torvalds committed
560 561 562 563 564

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

565 566 567 568 569 570
	if (port_callback) {
		ret = port_callback(device, port_num, &p->kobj);
		if (ret)
			goto err_remove_pkey;
	}

Linus Torvalds's avatar
Linus Torvalds committed
571 572
	list_add_tail(&p->kobj.entry, &device->port_list);

573
	kobject_uevent(&p->kobj, KOBJ_ADD);
Linus Torvalds's avatar
Linus Torvalds committed
574 575
	return 0;

576 577 578
err_remove_pkey:
	sysfs_remove_group(&p->kobj, &p->pkey_group);

Linus Torvalds's avatar
Linus Torvalds committed
579
err_free_pkey:
580 581
	for (i = 0; i < attr.pkey_tbl_len; ++i)
		kfree(p->pkey_group.attrs[i]);
Linus Torvalds's avatar
Linus Torvalds committed
582

583
	kfree(p->pkey_group.attrs);
Linus Torvalds's avatar
Linus Torvalds committed
584 585 586 587 588

err_remove_gid:
	sysfs_remove_group(&p->kobj, &p->gid_group);

err_free_gid:
589 590
	for (i = 0; i < attr.gid_tbl_len; ++i)
		kfree(p->gid_group.attrs[i]);
Linus Torvalds's avatar
Linus Torvalds committed
591

592
	kfree(p->gid_group.attrs);
Linus Torvalds's avatar
Linus Torvalds committed
593 594 595 596 597

err_remove_pma:
	sysfs_remove_group(&p->kobj, &pma_group);

err_put:
598
	kobject_put(device->ports_parent);
Linus Torvalds's avatar
Linus Torvalds committed
599 600 601 602
	kfree(p);
	return ret;
}

603 604
static ssize_t show_node_type(struct device *device,
			      struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
605
{
606
	struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds's avatar
Linus Torvalds committed
607 608

	switch (dev->node_type) {
Tom Tucker's avatar
Tom Tucker committed
609 610 611 612 613
	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);
	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);
Linus Torvalds's avatar
Linus Torvalds committed
614 615 616
	}
}

617 618
static ssize_t show_sys_image_guid(struct device *device,
				   struct device_attribute *dev_attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
619
{
620
	struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds's avatar
Linus Torvalds committed
621 622 623 624 625 626 627 628
	struct ib_device_attr attr;
	ssize_t ret;

	ret = ib_query_device(dev, &attr);
	if (ret)
		return ret;

	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
629 630 631 632
		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]),
		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]),
		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]),
		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
Linus Torvalds's avatar
Linus Torvalds committed
633 634
}

635 636
static ssize_t show_node_guid(struct device *device,
			      struct device_attribute *attr, char *buf)
Linus Torvalds's avatar
Linus Torvalds committed
637
{
638
	struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds's avatar
Linus Torvalds committed
639 640

	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
641 642 643 644
		       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]));
Linus Torvalds's avatar
Linus Torvalds committed
645 646
}

647 648
static ssize_t show_node_desc(struct device *device,
			      struct device_attribute *attr, char *buf)
649
{
650
	struct ib_device *dev = container_of(device, struct ib_device, dev);
651 652 653 654

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

655 656 657
static ssize_t set_node_desc(struct device *device,
			     struct device_attribute *attr,
			     const char *buf, size_t count)
658
{
659
	struct ib_device *dev = container_of(device, struct ib_device, dev);
660 661 662 663 664 665 666 667 668 669 670 671 672 673
	struct ib_device_modify desc = {};
	int ret;

	if (!dev->modify_device)
		return -EIO;

	memcpy(desc.node_desc, buf, min_t(int, count, 64));
	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
	if (ret)
		return ret;

	return count;
}

674 675 676 677 678 679 680 681 682 683
static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);

static struct device_attribute *ib_class_attributes[] = {
	&dev_attr_node_type,
	&dev_attr_sys_image_guid,
	&dev_attr_node_guid,
	&dev_attr_node_desc
Linus Torvalds's avatar
Linus Torvalds committed
684 685 686 687
};

static struct class ib_class = {
	.name    = "infiniband",
688 689
	.dev_release = ib_device_release,
	.dev_uevent = ib_device_uevent,
Linus Torvalds's avatar
Linus Torvalds committed
690 691
};

692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805
/* Show a given an attribute in the statistics group */
static ssize_t show_protocol_stat(const struct device *device,
			    struct device_attribute *attr, char *buf,
			    unsigned offset)
{
	struct ib_device *dev = container_of(device, struct ib_device, dev);
	union rdma_protocol_stats stats;
	ssize_t ret;

	ret = dev->get_protocol_stats(dev, &stats);
	if (ret)
		return ret;

	return sprintf(buf, "%llu\n",
		       (unsigned long long) ((u64 *) &stats)[offset]);
}

/* generate a read-only iwarp statistics attribute */
#define IW_STATS_ENTRY(name)						\
static ssize_t show_##name(struct device *device,			\
			   struct device_attribute *attr, char *buf)	\
{									\
	return show_protocol_stat(device, attr, buf,			\
				  offsetof(struct iw_protocol_stats, name) / \
				  sizeof (u64));			\
}									\
static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)

IW_STATS_ENTRY(ipInReceives);
IW_STATS_ENTRY(ipInHdrErrors);
IW_STATS_ENTRY(ipInTooBigErrors);
IW_STATS_ENTRY(ipInNoRoutes);
IW_STATS_ENTRY(ipInAddrErrors);
IW_STATS_ENTRY(ipInUnknownProtos);
IW_STATS_ENTRY(ipInTruncatedPkts);
IW_STATS_ENTRY(ipInDiscards);
IW_STATS_ENTRY(ipInDelivers);
IW_STATS_ENTRY(ipOutForwDatagrams);
IW_STATS_ENTRY(ipOutRequests);
IW_STATS_ENTRY(ipOutDiscards);
IW_STATS_ENTRY(ipOutNoRoutes);
IW_STATS_ENTRY(ipReasmTimeout);
IW_STATS_ENTRY(ipReasmReqds);
IW_STATS_ENTRY(ipReasmOKs);
IW_STATS_ENTRY(ipReasmFails);
IW_STATS_ENTRY(ipFragOKs);
IW_STATS_ENTRY(ipFragFails);
IW_STATS_ENTRY(ipFragCreates);
IW_STATS_ENTRY(ipInMcastPkts);
IW_STATS_ENTRY(ipOutMcastPkts);
IW_STATS_ENTRY(ipInBcastPkts);
IW_STATS_ENTRY(ipOutBcastPkts);
IW_STATS_ENTRY(tcpRtoAlgorithm);
IW_STATS_ENTRY(tcpRtoMin);
IW_STATS_ENTRY(tcpRtoMax);
IW_STATS_ENTRY(tcpMaxConn);
IW_STATS_ENTRY(tcpActiveOpens);
IW_STATS_ENTRY(tcpPassiveOpens);
IW_STATS_ENTRY(tcpAttemptFails);
IW_STATS_ENTRY(tcpEstabResets);
IW_STATS_ENTRY(tcpCurrEstab);
IW_STATS_ENTRY(tcpInSegs);
IW_STATS_ENTRY(tcpOutSegs);
IW_STATS_ENTRY(tcpRetransSegs);
IW_STATS_ENTRY(tcpInErrs);
IW_STATS_ENTRY(tcpOutRsts);

static struct attribute *iw_proto_stats_attrs[] = {
	&dev_attr_ipInReceives.attr,
	&dev_attr_ipInHdrErrors.attr,
	&dev_attr_ipInTooBigErrors.attr,
	&dev_attr_ipInNoRoutes.attr,
	&dev_attr_ipInAddrErrors.attr,
	&dev_attr_ipInUnknownProtos.attr,
	&dev_attr_ipInTruncatedPkts.attr,
	&dev_attr_ipInDiscards.attr,
	&dev_attr_ipInDelivers.attr,
	&dev_attr_ipOutForwDatagrams.attr,
	&dev_attr_ipOutRequests.attr,
	&dev_attr_ipOutDiscards.attr,
	&dev_attr_ipOutNoRoutes.attr,
	&dev_attr_ipReasmTimeout.attr,
	&dev_attr_ipReasmReqds.attr,
	&dev_attr_ipReasmOKs.attr,
	&dev_attr_ipReasmFails.attr,
	&dev_attr_ipFragOKs.attr,
	&dev_attr_ipFragFails.attr,
	&dev_attr_ipFragCreates.attr,
	&dev_attr_ipInMcastPkts.attr,
	&dev_attr_ipOutMcastPkts.attr,
	&dev_attr_ipInBcastPkts.attr,
	&dev_attr_ipOutBcastPkts.attr,
	&dev_attr_tcpRtoAlgorithm.attr,
	&dev_attr_tcpRtoMin.attr,
	&dev_attr_tcpRtoMax.attr,
	&dev_attr_tcpMaxConn.attr,
	&dev_attr_tcpActiveOpens.attr,
	&dev_attr_tcpPassiveOpens.attr,
	&dev_attr_tcpAttemptFails.attr,
	&dev_attr_tcpEstabResets.attr,
	&dev_attr_tcpCurrEstab.attr,
	&dev_attr_tcpInSegs.attr,
	&dev_attr_tcpOutSegs.attr,
	&dev_attr_tcpRetransSegs.attr,
	&dev_attr_tcpInErrs.attr,
	&dev_attr_tcpOutRsts.attr,
	NULL
};

static struct attribute_group iw_stats_group = {
	.name	= "proto_stats",
	.attrs	= iw_proto_stats_attrs,
};

806 807 808
int ib_device_register_sysfs(struct ib_device *device,
			     int (*port_callback)(struct ib_device *,
						  u8, struct kobject *))
Linus Torvalds's avatar
Linus Torvalds committed
809
{
810
	struct device *class_dev = &device->dev;
Linus Torvalds's avatar
Linus Torvalds committed
811 812 813 814
	int ret;
	int i;

	class_dev->class      = &ib_class;
815
	class_dev->parent     = device->dma_device;
816
	dev_set_name(class_dev, "%s", device->name);
817
	dev_set_drvdata(class_dev, device);
Linus Torvalds's avatar
Linus Torvalds committed
818 819 820

	INIT_LIST_HEAD(&device->port_list);

821
	ret = device_register(class_dev);
Linus Torvalds's avatar
Linus Torvalds committed
822 823 824 825
	if (ret)
		goto err;

	for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
826
		ret = device_create_file(class_dev, ib_class_attributes[i]);
Linus Torvalds's avatar
Linus Torvalds committed
827 828 829 830
		if (ret)
			goto err_unregister;
	}

831 832
	device->ports_parent = kobject_create_and_add("ports",
					kobject_get(&class_dev->kobj));
833 834
	if (!device->ports_parent) {
		ret = -ENOMEM;
Linus Torvalds's avatar
Linus Torvalds committed
835
		goto err_put;
836
	}
Linus Torvalds's avatar
Linus Torvalds committed
837

Tom Tucker's avatar
Tom Tucker committed
838
	if (device->node_type == RDMA_NODE_IB_SWITCH) {
839
		ret = add_port(device, 0, port_callback);
Linus Torvalds's avatar
Linus Torvalds committed
840 841 842 843
		if (ret)
			goto err_put;
	} else {
		for (i = 1; i <= device->phys_port_cnt; ++i) {
844
			ret = add_port(device, i, port_callback);
Linus Torvalds's avatar
Linus Torvalds committed
845 846 847 848 849
			if (ret)
				goto err_put;
		}
	}

850 851 852 853 854 855
	if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
		ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
		if (ret)
			goto err_put;
	}

Linus Torvalds's avatar
Linus Torvalds committed
856 857 858 859 860 861 862 863 864 865 866 867 868
	return 0;

err_put:
	{
		struct kobject *p, *t;
		struct ib_port *port;

		list_for_each_entry_safe(p, t, &device->port_list, entry) {
			list_del(&p->entry);
			port = container_of(p, struct ib_port, kobj);
			sysfs_remove_group(p, &pma_group);
			sysfs_remove_group(p, &port->pkey_group);
			sysfs_remove_group(p, &port->gid_group);
869
			kobject_put(p);
Linus Torvalds's avatar
Linus Torvalds committed
870 871 872 873 874 875
		}
	}

	kobject_put(&class_dev->kobj);

err_unregister:
876
	device_unregister(class_dev);
Linus Torvalds's avatar
Linus Torvalds committed
877 878 879 880 881 882 883 884 885 886

err:
	return ret;
}

void ib_device_unregister_sysfs(struct ib_device *device)
{
	struct kobject *p, *t;
	struct ib_port *port;

887 888 889
	/* Hold kobject until ib_dealloc_device() */
	kobject_get(&device->dev.kobj);

Linus Torvalds's avatar
Linus Torvalds committed
890 891 892 893 894 895
	list_for_each_entry_safe(p, t, &device->port_list, entry) {
		list_del(&p->entry);
		port = container_of(p, struct ib_port, kobj);
		sysfs_remove_group(p, &pma_group);
		sysfs_remove_group(p, &port->pkey_group);
		sysfs_remove_group(p, &port->gid_group);
896
		kobject_put(p);
Linus Torvalds's avatar
Linus Torvalds committed
897 898
	}

899
	kobject_put(device->ports_parent);
900
	device_unregister(&device->dev);
Linus Torvalds's avatar
Linus Torvalds committed
901 902 903 904 905 906 907 908 909 910 911
}

int ib_sysfs_setup(void)
{
	return class_register(&ib_class);
}

void ib_sysfs_cleanup(void)
{
	class_unregister(&ib_class);
}