2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * $Id: cache.c 1349 2004-12-16 21:09:43Z roland $
35 #include <linux/version.h>
36 #include <linux/module.h>
37 #include <linux/errno.h>
38 #include <linux/slab.h>
42 #include "core_priv.h"
44 struct ib_pkey_cache
{
51 union ib_gid table
[0];
54 struct ib_update_work
{
55 struct work_struct work
;
56 struct ib_device
*device
;
60 static inline int start_port(struct ib_device
*device
)
62 return device
->node_type
== IB_NODE_SWITCH
? 0 : 1;
65 static inline int end_port(struct ib_device
*device
)
67 return device
->node_type
== IB_NODE_SWITCH
? 0 : device
->phys_port_cnt
;
70 int ib_get_cached_gid(struct ib_device
*device
,
75 struct ib_gid_cache
*cache
;
79 if (port_num
< start_port(device
) || port_num
> end_port(device
))
82 read_lock_irqsave(&device
->cache
.lock
, flags
);
84 cache
= device
->cache
.gid_cache
[port_num
- start_port(device
)];
86 if (index
< 0 || index
>= cache
->table_len
)
89 *gid
= cache
->table
[index
];
91 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
95 EXPORT_SYMBOL(ib_get_cached_gid
);
97 int ib_find_cached_gid(struct ib_device
*device
,
102 struct ib_gid_cache
*cache
;
111 read_lock_irqsave(&device
->cache
.lock
, flags
);
113 for (p
= 0; p
<= end_port(device
) - start_port(device
); ++p
) {
114 cache
= device
->cache
.gid_cache
[p
];
115 for (i
= 0; i
< cache
->table_len
; ++i
) {
116 if (!memcmp(gid
, &cache
->table
[i
], sizeof *gid
)) {
117 *port_num
= p
+ start_port(device
);
126 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
130 EXPORT_SYMBOL(ib_find_cached_gid
);
132 int ib_get_cached_pkey(struct ib_device
*device
,
137 struct ib_pkey_cache
*cache
;
141 if (port_num
< start_port(device
) || port_num
> end_port(device
))
144 read_lock_irqsave(&device
->cache
.lock
, flags
);
146 cache
= device
->cache
.pkey_cache
[port_num
- start_port(device
)];
148 if (index
< 0 || index
>= cache
->table_len
)
151 *pkey
= cache
->table
[index
];
153 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
157 EXPORT_SYMBOL(ib_get_cached_pkey
);
159 int ib_find_cached_pkey(struct ib_device
*device
,
164 struct ib_pkey_cache
*cache
;
169 if (port_num
< start_port(device
) || port_num
> end_port(device
))
172 read_lock_irqsave(&device
->cache
.lock
, flags
);
174 cache
= device
->cache
.pkey_cache
[port_num
- start_port(device
)];
178 for (i
= 0; i
< cache
->table_len
; ++i
)
179 if ((cache
->table
[i
] & 0x7fff) == (pkey
& 0x7fff)) {
185 read_unlock_irqrestore(&device
->cache
.lock
, flags
);
189 EXPORT_SYMBOL(ib_find_cached_pkey
);
191 static void ib_cache_update(struct ib_device
*device
,
194 struct ib_port_attr
*tprops
= NULL
;
195 struct ib_pkey_cache
*pkey_cache
= NULL
, *old_pkey_cache
;
196 struct ib_gid_cache
*gid_cache
= NULL
, *old_gid_cache
;
200 tprops
= kmalloc(sizeof *tprops
, GFP_KERNEL
);
204 ret
= ib_query_port(device
, port
, tprops
);
206 printk(KERN_WARNING
"ib_query_port failed (%d) for %s\n",
211 pkey_cache
= kmalloc(sizeof *pkey_cache
+ tprops
->pkey_tbl_len
*
212 sizeof *pkey_cache
->table
, GFP_KERNEL
);
216 pkey_cache
->table_len
= tprops
->pkey_tbl_len
;
218 gid_cache
= kmalloc(sizeof *gid_cache
+ tprops
->gid_tbl_len
*
219 sizeof *gid_cache
->table
, GFP_KERNEL
);
223 gid_cache
->table_len
= tprops
->gid_tbl_len
;
225 for (i
= 0; i
< pkey_cache
->table_len
; ++i
) {
226 ret
= ib_query_pkey(device
, port
, i
, pkey_cache
->table
+ i
);
228 printk(KERN_WARNING
"ib_query_pkey failed (%d) for %s (index %d)\n",
229 ret
, device
->name
, i
);
234 for (i
= 0; i
< gid_cache
->table_len
; ++i
) {
235 ret
= ib_query_gid(device
, port
, i
, gid_cache
->table
+ i
);
237 printk(KERN_WARNING
"ib_query_gid failed (%d) for %s (index %d)\n",
238 ret
, device
->name
, i
);
243 write_lock_irq(&device
->cache
.lock
);
245 old_pkey_cache
= device
->cache
.pkey_cache
[port
- start_port(device
)];
246 old_gid_cache
= device
->cache
.gid_cache
[port
- start_port(device
)];
248 device
->cache
.pkey_cache
[port
- start_port(device
)] = pkey_cache
;
249 device
->cache
.gid_cache
[port
- start_port(device
)] = gid_cache
;
251 write_unlock_irq(&device
->cache
.lock
);
253 kfree(old_pkey_cache
);
254 kfree(old_gid_cache
);
264 static void ib_cache_task(void *work_ptr
)
266 struct ib_update_work
*work
= work_ptr
;
268 ib_cache_update(work
->device
, work
->port_num
);
272 static void ib_cache_event(struct ib_event_handler
*handler
,
273 struct ib_event
*event
)
275 struct ib_update_work
*work
;
277 if (event
->event
== IB_EVENT_PORT_ERR
||
278 event
->event
== IB_EVENT_PORT_ACTIVE
||
279 event
->event
== IB_EVENT_LID_CHANGE
||
280 event
->event
== IB_EVENT_PKEY_CHANGE
||
281 event
->event
== IB_EVENT_SM_CHANGE
) {
282 work
= kmalloc(sizeof *work
, GFP_ATOMIC
);
284 INIT_WORK(&work
->work
, ib_cache_task
, work
);
285 work
->device
= event
->device
;
286 work
->port_num
= event
->element
.port_num
;
287 schedule_work(&work
->work
);
292 static void ib_cache_setup_one(struct ib_device
*device
)
296 rwlock_init(&device
->cache
.lock
);
298 device
->cache
.pkey_cache
=
299 kmalloc(sizeof *device
->cache
.pkey_cache
*
300 (end_port(device
) - start_port(device
) + 1), GFP_KERNEL
);
301 device
->cache
.gid_cache
=
302 kmalloc(sizeof *device
->cache
.pkey_cache
*
303 (end_port(device
) - start_port(device
) + 1), GFP_KERNEL
);
305 if (!device
->cache
.pkey_cache
|| !device
->cache
.gid_cache
) {
306 printk(KERN_WARNING
"Couldn't allocate cache "
307 "for %s\n", device
->name
);
311 for (p
= 0; p
<= end_port(device
) - start_port(device
); ++p
) {
312 device
->cache
.pkey_cache
[p
] = NULL
;
313 device
->cache
.gid_cache
[p
] = NULL
;
314 ib_cache_update(device
, p
+ start_port(device
));
317 INIT_IB_EVENT_HANDLER(&device
->cache
.event_handler
,
318 device
, ib_cache_event
);
319 if (ib_register_event_handler(&device
->cache
.event_handler
))
325 for (p
= 0; p
<= end_port(device
) - start_port(device
); ++p
) {
326 kfree(device
->cache
.pkey_cache
[p
]);
327 kfree(device
->cache
.gid_cache
[p
]);
331 kfree(device
->cache
.pkey_cache
);
332 kfree(device
->cache
.gid_cache
);
335 static void ib_cache_cleanup_one(struct ib_device
*device
)
339 ib_unregister_event_handler(&device
->cache
.event_handler
);
340 flush_scheduled_work();
342 for (p
= 0; p
<= end_port(device
) - start_port(device
); ++p
) {
343 kfree(device
->cache
.pkey_cache
[p
]);
344 kfree(device
->cache
.gid_cache
[p
]);
347 kfree(device
->cache
.pkey_cache
);
348 kfree(device
->cache
.gid_cache
);
351 static struct ib_client cache_client
= {
353 .add
= ib_cache_setup_one
,
354 .remove
= ib_cache_cleanup_one
357 int __init
ib_cache_setup(void)
359 return ib_register_client(&cache_client
);
362 void __exit
ib_cache_cleanup(void)
364 ib_unregister_client(&cache_client
);