xref: /linux/drivers/infiniband/hw/mlx4/mr.c (revision d91517839e5d95adc0cf4b28caa7af62a71de526)
1 /*
2  * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3  * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
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  * OpenIB.org BSD license below:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      - Redistributions of source code must retain the above
16  *        copyright notice, this list of conditions and the following
17  *        disclaimer.
18  *
19  *      - Redistributions in binary form must reproduce the above
20  *        copyright notice, this list of conditions and the following
21  *        disclaimer in the documentation and/or other materials
22  *        provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/slab.h>
35 
36 #include "mlx4_ib.h"
37 
38 static u32 convert_access(int acc)
39 {
40 	return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX4_PERM_ATOMIC       : 0) |
41 	       (acc & IB_ACCESS_REMOTE_WRITE  ? MLX4_PERM_REMOTE_WRITE : 0) |
42 	       (acc & IB_ACCESS_REMOTE_READ   ? MLX4_PERM_REMOTE_READ  : 0) |
43 	       (acc & IB_ACCESS_LOCAL_WRITE   ? MLX4_PERM_LOCAL_WRITE  : 0) |
44 	       (acc & IB_ACCESS_MW_BIND	      ? MLX4_PERM_BIND_MW      : 0) |
45 	       MLX4_PERM_LOCAL_READ;
46 }
47 
48 static enum mlx4_mw_type to_mlx4_type(enum ib_mw_type type)
49 {
50 	switch (type) {
51 	case IB_MW_TYPE_1:	return MLX4_MW_TYPE_1;
52 	case IB_MW_TYPE_2:	return MLX4_MW_TYPE_2;
53 	default:		return -1;
54 	}
55 }
56 
57 struct ib_mr *mlx4_ib_get_dma_mr(struct ib_pd *pd, int acc)
58 {
59 	struct mlx4_ib_mr *mr;
60 	int err;
61 
62 	mr = kmalloc(sizeof *mr, GFP_KERNEL);
63 	if (!mr)
64 		return ERR_PTR(-ENOMEM);
65 
66 	err = mlx4_mr_alloc(to_mdev(pd->device)->dev, to_mpd(pd)->pdn, 0,
67 			    ~0ull, convert_access(acc), 0, 0, &mr->mmr);
68 	if (err)
69 		goto err_free;
70 
71 	err = mlx4_mr_enable(to_mdev(pd->device)->dev, &mr->mmr);
72 	if (err)
73 		goto err_mr;
74 
75 	mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
76 	mr->umem = NULL;
77 
78 	return &mr->ibmr;
79 
80 err_mr:
81 	(void) mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
82 
83 err_free:
84 	kfree(mr);
85 
86 	return ERR_PTR(err);
87 }
88 
89 int mlx4_ib_umem_write_mtt(struct mlx4_ib_dev *dev, struct mlx4_mtt *mtt,
90 			   struct ib_umem *umem)
91 {
92 	u64 *pages;
93 	struct ib_umem_chunk *chunk;
94 	int i, j, k;
95 	int n;
96 	int len;
97 	int err = 0;
98 
99 	pages = (u64 *) __get_free_page(GFP_KERNEL);
100 	if (!pages)
101 		return -ENOMEM;
102 
103 	i = n = 0;
104 
105 	list_for_each_entry(chunk, &umem->chunk_list, list)
106 		for (j = 0; j < chunk->nmap; ++j) {
107 			len = sg_dma_len(&chunk->page_list[j]) >> mtt->page_shift;
108 			for (k = 0; k < len; ++k) {
109 				pages[i++] = sg_dma_address(&chunk->page_list[j]) +
110 					umem->page_size * k;
111 				/*
112 				 * Be friendly to mlx4_write_mtt() and
113 				 * pass it chunks of appropriate size.
114 				 */
115 				if (i == PAGE_SIZE / sizeof (u64)) {
116 					err = mlx4_write_mtt(dev->dev, mtt, n,
117 							     i, pages);
118 					if (err)
119 						goto out;
120 					n += i;
121 					i = 0;
122 				}
123 			}
124 		}
125 
126 	if (i)
127 		err = mlx4_write_mtt(dev->dev, mtt, n, i, pages);
128 
129 out:
130 	free_page((unsigned long) pages);
131 	return err;
132 }
133 
134 struct ib_mr *mlx4_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
135 				  u64 virt_addr, int access_flags,
136 				  struct ib_udata *udata)
137 {
138 	struct mlx4_ib_dev *dev = to_mdev(pd->device);
139 	struct mlx4_ib_mr *mr;
140 	int shift;
141 	int err;
142 	int n;
143 
144 	mr = kmalloc(sizeof *mr, GFP_KERNEL);
145 	if (!mr)
146 		return ERR_PTR(-ENOMEM);
147 
148 	mr->umem = ib_umem_get(pd->uobject->context, start, length,
149 			       access_flags, 0);
150 	if (IS_ERR(mr->umem)) {
151 		err = PTR_ERR(mr->umem);
152 		goto err_free;
153 	}
154 
155 	n = ib_umem_page_count(mr->umem);
156 	shift = ilog2(mr->umem->page_size);
157 
158 	err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, virt_addr, length,
159 			    convert_access(access_flags), n, shift, &mr->mmr);
160 	if (err)
161 		goto err_umem;
162 
163 	err = mlx4_ib_umem_write_mtt(dev, &mr->mmr.mtt, mr->umem);
164 	if (err)
165 		goto err_mr;
166 
167 	err = mlx4_mr_enable(dev->dev, &mr->mmr);
168 	if (err)
169 		goto err_mr;
170 
171 	mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
172 
173 	return &mr->ibmr;
174 
175 err_mr:
176 	(void) mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
177 
178 err_umem:
179 	ib_umem_release(mr->umem);
180 
181 err_free:
182 	kfree(mr);
183 
184 	return ERR_PTR(err);
185 }
186 
187 int mlx4_ib_dereg_mr(struct ib_mr *ibmr)
188 {
189 	struct mlx4_ib_mr *mr = to_mmr(ibmr);
190 	int ret;
191 
192 	ret = mlx4_mr_free(to_mdev(ibmr->device)->dev, &mr->mmr);
193 	if (ret)
194 		return ret;
195 	if (mr->umem)
196 		ib_umem_release(mr->umem);
197 	kfree(mr);
198 
199 	return 0;
200 }
201 
202 struct ib_mw *mlx4_ib_alloc_mw(struct ib_pd *pd, enum ib_mw_type type)
203 {
204 	struct mlx4_ib_dev *dev = to_mdev(pd->device);
205 	struct mlx4_ib_mw *mw;
206 	int err;
207 
208 	mw = kmalloc(sizeof(*mw), GFP_KERNEL);
209 	if (!mw)
210 		return ERR_PTR(-ENOMEM);
211 
212 	err = mlx4_mw_alloc(dev->dev, to_mpd(pd)->pdn,
213 			    to_mlx4_type(type), &mw->mmw);
214 	if (err)
215 		goto err_free;
216 
217 	err = mlx4_mw_enable(dev->dev, &mw->mmw);
218 	if (err)
219 		goto err_mw;
220 
221 	mw->ibmw.rkey = mw->mmw.key;
222 
223 	return &mw->ibmw;
224 
225 err_mw:
226 	mlx4_mw_free(dev->dev, &mw->mmw);
227 
228 err_free:
229 	kfree(mw);
230 
231 	return ERR_PTR(err);
232 }
233 
234 int mlx4_ib_bind_mw(struct ib_qp *qp, struct ib_mw *mw,
235 		    struct ib_mw_bind *mw_bind)
236 {
237 	struct ib_send_wr  wr;
238 	struct ib_send_wr *bad_wr;
239 	int ret;
240 
241 	memset(&wr, 0, sizeof(wr));
242 	wr.opcode               = IB_WR_BIND_MW;
243 	wr.wr_id                = mw_bind->wr_id;
244 	wr.send_flags           = mw_bind->send_flags;
245 	wr.wr.bind_mw.mw        = mw;
246 	wr.wr.bind_mw.bind_info = mw_bind->bind_info;
247 	wr.wr.bind_mw.rkey      = ib_inc_rkey(mw->rkey);
248 
249 	ret = mlx4_ib_post_send(qp, &wr, &bad_wr);
250 	if (!ret)
251 		mw->rkey = wr.wr.bind_mw.rkey;
252 
253 	return ret;
254 }
255 
256 int mlx4_ib_dealloc_mw(struct ib_mw *ibmw)
257 {
258 	struct mlx4_ib_mw *mw = to_mmw(ibmw);
259 
260 	mlx4_mw_free(to_mdev(ibmw->device)->dev, &mw->mmw);
261 	kfree(mw);
262 
263 	return 0;
264 }
265 
266 struct ib_mr *mlx4_ib_alloc_fast_reg_mr(struct ib_pd *pd,
267 					int max_page_list_len)
268 {
269 	struct mlx4_ib_dev *dev = to_mdev(pd->device);
270 	struct mlx4_ib_mr *mr;
271 	int err;
272 
273 	mr = kmalloc(sizeof *mr, GFP_KERNEL);
274 	if (!mr)
275 		return ERR_PTR(-ENOMEM);
276 
277 	err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, 0, 0, 0,
278 			    max_page_list_len, 0, &mr->mmr);
279 	if (err)
280 		goto err_free;
281 
282 	err = mlx4_mr_enable(dev->dev, &mr->mmr);
283 	if (err)
284 		goto err_mr;
285 
286 	mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
287 	mr->umem = NULL;
288 
289 	return &mr->ibmr;
290 
291 err_mr:
292 	(void) mlx4_mr_free(dev->dev, &mr->mmr);
293 
294 err_free:
295 	kfree(mr);
296 	return ERR_PTR(err);
297 }
298 
299 struct ib_fast_reg_page_list *mlx4_ib_alloc_fast_reg_page_list(struct ib_device *ibdev,
300 							       int page_list_len)
301 {
302 	struct mlx4_ib_dev *dev = to_mdev(ibdev);
303 	struct mlx4_ib_fast_reg_page_list *mfrpl;
304 	int size = page_list_len * sizeof (u64);
305 
306 	if (page_list_len > MLX4_MAX_FAST_REG_PAGES)
307 		return ERR_PTR(-EINVAL);
308 
309 	mfrpl = kmalloc(sizeof *mfrpl, GFP_KERNEL);
310 	if (!mfrpl)
311 		return ERR_PTR(-ENOMEM);
312 
313 	mfrpl->ibfrpl.page_list = kmalloc(size, GFP_KERNEL);
314 	if (!mfrpl->ibfrpl.page_list)
315 		goto err_free;
316 
317 	mfrpl->mapped_page_list = dma_alloc_coherent(&dev->dev->pdev->dev,
318 						     size, &mfrpl->map,
319 						     GFP_KERNEL);
320 	if (!mfrpl->mapped_page_list)
321 		goto err_free;
322 
323 	WARN_ON(mfrpl->map & 0x3f);
324 
325 	return &mfrpl->ibfrpl;
326 
327 err_free:
328 	kfree(mfrpl->ibfrpl.page_list);
329 	kfree(mfrpl);
330 	return ERR_PTR(-ENOMEM);
331 }
332 
333 void mlx4_ib_free_fast_reg_page_list(struct ib_fast_reg_page_list *page_list)
334 {
335 	struct mlx4_ib_dev *dev = to_mdev(page_list->device);
336 	struct mlx4_ib_fast_reg_page_list *mfrpl = to_mfrpl(page_list);
337 	int size = page_list->max_page_list_len * sizeof (u64);
338 
339 	dma_free_coherent(&dev->dev->pdev->dev, size, mfrpl->mapped_page_list,
340 			  mfrpl->map);
341 	kfree(mfrpl->ibfrpl.page_list);
342 	kfree(mfrpl);
343 }
344 
345 struct ib_fmr *mlx4_ib_fmr_alloc(struct ib_pd *pd, int acc,
346 				 struct ib_fmr_attr *fmr_attr)
347 {
348 	struct mlx4_ib_dev *dev = to_mdev(pd->device);
349 	struct mlx4_ib_fmr *fmr;
350 	int err = -ENOMEM;
351 
352 	fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
353 	if (!fmr)
354 		return ERR_PTR(-ENOMEM);
355 
356 	err = mlx4_fmr_alloc(dev->dev, to_mpd(pd)->pdn, convert_access(acc),
357 			     fmr_attr->max_pages, fmr_attr->max_maps,
358 			     fmr_attr->page_shift, &fmr->mfmr);
359 	if (err)
360 		goto err_free;
361 
362 	err = mlx4_fmr_enable(to_mdev(pd->device)->dev, &fmr->mfmr);
363 	if (err)
364 		goto err_mr;
365 
366 	fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mfmr.mr.key;
367 
368 	return &fmr->ibfmr;
369 
370 err_mr:
371 	(void) mlx4_mr_free(to_mdev(pd->device)->dev, &fmr->mfmr.mr);
372 
373 err_free:
374 	kfree(fmr);
375 
376 	return ERR_PTR(err);
377 }
378 
379 int mlx4_ib_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
380 		      int npages, u64 iova)
381 {
382 	struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
383 	struct mlx4_ib_dev *dev = to_mdev(ifmr->ibfmr.device);
384 
385 	return mlx4_map_phys_fmr(dev->dev, &ifmr->mfmr, page_list, npages, iova,
386 				 &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
387 }
388 
389 int mlx4_ib_unmap_fmr(struct list_head *fmr_list)
390 {
391 	struct ib_fmr *ibfmr;
392 	int err;
393 	struct mlx4_dev *mdev = NULL;
394 
395 	list_for_each_entry(ibfmr, fmr_list, list) {
396 		if (mdev && to_mdev(ibfmr->device)->dev != mdev)
397 			return -EINVAL;
398 		mdev = to_mdev(ibfmr->device)->dev;
399 	}
400 
401 	if (!mdev)
402 		return 0;
403 
404 	list_for_each_entry(ibfmr, fmr_list, list) {
405 		struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
406 
407 		mlx4_fmr_unmap(mdev, &ifmr->mfmr, &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
408 	}
409 
410 	/*
411 	 * Make sure all MPT status updates are visible before issuing
412 	 * SYNC_TPT firmware command.
413 	 */
414 	wmb();
415 
416 	err = mlx4_SYNC_TPT(mdev);
417 	if (err)
418 		pr_warn("SYNC_TPT error %d when "
419 		       "unmapping FMRs\n", err);
420 
421 	return 0;
422 }
423 
424 int mlx4_ib_fmr_dealloc(struct ib_fmr *ibfmr)
425 {
426 	struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
427 	struct mlx4_ib_dev *dev = to_mdev(ibfmr->device);
428 	int err;
429 
430 	err = mlx4_fmr_free(dev->dev, &ifmr->mfmr);
431 
432 	if (!err)
433 		kfree(ifmr);
434 
435 	return err;
436 }
437