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"
40 #include <mlx4_debug.h>
42 #if defined(EVENT_TRACING)
49 #pragma warning( disable : 4200)
50 struct ib_pkey_cache {
57 union ib_gid table[0];
59 #pragma warning( default : 4200)
61 struct ib_update_work {
62 PIO_WORKITEM work_item;
63 struct ib_device *device;
67 static inline int start_port(struct ib_device *device)
69 return (device->node_type == RDMA_NODE_IB_SWITCH) ? 0 : 1;
72 static inline int end_port(struct ib_device *device)
74 return (device->node_type == RDMA_NODE_IB_SWITCH) ?
75 0 : device->phys_port_cnt;
78 int ib_get_cached_gid(struct ib_device *device,
83 struct ib_gid_cache *cache;
87 if (mlx4_is_barred(device->dma_device))
90 if (port_num < start_port(device) || port_num > end_port(device))
93 read_lock_irqsave(&device->cache.lock, &flags);
95 cache = device->cache.gid_cache[port_num - start_port(device)];
97 if (index < 0 || index >= cache->table_len)
100 *gid = cache->table[index];
102 read_unlock_irqrestore(&device->cache.lock, flags);
106 EXPORT_SYMBOL(ib_get_cached_gid);
108 int ib_find_cached_gid(struct ib_device *device,
113 struct ib_gid_cache *cache;
118 if (mlx4_is_barred(device->dma_device))
125 read_lock_irqsave(&device->cache.lock, &flags);
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));
140 read_unlock_irqrestore(&device->cache.lock, flags);
144 EXPORT_SYMBOL(ib_find_cached_gid);
146 int ib_get_cached_pkey(struct ib_device *device,
151 struct ib_pkey_cache *cache;
155 if (mlx4_is_barred(device->dma_device))
158 if (port_num < start_port(device) || port_num > end_port(device))
161 read_lock_irqsave(&device->cache.lock, &flags);
163 cache = device->cache.pkey_cache[port_num - start_port(device)];
165 if (index < 0 || index >= cache->table_len)
168 *pkey = cache->table[index];
170 read_unlock_irqrestore(&device->cache.lock, flags);
174 EXPORT_SYMBOL(ib_get_cached_pkey);
176 int ib_find_cached_pkey(struct ib_device *device,
181 struct ib_pkey_cache *cache;
186 if (mlx4_is_barred(device->dma_device))
189 if (port_num < start_port(device) || port_num > end_port(device))
192 read_lock_irqsave(&device->cache.lock, &flags);
194 cache = device->cache.pkey_cache[port_num - start_port(device)];
198 for (i = 0; i < cache->table_len; ++i)
199 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
205 read_unlock_irqrestore(&device->cache.lock, flags);
209 EXPORT_SYMBOL(ib_find_cached_pkey);
211 int ib_get_cached_lmc(struct ib_device *device,
218 if (port_num < start_port(device) || port_num > end_port(device))
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);
227 EXPORT_SYMBOL(ib_get_cached_lmc);
229 static void ib_cache_update(struct ib_device *device,
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;
238 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
242 ret = ib_query_port(device, port, tprops);
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) );
249 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
250 sizeof *pkey_cache->table, GFP_KERNEL);
254 pkey_cache->table_len = tprops->pkey_tbl_len;
256 gid_cache = kmalloc(sizeof *gid_cache + tprops->gid_tbl_len *
257 sizeof *gid_cache->table, GFP_KERNEL);
261 gid_cache->table_len = tprops->gid_tbl_len;
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 );
267 printk(KERN_WARNING "ib_query_pkey_chunk failed (%d) for %s (index %d)\n",
268 ret, device->name, i);
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);
277 printk(KERN_WARNING "ib_query_gid_chunk failed (%d) for %s (index %d)\n",
278 ret, device->name, i);
283 write_lock_irq(&device->cache.lock);
285 old_pkey_cache = device->cache.pkey_cache[port - start_port(device)];
286 old_gid_cache = device->cache.gid_cache [port - start_port(device)];
288 device->cache.pkey_cache[port - start_port(device)] = pkey_cache;
289 device->cache.gid_cache [port - start_port(device)] = gid_cache;
291 device->cache.lmc_cache[port - start_port(device)] = tprops->lmc;
293 write_unlock_irq(&device->cache.lock);
295 kfree(old_pkey_cache);
296 kfree(old_gid_cache);
306 static void ib_cache_task(void *work_ptr)
308 struct ib_update_work *work = work_ptr;
310 ib_cache_update(work->device, work->port_num);
313 static void ib_work_item (
314 IN PDEVICE_OBJECT DeviceObject,
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);
326 static void ib_cache_event(struct ib_event_handler *handler,
327 struct ib_event *event)
329 struct ib_update_work *work;
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) {
338 work = kmalloc(sizeof *work, GFP_ATOMIC);
340 PDEVICE_OBJECT pdo = to_mdev(handler->device)->dev->pdev->p_self_do;
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
350 // check whether we are in shutdown
351 if (shutter_use( &event->device->cache.x.work_thread ) <= 0) {
357 work->device = event->device;
358 work->port_num = event->element.port_num;
359 IoQueueWorkItem( work->work_item, ib_work_item, DelayedWorkQueue, work );
364 static void ib_cache_setup_one(struct ib_device *device)
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);
375 port_num = end_port(device) - start_port(device) + 1;
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);
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);
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)));
402 kfree(device->cache.pkey_cache);
403 kfree(device->cache.gid_cache);
404 kfree(device->cache.lmc_cache);
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)
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 );
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]);
425 kfree(device->cache.pkey_cache);
426 kfree(device->cache.gid_cache);
427 kfree(device->cache.lmc_cache);
430 static struct ib_client cache_client = { "cache", ib_cache_setup_one, ib_cache_cleanup_one };
432 int __init ib_cache_setup(void)
434 return ib_register_client(&cache_client);
437 void __exit ib_cache_cleanup(void)
439 ib_unregister_client(&cache_client);