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