blob: b2e779996cbe0a231f5448b85c5bd61c7cdf748b [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.
33 *
34 * $Id: sysfs.c 1349 2004-12-16 21:09:43Z roland $
35 */
36
37#include "core_priv.h"
38
39#include <ib_mad.h>
40
41struct ib_port {
42 struct kobject kobj;
43 struct ib_device *ibdev;
44 struct attribute_group gid_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct attribute_group pkey_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 u8 port_num;
47};
48
49struct port_attribute {
50 struct attribute attr;
51 ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
52 ssize_t (*store)(struct ib_port *, struct port_attribute *,
53 const char *buf, size_t count);
54};
55
56#define PORT_ATTR(_name, _mode, _show, _store) \
57struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
58
59#define PORT_ATTR_RO(_name) \
60struct port_attribute port_attr_##_name = __ATTR_RO(_name)
61
62struct port_table_attribute {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -050063 struct port_attribute attr;
64 char name[8];
65 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066};
67
68static ssize_t port_attr_show(struct kobject *kobj,
69 struct attribute *attr, char *buf)
70{
71 struct port_attribute *port_attr =
72 container_of(attr, struct port_attribute, attr);
73 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
74
75 if (!port_attr->show)
Dmitry Torokhov70f28172005-04-29 01:27:34 -050076 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 return port_attr->show(p, port_attr, buf);
79}
80
81static struct sysfs_ops port_sysfs_ops = {
82 .show = port_attr_show
83};
84
85static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
86 char *buf)
87{
88 struct ib_port_attr attr;
89 ssize_t ret;
90
91 static const char *state_name[] = {
92 [IB_PORT_NOP] = "NOP",
93 [IB_PORT_DOWN] = "DOWN",
94 [IB_PORT_INIT] = "INIT",
95 [IB_PORT_ARMED] = "ARMED",
96 [IB_PORT_ACTIVE] = "ACTIVE",
97 [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
98 };
99
100 ret = ib_query_port(p->ibdev, p->port_num, &attr);
101 if (ret)
102 return ret;
103
104 return sprintf(buf, "%d: %s\n", attr.state,
105 attr.state >= 0 && attr.state <= ARRAY_SIZE(state_name) ?
106 state_name[attr.state] : "UNKNOWN");
107}
108
109static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
110 char *buf)
111{
112 struct ib_port_attr attr;
113 ssize_t ret;
114
115 ret = ib_query_port(p->ibdev, p->port_num, &attr);
116 if (ret)
117 return ret;
118
119 return sprintf(buf, "0x%x\n", attr.lid);
120}
121
122static ssize_t lid_mask_count_show(struct ib_port *p,
123 struct port_attribute *unused,
124 char *buf)
125{
126 struct ib_port_attr attr;
127 ssize_t ret;
128
129 ret = ib_query_port(p->ibdev, p->port_num, &attr);
130 if (ret)
131 return ret;
132
133 return sprintf(buf, "%d\n", attr.lmc);
134}
135
136static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
137 char *buf)
138{
139 struct ib_port_attr attr;
140 ssize_t ret;
141
142 ret = ib_query_port(p->ibdev, p->port_num, &attr);
143 if (ret)
144 return ret;
145
146 return sprintf(buf, "0x%x\n", attr.sm_lid);
147}
148
149static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
150 char *buf)
151{
152 struct ib_port_attr attr;
153 ssize_t ret;
154
155 ret = ib_query_port(p->ibdev, p->port_num, &attr);
156 if (ret)
157 return ret;
158
159 return sprintf(buf, "%d\n", attr.sm_sl);
160}
161
162static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
163 char *buf)
164{
165 struct ib_port_attr attr;
166 ssize_t ret;
167
168 ret = ib_query_port(p->ibdev, p->port_num, &attr);
169 if (ret)
170 return ret;
171
172 return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
173}
174
175static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
176 char *buf)
177{
178 struct ib_port_attr attr;
179 char *speed = "";
180 int rate;
181 ssize_t ret;
182
183 ret = ib_query_port(p->ibdev, p->port_num, &attr);
184 if (ret)
185 return ret;
186
187 switch (attr.active_speed) {
188 case 2: speed = " DDR"; break;
189 case 4: speed = " QDR"; break;
190 }
191
192 rate = 25 * ib_width_enum_to_int(attr.active_width) * attr.active_speed;
193 if (rate < 0)
194 return -EINVAL;
195
196 return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
197 rate / 10, rate % 10 ? ".5" : "",
198 ib_width_enum_to_int(attr.active_width), speed);
199}
200
201static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
202 char *buf)
203{
204 struct ib_port_attr attr;
205
206 ssize_t ret;
207
208 ret = ib_query_port(p->ibdev, p->port_num, &attr);
209 if (ret)
210 return ret;
211
212 switch (attr.phys_state) {
213 case 1: return sprintf(buf, "1: Sleep\n");
214 case 2: return sprintf(buf, "2: Polling\n");
215 case 3: return sprintf(buf, "3: Disabled\n");
216 case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
217 case 5: return sprintf(buf, "5: LinkUp\n");
218 case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
219 case 7: return sprintf(buf, "7: Phy Test\n");
220 default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
221 }
222}
223
224static PORT_ATTR_RO(state);
225static PORT_ATTR_RO(lid);
226static PORT_ATTR_RO(lid_mask_count);
227static PORT_ATTR_RO(sm_lid);
228static PORT_ATTR_RO(sm_sl);
229static PORT_ATTR_RO(cap_mask);
230static PORT_ATTR_RO(rate);
231static PORT_ATTR_RO(phys_state);
232
233static struct attribute *port_default_attrs[] = {
234 &port_attr_state.attr,
235 &port_attr_lid.attr,
236 &port_attr_lid_mask_count.attr,
237 &port_attr_sm_lid.attr,
238 &port_attr_sm_sl.attr,
239 &port_attr_cap_mask.attr,
240 &port_attr_rate.attr,
241 &port_attr_phys_state.attr,
242 NULL
243};
244
245static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
246 char *buf)
247{
248 struct port_table_attribute *tab_attr =
249 container_of(attr, struct port_table_attribute, attr);
250 union ib_gid gid;
251 ssize_t ret;
252
253 ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
254 if (ret)
255 return ret;
256
257 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
258 be16_to_cpu(((u16 *) gid.raw)[0]),
259 be16_to_cpu(((u16 *) gid.raw)[1]),
260 be16_to_cpu(((u16 *) gid.raw)[2]),
261 be16_to_cpu(((u16 *) gid.raw)[3]),
262 be16_to_cpu(((u16 *) gid.raw)[4]),
263 be16_to_cpu(((u16 *) gid.raw)[5]),
264 be16_to_cpu(((u16 *) gid.raw)[6]),
265 be16_to_cpu(((u16 *) gid.raw)[7]));
266}
267
268static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
269 char *buf)
270{
271 struct port_table_attribute *tab_attr =
272 container_of(attr, struct port_table_attribute, attr);
273 u16 pkey;
274 ssize_t ret;
275
276 ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
277 if (ret)
278 return ret;
279
280 return sprintf(buf, "0x%04x\n", pkey);
281}
282
283#define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
284struct port_table_attribute port_pma_attr_##_name = { \
285 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
286 .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \
287}
288
289static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
290 char *buf)
291{
292 struct port_table_attribute *tab_attr =
293 container_of(attr, struct port_table_attribute, attr);
294 int offset = tab_attr->index & 0xffff;
295 int width = (tab_attr->index >> 16) & 0xff;
296 struct ib_mad *in_mad = NULL;
297 struct ib_mad *out_mad = NULL;
298 ssize_t ret;
299
300 if (!p->ibdev->process_mad)
301 return sprintf(buf, "N/A (no PMA)\n");
302
303 in_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
304 out_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
305 if (!in_mad || !out_mad) {
306 ret = -ENOMEM;
307 goto out;
308 }
309
310 memset(in_mad, 0, sizeof *in_mad);
311 in_mad->mad_hdr.base_version = 1;
312 in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
313 in_mad->mad_hdr.class_version = 1;
314 in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
315 in_mad->mad_hdr.attr_id = cpu_to_be16(0x12); /* PortCounters */
316
317 in_mad->data[41] = p->port_num; /* PortSelect field */
318
319 if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
320 p->port_num, NULL, NULL, in_mad, out_mad) &
321 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
322 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
323 ret = -EINVAL;
324 goto out;
325 }
326
327 switch (width) {
328 case 4:
329 ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
330 (offset % 4)) & 0xf);
331 break;
332 case 8:
333 ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
334 break;
335 case 16:
336 ret = sprintf(buf, "%u\n",
337 be16_to_cpup((u16 *)(out_mad->data + 40 + offset / 8)));
338 break;
339 case 32:
340 ret = sprintf(buf, "%u\n",
341 be32_to_cpup((u32 *)(out_mad->data + 40 + offset / 8)));
342 break;
343 default:
344 ret = 0;
345 }
346
347out:
348 kfree(in_mad);
349 kfree(out_mad);
350
351 return ret;
352}
353
354static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
355static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
356static PORT_PMA_ATTR(link_downed , 2, 8, 56);
357static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
358static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
359static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
360static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
361static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
362static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
363static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
364static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
365static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
366static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
367static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
368static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
369static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
370
371static struct attribute *pma_attrs[] = {
372 &port_pma_attr_symbol_error.attr.attr,
373 &port_pma_attr_link_error_recovery.attr.attr,
374 &port_pma_attr_link_downed.attr.attr,
375 &port_pma_attr_port_rcv_errors.attr.attr,
376 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
377 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
378 &port_pma_attr_port_xmit_discards.attr.attr,
379 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
380 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
381 &port_pma_attr_local_link_integrity_errors.attr.attr,
382 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
383 &port_pma_attr_VL15_dropped.attr.attr,
384 &port_pma_attr_port_xmit_data.attr.attr,
385 &port_pma_attr_port_rcv_data.attr.attr,
386 &port_pma_attr_port_xmit_packets.attr.attr,
387 &port_pma_attr_port_rcv_packets.attr.attr,
388 NULL
389};
390
391static struct attribute_group pma_group = {
392 .name = "counters",
393 .attrs = pma_attrs
394};
395
396static void ib_port_release(struct kobject *kobj)
397{
398 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
399 struct attribute *a;
400 int i;
401
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500402 for (i = 0; (a = p->gid_group.attrs[i]); ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500405 kfree(p->gid_group.attrs);
406
407 for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500410 kfree(p->pkey_group.attrs);
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 kfree(p);
413}
414
415static struct kobj_type port_type = {
416 .release = ib_port_release,
417 .sysfs_ops = &port_sysfs_ops,
418 .default_attrs = port_default_attrs
419};
420
421static void ib_device_release(struct class_device *cdev)
422{
423 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
424
425 kfree(dev);
426}
427
428static int ib_device_hotplug(struct class_device *cdev, char **envp,
429 int num_envp, char *buf, int size)
430{
431 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
432 int i = 0, len = 0;
433
434 if (add_hotplug_env_var(envp, num_envp, &i, buf, size, &len,
435 "NAME=%s", dev->name))
436 return -ENOMEM;
437
438 /*
439 * It might be nice to pass the node GUID to hotplug, but
440 * right now the only way to get it is to query the device
441 * provider, and this can crash during device removal because
442 * we are will be running after driver removal has started.
443 * We could add a node_guid field to struct ib_device, or we
444 * could just let the hotplug script read the node GUID from
445 * sysfs when devices are added.
446 */
447
448 envp[i] = NULL;
449 return 0;
450}
451
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500452static struct attribute **
453alloc_group_attrs(ssize_t (*show)(struct ib_port *,
454 struct port_attribute *, char *buf),
455 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500457 struct attribute **tab_attr;
458 struct port_table_attribute *element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500461 tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
462 if (!tab_attr)
463 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500465 for (i = 0; i < len; i++) {
466 element = kcalloc(1, sizeof(struct port_table_attribute),
467 GFP_KERNEL);
468 if (!element)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500471 if (snprintf(element->name, sizeof(element->name),
472 "%d", i) >= sizeof(element->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500475 element->attr.attr.name = element->name;
476 element->attr.attr.mode = S_IRUGO;
477 element->attr.attr.owner = THIS_MODULE;
478 element->attr.show = show;
479 element->index = i;
480
481 tab_attr[i] = &element->attr.attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500484 return tab_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486err:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500487 while (--i >= 0)
488 kfree(tab_attr[i]);
489 kfree(tab_attr);
490 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493static int add_port(struct ib_device *device, int port_num)
494{
495 struct ib_port *p;
496 struct ib_port_attr attr;
497 int i;
498 int ret;
499
500 ret = ib_query_port(device, port_num, &attr);
501 if (ret)
502 return ret;
503
504 p = kmalloc(sizeof *p, GFP_KERNEL);
505 if (!p)
506 return -ENOMEM;
507 memset(p, 0, sizeof *p);
508
509 p->ibdev = device;
510 p->port_num = port_num;
511 p->kobj.ktype = &port_type;
512
513 p->kobj.parent = kobject_get(&device->ports_parent);
514 if (!p->kobj.parent) {
515 ret = -EBUSY;
516 goto err;
517 }
518
519 ret = kobject_set_name(&p->kobj, "%d", port_num);
520 if (ret)
521 goto err_put;
522
523 ret = kobject_register(&p->kobj);
524 if (ret)
525 goto err_put;
526
527 ret = sysfs_create_group(&p->kobj, &pma_group);
528 if (ret)
529 goto err_put;
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 p->gid_group.name = "gids";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500532 p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
533 if (!p->gid_group.attrs)
534 goto err_remove_pma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
536 ret = sysfs_create_group(&p->kobj, &p->gid_group);
537 if (ret)
538 goto err_free_gid;
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 p->pkey_group.name = "pkeys";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500541 p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
542 attr.pkey_tbl_len);
543 if (!p->pkey_group.attrs)
544 goto err_remove_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 ret = sysfs_create_group(&p->kobj, &p->pkey_group);
547 if (ret)
548 goto err_free_pkey;
549
550 list_add_tail(&p->kobj.entry, &device->port_list);
551
552 return 0;
553
554err_free_pkey:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500555 for (i = 0; i < attr.pkey_tbl_len; ++i)
556 kfree(p->pkey_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500558 kfree(p->pkey_group.attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560err_remove_gid:
561 sysfs_remove_group(&p->kobj, &p->gid_group);
562
563err_free_gid:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500564 for (i = 0; i < attr.gid_tbl_len; ++i)
565 kfree(p->gid_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500567 kfree(p->gid_group.attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
569err_remove_pma:
570 sysfs_remove_group(&p->kobj, &pma_group);
571
572err_put:
573 kobject_put(&device->ports_parent);
574
575err:
576 kfree(p);
577 return ret;
578}
579
580static ssize_t show_node_type(struct class_device *cdev, char *buf)
581{
582 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
583
584 switch (dev->node_type) {
585 case IB_NODE_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
586 case IB_NODE_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
587 case IB_NODE_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
588 default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
589 }
590}
591
592static ssize_t show_sys_image_guid(struct class_device *cdev, char *buf)
593{
594 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
595 struct ib_device_attr attr;
596 ssize_t ret;
597
598 ret = ib_query_device(dev, &attr);
599 if (ret)
600 return ret;
601
602 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
603 be16_to_cpu(((u16 *) &attr.sys_image_guid)[0]),
604 be16_to_cpu(((u16 *) &attr.sys_image_guid)[1]),
605 be16_to_cpu(((u16 *) &attr.sys_image_guid)[2]),
606 be16_to_cpu(((u16 *) &attr.sys_image_guid)[3]));
607}
608
609static ssize_t show_node_guid(struct class_device *cdev, char *buf)
610{
611 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
612 struct ib_device_attr attr;
613 ssize_t ret;
614
615 ret = ib_query_device(dev, &attr);
616 if (ret)
617 return ret;
618
619 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
620 be16_to_cpu(((u16 *) &attr.node_guid)[0]),
621 be16_to_cpu(((u16 *) &attr.node_guid)[1]),
622 be16_to_cpu(((u16 *) &attr.node_guid)[2]),
623 be16_to_cpu(((u16 *) &attr.node_guid)[3]));
624}
625
626static CLASS_DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
627static CLASS_DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
628static CLASS_DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
629
630static struct class_device_attribute *ib_class_attributes[] = {
631 &class_device_attr_node_type,
632 &class_device_attr_sys_image_guid,
633 &class_device_attr_node_guid
634};
635
636static struct class ib_class = {
637 .name = "infiniband",
638 .release = ib_device_release,
639 .hotplug = ib_device_hotplug,
640};
641
642int ib_device_register_sysfs(struct ib_device *device)
643{
644 struct class_device *class_dev = &device->class_dev;
645 int ret;
646 int i;
647
648 class_dev->class = &ib_class;
649 class_dev->class_data = device;
650 strlcpy(class_dev->class_id, device->name, BUS_ID_SIZE);
651
652 INIT_LIST_HEAD(&device->port_list);
653
654 ret = class_device_register(class_dev);
655 if (ret)
656 goto err;
657
658 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
659 ret = class_device_create_file(class_dev, ib_class_attributes[i]);
660 if (ret)
661 goto err_unregister;
662 }
663
664 device->ports_parent.parent = kobject_get(&class_dev->kobj);
665 if (!device->ports_parent.parent) {
666 ret = -EBUSY;
667 goto err_unregister;
668 }
669 ret = kobject_set_name(&device->ports_parent, "ports");
670 if (ret)
671 goto err_put;
672 ret = kobject_register(&device->ports_parent);
673 if (ret)
674 goto err_put;
675
676 if (device->node_type == IB_NODE_SWITCH) {
677 ret = add_port(device, 0);
678 if (ret)
679 goto err_put;
680 } else {
681 int i;
682
683 for (i = 1; i <= device->phys_port_cnt; ++i) {
684 ret = add_port(device, i);
685 if (ret)
686 goto err_put;
687 }
688 }
689
690 return 0;
691
692err_put:
693 {
694 struct kobject *p, *t;
695 struct ib_port *port;
696
697 list_for_each_entry_safe(p, t, &device->port_list, entry) {
698 list_del(&p->entry);
699 port = container_of(p, struct ib_port, kobj);
700 sysfs_remove_group(p, &pma_group);
701 sysfs_remove_group(p, &port->pkey_group);
702 sysfs_remove_group(p, &port->gid_group);
703 kobject_unregister(p);
704 }
705 }
706
707 kobject_put(&class_dev->kobj);
708
709err_unregister:
710 class_device_unregister(class_dev);
711
712err:
713 return ret;
714}
715
716void ib_device_unregister_sysfs(struct ib_device *device)
717{
718 struct kobject *p, *t;
719 struct ib_port *port;
720
721 list_for_each_entry_safe(p, t, &device->port_list, entry) {
722 list_del(&p->entry);
723 port = container_of(p, struct ib_port, kobj);
724 sysfs_remove_group(p, &pma_group);
725 sysfs_remove_group(p, &port->pkey_group);
726 sysfs_remove_group(p, &port->gid_group);
727 kobject_unregister(p);
728 }
729
730 kobject_unregister(&device->ports_parent);
731 class_device_unregister(&device->class_dev);
732}
733
734int ib_sysfs_setup(void)
735{
736 return class_register(&ib_class);
737}
738
739void ib_sysfs_cleanup(void)
740{
741 class_unregister(&ib_class);
742}