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