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.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
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.
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
35 * $Id: cache.c 1349 2004-12-16 21:09:43Z roland $
38 #include "ib\mlx4_ib.h"
41 #if defined(EVENT_TRACING)
48 #pragma warning( disable : 4200)
49 struct ib_pkey_cache {
56 union ib_gid table[0];
58 #pragma warning( default : 4200)
60 struct ib_update_work {
61 PIO_WORKITEM work_item;
62 struct ib_device *device;
66 static inline int start_port(struct ib_device *device)
68 return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
71 static inline int end_port(struct ib_device *device)
73 return (device->node_type == RDMA_NODE_IB_SWITCH) ?
74 0 : device->phys_port_cnt;
77 int ib_get_cached_gid(struct ib_device *device,
82 struct ib_gid_cache *cache;
86 if (port_num < start_port(device) || port_num > end_port(device))
89 read_lock_irqsave(&device->cache.lock, &flags);
91 cache = device->cache.gid_cache[port_num - start_port(device)];
93 if (index < 0 || index >= cache->table_len)
96 *gid = cache->table[index];
98 read_unlock_irqrestore(&device->cache.lock, flags);
102 EXPORT_SYMBOL(ib_get_cached_gid);
104 int ib_find_cached_gid(struct ib_device *device,
109 struct ib_gid_cache *cache;
118 read_lock_irqsave(&device->cache.lock, &flags);
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));
133 read_unlock_irqrestore(&device->cache.lock, flags);
137 EXPORT_SYMBOL(ib_find_cached_gid);
139 int ib_get_cached_pkey(struct ib_device *device,
144 struct ib_pkey_cache *cache;
148 if (port_num < start_port(device) || port_num > end_port(device))
151 read_lock_irqsave(&device->cache.lock, &flags);
153 cache = device->cache.pkey_cache[port_num - start_port(device)];
155 if (index < 0 || index >= cache->table_len)
158 *pkey = cache->table[index];
160 read_unlock_irqrestore(&device->cache.lock, flags);
164 EXPORT_SYMBOL(ib_get_cached_pkey);
166 int ib_find_cached_pkey(struct ib_device *device,
171 struct ib_pkey_cache *cache;
176 if (port_num < start_port(device) || port_num > end_port(device))
179 read_lock_irqsave(&device->cache.lock, &flags);
181 cache = device->cache.pkey_cache[port_num - start_port(device)];
185 for (i = 0; i < cache->table_len; ++i)
186 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
192 read_unlock_irqrestore(&device->cache.lock, flags);
196 EXPORT_SYMBOL(ib_find_cached_pkey);
198 int ib_get_cached_lmc(struct ib_device *device,
205 if (port_num < start_port(device) || port_num > end_port(device))
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);
214 EXPORT_SYMBOL(ib_get_cached_lmc);
216 static void ib_cache_update(struct ib_device *device,
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;
225 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
229 ret = ib_query_port(device, port, tprops);
231 printk(KERN_WARNING "ib_query_port failed (%d) for %s\n",
236 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
237 sizeof *pkey_cache->table, GFP_KERNEL);
241 pkey_cache->table_len = tprops->pkey_tbl_len;
243 gid_cache = kmalloc(sizeof *gid_cache + tprops->gid_tbl_len *
244 sizeof *gid_cache->table, GFP_KERNEL);
248 gid_cache->table_len = tprops->gid_tbl_len;
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 );
254 printk(KERN_WARNING "ib_query_pkey_chunk failed (%d) for %s (index %d)\n",
255 ret, device->name, i);
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);
264 printk(KERN_WARNING "ib_query_gid_chunk failed (%d) for %s (index %d)\n",
265 ret, device->name, i);
270 write_lock_irq(&device->cache.lock);
272 old_pkey_cache = device->cache.pkey_cache[port - start_port(device)];
273 old_gid_cache = device->cache.gid_cache [port - start_port(device)];
275 device->cache.pkey_cache[port - start_port(device)] = pkey_cache;
276 device->cache.gid_cache [port - start_port(device)] = gid_cache;
278 device->cache.lmc_cache[port - start_port(device)] = tprops->lmc;
280 write_unlock_irq(&device->cache.lock);
282 kfree(old_pkey_cache);
283 kfree(old_gid_cache);
293 static void ib_cache_task(void *work_ptr)
295 struct ib_update_work *work = work_ptr;
297 ib_cache_update(work->device, work->port_num);
300 static void ib_work_item (
301 IN PDEVICE_OBJECT DeviceObject,
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);
313 static void ib_cache_event(struct ib_event_handler *handler,
314 struct ib_event *event)
316 struct ib_update_work *work;
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) {
325 work = kmalloc(sizeof *work, GFP_ATOMIC);
327 PDEVICE_OBJECT pdo = to_mdev(handler->device)->dev->pdev->p_self_do;
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
337 // check whether we are in shutdown
338 if (shutter_use( &event->device->cache.x.work_thread ) <= 0) {
344 work->device = event->device;
345 work->port_num = event->element.port_num;
346 IoQueueWorkItem( work->work_item, ib_work_item, DelayedWorkQueue, work );
351 static void ib_cache_setup_one(struct ib_device *device)
355 rwlock_init(&device->cache.lock);
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);
364 device->cache.lmc_cache = kmalloc(sizeof *device->cache.lmc_cache *
366 start_port(device) + 1),
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);
376 shutter_init( &device->cache.x.work_thread );
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)));
384 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
385 device, ib_cache_event);
386 if (ib_register_event_handler(&device->cache.event_handler))
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]);
398 kfree(device->cache.pkey_cache);
399 kfree(device->cache.gid_cache);
400 kfree(device->cache.lmc_cache);
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)
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 );
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]);
420 kfree(device->cache.pkey_cache);
421 kfree(device->cache.gid_cache);
422 kfree(device->cache.lmc_cache);
425 static struct ib_client cache_client = { "cache", ib_cache_setup_one, ib_cache_cleanup_one };
427 int __init ib_cache_setup(void)
429 return ib_register_client(&cache_client);
432 void __exit ib_cache_cleanup(void)
434 ib_unregister_client(&cache_client);