1219820Sjeff/*
2219820Sjeff * Copyright (c) 2006, 2007 Cisco, Inc.  All rights reserved.
3219820Sjeff *
4219820Sjeff * This software is available to you under a choice of one of two
5219820Sjeff * licenses.  You may choose to be licensed under the terms of the GNU
6219820Sjeff * General Public License (GPL) Version 2, available from the file
7219820Sjeff * COPYING in the main directory of this source tree, or the
8219820Sjeff * OpenIB.org BSD license below:
9219820Sjeff *
10219820Sjeff *     Redistribution and use in source and binary forms, with or
11219820Sjeff *     without modification, are permitted provided that the following
12219820Sjeff *     conditions are met:
13219820Sjeff *
14219820Sjeff *      - Redistributions of source code must retain the above
15219820Sjeff *        copyright notice, this list of conditions and the following
16219820Sjeff *        disclaimer.
17219820Sjeff *
18219820Sjeff *      - Redistributions in binary form must reproduce the above
19219820Sjeff *        copyright notice, this list of conditions and the following
20219820Sjeff *        disclaimer in the documentation and/or other materials
21219820Sjeff *        provided with the distribution.
22219820Sjeff *
23219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30219820Sjeff * SOFTWARE.
31219820Sjeff */
32219820Sjeff
33219820Sjeff#if HAVE_CONFIG_H
34219820Sjeff#  include <config.h>
35219820Sjeff#endif /* HAVE_CONFIG_H */
36219820Sjeff
37219820Sjeff#include <stdlib.h>
38219820Sjeff#include <errno.h>
39219820Sjeff#include <sys/mman.h>
40219820Sjeff
41219820Sjeff#include "mlx4.h"
42219820Sjeff
43219820Sjeff#if !(defined(HAVE_IBV_DONTFORK_RANGE) && defined(HAVE_IBV_DOFORK_RANGE))
44219820Sjeff
45219820Sjeff/*
46219820Sjeff * If libibverbs isn't exporting these functions, then there's no
47219820Sjeff * point in doing it here, because the rest of libibverbs isn't going
48219820Sjeff * to be fork-safe anyway.
49219820Sjeff */
50219820Sjeffstatic int ibv_dontfork_range(void *base, size_t size)
51219820Sjeff{
52219820Sjeff	return 0;
53219820Sjeff}
54219820Sjeff
55219820Sjeffstatic int ibv_dofork_range(void *base, size_t size)
56219820Sjeff{
57219820Sjeff	return 0;
58219820Sjeff}
59219820Sjeff
60219820Sjeff#endif /* HAVE_IBV_DONTFORK_RANGE && HAVE_IBV_DOFORK_RANGE */
61219820Sjeff
62219820Sjeffint mlx4_alloc_buf(struct mlx4_buf *buf, size_t size, int page_size)
63219820Sjeff{
64219820Sjeff	int ret;
65219820Sjeff
66219820Sjeff	buf->length = align(size, page_size);
67219820Sjeff	buf->buf = mmap(NULL, buf->length, PROT_READ | PROT_WRITE,
68219820Sjeff			MAP_PRIVATE | MAP_ANON, -1, 0);
69219820Sjeff	if (buf->buf == MAP_FAILED)
70219820Sjeff		return errno;
71219820Sjeff
72219820Sjeff	ret = ibv_dontfork_range(buf->buf, size);
73219820Sjeff	if (ret)
74219820Sjeff		munmap(buf->buf, buf->length);
75219820Sjeff
76219820Sjeff	return ret;
77219820Sjeff}
78219820Sjeff
79219820Sjeffvoid mlx4_free_buf(struct mlx4_buf *buf)
80219820Sjeff{
81219820Sjeff	ibv_dofork_range(buf->buf, buf->length);
82219820Sjeff	munmap(buf->buf, buf->length);
83219820Sjeff}
84