[PATCH] skge: stop bogus sensor messages
[linux/fpc-iii.git] / drivers / infiniband / core / cache.c
blob3042360c97e1910b052ff48c8e57378c7390749e
1 /*
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
12 * conditions are met:
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
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
30 * SOFTWARE.
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 <ib_cache.h>
42 #include "core_priv.h"
44 struct ib_pkey_cache {
45 int table_len;
46 u16 table[0];
49 struct ib_gid_cache {
50 int table_len;
51 union ib_gid table[0];
54 struct ib_update_work {
55 struct work_struct work;
56 struct ib_device *device;
57 u8 port_num;
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,
71 u8 port_num,
72 int index,
73 union ib_gid *gid)
75 struct ib_gid_cache *cache;
76 unsigned long flags;
77 int ret = 0;
79 if (port_num < start_port(device) || port_num > end_port(device))
80 return -EINVAL;
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)
87 ret = -EINVAL;
88 else
89 *gid = cache->table[index];
91 read_unlock_irqrestore(&device->cache.lock, flags);
93 return ret;
95 EXPORT_SYMBOL(ib_get_cached_gid);
97 int ib_find_cached_gid(struct ib_device *device,
98 union ib_gid *gid,
99 u8 *port_num,
100 u16 *index)
102 struct ib_gid_cache *cache;
103 unsigned long flags;
104 int p, i;
105 int ret = -ENOENT;
107 *port_num = -1;
108 if (index)
109 *index = -1;
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);
118 if (index)
119 *index = i;
120 ret = 0;
121 goto found;
125 found:
126 read_unlock_irqrestore(&device->cache.lock, flags);
128 return ret;
130 EXPORT_SYMBOL(ib_find_cached_gid);
132 int ib_get_cached_pkey(struct ib_device *device,
133 u8 port_num,
134 int index,
135 u16 *pkey)
137 struct ib_pkey_cache *cache;
138 unsigned long flags;
139 int ret = 0;
141 if (port_num < start_port(device) || port_num > end_port(device))
142 return -EINVAL;
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)
149 ret = -EINVAL;
150 else
151 *pkey = cache->table[index];
153 read_unlock_irqrestore(&device->cache.lock, flags);
155 return ret;
157 EXPORT_SYMBOL(ib_get_cached_pkey);
159 int ib_find_cached_pkey(struct ib_device *device,
160 u8 port_num,
161 u16 pkey,
162 u16 *index)
164 struct ib_pkey_cache *cache;
165 unsigned long flags;
166 int i;
167 int ret = -ENOENT;
169 if (port_num < start_port(device) || port_num > end_port(device))
170 return -EINVAL;
172 read_lock_irqsave(&device->cache.lock, flags);
174 cache = device->cache.pkey_cache[port_num - start_port(device)];
176 *index = -1;
178 for (i = 0; i < cache->table_len; ++i)
179 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
180 *index = i;
181 ret = 0;
182 break;
185 read_unlock_irqrestore(&device->cache.lock, flags);
187 return ret;
189 EXPORT_SYMBOL(ib_find_cached_pkey);
191 static void ib_cache_update(struct ib_device *device,
192 u8 port)
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;
197 int i;
198 int ret;
200 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
201 if (!tprops)
202 return;
204 ret = ib_query_port(device, port, tprops);
205 if (ret) {
206 printk(KERN_WARNING "ib_query_port failed (%d) for %s\n",
207 ret, device->name);
208 goto err;
211 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
212 sizeof *pkey_cache->table, GFP_KERNEL);
213 if (!pkey_cache)
214 goto err;
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);
220 if (!gid_cache)
221 goto err;
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);
227 if (ret) {
228 printk(KERN_WARNING "ib_query_pkey failed (%d) for %s (index %d)\n",
229 ret, device->name, i);
230 goto err;
234 for (i = 0; i < gid_cache->table_len; ++i) {
235 ret = ib_query_gid(device, port, i, gid_cache->table + i);
236 if (ret) {
237 printk(KERN_WARNING "ib_query_gid failed (%d) for %s (index %d)\n",
238 ret, device->name, i);
239 goto err;
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);
255 kfree(tprops);
256 return;
258 err:
259 kfree(pkey_cache);
260 kfree(gid_cache);
261 kfree(tprops);
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);
269 kfree(work);
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);
283 if (work) {
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)
294 int p;
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);
308 goto err;
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))
320 goto err_cache;
322 return;
324 err_cache:
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]);
330 err:
331 kfree(device->cache.pkey_cache);
332 kfree(device->cache.gid_cache);
335 static void ib_cache_cleanup_one(struct ib_device *device)
337 int p;
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 = {
352 .name = "cache",
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);