xref: /openbmc/linux/drivers/infiniband/core/sysfs.c (revision 943bd984)
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 #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>
46 
47 struct ib_port;
48 
49 struct gid_attr_group {
50 	struct ib_port		*port;
51 	struct kobject		kobj;
52 	struct attribute_group	ndev;
53 	struct attribute_group	type;
54 };
55 struct ib_port {
56 	struct kobject         kobj;
57 	struct ib_device      *ibdev;
58 	struct gid_attr_group *gid_attr_group;
59 	struct attribute_group gid_group;
60 	struct attribute_group pkey_group;
61 	struct attribute_group *pma_table;
62 	struct attribute_group *hw_stats_ag;
63 	struct rdma_hw_stats   *hw_stats;
64 	u8                     port_num;
65 };
66 
67 struct port_attribute {
68 	struct attribute attr;
69 	ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
70 	ssize_t (*store)(struct ib_port *, struct port_attribute *,
71 			 const char *buf, size_t count);
72 };
73 
74 #define PORT_ATTR(_name, _mode, _show, _store) \
75 struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
76 
77 #define PORT_ATTR_RO(_name) \
78 struct port_attribute port_attr_##_name = __ATTR_RO(_name)
79 
80 struct port_table_attribute {
81 	struct port_attribute	attr;
82 	char			name[8];
83 	int			index;
84 	__be16			attr_id;
85 };
86 
87 struct hw_stats_attribute {
88 	struct attribute	attr;
89 	ssize_t			(*show)(struct kobject *kobj,
90 					struct attribute *attr, char *buf);
91 	ssize_t			(*store)(struct kobject *kobj,
92 					 struct attribute *attr,
93 					 const char *buf,
94 					 size_t count);
95 	int			index;
96 	u8			port_num;
97 };
98 
99 static ssize_t port_attr_show(struct kobject *kobj,
100 			      struct attribute *attr, char *buf)
101 {
102 	struct port_attribute *port_attr =
103 		container_of(attr, struct port_attribute, attr);
104 	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
105 
106 	if (!port_attr->show)
107 		return -EIO;
108 
109 	return port_attr->show(p, port_attr, buf);
110 }
111 
112 static ssize_t port_attr_store(struct kobject *kobj,
113 			       struct attribute *attr,
114 			       const char *buf, size_t count)
115 {
116 	struct port_attribute *port_attr =
117 		container_of(attr, struct port_attribute, attr);
118 	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
119 
120 	if (!port_attr->store)
121 		return -EIO;
122 	return port_attr->store(p, port_attr, buf, count);
123 }
124 
125 static const struct sysfs_ops port_sysfs_ops = {
126 	.show	= port_attr_show,
127 	.store	= port_attr_store
128 };
129 
130 static ssize_t gid_attr_show(struct kobject *kobj,
131 			     struct attribute *attr, char *buf)
132 {
133 	struct port_attribute *port_attr =
134 		container_of(attr, struct port_attribute, attr);
135 	struct ib_port *p = container_of(kobj, struct gid_attr_group,
136 					 kobj)->port;
137 
138 	if (!port_attr->show)
139 		return -EIO;
140 
141 	return port_attr->show(p, port_attr, buf);
142 }
143 
144 static const struct sysfs_ops gid_attr_sysfs_ops = {
145 	.show = gid_attr_show
146 };
147 
148 static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
149 			  char *buf)
150 {
151 	struct ib_port_attr attr;
152 	ssize_t ret;
153 
154 	static const char *state_name[] = {
155 		[IB_PORT_NOP]		= "NOP",
156 		[IB_PORT_DOWN]		= "DOWN",
157 		[IB_PORT_INIT]		= "INIT",
158 		[IB_PORT_ARMED]		= "ARMED",
159 		[IB_PORT_ACTIVE]	= "ACTIVE",
160 		[IB_PORT_ACTIVE_DEFER]	= "ACTIVE_DEFER"
161 	};
162 
163 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
164 	if (ret)
165 		return ret;
166 
167 	return sprintf(buf, "%d: %s\n", attr.state,
168 		       attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
169 		       state_name[attr.state] : "UNKNOWN");
170 }
171 
172 static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
173 			char *buf)
174 {
175 	struct ib_port_attr attr;
176 	ssize_t ret;
177 
178 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
179 	if (ret)
180 		return ret;
181 
182 	return sprintf(buf, "0x%x\n", attr.lid);
183 }
184 
185 static ssize_t lid_mask_count_show(struct ib_port *p,
186 				   struct port_attribute *unused,
187 				   char *buf)
188 {
189 	struct ib_port_attr attr;
190 	ssize_t ret;
191 
192 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
193 	if (ret)
194 		return ret;
195 
196 	return sprintf(buf, "%d\n", attr.lmc);
197 }
198 
199 static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
200 			   char *buf)
201 {
202 	struct ib_port_attr attr;
203 	ssize_t ret;
204 
205 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
206 	if (ret)
207 		return ret;
208 
209 	return sprintf(buf, "0x%x\n", attr.sm_lid);
210 }
211 
212 static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
213 			  char *buf)
214 {
215 	struct ib_port_attr attr;
216 	ssize_t ret;
217 
218 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
219 	if (ret)
220 		return ret;
221 
222 	return sprintf(buf, "%d\n", attr.sm_sl);
223 }
224 
225 static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
226 			     char *buf)
227 {
228 	struct ib_port_attr attr;
229 	ssize_t ret;
230 
231 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
232 	if (ret)
233 		return ret;
234 
235 	return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
236 }
237 
238 static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
239 			 char *buf)
240 {
241 	struct ib_port_attr attr;
242 	char *speed = "";
243 	int rate;		/* in deci-Gb/sec */
244 	ssize_t ret;
245 
246 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
247 	if (ret)
248 		return ret;
249 
250 	switch (attr.active_speed) {
251 	case IB_SPEED_DDR:
252 		speed = " DDR";
253 		rate = 50;
254 		break;
255 	case IB_SPEED_QDR:
256 		speed = " QDR";
257 		rate = 100;
258 		break;
259 	case IB_SPEED_FDR10:
260 		speed = " FDR10";
261 		rate = 100;
262 		break;
263 	case IB_SPEED_FDR:
264 		speed = " FDR";
265 		rate = 140;
266 		break;
267 	case IB_SPEED_EDR:
268 		speed = " EDR";
269 		rate = 250;
270 		break;
271 	case IB_SPEED_HDR:
272 		speed = " HDR";
273 		rate = 500;
274 		break;
275 	case IB_SPEED_SDR:
276 	default:		/* default to SDR for invalid rates */
277 		speed = " SDR";
278 		rate = 25;
279 		break;
280 	}
281 
282 	rate *= ib_width_enum_to_int(attr.active_width);
283 	if (rate < 0)
284 		return -EINVAL;
285 
286 	return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
287 		       rate / 10, rate % 10 ? ".5" : "",
288 		       ib_width_enum_to_int(attr.active_width), speed);
289 }
290 
291 static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
292 			       char *buf)
293 {
294 	struct ib_port_attr attr;
295 
296 	ssize_t ret;
297 
298 	ret = ib_query_port(p->ibdev, p->port_num, &attr);
299 	if (ret)
300 		return ret;
301 
302 	switch (attr.phys_state) {
303 	case 1:  return sprintf(buf, "1: Sleep\n");
304 	case 2:  return sprintf(buf, "2: Polling\n");
305 	case 3:  return sprintf(buf, "3: Disabled\n");
306 	case 4:  return sprintf(buf, "4: PortConfigurationTraining\n");
307 	case 5:  return sprintf(buf, "5: LinkUp\n");
308 	case 6:  return sprintf(buf, "6: LinkErrorRecovery\n");
309 	case 7:  return sprintf(buf, "7: Phy Test\n");
310 	default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
311 	}
312 }
313 
314 static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
315 			       char *buf)
316 {
317 	switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
318 	case IB_LINK_LAYER_INFINIBAND:
319 		return sprintf(buf, "%s\n", "InfiniBand");
320 	case IB_LINK_LAYER_ETHERNET:
321 		return sprintf(buf, "%s\n", "Ethernet");
322 	default:
323 		return sprintf(buf, "%s\n", "Unknown");
324 	}
325 }
326 
327 static PORT_ATTR_RO(state);
328 static PORT_ATTR_RO(lid);
329 static PORT_ATTR_RO(lid_mask_count);
330 static PORT_ATTR_RO(sm_lid);
331 static PORT_ATTR_RO(sm_sl);
332 static PORT_ATTR_RO(cap_mask);
333 static PORT_ATTR_RO(rate);
334 static PORT_ATTR_RO(phys_state);
335 static PORT_ATTR_RO(link_layer);
336 
337 static struct attribute *port_default_attrs[] = {
338 	&port_attr_state.attr,
339 	&port_attr_lid.attr,
340 	&port_attr_lid_mask_count.attr,
341 	&port_attr_sm_lid.attr,
342 	&port_attr_sm_sl.attr,
343 	&port_attr_cap_mask.attr,
344 	&port_attr_rate.attr,
345 	&port_attr_phys_state.attr,
346 	&port_attr_link_layer.attr,
347 	NULL
348 };
349 
350 static size_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
351 {
352 	struct net_device *ndev;
353 	size_t ret = -EINVAL;
354 
355 	rcu_read_lock();
356 	ndev = rcu_dereference(gid_attr->ndev);
357 	if (ndev)
358 		ret = sprintf(buf, "%s\n", ndev->name);
359 	rcu_read_unlock();
360 	return ret;
361 }
362 
363 static size_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
364 {
365 	return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
366 }
367 
368 static ssize_t _show_port_gid_attr(
369 	struct ib_port *p, struct port_attribute *attr, char *buf,
370 	size_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
371 {
372 	struct port_table_attribute *tab_attr =
373 		container_of(attr, struct port_table_attribute, attr);
374 	const struct ib_gid_attr *gid_attr;
375 	ssize_t ret;
376 
377 	gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
378 	if (IS_ERR(gid_attr))
379 		return PTR_ERR(gid_attr);
380 
381 	ret = print(gid_attr, buf);
382 	rdma_put_gid_attr(gid_attr);
383 	return ret;
384 }
385 
386 static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
387 			     char *buf)
388 {
389 	struct port_table_attribute *tab_attr =
390 		container_of(attr, struct port_table_attribute, attr);
391 	const struct ib_gid_attr *gid_attr;
392 	ssize_t ret;
393 
394 	gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
395 	if (IS_ERR(gid_attr)) {
396 		const union ib_gid zgid = {};
397 
398 		/* If reading GID fails, it is likely due to GID entry being
399 		 * empty (invalid) or reserved GID in the table.  User space
400 		 * expects to read GID table entries as long as it given index
401 		 * is within GID table size.  Administrative/debugging tool
402 		 * fails to query rest of the GID entries if it hits error
403 		 * while querying a GID of the given index.  To avoid user
404 		 * space throwing such error on fail to read gid, return zero
405 		 * GID as before. This maintains backward compatibility.
406 		 */
407 		return sprintf(buf, "%pI6\n", zgid.raw);
408 	}
409 
410 	ret = sprintf(buf, "%pI6\n", gid_attr->gid.raw);
411 	rdma_put_gid_attr(gid_attr);
412 	return ret;
413 }
414 
415 static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
416 				       struct port_attribute *attr, char *buf)
417 {
418 	return _show_port_gid_attr(p, attr, buf, print_ndev);
419 }
420 
421 static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
422 					   struct port_attribute *attr,
423 					   char *buf)
424 {
425 	return _show_port_gid_attr(p, attr, buf, print_gid_type);
426 }
427 
428 static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
429 			      char *buf)
430 {
431 	struct port_table_attribute *tab_attr =
432 		container_of(attr, struct port_table_attribute, attr);
433 	u16 pkey;
434 	ssize_t ret;
435 
436 	ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
437 	if (ret)
438 		return ret;
439 
440 	return sprintf(buf, "0x%04x\n", pkey);
441 }
442 
443 #define PORT_PMA_ATTR(_name, _counter, _width, _offset)			\
444 struct port_table_attribute port_pma_attr_##_name = {			\
445 	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
446 	.index = (_offset) | ((_width) << 16) | ((_counter) << 24),	\
447 	.attr_id = IB_PMA_PORT_COUNTERS ,				\
448 }
449 
450 #define PORT_PMA_ATTR_EXT(_name, _width, _offset)			\
451 struct port_table_attribute port_pma_attr_ext_##_name = {		\
452 	.attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),	\
453 	.index = (_offset) | ((_width) << 16),				\
454 	.attr_id = IB_PMA_PORT_COUNTERS_EXT ,				\
455 }
456 
457 /*
458  * Get a Perfmgmt MAD block of data.
459  * Returns error code or the number of bytes retrieved.
460  */
461 static int get_perf_mad(struct ib_device *dev, int port_num, __be16 attr,
462 		void *data, int offset, size_t size)
463 {
464 	struct ib_mad *in_mad;
465 	struct ib_mad *out_mad;
466 	size_t mad_size = sizeof(*out_mad);
467 	u16 out_mad_pkey_index = 0;
468 	ssize_t ret;
469 
470 	if (!dev->ops.process_mad)
471 		return -ENOSYS;
472 
473 	in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
474 	out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
475 	if (!in_mad || !out_mad) {
476 		ret = -ENOMEM;
477 		goto out;
478 	}
479 
480 	in_mad->mad_hdr.base_version  = 1;
481 	in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
482 	in_mad->mad_hdr.class_version = 1;
483 	in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
484 	in_mad->mad_hdr.attr_id       = attr;
485 
486 	if (attr != IB_PMA_CLASS_PORT_INFO)
487 		in_mad->data[41] = port_num;	/* PortSelect field */
488 
489 	if ((dev->ops.process_mad(dev, IB_MAD_IGNORE_MKEY,
490 				  port_num, NULL, NULL,
491 				  (const struct ib_mad_hdr *)in_mad, mad_size,
492 				  (struct ib_mad_hdr *)out_mad, &mad_size,
493 				  &out_mad_pkey_index) &
494 	     (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
495 	    (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
496 		ret = -EINVAL;
497 		goto out;
498 	}
499 	memcpy(data, out_mad->data + offset, size);
500 	ret = size;
501 out:
502 	kfree(in_mad);
503 	kfree(out_mad);
504 	return ret;
505 }
506 
507 static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
508 				char *buf)
509 {
510 	struct port_table_attribute *tab_attr =
511 		container_of(attr, struct port_table_attribute, attr);
512 	int offset = tab_attr->index & 0xffff;
513 	int width  = (tab_attr->index >> 16) & 0xff;
514 	ssize_t ret;
515 	u8 data[8];
516 
517 	ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
518 			40 + offset / 8, sizeof(data));
519 	if (ret < 0)
520 		return ret;
521 
522 	switch (width) {
523 	case 4:
524 		ret = sprintf(buf, "%u\n", (*data >>
525 					    (4 - (offset % 8))) & 0xf);
526 		break;
527 	case 8:
528 		ret = sprintf(buf, "%u\n", *data);
529 		break;
530 	case 16:
531 		ret = sprintf(buf, "%u\n",
532 			      be16_to_cpup((__be16 *)data));
533 		break;
534 	case 32:
535 		ret = sprintf(buf, "%u\n",
536 			      be32_to_cpup((__be32 *)data));
537 		break;
538 	case 64:
539 		ret = sprintf(buf, "%llu\n",
540 				be64_to_cpup((__be64 *)data));
541 		break;
542 
543 	default:
544 		ret = 0;
545 	}
546 
547 	return ret;
548 }
549 
550 static PORT_PMA_ATTR(symbol_error		    ,  0, 16,  32);
551 static PORT_PMA_ATTR(link_error_recovery	    ,  1,  8,  48);
552 static PORT_PMA_ATTR(link_downed		    ,  2,  8,  56);
553 static PORT_PMA_ATTR(port_rcv_errors		    ,  3, 16,  64);
554 static PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
555 static PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
556 static PORT_PMA_ATTR(port_xmit_discards		    ,  6, 16, 112);
557 static PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
558 static PORT_PMA_ATTR(port_rcv_constraint_errors	    ,  8,  8, 136);
559 static PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
560 static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
561 static PORT_PMA_ATTR(VL15_dropped		    , 11, 16, 176);
562 static PORT_PMA_ATTR(port_xmit_data		    , 12, 32, 192);
563 static PORT_PMA_ATTR(port_rcv_data		    , 13, 32, 224);
564 static PORT_PMA_ATTR(port_xmit_packets		    , 14, 32, 256);
565 static PORT_PMA_ATTR(port_rcv_packets		    , 15, 32, 288);
566 static PORT_PMA_ATTR(port_xmit_wait		    ,  0, 32, 320);
567 
568 /*
569  * Counters added by extended set
570  */
571 static PORT_PMA_ATTR_EXT(port_xmit_data		    , 64,  64);
572 static PORT_PMA_ATTR_EXT(port_rcv_data		    , 64, 128);
573 static PORT_PMA_ATTR_EXT(port_xmit_packets	    , 64, 192);
574 static PORT_PMA_ATTR_EXT(port_rcv_packets	    , 64, 256);
575 static PORT_PMA_ATTR_EXT(unicast_xmit_packets	    , 64, 320);
576 static PORT_PMA_ATTR_EXT(unicast_rcv_packets	    , 64, 384);
577 static PORT_PMA_ATTR_EXT(multicast_xmit_packets	    , 64, 448);
578 static PORT_PMA_ATTR_EXT(multicast_rcv_packets	    , 64, 512);
579 
580 static struct attribute *pma_attrs[] = {
581 	&port_pma_attr_symbol_error.attr.attr,
582 	&port_pma_attr_link_error_recovery.attr.attr,
583 	&port_pma_attr_link_downed.attr.attr,
584 	&port_pma_attr_port_rcv_errors.attr.attr,
585 	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
586 	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
587 	&port_pma_attr_port_xmit_discards.attr.attr,
588 	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
589 	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
590 	&port_pma_attr_local_link_integrity_errors.attr.attr,
591 	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
592 	&port_pma_attr_VL15_dropped.attr.attr,
593 	&port_pma_attr_port_xmit_data.attr.attr,
594 	&port_pma_attr_port_rcv_data.attr.attr,
595 	&port_pma_attr_port_xmit_packets.attr.attr,
596 	&port_pma_attr_port_rcv_packets.attr.attr,
597 	&port_pma_attr_port_xmit_wait.attr.attr,
598 	NULL
599 };
600 
601 static struct attribute *pma_attrs_ext[] = {
602 	&port_pma_attr_symbol_error.attr.attr,
603 	&port_pma_attr_link_error_recovery.attr.attr,
604 	&port_pma_attr_link_downed.attr.attr,
605 	&port_pma_attr_port_rcv_errors.attr.attr,
606 	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
607 	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
608 	&port_pma_attr_port_xmit_discards.attr.attr,
609 	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
610 	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
611 	&port_pma_attr_local_link_integrity_errors.attr.attr,
612 	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
613 	&port_pma_attr_VL15_dropped.attr.attr,
614 	&port_pma_attr_ext_port_xmit_data.attr.attr,
615 	&port_pma_attr_ext_port_rcv_data.attr.attr,
616 	&port_pma_attr_ext_port_xmit_packets.attr.attr,
617 	&port_pma_attr_port_xmit_wait.attr.attr,
618 	&port_pma_attr_ext_port_rcv_packets.attr.attr,
619 	&port_pma_attr_ext_unicast_rcv_packets.attr.attr,
620 	&port_pma_attr_ext_unicast_xmit_packets.attr.attr,
621 	&port_pma_attr_ext_multicast_rcv_packets.attr.attr,
622 	&port_pma_attr_ext_multicast_xmit_packets.attr.attr,
623 	NULL
624 };
625 
626 static struct attribute *pma_attrs_noietf[] = {
627 	&port_pma_attr_symbol_error.attr.attr,
628 	&port_pma_attr_link_error_recovery.attr.attr,
629 	&port_pma_attr_link_downed.attr.attr,
630 	&port_pma_attr_port_rcv_errors.attr.attr,
631 	&port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
632 	&port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
633 	&port_pma_attr_port_xmit_discards.attr.attr,
634 	&port_pma_attr_port_xmit_constraint_errors.attr.attr,
635 	&port_pma_attr_port_rcv_constraint_errors.attr.attr,
636 	&port_pma_attr_local_link_integrity_errors.attr.attr,
637 	&port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
638 	&port_pma_attr_VL15_dropped.attr.attr,
639 	&port_pma_attr_ext_port_xmit_data.attr.attr,
640 	&port_pma_attr_ext_port_rcv_data.attr.attr,
641 	&port_pma_attr_ext_port_xmit_packets.attr.attr,
642 	&port_pma_attr_ext_port_rcv_packets.attr.attr,
643 	&port_pma_attr_port_xmit_wait.attr.attr,
644 	NULL
645 };
646 
647 static struct attribute_group pma_group = {
648 	.name  = "counters",
649 	.attrs  = pma_attrs
650 };
651 
652 static struct attribute_group pma_group_ext = {
653 	.name  = "counters",
654 	.attrs  = pma_attrs_ext
655 };
656 
657 static struct attribute_group pma_group_noietf = {
658 	.name  = "counters",
659 	.attrs  = pma_attrs_noietf
660 };
661 
662 static void ib_port_release(struct kobject *kobj)
663 {
664 	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
665 	struct attribute *a;
666 	int i;
667 
668 	if (p->gid_group.attrs) {
669 		for (i = 0; (a = p->gid_group.attrs[i]); ++i)
670 			kfree(a);
671 
672 		kfree(p->gid_group.attrs);
673 	}
674 
675 	if (p->pkey_group.attrs) {
676 		for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
677 			kfree(a);
678 
679 		kfree(p->pkey_group.attrs);
680 	}
681 
682 	kfree(p);
683 }
684 
685 static void ib_port_gid_attr_release(struct kobject *kobj)
686 {
687 	struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
688 						kobj);
689 	struct attribute *a;
690 	int i;
691 
692 	if (g->ndev.attrs) {
693 		for (i = 0; (a = g->ndev.attrs[i]); ++i)
694 			kfree(a);
695 
696 		kfree(g->ndev.attrs);
697 	}
698 
699 	if (g->type.attrs) {
700 		for (i = 0; (a = g->type.attrs[i]); ++i)
701 			kfree(a);
702 
703 		kfree(g->type.attrs);
704 	}
705 
706 	kfree(g);
707 }
708 
709 static struct kobj_type port_type = {
710 	.release       = ib_port_release,
711 	.sysfs_ops     = &port_sysfs_ops,
712 	.default_attrs = port_default_attrs
713 };
714 
715 static struct kobj_type gid_attr_type = {
716 	.sysfs_ops      = &gid_attr_sysfs_ops,
717 	.release        = ib_port_gid_attr_release
718 };
719 
720 static struct attribute **
721 alloc_group_attrs(ssize_t (*show)(struct ib_port *,
722 				  struct port_attribute *, char *buf),
723 		  int len)
724 {
725 	struct attribute **tab_attr;
726 	struct port_table_attribute *element;
727 	int i;
728 
729 	tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
730 	if (!tab_attr)
731 		return NULL;
732 
733 	for (i = 0; i < len; i++) {
734 		element = kzalloc(sizeof(struct port_table_attribute),
735 				  GFP_KERNEL);
736 		if (!element)
737 			goto err;
738 
739 		if (snprintf(element->name, sizeof(element->name),
740 			     "%d", i) >= sizeof(element->name)) {
741 			kfree(element);
742 			goto err;
743 		}
744 
745 		element->attr.attr.name  = element->name;
746 		element->attr.attr.mode  = S_IRUGO;
747 		element->attr.show       = show;
748 		element->index		 = i;
749 		sysfs_attr_init(&element->attr.attr);
750 
751 		tab_attr[i] = &element->attr.attr;
752 	}
753 
754 	return tab_attr;
755 
756 err:
757 	while (--i >= 0)
758 		kfree(tab_attr[i]);
759 	kfree(tab_attr);
760 	return NULL;
761 }
762 
763 /*
764  * Figure out which counter table to use depending on
765  * the device capabilities.
766  */
767 static struct attribute_group *get_counter_table(struct ib_device *dev,
768 						 int port_num)
769 {
770 	struct ib_class_port_info cpi;
771 
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 			/* We have extended counters */
776 			return &pma_group_ext;
777 
778 		if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH_NOIETF)
779 			/* But not the IETF ones */
780 			return &pma_group_noietf;
781 	}
782 
783 	/* Fall back to normal counters */
784 	return &pma_group;
785 }
786 
787 static int update_hw_stats(struct ib_device *dev, struct rdma_hw_stats *stats,
788 			   u8 port_num, int index)
789 {
790 	int ret;
791 
792 	if (time_is_after_eq_jiffies(stats->timestamp + stats->lifespan))
793 		return 0;
794 	ret = dev->ops.get_hw_stats(dev, stats, port_num, index);
795 	if (ret < 0)
796 		return ret;
797 	if (ret == stats->num_counters)
798 		stats->timestamp = jiffies;
799 
800 	return 0;
801 }
802 
803 static ssize_t print_hw_stat(struct rdma_hw_stats *stats, int index, char *buf)
804 {
805 	return sprintf(buf, "%llu\n", stats->value[index]);
806 }
807 
808 static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr,
809 			     char *buf)
810 {
811 	struct ib_device *dev;
812 	struct ib_port *port;
813 	struct hw_stats_attribute *hsa;
814 	struct rdma_hw_stats *stats;
815 	int ret;
816 
817 	hsa = container_of(attr, struct hw_stats_attribute, attr);
818 	if (!hsa->port_num) {
819 		dev = container_of((struct device *)kobj,
820 				   struct ib_device, dev);
821 		stats = dev->hw_stats;
822 	} else {
823 		port = container_of(kobj, struct ib_port, kobj);
824 		dev = port->ibdev;
825 		stats = port->hw_stats;
826 	}
827 	mutex_lock(&stats->lock);
828 	ret = update_hw_stats(dev, stats, hsa->port_num, hsa->index);
829 	if (ret)
830 		goto unlock;
831 	ret = print_hw_stat(stats, hsa->index, buf);
832 unlock:
833 	mutex_unlock(&stats->lock);
834 
835 	return ret;
836 }
837 
838 static ssize_t show_stats_lifespan(struct kobject *kobj,
839 				   struct attribute *attr,
840 				   char *buf)
841 {
842 	struct hw_stats_attribute *hsa;
843 	struct rdma_hw_stats *stats;
844 	int msecs;
845 
846 	hsa = container_of(attr, struct hw_stats_attribute, attr);
847 	if (!hsa->port_num) {
848 		struct ib_device *dev = container_of((struct device *)kobj,
849 						     struct ib_device, dev);
850 
851 		stats = dev->hw_stats;
852 	} else {
853 		struct ib_port *p = container_of(kobj, struct ib_port, kobj);
854 
855 		stats = p->hw_stats;
856 	}
857 
858 	mutex_lock(&stats->lock);
859 	msecs = jiffies_to_msecs(stats->lifespan);
860 	mutex_unlock(&stats->lock);
861 
862 	return sprintf(buf, "%d\n", msecs);
863 }
864 
865 static ssize_t set_stats_lifespan(struct kobject *kobj,
866 				  struct attribute *attr,
867 				  const char *buf, size_t count)
868 {
869 	struct hw_stats_attribute *hsa;
870 	struct rdma_hw_stats *stats;
871 	int msecs;
872 	int jiffies;
873 	int ret;
874 
875 	ret = kstrtoint(buf, 10, &msecs);
876 	if (ret)
877 		return ret;
878 	if (msecs < 0 || msecs > 10000)
879 		return -EINVAL;
880 	jiffies = msecs_to_jiffies(msecs);
881 	hsa = container_of(attr, struct hw_stats_attribute, attr);
882 	if (!hsa->port_num) {
883 		struct ib_device *dev = container_of((struct device *)kobj,
884 						     struct ib_device, dev);
885 
886 		stats = dev->hw_stats;
887 	} else {
888 		struct ib_port *p = container_of(kobj, struct ib_port, kobj);
889 
890 		stats = p->hw_stats;
891 	}
892 
893 	mutex_lock(&stats->lock);
894 	stats->lifespan = jiffies;
895 	mutex_unlock(&stats->lock);
896 
897 	return count;
898 }
899 
900 static void free_hsag(struct kobject *kobj, struct attribute_group *attr_group)
901 {
902 	struct attribute **attr;
903 
904 	sysfs_remove_group(kobj, attr_group);
905 
906 	for (attr = attr_group->attrs; *attr; attr++)
907 		kfree(*attr);
908 	kfree(attr_group);
909 }
910 
911 static struct attribute *alloc_hsa(int index, u8 port_num, const char *name)
912 {
913 	struct hw_stats_attribute *hsa;
914 
915 	hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
916 	if (!hsa)
917 		return NULL;
918 
919 	hsa->attr.name = (char *)name;
920 	hsa->attr.mode = S_IRUGO;
921 	hsa->show = show_hw_stats;
922 	hsa->store = NULL;
923 	hsa->index = index;
924 	hsa->port_num = port_num;
925 
926 	return &hsa->attr;
927 }
928 
929 static struct attribute *alloc_hsa_lifespan(char *name, u8 port_num)
930 {
931 	struct hw_stats_attribute *hsa;
932 
933 	hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
934 	if (!hsa)
935 		return NULL;
936 
937 	hsa->attr.name = name;
938 	hsa->attr.mode = S_IWUSR | S_IRUGO;
939 	hsa->show = show_stats_lifespan;
940 	hsa->store = set_stats_lifespan;
941 	hsa->index = 0;
942 	hsa->port_num = port_num;
943 
944 	return &hsa->attr;
945 }
946 
947 static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
948 			   u8 port_num)
949 {
950 	struct attribute_group *hsag;
951 	struct rdma_hw_stats *stats;
952 	int i, ret;
953 
954 	stats = device->ops.alloc_hw_stats(device, port_num);
955 
956 	if (!stats)
957 		return;
958 
959 	if (!stats->names || stats->num_counters <= 0)
960 		goto err_free_stats;
961 
962 	/*
963 	 * Two extra attribue elements here, one for the lifespan entry and
964 	 * one to NULL terminate the list for the sysfs core code
965 	 */
966 	hsag = kzalloc(sizeof(*hsag) +
967 		       sizeof(void *) * (stats->num_counters + 2),
968 		       GFP_KERNEL);
969 	if (!hsag)
970 		goto err_free_stats;
971 
972 	ret = device->ops.get_hw_stats(device, stats, port_num,
973 				       stats->num_counters);
974 	if (ret != stats->num_counters)
975 		goto err_free_hsag;
976 
977 	stats->timestamp = jiffies;
978 
979 	hsag->name = "hw_counters";
980 	hsag->attrs = (void *)hsag + sizeof(*hsag);
981 
982 	for (i = 0; i < stats->num_counters; i++) {
983 		hsag->attrs[i] = alloc_hsa(i, port_num, stats->names[i]);
984 		if (!hsag->attrs[i])
985 			goto err;
986 		sysfs_attr_init(hsag->attrs[i]);
987 	}
988 
989 	mutex_init(&stats->lock);
990 	/* treat an error here as non-fatal */
991 	hsag->attrs[i] = alloc_hsa_lifespan("lifespan", port_num);
992 	if (hsag->attrs[i])
993 		sysfs_attr_init(hsag->attrs[i]);
994 
995 	if (port) {
996 		struct kobject *kobj = &port->kobj;
997 		ret = sysfs_create_group(kobj, hsag);
998 		if (ret)
999 			goto err;
1000 		port->hw_stats_ag = hsag;
1001 		port->hw_stats = stats;
1002 	} else {
1003 		struct kobject *kobj = &device->dev.kobj;
1004 		ret = sysfs_create_group(kobj, hsag);
1005 		if (ret)
1006 			goto err;
1007 		device->hw_stats_ag = hsag;
1008 		device->hw_stats = stats;
1009 	}
1010 
1011 	return;
1012 
1013 err:
1014 	for (; i >= 0; i--)
1015 		kfree(hsag->attrs[i]);
1016 err_free_hsag:
1017 	kfree(hsag);
1018 err_free_stats:
1019 	kfree(stats);
1020 	return;
1021 }
1022 
1023 static int add_port(struct ib_core_device *coredev, int port_num)
1024 {
1025 	struct ib_device *device = rdma_device_to_ibdev(&coredev->dev);
1026 	bool is_full_dev = &device->coredev == coredev;
1027 	struct ib_port *p;
1028 	struct ib_port_attr attr;
1029 	int i;
1030 	int ret;
1031 
1032 	ret = ib_query_port(device, port_num, &attr);
1033 	if (ret)
1034 		return ret;
1035 
1036 	p = kzalloc(sizeof *p, GFP_KERNEL);
1037 	if (!p)
1038 		return -ENOMEM;
1039 
1040 	p->ibdev      = device;
1041 	p->port_num   = port_num;
1042 
1043 	ret = kobject_init_and_add(&p->kobj, &port_type,
1044 				   coredev->ports_kobj,
1045 				   "%d", port_num);
1046 	if (ret) {
1047 		kfree(p);
1048 		return ret;
1049 	}
1050 
1051 	p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
1052 	if (!p->gid_attr_group) {
1053 		ret = -ENOMEM;
1054 		goto err_put;
1055 	}
1056 
1057 	p->gid_attr_group->port = p;
1058 	ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
1059 				   &p->kobj, "gid_attrs");
1060 	if (ret) {
1061 		kfree(p->gid_attr_group);
1062 		goto err_put;
1063 	}
1064 
1065 	if (device->ops.process_mad && is_full_dev) {
1066 		p->pma_table = get_counter_table(device, port_num);
1067 		ret = sysfs_create_group(&p->kobj, p->pma_table);
1068 		if (ret)
1069 			goto err_put_gid_attrs;
1070 	}
1071 
1072 	p->gid_group.name  = "gids";
1073 	p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
1074 	if (!p->gid_group.attrs) {
1075 		ret = -ENOMEM;
1076 		goto err_remove_pma;
1077 	}
1078 
1079 	ret = sysfs_create_group(&p->kobj, &p->gid_group);
1080 	if (ret)
1081 		goto err_free_gid;
1082 
1083 	p->gid_attr_group->ndev.name = "ndevs";
1084 	p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
1085 							  attr.gid_tbl_len);
1086 	if (!p->gid_attr_group->ndev.attrs) {
1087 		ret = -ENOMEM;
1088 		goto err_remove_gid;
1089 	}
1090 
1091 	ret = sysfs_create_group(&p->gid_attr_group->kobj,
1092 				 &p->gid_attr_group->ndev);
1093 	if (ret)
1094 		goto err_free_gid_ndev;
1095 
1096 	p->gid_attr_group->type.name = "types";
1097 	p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
1098 							  attr.gid_tbl_len);
1099 	if (!p->gid_attr_group->type.attrs) {
1100 		ret = -ENOMEM;
1101 		goto err_remove_gid_ndev;
1102 	}
1103 
1104 	ret = sysfs_create_group(&p->gid_attr_group->kobj,
1105 				 &p->gid_attr_group->type);
1106 	if (ret)
1107 		goto err_free_gid_type;
1108 
1109 	p->pkey_group.name  = "pkeys";
1110 	p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
1111 						attr.pkey_tbl_len);
1112 	if (!p->pkey_group.attrs) {
1113 		ret = -ENOMEM;
1114 		goto err_remove_gid_type;
1115 	}
1116 
1117 	ret = sysfs_create_group(&p->kobj, &p->pkey_group);
1118 	if (ret)
1119 		goto err_free_pkey;
1120 
1121 	if (device->ops.init_port && is_full_dev) {
1122 		ret = device->ops.init_port(device, port_num, &p->kobj);
1123 		if (ret)
1124 			goto err_remove_pkey;
1125 	}
1126 
1127 	/*
1128 	 * If port == 0, it means hw_counters are per device and not per
1129 	 * port, so holder should be device. Therefore skip per port conunter
1130 	 * initialization.
1131 	 */
1132 	if (device->ops.alloc_hw_stats && port_num && is_full_dev)
1133 		setup_hw_stats(device, p, port_num);
1134 
1135 	list_add_tail(&p->kobj.entry, &coredev->port_list);
1136 
1137 	kobject_uevent(&p->kobj, KOBJ_ADD);
1138 	return 0;
1139 
1140 err_remove_pkey:
1141 	sysfs_remove_group(&p->kobj, &p->pkey_group);
1142 
1143 err_free_pkey:
1144 	for (i = 0; i < attr.pkey_tbl_len; ++i)
1145 		kfree(p->pkey_group.attrs[i]);
1146 
1147 	kfree(p->pkey_group.attrs);
1148 	p->pkey_group.attrs = NULL;
1149 
1150 err_remove_gid_type:
1151 	sysfs_remove_group(&p->gid_attr_group->kobj,
1152 			   &p->gid_attr_group->type);
1153 
1154 err_free_gid_type:
1155 	for (i = 0; i < attr.gid_tbl_len; ++i)
1156 		kfree(p->gid_attr_group->type.attrs[i]);
1157 
1158 	kfree(p->gid_attr_group->type.attrs);
1159 	p->gid_attr_group->type.attrs = NULL;
1160 
1161 err_remove_gid_ndev:
1162 	sysfs_remove_group(&p->gid_attr_group->kobj,
1163 			   &p->gid_attr_group->ndev);
1164 
1165 err_free_gid_ndev:
1166 	for (i = 0; i < attr.gid_tbl_len; ++i)
1167 		kfree(p->gid_attr_group->ndev.attrs[i]);
1168 
1169 	kfree(p->gid_attr_group->ndev.attrs);
1170 	p->gid_attr_group->ndev.attrs = NULL;
1171 
1172 err_remove_gid:
1173 	sysfs_remove_group(&p->kobj, &p->gid_group);
1174 
1175 err_free_gid:
1176 	for (i = 0; i < attr.gid_tbl_len; ++i)
1177 		kfree(p->gid_group.attrs[i]);
1178 
1179 	kfree(p->gid_group.attrs);
1180 	p->gid_group.attrs = NULL;
1181 
1182 err_remove_pma:
1183 	if (p->pma_table)
1184 		sysfs_remove_group(&p->kobj, p->pma_table);
1185 
1186 err_put_gid_attrs:
1187 	kobject_put(&p->gid_attr_group->kobj);
1188 
1189 err_put:
1190 	kobject_put(&p->kobj);
1191 	return ret;
1192 }
1193 
1194 static ssize_t node_type_show(struct device *device,
1195 			      struct device_attribute *attr, char *buf)
1196 {
1197 	struct ib_device *dev = rdma_device_to_ibdev(device);
1198 
1199 	switch (dev->node_type) {
1200 	case RDMA_NODE_IB_CA:	  return sprintf(buf, "%d: CA\n", dev->node_type);
1201 	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);
1204 	case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
1205 	case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
1206 	default:		  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
1207 	}
1208 }
1209 static DEVICE_ATTR_RO(node_type);
1210 
1211 static ssize_t sys_image_guid_show(struct device *device,
1212 				   struct device_attribute *dev_attr, char *buf)
1213 {
1214 	struct ib_device *dev = rdma_device_to_ibdev(device);
1215 
1216 	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
1217 		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
1218 		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
1219 		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
1220 		       be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
1221 }
1222 static DEVICE_ATTR_RO(sys_image_guid);
1223 
1224 static ssize_t node_guid_show(struct device *device,
1225 			      struct device_attribute *attr, char *buf)
1226 {
1227 	struct ib_device *dev = rdma_device_to_ibdev(device);
1228 
1229 	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
1230 		       be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
1231 		       be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
1232 		       be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
1233 		       be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
1234 }
1235 static DEVICE_ATTR_RO(node_guid);
1236 
1237 static ssize_t node_desc_show(struct device *device,
1238 			      struct device_attribute *attr, char *buf)
1239 {
1240 	struct ib_device *dev = rdma_device_to_ibdev(device);
1241 
1242 	return sprintf(buf, "%.64s\n", dev->node_desc);
1243 }
1244 
1245 static ssize_t node_desc_store(struct device *device,
1246 			       struct device_attribute *attr,
1247 			       const char *buf, size_t count)
1248 {
1249 	struct ib_device *dev = rdma_device_to_ibdev(device);
1250 	struct ib_device_modify desc = {};
1251 	int ret;
1252 
1253 	if (!dev->ops.modify_device)
1254 		return -EIO;
1255 
1256 	memcpy(desc.node_desc, buf, min_t(int, count, IB_DEVICE_NODE_DESC_MAX));
1257 	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
1258 	if (ret)
1259 		return ret;
1260 
1261 	return count;
1262 }
1263 static DEVICE_ATTR_RW(node_desc);
1264 
1265 static ssize_t fw_ver_show(struct device *device, struct device_attribute *attr,
1266 			   char *buf)
1267 {
1268 	struct ib_device *dev = rdma_device_to_ibdev(device);
1269 
1270 	ib_get_device_fw_str(dev, buf);
1271 	strlcat(buf, "\n", IB_FW_VERSION_NAME_MAX);
1272 	return strlen(buf);
1273 }
1274 static DEVICE_ATTR_RO(fw_ver);
1275 
1276 static struct attribute *ib_dev_attrs[] = {
1277 	&dev_attr_node_type.attr,
1278 	&dev_attr_node_guid.attr,
1279 	&dev_attr_sys_image_guid.attr,
1280 	&dev_attr_fw_ver.attr,
1281 	&dev_attr_node_desc.attr,
1282 	NULL,
1283 };
1284 
1285 const struct attribute_group ib_dev_attr_group = {
1286 	.attrs = ib_dev_attrs,
1287 };
1288 
1289 void ib_free_port_attrs(struct ib_core_device *coredev)
1290 {
1291 	struct kobject *p, *t;
1292 
1293 	list_for_each_entry_safe(p, t, &coredev->port_list, entry) {
1294 		struct ib_port *port = container_of(p, struct ib_port, kobj);
1295 
1296 		list_del(&p->entry);
1297 		if (port->hw_stats_ag)
1298 			free_hsag(&port->kobj, port->hw_stats_ag);
1299 		kfree(port->hw_stats);
1300 
1301 		if (port->pma_table)
1302 			sysfs_remove_group(p, port->pma_table);
1303 		sysfs_remove_group(p, &port->pkey_group);
1304 		sysfs_remove_group(p, &port->gid_group);
1305 		sysfs_remove_group(&port->gid_attr_group->kobj,
1306 				   &port->gid_attr_group->ndev);
1307 		sysfs_remove_group(&port->gid_attr_group->kobj,
1308 				   &port->gid_attr_group->type);
1309 		kobject_put(&port->gid_attr_group->kobj);
1310 		kobject_put(p);
1311 	}
1312 
1313 	kobject_put(coredev->ports_kobj);
1314 }
1315 
1316 int ib_setup_port_attrs(struct ib_core_device *coredev)
1317 {
1318 	struct ib_device *device = rdma_device_to_ibdev(&coredev->dev);
1319 	unsigned int port;
1320 	int ret;
1321 
1322 	coredev->ports_kobj = kobject_create_and_add("ports",
1323 						     &coredev->dev.kobj);
1324 	if (!coredev->ports_kobj)
1325 		return -ENOMEM;
1326 
1327 	rdma_for_each_port (device, port) {
1328 		ret = add_port(coredev, port);
1329 		if (ret)
1330 			goto err_put;
1331 	}
1332 
1333 	return 0;
1334 
1335 err_put:
1336 	ib_free_port_attrs(coredev);
1337 	return ret;
1338 }
1339 
1340 int ib_device_register_sysfs(struct ib_device *device)
1341 {
1342 	int ret;
1343 
1344 	ret = ib_setup_port_attrs(&device->coredev);
1345 	if (ret)
1346 		return ret;
1347 
1348 	if (device->ops.alloc_hw_stats)
1349 		setup_hw_stats(device, NULL, 0);
1350 
1351 	return 0;
1352 }
1353 
1354 void ib_device_unregister_sysfs(struct ib_device *device)
1355 {
1356 	if (device->hw_stats_ag)
1357 		free_hsag(&device->dev.kobj, device->hw_stats_ag);
1358 	kfree(device->hw_stats);
1359 
1360 	ib_free_port_attrs(&device->coredev);
1361 }
1362 
1363 /**
1364  * ib_port_register_module_stat - add module counters under relevant port
1365  *  of IB device.
1366  *
1367  * @device: IB device to add counters
1368  * @port_num: valid port number
1369  * @kobj: pointer to the kobject to initialize
1370  * @ktype: pointer to the ktype for this kobject.
1371  * @name: the name of the kobject
1372  */
1373 int ib_port_register_module_stat(struct ib_device *device, u8 port_num,
1374 				 struct kobject *kobj, struct kobj_type *ktype,
1375 				 const char *name)
1376 {
1377 	struct kobject *p, *t;
1378 	int ret;
1379 
1380 	list_for_each_entry_safe(p, t, &device->coredev.port_list, entry) {
1381 		struct ib_port *port = container_of(p, struct ib_port, kobj);
1382 
1383 		if (port->port_num != port_num)
1384 			continue;
1385 
1386 		ret = kobject_init_and_add(kobj, ktype, &port->kobj, "%s",
1387 					   name);
1388 		if (ret)
1389 			return ret;
1390 	}
1391 
1392 	return 0;
1393 }
1394 EXPORT_SYMBOL(ib_port_register_module_stat);
1395 
1396 /**
1397  * ib_port_unregister_module_stat - release module counters
1398  * @kobj: pointer to the kobject to release
1399  */
1400 void ib_port_unregister_module_stat(struct kobject *kobj)
1401 {
1402 	kobject_put(kobj);
1403 }
1404 EXPORT_SYMBOL(ib_port_unregister_module_stat);
1405