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 = "";
183338615Shselasky	int rate;		/* in deci-Gb/sec */
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) {
191338615Shselasky	case IB_SPEED_DDR:
192338615Shselasky		speed = " DDR";
193338615Shselasky		rate = 50;
194338615Shselasky		break;
195338615Shselasky	case IB_SPEED_QDR:
196338615Shselasky		speed = " QDR";
197338615Shselasky		rate = 100;
198338615Shselasky		break;
199338615Shselasky	case IB_SPEED_FDR10:
200338615Shselasky		speed = " FDR10";
201338615Shselasky		rate = 100;
202338615Shselasky		break;
203338615Shselasky	case IB_SPEED_FDR:
204338615Shselasky		speed = " FDR";
205338615Shselasky		rate = 140;
206338615Shselasky		break;
207338615Shselasky	case IB_SPEED_EDR:
208338615Shselasky		speed = " EDR";
209338615Shselasky		rate = 250;
210338615Shselasky		break;
211338615Shselasky	case IB_SPEED_SDR:
212338615Shselasky	default:		/* default to SDR for invalid rates */
213338615Shselasky		rate = 25;
214338615Shselasky		break;
215219820Sjeff	}
216219820Sjeff
217338615Shselasky	rate *= ib_width_enum_to_int(attr.active_width);
218219820Sjeff	if (rate < 0)
219219820Sjeff		return -EINVAL;
220219820Sjeff
221219820Sjeff	return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
222219820Sjeff		       rate / 10, rate % 10 ? ".5" : "",
223219820Sjeff		       ib_width_enum_to_int(attr.active_width), speed);
224219820Sjeff}
225219820Sjeff
226219820Sjeffstatic ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
227219820Sjeff			       char *buf)
228219820Sjeff{
229219820Sjeff	struct ib_port_attr attr;
230219820Sjeff
231219820Sjeff	ssize_t ret;
232219820Sjeff
233219820Sjeff	ret = ib_query_port(p->ibdev, p->port_num, &attr);
234219820Sjeff	if (ret)
235219820Sjeff		return ret;
236219820Sjeff
237219820Sjeff	switch (attr.phys_state) {
238219820Sjeff	case 1:  return sprintf(buf, "1: Sleep\n");
239219820Sjeff	case 2:  return sprintf(buf, "2: Polling\n");
240219820Sjeff	case 3:  return sprintf(buf, "3: Disabled\n");
241219820Sjeff	case 4:  return sprintf(buf, "4: PortConfigurationTraining\n");
242219820Sjeff	case 5:  return sprintf(buf, "5: LinkUp\n");
243219820Sjeff	case 6:  return sprintf(buf, "6: LinkErrorRecovery\n");
244219820Sjeff	case 7:  return sprintf(buf, "7: Phy Test\n");
245219820Sjeff	default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
246219820Sjeff	}
247219820Sjeff}
248219820Sjeff
249219820Sjeffstatic ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
250219820Sjeff			       char *buf)
251219820Sjeff{
252219820Sjeff	switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
253219820Sjeff	case IB_LINK_LAYER_INFINIBAND:
254219820Sjeff		return sprintf(buf, "%s\n", "IB");
255219820Sjeff	case IB_LINK_LAYER_ETHERNET:
256219820Sjeff		return sprintf(buf, "%s\n", "Ethernet");
257219820Sjeff	default:
258219820Sjeff		return sprintf(buf, "%s\n", "Unknown");
259219820Sjeff	}
260219820Sjeff}
261219820Sjeff
262219820Sjeffstatic PORT_ATTR_RO(state);
263219820Sjeffstatic PORT_ATTR_RO(lid);
264219820Sjeffstatic PORT_ATTR_RO(lid_mask_count);
265219820Sjeffstatic PORT_ATTR_RO(sm_lid);
266219820Sjeffstatic PORT_ATTR_RO(sm_sl);
267219820Sjeffstatic PORT_ATTR_RO(cap_mask);
268219820Sjeffstatic PORT_ATTR_RO(rate);
269219820Sjeffstatic PORT_ATTR_RO(phys_state);
270219820Sjeffstatic PORT_ATTR_RO(link_layer);
271219820Sjeff
272219820Sjeffstatic struct attribute *port_default_attrs[] = {
273219820Sjeff	&port_attr_state.attr,
274219820Sjeff	&port_attr_lid.attr,
275219820Sjeff	&port_attr_lid_mask_count.attr,
276219820Sjeff	&port_attr_sm_lid.attr,
277219820Sjeff	&port_attr_sm_sl.attr,
278219820Sjeff	&port_attr_cap_mask.attr,
279219820Sjeff	&port_attr_rate.attr,
280219820Sjeff	&port_attr_phys_state.attr,
281219820Sjeff	&port_attr_link_layer.attr,
282219820Sjeff	NULL
283219820Sjeff};
284219820Sjeff
285219820Sjeffstatic ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
286219820Sjeff			     char *buf)
287219820Sjeff{
288219820Sjeff	struct port_table_attribute *tab_attr =
289219820Sjeff		container_of(attr, struct port_table_attribute, attr);
290219820Sjeff	union ib_gid gid;
291219820Sjeff	ssize_t ret;
292219820Sjeff	u16 *raw;
293219820Sjeff
294219820Sjeff	ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
295219820Sjeff	if (ret)
296219820Sjeff		return ret;
297219820Sjeff
298219820Sjeff	raw = (u16 *)gid.raw;
299219820Sjeff	return sprintf(buf, "%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x:%.4x\n",
300219820Sjeff	    htons(raw[0]), htons(raw[1]), htons(raw[2]), htons(raw[3]),
301219820Sjeff	    htons(raw[4]), htons(raw[5]), htons(raw[6]), htons(raw[7]));
302219820Sjeff}
303219820Sjeff
304219820Sjeffstatic ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
305219820Sjeff			      char *buf)
306219820Sjeff{
307219820Sjeff	struct port_table_attribute *tab_attr =
308219820Sjeff		container_of(attr, struct port_table_attribute, attr);
309219820Sjeff	u16 pkey;
310219820Sjeff	ssize_t ret;
311219820Sjeff
312219820Sjeff	ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
313219820Sjeff	if (ret)
314219820Sjeff		return ret;
315219820Sjeff
316219820Sjeff	return sprintf(buf, "0x%04x\n", pkey);
317219820Sjeff}
318219820Sjeff
319255932Salfredstatic ssize_t get_pma_counters(struct ib_port *p, struct port_attribute *attr,
320255932Salfred                                char *buf, int c_ext)
321219820Sjeff{
322255932Salfred        struct port_table_attribute *tab_attr =
323255932Salfred                container_of(attr, struct port_table_attribute, attr);
324255932Salfred        int offset = tab_attr->index & 0xffff;
325255932Salfred        int width  = (tab_attr->index >> 16) & 0xff;
326255932Salfred        struct ib_mad *in_mad  = NULL;
327255932Salfred        struct ib_mad *out_mad = NULL;
328255932Salfred        ssize_t ret;
329219820Sjeff
330255932Salfred        if (!p->ibdev->process_mad)
331255932Salfred                return -ENXIO;
332219820Sjeff
333255932Salfred        in_mad  = kzalloc(sizeof *in_mad, GFP_KERNEL);
334255932Salfred        out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
335255932Salfred        if (!in_mad || !out_mad) {
336255932Salfred                ret = -ENOMEM;
337255932Salfred                goto out;
338255932Salfred        }
339219820Sjeff
340255932Salfred        in_mad->mad_hdr.base_version  = 1;
341255932Salfred        in_mad->mad_hdr.mgmt_class    = IB_MGMT_CLASS_PERF_MGMT;
342255932Salfred        in_mad->mad_hdr.class_version = 1;
343255932Salfred        in_mad->mad_hdr.method        = IB_MGMT_METHOD_GET;
344255932Salfred        if (c_ext)
345255932Salfred                in_mad->mad_hdr.attr_id = IB_PMA_PORT_COUNTERS_EXT;
346255932Salfred        else
347255932Salfred                in_mad->mad_hdr.attr_id = IB_PMA_PORT_COUNTERS;
348219820Sjeff
349255932Salfred        in_mad->data[41] = p->port_num; /* PortSelect field */
350219820Sjeff
351255932Salfred        if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
352255932Salfred                 p->port_num, NULL, NULL, in_mad, out_mad) &
353255932Salfred             (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
354255932Salfred            (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
355255932Salfred                ret = -EINVAL;
356255932Salfred                goto out;
357255932Salfred        }
358219820Sjeff
359255932Salfred        switch (width) {
360255932Salfred        case 4:
361255932Salfred                ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
362255932Salfred                                            (4 - (offset % 8))) & 0xf);
363255932Salfred                break;
364255932Salfred        case 8:
365255932Salfred                ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
366255932Salfred                break;
367255932Salfred        case 16:
368255932Salfred                ret = sprintf(buf, "%u\n",
369255932Salfred                              be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8)));
370255932Salfred                break;
371255932Salfred        case 32:
372255932Salfred                ret = sprintf(buf, "%u\n",
373255932Salfred                              be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8)));
374255932Salfred                break;
375255932Salfred        case 64:
376255932Salfred                ret = sprintf(buf, "%llu\n", (unsigned long long)
377255932Salfred                              be64_to_cpup((__be64 *)(out_mad->data + 40 + offset / 8)));
378255932Salfred                break;
379255932Salfred        default:
380255932Salfred                ret = 0;
381255932Salfred        }
382219820Sjeff
383219820Sjeffout:
384255932Salfred        kfree(in_mad);
385255932Salfred        kfree(out_mad);
386219820Sjeff
387255932Salfred        return ret;
388219820Sjeff}
389219820Sjeff
390255932Salfred#define PORT_PMA_ATTR(_name, _counter, _width, _offset)                 \
391255932Salfredstruct port_table_attribute port_pma_attr_##_name = {                   \
392255932Salfred        .attr  = __ATTR(_name, S_IRUGO, show_pma_counter, NULL),        \
393255932Salfred        .index = (_offset) | ((_width) << 16) | ((_counter) << 24)      \
394255932Salfred}
395255932Salfred
396255932Salfredstatic ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
397255932Salfred                                char *buf)
398255932Salfred{
399255932Salfred        return get_pma_counters(p, attr, buf, 0);
400255932Salfred}
401255932Salfred
402255932Salfredstatic PORT_PMA_ATTR(symbol_error                   ,  0, 16,  32);
403255932Salfredstatic PORT_PMA_ATTR(link_error_recovery            ,  1,  8,  48);
404255932Salfredstatic PORT_PMA_ATTR(link_downed                    ,  2,  8,  56);
405255932Salfredstatic PORT_PMA_ATTR(port_rcv_errors                ,  3, 16,  64);
406219820Sjeffstatic PORT_PMA_ATTR(port_rcv_remote_physical_errors,  4, 16,  80);
407219820Sjeffstatic PORT_PMA_ATTR(port_rcv_switch_relay_errors   ,  5, 16,  96);
408255932Salfredstatic PORT_PMA_ATTR(port_xmit_discards             ,  6, 16, 112);
409219820Sjeffstatic PORT_PMA_ATTR(port_xmit_constraint_errors    ,  7,  8, 128);
410255932Salfredstatic PORT_PMA_ATTR(port_rcv_constraint_errors     ,  8,  8, 136);
411219820Sjeffstatic PORT_PMA_ATTR(local_link_integrity_errors    ,  9,  4, 152);
412219820Sjeffstatic PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10,  4, 156);
413255932Salfredstatic PORT_PMA_ATTR(VL15_dropped                   , 11, 16, 176);
414255932Salfredstatic PORT_PMA_ATTR(port_xmit_data                 , 12, 32, 192);
415255932Salfredstatic PORT_PMA_ATTR(port_rcv_data                  , 13, 32, 224);
416255932Salfredstatic PORT_PMA_ATTR(port_xmit_packets              , 14, 32, 256);
417255932Salfredstatic PORT_PMA_ATTR(port_rcv_packets               , 15, 32, 288);
418219820Sjeff
419219820Sjeffstatic struct attribute *pma_attrs[] = {
420255932Salfred        &port_pma_attr_symbol_error.attr.attr,
421255932Salfred        &port_pma_attr_link_error_recovery.attr.attr,
422255932Salfred        &port_pma_attr_link_downed.attr.attr,
423255932Salfred        &port_pma_attr_port_rcv_errors.attr.attr,
424255932Salfred        &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
425255932Salfred        &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
426255932Salfred        &port_pma_attr_port_xmit_discards.attr.attr,
427255932Salfred        &port_pma_attr_port_xmit_constraint_errors.attr.attr,
428255932Salfred        &port_pma_attr_port_rcv_constraint_errors.attr.attr,
429255932Salfred        &port_pma_attr_local_link_integrity_errors.attr.attr,
430255932Salfred        &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
431255932Salfred        &port_pma_attr_VL15_dropped.attr.attr,
432255932Salfred        &port_pma_attr_port_xmit_data.attr.attr,
433255932Salfred        &port_pma_attr_port_rcv_data.attr.attr,
434255932Salfred        &port_pma_attr_port_xmit_packets.attr.attr,
435255932Salfred        &port_pma_attr_port_rcv_packets.attr.attr,
436255932Salfred        NULL
437219820Sjeff};
438219820Sjeff
439219820Sjeffstatic struct attribute_group pma_group = {
440219820Sjeff	.name  = "counters",
441219820Sjeff	.attrs  = pma_attrs
442219820Sjeff};
443219820Sjeff
444255932Salfred#define PORT_PMA_ATTR_EXT(_name, _counter, _width, _offset)             \
445255932Salfredstruct port_table_attribute port_pma_attr_ext_##_name = {               \
446255932Salfred        .attr  = __ATTR(_name, S_IRUGO, show_pma_counter_ext, NULL),    \
447255932Salfred        .index = (_offset) | ((_width) << 16) | ((_counter) << 24)      \
448255932Salfred}
449255932Salfred
450255932Salfredstatic ssize_t show_pma_counter_ext(struct ib_port *p,
451255932Salfred                                    struct port_attribute *attr, char *buf)
452255932Salfred{
453255932Salfred        return get_pma_counters(p, attr, buf, 1);
454255932Salfred}
455255932Salfred
456255932Salfredstatic PORT_PMA_ATTR_EXT(port_xmit_data_64           ,  0, 64,  64);
457255932Salfredstatic PORT_PMA_ATTR_EXT(port_rcv_data_64            ,  0, 64,  128);
458255932Salfredstatic PORT_PMA_ATTR_EXT(port_xmit_packets_64        ,  0, 64,  192);
459255932Salfredstatic PORT_PMA_ATTR_EXT(port_rcv_packets_64         ,  0, 64,  256);
460255932Salfredstatic PORT_PMA_ATTR_EXT(port_unicast_xmit_packets   ,  0, 64,  320);
461255932Salfredstatic PORT_PMA_ATTR_EXT(port_unicast_rcv_packets    ,  0, 64,  384);
462255932Salfredstatic PORT_PMA_ATTR_EXT(port_multicast_xmit_packets ,  0, 64,  448);
463255932Salfredstatic PORT_PMA_ATTR_EXT(port_multicast_rcv_packets  ,  0, 64,  512);
464255932Salfred
465255932Salfredstatic struct attribute *pma_attrs_ext[] = {
466255932Salfred        &port_pma_attr_ext_port_xmit_data_64.attr.attr,
467255932Salfred        &port_pma_attr_ext_port_rcv_data_64.attr.attr,
468255932Salfred        &port_pma_attr_ext_port_xmit_packets_64.attr.attr,
469255932Salfred        &port_pma_attr_ext_port_rcv_packets_64.attr.attr,
470255932Salfred        &port_pma_attr_ext_port_unicast_xmit_packets.attr.attr,
471255932Salfred        &port_pma_attr_ext_port_unicast_rcv_packets.attr.attr,
472255932Salfred        &port_pma_attr_ext_port_multicast_xmit_packets.attr.attr,
473255932Salfred        &port_pma_attr_ext_port_multicast_rcv_packets.attr.attr,
474255932Salfred        NULL
475255932Salfred};
476255932Salfred
477255932Salfredstatic struct attribute_group pma_ext_group = {
478255932Salfred        .name  = "counters_ext",
479255932Salfred        .attrs  = pma_attrs_ext
480255932Salfred};
481255932Salfred
482219820Sjeffstatic void ib_port_release(struct kobject *kobj)
483219820Sjeff{
484219820Sjeff	struct ib_port *p = container_of(kobj, struct ib_port, kobj);
485219820Sjeff	struct attribute *a;
486219820Sjeff	int i;
487219820Sjeff
488219820Sjeff	for (i = 0; (a = p->gid_group.attrs[i]); ++i)
489219820Sjeff		kfree(a);
490219820Sjeff
491219820Sjeff	kfree(p->gid_group.attrs);
492219820Sjeff
493219820Sjeff	for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
494219820Sjeff		kfree(a);
495219820Sjeff
496219820Sjeff	kfree(p->pkey_group.attrs);
497219820Sjeff
498219820Sjeff	kfree(p);
499219820Sjeff}
500219820Sjeff
501219820Sjeffstatic struct kobj_type port_type = {
502219820Sjeff	.release       = ib_port_release,
503219820Sjeff	.sysfs_ops     = &port_sysfs_ops,
504219820Sjeff	.default_attrs = port_default_attrs
505219820Sjeff};
506219820Sjeff
507219820Sjeffstatic void ib_device_release(struct device *device)
508219820Sjeff{
509219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
510219820Sjeff
511219820Sjeff	kfree(dev);
512219820Sjeff}
513219820Sjeff
514219820Sjeff#ifdef __linux__
515219820Sjeff/* BSD supports this through devfs(5) and devd(8). */
516219820Sjeffstatic int ib_device_uevent(struct device *device,
517219820Sjeff			    struct kobj_uevent_env *env)
518219820Sjeff{
519219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
520219820Sjeff
521219820Sjeff	if (add_uevent_var(env, "NAME=%s", dev->name))
522219820Sjeff		return -ENOMEM;
523219820Sjeff
524219820Sjeff	/*
525219820Sjeff	 * It would be nice to pass the node GUID with the event...
526219820Sjeff	 */
527219820Sjeff
528219820Sjeff	return 0;
529219820Sjeff}
530219820Sjeff#endif
531219820Sjeff
532219820Sjeffstatic struct attribute **
533219820Sjeffalloc_group_attrs(ssize_t (*show)(struct ib_port *,
534219820Sjeff				  struct port_attribute *, char *buf),
535219820Sjeff		  int len)
536219820Sjeff{
537219820Sjeff	struct attribute **tab_attr;
538219820Sjeff	struct port_table_attribute *element;
539219820Sjeff	int i;
540219820Sjeff
541219820Sjeff	tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
542219820Sjeff	if (!tab_attr)
543219820Sjeff		return NULL;
544219820Sjeff
545219820Sjeff	for (i = 0; i < len; i++) {
546219820Sjeff		element = kzalloc(sizeof(struct port_table_attribute),
547219820Sjeff				  GFP_KERNEL);
548219820Sjeff		if (!element)
549219820Sjeff			goto err;
550219820Sjeff
551219820Sjeff		if (snprintf(element->name, sizeof(element->name),
552219820Sjeff			     "%d", i) >= sizeof(element->name)) {
553219820Sjeff			kfree(element);
554219820Sjeff			goto err;
555219820Sjeff		}
556219820Sjeff
557219820Sjeff		element->attr.attr.name  = element->name;
558219820Sjeff		element->attr.attr.mode  = S_IRUGO;
559219820Sjeff		element->attr.show       = show;
560219820Sjeff		element->index		 = i;
561219820Sjeff
562219820Sjeff		tab_attr[i] = &element->attr.attr;
563219820Sjeff	}
564219820Sjeff
565219820Sjeff	return tab_attr;
566219820Sjeff
567219820Sjefferr:
568219820Sjeff	while (--i >= 0)
569219820Sjeff		kfree(tab_attr[i]);
570219820Sjeff	kfree(tab_attr);
571219820Sjeff	return NULL;
572219820Sjeff}
573219820Sjeff
574255932Salfredstatic int add_port(struct ib_device *device, int port_num,
575255932Salfred                    int (*port_callback)(struct ib_device *,
576255932Salfred                                         u8, struct kobject *))
577219820Sjeff{
578219820Sjeff	struct ib_port *p;
579219820Sjeff	struct ib_port_attr attr;
580219820Sjeff	int i;
581219820Sjeff	int ret;
582219820Sjeff
583219820Sjeff	ret = ib_query_port(device, port_num, &attr);
584219820Sjeff	if (ret)
585219820Sjeff		return ret;
586219820Sjeff
587219820Sjeff	p = kzalloc(sizeof *p, GFP_KERNEL);
588219820Sjeff	if (!p)
589219820Sjeff		return -ENOMEM;
590219820Sjeff
591219820Sjeff	p->ibdev      = device;
592219820Sjeff	p->port_num   = port_num;
593219820Sjeff
594219820Sjeff	ret = kobject_init_and_add(&p->kobj, &port_type,
595255932Salfred				   kobject_get(device->ports_parent),
596219820Sjeff				   "%d", port_num);
597219820Sjeff	if (ret)
598219820Sjeff		goto err_put;
599219820Sjeff
600219820Sjeff	ret = sysfs_create_group(&p->kobj, &pma_group);
601219820Sjeff	if (ret)
602219820Sjeff		goto err_put;
603219820Sjeff
604255932Salfred        ret = sysfs_create_group(&p->kobj, &pma_ext_group);
605255932Salfred        if (ret)
606255932Salfred                goto err_remove_pma;
607255932Salfred
608219820Sjeff	p->gid_group.name  = "gids";
609219820Sjeff	p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
610219820Sjeff	if (!p->gid_group.attrs)
611255932Salfred		goto err_remove_pma_ext;
612219820Sjeff
613219820Sjeff	ret = sysfs_create_group(&p->kobj, &p->gid_group);
614219820Sjeff	if (ret)
615219820Sjeff		goto err_free_gid;
616219820Sjeff
617219820Sjeff	p->pkey_group.name  = "pkeys";
618219820Sjeff	p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
619219820Sjeff						attr.pkey_tbl_len);
620219820Sjeff	if (!p->pkey_group.attrs)
621219820Sjeff		goto err_remove_gid;
622219820Sjeff
623219820Sjeff	ret = sysfs_create_group(&p->kobj, &p->pkey_group);
624219820Sjeff	if (ret)
625219820Sjeff		goto err_free_pkey;
626219820Sjeff
627255932Salfred        if (port_callback) {
628255932Salfred                ret = port_callback(device, port_num, &p->kobj);
629255932Salfred                if (ret)
630255932Salfred                        goto err_remove_pkey;
631255932Salfred        }
632255932Salfred
633219820Sjeff	list_add_tail(&p->kobj.entry, &device->port_list);
634219820Sjeff
635219820Sjeff#ifdef __linux__
636219820Sjeff	kobject_uevent(&p->kobj, KOBJ_ADD);
637219820Sjeff#endif
638219820Sjeff	return 0;
639219820Sjeff
640255932Salfrederr_remove_pkey:
641255932Salfred        sysfs_remove_group(&p->kobj, &p->pkey_group);
642255932Salfred
643219820Sjefferr_free_pkey:
644219820Sjeff	for (i = 0; i < attr.pkey_tbl_len; ++i)
645219820Sjeff		kfree(p->pkey_group.attrs[i]);
646219820Sjeff
647219820Sjeff	kfree(p->pkey_group.attrs);
648219820Sjeff
649219820Sjefferr_remove_gid:
650219820Sjeff	sysfs_remove_group(&p->kobj, &p->gid_group);
651219820Sjeff
652219820Sjefferr_free_gid:
653219820Sjeff	for (i = 0; i < attr.gid_tbl_len; ++i)
654219820Sjeff		kfree(p->gid_group.attrs[i]);
655219820Sjeff
656219820Sjeff	kfree(p->gid_group.attrs);
657219820Sjeff
658255932Salfrederr_remove_pma_ext:
659255932Salfred        sysfs_remove_group(&p->kobj, &pma_ext_group);
660255932Salfred
661219820Sjefferr_remove_pma:
662219820Sjeff	sysfs_remove_group(&p->kobj, &pma_group);
663219820Sjeff
664219820Sjefferr_put:
665219820Sjeff	kobject_put(device->ports_parent);
666219820Sjeff	kfree(p);
667219820Sjeff	return ret;
668219820Sjeff}
669219820Sjeff
670219820Sjeffstatic ssize_t show_node_type(struct device *device,
671219820Sjeff			      struct device_attribute *attr, char *buf)
672219820Sjeff{
673219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
674219820Sjeff
675219820Sjeff	switch (dev->node_type) {
676219820Sjeff	case RDMA_NODE_IB_CA:	  return sprintf(buf, "%d: CA\n", dev->node_type);
677219820Sjeff	case RDMA_NODE_RNIC:	  return sprintf(buf, "%d: RNIC\n", dev->node_type);
678219820Sjeff	case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
679219820Sjeff	case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
680219820Sjeff	default:		  return sprintf(buf, "%d: <unknown>\n", dev->node_type);
681219820Sjeff	}
682219820Sjeff}
683219820Sjeff
684219820Sjeffstatic ssize_t show_sys_image_guid(struct device *device,
685219820Sjeff				   struct device_attribute *dev_attr, char *buf)
686219820Sjeff{
687219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
688219820Sjeff	struct ib_device_attr attr;
689219820Sjeff	ssize_t ret;
690219820Sjeff
691219820Sjeff	ret = ib_query_device(dev, &attr);
692219820Sjeff	if (ret)
693219820Sjeff		return ret;
694219820Sjeff
695219820Sjeff	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
696219820Sjeff		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]),
697219820Sjeff		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]),
698219820Sjeff		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]),
699219820Sjeff		       be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
700219820Sjeff}
701219820Sjeff
702219820Sjeffstatic ssize_t show_node_guid(struct device *device,
703219820Sjeff			      struct device_attribute *attr, char *buf)
704219820Sjeff{
705219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
706219820Sjeff
707219820Sjeff	return sprintf(buf, "%04x:%04x:%04x:%04x\n",
708219820Sjeff		       be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
709219820Sjeff		       be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
710219820Sjeff		       be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
711219820Sjeff		       be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
712219820Sjeff}
713219820Sjeff
714219820Sjeffstatic ssize_t show_node_desc(struct device *device,
715219820Sjeff			      struct device_attribute *attr, char *buf)
716219820Sjeff{
717219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
718219820Sjeff
719219820Sjeff	return sprintf(buf, "%.64s\n", dev->node_desc);
720219820Sjeff}
721219820Sjeff
722219820Sjeffstatic ssize_t set_node_desc(struct device *device,
723219820Sjeff			     struct device_attribute *attr,
724219820Sjeff			     const char *buf, size_t count)
725219820Sjeff{
726219820Sjeff	struct ib_device *dev = container_of(device, struct ib_device, dev);
727219820Sjeff	struct ib_device_modify desc = {};
728219820Sjeff	int ret;
729219820Sjeff
730219820Sjeff	if (!dev->modify_device)
731219820Sjeff		return -EIO;
732219820Sjeff
733219820Sjeff	memcpy(desc.node_desc, buf, min_t(int, count, 64));
734219820Sjeff	ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
735219820Sjeff	if (ret)
736219820Sjeff		return ret;
737219820Sjeff
738219820Sjeff	return count;
739219820Sjeff}
740219820Sjeff
741219820Sjeffstatic DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
742219820Sjeffstatic DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
743219820Sjeffstatic DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
744219820Sjeffstatic DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
745219820Sjeff
746219820Sjeffstatic struct device_attribute *ib_class_attributes[] = {
747219820Sjeff	&dev_attr_node_type,
748219820Sjeff	&dev_attr_sys_image_guid,
749219820Sjeff	&dev_attr_node_guid,
750219820Sjeff	&dev_attr_node_desc
751219820Sjeff};
752219820Sjeff
753219820Sjeffstatic struct class ib_class = {
754219820Sjeff	.name    = "infiniband",
755219820Sjeff	.dev_release = ib_device_release,
756219820Sjeff#ifdef __linux__
757219820Sjeff	.dev_uevent = ib_device_uevent,
758219820Sjeff#endif
759219820Sjeff};
760219820Sjeff
761219820Sjeff/* Show a given an attribute in the statistics group */
762219820Sjeffstatic ssize_t show_protocol_stat(const struct device *device,
763219820Sjeff			    struct device_attribute *attr, char *buf,
764219820Sjeff			    unsigned offset)
765219820Sjeff{
766219820Sjeff	struct ib_device *dev = container_of(__DECONST(struct device *, device), struct ib_device, dev);
767219820Sjeff	union rdma_protocol_stats stats;
768219820Sjeff	ssize_t ret;
769219820Sjeff
770219820Sjeff	ret = dev->get_protocol_stats(dev, &stats);
771219820Sjeff	if (ret)
772219820Sjeff		return ret;
773219820Sjeff
774219820Sjeff	return sprintf(buf, "%llu\n",
775219820Sjeff		       (unsigned long long) ((u64 *) &stats)[offset]);
776219820Sjeff}
777219820Sjeff
778219820Sjeff/* generate a read-only iwarp statistics attribute */
779219820Sjeff#define IW_STATS_ENTRY(name)						\
780219820Sjeffstatic ssize_t show_##name(struct device *device,			\
781219820Sjeff			   struct device_attribute *attr, char *buf)	\
782219820Sjeff{									\
783219820Sjeff	return show_protocol_stat(device, attr, buf,			\
784219820Sjeff				  offsetof(struct iw_protocol_stats, name) / \
785219820Sjeff				  sizeof (u64));			\
786219820Sjeff}									\
787219820Sjeffstatic DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
788219820Sjeff
789219820SjeffIW_STATS_ENTRY(ipInReceives);
790219820SjeffIW_STATS_ENTRY(ipInHdrErrors);
791219820SjeffIW_STATS_ENTRY(ipInTooBigErrors);
792219820SjeffIW_STATS_ENTRY(ipInNoRoutes);
793219820SjeffIW_STATS_ENTRY(ipInAddrErrors);
794219820SjeffIW_STATS_ENTRY(ipInUnknownProtos);
795219820SjeffIW_STATS_ENTRY(ipInTruncatedPkts);
796219820SjeffIW_STATS_ENTRY(ipInDiscards);
797219820SjeffIW_STATS_ENTRY(ipInDelivers);
798219820SjeffIW_STATS_ENTRY(ipOutForwDatagrams);
799219820SjeffIW_STATS_ENTRY(ipOutRequests);
800219820SjeffIW_STATS_ENTRY(ipOutDiscards);
801219820SjeffIW_STATS_ENTRY(ipOutNoRoutes);
802219820SjeffIW_STATS_ENTRY(ipReasmTimeout);
803219820SjeffIW_STATS_ENTRY(ipReasmReqds);
804219820SjeffIW_STATS_ENTRY(ipReasmOKs);
805219820SjeffIW_STATS_ENTRY(ipReasmFails);
806219820SjeffIW_STATS_ENTRY(ipFragOKs);
807219820SjeffIW_STATS_ENTRY(ipFragFails);
808219820SjeffIW_STATS_ENTRY(ipFragCreates);
809219820SjeffIW_STATS_ENTRY(ipInMcastPkts);
810219820SjeffIW_STATS_ENTRY(ipOutMcastPkts);
811219820SjeffIW_STATS_ENTRY(ipInBcastPkts);
812219820SjeffIW_STATS_ENTRY(ipOutBcastPkts);
813219820SjeffIW_STATS_ENTRY(tcpRtoAlgorithm);
814219820SjeffIW_STATS_ENTRY(tcpRtoMin);
815219820SjeffIW_STATS_ENTRY(tcpRtoMax);
816219820SjeffIW_STATS_ENTRY(tcpMaxConn);
817219820SjeffIW_STATS_ENTRY(tcpActiveOpens);
818219820SjeffIW_STATS_ENTRY(tcpPassiveOpens);
819219820SjeffIW_STATS_ENTRY(tcpAttemptFails);
820219820SjeffIW_STATS_ENTRY(tcpEstabResets);
821219820SjeffIW_STATS_ENTRY(tcpCurrEstab);
822219820SjeffIW_STATS_ENTRY(tcpInSegs);
823219820SjeffIW_STATS_ENTRY(tcpOutSegs);
824219820SjeffIW_STATS_ENTRY(tcpRetransSegs);
825219820SjeffIW_STATS_ENTRY(tcpInErrs);
826219820SjeffIW_STATS_ENTRY(tcpOutRsts);
827219820Sjeff
828219820Sjeffstatic struct attribute *iw_proto_stats_attrs[] = {
829219820Sjeff	&dev_attr_ipInReceives.attr,
830219820Sjeff	&dev_attr_ipInHdrErrors.attr,
831219820Sjeff	&dev_attr_ipInTooBigErrors.attr,
832219820Sjeff	&dev_attr_ipInNoRoutes.attr,
833219820Sjeff	&dev_attr_ipInAddrErrors.attr,
834219820Sjeff	&dev_attr_ipInUnknownProtos.attr,
835219820Sjeff	&dev_attr_ipInTruncatedPkts.attr,
836219820Sjeff	&dev_attr_ipInDiscards.attr,
837219820Sjeff	&dev_attr_ipInDelivers.attr,
838219820Sjeff	&dev_attr_ipOutForwDatagrams.attr,
839219820Sjeff	&dev_attr_ipOutRequests.attr,
840219820Sjeff	&dev_attr_ipOutDiscards.attr,
841219820Sjeff	&dev_attr_ipOutNoRoutes.attr,
842219820Sjeff	&dev_attr_ipReasmTimeout.attr,
843219820Sjeff	&dev_attr_ipReasmReqds.attr,
844219820Sjeff	&dev_attr_ipReasmOKs.attr,
845219820Sjeff	&dev_attr_ipReasmFails.attr,
846219820Sjeff	&dev_attr_ipFragOKs.attr,
847219820Sjeff	&dev_attr_ipFragFails.attr,
848219820Sjeff	&dev_attr_ipFragCreates.attr,
849219820Sjeff	&dev_attr_ipInMcastPkts.attr,
850219820Sjeff	&dev_attr_ipOutMcastPkts.attr,
851219820Sjeff	&dev_attr_ipInBcastPkts.attr,
852219820Sjeff	&dev_attr_ipOutBcastPkts.attr,
853219820Sjeff	&dev_attr_tcpRtoAlgorithm.attr,
854219820Sjeff	&dev_attr_tcpRtoMin.attr,
855219820Sjeff	&dev_attr_tcpRtoMax.attr,
856219820Sjeff	&dev_attr_tcpMaxConn.attr,
857219820Sjeff	&dev_attr_tcpActiveOpens.attr,
858219820Sjeff	&dev_attr_tcpPassiveOpens.attr,
859219820Sjeff	&dev_attr_tcpAttemptFails.attr,
860219820Sjeff	&dev_attr_tcpEstabResets.attr,
861219820Sjeff	&dev_attr_tcpCurrEstab.attr,
862219820Sjeff	&dev_attr_tcpInSegs.attr,
863219820Sjeff	&dev_attr_tcpOutSegs.attr,
864219820Sjeff	&dev_attr_tcpRetransSegs.attr,
865219820Sjeff	&dev_attr_tcpInErrs.attr,
866219820Sjeff	&dev_attr_tcpOutRsts.attr,
867219820Sjeff	NULL
868219820Sjeff};
869219820Sjeff
870219820Sjeffstatic struct attribute_group iw_stats_group = {
871219820Sjeff	.name	= "proto_stats",
872219820Sjeff	.attrs	= iw_proto_stats_attrs,
873219820Sjeff};
874219820Sjeff
875255932Salfredint ib_device_register_sysfs(struct ib_device *device,
876255932Salfred                                int (*port_callback)(struct ib_device *, u8, struct kobject *))
877219820Sjeff{
878219820Sjeff	struct device *class_dev = &device->dev;
879219820Sjeff	int ret;
880219820Sjeff	int i;
881219820Sjeff
882219820Sjeff	class_dev->class      = &ib_class;
883219820Sjeff	class_dev->parent     = device->dma_device;
884255932Salfred        dev_set_name(class_dev, device->name);
885255932Salfred        dev_set_drvdata(class_dev, device);
886219820Sjeff
887219820Sjeff	INIT_LIST_HEAD(&device->port_list);
888219820Sjeff
889219820Sjeff	ret = device_register(class_dev);
890219820Sjeff	if (ret)
891219820Sjeff		goto err;
892219820Sjeff
893219820Sjeff	for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
894219820Sjeff		ret = device_create_file(class_dev, ib_class_attributes[i]);
895219820Sjeff		if (ret)
896219820Sjeff			goto err_unregister;
897219820Sjeff	}
898219820Sjeff
899219820Sjeff	device->ports_parent = kobject_create_and_add("ports",
900255932Salfred                        	        kobject_get(&class_dev->kobj));
901255932Salfred        if (!device->ports_parent) {
902219820Sjeff		ret = -ENOMEM;
903219820Sjeff		goto err_put;
904219820Sjeff	}
905219820Sjeff
906219820Sjeff	if (device->node_type == RDMA_NODE_IB_SWITCH) {
907255932Salfred		ret = add_port(device, 0, port_callback);
908219820Sjeff		if (ret)
909219820Sjeff			goto err_put;
910219820Sjeff	} else {
911219820Sjeff		for (i = 1; i <= device->phys_port_cnt; ++i) {
912255932Salfred			ret = add_port(device, i, port_callback);
913219820Sjeff			if (ret)
914219820Sjeff				goto err_put;
915219820Sjeff		}
916219820Sjeff	}
917219820Sjeff
918219820Sjeff	if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
919219820Sjeff		ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
920219820Sjeff		if (ret)
921219820Sjeff			goto err_put;
922219820Sjeff	}
923219820Sjeff
924219820Sjeff	return 0;
925219820Sjeff
926219820Sjefferr_put:
927219820Sjeff	{
928219820Sjeff		struct kobject *p, *t;
929219820Sjeff		struct ib_port *port;
930219820Sjeff
931219820Sjeff		list_for_each_entry_safe(p, t, &device->port_list, entry) {
932219820Sjeff			list_del(&p->entry);
933219820Sjeff			port = container_of(p, struct ib_port, kobj);
934219820Sjeff			sysfs_remove_group(p, &pma_group);
935219820Sjeff			sysfs_remove_group(p, &port->pkey_group);
936219820Sjeff			sysfs_remove_group(p, &port->gid_group);
937219820Sjeff			kobject_put(p);
938219820Sjeff		}
939219820Sjeff	}
940219820Sjeff
941219820Sjeff	kobject_put(&class_dev->kobj);
942219820Sjeff
943219820Sjefferr_unregister:
944219820Sjeff	device_unregister(class_dev);
945219820Sjeff
946219820Sjefferr:
947219820Sjeff	return ret;
948219820Sjeff}
949219820Sjeff
950219820Sjeffvoid ib_device_unregister_sysfs(struct ib_device *device)
951219820Sjeff{
952219820Sjeff	struct kobject *p, *t;
953219820Sjeff	struct ib_port *port;
954255932Salfred	int i;
955219820Sjeff
956219820Sjeff	/* Hold kobject until ib_dealloc_device() */
957219820Sjeff	kobject_get(&device->dev.kobj);
958219820Sjeff
959255932Salfred	for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
960255932Salfred			device_remove_file(&device->dev, ib_class_attributes[i]);
961255932Salfred	}
962255932Salfred
963219820Sjeff	list_for_each_entry_safe(p, t, &device->port_list, entry) {
964219820Sjeff		list_del(&p->entry);
965219820Sjeff		port = container_of(p, struct ib_port, kobj);
966219820Sjeff		sysfs_remove_group(p, &pma_group);
967219820Sjeff		sysfs_remove_group(p, &port->pkey_group);
968219820Sjeff		sysfs_remove_group(p, &port->gid_group);
969219820Sjeff		kobject_put(p);
970219820Sjeff	}
971219820Sjeff
972219820Sjeff	kobject_put(device->ports_parent);
973219820Sjeff	device_unregister(&device->dev);
974219820Sjeff}
975219820Sjeff
976219820Sjeffint ib_sysfs_setup(void)
977219820Sjeff{
978219820Sjeff	return class_register(&ib_class);
979219820Sjeff}
980219820Sjeff
981219820Sjeffvoid ib_sysfs_cleanup(void)
982219820Sjeff{
983219820Sjeff	class_unregister(&ib_class);
984219820Sjeff}
985219820Sjeff
986255932Salfred/*int ib_sysfs_create_port_files(struct ib_device *device,
987219820Sjeff			       int (*create)(struct ib_device *dev, u8 port_num,
988219820Sjeff					     struct kobject *kobj))
989219820Sjeff{
990219820Sjeff	struct kobject *p;
991219820Sjeff	struct ib_port *port;
992219820Sjeff	int ret = 0;
993219820Sjeff
994219820Sjeff	list_for_each_entry(p, &device->port_list, entry) {
995219820Sjeff		port = container_of(p, struct ib_port, kobj);
996219820Sjeff		ret = create(device, port->port_num, &port->kobj);
997219820Sjeff		if (ret)
998219820Sjeff			break;
999219820Sjeff	}
1000219820Sjeff
1001219820Sjeff	return ret;
1002219820Sjeff}
1003255932SalfredEXPORT_SYMBOL(ib_sysfs_create_port_files);*/
1004