1219820Sjeff/*
2219820Sjeff * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3219820Sjeff * Copyright (c) 2005 Mellanox Technologies Ltd.  All rights reserved.
4219820Sjeff * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5219820Sjeff *
6219820Sjeff * This software is available to you under a choice of one of two
7219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
8219820Sjeff * General Public License (GPL) Version 2, available from the file
9219820Sjeff * COPYING in the main directory of this source tree, or the
10219820Sjeff * OpenIB.org BSD license below:
11219820Sjeff *
12219820Sjeff *     Redistribution and use in source and binary forms, with or
13219820Sjeff *     without modification, are permitted provided that the following
14219820Sjeff *     conditions are met:
15219820Sjeff *
16219820Sjeff *      - Redistributions of source code must retain the above
17219820Sjeff *        copyright notice, this list of conditions and the following
18219820Sjeff *        disclaimer.
19219820Sjeff *
20219820Sjeff *      - Redistributions in binary form must reproduce the above
21219820Sjeff *        copyright notice, this list of conditions and the following
22219820Sjeff *        disclaimer in the documentation and/or other materials
23219820Sjeff *        provided with the distribution.
24219820Sjeff *
25219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32219820Sjeff * SOFTWARE.
33219820Sjeff */
34219820Sjeff
35219820Sjeff#include "core_priv.h"
36219820Sjeff
37219820Sjeff#include <linux/slab.h>
38219820Sjeff#include <linux/string.h>
39271127Shselasky#include <linux/fs.h>
40219820Sjeff
41219820Sjeff#include <rdma/ib_mad.h>
42255932Salfred#include <rdma/ib_pma.h>
43219820Sjeff
44219820Sjeffstruct ib_port {
45219820Sjeff	struct kobject         kobj;
46219820Sjeff	struct ib_device      *ibdev;
47219820Sjeff	struct attribute_group gid_group;
48219820Sjeff	struct attribute_group pkey_group;
49219820Sjeff	u8                     port_num;
50219820Sjeff};
51219820Sjeff
52219820Sjeffstruct port_attribute {
53219820Sjeff	struct attribute attr;
54219820Sjeff	ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
55219820Sjeff	ssize_t (*store)(struct ib_port *, struct port_attribute *,
56219820Sjeff			 const char *buf, size_t count);
57219820Sjeff};
58219820Sjeff
59219820Sjeff#define PORT_ATTR(_name, _mode, _show, _store) \
60219820Sjeffstruct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
61219820Sjeff
62219820Sjeff#define PORT_ATTR_RO(_name) \
63219820Sjeffstruct port_attribute port_attr_##_name = __ATTR_RO(_name)
64219820Sjeff
65219820Sjeffstruct port_table_attribute {
66219820Sjeff	struct port_attribute	attr;
67219820Sjeff	char			name[8];
68219820Sjeff	int			index;
69219820Sjeff};
70219820Sjeff
71219820Sjeffstatic ssize_t port_attr_show(struct kobject *kobj,
72219820Sjeff			      struct attribute *attr, char *buf)
73219820Sjeff{
74219820Sjeff	struct port_attribute *port_attr =
75219820Sjeff		container_of(attr, struct port_attribute, attr);
76219820Sjeff	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
77219820Sjeff
78219820Sjeff	if (!port_attr->show)
79219820Sjeff		return -EIO;
80219820Sjeff
81219820Sjeff	return port_attr->show(p, port_attr, buf);
82219820Sjeff}
83219820Sjeff
84219820Sjeffstatic const struct sysfs_ops port_sysfs_ops = {
85219820Sjeff	.show = port_attr_show
86219820Sjeff};
87219820Sjeff
88219820Sjeffstatic ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
89219820Sjeff			  char *buf)
90219820Sjeff{
91219820Sjeff	struct ib_port_attr attr;
92219820Sjeff	ssize_t ret;
93219820Sjeff
94219820Sjeff	static const char *state_name[] = {
95219820Sjeff		[IB_PORT_NOP]		= "NOP",
96219820Sjeff		[IB_PORT_DOWN]		= "DOWN",
97219820Sjeff		[IB_PORT_INIT]		= "INIT",
98219820Sjeff		[IB_PORT_ARMED]		= "ARMED",
99219820Sjeff		[IB_PORT_ACTIVE]	= "ACTIVE",
100219820Sjeff		[IB_PORT_ACTIVE_DEFER]	= "ACTIVE_DEFER"
101219820Sjeff	};
102219820Sjeff
103219820Sjeff	ret = ib_query_port(p->ibdev, p->port_num, &attr);
104219820Sjeff	if (ret)
105219820Sjeff		return ret;
106219820Sjeff
107219820Sjeff	return sprintf(buf, "%d: %s\n", attr.state,
108255932Salfred		       attr.state < ARRAY_SIZE(state_name) ?
109219820Sjeff		       state_name[attr.state] : "UNKNOWN");
110219820Sjeff}
111219820Sjeff
112219820Sjeffstatic ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
113219820Sjeff			char *buf)
114219820Sjeff{
115219820Sjeff	struct ib_port_attr attr;
116219820Sjeff	ssize_t ret;
117219820Sjeff
118219820Sjeff	ret = ib_query_port(p->ibdev, p->port_num, &attr);
119219820Sjeff	if (ret)
120219820Sjeff		return ret;
121219820Sjeff
122219820Sjeff	return sprintf(buf, "0x%x\n", attr.lid);
123219820Sjeff}
124219820Sjeff
125219820Sjeffstatic ssize_t lid_mask_count_show(struct ib_port *p,
126219820Sjeff				   struct port_attribute *unused,
127219820Sjeff				   char *buf)
128219820Sjeff{
129219820Sjeff	struct ib_port_attr attr;
130219820Sjeff	ssize_t ret;
131219820Sjeff
132219820Sjeff	ret = ib_query_port(p->ibdev, p->port_num, &attr);
133219820Sjeff	if (ret)
134219820Sjeff		return ret;
135219820Sjeff
136219820Sjeff	return sprintf(buf, "%d\n", attr.lmc);
137219820Sjeff}
138219820Sjeff
139219820Sjeffstatic ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
140219820Sjeff			   char *buf)
141219820Sjeff{
142219820Sjeff	struct ib_port_attr attr;
143219820Sjeff	ssize_t ret;
144219820Sjeff
145219820Sjeff	ret = ib_query_port(p->ibdev, p->port_num, &attr);
146219820Sjeff	if (ret)
147219820Sjeff		return ret;
148219820Sjeff
149219820Sjeff	return sprintf(buf, "0x%x\n", attr.sm_lid);
150219820Sjeff}
151219820Sjeff
152219820Sjeffstatic ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
153219820Sjeff			  char *buf)
154219820Sjeff{
155219820Sjeff	struct ib_port_attr attr;
156219820Sjeff	ssize_t ret;
157219820Sjeff
158219820Sjeff	ret = ib_query_port(p->ibdev, p->port_num, &attr);
159219820Sjeff	if (ret)
160219820Sjeff		return ret;
161219820Sjeff
162219820Sjeff	return sprintf(buf, "%d\n", attr.sm_sl);
163219820Sjeff}
164219820Sjeff
165219820Sjeffstatic ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
166219820Sjeff			     char *buf)
167219820Sjeff{
168219820Sjeff	struct ib_port_attr attr;
169219820Sjeff	ssize_t ret;
170219820Sjeff
171219820Sjeff	ret = ib_query_port(p->ibdev, p->port_num, &attr);
172219820Sjeff	if (ret)
173219820Sjeff		return ret;
174219820Sjeff
175219820Sjeff	return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
176219820Sjeff}
177219820Sjeff
178219820Sjeffstatic ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
179219820Sjeff			 char *buf)
180219820Sjeff{
181219820Sjeff	struct ib_port_attr attr;
182219820Sjeff	char *speed = "";
183219820Sjeff	int rate;
184219820Sjeff	ssize_t ret;
185219820Sjeff
186219820Sjeff	ret = ib_query_port(p->ibdev, p->port_num, &attr);
187219820Sjeff	if (ret)
188219820Sjeff		return ret;
189219820Sjeff
190219820Sjeff	switch (attr.active_speed) {
191219820Sjeff	case 2: speed = " DDR"; break;
192219820Sjeff	case 4: speed = " QDR"; break;
193219820Sjeff	}
194219820Sjeff
195219820Sjeff	rate = 25 * ib_width_enum_to_int(attr.active_width) * attr.active_speed;
196219820Sjeff	if (rate < 0)
197219820Sjeff		return -EINVAL;
198219820Sjeff
199219820Sjeff	return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
200219820Sjeff		       rate / 10, rate % 10 ? ".5" : "",
201219820Sjeff		       ib_width_enum_to_int(attr.active_width), speed);
202219820Sjeff}
203219820Sjeff
204219820Sjeffstatic ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
205219820Sjeff			       char *buf)
206219820Sjeff{
207219820Sjeff	struct ib_port_attr attr;
208219820Sjeff
209219820Sjeff	ssize_t ret;
210219820Sjeff
211219820Sjeff	ret = ib_query_port(p->ibdev, p->port_num, &attr);
212219820Sjeff	if (ret)
213219820Sjeff		return ret;
214219820Sjeff
215219820Sjeff	switch (attr.phys_state) {
216219820Sjeff	case 1:  return sprintf(buf, "1: Sleep\n");
217219820Sjeff	case 2:  return sprintf(buf, "2: Polling\n");
218219820Sjeff	case 3:  return sprintf(buf, "3: Disabled\n");
219219820Sjeff	case 4:  return sprintf(buf, "4: PortConfigurationTraining\n");
220219820Sjeff	case 5:  return sprintf(buf, "5: LinkUp\n");
221219820Sjeff	case 6:  return sprintf(buf, "6: LinkErrorRecovery\n");
222219820Sjeff	case 7:  return sprintf(buf, "7: Phy Test\n");
223219820Sjeff	default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
224219820Sjeff	}
225219820Sjeff}
226219820Sjeff
227219820Sjeffstatic ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
228219820Sjeff			       char *buf)
229219820Sjeff{
230219820Sjeff	switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
231219820Sjeff	case IB_LINK_LAYER_INFINIBAND:
232219820Sjeff		return sprintf(buf, "%s\n", "IB");
233219820Sjeff	case IB_LINK_LAYER_ETHERNET:
234219820Sjeff		return sprintf(buf, "%s\n", "Ethernet");
235219820Sjeff	default:
236219820Sjeff		return sprintf(buf, "%s\n", "Unknown");
237219820Sjeff	}
238219820Sjeff}
239219820Sjeff
240219820Sjeffstatic PORT_ATTR_RO(state);
241219820Sjeffstatic PORT_ATTR_RO(lid);
242219820Sjeffstatic PORT_ATTR_RO(lid_mask_count);
243219820Sjeffstatic PORT_ATTR_RO(sm_lid);
244219820Sjeffstatic PORT_ATTR_RO(sm_sl);
245219820Sjeffstatic PORT_ATTR_RO(cap_mask);
246219820Sjeffstatic PORT_ATTR_RO(rate);
247219820Sjeffstatic PORT_ATTR_RO(phys_state);
248219820Sjeffstatic PORT_ATTR_RO(link_layer);
249219820Sjeff
250219820Sjeffstatic struct attribute *port_default_attrs[] = {
251219820Sjeff	&port_attr_state.attr,
252219820Sjeff	&port_attr_lid.attr,
253219820Sjeff	&port_attr_lid_mask_count.attr,
254219820Sjeff	&port_attr_sm_lid.attr,
255219820Sjeff	&port_attr_sm_sl.attr,
256219820Sjeff	&port_attr_cap_mask.attr,
257219820Sjeff	&port_attr_rate.attr,
258219820Sjeff	&port_attr_phys_state.attr,
259219820Sjeff	&port_attr_link_layer.attr,
260219820Sjeff	NULL
261219820Sjeff};
262219820Sjeff
263219820Sjeffstatic ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
264219820Sjeff			     char *buf)
265219820Sjeff{
266219820Sjeff	struct port_table_attribute *tab_attr =
267219820Sjeff		container_of(attr, struct port_table_attribute, attr);
268219820Sjeff	union ib_gid gid;
269219820Sjeff	ssize_t ret;
270219820Sjeff	u16 *raw;
271219820Sjeff
272219820Sjeff	ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
273219820Sjeff	if (ret)
274219820Sjeff		return ret;
275219820Sjeff
276219820Sjeff	raw = (u16 *)gid.raw;
277219820Sjeff	return sprintf(buf, "%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x\n",
278219820Sjeff	    htons(raw[0]), htons(raw[1]), htons(raw[2]), htons(raw[3]),
279219820Sjeff	    htons(raw[4]), htons(raw[5]), htons(raw[6]), htons(raw[7]));
280219820Sjeff}
281219820Sjeff
282219820Sjeffstatic ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
283219820Sjeff			      char *buf)
284219820Sjeff{
285219820Sjeff	struct port_table_attribute *tab_attr =
286219820Sjeff		container_of(attr, struct port_table_attribute, attr);
287219820Sjeff	u16 pkey;
288219820Sjeff	ssize_t ret;
289219820Sjeff
290219820Sjeff	ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
291219820Sjeff	if (ret)
292219820Sjeff		return ret;
293219820Sjeff
294219820Sjeff	return sprintf(buf, "0x%04x\n", pkey);
295219820Sjeff}
296219820Sjeff
297255932Salfredstatic ssize_t get_pma_counters(struct ib_port *p, struct port_attribute *attr,
298255932Salfred                                char *buf, int c_ext)
299219820Sjeff{
300255932Salfred        struct port_table_attribute *tab_attr =
301255932Salfred                container_of(attr, struct port_table_attribute, attr);
302255932Salfred        int offset = tab_attr->index & 0xffff;
303255932Salfred        int width  = (tab_attr->index >> 16) & 0xff;
304255932Salfred        struct ib_mad *in_mad  = NULL;
305255932Salfred        struct ib_mad *out_mad = NULL;
306255932Salfred        ssize_t ret;
307219820Sjeff
308255932Salfred        if (!p->ibdev->process_mad)
309255932Salfred                return -ENXIO;
310219820Sjeff
311255932Salfred        in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
312255932Salfred        out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
313255932Salfred        if (!in_mad || !out_mad) {
314255932Salfred                ret = -ENOMEM;
315255932Salfred                goto out;
316255932Salfred        }
317219820Sjeff
318255932Salfred        in_mad->mad_hdr.base_version  = 1;
319255932Salfred        in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
320255932Salfred        in_mad->mad_hdr.class_version = 1;
321255932Salfred        in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
322255932Salfred        if (c_ext)
323255932Salfred                in_mad->mad_hdr.attr_id = IB_PMA_PORT_COUNTERS_EXT;
324255932Salfred        else
325255932Salfred                in_mad->mad_hdr.attr_id = IB_PMA_PORT_COUNTERS;
326219820Sjeff
327255932Salfred        in_mad->data[41] = p->port_num; /* PortSelect field */
328219820Sjeff
329255932Salfred        if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
330255932Salfred                 p->port_num, NULL, NULL, in_mad, out_mad) &
331255932Salfred             (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
332255932Salfred            (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
333255932Salfred                ret = -EINVAL;
334255932Salfred                goto out;
335255932Salfred        }
336219820Sjeff
337255932Salfred        switch (width) {
338255932Salfred        case 4:
339255932Salfred                ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
340255932Salfred                                            (4 - (offset % 8))) & 0xf);
341255932Salfred                break;
342255932Salfred        case 8:
343255932Salfred                ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
344255932Salfred                break;
345255932Salfred        case 16:
346255932Salfred                ret = sprintf(buf, "%u\n",
347255932Salfred                              be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8)));
348255932Salfred                break;
349255932Salfred        case 32:
350255932Salfred                ret = sprintf(buf, "%u\n",
351255932Salfred                              be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8)));
352255932Salfred                break;
353255932Salfred        case 64:
354255932Salfred                ret = sprintf(buf, "%llu\n", (unsigned long long)
355255932Salfred                              be64_to_cpup((__be64 *)(out_mad->data + 40 + offset / 8)));
356255932Salfred                break;
357255932Salfred        default:
358255932Salfred                ret = 0;
359255932Salfred        }
360219820Sjeff
361219820Sjeffout:
362255932Salfred        kfree(in_mad);
363255932Salfred        kfree(out_mad);
364219820Sjeff
365255932Salfred        return ret;
366219820Sjeff}
367219820Sjeff
368255932Salfred#define PORT_PMA_ATTR(_name, _counter, _width, _offset)                 \
369255932Salfredstruct port_table_attribute port_pma_attr_##_name = {                   \
370255932Salfred        .attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),        \
371255932Salfred        .index = (_offset) | ((_width) << 16) | ((_counter) << 24)      \
372255932Salfred}
373255932Salfred
374255932Salfredstatic ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
375255932Salfred                                char *buf)
376255932Salfred{
377255932Salfred        return get_pma_counters(p, attr, buf, 0);
378255932Salfred}
379255932Salfred
380255932Salfredstatic PORT_PMA_ATTR(symbol_error                   ,  0, 16,  32);
381255932Salfredstatic PORT_PMA_ATTR(link_error_recovery            ,  1,  8,  48);
382255932Salfredstatic PORT_PMA_ATTR(link_downed                    ,  2,  8,  56);
383255932Salfredstatic PORT_PMA_ATTR(port_rcv_errors                ,  3, 16,  64);
384219820Sjeffstatic PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
385219820Sjeffstatic PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
386255932Salfredstatic PORT_PMA_ATTR(port_xmit_discards             ,  6, 16, 112);
387219820Sjeffstatic PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
388255932Salfredstatic PORT_PMA_ATTR(port_rcv_constraint_errors     ,  8,  8, 136);
389219820Sjeffstatic PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
390219820Sjeffstatic PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
391255932Salfredstatic PORT_PMA_ATTR(VL15_dropped                   , 11, 16, 176);
392255932Salfredstatic PORT_PMA_ATTR(port_xmit_data                 , 12, 32, 192);
393255932Salfredstatic PORT_PMA_ATTR(port_rcv_data                  , 13, 32, 224);
394255932Salfredstatic PORT_PMA_ATTR(port_xmit_packets              , 14, 32, 256);
395255932Salfredstatic PORT_PMA_ATTR(port_rcv_packets               , 15, 32, 288);
396219820Sjeff
397219820Sjeffstatic struct attribute *pma_attrs[] = {
398255932Salfred        &port_pma_attr_symbol_error.attr.attr,
399255932Salfred        &port_pma_attr_link_error_recovery.attr.attr,
400255932Salfred        &port_pma_attr_link_downed.attr.attr,
401255932Salfred        &port_pma_attr_port_rcv_errors.attr.attr,
402255932Salfred        &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
403255932Salfred        &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
404255932Salfred        &port_pma_attr_port_xmit_discards.attr.attr,
405255932Salfred        &port_pma_attr_port_xmit_constraint_errors.attr.attr,
406255932Salfred        &port_pma_attr_port_rcv_constraint_errors.attr.attr,
407255932Salfred        &port_pma_attr_local_link_integrity_errors.attr.attr,
408255932Salfred        &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
409255932Salfred        &port_pma_attr_VL15_dropped.attr.attr,
410255932Salfred        &port_pma_attr_port_xmit_data.attr.attr,
411255932Salfred        &port_pma_attr_port_rcv_data.attr.attr,
412255932Salfred        &port_pma_attr_port_xmit_packets.attr.attr,
413255932Salfred        &port_pma_attr_port_rcv_packets.attr.attr,
414255932Salfred        NULL
415219820Sjeff};
416219820Sjeff
417219820Sjeffstatic struct attribute_group pma_group = {
418219820Sjeff	.name  = "counters",
419219820Sjeff	.attrs  = pma_attrs
420219820Sjeff};
421219820Sjeff
422255932Salfred#define PORT_PMA_ATTR_EXT(_name, _counter, _width, _offset)             \
423255932Salfredstruct port_table_attribute port_pma_attr_ext_##_name = {               \
424255932Salfred        .attr  = __ATTR(_name, S_IRUGO, show_pma_counter_ext, NULL),    \
425255932Salfred        .index = (_offset) | ((_width) << 16) | ((_counter) << 24)      \
426255932Salfred}
427255932Salfred
428255932Salfredstatic ssize_t show_pma_counter_ext(struct ib_port *p,
429255932Salfred                                    struct port_attribute *attr, char *buf)
430255932Salfred{
431255932Salfred        return get_pma_counters(p, attr, buf, 1);
432255932Salfred}
433255932Salfred
434255932Salfredstatic PORT_PMA_ATTR_EXT(port_xmit_data_64           ,  0, 64,  64);
435255932Salfredstatic PORT_PMA_ATTR_EXT(port_rcv_data_64            ,  0, 64,  128);
436255932Salfredstatic PORT_PMA_ATTR_EXT(port_xmit_packets_64        ,  0, 64,  192);
437255932Salfredstatic PORT_PMA_ATTR_EXT(port_rcv_packets_64         ,  0, 64,  256);
438255932Salfredstatic PORT_PMA_ATTR_EXT(port_unicast_xmit_packets   ,  0, 64,  320);
439255932Salfredstatic PORT_PMA_ATTR_EXT(port_unicast_rcv_packets    ,  0, 64,  384);
440255932Salfredstatic PORT_PMA_ATTR_EXT(port_multicast_xmit_packets ,  0, 64,  448);
441255932Salfredstatic PORT_PMA_ATTR_EXT(port_multicast_rcv_packets  ,  0, 64,  512);
442255932Salfred
443255932Salfredstatic struct attribute *pma_attrs_ext[] = {
444255932Salfred        &port_pma_attr_ext_port_xmit_data_64.attr.attr,
445255932Salfred        &port_pma_attr_ext_port_rcv_data_64.attr.attr,
446255932Salfred        &port_pma_attr_ext_port_xmit_packets_64.attr.attr,
447255932Salfred        &port_pma_attr_ext_port_rcv_packets_64.attr.attr,
448255932Salfred        &port_pma_attr_ext_port_unicast_xmit_packets.attr.attr,
449255932Salfred        &port_pma_attr_ext_port_unicast_rcv_packets.attr.attr,
450255932Salfred        &port_pma_attr_ext_port_multicast_xmit_packets.attr.attr,
451255932Salfred        &port_pma_attr_ext_port_multicast_rcv_packets.attr.attr,
452255932Salfred        NULL
453255932Salfred};
454255932Salfred
455255932Salfredstatic struct attribute_group pma_ext_group = {
456255932Salfred        .name  = "counters_ext",
457255932Salfred        .attrs  = pma_attrs_ext
458255932Salfred};
459255932Salfred
460219820Sjeffstatic void ib_port_release(struct kobject *kobj)
461219820Sjeff{
462219820Sjeff	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
463219820Sjeff	struct attribute *a;
464219820Sjeff	int i;
465219820Sjeff
466219820Sjeff	for (i = 0; (a = p->gid_group.attrs[i]); ++i)
467219820Sjeff		kfree(a);
468219820Sjeff
469219820Sjeff	kfree(p->gid_group.attrs);
470219820Sjeff
471219820Sjeff	for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
472219820Sjeff		kfree(a);
473219820Sjeff
474219820Sjeff	kfree(p->pkey_group.attrs);
475219820Sjeff
476219820Sjeff	kfree(p);
477219820Sjeff}
478219820Sjeff
479219820Sjeffstatic struct kobj_type port_type = {
480219820Sjeff	.release       = ib_port_release,
481219820Sjeff	.sysfs_ops     = &port_sysfs_ops,
482219820Sjeff	.default_attrs = port_default_attrs
483219820Sjeff};
484219820Sjeff
485219820Sjeffstatic void ib_device_release(struct device *device)
486219820Sjeff{
487219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
488219820Sjeff
489219820Sjeff	kfree(dev);
490219820Sjeff}
491219820Sjeff
492219820Sjeff#ifdef __linux__
493219820Sjeff/* BSD supports this through devfs(5) and devd(8). */
494219820Sjeffstatic int ib_device_uevent(struct device *device,
495219820Sjeff			    struct kobj_uevent_env *env)
496219820Sjeff{
497219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
498219820Sjeff
499219820Sjeff	if (add_uevent_var(env, "NAME=%s", dev->name))
500219820Sjeff		return -ENOMEM;
501219820Sjeff
502219820Sjeff	/*
503219820Sjeff	 * It would be nice to pass the node GUID with the event...
504219820Sjeff	 */
505219820Sjeff
506219820Sjeff	return 0;
507219820Sjeff}
508219820Sjeff#endif
509219820Sjeff
510219820Sjeffstatic struct attribute **
511219820Sjeffalloc_group_attrs(ssize_t (*show)(struct ib_port *,
512219820Sjeff				  struct port_attribute *, char *buf),
513219820Sjeff		  int len)
514219820Sjeff{
515219820Sjeff	struct attribute **tab_attr;
516219820Sjeff	struct port_table_attribute *element;
517219820Sjeff	int i;
518219820Sjeff
519219820Sjeff	tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
520219820Sjeff	if (!tab_attr)
521219820Sjeff		return NULL;
522219820Sjeff
523219820Sjeff	for (i = 0; i < len; i++) {
524219820Sjeff		element = kzalloc(sizeof(struct port_table_attribute),
525219820Sjeff				  GFP_KERNEL);
526219820Sjeff		if (!element)
527219820Sjeff			goto err;
528219820Sjeff
529219820Sjeff		if (snprintf(element->name, sizeof(element->name),
530219820Sjeff			     "%d", i) >= sizeof(element->name)) {
531219820Sjeff			kfree(element);
532219820Sjeff			goto err;
533219820Sjeff		}
534219820Sjeff
535219820Sjeff		element->attr.attr.name  = element->name;
536219820Sjeff		element->attr.attr.mode  = S_IRUGO;
537219820Sjeff		element->attr.show       = show;
538219820Sjeff		element->index		 = i;
539219820Sjeff
540219820Sjeff		tab_attr[i] = &element->attr.attr;
541219820Sjeff	}
542219820Sjeff
543219820Sjeff	return tab_attr;
544219820Sjeff
545219820Sjefferr:
546219820Sjeff	while (--i >= 0)
547219820Sjeff		kfree(tab_attr[i]);
548219820Sjeff	kfree(tab_attr);
549219820Sjeff	return NULL;
550219820Sjeff}
551219820Sjeff
552255932Salfredstatic int add_port(struct ib_device *device, int port_num,
553255932Salfred                    int (*port_callback)(struct ib_device *,
554255932Salfred                                         u8, struct kobject *))
555219820Sjeff{
556219820Sjeff	struct ib_port *p;
557219820Sjeff	struct ib_port_attr attr;
558219820Sjeff	int i;
559219820Sjeff	int ret;
560219820Sjeff
561219820Sjeff	ret = ib_query_port(device, port_num, &attr);
562219820Sjeff	if (ret)
563219820Sjeff		return ret;
564219820Sjeff
565219820Sjeff	p = kzalloc(sizeof *p, GFP_KERNEL);
566219820Sjeff	if (!p)
567219820Sjeff		return -ENOMEM;
568219820Sjeff
569219820Sjeff	p->ibdev      = device;
570219820Sjeff	p->port_num   = port_num;
571219820Sjeff
572219820Sjeff	ret = kobject_init_and_add(&p->kobj, &port_type,
573255932Salfred				   kobject_get(device->ports_parent),
574219820Sjeff				   "%d", port_num);
575219820Sjeff	if (ret)
576219820Sjeff		goto err_put;
577219820Sjeff
578219820Sjeff	ret = sysfs_create_group(&p->kobj, &pma_group);
579219820Sjeff	if (ret)
580219820Sjeff		goto err_put;
581219820Sjeff
582255932Salfred        ret = sysfs_create_group(&p->kobj, &pma_ext_group);
583255932Salfred        if (ret)
584255932Salfred                goto err_remove_pma;
585255932Salfred
586219820Sjeff	p->gid_group.name  = "gids";
587219820Sjeff	p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
588219820Sjeff	if (!p->gid_group.attrs)
589255932Salfred		goto err_remove_pma_ext;
590219820Sjeff
591219820Sjeff	ret = sysfs_create_group(&p->kobj, &p->gid_group);
592219820Sjeff	if (ret)
593219820Sjeff		goto err_free_gid;
594219820Sjeff
595219820Sjeff	p->pkey_group.name  = "pkeys";
596219820Sjeff	p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
597219820Sjeff						attr.pkey_tbl_len);
598219820Sjeff	if (!p->pkey_group.attrs)
599219820Sjeff		goto err_remove_gid;
600219820Sjeff
601219820Sjeff	ret = sysfs_create_group(&p->kobj, &p->pkey_group);
602219820Sjeff	if (ret)
603219820Sjeff		goto err_free_pkey;
604219820Sjeff
605255932Salfred        if (port_callback) {
606255932Salfred                ret = port_callback(device, port_num, &p->kobj);
607255932Salfred                if (ret)
608255932Salfred                        goto err_remove_pkey;
609255932Salfred        }
610255932Salfred
611219820Sjeff	list_add_tail(&p->kobj.entry, &device->port_list);
612219820Sjeff
613219820Sjeff#ifdef __linux__
614219820Sjeff	kobject_uevent(&p->kobj, KOBJ_ADD);
615219820Sjeff#endif
616219820Sjeff	return 0;
617219820Sjeff
618255932Salfrederr_remove_pkey:
619255932Salfred        sysfs_remove_group(&p->kobj, &p->pkey_group);
620255932Salfred
621219820Sjefferr_free_pkey:
622219820Sjeff	for (i = 0; i < attr.pkey_tbl_len; ++i)
623219820Sjeff		kfree(p->pkey_group.attrs[i]);
624219820Sjeff
625219820Sjeff	kfree(p->pkey_group.attrs);
626219820Sjeff
627219820Sjefferr_remove_gid:
628219820Sjeff	sysfs_remove_group(&p->kobj, &p->gid_group);
629219820Sjeff
630219820Sjefferr_free_gid:
631219820Sjeff	for (i = 0; i < attr.gid_tbl_len; ++i)
632219820Sjeff		kfree(p->gid_group.attrs[i]);
633219820Sjeff
634219820Sjeff	kfree(p->gid_group.attrs);
635219820Sjeff
636255932Salfrederr_remove_pma_ext:
637255932Salfred        sysfs_remove_group(&p->kobj, &pma_ext_group);
638255932Salfred
639219820Sjefferr_remove_pma:
640219820Sjeff	sysfs_remove_group(&p->kobj, &pma_group);
641219820Sjeff
642219820Sjefferr_put:
643219820Sjeff	kobject_put(device->ports_parent);
644219820Sjeff	kfree(p);
645219820Sjeff	return ret;
646219820Sjeff}
647219820Sjeff
648219820Sjeffstatic ssize_t show_node_type(struct device *device,
649219820Sjeff			      struct device_attribute *attr, char *buf)
650219820Sjeff{
651219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
652219820Sjeff
653219820Sjeff	switch (dev->node_type) {
654219820Sjeff	case RDMA_NODE_IB_CA:	  return sprintf(buf, "%d: CA\n", dev->node_type);
655219820Sjeff	case RDMA_NODE_RNIC:	  return sprintf(buf, "%d: RNIC\n", dev->node_type);
656219820Sjeff	case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
657219820Sjeff	case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
658219820Sjeff	default:		  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
659219820Sjeff	}
660219820Sjeff}
661219820Sjeff
662219820Sjeffstatic ssize_t show_sys_image_guid(struct device *device,
663219820Sjeff				   struct device_attribute *dev_attr, char *buf)
664219820Sjeff{
665219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
666219820Sjeff	struct ib_device_attr attr;
667219820Sjeff	ssize_t ret;
668219820Sjeff
669219820Sjeff	ret = ib_query_device(dev, &attr);
670219820Sjeff	if (ret)
671219820Sjeff		return ret;
672219820Sjeff
673219820Sjeff	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
674219820Sjeff		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]),
675219820Sjeff		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]),
676219820Sjeff		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]),
677219820Sjeff		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
678219820Sjeff}
679219820Sjeff
680219820Sjeffstatic ssize_t show_node_guid(struct device *device,
681219820Sjeff			      struct device_attribute *attr, char *buf)
682219820Sjeff{
683219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
684219820Sjeff
685219820Sjeff	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
686219820Sjeff		       be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
687219820Sjeff		       be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
688219820Sjeff		       be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
689219820Sjeff		       be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
690219820Sjeff}
691219820Sjeff
692219820Sjeffstatic ssize_t show_node_desc(struct device *device,
693219820Sjeff			      struct device_attribute *attr, char *buf)
694219820Sjeff{
695219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
696219820Sjeff
697219820Sjeff	return sprintf(buf, "%.64s\n", dev->node_desc);
698219820Sjeff}
699219820Sjeff
700219820Sjeffstatic ssize_t set_node_desc(struct device *device,
701219820Sjeff			     struct device_attribute *attr,
702219820Sjeff			     const char *buf, size_t count)
703219820Sjeff{
704219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
705219820Sjeff	struct ib_device_modify desc = {};
706219820Sjeff	int ret;
707219820Sjeff
708219820Sjeff	if (!dev->modify_device)
709219820Sjeff		return -EIO;
710219820Sjeff
711219820Sjeff	memcpy(desc.node_desc, buf, min_t(int, count, 64));
712219820Sjeff	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
713219820Sjeff	if (ret)
714219820Sjeff		return ret;
715219820Sjeff
716219820Sjeff	return count;
717219820Sjeff}
718219820Sjeff
719219820Sjeffstatic DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
720219820Sjeffstatic DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
721219820Sjeffstatic DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
722219820Sjeffstatic DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
723219820Sjeff
724219820Sjeffstatic struct device_attribute *ib_class_attributes[] = {
725219820Sjeff	&dev_attr_node_type,
726219820Sjeff	&dev_attr_sys_image_guid,
727219820Sjeff	&dev_attr_node_guid,
728219820Sjeff	&dev_attr_node_desc
729219820Sjeff};
730219820Sjeff
731219820Sjeffstatic struct class ib_class = {
732219820Sjeff	.name    = "infiniband",
733219820Sjeff	.dev_release = ib_device_release,
734219820Sjeff#ifdef __linux__
735219820Sjeff	.dev_uevent = ib_device_uevent,
736219820Sjeff#endif
737219820Sjeff};
738219820Sjeff
739219820Sjeff/* Show a given an attribute in the statistics group */
740219820Sjeffstatic ssize_t show_protocol_stat(const struct device *device,
741219820Sjeff			    struct device_attribute *attr, char *buf,
742219820Sjeff			    unsigned offset)
743219820Sjeff{
744219820Sjeff	struct ib_device *dev = container_of(__DECONST(struct device *, device), struct ib_device, dev);
745219820Sjeff	union rdma_protocol_stats stats;
746219820Sjeff	ssize_t ret;
747219820Sjeff
748219820Sjeff	ret = dev->get_protocol_stats(dev, &stats);
749219820Sjeff	if (ret)
750219820Sjeff		return ret;
751219820Sjeff
752219820Sjeff	return sprintf(buf, "%llu\n",
753219820Sjeff		       (unsigned long long) ((u64 *) &stats)[offset]);
754219820Sjeff}
755219820Sjeff
756219820Sjeff/* generate a read-only iwarp statistics attribute */
757219820Sjeff#define IW_STATS_ENTRY(name)						\
758219820Sjeffstatic ssize_t show_##name(struct device *device,			\
759219820Sjeff			   struct device_attribute *attr, char *buf)	\
760219820Sjeff{									\
761219820Sjeff	return show_protocol_stat(device, attr, buf,			\
762219820Sjeff				  offsetof(struct iw_protocol_stats, name) / \
763219820Sjeff				  sizeof (u64));			\
764219820Sjeff}									\
765219820Sjeffstatic DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
766219820Sjeff
767219820SjeffIW_STATS_ENTRY(ipInReceives);
768219820SjeffIW_STATS_ENTRY(ipInHdrErrors);
769219820SjeffIW_STATS_ENTRY(ipInTooBigErrors);
770219820SjeffIW_STATS_ENTRY(ipInNoRoutes);
771219820SjeffIW_STATS_ENTRY(ipInAddrErrors);
772219820SjeffIW_STATS_ENTRY(ipInUnknownProtos);
773219820SjeffIW_STATS_ENTRY(ipInTruncatedPkts);
774219820SjeffIW_STATS_ENTRY(ipInDiscards);
775219820SjeffIW_STATS_ENTRY(ipInDelivers);
776219820SjeffIW_STATS_ENTRY(ipOutForwDatagrams);
777219820SjeffIW_STATS_ENTRY(ipOutRequests);
778219820SjeffIW_STATS_ENTRY(ipOutDiscards);
779219820SjeffIW_STATS_ENTRY(ipOutNoRoutes);
780219820SjeffIW_STATS_ENTRY(ipReasmTimeout);
781219820SjeffIW_STATS_ENTRY(ipReasmReqds);
782219820SjeffIW_STATS_ENTRY(ipReasmOKs);
783219820SjeffIW_STATS_ENTRY(ipReasmFails);
784219820SjeffIW_STATS_ENTRY(ipFragOKs);
785219820SjeffIW_STATS_ENTRY(ipFragFails);
786219820SjeffIW_STATS_ENTRY(ipFragCreates);
787219820SjeffIW_STATS_ENTRY(ipInMcastPkts);
788219820SjeffIW_STATS_ENTRY(ipOutMcastPkts);
789219820SjeffIW_STATS_ENTRY(ipInBcastPkts);
790219820SjeffIW_STATS_ENTRY(ipOutBcastPkts);
791219820SjeffIW_STATS_ENTRY(tcpRtoAlgorithm);
792219820SjeffIW_STATS_ENTRY(tcpRtoMin);
793219820SjeffIW_STATS_ENTRY(tcpRtoMax);
794219820SjeffIW_STATS_ENTRY(tcpMaxConn);
795219820SjeffIW_STATS_ENTRY(tcpActiveOpens);
796219820SjeffIW_STATS_ENTRY(tcpPassiveOpens);
797219820SjeffIW_STATS_ENTRY(tcpAttemptFails);
798219820SjeffIW_STATS_ENTRY(tcpEstabResets);
799219820SjeffIW_STATS_ENTRY(tcpCurrEstab);
800219820SjeffIW_STATS_ENTRY(tcpInSegs);
801219820SjeffIW_STATS_ENTRY(tcpOutSegs);
802219820SjeffIW_STATS_ENTRY(tcpRetransSegs);
803219820SjeffIW_STATS_ENTRY(tcpInErrs);
804219820SjeffIW_STATS_ENTRY(tcpOutRsts);
805219820Sjeff
806219820Sjeffstatic struct attribute *iw_proto_stats_attrs[] = {
807219820Sjeff	&dev_attr_ipInReceives.attr,
808219820Sjeff	&dev_attr_ipInHdrErrors.attr,
809219820Sjeff	&dev_attr_ipInTooBigErrors.attr,
810219820Sjeff	&dev_attr_ipInNoRoutes.attr,
811219820Sjeff	&dev_attr_ipInAddrErrors.attr,
812219820Sjeff	&dev_attr_ipInUnknownProtos.attr,
813219820Sjeff	&dev_attr_ipInTruncatedPkts.attr,
814219820Sjeff	&dev_attr_ipInDiscards.attr,
815219820Sjeff	&dev_attr_ipInDelivers.attr,
816219820Sjeff	&dev_attr_ipOutForwDatagrams.attr,
817219820Sjeff	&dev_attr_ipOutRequests.attr,
818219820Sjeff	&dev_attr_ipOutDiscards.attr,
819219820Sjeff	&dev_attr_ipOutNoRoutes.attr,
820219820Sjeff	&dev_attr_ipReasmTimeout.attr,
821219820Sjeff	&dev_attr_ipReasmReqds.attr,
822219820Sjeff	&dev_attr_ipReasmOKs.attr,
823219820Sjeff	&dev_attr_ipReasmFails.attr,
824219820Sjeff	&dev_attr_ipFragOKs.attr,
825219820Sjeff	&dev_attr_ipFragFails.attr,
826219820Sjeff	&dev_attr_ipFragCreates.attr,
827219820Sjeff	&dev_attr_ipInMcastPkts.attr,
828219820Sjeff	&dev_attr_ipOutMcastPkts.attr,
829219820Sjeff	&dev_attr_ipInBcastPkts.attr,
830219820Sjeff	&dev_attr_ipOutBcastPkts.attr,
831219820Sjeff	&dev_attr_tcpRtoAlgorithm.attr,
832219820Sjeff	&dev_attr_tcpRtoMin.attr,
833219820Sjeff	&dev_attr_tcpRtoMax.attr,
834219820Sjeff	&dev_attr_tcpMaxConn.attr,
835219820Sjeff	&dev_attr_tcpActiveOpens.attr,
836219820Sjeff	&dev_attr_tcpPassiveOpens.attr,
837219820Sjeff	&dev_attr_tcpAttemptFails.attr,
838219820Sjeff	&dev_attr_tcpEstabResets.attr,
839219820Sjeff	&dev_attr_tcpCurrEstab.attr,
840219820Sjeff	&dev_attr_tcpInSegs.attr,
841219820Sjeff	&dev_attr_tcpOutSegs.attr,
842219820Sjeff	&dev_attr_tcpRetransSegs.attr,
843219820Sjeff	&dev_attr_tcpInErrs.attr,
844219820Sjeff	&dev_attr_tcpOutRsts.attr,
845219820Sjeff	NULL
846219820Sjeff};
847219820Sjeff
848219820Sjeffstatic struct attribute_group iw_stats_group = {
849219820Sjeff	.name	= "proto_stats",
850219820Sjeff	.attrs	= iw_proto_stats_attrs,
851219820Sjeff};
852219820Sjeff
853255932Salfredint ib_device_register_sysfs(struct ib_device *device,
854255932Salfred                                int (*port_callback)(struct ib_device *, u8, struct kobject *))
855219820Sjeff{
856219820Sjeff	struct device *class_dev = &device->dev;
857219820Sjeff	int ret;
858219820Sjeff	int i;
859219820Sjeff
860219820Sjeff	class_dev->class      = &ib_class;
861219820Sjeff	class_dev->parent     = device->dma_device;
862255932Salfred        dev_set_name(class_dev, device->name);
863255932Salfred        dev_set_drvdata(class_dev, device);
864219820Sjeff
865219820Sjeff	INIT_LIST_HEAD(&device->port_list);
866219820Sjeff
867219820Sjeff	ret = device_register(class_dev);
868219820Sjeff	if (ret)
869219820Sjeff		goto err;
870219820Sjeff
871219820Sjeff	for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
872219820Sjeff		ret = device_create_file(class_dev, ib_class_attributes[i]);
873219820Sjeff		if (ret)
874219820Sjeff			goto err_unregister;
875219820Sjeff	}
876219820Sjeff
877219820Sjeff	device->ports_parent = kobject_create_and_add("ports",
878255932Salfred                        	        kobject_get(&class_dev->kobj));
879255932Salfred        if (!device->ports_parent) {
880219820Sjeff		ret = -ENOMEM;
881219820Sjeff		goto err_put;
882219820Sjeff	}
883219820Sjeff
884219820Sjeff	if (device->node_type == RDMA_NODE_IB_SWITCH) {
885255932Salfred		ret = add_port(device, 0, port_callback);
886219820Sjeff		if (ret)
887219820Sjeff			goto err_put;
888219820Sjeff	} else {
889219820Sjeff		for (i = 1; i <= device->phys_port_cnt; ++i) {
890255932Salfred			ret = add_port(device, i, port_callback);
891219820Sjeff			if (ret)
892219820Sjeff				goto err_put;
893219820Sjeff		}
894219820Sjeff	}
895219820Sjeff
896219820Sjeff	if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
897219820Sjeff		ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
898219820Sjeff		if (ret)
899219820Sjeff			goto err_put;
900219820Sjeff	}
901219820Sjeff
902219820Sjeff	return 0;
903219820Sjeff
904219820Sjefferr_put:
905219820Sjeff	{
906219820Sjeff		struct kobject *p, *t;
907219820Sjeff		struct ib_port *port;
908219820Sjeff
909219820Sjeff		list_for_each_entry_safe(p, t, &device->port_list, entry) {
910219820Sjeff			list_del(&p->entry);
911219820Sjeff			port = container_of(p, struct ib_port, kobj);
912219820Sjeff			sysfs_remove_group(p, &pma_group);
913219820Sjeff			sysfs_remove_group(p, &port->pkey_group);
914219820Sjeff			sysfs_remove_group(p, &port->gid_group);
915219820Sjeff			kobject_put(p);
916219820Sjeff		}
917219820Sjeff	}
918219820Sjeff
919219820Sjeff	kobject_put(&class_dev->kobj);
920219820Sjeff
921219820Sjefferr_unregister:
922219820Sjeff	device_unregister(class_dev);
923219820Sjeff
924219820Sjefferr:
925219820Sjeff	return ret;
926219820Sjeff}
927219820Sjeff
928219820Sjeffvoid ib_device_unregister_sysfs(struct ib_device *device)
929219820Sjeff{
930219820Sjeff	struct kobject *p, *t;
931219820Sjeff	struct ib_port *port;
932255932Salfred	int i;
933219820Sjeff
934219820Sjeff	/* Hold kobject until ib_dealloc_device() */
935219820Sjeff	kobject_get(&device->dev.kobj);
936219820Sjeff
937255932Salfred	for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
938255932Salfred			device_remove_file(&device->dev, ib_class_attributes[i]);
939255932Salfred	}
940255932Salfred
941219820Sjeff	list_for_each_entry_safe(p, t, &device->port_list, entry) {
942219820Sjeff		list_del(&p->entry);
943219820Sjeff		port = container_of(p, struct ib_port, kobj);
944219820Sjeff		sysfs_remove_group(p, &pma_group);
945219820Sjeff		sysfs_remove_group(p, &port->pkey_group);
946219820Sjeff		sysfs_remove_group(p, &port->gid_group);
947219820Sjeff		kobject_put(p);
948219820Sjeff	}
949219820Sjeff
950219820Sjeff	kobject_put(device->ports_parent);
951219820Sjeff	device_unregister(&device->dev);
952219820Sjeff}
953219820Sjeff
954219820Sjeffint ib_sysfs_setup(void)
955219820Sjeff{
956219820Sjeff	return class_register(&ib_class);
957219820Sjeff}
958219820Sjeff
959219820Sjeffvoid ib_sysfs_cleanup(void)
960219820Sjeff{
961219820Sjeff	class_unregister(&ib_class);
962219820Sjeff}
963219820Sjeff
964255932Salfred/*int ib_sysfs_create_port_files(struct ib_device *device,
965219820Sjeff			       int (*create)(struct ib_device *dev, u8 port_num,
966219820Sjeff					     struct kobject *kobj))
967219820Sjeff{
968219820Sjeff	struct kobject *p;
969219820Sjeff	struct ib_port *port;
970219820Sjeff	int ret = 0;
971219820Sjeff
972219820Sjeff	list_for_each_entry(p, &device->port_list, entry) {
973219820Sjeff		port = container_of(p, struct ib_port, kobj);
974219820Sjeff		ret = create(device, port->port_num, &port->kobj);
975219820Sjeff		if (ret)
976219820Sjeff			break;
977219820Sjeff	}
978219820Sjeff
979219820Sjeff	return ret;
980219820Sjeff}
981255932SalfredEXPORT_SYMBOL(ib_sysfs_create_port_files);*/
982