xref: /linux/drivers/infiniband/core/sysfs.c (revision d229807f669ba3dea9f64467ee965051c4366aed)
1 /*
2  * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3  * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
4  * Copyright (c) 2005 Sun Microsystems, 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 "core_priv.h"
36 
37 #include <linux/slab.h>
38 #include <linux/stat.h>
39 #include <linux/string.h>
40 
41 #include <rdma/ib_mad.h>
42 
43 struct ib_port {
44 	struct kobject         kobj;
45 	struct ib_device      *ibdev;
46 	struct attribute_group gid_group;
47 	struct attribute_group pkey_group;
48 	u8                     port_num;
49 };
50 
51 struct port_attribute {
52 	struct attribute attr;
53 	ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
54 	ssize_t (*store)(struct ib_port *, struct port_attribute *,
55 			 const char *buf, size_t count);
56 };
57 
58 #define PORT_ATTR(_name, _mode, _show, _store) \
59 struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
60 
61 #define PORT_ATTR_RO(_name) \
62 struct port_attribute port_attr_##_name = __ATTR_RO(_name)
63 
64 struct port_table_attribute {
65 	struct port_attribute	attr;
66 	char			name[8];
67 	int			index;
68 };
69 
70 static ssize_t port_attr_show(struct kobject *kobj,
71 			      struct attribute *attr, char *buf)
72 {
73 	struct port_attribute *port_attr =
74 		container_of(attr, struct port_attribute, attr);
75 	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
76 
77 	if (!port_attr->show)
78 		return -EIO;
79 
80 	return port_attr->show(p, port_attr, buf);
81 }
82 
83 static const struct sysfs_ops port_sysfs_ops = {
84 	.show = port_attr_show
85 };
86 
87 static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
88 			  char *buf)
89 {
90 	struct ib_port_attr attr;
91 	ssize_t ret;
92 
93 	static const char *state_name[] = {
94 		[IB_PORT_NOP]		= "NOP",
95 		[IB_PORT_DOWN]		= "DOWN",
96 		[IB_PORT_INIT]		= "INIT",
97 		[IB_PORT_ARMED]		= "ARMED",
98 		[IB_PORT_ACTIVE]	= "ACTIVE",
99 		[IB_PORT_ACTIVE_DEFER]	= "ACTIVE_DEFER"
100 	};
101 
102 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
103 	if (ret)
104 		return ret;
105 
106 	return sprintf(buf, "%d: %s\n", attr.state,
107 		       attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
108 		       state_name[attr.state] : "UNKNOWN");
109 }
110 
111 static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
112 			char *buf)
113 {
114 	struct ib_port_attr attr;
115 	ssize_t ret;
116 
117 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
118 	if (ret)
119 		return ret;
120 
121 	return sprintf(buf, "0x%x\n", attr.lid);
122 }
123 
124 static ssize_t lid_mask_count_show(struct ib_port *p,
125 				   struct port_attribute *unused,
126 				   char *buf)
127 {
128 	struct ib_port_attr attr;
129 	ssize_t ret;
130 
131 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
132 	if (ret)
133 		return ret;
134 
135 	return sprintf(buf, "%d\n", attr.lmc);
136 }
137 
138 static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
139 			   char *buf)
140 {
141 	struct ib_port_attr attr;
142 	ssize_t ret;
143 
144 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
145 	if (ret)
146 		return ret;
147 
148 	return sprintf(buf, "0x%x\n", attr.sm_lid);
149 }
150 
151 static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
152 			  char *buf)
153 {
154 	struct ib_port_attr attr;
155 	ssize_t ret;
156 
157 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
158 	if (ret)
159 		return ret;
160 
161 	return sprintf(buf, "%d\n", attr.sm_sl);
162 }
163 
164 static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
165 			     char *buf)
166 {
167 	struct ib_port_attr attr;
168 	ssize_t ret;
169 
170 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
171 	if (ret)
172 		return ret;
173 
174 	return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
175 }
176 
177 static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
178 			 char *buf)
179 {
180 	struct ib_port_attr attr;
181 	char *speed = "";
182 	int rate;
183 	ssize_t ret;
184 
185 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
186 	if (ret)
187 		return ret;
188 
189 	switch (attr.active_speed) {
190 	case 2: speed = " DDR"; break;
191 	case 4: speed = " QDR"; break;
192 	}
193 
194 	rate = 25 * ib_width_enum_to_int(attr.active_width) * attr.active_speed;
195 	if (rate < 0)
196 		return -EINVAL;
197 
198 	return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
199 		       rate / 10, rate % 10 ? ".5" : "",
200 		       ib_width_enum_to_int(attr.active_width), speed);
201 }
202 
203 static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
204 			       char *buf)
205 {
206 	struct ib_port_attr attr;
207 
208 	ssize_t ret;
209 
210 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
211 	if (ret)
212 		return ret;
213 
214 	switch (attr.phys_state) {
215 	case 1:  return sprintf(buf, "1: Sleep\n");
216 	case 2:  return sprintf(buf, "2: Polling\n");
217 	case 3:  return sprintf(buf, "3: Disabled\n");
218 	case 4:  return sprintf(buf, "4: PortConfigurationTraining\n");
219 	case 5:  return sprintf(buf, "5: LinkUp\n");
220 	case 6:  return sprintf(buf, "6: LinkErrorRecovery\n");
221 	case 7:  return sprintf(buf, "7: Phy Test\n");
222 	default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
223 	}
224 }
225 
226 static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
227 			       char *buf)
228 {
229 	switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
230 	case IB_LINK_LAYER_INFINIBAND:
231 		return sprintf(buf, "%s\n", "InfiniBand");
232 	case IB_LINK_LAYER_ETHERNET:
233 		return sprintf(buf, "%s\n", "Ethernet");
234 	default:
235 		return sprintf(buf, "%s\n", "Unknown");
236 	}
237 }
238 
239 static PORT_ATTR_RO(state);
240 static PORT_ATTR_RO(lid);
241 static PORT_ATTR_RO(lid_mask_count);
242 static PORT_ATTR_RO(sm_lid);
243 static PORT_ATTR_RO(sm_sl);
244 static PORT_ATTR_RO(cap_mask);
245 static PORT_ATTR_RO(rate);
246 static PORT_ATTR_RO(phys_state);
247 static PORT_ATTR_RO(link_layer);
248 
249 static struct attribute *port_default_attrs[] = {
250 	&port_attr_state.attr,
251 	&port_attr_lid.attr,
252 	&port_attr_lid_mask_count.attr,
253 	&port_attr_sm_lid.attr,
254 	&port_attr_sm_sl.attr,
255 	&port_attr_cap_mask.attr,
256 	&port_attr_rate.attr,
257 	&port_attr_phys_state.attr,
258 	&port_attr_link_layer.attr,
259 	NULL
260 };
261 
262 static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
263 			     char *buf)
264 {
265 	struct port_table_attribute *tab_attr =
266 		container_of(attr, struct port_table_attribute, attr);
267 	union ib_gid gid;
268 	ssize_t ret;
269 
270 	ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
271 	if (ret)
272 		return ret;
273 
274 	return sprintf(buf, "%pI6\n", gid.raw);
275 }
276 
277 static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
278 			      char *buf)
279 {
280 	struct port_table_attribute *tab_attr =
281 		container_of(attr, struct port_table_attribute, attr);
282 	u16 pkey;
283 	ssize_t ret;
284 
285 	ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
286 	if (ret)
287 		return ret;
288 
289 	return sprintf(buf, "0x%04x\n", pkey);
290 }
291 
292 #define PORT_PMA_ATTR(_name, _counter, _width, _offset)			\
293 struct port_table_attribute port_pma_attr_##_name = {			\
294 	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
295 	.index = (_offset) | ((_width) << 16) | ((_counter) << 24)	\
296 }
297 
298 static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
299 				char *buf)
300 {
301 	struct port_table_attribute *tab_attr =
302 		container_of(attr, struct port_table_attribute, attr);
303 	int offset = tab_attr->index & 0xffff;
304 	int width  = (tab_attr->index >> 16) & 0xff;
305 	struct ib_mad *in_mad  = NULL;
306 	struct ib_mad *out_mad = NULL;
307 	ssize_t ret;
308 
309 	if (!p->ibdev->process_mad)
310 		return sprintf(buf, "N/A (no PMA)\n");
311 
312 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
313 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
314 	if (!in_mad || !out_mad) {
315 		ret = -ENOMEM;
316 		goto out;
317 	}
318 
319 	in_mad->mad_hdr.base_version  = 1;
320 	in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
321 	in_mad->mad_hdr.class_version = 1;
322 	in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
323 	in_mad->mad_hdr.attr_id       = cpu_to_be16(0x12); /* PortCounters */
324 
325 	in_mad->data[41] = p->port_num;	/* PortSelect field */
326 
327 	if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
328 		 p->port_num, NULL, NULL, in_mad, out_mad) &
329 	     (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
330 	    (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
331 		ret = -EINVAL;
332 		goto out;
333 	}
334 
335 	switch (width) {
336 	case 4:
337 		ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
338 					    (4 - (offset % 8))) & 0xf);
339 		break;
340 	case 8:
341 		ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
342 		break;
343 	case 16:
344 		ret = sprintf(buf, "%u\n",
345 			      be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8)));
346 		break;
347 	case 32:
348 		ret = sprintf(buf, "%u\n",
349 			      be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8)));
350 		break;
351 	default:
352 		ret = 0;
353 	}
354 
355 out:
356 	kfree(in_mad);
357 	kfree(out_mad);
358 
359 	return ret;
360 }
361 
362 static PORT_PMA_ATTR(symbol_error		    ,  0, 16,  32);
363 static PORT_PMA_ATTR(link_error_recovery	    ,  1,  8,  48);
364 static PORT_PMA_ATTR(link_downed		    ,  2,  8,  56);
365 static PORT_PMA_ATTR(port_rcv_errors		    ,  3, 16,  64);
366 static PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
367 static PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
368 static PORT_PMA_ATTR(port_xmit_discards		    ,  6, 16, 112);
369 static PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
370 static PORT_PMA_ATTR(port_rcv_constraint_errors	    ,  8,  8, 136);
371 static PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
372 static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
373 static PORT_PMA_ATTR(VL15_dropped		    , 11, 16, 176);
374 static PORT_PMA_ATTR(port_xmit_data		    , 12, 32, 192);
375 static PORT_PMA_ATTR(port_rcv_data		    , 13, 32, 224);
376 static PORT_PMA_ATTR(port_xmit_packets		    , 14, 32, 256);
377 static PORT_PMA_ATTR(port_rcv_packets		    , 15, 32, 288);
378 
379 static struct attribute *pma_attrs[] = {
380 	&port_pma_attr_symbol_error.attr.attr,
381 	&port_pma_attr_link_error_recovery.attr.attr,
382 	&port_pma_attr_link_downed.attr.attr,
383 	&port_pma_attr_port_rcv_errors.attr.attr,
384 	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
385 	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
386 	&port_pma_attr_port_xmit_discards.attr.attr,
387 	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
388 	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
389 	&port_pma_attr_local_link_integrity_errors.attr.attr,
390 	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
391 	&port_pma_attr_VL15_dropped.attr.attr,
392 	&port_pma_attr_port_xmit_data.attr.attr,
393 	&port_pma_attr_port_rcv_data.attr.attr,
394 	&port_pma_attr_port_xmit_packets.attr.attr,
395 	&port_pma_attr_port_rcv_packets.attr.attr,
396 	NULL
397 };
398 
399 static struct attribute_group pma_group = {
400 	.name  = "counters",
401 	.attrs  = pma_attrs
402 };
403 
404 static void ib_port_release(struct kobject *kobj)
405 {
406 	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
407 	struct attribute *a;
408 	int i;
409 
410 	for (i = 0; (a = p->gid_group.attrs[i]); ++i)
411 		kfree(a);
412 
413 	kfree(p->gid_group.attrs);
414 
415 	for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
416 		kfree(a);
417 
418 	kfree(p->pkey_group.attrs);
419 
420 	kfree(p);
421 }
422 
423 static struct kobj_type port_type = {
424 	.release       = ib_port_release,
425 	.sysfs_ops     = &port_sysfs_ops,
426 	.default_attrs = port_default_attrs
427 };
428 
429 static void ib_device_release(struct device *device)
430 {
431 	struct ib_device *dev = container_of(device, struct ib_device, dev);
432 
433 	kfree(dev);
434 }
435 
436 static int ib_device_uevent(struct device *device,
437 			    struct kobj_uevent_env *env)
438 {
439 	struct ib_device *dev = container_of(device, struct ib_device, dev);
440 
441 	if (add_uevent_var(env, "NAME=%s", dev->name))
442 		return -ENOMEM;
443 
444 	/*
445 	 * It would be nice to pass the node GUID with the event...
446 	 */
447 
448 	return 0;
449 }
450 
451 static struct attribute **
452 alloc_group_attrs(ssize_t (*show)(struct ib_port *,
453 				  struct port_attribute *, char *buf),
454 		  int len)
455 {
456 	struct attribute **tab_attr;
457 	struct port_table_attribute *element;
458 	int i;
459 
460 	tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
461 	if (!tab_attr)
462 		return NULL;
463 
464 	for (i = 0; i < len; i++) {
465 		element = kzalloc(sizeof(struct port_table_attribute),
466 				  GFP_KERNEL);
467 		if (!element)
468 			goto err;
469 
470 		if (snprintf(element->name, sizeof(element->name),
471 			     "%d", i) >= sizeof(element->name)) {
472 			kfree(element);
473 			goto err;
474 		}
475 
476 		element->attr.attr.name  = element->name;
477 		element->attr.attr.mode  = S_IRUGO;
478 		element->attr.show       = show;
479 		element->index		 = i;
480 		sysfs_attr_init(&element->attr.attr);
481 
482 		tab_attr[i] = &element->attr.attr;
483 	}
484 
485 	return tab_attr;
486 
487 err:
488 	while (--i >= 0)
489 		kfree(tab_attr[i]);
490 	kfree(tab_attr);
491 	return NULL;
492 }
493 
494 static int add_port(struct ib_device *device, int port_num,
495 		    int (*port_callback)(struct ib_device *,
496 					 u8, struct kobject *))
497 {
498 	struct ib_port *p;
499 	struct ib_port_attr attr;
500 	int i;
501 	int ret;
502 
503 	ret = ib_query_port(device, port_num, &attr);
504 	if (ret)
505 		return ret;
506 
507 	p = kzalloc(sizeof *p, GFP_KERNEL);
508 	if (!p)
509 		return -ENOMEM;
510 
511 	p->ibdev      = device;
512 	p->port_num   = port_num;
513 
514 	ret = kobject_init_and_add(&p->kobj, &port_type,
515 				   kobject_get(device->ports_parent),
516 				   "%d", port_num);
517 	if (ret)
518 		goto err_put;
519 
520 	ret = sysfs_create_group(&p->kobj, &pma_group);
521 	if (ret)
522 		goto err_put;
523 
524 	p->gid_group.name  = "gids";
525 	p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
526 	if (!p->gid_group.attrs)
527 		goto err_remove_pma;
528 
529 	ret = sysfs_create_group(&p->kobj, &p->gid_group);
530 	if (ret)
531 		goto err_free_gid;
532 
533 	p->pkey_group.name  = "pkeys";
534 	p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
535 						attr.pkey_tbl_len);
536 	if (!p->pkey_group.attrs)
537 		goto err_remove_gid;
538 
539 	ret = sysfs_create_group(&p->kobj, &p->pkey_group);
540 	if (ret)
541 		goto err_free_pkey;
542 
543 	if (port_callback) {
544 		ret = port_callback(device, port_num, &p->kobj);
545 		if (ret)
546 			goto err_remove_pkey;
547 	}
548 
549 	list_add_tail(&p->kobj.entry, &device->port_list);
550 
551 	kobject_uevent(&p->kobj, KOBJ_ADD);
552 	return 0;
553 
554 err_remove_pkey:
555 	sysfs_remove_group(&p->kobj, &p->pkey_group);
556 
557 err_free_pkey:
558 	for (i = 0; i < attr.pkey_tbl_len; ++i)
559 		kfree(p->pkey_group.attrs[i]);
560 
561 	kfree(p->pkey_group.attrs);
562 
563 err_remove_gid:
564 	sysfs_remove_group(&p->kobj, &p->gid_group);
565 
566 err_free_gid:
567 	for (i = 0; i < attr.gid_tbl_len; ++i)
568 		kfree(p->gid_group.attrs[i]);
569 
570 	kfree(p->gid_group.attrs);
571 
572 err_remove_pma:
573 	sysfs_remove_group(&p->kobj, &pma_group);
574 
575 err_put:
576 	kobject_put(device->ports_parent);
577 	kfree(p);
578 	return ret;
579 }
580 
581 static ssize_t show_node_type(struct device *device,
582 			      struct device_attribute *attr, char *buf)
583 {
584 	struct ib_device *dev = container_of(device, struct ib_device, dev);
585 
586 	switch (dev->node_type) {
587 	case RDMA_NODE_IB_CA:	  return sprintf(buf, "%d: CA\n", dev->node_type);
588 	case RDMA_NODE_RNIC:	  return sprintf(buf, "%d: RNIC\n", dev->node_type);
589 	case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
590 	case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
591 	default:		  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
592 	}
593 }
594 
595 static ssize_t show_sys_image_guid(struct device *device,
596 				   struct device_attribute *dev_attr, char *buf)
597 {
598 	struct ib_device *dev = container_of(device, struct ib_device, dev);
599 	struct ib_device_attr attr;
600 	ssize_t ret;
601 
602 	ret = ib_query_device(dev, &attr);
603 	if (ret)
604 		return ret;
605 
606 	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
607 		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]),
608 		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]),
609 		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]),
610 		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
611 }
612 
613 static ssize_t show_node_guid(struct device *device,
614 			      struct device_attribute *attr, char *buf)
615 {
616 	struct ib_device *dev = container_of(device, struct ib_device, dev);
617 
618 	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
619 		       be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
620 		       be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
621 		       be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
622 		       be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
623 }
624 
625 static ssize_t show_node_desc(struct device *device,
626 			      struct device_attribute *attr, char *buf)
627 {
628 	struct ib_device *dev = container_of(device, struct ib_device, dev);
629 
630 	return sprintf(buf, "%.64s\n", dev->node_desc);
631 }
632 
633 static ssize_t set_node_desc(struct device *device,
634 			     struct device_attribute *attr,
635 			     const char *buf, size_t count)
636 {
637 	struct ib_device *dev = container_of(device, struct ib_device, dev);
638 	struct ib_device_modify desc = {};
639 	int ret;
640 
641 	if (!dev->modify_device)
642 		return -EIO;
643 
644 	memcpy(desc.node_desc, buf, min_t(int, count, 64));
645 	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
646 	if (ret)
647 		return ret;
648 
649 	return count;
650 }
651 
652 static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
653 static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
654 static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
655 static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
656 
657 static struct device_attribute *ib_class_attributes[] = {
658 	&dev_attr_node_type,
659 	&dev_attr_sys_image_guid,
660 	&dev_attr_node_guid,
661 	&dev_attr_node_desc
662 };
663 
664 static struct class ib_class = {
665 	.name    = "infiniband",
666 	.dev_release = ib_device_release,
667 	.dev_uevent = ib_device_uevent,
668 };
669 
670 /* Show a given an attribute in the statistics group */
671 static ssize_t show_protocol_stat(const struct device *device,
672 			    struct device_attribute *attr, char *buf,
673 			    unsigned offset)
674 {
675 	struct ib_device *dev = container_of(device, struct ib_device, dev);
676 	union rdma_protocol_stats stats;
677 	ssize_t ret;
678 
679 	ret = dev->get_protocol_stats(dev, &stats);
680 	if (ret)
681 		return ret;
682 
683 	return sprintf(buf, "%llu\n",
684 		       (unsigned long long) ((u64 *) &stats)[offset]);
685 }
686 
687 /* generate a read-only iwarp statistics attribute */
688 #define IW_STATS_ENTRY(name)						\
689 static ssize_t show_##name(struct device *device,			\
690 			   struct device_attribute *attr, char *buf)	\
691 {									\
692 	return show_protocol_stat(device, attr, buf,			\
693 				  offsetof(struct iw_protocol_stats, name) / \
694 				  sizeof (u64));			\
695 }									\
696 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
697 
698 IW_STATS_ENTRY(ipInReceives);
699 IW_STATS_ENTRY(ipInHdrErrors);
700 IW_STATS_ENTRY(ipInTooBigErrors);
701 IW_STATS_ENTRY(ipInNoRoutes);
702 IW_STATS_ENTRY(ipInAddrErrors);
703 IW_STATS_ENTRY(ipInUnknownProtos);
704 IW_STATS_ENTRY(ipInTruncatedPkts);
705 IW_STATS_ENTRY(ipInDiscards);
706 IW_STATS_ENTRY(ipInDelivers);
707 IW_STATS_ENTRY(ipOutForwDatagrams);
708 IW_STATS_ENTRY(ipOutRequests);
709 IW_STATS_ENTRY(ipOutDiscards);
710 IW_STATS_ENTRY(ipOutNoRoutes);
711 IW_STATS_ENTRY(ipReasmTimeout);
712 IW_STATS_ENTRY(ipReasmReqds);
713 IW_STATS_ENTRY(ipReasmOKs);
714 IW_STATS_ENTRY(ipReasmFails);
715 IW_STATS_ENTRY(ipFragOKs);
716 IW_STATS_ENTRY(ipFragFails);
717 IW_STATS_ENTRY(ipFragCreates);
718 IW_STATS_ENTRY(ipInMcastPkts);
719 IW_STATS_ENTRY(ipOutMcastPkts);
720 IW_STATS_ENTRY(ipInBcastPkts);
721 IW_STATS_ENTRY(ipOutBcastPkts);
722 IW_STATS_ENTRY(tcpRtoAlgorithm);
723 IW_STATS_ENTRY(tcpRtoMin);
724 IW_STATS_ENTRY(tcpRtoMax);
725 IW_STATS_ENTRY(tcpMaxConn);
726 IW_STATS_ENTRY(tcpActiveOpens);
727 IW_STATS_ENTRY(tcpPassiveOpens);
728 IW_STATS_ENTRY(tcpAttemptFails);
729 IW_STATS_ENTRY(tcpEstabResets);
730 IW_STATS_ENTRY(tcpCurrEstab);
731 IW_STATS_ENTRY(tcpInSegs);
732 IW_STATS_ENTRY(tcpOutSegs);
733 IW_STATS_ENTRY(tcpRetransSegs);
734 IW_STATS_ENTRY(tcpInErrs);
735 IW_STATS_ENTRY(tcpOutRsts);
736 
737 static struct attribute *iw_proto_stats_attrs[] = {
738 	&dev_attr_ipInReceives.attr,
739 	&dev_attr_ipInHdrErrors.attr,
740 	&dev_attr_ipInTooBigErrors.attr,
741 	&dev_attr_ipInNoRoutes.attr,
742 	&dev_attr_ipInAddrErrors.attr,
743 	&dev_attr_ipInUnknownProtos.attr,
744 	&dev_attr_ipInTruncatedPkts.attr,
745 	&dev_attr_ipInDiscards.attr,
746 	&dev_attr_ipInDelivers.attr,
747 	&dev_attr_ipOutForwDatagrams.attr,
748 	&dev_attr_ipOutRequests.attr,
749 	&dev_attr_ipOutDiscards.attr,
750 	&dev_attr_ipOutNoRoutes.attr,
751 	&dev_attr_ipReasmTimeout.attr,
752 	&dev_attr_ipReasmReqds.attr,
753 	&dev_attr_ipReasmOKs.attr,
754 	&dev_attr_ipReasmFails.attr,
755 	&dev_attr_ipFragOKs.attr,
756 	&dev_attr_ipFragFails.attr,
757 	&dev_attr_ipFragCreates.attr,
758 	&dev_attr_ipInMcastPkts.attr,
759 	&dev_attr_ipOutMcastPkts.attr,
760 	&dev_attr_ipInBcastPkts.attr,
761 	&dev_attr_ipOutBcastPkts.attr,
762 	&dev_attr_tcpRtoAlgorithm.attr,
763 	&dev_attr_tcpRtoMin.attr,
764 	&dev_attr_tcpRtoMax.attr,
765 	&dev_attr_tcpMaxConn.attr,
766 	&dev_attr_tcpActiveOpens.attr,
767 	&dev_attr_tcpPassiveOpens.attr,
768 	&dev_attr_tcpAttemptFails.attr,
769 	&dev_attr_tcpEstabResets.attr,
770 	&dev_attr_tcpCurrEstab.attr,
771 	&dev_attr_tcpInSegs.attr,
772 	&dev_attr_tcpOutSegs.attr,
773 	&dev_attr_tcpRetransSegs.attr,
774 	&dev_attr_tcpInErrs.attr,
775 	&dev_attr_tcpOutRsts.attr,
776 	NULL
777 };
778 
779 static struct attribute_group iw_stats_group = {
780 	.name	= "proto_stats",
781 	.attrs	= iw_proto_stats_attrs,
782 };
783 
784 int ib_device_register_sysfs(struct ib_device *device,
785 			     int (*port_callback)(struct ib_device *,
786 						  u8, struct kobject *))
787 {
788 	struct device *class_dev = &device->dev;
789 	int ret;
790 	int i;
791 
792 	class_dev->class      = &ib_class;
793 	class_dev->parent     = device->dma_device;
794 	dev_set_name(class_dev, device->name);
795 	dev_set_drvdata(class_dev, device);
796 
797 	INIT_LIST_HEAD(&device->port_list);
798 
799 	ret = device_register(class_dev);
800 	if (ret)
801 		goto err;
802 
803 	for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
804 		ret = device_create_file(class_dev, ib_class_attributes[i]);
805 		if (ret)
806 			goto err_unregister;
807 	}
808 
809 	device->ports_parent = kobject_create_and_add("ports",
810 					kobject_get(&class_dev->kobj));
811 	if (!device->ports_parent) {
812 		ret = -ENOMEM;
813 		goto err_put;
814 	}
815 
816 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
817 		ret = add_port(device, 0, port_callback);
818 		if (ret)
819 			goto err_put;
820 	} else {
821 		for (i = 1; i <= device->phys_port_cnt; ++i) {
822 			ret = add_port(device, i, port_callback);
823 			if (ret)
824 				goto err_put;
825 		}
826 	}
827 
828 	if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
829 		ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
830 		if (ret)
831 			goto err_put;
832 	}
833 
834 	return 0;
835 
836 err_put:
837 	{
838 		struct kobject *p, *t;
839 		struct ib_port *port;
840 
841 		list_for_each_entry_safe(p, t, &device->port_list, entry) {
842 			list_del(&p->entry);
843 			port = container_of(p, struct ib_port, kobj);
844 			sysfs_remove_group(p, &pma_group);
845 			sysfs_remove_group(p, &port->pkey_group);
846 			sysfs_remove_group(p, &port->gid_group);
847 			kobject_put(p);
848 		}
849 	}
850 
851 	kobject_put(&class_dev->kobj);
852 
853 err_unregister:
854 	device_unregister(class_dev);
855 
856 err:
857 	return ret;
858 }
859 
860 void ib_device_unregister_sysfs(struct ib_device *device)
861 {
862 	struct kobject *p, *t;
863 	struct ib_port *port;
864 
865 	/* Hold kobject until ib_dealloc_device() */
866 	kobject_get(&device->dev.kobj);
867 
868 	list_for_each_entry_safe(p, t, &device->port_list, entry) {
869 		list_del(&p->entry);
870 		port = container_of(p, struct ib_port, kobj);
871 		sysfs_remove_group(p, &pma_group);
872 		sysfs_remove_group(p, &port->pkey_group);
873 		sysfs_remove_group(p, &port->gid_group);
874 		kobject_put(p);
875 	}
876 
877 	kobject_put(device->ports_parent);
878 	device_unregister(&device->dev);
879 }
880 
881 int ib_sysfs_setup(void)
882 {
883 	return class_register(&ib_class);
884 }
885 
886 void ib_sysfs_cleanup(void)
887 {
888 	class_unregister(&ib_class);
889 }
890