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