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
35 * $Id: cache.c 1349 2004-12-16 21:09:43Z roland $
38 #include <linux/module.h>
39 #include <linux/errno.h>
40 #include <linux/slab.h>
41 #include <linux/sched.h> /* INIT_WORK, schedule_work(), flush_scheduled_work() */
43 #include <rdma/ib_cache.h>
45 #include "core_priv.h"
47 struct ib_pkey_cache
{
54 union ib_gid table
[0];
57 struct ib_update_work
{
58 struct work_struct work
;
59 struct ib_device
*device
;
63 static inline int start_port(struct ib_device
*device
)
65 return device
->node_type
== IB_NODE_SWITCH
? 0 : 1;
68 static inline int end_port(struct ib_device
*device
)
70 return device
->node_type
== IB_NODE_SWITCH
? 0 : device
->phys_port_cnt
;
73 int ib_get_cached_gid(struct ib_device
*device
,
78 struct ib_gid_cache
*cache
;
82 if (port_num
< start_port(device
) || port_num
> end_port(device
))
85 read_lock_irqsave(&device
->cache
.lock
, flags
);
87 cache
= device
->cache
.gid_cache
[port_num
- start_port(device
)];
89 if (index
< 0 || index
>= cache
->table_len
)
92 *gid
= cache
->table
[index
];
94 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
98 EXPORT_SYMBOL(ib_get_cached_gid
);
100 int ib_find_cached_gid(struct ib_device
*device
,
105 struct ib_gid_cache
*cache
;
114 read_lock_irqsave(&device
->cache
.lock
, flags
);
116 for (p
= 0; p
<= end_port(device
) - start_port(device
); ++p
) {
117 cache
= device
->cache
.gid_cache
[p
];
118 for (i
= 0; i
< cache
->table_len
; ++i
) {
119 if (!memcmp(gid
, &cache
->table
[i
], sizeof *gid
)) {
120 *port_num
= p
+ start_port(device
);
129 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
133 EXPORT_SYMBOL(ib_find_cached_gid
);
135 int ib_get_cached_pkey(struct ib_device
*device
,
140 struct ib_pkey_cache
*cache
;
144 if (port_num
< start_port(device
) || port_num
> end_port(device
))
147 read_lock_irqsave(&device
->cache
.lock
, flags
);
149 cache
= device
->cache
.pkey_cache
[port_num
- start_port(device
)];
151 if (index
< 0 || index
>= cache
->table_len
)
154 *pkey
= cache
->table
[index
];
156 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
160 EXPORT_SYMBOL(ib_get_cached_pkey
);
162 int ib_find_cached_pkey(struct ib_device
*device
,
167 struct ib_pkey_cache
*cache
;
172 if (port_num
< start_port(device
) || port_num
> end_port(device
))
175 read_lock_irqsave(&device
->cache
.lock
, flags
);
177 cache
= device
->cache
.pkey_cache
[port_num
- start_port(device
)];
181 for (i
= 0; i
< cache
->table_len
; ++i
)
182 if ((cache
->table
[i
] & 0x7fff) == (pkey
& 0x7fff)) {
188 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
192 EXPORT_SYMBOL(ib_find_cached_pkey
);
194 static void ib_cache_update(struct ib_device
*device
,
197 struct ib_port_attr
*tprops
= NULL
;
198 struct ib_pkey_cache
*pkey_cache
= NULL
, *old_pkey_cache
;
199 struct ib_gid_cache
*gid_cache
= NULL
, *old_gid_cache
;
203 tprops
= kmalloc(sizeof *tprops
, GFP_KERNEL
);
207 ret
= ib_query_port(device
, port
, tprops
);
209 printk(KERN_WARNING
"ib_query_port failed (%d) for %s\n",
214 pkey_cache
= kmalloc(sizeof *pkey_cache
+ tprops
->pkey_tbl_len
*
215 sizeof *pkey_cache
->table
, GFP_KERNEL
);
219 pkey_cache
->table_len
= tprops
->pkey_tbl_len
;
221 gid_cache
= kmalloc(sizeof *gid_cache
+ tprops
->gid_tbl_len
*
222 sizeof *gid_cache
->table
, GFP_KERNEL
);
226 gid_cache
->table_len
= tprops
->gid_tbl_len
;
228 for (i
= 0; i
< pkey_cache
->table_len
; ++i
) {
229 ret
= ib_query_pkey(device
, port
, i
, pkey_cache
->table
+ i
);
231 printk(KERN_WARNING
"ib_query_pkey failed (%d) for %s (index %d)\n",
232 ret
, device
->name
, i
);
237 for (i
= 0; i
< gid_cache
->table_len
; ++i
) {
238 ret
= ib_query_gid(device
, port
, i
, gid_cache
->table
+ i
);
240 printk(KERN_WARNING
"ib_query_gid failed (%d) for %s (index %d)\n",
241 ret
, device
->name
, i
);
246 write_lock_irq(&device
->cache
.lock
);
248 old_pkey_cache
= device
->cache
.pkey_cache
[port
- start_port(device
)];
249 old_gid_cache
= device
->cache
.gid_cache
[port
- start_port(device
)];
251 device
->cache
.pkey_cache
[port
- start_port(device
)] = pkey_cache
;
252 device
->cache
.gid_cache
[port
- start_port(device
)] = gid_cache
;
254 write_unlock_irq(&device
->cache
.lock
);
256 kfree(old_pkey_cache
);
257 kfree(old_gid_cache
);
267 static void ib_cache_task(void *work_ptr
)
269 struct ib_update_work
*work
= work_ptr
;
271 ib_cache_update(work
->device
, work
->port_num
);
275 static void ib_cache_event(struct ib_event_handler
*handler
,
276 struct ib_event
*event
)
278 struct ib_update_work
*work
;
280 if (event
->event
== IB_EVENT_PORT_ERR
||
281 event
->event
== IB_EVENT_PORT_ACTIVE
||
282 event
->event
== IB_EVENT_LID_CHANGE
||
283 event
->event
== IB_EVENT_PKEY_CHANGE
||
284 event
->event
== IB_EVENT_SM_CHANGE
) {
285 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
287 INIT_WORK(&work
->work
, ib_cache_task
, work
);
288 work
->device
= event
->device
;
289 work
->port_num
= event
->element
.port_num
;
290 schedule_work(&work
->work
);
295 static void ib_cache_setup_one(struct ib_device
*device
)
299 rwlock_init(&device
->cache
.lock
);
301 device
->cache
.pkey_cache
=
302 kmalloc(sizeof *device
->cache
.pkey_cache
*
303 (end_port(device
) - start_port(device
) + 1), GFP_KERNEL
);
304 device
->cache
.gid_cache
=
305 kmalloc(sizeof *device
->cache
.gid_cache
*
306 (end_port(device
) - start_port(device
) + 1), GFP_KERNEL
);
308 if (!device
->cache
.pkey_cache
|| !device
->cache
.gid_cache
) {
309 printk(KERN_WARNING
"Couldn't allocate cache "
310 "for %s\n", device
->name
);
314 for (p
= 0; p
<= end_port(device
) - start_port(device
); ++p
) {
315 device
->cache
.pkey_cache
[p
] = NULL
;
316 device
->cache
.gid_cache
[p
] = NULL
;
317 ib_cache_update(device
, p
+ start_port(device
));
320 INIT_IB_EVENT_HANDLER(&device
->cache
.event_handler
,
321 device
, ib_cache_event
);
322 if (ib_register_event_handler(&device
->cache
.event_handler
))
328 for (p
= 0; p
<= end_port(device
) - start_port(device
); ++p
) {
329 kfree(device
->cache
.pkey_cache
[p
]);
330 kfree(device
->cache
.gid_cache
[p
]);
334 kfree(device
->cache
.pkey_cache
);
335 kfree(device
->cache
.gid_cache
);
338 static void ib_cache_cleanup_one(struct ib_device
*device
)
342 ib_unregister_event_handler(&device
->cache
.event_handler
);
343 flush_scheduled_work();
345 for (p
= 0; p
<= end_port(device
) - start_port(device
); ++p
) {
346 kfree(device
->cache
.pkey_cache
[p
]);
347 kfree(device
->cache
.gid_cache
[p
]);
350 kfree(device
->cache
.pkey_cache
);
351 kfree(device
->cache
.gid_cache
);
354 static struct ib_client cache_client
= {
356 .add
= ib_cache_setup_one
,
357 .remove
= ib_cache_cleanup_one
360 int __init
ib_cache_setup(void)
362 return ib_register_client(&cache_client
);
365 void __exit
ib_cache_cleanup(void)
367 ib_unregister_client(&cache_client
);