pandora: defconfig: update
[pandora-kernel.git] / drivers / infiniband / core / cache.c
1 /*
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.
6  *
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:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
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.
25  *
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
33  * SOFTWARE.
34  */
35
36 #include <linux/module.h>
37 #include <linux/errno.h>
38 #include <linux/slab.h>
39 #include <linux/workqueue.h>
40
41 #include <rdma/ib_cache.h>
42
43 #include "core_priv.h"
44
45 struct ib_pkey_cache {
46         int             table_len;
47         u16             table[0];
48 };
49
50 struct ib_gid_cache {
51         int             table_len;
52         union ib_gid    table[0];
53 };
54
55 struct ib_update_work {
56         struct work_struct work;
57         struct ib_device  *device;
58         u8                 port_num;
59 };
60
61 int ib_get_cached_gid(struct ib_device *device,
62                       u8                port_num,
63                       int               index,
64                       union ib_gid     *gid)
65 {
66         struct ib_gid_cache *cache;
67         unsigned long flags;
68         int ret = 0;
69
70         if (!rdma_is_port_valid(device, port_num))
71                 return -EINVAL;
72
73         read_lock_irqsave(&device->cache.lock, flags);
74
75         cache = device->cache.gid_cache[port_num - rdma_start_port(device)];
76
77         if (index < 0 || index >= cache->table_len)
78                 ret = -EINVAL;
79         else
80                 *gid = cache->table[index];
81
82         read_unlock_irqrestore(&device->cache.lock, flags);
83
84         return ret;
85 }
86 EXPORT_SYMBOL(ib_get_cached_gid);
87
88 int ib_find_cached_gid(struct ib_device *device,
89                        union ib_gid     *gid,
90                        u8               *port_num,
91                        u16              *index)
92 {
93         struct ib_gid_cache *cache;
94         unsigned long flags;
95         int p, i;
96         int ret = -ENOENT;
97
98         *port_num = -1;
99         if (index)
100                 *index = -1;
101
102         read_lock_irqsave(&device->cache.lock, flags);
103
104         for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p) {
105                 cache = device->cache.gid_cache[p];
106                 for (i = 0; i < cache->table_len; ++i) {
107                         if (!memcmp(gid, &cache->table[i], sizeof *gid)) {
108                                 *port_num = p + rdma_start_port(device);
109                                 if (index)
110                                         *index = i;
111                                 ret = 0;
112                                 goto found;
113                         }
114                 }
115         }
116 found:
117         read_unlock_irqrestore(&device->cache.lock, flags);
118
119         return ret;
120 }
121 EXPORT_SYMBOL(ib_find_cached_gid);
122
123 int ib_get_cached_pkey(struct ib_device *device,
124                        u8                port_num,
125                        int               index,
126                        u16              *pkey)
127 {
128         struct ib_pkey_cache *cache;
129         unsigned long flags;
130         int ret = 0;
131
132         if (!rdma_is_port_valid(device, port_num))
133                 return -EINVAL;
134
135         read_lock_irqsave(&device->cache.lock, flags);
136
137         cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
138
139         if (index < 0 || index >= cache->table_len)
140                 ret = -EINVAL;
141         else
142                 *pkey = cache->table[index];
143
144         read_unlock_irqrestore(&device->cache.lock, flags);
145
146         return ret;
147 }
148 EXPORT_SYMBOL(ib_get_cached_pkey);
149
150 int ib_find_cached_pkey(struct ib_device *device,
151                         u8                port_num,
152                         u16               pkey,
153                         u16              *index)
154 {
155         struct ib_pkey_cache *cache;
156         unsigned long flags;
157         int i;
158         int ret = -ENOENT;
159
160         if (!rdma_is_port_valid(device, port_num))
161                 return -EINVAL;
162
163         read_lock_irqsave(&device->cache.lock, flags);
164
165         cache = device->cache.pkey_cache[port_num - rdma_start_port(device)];
166
167         *index = -1;
168
169         for (i = 0; i < cache->table_len; ++i)
170                 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
171                         *index = i;
172                         ret = 0;
173                         break;
174                 }
175
176         read_unlock_irqrestore(&device->cache.lock, flags);
177
178         return ret;
179 }
180 EXPORT_SYMBOL(ib_find_cached_pkey);
181
182 int ib_get_cached_lmc(struct ib_device *device,
183                       u8                port_num,
184                       u8                *lmc)
185 {
186         unsigned long flags;
187         int ret = 0;
188
189         if (!rdma_is_port_valid(device, port_num))
190                 return -EINVAL;
191
192         read_lock_irqsave(&device->cache.lock, flags);
193         *lmc = device->cache.lmc_cache[port_num - rdma_start_port(device)];
194         read_unlock_irqrestore(&device->cache.lock, flags);
195
196         return ret;
197 }
198 EXPORT_SYMBOL(ib_get_cached_lmc);
199
200 static void ib_cache_update(struct ib_device *device,
201                             u8                port)
202 {
203         struct ib_port_attr       *tprops = NULL;
204         struct ib_pkey_cache      *pkey_cache = NULL, *old_pkey_cache;
205         struct ib_gid_cache       *gid_cache = NULL, *old_gid_cache;
206         int                        i;
207         int                        ret;
208
209         tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
210         if (!tprops)
211                 return;
212
213         ret = ib_query_port(device, port, tprops);
214         if (ret) {
215                 printk(KERN_WARNING "ib_query_port failed (%d) for %s\n",
216                        ret, device->name);
217                 goto err;
218         }
219
220         pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
221                              sizeof *pkey_cache->table, GFP_KERNEL);
222         if (!pkey_cache)
223                 goto err;
224
225         pkey_cache->table_len = tprops->pkey_tbl_len;
226
227         gid_cache = kmalloc(sizeof *gid_cache + tprops->gid_tbl_len *
228                             sizeof *gid_cache->table, GFP_KERNEL);
229         if (!gid_cache)
230                 goto err;
231
232         gid_cache->table_len = tprops->gid_tbl_len;
233
234         for (i = 0; i < pkey_cache->table_len; ++i) {
235                 ret = ib_query_pkey(device, port, i, pkey_cache->table + i);
236                 if (ret) {
237                         printk(KERN_WARNING "ib_query_pkey failed (%d) for %s (index %d)\n",
238                                ret, device->name, i);
239                         goto err;
240                 }
241         }
242
243         for (i = 0; i < gid_cache->table_len; ++i) {
244                 ret = ib_query_gid(device, port, i, gid_cache->table + i);
245                 if (ret) {
246                         printk(KERN_WARNING "ib_query_gid failed (%d) for %s (index %d)\n",
247                                ret, device->name, i);
248                         goto err;
249                 }
250         }
251
252         write_lock_irq(&device->cache.lock);
253
254         old_pkey_cache = device->cache.pkey_cache[port - rdma_start_port(device)];
255         old_gid_cache  = device->cache.gid_cache [port - rdma_start_port(device)];
256
257         device->cache.pkey_cache[port - rdma_start_port(device)] = pkey_cache;
258         device->cache.gid_cache [port - rdma_start_port(device)] = gid_cache;
259
260         device->cache.lmc_cache[port - rdma_start_port(device)] = tprops->lmc;
261
262         write_unlock_irq(&device->cache.lock);
263
264         kfree(old_pkey_cache);
265         kfree(old_gid_cache);
266         kfree(tprops);
267         return;
268
269 err:
270         kfree(pkey_cache);
271         kfree(gid_cache);
272         kfree(tprops);
273 }
274
275 static void ib_cache_task(struct work_struct *_work)
276 {
277         struct ib_update_work *work =
278                 container_of(_work, struct ib_update_work, work);
279
280         ib_cache_update(work->device, work->port_num);
281         kfree(work);
282 }
283
284 static void ib_cache_event(struct ib_event_handler *handler,
285                            struct ib_event *event)
286 {
287         struct ib_update_work *work;
288
289         if (event->event == IB_EVENT_PORT_ERR    ||
290             event->event == IB_EVENT_PORT_ACTIVE ||
291             event->event == IB_EVENT_LID_CHANGE  ||
292             event->event == IB_EVENT_PKEY_CHANGE ||
293             event->event == IB_EVENT_SM_CHANGE   ||
294             event->event == IB_EVENT_CLIENT_REREGISTER ||
295             event->event == IB_EVENT_GID_CHANGE) {
296                 work = kmalloc(sizeof *work, GFP_ATOMIC);
297                 if (work) {
298                         INIT_WORK(&work->work, ib_cache_task);
299                         work->device   = event->device;
300                         work->port_num = event->element.port_num;
301                         queue_work(ib_wq, &work->work);
302                 }
303         }
304 }
305
306 static void ib_cache_setup_one(struct ib_device *device)
307 {
308         int p;
309
310         rwlock_init(&device->cache.lock);
311
312         device->cache.pkey_cache =
313                 kmalloc(sizeof *device->cache.pkey_cache *
314                         (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
315         device->cache.gid_cache =
316                 kmalloc(sizeof *device->cache.gid_cache *
317                         (rdma_end_port(device) - rdma_start_port(device) + 1), GFP_KERNEL);
318
319         device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
320                                           (rdma_end_port(device) -
321                                            rdma_start_port(device) + 1),
322                                           GFP_KERNEL);
323
324         if (!device->cache.pkey_cache || !device->cache.gid_cache ||
325             !device->cache.lmc_cache) {
326                 printk(KERN_WARNING "Couldn't allocate cache "
327                        "for %s\n", device->name);
328                 goto err;
329         }
330
331         for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p) {
332                 device->cache.pkey_cache[p] = NULL;
333                 device->cache.gid_cache [p] = NULL;
334                 ib_cache_update(device, p + rdma_start_port(device));
335         }
336
337         INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
338                               device, ib_cache_event);
339         if (ib_register_event_handler(&device->cache.event_handler))
340                 goto err_cache;
341
342         return;
343
344 err_cache:
345         for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p) {
346                 kfree(device->cache.pkey_cache[p]);
347                 kfree(device->cache.gid_cache[p]);
348         }
349
350 err:
351         kfree(device->cache.pkey_cache);
352         kfree(device->cache.gid_cache);
353         kfree(device->cache.lmc_cache);
354 }
355
356 static void ib_cache_cleanup_one(struct ib_device *device)
357 {
358         int p;
359
360         ib_unregister_event_handler(&device->cache.event_handler);
361         flush_workqueue(ib_wq);
362
363         for (p = 0; p <= rdma_end_port(device) - rdma_start_port(device); ++p) {
364                 kfree(device->cache.pkey_cache[p]);
365                 kfree(device->cache.gid_cache[p]);
366         }
367
368         kfree(device->cache.pkey_cache);
369         kfree(device->cache.gid_cache);
370         kfree(device->cache.lmc_cache);
371 }
372
373 static struct ib_client cache_client = {
374         .name   = "cache",
375         .add    = ib_cache_setup_one,
376         .remove = ib_cache_cleanup_one
377 };
378
379 int __init ib_cache_setup(void)
380 {
381         return ib_register_client(&cache_client);
382 }
383
384 void __exit ib_cache_cleanup(void)
385 {
386         ib_unregister_client(&cache_client);
387 }