1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
|
/*
* Copyright (c) 2005 Topspin Communications. All rights reserved.
*
* This software is available to you under a choice of one of two
* licenses. You may choose to be licensed under the terms of the GNU
* General Public License (GPL) Version 2, available from the file
* COPYING in the main directory of this source tree, or the
* OpenIB.org BSD license below:
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above
* copyright notice, this list of conditions and the following
* disclaimer.
*
* - Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* $Id: memfree.c 8817 2006-08-03 16:46:09Z roland $
*/
#if HAVE_CONFIG_H
# include <config.h>
#endif /* HAVE_CONFIG_H */
#include <stdlib.h>
#include <netinet/in.h>
#include <pthread.h>
#include <string.h>
#include "mthca.h"
#define MTHCA_FREE_MAP_SIZE (MTHCA_DB_REC_PER_PAGE / (SIZEOF_LONG * 8))
struct mthca_db_page {
unsigned long free[MTHCA_FREE_MAP_SIZE];
struct mthca_buf db_rec;
};
struct mthca_db_table {
int npages;
int max_group1;
int min_group2;
pthread_mutex_t mutex;
struct mthca_db_page page[];
};
int mthca_alloc_db(struct mthca_db_table *db_tab, enum mthca_db_type type,
uint32_t **db)
{
int i, j, k;
int group, start, end, dir;
int ret = 0;
pthread_mutex_lock(&db_tab->mutex);
switch (type) {
case MTHCA_DB_TYPE_CQ_ARM:
case MTHCA_DB_TYPE_SQ:
group = 0;
start = 0;
end = db_tab->max_group1;
dir = 1;
break;
case MTHCA_DB_TYPE_CQ_SET_CI:
case MTHCA_DB_TYPE_RQ:
case MTHCA_DB_TYPE_SRQ:
group = 1;
start = db_tab->npages - 1;
end = db_tab->min_group2;
dir = -1;
break;
default:
ret = -1;
goto out;
}
for (i = start; i != end; i += dir)
if (db_tab->page[i].db_rec.buf)
for (j = 0; j < MTHCA_FREE_MAP_SIZE; ++j)
if (db_tab->page[i].free[j])
goto found;
if (db_tab->max_group1 >= db_tab->min_group2 - 1) {
ret = -1;
goto out;
}
if (mthca_alloc_buf(&db_tab->page[i].db_rec,
MTHCA_DB_REC_PAGE_SIZE,
MTHCA_DB_REC_PAGE_SIZE)) {
ret = -1;
goto out;
}
memset(db_tab->page[i].db_rec.buf, 0, MTHCA_DB_REC_PAGE_SIZE);
memset(db_tab->page[i].free, 0xff, sizeof db_tab->page[i].free);
if (group == 0)
++db_tab->max_group1;
else
--db_tab->min_group2;
found:
for (j = 0; j < MTHCA_FREE_MAP_SIZE; ++j) {
k = ffsl(db_tab->page[i].free[j]);
if (k)
break;
}
if (!k) {
ret = -1;
goto out;
}
--k;
db_tab->page[i].free[j] &= ~(1UL << k);
j = j * SIZEOF_LONG * 8 + k;
if (group == 1)
j = MTHCA_DB_REC_PER_PAGE - 1 - j;
ret = i * MTHCA_DB_REC_PER_PAGE + j;
*db = db_tab->page[i].db_rec.buf + j * 8;
out:
pthread_mutex_unlock(&db_tab->mutex);
return ret;
}
void mthca_set_db_qn(uint32_t *db, enum mthca_db_type type, uint32_t qn)
{
db[1] = htonl((qn << 8) | (type << 5));
}
void mthca_free_db(struct mthca_db_table *db_tab, enum mthca_db_type type, int db_index)
{
int i, j;
struct mthca_db_page *page;
i = db_index / MTHCA_DB_REC_PER_PAGE;
j = db_index % MTHCA_DB_REC_PER_PAGE;
page = db_tab->page + i;
pthread_mutex_lock(&db_tab->mutex);
*(uint64_t *) (page->db_rec.buf + j * 8) = 0;
if (i >= db_tab->min_group2)
j = MTHCA_DB_REC_PER_PAGE - 1 - j;
page->free[j / (SIZEOF_LONG * 8)] |= 1UL << (j % (SIZEOF_LONG * 8));
pthread_mutex_unlock(&db_tab->mutex);
}
struct mthca_db_table *mthca_alloc_db_tab(int uarc_size)
{
struct mthca_db_table *db_tab;
int npages;
int i;
npages = uarc_size / MTHCA_DB_REC_PAGE_SIZE;
db_tab = malloc(sizeof (struct mthca_db_table) +
npages * sizeof (struct mthca_db_page));
pthread_mutex_init(&db_tab->mutex, NULL);
db_tab->npages = npages;
db_tab->max_group1 = 0;
db_tab->min_group2 = npages - 1;
for (i = 0; i < npages; ++i)
db_tab->page[i].db_rec.buf = NULL;
return db_tab;
}
void mthca_free_db_tab(struct mthca_db_table *db_tab)
{
int i;
if (!db_tab)
return;
for (i = 0; i < db_tab->npages; ++i)
if (db_tab->page[i].db_rec.buf)
mthca_free_buf(&db_tab->page[i].db_rec);
free(db_tab);
}
|