freebsd-dev/contrib/ofed/libmlx5/dbrec.c
Hans Petter Selasky d6b92ffa99 OFED user-space import and update for use with Linux-4.9 compatible RDMA
kernel APIs.

List of sources used:

1) rdma-core was cloned from "https://github.com/linux-rdma/rdma-core.git"
Top commit d65138ef93af30b3ea249f3a84aa6a24ba7f8a75

2) OpenSM was cloned from git://git.openfabrics.org/~halr/opensm.git
Top commit 85f841cf209f791c89a075048a907020e924528d

3) libibmad was cloned from "git://git.openfabrics.org/~iraweiny/libibmad.git"
Tag 1.3.13 with some additional patches from Mellanox.

4) infiniband-diags was cloned from "git://git.openfabrics.org/~iraweiny/infiniband-diags.git"
Tag 1.6.7 with some additional patches from Mellanox.

Added the required Makefiles for building and installing.

Sponsored by:	Mellanox Technologies
2017-08-02 16:00:30 +00:00

149 lines
3.7 KiB
C

/*
* Copyright (c) 2012 Mellanox Technologies, Inc. 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.
*/
#define _GNU_SOURCE
#include <config.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include "mlx5.h"
struct mlx5_db_page {
struct mlx5_db_page *prev, *next;
struct mlx5_buf buf;
int num_db;
int use_cnt;
unsigned long free[0];
};
static struct mlx5_db_page *__add_page(struct mlx5_context *context)
{
struct mlx5_db_page *page;
int ps = to_mdev(context->ibv_ctx.device)->page_size;
int pp;
int i;
int nlong;
pp = ps / context->cache_line_size;
nlong = (pp + 8 * sizeof(long) - 1) / (8 * sizeof(long));
page = malloc(sizeof *page + nlong * sizeof(long));
if (!page)
return NULL;
if (mlx5_alloc_buf(&page->buf, ps, ps)) {
free(page);
return NULL;
}
page->num_db = pp;
page->use_cnt = 0;
for (i = 0; i < nlong; ++i)
page->free[i] = ~0;
page->prev = NULL;
page->next = context->db_list;
context->db_list = page;
if (page->next)
page->next->prev = page;
return page;
}
uint32_t *mlx5_alloc_dbrec(struct mlx5_context *context)
{
struct mlx5_db_page *page;
uint32_t *db = NULL;
int i, j;
pthread_mutex_lock(&context->db_list_mutex);
for (page = context->db_list; page; page = page->next)
if (page->use_cnt < page->num_db)
goto found;
page = __add_page(context);
if (!page)
goto out;
found:
++page->use_cnt;
for (i = 0; !page->free[i]; ++i)
/* nothing */;
j = ffsl(page->free[i]);
--j;
page->free[i] &= ~(1UL << j);
db = page->buf.buf + (i * 8 * sizeof(long) + j) * context->cache_line_size;
out:
pthread_mutex_unlock(&context->db_list_mutex);
return db;
}
void mlx5_free_db(struct mlx5_context *context, uint32_t *db)
{
struct mlx5_db_page *page;
uintptr_t ps = to_mdev(context->ibv_ctx.device)->page_size;
int i;
pthread_mutex_lock(&context->db_list_mutex);
for (page = context->db_list; page; page = page->next)
if (((uintptr_t) db & ~(ps - 1)) == (uintptr_t) page->buf.buf)
break;
if (!page)
goto out;
i = ((void *) db - page->buf.buf) / context->cache_line_size;
page->free[i / (8 * sizeof(long))] |= 1UL << (i % (8 * sizeof(long)));
if (!--page->use_cnt) {
if (page->prev)
page->prev->next = page->next;
else
context->db_list = page->next;
if (page->next)
page->next->prev = page->prev;
mlx5_free_buf(&page->buf);
free(page);
}
out:
pthread_mutex_unlock(&context->db_list_mutex);
}