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
39 #include "mthca_dev.h"
40 #if defined(EVENT_TRACING)
44 #include "mt_cache.tmh"
51 #pragma alloc_text (PAGE, ib_cache_setup)
52 #pragma alloc_text (PAGE, ib_cache_cleanup)
56 #pragma warning( disable : 4200)
57 struct ib_pkey_cache {
64 union ib_gid table[0];
66 #pragma warning( default : 4200)
68 struct ib_update_work {
69 PIO_WORKITEM work_item;
70 struct ib_device *device;
74 int ib_get_cached_gid(struct ib_device *device,
79 struct ib_gid_cache *cache;
84 if (port_num < start_port(device) || port_num > end_port(device))
86 if (!device->cache.gid_cache)
89 read_lock_irqsave(&device->cache.lock, &lh);
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(&lh);
103 int ib_find_cached_gid(struct ib_device *device,
108 struct ib_gid_cache *cache;
118 read_lock_irqsave(&device->cache.lock, &lh);
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 = p + start_port(device);
133 read_unlock_irqrestore(&lh);
138 int ib_get_cached_pkey(struct ib_device *device,
143 struct ib_pkey_cache *cache;
148 if (port_num < start_port(device) || port_num > end_port(device))
150 if (!device->cache.gid_cache)
153 read_lock_irqsave(&device->cache.lock, &lh);
155 cache = device->cache.pkey_cache[port_num - start_port(device)];
157 if (index < 0 || index >= cache->table_len)
160 *pkey = cache->table[index];
162 read_unlock_irqrestore(&lh);
167 int ib_find_cached_pkey(struct ib_device *device,
172 struct ib_pkey_cache *cache;
177 if (port_num < start_port(device) || port_num > end_port(device))
180 read_lock_irqsave(&device->cache.lock, &lh);
182 cache = device->cache.pkey_cache[port_num - start_port(device)];
186 for (i = 0; i < cache->table_len; ++i)
187 if ((cache->table[i] & 0x7fff) == (pkey & 0x7fff)) {
193 read_unlock_irqrestore(&lh);
198 static void ib_cache_update(struct ib_device *device,
201 struct ib_port_attr *tprops = NULL;
202 struct ib_pkey_cache *pkey_cache = NULL, *old_pkey_cache;
203 struct ib_gid_cache *gid_cache = NULL, *old_gid_cache;
208 tprops = kmalloc(sizeof *tprops, GFP_KERNEL);
212 ret = ib_query_port(device, port, tprops);
214 HCA_PRINT(TRACE_LEVEL_WARNING,HCA_DBG_LOW,("ib_query_port failed (%d) for %s, port %d\n",
215 ret, device->name, port));
219 pkey_cache = kmalloc(sizeof *pkey_cache + tprops->pkey_tbl_len *
220 sizeof *pkey_cache->table, GFP_KERNEL);
224 pkey_cache->table_len = tprops->pkey_tbl_len;
226 gid_cache = kmalloc(sizeof *gid_cache + tprops->gid_tbl_len *
227 sizeof *gid_cache->table, GFP_KERNEL);
231 gid_cache->table_len = tprops->gid_tbl_len;
233 for (i = 0; i < pkey_cache->table_len; i+=32) {
234 __be16 pkey_chunk[32];
236 ret = ib_query_pkey_chunk(device, port, (u16)i, pkey_chunk);
238 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("ib_query_pkey_chunk failed (%d) for %s (index %d)\n",
239 ret, device->name, i));
242 size = min(32, pkey_cache->table_len - i);
243 RtlCopyMemory(pkey_cache->table + i, pkey_chunk, size*sizeof(u16));
246 for (i = 0; i < gid_cache->table_len; i+=8) {
247 union ib_gid gid_chunk[8];
249 ret = ib_query_gid_chunk(device, port, i, gid_chunk);
251 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("ib_query_gid_chunk failed (%d) for %s (index %d)\n",
252 ret, device->name, i));
255 size = min(8, gid_cache->table_len - i);
256 RtlCopyMemory(gid_cache->table + i, gid_chunk, size*sizeof(union ib_gid));
259 write_lock_irq(&device->cache.lock, &lh);
261 old_pkey_cache = device->cache.pkey_cache[port - start_port(device)];
262 old_gid_cache = device->cache.gid_cache [port - start_port(device)];
264 device->cache.pkey_cache[port - start_port(device)] = pkey_cache;
265 device->cache.gid_cache [port - start_port(device)] = gid_cache;
267 write_unlock_irq(&lh);
269 kfree(old_pkey_cache);
270 kfree(old_gid_cache);
280 static void ib_cache_task(void *work_ptr)
282 struct ib_update_work *work = work_ptr;
284 ib_cache_update(work->device, work->port_num);
287 /* leo: wrapper for Linux work_item callback */
290 IN PDEVICE_OBJECT DeviceObject,
294 struct ib_update_work *work = (struct ib_update_work *)Context;
295 UNREFERENCED_PARAMETER(DeviceObject);
296 ib_cache_task(Context);
297 IoFreeWorkItem(work->work_item);
301 static void ib_cache_event(struct ib_event_handler *handler,
302 struct ib_event *event)
304 struct ib_update_work *work;
306 if (event->event == IB_EVENT_PORT_ERR ||
307 event->event == IB_EVENT_PORT_ACTIVE ||
308 event->event == IB_EVENT_LID_CHANGE ||
309 event->event == IB_EVENT_PKEY_CHANGE ||
310 event->event == IB_EVENT_SM_CHANGE) {
311 work = kmalloc(sizeof *work, GFP_ATOMIC);
312 //TODO: what will happen on allocation failure ?
314 work->device = event->device;
315 work->port_num = event->element.port_num;
317 { // schedule a work item to work
319 PDEVICE_OBJECT pdo = handler->device->mdev->ext->cl_ext.p_self_do;
321 // allocate work item
322 work->work_item = IoAllocateWorkItem(pdo);
323 if (work->work_item == NULL) {
324 HCA_PRINT(TRACE_LEVEL_ERROR ,HCA_DBG_LOW,("Failed to allocate workitem for cache update.\n"));
326 else { // schedule the work
339 HCA_PRINT(TRACE_LEVEL_ERROR ,HCA_DBG_MEMORY,("Failed to memory for workitem.\n"));
344 static void ib_cache_setup_one(struct ib_device *device)
348 rwlock_init(&device->cache.lock);
350 device->cache.pkey_cache =
351 kmalloc(sizeof *device->cache.pkey_cache *
352 (end_port(device) - start_port(device) + 1), GFP_KERNEL);
353 device->cache.gid_cache =
354 kmalloc(sizeof *device->cache.gid_cache *
355 (end_port(device) - start_port(device) + 1), GFP_KERNEL);
357 if (!device->cache.pkey_cache || !device->cache.gid_cache) {
358 HCA_PRINT(TRACE_LEVEL_WARNING ,HCA_DBG_LOW,("Couldn't allocate cache "
359 "for %s\n", device->name));
363 for (p = 0; p <= end_port(device) - start_port(device); ++p) {
364 device->cache.pkey_cache[p] = NULL;
365 device->cache.gid_cache [p] = NULL;
366 ib_cache_update(device, p + start_port(device));
369 INIT_IB_EVENT_HANDLER(&device->cache.event_handler,
370 device, ib_cache_event);
371 if (ib_register_event_handler(&device->cache.event_handler))
377 for (p = 0; p <= end_port(device) - start_port(device); ++p) {
378 kfree(device->cache.pkey_cache[p]);
379 kfree(device->cache.gid_cache[p]);
383 kfree(device->cache.pkey_cache);
384 kfree(device->cache.gid_cache);
387 static void ib_cache_cleanup_one(struct ib_device *device)
391 ib_unregister_event_handler(&device->cache.event_handler);
392 //TODO: how to do that ?
393 // LINUX: flush_scheduled_work();
395 for (p = 0; p <= end_port(device) - start_port(device); ++p) {
396 kfree(device->cache.pkey_cache[p]);
397 kfree(device->cache.gid_cache[p]);
400 kfree(device->cache.pkey_cache);
401 kfree(device->cache.gid_cache);
404 static struct ib_client cache_client = { "cache", ib_cache_setup_one, ib_cache_cleanup_one };
406 int ib_cache_setup(void)
408 return ib_register_client(&cache_client);
411 void ib_cache_cleanup(void)
413 ib_unregister_client(&cache_client);