blob: 5982d687a0009e2db11befcce2d0c4d9fde519f8 [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
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080039#include <linux/slab.h>
40#include <linux/string.h>
41
Roland Dreiera4d61e82005-08-25 13:40:04 -070042#include <rdma/ib_mad.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44struct ib_port {
45 struct kobject kobj;
46 struct ib_device *ibdev;
47 struct attribute_group gid_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct attribute_group pkey_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 u8 port_num;
50};
51
52struct port_attribute {
53 struct attribute attr;
54 ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
55 ssize_t (*store)(struct ib_port *, struct port_attribute *,
56 const char *buf, size_t count);
57};
58
59#define PORT_ATTR(_name, _mode, _show, _store) \
60struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
61
62#define PORT_ATTR_RO(_name) \
63struct port_attribute port_attr_##_name = __ATTR_RO(_name)
64
65struct port_table_attribute {
Dmitry Torokhovd48593b2005-04-29 00:58:46 -050066 struct port_attribute attr;
67 char name[8];
68 int index;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069};
70
Roland Dreierba8e9312005-10-18 14:14:56 -070071static inline int ibdev_is_alive(const struct ib_device *dev)
72{
73 return dev->reg_state == IB_DEV_REGISTERED;
74}
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static ssize_t port_attr_show(struct kobject *kobj,
77 struct attribute *attr, char *buf)
78{
79 struct port_attribute *port_attr =
80 container_of(attr, struct port_attribute, attr);
81 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
82
83 if (!port_attr->show)
Dmitry Torokhov70f28172005-04-29 01:27:34 -050084 return -EIO;
Roland Dreierba8e9312005-10-18 14:14:56 -070085 if (!ibdev_is_alive(p->ibdev))
86 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 return port_attr->show(p, port_attr, buf);
89}
90
91static struct sysfs_ops port_sysfs_ops = {
92 .show = port_attr_show
93};
94
95static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
96 char *buf)
97{
98 struct ib_port_attr attr;
99 ssize_t ret;
100
101 static const char *state_name[] = {
102 [IB_PORT_NOP] = "NOP",
103 [IB_PORT_DOWN] = "DOWN",
104 [IB_PORT_INIT] = "INIT",
105 [IB_PORT_ARMED] = "ARMED",
106 [IB_PORT_ACTIVE] = "ACTIVE",
107 [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
108 };
109
110 ret = ib_query_port(p->ibdev, p->port_num, &attr);
111 if (ret)
112 return ret;
113
114 return sprintf(buf, "%d: %s\n", attr.state,
115 attr.state >= 0 && attr.state <= ARRAY_SIZE(state_name) ?
116 state_name[attr.state] : "UNKNOWN");
117}
118
119static ssize_t lid_show(struct ib_port *p, struct port_attribute *unused,
120 char *buf)
121{
122 struct ib_port_attr attr;
123 ssize_t ret;
124
125 ret = ib_query_port(p->ibdev, p->port_num, &attr);
126 if (ret)
127 return ret;
128
129 return sprintf(buf, "0x%x\n", attr.lid);
130}
131
132static ssize_t lid_mask_count_show(struct ib_port *p,
133 struct port_attribute *unused,
134 char *buf)
135{
136 struct ib_port_attr attr;
137 ssize_t ret;
138
139 ret = ib_query_port(p->ibdev, p->port_num, &attr);
140 if (ret)
141 return ret;
142
143 return sprintf(buf, "%d\n", attr.lmc);
144}
145
146static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
147 char *buf)
148{
149 struct ib_port_attr attr;
150 ssize_t ret;
151
152 ret = ib_query_port(p->ibdev, p->port_num, &attr);
153 if (ret)
154 return ret;
155
156 return sprintf(buf, "0x%x\n", attr.sm_lid);
157}
158
159static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
160 char *buf)
161{
162 struct ib_port_attr attr;
163 ssize_t ret;
164
165 ret = ib_query_port(p->ibdev, p->port_num, &attr);
166 if (ret)
167 return ret;
168
169 return sprintf(buf, "%d\n", attr.sm_sl);
170}
171
172static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
173 char *buf)
174{
175 struct ib_port_attr attr;
176 ssize_t ret;
177
178 ret = ib_query_port(p->ibdev, p->port_num, &attr);
179 if (ret)
180 return ret;
181
182 return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
183}
184
185static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
186 char *buf)
187{
188 struct ib_port_attr attr;
189 char *speed = "";
190 int rate;
191 ssize_t ret;
192
193 ret = ib_query_port(p->ibdev, p->port_num, &attr);
194 if (ret)
195 return ret;
196
197 switch (attr.active_speed) {
198 case 2: speed = " DDR"; break;
199 case 4: speed = " QDR"; break;
200 }
201
202 rate = 25 * ib_width_enum_to_int(attr.active_width) * attr.active_speed;
203 if (rate < 0)
204 return -EINVAL;
205
206 return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
207 rate / 10, rate % 10 ? ".5" : "",
208 ib_width_enum_to_int(attr.active_width), speed);
209}
210
211static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
212 char *buf)
213{
214 struct ib_port_attr attr;
215
216 ssize_t ret;
217
218 ret = ib_query_port(p->ibdev, p->port_num, &attr);
219 if (ret)
220 return ret;
221
222 switch (attr.phys_state) {
223 case 1: return sprintf(buf, "1: Sleep\n");
224 case 2: return sprintf(buf, "2: Polling\n");
225 case 3: return sprintf(buf, "3: Disabled\n");
226 case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
227 case 5: return sprintf(buf, "5: LinkUp\n");
228 case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
229 case 7: return sprintf(buf, "7: Phy Test\n");
230 default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
231 }
232}
233
234static PORT_ATTR_RO(state);
235static PORT_ATTR_RO(lid);
236static PORT_ATTR_RO(lid_mask_count);
237static PORT_ATTR_RO(sm_lid);
238static PORT_ATTR_RO(sm_sl);
239static PORT_ATTR_RO(cap_mask);
240static PORT_ATTR_RO(rate);
241static PORT_ATTR_RO(phys_state);
242
243static struct attribute *port_default_attrs[] = {
244 &port_attr_state.attr,
245 &port_attr_lid.attr,
246 &port_attr_lid_mask_count.attr,
247 &port_attr_sm_lid.attr,
248 &port_attr_sm_sl.attr,
249 &port_attr_cap_mask.attr,
250 &port_attr_rate.attr,
251 &port_attr_phys_state.attr,
252 NULL
253};
254
255static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
256 char *buf)
257{
258 struct port_table_attribute *tab_attr =
259 container_of(attr, struct port_table_attribute, attr);
260 union ib_gid gid;
261 ssize_t ret;
262
263 ret = ib_query_gid(p->ibdev, p->port_num, tab_attr->index, &gid);
264 if (ret)
265 return ret;
266
267 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700268 be16_to_cpu(((__be16 *) gid.raw)[0]),
269 be16_to_cpu(((__be16 *) gid.raw)[1]),
270 be16_to_cpu(((__be16 *) gid.raw)[2]),
271 be16_to_cpu(((__be16 *) gid.raw)[3]),
272 be16_to_cpu(((__be16 *) gid.raw)[4]),
273 be16_to_cpu(((__be16 *) gid.raw)[5]),
274 be16_to_cpu(((__be16 *) gid.raw)[6]),
275 be16_to_cpu(((__be16 *) gid.raw)[7]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
278static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
279 char *buf)
280{
281 struct port_table_attribute *tab_attr =
282 container_of(attr, struct port_table_attribute, attr);
283 u16 pkey;
284 ssize_t ret;
285
286 ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
287 if (ret)
288 return ret;
289
290 return sprintf(buf, "0x%04x\n", pkey);
291}
292
293#define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
294struct port_table_attribute port_pma_attr_##_name = { \
295 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
296 .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \
297}
298
299static ssize_t show_pma_counter(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 int offset = tab_attr->index & 0xffff;
305 int width = (tab_attr->index >> 16) & 0xff;
306 struct ib_mad *in_mad = NULL;
307 struct ib_mad *out_mad = NULL;
308 ssize_t ret;
309
310 if (!p->ibdev->process_mad)
311 return sprintf(buf, "N/A (no PMA)\n");
312
Roland Dreierde6eb662005-11-02 07:23:14 -0800313 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 out_mad = kmalloc(sizeof *in_mad, GFP_KERNEL);
315 if (!in_mad || !out_mad) {
316 ret = -ENOMEM;
317 goto out;
318 }
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 in_mad->mad_hdr.base_version = 1;
321 in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
322 in_mad->mad_hdr.class_version = 1;
323 in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
324 in_mad->mad_hdr.attr_id = cpu_to_be16(0x12); /* PortCounters */
325
326 in_mad->data[41] = p->port_num; /* PortSelect field */
327
328 if ((p->ibdev->process_mad(p->ibdev, IB_MAD_IGNORE_MKEY,
329 p->port_num, NULL, NULL, in_mad, out_mad) &
330 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
331 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
332 ret = -EINVAL;
333 goto out;
334 }
335
336 switch (width) {
337 case 4:
338 ret = sprintf(buf, "%u\n", (out_mad->data[40 + offset / 8] >>
339 (offset % 4)) & 0xf);
340 break;
341 case 8:
342 ret = sprintf(buf, "%u\n", out_mad->data[40 + offset / 8]);
343 break;
344 case 16:
345 ret = sprintf(buf, "%u\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700346 be16_to_cpup((__be16 *)(out_mad->data + 40 + offset / 8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 break;
348 case 32:
349 ret = sprintf(buf, "%u\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700350 be32_to_cpup((__be32 *)(out_mad->data + 40 + offset / 8)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 break;
352 default:
353 ret = 0;
354 }
355
356out:
357 kfree(in_mad);
358 kfree(out_mad);
359
360 return ret;
361}
362
363static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
364static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
365static PORT_PMA_ATTR(link_downed , 2, 8, 56);
366static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
367static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
368static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
369static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
370static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
371static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
372static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
373static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
374static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
375static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
376static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
377static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
378static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
379
380static struct attribute *pma_attrs[] = {
381 &port_pma_attr_symbol_error.attr.attr,
382 &port_pma_attr_link_error_recovery.attr.attr,
383 &port_pma_attr_link_downed.attr.attr,
384 &port_pma_attr_port_rcv_errors.attr.attr,
385 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
386 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
387 &port_pma_attr_port_xmit_discards.attr.attr,
388 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
389 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
390 &port_pma_attr_local_link_integrity_errors.attr.attr,
391 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
392 &port_pma_attr_VL15_dropped.attr.attr,
393 &port_pma_attr_port_xmit_data.attr.attr,
394 &port_pma_attr_port_rcv_data.attr.attr,
395 &port_pma_attr_port_xmit_packets.attr.attr,
396 &port_pma_attr_port_rcv_packets.attr.attr,
397 NULL
398};
399
400static struct attribute_group pma_group = {
401 .name = "counters",
402 .attrs = pma_attrs
403};
404
405static void ib_port_release(struct kobject *kobj)
406{
407 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
408 struct attribute *a;
409 int i;
410
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500411 for (i = 0; (a = p->gid_group.attrs[i]); ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500414 kfree(p->gid_group.attrs);
415
416 for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 kfree(a);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500419 kfree(p->pkey_group.attrs);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 kfree(p);
422}
423
424static struct kobj_type port_type = {
425 .release = ib_port_release,
426 .sysfs_ops = &port_sysfs_ops,
427 .default_attrs = port_default_attrs
428};
429
430static void ib_device_release(struct class_device *cdev)
431{
432 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
433
434 kfree(dev);
435}
436
Kay Sievers312c0042005-11-16 09:00:00 +0100437static int ib_device_uevent(struct class_device *cdev, char **envp,
438 int num_envp, char *buf, int size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
441 int i = 0, len = 0;
442
Kay Sievers312c0042005-11-16 09:00:00 +0100443 if (add_uevent_var(envp, num_envp, &i, buf, size, &len,
444 "NAME=%s", dev->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -ENOMEM;
446
447 /*
Sean Heftycf311cd2006-01-10 07:39:34 -0800448 * It would be nice to pass the node GUID with the event...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
450
451 envp[i] = NULL;
452 return 0;
453}
454
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500455static struct attribute **
456alloc_group_attrs(ssize_t (*show)(struct ib_port *,
457 struct port_attribute *, char *buf),
458 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500460 struct attribute **tab_attr;
461 struct port_table_attribute *element;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500464 tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
465 if (!tab_attr)
466 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500468 for (i = 0; i < len; i++) {
Pekka Enberg82ca76b2005-09-06 15:18:35 -0700469 element = kzalloc(sizeof(struct port_table_attribute),
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500470 GFP_KERNEL);
471 if (!element)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500474 if (snprintf(element->name, sizeof(element->name),
475 "%d", i) >= sizeof(element->name))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 goto err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500478 element->attr.attr.name = element->name;
479 element->attr.attr.mode = S_IRUGO;
480 element->attr.attr.owner = THIS_MODULE;
481 element->attr.show = show;
482 element->index = i;
483
484 tab_attr[i] = &element->attr.attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500487 return tab_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
489err:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500490 while (--i >= 0)
491 kfree(tab_attr[i]);
492 kfree(tab_attr);
493 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496static int add_port(struct ib_device *device, int port_num)
497{
498 struct ib_port *p;
499 struct ib_port_attr attr;
500 int i;
501 int ret;
502
503 ret = ib_query_port(device, port_num, &attr);
504 if (ret)
505 return ret;
506
Roland Dreierde6eb662005-11-02 07:23:14 -0800507 p = kzalloc(sizeof *p, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (!p)
509 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 p->ibdev = device;
512 p->port_num = port_num;
513 p->kobj.ktype = &port_type;
514
515 p->kobj.parent = kobject_get(&device->ports_parent);
516 if (!p->kobj.parent) {
517 ret = -EBUSY;
518 goto err;
519 }
520
521 ret = kobject_set_name(&p->kobj, "%d", port_num);
522 if (ret)
523 goto err_put;
524
525 ret = kobject_register(&p->kobj);
526 if (ret)
527 goto err_put;
528
529 ret = sysfs_create_group(&p->kobj, &pma_group);
530 if (ret)
531 goto err_put;
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 p->gid_group.name = "gids";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500534 p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
535 if (!p->gid_group.attrs)
536 goto err_remove_pma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 ret = sysfs_create_group(&p->kobj, &p->gid_group);
539 if (ret)
540 goto err_free_gid;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 p->pkey_group.name = "pkeys";
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500543 p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
544 attr.pkey_tbl_len);
545 if (!p->pkey_group.attrs)
546 goto err_remove_gid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 ret = sysfs_create_group(&p->kobj, &p->pkey_group);
549 if (ret)
550 goto err_free_pkey;
551
552 list_add_tail(&p->kobj.entry, &device->port_list);
553
554 return 0;
555
556err_free_pkey:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500557 for (i = 0; i < attr.pkey_tbl_len; ++i)
558 kfree(p->pkey_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500560 kfree(p->pkey_group.attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
562err_remove_gid:
563 sysfs_remove_group(&p->kobj, &p->gid_group);
564
565err_free_gid:
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500566 for (i = 0; i < attr.gid_tbl_len; ++i)
567 kfree(p->gid_group.attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Dmitry Torokhovd48593b2005-04-29 00:58:46 -0500569 kfree(p->gid_group.attrs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571err_remove_pma:
572 sysfs_remove_group(&p->kobj, &pma_group);
573
574err_put:
575 kobject_put(&device->ports_parent);
576
577err:
578 kfree(p);
579 return ret;
580}
581
582static ssize_t show_node_type(struct class_device *cdev, char *buf)
583{
584 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
585
Roland Dreierba8e9312005-10-18 14:14:56 -0700586 if (!ibdev_is_alive(dev))
587 return -ENODEV;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 switch (dev->node_type) {
590 case IB_NODE_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
591 case IB_NODE_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
592 case IB_NODE_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
593 default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
594 }
595}
596
597static ssize_t show_sys_image_guid(struct class_device *cdev, char *buf)
598{
599 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
600 struct ib_device_attr attr;
601 ssize_t ret;
602
Roland Dreierba8e9312005-10-18 14:14:56 -0700603 if (!ibdev_is_alive(dev))
604 return -ENODEV;
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ret = ib_query_device(dev, &attr);
607 if (ret)
608 return ret;
609
610 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700611 be16_to_cpu(((__be16 *) &attr.sys_image_guid)[0]),
612 be16_to_cpu(((__be16 *) &attr.sys_image_guid)[1]),
613 be16_to_cpu(((__be16 *) &attr.sys_image_guid)[2]),
614 be16_to_cpu(((__be16 *) &attr.sys_image_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617static ssize_t show_node_guid(struct class_device *cdev, char *buf)
618{
619 struct ib_device *dev = container_of(cdev, struct ib_device, class_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Roland Dreierba8e9312005-10-18 14:14:56 -0700621 if (!ibdev_is_alive(dev))
622 return -ENODEV;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
Sean Heftycf311cd2006-01-10 07:39:34 -0800625 be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
626 be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
627 be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
628 be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
631static CLASS_DEVICE_ATTR(node_type, S_IRUGO, show_node_type, NULL);
632static CLASS_DEVICE_ATTR(sys_image_guid, S_IRUGO, show_sys_image_guid, NULL);
633static CLASS_DEVICE_ATTR(node_guid, S_IRUGO, show_node_guid, NULL);
634
635static struct class_device_attribute *ib_class_attributes[] = {
636 &class_device_attr_node_type,
637 &class_device_attr_sys_image_guid,
638 &class_device_attr_node_guid
639};
640
641static struct class ib_class = {
642 .name = "infiniband",
643 .release = ib_device_release,
Kay Sievers312c0042005-11-16 09:00:00 +0100644 .uevent = ib_device_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645};
646
647int ib_device_register_sysfs(struct ib_device *device)
648{
649 struct class_device *class_dev = &device->class_dev;
650 int ret;
651 int i;
652
653 class_dev->class = &ib_class;
654 class_dev->class_data = device;
655 strlcpy(class_dev->class_id, device->name, BUS_ID_SIZE);
656
657 INIT_LIST_HEAD(&device->port_list);
658
659 ret = class_device_register(class_dev);
660 if (ret)
661 goto err;
662
663 for (i = 0; i < ARRAY_SIZE(ib_class_attributes); ++i) {
664 ret = class_device_create_file(class_dev, ib_class_attributes[i]);
665 if (ret)
666 goto err_unregister;
667 }
668
669 device->ports_parent.parent = kobject_get(&class_dev->kobj);
670 if (!device->ports_parent.parent) {
671 ret = -EBUSY;
672 goto err_unregister;
673 }
674 ret = kobject_set_name(&device->ports_parent, "ports");
675 if (ret)
676 goto err_put;
677 ret = kobject_register(&device->ports_parent);
678 if (ret)
679 goto err_put;
680
681 if (device->node_type == IB_NODE_SWITCH) {
682 ret = add_port(device, 0);
683 if (ret)
684 goto err_put;
685 } else {
686 int i;
687
688 for (i = 1; i <= device->phys_port_cnt; ++i) {
689 ret = add_port(device, i);
690 if (ret)
691 goto err_put;
692 }
693 }
694
695 return 0;
696
697err_put:
698 {
699 struct kobject *p, *t;
700 struct ib_port *port;
701
702 list_for_each_entry_safe(p, t, &device->port_list, entry) {
703 list_del(&p->entry);
704 port = container_of(p, struct ib_port, kobj);
705 sysfs_remove_group(p, &pma_group);
706 sysfs_remove_group(p, &port->pkey_group);
707 sysfs_remove_group(p, &port->gid_group);
708 kobject_unregister(p);
709 }
710 }
711
712 kobject_put(&class_dev->kobj);
713
714err_unregister:
715 class_device_unregister(class_dev);
716
717err:
718 return ret;
719}
720
721void ib_device_unregister_sysfs(struct ib_device *device)
722{
723 struct kobject *p, *t;
724 struct ib_port *port;
725
726 list_for_each_entry_safe(p, t, &device->port_list, entry) {
727 list_del(&p->entry);
728 port = container_of(p, struct ib_port, kobj);
729 sysfs_remove_group(p, &pma_group);
730 sysfs_remove_group(p, &port->pkey_group);
731 sysfs_remove_group(p, &port->gid_group);
732 kobject_unregister(p);
733 }
734
735 kobject_unregister(&device->ports_parent);
736 class_device_unregister(&device->class_dev);
737}
738
739int ib_sysfs_setup(void)
740{
741 return class_register(&ib_class);
742}
743
744void ib_sysfs_cleanup(void)
745{
746 class_unregister(&ib_class);
747}