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