2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * $Id: sysfs.c 1349 2004-12-16 21:09:43Z roland $
35 #include "core_priv.h"
41 struct ib_device
*ibdev
;
42 struct attribute_group gid_group
;
43 struct attribute
**gid_attr
;
44 struct attribute_group pkey_group
;
45 struct attribute
**pkey_attr
;
49 struct 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
);
56 #define PORT_ATTR(_name, _mode, _show, _store) \
57 struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
59 #define PORT_ATTR_RO(_name) \
60 struct port_attribute port_attr_##_name = __ATTR_RO(_name)
62 struct port_table_attribute
{
63 struct port_attribute attr
;
67 static ssize_t
port_attr_show(struct kobject
*kobj
,
68 struct attribute
*attr
, char *buf
)
70 struct port_attribute
*port_attr
=
71 container_of(attr
, struct port_attribute
, attr
);
72 struct ib_port
*p
= container_of(kobj
, struct ib_port
, kobj
);
77 return port_attr
->show(p
, port_attr
, buf
);
80 static struct sysfs_ops port_sysfs_ops
= {
81 .show
= port_attr_show
84 static ssize_t
state_show(struct ib_port
*p
, struct port_attribute
*unused
,
87 struct ib_port_attr attr
;
90 static const char *state_name
[] = {
91 [IB_PORT_NOP
] = "NOP",
92 [IB_PORT_DOWN
] = "DOWN",
93 [IB_PORT_INIT
] = "INIT",
94 [IB_PORT_ARMED
] = "ARMED",
95 [IB_PORT_ACTIVE
] = "ACTIVE",
96 [IB_PORT_ACTIVE_DEFER
] = "ACTIVE_DEFER"
99 ret
= ib_query_port(p
->ibdev
, p
->port_num
, &attr
);
103 return sprintf(buf
, "%d: %s\n", attr
.state
,
104 attr
.state
>= 0 && attr
.state
<= ARRAY_SIZE(state_name
) ?
105 state_name
[attr
.state
] : "UNKNOWN");
108 static ssize_t
lid_show(struct ib_port
*p
, struct port_attribute
*unused
,
111 struct ib_port_attr attr
;
114 ret
= ib_query_port(p
->ibdev
, p
->port_num
, &attr
);
118 return sprintf(buf
, "0x%x\n", attr
.lid
);
121 static ssize_t
lid_mask_count_show(struct ib_port
*p
,
122 struct port_attribute
*unused
,
125 struct ib_port_attr attr
;
128 ret
= ib_query_port(p
->ibdev
, p
->port_num
, &attr
);
132 return sprintf(buf
, "%d\n", attr
.lmc
);
135 static ssize_t
sm_lid_show(struct ib_port
*p
, struct port_attribute
*unused
,
138 struct ib_port_attr attr
;
141 ret
= ib_query_port(p
->ibdev
, p
->port_num
, &attr
);
145 return sprintf(buf
, "0x%x\n", attr
.sm_lid
);
148 static ssize_t
sm_sl_show(struct ib_port
*p
, struct port_attribute
*unused
,
151 struct ib_port_attr attr
;
154 ret
= ib_query_port(p
->ibdev
, p
->port_num
, &attr
);
158 return sprintf(buf
, "%d\n", attr
.sm_sl
);
161 static ssize_t
cap_mask_show(struct ib_port
*p
, struct port_attribute
*unused
,
164 struct ib_port_attr attr
;
167 ret
= ib_query_port(p
->ibdev
, p
->port_num
, &attr
);
171 return sprintf(buf
, "0x%08x\n", attr
.port_cap_flags
);
174 static ssize_t
rate_show(struct ib_port
*p
, struct port_attribute
*unused
,
177 struct ib_port_attr attr
;
182 ret
= ib_query_port(p
->ibdev
, p
->port_num
, &attr
);
186 switch (attr
.active_speed
) {
187 case 2: speed
= " DDR"; break;
188 case 4: speed
= " QDR"; break;
191 rate
= 25 * ib_width_enum_to_int(attr
.active_width
) * attr
.active_speed
;
195 return sprintf(buf
, "%d%s Gb/sec (%dX%s)\n",
196 rate
/ 10, rate
% 10 ? ".5" : "",
197 ib_width_enum_to_int(attr
.active_width
), speed
);
200 static ssize_t
phys_state_show(struct ib_port
*p
, struct port_attribute
*unused
,
203 struct ib_port_attr attr
;
207 ret
= ib_query_port(p
->ibdev
, p
->port_num
, &attr
);
211 switch (attr
.phys_state
) {
212 case 1: return sprintf(buf
, "1: Sleep\n");
213 case 2: return sprintf(buf
, "2: Polling\n");
214 case 3: return sprintf(buf
, "3: Disabled\n");
215 case 4: return sprintf(buf
, "4: PortConfigurationTraining\n");
216 case 5: return sprintf(buf
, "5: LinkUp\n");
217 case 6: return sprintf(buf
, "6: LinkErrorRecovery\n");
218 case 7: return sprintf(buf
, "7: Phy Test\n");
219 default: return sprintf(buf
, "%d: <unknown>\n", attr
.phys_state
);
223 static PORT_ATTR_RO(state
);
224 static PORT_ATTR_RO(lid
);
225 static PORT_ATTR_RO(lid_mask_count
);
226 static PORT_ATTR_RO(sm_lid
);
227 static PORT_ATTR_RO(sm_sl
);
228 static PORT_ATTR_RO(cap_mask
);
229 static PORT_ATTR_RO(rate
);
230 static PORT_ATTR_RO(phys_state
);
232 static struct attribute
*port_default_attrs
[] = {
233 &port_attr_state
.attr
,
235 &port_attr_lid_mask_count
.attr
,
236 &port_attr_sm_lid
.attr
,
237 &port_attr_sm_sl
.attr
,
238 &port_attr_cap_mask
.attr
,
239 &port_attr_rate
.attr
,
240 &port_attr_phys_state
.attr
,
244 static ssize_t
show_port_gid(struct ib_port
*p
, struct port_attribute
*attr
,
247 struct port_table_attribute
*tab_attr
=
248 container_of(attr
, struct port_table_attribute
, attr
);
252 ret
= ib_query_gid(p
->ibdev
, p
->port_num
, tab_attr
->index
, &gid
);
256 return sprintf(buf
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
257 be16_to_cpu(((u16
*) gid
.raw
)[0]),
258 be16_to_cpu(((u16
*) gid
.raw
)[1]),
259 be16_to_cpu(((u16
*) gid
.raw
)[2]),
260 be16_to_cpu(((u16
*) gid
.raw
)[3]),
261 be16_to_cpu(((u16
*) gid
.raw
)[4]),
262 be16_to_cpu(((u16
*) gid
.raw
)[5]),
263 be16_to_cpu(((u16
*) gid
.raw
)[6]),
264 be16_to_cpu(((u16
*) gid
.raw
)[7]));
267 static ssize_t
show_port_pkey(struct ib_port
*p
, struct port_attribute
*attr
,
270 struct port_table_attribute
*tab_attr
=
271 container_of(attr
, struct port_table_attribute
, attr
);
275 ret
= ib_query_pkey(p
->ibdev
, p
->port_num
, tab_attr
->index
, &pkey
);
279 return sprintf(buf
, "0x%04x\n", pkey
);
282 #define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
283 struct port_table_attribute port_pma_attr_##_name = { \
284 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
285 .index = (_offset) | ((_width) << 16) | ((_counter) << 24) \
288 static ssize_t
show_pma_counter(struct ib_port
*p
, struct port_attribute
*attr
,
291 struct port_table_attribute
*tab_attr
=
292 container_of(attr
, struct port_table_attribute
, attr
);
293 int offset
= tab_attr
->index
& 0xffff;
294 int width
= (tab_attr
->index
>> 16) & 0xff;
295 struct ib_mad
*in_mad
= NULL
;
296 struct ib_mad
*out_mad
= NULL
;
299 if (!p
->ibdev
->process_mad
)
300 return sprintf(buf
, "N/A (no PMA)\n");
302 in_mad
= kmalloc(sizeof *in_mad
, GFP_KERNEL
);
303 out_mad
= kmalloc(sizeof *in_mad
, GFP_KERNEL
);
304 if (!in_mad
|| !out_mad
) {
309 memset(in_mad
, 0, sizeof *in_mad
);
310 in_mad
->mad_hdr
.base_version
= 1;
311 in_mad
->mad_hdr
.mgmt_class
= IB_MGMT_CLASS_PERF_MGMT
;
312 in_mad
->mad_hdr
.class_version
= 1;
313 in_mad
->mad_hdr
.method
= IB_MGMT_METHOD_GET
;
314 in_mad
->mad_hdr
.attr_id
= cpu_to_be16(0x12); /* PortCounters */
316 in_mad
->data
[41] = p
->port_num
; /* PortSelect field */
318 if ((p
->ibdev
->process_mad(p
->ibdev
, IB_MAD_IGNORE_MKEY
,
319 p
->port_num
, NULL
, NULL
, in_mad
, out_mad
) &
320 (IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
)) !=
321 (IB_MAD_RESULT_SUCCESS
| IB_MAD_RESULT_REPLY
)) {
328 ret
= sprintf(buf
, "%u\n", (out_mad
->data
[40 + offset
/ 8] >>
329 (offset
% 4)) & 0xf);
332 ret
= sprintf(buf
, "%u\n", out_mad
->data
[40 + offset
/ 8]);
335 ret
= sprintf(buf
, "%u\n",
336 be16_to_cpup((u16
*)(out_mad
->data
+ 40 + offset
/ 8)));
339 ret
= sprintf(buf
, "%u\n",
340 be32_to_cpup((u32
*)(out_mad
->data
+ 40 + offset
/ 8)));
353 static PORT_PMA_ATTR(symbol_error
, 0, 16, 32);
354 static PORT_PMA_ATTR(link_error_recovery
, 1, 8, 48);
355 static PORT_PMA_ATTR(link_downed
, 2, 8, 56);
356 static PORT_PMA_ATTR(port_rcv_errors
, 3, 16, 64);
357 static PORT_PMA_ATTR(port_rcv_remote_physical_errors
, 4, 16, 80);
358 static PORT_PMA_ATTR(port_rcv_switch_relay_errors
, 5, 16, 96);
359 static PORT_PMA_ATTR(port_xmit_discards
, 6, 16, 112);
360 static PORT_PMA_ATTR(port_xmit_constraint_errors
, 7, 8, 128);
361 static PORT_PMA_ATTR(port_rcv_constraint_errors
, 8, 8, 136);
362 static PORT_PMA_ATTR(local_link_integrity_errors
, 9, 4, 152);
363 static PORT_PMA_ATTR(excessive_buffer_overrun_errors
, 10, 4, 156);
364 static PORT_PMA_ATTR(VL15_dropped
, 11, 16, 176);
365 static PORT_PMA_ATTR(port_xmit_data
, 12, 32, 192);
366 static PORT_PMA_ATTR(port_rcv_data
, 13, 32, 224);
367 static PORT_PMA_ATTR(port_xmit_packets
, 14, 32, 256);
368 static PORT_PMA_ATTR(port_rcv_packets
, 15, 32, 288);
370 static struct attribute
*pma_attrs
[] = {
371 &port_pma_attr_symbol_error
.attr
.attr
,
372 &port_pma_attr_link_error_recovery
.attr
.attr
,
373 &port_pma_attr_link_downed
.attr
.attr
,
374 &port_pma_attr_port_rcv_errors
.attr
.attr
,
375 &port_pma_attr_port_rcv_remote_physical_errors
.attr
.attr
,
376 &port_pma_attr_port_rcv_switch_relay_errors
.attr
.attr
,
377 &port_pma_attr_port_xmit_discards
.attr
.attr
,
378 &port_pma_attr_port_xmit_constraint_errors
.attr
.attr
,
379 &port_pma_attr_port_rcv_constraint_errors
.attr
.attr
,
380 &port_pma_attr_local_link_integrity_errors
.attr
.attr
,
381 &port_pma_attr_excessive_buffer_overrun_errors
.attr
.attr
,
382 &port_pma_attr_VL15_dropped
.attr
.attr
,
383 &port_pma_attr_port_xmit_data
.attr
.attr
,
384 &port_pma_attr_port_rcv_data
.attr
.attr
,
385 &port_pma_attr_port_xmit_packets
.attr
.attr
,
386 &port_pma_attr_port_rcv_packets
.attr
.attr
,
390 static struct attribute_group pma_group
= {
395 static void ib_port_release(struct kobject
*kobj
)
397 struct ib_port
*p
= container_of(kobj
, struct ib_port
, kobj
);
401 for (i
= 0; (a
= p
->gid_attr
[i
]); ++i
) {
406 for (i
= 0; (a
= p
->pkey_attr
[i
]); ++i
) {
415 static struct kobj_type port_type
= {
416 .release
= ib_port_release
,
417 .sysfs_ops
= &port_sysfs_ops
,
418 .default_attrs
= port_default_attrs
421 static void ib_device_release(struct class_device
*cdev
)
423 struct ib_device
*dev
= container_of(cdev
, struct ib_device
, class_dev
);
428 static int ib_device_hotplug(struct class_device
*cdev
, char **envp
,
429 int num_envp
, char *buf
, int size
)
431 struct ib_device
*dev
= container_of(cdev
, struct ib_device
, class_dev
);
434 if (add_hotplug_env_var(envp
, num_envp
, &i
, buf
, size
, &len
,
435 "NAME=%s", dev
->name
))
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.
452 static int alloc_group(struct attribute
***attr
,
453 ssize_t (*show
)(struct ib_port
*,
454 struct port_attribute
*, char *buf
),
457 struct port_table_attribute
***tab_attr
=
458 (struct port_table_attribute
***) attr
;
462 *tab_attr
= kmalloc((1 + len
) * sizeof *tab_attr
, GFP_KERNEL
);
466 memset(*tab_attr
, 0, (1 + len
) * sizeof *tab_attr
);
468 for (i
= 0; i
< len
; ++i
) {
469 (*tab_attr
)[i
] = kmalloc(sizeof *(*tab_attr
)[i
], GFP_KERNEL
);
470 if (!(*tab_attr
)[i
]) {
474 memset((*tab_attr
)[i
], 0, sizeof *(*tab_attr
)[i
]);
475 (*tab_attr
)[i
]->attr
.attr
.name
= kmalloc(8, GFP_KERNEL
);
476 if (!(*tab_attr
)[i
]->attr
.attr
.name
) {
481 if (snprintf((*tab_attr
)[i
]->attr
.attr
.name
, 8, "%d", i
) >= 8) {
486 (*tab_attr
)[i
]->attr
.attr
.mode
= S_IRUGO
;
487 (*tab_attr
)[i
]->attr
.attr
.owner
= THIS_MODULE
;
488 (*tab_attr
)[i
]->attr
.show
= show
;
489 (*tab_attr
)[i
]->index
= i
;
495 for (i
= 0; i
< len
; ++i
) {
497 kfree((*tab_attr
)[i
]->attr
.attr
.name
);
498 kfree((*tab_attr
)[i
]);
506 static int add_port(struct ib_device
*device
, int port_num
)
509 struct ib_port_attr attr
;
513 ret
= ib_query_port(device
, port_num
, &attr
);
517 p
= kmalloc(sizeof *p
, GFP_KERNEL
);
520 memset(p
, 0, sizeof *p
);
523 p
->port_num
= port_num
;
524 p
->kobj
.ktype
= &port_type
;
526 p
->kobj
.parent
= kobject_get(&device
->ports_parent
);
527 if (!p
->kobj
.parent
) {
532 ret
= kobject_set_name(&p
->kobj
, "%d", port_num
);
536 ret
= kobject_register(&p
->kobj
);
540 ret
= sysfs_create_group(&p
->kobj
, &pma_group
);
544 ret
= alloc_group(&p
->gid_attr
, show_port_gid
, attr
.gid_tbl_len
);
548 p
->gid_group
.name
= "gids";
549 p
->gid_group
.attrs
= p
->gid_attr
;
551 ret
= sysfs_create_group(&p
->kobj
, &p
->gid_group
);
555 ret
= alloc_group(&p
->pkey_attr
, show_port_pkey
, attr
.pkey_tbl_len
);
559 p
->pkey_group
.name
= "pkeys";
560 p
->pkey_group
.attrs
= p
->pkey_attr
;
562 ret
= sysfs_create_group(&p
->kobj
, &p
->pkey_group
);
566 list_add_tail(&p
->kobj
.entry
, &device
->port_list
);
571 for (i
= 0; i
< attr
.pkey_tbl_len
; ++i
) {
572 kfree(p
->pkey_attr
[i
]->name
);
573 kfree(p
->pkey_attr
[i
]);
579 sysfs_remove_group(&p
->kobj
, &p
->gid_group
);
582 for (i
= 0; i
< attr
.gid_tbl_len
; ++i
) {
583 kfree(p
->gid_attr
[i
]->name
);
584 kfree(p
->gid_attr
[i
]);
590 sysfs_remove_group(&p
->kobj
, &pma_group
);
593 kobject_put(&device
->ports_parent
);
600 static ssize_t
show_node_type(struct class_device
*cdev
, char *buf
)
602 struct ib_device
*dev
= container_of(cdev
, struct ib_device
, class_dev
);
604 switch (dev
->node_type
) {
605 case IB_NODE_CA
: return sprintf(buf
, "%d: CA\n", dev
->node_type
);
606 case IB_NODE_SWITCH
: return sprintf(buf
, "%d: switch\n", dev
->node_type
);
607 case IB_NODE_ROUTER
: return sprintf(buf
, "%d: router\n", dev
->node_type
);
608 default: return sprintf(buf
, "%d: <unknown>\n", dev
->node_type
);
612 static ssize_t
show_sys_image_guid(struct class_device
*cdev
, char *buf
)
614 struct ib_device
*dev
= container_of(cdev
, struct ib_device
, class_dev
);
615 struct ib_device_attr attr
;
618 ret
= ib_query_device(dev
, &attr
);
622 return sprintf(buf
, "%04x:%04x:%04x:%04x\n",
623 be16_to_cpu(((u16
*) &attr
.sys_image_guid
)[0]),
624 be16_to_cpu(((u16
*) &attr
.sys_image_guid
)[1]),
625 be16_to_cpu(((u16
*) &attr
.sys_image_guid
)[2]),
626 be16_to_cpu(((u16
*) &attr
.sys_image_guid
)[3]));
629 static ssize_t
show_node_guid(struct class_device
*cdev
, char *buf
)
631 struct ib_device
*dev
= container_of(cdev
, struct ib_device
, class_dev
);
632 struct ib_device_attr attr
;
635 ret
= ib_query_device(dev
, &attr
);
639 return sprintf(buf
, "%04x:%04x:%04x:%04x\n",
640 be16_to_cpu(((u16
*) &attr
.node_guid
)[0]),
641 be16_to_cpu(((u16
*) &attr
.node_guid
)[1]),
642 be16_to_cpu(((u16
*) &attr
.node_guid
)[2]),
643 be16_to_cpu(((u16
*) &attr
.node_guid
)[3]));
646 static CLASS_DEVICE_ATTR(node_type
, S_IRUGO
, show_node_type
, NULL
);
647 static CLASS_DEVICE_ATTR(sys_image_guid
, S_IRUGO
, show_sys_image_guid
, NULL
);
648 static CLASS_DEVICE_ATTR(node_guid
, S_IRUGO
, show_node_guid
, NULL
);
650 static struct class_device_attribute
*ib_class_attributes
[] = {
651 &class_device_attr_node_type
,
652 &class_device_attr_sys_image_guid
,
653 &class_device_attr_node_guid
656 static struct class ib_class
= {
657 .name
= "infiniband",
658 .release
= ib_device_release
,
659 .hotplug
= ib_device_hotplug
,
662 int ib_device_register_sysfs(struct ib_device
*device
)
664 struct class_device
*class_dev
= &device
->class_dev
;
668 class_dev
->class = &ib_class
;
669 class_dev
->class_data
= device
;
670 strlcpy(class_dev
->class_id
, device
->name
, BUS_ID_SIZE
);
672 INIT_LIST_HEAD(&device
->port_list
);
674 ret
= class_device_register(class_dev
);
678 for (i
= 0; i
< ARRAY_SIZE(ib_class_attributes
); ++i
) {
679 ret
= class_device_create_file(class_dev
, ib_class_attributes
[i
]);
684 device
->ports_parent
.parent
= kobject_get(&class_dev
->kobj
);
685 if (!device
->ports_parent
.parent
) {
689 ret
= kobject_set_name(&device
->ports_parent
, "ports");
692 ret
= kobject_register(&device
->ports_parent
);
696 if (device
->node_type
== IB_NODE_SWITCH
) {
697 ret
= add_port(device
, 0);
703 for (i
= 1; i
<= device
->phys_port_cnt
; ++i
) {
704 ret
= add_port(device
, i
);
714 struct kobject
*p
, *t
;
715 struct ib_port
*port
;
717 list_for_each_entry_safe(p
, t
, &device
->port_list
, entry
) {
719 port
= container_of(p
, struct ib_port
, kobj
);
720 sysfs_remove_group(p
, &pma_group
);
721 sysfs_remove_group(p
, &port
->pkey_group
);
722 sysfs_remove_group(p
, &port
->gid_group
);
723 kobject_unregister(p
);
727 kobject_put(&class_dev
->kobj
);
730 class_device_unregister(class_dev
);
736 void ib_device_unregister_sysfs(struct ib_device
*device
)
738 struct kobject
*p
, *t
;
739 struct ib_port
*port
;
741 list_for_each_entry_safe(p
, t
, &device
->port_list
, entry
) {
743 port
= container_of(p
, struct ib_port
, kobj
);
744 sysfs_remove_group(p
, &pma_group
);
745 sysfs_remove_group(p
, &port
->pkey_group
);
746 sysfs_remove_group(p
, &port
->gid_group
);
747 kobject_unregister(p
);
750 kobject_unregister(&device
->ports_parent
);
751 class_device_unregister(&device
->class_dev
);
754 int ib_sysfs_setup(void)
756 return class_register(&ib_class
);
759 void ib_sysfs_cleanup(void)
761 class_unregister(&ib_class
);