]> git.proxmox.com Git - mirror_ubuntu-kernels.git/blame - drivers/infiniband/core/sysfs.c
Merge branches 'for-5.1/upstream-fixes', 'for-5.2/core', 'for-5.2/ish', 'for-5.2...
[mirror_ubuntu-kernels.git] / drivers / infiniband / core / sysfs.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
2a1d9b7f
RD
3 * Copyright (c) 2005 Mellanox Technologies Ltd. All rights reserved.
4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
1da177e4
LT
5 *
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.
1da177e4
LT
33 */
34
35#include "core_priv.h"
36
8c65b4a6 37#include <linux/slab.h>
fc87af74 38#include <linux/stat.h>
8c65b4a6 39#include <linux/string.h>
470be516 40#include <linux/netdevice.h>
41a6ae1e 41#include <linux/ethtool.h>
8c65b4a6 42
a4d61e84 43#include <rdma/ib_mad.h>
b2788ce5 44#include <rdma/ib_pma.h>
77e786fc 45#include <rdma/ib_cache.h>
1da177e4 46
470be516
MB
47struct ib_port;
48
49struct gid_attr_group {
50 struct ib_port *port;
51 struct kobject kobj;
52 struct attribute_group ndev;
53 struct attribute_group type;
54};
1da177e4
LT
55struct ib_port {
56 struct kobject kobj;
57 struct ib_device *ibdev;
470be516 58 struct gid_attr_group *gid_attr_group;
1da177e4 59 struct attribute_group gid_group;
1da177e4 60 struct attribute_group pkey_group;
145d9c54 61 struct attribute_group *pma_table;
b40f4757
CL
62 struct attribute_group *hw_stats_ag;
63 struct rdma_hw_stats *hw_stats;
64 u8 port_num;
1da177e4
LT
65};
66
67struct port_attribute {
68 struct attribute attr;
69 ssize_t (*show)(struct ib_port *, struct port_attribute *, char *buf);
70 ssize_t (*store)(struct ib_port *, struct port_attribute *,
71 const char *buf, size_t count);
72};
73
74#define PORT_ATTR(_name, _mode, _show, _store) \
75struct port_attribute port_attr_##_name = __ATTR(_name, _mode, _show, _store)
76
77#define PORT_ATTR_RO(_name) \
78struct port_attribute port_attr_##_name = __ATTR_RO(_name)
79
80struct port_table_attribute {
d48593bf
DT
81 struct port_attribute attr;
82 char name[8];
83 int index;
65487fdc 84 __be16 attr_id;
1da177e4
LT
85};
86
b40f4757
CL
87struct hw_stats_attribute {
88 struct attribute attr;
89 ssize_t (*show)(struct kobject *kobj,
90 struct attribute *attr, char *buf);
91 ssize_t (*store)(struct kobject *kobj,
92 struct attribute *attr,
93 const char *buf,
94 size_t count);
95 int index;
96 u8 port_num;
97};
98
1da177e4
LT
99static ssize_t port_attr_show(struct kobject *kobj,
100 struct attribute *attr, char *buf)
101{
102 struct port_attribute *port_attr =
103 container_of(attr, struct port_attribute, attr);
104 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
105
106 if (!port_attr->show)
70f2817a 107 return -EIO;
1da177e4
LT
108
109 return port_attr->show(p, port_attr, buf);
110}
111
79c4d80b
PP
112static ssize_t port_attr_store(struct kobject *kobj,
113 struct attribute *attr,
114 const char *buf, size_t count)
115{
116 struct port_attribute *port_attr =
117 container_of(attr, struct port_attribute, attr);
118 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
119
120 if (!port_attr->store)
121 return -EIO;
122 return port_attr->store(p, port_attr, buf, count);
123}
124
52cf25d0 125static const struct sysfs_ops port_sysfs_ops = {
79c4d80b
PP
126 .show = port_attr_show,
127 .store = port_attr_store
1da177e4
LT
128};
129
470be516
MB
130static ssize_t gid_attr_show(struct kobject *kobj,
131 struct attribute *attr, char *buf)
132{
133 struct port_attribute *port_attr =
134 container_of(attr, struct port_attribute, attr);
135 struct ib_port *p = container_of(kobj, struct gid_attr_group,
136 kobj)->port;
137
138 if (!port_attr->show)
139 return -EIO;
140
141 return port_attr->show(p, port_attr, buf);
142}
143
144static const struct sysfs_ops gid_attr_sysfs_ops = {
145 .show = gid_attr_show
146};
147
1da177e4
LT
148static ssize_t state_show(struct ib_port *p, struct port_attribute *unused,
149 char *buf)
150{
151 struct ib_port_attr attr;
152 ssize_t ret;
153
154 static const char *state_name[] = {
155 [IB_PORT_NOP] = "NOP",
156 [IB_PORT_DOWN] = "DOWN",
157 [IB_PORT_INIT] = "INIT",
158 [IB_PORT_ARMED] = "ARMED",
159 [IB_PORT_ACTIVE] = "ACTIVE",
160 [IB_PORT_ACTIVE_DEFER] = "ACTIVE_DEFER"
161 };
162
163 ret = ib_query_port(p->ibdev, p->port_num, &attr);
164 if (ret)
165 return ret;
166
167 return sprintf(buf, "%d: %s\n", attr.state,
048975ac 168 attr.state >= 0 && attr.state < ARRAY_SIZE(state_name) ?
1da177e4
LT
169 state_name[attr.state] : "UNKNOWN");
170}
171
172static ssize_t lid_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%x\n", attr.lid);
183}
184
185static ssize_t lid_mask_count_show(struct ib_port *p,
186 struct port_attribute *unused,
187 char *buf)
188{
189 struct ib_port_attr attr;
190 ssize_t ret;
191
192 ret = ib_query_port(p->ibdev, p->port_num, &attr);
193 if (ret)
194 return ret;
195
196 return sprintf(buf, "%d\n", attr.lmc);
197}
198
199static ssize_t sm_lid_show(struct ib_port *p, struct port_attribute *unused,
200 char *buf)
201{
202 struct ib_port_attr attr;
203 ssize_t ret;
204
205 ret = ib_query_port(p->ibdev, p->port_num, &attr);
206 if (ret)
207 return ret;
208
209 return sprintf(buf, "0x%x\n", attr.sm_lid);
210}
211
212static ssize_t sm_sl_show(struct ib_port *p, struct port_attribute *unused,
213 char *buf)
214{
215 struct ib_port_attr attr;
216 ssize_t ret;
217
218 ret = ib_query_port(p->ibdev, p->port_num, &attr);
219 if (ret)
220 return ret;
221
222 return sprintf(buf, "%d\n", attr.sm_sl);
223}
224
225static ssize_t cap_mask_show(struct ib_port *p, struct port_attribute *unused,
226 char *buf)
227{
228 struct ib_port_attr attr;
229 ssize_t ret;
230
231 ret = ib_query_port(p->ibdev, p->port_num, &attr);
232 if (ret)
233 return ret;
234
235 return sprintf(buf, "0x%08x\n", attr.port_cap_flags);
236}
237
238static ssize_t rate_show(struct ib_port *p, struct port_attribute *unused,
239 char *buf)
240{
241 struct ib_port_attr attr;
242 char *speed = "";
0559d8dc 243 int rate; /* in deci-Gb/sec */
1da177e4
LT
244 ssize_t ret;
245
246 ret = ib_query_port(p->ibdev, p->port_num, &attr);
247 if (ret)
248 return ret;
249
250 switch (attr.active_speed) {
2e96691c 251 case IB_SPEED_DDR:
71eeba16 252 speed = " DDR";
e9319b0c 253 rate = 50;
71eeba16 254 break;
2e96691c 255 case IB_SPEED_QDR:
71eeba16 256 speed = " QDR";
e9319b0c 257 rate = 100;
71eeba16 258 break;
2e96691c 259 case IB_SPEED_FDR10:
71eeba16 260 speed = " FDR10";
e9319b0c 261 rate = 100;
71eeba16 262 break;
2e96691c 263 case IB_SPEED_FDR:
71eeba16 264 speed = " FDR";
e9319b0c 265 rate = 140;
71eeba16 266 break;
2e96691c 267 case IB_SPEED_EDR:
71eeba16 268 speed = " EDR";
e9319b0c 269 rate = 250;
71eeba16 270 break;
12113a35
NO
271 case IB_SPEED_HDR:
272 speed = " HDR";
273 rate = 500;
274 break;
0559d8dc
RD
275 case IB_SPEED_SDR:
276 default: /* default to SDR for invalid rates */
311d0da9 277 speed = " SDR";
0559d8dc
RD
278 rate = 25;
279 break;
1da177e4
LT
280 }
281
71eeba16 282 rate *= ib_width_enum_to_int(attr.active_width);
1da177e4
LT
283 if (rate < 0)
284 return -EINVAL;
285
286 return sprintf(buf, "%d%s Gb/sec (%dX%s)\n",
e9319b0c 287 rate / 10, rate % 10 ? ".5" : "",
1da177e4
LT
288 ib_width_enum_to_int(attr.active_width), speed);
289}
290
291static ssize_t phys_state_show(struct ib_port *p, struct port_attribute *unused,
292 char *buf)
293{
294 struct ib_port_attr attr;
295
296 ssize_t ret;
297
298 ret = ib_query_port(p->ibdev, p->port_num, &attr);
299 if (ret)
300 return ret;
301
302 switch (attr.phys_state) {
303 case 1: return sprintf(buf, "1: Sleep\n");
304 case 2: return sprintf(buf, "2: Polling\n");
305 case 3: return sprintf(buf, "3: Disabled\n");
306 case 4: return sprintf(buf, "4: PortConfigurationTraining\n");
307 case 5: return sprintf(buf, "5: LinkUp\n");
308 case 6: return sprintf(buf, "6: LinkErrorRecovery\n");
309 case 7: return sprintf(buf, "7: Phy Test\n");
310 default: return sprintf(buf, "%d: <unknown>\n", attr.phys_state);
311 }
312}
313
8ad330a0
EC
314static ssize_t link_layer_show(struct ib_port *p, struct port_attribute *unused,
315 char *buf)
316{
317 switch (rdma_port_get_link_layer(p->ibdev, p->port_num)) {
318 case IB_LINK_LAYER_INFINIBAND:
319 return sprintf(buf, "%s\n", "InfiniBand");
320 case IB_LINK_LAYER_ETHERNET:
321 return sprintf(buf, "%s\n", "Ethernet");
322 default:
323 return sprintf(buf, "%s\n", "Unknown");
324 }
325}
326
1da177e4
LT
327static PORT_ATTR_RO(state);
328static PORT_ATTR_RO(lid);
329static PORT_ATTR_RO(lid_mask_count);
330static PORT_ATTR_RO(sm_lid);
331static PORT_ATTR_RO(sm_sl);
332static PORT_ATTR_RO(cap_mask);
333static PORT_ATTR_RO(rate);
334static PORT_ATTR_RO(phys_state);
8ad330a0 335static PORT_ATTR_RO(link_layer);
1da177e4
LT
336
337static struct attribute *port_default_attrs[] = {
338 &port_attr_state.attr,
339 &port_attr_lid.attr,
340 &port_attr_lid_mask_count.attr,
341 &port_attr_sm_lid.attr,
342 &port_attr_sm_sl.attr,
343 &port_attr_cap_mask.attr,
344 &port_attr_rate.attr,
345 &port_attr_phys_state.attr,
8ad330a0 346 &port_attr_link_layer.attr,
1da177e4
LT
347 NULL
348};
349
77e786fc 350static size_t print_ndev(const struct ib_gid_attr *gid_attr, char *buf)
470be516
MB
351{
352 if (!gid_attr->ndev)
353 return -EINVAL;
354
355 return sprintf(buf, "%s\n", gid_attr->ndev->name);
356}
357
77e786fc 358static size_t print_gid_type(const struct ib_gid_attr *gid_attr, char *buf)
470be516
MB
359{
360 return sprintf(buf, "%s\n", ib_cache_gid_type_str(gid_attr->gid_type));
361}
362
77e786fc
PP
363static ssize_t _show_port_gid_attr(
364 struct ib_port *p, struct port_attribute *attr, char *buf,
365 size_t (*print)(const struct ib_gid_attr *gid_attr, char *buf))
470be516
MB
366{
367 struct port_table_attribute *tab_attr =
368 container_of(attr, struct port_table_attribute, attr);
77e786fc 369 const struct ib_gid_attr *gid_attr;
470be516 370 ssize_t ret;
470be516 371
77e786fc
PP
372 gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
373 if (IS_ERR(gid_attr))
374 return PTR_ERR(gid_attr);
470be516 375
77e786fc
PP
376 ret = print(gid_attr, buf);
377 rdma_put_gid_attr(gid_attr);
470be516
MB
378 return ret;
379}
380
1da177e4
LT
381static ssize_t show_port_gid(struct ib_port *p, struct port_attribute *attr,
382 char *buf)
383{
384 struct port_table_attribute *tab_attr =
385 container_of(attr, struct port_table_attribute, attr);
77e786fc 386 const struct ib_gid_attr *gid_attr;
1da177e4
LT
387 ssize_t ret;
388
77e786fc
PP
389 gid_attr = rdma_get_gid_attr(p->ibdev, p->port_num, tab_attr->index);
390 if (IS_ERR(gid_attr)) {
391 const union ib_gid zgid = {};
392
393 /* If reading GID fails, it is likely due to GID entry being
394 * empty (invalid) or reserved GID in the table. User space
395 * expects to read GID table entries as long as it given index
396 * is within GID table size. Administrative/debugging tool
397 * fails to query rest of the GID entries if it hits error
398 * while querying a GID of the given index. To avoid user
399 * space throwing such error on fail to read gid, return zero
400 * GID as before. This maintains backward compatibility.
401 */
402 return sprintf(buf, "%pI6\n", zgid.raw);
403 }
404
405 ret = sprintf(buf, "%pI6\n", gid_attr->gid.raw);
406 rdma_put_gid_attr(gid_attr);
407 return ret;
1da177e4
LT
408}
409
470be516
MB
410static ssize_t show_port_gid_attr_ndev(struct ib_port *p,
411 struct port_attribute *attr, char *buf)
412{
413 return _show_port_gid_attr(p, attr, buf, print_ndev);
414}
415
416static ssize_t show_port_gid_attr_gid_type(struct ib_port *p,
417 struct port_attribute *attr,
418 char *buf)
419{
420 return _show_port_gid_attr(p, attr, buf, print_gid_type);
421}
422
1da177e4
LT
423static ssize_t show_port_pkey(struct ib_port *p, struct port_attribute *attr,
424 char *buf)
425{
426 struct port_table_attribute *tab_attr =
427 container_of(attr, struct port_table_attribute, attr);
428 u16 pkey;
429 ssize_t ret;
430
431 ret = ib_query_pkey(p->ibdev, p->port_num, tab_attr->index, &pkey);
432 if (ret)
433 return ret;
434
435 return sprintf(buf, "0x%04x\n", pkey);
436}
437
438#define PORT_PMA_ATTR(_name, _counter, _width, _offset) \
439struct port_table_attribute port_pma_attr_##_name = { \
440 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
b2788ce5
CL
441 .index = (_offset) | ((_width) << 16) | ((_counter) << 24), \
442 .attr_id = IB_PMA_PORT_COUNTERS , \
1da177e4
LT
443}
444
145d9c54
CL
445#define PORT_PMA_ATTR_EXT(_name, _width, _offset) \
446struct port_table_attribute port_pma_attr_ext_##_name = { \
447 .attr = __ATTR(_name, S_IRUGO, show_pma_counter, NULL), \
448 .index = (_offset) | ((_width) << 16), \
449 .attr_id = IB_PMA_PORT_COUNTERS_EXT , \
450}
451
35c4cbb1
CL
452/*
453 * Get a Perfmgmt MAD block of data.
454 * Returns error code or the number of bytes retrieved.
455 */
65487fdc 456static int get_perf_mad(struct ib_device *dev, int port_num, __be16 attr,
35c4cbb1 457 void *data, int offset, size_t size)
1da177e4 458{
35c4cbb1
CL
459 struct ib_mad *in_mad;
460 struct ib_mad *out_mad;
4cd7c947
IW
461 size_t mad_size = sizeof(*out_mad);
462 u16 out_mad_pkey_index = 0;
1da177e4
LT
463 ssize_t ret;
464
3023a1e9 465 if (!dev->ops.process_mad)
35c4cbb1 466 return -ENOSYS;
1da177e4 467
de6eb66b 468 in_mad = kzalloc(sizeof *in_mad, GFP_KERNEL);
856c52a7 469 out_mad = kmalloc(sizeof *out_mad, GFP_KERNEL);
1da177e4
LT
470 if (!in_mad || !out_mad) {
471 ret = -ENOMEM;
472 goto out;
473 }
474
1da177e4
LT
475 in_mad->mad_hdr.base_version = 1;
476 in_mad->mad_hdr.mgmt_class = IB_MGMT_CLASS_PERF_MGMT;
477 in_mad->mad_hdr.class_version = 1;
478 in_mad->mad_hdr.method = IB_MGMT_METHOD_GET;
35c4cbb1 479 in_mad->mad_hdr.attr_id = attr;
1da177e4 480
6e2a51a0
HR
481 if (attr != IB_PMA_CLASS_PORT_INFO)
482 in_mad->data[41] = port_num; /* PortSelect field */
1da177e4 483
3023a1e9
KH
484 if ((dev->ops.process_mad(dev, IB_MAD_IGNORE_MKEY,
485 port_num, NULL, NULL,
486 (const struct ib_mad_hdr *)in_mad, mad_size,
487 (struct ib_mad_hdr *)out_mad, &mad_size,
488 &out_mad_pkey_index) &
1da177e4
LT
489 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) !=
490 (IB_MAD_RESULT_SUCCESS | IB_MAD_RESULT_REPLY)) {
491 ret = -EINVAL;
492 goto out;
493 }
35c4cbb1
CL
494 memcpy(data, out_mad->data + offset, size);
495 ret = size;
496out:
497 kfree(in_mad);
498 kfree(out_mad);
499 return ret;
500}
501
502static ssize_t show_pma_counter(struct ib_port *p, struct port_attribute *attr,
503 char *buf)
504{
505 struct port_table_attribute *tab_attr =
506 container_of(attr, struct port_table_attribute, attr);
507 int offset = tab_attr->index & 0xffff;
508 int width = (tab_attr->index >> 16) & 0xff;
509 ssize_t ret;
510 u8 data[8];
511
b2788ce5 512 ret = get_perf_mad(p->ibdev, p->port_num, tab_attr->attr_id, &data,
35c4cbb1
CL
513 40 + offset / 8, sizeof(data));
514 if (ret < 0)
0f6ef65d 515 return ret;
1da177e4
LT
516
517 switch (width) {
518 case 4:
35c4cbb1 519 ret = sprintf(buf, "%u\n", (*data >>
d8b9f23b 520 (4 - (offset % 8))) & 0xf);
1da177e4
LT
521 break;
522 case 8:
35c4cbb1 523 ret = sprintf(buf, "%u\n", *data);
1da177e4
LT
524 break;
525 case 16:
526 ret = sprintf(buf, "%u\n",
35c4cbb1 527 be16_to_cpup((__be16 *)data));
1da177e4
LT
528 break;
529 case 32:
530 ret = sprintf(buf, "%u\n",
35c4cbb1 531 be32_to_cpup((__be32 *)data));
1da177e4 532 break;
145d9c54
CL
533 case 64:
534 ret = sprintf(buf, "%llu\n",
535 be64_to_cpup((__be64 *)data));
536 break;
537
1da177e4
LT
538 default:
539 ret = 0;
540 }
541
1da177e4
LT
542 return ret;
543}
544
545static PORT_PMA_ATTR(symbol_error , 0, 16, 32);
546static PORT_PMA_ATTR(link_error_recovery , 1, 8, 48);
547static PORT_PMA_ATTR(link_downed , 2, 8, 56);
548static PORT_PMA_ATTR(port_rcv_errors , 3, 16, 64);
549static PORT_PMA_ATTR(port_rcv_remote_physical_errors, 4, 16, 80);
550static PORT_PMA_ATTR(port_rcv_switch_relay_errors , 5, 16, 96);
551static PORT_PMA_ATTR(port_xmit_discards , 6, 16, 112);
552static PORT_PMA_ATTR(port_xmit_constraint_errors , 7, 8, 128);
553static PORT_PMA_ATTR(port_rcv_constraint_errors , 8, 8, 136);
554static PORT_PMA_ATTR(local_link_integrity_errors , 9, 4, 152);
555static PORT_PMA_ATTR(excessive_buffer_overrun_errors, 10, 4, 156);
556static PORT_PMA_ATTR(VL15_dropped , 11, 16, 176);
557static PORT_PMA_ATTR(port_xmit_data , 12, 32, 192);
558static PORT_PMA_ATTR(port_rcv_data , 13, 32, 224);
559static PORT_PMA_ATTR(port_xmit_packets , 14, 32, 256);
560static PORT_PMA_ATTR(port_rcv_packets , 15, 32, 288);
c5a81d11 561static PORT_PMA_ATTR(port_xmit_wait , 0, 32, 320);
1da177e4 562
145d9c54
CL
563/*
564 * Counters added by extended set
565 */
566static PORT_PMA_ATTR_EXT(port_xmit_data , 64, 64);
567static PORT_PMA_ATTR_EXT(port_rcv_data , 64, 128);
568static PORT_PMA_ATTR_EXT(port_xmit_packets , 64, 192);
569static PORT_PMA_ATTR_EXT(port_rcv_packets , 64, 256);
570static PORT_PMA_ATTR_EXT(unicast_xmit_packets , 64, 320);
571static PORT_PMA_ATTR_EXT(unicast_rcv_packets , 64, 384);
572static PORT_PMA_ATTR_EXT(multicast_xmit_packets , 64, 448);
573static PORT_PMA_ATTR_EXT(multicast_rcv_packets , 64, 512);
574
1da177e4
LT
575static struct attribute *pma_attrs[] = {
576 &port_pma_attr_symbol_error.attr.attr,
577 &port_pma_attr_link_error_recovery.attr.attr,
578 &port_pma_attr_link_downed.attr.attr,
579 &port_pma_attr_port_rcv_errors.attr.attr,
580 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
581 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
582 &port_pma_attr_port_xmit_discards.attr.attr,
583 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
584 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
585 &port_pma_attr_local_link_integrity_errors.attr.attr,
586 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
587 &port_pma_attr_VL15_dropped.attr.attr,
588 &port_pma_attr_port_xmit_data.attr.attr,
589 &port_pma_attr_port_rcv_data.attr.attr,
590 &port_pma_attr_port_xmit_packets.attr.attr,
591 &port_pma_attr_port_rcv_packets.attr.attr,
c5a81d11 592 &port_pma_attr_port_xmit_wait.attr.attr,
1da177e4
LT
593 NULL
594};
595
145d9c54
CL
596static struct attribute *pma_attrs_ext[] = {
597 &port_pma_attr_symbol_error.attr.attr,
598 &port_pma_attr_link_error_recovery.attr.attr,
599 &port_pma_attr_link_downed.attr.attr,
600 &port_pma_attr_port_rcv_errors.attr.attr,
601 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
602 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
603 &port_pma_attr_port_xmit_discards.attr.attr,
604 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
605 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
606 &port_pma_attr_local_link_integrity_errors.attr.attr,
607 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
608 &port_pma_attr_VL15_dropped.attr.attr,
609 &port_pma_attr_ext_port_xmit_data.attr.attr,
610 &port_pma_attr_ext_port_rcv_data.attr.attr,
611 &port_pma_attr_ext_port_xmit_packets.attr.attr,
c5a81d11 612 &port_pma_attr_port_xmit_wait.attr.attr,
145d9c54
CL
613 &port_pma_attr_ext_port_rcv_packets.attr.attr,
614 &port_pma_attr_ext_unicast_rcv_packets.attr.attr,
615 &port_pma_attr_ext_unicast_xmit_packets.attr.attr,
616 &port_pma_attr_ext_multicast_rcv_packets.attr.attr,
617 &port_pma_attr_ext_multicast_xmit_packets.attr.attr,
618 NULL
619};
620
621static struct attribute *pma_attrs_noietf[] = {
622 &port_pma_attr_symbol_error.attr.attr,
623 &port_pma_attr_link_error_recovery.attr.attr,
624 &port_pma_attr_link_downed.attr.attr,
625 &port_pma_attr_port_rcv_errors.attr.attr,
626 &port_pma_attr_port_rcv_remote_physical_errors.attr.attr,
627 &port_pma_attr_port_rcv_switch_relay_errors.attr.attr,
628 &port_pma_attr_port_xmit_discards.attr.attr,
629 &port_pma_attr_port_xmit_constraint_errors.attr.attr,
630 &port_pma_attr_port_rcv_constraint_errors.attr.attr,
631 &port_pma_attr_local_link_integrity_errors.attr.attr,
632 &port_pma_attr_excessive_buffer_overrun_errors.attr.attr,
633 &port_pma_attr_VL15_dropped.attr.attr,
634 &port_pma_attr_ext_port_xmit_data.attr.attr,
635 &port_pma_attr_ext_port_rcv_data.attr.attr,
636 &port_pma_attr_ext_port_xmit_packets.attr.attr,
637 &port_pma_attr_ext_port_rcv_packets.attr.attr,
c5a81d11 638 &port_pma_attr_port_xmit_wait.attr.attr,
145d9c54
CL
639 NULL
640};
641
1da177e4
LT
642static struct attribute_group pma_group = {
643 .name = "counters",
644 .attrs = pma_attrs
645};
646
145d9c54
CL
647static struct attribute_group pma_group_ext = {
648 .name = "counters",
649 .attrs = pma_attrs_ext
650};
651
652static struct attribute_group pma_group_noietf = {
653 .name = "counters",
654 .attrs = pma_attrs_noietf
655};
656
1da177e4
LT
657static void ib_port_release(struct kobject *kobj)
658{
659 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
660 struct attribute *a;
661 int i;
662
cad6d02a
HE
663 if (p->gid_group.attrs) {
664 for (i = 0; (a = p->gid_group.attrs[i]); ++i)
665 kfree(a);
1da177e4 666
cad6d02a
HE
667 kfree(p->gid_group.attrs);
668 }
d48593bf 669
cad6d02a
HE
670 if (p->pkey_group.attrs) {
671 for (i = 0; (a = p->pkey_group.attrs[i]); ++i)
672 kfree(a);
1da177e4 673
cad6d02a
HE
674 kfree(p->pkey_group.attrs);
675 }
d48593bf 676
1da177e4
LT
677 kfree(p);
678}
679
470be516
MB
680static void ib_port_gid_attr_release(struct kobject *kobj)
681{
682 struct gid_attr_group *g = container_of(kobj, struct gid_attr_group,
683 kobj);
684 struct attribute *a;
685 int i;
686
687 if (g->ndev.attrs) {
688 for (i = 0; (a = g->ndev.attrs[i]); ++i)
689 kfree(a);
690
691 kfree(g->ndev.attrs);
692 }
693
694 if (g->type.attrs) {
695 for (i = 0; (a = g->type.attrs[i]); ++i)
696 kfree(a);
697
698 kfree(g->type.attrs);
699 }
700
701 kfree(g);
702}
703
1da177e4
LT
704static struct kobj_type port_type = {
705 .release = ib_port_release,
706 .sysfs_ops = &port_sysfs_ops,
707 .default_attrs = port_default_attrs
708};
709
470be516
MB
710static struct kobj_type gid_attr_type = {
711 .sysfs_ops = &gid_attr_sysfs_ops,
712 .release = ib_port_gid_attr_release
713};
714
d48593bf
DT
715static struct attribute **
716alloc_group_attrs(ssize_t (*show)(struct ib_port *,
717 struct port_attribute *, char *buf),
718 int len)
1da177e4 719{
d48593bf
DT
720 struct attribute **tab_attr;
721 struct port_table_attribute *element;
1da177e4 722 int i;
1da177e4 723
d48593bf
DT
724 tab_attr = kcalloc(1 + len, sizeof(struct attribute *), GFP_KERNEL);
725 if (!tab_attr)
726 return NULL;
1da177e4 727
d48593bf 728 for (i = 0; i < len; i++) {
82ca76b6 729 element = kzalloc(sizeof(struct port_table_attribute),
d48593bf
DT
730 GFP_KERNEL);
731 if (!element)
1da177e4 732 goto err;
1da177e4 733
d48593bf 734 if (snprintf(element->name, sizeof(element->name),
048975ac
RD
735 "%d", i) >= sizeof(element->name)) {
736 kfree(element);
1da177e4 737 goto err;
048975ac 738 }
1da177e4 739
d48593bf
DT
740 element->attr.attr.name = element->name;
741 element->attr.attr.mode = S_IRUGO;
d48593bf
DT
742 element->attr.show = show;
743 element->index = i;
21e3bde9 744 sysfs_attr_init(&element->attr.attr);
1da177e4 745
d48593bf 746 tab_attr[i] = &element->attr.attr;
1da177e4
LT
747 }
748
d48593bf 749 return tab_attr;
1da177e4 750
d48593bf
DT
751err:
752 while (--i >= 0)
753 kfree(tab_attr[i]);
754 kfree(tab_attr);
755 return NULL;
1da177e4
LT
756}
757
145d9c54
CL
758/*
759 * Figure out which counter table to use depending on
760 * the device capabilities.
761 */
6e2a51a0
HR
762static struct attribute_group *get_counter_table(struct ib_device *dev,
763 int port_num)
145d9c54
CL
764{
765 struct ib_class_port_info cpi;
766
6e2a51a0 767 if (get_perf_mad(dev, port_num, IB_PMA_CLASS_PORT_INFO,
145d9c54 768 &cpi, 40, sizeof(cpi)) >= 0) {
ee50aeac 769 if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH)
145d9c54
CL
770 /* We have extended counters */
771 return &pma_group_ext;
772
ee50aeac 773 if (cpi.capability_mask & IB_PMA_CLASS_CAP_EXT_WIDTH_NOIETF)
145d9c54
CL
774 /* But not the IETF ones */
775 return &pma_group_noietf;
776 }
777
778 /* Fall back to normal counters */
779 return &pma_group;
780}
781
b40f4757
CL
782static int update_hw_stats(struct ib_device *dev, struct rdma_hw_stats *stats,
783 u8 port_num, int index)
784{
785 int ret;
786
787 if (time_is_after_eq_jiffies(stats->timestamp + stats->lifespan))
788 return 0;
3023a1e9 789 ret = dev->ops.get_hw_stats(dev, stats, port_num, index);
b40f4757
CL
790 if (ret < 0)
791 return ret;
792 if (ret == stats->num_counters)
793 stats->timestamp = jiffies;
794
795 return 0;
796}
797
798static ssize_t print_hw_stat(struct rdma_hw_stats *stats, int index, char *buf)
799{
800 return sprintf(buf, "%llu\n", stats->value[index]);
801}
802
803static ssize_t show_hw_stats(struct kobject *kobj, struct attribute *attr,
804 char *buf)
805{
806 struct ib_device *dev;
807 struct ib_port *port;
808 struct hw_stats_attribute *hsa;
809 struct rdma_hw_stats *stats;
810 int ret;
811
812 hsa = container_of(attr, struct hw_stats_attribute, attr);
813 if (!hsa->port_num) {
814 dev = container_of((struct device *)kobj,
815 struct ib_device, dev);
816 stats = dev->hw_stats;
817 } else {
818 port = container_of(kobj, struct ib_port, kobj);
819 dev = port->ibdev;
820 stats = port->hw_stats;
821 }
e945130b 822 mutex_lock(&stats->lock);
b40f4757
CL
823 ret = update_hw_stats(dev, stats, hsa->port_num, hsa->index);
824 if (ret)
e945130b
MB
825 goto unlock;
826 ret = print_hw_stat(stats, hsa->index, buf);
827unlock:
828 mutex_unlock(&stats->lock);
829
830 return ret;
b40f4757
CL
831}
832
833static ssize_t show_stats_lifespan(struct kobject *kobj,
834 struct attribute *attr,
835 char *buf)
836{
837 struct hw_stats_attribute *hsa;
e945130b 838 struct rdma_hw_stats *stats;
b40f4757
CL
839 int msecs;
840
841 hsa = container_of(attr, struct hw_stats_attribute, attr);
842 if (!hsa->port_num) {
843 struct ib_device *dev = container_of((struct device *)kobj,
844 struct ib_device, dev);
e945130b
MB
845
846 stats = dev->hw_stats;
b40f4757
CL
847 } else {
848 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
e945130b
MB
849
850 stats = p->hw_stats;
b40f4757 851 }
e945130b
MB
852
853 mutex_lock(&stats->lock);
854 msecs = jiffies_to_msecs(stats->lifespan);
855 mutex_unlock(&stats->lock);
856
b40f4757
CL
857 return sprintf(buf, "%d\n", msecs);
858}
859
860static ssize_t set_stats_lifespan(struct kobject *kobj,
861 struct attribute *attr,
862 const char *buf, size_t count)
863{
864 struct hw_stats_attribute *hsa;
e945130b 865 struct rdma_hw_stats *stats;
b40f4757
CL
866 int msecs;
867 int jiffies;
868 int ret;
869
870 ret = kstrtoint(buf, 10, &msecs);
871 if (ret)
872 return ret;
873 if (msecs < 0 || msecs > 10000)
874 return -EINVAL;
875 jiffies = msecs_to_jiffies(msecs);
876 hsa = container_of(attr, struct hw_stats_attribute, attr);
877 if (!hsa->port_num) {
878 struct ib_device *dev = container_of((struct device *)kobj,
879 struct ib_device, dev);
e945130b
MB
880
881 stats = dev->hw_stats;
b40f4757
CL
882 } else {
883 struct ib_port *p = container_of(kobj, struct ib_port, kobj);
e945130b
MB
884
885 stats = p->hw_stats;
b40f4757 886 }
e945130b
MB
887
888 mutex_lock(&stats->lock);
889 stats->lifespan = jiffies;
890 mutex_unlock(&stats->lock);
891
b40f4757
CL
892 return count;
893}
894
895static void free_hsag(struct kobject *kobj, struct attribute_group *attr_group)
896{
897 struct attribute **attr;
898
899 sysfs_remove_group(kobj, attr_group);
900
901 for (attr = attr_group->attrs; *attr; attr++)
902 kfree(*attr);
903 kfree(attr_group);
904}
905
906static struct attribute *alloc_hsa(int index, u8 port_num, const char *name)
907{
908 struct hw_stats_attribute *hsa;
909
910 hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
911 if (!hsa)
912 return NULL;
913
914 hsa->attr.name = (char *)name;
915 hsa->attr.mode = S_IRUGO;
916 hsa->show = show_hw_stats;
917 hsa->store = NULL;
918 hsa->index = index;
919 hsa->port_num = port_num;
920
921 return &hsa->attr;
922}
923
924static struct attribute *alloc_hsa_lifespan(char *name, u8 port_num)
925{
926 struct hw_stats_attribute *hsa;
927
928 hsa = kmalloc(sizeof(*hsa), GFP_KERNEL);
929 if (!hsa)
930 return NULL;
931
932 hsa->attr.name = name;
933 hsa->attr.mode = S_IWUSR | S_IRUGO;
934 hsa->show = show_stats_lifespan;
935 hsa->store = set_stats_lifespan;
936 hsa->index = 0;
937 hsa->port_num = port_num;
938
939 return &hsa->attr;
940}
941
942static void setup_hw_stats(struct ib_device *device, struct ib_port *port,
943 u8 port_num)
944{
495fbae6 945 struct attribute_group *hsag;
b40f4757 946 struct rdma_hw_stats *stats;
495fbae6 947 int i, ret;
b40f4757 948
3023a1e9 949 stats = device->ops.alloc_hw_stats(device, port_num);
b40f4757
CL
950
951 if (!stats)
952 return;
953
954 if (!stats->names || stats->num_counters <= 0)
0147ebcf 955 goto err_free_stats;
b40f4757 956
41aaa99f
DL
957 /*
958 * Two extra attribue elements here, one for the lifespan entry and
959 * one to NULL terminate the list for the sysfs core code
960 */
b40f4757 961 hsag = kzalloc(sizeof(*hsag) +
41aaa99f 962 sizeof(void *) * (stats->num_counters + 2),
b40f4757
CL
963 GFP_KERNEL);
964 if (!hsag)
0147ebcf 965 goto err_free_stats;
b40f4757 966
3023a1e9
KH
967 ret = device->ops.get_hw_stats(device, stats, port_num,
968 stats->num_counters);
b40f4757 969 if (ret != stats->num_counters)
495fbae6 970 goto err_free_hsag;
b40f4757
CL
971
972 stats->timestamp = jiffies;
973
974 hsag->name = "hw_counters";
975 hsag->attrs = (void *)hsag + sizeof(*hsag);
976
977 for (i = 0; i < stats->num_counters; i++) {
978 hsag->attrs[i] = alloc_hsa(i, port_num, stats->names[i]);
979 if (!hsag->attrs[i])
980 goto err;
8aec013a 981 sysfs_attr_init(hsag->attrs[i]);
b40f4757
CL
982 }
983
e945130b 984 mutex_init(&stats->lock);
b40f4757
CL
985 /* treat an error here as non-fatal */
986 hsag->attrs[i] = alloc_hsa_lifespan("lifespan", port_num);
8aec013a
MB
987 if (hsag->attrs[i])
988 sysfs_attr_init(hsag->attrs[i]);
b40f4757
CL
989
990 if (port) {
991 struct kobject *kobj = &port->kobj;
992 ret = sysfs_create_group(kobj, hsag);
993 if (ret)
994 goto err;
995 port->hw_stats_ag = hsag;
996 port->hw_stats = stats;
997 } else {
998 struct kobject *kobj = &device->dev.kobj;
999 ret = sysfs_create_group(kobj, hsag);
1000 if (ret)
1001 goto err;
1002 device->hw_stats_ag = hsag;
1003 device->hw_stats = stats;
1004 }
1005
1006 return;
1007
1008err:
b40f4757
CL
1009 for (; i >= 0; i--)
1010 kfree(hsag->attrs[i]);
495fbae6 1011err_free_hsag:
b40f4757 1012 kfree(hsag);
0147ebcf
CIK
1013err_free_stats:
1014 kfree(stats);
b40f4757
CL
1015 return;
1016}
1017
ea4baf7f 1018static int add_port(struct ib_device *device, int port_num)
1da177e4
LT
1019{
1020 struct ib_port *p;
1021 struct ib_port_attr attr;
1022 int i;
1023 int ret;
1024
1025 ret = ib_query_port(device, port_num, &attr);
1026 if (ret)
1027 return ret;
1028
de6eb66b 1029 p = kzalloc(sizeof *p, GFP_KERNEL);
1da177e4
LT
1030 if (!p)
1031 return -ENOMEM;
1da177e4
LT
1032
1033 p->ibdev = device;
1034 p->port_num = port_num;
1da177e4 1035
35be0681 1036 ret = kobject_init_and_add(&p->kobj, &port_type,
1ae4cfa0 1037 device->ports_kobj,
35be0681 1038 "%d", port_num);
cad6d02a
HE
1039 if (ret) {
1040 kfree(p);
1041 return ret;
1042 }
1da177e4 1043
470be516
MB
1044 p->gid_attr_group = kzalloc(sizeof(*p->gid_attr_group), GFP_KERNEL);
1045 if (!p->gid_attr_group) {
1046 ret = -ENOMEM;
1047 goto err_put;
1048 }
1049
1050 p->gid_attr_group->port = p;
1051 ret = kobject_init_and_add(&p->gid_attr_group->kobj, &gid_attr_type,
1052 &p->kobj, "gid_attrs");
1053 if (ret) {
1054 kfree(p->gid_attr_group);
1055 goto err_put;
1056 }
1057
3023a1e9 1058 if (device->ops.process_mad) {
0f6ef65d
PP
1059 p->pma_table = get_counter_table(device, port_num);
1060 ret = sysfs_create_group(&p->kobj, p->pma_table);
1061 if (ret)
1062 goto err_put_gid_attrs;
1063 }
1da177e4 1064
1da177e4 1065 p->gid_group.name = "gids";
d48593bf 1066 p->gid_group.attrs = alloc_group_attrs(show_port_gid, attr.gid_tbl_len);
80b15043
WY
1067 if (!p->gid_group.attrs) {
1068 ret = -ENOMEM;
d48593bf 1069 goto err_remove_pma;
80b15043 1070 }
1da177e4
LT
1071
1072 ret = sysfs_create_group(&p->kobj, &p->gid_group);
1073 if (ret)
1074 goto err_free_gid;
1075
470be516
MB
1076 p->gid_attr_group->ndev.name = "ndevs";
1077 p->gid_attr_group->ndev.attrs = alloc_group_attrs(show_port_gid_attr_ndev,
1078 attr.gid_tbl_len);
1079 if (!p->gid_attr_group->ndev.attrs) {
1080 ret = -ENOMEM;
1081 goto err_remove_gid;
1082 }
1083
1084 ret = sysfs_create_group(&p->gid_attr_group->kobj,
1085 &p->gid_attr_group->ndev);
1086 if (ret)
1087 goto err_free_gid_ndev;
1088
1089 p->gid_attr_group->type.name = "types";
1090 p->gid_attr_group->type.attrs = alloc_group_attrs(show_port_gid_attr_gid_type,
1091 attr.gid_tbl_len);
1092 if (!p->gid_attr_group->type.attrs) {
1093 ret = -ENOMEM;
1094 goto err_remove_gid_ndev;
1095 }
1096
1097 ret = sysfs_create_group(&p->gid_attr_group->kobj,
1098 &p->gid_attr_group->type);
1099 if (ret)
1100 goto err_free_gid_type;
1101
1da177e4 1102 p->pkey_group.name = "pkeys";
d48593bf
DT
1103 p->pkey_group.attrs = alloc_group_attrs(show_port_pkey,
1104 attr.pkey_tbl_len);
80b15043
WY
1105 if (!p->pkey_group.attrs) {
1106 ret = -ENOMEM;
470be516 1107 goto err_remove_gid_type;
80b15043 1108 }
1da177e4
LT
1109
1110 ret = sysfs_create_group(&p->kobj, &p->pkey_group);
1111 if (ret)
1112 goto err_free_pkey;
1113
ea4baf7f
PP
1114 if (device->ops.init_port) {
1115 ret = device->ops.init_port(device, port_num, &p->kobj);
9a6edb60
RC
1116 if (ret)
1117 goto err_remove_pkey;
1118 }
1119
b40f4757 1120 /*
76d865b8
PP
1121 * If port == 0, it means hw_counters are per device and not per
1122 * port, so holder should be device. Therefore skip per port conunter
1123 * initialization.
b40f4757 1124 */
3023a1e9 1125 if (device->ops.alloc_hw_stats && port_num)
b40f4757
CL
1126 setup_hw_stats(device, p, port_num);
1127
1da177e4
LT
1128 list_add_tail(&p->kobj.entry, &device->port_list);
1129
35be0681 1130 kobject_uevent(&p->kobj, KOBJ_ADD);
1da177e4
LT
1131 return 0;
1132
9a6edb60
RC
1133err_remove_pkey:
1134 sysfs_remove_group(&p->kobj, &p->pkey_group);
1135
1da177e4 1136err_free_pkey:
d48593bf
DT
1137 for (i = 0; i < attr.pkey_tbl_len; ++i)
1138 kfree(p->pkey_group.attrs[i]);
1da177e4 1139
d48593bf 1140 kfree(p->pkey_group.attrs);
cad6d02a 1141 p->pkey_group.attrs = NULL;
1da177e4 1142
470be516
MB
1143err_remove_gid_type:
1144 sysfs_remove_group(&p->gid_attr_group->kobj,
1145 &p->gid_attr_group->type);
1146
1147err_free_gid_type:
1148 for (i = 0; i < attr.gid_tbl_len; ++i)
1149 kfree(p->gid_attr_group->type.attrs[i]);
1150
1151 kfree(p->gid_attr_group->type.attrs);
1152 p->gid_attr_group->type.attrs = NULL;
1153
1154err_remove_gid_ndev:
1155 sysfs_remove_group(&p->gid_attr_group->kobj,
1156 &p->gid_attr_group->ndev);
1157
1158err_free_gid_ndev:
1159 for (i = 0; i < attr.gid_tbl_len; ++i)
1160 kfree(p->gid_attr_group->ndev.attrs[i]);
1161
1162 kfree(p->gid_attr_group->ndev.attrs);
1163 p->gid_attr_group->ndev.attrs = NULL;
1164
1da177e4
LT
1165err_remove_gid:
1166 sysfs_remove_group(&p->kobj, &p->gid_group);
1167
1168err_free_gid:
d48593bf
DT
1169 for (i = 0; i < attr.gid_tbl_len; ++i)
1170 kfree(p->gid_group.attrs[i]);
1da177e4 1171
d48593bf 1172 kfree(p->gid_group.attrs);
cad6d02a 1173 p->gid_group.attrs = NULL;
1da177e4
LT
1174
1175err_remove_pma:
0f6ef65d
PP
1176 if (p->pma_table)
1177 sysfs_remove_group(&p->kobj, p->pma_table);
1da177e4 1178
470be516
MB
1179err_put_gid_attrs:
1180 kobject_put(&p->gid_attr_group->kobj);
1181
1da177e4 1182err_put:
cad6d02a 1183 kobject_put(&p->kobj);
1da177e4
LT
1184 return ret;
1185}
1186
adee9f3f 1187static ssize_t node_type_show(struct device *device,
f4e91eb4 1188 struct device_attribute *attr, char *buf)
1da177e4 1189{
54747231 1190 struct ib_device *dev = rdma_device_to_ibdev(device);
1da177e4
LT
1191
1192 switch (dev->node_type) {
07ebafba
TT
1193 case RDMA_NODE_IB_CA: return sprintf(buf, "%d: CA\n", dev->node_type);
1194 case RDMA_NODE_RNIC: return sprintf(buf, "%d: RNIC\n", dev->node_type);
180771a3 1195 case RDMA_NODE_USNIC: return sprintf(buf, "%d: usNIC\n", dev->node_type);
5db5765e 1196 case RDMA_NODE_USNIC_UDP: return sprintf(buf, "%d: usNIC UDP\n", dev->node_type);
07ebafba
TT
1197 case RDMA_NODE_IB_SWITCH: return sprintf(buf, "%d: switch\n", dev->node_type);
1198 case RDMA_NODE_IB_ROUTER: return sprintf(buf, "%d: router\n", dev->node_type);
1199 default: return sprintf(buf, "%d: <unknown>\n", dev->node_type);
1da177e4
LT
1200 }
1201}
adee9f3f 1202static DEVICE_ATTR_RO(node_type);
1da177e4 1203
adee9f3f 1204static ssize_t sys_image_guid_show(struct device *device,
f4e91eb4 1205 struct device_attribute *dev_attr, char *buf)
1da177e4 1206{
54747231 1207 struct ib_device *dev = rdma_device_to_ibdev(device);
1da177e4
LT
1208
1209 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
86bee4c9
OG
1210 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[0]),
1211 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[1]),
1212 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[2]),
1213 be16_to_cpu(((__be16 *) &dev->attrs.sys_image_guid)[3]));
1da177e4 1214}
adee9f3f 1215static DEVICE_ATTR_RO(sys_image_guid);
1da177e4 1216
adee9f3f 1217static ssize_t node_guid_show(struct device *device,
f4e91eb4 1218 struct device_attribute *attr, char *buf)
1da177e4 1219{
54747231 1220 struct ib_device *dev = rdma_device_to_ibdev(device);
1da177e4 1221
1da177e4 1222 return sprintf(buf, "%04x:%04x:%04x:%04x\n",
cf311cd4
SH
1223 be16_to_cpu(((__be16 *) &dev->node_guid)[0]),
1224 be16_to_cpu(((__be16 *) &dev->node_guid)[1]),
1225 be16_to_cpu(((__be16 *) &dev->node_guid)[2]),
1226 be16_to_cpu(((__be16 *) &dev->node_guid)[3]));
1da177e4 1227}
adee9f3f 1228static DEVICE_ATTR_RO(node_guid);
1da177e4 1229
adee9f3f 1230static ssize_t node_desc_show(struct device *device,
f4e91eb4 1231 struct device_attribute *attr, char *buf)
c5bcbbb9 1232{
54747231 1233 struct ib_device *dev = rdma_device_to_ibdev(device);
c5bcbbb9
RD
1234
1235 return sprintf(buf, "%.64s\n", dev->node_desc);
1236}
1237
adee9f3f
PP
1238static ssize_t node_desc_store(struct device *device,
1239 struct device_attribute *attr,
1240 const char *buf, size_t count)
c5bcbbb9 1241{
54747231 1242 struct ib_device *dev = rdma_device_to_ibdev(device);
c5bcbbb9
RD
1243 struct ib_device_modify desc = {};
1244 int ret;
1245
3023a1e9 1246 if (!dev->ops.modify_device)
c5bcbbb9
RD
1247 return -EIO;
1248
bd99fdea 1249 memcpy(desc.node_desc, buf, min_t(int, count, IB_DEVICE_NODE_DESC_MAX));
c5bcbbb9
RD
1250 ret = ib_modify_device(dev, IB_DEVICE_MODIFY_NODE_DESC, &desc);
1251 if (ret)
1252 return ret;
1253
1254 return count;
1255}
adee9f3f 1256static DEVICE_ATTR_RW(node_desc);
c5bcbbb9 1257
adee9f3f 1258static ssize_t fw_ver_show(struct device *device, struct device_attribute *attr,
41a6ae1e
IW
1259 char *buf)
1260{
54747231 1261 struct ib_device *dev = rdma_device_to_ibdev(device);
41a6ae1e 1262
9abb0d1b
LR
1263 ib_get_device_fw_str(dev, buf);
1264 strlcat(buf, "\n", IB_FW_VERSION_NAME_MAX);
41a6ae1e
IW
1265 return strlen(buf);
1266}
adee9f3f
PP
1267static DEVICE_ATTR_RO(fw_ver);
1268
1269static struct attribute *ib_dev_attrs[] = {
1270 &dev_attr_node_type.attr,
1271 &dev_attr_node_guid.attr,
1272 &dev_attr_sys_image_guid.attr,
1273 &dev_attr_fw_ver.attr,
1274 &dev_attr_node_desc.attr,
1275 NULL,
1276};
41a6ae1e 1277
5f8f5499 1278const struct attribute_group ib_dev_attr_group = {
adee9f3f 1279 .attrs = ib_dev_attrs,
1da177e4
LT
1280};
1281
5767198a 1282static void ib_free_port_attrs(struct ib_device *device)
584482ac
HE
1283{
1284 struct kobject *p, *t;
1285
1286 list_for_each_entry_safe(p, t, &device->port_list, entry) {
1287 struct ib_port *port = container_of(p, struct ib_port, kobj);
5767198a 1288
584482ac 1289 list_del(&p->entry);
5767198a 1290 if (port->hw_stats_ag)
b40f4757 1291 free_hsag(&port->kobj, port->hw_stats_ag);
5767198a 1292 kfree(port->hw_stats);
0f6ef65d
PP
1293
1294 if (port->pma_table)
1295 sysfs_remove_group(p, port->pma_table);
584482ac
HE
1296 sysfs_remove_group(p, &port->pkey_group);
1297 sysfs_remove_group(p, &port->gid_group);
470be516
MB
1298 sysfs_remove_group(&port->gid_attr_group->kobj,
1299 &port->gid_attr_group->ndev);
1300 sysfs_remove_group(&port->gid_attr_group->kobj,
1301 &port->gid_attr_group->type);
1302 kobject_put(&port->gid_attr_group->kobj);
584482ac
HE
1303 kobject_put(p);
1304 }
1305
1ae4cfa0 1306 kobject_put(device->ports_kobj);
584482ac
HE
1307}
1308
5767198a 1309static int ib_setup_port_attrs(struct ib_device *device)
1da177e4 1310{
ea1075ed 1311 unsigned int port;
1da177e4 1312 int ret;
1da177e4 1313
5767198a
PP
1314 device->ports_kobj = kobject_create_and_add("ports", &device->dev.kobj);
1315 if (!device->ports_kobj)
1316 return -ENOMEM;
1da177e4 1317
ea1075ed
JG
1318 rdma_for_each_port (device, port) {
1319 ret = add_port(device, port);
1da177e4
LT
1320 if (ret)
1321 goto err_put;
1da177e4
LT
1322 }
1323
1324 return 0;
1325
1326err_put:
5767198a 1327 ib_free_port_attrs(device);
1da177e4
LT
1328 return ret;
1329}
1330
5767198a 1331int ib_device_register_sysfs(struct ib_device *device)
1da177e4 1332{
5767198a 1333 int ret;
584482ac 1334
5f8f5499 1335 ret = ib_setup_port_attrs(device);
5767198a
PP
1336 if (ret)
1337 return ret;
1338
5767198a
PP
1339 if (device->ops.alloc_hw_stats)
1340 setup_hw_stats(device, NULL, 0);
1341
1342 return 0;
1343}
1344
1345void ib_device_unregister_sysfs(struct ib_device *device)
1346{
1347 if (device->hw_stats_ag)
1348 free_hsag(&device->dev.kobj, device->hw_stats_ag);
1349 kfree(device->hw_stats);
1350
1351 ib_free_port_attrs(device);
1da177e4 1352}