]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/ofed/libmlx4/dbrec.c
Add liblutok a lightweight C++ API for lua.
[FreeBSD/FreeBSD.git] / contrib / ofed / libmlx4 / dbrec.c
1 /*
2  * Copyright (c) 2005 Topspin Communications.  All rights reserved.
3  *
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:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
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.
22  *
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
30  * SOFTWARE.
31  */
32 #define _GNU_SOURCE
33 #include <config.h>
34
35 #include <stdlib.h>
36 #include <pthread.h>
37 #include <string.h>
38
39 #include "mlx4.h"
40
41 struct mlx4_db_page {
42         struct mlx4_db_page            *prev, *next;
43         struct mlx4_buf                 buf;
44         int                             num_db;
45         int                             use_cnt;
46         unsigned long                   free[0];
47 };
48
49 static const int db_size[] = {
50         [MLX4_DB_TYPE_CQ] = 8,
51         [MLX4_DB_TYPE_RQ] = 4,
52 };
53
54 static struct mlx4_db_page *__add_page(struct mlx4_context *context,
55                                        enum mlx4_db_type type)
56 {
57         struct mlx4_db_page *page;
58         int ps = to_mdev(context->ibv_ctx.device)->page_size;
59         int pp;
60         int i;
61
62         pp = ps / db_size[type];
63
64         page = malloc(sizeof *page + pp / 8);
65         if (!page)
66                 return NULL;
67
68         if (mlx4_alloc_buf(&page->buf, ps, ps)) {
69                 free(page);
70                 return NULL;
71         }
72
73         page->num_db  = pp;
74         page->use_cnt = 0;
75         for (i = 0; i < pp / (sizeof (long) * 8); ++i)
76                 page->free[i] = ~0;
77
78         page->prev = NULL;
79         page->next = context->db_list[type];
80         context->db_list[type] = page;
81         if (page->next)
82                 page->next->prev = page;
83
84         return page;
85 }
86
87 uint32_t *mlx4_alloc_db(struct mlx4_context *context, enum mlx4_db_type type)
88 {
89         struct mlx4_db_page *page;
90         uint32_t *db = NULL;
91         int i, j;
92
93         pthread_mutex_lock(&context->db_list_mutex);
94
95         for (page = context->db_list[type]; page; page = page->next)
96                 if (page->use_cnt < page->num_db)
97                         goto found;
98
99         page = __add_page(context, type);
100         if (!page)
101                 goto out;
102
103 found:
104         ++page->use_cnt;
105
106         for (i = 0; !page->free[i]; ++i)
107                 /* nothing */;
108
109         j = ffsl(page->free[i]);
110         page->free[i] &= ~(1UL << (j - 1));
111         db = page->buf.buf + (i * 8 * sizeof (long) + (j - 1)) * db_size[type];
112
113 out:
114         pthread_mutex_unlock(&context->db_list_mutex);
115
116         return db;
117 }
118
119 void mlx4_free_db(struct mlx4_context *context, enum mlx4_db_type type, uint32_t *db)
120 {
121         struct mlx4_db_page *page;
122         uintptr_t ps = to_mdev(context->ibv_ctx.device)->page_size;
123         int i;
124
125         pthread_mutex_lock(&context->db_list_mutex);
126
127         for (page = context->db_list[type]; page; page = page->next)
128                 if (((uintptr_t) db & ~(ps - 1)) == (uintptr_t) page->buf.buf)
129                         break;
130
131         if (!page)
132                 goto out;
133
134         i = ((void *) db - page->buf.buf) / db_size[type];
135         page->free[i / (8 * sizeof (long))] |= 1UL << (i % (8 * sizeof (long)));
136
137         if (!--page->use_cnt) {
138                 if (page->prev)
139                         page->prev->next = page->next;
140                 else
141                         context->db_list[type] = page->next;
142                 if (page->next)
143                         page->next->prev = page->prev;
144
145                 mlx4_free_buf(&page->buf);
146                 free(page);
147         }
148
149 out:
150         pthread_mutex_unlock(&context->db_list_mutex);
151 }