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
;
58 static union ib_gid zgid
;
60 static const struct ib_gid_attr zattr
;
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,
68 enum gid_table_entry_props
{
69 GID_TABLE_ENTRY_INVALID
= 1UL << 0,
70 GID_TABLE_ENTRY_DEFAULT
= 1UL << 1,
73 enum gid_table_write_action
{
74 GID_TABLE_WRITE_ACTION_ADD
,
75 GID_TABLE_WRITE_ACTION_DEL
,
76 /* MODIFY only updates the GID table. Currently only used by
79 GID_TABLE_WRITE_ACTION_MODIFY
82 struct ib_gid_table_entry
{
83 /* This lock protects an entry from being
84 * read and written simultaneously.
89 struct ib_gid_attr attr
;
95 /* In RoCE, adding a GID to the table requires:
96 * (a) Find if this GID is already exists.
97 * (b) Find a free space.
98 * (c) Write the new GID
100 * Delete requires different set of operations:
104 * Add/delete should be carried out atomically.
105 * This is done by locking this mutex from multiple
106 * writers. We don't need this lock for IB, as the MAD
107 * layer replaces all entries. All data_vec entries
108 * are locked by this lock.
111 struct ib_gid_table_entry
*data_vec
;
114 static int write_gid(struct ib_device
*ib_dev
, u8 port
,
115 struct ib_gid_table
*table
, int ix
,
116 const union ib_gid
*gid
,
117 const struct ib_gid_attr
*attr
,
118 enum gid_table_write_action action
,
122 struct net_device
*old_net_dev
;
125 /* in rdma_cap_roce_gid_table, this funciton should be protected by a
128 write_lock_irqsave(&table
->data_vec
[ix
].lock
, flags
);
130 if (rdma_cap_roce_gid_table(ib_dev
, port
)) {
131 table
->data_vec
[ix
].props
|= GID_TABLE_ENTRY_INVALID
;
132 write_unlock_irqrestore(&table
->data_vec
[ix
].lock
, flags
);
133 /* GID_TABLE_WRITE_ACTION_MODIFY currently isn't supported by
134 * RoCE providers and thus only updates the cache.
136 if (action
== GID_TABLE_WRITE_ACTION_ADD
)
137 ret
= ib_dev
->add_gid(ib_dev
, port
, ix
, gid
, attr
,
138 &table
->data_vec
[ix
].context
);
139 else if (action
== GID_TABLE_WRITE_ACTION_DEL
)
140 ret
= ib_dev
->del_gid(ib_dev
, port
, ix
,
141 &table
->data_vec
[ix
].context
);
142 write_lock_irqsave(&table
->data_vec
[ix
].lock
, flags
);
145 old_net_dev
= table
->data_vec
[ix
].attr
.ndev
;
146 if (old_net_dev
&& old_net_dev
!= attr
->ndev
)
147 dev_put(old_net_dev
);
148 /* if modify_gid failed, just delete the old gid */
149 if (ret
|| action
== GID_TABLE_WRITE_ACTION_DEL
) {
152 table
->data_vec
[ix
].context
= NULL
;
155 table
->data_vec
[ix
].props
|= GID_TABLE_ENTRY_DEFAULT
;
156 memcpy(&table
->data_vec
[ix
].gid
, gid
, sizeof(*gid
));
157 memcpy(&table
->data_vec
[ix
].attr
, attr
, sizeof(*attr
));
158 if (table
->data_vec
[ix
].attr
.ndev
&&
159 table
->data_vec
[ix
].attr
.ndev
!= old_net_dev
)
160 dev_hold(table
->data_vec
[ix
].attr
.ndev
);
162 table
->data_vec
[ix
].props
&= ~GID_TABLE_ENTRY_INVALID
;
164 write_unlock_irqrestore(&table
->data_vec
[ix
].lock
, flags
);
166 if (!ret
&& rdma_cap_roce_gid_table(ib_dev
, port
)) {
167 struct ib_event event
;
169 event
.device
= ib_dev
;
170 event
.element
.port_num
= port
;
171 event
.event
= IB_EVENT_GID_CHANGE
;
173 ib_dispatch_event(&event
);
178 static int add_gid(struct ib_device
*ib_dev
, u8 port
,
179 struct ib_gid_table
*table
, int ix
,
180 const union ib_gid
*gid
,
181 const struct ib_gid_attr
*attr
,
183 return write_gid(ib_dev
, port
, table
, ix
, gid
, attr
,
184 GID_TABLE_WRITE_ACTION_ADD
, default_gid
);
187 static int modify_gid(struct ib_device
*ib_dev
, u8 port
,
188 struct ib_gid_table
*table
, int ix
,
189 const union ib_gid
*gid
,
190 const struct ib_gid_attr
*attr
,
192 return write_gid(ib_dev
, port
, table
, ix
, gid
, attr
,
193 GID_TABLE_WRITE_ACTION_MODIFY
, default_gid
);
196 static int del_gid(struct ib_device
*ib_dev
, u8 port
,
197 struct ib_gid_table
*table
, int ix
,
199 return write_gid(ib_dev
, port
, table
, ix
, &zgid
, &zattr
,
200 GID_TABLE_WRITE_ACTION_DEL
, default_gid
);
203 static int find_gid(struct ib_gid_table
*table
, const union ib_gid
*gid
,
204 const struct ib_gid_attr
*val
, bool default_gid
,
209 for (i
= 0; i
< table
->sz
; i
++) {
211 struct ib_gid_attr
*attr
= &table
->data_vec
[i
].attr
;
213 read_lock_irqsave(&table
->data_vec
[i
].lock
, flags
);
215 if (table
->data_vec
[i
].props
& GID_TABLE_ENTRY_INVALID
)
218 if (mask
& GID_ATTR_FIND_MASK_GID
&&
219 memcmp(gid
, &table
->data_vec
[i
].gid
, sizeof(*gid
)))
222 if (mask
& GID_ATTR_FIND_MASK_NETDEV
&&
223 attr
->ndev
!= val
->ndev
)
226 if (mask
& GID_ATTR_FIND_MASK_DEFAULT
&&
227 !!(table
->data_vec
[i
].props
& GID_TABLE_ENTRY_DEFAULT
) !=
231 read_unlock_irqrestore(&table
->data_vec
[i
].lock
, flags
);
234 read_unlock_irqrestore(&table
->data_vec
[i
].lock
, flags
);
240 static void make_default_gid(struct net_device
*dev
, union ib_gid
*gid
)
242 gid
->global
.subnet_prefix
= cpu_to_be64(0xfe80000000000000LL
);
243 addrconf_ifid_eui48(&gid
->raw
[8], dev
);
246 int ib_cache_gid_add(struct ib_device
*ib_dev
, u8 port
,
247 union ib_gid
*gid
, struct ib_gid_attr
*attr
)
249 struct ib_gid_table
**ports_table
= ib_dev
->cache
.gid_cache
;
250 struct ib_gid_table
*table
;
253 struct net_device
*idev
;
255 table
= ports_table
[port
- rdma_start_port(ib_dev
)];
257 if (!memcmp(gid
, &zgid
, sizeof(*gid
)))
260 if (ib_dev
->get_netdev
) {
261 idev
= ib_dev
->get_netdev(ib_dev
, port
);
262 if (idev
&& attr
->ndev
!= idev
) {
263 union ib_gid default_gid
;
265 /* Adding default GIDs in not permitted */
266 make_default_gid(idev
, &default_gid
);
267 if (!memcmp(gid
, &default_gid
, sizeof(*gid
))) {
276 mutex_lock(&table
->lock
);
278 ix
= find_gid(table
, gid
, attr
, false, GID_ATTR_FIND_MASK_GID
|
279 GID_ATTR_FIND_MASK_NETDEV
);
283 ix
= find_gid(table
, &zgid
, NULL
, false, GID_ATTR_FIND_MASK_GID
|
284 GID_ATTR_FIND_MASK_DEFAULT
);
290 add_gid(ib_dev
, port
, table
, ix
, gid
, attr
, false);
293 mutex_unlock(&table
->lock
);
297 int ib_cache_gid_del(struct ib_device
*ib_dev
, u8 port
,
298 union ib_gid
*gid
, struct ib_gid_attr
*attr
)
300 struct ib_gid_table
**ports_table
= ib_dev
->cache
.gid_cache
;
301 struct ib_gid_table
*table
;
304 table
= ports_table
[port
- rdma_start_port(ib_dev
)];
306 mutex_lock(&table
->lock
);
308 ix
= find_gid(table
, gid
, attr
, false,
309 GID_ATTR_FIND_MASK_GID
|
310 GID_ATTR_FIND_MASK_NETDEV
|
311 GID_ATTR_FIND_MASK_DEFAULT
);
315 del_gid(ib_dev
, port
, table
, ix
, false);
318 mutex_unlock(&table
->lock
);
322 int ib_cache_gid_del_all_netdev_gids(struct ib_device
*ib_dev
, u8 port
,
323 struct net_device
*ndev
)
325 struct ib_gid_table
**ports_table
= ib_dev
->cache
.gid_cache
;
326 struct ib_gid_table
*table
;
329 table
= ports_table
[port
- rdma_start_port(ib_dev
)];
331 mutex_lock(&table
->lock
);
333 for (ix
= 0; ix
< table
->sz
; ix
++)
334 if (table
->data_vec
[ix
].attr
.ndev
== ndev
)
335 del_gid(ib_dev
, port
, table
, ix
, false);
337 mutex_unlock(&table
->lock
);
341 static int __ib_cache_gid_get(struct ib_device
*ib_dev
, u8 port
, int index
,
342 union ib_gid
*gid
, struct ib_gid_attr
*attr
)
344 struct ib_gid_table
**ports_table
= ib_dev
->cache
.gid_cache
;
345 struct ib_gid_table
*table
;
348 table
= ports_table
[port
- rdma_start_port(ib_dev
)];
350 if (index
< 0 || index
>= table
->sz
)
353 read_lock_irqsave(&table
->data_vec
[index
].lock
, flags
);
354 if (table
->data_vec
[index
].props
& GID_TABLE_ENTRY_INVALID
) {
355 read_unlock_irqrestore(&table
->data_vec
[index
].lock
, flags
);
359 memcpy(gid
, &table
->data_vec
[index
].gid
, sizeof(*gid
));
361 memcpy(attr
, &table
->data_vec
[index
].attr
, sizeof(*attr
));
363 dev_hold(attr
->ndev
);
366 read_unlock_irqrestore(&table
->data_vec
[index
].lock
, flags
);
370 static int _ib_cache_gid_table_find(struct ib_device
*ib_dev
,
371 const union ib_gid
*gid
,
372 const struct ib_gid_attr
*val
,
374 u8
*port
, u16
*index
)
376 struct ib_gid_table
**ports_table
= ib_dev
->cache
.gid_cache
;
377 struct ib_gid_table
*table
;
381 for (p
= 0; p
< ib_dev
->phys_port_cnt
; p
++) {
382 table
= ports_table
[p
];
383 local_index
= find_gid(table
, gid
, val
, false, mask
);
384 if (local_index
>= 0) {
386 *index
= local_index
;
388 *port
= p
+ rdma_start_port(ib_dev
);
396 static int ib_cache_gid_find(struct ib_device
*ib_dev
,
397 const union ib_gid
*gid
,
398 struct net_device
*ndev
, u8
*port
,
401 unsigned long mask
= GID_ATTR_FIND_MASK_GID
;
402 struct ib_gid_attr gid_attr_val
= {.ndev
= ndev
};
405 mask
|= GID_ATTR_FIND_MASK_NETDEV
;
407 return _ib_cache_gid_table_find(ib_dev
, gid
, &gid_attr_val
,
411 int ib_cache_gid_find_by_port(struct ib_device
*ib_dev
,
412 const union ib_gid
*gid
,
413 u8 port
, struct net_device
*ndev
,
417 struct ib_gid_table
**ports_table
= ib_dev
->cache
.gid_cache
;
418 struct ib_gid_table
*table
;
419 unsigned long mask
= GID_ATTR_FIND_MASK_GID
;
420 struct ib_gid_attr val
= {.ndev
= ndev
};
422 if (port
< rdma_start_port(ib_dev
) ||
423 port
> rdma_end_port(ib_dev
))
426 table
= ports_table
[port
- rdma_start_port(ib_dev
)];
429 mask
|= GID_ATTR_FIND_MASK_NETDEV
;
431 local_index
= find_gid(table
, gid
, &val
, false, mask
);
432 if (local_index
>= 0) {
434 *index
= local_index
;
441 static struct ib_gid_table
*alloc_gid_table(int sz
)
444 struct ib_gid_table
*table
=
445 kzalloc(sizeof(struct ib_gid_table
), GFP_KERNEL
);
449 table
->data_vec
= kcalloc(sz
, sizeof(*table
->data_vec
), GFP_KERNEL
);
450 if (!table
->data_vec
)
453 mutex_init(&table
->lock
);
457 for (i
= 0; i
< sz
; i
++)
458 rwlock_init(&table
->data_vec
[i
].lock
);
467 static void release_gid_table(struct ib_gid_table
*table
)
470 kfree(table
->data_vec
);
475 static void cleanup_gid_table_port(struct ib_device
*ib_dev
, u8 port
,
476 struct ib_gid_table
*table
)
483 for (i
= 0; i
< table
->sz
; ++i
) {
484 if (memcmp(&table
->data_vec
[i
].gid
, &zgid
,
485 sizeof(table
->data_vec
[i
].gid
)))
486 del_gid(ib_dev
, port
, table
, i
,
487 table
->data_vec
[i
].props
&
488 GID_ATTR_FIND_MASK_DEFAULT
);
492 void ib_cache_gid_set_default_gid(struct ib_device
*ib_dev
, u8 port
,
493 struct net_device
*ndev
,
494 enum ib_cache_gid_default_mode mode
)
496 struct ib_gid_table
**ports_table
= ib_dev
->cache
.gid_cache
;
498 struct ib_gid_attr gid_attr
;
499 struct ib_gid_table
*table
;
501 union ib_gid current_gid
;
502 struct ib_gid_attr current_gid_attr
= {};
504 table
= ports_table
[port
- rdma_start_port(ib_dev
)];
506 make_default_gid(ndev
, &gid
);
507 memset(&gid_attr
, 0, sizeof(gid_attr
));
508 gid_attr
.ndev
= ndev
;
510 ix
= find_gid(table
, NULL
, NULL
, true, GID_ATTR_FIND_MASK_DEFAULT
);
512 /* Coudn't find default GID location */
515 mutex_lock(&table
->lock
);
516 if (!__ib_cache_gid_get(ib_dev
, port
, ix
,
517 ¤t_gid
, ¤t_gid_attr
) &&
518 mode
== IB_CACHE_GID_DEFAULT_MODE_SET
&&
519 !memcmp(&gid
, ¤t_gid
, sizeof(gid
)) &&
520 !memcmp(&gid_attr
, ¤t_gid_attr
, sizeof(gid_attr
)))
523 if ((memcmp(¤t_gid
, &zgid
, sizeof(current_gid
)) ||
524 memcmp(¤t_gid_attr
, &zattr
,
525 sizeof(current_gid_attr
))) &&
526 del_gid(ib_dev
, port
, table
, ix
, true)) {
527 pr_warn("ib_cache_gid: can't delete index %d for default gid %pI6\n",
532 if (mode
== IB_CACHE_GID_DEFAULT_MODE_SET
)
533 if (add_gid(ib_dev
, port
, table
, ix
, &gid
, &gid_attr
, true))
534 pr_warn("ib_cache_gid: unable to add default gid %pI6\n",
538 if (current_gid_attr
.ndev
)
539 dev_put(current_gid_attr
.ndev
);
540 mutex_unlock(&table
->lock
);
543 static int gid_table_reserve_default(struct ib_device
*ib_dev
, u8 port
,
544 struct ib_gid_table
*table
)
546 if (rdma_protocol_roce(ib_dev
, port
)) {
547 struct ib_gid_table_entry
*entry
= &table
->data_vec
[0];
549 entry
->props
|= GID_TABLE_ENTRY_DEFAULT
;
555 static int _gid_table_setup_one(struct ib_device
*ib_dev
)
558 struct ib_gid_table
**table
;
561 table
= kcalloc(ib_dev
->phys_port_cnt
, sizeof(*table
), GFP_KERNEL
);
564 pr_warn("failed to allocate ib gid cache for %s\n",
569 for (port
= 0; port
< ib_dev
->phys_port_cnt
; port
++) {
570 u8 rdma_port
= port
+ rdma_start_port(ib_dev
);
574 ib_dev
->port_immutable
[rdma_port
].gid_tbl_len
);
577 goto rollback_table_setup
;
580 err
= gid_table_reserve_default(ib_dev
,
581 port
+ rdma_start_port(ib_dev
),
584 goto rollback_table_setup
;
587 ib_dev
->cache
.gid_cache
= table
;
590 rollback_table_setup
:
591 for (port
= 0; port
< ib_dev
->phys_port_cnt
; port
++) {
592 cleanup_gid_table_port(ib_dev
, port
+ rdma_start_port(ib_dev
),
594 release_gid_table(table
[port
]);
601 static void gid_table_release_one(struct ib_device
*ib_dev
)
603 struct ib_gid_table
**table
= ib_dev
->cache
.gid_cache
;
609 for (port
= 0; port
< ib_dev
->phys_port_cnt
; port
++)
610 release_gid_table(table
[port
]);
613 ib_dev
->cache
.gid_cache
= NULL
;
616 static void gid_table_cleanup_one(struct ib_device
*ib_dev
)
618 struct ib_gid_table
**table
= ib_dev
->cache
.gid_cache
;
624 for (port
= 0; port
< ib_dev
->phys_port_cnt
; port
++)
625 cleanup_gid_table_port(ib_dev
, port
+ rdma_start_port(ib_dev
),
629 static int gid_table_setup_one(struct ib_device
*ib_dev
)
633 err
= _gid_table_setup_one(ib_dev
);
638 err
= roce_rescan_device(ib_dev
);
641 gid_table_cleanup_one(ib_dev
);
642 gid_table_release_one(ib_dev
);
648 int ib_get_cached_gid(struct ib_device
*device
,
653 if (port_num
< rdma_start_port(device
) || port_num
> rdma_end_port(device
))
656 return __ib_cache_gid_get(device
, port_num
, index
, gid
, NULL
);
658 EXPORT_SYMBOL(ib_get_cached_gid
);
660 int ib_find_cached_gid(struct ib_device
*device
,
661 const union ib_gid
*gid
,
665 return ib_cache_gid_find(device
, gid
, NULL
, port_num
, index
);
667 EXPORT_SYMBOL(ib_find_cached_gid
);
669 int ib_get_cached_pkey(struct ib_device
*device
,
674 struct ib_pkey_cache
*cache
;
678 if (port_num
< rdma_start_port(device
) || port_num
> rdma_end_port(device
))
681 read_lock_irqsave(&device
->cache
.lock
, flags
);
683 cache
= device
->cache
.pkey_cache
[port_num
- rdma_start_port(device
)];
685 if (index
< 0 || index
>= cache
->table_len
)
688 *pkey
= cache
->table
[index
];
690 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
694 EXPORT_SYMBOL(ib_get_cached_pkey
);
696 int ib_find_cached_pkey(struct ib_device
*device
,
701 struct ib_pkey_cache
*cache
;
707 if (port_num
< rdma_start_port(device
) || port_num
> rdma_end_port(device
))
710 read_lock_irqsave(&device
->cache
.lock
, flags
);
712 cache
= device
->cache
.pkey_cache
[port_num
- rdma_start_port(device
)];
716 for (i
= 0; i
< cache
->table_len
; ++i
)
717 if ((cache
->table
[i
] & 0x7fff) == (pkey
& 0x7fff)) {
718 if (cache
->table
[i
] & 0x8000) {
726 if (ret
&& partial_ix
>= 0) {
731 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
735 EXPORT_SYMBOL(ib_find_cached_pkey
);
737 int ib_find_exact_cached_pkey(struct ib_device
*device
,
742 struct ib_pkey_cache
*cache
;
747 if (port_num
< rdma_start_port(device
) || port_num
> rdma_end_port(device
))
750 read_lock_irqsave(&device
->cache
.lock
, flags
);
752 cache
= device
->cache
.pkey_cache
[port_num
- rdma_start_port(device
)];
756 for (i
= 0; i
< cache
->table_len
; ++i
)
757 if (cache
->table
[i
] == pkey
) {
763 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
767 EXPORT_SYMBOL(ib_find_exact_cached_pkey
);
769 int ib_get_cached_lmc(struct ib_device
*device
,
776 if (port_num
< rdma_start_port(device
) || port_num
> rdma_end_port(device
))
779 read_lock_irqsave(&device
->cache
.lock
, flags
);
780 *lmc
= device
->cache
.lmc_cache
[port_num
- rdma_start_port(device
)];
781 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
785 EXPORT_SYMBOL(ib_get_cached_lmc
);
787 static void ib_cache_update(struct ib_device
*device
,
790 struct ib_port_attr
*tprops
= NULL
;
791 struct ib_pkey_cache
*pkey_cache
= NULL
, *old_pkey_cache
;
792 struct ib_gid_cache
{
794 union ib_gid table
[0];
798 struct ib_gid_table
*table
;
799 struct ib_gid_table
**ports_table
= device
->cache
.gid_cache
;
800 bool use_roce_gid_table
=
801 rdma_cap_roce_gid_table(device
, port
);
803 if (port
< rdma_start_port(device
) || port
> rdma_end_port(device
))
806 table
= ports_table
[port
- rdma_start_port(device
)];
808 tprops
= kmalloc(sizeof *tprops
, GFP_KERNEL
);
812 ret
= ib_query_port(device
, port
, tprops
);
814 printk(KERN_WARNING
"ib_query_port failed (%d) for %s\n",
819 pkey_cache
= kmalloc(sizeof *pkey_cache
+ tprops
->pkey_tbl_len
*
820 sizeof *pkey_cache
->table
, GFP_KERNEL
);
824 pkey_cache
->table_len
= tprops
->pkey_tbl_len
;
826 if (!use_roce_gid_table
) {
827 gid_cache
= kmalloc(sizeof(*gid_cache
) + tprops
->gid_tbl_len
*
828 sizeof(*gid_cache
->table
), GFP_KERNEL
);
832 gid_cache
->table_len
= tprops
->gid_tbl_len
;
835 for (i
= 0; i
< pkey_cache
->table_len
; ++i
) {
836 ret
= ib_query_pkey(device
, port
, i
, pkey_cache
->table
+ i
);
838 printk(KERN_WARNING
"ib_query_pkey failed (%d) for %s (index %d)\n",
839 ret
, device
->name
, i
);
844 if (!use_roce_gid_table
) {
845 for (i
= 0; i
< gid_cache
->table_len
; ++i
) {
846 ret
= ib_query_gid(device
, port
, i
,
847 gid_cache
->table
+ i
);
849 printk(KERN_WARNING
"ib_query_gid failed (%d) for %s (index %d)\n",
850 ret
, device
->name
, i
);
856 write_lock_irq(&device
->cache
.lock
);
858 old_pkey_cache
= device
->cache
.pkey_cache
[port
- rdma_start_port(device
)];
860 device
->cache
.pkey_cache
[port
- rdma_start_port(device
)] = pkey_cache
;
861 if (!use_roce_gid_table
) {
862 for (i
= 0; i
< gid_cache
->table_len
; i
++) {
863 modify_gid(device
, port
, table
, i
, gid_cache
->table
+ i
,
868 device
->cache
.lmc_cache
[port
- rdma_start_port(device
)] = tprops
->lmc
;
870 write_unlock_irq(&device
->cache
.lock
);
873 kfree(old_pkey_cache
);
883 static void ib_cache_task(struct work_struct
*_work
)
885 struct ib_update_work
*work
=
886 container_of(_work
, struct ib_update_work
, work
);
888 ib_cache_update(work
->device
, work
->port_num
);
892 static void ib_cache_event(struct ib_event_handler
*handler
,
893 struct ib_event
*event
)
895 struct ib_update_work
*work
;
897 if (event
->event
== IB_EVENT_PORT_ERR
||
898 event
->event
== IB_EVENT_PORT_ACTIVE
||
899 event
->event
== IB_EVENT_LID_CHANGE
||
900 event
->event
== IB_EVENT_PKEY_CHANGE
||
901 event
->event
== IB_EVENT_SM_CHANGE
||
902 event
->event
== IB_EVENT_CLIENT_REREGISTER
||
903 event
->event
== IB_EVENT_GID_CHANGE
) {
904 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
906 INIT_WORK(&work
->work
, ib_cache_task
);
907 work
->device
= event
->device
;
908 work
->port_num
= event
->element
.port_num
;
909 queue_work(ib_wq
, &work
->work
);
914 int ib_cache_setup_one(struct ib_device
*device
)
919 rwlock_init(&device
->cache
.lock
);
921 device
->cache
.pkey_cache
=
922 kzalloc(sizeof *device
->cache
.pkey_cache
*
923 (rdma_end_port(device
) - rdma_start_port(device
) + 1), GFP_KERNEL
);
924 device
->cache
.lmc_cache
= kmalloc(sizeof *device
->cache
.lmc_cache
*
925 (rdma_end_port(device
) -
926 rdma_start_port(device
) + 1),
928 if (!device
->cache
.pkey_cache
||
929 !device
->cache
.lmc_cache
) {
930 printk(KERN_WARNING
"Couldn't allocate cache "
931 "for %s\n", device
->name
);
935 err
= gid_table_setup_one(device
);
937 /* Allocated memory will be cleaned in the release function */
940 for (p
= 0; p
<= rdma_end_port(device
) - rdma_start_port(device
); ++p
)
941 ib_cache_update(device
, p
+ rdma_start_port(device
));
943 INIT_IB_EVENT_HANDLER(&device
->cache
.event_handler
,
944 device
, ib_cache_event
);
945 err
= ib_register_event_handler(&device
->cache
.event_handler
);
952 gid_table_cleanup_one(device
);
956 void ib_cache_release_one(struct ib_device
*device
)
961 * The release function frees all the cache elements.
962 * This function should be called as part of freeing
963 * all the device's resources when the cache could no
964 * longer be accessed.
966 if (device
->cache
.pkey_cache
)
968 p
<= rdma_end_port(device
) - rdma_start_port(device
); ++p
)
969 kfree(device
->cache
.pkey_cache
[p
]);
971 gid_table_release_one(device
);
972 kfree(device
->cache
.pkey_cache
);
973 kfree(device
->cache
.lmc_cache
);
976 void ib_cache_cleanup_one(struct ib_device
*device
)
978 /* The cleanup function unregisters the event handler,
979 * waits for all in-progress workqueue elements and cleans
980 * up the GID cache. This function should be called after
981 * the device was removed from the devices list and all
982 * clients were removed, so the cache exists but is
983 * non-functional and shouldn't be updated anymore.
985 ib_unregister_event_handler(&device
->cache
.event_handler
);
986 flush_workqueue(ib_wq
);
987 gid_table_cleanup_one(device
);
990 void __init
ib_cache_setup(void)
992 roce_gid_mgmt_init();
995 void __exit
ib_cache_cleanup(void)
997 roce_gid_mgmt_cleanup();