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