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