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>
44 #define MTHCA_FREE_MAP_SIZE (MTHCA_DB_REC_PER_PAGE / (SIZEOF_LONG * 8))
46 struct mthca_db_page {
47 unsigned long free[MTHCA_FREE_MAP_SIZE];
48 struct mthca_buf db_rec;
51 struct mthca_db_table {
55 pthread_mutex_t mutex;
56 struct mthca_db_page page[];
59 int mthca_alloc_db(struct mthca_db_table *db_tab, enum mthca_db_type type,
63 int group, start, end, dir;
66 pthread_mutex_lock(&db_tab->mutex);
69 case MTHCA_DB_TYPE_CQ_ARM:
70 case MTHCA_DB_TYPE_SQ:
73 end = db_tab->max_group1;
77 case MTHCA_DB_TYPE_CQ_SET_CI:
78 case MTHCA_DB_TYPE_RQ:
79 case MTHCA_DB_TYPE_SRQ:
81 start = db_tab->npages - 1;
82 end = db_tab->min_group2;
91 for (i = start; i != end; i += dir)
92 if (db_tab->page[i].db_rec.buf)
93 for (j = 0; j < MTHCA_FREE_MAP_SIZE; ++j)
94 if (db_tab->page[i].free[j])
97 if (db_tab->max_group1 >= db_tab->min_group2 - 1) {
102 if (mthca_alloc_buf(&db_tab->page[i].db_rec,
103 MTHCA_DB_REC_PAGE_SIZE,
104 MTHCA_DB_REC_PAGE_SIZE)) {
109 memset(db_tab->page[i].db_rec.buf, 0, MTHCA_DB_REC_PAGE_SIZE);
110 memset(db_tab->page[i].free, 0xff, sizeof db_tab->page[i].free);
113 ++db_tab->max_group1;
115 --db_tab->min_group2;
118 for (j = 0; j < MTHCA_FREE_MAP_SIZE; ++j) {
119 k = ffsl(db_tab->page[i].free[j]);
130 db_tab->page[i].free[j] &= ~(1UL << k);
132 j = j * SIZEOF_LONG * 8 + k;
134 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
136 ret = i * MTHCA_DB_REC_PER_PAGE + j;
137 *db = db_tab->page[i].db_rec.buf + j * 8;
140 pthread_mutex_unlock(&db_tab->mutex);
144 void mthca_set_db_qn(uint32_t *db, enum mthca_db_type type, uint32_t qn)
146 db[1] = htonl((qn << 8) | (type << 5));
149 void mthca_free_db(struct mthca_db_table *db_tab, enum mthca_db_type type, int db_index)
152 struct mthca_db_page *page;
154 i = db_index / MTHCA_DB_REC_PER_PAGE;
155 j = db_index % MTHCA_DB_REC_PER_PAGE;
157 page = db_tab->page + i;
159 pthread_mutex_lock(&db_tab->mutex);
160 *(uint64_t *) (page->db_rec.buf + j * 8) = 0;
162 if (i >= db_tab->min_group2)
163 j = MTHCA_DB_REC_PER_PAGE - 1 - j;
165 page->free[j / (SIZEOF_LONG * 8)] |= 1UL << (j % (SIZEOF_LONG * 8));
167 pthread_mutex_unlock(&db_tab->mutex);
170 struct mthca_db_table *mthca_alloc_db_tab(int uarc_size)
172 struct mthca_db_table *db_tab;
176 npages = uarc_size / MTHCA_DB_REC_PAGE_SIZE;
177 db_tab = malloc(sizeof (struct mthca_db_table) +
178 npages * sizeof (struct mthca_db_page));
180 pthread_mutex_init(&db_tab->mutex, NULL);
182 db_tab->npages = npages;
183 db_tab->max_group1 = 0;
184 db_tab->min_group2 = npages - 1;
186 for (i = 0; i < npages; ++i)
187 db_tab->page[i].db_rec.buf = NULL;
192 void mthca_free_db_tab(struct mthca_db_table *db_tab)
199 for (i = 0; i < db_tab->npages; ++i)
200 if (db_tab->page[i].db_rec.buf)
201 mthca_free_buf(&db_tab->page[i].db_rec);