blob: 0b84a9cdfe5b90636d3633dbb42d84ef84c4e98e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include "core_priv.h"
36
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080037#include <linux/slab.h>
Paul Gortmakerfc87af72011-05-27 13:27:45 -040038#include <linux/stat.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080039#include <linux/string.h>
40
Roland Dreiera4d61e82005-08-25 13:40:04 -070041#include <rdma/ib_mad.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43struct ib_port {
44 struct kobject kobj;
45 struct ib_device *ibdev;
46 struct attribute_group gid_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct attribute_group pkey_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 u8 port_num;
49};
50
51struct port_attribute {
52 struct attribute attr;
53 ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
54 ssize_t (*store)(struct ib_port *, struct port_attribute *,
55 const char *buf, size_t count);
56};
57
58#define PORT_ATTR(_name, _mode, _show, _store) \
59struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
60
61#define PORT_ATTR_RO(_name) \
62struct port_attribute port_attr_##_name = __ATTR_RO(_name)
63
64struct port_table_attribute {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -050065 struct port_attribute attr;
66 char name[8];
67 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068};
69
70static ssize_t port_attr_show(struct kobject *kobj,
71 struct attribute *attr, char *buf)
72{
73 struct port_attribute *port_attr =
74 container_of(attr, struct port_attribute, attr);
75 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
76
77 if (!port_attr->show)
Dmitry Torokhov70f28172005-04-29 01:27:34 -050078 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 return port_attr->show(p, port_attr, buf);
81}
82
Emese Revfy52cf25d2010-01-19 02:58:23 +010083static const struct sysfs_ops port_sysfs_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 .show = port_attr_show
85};
86
87static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
88 char *buf)
89{
90 struct ib_port_attr attr;
91 ssize_t ret;
92
93 static const char *state_name[] = {
94 [IB_PORT_NOP] = "NOP",
95 [IB_PORT_DOWN] = "DOWN",
96 [IB_PORT_INIT] = "INIT",
97 [IB_PORT_ARMED] = "ARMED",
98 [IB_PORT_ACTIVE] = "ACTIVE",
99 [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
100 };
101
102 ret = ib_query_port(p->ibdev, p->port_num, &attr);
103 if (ret)
104 return ret;
105
106 return sprintf(buf, "%d: %s\n", attr.state,
Roland Dreier048975a2006-03-20 10:08:25 -0800107 attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 state_name[attr.state] : "UNKNOWN");
109}
110
111static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
112 char *buf)
113{
114 struct ib_port_attr attr;
115 ssize_t ret;
116
117 ret = ib_query_port(p->ibdev, p->port_num, &attr);
118 if (ret)
119 return ret;
120
121 return sprintf(buf, "0x%x\n", attr.lid);
122}
123
124static ssize_t lid_mask_count_show(struct ib_port *p,
125 struct port_attribute *unused,
126 char *buf)
127{
128 struct ib_port_attr attr;
129 ssize_t ret;
130
131 ret = ib_query_port(p->ibdev, p->port_num, &attr);
132 if (ret)
133 return ret;
134
135 return sprintf(buf, "%d\n", attr.lmc);
136}
137
138static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
139 char *buf)
140{
141 struct ib_port_attr attr;
142 ssize_t ret;
143
144 ret = ib_query_port(p->ibdev, p->port_num, &attr);
145 if (ret)
146 return ret;
147
148 return sprintf(buf, "0x%x\n", attr.sm_lid);
149}
150
151static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
152 char *buf)
153{
154 struct ib_port_attr attr;
155 ssize_t ret;
156
157 ret = ib_query_port(p->ibdev, p->port_num, &attr);
158 if (ret)
159 return ret;
160
161 return sprintf(buf, "%d\n", attr.sm_sl);
162}
163
164static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
165 char *buf)
166{
167 struct ib_port_attr attr;
168 ssize_t ret;
169
170 ret = ib_query_port(p->ibdev, p->port_num, &attr);
171 if (ret)
172 return ret;
173
174 return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
175}
176
177static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
178 char *buf)
179{
180 struct ib_port_attr attr;
181 char *speed = "";
Roland Dreier0559d8d2012-04-02 10:57:31 -0700182 int rate; /* in deci-Gb/sec */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 ssize_t ret;
184
185 ret = ib_query_port(p->ibdev, p->port_num, &attr);
186 if (ret)
187 return ret;
188
189 switch (attr.active_speed) {
Or Gerlitz2e966912012-02-28 18:49:50 +0200190 case IB_SPEED_DDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300191 speed = " DDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800192 rate = 50;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300193 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200194 case IB_SPEED_QDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300195 speed = " QDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800196 rate = 100;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300197 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200198 case IB_SPEED_FDR10:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300199 speed = " FDR10";
Roland Dreiere9319b02012-02-27 09:15:08 -0800200 rate = 100;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300201 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200202 case IB_SPEED_FDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300203 speed = " FDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800204 rate = 140;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300205 break;
Or Gerlitz2e966912012-02-28 18:49:50 +0200206 case IB_SPEED_EDR:
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300207 speed = " EDR";
Roland Dreiere9319b02012-02-27 09:15:08 -0800208 rate = 250;
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300209 break;
Roland Dreier0559d8d2012-04-02 10:57:31 -0700210 case IB_SPEED_SDR:
211 default: /* default to SDR for invalid rates */
212 rate = 25;
213 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215
Marcel Apfelbaum71eeba12011-10-05 14:21:47 +0300216 rate *= ib_width_enum_to_int(attr.active_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (rate < 0)
218 return -EINVAL;
219
220 return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
Roland Dreiere9319b02012-02-27 09:15:08 -0800221 rate / 10, rate % 10 ? ".5" : "",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ib_width_enum_to_int(attr.active_width), speed);
223}
224
225static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
226 char *buf)
227{
228 struct ib_port_attr attr;
229
230 ssize_t ret;
231
232 ret = ib_query_port(p->ibdev, p->port_num, &attr);
233 if (ret)
234 return ret;
235
236 switch (attr.phys_state) {
237 case 1: return sprintf(buf, "1: Sleep\n");
238 case 2: return sprintf(buf, "2: Polling\n");
239 case 3: return sprintf(buf, "3: Disabled\n");
240 case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
241 case 5: return sprintf(buf, "5: LinkUp\n");
242 case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
243 case 7: return sprintf(buf, "7: Phy Test\n");
244 default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
245 }
246}
247
Eli Cohen8ad330a2010-10-22 14:32:05 +0200248static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
249 char *buf)
250{
251 switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
252 case IB_LINK_LAYER_INFINIBAND:
253 return sprintf(buf, "%s\n", "InfiniBand");
254 case IB_LINK_LAYER_ETHERNET:
255 return sprintf(buf, "%s\n", "Ethernet");
256 default:
257 return sprintf(buf, "%s\n", "Unknown");
258 }
259}
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static PORT_ATTR_RO(state);
262static PORT_ATTR_RO(lid);
263static PORT_ATTR_RO(lid_mask_count);
264static PORT_ATTR_RO(sm_lid);
265static PORT_ATTR_RO(sm_sl);
266static PORT_ATTR_RO(cap_mask);
267static PORT_ATTR_RO(rate);
268static PORT_ATTR_RO(phys_state);
Eli Cohen8ad330a2010-10-22 14:32:05 +0200269static PORT_ATTR_RO(link_layer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271static struct attribute *port_default_attrs[] = {
272 &port_attr_state.attr,
273 &port_attr_lid.attr,
274 &port_attr_lid_mask_count.attr,
275 &port_attr_sm_lid.attr,
276 &port_attr_sm_sl.attr,
277 &port_attr_cap_mask.attr,
278 &port_attr_rate.attr,
279 &port_attr_phys_state.attr,
Eli Cohen8ad330a2010-10-22 14:32:05 +0200280 &port_attr_link_layer.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 NULL
282};
283
284static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
285 char *buf)
286{
287 struct port_table_attribute *tab_attr =
288 container_of(attr, struct port_table_attribute, attr);
289 union ib_gid gid;
290 ssize_t ret;
291
292 ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
293 if (ret)
294 return ret;
295
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700296 return sprintf(buf, "%pI6\n", gid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
299static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
300 char *buf)
301{
302 struct port_table_attribute *tab_attr =
303 container_of(attr, struct port_table_attribute, attr);
304 u16 pkey;
305 ssize_t ret;
306
307 ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
308 if (ret)
309 return ret;
310
311 return sprintf(buf, "0x%04x\n", pkey);
312}
313
314#define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
315struct port_table_attribute port_pma_attr_##_name = { \
316 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
317 .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \
318}
319
320static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
321 char *buf)
322{
323 struct port_table_attribute *tab_attr =
324 container_of(attr, struct port_table_attribute, attr);
325 int offset = tab_attr->index & 0xffff;
326 int width = (tab_attr->index >> 16) & 0xff;
327 struct ib_mad *in_mad = NULL;
328 struct ib_mad *out_mad = NULL;
Ira Weiny4cd7c942015-06-06 14:38:31 -0400329 size_t mad_size = sizeof(*out_mad);
330 u16 out_mad_pkey_index = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 ssize_t ret;
332
333 if (!p->ibdev->process_mad)
334 return sprintf(buf, "N/A (no PMA)\n");
335
Roland Dreierde6eb662005-11-02 07:23:14 -0800336 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
Dotan Barak856c52a2007-07-10 16:55:57 +0300337 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 if (!in_mad || !out_mad) {
339 ret = -ENOMEM;
340 goto out;
341 }
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 in_mad->mad_hdr.base_version = 1;
344 in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
345 in_mad->mad_hdr.class_version = 1;
346 in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
347 in_mad->mad_hdr.attr_id = cpu_to_be16(0x12); /* PortCounters */
348
349 in_mad->data[41] = p->port_num; /* PortSelect field */
350
351 if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
Ira Weiny4cd7c942015-06-06 14:38:31 -0400352 p->port_num, NULL, NULL,
353 (const struct ib_mad_hdr *)in_mad, mad_size,
354 (struct ib_mad_hdr *)out_mad, &mad_size,
355 &out_mad_pkey_index) &
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
357 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
358 ret = -EINVAL;
359 goto out;
360 }
361
362 switch (width) {
363 case 4:
364 ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
Ralph Campbelld8b9f232006-05-09 10:50:28 -0700365 (4 - (offset % 8))) & 0xf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 break;
367 case 8:
368 ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
369 break;
370 case 16:
371 ret = sprintf(buf, "%u\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700372 be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 break;
374 case 32:
375 ret = sprintf(buf, "%u\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700376 be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 break;
378 default:
379 ret = 0;
380 }
381
382out:
383 kfree(in_mad);
384 kfree(out_mad);
385
386 return ret;
387}
388
389static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
390static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
391static PORT_PMA_ATTR(link_downed , 2, 8, 56);
392static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
393static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
394static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
395static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
396static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
397static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
398static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
399static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
400static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
401static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
402static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
403static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
404static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
405
406static struct attribute *pma_attrs[] = {
407 &port_pma_attr_symbol_error.attr.attr,
408 &port_pma_attr_link_error_recovery.attr.attr,
409 &port_pma_attr_link_downed.attr.attr,
410 &port_pma_attr_port_rcv_errors.attr.attr,
411 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
412 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
413 &port_pma_attr_port_xmit_discards.attr.attr,
414 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
415 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
416 &port_pma_attr_local_link_integrity_errors.attr.attr,
417 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
418 &port_pma_attr_VL15_dropped.attr.attr,
419 &port_pma_attr_port_xmit_data.attr.attr,
420 &port_pma_attr_port_rcv_data.attr.attr,
421 &port_pma_attr_port_xmit_packets.attr.attr,
422 &port_pma_attr_port_rcv_packets.attr.attr,
423 NULL
424};
425
426static struct attribute_group pma_group = {
427 .name = "counters",
428 .attrs = pma_attrs
429};
430
431static void ib_port_release(struct kobject *kobj)
432{
433 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
434 struct attribute *a;
435 int i;
436
Haggai Erancad6d022014-05-18 11:12:25 +0300437 if (p->gid_group.attrs) {
438 for (i = 0; (a = p->gid_group.attrs[i]); ++i)
439 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Haggai Erancad6d022014-05-18 11:12:25 +0300441 kfree(p->gid_group.attrs);
442 }
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500443
Haggai Erancad6d022014-05-18 11:12:25 +0300444 if (p->pkey_group.attrs) {
445 for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
446 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Haggai Erancad6d022014-05-18 11:12:25 +0300448 kfree(p->pkey_group.attrs);
449 }
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 kfree(p);
452}
453
454static struct kobj_type port_type = {
455 .release = ib_port_release,
456 .sysfs_ops = &port_sysfs_ops,
457 .default_attrs = port_default_attrs
458};
459
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100460static void ib_device_release(struct device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100462 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Ira Weiny77386132015-05-13 20:02:58 -0400464 kfree(dev->port_immutable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 kfree(dev);
466}
467
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100468static int ib_device_uevent(struct device *device,
Kay Sievers7eff2e72007-08-14 15:15:12 +0200469 struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100471 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Kay Sievers7eff2e72007-08-14 15:15:12 +0200473 if (add_uevent_var(env, "NAME=%s", dev->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return -ENOMEM;
475
476 /*
Sean Heftycf311cd2006-01-10 07:39:34 -0800477 * It would be nice to pass the node GUID with the event...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 */
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 0;
481}
482
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500483static struct attribute **
484alloc_group_attrs(ssize_t (*show)(struct ib_port *,
485 struct port_attribute *, char *buf),
486 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500488 struct attribute **tab_attr;
489 struct port_table_attribute *element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500492 tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
493 if (!tab_attr)
494 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500496 for (i = 0; i < len; i++) {
Pekka Enberg82ca76b2005-09-06 15:18:35 -0700497 element = kzalloc(sizeof(struct port_table_attribute),
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500498 GFP_KERNEL);
499 if (!element)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500502 if (snprintf(element->name, sizeof(element->name),
Roland Dreier048975a2006-03-20 10:08:25 -0800503 "%d", i) >= sizeof(element->name)) {
504 kfree(element);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 goto err;
Roland Dreier048975a2006-03-20 10:08:25 -0800506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500508 element->attr.attr.name = element->name;
509 element->attr.attr.mode = S_IRUGO;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500510 element->attr.show = show;
511 element->index = i;
Greg Kroah-Hartman21e3bde2010-03-15 14:01:25 -0700512 sysfs_attr_init(&element->attr.attr);
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500513
514 tab_attr[i] = &element->attr.attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
516
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500517 return tab_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
519err:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500520 while (--i >= 0)
521 kfree(tab_attr[i]);
522 kfree(tab_attr);
523 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524}
525
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700526static int add_port(struct ib_device *device, int port_num,
527 int (*port_callback)(struct ib_device *,
528 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 struct ib_port *p;
531 struct ib_port_attr attr;
532 int i;
533 int ret;
534
535 ret = ib_query_port(device, port_num, &attr);
536 if (ret)
537 return ret;
538
Roland Dreierde6eb662005-11-02 07:23:14 -0800539 p = kzalloc(sizeof *p, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (!p)
541 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 p->ibdev = device;
544 p->port_num = port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400546 ret = kobject_init_and_add(&p->kobj, &port_type,
Haggai Eran373c0ea2014-05-18 11:12:24 +0300547 device->ports_parent,
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400548 "%d", port_num);
Haggai Erancad6d022014-05-18 11:12:25 +0300549 if (ret) {
550 kfree(p);
551 return ret;
552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 ret = sysfs_create_group(&p->kobj, &pma_group);
555 if (ret)
556 goto err_put;
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 p->gid_group.name = "gids";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500559 p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
Wei Yongjun80b15042013-06-21 11:24:27 +0800560 if (!p->gid_group.attrs) {
561 ret = -ENOMEM;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500562 goto err_remove_pma;
Wei Yongjun80b15042013-06-21 11:24:27 +0800563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565 ret = sysfs_create_group(&p->kobj, &p->gid_group);
566 if (ret)
567 goto err_free_gid;
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 p->pkey_group.name = "pkeys";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500570 p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
571 attr.pkey_tbl_len);
Wei Yongjun80b15042013-06-21 11:24:27 +0800572 if (!p->pkey_group.attrs) {
573 ret = -ENOMEM;
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500574 goto err_remove_gid;
Wei Yongjun80b15042013-06-21 11:24:27 +0800575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 ret = sysfs_create_group(&p->kobj, &p->pkey_group);
578 if (ret)
579 goto err_free_pkey;
580
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700581 if (port_callback) {
582 ret = port_callback(device, port_num, &p->kobj);
583 if (ret)
584 goto err_remove_pkey;
585 }
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 list_add_tail(&p->kobj.entry, &device->port_list);
588
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400589 kobject_uevent(&p->kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 return 0;
591
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700592err_remove_pkey:
593 sysfs_remove_group(&p->kobj, &p->pkey_group);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595err_free_pkey:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500596 for (i = 0; i < attr.pkey_tbl_len; ++i)
597 kfree(p->pkey_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500599 kfree(p->pkey_group.attrs);
Haggai Erancad6d022014-05-18 11:12:25 +0300600 p->pkey_group.attrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601
602err_remove_gid:
603 sysfs_remove_group(&p->kobj, &p->gid_group);
604
605err_free_gid:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500606 for (i = 0; i < attr.gid_tbl_len; ++i)
607 kfree(p->gid_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500609 kfree(p->gid_group.attrs);
Haggai Erancad6d022014-05-18 11:12:25 +0300610 p->gid_group.attrs = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612err_remove_pma:
613 sysfs_remove_group(&p->kobj, &pma_group);
614
615err_put:
Haggai Erancad6d022014-05-18 11:12:25 +0300616 kobject_put(&p->kobj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 return ret;
618}
619
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100620static ssize_t show_node_type(struct device *device,
621 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100623 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 switch (dev->node_type) {
Tom Tucker07ebafb2006-08-03 16:02:42 -0500626 case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
627 case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type);
Upinder Malhi \(umalhi\)180771a2013-09-10 03:36:59 +0000628 case RDMA_NODE_USNIC: return sprintf(buf, "%d: usNIC\n", dev->node_type);
Upinder Malhi5db57652014-01-15 17:02:36 -0800629 case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
Tom Tucker07ebafb2006-08-03 16:02:42 -0500630 case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
631 case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
632 default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 }
634}
635
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100636static ssize_t show_sys_image_guid(struct device *device,
637 struct device_attribute *dev_attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100639 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct ib_device_attr attr;
641 ssize_t ret;
642
643 ret = ib_query_device(dev, &attr);
644 if (ret)
645 return ret;
646
647 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700648 be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]),
649 be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]),
650 be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]),
651 be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100654static ssize_t show_node_guid(struct device *device,
655 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100657 struct ib_device *dev = container_of(device, struct ib_device, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Sean Heftycf311cd2006-01-10 07:39:34 -0800660 be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
661 be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
662 be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
663 be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100666static ssize_t show_node_desc(struct device *device,
667 struct device_attribute *attr, char *buf)
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800668{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100669 struct ib_device *dev = container_of(device, struct ib_device, dev);
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800670
671 return sprintf(buf, "%.64s\n", dev->node_desc);
672}
673
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100674static ssize_t set_node_desc(struct device *device,
675 struct device_attribute *attr,
676 const char *buf, size_t count)
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800677{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100678 struct ib_device *dev = container_of(device, struct ib_device, dev);
Roland Dreierc5bcbbb2006-02-02 09:47:14 -0800679 struct ib_device_modify desc = {};
680 int ret;
681
682 if (!dev->modify_device)
683 return -EIO;
684
685 memcpy(desc.node_desc, buf, min_t(int, count, 64));
686 ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
687 if (ret)
688 return ret;
689
690 return count;
691}
692
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100693static DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
694static DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
695static DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
696static DEVICE_ATTR(node_desc, S_IRUGO | S_IWUSR, show_node_desc, set_node_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100698static struct device_attribute *ib_class_attributes[] = {
699 &dev_attr_node_type,
700 &dev_attr_sys_image_guid,
701 &dev_attr_node_guid,
702 &dev_attr_node_desc
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703};
704
705static struct class ib_class = {
706 .name = "infiniband",
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100707 .dev_release = ib_device_release,
708 .dev_uevent = ib_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709};
710
Steve Wise7f624d02008-07-14 23:48:48 -0700711/* Show a given an attribute in the statistics group */
712static ssize_t show_protocol_stat(const struct device *device,
713 struct device_attribute *attr, char *buf,
714 unsigned offset)
715{
716 struct ib_device *dev = container_of(device, struct ib_device, dev);
717 union rdma_protocol_stats stats;
718 ssize_t ret;
719
720 ret = dev->get_protocol_stats(dev, &stats);
721 if (ret)
722 return ret;
723
724 return sprintf(buf, "%llu\n",
725 (unsigned long long) ((u64 *) &stats)[offset]);
726}
727
728/* generate a read-only iwarp statistics attribute */
729#define IW_STATS_ENTRY(name) \
730static ssize_t show_##name(struct device *device, \
731 struct device_attribute *attr, char *buf) \
732{ \
733 return show_protocol_stat(device, attr, buf, \
734 offsetof(struct iw_protocol_stats, name) / \
735 sizeof (u64)); \
736} \
737static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
738
739IW_STATS_ENTRY(ipInReceives);
740IW_STATS_ENTRY(ipInHdrErrors);
741IW_STATS_ENTRY(ipInTooBigErrors);
742IW_STATS_ENTRY(ipInNoRoutes);
743IW_STATS_ENTRY(ipInAddrErrors);
744IW_STATS_ENTRY(ipInUnknownProtos);
745IW_STATS_ENTRY(ipInTruncatedPkts);
746IW_STATS_ENTRY(ipInDiscards);
747IW_STATS_ENTRY(ipInDelivers);
748IW_STATS_ENTRY(ipOutForwDatagrams);
749IW_STATS_ENTRY(ipOutRequests);
750IW_STATS_ENTRY(ipOutDiscards);
751IW_STATS_ENTRY(ipOutNoRoutes);
752IW_STATS_ENTRY(ipReasmTimeout);
753IW_STATS_ENTRY(ipReasmReqds);
754IW_STATS_ENTRY(ipReasmOKs);
755IW_STATS_ENTRY(ipReasmFails);
756IW_STATS_ENTRY(ipFragOKs);
757IW_STATS_ENTRY(ipFragFails);
758IW_STATS_ENTRY(ipFragCreates);
759IW_STATS_ENTRY(ipInMcastPkts);
760IW_STATS_ENTRY(ipOutMcastPkts);
761IW_STATS_ENTRY(ipInBcastPkts);
762IW_STATS_ENTRY(ipOutBcastPkts);
763IW_STATS_ENTRY(tcpRtoAlgorithm);
764IW_STATS_ENTRY(tcpRtoMin);
765IW_STATS_ENTRY(tcpRtoMax);
766IW_STATS_ENTRY(tcpMaxConn);
767IW_STATS_ENTRY(tcpActiveOpens);
768IW_STATS_ENTRY(tcpPassiveOpens);
769IW_STATS_ENTRY(tcpAttemptFails);
770IW_STATS_ENTRY(tcpEstabResets);
771IW_STATS_ENTRY(tcpCurrEstab);
772IW_STATS_ENTRY(tcpInSegs);
773IW_STATS_ENTRY(tcpOutSegs);
774IW_STATS_ENTRY(tcpRetransSegs);
775IW_STATS_ENTRY(tcpInErrs);
776IW_STATS_ENTRY(tcpOutRsts);
777
778static struct attribute *iw_proto_stats_attrs[] = {
779 &dev_attr_ipInReceives.attr,
780 &dev_attr_ipInHdrErrors.attr,
781 &dev_attr_ipInTooBigErrors.attr,
782 &dev_attr_ipInNoRoutes.attr,
783 &dev_attr_ipInAddrErrors.attr,
784 &dev_attr_ipInUnknownProtos.attr,
785 &dev_attr_ipInTruncatedPkts.attr,
786 &dev_attr_ipInDiscards.attr,
787 &dev_attr_ipInDelivers.attr,
788 &dev_attr_ipOutForwDatagrams.attr,
789 &dev_attr_ipOutRequests.attr,
790 &dev_attr_ipOutDiscards.attr,
791 &dev_attr_ipOutNoRoutes.attr,
792 &dev_attr_ipReasmTimeout.attr,
793 &dev_attr_ipReasmReqds.attr,
794 &dev_attr_ipReasmOKs.attr,
795 &dev_attr_ipReasmFails.attr,
796 &dev_attr_ipFragOKs.attr,
797 &dev_attr_ipFragFails.attr,
798 &dev_attr_ipFragCreates.attr,
799 &dev_attr_ipInMcastPkts.attr,
800 &dev_attr_ipOutMcastPkts.attr,
801 &dev_attr_ipInBcastPkts.attr,
802 &dev_attr_ipOutBcastPkts.attr,
803 &dev_attr_tcpRtoAlgorithm.attr,
804 &dev_attr_tcpRtoMin.attr,
805 &dev_attr_tcpRtoMax.attr,
806 &dev_attr_tcpMaxConn.attr,
807 &dev_attr_tcpActiveOpens.attr,
808 &dev_attr_tcpPassiveOpens.attr,
809 &dev_attr_tcpAttemptFails.attr,
810 &dev_attr_tcpEstabResets.attr,
811 &dev_attr_tcpCurrEstab.attr,
812 &dev_attr_tcpInSegs.attr,
813 &dev_attr_tcpOutSegs.attr,
814 &dev_attr_tcpRetransSegs.attr,
815 &dev_attr_tcpInErrs.attr,
816 &dev_attr_tcpOutRsts.attr,
817 NULL
818};
819
820static struct attribute_group iw_stats_group = {
821 .name = "proto_stats",
822 .attrs = iw_proto_stats_attrs,
823};
824
Haggai Eran584482a2014-05-18 11:12:26 +0300825static void free_port_list_attributes(struct ib_device *device)
826{
827 struct kobject *p, *t;
828
829 list_for_each_entry_safe(p, t, &device->port_list, entry) {
830 struct ib_port *port = container_of(p, struct ib_port, kobj);
831 list_del(&p->entry);
832 sysfs_remove_group(p, &pma_group);
833 sysfs_remove_group(p, &port->pkey_group);
834 sysfs_remove_group(p, &port->gid_group);
835 kobject_put(p);
836 }
837
838 kobject_put(device->ports_parent);
839}
840
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700841int ib_device_register_sysfs(struct ib_device *device,
842 int (*port_callback)(struct ib_device *,
843 u8, struct kobject *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100845 struct device *class_dev = &device->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 int ret;
847 int i;
848
849 class_dev->class = &ib_class;
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100850 class_dev->parent = device->dma_device;
Kees Cook02aa2a32013-07-03 15:04:56 -0700851 dev_set_name(class_dev, "%s", device->name);
Greg Kroah-Hartman3f7c58a2009-04-30 14:43:31 -0700852 dev_set_drvdata(class_dev, device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 INIT_LIST_HEAD(&device->port_list);
855
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100856 ret = device_register(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (ret)
858 goto err;
859
860 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100861 ret = device_create_file(class_dev, ib_class_attributes[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (ret)
863 goto err_unregister;
864 }
865
Greg Kroah-Hartman35be0682007-12-17 15:54:39 -0400866 device->ports_parent = kobject_create_and_add("ports",
Haggai Eran373c0ea2014-05-18 11:12:24 +0300867 &class_dev->kobj);
Li Zefanc7482b82008-02-15 10:24:49 +0800868 if (!device->ports_parent) {
869 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto err_put;
Li Zefanc7482b82008-02-15 10:24:49 +0800871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Hal Rosenstock41390322015-06-29 09:57:00 -0400873 if (rdma_cap_ib_switch(device)) {
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700874 ret = add_port(device, 0, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (ret)
876 goto err_put;
877 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 for (i = 1; i <= device->phys_port_cnt; ++i) {
Ralph Campbell9a6edb62010-05-06 17:03:25 -0700879 ret = add_port(device, i, port_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (ret)
881 goto err_put;
882 }
883 }
884
Steve Wise7f624d02008-07-14 23:48:48 -0700885 if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats) {
886 ret = sysfs_create_group(&class_dev->kobj, &iw_stats_group);
887 if (ret)
888 goto err_put;
889 }
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return 0;
892
893err_put:
Haggai Eran584482a2014-05-18 11:12:26 +0300894 free_port_list_attributes(device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896err_unregister:
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100897 device_unregister(class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
899err:
900 return ret;
901}
902
903void ib_device_unregister_sysfs(struct ib_device *device)
904{
Roland Dreier9206dff2009-02-25 13:27:46 -0800905 /* Hold kobject until ib_dealloc_device() */
Haggai Eran584482a2014-05-18 11:12:26 +0300906 struct kobject *kobj_dev = kobject_get(&device->dev.kobj);
907 int i;
Roland Dreier9206dff2009-02-25 13:27:46 -0800908
Haggai Eran584482a2014-05-18 11:12:26 +0300909 if (device->node_type == RDMA_NODE_RNIC && device->get_protocol_stats)
910 sysfs_remove_group(kobj_dev, &iw_stats_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Haggai Eran584482a2014-05-18 11:12:26 +0300912 free_port_list_attributes(device);
913
914 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i)
915 device_remove_file(&device->dev, ib_class_attributes[i]);
916
Tony Jonesf4e91eb2008-02-22 00:13:36 +0100917 device_unregister(&device->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918}
919
920int ib_sysfs_setup(void)
921{
922 return class_register(&ib_class);
923}
924
925void ib_sysfs_cleanup(void)
926{
927 class_unregister(&ib_class);
928}