2 * Copyright (c) 2012 Mellanox Technologies, Inc. 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
42 struct mlx5_db_page *prev, *next;
46 unsigned long free[0];
49 static struct mlx5_db_page *__add_page(struct mlx5_context *context)
51 struct mlx5_db_page *page;
52 int ps = to_mdev(context->ibv_ctx.device)->page_size;
57 pp = ps / context->cache_line_size;
58 nlong = (pp + 8 * sizeof(long) - 1) / (8 * sizeof(long));
60 page = malloc(sizeof *page + nlong * sizeof(long));
64 if (mlx5_alloc_buf(&page->buf, ps, ps)) {
71 for (i = 0; i < nlong; ++i)
75 page->next = context->db_list;
76 context->db_list = page;
78 page->next->prev = page;
83 uint32_t *mlx5_alloc_dbrec(struct mlx5_context *context)
85 struct mlx5_db_page *page;
89 pthread_mutex_lock(&context->db_list_mutex);
91 for (page = context->db_list; page; page = page->next)
92 if (page->use_cnt < page->num_db)
95 page = __add_page(context);
102 for (i = 0; !page->free[i]; ++i)
105 j = ffsl(page->free[i]);
107 page->free[i] &= ~(1UL << j);
108 db = page->buf.buf + (i * 8 * sizeof(long) + j) * context->cache_line_size;
111 pthread_mutex_unlock(&context->db_list_mutex);
116 void mlx5_free_db(struct mlx5_context *context, uint32_t *db)
118 struct mlx5_db_page *page;
119 uintptr_t ps = to_mdev(context->ibv_ctx.device)->page_size;
122 pthread_mutex_lock(&context->db_list_mutex);
124 for (page = context->db_list; page; page = page->next)
125 if (((uintptr_t) db & ~(ps - 1)) == (uintptr_t) page->buf.buf)
131 i = ((void *) db - page->buf.buf) / context->cache_line_size;
132 page->free[i / (8 * sizeof(long))] |= 1UL << (i % (8 * sizeof(long)));
134 if (!--page->use_cnt) {
136 page->prev->next = page->next;
138 context->db_list = page->next;
140 page->next->prev = page->prev;
142 mlx5_free_buf(&page->buf);
147 pthread_mutex_unlock(&context->db_list_mutex);