1 /* 2 * Copyright (c) 2012 Mellanox Technologies. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 */ 32 33 /*#include "core_priv.h"*/ 34 #include "mlx4_ib.h" 35 #include <linux/slab.h> 36 #include <linux/string.h> 37 #include <linux/stat.h> 38 39 #include <rdma/ib_mad.h> 40 /*show_admin_alias_guid returns the administratively assigned value of that GUID. 41 * Values returned in buf parameter string: 42 * 0 - requests opensm to assign a value. 43 * ffffffffffffffff - delete this entry. 44 * other - value assigned by administrator. 45 */ 46 static ssize_t show_admin_alias_guid(struct device *dev, 47 struct device_attribute *attr, char *buf) 48 { 49 struct mlx4_ib_iov_sysfs_attr *mlx4_ib_iov_dentry = 50 container_of(attr, struct mlx4_ib_iov_sysfs_attr, dentry); 51 struct mlx4_ib_iov_port *port = mlx4_ib_iov_dentry->ctx; 52 struct mlx4_ib_dev *mdev = port->dev; 53 __be64 sysadmin_ag_val; 54 55 sysadmin_ag_val = mlx4_get_admin_guid(mdev->dev, 56 mlx4_ib_iov_dentry->entry_num, 57 port->num); 58 59 return sprintf(buf, "%llx\n", be64_to_cpu(sysadmin_ag_val)); 60 } 61 62 /* store_admin_alias_guid stores the (new) administratively assigned value of that GUID. 63 * Values in buf parameter string: 64 * 0 - requests opensm to assign a value. 65 * 0xffffffffffffffff - delete this entry. 66 * other - guid value assigned by the administrator. 67 */ 68 static ssize_t store_admin_alias_guid(struct device *dev, 69 struct device_attribute *attr, 70 const char *buf, size_t count) 71 { 72 int record_num;/*0-15*/ 73 int guid_index_in_rec; /*0 - 7*/ 74 struct mlx4_ib_iov_sysfs_attr *mlx4_ib_iov_dentry = 75 container_of(attr, struct mlx4_ib_iov_sysfs_attr, dentry); 76 struct mlx4_ib_iov_port *port = mlx4_ib_iov_dentry->ctx; 77 struct mlx4_ib_dev *mdev = port->dev; 78 u64 sysadmin_ag_val; 79 unsigned long flags; 80 81 record_num = mlx4_ib_iov_dentry->entry_num / 8; 82 guid_index_in_rec = mlx4_ib_iov_dentry->entry_num % 8; 83 if (0 == record_num && 0 == guid_index_in_rec) { 84 pr_err("GUID 0 block 0 is RO\n"); 85 return count; 86 } 87 spin_lock_irqsave(&mdev->sriov.alias_guid.ag_work_lock, flags); 88 sscanf(buf, "%llx", &sysadmin_ag_val); 89 *(__be64 *)&mdev->sriov.alias_guid.ports_guid[port->num - 1]. 90 all_rec_per_port[record_num]. 91 all_recs[GUID_REC_SIZE * guid_index_in_rec] = 92 cpu_to_be64(sysadmin_ag_val); 93 94 /* Change the state to be pending for update */ 95 mdev->sriov.alias_guid.ports_guid[port->num - 1].all_rec_per_port[record_num].status 96 = MLX4_GUID_INFO_STATUS_IDLE ; 97 mlx4_set_admin_guid(mdev->dev, cpu_to_be64(sysadmin_ag_val), 98 mlx4_ib_iov_dentry->entry_num, 99 port->num); 100 101 /* set the record index */ 102 mdev->sriov.alias_guid.ports_guid[port->num - 1].all_rec_per_port[record_num].guid_indexes 103 |= mlx4_ib_get_aguid_comp_mask_from_ix(guid_index_in_rec); 104 105 spin_unlock_irqrestore(&mdev->sriov.alias_guid.ag_work_lock, flags); 106 mlx4_ib_init_alias_guid_work(mdev, port->num - 1); 107 108 return count; 109 } 110 111 static ssize_t show_port_gid(struct device *dev, 112 struct device_attribute *attr, 113 char *buf) 114 { 115 struct mlx4_ib_iov_sysfs_attr *mlx4_ib_iov_dentry = 116 container_of(attr, struct mlx4_ib_iov_sysfs_attr, dentry); 117 struct mlx4_ib_iov_port *port = mlx4_ib_iov_dentry->ctx; 118 struct mlx4_ib_dev *mdev = port->dev; 119 union ib_gid gid; 120 ssize_t ret; 121 122 ret = __mlx4_ib_query_gid(&mdev->ib_dev, port->num, 123 mlx4_ib_iov_dentry->entry_num, &gid, 1); 124 if (ret) 125 return ret; 126 ret = sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n", 127 be16_to_cpu(((__be16 *) gid.raw)[0]), 128 be16_to_cpu(((__be16 *) gid.raw)[1]), 129 be16_to_cpu(((__be16 *) gid.raw)[2]), 130 be16_to_cpu(((__be16 *) gid.raw)[3]), 131 be16_to_cpu(((__be16 *) gid.raw)[4]), 132 be16_to_cpu(((__be16 *) gid.raw)[5]), 133 be16_to_cpu(((__be16 *) gid.raw)[6]), 134 be16_to_cpu(((__be16 *) gid.raw)[7])); 135 return ret; 136 } 137 138 static ssize_t show_phys_port_pkey(struct device *dev, 139 struct device_attribute *attr, 140 char *buf) 141 { 142 struct mlx4_ib_iov_sysfs_attr *mlx4_ib_iov_dentry = 143 container_of(attr, struct mlx4_ib_iov_sysfs_attr, dentry); 144 struct mlx4_ib_iov_port *port = mlx4_ib_iov_dentry->ctx; 145 struct mlx4_ib_dev *mdev = port->dev; 146 u16 pkey; 147 ssize_t ret; 148 149 ret = __mlx4_ib_query_pkey(&mdev->ib_dev, port->num, 150 mlx4_ib_iov_dentry->entry_num, &pkey, 1); 151 if (ret) 152 return ret; 153 154 return sprintf(buf, "0x%04x\n", pkey); 155 } 156 157 #define DENTRY_REMOVE(_dentry) \ 158 do { \ 159 sysfs_remove_file((_dentry)->kobj, &(_dentry)->dentry.attr); \ 160 } while (0); 161 162 static int create_sysfs_entry(void *_ctx, struct mlx4_ib_iov_sysfs_attr *_dentry, 163 char *_name, struct kobject *_kobj, 164 ssize_t (*show)(struct device *dev, 165 struct device_attribute *attr, 166 char *buf), 167 ssize_t (*store)(struct device *dev, 168 struct device_attribute *attr, 169 const char *buf, size_t count) 170 ) 171 { 172 int ret = 0; 173 struct mlx4_ib_iov_sysfs_attr *vdentry = _dentry; 174 175 vdentry->ctx = _ctx; 176 vdentry->dentry.show = show; 177 vdentry->dentry.store = store; 178 sysfs_attr_init(&vdentry->dentry.attr); 179 vdentry->dentry.attr.name = vdentry->name; 180 vdentry->dentry.attr.mode = 0; 181 vdentry->kobj = _kobj; 182 snprintf(vdentry->name, 15, "%s", _name); 183 184 if (vdentry->dentry.store) 185 vdentry->dentry.attr.mode |= S_IWUSR; 186 187 if (vdentry->dentry.show) 188 vdentry->dentry.attr.mode |= S_IRUGO; 189 190 ret = sysfs_create_file(vdentry->kobj, &vdentry->dentry.attr); 191 if (ret) { 192 pr_err("failed to create %s\n", vdentry->dentry.attr.name); 193 vdentry->ctx = NULL; 194 return ret; 195 } 196 197 return ret; 198 } 199 200 int add_sysfs_port_mcg_attr(struct mlx4_ib_dev *device, int port_num, 201 struct attribute *attr) 202 { 203 struct mlx4_ib_iov_port *port = &device->iov_ports[port_num - 1]; 204 int ret; 205 206 ret = sysfs_create_file(port->mcgs_parent, attr); 207 if (ret) 208 pr_err("failed to create %s\n", attr->name); 209 210 return ret; 211 } 212 213 void del_sysfs_port_mcg_attr(struct mlx4_ib_dev *device, int port_num, 214 struct attribute *attr) 215 { 216 struct mlx4_ib_iov_port *port = &device->iov_ports[port_num - 1]; 217 218 sysfs_remove_file(port->mcgs_parent, attr); 219 } 220 221 static int add_port_entries(struct mlx4_ib_dev *device, int port_num) 222 { 223 int i; 224 char buff[11]; 225 struct mlx4_ib_iov_port *port = NULL; 226 int ret = 0 ; 227 struct ib_port_attr attr; 228 229 memset(&attr, 0, sizeof(attr)); 230 /* get the physical gid and pkey table sizes.*/ 231 ret = __mlx4_ib_query_port(&device->ib_dev, port_num, &attr, 1); 232 if (ret) 233 goto err; 234 235 port = &device->iov_ports[port_num - 1]; 236 port->dev = device; 237 port->num = port_num; 238 /* Directory structure: 239 * iov - 240 * port num - 241 * admin_guids 242 * gids (operational) 243 * mcg_table 244 */ 245 port->dentr_ar = kzalloc(sizeof (struct mlx4_ib_iov_sysfs_attr_ar), 246 GFP_KERNEL); 247 if (!port->dentr_ar) { 248 ret = -ENOMEM; 249 goto err; 250 } 251 sprintf(buff, "%d", port_num); 252 port->cur_port = kobject_create_and_add(buff, 253 kobject_get(device->ports_parent)); 254 if (!port->cur_port) { 255 ret = -ENOMEM; 256 goto kobj_create_err; 257 } 258 /* admin GUIDs */ 259 port->admin_alias_parent = kobject_create_and_add("admin_guids", 260 kobject_get(port->cur_port)); 261 if (!port->admin_alias_parent) { 262 ret = -ENOMEM; 263 goto err_admin_guids; 264 } 265 for (i = 0 ; i < attr.gid_tbl_len; i++) { 266 sprintf(buff, "%d", i); 267 port->dentr_ar->dentries[i].entry_num = i; 268 ret = create_sysfs_entry(port, &port->dentr_ar->dentries[i], 269 buff, port->admin_alias_parent, 270 show_admin_alias_guid, store_admin_alias_guid); 271 if (ret) 272 goto err_admin_alias_parent; 273 } 274 275 /* gids subdirectory (operational gids) */ 276 port->gids_parent = kobject_create_and_add("gids", 277 kobject_get(port->cur_port)); 278 if (!port->gids_parent) { 279 ret = -ENOMEM; 280 goto err_gids; 281 } 282 283 for (i = 0 ; i < attr.gid_tbl_len; i++) { 284 sprintf(buff, "%d", i); 285 port->dentr_ar->dentries[attr.gid_tbl_len + i].entry_num = i; 286 ret = create_sysfs_entry(port, 287 &port->dentr_ar->dentries[attr.gid_tbl_len + i], 288 buff, 289 port->gids_parent, show_port_gid, NULL); 290 if (ret) 291 goto err_gids_parent; 292 } 293 294 /* physical port pkey table */ 295 port->pkeys_parent = 296 kobject_create_and_add("pkeys", kobject_get(port->cur_port)); 297 if (!port->pkeys_parent) { 298 ret = -ENOMEM; 299 goto err_pkeys; 300 } 301 302 for (i = 0 ; i < attr.pkey_tbl_len; i++) { 303 sprintf(buff, "%d", i); 304 port->dentr_ar->dentries[2 * attr.gid_tbl_len + i].entry_num = i; 305 ret = create_sysfs_entry(port, 306 &port->dentr_ar->dentries[2 * attr.gid_tbl_len + i], 307 buff, port->pkeys_parent, 308 show_phys_port_pkey, NULL); 309 if (ret) 310 goto err_pkeys_parent; 311 } 312 313 /* MCGs table */ 314 port->mcgs_parent = 315 kobject_create_and_add("mcgs", kobject_get(port->cur_port)); 316 if (!port->mcgs_parent) { 317 ret = -ENOMEM; 318 goto err_mcgs; 319 } 320 return 0; 321 322 err_mcgs: 323 kobject_put(port->cur_port); 324 325 err_pkeys_parent: 326 kobject_put(port->pkeys_parent); 327 328 err_pkeys: 329 kobject_put(port->cur_port); 330 331 err_gids_parent: 332 kobject_put(port->gids_parent); 333 334 err_gids: 335 kobject_put(port->cur_port); 336 337 err_admin_alias_parent: 338 kobject_put(port->admin_alias_parent); 339 340 err_admin_guids: 341 kobject_put(port->cur_port); 342 kobject_put(port->cur_port); /* once more for create_and_add buff */ 343 344 kobj_create_err: 345 kobject_put(device->ports_parent); 346 kfree(port->dentr_ar); 347 348 err: 349 pr_err("add_port_entries FAILED: for port:%d, error: %d\n", 350 port_num, ret); 351 return ret; 352 } 353 354 static void get_name(struct mlx4_ib_dev *dev, char *name, int i, int max) 355 { 356 /* pci_name format is: bus:dev:func -> xxxx:yy:zz.n 357 * with no ARI only 3 last bits are used so when the fn is higher than 8 358 * need to add it to the dev num, so count in the last number will be 359 * modulo 8 */ 360 snprintf(name, max, "%.8s%.2d.%d", pci_name(dev->dev->persist->pdev), 361 i / 8, i % 8); 362 } 363 364 struct mlx4_port { 365 struct kobject kobj; 366 struct mlx4_ib_dev *dev; 367 struct attribute_group pkey_group; 368 struct attribute_group gid_group; 369 struct device_attribute enable_smi_admin; 370 struct device_attribute smi_enabled; 371 int slave; 372 u8 port_num; 373 }; 374 375 376 static void mlx4_port_release(struct kobject *kobj) 377 { 378 struct mlx4_port *p = container_of(kobj, struct mlx4_port, kobj); 379 struct attribute *a; 380 int i; 381 382 for (i = 0; (a = p->pkey_group.attrs[i]); ++i) 383 kfree(a); 384 kfree(p->pkey_group.attrs); 385 for (i = 0; (a = p->gid_group.attrs[i]); ++i) 386 kfree(a); 387 kfree(p->gid_group.attrs); 388 kfree(p); 389 } 390 391 struct port_attribute { 392 struct attribute attr; 393 ssize_t (*show)(struct mlx4_port *, struct port_attribute *, char *buf); 394 ssize_t (*store)(struct mlx4_port *, struct port_attribute *, 395 const char *buf, size_t count); 396 }; 397 398 static ssize_t port_attr_show(struct kobject *kobj, 399 struct attribute *attr, char *buf) 400 { 401 struct port_attribute *port_attr = 402 container_of(attr, struct port_attribute, attr); 403 struct mlx4_port *p = container_of(kobj, struct mlx4_port, kobj); 404 405 if (!port_attr->show) 406 return -EIO; 407 return port_attr->show(p, port_attr, buf); 408 } 409 410 static ssize_t port_attr_store(struct kobject *kobj, 411 struct attribute *attr, 412 const char *buf, size_t size) 413 { 414 struct port_attribute *port_attr = 415 container_of(attr, struct port_attribute, attr); 416 struct mlx4_port *p = container_of(kobj, struct mlx4_port, kobj); 417 418 if (!port_attr->store) 419 return -EIO; 420 return port_attr->store(p, port_attr, buf, size); 421 } 422 423 static const struct sysfs_ops port_sysfs_ops = { 424 .show = port_attr_show, 425 .store = port_attr_store, 426 }; 427 428 static struct kobj_type port_type = { 429 .release = mlx4_port_release, 430 .sysfs_ops = &port_sysfs_ops, 431 }; 432 433 struct port_table_attribute { 434 struct port_attribute attr; 435 char name[8]; 436 int index; 437 }; 438 439 static ssize_t show_port_pkey(struct mlx4_port *p, struct port_attribute *attr, 440 char *buf) 441 { 442 struct port_table_attribute *tab_attr = 443 container_of(attr, struct port_table_attribute, attr); 444 ssize_t ret = -ENODEV; 445 446 if (p->dev->pkeys.virt2phys_pkey[p->slave][p->port_num - 1][tab_attr->index] >= 447 (p->dev->dev->caps.pkey_table_len[p->port_num])) 448 ret = sprintf(buf, "none\n"); 449 else 450 ret = sprintf(buf, "%d\n", 451 p->dev->pkeys.virt2phys_pkey[p->slave] 452 [p->port_num - 1][tab_attr->index]); 453 return ret; 454 } 455 456 static ssize_t store_port_pkey(struct mlx4_port *p, struct port_attribute *attr, 457 const char *buf, size_t count) 458 { 459 struct port_table_attribute *tab_attr = 460 container_of(attr, struct port_table_attribute, attr); 461 int idx; 462 int err; 463 464 /* do not allow remapping Dom0 virtual pkey table */ 465 if (p->slave == mlx4_master_func_num(p->dev->dev)) 466 return -EINVAL; 467 468 if (!strncasecmp(buf, "no", 2)) 469 idx = p->dev->dev->phys_caps.pkey_phys_table_len[p->port_num] - 1; 470 else if (sscanf(buf, "%i", &idx) != 1 || 471 idx >= p->dev->dev->caps.pkey_table_len[p->port_num] || 472 idx < 0) 473 return -EINVAL; 474 475 p->dev->pkeys.virt2phys_pkey[p->slave][p->port_num - 1] 476 [tab_attr->index] = idx; 477 mlx4_sync_pkey_table(p->dev->dev, p->slave, p->port_num, 478 tab_attr->index, idx); 479 err = mlx4_gen_pkey_eqe(p->dev->dev, p->slave, p->port_num); 480 if (err) { 481 pr_err("mlx4_gen_pkey_eqe failed for slave %d," 482 " port %d, index %d\n", p->slave, p->port_num, idx); 483 return err; 484 } 485 return count; 486 } 487 488 static ssize_t show_port_gid_idx(struct mlx4_port *p, 489 struct port_attribute *attr, char *buf) 490 { 491 return sprintf(buf, "%d\n", p->slave); 492 } 493 494 static struct attribute ** 495 alloc_group_attrs(ssize_t (*show)(struct mlx4_port *, 496 struct port_attribute *, char *buf), 497 ssize_t (*store)(struct mlx4_port *, struct port_attribute *, 498 const char *buf, size_t count), 499 int len) 500 { 501 struct attribute **tab_attr; 502 struct port_table_attribute *element; 503 int i; 504 505 tab_attr = kcalloc(1 + len, sizeof (struct attribute *), GFP_KERNEL); 506 if (!tab_attr) 507 return NULL; 508 509 for (i = 0; i < len; i++) { 510 element = kzalloc(sizeof (struct port_table_attribute), 511 GFP_KERNEL); 512 if (!element) 513 goto err; 514 if (snprintf(element->name, sizeof (element->name), 515 "%d", i) >= sizeof (element->name)) { 516 kfree(element); 517 goto err; 518 } 519 sysfs_attr_init(&element->attr.attr); 520 element->attr.attr.name = element->name; 521 if (store) { 522 element->attr.attr.mode = S_IWUSR | S_IRUGO; 523 element->attr.store = store; 524 } else 525 element->attr.attr.mode = S_IRUGO; 526 527 element->attr.show = show; 528 element->index = i; 529 tab_attr[i] = &element->attr.attr; 530 } 531 return tab_attr; 532 533 err: 534 while (--i >= 0) 535 kfree(tab_attr[i]); 536 kfree(tab_attr); 537 return NULL; 538 } 539 540 static ssize_t sysfs_show_smi_enabled(struct device *dev, 541 struct device_attribute *attr, char *buf) 542 { 543 struct mlx4_port *p = 544 container_of(attr, struct mlx4_port, smi_enabled); 545 ssize_t len = 0; 546 547 if (mlx4_vf_smi_enabled(p->dev->dev, p->slave, p->port_num)) 548 len = sprintf(buf, "%d\n", 1); 549 else 550 len = sprintf(buf, "%d\n", 0); 551 552 return len; 553 } 554 555 static ssize_t sysfs_show_enable_smi_admin(struct device *dev, 556 struct device_attribute *attr, 557 char *buf) 558 { 559 struct mlx4_port *p = 560 container_of(attr, struct mlx4_port, enable_smi_admin); 561 ssize_t len = 0; 562 563 if (mlx4_vf_get_enable_smi_admin(p->dev->dev, p->slave, p->port_num)) 564 len = sprintf(buf, "%d\n", 1); 565 else 566 len = sprintf(buf, "%d\n", 0); 567 568 return len; 569 } 570 571 static ssize_t sysfs_store_enable_smi_admin(struct device *dev, 572 struct device_attribute *attr, 573 const char *buf, size_t count) 574 { 575 struct mlx4_port *p = 576 container_of(attr, struct mlx4_port, enable_smi_admin); 577 int enable; 578 579 if (sscanf(buf, "%i", &enable) != 1 || 580 enable < 0 || enable > 1) 581 return -EINVAL; 582 583 if (mlx4_vf_set_enable_smi_admin(p->dev->dev, p->slave, p->port_num, enable)) 584 return -EINVAL; 585 return count; 586 } 587 588 static int add_vf_smi_entries(struct mlx4_port *p) 589 { 590 int is_eth = rdma_port_get_link_layer(&p->dev->ib_dev, p->port_num) == 591 IB_LINK_LAYER_ETHERNET; 592 int ret; 593 594 /* do not display entries if eth transport, or if master */ 595 if (is_eth || p->slave == mlx4_master_func_num(p->dev->dev)) 596 return 0; 597 598 sysfs_attr_init(&p->smi_enabled.attr); 599 p->smi_enabled.show = sysfs_show_smi_enabled; 600 p->smi_enabled.store = NULL; 601 p->smi_enabled.attr.name = "smi_enabled"; 602 p->smi_enabled.attr.mode = 0444; 603 ret = sysfs_create_file(&p->kobj, &p->smi_enabled.attr); 604 if (ret) { 605 pr_err("failed to create smi_enabled\n"); 606 return ret; 607 } 608 609 sysfs_attr_init(&p->enable_smi_admin.attr); 610 p->enable_smi_admin.show = sysfs_show_enable_smi_admin; 611 p->enable_smi_admin.store = sysfs_store_enable_smi_admin; 612 p->enable_smi_admin.attr.name = "enable_smi_admin"; 613 p->enable_smi_admin.attr.mode = 0644; 614 ret = sysfs_create_file(&p->kobj, &p->enable_smi_admin.attr); 615 if (ret) { 616 pr_err("failed to create enable_smi_admin\n"); 617 sysfs_remove_file(&p->kobj, &p->smi_enabled.attr); 618 return ret; 619 } 620 return 0; 621 } 622 623 static void remove_vf_smi_entries(struct mlx4_port *p) 624 { 625 int is_eth = rdma_port_get_link_layer(&p->dev->ib_dev, p->port_num) == 626 IB_LINK_LAYER_ETHERNET; 627 628 if (is_eth || p->slave == mlx4_master_func_num(p->dev->dev)) 629 return; 630 631 sysfs_remove_file(&p->kobj, &p->smi_enabled.attr); 632 sysfs_remove_file(&p->kobj, &p->enable_smi_admin.attr); 633 } 634 635 static int add_port(struct mlx4_ib_dev *dev, int port_num, int slave) 636 { 637 struct mlx4_port *p; 638 int i; 639 int ret; 640 int is_eth = rdma_port_get_link_layer(&dev->ib_dev, port_num) == 641 IB_LINK_LAYER_ETHERNET; 642 643 p = kzalloc(sizeof *p, GFP_KERNEL); 644 if (!p) 645 return -ENOMEM; 646 647 p->dev = dev; 648 p->port_num = port_num; 649 p->slave = slave; 650 651 ret = kobject_init_and_add(&p->kobj, &port_type, 652 kobject_get(dev->dev_ports_parent[slave]), 653 "%d", port_num); 654 if (ret) 655 goto err_alloc; 656 657 p->pkey_group.name = "pkey_idx"; 658 p->pkey_group.attrs = 659 alloc_group_attrs(show_port_pkey, 660 is_eth ? NULL : store_port_pkey, 661 dev->dev->caps.pkey_table_len[port_num]); 662 if (!p->pkey_group.attrs) { 663 ret = -ENOMEM; 664 goto err_alloc; 665 } 666 667 ret = sysfs_create_group(&p->kobj, &p->pkey_group); 668 if (ret) 669 goto err_free_pkey; 670 671 p->gid_group.name = "gid_idx"; 672 p->gid_group.attrs = alloc_group_attrs(show_port_gid_idx, NULL, 1); 673 if (!p->gid_group.attrs) { 674 ret = -ENOMEM; 675 goto err_free_pkey; 676 } 677 678 ret = sysfs_create_group(&p->kobj, &p->gid_group); 679 if (ret) 680 goto err_free_gid; 681 682 ret = add_vf_smi_entries(p); 683 if (ret) 684 goto err_free_gid; 685 686 list_add_tail(&p->kobj.entry, &dev->pkeys.pkey_port_list[slave]); 687 return 0; 688 689 err_free_gid: 690 kfree(p->gid_group.attrs[0]); 691 kfree(p->gid_group.attrs); 692 693 err_free_pkey: 694 for (i = 0; i < dev->dev->caps.pkey_table_len[port_num]; ++i) 695 kfree(p->pkey_group.attrs[i]); 696 kfree(p->pkey_group.attrs); 697 698 err_alloc: 699 kobject_put(dev->dev_ports_parent[slave]); 700 kfree(p); 701 return ret; 702 } 703 704 static int register_one_pkey_tree(struct mlx4_ib_dev *dev, int slave) 705 { 706 char name[32]; 707 int err; 708 int port; 709 struct kobject *p, *t; 710 struct mlx4_port *mport; 711 struct mlx4_active_ports actv_ports; 712 713 get_name(dev, name, slave, sizeof name); 714 715 dev->pkeys.device_parent[slave] = 716 kobject_create_and_add(name, kobject_get(dev->iov_parent)); 717 718 if (!dev->pkeys.device_parent[slave]) { 719 err = -ENOMEM; 720 goto fail_dev; 721 } 722 723 INIT_LIST_HEAD(&dev->pkeys.pkey_port_list[slave]); 724 725 dev->dev_ports_parent[slave] = 726 kobject_create_and_add("ports", 727 kobject_get(dev->pkeys.device_parent[slave])); 728 729 if (!dev->dev_ports_parent[slave]) { 730 err = -ENOMEM; 731 goto err_ports; 732 } 733 734 actv_ports = mlx4_get_active_ports(dev->dev, slave); 735 736 for (port = 1; port <= dev->dev->caps.num_ports; ++port) { 737 if (!test_bit(port - 1, actv_ports.ports)) 738 continue; 739 err = add_port(dev, port, slave); 740 if (err) 741 goto err_add; 742 } 743 return 0; 744 745 err_add: 746 list_for_each_entry_safe(p, t, 747 &dev->pkeys.pkey_port_list[slave], 748 entry) { 749 list_del(&p->entry); 750 mport = container_of(p, struct mlx4_port, kobj); 751 sysfs_remove_group(p, &mport->pkey_group); 752 sysfs_remove_group(p, &mport->gid_group); 753 remove_vf_smi_entries(mport); 754 kobject_put(p); 755 } 756 kobject_put(dev->dev_ports_parent[slave]); 757 758 err_ports: 759 kobject_put(dev->pkeys.device_parent[slave]); 760 /* extra put for the device_parent create_and_add */ 761 kobject_put(dev->pkeys.device_parent[slave]); 762 763 fail_dev: 764 kobject_put(dev->iov_parent); 765 return err; 766 } 767 768 static int register_pkey_tree(struct mlx4_ib_dev *device) 769 { 770 int i; 771 772 if (!mlx4_is_master(device->dev)) 773 return 0; 774 775 for (i = 0; i <= device->dev->persist->num_vfs; ++i) 776 register_one_pkey_tree(device, i); 777 778 return 0; 779 } 780 781 static void unregister_pkey_tree(struct mlx4_ib_dev *device) 782 { 783 int slave; 784 struct kobject *p, *t; 785 struct mlx4_port *port; 786 787 if (!mlx4_is_master(device->dev)) 788 return; 789 790 for (slave = device->dev->persist->num_vfs; slave >= 0; --slave) { 791 list_for_each_entry_safe(p, t, 792 &device->pkeys.pkey_port_list[slave], 793 entry) { 794 list_del(&p->entry); 795 port = container_of(p, struct mlx4_port, kobj); 796 sysfs_remove_group(p, &port->pkey_group); 797 sysfs_remove_group(p, &port->gid_group); 798 remove_vf_smi_entries(port); 799 kobject_put(p); 800 kobject_put(device->dev_ports_parent[slave]); 801 } 802 kobject_put(device->dev_ports_parent[slave]); 803 kobject_put(device->pkeys.device_parent[slave]); 804 kobject_put(device->pkeys.device_parent[slave]); 805 kobject_put(device->iov_parent); 806 } 807 } 808 809 int mlx4_ib_device_register_sysfs(struct mlx4_ib_dev *dev) 810 { 811 int i; 812 int ret = 0; 813 814 if (!mlx4_is_master(dev->dev)) 815 return 0; 816 817 dev->iov_parent = kobject_create_and_add("iov", &dev->ib_dev.dev.kobj); 818 if (!dev->iov_parent) { 819 ret = -ENOMEM; 820 goto err; 821 } 822 dev->ports_parent = 823 kobject_create_and_add("ports", 824 kobject_get(dev->iov_parent)); 825 if (!dev->ports_parent) { 826 ret = -ENOMEM; 827 goto err_ports; 828 } 829 830 for (i = 1; i <= dev->ib_dev.phys_port_cnt; ++i) { 831 ret = add_port_entries(dev, i); 832 if (ret) 833 goto err_add_entries; 834 } 835 836 ret = register_pkey_tree(dev); 837 if (ret) 838 goto err_add_entries; 839 return 0; 840 841 err_add_entries: 842 kobject_put(dev->ports_parent); 843 844 err_ports: 845 kobject_put(dev->iov_parent); 846 err: 847 pr_err("mlx4_ib_device_register_sysfs error (%d)\n", ret); 848 return ret; 849 } 850 851 static void unregister_alias_guid_tree(struct mlx4_ib_dev *device) 852 { 853 struct mlx4_ib_iov_port *p; 854 int i; 855 856 if (!mlx4_is_master(device->dev)) 857 return; 858 859 for (i = 0; i < device->dev->caps.num_ports; i++) { 860 p = &device->iov_ports[i]; 861 kobject_put(p->admin_alias_parent); 862 kobject_put(p->gids_parent); 863 kobject_put(p->pkeys_parent); 864 kobject_put(p->mcgs_parent); 865 kobject_put(p->cur_port); 866 kobject_put(p->cur_port); 867 kobject_put(p->cur_port); 868 kobject_put(p->cur_port); 869 kobject_put(p->cur_port); 870 kobject_put(p->dev->ports_parent); 871 kfree(p->dentr_ar); 872 } 873 } 874 875 void mlx4_ib_device_unregister_sysfs(struct mlx4_ib_dev *device) 876 { 877 unregister_alias_guid_tree(device); 878 unregister_pkey_tree(device); 879 kobject_put(device->ports_parent); 880 kobject_put(device->iov_parent); 881 kobject_put(device->iov_parent); 882 } 883