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