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