net/smc: Replace ib_query_gid with rdma_get_gid_attr
[linux-2.6-block.git] / drivers / infiniband / core / cache.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
2a1d9b7f
RD
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.
1da177e4
LT
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.
1da177e4
LT
34 */
35
1da177e4
LT
36#include <linux/module.h>
37#include <linux/errno.h>
38#include <linux/slab.h>
e8edc6e0 39#include <linux/workqueue.h>
03db3a2d
MB
40#include <linux/netdevice.h>
41#include <net/addrconf.h>
1da177e4 42
a4d61e84 43#include <rdma/ib_cache.h>
1da177e4
LT
44
45#include "core_priv.h"
46
47struct ib_pkey_cache {
48 int table_len;
49 u16 table[0];
50};
51
1da177e4
LT
52struct ib_update_work {
53 struct work_struct work;
54 struct ib_device *device;
55 u8 port_num;
d291f1a6 56 bool enforce_security;
1da177e4
LT
57};
58
e26be1bf
MS
59union ib_gid zgid;
60EXPORT_SYMBOL(zgid);
03db3a2d 61
03db3a2d
MB
62enum 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,
b39ffa1d 66 GID_ATTR_FIND_MASK_GID_TYPE = 1UL << 3,
03db3a2d
MB
67};
68
b150c386
PP
69enum gid_table_entry_state {
70 GID_TABLE_ENTRY_INVALID = 1,
71 GID_TABLE_ENTRY_VALID = 2,
72 /*
73 * Indicates that entry is pending to be removed, there may
74 * be active users of this GID entry.
75 * When last user of the GID entry releases reference to it,
76 * GID entry is detached from the table.
77 */
78 GID_TABLE_ENTRY_PENDING_DEL = 3,
03db3a2d
MB
79};
80
03db3a2d 81struct ib_gid_table_entry {
b150c386
PP
82 struct kref kref;
83 struct work_struct del_work;
84 struct ib_gid_attr attr;
85 void *context;
86 enum gid_table_entry_state state;
03db3a2d
MB
87};
88
89struct ib_gid_table {
1c36cf91 90 int sz;
03db3a2d
MB
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
95 *
96 * Delete requires different set of operations:
97 * (a) Find the GID
98 * (b) Delete it.
99 *
03db3a2d 100 **/
598ff6ba 101 /* Any writer to data_vec must hold this lock and the write side of
b150c386 102 * rwlock. Readers must hold only rwlock. All writers must be in a
598ff6ba 103 * sleepable context.
9c584f04 104 */
1c36cf91 105 struct mutex lock;
b150c386
PP
106 /* rwlock protects data_vec[ix]->state and entry pointer.
107 */
1c36cf91 108 rwlock_t rwlock;
b150c386 109 struct ib_gid_table_entry **data_vec;
1c36cf91
PP
110 /* bit field, each bit indicates the index of default GID */
111 u32 default_gid_indices;
03db3a2d
MB
112};
113
f3906bd3
MB
114static void dispatch_gid_change_event(struct ib_device *ib_dev, u8 port)
115{
3401857e 116 struct ib_event event;
f3906bd3 117
3401857e
PP
118 event.device = ib_dev;
119 event.element.port_num = port;
120 event.event = IB_EVENT_GID_CHANGE;
f3906bd3 121
3401857e 122 ib_dispatch_event(&event);
f3906bd3
MB
123}
124
b39ffa1d
MB
125static const char * const gid_type_str[] = {
126 [IB_GID_TYPE_IB] = "IB/RoCE v1",
7766a99f 127 [IB_GID_TYPE_ROCE_UDP_ENCAP] = "RoCE v2",
b39ffa1d
MB
128};
129
130const char *ib_cache_gid_type_str(enum ib_gid_type gid_type)
131{
132 if (gid_type < ARRAY_SIZE(gid_type_str) && gid_type_str[gid_type])
133 return gid_type_str[gid_type];
134
135 return "Invalid GID type";
136}
137EXPORT_SYMBOL(ib_cache_gid_type_str);
138
25e62655
PP
139/** rdma_is_zero_gid - Check if given GID is zero or not.
140 * @gid: GID to check
141 * Returns true if given GID is zero, returns false otherwise.
142 */
143bool rdma_is_zero_gid(const union ib_gid *gid)
144{
145 return !memcmp(gid, &zgid, sizeof(*gid));
146}
147EXPORT_SYMBOL(rdma_is_zero_gid);
148
1c36cf91
PP
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
154 * returns false.
155 */
156static bool is_gid_index_default(const struct ib_gid_table *table,
157 unsigned int index)
158{
159 return index < 32 && (BIT(index) & table->default_gid_indices);
160}
161
045959db
MB
162int ib_cache_gid_parse_type_str(const char *buf)
163{
164 unsigned int i;
165 size_t len;
166 int err = -EINVAL;
167
168 len = strlen(buf);
169 if (len == 0)
170 return -EINVAL;
171
172 if (buf[len - 1] == '\n')
173 len--;
174
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])) {
178 err = i;
179 break;
180 }
181
182 return err;
183}
184EXPORT_SYMBOL(ib_cache_gid_parse_type_str);
185
724631a9
PP
186static struct ib_gid_table *rdma_gid_table(struct ib_device *device, u8 port)
187{
188 return device->cache.ports[port - rdma_start_port(device)].gid;
189}
190
b150c386
PP
191static bool is_gid_entry_free(const struct ib_gid_table_entry *entry)
192{
193 return !entry;
194}
195
196static bool is_gid_entry_valid(const struct ib_gid_table_entry *entry)
197{
198 return entry && entry->state == GID_TABLE_ENTRY_VALID;
199}
200
201static void schedule_free_gid(struct kref *kref)
202{
203 struct ib_gid_table_entry *entry =
204 container_of(kref, struct ib_gid_table_entry, kref);
205
206 queue_work(ib_wq, &entry->del_work);
207}
208
209static void free_gid_entry(struct ib_gid_table_entry *entry)
1da177e4 210{
b150c386
PP
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);
214
598ff6ba 215 pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
b150c386
PP
216 device->name, port_num, entry->attr.index,
217 entry->attr.gid.raw);
218
219 mutex_lock(&table->lock);
220 if (rdma_cap_roce_gid_table(device, port_num) &&
221 entry->state != GID_TABLE_ENTRY_INVALID)
222 device->del_gid(&entry->attr, &entry->context);
223 write_lock_irq(&table->rwlock);
598ff6ba 224
b150c386
PP
225 /*
226 * The only way to avoid overwriting NULL in table is
227 * by comparing if it is same entry in table or not!
228 * If new entry in table is added by the time we free here,
229 * don't overwrite the table entry.
230 */
231 if (entry == table->data_vec[entry->attr.index])
232 table->data_vec[entry->attr.index] = NULL;
233 /* Now this index is ready to be allocated */
234 write_unlock_irq(&table->rwlock);
235 mutex_unlock(&table->lock);
236
237 if (entry->attr.ndev)
238 dev_put(entry->attr.ndev);
239 kfree(entry);
240}
241
242/**
243 * free_gid_work - Release reference to the GID entry
244 * @work: Work structure to refer to GID entry which needs to be
245 * deleted.
246 *
247 * free_gid_work() frees the entry from the HCA's hardware table
248 * if provider supports it. It releases reference to netdevice.
249 */
250static void free_gid_work(struct work_struct *work)
251{
252 struct ib_gid_table_entry *entry =
253 container_of(work, struct ib_gid_table_entry, del_work);
254 free_gid_entry(entry);
598ff6ba 255}
03db3a2d 256
b150c386
PP
257static struct ib_gid_table_entry *
258alloc_gid_entry(const struct ib_gid_attr *attr)
598ff6ba
PP
259{
260 struct ib_gid_table_entry *entry;
b150c386
PP
261
262 entry = kzalloc(sizeof(*entry), GFP_KERNEL);
263 if (!entry)
264 return NULL;
265 kref_init(&entry->kref);
266 memcpy(&entry->attr, attr, sizeof(*attr));
267 if (entry->attr.ndev)
268 dev_hold(entry->attr.ndev);
269 INIT_WORK(&entry->del_work, free_gid_work);
270 entry->state = GID_TABLE_ENTRY_INVALID;
271 return entry;
272}
273
274static void store_gid_entry(struct ib_gid_table *table,
275 struct ib_gid_table_entry *entry)
276{
277 entry->state = GID_TABLE_ENTRY_VALID;
278
279 pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
280 entry->attr.device->name, entry->attr.port_num,
281 entry->attr.index, entry->attr.gid.raw);
282
283 lockdep_assert_held(&table->lock);
284 write_lock_irq(&table->rwlock);
285 table->data_vec[entry->attr.index] = entry;
286 write_unlock_irq(&table->rwlock);
287}
288
bf399c2c
PP
289static void get_gid_entry(struct ib_gid_table_entry *entry)
290{
291 kref_get(&entry->kref);
292}
293
b150c386
PP
294static void put_gid_entry(struct ib_gid_table_entry *entry)
295{
296 kref_put(&entry->kref, schedule_free_gid);
297}
298
299static int add_roce_gid(struct ib_gid_table_entry *entry)
300{
301 const struct ib_gid_attr *attr = &entry->attr;
302 int ret;
03db3a2d 303
598ff6ba
PP
304 if (!attr->ndev) {
305 pr_err("%s NULL netdev device=%s port=%d index=%d\n",
306 __func__, attr->device->name, attr->port_num,
307 attr->index);
308 return -EINVAL;
03db3a2d 309 }
598ff6ba 310 if (rdma_cap_roce_gid_table(attr->device, attr->port_num)) {
f4df9a7c 311 ret = attr->device->add_gid(attr, &entry->context);
598ff6ba
PP
312 if (ret) {
313 pr_err("%s GID add failed device=%s port=%d index=%d\n",
314 __func__, attr->device->name, attr->port_num,
315 attr->index);
b150c386 316 return ret;
598ff6ba 317 }
8e787646 318 }
b150c386 319 return 0;
03db3a2d
MB
320}
321
598ff6ba
PP
322/**
323 * add_modify_gid - Add or modify GID table entry
324 *
325 * @table: GID table in which GID to be added or modified
598ff6ba
PP
326 * @attr: Attributes of the GID
327 *
328 * Returns 0 on success or appropriate error code. It accepts zero
329 * GID addition for non RoCE ports for HCA's who report them as valid
330 * GID. However such zero GIDs are not added to the cache.
331 */
332static int add_modify_gid(struct ib_gid_table *table,
598ff6ba
PP
333 const struct ib_gid_attr *attr)
334{
b150c386
PP
335 struct ib_gid_table_entry *entry;
336 int ret = 0;
337
338 /*
339 * Invalidate any old entry in the table to make it safe to write to
340 * this index.
341 */
342 if (is_gid_entry_valid(table->data_vec[attr->index]))
343 put_gid_entry(table->data_vec[attr->index]);
344
345 /*
346 * Some HCA's report multiple GID entries with only one valid GID, and
347 * leave other unused entries as the zero GID. Convert zero GIDs to
348 * empty table entries instead of storing them.
349 */
350 if (rdma_is_zero_gid(&attr->gid))
351 return 0;
352
353 entry = alloc_gid_entry(attr);
354 if (!entry)
355 return -ENOMEM;
598ff6ba
PP
356
357 if (rdma_protocol_roce(attr->device, attr->port_num)) {
b150c386 358 ret = add_roce_gid(entry);
598ff6ba 359 if (ret)
b150c386 360 goto done;
598ff6ba
PP
361 }
362
b150c386 363 store_gid_entry(table, entry);
598ff6ba 364 return 0;
b150c386
PP
365
366done:
367 put_gid_entry(entry);
368 return ret;
03db3a2d
MB
369}
370
598ff6ba
PP
371/**
372 * del_gid - Delete GID table entry
373 *
374 * @ib_dev: IB device whose GID entry to be deleted
375 * @port: Port number of the IB device
376 * @table: GID table of the IB device for a port
377 * @ix: GID entry index to delete
378 *
379 */
380static void del_gid(struct ib_device *ib_dev, u8 port,
381 struct ib_gid_table *table, int ix)
382{
b150c386
PP
383 struct ib_gid_table_entry *entry;
384
598ff6ba 385 lockdep_assert_held(&table->lock);
b150c386
PP
386
387 pr_debug("%s device=%s port=%d index=%d gid %pI6\n", __func__,
388 ib_dev->name, port, ix,
389 table->data_vec[ix]->attr.gid.raw);
390
598ff6ba 391 write_lock_irq(&table->rwlock);
b150c386
PP
392 entry = table->data_vec[ix];
393 entry->state = GID_TABLE_ENTRY_PENDING_DEL;
394 /*
395 * For non RoCE protocol, GID entry slot is ready to use.
396 */
397 if (!rdma_protocol_roce(ib_dev, port))
398 table->data_vec[ix] = NULL;
598ff6ba
PP
399 write_unlock_irq(&table->rwlock);
400
b150c386 401 put_gid_entry(entry);
03db3a2d
MB
402}
403
598ff6ba 404/* rwlock should be read locked, or lock should be held */
03db3a2d
MB
405static int find_gid(struct ib_gid_table *table, const union ib_gid *gid,
406 const struct ib_gid_attr *val, bool default_gid,
cee3c4d0 407 unsigned long mask, int *pempty)
03db3a2d 408{
cee3c4d0
MB
409 int i = 0;
410 int found = -1;
411 int empty = pempty ? -1 : 0;
03db3a2d 412
cee3c4d0 413 while (i < table->sz && (found < 0 || empty < 0)) {
b150c386
PP
414 struct ib_gid_table_entry *data = table->data_vec[i];
415 struct ib_gid_attr *attr;
cee3c4d0 416 int curr_index = i;
03db3a2d 417
cee3c4d0 418 i++;
03db3a2d 419
598ff6ba
PP
420 /* find_gid() is used during GID addition where it is expected
421 * to return a free entry slot which is not duplicate.
422 * Free entry slot is requested and returned if pempty is set,
423 * so lookup free slot only if requested.
424 */
425 if (pempty && empty < 0) {
b150c386
PP
426 if (is_gid_entry_free(data) &&
427 default_gid ==
428 is_gid_index_default(table, curr_index)) {
a66ed149
PP
429 /*
430 * Found an invalid (free) entry; allocate it.
431 * If default GID is requested, then our
432 * found slot must be one of the DEFAULT
433 * reserved slots or we fail.
434 * This ensures that only DEFAULT reserved
435 * slots are used for default property GIDs.
436 */
437 empty = curr_index;
598ff6ba
PP
438 }
439 }
440
441 /*
442 * Additionally find_gid() is used to find valid entry during
b150c386
PP
443 * lookup operation; so ignore the entries which are marked as
444 * pending for removal and the entries which are marked as
445 * invalid.
598ff6ba 446 */
b150c386 447 if (!is_gid_entry_valid(data))
cee3c4d0
MB
448 continue;
449
cee3c4d0 450 if (found >= 0)
9c584f04 451 continue;
03db3a2d 452
b150c386 453 attr = &data->attr;
b39ffa1d
MB
454 if (mask & GID_ATTR_FIND_MASK_GID_TYPE &&
455 attr->gid_type != val->gid_type)
456 continue;
457
03db3a2d 458 if (mask & GID_ATTR_FIND_MASK_GID &&
b150c386 459 memcmp(gid, &data->attr.gid, sizeof(*gid)))
9c584f04 460 continue;
03db3a2d
MB
461
462 if (mask & GID_ATTR_FIND_MASK_NETDEV &&
463 attr->ndev != val->ndev)
9c584f04 464 continue;
03db3a2d
MB
465
466 if (mask & GID_ATTR_FIND_MASK_DEFAULT &&
1c36cf91 467 is_gid_index_default(table, curr_index) != default_gid)
9c584f04 468 continue;
03db3a2d 469
cee3c4d0 470 found = curr_index;
03db3a2d
MB
471 }
472
cee3c4d0
MB
473 if (pempty)
474 *pempty = empty;
475
476 return found;
03db3a2d
MB
477}
478
479static void make_default_gid(struct net_device *dev, union ib_gid *gid)
480{
481 gid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
482 addrconf_ifid_eui48(&gid->raw[8], dev);
483}
484
598ff6ba
PP
485static int __ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
486 union ib_gid *gid, struct ib_gid_attr *attr,
487 unsigned long mask, bool default_gid)
03db3a2d 488{
03db3a2d 489 struct ib_gid_table *table;
1da177e4 490 int ret = 0;
cee3c4d0 491 int empty;
598ff6ba 492 int ix;
1da177e4 493
598ff6ba
PP
494 /* Do not allow adding zero GID in support of
495 * IB spec version 1.3 section 4.1.1 point (6) and
496 * section 12.7.10 and section 12.7.20
497 */
25e62655 498 if (rdma_is_zero_gid(gid))
1da177e4
LT
499 return -EINVAL;
500
724631a9 501 table = rdma_gid_table(ib_dev, port);
598ff6ba
PP
502
503 mutex_lock(&table->lock);
504
505 ix = find_gid(table, gid, attr, default_gid, mask, &empty);
506 if (ix >= 0)
507 goto out_unlock;
508
509 if (empty < 0) {
510 ret = -ENOSPC;
511 goto out_unlock;
512 }
513 attr->device = ib_dev;
514 attr->index = empty;
515 attr->port_num = port;
b150c386
PP
516 attr->gid = *gid;
517 ret = add_modify_gid(table, attr);
598ff6ba
PP
518 if (!ret)
519 dispatch_gid_change_event(ib_dev, port);
520
521out_unlock:
522 mutex_unlock(&table->lock);
523 if (ret)
524 pr_warn("%s: unable to add gid %pI6 error=%d\n",
525 __func__, gid->raw, ret);
526 return ret;
527}
528
529int ib_cache_gid_add(struct ib_device *ib_dev, u8 port,
530 union ib_gid *gid, struct ib_gid_attr *attr)
531{
532 struct net_device *idev;
533 unsigned long mask;
534 int ret;
535
03db3a2d
MB
536 if (ib_dev->get_netdev) {
537 idev = ib_dev->get_netdev(ib_dev, port);
538 if (idev && attr->ndev != idev) {
539 union ib_gid default_gid;
1da177e4 540
03db3a2d
MB
541 /* Adding default GIDs in not permitted */
542 make_default_gid(idev, &default_gid);
543 if (!memcmp(gid, &default_gid, sizeof(*gid))) {
544 dev_put(idev);
545 return -EPERM;
546 }
547 }
548 if (idev)
549 dev_put(idev);
550 }
1da177e4 551
598ff6ba
PP
552 mask = GID_ATTR_FIND_MASK_GID |
553 GID_ATTR_FIND_MASK_GID_TYPE |
554 GID_ATTR_FIND_MASK_NETDEV;
03db3a2d 555
598ff6ba 556 ret = __ib_cache_gid_add(ib_dev, port, gid, attr, mask, false);
1da177e4
LT
557 return ret;
558}
1da177e4 559
22c01ee4
PP
560static int
561_ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
562 union ib_gid *gid, struct ib_gid_attr *attr,
dc5640f2 563 unsigned long mask, bool default_gid)
1da177e4 564{
03db3a2d 565 struct ib_gid_table *table;
598ff6ba 566 int ret = 0;
03db3a2d
MB
567 int ix;
568
724631a9 569 table = rdma_gid_table(ib_dev, port);
03db3a2d
MB
570
571 mutex_lock(&table->lock);
572
dc5640f2 573 ix = find_gid(table, gid, attr, default_gid, mask, NULL);
598ff6ba
PP
574 if (ix < 0) {
575 ret = -EINVAL;
03db3a2d 576 goto out_unlock;
598ff6ba 577 }
03db3a2d 578
598ff6ba
PP
579 del_gid(ib_dev, port, table, ix);
580 dispatch_gid_change_event(ib_dev, port);
03db3a2d
MB
581
582out_unlock:
583 mutex_unlock(&table->lock);
598ff6ba
PP
584 if (ret)
585 pr_debug("%s: can't delete gid %pI6 error=%d\n",
586 __func__, gid->raw, ret);
587 return ret;
03db3a2d
MB
588}
589
22c01ee4
PP
590int ib_cache_gid_del(struct ib_device *ib_dev, u8 port,
591 union ib_gid *gid, struct ib_gid_attr *attr)
592{
dc5640f2
PP
593 unsigned long mask = GID_ATTR_FIND_MASK_GID |
594 GID_ATTR_FIND_MASK_GID_TYPE |
595 GID_ATTR_FIND_MASK_DEFAULT |
596 GID_ATTR_FIND_MASK_NETDEV;
597
598 return _ib_cache_gid_del(ib_dev, port, gid, attr, mask, false);
22c01ee4
PP
599}
600
03db3a2d
MB
601int ib_cache_gid_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
602 struct net_device *ndev)
603{
03db3a2d
MB
604 struct ib_gid_table *table;
605 int ix;
9c584f04 606 bool deleted = false;
03db3a2d 607
724631a9 608 table = rdma_gid_table(ib_dev, port);
03db3a2d
MB
609
610 mutex_lock(&table->lock);
611
598ff6ba 612 for (ix = 0; ix < table->sz; ix++) {
b150c386
PP
613 if (is_gid_entry_valid(table->data_vec[ix]) &&
614 table->data_vec[ix]->attr.ndev == ndev) {
598ff6ba
PP
615 del_gid(ib_dev, port, table, ix);
616 deleted = true;
617 }
618 }
03db3a2d
MB
619
620 mutex_unlock(&table->lock);
9c584f04
MB
621
622 if (deleted)
623 dispatch_gid_change_event(ib_dev, port);
624
03db3a2d
MB
625 return 0;
626}
627
628static int __ib_cache_gid_get(struct ib_device *ib_dev, u8 port, int index,
629 union ib_gid *gid, struct ib_gid_attr *attr)
630{
03db3a2d 631 struct ib_gid_table *table;
1da177e4 632
724631a9 633 table = rdma_gid_table(ib_dev, port);
1da177e4 634
03db3a2d
MB
635 if (index < 0 || index >= table->sz)
636 return -EINVAL;
1da177e4 637
b150c386 638 if (!is_gid_entry_valid(table->data_vec[index]))
a840c93c 639 return -EINVAL;
03db3a2d 640
b150c386 641 memcpy(gid, &table->data_vec[index]->attr.gid, sizeof(*gid));
03db3a2d 642 if (attr) {
b150c386
PP
643 memcpy(attr, &table->data_vec[index]->attr,
644 sizeof(*attr));
03db3a2d
MB
645 if (attr->ndev)
646 dev_hold(attr->ndev);
647 }
648
03db3a2d
MB
649 return 0;
650}
651
652static int _ib_cache_gid_table_find(struct ib_device *ib_dev,
653 const union ib_gid *gid,
654 const struct ib_gid_attr *val,
655 unsigned long mask,
656 u8 *port, u16 *index)
657{
03db3a2d
MB
658 struct ib_gid_table *table;
659 u8 p;
660 int local_index;
9c584f04 661 unsigned long flags;
03db3a2d
MB
662
663 for (p = 0; p < ib_dev->phys_port_cnt; p++) {
21d6454a 664 table = ib_dev->cache.ports[p].gid;
9c584f04 665 read_lock_irqsave(&table->rwlock, flags);
cee3c4d0 666 local_index = find_gid(table, gid, val, false, mask, NULL);
03db3a2d
MB
667 if (local_index >= 0) {
668 if (index)
669 *index = local_index;
670 if (port)
671 *port = p + rdma_start_port(ib_dev);
9c584f04 672 read_unlock_irqrestore(&table->rwlock, flags);
03db3a2d 673 return 0;
1da177e4 674 }
9c584f04 675 read_unlock_irqrestore(&table->rwlock, flags);
1da177e4 676 }
1da177e4 677
03db3a2d
MB
678 return -ENOENT;
679}
680
681static int ib_cache_gid_find(struct ib_device *ib_dev,
682 const union ib_gid *gid,
b39ffa1d 683 enum ib_gid_type gid_type,
03db3a2d
MB
684 struct net_device *ndev, u8 *port,
685 u16 *index)
686{
b39ffa1d
MB
687 unsigned long mask = GID_ATTR_FIND_MASK_GID |
688 GID_ATTR_FIND_MASK_GID_TYPE;
689 struct ib_gid_attr gid_attr_val = {.ndev = ndev, .gid_type = gid_type};
03db3a2d
MB
690
691 if (ndev)
692 mask |= GID_ATTR_FIND_MASK_NETDEV;
693
694 return _ib_cache_gid_table_find(ib_dev, gid, &gid_attr_val,
695 mask, port, index);
696}
697
6612b498
PP
698/**
699 * ib_find_cached_gid_by_port - Returns the GID table index where a specified
700 * GID value occurs. It searches for the specified GID value in the local
701 * software cache.
702 * @device: The device to query.
703 * @gid: The GID value to search for.
704 * @gid_type: The GID type to search for.
705 * @port_num: The port number of the device where the GID value should be
706 * searched.
707 * @ndev: In RoCE, the net device of the device. Null means ignore.
708 * @index: The index into the cached GID table where the GID was found. This
709 * parameter may be NULL.
710 */
d300ec52
MB
711int ib_find_cached_gid_by_port(struct ib_device *ib_dev,
712 const union ib_gid *gid,
b39ffa1d 713 enum ib_gid_type gid_type,
d300ec52
MB
714 u8 port, struct net_device *ndev,
715 u16 *index)
03db3a2d
MB
716{
717 int local_index;
03db3a2d 718 struct ib_gid_table *table;
b39ffa1d
MB
719 unsigned long mask = GID_ATTR_FIND_MASK_GID |
720 GID_ATTR_FIND_MASK_GID_TYPE;
721 struct ib_gid_attr val = {.ndev = ndev, .gid_type = gid_type};
9c584f04 722 unsigned long flags;
03db3a2d 723
24dc831b 724 if (!rdma_is_port_valid(ib_dev, port))
03db3a2d
MB
725 return -ENOENT;
726
724631a9 727 table = rdma_gid_table(ib_dev, port);
03db3a2d
MB
728
729 if (ndev)
730 mask |= GID_ATTR_FIND_MASK_NETDEV;
731
9c584f04 732 read_lock_irqsave(&table->rwlock, flags);
cee3c4d0 733 local_index = find_gid(table, gid, &val, false, mask, NULL);
03db3a2d
MB
734 if (local_index >= 0) {
735 if (index)
736 *index = local_index;
9c584f04 737 read_unlock_irqrestore(&table->rwlock, flags);
03db3a2d
MB
738 return 0;
739 }
740
9c584f04 741 read_unlock_irqrestore(&table->rwlock, flags);
03db3a2d
MB
742 return -ENOENT;
743}
d300ec52 744EXPORT_SYMBOL(ib_find_cached_gid_by_port);
03db3a2d 745
99b27e3b 746/**
6612b498 747 * ib_cache_gid_find_by_filter - Returns the GID table index where a specified
99b27e3b
MB
748 * GID value occurs
749 * @device: The device to query.
750 * @gid: The GID value to search for.
751 * @port_num: The port number of the device where the GID value could be
752 * searched.
753 * @filter: The filter function is executed on any matching GID in the table.
754 * If the filter function returns true, the corresponding index is returned,
755 * otherwise, we continue searching the GID table. It's guaranteed that
756 * while filter is executed, ndev field is valid and the structure won't
757 * change. filter is executed in an atomic context. filter must not be NULL.
6612b498 758 * @index: The index into the cached GID table where the GID was found. This
99b27e3b
MB
759 * parameter may be NULL.
760 *
761 * ib_cache_gid_find_by_filter() searches for the specified GID value
762 * of which the filter function returns true in the port's GID table.
763 * This function is only supported on RoCE ports.
764 *
765 */
766static int ib_cache_gid_find_by_filter(struct ib_device *ib_dev,
767 const union ib_gid *gid,
768 u8 port,
769 bool (*filter)(const union ib_gid *,
770 const struct ib_gid_attr *,
771 void *),
772 void *context,
773 u16 *index)
774{
99b27e3b
MB
775 struct ib_gid_table *table;
776 unsigned int i;
9c584f04 777 unsigned long flags;
99b27e3b
MB
778 bool found = false;
779
99b27e3b 780
24dc831b 781 if (!rdma_is_port_valid(ib_dev, port) ||
99b27e3b
MB
782 !rdma_protocol_roce(ib_dev, port))
783 return -EPROTONOSUPPORT;
784
724631a9 785 table = rdma_gid_table(ib_dev, port);
99b27e3b 786
9c584f04 787 read_lock_irqsave(&table->rwlock, flags);
99b27e3b
MB
788 for (i = 0; i < table->sz; i++) {
789 struct ib_gid_attr attr;
99b27e3b 790
b150c386 791 if (!is_gid_entry_valid(table->data_vec[i]))
151ed9d7 792 continue;
99b27e3b 793
b150c386
PP
794 if (memcmp(gid, &table->data_vec[i]->attr.gid,
795 sizeof(*gid)))
151ed9d7 796 continue;
99b27e3b 797
b150c386 798 memcpy(&attr, &table->data_vec[i]->attr, sizeof(attr));
99b27e3b 799
151ed9d7 800 if (filter(gid, &attr, context)) {
99b27e3b 801 found = true;
151ed9d7
PP
802 if (index)
803 *index = i;
99b27e3b 804 break;
151ed9d7 805 }
99b27e3b 806 }
9c584f04 807 read_unlock_irqrestore(&table->rwlock, flags);
99b27e3b
MB
808
809 if (!found)
810 return -ENOENT;
99b27e3b
MB
811 return 0;
812}
813
03db3a2d
MB
814static struct ib_gid_table *alloc_gid_table(int sz)
815{
b150c386 816 struct ib_gid_table *table = kzalloc(sizeof(*table), GFP_KERNEL);
9c584f04 817
03db3a2d
MB
818 if (!table)
819 return NULL;
820
821 table->data_vec = kcalloc(sz, sizeof(*table->data_vec), GFP_KERNEL);
822 if (!table->data_vec)
823 goto err_free_table;
824
825 mutex_init(&table->lock);
826
827 table->sz = sz;
9c584f04 828 rwlock_init(&table->rwlock);
03db3a2d
MB
829 return table;
830
831err_free_table:
832 kfree(table);
833 return NULL;
834}
835
b150c386
PP
836static void release_gid_table(struct ib_device *device, u8 port,
837 struct ib_gid_table *table)
03db3a2d 838{
b150c386
PP
839 bool leak = false;
840 int i;
841
842 if (!table)
843 return;
844
845 for (i = 0; i < table->sz; i++) {
846 if (is_gid_entry_free(table->data_vec[i]))
847 continue;
848 if (kref_read(&table->data_vec[i]->kref) > 1) {
849 pr_err("GID entry ref leak for %s (index %d) ref=%d\n",
850 device->name, i,
851 kref_read(&table->data_vec[i]->kref));
852 leak = true;
853 }
03db3a2d 854 }
b150c386
PP
855 if (leak)
856 return;
857
858 kfree(table->data_vec);
859 kfree(table);
03db3a2d
MB
860}
861
862static void cleanup_gid_table_port(struct ib_device *ib_dev, u8 port,
863 struct ib_gid_table *table)
864{
865 int i;
9c584f04 866 bool deleted = false;
03db3a2d
MB
867
868 if (!table)
869 return;
870
598ff6ba 871 mutex_lock(&table->lock);
03db3a2d 872 for (i = 0; i < table->sz; ++i) {
b150c386 873 if (is_gid_entry_valid(table->data_vec[i])) {
598ff6ba
PP
874 del_gid(ib_dev, port, table, i);
875 deleted = true;
876 }
03db3a2d 877 }
598ff6ba 878 mutex_unlock(&table->lock);
9c584f04
MB
879
880 if (deleted)
881 dispatch_gid_change_event(ib_dev, port);
03db3a2d
MB
882}
883
884void ib_cache_gid_set_default_gid(struct ib_device *ib_dev, u8 port,
885 struct net_device *ndev,
b39ffa1d 886 unsigned long gid_type_mask,
03db3a2d
MB
887 enum ib_cache_gid_default_mode mode)
888{
dc5640f2 889 union ib_gid gid = { };
03db3a2d
MB
890 struct ib_gid_attr gid_attr;
891 struct ib_gid_table *table;
b39ffa1d 892 unsigned int gid_type;
598ff6ba 893 unsigned long mask;
03db3a2d 894
724631a9 895 table = rdma_gid_table(ib_dev, port);
03db3a2d 896
dc5640f2
PP
897 mask = GID_ATTR_FIND_MASK_GID_TYPE |
898 GID_ATTR_FIND_MASK_DEFAULT |
899 GID_ATTR_FIND_MASK_NETDEV;
03db3a2d
MB
900 memset(&gid_attr, 0, sizeof(gid_attr));
901 gid_attr.ndev = ndev;
902
b39ffa1d 903 for (gid_type = 0; gid_type < IB_GID_TYPE_SIZE; ++gid_type) {
b39ffa1d
MB
904 if (1UL << gid_type & ~gid_type_mask)
905 continue;
906
907 gid_attr.gid_type = gid_type;
908
b39ffa1d 909 if (mode == IB_CACHE_GID_DEFAULT_MODE_SET) {
dc5640f2 910 make_default_gid(ndev, &gid);
598ff6ba
PP
911 __ib_cache_gid_add(ib_dev, port, &gid,
912 &gid_attr, mask, true);
913 } else if (mode == IB_CACHE_GID_DEFAULT_MODE_DELETE) {
dc5640f2
PP
914 _ib_cache_gid_del(ib_dev, port, &gid,
915 &gid_attr, mask, true);
9c584f04 916 }
b39ffa1d 917 }
03db3a2d
MB
918}
919
25a1cd3f
PP
920static void gid_table_reserve_default(struct ib_device *ib_dev, u8 port,
921 struct ib_gid_table *table)
03db3a2d 922{
b39ffa1d
MB
923 unsigned int i;
924 unsigned long roce_gid_type_mask;
925 unsigned int num_default_gids;
b39ffa1d
MB
926
927 roce_gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
928 num_default_gids = hweight_long(roce_gid_type_mask);
1c36cf91
PP
929 /* Reserve starting indices for default GIDs */
930 for (i = 0; i < num_default_gids && i < table->sz; i++)
931 table->default_gid_indices |= BIT(i);
03db3a2d 932}
03db3a2d 933
be0e8f34
PP
934
935static void gid_table_release_one(struct ib_device *ib_dev)
936{
937 struct ib_gid_table *table;
938 u8 port;
939
940 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
941 table = ib_dev->cache.ports[port].gid;
b150c386 942 release_gid_table(ib_dev, port, table);
be0e8f34
PP
943 ib_dev->cache.ports[port].gid = NULL;
944 }
03db3a2d
MB
945}
946
947static int _gid_table_setup_one(struct ib_device *ib_dev)
948{
949 u8 port;
21d6454a 950 struct ib_gid_table *table;
03db3a2d 951
03db3a2d
MB
952 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
953 u8 rdma_port = port + rdma_start_port(ib_dev);
954
25a1cd3f 955 table = alloc_gid_table(
03db3a2d 956 ib_dev->port_immutable[rdma_port].gid_tbl_len);
be0e8f34 957 if (!table)
03db3a2d 958 goto rollback_table_setup;
03db3a2d 959
25a1cd3f 960 gid_table_reserve_default(ib_dev, rdma_port, table);
21d6454a 961 ib_dev->cache.ports[port].gid = table;
03db3a2d 962 }
03db3a2d
MB
963 return 0;
964
965rollback_table_setup:
be0e8f34
PP
966 gid_table_release_one(ib_dev);
967 return -ENOMEM;
03db3a2d
MB
968}
969
970static void gid_table_cleanup_one(struct ib_device *ib_dev)
971{
21d6454a 972 struct ib_gid_table *table;
03db3a2d
MB
973 u8 port;
974
21d6454a
JW
975 for (port = 0; port < ib_dev->phys_port_cnt; port++) {
976 table = ib_dev->cache.ports[port].gid;
03db3a2d 977 cleanup_gid_table_port(ib_dev, port + rdma_start_port(ib_dev),
21d6454a
JW
978 table);
979 }
03db3a2d
MB
980}
981
982static int gid_table_setup_one(struct ib_device *ib_dev)
983{
984 int err;
985
986 err = _gid_table_setup_one(ib_dev);
987
988 if (err)
989 return err;
990
32f69e4b 991 rdma_roce_rescan_device(ib_dev);
03db3a2d
MB
992
993 return err;
994}
995
996int ib_get_cached_gid(struct ib_device *device,
997 u8 port_num,
998 int index,
55ee3ab2
MB
999 union ib_gid *gid,
1000 struct ib_gid_attr *gid_attr)
03db3a2d 1001{
9c584f04
MB
1002 int res;
1003 unsigned long flags;
21d6454a 1004 struct ib_gid_table *table;
9c584f04 1005
24dc831b 1006 if (!rdma_is_port_valid(device, port_num))
03db3a2d
MB
1007 return -EINVAL;
1008
724631a9 1009 table = rdma_gid_table(device, port_num);
9c584f04
MB
1010 read_lock_irqsave(&table->rwlock, flags);
1011 res = __ib_cache_gid_get(device, port_num, index, gid, gid_attr);
1012 read_unlock_irqrestore(&table->rwlock, flags);
1013
1014 return res;
03db3a2d
MB
1015}
1016EXPORT_SYMBOL(ib_get_cached_gid);
1017
6612b498
PP
1018/**
1019 * ib_find_cached_gid - Returns the port number and GID table index where
1020 * a specified GID value occurs.
1021 * @device: The device to query.
1022 * @gid: The GID value to search for.
1023 * @gid_type: The GID type to search for.
1024 * @ndev: In RoCE, the net device of the device. NULL means ignore.
1025 * @port_num: The port number of the device where the GID value was found.
1026 * @index: The index into the cached GID table where the GID was found. This
1027 * parameter may be NULL.
1028 *
1029 * ib_find_cached_gid() searches for the specified GID value in
1030 * the local software cache.
1031 */
03db3a2d
MB
1032int ib_find_cached_gid(struct ib_device *device,
1033 const union ib_gid *gid,
b39ffa1d 1034 enum ib_gid_type gid_type,
55ee3ab2 1035 struct net_device *ndev,
03db3a2d
MB
1036 u8 *port_num,
1037 u16 *index)
1038{
b39ffa1d 1039 return ib_cache_gid_find(device, gid, gid_type, ndev, port_num, index);
1da177e4
LT
1040}
1041EXPORT_SYMBOL(ib_find_cached_gid);
1042
99b27e3b
MB
1043int ib_find_gid_by_filter(struct ib_device *device,
1044 const union ib_gid *gid,
1045 u8 port_num,
1046 bool (*filter)(const union ib_gid *gid,
1047 const struct ib_gid_attr *,
1048 void *),
1049 void *context, u16 *index)
1050{
1051 /* Only RoCE GID table supports filter function */
4ab7cb4b 1052 if (!rdma_protocol_roce(device, port_num) && filter)
99b27e3b
MB
1053 return -EPROTONOSUPPORT;
1054
1055 return ib_cache_gid_find_by_filter(device, gid,
1056 port_num, filter,
1057 context, index);
1058}
99b27e3b 1059
1da177e4
LT
1060int ib_get_cached_pkey(struct ib_device *device,
1061 u8 port_num,
1062 int index,
1063 u16 *pkey)
1064{
1065 struct ib_pkey_cache *cache;
1066 unsigned long flags;
1067 int ret = 0;
1068
24dc831b 1069 if (!rdma_is_port_valid(device, port_num))
1da177e4
LT
1070 return -EINVAL;
1071
1072 read_lock_irqsave(&device->cache.lock, flags);
1073
21d6454a 1074 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1da177e4
LT
1075
1076 if (index < 0 || index >= cache->table_len)
1077 ret = -EINVAL;
1078 else
1079 *pkey = cache->table[index];
1080
1081 read_unlock_irqrestore(&device->cache.lock, flags);
1082
1083 return ret;
1084}
1085EXPORT_SYMBOL(ib_get_cached_pkey);
1086
883c71fe
DJ
1087int ib_get_cached_subnet_prefix(struct ib_device *device,
1088 u8 port_num,
1089 u64 *sn_pfx)
1090{
1091 unsigned long flags;
1092 int p;
1093
6d5b2047 1094 if (!rdma_is_port_valid(device, port_num))
883c71fe
DJ
1095 return -EINVAL;
1096
1097 p = port_num - rdma_start_port(device);
1098 read_lock_irqsave(&device->cache.lock, flags);
1099 *sn_pfx = device->cache.ports[p].subnet_prefix;
1100 read_unlock_irqrestore(&device->cache.lock, flags);
1101
1102 return 0;
1103}
1104EXPORT_SYMBOL(ib_get_cached_subnet_prefix);
1105
1da177e4
LT
1106int ib_find_cached_pkey(struct ib_device *device,
1107 u8 port_num,
1108 u16 pkey,
1109 u16 *index)
1110{
1111 struct ib_pkey_cache *cache;
1112 unsigned long flags;
1113 int i;
1114 int ret = -ENOENT;
ff7166c4 1115 int partial_ix = -1;
1da177e4 1116
24dc831b 1117 if (!rdma_is_port_valid(device, port_num))
1da177e4
LT
1118 return -EINVAL;
1119
1120 read_lock_irqsave(&device->cache.lock, flags);
1121
21d6454a 1122 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
1da177e4
LT
1123
1124 *index = -1;
1125
1126 for (i = 0; i < cache->table_len; ++i)
1127 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
ff7166c4
JM
1128 if (cache->table[i] & 0x8000) {
1129 *index = i;
1130 ret = 0;
1131 break;
1132 } else
1133 partial_ix = i;
1da177e4
LT
1134 }
1135
ff7166c4
JM
1136 if (ret && partial_ix >= 0) {
1137 *index = partial_ix;
1138 ret = 0;
1139 }
1140
1da177e4
LT
1141 read_unlock_irqrestore(&device->cache.lock, flags);
1142
1143 return ret;
1144}
1145EXPORT_SYMBOL(ib_find_cached_pkey);
1146
73aaa741
JM
1147int ib_find_exact_cached_pkey(struct ib_device *device,
1148 u8 port_num,
1149 u16 pkey,
1150 u16 *index)
1151{
1152 struct ib_pkey_cache *cache;
1153 unsigned long flags;
1154 int i;
1155 int ret = -ENOENT;
1156
24dc831b 1157 if (!rdma_is_port_valid(device, port_num))
73aaa741
JM
1158 return -EINVAL;
1159
1160 read_lock_irqsave(&device->cache.lock, flags);
1161
21d6454a 1162 cache = device->cache.ports[port_num - rdma_start_port(device)].pkey;
73aaa741
JM
1163
1164 *index = -1;
1165
1166 for (i = 0; i < cache->table_len; ++i)
1167 if (cache->table[i] == pkey) {
1168 *index = i;
1169 ret = 0;
1170 break;
1171 }
1172
1173 read_unlock_irqrestore(&device->cache.lock, flags);
1174
1175 return ret;
1176}
1177EXPORT_SYMBOL(ib_find_exact_cached_pkey);
1178
6fb9cdbf
JM
1179int ib_get_cached_lmc(struct ib_device *device,
1180 u8 port_num,
1181 u8 *lmc)
1182{
1183 unsigned long flags;
1184 int ret = 0;
1185
24dc831b 1186 if (!rdma_is_port_valid(device, port_num))
6fb9cdbf
JM
1187 return -EINVAL;
1188
1189 read_lock_irqsave(&device->cache.lock, flags);
21d6454a 1190 *lmc = device->cache.ports[port_num - rdma_start_port(device)].lmc;
6fb9cdbf
JM
1191 read_unlock_irqrestore(&device->cache.lock, flags);
1192
1193 return ret;
1194}
1195EXPORT_SYMBOL(ib_get_cached_lmc);
1196
9e2c3f1c
JW
1197int ib_get_cached_port_state(struct ib_device *device,
1198 u8 port_num,
1199 enum ib_port_state *port_state)
1200{
1201 unsigned long flags;
1202 int ret = 0;
1203
6d5b2047 1204 if (!rdma_is_port_valid(device, port_num))
9e2c3f1c
JW
1205 return -EINVAL;
1206
1207 read_lock_irqsave(&device->cache.lock, flags);
21d6454a
JW
1208 *port_state = device->cache.ports[port_num
1209 - rdma_start_port(device)].port_state;
9e2c3f1c
JW
1210 read_unlock_irqrestore(&device->cache.lock, flags);
1211
1212 return ret;
1213}
1214EXPORT_SYMBOL(ib_get_cached_port_state);
1215
bf399c2c
PP
1216/**
1217 * rdma_get_gid_attr - Returns GID attributes for a port of a device
1218 * at a requested gid_index, if a valid GID entry exists.
1219 * @device: The device to query.
1220 * @port_num: The port number on the device where the GID value
1221 * is to be queried.
1222 * @index: Index of the GID table entry whose attributes are to
1223 * be queried.
1224 *
1225 * rdma_get_gid_attr() acquires reference count of gid attributes from the
1226 * cached GID table. Caller must invoke rdma_put_gid_attr() to release
1227 * reference to gid attribute regardless of link layer.
1228 *
1229 * Returns pointer to valid gid attribute or ERR_PTR for the appropriate error
1230 * code.
1231 */
1232const struct ib_gid_attr *
1233rdma_get_gid_attr(struct ib_device *device, u8 port_num, int index)
1234{
1235 const struct ib_gid_attr *attr = ERR_PTR(-EINVAL);
1236 struct ib_gid_table *table;
1237 unsigned long flags;
1238
1239 if (!rdma_is_port_valid(device, port_num))
1240 return ERR_PTR(-EINVAL);
1241
1242 table = rdma_gid_table(device, port_num);
1243 if (index < 0 || index >= table->sz)
1244 return ERR_PTR(-EINVAL);
1245
1246 read_lock_irqsave(&table->rwlock, flags);
1247 if (!is_gid_entry_valid(table->data_vec[index]))
1248 goto done;
1249
1250 get_gid_entry(table->data_vec[index]);
1251 attr = &table->data_vec[index]->attr;
1252done:
1253 read_unlock_irqrestore(&table->rwlock, flags);
1254 return attr;
1255}
1256EXPORT_SYMBOL(rdma_get_gid_attr);
1257
1258/**
1259 * rdma_put_gid_attr - Release reference to the GID attribute
1260 * @attr: Pointer to the GID attribute whose reference
1261 * needs to be released.
1262 *
1263 * rdma_put_gid_attr() must be used to release reference whose
1264 * reference is acquired using rdma_get_gid_attr() or any APIs
1265 * which returns a pointer to the ib_gid_attr regardless of link layer
1266 * of IB or RoCE.
1267 *
1268 */
1269void rdma_put_gid_attr(const struct ib_gid_attr *attr)
1270{
1271 struct ib_gid_table_entry *entry =
1272 container_of(attr, struct ib_gid_table_entry, attr);
1273
1274 put_gid_entry(entry);
1275}
1276EXPORT_SYMBOL(rdma_put_gid_attr);
1277
1278/**
1279 * rdma_hold_gid_attr - Get reference to existing GID attribute
1280 *
1281 * @attr: Pointer to the GID attribute whose reference
1282 * needs to be taken.
1283 *
1284 * Increase the reference count to a GID attribute to keep it from being
1285 * freed. Callers are required to already be holding a reference to attribute.
1286 *
1287 */
1288void rdma_hold_gid_attr(const struct ib_gid_attr *attr)
1289{
1290 struct ib_gid_table_entry *entry =
1291 container_of(attr, struct ib_gid_table_entry, attr);
1292
1293 get_gid_entry(entry);
1294}
1295EXPORT_SYMBOL(rdma_hold_gid_attr);
1296
598ff6ba
PP
1297static int config_non_roce_gid_cache(struct ib_device *device,
1298 u8 port, int gid_tbl_len)
1299{
1300 struct ib_gid_attr gid_attr = {};
1301 struct ib_gid_table *table;
598ff6ba
PP
1302 int ret = 0;
1303 int i;
1304
1305 gid_attr.device = device;
1306 gid_attr.port_num = port;
724631a9 1307 table = rdma_gid_table(device, port);
598ff6ba
PP
1308
1309 mutex_lock(&table->lock);
1310 for (i = 0; i < gid_tbl_len; ++i) {
1311 if (!device->query_gid)
1312 continue;
b150c386 1313 ret = device->query_gid(device, port, i, &gid_attr.gid);
598ff6ba
PP
1314 if (ret) {
1315 pr_warn("query_gid failed (%d) for %s (index %d)\n",
1316 ret, device->name, i);
1317 goto err;
1318 }
1319 gid_attr.index = i;
b150c386 1320 add_modify_gid(table, &gid_attr);
598ff6ba
PP
1321 }
1322err:
1323 mutex_unlock(&table->lock);
1324 return ret;
1325}
1326
1da177e4 1327static void ib_cache_update(struct ib_device *device,
d291f1a6
DJ
1328 u8 port,
1329 bool enforce_security)
1da177e4
LT
1330{
1331 struct ib_port_attr *tprops = NULL;
1332 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
1da177e4
LT
1333 int i;
1334 int ret;
03db3a2d 1335 struct ib_gid_table *table;
03db3a2d 1336
24dc831b 1337 if (!rdma_is_port_valid(device, port))
03db3a2d
MB
1338 return;
1339
724631a9 1340 table = rdma_gid_table(device, port);
1da177e4
LT
1341
1342 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
1343 if (!tprops)
1344 return;
1345
1346 ret = ib_query_port(device, port, tprops);
1347 if (ret) {
aba25a3e
PP
1348 pr_warn("ib_query_port failed (%d) for %s\n",
1349 ret, device->name);
1da177e4
LT
1350 goto err;
1351 }
1352
598ff6ba
PP
1353 if (!rdma_protocol_roce(device, port)) {
1354 ret = config_non_roce_gid_cache(device, port,
1355 tprops->gid_tbl_len);
1356 if (ret)
1357 goto err;
1358 }
1359
acafe7e3
KC
1360 pkey_cache = kmalloc(struct_size(pkey_cache, table,
1361 tprops->pkey_tbl_len),
1362 GFP_KERNEL);
1da177e4
LT
1363 if (!pkey_cache)
1364 goto err;
1365
1366 pkey_cache->table_len = tprops->pkey_tbl_len;
1367
1da177e4
LT
1368 for (i = 0; i < pkey_cache->table_len; ++i) {
1369 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
1370 if (ret) {
aba25a3e
PP
1371 pr_warn("ib_query_pkey failed (%d) for %s (index %d)\n",
1372 ret, device->name, i);
1da177e4
LT
1373 goto err;
1374 }
1375 }
1376
1da177e4
LT
1377 write_lock_irq(&device->cache.lock);
1378
21d6454a
JW
1379 old_pkey_cache = device->cache.ports[port -
1380 rdma_start_port(device)].pkey;
1da177e4 1381
21d6454a 1382 device->cache.ports[port - rdma_start_port(device)].pkey = pkey_cache;
21d6454a
JW
1383 device->cache.ports[port - rdma_start_port(device)].lmc = tprops->lmc;
1384 device->cache.ports[port - rdma_start_port(device)].port_state =
aaaca121 1385 tprops->state;
6fb9cdbf 1386
883c71fe
DJ
1387 device->cache.ports[port - rdma_start_port(device)].subnet_prefix =
1388 tprops->subnet_prefix;
1da177e4
LT
1389 write_unlock_irq(&device->cache.lock);
1390
d291f1a6
DJ
1391 if (enforce_security)
1392 ib_security_cache_change(device,
1393 port,
1394 tprops->subnet_prefix);
1395
1da177e4 1396 kfree(old_pkey_cache);
1da177e4
LT
1397 kfree(tprops);
1398 return;
1399
1400err:
1401 kfree(pkey_cache);
1da177e4
LT
1402 kfree(tprops);
1403}
1404
c4028958 1405static void ib_cache_task(struct work_struct *_work)
1da177e4 1406{
c4028958
DH
1407 struct ib_update_work *work =
1408 container_of(_work, struct ib_update_work, work);
1da177e4 1409
d291f1a6
DJ
1410 ib_cache_update(work->device,
1411 work->port_num,
1412 work->enforce_security);
1da177e4
LT
1413 kfree(work);
1414}
1415
1416static void ib_cache_event(struct ib_event_handler *handler,
1417 struct ib_event *event)
1418{
1419 struct ib_update_work *work;
1420
1421 if (event->event == IB_EVENT_PORT_ERR ||
1422 event->event == IB_EVENT_PORT_ACTIVE ||
1423 event->event == IB_EVENT_LID_CHANGE ||
1424 event->event == IB_EVENT_PKEY_CHANGE ||
acaea9ee 1425 event->event == IB_EVENT_SM_CHANGE ||
761d90ed
OG
1426 event->event == IB_EVENT_CLIENT_REREGISTER ||
1427 event->event == IB_EVENT_GID_CHANGE) {
1da177e4
LT
1428 work = kmalloc(sizeof *work, GFP_ATOMIC);
1429 if (work) {
c4028958 1430 INIT_WORK(&work->work, ib_cache_task);
1da177e4
LT
1431 work->device = event->device;
1432 work->port_num = event->element.port_num;
d291f1a6
DJ
1433 if (event->event == IB_EVENT_PKEY_CHANGE ||
1434 event->event == IB_EVENT_GID_CHANGE)
1435 work->enforce_security = true;
1436 else
1437 work->enforce_security = false;
1438
f0626710 1439 queue_work(ib_wq, &work->work);
1da177e4
LT
1440 }
1441 }
1442}
1443
03db3a2d 1444int ib_cache_setup_one(struct ib_device *device)
1da177e4
LT
1445{
1446 int p;
03db3a2d 1447 int err;
1da177e4
LT
1448
1449 rwlock_init(&device->cache.lock);
1450
21d6454a 1451 device->cache.ports =
6396bb22
KC
1452 kcalloc(rdma_end_port(device) - rdma_start_port(device) + 1,
1453 sizeof(*device->cache.ports),
1454 GFP_KERNEL);
dcc9881e
LR
1455 if (!device->cache.ports)
1456 return -ENOMEM;
1da177e4 1457
03db3a2d 1458 err = gid_table_setup_one(device);
dcc9881e
LR
1459 if (err) {
1460 kfree(device->cache.ports);
1461 device->cache.ports = NULL;
1462 return err;
1463 }
03db3a2d 1464
55aeed06 1465 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
d291f1a6 1466 ib_cache_update(device, p + rdma_start_port(device), true);
1da177e4
LT
1467
1468 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
1469 device, ib_cache_event);
dcc9881e 1470 ib_register_event_handler(&device->cache.event_handler);
03db3a2d 1471 return 0;
1da177e4
LT
1472}
1473
03db3a2d 1474void ib_cache_release_one(struct ib_device *device)
1da177e4
LT
1475{
1476 int p;
1477
03db3a2d
MB
1478 /*
1479 * The release function frees all the cache elements.
1480 * This function should be called as part of freeing
1481 * all the device's resources when the cache could no
1482 * longer be accessed.
1483 */
21d6454a
JW
1484 for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p)
1485 kfree(device->cache.ports[p].pkey);
03db3a2d
MB
1486
1487 gid_table_release_one(device);
21d6454a 1488 kfree(device->cache.ports);
1da177e4
LT
1489}
1490
03db3a2d
MB
1491void ib_cache_cleanup_one(struct ib_device *device)
1492{
1493 /* The cleanup function unregisters the event handler,
1494 * waits for all in-progress workqueue elements and cleans
1495 * up the GID cache. This function should be called after
1496 * the device was removed from the devices list and all
1497 * clients were removed, so the cache exists but is
1498 * non-functional and shouldn't be updated anymore.
1499 */
1500 ib_unregister_event_handler(&device->cache.event_handler);
1501 flush_workqueue(ib_wq);
1502 gid_table_cleanup_one(device);
b150c386
PP
1503
1504 /*
1505 * Flush the wq second time for any pending GID delete work.
1506 */
1507 flush_workqueue(ib_wq);
03db3a2d 1508}