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_state {
70 GID_TABLE_ENTRY_INVALID = 1,
71 GID_TABLE_ENTRY_VALID = 2,
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.
78 GID_TABLE_ENTRY_PENDING_DEL = 3,
81 struct ib_gid_table_entry {
83 struct work_struct del_work;
84 struct ib_gid_attr attr;
86 enum gid_table_entry_state state;
91 /* In RoCE, adding a GID to the table requires:
92 * (a) Find if this GID is already exists.
93 * (b) Find a free space.
94 * (c) Write the new GID
96 * Delete requires different set of operations:
101 /* Any writer to data_vec must hold this lock and the write side of
102 * rwlock. Readers must hold only rwlock. All writers must be in a
106 /* rwlock protects data_vec[ix]->state and entry pointer.
109 struct ib_gid_table_entry **data_vec;
110 /* bit field, each bit indicates the index of default GID */
111 u32 default_gid_indices;
114 static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
116 struct ib_event event;
118 event.device = ib_dev;
119 event.element.port_num = port;
120 event.event = IB_EVENT_GID_CHANGE;
122 ib_dispatch_event(&event);
125 static const char * const gid_type_str[] = {
126 [IB_GID_TYPE_IB] = "IB/RoCE v1",
127 [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2",
130 const char *ib_cache_gid_type_str(enum ib_gid_type gid_type)
132 if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type])
133 return gid_type_str[gid_type];
135 return "Invalid GID type";
137 EXPORT_SYMBOL(ib_cache_gid_type_str);
139 /** rdma_is_zero_gid - Check if given GID is zero or not.
141 * Returns true if given GID is zero, returns false otherwise.
143 bool rdma_is_zero_gid(const union ib_gid *gid)
145 return !memcmp(gid, &zgid, sizeof(*gid));
147 EXPORT_SYMBOL(rdma_is_zero_gid);
149 /** is_gid_index_default - Check if a given index belongs to
150 * reserved default GIDs or not.
151 * @table: GID table pointer
152 * @index: Index to check in GID table
153 * Returns true if index is one of the reserved default GID index otherwise
156 static bool is_gid_index_default(const struct ib_gid_table *table,
159 return index < 32 && (BIT(index) & table->default_gid_indices);
162 int ib_cache_gid_parse_type_str(const char *buf)
172 if (buf[len - 1] == '\n')
175 for (i = 0; i < ARRAY_SIZE(gid_type_str); ++i)
176 if (gid_type_str[i] && !strncmp(buf, gid_type_str[i], len) &&
177 len == strlen(gid_type_str[i])) {
184 EXPORT_SYMBOL(ib_cache_gid_parse_type_str);
186 static struct ib_gid_table *rdma_gid_table(struct ib_device *device, u8 port)
188 return device->cache.ports[port - rdma_start_port(device)].gid;
191 static bool is_gid_entry_free(const struct ib_gid_table_entry *entry)
196 static bool is_gid_entry_valid(const struct ib_gid_table_entry *entry)
198 return entry && entry->state == GID_TABLE_ENTRY_VALID;
201 static void schedule_free_gid(struct kref *kref)
203 struct ib_gid_table_entry *entry =
204 container_of(kref, struct ib_gid_table_entry, kref);
206 queue_work(ib_wq, &entry->del_work);
209 static void free_gid_entry_locked(struct ib_gid_table_entry *entry)
211 struct ib_device *device = entry->attr.device;
212 u8 port_num = entry->attr.port_num;
213 struct ib_gid_table *table = rdma_gid_table(device, port_num);
215 dev_dbg(&device->dev, "%s port=%d index=%d gid %pI6\n", __func__,
216 port_num, entry->attr.index, entry->attr.gid.raw);
218 if (rdma_cap_roce_gid_table(device, port_num) &&
219 entry->state != GID_TABLE_ENTRY_INVALID)
220 device->del_gid(&entry->attr, &entry->context);
222 write_lock_irq(&table->rwlock);
225 * The only way to avoid overwriting NULL in table is
226 * by comparing if it is same entry in table or not!
227 * If new entry in table is added by the time we free here,
228 * don't overwrite the table entry.
230 if (entry == table->data_vec[entry->attr.index])
231 table->data_vec[entry->attr.index] = NULL;
232 /* Now this index is ready to be allocated */
233 write_unlock_irq(&table->rwlock);
235 if (entry->attr.ndev)
236 dev_put(entry->attr.ndev);
240 static void free_gid_entry(struct kref *kref)
242 struct ib_gid_table_entry *entry =
243 container_of(kref, struct ib_gid_table_entry, kref);
245 free_gid_entry_locked(entry);
249 * free_gid_work - Release reference to the GID entry
250 * @work: Work structure to refer to GID entry which needs to be
253 * free_gid_work() frees the entry from the HCA's hardware table
254 * if provider supports it. It releases reference to netdevice.
256 static void free_gid_work(struct work_struct *work)
258 struct ib_gid_table_entry *entry =
259 container_of(work, struct ib_gid_table_entry, del_work);
260 struct ib_device *device = entry->attr.device;
261 u8 port_num = entry->attr.port_num;
262 struct ib_gid_table *table = rdma_gid_table(device, port_num);
264 mutex_lock(&table->lock);
265 free_gid_entry_locked(entry);
266 mutex_unlock(&table->lock);
269 static struct ib_gid_table_entry *
270 alloc_gid_entry(const struct ib_gid_attr *attr)
272 struct ib_gid_table_entry *entry;
274 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
277 kref_init(&entry->kref);
278 memcpy(&entry->attr, attr, sizeof(*attr));
279 if (entry->attr.ndev)
280 dev_hold(entry->attr.ndev);
281 INIT_WORK(&entry->del_work, free_gid_work);
282 entry->state = GID_TABLE_ENTRY_INVALID;
286 static void store_gid_entry(struct ib_gid_table *table,
287 struct ib_gid_table_entry *entry)
289 entry->state = GID_TABLE_ENTRY_VALID;
291 dev_dbg(&entry->attr.device->dev, "%s port=%d index=%d gid %pI6\n",
292 __func__, entry->attr.port_num, entry->attr.index,
293 entry->attr.gid.raw);
295 lockdep_assert_held(&table->lock);
296 write_lock_irq(&table->rwlock);
297 table->data_vec[entry->attr.index] = entry;
298 write_unlock_irq(&table->rwlock);
301 static void get_gid_entry(struct ib_gid_table_entry *entry)
303 kref_get(&entry->kref);
306 static void put_gid_entry(struct ib_gid_table_entry *entry)
308 kref_put(&entry->kref, schedule_free_gid);
311 static void put_gid_entry_locked(struct ib_gid_table_entry *entry)
313 kref_put(&entry->kref, free_gid_entry);
316 static int add_roce_gid(struct ib_gid_table_entry *entry)
318 const struct ib_gid_attr *attr = &entry->attr;
322 dev_err(&attr->device->dev, "%s NULL netdev port=%d index=%d\n",
323 __func__, attr->port_num, attr->index);
326 if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) {
327 ret = attr->device->add_gid(attr, &entry->context);
329 dev_err(&attr->device->dev,
330 "%s GID add failed port=%d index=%d\n",
331 __func__, attr->port_num, attr->index);
339 * del_gid - Delete GID table entry
341 * @ib_dev: IB device whose GID entry to be deleted
342 * @port: Port number of the IB device
343 * @table: GID table of the IB device for a port
344 * @ix: GID entry index to delete
347 static void del_gid(struct ib_device *ib_dev, u8 port,
348 struct ib_gid_table *table, int ix)
350 struct ib_gid_table_entry *entry;
352 lockdep_assert_held(&table->lock);
354 dev_dbg(&ib_dev->dev, "%s port=%d index=%d gid %pI6\n", __func__, port,
355 ix, table->data_vec[ix]->attr.gid.raw);
357 write_lock_irq(&table->rwlock);
358 entry = table->data_vec[ix];
359 entry->state = GID_TABLE_ENTRY_PENDING_DEL;
361 * For non RoCE protocol, GID entry slot is ready to use.
363 if (!rdma_protocol_roce(ib_dev, port))
364 table->data_vec[ix] = NULL;
365 write_unlock_irq(&table->rwlock);
367 put_gid_entry_locked(entry);
371 * add_modify_gid - Add or modify GID table entry
373 * @table: GID table in which GID to be added or modified
374 * @attr: Attributes of the GID
376 * Returns 0 on success or appropriate error code. It accepts zero
377 * GID addition for non RoCE ports for HCA's who report them as valid
378 * GID. However such zero GIDs are not added to the cache.
380 static int add_modify_gid(struct ib_gid_table *table,
381 const struct ib_gid_attr *attr)
383 struct ib_gid_table_entry *entry;
387 * Invalidate any old entry in the table to make it safe to write to
390 if (is_gid_entry_valid(table->data_vec[attr->index]))
391 del_gid(attr->device, attr->port_num, table, attr->index);
394 * Some HCA's report multiple GID entries with only one valid GID, and
395 * leave other unused entries as the zero GID. Convert zero GIDs to
396 * empty table entries instead of storing them.
398 if (rdma_is_zero_gid(&attr->gid))
401 entry = alloc_gid_entry(attr);
405 if (rdma_protocol_roce(attr->device, attr->port_num)) {
406 ret = add_roce_gid(entry);
411 store_gid_entry(table, entry);
415 put_gid_entry(entry);
419 /* rwlock should be read locked, or lock should be held */
420 static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
421 const struct ib_gid_attr *val, bool default_gid,
422 unsigned long mask, int *pempty)
426 int empty = pempty ? -1 : 0;
428 while (i < table->sz && (found < 0 || empty < 0)) {
429 struct ib_gid_table_entry *data = table->data_vec[i];
430 struct ib_gid_attr *attr;
435 /* find_gid() is used during GID addition where it is expected
436 * to return a free entry slot which is not duplicate.
437 * Free entry slot is requested and returned if pempty is set,
438 * so lookup free slot only if requested.
440 if (pempty && empty < 0) {
441 if (is_gid_entry_free(data) &&
443 is_gid_index_default(table, curr_index)) {
445 * Found an invalid (free) entry; allocate it.
446 * If default GID is requested, then our
447 * found slot must be one of the DEFAULT
448 * reserved slots or we fail.
449 * This ensures that only DEFAULT reserved
450 * slots are used for default property GIDs.
457 * Additionally find_gid() is used to find valid entry during
458 * lookup operation; so ignore the entries which are marked as
459 * pending for removal and the entries which are marked as
462 if (!is_gid_entry_valid(data))
469 if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
470 attr->gid_type != val->gid_type)
473 if (mask & GID_ATTR_FIND_MASK_GID &&
474 memcmp(gid, &data->attr.gid, sizeof(*gid)))
477 if (mask & GID_ATTR_FIND_MASK_NETDEV &&
478 attr->ndev != val->ndev)
481 if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
482 is_gid_index_default(table, curr_index) != default_gid)
494 static void make_default_gid(struct net_device *dev, union ib_gid *gid)
496 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
497 addrconf_ifid_eui48(&gid->raw[8], dev);
500 static int __ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
501 union ib_gid *gid, struct ib_gid_attr *attr,
502 unsigned long mask, bool default_gid)
504 struct ib_gid_table *table;
509 /* Do not allow adding zero GID in support of
510 * IB spec version 1.3 section 4.1.1 point (6) and
511 * section 12.7.10 and section 12.7.20
513 if (rdma_is_zero_gid(gid))
516 table = rdma_gid_table(ib_dev, port);
518 mutex_lock(&table->lock);
520 ix = find_gid(table, gid, attr, default_gid, mask, &empty);
528 attr->device = ib_dev;
530 attr->port_num = port;
532 ret = add_modify_gid(table, attr);
534 dispatch_gid_change_event(ib_dev, port);
537 mutex_unlock(&table->lock);
539 pr_warn("%s: unable to add gid %pI6 error=%d\n",
540 __func__, gid->raw, ret);
544 int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
545 union ib_gid *gid, struct ib_gid_attr *attr)
547 struct net_device *idev;
551 if (ib_dev->get_netdev) {
552 idev = ib_dev->get_netdev(ib_dev, port);
553 if (idev && attr->ndev != idev) {
554 union ib_gid default_gid;
556 /* Adding default GIDs in not permitted */
557 make_default_gid(idev, &default_gid);
558 if (!memcmp(gid, &default_gid, sizeof(*gid))) {
567 mask = GID_ATTR_FIND_MASK_GID |
568 GID_ATTR_FIND_MASK_GID_TYPE |
569 GID_ATTR_FIND_MASK_NETDEV;
571 ret = __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false);
576 _ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
577 union ib_gid *gid, struct ib_gid_attr *attr,
578 unsigned long mask, bool default_gid)
580 struct ib_gid_table *table;
584 table = rdma_gid_table(ib_dev, port);
586 mutex_lock(&table->lock);
588 ix = find_gid(table, gid, attr, default_gid, mask, NULL);
594 del_gid(ib_dev, port, table, ix);
595 dispatch_gid_change_event(ib_dev, port);
598 mutex_unlock(&table->lock);
600 pr_debug("%s: can't delete gid %pI6 error=%d\n",
601 __func__, gid->raw, ret);
605 int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
606 union ib_gid *gid, struct ib_gid_attr *attr)
608 unsigned long mask = GID_ATTR_FIND_MASK_GID |
609 GID_ATTR_FIND_MASK_GID_TYPE |
610 GID_ATTR_FIND_MASK_DEFAULT |
611 GID_ATTR_FIND_MASK_NETDEV;
613 return _ib_cache_gid_del(ib_dev, port, gid, attr, mask, false);
616 int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
617 struct net_device *ndev)
619 struct ib_gid_table *table;
621 bool deleted = false;
623 table = rdma_gid_table(ib_dev, port);
625 mutex_lock(&table->lock);
627 for (ix = 0; ix < table->sz; ix++) {
628 if (is_gid_entry_valid(table->data_vec[ix]) &&
629 table->data_vec[ix]->attr.ndev == ndev) {
630 del_gid(ib_dev, port, table, ix);
635 mutex_unlock(&table->lock);
638 dispatch_gid_change_event(ib_dev, port);
644 * rdma_find_gid_by_port - Returns the GID entry attributes when it finds
645 * a valid GID entry for given search parameters. It searches for the specified
646 * GID value in the local software cache.
647 * @device: The device to query.
648 * @gid: The GID value to search for.
649 * @gid_type: The GID type to search for.
650 * @port_num: The port number of the device where the GID value should be
652 * @ndev: In RoCE, the net device of the device. NULL means ignore.
654 * Returns sgid attributes if the GID is found with valid reference or
655 * returns ERR_PTR for the error.
656 * The caller must invoke rdma_put_gid_attr() to release the reference.
658 const struct ib_gid_attr *
659 rdma_find_gid_by_port(struct ib_device *ib_dev,
660 const union ib_gid *gid,
661 enum ib_gid_type gid_type,
662 u8 port, struct net_device *ndev)
665 struct ib_gid_table *table;
666 unsigned long mask = GID_ATTR_FIND_MASK_GID |
667 GID_ATTR_FIND_MASK_GID_TYPE;
668 struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
669 const struct ib_gid_attr *attr;
672 if (!rdma_is_port_valid(ib_dev, port))
673 return ERR_PTR(-ENOENT);
675 table = rdma_gid_table(ib_dev, port);
678 mask |= GID_ATTR_FIND_MASK_NETDEV;
680 read_lock_irqsave(&table->rwlock, flags);
681 local_index = find_gid(table, gid, &val, false, mask, NULL);
682 if (local_index >= 0) {
683 get_gid_entry(table->data_vec[local_index]);
684 attr = &table->data_vec[local_index]->attr;
685 read_unlock_irqrestore(&table->rwlock, flags);
689 read_unlock_irqrestore(&table->rwlock, flags);
690 return ERR_PTR(-ENOENT);
692 EXPORT_SYMBOL(rdma_find_gid_by_port);
695 * rdma_find_gid_by_filter - Returns the GID table attribute where a
696 * specified GID value occurs
697 * @device: The device to query.
698 * @gid: The GID value to search for.
699 * @port: The port number of the device where the GID value could be
701 * @filter: The filter function is executed on any matching GID in the table.
702 * If the filter function returns true, the corresponding index is returned,
703 * otherwise, we continue searching the GID table. It's guaranteed that
704 * while filter is executed, ndev field is valid and the structure won't
705 * change. filter is executed in an atomic context. filter must not be NULL.
707 * rdma_find_gid_by_filter() searches for the specified GID value
708 * of which the filter function returns true in the port's GID table.
711 const struct ib_gid_attr *rdma_find_gid_by_filter(
712 struct ib_device *ib_dev, const union ib_gid *gid, u8 port,
713 bool (*filter)(const union ib_gid *gid, const struct ib_gid_attr *,
717 const struct ib_gid_attr *res = ERR_PTR(-ENOENT);
718 struct ib_gid_table *table;
722 if (!rdma_is_port_valid(ib_dev, port))
723 return ERR_PTR(-EINVAL);
725 table = rdma_gid_table(ib_dev, port);
727 read_lock_irqsave(&table->rwlock, flags);
728 for (i = 0; i < table->sz; i++) {
729 struct ib_gid_table_entry *entry = table->data_vec[i];
731 if (!is_gid_entry_valid(entry))
734 if (memcmp(gid, &entry->attr.gid, sizeof(*gid)))
737 if (filter(gid, &entry->attr, context)) {
738 get_gid_entry(entry);
743 read_unlock_irqrestore(&table->rwlock, flags);
747 static struct ib_gid_table *alloc_gid_table(int sz)
749 struct ib_gid_table *table = kzalloc(sizeof(*table), GFP_KERNEL);
754 table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
755 if (!table->data_vec)
758 mutex_init(&table->lock);
761 rwlock_init(&table->rwlock);
769 static void release_gid_table(struct ib_device *device, u8 port,
770 struct ib_gid_table *table)
778 for (i = 0; i < table->sz; i++) {
779 if (is_gid_entry_free(table->data_vec[i]))
781 if (kref_read(&table->data_vec[i]->kref) > 1) {
782 dev_err(&device->dev,
783 "GID entry ref leak for index %d ref=%d\n", i,
784 kref_read(&table->data_vec[i]->kref));
791 kfree(table->data_vec);
795 static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
796 struct ib_gid_table *table)
799 bool deleted = false;
804 mutex_lock(&table->lock);
805 for (i = 0; i < table->sz; ++i) {
806 if (is_gid_entry_valid(table->data_vec[i])) {
807 del_gid(ib_dev, port, table, i);
811 mutex_unlock(&table->lock);
814 dispatch_gid_change_event(ib_dev, port);
817 void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
818 struct net_device *ndev,
819 unsigned long gid_type_mask,
820 enum ib_cache_gid_default_mode mode)
822 union ib_gid gid = { };
823 struct ib_gid_attr gid_attr;
824 unsigned int gid_type;
827 mask = GID_ATTR_FIND_MASK_GID_TYPE |
828 GID_ATTR_FIND_MASK_DEFAULT |
829 GID_ATTR_FIND_MASK_NETDEV;
830 memset(&gid_attr, 0, sizeof(gid_attr));
831 gid_attr.ndev = ndev;
833 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
834 if (1UL << gid_type & ~gid_type_mask)
837 gid_attr.gid_type = gid_type;
839 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
840 make_default_gid(ndev, &gid);
841 __ib_cache_gid_add(ib_dev, port, &gid,
842 &gid_attr, mask, true);
843 } else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) {
844 _ib_cache_gid_del(ib_dev, port, &gid,
845 &gid_attr, mask, true);
850 static void gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
851 struct ib_gid_table *table)
854 unsigned long roce_gid_type_mask;
855 unsigned int num_default_gids;
857 roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
858 num_default_gids = hweight_long(roce_gid_type_mask);
859 /* Reserve starting indices for default GIDs */
860 for (i = 0; i < num_default_gids && i < table->sz; i++)
861 table->default_gid_indices |= BIT(i);
865 static void gid_table_release_one(struct ib_device *ib_dev)
867 struct ib_gid_table *table;
870 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
871 table = ib_dev->cache.ports[port].gid;
872 release_gid_table(ib_dev, port, table);
873 ib_dev->cache.ports[port].gid = NULL;
877 static int _gid_table_setup_one(struct ib_device *ib_dev)
880 struct ib_gid_table *table;
882 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
883 u8 rdma_port = port + rdma_start_port(ib_dev);
885 table = alloc_gid_table(
886 ib_dev->port_immutable[rdma_port].gid_tbl_len);
888 goto rollback_table_setup;
890 gid_table_reserve_default(ib_dev, rdma_port, table);
891 ib_dev->cache.ports[port].gid = table;
895 rollback_table_setup:
896 gid_table_release_one(ib_dev);
900 static void gid_table_cleanup_one(struct ib_device *ib_dev)
902 struct ib_gid_table *table;
905 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
906 table = ib_dev->cache.ports[port].gid;
907 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
912 static int gid_table_setup_one(struct ib_device *ib_dev)
916 err = _gid_table_setup_one(ib_dev);
921 rdma_roce_rescan_device(ib_dev);
927 * rdma_query_gid - Read the GID content from the GID software cache
928 * @device: Device to query the GID
929 * @port_num: Port number of the device
930 * @index: Index of the GID table entry to read
931 * @gid: Pointer to GID where to store the entry's GID
933 * rdma_query_gid() only reads the GID entry content for requested device,
934 * port and index. It reads for IB, RoCE and iWarp link layers. It doesn't
935 * hold any reference to the GID table entry in the HCA or software cache.
937 * Returns 0 on success or appropriate error code.
940 int rdma_query_gid(struct ib_device *device, u8 port_num,
941 int index, union ib_gid *gid)
943 struct ib_gid_table *table;
947 if (!rdma_is_port_valid(device, port_num))
950 table = rdma_gid_table(device, port_num);
951 read_lock_irqsave(&table->rwlock, flags);
953 if (index < 0 || index >= table->sz ||
954 !is_gid_entry_valid(table->data_vec[index]))
957 memcpy(gid, &table->data_vec[index]->attr.gid, sizeof(*gid));
961 read_unlock_irqrestore(&table->rwlock, flags);
964 EXPORT_SYMBOL(rdma_query_gid);
967 * rdma_find_gid - Returns SGID attributes if the matching GID is found.
968 * @device: The device to query.
969 * @gid: The GID value to search for.
970 * @gid_type: The GID type to search for.
971 * @ndev: In RoCE, the net device of the device. NULL means ignore.
973 * rdma_find_gid() searches for the specified GID value in the software cache.
975 * Returns GID attributes if a valid GID is found or returns ERR_PTR for the
976 * error. The caller must invoke rdma_put_gid_attr() to release the reference.
979 const struct ib_gid_attr *rdma_find_gid(struct ib_device *device,
980 const union ib_gid *gid,
981 enum ib_gid_type gid_type,
982 struct net_device *ndev)
984 unsigned long mask = GID_ATTR_FIND_MASK_GID |
985 GID_ATTR_FIND_MASK_GID_TYPE;
986 struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type};
990 mask |= GID_ATTR_FIND_MASK_NETDEV;
992 for (p = 0; p < device->phys_port_cnt; p++) {
993 struct ib_gid_table *table;
997 table = device->cache.ports[p].gid;
998 read_lock_irqsave(&table->rwlock, flags);
999 index = find_gid(table, gid, &gid_attr_val, false, mask, NULL);
1001 const struct ib_gid_attr *attr;
1003 get_gid_entry(table->data_vec[index]);
1004 attr = &table->data_vec[index]->attr;
1005 read_unlock_irqrestore(&table->rwlock, flags);
1008 read_unlock_irqrestore(&table->rwlock, flags);
1011 return ERR_PTR(-ENOENT);
1013 EXPORT_SYMBOL(rdma_find_gid);
1015 int ib_get_cached_pkey(struct ib_device *device,
1020 struct ib_pkey_cache *cache;
1021 unsigned long flags;
1024 if (!rdma_is_port_valid(device, port_num))
1027 read_lock_irqsave(&device->cache.lock, flags);
1029 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1031 if (index < 0 || index >= cache->table_len)
1034 *pkey = cache->table[index];
1036 read_unlock_irqrestore(&device->cache.lock, flags);
1040 EXPORT_SYMBOL(ib_get_cached_pkey);
1042 int ib_get_cached_subnet_prefix(struct ib_device *device,
1046 unsigned long flags;
1049 if (!rdma_is_port_valid(device, port_num))
1052 p = port_num - rdma_start_port(device);
1053 read_lock_irqsave(&device->cache.lock, flags);
1054 *sn_pfx = device->cache.ports[p].subnet_prefix;
1055 read_unlock_irqrestore(&device->cache.lock, flags);
1059 EXPORT_SYMBOL(ib_get_cached_subnet_prefix);
1061 int ib_find_cached_pkey(struct ib_device *device,
1066 struct ib_pkey_cache *cache;
1067 unsigned long flags;
1070 int partial_ix = -1;
1072 if (!rdma_is_port_valid(device, port_num))
1075 read_lock_irqsave(&device->cache.lock, flags);
1077 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1081 for (i = 0; i < cache->table_len; ++i)
1082 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
1083 if (cache->table[i] & 0x8000) {
1091 if (ret && partial_ix >= 0) {
1092 *index = partial_ix;
1096 read_unlock_irqrestore(&device->cache.lock, flags);
1100 EXPORT_SYMBOL(ib_find_cached_pkey);
1102 int ib_find_exact_cached_pkey(struct ib_device *device,
1107 struct ib_pkey_cache *cache;
1108 unsigned long flags;
1112 if (!rdma_is_port_valid(device, port_num))
1115 read_lock_irqsave(&device->cache.lock, flags);
1117 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1121 for (i = 0; i < cache->table_len; ++i)
1122 if (cache->table[i] == pkey) {
1128 read_unlock_irqrestore(&device->cache.lock, flags);
1132 EXPORT_SYMBOL(ib_find_exact_cached_pkey);
1134 int ib_get_cached_lmc(struct ib_device *device,
1138 unsigned long flags;
1141 if (!rdma_is_port_valid(device, port_num))
1144 read_lock_irqsave(&device->cache.lock, flags);
1145 *lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc;
1146 read_unlock_irqrestore(&device->cache.lock, flags);
1150 EXPORT_SYMBOL(ib_get_cached_lmc);
1152 int ib_get_cached_port_state(struct ib_device *device,
1154 enum ib_port_state *port_state)
1156 unsigned long flags;
1159 if (!rdma_is_port_valid(device, port_num))
1162 read_lock_irqsave(&device->cache.lock, flags);
1163 *port_state = device->cache.ports[port_num
1164 - rdma_start_port(device)].port_state;
1165 read_unlock_irqrestore(&device->cache.lock, flags);
1169 EXPORT_SYMBOL(ib_get_cached_port_state);
1172 * rdma_get_gid_attr - Returns GID attributes for a port of a device
1173 * at a requested gid_index, if a valid GID entry exists.
1174 * @device: The device to query.
1175 * @port_num: The port number on the device where the GID value
1177 * @index: Index of the GID table entry whose attributes are to
1180 * rdma_get_gid_attr() acquires reference count of gid attributes from the
1181 * cached GID table. Caller must invoke rdma_put_gid_attr() to release
1182 * reference to gid attribute regardless of link layer.
1184 * Returns pointer to valid gid attribute or ERR_PTR for the appropriate error
1187 const struct ib_gid_attr *
1188 rdma_get_gid_attr(struct ib_device *device, u8 port_num, int index)
1190 const struct ib_gid_attr *attr = ERR_PTR(-EINVAL);
1191 struct ib_gid_table *table;
1192 unsigned long flags;
1194 if (!rdma_is_port_valid(device, port_num))
1195 return ERR_PTR(-EINVAL);
1197 table = rdma_gid_table(device, port_num);
1198 if (index < 0 || index >= table->sz)
1199 return ERR_PTR(-EINVAL);
1201 read_lock_irqsave(&table->rwlock, flags);
1202 if (!is_gid_entry_valid(table->data_vec[index]))
1205 get_gid_entry(table->data_vec[index]);
1206 attr = &table->data_vec[index]->attr;
1208 read_unlock_irqrestore(&table->rwlock, flags);
1211 EXPORT_SYMBOL(rdma_get_gid_attr);
1214 * rdma_put_gid_attr - Release reference to the GID attribute
1215 * @attr: Pointer to the GID attribute whose reference
1216 * needs to be released.
1218 * rdma_put_gid_attr() must be used to release reference whose
1219 * reference is acquired using rdma_get_gid_attr() or any APIs
1220 * which returns a pointer to the ib_gid_attr regardless of link layer
1224 void rdma_put_gid_attr(const struct ib_gid_attr *attr)
1226 struct ib_gid_table_entry *entry =
1227 container_of(attr, struct ib_gid_table_entry, attr);
1229 put_gid_entry(entry);
1231 EXPORT_SYMBOL(rdma_put_gid_attr);
1234 * rdma_hold_gid_attr - Get reference to existing GID attribute
1236 * @attr: Pointer to the GID attribute whose reference
1237 * needs to be taken.
1239 * Increase the reference count to a GID attribute to keep it from being
1240 * freed. Callers are required to already be holding a reference to attribute.
1243 void rdma_hold_gid_attr(const struct ib_gid_attr *attr)
1245 struct ib_gid_table_entry *entry =
1246 container_of(attr, struct ib_gid_table_entry, attr);
1248 get_gid_entry(entry);
1250 EXPORT_SYMBOL(rdma_hold_gid_attr);
1253 * rdma_read_gid_attr_ndev_rcu - Read GID attribute netdevice
1254 * which must be in UP state.
1256 * @attr:Pointer to the GID attribute
1258 * Returns pointer to netdevice if the netdevice was attached to GID and
1259 * netdevice is in UP state. Caller must hold RCU lock as this API
1260 * reads the netdev flags which can change while netdevice migrates to
1261 * different net namespace. Returns ERR_PTR with error code otherwise.
1264 struct net_device *rdma_read_gid_attr_ndev_rcu(const struct ib_gid_attr *attr)
1266 struct ib_gid_table_entry *entry =
1267 container_of(attr, struct ib_gid_table_entry, attr);
1268 struct ib_device *device = entry->attr.device;
1269 struct net_device *ndev = ERR_PTR(-ENODEV);
1270 u8 port_num = entry->attr.port_num;
1271 struct ib_gid_table *table;
1272 unsigned long flags;
1275 table = rdma_gid_table(device, port_num);
1277 read_lock_irqsave(&table->rwlock, flags);
1278 valid = is_gid_entry_valid(table->data_vec[attr->index]);
1279 if (valid && attr->ndev && (READ_ONCE(attr->ndev->flags) & IFF_UP))
1281 read_unlock_irqrestore(&table->rwlock, flags);
1285 static int config_non_roce_gid_cache(struct ib_device *device,
1286 u8 port, int gid_tbl_len)
1288 struct ib_gid_attr gid_attr = {};
1289 struct ib_gid_table *table;
1293 gid_attr.device = device;
1294 gid_attr.port_num = port;
1295 table = rdma_gid_table(device, port);
1297 mutex_lock(&table->lock);
1298 for (i = 0; i < gid_tbl_len; ++i) {
1299 if (!device->query_gid)
1301 ret = device->query_gid(device, port, i, &gid_attr.gid);
1303 dev_warn(&device->dev,
1304 "query_gid failed (%d) for index %d\n", ret,
1309 add_modify_gid(table, &gid_attr);
1312 mutex_unlock(&table->lock);
1316 static void ib_cache_update(struct ib_device *device,
1318 bool enforce_security)
1320 struct ib_port_attr *tprops = NULL;
1321 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
1325 if (!rdma_is_port_valid(device, port))
1328 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
1332 ret = ib_query_port(device, port, tprops);
1334 dev_warn(&device->dev, "ib_query_port failed (%d)\n", ret);
1338 if (!rdma_protocol_roce(device, port)) {
1339 ret = config_non_roce_gid_cache(device, port,
1340 tprops->gid_tbl_len);
1345 pkey_cache = kmalloc(struct_size(pkey_cache, table,
1346 tprops->pkey_tbl_len),
1351 pkey_cache->table_len = tprops->pkey_tbl_len;
1353 for (i = 0; i < pkey_cache->table_len; ++i) {
1354 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
1356 dev_warn(&device->dev,
1357 "ib_query_pkey failed (%d) for index %d\n",
1363 write_lock_irq(&device->cache.lock);
1365 old_pkey_cache = device->cache.ports[port -
1366 rdma_start_port(device)].pkey;
1368 device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache;
1369 device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc;
1370 device->cache.ports[port - rdma_start_port(device)].port_state =
1373 device->cache.ports[port - rdma_start_port(device)].subnet_prefix =
1374 tprops->subnet_prefix;
1375 write_unlock_irq(&device->cache.lock);
1377 if (enforce_security)
1378 ib_security_cache_change(device,
1380 tprops->subnet_prefix);
1382 kfree(old_pkey_cache);
1391 static void ib_cache_task(struct work_struct *_work)
1393 struct ib_update_work *work =
1394 container_of(_work, struct ib_update_work, work);
1396 ib_cache_update(work->device,
1398 work->enforce_security);
1402 static void ib_cache_event(struct ib_event_handler *handler,
1403 struct ib_event *event)
1405 struct ib_update_work *work;
1407 if (event->event == IB_EVENT_PORT_ERR ||
1408 event->event == IB_EVENT_PORT_ACTIVE ||
1409 event->event == IB_EVENT_LID_CHANGE ||
1410 event->event == IB_EVENT_PKEY_CHANGE ||
1411 event->event == IB_EVENT_SM_CHANGE ||
1412 event->event == IB_EVENT_CLIENT_REREGISTER ||
1413 event->event == IB_EVENT_GID_CHANGE) {
1414 work = kmalloc(sizeof *work, GFP_ATOMIC);
1416 INIT_WORK(&work->work, ib_cache_task);
1417 work->device = event->device;
1418 work->port_num = event->element.port_num;
1419 if (event->event == IB_EVENT_PKEY_CHANGE ||
1420 event->event == IB_EVENT_GID_CHANGE)
1421 work->enforce_security = true;
1423 work->enforce_security = false;
1425 queue_work(ib_wq, &work->work);
1430 int ib_cache_setup_one(struct ib_device *device)
1435 rwlock_init(&device->cache.lock);
1437 device->cache.ports =
1438 kcalloc(rdma_end_port(device) - rdma_start_port(device) + 1,
1439 sizeof(*device->cache.ports),
1441 if (!device->cache.ports)
1444 err = gid_table_setup_one(device);
1446 kfree(device->cache.ports);
1447 device->cache.ports = NULL;
1451 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1452 ib_cache_update(device, p + rdma_start_port(device), true);
1454 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
1455 device, ib_cache_event);
1456 ib_register_event_handler(&device->cache.event_handler);
1460 void ib_cache_release_one(struct ib_device *device)
1465 * The release function frees all the cache elements.
1466 * This function should be called as part of freeing
1467 * all the device's resources when the cache could no
1468 * longer be accessed.
1470 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1471 kfree(device->cache.ports[p].pkey);
1473 gid_table_release_one(device);
1474 kfree(device->cache.ports);
1477 void ib_cache_cleanup_one(struct ib_device *device)
1479 /* The cleanup function unregisters the event handler,
1480 * waits for all in-progress workqueue elements and cleans
1481 * up the GID cache. This function should be called after
1482 * the device was removed from the devices list and all
1483 * clients were removed, so the cache exists but is
1484 * non-functional and shouldn't be updated anymore.
1486 ib_unregister_event_handler(&device->cache.event_handler);
1487 flush_workqueue(ib_wq);
1488 gid_table_cleanup_one(device);
1491 * Flush the wq second time for any pending GID delete work.
1493 flush_workqueue(ib_wq);