07e046ec4beb0ec20b3a72cdd2da4671e5806263
[mirror/winof/.git] / hw / mthca / kernel / mt_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  * $Id$
36  */
37
38 #include <mt_l2w.h>
39 #include "mthca_dev.h"
40 #if defined(EVENT_TRACING)
41 #ifdef offsetof
42 #undef offsetof
43 #endif
44 #include "mt_cache.tmh"
45 #endif
46 #include <ib_cache.h>
47
48 #include "ib_cache.h"
49
50
51 #pragma warning( disable : 4200)
52 struct ib_pkey_cache {
53         int             table_len;
54         __be16          table[0];
55 };
56
57 struct ib_gid_cache {
58         int             table_len;
59         union ib_gid    table[0];
60 };
61 #pragma warning( default  : 4200)
62
63 struct ib_update_work {
64         PIO_WORKITEM work_item;
65         struct ib_device  *device;
66         u8                 port_num;
67 };
68
69 int ib_get_cached_gid(struct ib_device *device,
70                       u8                port_num,
71                       int               index,
72                       union ib_gid     *gid)
73 {
74         struct ib_gid_cache *cache;
75         int ret = 0;
76         SPIN_LOCK_PREP(lh);
77
78         // sanity checks
79         if (port_num < start_port(device) || port_num > end_port(device))
80                 return -EINVAL;
81         if (!device->cache.gid_cache)
82                 return -EFAULT;
83
84         read_lock_irqsave(&device->cache.lock, &lh);
85
86         cache = device->cache.gid_cache[port_num - start_port(device)];
87
88         if (index < 0 || index >= cache->table_len)
89                 ret = -EINVAL;
90         else
91                 *gid = cache->table[index];
92
93         read_unlock_irqrestore(&lh);
94
95         return ret;
96 }
97
98 int ib_find_cached_gid(struct ib_device *device,
99                        union ib_gid     *gid,
100                        u8               *port_num,
101                        u16              *index)
102 {
103         struct ib_gid_cache *cache;
104         int i;
105         u8 p;
106         int ret = -ENOENT;
107         SPIN_LOCK_PREP(lh);
108
109         *port_num = (u8)-1;
110         if (index)
111                 *index = (u16)-1;
112
113         read_lock_irqsave(&device->cache.lock, &lh);
114
115         for (p = 0; p <= end_port(device) - start_port(device); ++p) {
116                 cache = device->cache.gid_cache[p];
117                 for (i = 0; i < cache->table_len; ++i) {
118                         if (!memcmp(gid, &cache->table[i], sizeof *gid)) {
119                                 *port_num = p + start_port(device);
120                                 if (index)
121                                         *index = (u16)i;
122                                 ret = 0;
123                                 goto found;
124                         }
125                 }
126         }
127 found:
128         read_unlock_irqrestore(&lh);
129
130         return ret;
131 }
132
133 int ib_get_cached_pkey(struct ib_device *device,
134                        u8                port_num,
135                        int               index,
136                        __be16           *pkey)
137 {
138         struct ib_pkey_cache *cache;
139         int ret = 0;
140         SPIN_LOCK_PREP(lh);
141
142         // sanity checks
143         if (port_num < start_port(device) || port_num > end_port(device))
144                 return -EINVAL;
145         if (!device->cache.gid_cache)
146                 return -EFAULT;
147
148         read_lock_irqsave(&device->cache.lock, &lh);
149
150         cache = device->cache.pkey_cache[port_num - start_port(device)];
151
152         if (index < 0 || index >= cache->table_len)
153                 ret = -EINVAL;
154         else
155                 *pkey = cache->table[index];
156
157         read_unlock_irqrestore(&lh);
158
159         return ret;
160 }
161
162 int ib_find_cached_pkey(struct ib_device *device,
163                         u8                port_num,
164                         __be16            pkey,
165                         u16              *index)
166 {
167         struct ib_pkey_cache *cache;
168         int i;
169         int ret = -ENOENT;
170         SPIN_LOCK_PREP(lh);
171
172         if (port_num < start_port(device) || port_num > end_port(device))
173                 return -EINVAL;
174
175         read_lock_irqsave(&device->cache.lock, &lh);
176
177         cache = device->cache.pkey_cache[port_num - start_port(device)];
178
179         *index = (u16)-1;
180
181         for (i = 0; i < cache->table_len; ++i)
182                 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
183                         *index = (u16)i;
184                         ret = 0;
185                         break;
186                 }
187
188         read_unlock_irqrestore(&lh);
189
190         return ret;
191 }
192
193 static void ib_cache_update(struct ib_device *device,
194                             u8                port)
195 {
196         struct ib_port_attr       *tprops = NULL;
197         struct ib_pkey_cache      *pkey_cache = NULL, *old_pkey_cache;
198         struct ib_gid_cache       *gid_cache = NULL, *old_gid_cache;
199         int                        i;
200         int                        ret;
201         SPIN_LOCK_PREP(lh);
202
203         tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
204         if (!tprops)
205                 return;
206
207         ret = ib_query_port(device, port, tprops);
208         if (ret) {
209                 HCA_PRINT(TRACE_LEVEL_WARNING,HCA_DBG_LOW,("ib_query_port failed (%d) for %s, port %d\n",
210                        ret, device->name, port));
211                 goto err;
212         }
213
214         pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
215                              sizeof *pkey_cache->table, GFP_KERNEL);
216         if (!pkey_cache)
217                 goto err;
218
219         pkey_cache->table_len = tprops->pkey_tbl_len;
220
221         gid_cache = kmalloc(sizeof *gid_cache + tprops->gid_tbl_len *
222                             sizeof *gid_cache->table, GFP_KERNEL);
223         if (!gid_cache)
224                 goto err;
225
226         gid_cache->table_len = tprops->gid_tbl_len;
227
228         for (i = 0; i < pkey_cache->table_len; i+=32) {
229                 __be16 pkey_chunk[32];
230                 int size;
231                 ret = ib_query_pkey_chunk(device, port, (u16)i, pkey_chunk);
232                 if (ret) {
233                         HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("ib_query_pkey_chunk failed (%d) for %s (index %d)\n",
234                                ret, device->name, i));
235                         goto err;
236                 }
237                 size = min(32, pkey_cache->table_len - i);
238                 RtlCopyMemory(pkey_cache->table + i, pkey_chunk, size*sizeof(u16));
239         }
240
241         for (i = 0; i < gid_cache->table_len; i+=8) {
242                 union ib_gid gid_chunk[8];
243                 int size;
244                 ret = ib_query_gid_chunk(device, port, i, gid_chunk);
245                 if (ret) {
246                         HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("ib_query_gid_chunk failed (%d) for %s (index %d)\n",
247                                ret, device->name, i));
248                         goto err;
249                 }
250                 size = min(8, gid_cache->table_len - i);
251                 RtlCopyMemory(gid_cache->table + i, gid_chunk, size*sizeof(union ib_gid));
252         }
253
254         write_lock_irq(&device->cache.lock, &lh);
255
256         old_pkey_cache = device->cache.pkey_cache[port - start_port(device)];
257         old_gid_cache  = device->cache.gid_cache [port - start_port(device)];
258
259         device->cache.pkey_cache[port - start_port(device)] = pkey_cache;
260         device->cache.gid_cache [port - start_port(device)] = gid_cache;
261
262         write_unlock_irq(&lh);
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(void *work_ptr)
276 {
277         struct ib_update_work *work = work_ptr;
278
279         ib_cache_update(work->device, work->port_num);
280 }
281
282 /* leo: wrapper for Linux work_item callback */
283 VOID
284   ib_work_item (
285     IN PDEVICE_OBJECT  DeviceObject,
286     IN PVOID  Context 
287     )
288 {
289         struct ib_update_work *work = (struct ib_update_work *)Context;
290         UNREFERENCED_PARAMETER(DeviceObject);
291         ib_cache_task(Context);
292         IoFreeWorkItem(work->work_item);
293         kfree(Context);
294 }
295
296 static void ib_cache_event(struct ib_event_handler *handler,
297                            struct ib_event *event)
298 {
299         struct ib_update_work *work;
300
301         if (event->event == IB_EVENT_PORT_ERR    ||
302             event->event == IB_EVENT_PORT_ACTIVE ||
303             event->event == IB_EVENT_LID_CHANGE  ||
304             event->event == IB_EVENT_PKEY_CHANGE ||
305             event->event == IB_EVENT_SM_CHANGE) {
306                 work = kmalloc(sizeof *work, GFP_ATOMIC);
307                 //TODO: what will happen on allocation failure ?
308                 if (work) {
309                         work->device   = event->device;
310                         work->port_num = event->element.port_num;
311
312                         { // schedule a work item to work
313                                 // get PDO
314                                 PDEVICE_OBJECT pdo = handler->device->mdev->ext->cl_ext.p_self_do;
315
316                                 // allocate work item
317                                 work->work_item = IoAllocateWorkItem(pdo);
318                                 if (work->work_item == NULL) {
319                                         HCA_PRINT(TRACE_LEVEL_ERROR ,HCA_DBG_LOW,("Failed to allocate workitem for cache update.\n"));
320                                 }
321                                 else { // schedule the work
322                                         IoQueueWorkItem(
323                                                         work->work_item,
324                                                         ib_work_item,
325                                                         DelayedWorkQueue,
326                                                         work
327                                                         );
328                                 }
329                         }
330                         
331                 }
332                 else
333                 {
334                         HCA_PRINT(TRACE_LEVEL_ERROR ,HCA_DBG_MEMORY,("Failed to memory for workitem.\n"));
335                 }
336         }
337 }
338
339 static void ib_cache_setup_one(struct ib_device *device)
340 {
341         u8 p;
342
343         rwlock_init(&device->cache.lock);
344         INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
345                               device, ib_cache_event);
346         ib_register_event_handler(&device->cache.event_handler);
347
348         device->cache.pkey_cache =
349                 kmalloc(sizeof *device->cache.pkey_cache *
350                         (end_port(device) - start_port(device) + 1), GFP_KERNEL);
351         device->cache.gid_cache =
352                 kmalloc(sizeof *device->cache.gid_cache *
353                         (end_port(device) - start_port(device) + 1), GFP_KERNEL);
354
355         if (!device->cache.pkey_cache || !device->cache.gid_cache) {
356                 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("Couldn't allocate cache "
357                        "for %s\n", device->name));
358                 goto err;
359         }
360
361         for (p = 0; p <= end_port(device) - start_port(device); ++p) {
362                 device->cache.pkey_cache[p] = NULL;
363                 device->cache.gid_cache [p] = NULL;
364                 ib_cache_update(device, p + start_port(device));
365         }
366
367         return;
368
369 err:
370         kfree(device->cache.pkey_cache);
371         kfree(device->cache.gid_cache);
372 }
373
374 static void ib_cache_cleanup_one(struct ib_device *device)
375 {
376         int p;
377
378         ib_unregister_event_handler(&device->cache.event_handler);
379         //TODO: how to do that ?
380         // LINUX: flush_scheduled_work();
381
382         for (p = 0; p <= end_port(device) - start_port(device); ++p) {
383                 kfree(device->cache.pkey_cache[p]);
384                 kfree(device->cache.gid_cache[p]);
385         }
386
387         kfree(device->cache.pkey_cache);
388         kfree(device->cache.gid_cache);
389 }
390
391 static struct ib_client cache_client = { "cache", ib_cache_setup_one, ib_cache_cleanup_one };
392
393 int ib_cache_setup(void)
394 {
395         return ib_register_client(&cache_client);
396 }
397
398 void ib_cache_cleanup(void)
399 {
400         ib_unregister_client(&cache_client);
401 }
402