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>
40 #include "core_priv.h"
42 struct ib_pkey_cache {
49 union ib_gid table[0];
52 struct ib_update_work {
53 struct work_struct work;
54 struct ib_device *device;
58 static inline int start_port(struct ib_device *device)
60 return device->node_type == IB_NODE_SWITCH ? 0 : 1;
63 static inline int end_port(struct ib_device *device)
65 return device->node_type == IB_NODE_SWITCH ? 0 : device->phys_port_cnt;
68 int ib_get_cached_gid(struct ib_device *device,
73 struct ib_gid_cache *cache;
77 if (port_num < start_port(device) || port_num > end_port(device))
80 read_lock_irqsave(&device->cache.lock, flags);
82 cache = device->cache.gid_cache[port_num - start_port(device)];
84 if (index < 0 || index >= cache->table_len)
87 *gid = cache->table[index];
89 read_unlock_irqrestore(&device->cache.lock, flags);
93 EXPORT_SYMBOL(ib_get_cached_gid);
95 int ib_find_cached_gid(struct ib_device *device,
100 struct ib_gid_cache *cache;
109 read_lock_irqsave(&device->cache.lock, flags);
111 for (p = 0; p <= end_port(device) - start_port(device); ++p) {
112 cache = device->cache.gid_cache[p];
113 for (i = 0; i < cache->table_len; ++i) {
114 if (!memcmp(gid, &cache->table[i], sizeof *gid)) {
124 read_unlock_irqrestore(&device->cache.lock, flags);
128 EXPORT_SYMBOL(ib_find_cached_gid);
130 int ib_get_cached_pkey(struct ib_device *device,
135 struct ib_pkey_cache *cache;
139 if (port_num < start_port(device) || port_num > end_port(device))
142 read_lock_irqsave(&device->cache.lock, flags);
144 cache = device->cache.pkey_cache[port_num - start_port(device)];
146 if (index < 0 || index >= cache->table_len)
149 *pkey = cache->table[index];
151 read_unlock_irqrestore(&device->cache.lock, flags);
155 EXPORT_SYMBOL(ib_get_cached_pkey);
157 int ib_find_cached_pkey(struct ib_device *device,
162 struct ib_pkey_cache *cache;
167 if (port_num < start_port(device) || port_num > end_port(device))
170 read_lock_irqsave(&device->cache.lock, flags);
172 cache = device->cache.pkey_cache[port_num - start_port(device)];
176 for (i = 0; i < cache->table_len; ++i)
177 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
183 read_unlock_irqrestore(&device->cache.lock, flags);
187 EXPORT_SYMBOL(ib_find_cached_pkey);
189 static void ib_cache_update(struct ib_device *device,
192 struct ib_port_attr *tprops = NULL;
193 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
194 struct ib_gid_cache *gid_cache = NULL, *old_gid_cache;
198 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
202 ret = ib_query_port(device, port, tprops);
204 printk(KERN_WARNING "ib_query_port failed (%d) for %s\n",
209 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
210 sizeof *pkey_cache->table, GFP_KERNEL);
214 pkey_cache->table_len = tprops->pkey_tbl_len;
216 gid_cache = kmalloc(sizeof *gid_cache + tprops->gid_tbl_len *
217 sizeof *gid_cache->table, GFP_KERNEL);
221 gid_cache->table_len = tprops->gid_tbl_len;
223 for (i = 0; i < pkey_cache->table_len; ++i) {
224 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
226 printk(KERN_WARNING "ib_query_pkey failed (%d) for %s (index %d)\n",
227 ret, device->name, i);
232 for (i = 0; i < gid_cache->table_len; ++i) {
233 ret = ib_query_gid(device, port, i, gid_cache->table + i);
235 printk(KERN_WARNING "ib_query_gid failed (%d) for %s (index %d)\n",
236 ret, device->name, i);
241 write_lock_irq(&device->cache.lock);
243 old_pkey_cache = device->cache.pkey_cache[port - start_port(device)];
244 old_gid_cache = device->cache.gid_cache [port - start_port(device)];
246 device->cache.pkey_cache[port - start_port(device)] = pkey_cache;
247 device->cache.gid_cache [port - start_port(device)] = gid_cache;
249 write_unlock_irq(&device->cache.lock);
251 kfree(old_pkey_cache);
252 kfree(old_gid_cache);
262 static void ib_cache_task(void *work_ptr)
264 struct ib_update_work *work = work_ptr;
266 ib_cache_update(work->device, work->port_num);
270 static void ib_cache_event(struct ib_event_handler *handler,
271 struct ib_event *event)
273 struct ib_update_work *work;
275 if (event->event == IB_EVENT_PORT_ERR ||
276 event->event == IB_EVENT_PORT_ACTIVE ||
277 event->event == IB_EVENT_LID_CHANGE ||
278 event->event == IB_EVENT_PKEY_CHANGE ||
279 event->event == IB_EVENT_SM_CHANGE) {
280 work = kmalloc(sizeof *work, GFP_ATOMIC);
282 INIT_WORK(&work->work, ib_cache_task, work);
283 work->device = event->device;
284 work->port_num = event->element.port_num;
285 schedule_work(&work->work);
290 static void ib_cache_setup_one(struct ib_device *device)
294 rwlock_init(&device->cache.lock);
296 device->cache.pkey_cache =
297 kmalloc(sizeof *device->cache.pkey_cache *
298 (end_port(device) - start_port(device) + 1), GFP_KERNEL);
299 device->cache.gid_cache =
300 kmalloc(sizeof *device->cache.pkey_cache *
301 (end_port(device) - start_port(device) + 1), GFP_KERNEL);
303 if (!device->cache.pkey_cache || !device->cache.gid_cache) {
304 printk(KERN_WARNING "Couldn't allocate cache "
305 "for %s\n", device->name);
309 for (p = 0; p <= end_port(device) - start_port(device); ++p) {
310 device->cache.pkey_cache[p] = NULL;
311 device->cache.gid_cache [p] = NULL;
312 ib_cache_update(device, p + start_port(device));
315 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
316 device, ib_cache_event);
317 if (ib_register_event_handler(&device->cache.event_handler))
323 for (p = 0; p <= end_port(device) - start_port(device); ++p) {
324 kfree(device->cache.pkey_cache[p]);
325 kfree(device->cache.gid_cache[p]);
329 kfree(device->cache.pkey_cache);
330 kfree(device->cache.gid_cache);
333 static void ib_cache_cleanup_one(struct ib_device *device)
337 ib_unregister_event_handler(&device->cache.event_handler);
338 flush_scheduled_work();
340 for (p = 0; p <= end_port(device) - start_port(device); ++p) {
341 kfree(device->cache.pkey_cache[p]);
342 kfree(device->cache.gid_cache[p]);
345 kfree(device->cache.pkey_cache);
346 kfree(device->cache.gid_cache);
349 static struct ib_client cache_client = {
351 .add = ib_cache_setup_one,
352 .remove = ib_cache_cleanup_one
355 int __init ib_cache_setup(void)
357 return ib_register_client(&cache_client);
360 void __exit ib_cache_cleanup(void)
362 ib_unregister_client(&cache_client);