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