1 /* 2 * Copyright (c) 2004 Topspin Communications. All rights reserved. 3 * Copyright (c) 2005 Intel Corporation. All rights reserved. 4 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 5 * Copyright (c) 2005 Voltaire, Inc. All rights reserved. 6 * 7 * This software is available to you under a choice of one of two 8 * licenses. You may choose to be licensed under the terms of the GNU 9 * General Public License (GPL) Version 2, available from the file 10 * COPYING in the main directory of this source tree, or the 11 * OpenIB.org BSD license below: 12 * 13 * Redistribution and use in source and binary forms, with or 14 * without modification, are permitted provided that the following 15 * conditions are met: 16 * 17 * - Redistributions of source code must retain the above 18 * copyright notice, this list of conditions and the following 19 * disclaimer. 20 * 21 * - Redistributions in binary form must reproduce the above 22 * copyright notice, this list of conditions and the following 23 * disclaimer in the documentation and/or other materials 24 * provided with the distribution. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 30 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 31 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 32 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 33 * SOFTWARE. 34 */ 35 36 #include <linux/if_vlan.h> 37 #include <linux/module.h> 38 #include <linux/errno.h> 39 #include <linux/slab.h> 40 #include <linux/workqueue.h> 41 #include <linux/netdevice.h> 42 #include <net/addrconf.h> 43 44 #include <rdma/ib_cache.h> 45 46 #include "core_priv.h" 47 48 struct ib_pkey_cache { 49 int table_len; 50 u16 table[]; 51 }; 52 53 struct ib_update_work { 54 struct work_struct work; 55 struct ib_event event; 56 bool enforce_security; 57 }; 58 59 union ib_gid zgid; 60 EXPORT_SYMBOL(zgid); 61 62 enum gid_attr_find_mask { 63 GID_ATTR_FIND_MASK_GID = 1UL << 0, 64 GID_ATTR_FIND_MASK_NETDEV = 1UL << 1, 65 GID_ATTR_FIND_MASK_DEFAULT = 1UL << 2, 66 GID_ATTR_FIND_MASK_GID_TYPE = 1UL << 3, 67 }; 68 69 enum gid_table_entry_state { 70 GID_TABLE_ENTRY_INVALID = 1, 71 GID_TABLE_ENTRY_VALID = 2, 72 /* 73 * Indicates that entry is pending to be removed, there may 74 * be active users of this GID entry. 75 * When last user of the GID entry releases reference to it, 76 * GID entry is detached from the table. 77 */ 78 GID_TABLE_ENTRY_PENDING_DEL = 3, 79 }; 80 81 struct roce_gid_ndev_storage { 82 struct rcu_head rcu_head; 83 struct net_device *ndev; 84 }; 85 86 struct ib_gid_table_entry { 87 struct kref kref; 88 struct work_struct del_work; 89 struct ib_gid_attr attr; 90 void *context; 91 /* Store the ndev pointer to release reference later on in 92 * call_rcu context because by that time gid_table_entry 93 * and attr might be already freed. So keep a copy of it. 94 * ndev_storage is freed by rcu callback. 95 */ 96 struct roce_gid_ndev_storage *ndev_storage; 97 enum gid_table_entry_state state; 98 }; 99 100 struct ib_gid_table { 101 int sz; 102 /* In RoCE, adding a GID to the table requires: 103 * (a) Find if this GID is already exists. 104 * (b) Find a free space. 105 * (c) Write the new GID 106 * 107 * Delete requires different set of operations: 108 * (a) Find the GID 109 * (b) Delete it. 110 * 111 **/ 112 /* Any writer to data_vec must hold this lock and the write side of 113 * rwlock. Readers must hold only rwlock. All writers must be in a 114 * sleepable context. 115 */ 116 struct mutex lock; 117 /* rwlock protects data_vec[ix]->state and entry pointer. 118 */ 119 rwlock_t rwlock; 120 struct ib_gid_table_entry **data_vec; 121 /* bit field, each bit indicates the index of default GID */ 122 u32 default_gid_indices; 123 }; 124 125 static void dispatch_gid_change_event(struct ib_device *ib_dev, u32 port) 126 { 127 struct ib_event event; 128 129 event.device = ib_dev; 130 event.element.port_num = port; 131 event.event = IB_EVENT_GID_CHANGE; 132 133 ib_dispatch_event_clients(&event); 134 } 135 136 static const char * const gid_type_str[] = { 137 /* IB/RoCE v1 value is set for IB_GID_TYPE_IB and IB_GID_TYPE_ROCE for 138 * user space compatibility reasons. 139 */ 140 [IB_GID_TYPE_IB] = "IB/RoCE v1", 141 [IB_GID_TYPE_ROCE] = "IB/RoCE v1", 142 [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2", 143 }; 144 145 const char *ib_cache_gid_type_str(enum ib_gid_type gid_type) 146 { 147 if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type]) 148 return gid_type_str[gid_type]; 149 150 return "Invalid GID type"; 151 } 152 EXPORT_SYMBOL(ib_cache_gid_type_str); 153 154 /** rdma_is_zero_gid - Check if given GID is zero or not. 155 * @gid: GID to check 156 * Returns true if given GID is zero, returns false otherwise. 157 */ 158 bool rdma_is_zero_gid(const union ib_gid *gid) 159 { 160 return !memcmp(gid, &zgid, sizeof(*gid)); 161 } 162 EXPORT_SYMBOL(rdma_is_zero_gid); 163 164 /** is_gid_index_default - Check if a given index belongs to 165 * reserved default GIDs or not. 166 * @table: GID table pointer 167 * @index: Index to check in GID table 168 * Returns true if index is one of the reserved default GID index otherwise 169 * returns false. 170 */ 171 static bool is_gid_index_default(const struct ib_gid_table *table, 172 unsigned int index) 173 { 174 return index < 32 && (BIT(index) & table->default_gid_indices); 175 } 176 177 int ib_cache_gid_parse_type_str(const char *buf) 178 { 179 unsigned int i; 180 size_t len; 181 int err = -EINVAL; 182 183 len = strlen(buf); 184 if (len == 0) 185 return -EINVAL; 186 187 if (buf[len - 1] == '\n') 188 len--; 189 190 for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i) 191 if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) && 192 len == strlen(gid_type_str[i])) { 193 err = i; 194 break; 195 } 196 197 return err; 198 } 199 EXPORT_SYMBOL(ib_cache_gid_parse_type_str); 200 201 static struct ib_gid_table *rdma_gid_table(struct ib_device *device, u32 port) 202 { 203 return device->port_data[port].cache.gid; 204 } 205 206 static bool is_gid_entry_free(const struct ib_gid_table_entry *entry) 207 { 208 return !entry; 209 } 210 211 static bool is_gid_entry_valid(const struct ib_gid_table_entry *entry) 212 { 213 return entry && entry->state == GID_TABLE_ENTRY_VALID; 214 } 215 216 static void schedule_free_gid(struct kref *kref) 217 { 218 struct ib_gid_table_entry *entry = 219 container_of(kref, struct ib_gid_table_entry, kref); 220 221 queue_work(ib_wq, &entry->del_work); 222 } 223 224 static void put_gid_ndev(struct rcu_head *head) 225 { 226 struct roce_gid_ndev_storage *storage = 227 container_of(head, struct roce_gid_ndev_storage, rcu_head); 228 229 WARN_ON(!storage->ndev); 230 /* At this point its safe to release netdev reference, 231 * as all callers working on gid_attr->ndev are done 232 * using this netdev. 233 */ 234 dev_put(storage->ndev); 235 kfree(storage); 236 } 237 238 static void free_gid_entry_locked(struct ib_gid_table_entry *entry) 239 { 240 struct ib_device *device = entry->attr.device; 241 u32 port_num = entry->attr.port_num; 242 struct ib_gid_table *table = rdma_gid_table(device, port_num); 243 244 dev_dbg(&device->dev, "%s port=%u index=%u gid %pI6\n", __func__, 245 port_num, entry->attr.index, entry->attr.gid.raw); 246 247 write_lock_irq(&table->rwlock); 248 249 /* 250 * The only way to avoid overwriting NULL in table is 251 * by comparing if it is same entry in table or not! 252 * If new entry in table is added by the time we free here, 253 * don't overwrite the table entry. 254 */ 255 if (entry == table->data_vec[entry->attr.index]) 256 table->data_vec[entry->attr.index] = NULL; 257 /* Now this index is ready to be allocated */ 258 write_unlock_irq(&table->rwlock); 259 260 if (entry->ndev_storage) 261 call_rcu(&entry->ndev_storage->rcu_head, put_gid_ndev); 262 kfree(entry); 263 } 264 265 static void free_gid_entry(struct kref *kref) 266 { 267 struct ib_gid_table_entry *entry = 268 container_of(kref, struct ib_gid_table_entry, kref); 269 270 free_gid_entry_locked(entry); 271 } 272 273 /** 274 * free_gid_work - Release reference to the GID entry 275 * @work: Work structure to refer to GID entry which needs to be 276 * deleted. 277 * 278 * free_gid_work() frees the entry from the HCA's hardware table 279 * if provider supports it. It releases reference to netdevice. 280 */ 281 static void free_gid_work(struct work_struct *work) 282 { 283 struct ib_gid_table_entry *entry = 284 container_of(work, struct ib_gid_table_entry, del_work); 285 struct ib_device *device = entry->attr.device; 286 u32 port_num = entry->attr.port_num; 287 struct ib_gid_table *table = rdma_gid_table(device, port_num); 288 289 mutex_lock(&table->lock); 290 free_gid_entry_locked(entry); 291 mutex_unlock(&table->lock); 292 } 293 294 static struct ib_gid_table_entry * 295 alloc_gid_entry(const struct ib_gid_attr *attr) 296 { 297 struct ib_gid_table_entry *entry; 298 struct net_device *ndev; 299 300 entry = kzalloc(sizeof(*entry), GFP_KERNEL); 301 if (!entry) 302 return NULL; 303 304 ndev = rcu_dereference_protected(attr->ndev, 1); 305 if (ndev) { 306 entry->ndev_storage = kzalloc(sizeof(*entry->ndev_storage), 307 GFP_KERNEL); 308 if (!entry->ndev_storage) { 309 kfree(entry); 310 return NULL; 311 } 312 dev_hold(ndev); 313 entry->ndev_storage->ndev = ndev; 314 } 315 kref_init(&entry->kref); 316 memcpy(&entry->attr, attr, sizeof(*attr)); 317 INIT_WORK(&entry->del_work, free_gid_work); 318 entry->state = GID_TABLE_ENTRY_INVALID; 319 return entry; 320 } 321 322 static void store_gid_entry(struct ib_gid_table *table, 323 struct ib_gid_table_entry *entry) 324 { 325 entry->state = GID_TABLE_ENTRY_VALID; 326 327 dev_dbg(&entry->attr.device->dev, "%s port=%u index=%u gid %pI6\n", 328 __func__, entry->attr.port_num, entry->attr.index, 329 entry->attr.gid.raw); 330 331 lockdep_assert_held(&table->lock); 332 write_lock_irq(&table->rwlock); 333 table->data_vec[entry->attr.index] = entry; 334 write_unlock_irq(&table->rwlock); 335 } 336 337 static void get_gid_entry(struct ib_gid_table_entry *entry) 338 { 339 kref_get(&entry->kref); 340 } 341 342 static void put_gid_entry(struct ib_gid_table_entry *entry) 343 { 344 kref_put(&entry->kref, schedule_free_gid); 345 } 346 347 static void put_gid_entry_locked(struct ib_gid_table_entry *entry) 348 { 349 kref_put(&entry->kref, free_gid_entry); 350 } 351 352 static int add_roce_gid(struct ib_gid_table_entry *entry) 353 { 354 const struct ib_gid_attr *attr = &entry->attr; 355 int ret; 356 357 if (!attr->ndev) { 358 dev_err(&attr->device->dev, "%s NULL netdev port=%u index=%u\n", 359 __func__, attr->port_num, attr->index); 360 return -EINVAL; 361 } 362 if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) { 363 ret = attr->device->ops.add_gid(attr, &entry->context); 364 if (ret) { 365 dev_err(&attr->device->dev, 366 "%s GID add failed port=%u index=%u\n", 367 __func__, attr->port_num, attr->index); 368 return ret; 369 } 370 } 371 return 0; 372 } 373 374 /** 375 * del_gid - Delete GID table entry 376 * 377 * @ib_dev: IB device whose GID entry to be deleted 378 * @port: Port number of the IB device 379 * @table: GID table of the IB device for a port 380 * @ix: GID entry index to delete 381 * 382 */ 383 static void del_gid(struct ib_device *ib_dev, u32 port, 384 struct ib_gid_table *table, int ix) 385 { 386 struct roce_gid_ndev_storage *ndev_storage; 387 struct ib_gid_table_entry *entry; 388 389 lockdep_assert_held(&table->lock); 390 391 dev_dbg(&ib_dev->dev, "%s port=%u index=%d gid %pI6\n", __func__, port, 392 ix, table->data_vec[ix]->attr.gid.raw); 393 394 write_lock_irq(&table->rwlock); 395 entry = table->data_vec[ix]; 396 entry->state = GID_TABLE_ENTRY_PENDING_DEL; 397 /* 398 * For non RoCE protocol, GID entry slot is ready to use. 399 */ 400 if (!rdma_protocol_roce(ib_dev, port)) 401 table->data_vec[ix] = NULL; 402 write_unlock_irq(&table->rwlock); 403 404 ndev_storage = entry->ndev_storage; 405 if (ndev_storage) { 406 entry->ndev_storage = NULL; 407 rcu_assign_pointer(entry->attr.ndev, NULL); 408 call_rcu(&ndev_storage->rcu_head, put_gid_ndev); 409 } 410 411 if (rdma_cap_roce_gid_table(ib_dev, port)) 412 ib_dev->ops.del_gid(&entry->attr, &entry->context); 413 414 put_gid_entry_locked(entry); 415 } 416 417 /** 418 * add_modify_gid - Add or modify GID table entry 419 * 420 * @table: GID table in which GID to be added or modified 421 * @attr: Attributes of the GID 422 * 423 * Returns 0 on success or appropriate error code. It accepts zero 424 * GID addition for non RoCE ports for HCA's who report them as valid 425 * GID. However such zero GIDs are not added to the cache. 426 */ 427 static int add_modify_gid(struct ib_gid_table *table, 428 const struct ib_gid_attr *attr) 429 { 430 struct ib_gid_table_entry *entry; 431 int ret = 0; 432 433 /* 434 * Invalidate any old entry in the table to make it safe to write to 435 * this index. 436 */ 437 if (is_gid_entry_valid(table->data_vec[attr->index])) 438 del_gid(attr->device, attr->port_num, table, attr->index); 439 440 /* 441 * Some HCA's report multiple GID entries with only one valid GID, and 442 * leave other unused entries as the zero GID. Convert zero GIDs to 443 * empty table entries instead of storing them. 444 */ 445 if (rdma_is_zero_gid(&attr->gid)) 446 return 0; 447 448 entry = alloc_gid_entry(attr); 449 if (!entry) 450 return -ENOMEM; 451 452 if (rdma_protocol_roce(attr->device, attr->port_num)) { 453 ret = add_roce_gid(entry); 454 if (ret) 455 goto done; 456 } 457 458 store_gid_entry(table, entry); 459 return 0; 460 461 done: 462 put_gid_entry(entry); 463 return ret; 464 } 465 466 /* rwlock should be read locked, or lock should be held */ 467 static int find_gid(struct ib_gid_table *table, const union ib_gid *gid, 468 const struct ib_gid_attr *val, bool default_gid, 469 unsigned long mask, int *pempty) 470 { 471 int i = 0; 472 int found = -1; 473 int empty = pempty ? -1 : 0; 474 475 while (i < table->sz && (found < 0 || empty < 0)) { 476 struct ib_gid_table_entry *data = table->data_vec[i]; 477 struct ib_gid_attr *attr; 478 int curr_index = i; 479 480 i++; 481 482 /* find_gid() is used during GID addition where it is expected 483 * to return a free entry slot which is not duplicate. 484 * Free entry slot is requested and returned if pempty is set, 485 * so lookup free slot only if requested. 486 */ 487 if (pempty && empty < 0) { 488 if (is_gid_entry_free(data) && 489 default_gid == 490 is_gid_index_default(table, curr_index)) { 491 /* 492 * Found an invalid (free) entry; allocate it. 493 * If default GID is requested, then our 494 * found slot must be one of the DEFAULT 495 * reserved slots or we fail. 496 * This ensures that only DEFAULT reserved 497 * slots are used for default property GIDs. 498 */ 499 empty = curr_index; 500 } 501 } 502 503 /* 504 * Additionally find_gid() is used to find valid entry during 505 * lookup operation; so ignore the entries which are marked as 506 * pending for removal and the entries which are marked as 507 * invalid. 508 */ 509 if (!is_gid_entry_valid(data)) 510 continue; 511 512 if (found >= 0) 513 continue; 514 515 attr = &data->attr; 516 if (mask & GID_ATTR_FIND_MASK_GID_TYPE && 517 attr->gid_type != val->gid_type) 518 continue; 519 520 if (mask & GID_ATTR_FIND_MASK_GID && 521 memcmp(gid, &data->attr.gid, sizeof(*gid))) 522 continue; 523 524 if (mask & GID_ATTR_FIND_MASK_NETDEV && 525 attr->ndev != val->ndev) 526 continue; 527 528 if (mask & GID_ATTR_FIND_MASK_DEFAULT && 529 is_gid_index_default(table, curr_index) != default_gid) 530 continue; 531 532 found = curr_index; 533 } 534 535 if (pempty) 536 *pempty = empty; 537 538 return found; 539 } 540 541 static void make_default_gid(struct net_device *dev, union ib_gid *gid) 542 { 543 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL); 544 addrconf_ifid_eui48(&gid->raw[8], dev); 545 } 546 547 static int __ib_cache_gid_add(struct ib_device *ib_dev, u32 port, 548 union ib_gid *gid, struct ib_gid_attr *attr, 549 unsigned long mask, bool default_gid) 550 { 551 struct ib_gid_table *table; 552 int ret = 0; 553 int empty; 554 int ix; 555 556 /* Do not allow adding zero GID in support of 557 * IB spec version 1.3 section 4.1.1 point (6) and 558 * section 12.7.10 and section 12.7.20 559 */ 560 if (rdma_is_zero_gid(gid)) 561 return -EINVAL; 562 563 table = rdma_gid_table(ib_dev, port); 564 565 mutex_lock(&table->lock); 566 567 ix = find_gid(table, gid, attr, default_gid, mask, &empty); 568 if (ix >= 0) 569 goto out_unlock; 570 571 if (empty < 0) { 572 ret = -ENOSPC; 573 goto out_unlock; 574 } 575 attr->device = ib_dev; 576 attr->index = empty; 577 attr->port_num = port; 578 attr->gid = *gid; 579 ret = add_modify_gid(table, attr); 580 if (!ret) 581 dispatch_gid_change_event(ib_dev, port); 582 583 out_unlock: 584 mutex_unlock(&table->lock); 585 if (ret) 586 pr_warn("%s: unable to add gid %pI6 error=%d\n", 587 __func__, gid->raw, ret); 588 return ret; 589 } 590 591 int ib_cache_gid_add(struct ib_device *ib_dev, u32 port, 592 union ib_gid *gid, struct ib_gid_attr *attr) 593 { 594 unsigned long mask = GID_ATTR_FIND_MASK_GID | 595 GID_ATTR_FIND_MASK_GID_TYPE | 596 GID_ATTR_FIND_MASK_NETDEV; 597 598 return __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false); 599 } 600 601 static int 602 _ib_cache_gid_del(struct ib_device *ib_dev, u32 port, 603 union ib_gid *gid, struct ib_gid_attr *attr, 604 unsigned long mask, bool default_gid) 605 { 606 struct ib_gid_table *table; 607 int ret = 0; 608 int ix; 609 610 table = rdma_gid_table(ib_dev, port); 611 612 mutex_lock(&table->lock); 613 614 ix = find_gid(table, gid, attr, default_gid, mask, NULL); 615 if (ix < 0) { 616 ret = -EINVAL; 617 goto out_unlock; 618 } 619 620 del_gid(ib_dev, port, table, ix); 621 dispatch_gid_change_event(ib_dev, port); 622 623 out_unlock: 624 mutex_unlock(&table->lock); 625 if (ret) 626 pr_debug("%s: can't delete gid %pI6 error=%d\n", 627 __func__, gid->raw, ret); 628 return ret; 629 } 630 631 int ib_cache_gid_del(struct ib_device *ib_dev, u32 port, 632 union ib_gid *gid, struct ib_gid_attr *attr) 633 { 634 unsigned long mask = GID_ATTR_FIND_MASK_GID | 635 GID_ATTR_FIND_MASK_GID_TYPE | 636 GID_ATTR_FIND_MASK_DEFAULT | 637 GID_ATTR_FIND_MASK_NETDEV; 638 639 return _ib_cache_gid_del(ib_dev, port, gid, attr, mask, false); 640 } 641 642 int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u32 port, 643 struct net_device *ndev) 644 { 645 struct ib_gid_table *table; 646 int ix; 647 bool deleted = false; 648 649 table = rdma_gid_table(ib_dev, port); 650 651 mutex_lock(&table->lock); 652 653 for (ix = 0; ix < table->sz; ix++) { 654 if (is_gid_entry_valid(table->data_vec[ix]) && 655 table->data_vec[ix]->attr.ndev == ndev) { 656 del_gid(ib_dev, port, table, ix); 657 deleted = true; 658 } 659 } 660 661 mutex_unlock(&table->lock); 662 663 if (deleted) 664 dispatch_gid_change_event(ib_dev, port); 665 666 return 0; 667 } 668 669 /** 670 * rdma_find_gid_by_port - Returns the GID entry attributes when it finds 671 * a valid GID entry for given search parameters. It searches for the specified 672 * GID value in the local software cache. 673 * @ib_dev: The device to query. 674 * @gid: The GID value to search for. 675 * @gid_type: The GID type to search for. 676 * @port: The port number of the device where the GID value should be searched. 677 * @ndev: In RoCE, the net device of the device. NULL means ignore. 678 * 679 * Returns sgid attributes if the GID is found with valid reference or 680 * returns ERR_PTR for the error. 681 * The caller must invoke rdma_put_gid_attr() to release the reference. 682 */ 683 const struct ib_gid_attr * 684 rdma_find_gid_by_port(struct ib_device *ib_dev, 685 const union ib_gid *gid, 686 enum ib_gid_type gid_type, 687 u32 port, struct net_device *ndev) 688 { 689 int local_index; 690 struct ib_gid_table *table; 691 unsigned long mask = GID_ATTR_FIND_MASK_GID | 692 GID_ATTR_FIND_MASK_GID_TYPE; 693 struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type}; 694 const struct ib_gid_attr *attr; 695 unsigned long flags; 696 697 if (!rdma_is_port_valid(ib_dev, port)) 698 return ERR_PTR(-ENOENT); 699 700 table = rdma_gid_table(ib_dev, port); 701 702 if (ndev) 703 mask |= GID_ATTR_FIND_MASK_NETDEV; 704 705 read_lock_irqsave(&table->rwlock, flags); 706 local_index = find_gid(table, gid, &val, false, mask, NULL); 707 if (local_index >= 0) { 708 get_gid_entry(table->data_vec[local_index]); 709 attr = &table->data_vec[local_index]->attr; 710 read_unlock_irqrestore(&table->rwlock, flags); 711 return attr; 712 } 713 714 read_unlock_irqrestore(&table->rwlock, flags); 715 return ERR_PTR(-ENOENT); 716 } 717 EXPORT_SYMBOL(rdma_find_gid_by_port); 718 719 /** 720 * rdma_find_gid_by_filter - Returns the GID table attribute where a 721 * specified GID value occurs 722 * @ib_dev: The device to query. 723 * @gid: The GID value to search for. 724 * @port: The port number of the device where the GID value could be 725 * searched. 726 * @filter: The filter function is executed on any matching GID in the table. 727 * If the filter function returns true, the corresponding index is returned, 728 * otherwise, we continue searching the GID table. It's guaranteed that 729 * while filter is executed, ndev field is valid and the structure won't 730 * change. filter is executed in an atomic context. filter must not be NULL. 731 * @context: Private data to pass into the call-back. 732 * 733 * rdma_find_gid_by_filter() searches for the specified GID value 734 * of which the filter function returns true in the port's GID table. 735 * 736 */ 737 const struct ib_gid_attr *rdma_find_gid_by_filter( 738 struct ib_device *ib_dev, const union ib_gid *gid, u32 port, 739 bool (*filter)(const union ib_gid *gid, const struct ib_gid_attr *, 740 void *), 741 void *context) 742 { 743 const struct ib_gid_attr *res = ERR_PTR(-ENOENT); 744 struct ib_gid_table *table; 745 unsigned long flags; 746 unsigned int i; 747 748 if (!rdma_is_port_valid(ib_dev, port)) 749 return ERR_PTR(-EINVAL); 750 751 table = rdma_gid_table(ib_dev, port); 752 753 read_lock_irqsave(&table->rwlock, flags); 754 for (i = 0; i < table->sz; i++) { 755 struct ib_gid_table_entry *entry = table->data_vec[i]; 756 757 if (!is_gid_entry_valid(entry)) 758 continue; 759 760 if (memcmp(gid, &entry->attr.gid, sizeof(*gid))) 761 continue; 762 763 if (filter(gid, &entry->attr, context)) { 764 get_gid_entry(entry); 765 res = &entry->attr; 766 break; 767 } 768 } 769 read_unlock_irqrestore(&table->rwlock, flags); 770 return res; 771 } 772 773 static struct ib_gid_table *alloc_gid_table(int sz) 774 { 775 struct ib_gid_table *table = kzalloc(sizeof(*table), GFP_KERNEL); 776 777 if (!table) 778 return NULL; 779 780 table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL); 781 if (!table->data_vec) 782 goto err_free_table; 783 784 mutex_init(&table->lock); 785 786 table->sz = sz; 787 rwlock_init(&table->rwlock); 788 return table; 789 790 err_free_table: 791 kfree(table); 792 return NULL; 793 } 794 795 static void release_gid_table(struct ib_device *device, 796 struct ib_gid_table *table) 797 { 798 bool leak = false; 799 int i; 800 801 if (!table) 802 return; 803 804 for (i = 0; i < table->sz; i++) { 805 if (is_gid_entry_free(table->data_vec[i])) 806 continue; 807 if (kref_read(&table->data_vec[i]->kref) > 1) { 808 dev_err(&device->dev, 809 "GID entry ref leak for index %d ref=%u\n", i, 810 kref_read(&table->data_vec[i]->kref)); 811 leak = true; 812 } 813 } 814 if (leak) 815 return; 816 817 mutex_destroy(&table->lock); 818 kfree(table->data_vec); 819 kfree(table); 820 } 821 822 static void cleanup_gid_table_port(struct ib_device *ib_dev, u32 port, 823 struct ib_gid_table *table) 824 { 825 int i; 826 827 if (!table) 828 return; 829 830 mutex_lock(&table->lock); 831 for (i = 0; i < table->sz; ++i) { 832 if (is_gid_entry_valid(table->data_vec[i])) 833 del_gid(ib_dev, port, table, i); 834 } 835 mutex_unlock(&table->lock); 836 } 837 838 void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u32 port, 839 struct net_device *ndev, 840 unsigned long gid_type_mask, 841 enum ib_cache_gid_default_mode mode) 842 { 843 union ib_gid gid = { }; 844 struct ib_gid_attr gid_attr; 845 unsigned int gid_type; 846 unsigned long mask; 847 848 mask = GID_ATTR_FIND_MASK_GID_TYPE | 849 GID_ATTR_FIND_MASK_DEFAULT | 850 GID_ATTR_FIND_MASK_NETDEV; 851 memset(&gid_attr, 0, sizeof(gid_attr)); 852 gid_attr.ndev = ndev; 853 854 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) { 855 if (1UL << gid_type & ~gid_type_mask) 856 continue; 857 858 gid_attr.gid_type = gid_type; 859 860 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) { 861 make_default_gid(ndev, &gid); 862 __ib_cache_gid_add(ib_dev, port, &gid, 863 &gid_attr, mask, true); 864 } else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) { 865 _ib_cache_gid_del(ib_dev, port, &gid, 866 &gid_attr, mask, true); 867 } 868 } 869 } 870 871 static void gid_table_reserve_default(struct ib_device *ib_dev, u32 port, 872 struct ib_gid_table *table) 873 { 874 unsigned int i; 875 unsigned long roce_gid_type_mask; 876 unsigned int num_default_gids; 877 878 roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port); 879 num_default_gids = hweight_long(roce_gid_type_mask); 880 /* Reserve starting indices for default GIDs */ 881 for (i = 0; i < num_default_gids && i < table->sz; i++) 882 table->default_gid_indices |= BIT(i); 883 } 884 885 886 static void gid_table_release_one(struct ib_device *ib_dev) 887 { 888 u32 p; 889 890 rdma_for_each_port (ib_dev, p) { 891 release_gid_table(ib_dev, ib_dev->port_data[p].cache.gid); 892 ib_dev->port_data[p].cache.gid = NULL; 893 } 894 } 895 896 static int _gid_table_setup_one(struct ib_device *ib_dev) 897 { 898 struct ib_gid_table *table; 899 u32 rdma_port; 900 901 rdma_for_each_port (ib_dev, rdma_port) { 902 table = alloc_gid_table( 903 ib_dev->port_data[rdma_port].immutable.gid_tbl_len); 904 if (!table) 905 goto rollback_table_setup; 906 907 gid_table_reserve_default(ib_dev, rdma_port, table); 908 ib_dev->port_data[rdma_port].cache.gid = table; 909 } 910 return 0; 911 912 rollback_table_setup: 913 gid_table_release_one(ib_dev); 914 return -ENOMEM; 915 } 916 917 static void gid_table_cleanup_one(struct ib_device *ib_dev) 918 { 919 u32 p; 920 921 rdma_for_each_port (ib_dev, p) 922 cleanup_gid_table_port(ib_dev, p, 923 ib_dev->port_data[p].cache.gid); 924 } 925 926 static int gid_table_setup_one(struct ib_device *ib_dev) 927 { 928 int err; 929 930 err = _gid_table_setup_one(ib_dev); 931 932 if (err) 933 return err; 934 935 rdma_roce_rescan_device(ib_dev); 936 937 return err; 938 } 939 940 /** 941 * rdma_query_gid - Read the GID content from the GID software cache 942 * @device: Device to query the GID 943 * @port_num: Port number of the device 944 * @index: Index of the GID table entry to read 945 * @gid: Pointer to GID where to store the entry's GID 946 * 947 * rdma_query_gid() only reads the GID entry content for requested device, 948 * port and index. It reads for IB, RoCE and iWarp link layers. It doesn't 949 * hold any reference to the GID table entry in the HCA or software cache. 950 * 951 * Returns 0 on success or appropriate error code. 952 * 953 */ 954 int rdma_query_gid(struct ib_device *device, u32 port_num, 955 int index, union ib_gid *gid) 956 { 957 struct ib_gid_table *table; 958 unsigned long flags; 959 int res = -EINVAL; 960 961 if (!rdma_is_port_valid(device, port_num)) 962 return -EINVAL; 963 964 table = rdma_gid_table(device, port_num); 965 read_lock_irqsave(&table->rwlock, flags); 966 967 if (index < 0 || index >= table->sz || 968 !is_gid_entry_valid(table->data_vec[index])) 969 goto done; 970 971 memcpy(gid, &table->data_vec[index]->attr.gid, sizeof(*gid)); 972 res = 0; 973 974 done: 975 read_unlock_irqrestore(&table->rwlock, flags); 976 return res; 977 } 978 EXPORT_SYMBOL(rdma_query_gid); 979 980 /** 981 * rdma_read_gid_hw_context - Read the HW GID context from GID attribute 982 * @attr: Potinter to the GID attribute 983 * 984 * rdma_read_gid_hw_context() reads the drivers GID HW context corresponding 985 * to the SGID attr. Callers are required to already be holding the reference 986 * to an existing GID entry. 987 * 988 * Returns the HW GID context 989 * 990 */ 991 void *rdma_read_gid_hw_context(const struct ib_gid_attr *attr) 992 { 993 return container_of(attr, struct ib_gid_table_entry, attr)->context; 994 } 995 EXPORT_SYMBOL(rdma_read_gid_hw_context); 996 997 /** 998 * rdma_find_gid - Returns SGID attributes if the matching GID is found. 999 * @device: The device to query. 1000 * @gid: The GID value to search for. 1001 * @gid_type: The GID type to search for. 1002 * @ndev: In RoCE, the net device of the device. NULL means ignore. 1003 * 1004 * rdma_find_gid() searches for the specified GID value in the software cache. 1005 * 1006 * Returns GID attributes if a valid GID is found or returns ERR_PTR for the 1007 * error. The caller must invoke rdma_put_gid_attr() to release the reference. 1008 * 1009 */ 1010 const struct ib_gid_attr *rdma_find_gid(struct ib_device *device, 1011 const union ib_gid *gid, 1012 enum ib_gid_type gid_type, 1013 struct net_device *ndev) 1014 { 1015 unsigned long mask = GID_ATTR_FIND_MASK_GID | 1016 GID_ATTR_FIND_MASK_GID_TYPE; 1017 struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type}; 1018 u32 p; 1019 1020 if (ndev) 1021 mask |= GID_ATTR_FIND_MASK_NETDEV; 1022 1023 rdma_for_each_port(device, p) { 1024 struct ib_gid_table *table; 1025 unsigned long flags; 1026 int index; 1027 1028 table = device->port_data[p].cache.gid; 1029 read_lock_irqsave(&table->rwlock, flags); 1030 index = find_gid(table, gid, &gid_attr_val, false, mask, NULL); 1031 if (index >= 0) { 1032 const struct ib_gid_attr *attr; 1033 1034 get_gid_entry(table->data_vec[index]); 1035 attr = &table->data_vec[index]->attr; 1036 read_unlock_irqrestore(&table->rwlock, flags); 1037 return attr; 1038 } 1039 read_unlock_irqrestore(&table->rwlock, flags); 1040 } 1041 1042 return ERR_PTR(-ENOENT); 1043 } 1044 EXPORT_SYMBOL(rdma_find_gid); 1045 1046 int ib_get_cached_pkey(struct ib_device *device, 1047 u32 port_num, 1048 int index, 1049 u16 *pkey) 1050 { 1051 struct ib_pkey_cache *cache; 1052 unsigned long flags; 1053 int ret = 0; 1054 1055 if (!rdma_is_port_valid(device, port_num)) 1056 return -EINVAL; 1057 1058 read_lock_irqsave(&device->cache_lock, flags); 1059 1060 cache = device->port_data[port_num].cache.pkey; 1061 1062 if (!cache || index < 0 || index >= cache->table_len) 1063 ret = -EINVAL; 1064 else 1065 *pkey = cache->table[index]; 1066 1067 read_unlock_irqrestore(&device->cache_lock, flags); 1068 1069 return ret; 1070 } 1071 EXPORT_SYMBOL(ib_get_cached_pkey); 1072 1073 void ib_get_cached_subnet_prefix(struct ib_device *device, u32 port_num, 1074 u64 *sn_pfx) 1075 { 1076 unsigned long flags; 1077 1078 read_lock_irqsave(&device->cache_lock, flags); 1079 *sn_pfx = device->port_data[port_num].cache.subnet_prefix; 1080 read_unlock_irqrestore(&device->cache_lock, flags); 1081 } 1082 EXPORT_SYMBOL(ib_get_cached_subnet_prefix); 1083 1084 int ib_find_cached_pkey(struct ib_device *device, u32 port_num, 1085 u16 pkey, u16 *index) 1086 { 1087 struct ib_pkey_cache *cache; 1088 unsigned long flags; 1089 int i; 1090 int ret = -ENOENT; 1091 int partial_ix = -1; 1092 1093 if (!rdma_is_port_valid(device, port_num)) 1094 return -EINVAL; 1095 1096 read_lock_irqsave(&device->cache_lock, flags); 1097 1098 cache = device->port_data[port_num].cache.pkey; 1099 if (!cache) { 1100 ret = -EINVAL; 1101 goto err; 1102 } 1103 1104 *index = -1; 1105 1106 for (i = 0; i < cache->table_len; ++i) 1107 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) { 1108 if (cache->table[i] & 0x8000) { 1109 *index = i; 1110 ret = 0; 1111 break; 1112 } else { 1113 partial_ix = i; 1114 } 1115 } 1116 1117 if (ret && partial_ix >= 0) { 1118 *index = partial_ix; 1119 ret = 0; 1120 } 1121 1122 err: 1123 read_unlock_irqrestore(&device->cache_lock, flags); 1124 1125 return ret; 1126 } 1127 EXPORT_SYMBOL(ib_find_cached_pkey); 1128 1129 int ib_find_exact_cached_pkey(struct ib_device *device, u32 port_num, 1130 u16 pkey, u16 *index) 1131 { 1132 struct ib_pkey_cache *cache; 1133 unsigned long flags; 1134 int i; 1135 int ret = -ENOENT; 1136 1137 if (!rdma_is_port_valid(device, port_num)) 1138 return -EINVAL; 1139 1140 read_lock_irqsave(&device->cache_lock, flags); 1141 1142 cache = device->port_data[port_num].cache.pkey; 1143 if (!cache) { 1144 ret = -EINVAL; 1145 goto err; 1146 } 1147 1148 *index = -1; 1149 1150 for (i = 0; i < cache->table_len; ++i) 1151 if (cache->table[i] == pkey) { 1152 *index = i; 1153 ret = 0; 1154 break; 1155 } 1156 1157 err: 1158 read_unlock_irqrestore(&device->cache_lock, flags); 1159 1160 return ret; 1161 } 1162 EXPORT_SYMBOL(ib_find_exact_cached_pkey); 1163 1164 int ib_get_cached_lmc(struct ib_device *device, u32 port_num, u8 *lmc) 1165 { 1166 unsigned long flags; 1167 int ret = 0; 1168 1169 if (!rdma_is_port_valid(device, port_num)) 1170 return -EINVAL; 1171 1172 read_lock_irqsave(&device->cache_lock, flags); 1173 *lmc = device->port_data[port_num].cache.lmc; 1174 read_unlock_irqrestore(&device->cache_lock, flags); 1175 1176 return ret; 1177 } 1178 EXPORT_SYMBOL(ib_get_cached_lmc); 1179 1180 int ib_get_cached_port_state(struct ib_device *device, u32 port_num, 1181 enum ib_port_state *port_state) 1182 { 1183 unsigned long flags; 1184 int ret = 0; 1185 1186 if (!rdma_is_port_valid(device, port_num)) 1187 return -EINVAL; 1188 1189 read_lock_irqsave(&device->cache_lock, flags); 1190 *port_state = device->port_data[port_num].cache.port_state; 1191 read_unlock_irqrestore(&device->cache_lock, flags); 1192 1193 return ret; 1194 } 1195 EXPORT_SYMBOL(ib_get_cached_port_state); 1196 1197 /** 1198 * rdma_get_gid_attr - Returns GID attributes for a port of a device 1199 * at a requested gid_index, if a valid GID entry exists. 1200 * @device: The device to query. 1201 * @port_num: The port number on the device where the GID value 1202 * is to be queried. 1203 * @index: Index of the GID table entry whose attributes are to 1204 * be queried. 1205 * 1206 * rdma_get_gid_attr() acquires reference count of gid attributes from the 1207 * cached GID table. Caller must invoke rdma_put_gid_attr() to release 1208 * reference to gid attribute regardless of link layer. 1209 * 1210 * Returns pointer to valid gid attribute or ERR_PTR for the appropriate error 1211 * code. 1212 */ 1213 const struct ib_gid_attr * 1214 rdma_get_gid_attr(struct ib_device *device, u32 port_num, int index) 1215 { 1216 const struct ib_gid_attr *attr = ERR_PTR(-ENODATA); 1217 struct ib_gid_table *table; 1218 unsigned long flags; 1219 1220 if (!rdma_is_port_valid(device, port_num)) 1221 return ERR_PTR(-EINVAL); 1222 1223 table = rdma_gid_table(device, port_num); 1224 if (index < 0 || index >= table->sz) 1225 return ERR_PTR(-EINVAL); 1226 1227 read_lock_irqsave(&table->rwlock, flags); 1228 if (!is_gid_entry_valid(table->data_vec[index])) 1229 goto done; 1230 1231 get_gid_entry(table->data_vec[index]); 1232 attr = &table->data_vec[index]->attr; 1233 done: 1234 read_unlock_irqrestore(&table->rwlock, flags); 1235 return attr; 1236 } 1237 EXPORT_SYMBOL(rdma_get_gid_attr); 1238 1239 /** 1240 * rdma_query_gid_table - Reads GID table entries of all the ports of a device up to max_entries. 1241 * @device: The device to query. 1242 * @entries: Entries where GID entries are returned. 1243 * @max_entries: Maximum number of entries that can be returned. 1244 * Entries array must be allocated to hold max_entries number of entries. 1245 * 1246 * Returns number of entries on success or appropriate error code. 1247 */ 1248 ssize_t rdma_query_gid_table(struct ib_device *device, 1249 struct ib_uverbs_gid_entry *entries, 1250 size_t max_entries) 1251 { 1252 const struct ib_gid_attr *gid_attr; 1253 ssize_t num_entries = 0, ret; 1254 struct ib_gid_table *table; 1255 u32 port_num, i; 1256 struct net_device *ndev; 1257 unsigned long flags; 1258 1259 rdma_for_each_port(device, port_num) { 1260 table = rdma_gid_table(device, port_num); 1261 read_lock_irqsave(&table->rwlock, flags); 1262 for (i = 0; i < table->sz; i++) { 1263 if (!is_gid_entry_valid(table->data_vec[i])) 1264 continue; 1265 if (num_entries >= max_entries) { 1266 ret = -EINVAL; 1267 goto err; 1268 } 1269 1270 gid_attr = &table->data_vec[i]->attr; 1271 1272 memcpy(&entries->gid, &gid_attr->gid, 1273 sizeof(gid_attr->gid)); 1274 entries->gid_index = gid_attr->index; 1275 entries->port_num = gid_attr->port_num; 1276 entries->gid_type = gid_attr->gid_type; 1277 ndev = rcu_dereference_protected( 1278 gid_attr->ndev, 1279 lockdep_is_held(&table->rwlock)); 1280 if (ndev) 1281 entries->netdev_ifindex = ndev->ifindex; 1282 1283 num_entries++; 1284 entries++; 1285 } 1286 read_unlock_irqrestore(&table->rwlock, flags); 1287 } 1288 1289 return num_entries; 1290 err: 1291 read_unlock_irqrestore(&table->rwlock, flags); 1292 return ret; 1293 } 1294 EXPORT_SYMBOL(rdma_query_gid_table); 1295 1296 /** 1297 * rdma_put_gid_attr - Release reference to the GID attribute 1298 * @attr: Pointer to the GID attribute whose reference 1299 * needs to be released. 1300 * 1301 * rdma_put_gid_attr() must be used to release reference whose 1302 * reference is acquired using rdma_get_gid_attr() or any APIs 1303 * which returns a pointer to the ib_gid_attr regardless of link layer 1304 * of IB or RoCE. 1305 * 1306 */ 1307 void rdma_put_gid_attr(const struct ib_gid_attr *attr) 1308 { 1309 struct ib_gid_table_entry *entry = 1310 container_of(attr, struct ib_gid_table_entry, attr); 1311 1312 put_gid_entry(entry); 1313 } 1314 EXPORT_SYMBOL(rdma_put_gid_attr); 1315 1316 /** 1317 * rdma_hold_gid_attr - Get reference to existing GID attribute 1318 * 1319 * @attr: Pointer to the GID attribute whose reference 1320 * needs to be taken. 1321 * 1322 * Increase the reference count to a GID attribute to keep it from being 1323 * freed. Callers are required to already be holding a reference to attribute. 1324 * 1325 */ 1326 void rdma_hold_gid_attr(const struct ib_gid_attr *attr) 1327 { 1328 struct ib_gid_table_entry *entry = 1329 container_of(attr, struct ib_gid_table_entry, attr); 1330 1331 get_gid_entry(entry); 1332 } 1333 EXPORT_SYMBOL(rdma_hold_gid_attr); 1334 1335 /** 1336 * rdma_read_gid_attr_ndev_rcu - Read GID attribute netdevice 1337 * which must be in UP state. 1338 * 1339 * @attr:Pointer to the GID attribute 1340 * 1341 * Returns pointer to netdevice if the netdevice was attached to GID and 1342 * netdevice is in UP state. Caller must hold RCU lock as this API 1343 * reads the netdev flags which can change while netdevice migrates to 1344 * different net namespace. Returns ERR_PTR with error code otherwise. 1345 * 1346 */ 1347 struct net_device *rdma_read_gid_attr_ndev_rcu(const struct ib_gid_attr *attr) 1348 { 1349 struct ib_gid_table_entry *entry = 1350 container_of(attr, struct ib_gid_table_entry, attr); 1351 struct ib_device *device = entry->attr.device; 1352 struct net_device *ndev = ERR_PTR(-EINVAL); 1353 u32 port_num = entry->attr.port_num; 1354 struct ib_gid_table *table; 1355 unsigned long flags; 1356 bool valid; 1357 1358 table = rdma_gid_table(device, port_num); 1359 1360 read_lock_irqsave(&table->rwlock, flags); 1361 valid = is_gid_entry_valid(table->data_vec[attr->index]); 1362 if (valid) { 1363 ndev = rcu_dereference(attr->ndev); 1364 if (!ndev) 1365 ndev = ERR_PTR(-ENODEV); 1366 } 1367 read_unlock_irqrestore(&table->rwlock, flags); 1368 return ndev; 1369 } 1370 EXPORT_SYMBOL(rdma_read_gid_attr_ndev_rcu); 1371 1372 static int get_lower_dev_vlan(struct net_device *lower_dev, 1373 struct netdev_nested_priv *priv) 1374 { 1375 u16 *vlan_id = (u16 *)priv->data; 1376 1377 if (is_vlan_dev(lower_dev)) 1378 *vlan_id = vlan_dev_vlan_id(lower_dev); 1379 1380 /* We are interested only in first level vlan device, so 1381 * always return 1 to stop iterating over next level devices. 1382 */ 1383 return 1; 1384 } 1385 1386 /** 1387 * rdma_read_gid_l2_fields - Read the vlan ID and source MAC address 1388 * of a GID entry. 1389 * 1390 * @attr: GID attribute pointer whose L2 fields to be read 1391 * @vlan_id: Pointer to vlan id to fill up if the GID entry has 1392 * vlan id. It is optional. 1393 * @smac: Pointer to smac to fill up for a GID entry. It is optional. 1394 * 1395 * rdma_read_gid_l2_fields() returns 0 on success and returns vlan id 1396 * (if gid entry has vlan) and source MAC, or returns error. 1397 */ 1398 int rdma_read_gid_l2_fields(const struct ib_gid_attr *attr, 1399 u16 *vlan_id, u8 *smac) 1400 { 1401 struct netdev_nested_priv priv = { 1402 .data = (void *)vlan_id, 1403 }; 1404 struct net_device *ndev; 1405 1406 rcu_read_lock(); 1407 ndev = rcu_dereference(attr->ndev); 1408 if (!ndev) { 1409 rcu_read_unlock(); 1410 return -ENODEV; 1411 } 1412 if (smac) 1413 ether_addr_copy(smac, ndev->dev_addr); 1414 if (vlan_id) { 1415 *vlan_id = 0xffff; 1416 if (is_vlan_dev(ndev)) { 1417 *vlan_id = vlan_dev_vlan_id(ndev); 1418 } else { 1419 /* If the netdev is upper device and if it's lower 1420 * device is vlan device, consider vlan id of the 1421 * the lower vlan device for this gid entry. 1422 */ 1423 netdev_walk_all_lower_dev_rcu(attr->ndev, 1424 get_lower_dev_vlan, &priv); 1425 } 1426 } 1427 rcu_read_unlock(); 1428 return 0; 1429 } 1430 EXPORT_SYMBOL(rdma_read_gid_l2_fields); 1431 1432 static int config_non_roce_gid_cache(struct ib_device *device, 1433 u32 port, struct ib_port_attr *tprops) 1434 { 1435 struct ib_gid_attr gid_attr = {}; 1436 struct ib_gid_table *table; 1437 int ret = 0; 1438 int i; 1439 1440 gid_attr.device = device; 1441 gid_attr.port_num = port; 1442 table = rdma_gid_table(device, port); 1443 1444 mutex_lock(&table->lock); 1445 for (i = 0; i < tprops->gid_tbl_len; ++i) { 1446 if (!device->ops.query_gid) 1447 continue; 1448 ret = device->ops.query_gid(device, port, i, &gid_attr.gid); 1449 if (ret) { 1450 dev_warn(&device->dev, 1451 "query_gid failed (%d) for index %d\n", ret, 1452 i); 1453 goto err; 1454 } 1455 gid_attr.index = i; 1456 tprops->subnet_prefix = 1457 be64_to_cpu(gid_attr.gid.global.subnet_prefix); 1458 add_modify_gid(table, &gid_attr); 1459 } 1460 err: 1461 mutex_unlock(&table->lock); 1462 return ret; 1463 } 1464 1465 static int 1466 ib_cache_update(struct ib_device *device, u32 port, bool update_gids, 1467 bool update_pkeys, bool enforce_security) 1468 { 1469 struct ib_port_attr *tprops = NULL; 1470 struct ib_pkey_cache *pkey_cache = NULL; 1471 struct ib_pkey_cache *old_pkey_cache = NULL; 1472 int i; 1473 int ret; 1474 1475 if (!rdma_is_port_valid(device, port)) 1476 return -EINVAL; 1477 1478 tprops = kmalloc(sizeof *tprops, GFP_KERNEL); 1479 if (!tprops) 1480 return -ENOMEM; 1481 1482 ret = ib_query_port(device, port, tprops); 1483 if (ret) { 1484 dev_warn(&device->dev, "ib_query_port failed (%d)\n", ret); 1485 goto err; 1486 } 1487 1488 if (!rdma_protocol_roce(device, port) && update_gids) { 1489 ret = config_non_roce_gid_cache(device, port, 1490 tprops); 1491 if (ret) 1492 goto err; 1493 } 1494 1495 update_pkeys &= !!tprops->pkey_tbl_len; 1496 1497 if (update_pkeys) { 1498 pkey_cache = kmalloc(struct_size(pkey_cache, table, 1499 tprops->pkey_tbl_len), 1500 GFP_KERNEL); 1501 if (!pkey_cache) { 1502 ret = -ENOMEM; 1503 goto err; 1504 } 1505 1506 pkey_cache->table_len = tprops->pkey_tbl_len; 1507 1508 for (i = 0; i < pkey_cache->table_len; ++i) { 1509 ret = ib_query_pkey(device, port, i, 1510 pkey_cache->table + i); 1511 if (ret) { 1512 dev_warn(&device->dev, 1513 "ib_query_pkey failed (%d) for index %d\n", 1514 ret, i); 1515 goto err; 1516 } 1517 } 1518 } 1519 1520 write_lock_irq(&device->cache_lock); 1521 1522 if (update_pkeys) { 1523 old_pkey_cache = device->port_data[port].cache.pkey; 1524 device->port_data[port].cache.pkey = pkey_cache; 1525 } 1526 device->port_data[port].cache.lmc = tprops->lmc; 1527 device->port_data[port].cache.port_state = tprops->state; 1528 1529 device->port_data[port].cache.subnet_prefix = tprops->subnet_prefix; 1530 write_unlock_irq(&device->cache_lock); 1531 1532 if (enforce_security) 1533 ib_security_cache_change(device, 1534 port, 1535 tprops->subnet_prefix); 1536 1537 kfree(old_pkey_cache); 1538 kfree(tprops); 1539 return 0; 1540 1541 err: 1542 kfree(pkey_cache); 1543 kfree(tprops); 1544 return ret; 1545 } 1546 1547 static void ib_cache_event_task(struct work_struct *_work) 1548 { 1549 struct ib_update_work *work = 1550 container_of(_work, struct ib_update_work, work); 1551 int ret; 1552 1553 /* Before distributing the cache update event, first sync 1554 * the cache. 1555 */ 1556 ret = ib_cache_update(work->event.device, work->event.element.port_num, 1557 work->event.event == IB_EVENT_GID_CHANGE, 1558 work->event.event == IB_EVENT_PKEY_CHANGE, 1559 work->enforce_security); 1560 1561 /* GID event is notified already for individual GID entries by 1562 * dispatch_gid_change_event(). Hence, notifiy for rest of the 1563 * events. 1564 */ 1565 if (!ret && work->event.event != IB_EVENT_GID_CHANGE) 1566 ib_dispatch_event_clients(&work->event); 1567 1568 kfree(work); 1569 } 1570 1571 static void ib_generic_event_task(struct work_struct *_work) 1572 { 1573 struct ib_update_work *work = 1574 container_of(_work, struct ib_update_work, work); 1575 1576 ib_dispatch_event_clients(&work->event); 1577 kfree(work); 1578 } 1579 1580 static bool is_cache_update_event(const struct ib_event *event) 1581 { 1582 return (event->event == IB_EVENT_PORT_ERR || 1583 event->event == IB_EVENT_PORT_ACTIVE || 1584 event->event == IB_EVENT_LID_CHANGE || 1585 event->event == IB_EVENT_PKEY_CHANGE || 1586 event->event == IB_EVENT_CLIENT_REREGISTER || 1587 event->event == IB_EVENT_GID_CHANGE); 1588 } 1589 1590 /** 1591 * ib_dispatch_event - Dispatch an asynchronous event 1592 * @event:Event to dispatch 1593 * 1594 * Low-level drivers must call ib_dispatch_event() to dispatch the 1595 * event to all registered event handlers when an asynchronous event 1596 * occurs. 1597 */ 1598 void ib_dispatch_event(const struct ib_event *event) 1599 { 1600 struct ib_update_work *work; 1601 1602 work = kzalloc(sizeof(*work), GFP_ATOMIC); 1603 if (!work) 1604 return; 1605 1606 if (is_cache_update_event(event)) 1607 INIT_WORK(&work->work, ib_cache_event_task); 1608 else 1609 INIT_WORK(&work->work, ib_generic_event_task); 1610 1611 work->event = *event; 1612 if (event->event == IB_EVENT_PKEY_CHANGE || 1613 event->event == IB_EVENT_GID_CHANGE) 1614 work->enforce_security = true; 1615 1616 queue_work(ib_wq, &work->work); 1617 } 1618 EXPORT_SYMBOL(ib_dispatch_event); 1619 1620 int ib_cache_setup_one(struct ib_device *device) 1621 { 1622 u32 p; 1623 int err; 1624 1625 err = gid_table_setup_one(device); 1626 if (err) 1627 return err; 1628 1629 rdma_for_each_port (device, p) { 1630 err = ib_cache_update(device, p, true, true, true); 1631 if (err) 1632 return err; 1633 } 1634 1635 return 0; 1636 } 1637 1638 void ib_cache_release_one(struct ib_device *device) 1639 { 1640 u32 p; 1641 1642 /* 1643 * The release function frees all the cache elements. 1644 * This function should be called as part of freeing 1645 * all the device's resources when the cache could no 1646 * longer be accessed. 1647 */ 1648 rdma_for_each_port (device, p) 1649 kfree(device->port_data[p].cache.pkey); 1650 1651 gid_table_release_one(device); 1652 } 1653 1654 void ib_cache_cleanup_one(struct ib_device *device) 1655 { 1656 /* The cleanup function waits for all in-progress workqueue 1657 * elements and cleans up the GID cache. This function should be 1658 * called after the device was removed from the devices list and 1659 * all clients were removed, so the cache exists but is 1660 * non-functional and shouldn't be updated anymore. 1661 */ 1662 flush_workqueue(ib_wq); 1663 gid_table_cleanup_one(device); 1664 1665 /* 1666 * Flush the wq second time for any pending GID delete work. 1667 */ 1668 flush_workqueue(ib_wq); 1669 } 1670