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/module.h> 37 #include <linux/errno.h> 38 #include <linux/slab.h> 39 #include <linux/workqueue.h> 40 #include <linux/netdevice.h> 41 #include <net/addrconf.h> 42 43 #include <rdma/ib_cache.h> 44 45 #include "core_priv.h" 46 47 struct ib_pkey_cache { 48 int table_len; 49 u16 table[0]; 50 }; 51 52 struct ib_update_work { 53 struct work_struct work; 54 struct ib_device *device; 55 u8 port_num; 56 bool enforce_security; 57 }; 58 59 union ib_gid zgid; 60 EXPORT_SYMBOL(zgid); 61 62 static const struct ib_gid_attr zattr; 63 64 enum gid_attr_find_mask { 65 GID_ATTR_FIND_MASK_GID = 1UL << 0, 66 GID_ATTR_FIND_MASK_NETDEV = 1UL << 1, 67 GID_ATTR_FIND_MASK_DEFAULT = 1UL << 2, 68 GID_ATTR_FIND_MASK_GID_TYPE = 1UL << 3, 69 }; 70 71 enum gid_table_entry_props { 72 GID_TABLE_ENTRY_INVALID = 1UL << 0, 73 GID_TABLE_ENTRY_DEFAULT = 1UL << 1, 74 }; 75 76 enum gid_table_write_action { 77 GID_TABLE_WRITE_ACTION_ADD, 78 GID_TABLE_WRITE_ACTION_DEL, 79 /* MODIFY only updates the GID table. Currently only used by 80 * ib_cache_update. 81 */ 82 GID_TABLE_WRITE_ACTION_MODIFY 83 }; 84 85 struct ib_gid_table_entry { 86 unsigned long props; 87 union ib_gid gid; 88 struct ib_gid_attr attr; 89 void *context; 90 }; 91 92 struct ib_gid_table { 93 int sz; 94 /* In RoCE, adding a GID to the table requires: 95 * (a) Find if this GID is already exists. 96 * (b) Find a free space. 97 * (c) Write the new GID 98 * 99 * Delete requires different set of operations: 100 * (a) Find the GID 101 * (b) Delete it. 102 * 103 * Add/delete should be carried out atomically. 104 * This is done by locking this mutex from multiple 105 * writers. We don't need this lock for IB, as the MAD 106 * layer replaces all entries. All data_vec entries 107 * are locked by this lock. 108 **/ 109 struct mutex lock; 110 /* This lock protects the table entries from being 111 * read and written simultaneously. 112 */ 113 rwlock_t rwlock; 114 struct ib_gid_table_entry *data_vec; 115 }; 116 117 static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port) 118 { 119 if (rdma_cap_roce_gid_table(ib_dev, port)) { 120 struct ib_event event; 121 122 event.device = ib_dev; 123 event.element.port_num = port; 124 event.event = IB_EVENT_GID_CHANGE; 125 126 ib_dispatch_event(&event); 127 } 128 } 129 130 static const char * const gid_type_str[] = { 131 [IB_GID_TYPE_IB] = "IB/RoCE v1", 132 [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2", 133 }; 134 135 const char *ib_cache_gid_type_str(enum ib_gid_type gid_type) 136 { 137 if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type]) 138 return gid_type_str[gid_type]; 139 140 return "Invalid GID type"; 141 } 142 EXPORT_SYMBOL(ib_cache_gid_type_str); 143 144 int ib_cache_gid_parse_type_str(const char *buf) 145 { 146 unsigned int i; 147 size_t len; 148 int err = -EINVAL; 149 150 len = strlen(buf); 151 if (len == 0) 152 return -EINVAL; 153 154 if (buf[len - 1] == '\n') 155 len--; 156 157 for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i) 158 if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) && 159 len == strlen(gid_type_str[i])) { 160 err = i; 161 break; 162 } 163 164 return err; 165 } 166 EXPORT_SYMBOL(ib_cache_gid_parse_type_str); 167 168 /* This function expects that rwlock will be write locked in all 169 * scenarios and that lock will be locked in sleep-able (RoCE) 170 * scenarios. 171 */ 172 static int write_gid(struct ib_device *ib_dev, u8 port, 173 struct ib_gid_table *table, int ix, 174 const union ib_gid *gid, 175 const struct ib_gid_attr *attr, 176 enum gid_table_write_action action, 177 bool default_gid) 178 __releases(&table->rwlock) __acquires(&table->rwlock) 179 { 180 int ret = 0; 181 struct net_device *old_net_dev; 182 enum ib_gid_type old_gid_type; 183 184 /* in rdma_cap_roce_gid_table, this funciton should be protected by a 185 * sleep-able lock. 186 */ 187 188 if (rdma_cap_roce_gid_table(ib_dev, port)) { 189 table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID; 190 write_unlock_irq(&table->rwlock); 191 /* GID_TABLE_WRITE_ACTION_MODIFY currently isn't supported by 192 * RoCE providers and thus only updates the cache. 193 */ 194 if (action == GID_TABLE_WRITE_ACTION_ADD) 195 ret = ib_dev->add_gid(ib_dev, port, ix, gid, attr, 196 &table->data_vec[ix].context); 197 else if (action == GID_TABLE_WRITE_ACTION_DEL) 198 ret = ib_dev->del_gid(ib_dev, port, ix, 199 &table->data_vec[ix].context); 200 write_lock_irq(&table->rwlock); 201 } 202 203 old_net_dev = table->data_vec[ix].attr.ndev; 204 old_gid_type = table->data_vec[ix].attr.gid_type; 205 if (old_net_dev && old_net_dev != attr->ndev) 206 dev_put(old_net_dev); 207 /* if modify_gid failed, just delete the old gid */ 208 if (ret || action == GID_TABLE_WRITE_ACTION_DEL) { 209 gid = &zgid; 210 attr = &zattr; 211 table->data_vec[ix].context = NULL; 212 } 213 214 memcpy(&table->data_vec[ix].gid, gid, sizeof(*gid)); 215 memcpy(&table->data_vec[ix].attr, attr, sizeof(*attr)); 216 if (default_gid) { 217 table->data_vec[ix].props |= GID_TABLE_ENTRY_DEFAULT; 218 if (action == GID_TABLE_WRITE_ACTION_DEL) 219 table->data_vec[ix].attr.gid_type = old_gid_type; 220 } 221 if (table->data_vec[ix].attr.ndev && 222 table->data_vec[ix].attr.ndev != old_net_dev) 223 dev_hold(table->data_vec[ix].attr.ndev); 224 225 table->data_vec[ix].props &= ~GID_TABLE_ENTRY_INVALID; 226 227 return ret; 228 } 229 230 static int add_gid(struct ib_device *ib_dev, u8 port, 231 struct ib_gid_table *table, int ix, 232 const union ib_gid *gid, 233 const struct ib_gid_attr *attr, 234 bool default_gid) { 235 return write_gid(ib_dev, port, table, ix, gid, attr, 236 GID_TABLE_WRITE_ACTION_ADD, default_gid); 237 } 238 239 static int modify_gid(struct ib_device *ib_dev, u8 port, 240 struct ib_gid_table *table, int ix, 241 const union ib_gid *gid, 242 const struct ib_gid_attr *attr, 243 bool default_gid) { 244 return write_gid(ib_dev, port, table, ix, gid, attr, 245 GID_TABLE_WRITE_ACTION_MODIFY, default_gid); 246 } 247 248 static int del_gid(struct ib_device *ib_dev, u8 port, 249 struct ib_gid_table *table, int ix, 250 bool default_gid) { 251 return write_gid(ib_dev, port, table, ix, &zgid, &zattr, 252 GID_TABLE_WRITE_ACTION_DEL, default_gid); 253 } 254 255 /* rwlock should be read locked */ 256 static int find_gid(struct ib_gid_table *table, const union ib_gid *gid, 257 const struct ib_gid_attr *val, bool default_gid, 258 unsigned long mask, int *pempty) 259 { 260 int i = 0; 261 int found = -1; 262 int empty = pempty ? -1 : 0; 263 264 while (i < table->sz && (found < 0 || empty < 0)) { 265 struct ib_gid_table_entry *data = &table->data_vec[i]; 266 struct ib_gid_attr *attr = &data->attr; 267 int curr_index = i; 268 269 i++; 270 271 if (data->props & GID_TABLE_ENTRY_INVALID) 272 continue; 273 274 if (empty < 0) 275 if (!memcmp(&data->gid, &zgid, sizeof(*gid)) && 276 !memcmp(attr, &zattr, sizeof(*attr)) && 277 !data->props) 278 empty = curr_index; 279 280 if (found >= 0) 281 continue; 282 283 if (mask & GID_ATTR_FIND_MASK_GID_TYPE && 284 attr->gid_type != val->gid_type) 285 continue; 286 287 if (mask & GID_ATTR_FIND_MASK_GID && 288 memcmp(gid, &data->gid, sizeof(*gid))) 289 continue; 290 291 if (mask & GID_ATTR_FIND_MASK_NETDEV && 292 attr->ndev != val->ndev) 293 continue; 294 295 if (mask & GID_ATTR_FIND_MASK_DEFAULT && 296 !!(data->props & GID_TABLE_ENTRY_DEFAULT) != 297 default_gid) 298 continue; 299 300 found = curr_index; 301 } 302 303 if (pempty) 304 *pempty = empty; 305 306 return found; 307 } 308 309 static void make_default_gid(struct net_device *dev, union ib_gid *gid) 310 { 311 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL); 312 addrconf_ifid_eui48(&gid->raw[8], dev); 313 } 314 315 int ib_cache_gid_add(struct ib_device *ib_dev, u8 port, 316 union ib_gid *gid, struct ib_gid_attr *attr) 317 { 318 struct ib_gid_table *table; 319 int ix; 320 int ret = 0; 321 struct net_device *idev; 322 int empty; 323 324 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 325 326 if (!memcmp(gid, &zgid, sizeof(*gid))) 327 return -EINVAL; 328 329 if (ib_dev->get_netdev) { 330 idev = ib_dev->get_netdev(ib_dev, port); 331 if (idev && attr->ndev != idev) { 332 union ib_gid default_gid; 333 334 /* Adding default GIDs in not permitted */ 335 make_default_gid(idev, &default_gid); 336 if (!memcmp(gid, &default_gid, sizeof(*gid))) { 337 dev_put(idev); 338 return -EPERM; 339 } 340 } 341 if (idev) 342 dev_put(idev); 343 } 344 345 mutex_lock(&table->lock); 346 write_lock_irq(&table->rwlock); 347 348 ix = find_gid(table, gid, attr, false, GID_ATTR_FIND_MASK_GID | 349 GID_ATTR_FIND_MASK_GID_TYPE | 350 GID_ATTR_FIND_MASK_NETDEV, &empty); 351 if (ix >= 0) 352 goto out_unlock; 353 354 if (empty < 0) { 355 ret = -ENOSPC; 356 goto out_unlock; 357 } 358 359 ret = add_gid(ib_dev, port, table, empty, gid, attr, false); 360 if (!ret) 361 dispatch_gid_change_event(ib_dev, port); 362 363 out_unlock: 364 write_unlock_irq(&table->rwlock); 365 mutex_unlock(&table->lock); 366 return ret; 367 } 368 369 int ib_cache_gid_del(struct ib_device *ib_dev, u8 port, 370 union ib_gid *gid, struct ib_gid_attr *attr) 371 { 372 struct ib_gid_table *table; 373 int ix; 374 375 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 376 377 mutex_lock(&table->lock); 378 write_lock_irq(&table->rwlock); 379 380 ix = find_gid(table, gid, attr, false, 381 GID_ATTR_FIND_MASK_GID | 382 GID_ATTR_FIND_MASK_GID_TYPE | 383 GID_ATTR_FIND_MASK_NETDEV | 384 GID_ATTR_FIND_MASK_DEFAULT, 385 NULL); 386 if (ix < 0) 387 goto out_unlock; 388 389 if (!del_gid(ib_dev, port, table, ix, false)) 390 dispatch_gid_change_event(ib_dev, port); 391 392 out_unlock: 393 write_unlock_irq(&table->rwlock); 394 mutex_unlock(&table->lock); 395 return 0; 396 } 397 398 int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port, 399 struct net_device *ndev) 400 { 401 struct ib_gid_table *table; 402 int ix; 403 bool deleted = false; 404 405 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 406 407 mutex_lock(&table->lock); 408 write_lock_irq(&table->rwlock); 409 410 for (ix = 0; ix < table->sz; ix++) 411 if (table->data_vec[ix].attr.ndev == ndev) 412 if (!del_gid(ib_dev, port, table, ix, 413 !!(table->data_vec[ix].props & 414 GID_TABLE_ENTRY_DEFAULT))) 415 deleted = true; 416 417 write_unlock_irq(&table->rwlock); 418 mutex_unlock(&table->lock); 419 420 if (deleted) 421 dispatch_gid_change_event(ib_dev, port); 422 423 return 0; 424 } 425 426 static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index, 427 union ib_gid *gid, struct ib_gid_attr *attr) 428 { 429 struct ib_gid_table *table; 430 431 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 432 433 if (index < 0 || index >= table->sz) 434 return -EINVAL; 435 436 if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID) 437 return -EAGAIN; 438 439 memcpy(gid, &table->data_vec[index].gid, sizeof(*gid)); 440 if (attr) { 441 memcpy(attr, &table->data_vec[index].attr, sizeof(*attr)); 442 if (attr->ndev) 443 dev_hold(attr->ndev); 444 } 445 446 return 0; 447 } 448 449 static int _ib_cache_gid_table_find(struct ib_device *ib_dev, 450 const union ib_gid *gid, 451 const struct ib_gid_attr *val, 452 unsigned long mask, 453 u8 *port, u16 *index) 454 { 455 struct ib_gid_table *table; 456 u8 p; 457 int local_index; 458 unsigned long flags; 459 460 for (p = 0; p < ib_dev->phys_port_cnt; p++) { 461 table = ib_dev->cache.ports[p].gid; 462 read_lock_irqsave(&table->rwlock, flags); 463 local_index = find_gid(table, gid, val, false, mask, NULL); 464 if (local_index >= 0) { 465 if (index) 466 *index = local_index; 467 if (port) 468 *port = p + rdma_start_port(ib_dev); 469 read_unlock_irqrestore(&table->rwlock, flags); 470 return 0; 471 } 472 read_unlock_irqrestore(&table->rwlock, flags); 473 } 474 475 return -ENOENT; 476 } 477 478 static int ib_cache_gid_find(struct ib_device *ib_dev, 479 const union ib_gid *gid, 480 enum ib_gid_type gid_type, 481 struct net_device *ndev, u8 *port, 482 u16 *index) 483 { 484 unsigned long mask = GID_ATTR_FIND_MASK_GID | 485 GID_ATTR_FIND_MASK_GID_TYPE; 486 struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type}; 487 488 if (ndev) 489 mask |= GID_ATTR_FIND_MASK_NETDEV; 490 491 return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val, 492 mask, port, index); 493 } 494 495 /** 496 * ib_find_cached_gid_by_port - Returns the GID table index where a specified 497 * GID value occurs. It searches for the specified GID value in the local 498 * software cache. 499 * @device: The device to query. 500 * @gid: The GID value to search for. 501 * @gid_type: The GID type to search for. 502 * @port_num: The port number of the device where the GID value should be 503 * searched. 504 * @ndev: In RoCE, the net device of the device. Null means ignore. 505 * @index: The index into the cached GID table where the GID was found. This 506 * parameter may be NULL. 507 */ 508 int ib_find_cached_gid_by_port(struct ib_device *ib_dev, 509 const union ib_gid *gid, 510 enum ib_gid_type gid_type, 511 u8 port, struct net_device *ndev, 512 u16 *index) 513 { 514 int local_index; 515 struct ib_gid_table *table; 516 unsigned long mask = GID_ATTR_FIND_MASK_GID | 517 GID_ATTR_FIND_MASK_GID_TYPE; 518 struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type}; 519 unsigned long flags; 520 521 if (!rdma_is_port_valid(ib_dev, port)) 522 return -ENOENT; 523 524 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 525 526 if (ndev) 527 mask |= GID_ATTR_FIND_MASK_NETDEV; 528 529 read_lock_irqsave(&table->rwlock, flags); 530 local_index = find_gid(table, gid, &val, false, mask, NULL); 531 if (local_index >= 0) { 532 if (index) 533 *index = local_index; 534 read_unlock_irqrestore(&table->rwlock, flags); 535 return 0; 536 } 537 538 read_unlock_irqrestore(&table->rwlock, flags); 539 return -ENOENT; 540 } 541 EXPORT_SYMBOL(ib_find_cached_gid_by_port); 542 543 /** 544 * ib_cache_gid_find_by_filter - Returns the GID table index where a specified 545 * GID value occurs 546 * @device: The device to query. 547 * @gid: The GID value to search for. 548 * @port_num: The port number of the device where the GID value could be 549 * searched. 550 * @filter: The filter function is executed on any matching GID in the table. 551 * If the filter function returns true, the corresponding index is returned, 552 * otherwise, we continue searching the GID table. It's guaranteed that 553 * while filter is executed, ndev field is valid and the structure won't 554 * change. filter is executed in an atomic context. filter must not be NULL. 555 * @index: The index into the cached GID table where the GID was found. This 556 * parameter may be NULL. 557 * 558 * ib_cache_gid_find_by_filter() searches for the specified GID value 559 * of which the filter function returns true in the port's GID table. 560 * This function is only supported on RoCE ports. 561 * 562 */ 563 static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev, 564 const union ib_gid *gid, 565 u8 port, 566 bool (*filter)(const union ib_gid *, 567 const struct ib_gid_attr *, 568 void *), 569 void *context, 570 u16 *index) 571 { 572 struct ib_gid_table *table; 573 unsigned int i; 574 unsigned long flags; 575 bool found = false; 576 577 578 if (!rdma_is_port_valid(ib_dev, port) || 579 !rdma_protocol_roce(ib_dev, port)) 580 return -EPROTONOSUPPORT; 581 582 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 583 584 read_lock_irqsave(&table->rwlock, flags); 585 for (i = 0; i < table->sz; i++) { 586 struct ib_gid_attr attr; 587 588 if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID) 589 continue; 590 591 if (memcmp(gid, &table->data_vec[i].gid, sizeof(*gid))) 592 continue; 593 594 memcpy(&attr, &table->data_vec[i].attr, sizeof(attr)); 595 596 if (filter(gid, &attr, context)) { 597 found = true; 598 if (index) 599 *index = i; 600 break; 601 } 602 } 603 read_unlock_irqrestore(&table->rwlock, flags); 604 605 if (!found) 606 return -ENOENT; 607 return 0; 608 } 609 610 static struct ib_gid_table *alloc_gid_table(int sz) 611 { 612 struct ib_gid_table *table = 613 kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL); 614 615 if (!table) 616 return NULL; 617 618 table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL); 619 if (!table->data_vec) 620 goto err_free_table; 621 622 mutex_init(&table->lock); 623 624 table->sz = sz; 625 rwlock_init(&table->rwlock); 626 627 return table; 628 629 err_free_table: 630 kfree(table); 631 return NULL; 632 } 633 634 static void release_gid_table(struct ib_gid_table *table) 635 { 636 if (table) { 637 kfree(table->data_vec); 638 kfree(table); 639 } 640 } 641 642 static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port, 643 struct ib_gid_table *table) 644 { 645 int i; 646 bool deleted = false; 647 648 if (!table) 649 return; 650 651 write_lock_irq(&table->rwlock); 652 for (i = 0; i < table->sz; ++i) { 653 if (memcmp(&table->data_vec[i].gid, &zgid, 654 sizeof(table->data_vec[i].gid))) 655 if (!del_gid(ib_dev, port, table, i, 656 table->data_vec[i].props & 657 GID_ATTR_FIND_MASK_DEFAULT)) 658 deleted = true; 659 } 660 write_unlock_irq(&table->rwlock); 661 662 if (deleted) 663 dispatch_gid_change_event(ib_dev, port); 664 } 665 666 void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port, 667 struct net_device *ndev, 668 unsigned long gid_type_mask, 669 enum ib_cache_gid_default_mode mode) 670 { 671 union ib_gid gid; 672 struct ib_gid_attr gid_attr; 673 struct ib_gid_attr zattr_type = zattr; 674 struct ib_gid_table *table; 675 unsigned int gid_type; 676 677 table = ib_dev->cache.ports[port - rdma_start_port(ib_dev)].gid; 678 679 make_default_gid(ndev, &gid); 680 memset(&gid_attr, 0, sizeof(gid_attr)); 681 gid_attr.ndev = ndev; 682 683 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) { 684 int ix; 685 union ib_gid current_gid; 686 struct ib_gid_attr current_gid_attr = {}; 687 688 if (1UL << gid_type & ~gid_type_mask) 689 continue; 690 691 gid_attr.gid_type = gid_type; 692 693 mutex_lock(&table->lock); 694 write_lock_irq(&table->rwlock); 695 ix = find_gid(table, NULL, &gid_attr, true, 696 GID_ATTR_FIND_MASK_GID_TYPE | 697 GID_ATTR_FIND_MASK_DEFAULT, 698 NULL); 699 700 /* Coudn't find default GID location */ 701 if (WARN_ON(ix < 0)) 702 goto release; 703 704 zattr_type.gid_type = gid_type; 705 706 if (!__ib_cache_gid_get(ib_dev, port, ix, 707 ¤t_gid, ¤t_gid_attr) && 708 mode == IB_CACHE_GID_DEFAULT_MODE_SET && 709 !memcmp(&gid, ¤t_gid, sizeof(gid)) && 710 !memcmp(&gid_attr, ¤t_gid_attr, sizeof(gid_attr))) 711 goto release; 712 713 if (memcmp(¤t_gid, &zgid, sizeof(current_gid)) || 714 memcmp(¤t_gid_attr, &zattr_type, 715 sizeof(current_gid_attr))) { 716 if (del_gid(ib_dev, port, table, ix, true)) { 717 pr_warn("ib_cache_gid: can't delete index %d for default gid %pI6\n", 718 ix, gid.raw); 719 goto release; 720 } else { 721 dispatch_gid_change_event(ib_dev, port); 722 } 723 } 724 725 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) { 726 if (add_gid(ib_dev, port, table, ix, &gid, &gid_attr, true)) 727 pr_warn("ib_cache_gid: unable to add default gid %pI6\n", 728 gid.raw); 729 else 730 dispatch_gid_change_event(ib_dev, port); 731 } 732 733 release: 734 if (current_gid_attr.ndev) 735 dev_put(current_gid_attr.ndev); 736 write_unlock_irq(&table->rwlock); 737 mutex_unlock(&table->lock); 738 } 739 } 740 741 static int gid_table_reserve_default(struct ib_device *ib_dev, u8 port, 742 struct ib_gid_table *table) 743 { 744 unsigned int i; 745 unsigned long roce_gid_type_mask; 746 unsigned int num_default_gids; 747 unsigned int current_gid = 0; 748 749 roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port); 750 num_default_gids = hweight_long(roce_gid_type_mask); 751 for (i = 0; i < num_default_gids && i < table->sz; i++) { 752 struct ib_gid_table_entry *entry = 753 &table->data_vec[i]; 754 755 entry->props |= GID_TABLE_ENTRY_DEFAULT; 756 current_gid = find_next_bit(&roce_gid_type_mask, 757 BITS_PER_LONG, 758 current_gid); 759 entry->attr.gid_type = current_gid++; 760 } 761 762 return 0; 763 } 764 765 static int _gid_table_setup_one(struct ib_device *ib_dev) 766 { 767 u8 port; 768 struct ib_gid_table *table; 769 int err = 0; 770 771 for (port = 0; port < ib_dev->phys_port_cnt; port++) { 772 u8 rdma_port = port + rdma_start_port(ib_dev); 773 774 table = 775 alloc_gid_table( 776 ib_dev->port_immutable[rdma_port].gid_tbl_len); 777 if (!table) { 778 err = -ENOMEM; 779 goto rollback_table_setup; 780 } 781 782 err = gid_table_reserve_default(ib_dev, 783 port + rdma_start_port(ib_dev), 784 table); 785 if (err) 786 goto rollback_table_setup; 787 ib_dev->cache.ports[port].gid = table; 788 } 789 790 return 0; 791 792 rollback_table_setup: 793 for (port = 0; port < ib_dev->phys_port_cnt; port++) { 794 table = ib_dev->cache.ports[port].gid; 795 796 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev), 797 table); 798 release_gid_table(table); 799 } 800 801 return err; 802 } 803 804 static void gid_table_release_one(struct ib_device *ib_dev) 805 { 806 struct ib_gid_table *table; 807 u8 port; 808 809 for (port = 0; port < ib_dev->phys_port_cnt; port++) { 810 table = ib_dev->cache.ports[port].gid; 811 release_gid_table(table); 812 ib_dev->cache.ports[port].gid = NULL; 813 } 814 } 815 816 static void gid_table_cleanup_one(struct ib_device *ib_dev) 817 { 818 struct ib_gid_table *table; 819 u8 port; 820 821 for (port = 0; port < ib_dev->phys_port_cnt; port++) { 822 table = ib_dev->cache.ports[port].gid; 823 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev), 824 table); 825 } 826 } 827 828 static int gid_table_setup_one(struct ib_device *ib_dev) 829 { 830 int err; 831 832 err = _gid_table_setup_one(ib_dev); 833 834 if (err) 835 return err; 836 837 rdma_roce_rescan_device(ib_dev); 838 839 return err; 840 } 841 842 int ib_get_cached_gid(struct ib_device *device, 843 u8 port_num, 844 int index, 845 union ib_gid *gid, 846 struct ib_gid_attr *gid_attr) 847 { 848 int res; 849 unsigned long flags; 850 struct ib_gid_table *table; 851 852 if (!rdma_is_port_valid(device, port_num)) 853 return -EINVAL; 854 855 table = device->cache.ports[port_num - rdma_start_port(device)].gid; 856 read_lock_irqsave(&table->rwlock, flags); 857 res = __ib_cache_gid_get(device, port_num, index, gid, gid_attr); 858 read_unlock_irqrestore(&table->rwlock, flags); 859 860 return res; 861 } 862 EXPORT_SYMBOL(ib_get_cached_gid); 863 864 /** 865 * ib_find_cached_gid - Returns the port number and GID table index where 866 * a specified GID value occurs. 867 * @device: The device to query. 868 * @gid: The GID value to search for. 869 * @gid_type: The GID type to search for. 870 * @ndev: In RoCE, the net device of the device. NULL means ignore. 871 * @port_num: The port number of the device where the GID value was found. 872 * @index: The index into the cached GID table where the GID was found. This 873 * parameter may be NULL. 874 * 875 * ib_find_cached_gid() searches for the specified GID value in 876 * the local software cache. 877 */ 878 int ib_find_cached_gid(struct ib_device *device, 879 const union ib_gid *gid, 880 enum ib_gid_type gid_type, 881 struct net_device *ndev, 882 u8 *port_num, 883 u16 *index) 884 { 885 return ib_cache_gid_find(device, gid, gid_type, ndev, port_num, index); 886 } 887 EXPORT_SYMBOL(ib_find_cached_gid); 888 889 int ib_find_gid_by_filter(struct ib_device *device, 890 const union ib_gid *gid, 891 u8 port_num, 892 bool (*filter)(const union ib_gid *gid, 893 const struct ib_gid_attr *, 894 void *), 895 void *context, u16 *index) 896 { 897 /* Only RoCE GID table supports filter function */ 898 if (!rdma_cap_roce_gid_table(device, port_num) && filter) 899 return -EPROTONOSUPPORT; 900 901 return ib_cache_gid_find_by_filter(device, gid, 902 port_num, filter, 903 context, index); 904 } 905 906 int ib_get_cached_pkey(struct ib_device *device, 907 u8 port_num, 908 int index, 909 u16 *pkey) 910 { 911 struct ib_pkey_cache *cache; 912 unsigned long flags; 913 int ret = 0; 914 915 if (!rdma_is_port_valid(device, port_num)) 916 return -EINVAL; 917 918 read_lock_irqsave(&device->cache.lock, flags); 919 920 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey; 921 922 if (index < 0 || index >= cache->table_len) 923 ret = -EINVAL; 924 else 925 *pkey = cache->table[index]; 926 927 read_unlock_irqrestore(&device->cache.lock, flags); 928 929 return ret; 930 } 931 EXPORT_SYMBOL(ib_get_cached_pkey); 932 933 int ib_get_cached_subnet_prefix(struct ib_device *device, 934 u8 port_num, 935 u64 *sn_pfx) 936 { 937 unsigned long flags; 938 int p; 939 940 if (!rdma_is_port_valid(device, port_num)) 941 return -EINVAL; 942 943 p = port_num - rdma_start_port(device); 944 read_lock_irqsave(&device->cache.lock, flags); 945 *sn_pfx = device->cache.ports[p].subnet_prefix; 946 read_unlock_irqrestore(&device->cache.lock, flags); 947 948 return 0; 949 } 950 EXPORT_SYMBOL(ib_get_cached_subnet_prefix); 951 952 int ib_find_cached_pkey(struct ib_device *device, 953 u8 port_num, 954 u16 pkey, 955 u16 *index) 956 { 957 struct ib_pkey_cache *cache; 958 unsigned long flags; 959 int i; 960 int ret = -ENOENT; 961 int partial_ix = -1; 962 963 if (!rdma_is_port_valid(device, port_num)) 964 return -EINVAL; 965 966 read_lock_irqsave(&device->cache.lock, flags); 967 968 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey; 969 970 *index = -1; 971 972 for (i = 0; i < cache->table_len; ++i) 973 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) { 974 if (cache->table[i] & 0x8000) { 975 *index = i; 976 ret = 0; 977 break; 978 } else 979 partial_ix = i; 980 } 981 982 if (ret && partial_ix >= 0) { 983 *index = partial_ix; 984 ret = 0; 985 } 986 987 read_unlock_irqrestore(&device->cache.lock, flags); 988 989 return ret; 990 } 991 EXPORT_SYMBOL(ib_find_cached_pkey); 992 993 int ib_find_exact_cached_pkey(struct ib_device *device, 994 u8 port_num, 995 u16 pkey, 996 u16 *index) 997 { 998 struct ib_pkey_cache *cache; 999 unsigned long flags; 1000 int i; 1001 int ret = -ENOENT; 1002 1003 if (!rdma_is_port_valid(device, port_num)) 1004 return -EINVAL; 1005 1006 read_lock_irqsave(&device->cache.lock, flags); 1007 1008 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey; 1009 1010 *index = -1; 1011 1012 for (i = 0; i < cache->table_len; ++i) 1013 if (cache->table[i] == pkey) { 1014 *index = i; 1015 ret = 0; 1016 break; 1017 } 1018 1019 read_unlock_irqrestore(&device->cache.lock, flags); 1020 1021 return ret; 1022 } 1023 EXPORT_SYMBOL(ib_find_exact_cached_pkey); 1024 1025 int ib_get_cached_lmc(struct ib_device *device, 1026 u8 port_num, 1027 u8 *lmc) 1028 { 1029 unsigned long flags; 1030 int ret = 0; 1031 1032 if (!rdma_is_port_valid(device, port_num)) 1033 return -EINVAL; 1034 1035 read_lock_irqsave(&device->cache.lock, flags); 1036 *lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc; 1037 read_unlock_irqrestore(&device->cache.lock, flags); 1038 1039 return ret; 1040 } 1041 EXPORT_SYMBOL(ib_get_cached_lmc); 1042 1043 int ib_get_cached_port_state(struct ib_device *device, 1044 u8 port_num, 1045 enum ib_port_state *port_state) 1046 { 1047 unsigned long flags; 1048 int ret = 0; 1049 1050 if (!rdma_is_port_valid(device, port_num)) 1051 return -EINVAL; 1052 1053 read_lock_irqsave(&device->cache.lock, flags); 1054 *port_state = device->cache.ports[port_num 1055 - rdma_start_port(device)].port_state; 1056 read_unlock_irqrestore(&device->cache.lock, flags); 1057 1058 return ret; 1059 } 1060 EXPORT_SYMBOL(ib_get_cached_port_state); 1061 1062 static void ib_cache_update(struct ib_device *device, 1063 u8 port, 1064 bool enforce_security) 1065 { 1066 struct ib_port_attr *tprops = NULL; 1067 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache; 1068 struct ib_gid_cache { 1069 int table_len; 1070 union ib_gid table[0]; 1071 } *gid_cache = NULL; 1072 int i; 1073 int ret; 1074 struct ib_gid_table *table; 1075 bool use_roce_gid_table = 1076 rdma_cap_roce_gid_table(device, port); 1077 1078 if (!rdma_is_port_valid(device, port)) 1079 return; 1080 1081 table = device->cache.ports[port - rdma_start_port(device)].gid; 1082 1083 tprops = kmalloc(sizeof *tprops, GFP_KERNEL); 1084 if (!tprops) 1085 return; 1086 1087 ret = ib_query_port(device, port, tprops); 1088 if (ret) { 1089 pr_warn("ib_query_port failed (%d) for %s\n", 1090 ret, device->name); 1091 goto err; 1092 } 1093 1094 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len * 1095 sizeof *pkey_cache->table, GFP_KERNEL); 1096 if (!pkey_cache) 1097 goto err; 1098 1099 pkey_cache->table_len = tprops->pkey_tbl_len; 1100 1101 if (!use_roce_gid_table) { 1102 gid_cache = kmalloc(sizeof(*gid_cache) + tprops->gid_tbl_len * 1103 sizeof(*gid_cache->table), GFP_KERNEL); 1104 if (!gid_cache) 1105 goto err; 1106 1107 gid_cache->table_len = tprops->gid_tbl_len; 1108 } 1109 1110 for (i = 0; i < pkey_cache->table_len; ++i) { 1111 ret = ib_query_pkey(device, port, i, pkey_cache->table + i); 1112 if (ret) { 1113 pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n", 1114 ret, device->name, i); 1115 goto err; 1116 } 1117 } 1118 1119 if (!use_roce_gid_table) { 1120 for (i = 0; i < gid_cache->table_len; ++i) { 1121 ret = ib_query_gid(device, port, i, 1122 gid_cache->table + i, NULL); 1123 if (ret) { 1124 pr_warn("ib_query_gid failed (%d) for %s (index %d)\n", 1125 ret, device->name, i); 1126 goto err; 1127 } 1128 } 1129 } 1130 1131 write_lock_irq(&device->cache.lock); 1132 1133 old_pkey_cache = device->cache.ports[port - 1134 rdma_start_port(device)].pkey; 1135 1136 device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache; 1137 if (!use_roce_gid_table) { 1138 write_lock(&table->rwlock); 1139 for (i = 0; i < gid_cache->table_len; i++) { 1140 modify_gid(device, port, table, i, gid_cache->table + i, 1141 &zattr, false); 1142 } 1143 write_unlock(&table->rwlock); 1144 } 1145 1146 device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc; 1147 device->cache.ports[port - rdma_start_port(device)].port_state = 1148 tprops->state; 1149 1150 device->cache.ports[port - rdma_start_port(device)].subnet_prefix = 1151 tprops->subnet_prefix; 1152 write_unlock_irq(&device->cache.lock); 1153 1154 if (enforce_security) 1155 ib_security_cache_change(device, 1156 port, 1157 tprops->subnet_prefix); 1158 1159 kfree(gid_cache); 1160 kfree(old_pkey_cache); 1161 kfree(tprops); 1162 return; 1163 1164 err: 1165 kfree(pkey_cache); 1166 kfree(gid_cache); 1167 kfree(tprops); 1168 } 1169 1170 static void ib_cache_task(struct work_struct *_work) 1171 { 1172 struct ib_update_work *work = 1173 container_of(_work, struct ib_update_work, work); 1174 1175 ib_cache_update(work->device, 1176 work->port_num, 1177 work->enforce_security); 1178 kfree(work); 1179 } 1180 1181 static void ib_cache_event(struct ib_event_handler *handler, 1182 struct ib_event *event) 1183 { 1184 struct ib_update_work *work; 1185 1186 if (event->event == IB_EVENT_PORT_ERR || 1187 event->event == IB_EVENT_PORT_ACTIVE || 1188 event->event == IB_EVENT_LID_CHANGE || 1189 event->event == IB_EVENT_PKEY_CHANGE || 1190 event->event == IB_EVENT_SM_CHANGE || 1191 event->event == IB_EVENT_CLIENT_REREGISTER || 1192 event->event == IB_EVENT_GID_CHANGE) { 1193 work = kmalloc(sizeof *work, GFP_ATOMIC); 1194 if (work) { 1195 INIT_WORK(&work->work, ib_cache_task); 1196 work->device = event->device; 1197 work->port_num = event->element.port_num; 1198 if (event->event == IB_EVENT_PKEY_CHANGE || 1199 event->event == IB_EVENT_GID_CHANGE) 1200 work->enforce_security = true; 1201 else 1202 work->enforce_security = false; 1203 1204 queue_work(ib_wq, &work->work); 1205 } 1206 } 1207 } 1208 1209 int ib_cache_setup_one(struct ib_device *device) 1210 { 1211 int p; 1212 int err; 1213 1214 rwlock_init(&device->cache.lock); 1215 1216 device->cache.ports = 1217 kzalloc(sizeof(*device->cache.ports) * 1218 (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL); 1219 if (!device->cache.ports) 1220 return -ENOMEM; 1221 1222 err = gid_table_setup_one(device); 1223 if (err) { 1224 kfree(device->cache.ports); 1225 device->cache.ports = NULL; 1226 return err; 1227 } 1228 1229 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p) 1230 ib_cache_update(device, p + rdma_start_port(device), true); 1231 1232 INIT_IB_EVENT_HANDLER(&device->cache.event_handler, 1233 device, ib_cache_event); 1234 ib_register_event_handler(&device->cache.event_handler); 1235 return 0; 1236 } 1237 1238 void ib_cache_release_one(struct ib_device *device) 1239 { 1240 int p; 1241 1242 /* 1243 * The release function frees all the cache elements. 1244 * This function should be called as part of freeing 1245 * all the device's resources when the cache could no 1246 * longer be accessed. 1247 */ 1248 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p) 1249 kfree(device->cache.ports[p].pkey); 1250 1251 gid_table_release_one(device); 1252 kfree(device->cache.ports); 1253 } 1254 1255 void ib_cache_cleanup_one(struct ib_device *device) 1256 { 1257 /* The cleanup function unregisters the event handler, 1258 * waits for all in-progress workqueue elements and cleans 1259 * up the GID cache. This function should be called after 1260 * the device was removed from the devices list and all 1261 * clients were removed, so the cache exists but is 1262 * non-functional and shouldn't be updated anymore. 1263 */ 1264 ib_unregister_event_handler(&device->cache.event_handler); 1265 flush_workqueue(ib_wq); 1266 gid_table_cleanup_one(device); 1267 } 1268 1269 void __init ib_cache_setup(void) 1270 { 1271 roce_gid_mgmt_init(); 1272 } 1273 1274 void __exit ib_cache_cleanup(void) 1275 { 1276 roce_gid_mgmt_cleanup(); 1277 } 1278