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