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.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
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.
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
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>
43 #include <rdma/ib_cache.h>
45 #include "core_priv.h"
47 struct ib_pkey_cache {
52 struct ib_update_work {
53 struct work_struct work;
54 struct ib_device *device;
56 bool enforce_security;
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,
69 enum gid_table_entry_props {
70 GID_TABLE_ENTRY_INVALID = 1UL << 0,
71 GID_TABLE_ENTRY_DEFAULT = 1UL << 1,
74 struct ib_gid_table_entry {
77 struct ib_gid_attr attr;
83 /* In RoCE, adding a GID to the table requires:
84 * (a) Find if this GID is already exists.
85 * (b) Find a free space.
86 * (c) Write the new GID
88 * Delete requires different set of operations:
93 /* Any writer to data_vec must hold this lock and the write side of
94 * rwlock. readers must hold only rwlock. All writers must be in a
98 /* rwlock protects data_vec[ix]->props. */
100 struct ib_gid_table_entry *data_vec;
103 static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
105 struct ib_event event;
107 event.device = ib_dev;
108 event.element.port_num = port;
109 event.event = IB_EVENT_GID_CHANGE;
111 ib_dispatch_event(&event);
114 static const char * const gid_type_str[] = {
115 [IB_GID_TYPE_IB] = "IB/RoCE v1",
116 [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2",
119 const char *ib_cache_gid_type_str(enum ib_gid_type gid_type)
121 if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type])
122 return gid_type_str[gid_type];
124 return "Invalid GID type";
126 EXPORT_SYMBOL(ib_cache_gid_type_str);
128 /** rdma_is_zero_gid - Check if given GID is zero or not.
130 * Returns true if given GID is zero, returns false otherwise.
132 bool rdma_is_zero_gid(const union ib_gid *gid)
134 return !memcmp(gid, &zgid, sizeof(*gid));
136 EXPORT_SYMBOL(rdma_is_zero_gid);
138 int ib_cache_gid_parse_type_str(const char *buf)
148 if (buf[len - 1] == '\n')
151 for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i)
152 if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) &&
153 len == strlen(gid_type_str[i])) {
160 EXPORT_SYMBOL(ib_cache_gid_parse_type_str);
162 static struct ib_gid_table *rdma_gid_table(struct ib_device *device, u8 port)
164 return device->cache.ports[port - rdma_start_port(device)].gid;
167 static void del_roce_gid(struct ib_device *device, u8 port_num,
168 struct ib_gid_table *table, int ix)
170 pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
171 device->name, port_num, ix,
172 table->data_vec[ix].gid.raw);
174 if (rdma_cap_roce_gid_table(device, port_num))
175 device->del_gid(&table->data_vec[ix].attr,
176 &table->data_vec[ix].context);
177 dev_put(table->data_vec[ix].attr.ndev);
180 static int add_roce_gid(struct ib_gid_table *table,
181 const union ib_gid *gid,
182 const struct ib_gid_attr *attr)
184 struct ib_gid_table_entry *entry;
185 int ix = attr->index;
189 pr_err("%s NULL netdev device=%s port=%d index=%d\n",
190 __func__, attr->device->name, attr->port_num,
195 entry = &table->data_vec[ix];
196 if ((entry->props & GID_TABLE_ENTRY_INVALID) == 0) {
197 WARN(1, "GID table corruption device=%s port=%d index=%d\n",
198 attr->device->name, attr->port_num,
203 if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) {
204 ret = attr->device->add_gid(gid, attr, &entry->context);
206 pr_err("%s GID add failed device=%s port=%d index=%d\n",
207 __func__, attr->device->name, attr->port_num,
212 dev_hold(attr->ndev);
216 pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
217 attr->device->name, attr->port_num, ix, gid->raw);
222 * add_modify_gid - Add or modify GID table entry
224 * @table: GID table in which GID to be added or modified
226 * @attr: Attributes of the GID
228 * Returns 0 on success or appropriate error code. It accepts zero
229 * GID addition for non RoCE ports for HCA's who report them as valid
230 * GID. However such zero GIDs are not added to the cache.
232 static int add_modify_gid(struct ib_gid_table *table,
233 const union ib_gid *gid,
234 const struct ib_gid_attr *attr)
238 if (rdma_protocol_roce(attr->device, attr->port_num)) {
239 ret = add_roce_gid(table, gid, attr);
244 * Some HCA's report multiple GID entries with only one
245 * valid GID, but remaining as zero GID.
246 * So ignore such behavior for IB link layer and don't
247 * fail the call, but don't add such entry to GID cache.
249 if (rdma_is_zero_gid(gid))
253 lockdep_assert_held(&table->lock);
254 memcpy(&table->data_vec[attr->index].gid, gid, sizeof(*gid));
255 memcpy(&table->data_vec[attr->index].attr, attr, sizeof(*attr));
257 write_lock_irq(&table->rwlock);
258 table->data_vec[attr->index].props &= ~GID_TABLE_ENTRY_INVALID;
259 write_unlock_irq(&table->rwlock);
264 * del_gid - Delete GID table entry
266 * @ib_dev: IB device whose GID entry to be deleted
267 * @port: Port number of the IB device
268 * @table: GID table of the IB device for a port
269 * @ix: GID entry index to delete
272 static void del_gid(struct ib_device *ib_dev, u8 port,
273 struct ib_gid_table *table, int ix)
275 lockdep_assert_held(&table->lock);
276 write_lock_irq(&table->rwlock);
277 table->data_vec[ix].props |= GID_TABLE_ENTRY_INVALID;
278 write_unlock_irq(&table->rwlock);
280 if (rdma_protocol_roce(ib_dev, port))
281 del_roce_gid(ib_dev, port, table, ix);
282 memset(&table->data_vec[ix].gid, 0, sizeof(table->data_vec[ix].gid));
283 memset(&table->data_vec[ix].attr, 0, sizeof(table->data_vec[ix].attr));
284 table->data_vec[ix].context = NULL;
287 /* rwlock should be read locked, or lock should be held */
288 static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
289 const struct ib_gid_attr *val, bool default_gid,
290 unsigned long mask, int *pempty)
294 int empty = pempty ? -1 : 0;
296 while (i < table->sz && (found < 0 || empty < 0)) {
297 struct ib_gid_table_entry *data = &table->data_vec[i];
298 struct ib_gid_attr *attr = &data->attr;
303 /* find_gid() is used during GID addition where it is expected
304 * to return a free entry slot which is not duplicate.
305 * Free entry slot is requested and returned if pempty is set,
306 * so lookup free slot only if requested.
308 if (pempty && empty < 0) {
309 if (data->props & GID_TABLE_ENTRY_INVALID &&
311 !!(data->props & GID_TABLE_ENTRY_DEFAULT))) {
313 * Found an invalid (free) entry; allocate it.
314 * If default GID is requested, then our
315 * found slot must be one of the DEFAULT
316 * reserved slots or we fail.
317 * This ensures that only DEFAULT reserved
318 * slots are used for default property GIDs.
325 * Additionally find_gid() is used to find valid entry during
326 * lookup operation, where validity needs to be checked. So
327 * find the empty entry first to continue to search for a free
328 * slot and ignore its INVALID flag.
330 if (data->props & GID_TABLE_ENTRY_INVALID)
336 if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
337 attr->gid_type != val->gid_type)
340 if (mask & GID_ATTR_FIND_MASK_GID &&
341 memcmp(gid, &data->gid, sizeof(*gid)))
344 if (mask & GID_ATTR_FIND_MASK_NETDEV &&
345 attr->ndev != val->ndev)
348 if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
349 !!(data->props & GID_TABLE_ENTRY_DEFAULT) !=
362 static void make_default_gid(struct net_device *dev, union ib_gid *gid)
364 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
365 addrconf_ifid_eui48(&gid->raw[8], dev);
368 static int __ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
369 union ib_gid *gid, struct ib_gid_attr *attr,
370 unsigned long mask, bool default_gid)
372 struct ib_gid_table *table;
377 /* Do not allow adding zero GID in support of
378 * IB spec version 1.3 section 4.1.1 point (6) and
379 * section 12.7.10 and section 12.7.20
381 if (rdma_is_zero_gid(gid))
384 table = rdma_gid_table(ib_dev, port);
386 mutex_lock(&table->lock);
388 ix = find_gid(table, gid, attr, default_gid, mask, &empty);
396 attr->device = ib_dev;
398 attr->port_num = port;
399 ret = add_modify_gid(table, gid, attr);
401 dispatch_gid_change_event(ib_dev, port);
404 mutex_unlock(&table->lock);
406 pr_warn("%s: unable to add gid %pI6 error=%d\n",
407 __func__, gid->raw, ret);
411 int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
412 union ib_gid *gid, struct ib_gid_attr *attr)
414 struct net_device *idev;
418 if (ib_dev->get_netdev) {
419 idev = ib_dev->get_netdev(ib_dev, port);
420 if (idev && attr->ndev != idev) {
421 union ib_gid default_gid;
423 /* Adding default GIDs in not permitted */
424 make_default_gid(idev, &default_gid);
425 if (!memcmp(gid, &default_gid, sizeof(*gid))) {
434 mask = GID_ATTR_FIND_MASK_GID |
435 GID_ATTR_FIND_MASK_GID_TYPE |
436 GID_ATTR_FIND_MASK_NETDEV;
438 ret = __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false);
443 _ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
444 union ib_gid *gid, struct ib_gid_attr *attr,
445 unsigned long mask, bool default_gid)
447 struct ib_gid_table *table;
451 table = rdma_gid_table(ib_dev, port);
453 mutex_lock(&table->lock);
455 ix = find_gid(table, gid, attr, default_gid, mask, NULL);
461 del_gid(ib_dev, port, table, ix);
462 dispatch_gid_change_event(ib_dev, port);
465 mutex_unlock(&table->lock);
467 pr_debug("%s: can't delete gid %pI6 error=%d\n",
468 __func__, gid->raw, ret);
472 int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
473 union ib_gid *gid, struct ib_gid_attr *attr)
475 unsigned long mask = GID_ATTR_FIND_MASK_GID |
476 GID_ATTR_FIND_MASK_GID_TYPE |
477 GID_ATTR_FIND_MASK_DEFAULT |
478 GID_ATTR_FIND_MASK_NETDEV;
480 return _ib_cache_gid_del(ib_dev, port, gid, attr, mask, false);
483 int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
484 struct net_device *ndev)
486 struct ib_gid_table *table;
488 bool deleted = false;
490 table = rdma_gid_table(ib_dev, port);
492 mutex_lock(&table->lock);
494 for (ix = 0; ix < table->sz; ix++) {
495 if (table->data_vec[ix].attr.ndev == ndev) {
496 del_gid(ib_dev, port, table, ix);
501 mutex_unlock(&table->lock);
504 dispatch_gid_change_event(ib_dev, port);
509 static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
510 union ib_gid *gid, struct ib_gid_attr *attr)
512 struct ib_gid_table *table;
514 table = rdma_gid_table(ib_dev, port);
516 if (index < 0 || index >= table->sz)
519 if (table->data_vec[index].props & GID_TABLE_ENTRY_INVALID)
522 memcpy(gid, &table->data_vec[index].gid, sizeof(*gid));
524 memcpy(attr, &table->data_vec[index].attr, sizeof(*attr));
526 dev_hold(attr->ndev);
532 static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
533 const union ib_gid *gid,
534 const struct ib_gid_attr *val,
536 u8 *port, u16 *index)
538 struct ib_gid_table *table;
543 for (p = 0; p < ib_dev->phys_port_cnt; p++) {
544 table = ib_dev->cache.ports[p].gid;
545 read_lock_irqsave(&table->rwlock, flags);
546 local_index = find_gid(table, gid, val, false, mask, NULL);
547 if (local_index >= 0) {
549 *index = local_index;
551 *port = p + rdma_start_port(ib_dev);
552 read_unlock_irqrestore(&table->rwlock, flags);
555 read_unlock_irqrestore(&table->rwlock, flags);
561 static int ib_cache_gid_find(struct ib_device *ib_dev,
562 const union ib_gid *gid,
563 enum ib_gid_type gid_type,
564 struct net_device *ndev, u8 *port,
567 unsigned long mask = GID_ATTR_FIND_MASK_GID |
568 GID_ATTR_FIND_MASK_GID_TYPE;
569 struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type};
572 mask |= GID_ATTR_FIND_MASK_NETDEV;
574 return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
579 * ib_find_cached_gid_by_port - Returns the GID table index where a specified
580 * GID value occurs. It searches for the specified GID value in the local
582 * @device: The device to query.
583 * @gid: The GID value to search for.
584 * @gid_type: The GID type to search for.
585 * @port_num: The port number of the device where the GID value should be
587 * @ndev: In RoCE, the net device of the device. Null means ignore.
588 * @index: The index into the cached GID table where the GID was found. This
589 * parameter may be NULL.
591 int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
592 const union ib_gid *gid,
593 enum ib_gid_type gid_type,
594 u8 port, struct net_device *ndev,
598 struct ib_gid_table *table;
599 unsigned long mask = GID_ATTR_FIND_MASK_GID |
600 GID_ATTR_FIND_MASK_GID_TYPE;
601 struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
604 if (!rdma_is_port_valid(ib_dev, port))
607 table = rdma_gid_table(ib_dev, port);
610 mask |= GID_ATTR_FIND_MASK_NETDEV;
612 read_lock_irqsave(&table->rwlock, flags);
613 local_index = find_gid(table, gid, &val, false, mask, NULL);
614 if (local_index >= 0) {
616 *index = local_index;
617 read_unlock_irqrestore(&table->rwlock, flags);
621 read_unlock_irqrestore(&table->rwlock, flags);
624 EXPORT_SYMBOL(ib_find_cached_gid_by_port);
627 * ib_cache_gid_find_by_filter - Returns the GID table index where a specified
629 * @device: The device to query.
630 * @gid: The GID value to search for.
631 * @port_num: The port number of the device where the GID value could be
633 * @filter: The filter function is executed on any matching GID in the table.
634 * If the filter function returns true, the corresponding index is returned,
635 * otherwise, we continue searching the GID table. It's guaranteed that
636 * while filter is executed, ndev field is valid and the structure won't
637 * change. filter is executed in an atomic context. filter must not be NULL.
638 * @index: The index into the cached GID table where the GID was found. This
639 * parameter may be NULL.
641 * ib_cache_gid_find_by_filter() searches for the specified GID value
642 * of which the filter function returns true in the port's GID table.
643 * This function is only supported on RoCE ports.
646 static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev,
647 const union ib_gid *gid,
649 bool (*filter)(const union ib_gid *,
650 const struct ib_gid_attr *,
655 struct ib_gid_table *table;
661 if (!rdma_is_port_valid(ib_dev, port) ||
662 !rdma_protocol_roce(ib_dev, port))
663 return -EPROTONOSUPPORT;
665 table = rdma_gid_table(ib_dev, port);
667 read_lock_irqsave(&table->rwlock, flags);
668 for (i = 0; i < table->sz; i++) {
669 struct ib_gid_attr attr;
671 if (table->data_vec[i].props & GID_TABLE_ENTRY_INVALID)
674 if (memcmp(gid, &table->data_vec[i].gid, sizeof(*gid)))
677 memcpy(&attr, &table->data_vec[i].attr, sizeof(attr));
679 if (filter(gid, &attr, context)) {
686 read_unlock_irqrestore(&table->rwlock, flags);
693 static struct ib_gid_table *alloc_gid_table(int sz)
695 struct ib_gid_table *table =
696 kzalloc(sizeof(struct ib_gid_table), GFP_KERNEL);
702 table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
703 if (!table->data_vec)
706 mutex_init(&table->lock);
709 rwlock_init(&table->rwlock);
711 /* Mark all entries as invalid so that allocator can allocate
712 * one of the invalid (free) entry.
714 for (i = 0; i < sz; i++)
715 table->data_vec[i].props |= GID_TABLE_ENTRY_INVALID;
723 static void release_gid_table(struct ib_gid_table *table)
726 kfree(table->data_vec);
731 static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
732 struct ib_gid_table *table)
735 bool deleted = false;
740 mutex_lock(&table->lock);
741 for (i = 0; i < table->sz; ++i) {
742 if (!rdma_is_zero_gid(&table->data_vec[i].gid)) {
743 del_gid(ib_dev, port, table, i);
747 mutex_unlock(&table->lock);
750 dispatch_gid_change_event(ib_dev, port);
753 void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
754 struct net_device *ndev,
755 unsigned long gid_type_mask,
756 enum ib_cache_gid_default_mode mode)
758 union ib_gid gid = { };
759 struct ib_gid_attr gid_attr;
760 struct ib_gid_table *table;
761 unsigned int gid_type;
764 table = rdma_gid_table(ib_dev, port);
766 mask = GID_ATTR_FIND_MASK_GID_TYPE |
767 GID_ATTR_FIND_MASK_DEFAULT |
768 GID_ATTR_FIND_MASK_NETDEV;
769 memset(&gid_attr, 0, sizeof(gid_attr));
770 gid_attr.ndev = ndev;
772 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
773 if (1UL << gid_type & ~gid_type_mask)
776 gid_attr.gid_type = gid_type;
778 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
779 make_default_gid(ndev, &gid);
780 __ib_cache_gid_add(ib_dev, port, &gid,
781 &gid_attr, mask, true);
782 } else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) {
783 _ib_cache_gid_del(ib_dev, port, &gid,
784 &gid_attr, mask, true);
789 static void gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
790 struct ib_gid_table *table)
793 unsigned long roce_gid_type_mask;
794 unsigned int num_default_gids;
795 unsigned int current_gid = 0;
797 roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
798 num_default_gids = hweight_long(roce_gid_type_mask);
799 for (i = 0; i < num_default_gids && i < table->sz; i++) {
800 struct ib_gid_table_entry *entry = &table->data_vec[i];
802 entry->props |= GID_TABLE_ENTRY_DEFAULT;
803 current_gid = find_next_bit(&roce_gid_type_mask,
806 entry->attr.gid_type = current_gid++;
811 static void gid_table_release_one(struct ib_device *ib_dev)
813 struct ib_gid_table *table;
816 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
817 table = ib_dev->cache.ports[port].gid;
818 release_gid_table(table);
819 ib_dev->cache.ports[port].gid = NULL;
823 static int _gid_table_setup_one(struct ib_device *ib_dev)
826 struct ib_gid_table *table;
828 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
829 u8 rdma_port = port + rdma_start_port(ib_dev);
831 table = alloc_gid_table(
832 ib_dev->port_immutable[rdma_port].gid_tbl_len);
834 goto rollback_table_setup;
836 gid_table_reserve_default(ib_dev, rdma_port, table);
837 ib_dev->cache.ports[port].gid = table;
841 rollback_table_setup:
842 gid_table_release_one(ib_dev);
846 static void gid_table_cleanup_one(struct ib_device *ib_dev)
848 struct ib_gid_table *table;
851 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
852 table = ib_dev->cache.ports[port].gid;
853 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
858 static int gid_table_setup_one(struct ib_device *ib_dev)
862 err = _gid_table_setup_one(ib_dev);
867 rdma_roce_rescan_device(ib_dev);
872 int ib_get_cached_gid(struct ib_device *device,
876 struct ib_gid_attr *gid_attr)
880 struct ib_gid_table *table;
882 if (!rdma_is_port_valid(device, port_num))
885 table = rdma_gid_table(device, port_num);
886 read_lock_irqsave(&table->rwlock, flags);
887 res = __ib_cache_gid_get(device, port_num, index, gid, gid_attr);
888 read_unlock_irqrestore(&table->rwlock, flags);
892 EXPORT_SYMBOL(ib_get_cached_gid);
895 * ib_find_cached_gid - Returns the port number and GID table index where
896 * a specified GID value occurs.
897 * @device: The device to query.
898 * @gid: The GID value to search for.
899 * @gid_type: The GID type to search for.
900 * @ndev: In RoCE, the net device of the device. NULL means ignore.
901 * @port_num: The port number of the device where the GID value was found.
902 * @index: The index into the cached GID table where the GID was found. This
903 * parameter may be NULL.
905 * ib_find_cached_gid() searches for the specified GID value in
906 * the local software cache.
908 int ib_find_cached_gid(struct ib_device *device,
909 const union ib_gid *gid,
910 enum ib_gid_type gid_type,
911 struct net_device *ndev,
915 return ib_cache_gid_find(device, gid, gid_type, ndev, port_num, index);
917 EXPORT_SYMBOL(ib_find_cached_gid);
919 int ib_find_gid_by_filter(struct ib_device *device,
920 const union ib_gid *gid,
922 bool (*filter)(const union ib_gid *gid,
923 const struct ib_gid_attr *,
925 void *context, u16 *index)
927 /* Only RoCE GID table supports filter function */
928 if (!rdma_protocol_roce(device, port_num) && filter)
929 return -EPROTONOSUPPORT;
931 return ib_cache_gid_find_by_filter(device, gid,
936 int ib_get_cached_pkey(struct ib_device *device,
941 struct ib_pkey_cache *cache;
945 if (!rdma_is_port_valid(device, port_num))
948 read_lock_irqsave(&device->cache.lock, flags);
950 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
952 if (index < 0 || index >= cache->table_len)
955 *pkey = cache->table[index];
957 read_unlock_irqrestore(&device->cache.lock, flags);
961 EXPORT_SYMBOL(ib_get_cached_pkey);
963 int ib_get_cached_subnet_prefix(struct ib_device *device,
970 if (!rdma_is_port_valid(device, port_num))
973 p = port_num - rdma_start_port(device);
974 read_lock_irqsave(&device->cache.lock, flags);
975 *sn_pfx = device->cache.ports[p].subnet_prefix;
976 read_unlock_irqrestore(&device->cache.lock, flags);
980 EXPORT_SYMBOL(ib_get_cached_subnet_prefix);
982 int ib_find_cached_pkey(struct ib_device *device,
987 struct ib_pkey_cache *cache;
993 if (!rdma_is_port_valid(device, port_num))
996 read_lock_irqsave(&device->cache.lock, flags);
998 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1002 for (i = 0; i < cache->table_len; ++i)
1003 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
1004 if (cache->table[i] & 0x8000) {
1012 if (ret && partial_ix >= 0) {
1013 *index = partial_ix;
1017 read_unlock_irqrestore(&device->cache.lock, flags);
1021 EXPORT_SYMBOL(ib_find_cached_pkey);
1023 int ib_find_exact_cached_pkey(struct ib_device *device,
1028 struct ib_pkey_cache *cache;
1029 unsigned long flags;
1033 if (!rdma_is_port_valid(device, port_num))
1036 read_lock_irqsave(&device->cache.lock, flags);
1038 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1042 for (i = 0; i < cache->table_len; ++i)
1043 if (cache->table[i] == pkey) {
1049 read_unlock_irqrestore(&device->cache.lock, flags);
1053 EXPORT_SYMBOL(ib_find_exact_cached_pkey);
1055 int ib_get_cached_lmc(struct ib_device *device,
1059 unsigned long flags;
1062 if (!rdma_is_port_valid(device, port_num))
1065 read_lock_irqsave(&device->cache.lock, flags);
1066 *lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc;
1067 read_unlock_irqrestore(&device->cache.lock, flags);
1071 EXPORT_SYMBOL(ib_get_cached_lmc);
1073 int ib_get_cached_port_state(struct ib_device *device,
1075 enum ib_port_state *port_state)
1077 unsigned long flags;
1080 if (!rdma_is_port_valid(device, port_num))
1083 read_lock_irqsave(&device->cache.lock, flags);
1084 *port_state = device->cache.ports[port_num
1085 - rdma_start_port(device)].port_state;
1086 read_unlock_irqrestore(&device->cache.lock, flags);
1090 EXPORT_SYMBOL(ib_get_cached_port_state);
1092 static int config_non_roce_gid_cache(struct ib_device *device,
1093 u8 port, int gid_tbl_len)
1095 struct ib_gid_attr gid_attr = {};
1096 struct ib_gid_table *table;
1101 gid_attr.device = device;
1102 gid_attr.port_num = port;
1103 table = rdma_gid_table(device, port);
1105 mutex_lock(&table->lock);
1106 for (i = 0; i < gid_tbl_len; ++i) {
1107 if (!device->query_gid)
1109 ret = device->query_gid(device, port, i, &gid);
1111 pr_warn("query_gid failed (%d) for %s (index %d)\n",
1112 ret, device->name, i);
1116 add_modify_gid(table, &gid, &gid_attr);
1119 mutex_unlock(&table->lock);
1123 static void ib_cache_update(struct ib_device *device,
1125 bool enforce_security)
1127 struct ib_port_attr *tprops = NULL;
1128 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
1131 struct ib_gid_table *table;
1133 if (!rdma_is_port_valid(device, port))
1136 table = rdma_gid_table(device, port);
1138 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
1142 ret = ib_query_port(device, port, tprops);
1144 pr_warn("ib_query_port failed (%d) for %s\n",
1149 if (!rdma_protocol_roce(device, port)) {
1150 ret = config_non_roce_gid_cache(device, port,
1151 tprops->gid_tbl_len);
1156 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
1157 sizeof *pkey_cache->table, GFP_KERNEL);
1161 pkey_cache->table_len = tprops->pkey_tbl_len;
1163 for (i = 0; i < pkey_cache->table_len; ++i) {
1164 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
1166 pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n",
1167 ret, device->name, i);
1172 write_lock_irq(&device->cache.lock);
1174 old_pkey_cache = device->cache.ports[port -
1175 rdma_start_port(device)].pkey;
1177 device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache;
1178 device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc;
1179 device->cache.ports[port - rdma_start_port(device)].port_state =
1182 device->cache.ports[port - rdma_start_port(device)].subnet_prefix =
1183 tprops->subnet_prefix;
1184 write_unlock_irq(&device->cache.lock);
1186 if (enforce_security)
1187 ib_security_cache_change(device,
1189 tprops->subnet_prefix);
1191 kfree(old_pkey_cache);
1200 static void ib_cache_task(struct work_struct *_work)
1202 struct ib_update_work *work =
1203 container_of(_work, struct ib_update_work, work);
1205 ib_cache_update(work->device,
1207 work->enforce_security);
1211 static void ib_cache_event(struct ib_event_handler *handler,
1212 struct ib_event *event)
1214 struct ib_update_work *work;
1216 if (event->event == IB_EVENT_PORT_ERR ||
1217 event->event == IB_EVENT_PORT_ACTIVE ||
1218 event->event == IB_EVENT_LID_CHANGE ||
1219 event->event == IB_EVENT_PKEY_CHANGE ||
1220 event->event == IB_EVENT_SM_CHANGE ||
1221 event->event == IB_EVENT_CLIENT_REREGISTER ||
1222 event->event == IB_EVENT_GID_CHANGE) {
1223 work = kmalloc(sizeof *work, GFP_ATOMIC);
1225 INIT_WORK(&work->work, ib_cache_task);
1226 work->device = event->device;
1227 work->port_num = event->element.port_num;
1228 if (event->event == IB_EVENT_PKEY_CHANGE ||
1229 event->event == IB_EVENT_GID_CHANGE)
1230 work->enforce_security = true;
1232 work->enforce_security = false;
1234 queue_work(ib_wq, &work->work);
1239 int ib_cache_setup_one(struct ib_device *device)
1244 rwlock_init(&device->cache.lock);
1246 device->cache.ports =
1247 kzalloc(sizeof(*device->cache.ports) *
1248 (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
1249 if (!device->cache.ports)
1252 err = gid_table_setup_one(device);
1254 kfree(device->cache.ports);
1255 device->cache.ports = NULL;
1259 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1260 ib_cache_update(device, p + rdma_start_port(device), true);
1262 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
1263 device, ib_cache_event);
1264 ib_register_event_handler(&device->cache.event_handler);
1268 void ib_cache_release_one(struct ib_device *device)
1273 * The release function frees all the cache elements.
1274 * This function should be called as part of freeing
1275 * all the device's resources when the cache could no
1276 * longer be accessed.
1278 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1279 kfree(device->cache.ports[p].pkey);
1281 gid_table_release_one(device);
1282 kfree(device->cache.ports);
1285 void ib_cache_cleanup_one(struct ib_device *device)
1287 /* The cleanup function unregisters the event handler,
1288 * waits for all in-progress workqueue elements and cleans
1289 * up the GID cache. This function should be called after
1290 * the device was removed from the devices list and all
1291 * clients were removed, so the cache exists but is
1292 * non-functional and shouldn't be updated anymore.
1294 ib_unregister_event_handler(&device->cache.event_handler);
1295 flush_workqueue(ib_wq);
1296 gid_table_cleanup_one(device);