2 * Copyright (c) 2005 Topspin Communications. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #endif /* HAVE_CONFIG_H */
38 #include <netinet/in.h>
45 struct mlx4_db_page *prev, *next;
49 unsigned long free[0];
52 static const int db_size[] = {
53 [MLX4_DB_TYPE_CQ] = 8,
54 [MLX4_DB_TYPE_RQ] = 4,
57 static struct mlx4_db_page *__add_page(struct mlx4_context *context,
58 enum mlx4_db_type type)
60 struct mlx4_db_page *page;
61 int ps = to_mdev(context->ibv_ctx.device)->page_size;
65 pp = ps / db_size[type];
67 page = malloc(sizeof *page + pp / 8);
71 if (mlx4_alloc_buf(&page->buf, ps, ps)) {
78 for (i = 0; i < pp / (sizeof (long) * 8); ++i)
82 page->next = context->db_list[type];
83 context->db_list[type] = page;
85 page->next->prev = page;
90 uint32_t *mlx4_alloc_db(struct mlx4_context *context, enum mlx4_db_type type)
92 struct mlx4_db_page *page;
96 pthread_mutex_lock(&context->db_list_mutex);
98 for (page = context->db_list[type]; page; page = page->next)
99 if (page->use_cnt < page->num_db)
102 page = __add_page(context, type);
109 for (i = 0; !page->free[i]; ++i)
112 j = ffsl(page->free[i]);
113 page->free[i] &= ~(1UL << (j - 1));
114 db = page->buf.buf + (i * 8 * sizeof (long) + (j - 1)) * db_size[type];
117 pthread_mutex_unlock(&context->db_list_mutex);
122 void mlx4_free_db(struct mlx4_context *context, enum mlx4_db_type type, uint32_t *db)
124 struct mlx4_db_page *page;
125 uintptr_t ps = to_mdev(context->ibv_ctx.device)->page_size;
128 pthread_mutex_lock(&context->db_list_mutex);
130 for (page = context->db_list[type]; page; page = page->next)
131 if (((uintptr_t) db & ~(ps - 1)) == (uintptr_t) page->buf.buf)
137 i = ((void *) db - page->buf.buf) / db_size[type];
138 page->free[i / (8 * sizeof (long))] |= 1UL << (i % (8 * sizeof (long)));
140 if (!--page->use_cnt) {
142 page->prev->next = page->next;
144 context->db_list[type] = page->next;
146 page->next->prev = page->prev;
148 mlx4_free_buf(&page->buf);
153 pthread_mutex_unlock(&context->db_list_mutex);