|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Broadcom. All rights reserved. The term |
| 3 | + * Broadcom refers to Broadcom Limited and/or its subsidiaries. |
| 4 | + * |
| 5 | + * This software is available to you under a choice of one of two |
| 6 | + * licenses. You may choose to be licensed under the terms of the GNU |
| 7 | + * General Public License (GPL) Version 2, available from the file |
| 8 | + * COPYING in the main directory of this source tree, or the |
| 9 | + * BSD license below: |
| 10 | + * |
| 11 | + * Redistribution and use in source and binary forms, with or without |
| 12 | + * modification, are permitted provided that the following conditions |
| 13 | + * are met: |
| 14 | + * |
| 15 | + * 1. Redistributions of source code must retain the above copyright |
| 16 | + * notice, this list of conditions and the following disclaimer. |
| 17 | + * 2. Redistributions in binary form must reproduce the above copyright |
| 18 | + * notice, this list of conditions and the following disclaimer in |
| 19 | + * the documentation and/or other materials provided with the |
| 20 | + * distribution. |
| 21 | + * |
| 22 | + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' |
| 23 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 24 | + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 25 | + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS |
| 26 | + * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 27 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 28 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 29 | + * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 30 | + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 31 | + * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 32 | + * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | + * |
| 34 | + * Description: Direct verbs API function definitions. |
| 35 | + */ |
| 36 | + |
| 37 | +#include <stdio.h> |
| 38 | +#include <sys/mman.h> |
| 39 | + |
| 40 | +#include "main.h" |
| 41 | +#include "bnxt_re-abi.h" |
| 42 | +#include "bnxt_re_dv.h" |
| 43 | +#include "./verbs.h" |
| 44 | +#include "dv_internal.h" |
| 45 | + |
| 46 | +/* Returns details about the default Doorbell page for ucontext */ |
| 47 | +int bnxt_re_dv_get_default_db_region(struct ibv_context *ibvctx, |
| 48 | + struct bnxt_re_dv_db_region_attr *out) |
| 49 | +{ |
| 50 | + struct bnxt_re_context *cntx = to_bnxt_re_context(ibvctx); |
| 51 | + struct bnxt_re_dv_db_region_attr attr = {}; |
| 52 | + int ret; |
| 53 | + |
| 54 | + DECLARE_COMMAND_BUFFER(cmd, |
| 55 | + BNXT_RE_OBJECT_DEFAULT_DBR, |
| 56 | + BNXT_RE_METHOD_GET_DEFAULT_DBR, |
| 57 | + 1); |
| 58 | + |
| 59 | + fill_attr_out_ptr(cmd, BNXT_RE_DV_DEFAULT_DBR_ATTR, &attr); |
| 60 | + |
| 61 | + ret = execute_ioctl(ibvctx, cmd); |
| 62 | + if (ret) { |
| 63 | + fprintf(stderr, "%s: execute_ioctl() failed: %d\n", __func__, ret); |
| 64 | + return ret; |
| 65 | + } |
| 66 | + out->dbr = cntx->udpi.dbpage; |
| 67 | + out->dpi = attr.dpi; |
| 68 | + out->umdbr = attr.umdbr; |
| 69 | + return 0; |
| 70 | +} |
| 71 | + |
| 72 | +int bnxt_re_dv_free_db_region(struct ibv_context *ctx, |
| 73 | + struct bnxt_re_dv_db_region_attr *attr) |
| 74 | +{ |
| 75 | + struct bnxt_re_dev *dev = to_bnxt_re_dev(ctx->device); |
| 76 | + int ret; |
| 77 | + |
| 78 | + DECLARE_COMMAND_BUFFER(cmd, |
| 79 | + BNXT_RE_OBJECT_DBR, |
| 80 | + BNXT_RE_METHOD_DBR_FREE, |
| 81 | + 1); |
| 82 | + |
| 83 | + if (attr->dbr != MAP_FAILED) |
| 84 | + munmap(attr->dbr, dev->pg_size); |
| 85 | + |
| 86 | + fill_attr_in_obj(cmd, BNXT_RE_DV_FREE_DBR_HANDLE, attr->handle); |
| 87 | + |
| 88 | + ret = execute_ioctl(ctx, cmd); |
| 89 | + if (ret) { |
| 90 | + fprintf(stderr, "%s: execute_ioctl() failed: %d\n", |
| 91 | + __func__, ret); |
| 92 | + errno = ret; |
| 93 | + return ret; |
| 94 | + } |
| 95 | + |
| 96 | + free(attr); |
| 97 | + return 0; |
| 98 | +} |
| 99 | + |
| 100 | +struct bnxt_re_dv_db_region_attr * |
| 101 | +bnxt_re_dv_alloc_db_region(struct ibv_context *ctx) |
| 102 | +{ |
| 103 | + struct bnxt_re_dev *dev = to_bnxt_re_dev(ctx->device); |
| 104 | + struct bnxt_re_dv_db_region_attr attr = {}, *out; |
| 105 | + struct ib_uverbs_attr *handle; |
| 106 | + uint64_t mmap_offset = 0; |
| 107 | + int ret; |
| 108 | + |
| 109 | + DECLARE_COMMAND_BUFFER(cmd, |
| 110 | + BNXT_RE_OBJECT_DBR, |
| 111 | + BNXT_RE_METHOD_DBR_ALLOC, |
| 112 | + 3); |
| 113 | + |
| 114 | + out = calloc(1, sizeof(*out)); |
| 115 | + if (!out) { |
| 116 | + errno = ENOMEM; |
| 117 | + return NULL; |
| 118 | + } |
| 119 | + |
| 120 | + handle = fill_attr_out_obj(cmd, BNXT_RE_DV_ALLOC_DBR_HANDLE); |
| 121 | + fill_attr_out_ptr(cmd, BNXT_RE_DV_ALLOC_DBR_ATTR, &attr); |
| 122 | + fill_attr_out_ptr(cmd, BNXT_RE_DV_ALLOC_DBR_OFFSET, &mmap_offset); |
| 123 | + |
| 124 | + ret = execute_ioctl(ctx, cmd); |
| 125 | + if (ret) { |
| 126 | + fprintf(stderr, "%s: execute_ioctl() failed: %d\n", |
| 127 | + __func__, ret); |
| 128 | + free(out); |
| 129 | + errno = ret; |
| 130 | + return NULL; |
| 131 | + } |
| 132 | + out->handle = read_attr_obj(BNXT_RE_DV_ALLOC_DBR_HANDLE, handle); |
| 133 | + out->dpi = attr.dpi; |
| 134 | + out->umdbr = attr.umdbr; |
| 135 | + |
| 136 | + out->dbr = mmap(NULL, dev->pg_size, PROT_WRITE, |
| 137 | + MAP_SHARED, ctx->cmd_fd, mmap_offset); |
| 138 | + if (out->dbr == MAP_FAILED) { |
| 139 | + fprintf(stderr, DEV "%s: mmap failed\n", __func__); |
| 140 | + bnxt_re_dv_free_db_region(ctx, out); |
| 141 | + errno = ENOMEM; |
| 142 | + return NULL; |
| 143 | + } |
| 144 | + |
| 145 | + return out; |
| 146 | +} |
| 147 | + |
| 148 | +void *bnxt_re_dv_umem_reg(struct ibv_context *ibvctx, struct bnxt_re_dv_umem_reg_attr *in) |
| 149 | +{ |
| 150 | + struct bnxt_re_dv_umem_internal *umem; |
| 151 | + int ret; |
| 152 | + |
| 153 | + ret = ibv_dontfork_range(in->addr, in->size); |
| 154 | + if (ret) { |
| 155 | + errno = ret; |
| 156 | + return NULL; |
| 157 | + } |
| 158 | + |
| 159 | + if (in->comp_mask & BNXT_RE_DV_UMEM_FLAGS_DMABUF && |
| 160 | + (in->dmabuf_fd == -1)) { |
| 161 | + fprintf(stderr, "%s: failed: EBADF\n", __func__); |
| 162 | + errno = EBADF; |
| 163 | + goto err; |
| 164 | + } |
| 165 | + |
| 166 | + umem = calloc(1, sizeof(*umem)); |
| 167 | + if (!umem) { |
| 168 | + errno = ENOMEM; |
| 169 | + goto err; |
| 170 | + } |
| 171 | + |
| 172 | + umem->context = ibvctx; |
| 173 | + umem->addr = in->addr; |
| 174 | + umem->size = in->size; |
| 175 | + umem->access_flags = in->access_flags; |
| 176 | + umem->pgsz_bitmap = in->pgsz_bitmap; |
| 177 | + umem->dmabuf_fd = in->dmabuf_fd; |
| 178 | + |
| 179 | + return (void *)umem; |
| 180 | + |
| 181 | +err: |
| 182 | + ibv_dofork_range(in->addr, in->size); |
| 183 | + return NULL; |
| 184 | +} |
| 185 | + |
| 186 | +int bnxt_re_dv_umem_dereg(void *umem_handle) |
| 187 | +{ |
| 188 | + struct bnxt_re_dv_umem_internal *umem = umem_handle; |
| 189 | + |
| 190 | + ibv_dofork_range(umem->addr, umem->size); |
| 191 | + free(umem); |
| 192 | + return 0; |
| 193 | +} |
0 commit comments