| 1 |
/* |
| 2 |
* Copyright (c) 2012-2016 VMware, Inc. All rights reserved. |
| 3 |
* |
| 4 |
* This program is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of EITHER the GNU General Public License |
| 6 |
* version 2 as published by the Free Software Foundation or the BSD |
| 7 |
* 2-Clause License. This program is distributed in the hope that it |
| 8 |
* will be useful, but WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED |
| 9 |
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. |
| 10 |
* See the GNU General Public License version 2 for more details at |
| 11 |
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html. |
| 12 |
* |
| 13 |
* You should have received a copy of the GNU General Public License |
| 14 |
* along with this program available in the file COPYING in the main |
| 15 |
* directory of this source tree. |
| 16 |
* |
| 17 |
* The BSD 2-Clause License |
| 18 |
* |
| 19 |
* Redistribution and use in source and binary forms, with or |
| 20 |
* without modification, are permitted provided that the following |
| 21 |
* conditions are met: |
| 22 |
* |
| 23 |
* - Redistributions of source code must retain the above |
| 24 |
* copyright notice, this list of conditions and the following |
| 25 |
* disclaimer. |
| 26 |
* |
| 27 |
* - Redistributions in binary form must reproduce the above |
| 28 |
* copyright notice, this list of conditions and the following |
| 29 |
* disclaimer in the documentation and/or other materials |
| 30 |
* provided with the distribution. |
| 31 |
* |
| 32 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 33 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 34 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 35 |
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 36 |
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 37 |
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 38 |
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 39 |
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 40 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 41 |
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 42 |
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 43 |
* OF THE POSSIBILITY OF SUCH DAMAGE. |
| 44 |
*/ |
| 45 |
|
| 46 |
#include <linux/errno.h> |
| 47 |
#include <linux/slab.h> |
| 48 |
#include <linux/bitmap.h> |
| 49 |
|
| 50 |
#include "pvrdma.h" |
| 51 |
|
| 52 |
int pvrdma_page_dir_init(struct pvrdma_dev *dev, struct pvrdma_page_dir *pdir, |
| 53 |
u64 npages, bool alloc_pages) |
| 54 |
{ |
| 55 |
u64 i; |
| 56 |
|
| 57 |
if (npages > PVRDMA_PAGE_DIR_MAX_PAGES) |
| 58 |
return -EINVAL; |
| 59 |
|
| 60 |
memset(pdir, 0, sizeof(*pdir)); |
| 61 |
|
| 62 |
pdir->dir = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE, |
| 63 |
&pdir->dir_dma, GFP_KERNEL); |
| 64 |
if (!pdir->dir) |
| 65 |
goto err; |
| 66 |
|
| 67 |
pdir->ntables = PVRDMA_PAGE_DIR_TABLE(npages - 1) + 1; |
| 68 |
pdir->tables = kcalloc(pdir->ntables, sizeof(*pdir->tables), |
| 69 |
GFP_KERNEL); |
| 70 |
if (!pdir->tables) |
| 71 |
goto err; |
| 72 |
|
| 73 |
for (i = 0; i < pdir->ntables; i++) { |
| 74 |
pdir->tables[i] = dma_alloc_coherent(&dev->pdev->dev, PAGE_SIZE, |
| 75 |
(dma_addr_t *)&pdir->dir[i], |
| 76 |
GFP_KERNEL); |
| 77 |
if (!pdir->tables[i]) |
| 78 |
goto err; |
| 79 |
} |
| 80 |
|
| 81 |
pdir->npages = npages; |
| 82 |
|
| 83 |
if (alloc_pages) { |
| 84 |
pdir->pages = kzalloc_objs(*pdir->pages, npages); |
| 85 |
if (!pdir->pages) |
| 86 |
goto err; |
| 87 |
|
| 88 |
for (i = 0; i < pdir->npages; i++) { |
| 89 |
dma_addr_t page_dma; |
| 90 |
|
| 91 |
pdir->pages[i] = dma_alloc_coherent(&dev->pdev->dev, |
| 92 |
PAGE_SIZE, |
| 93 |
&page_dma, |
| 94 |
GFP_KERNEL); |
| 95 |
if (!pdir->pages[i]) |
| 96 |
goto err; |
| 97 |
|
| 98 |
pvrdma_page_dir_insert_dma(pdir, i, page_dma); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
return 0; |
| 103 |
|
| 104 |
err: |
| 105 |
pvrdma_page_dir_cleanup(dev, pdir); |
| 106 |
|
| 107 |
return -ENOMEM; |
| 108 |
} |
| 109 |
|
| 110 |
static u64 *pvrdma_page_dir_table(struct pvrdma_page_dir *pdir, u64 idx) |
| 111 |
{ |
| 112 |
return pdir->tables[PVRDMA_PAGE_DIR_TABLE(idx)]; |
| 113 |
} |
| 114 |
|
| 115 |
dma_addr_t pvrdma_page_dir_get_dma(struct pvrdma_page_dir *pdir, u64 idx) |
| 116 |
{ |
| 117 |
return pvrdma_page_dir_table(pdir, idx)[PVRDMA_PAGE_DIR_PAGE(idx)]; |
| 118 |
} |
| 119 |
|
| 120 |
static void pvrdma_page_dir_cleanup_pages(struct pvrdma_dev *dev, |
| 121 |
struct pvrdma_page_dir *pdir) |
| 122 |
{ |
| 123 |
if (pdir->pages) { |
| 124 |
u64 i; |
| 125 |
|
| 126 |
for (i = 0; i < pdir->npages && pdir->pages[i]; i++) { |
| 127 |
dma_addr_t page_dma = pvrdma_page_dir_get_dma(pdir, i); |
| 128 |
|
| 129 |
dma_free_coherent(&dev->pdev->dev, PAGE_SIZE, |
| 130 |
pdir->pages[i], page_dma); |
| 131 |
} |
| 132 |
|
| 133 |
kfree(pdir->pages); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
static void pvrdma_page_dir_cleanup_tables(struct pvrdma_dev *dev, |
| 138 |
struct pvrdma_page_dir *pdir) |
| 139 |
{ |
| 140 |
if (pdir->tables) { |
| 141 |
int i; |
| 142 |
|
| 143 |
pvrdma_page_dir_cleanup_pages(dev, pdir); |
| 144 |
|
| 145 |
for (i = 0; i < pdir->ntables; i++) { |
| 146 |
u64 *table = pdir->tables[i]; |
| 147 |
|
| 148 |
if (table) |
| 149 |
dma_free_coherent(&dev->pdev->dev, PAGE_SIZE, |
| 150 |
table, pdir->dir[i]); |
| 151 |
} |
| 152 |
|
| 153 |
kfree(pdir->tables); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
void pvrdma_page_dir_cleanup(struct pvrdma_dev *dev, |
| 158 |
struct pvrdma_page_dir *pdir) |
| 159 |
{ |
| 160 |
if (pdir->dir) { |
| 161 |
pvrdma_page_dir_cleanup_tables(dev, pdir); |
| 162 |
dma_free_coherent(&dev->pdev->dev, PAGE_SIZE, |
| 163 |
pdir->dir, pdir->dir_dma); |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
int pvrdma_page_dir_insert_dma(struct pvrdma_page_dir *pdir, u64 idx, |
| 168 |
dma_addr_t daddr) |
| 169 |
{ |
| 170 |
u64 *table; |
| 171 |
|
| 172 |
if (idx >= pdir->npages) |
| 173 |
return -EINVAL; |
| 174 |
|
| 175 |
table = pvrdma_page_dir_table(pdir, idx); |
| 176 |
table[PVRDMA_PAGE_DIR_PAGE(idx)] = daddr; |
| 177 |
|
| 178 |
return 0; |
| 179 |
} |
| 180 |
|
| 181 |
int pvrdma_page_dir_insert_umem(struct pvrdma_page_dir *pdir, |
| 182 |
struct ib_umem *umem, u64 offset) |
| 183 |
{ |
| 184 |
struct ib_block_iter biter; |
| 185 |
u64 i = offset; |
| 186 |
int ret = 0; |
| 187 |
|
| 188 |
if (offset >= pdir->npages) |
| 189 |
return -EINVAL; |
| 190 |
|
| 191 |
rdma_umem_for_each_dma_block (umem, &biter, PAGE_SIZE) { |
| 192 |
ret = pvrdma_page_dir_insert_dma( |
| 193 |
pdir, i, rdma_block_iter_dma_address(&biter)); |
| 194 |
if (ret) |
| 195 |
goto exit; |
| 196 |
|
| 197 |
i++; |
| 198 |
} |
| 199 |
|
| 200 |
exit: |
| 201 |
return ret; |
| 202 |
} |
| 203 |
|
| 204 |
int pvrdma_page_dir_insert_page_list(struct pvrdma_page_dir *pdir, |
| 205 |
u64 *page_list, |
| 206 |
int num_pages) |
| 207 |
{ |
| 208 |
int i; |
| 209 |
int ret; |
| 210 |
|
| 211 |
if (num_pages > pdir->npages) |
| 212 |
return -EINVAL; |
| 213 |
|
| 214 |
for (i = 0; i < num_pages; i++) { |
| 215 |
ret = pvrdma_page_dir_insert_dma(pdir, i, page_list[i]); |
| 216 |
if (ret) |
| 217 |
return ret; |
| 218 |
} |
| 219 |
|
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
void pvrdma_qp_cap_to_ib(struct ib_qp_cap *dst, const struct pvrdma_qp_cap *src) |
| 224 |
{ |
| 225 |
dst->max_send_wr = src->max_send_wr; |
| 226 |
dst->max_recv_wr = src->max_recv_wr; |
| 227 |
dst->max_send_sge = src->max_send_sge; |
| 228 |
dst->max_recv_sge = src->max_recv_sge; |
| 229 |
dst->max_inline_data = src->max_inline_data; |
| 230 |
} |
| 231 |
|
| 232 |
void ib_qp_cap_to_pvrdma(struct pvrdma_qp_cap *dst, const struct ib_qp_cap *src) |
| 233 |
{ |
| 234 |
dst->max_send_wr = src->max_send_wr; |
| 235 |
dst->max_recv_wr = src->max_recv_wr; |
| 236 |
dst->max_send_sge = src->max_send_sge; |
| 237 |
dst->max_recv_sge = src->max_recv_sge; |
| 238 |
dst->max_inline_data = src->max_inline_data; |
| 239 |
} |
| 240 |
|
| 241 |
void pvrdma_gid_to_ib(union ib_gid *dst, const union pvrdma_gid *src) |
| 242 |
{ |
| 243 |
BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid)); |
| 244 |
memcpy(dst, src, sizeof(*src)); |
| 245 |
} |
| 246 |
|
| 247 |
void ib_gid_to_pvrdma(union pvrdma_gid *dst, const union ib_gid *src) |
| 248 |
{ |
| 249 |
BUILD_BUG_ON(sizeof(union pvrdma_gid) != sizeof(union ib_gid)); |
| 250 |
memcpy(dst, src, sizeof(*src)); |
| 251 |
} |
| 252 |
|
| 253 |
void pvrdma_global_route_to_ib(struct ib_global_route *dst, |
| 254 |
const struct pvrdma_global_route *src) |
| 255 |
{ |
| 256 |
pvrdma_gid_to_ib(&dst->dgid, &src->dgid); |
| 257 |
dst->flow_label = src->flow_label; |
| 258 |
dst->sgid_index = src->sgid_index; |
| 259 |
dst->hop_limit = src->hop_limit; |
| 260 |
dst->traffic_class = src->traffic_class; |
| 261 |
} |
| 262 |
|
| 263 |
void ib_global_route_to_pvrdma(struct pvrdma_global_route *dst, |
| 264 |
const struct ib_global_route *src) |
| 265 |
{ |
| 266 |
ib_gid_to_pvrdma(&dst->dgid, &src->dgid); |
| 267 |
dst->flow_label = src->flow_label; |
| 268 |
dst->sgid_index = src->sgid_index; |
| 269 |
dst->hop_limit = src->hop_limit; |
| 270 |
dst->traffic_class = src->traffic_class; |
| 271 |
} |
| 272 |
|
| 273 |
void pvrdma_ah_attr_to_rdma(struct rdma_ah_attr *dst, |
| 274 |
const struct pvrdma_ah_attr *src) |
| 275 |
{ |
| 276 |
dst->type = RDMA_AH_ATTR_TYPE_ROCE; |
| 277 |
pvrdma_global_route_to_ib(rdma_ah_retrieve_grh(dst), &src->grh); |
| 278 |
rdma_ah_set_dlid(dst, src->dlid); |
| 279 |
rdma_ah_set_sl(dst, src->sl); |
| 280 |
rdma_ah_set_path_bits(dst, src->src_path_bits); |
| 281 |
rdma_ah_set_static_rate(dst, src->static_rate); |
| 282 |
rdma_ah_set_ah_flags(dst, src->ah_flags); |
| 283 |
rdma_ah_set_port_num(dst, src->port_num); |
| 284 |
memcpy(dst->roce.dmac, &src->dmac, ETH_ALEN); |
| 285 |
} |
| 286 |
|
| 287 |
void rdma_ah_attr_to_pvrdma(struct pvrdma_ah_attr *dst, |
| 288 |
const struct rdma_ah_attr *src) |
| 289 |
{ |
| 290 |
ib_global_route_to_pvrdma(&dst->grh, rdma_ah_read_grh(src)); |
| 291 |
dst->dlid = rdma_ah_get_dlid(src); |
| 292 |
dst->sl = rdma_ah_get_sl(src); |
| 293 |
dst->src_path_bits = rdma_ah_get_path_bits(src); |
| 294 |
dst->static_rate = rdma_ah_get_static_rate(src); |
| 295 |
dst->ah_flags = rdma_ah_get_ah_flags(src); |
| 296 |
dst->port_num = rdma_ah_get_port_num(src); |
| 297 |
memcpy(&dst->dmac, src->roce.dmac, sizeof(dst->dmac)); |
| 298 |
} |
| 299 |
|
| 300 |
u8 ib_gid_type_to_pvrdma(enum ib_gid_type gid_type) |
| 301 |
{ |
| 302 |
return (gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP) ? |
| 303 |
PVRDMA_GID_TYPE_FLAG_ROCE_V2 : |
| 304 |
PVRDMA_GID_TYPE_FLAG_ROCE_V1; |
| 305 |
} |