[HW] allow retrieving CA attributes with pageable memory
[mirror/winof/.git] / hw / mlx4 / kernel / bus / 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  * $Id: cache.c 1349 2004-12-16 21:09:43Z roland $
36  */
37
38 #include "ib\mlx4_ib.h"
39 #include "ib_cache.h"
40 #include <mlx4_debug.h>
41
42 #if defined(EVENT_TRACING)
43 #ifdef offsetof
44 #undef offsetof
45 #endif
46 #include "cache.tmh"
47 #endif
48
49 #pragma warning( disable : 4200)
50 struct ib_pkey_cache {
51         int             table_len;
52         __be16          table[0];
53 };
54
55 struct ib_gid_cache {
56         int             table_len;
57         union ib_gid    table[0];
58 };
59 #pragma warning( default  : 4200)
60
61 struct ib_update_work {
62         PIO_WORKITEM work_item;
63         struct ib_device  *device;
64         u8                 port_num;
65 };
66
67 static inline int start_port(struct ib_device *device)
68 {
69         return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
70 }
71
72 static inline int end_port(struct ib_device *device)
73 {
74         return (device->node_type == RDMA_NODE_IB_SWITCH) ?
75                 0 : device->phys_port_cnt;
76 }
77
78 int ib_get_cached_gid(struct ib_device *device,
79                       u8                port_num,
80                       int               index,
81                       union ib_gid     *gid)
82 {
83         union ib_gid cgid;
84         struct ib_gid_cache *cache;
85         unsigned long flags;
86
87         if (mlx4_is_barred(device->dma_device))
88                 return -EFAULT;
89
90         if (port_num < start_port(device) || port_num > end_port(device))
91                 return -EINVAL;
92
93         read_lock_irqsave(&device->cache.lock, &flags);
94
95         cache = device->cache.gid_cache[port_num - start_port(device)];
96
97         if (index < 0 || index >= cache->table_len) {
98                 read_unlock_irqrestore(&device->cache.lock, flags);
99                 return -EINVAL;
100         }
101         
102         cgid = cache->table[index];
103         read_unlock_irqrestore(&device->cache.lock, flags);
104         *gid = cgid;
105
106         return 0;
107 }
108 EXPORT_SYMBOL(ib_get_cached_gid);
109
110 int ib_find_cached_gid(struct ib_device *device,
111                        union ib_gid     *gid,
112                        u8               *port_num,
113                        u16              *index)
114 {
115         struct ib_gid_cache *cache;
116         unsigned long flags;
117         int p, i;
118
119         if (mlx4_is_barred(device->dma_device))
120                 return -EFAULT;
121
122         read_lock_irqsave(&device->cache.lock, &flags);
123
124         for (p = 0; p <= end_port(device) - start_port(device); ++p) {
125                 cache = device->cache.gid_cache[p];
126                 for (i = 0; i < cache->table_len; ++i) {
127                         if (!memcmp(gid, &cache->table[i], sizeof *gid)) {
128                                 goto found;
129                         }
130                 }
131         }
132
133         read_unlock_irqrestore(&device->cache.lock, flags);
134         *port_num = (u8)-1;
135         if (index)
136                 *index = (u16)-1;
137         return -ENOENT;
138
139 found:
140         read_unlock_irqrestore(&device->cache.lock, flags);
141         *port_num = (u8)(p + start_port(device));
142         if (index)
143                 *index = (u16)i;
144
145         return 0;
146 }
147 EXPORT_SYMBOL(ib_find_cached_gid);
148
149 int ib_get_cached_pkey(struct ib_device *device,
150                        u8                port_num,
151                        int               index,
152                        __be16           *pkey)
153 {
154         struct ib_pkey_cache *cache;
155         __be16 cpkey;
156         unsigned long flags;
157
158         if (mlx4_is_barred(device->dma_device))
159                 return -EFAULT;
160
161         if (port_num < start_port(device) || port_num > end_port(device))
162                 return -EINVAL;
163
164         read_lock_irqsave(&device->cache.lock, &flags);
165
166         cache = device->cache.pkey_cache[port_num - start_port(device)];
167
168         if (index < 0 || index >= cache->table_len) {
169                 read_unlock_irqrestore(&device->cache.lock, flags);
170                 return -EINVAL;
171         }
172
173         cpkey = cache->table[index];
174         read_unlock_irqrestore(&device->cache.lock, flags);
175         *pkey = cpkey;
176
177         return 0;
178 }
179 EXPORT_SYMBOL(ib_get_cached_pkey);
180
181 int ib_find_cached_pkey(struct ib_device *device,
182                         u8                port_num,
183                         __be16            pkey,
184                         u16              *index)
185 {
186         struct ib_pkey_cache *cache;
187         unsigned long flags;
188         int i;
189
190         if (mlx4_is_barred(device->dma_device))
191                 return -EFAULT;
192
193         if (port_num < start_port(device) || port_num > end_port(device))
194                 return -EINVAL;
195
196         read_lock_irqsave(&device->cache.lock, &flags);
197
198         cache = device->cache.pkey_cache[port_num - start_port(device)];
199
200         *index = (u16)-1;
201
202         for (i = 0; i < cache->table_len; ++i)
203                 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
204                         goto found;
205                 }
206
207         read_unlock_irqrestore(&device->cache.lock, flags);
208         *index = (u16)-1;
209         return -ENOENT;
210
211 found:
212         read_unlock_irqrestore(&device->cache.lock, flags);
213         *index = (u16)i;
214         return 0;
215 }
216 EXPORT_SYMBOL(ib_find_cached_pkey);
217
218 int ib_get_cached_lmc(struct ib_device *device,
219                       u8                port_num,
220                       u8                *lmc)
221 {
222         unsigned long flags;
223         u8 clmc;
224         int ret = 0;
225
226         if (port_num < start_port(device) || port_num > end_port(device))
227                 return -EINVAL;
228
229         read_lock_irqsave(&device->cache.lock, &flags);
230         clmc = device->cache.lmc_cache[port_num - start_port(device)];
231         read_unlock_irqrestore(&device->cache.lock, flags);
232         *lmc = clmc;
233
234         return ret;
235 }
236 EXPORT_SYMBOL(ib_get_cached_lmc);
237
238 static void ib_cache_update(struct ib_device *device,
239                             u8                port)
240 {
241         struct ib_port_attr       *tprops = NULL;
242         struct ib_pkey_cache      *pkey_cache = NULL, *old_pkey_cache;
243         struct ib_gid_cache       *gid_cache = NULL, *old_gid_cache;
244         int                        i;
245         int                        ret;
246
247         tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
248         if (!tprops)
249                 return;
250
251         ret = ib_query_port(device, port, tprops);
252         if (ret) {
253                 printk(KERN_WARNING "ib_query_port failed (%d) for %s. port_num %d (%d,%d)\n",
254                         ret, device->name, (int)port, start_port(device), end_port(device) );
255                 goto err;
256         }
257
258         pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
259                              sizeof *pkey_cache->table, GFP_KERNEL);
260         if (!pkey_cache)
261                 goto err;
262
263         pkey_cache->table_len = tprops->pkey_tbl_len;
264
265         gid_cache = kmalloc(sizeof *gid_cache + tprops->gid_tbl_len *
266                             sizeof *gid_cache->table, GFP_KERNEL);
267         if (!gid_cache)
268                 goto err;
269
270         gid_cache->table_len = tprops->gid_tbl_len;
271
272         for (i = 0; i < pkey_cache->table_len; i+=32) {
273                 int size = min(32, pkey_cache->table_len - i);
274                 ret = ib_query_pkey_chunk(device, port, (u16)i, pkey_cache->table + i, size );
275                 if (ret) {
276                         printk(KERN_WARNING "ib_query_pkey_chunk failed (%d) for %s (index %d)\n",
277                                ret, device->name, i);
278                         goto err;
279                 }
280         }
281
282         for (i = 0; i < gid_cache->table_len; i+=8) {
283                 int size = min(8, gid_cache->table_len - i);
284                 ret = ib_query_gid_chunk(device, port, i, gid_cache->table + i, size);
285                 if (ret) {
286                         printk(KERN_WARNING "ib_query_gid_chunk failed (%d) for %s (index %d)\n",
287                                ret, device->name, i);
288                         goto err;
289                 }
290         }
291
292         write_lock_irq(&device->cache.lock);
293
294         old_pkey_cache = device->cache.pkey_cache[port - start_port(device)];
295         old_gid_cache  = device->cache.gid_cache [port - start_port(device)];
296
297         device->cache.pkey_cache[port - start_port(device)] = pkey_cache;
298         device->cache.gid_cache [port - start_port(device)] = gid_cache;
299
300         device->cache.lmc_cache[port - start_port(device)] = tprops->lmc;
301
302         write_unlock_irq(&device->cache.lock);
303
304         kfree(old_pkey_cache);
305         kfree(old_gid_cache);
306         kfree(tprops);
307         return;
308
309 err:
310         kfree(pkey_cache);
311         kfree(gid_cache);
312         kfree(tprops);
313 }
314
315 static void ib_cache_task(void *work_ptr)
316 {
317         struct ib_update_work *work = work_ptr;
318
319         ib_cache_update(work->device, work->port_num);
320 }
321
322 static void ib_work_item (
323         IN PDEVICE_OBJECT  DeviceObject,
324         IN PVOID  Context 
325         )
326 {
327         struct ib_update_work *work = (struct ib_update_work *)Context;
328         UNREFERENCED_PARAMETER(DeviceObject);
329         ib_cache_task(Context);
330         shutter_loose( &work->device->cache.x.work_thread );
331         IoFreeWorkItem(work->work_item);
332         kfree(Context);
333 }
334
335 static void ib_cache_event(struct ib_event_handler *handler,
336                            struct ib_event *event)
337 {
338         struct ib_update_work *work;
339
340         if (event->event == IB_EVENT_PORT_ERR    ||
341                 event->event == IB_EVENT_PORT_ACTIVE ||
342                 event->event == IB_EVENT_LID_CHANGE  ||
343                 event->event == IB_EVENT_PKEY_CHANGE ||
344                 event->event == IB_EVENT_SM_CHANGE   ||
345                 event->event == IB_EVENT_CLIENT_REREGISTER) {
346
347                 work = kmalloc(sizeof *work, GFP_ATOMIC);
348                 if (work) {
349                         PDEVICE_OBJECT pdo = to_mdev(handler->device)->dev->pdev->p_self_do;
350
351                         // allocate work item
352                         work->work_item = IoAllocateWorkItem(pdo);
353                         if (work->work_item == NULL) {
354                                 //TODO: at least - print error. Need to return code, but the function is void 
355                                 kfree( work );
356                                 return;
357                         }
358
359                         // check whether we are in shutdown
360                         if (shutter_use( &event->device->cache.x.work_thread ) <= 0) {
361                                 kfree( work );
362                                 return;
363                         }
364
365                         // schedule the work
366                         work->device   = event->device;
367                         work->port_num = event->element.port_num;
368                         IoQueueWorkItem( work->work_item, ib_work_item, DelayedWorkQueue, work );
369                 }
370         }
371 }
372
373 static void ib_cache_setup_one(struct ib_device *device)
374 {
375         int p;
376         int port_num;
377         
378         shutter_init( &device->cache.x.work_thread );
379         rwlock_init(&device->cache.lock);
380         INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
381                               device, ib_cache_event, NULL, NULL, 0);
382         ib_register_event_handler(&device->cache.event_handler);
383
384         port_num = end_port(device) - start_port(device) + 1;
385         if (port_num > 0 ) { 
386                 // if port_num ==0   ==> there are no IB ports
387                 device->cache.pkey_cache =
388                         kmalloc(sizeof *device->cache.pkey_cache * port_num, GFP_KERNEL);
389                 device->cache.gid_cache =
390                         kmalloc(sizeof *device->cache.gid_cache * port_num, GFP_KERNEL);
391                 device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
392                         port_num, GFP_KERNEL);
393
394                 if (!device->cache.pkey_cache || !device->cache.gid_cache ||
395                         !device->cache.lmc_cache) {
396                         printk(KERN_WARNING "Couldn't allocate cache "
397                                 "for %s\n", device->name);
398                         goto err;
399                 }
400         }
401
402         for (p = 0; p < port_num; ++p) {
403                 device->cache.pkey_cache[p] = NULL;
404                 device->cache.gid_cache [p] = NULL;
405                 ib_cache_update(device, (u8)(p + start_port(device)));
406         }
407
408         return;
409
410 err:
411         kfree(device->cache.pkey_cache);
412         kfree(device->cache.gid_cache);
413         kfree(device->cache.lmc_cache);
414 }
415
416 // calling sequence:
417 // EvtReleaseHardware ==> mlx4_ib_cleanup ==> mlx4_unregister_interface ==>
418 // ==> mlx4_remove_device ==> mlx4_ib_remove ==> ib_unregister_device ==>
419 // ==> ib_cache_cleanup
420 static void ib_cache_cleanup_one(struct ib_device *device)
421 {
422         int p;
423
424         ASSERT(device->cache.event_handler.device);
425         ib_unregister_event_handler(&device->cache.event_handler);
426         // instead of Linux flush_scheduled_work(): wait for them to quit
427         shutter_shut( &device->cache.x.work_thread );
428
429         for (p = 0; p <= end_port(device) - start_port(device); ++p) {
430                 kfree(device->cache.pkey_cache[p]);
431                 kfree(device->cache.gid_cache[p]);
432         }
433
434         kfree(device->cache.pkey_cache);
435         kfree(device->cache.gid_cache);
436         kfree(device->cache.lmc_cache);
437 }
438
439 static struct ib_client cache_client = { "cache", ib_cache_setup_one, ib_cache_cleanup_one };
440
441 int __init ib_cache_setup(void)
442 {
443         return ib_register_client(&cache_client);
444 }
445
446 void __exit ib_cache_cleanup(void)
447 {
448         ib_unregister_client(&cache_client);
449 }