]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/staging/rdma/hfi1/mr.c
402bd64141769e263e7bc22e67ca86e197464360
[karo-tx-linux.git] / drivers / staging / rdma / hfi1 / mr.c
1 /*
2  *
3  * This file is provided under a dual BSD/GPLv2 license.  When using or
4  * redistributing this file, you may do so under either license.
5  *
6  * GPL LICENSE SUMMARY
7  *
8  * Copyright(c) 2015 Intel Corporation.
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of version 2 of the GNU General Public License as
12  * published by the Free Software Foundation.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * BSD LICENSE
20  *
21  * Copyright(c) 2015 Intel Corporation.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  *
27  *  - Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  *  - Redistributions in binary form must reproduce the above copyright
30  *    notice, this list of conditions and the following disclaimer in
31  *    the documentation and/or other materials provided with the
32  *    distribution.
33  *  - Neither the name of Intel Corporation nor the names of its
34  *    contributors may be used to endorse or promote products derived
35  *    from this software without specific prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
39  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
40  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
41  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
44  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
45  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
46  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
47  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48  *
49  */
50
51 #include <rdma/ib_umem.h>
52 #include <rdma/ib_smi.h>
53
54 #include "hfi.h"
55
56 /* Fast memory region */
57 struct hfi1_fmr {
58         struct ib_fmr ibfmr;
59         struct hfi1_mregion mr;        /* must be last */
60 };
61
62 static inline struct hfi1_fmr *to_ifmr(struct ib_fmr *ibfmr)
63 {
64         return container_of(ibfmr, struct hfi1_fmr, ibfmr);
65 }
66
67 static int init_mregion(struct hfi1_mregion *mr, struct ib_pd *pd,
68                         int count)
69 {
70         int m, i = 0;
71         int rval = 0;
72
73         m = (count + HFI1_SEGSZ - 1) / HFI1_SEGSZ;
74         for (; i < m; i++) {
75                 mr->map[i] = kzalloc(sizeof(*mr->map[0]), GFP_KERNEL);
76                 if (!mr->map[i])
77                         goto bail;
78         }
79         mr->mapsz = m;
80         init_completion(&mr->comp);
81         /* count returning the ptr to user */
82         atomic_set(&mr->refcount, 1);
83         mr->pd = pd;
84         mr->max_segs = count;
85 out:
86         return rval;
87 bail:
88         while (i)
89                 kfree(mr->map[--i]);
90         rval = -ENOMEM;
91         goto out;
92 }
93
94 static void deinit_mregion(struct hfi1_mregion *mr)
95 {
96         int i = mr->mapsz;
97
98         mr->mapsz = 0;
99         while (i)
100                 kfree(mr->map[--i]);
101 }
102
103
104 /**
105  * hfi1_get_dma_mr - get a DMA memory region
106  * @pd: protection domain for this memory region
107  * @acc: access flags
108  *
109  * Returns the memory region on success, otherwise returns an errno.
110  * Note that all DMA addresses should be created via the
111  * struct ib_dma_mapping_ops functions (see dma.c).
112  */
113 struct ib_mr *hfi1_get_dma_mr(struct ib_pd *pd, int acc)
114 {
115         struct hfi1_mr *mr = NULL;
116         struct ib_mr *ret;
117         int rval;
118
119         if (to_ipd(pd)->user) {
120                 ret = ERR_PTR(-EPERM);
121                 goto bail;
122         }
123
124         mr = kzalloc(sizeof(*mr), GFP_KERNEL);
125         if (!mr) {
126                 ret = ERR_PTR(-ENOMEM);
127                 goto bail;
128         }
129
130         rval = init_mregion(&mr->mr, pd, 0);
131         if (rval) {
132                 ret = ERR_PTR(rval);
133                 goto bail;
134         }
135
136
137         rval = hfi1_alloc_lkey(&mr->mr, 1);
138         if (rval) {
139                 ret = ERR_PTR(rval);
140                 goto bail_mregion;
141         }
142
143         mr->mr.access_flags = acc;
144         ret = &mr->ibmr;
145 done:
146         return ret;
147
148 bail_mregion:
149         deinit_mregion(&mr->mr);
150 bail:
151         kfree(mr);
152         goto done;
153 }
154
155 static struct hfi1_mr *alloc_mr(int count, struct ib_pd *pd)
156 {
157         struct hfi1_mr *mr;
158         int rval = -ENOMEM;
159         int m;
160
161         /* Allocate struct plus pointers to first level page tables. */
162         m = (count + HFI1_SEGSZ - 1) / HFI1_SEGSZ;
163         mr = kzalloc(sizeof(*mr) + m * sizeof(mr->mr.map[0]), GFP_KERNEL);
164         if (!mr)
165                 goto bail;
166
167         rval = init_mregion(&mr->mr, pd, count);
168         if (rval)
169                 goto bail;
170         /*
171          * ib_reg_phys_mr() will initialize mr->ibmr except for
172          * lkey and rkey.
173          */
174         rval = hfi1_alloc_lkey(&mr->mr, 0);
175         if (rval)
176                 goto bail_mregion;
177         mr->ibmr.lkey = mr->mr.lkey;
178         mr->ibmr.rkey = mr->mr.lkey;
179 done:
180         return mr;
181
182 bail_mregion:
183         deinit_mregion(&mr->mr);
184 bail:
185         kfree(mr);
186         mr = ERR_PTR(rval);
187         goto done;
188 }
189
190 /**
191  * hfi1_reg_phys_mr - register a physical memory region
192  * @pd: protection domain for this memory region
193  * @buffer_list: pointer to the list of physical buffers to register
194  * @num_phys_buf: the number of physical buffers to register
195  * @iova_start: the starting address passed over IB which maps to this MR
196  *
197  * Returns the memory region on success, otherwise returns an errno.
198  */
199 struct ib_mr *hfi1_reg_phys_mr(struct ib_pd *pd,
200                                struct ib_phys_buf *buffer_list,
201                                int num_phys_buf, int acc, u64 *iova_start)
202 {
203         struct hfi1_mr *mr;
204         int n, m, i;
205         struct ib_mr *ret;
206
207         mr = alloc_mr(num_phys_buf, pd);
208         if (IS_ERR(mr)) {
209                 ret = (struct ib_mr *)mr;
210                 goto bail;
211         }
212
213         mr->mr.user_base = *iova_start;
214         mr->mr.iova = *iova_start;
215         mr->mr.access_flags = acc;
216
217         m = 0;
218         n = 0;
219         for (i = 0; i < num_phys_buf; i++) {
220                 mr->mr.map[m]->segs[n].vaddr = (void *) buffer_list[i].addr;
221                 mr->mr.map[m]->segs[n].length = buffer_list[i].size;
222                 mr->mr.length += buffer_list[i].size;
223                 n++;
224                 if (n == HFI1_SEGSZ) {
225                         m++;
226                         n = 0;
227                 }
228         }
229
230         ret = &mr->ibmr;
231
232 bail:
233         return ret;
234 }
235
236 /**
237  * hfi1_reg_user_mr - register a userspace memory region
238  * @pd: protection domain for this memory region
239  * @start: starting userspace address
240  * @length: length of region to register
241  * @mr_access_flags: access flags for this memory region
242  * @udata: unused by the driver
243  *
244  * Returns the memory region on success, otherwise returns an errno.
245  */
246 struct ib_mr *hfi1_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
247                                u64 virt_addr, int mr_access_flags,
248                                struct ib_udata *udata)
249 {
250         struct hfi1_mr *mr;
251         struct ib_umem *umem;
252         struct scatterlist *sg;
253         int n, m, entry;
254         struct ib_mr *ret;
255
256         if (length == 0) {
257                 ret = ERR_PTR(-EINVAL);
258                 goto bail;
259         }
260
261         umem = ib_umem_get(pd->uobject->context, start, length,
262                            mr_access_flags, 0);
263         if (IS_ERR(umem))
264                 return (void *) umem;
265
266         n = umem->nmap;
267
268         mr = alloc_mr(n, pd);
269         if (IS_ERR(mr)) {
270                 ret = (struct ib_mr *)mr;
271                 ib_umem_release(umem);
272                 goto bail;
273         }
274
275         mr->mr.user_base = start;
276         mr->mr.iova = virt_addr;
277         mr->mr.length = length;
278         mr->mr.offset = ib_umem_offset(umem);
279         mr->mr.access_flags = mr_access_flags;
280         mr->umem = umem;
281
282         if (is_power_of_2(umem->page_size))
283                 mr->mr.page_shift = ilog2(umem->page_size);
284         m = 0;
285         n = 0;
286         for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
287                         void *vaddr;
288
289                         vaddr = page_address(sg_page(sg));
290                         if (!vaddr) {
291                                 ret = ERR_PTR(-EINVAL);
292                                 goto bail;
293                         }
294                         mr->mr.map[m]->segs[n].vaddr = vaddr;
295                         mr->mr.map[m]->segs[n].length = umem->page_size;
296                         n++;
297                         if (n == HFI1_SEGSZ) {
298                                 m++;
299                                 n = 0;
300                         }
301         }
302         ret = &mr->ibmr;
303
304 bail:
305         return ret;
306 }
307
308 /**
309  * hfi1_dereg_mr - unregister and free a memory region
310  * @ibmr: the memory region to free
311  *
312  * Returns 0 on success.
313  *
314  * Note that this is called to free MRs created by hfi1_get_dma_mr()
315  * or hfi1_reg_user_mr().
316  */
317 int hfi1_dereg_mr(struct ib_mr *ibmr)
318 {
319         struct hfi1_mr *mr = to_imr(ibmr);
320         int ret = 0;
321         unsigned long timeout;
322
323         hfi1_free_lkey(&mr->mr);
324
325         hfi1_put_mr(&mr->mr); /* will set completion if last */
326         timeout = wait_for_completion_timeout(&mr->mr.comp,
327                 5 * HZ);
328         if (!timeout) {
329                 dd_dev_err(
330                         dd_from_ibdev(mr->mr.pd->device),
331                         "hfi1_dereg_mr timeout mr %p pd %p refcount %u\n",
332                         mr, mr->mr.pd, atomic_read(&mr->mr.refcount));
333                 hfi1_get_mr(&mr->mr);
334                 ret = -EBUSY;
335                 goto out;
336         }
337         deinit_mregion(&mr->mr);
338         if (mr->umem)
339                 ib_umem_release(mr->umem);
340         kfree(mr);
341 out:
342         return ret;
343 }
344
345 /*
346  * Allocate a memory region usable with the
347  * IB_WR_REG_MR send work request.
348  *
349  * Return the memory region on success, otherwise return an errno.
350  * FIXME: IB_WR_REG_MR is not supported
351  */
352 struct ib_mr *hfi1_alloc_mr(struct ib_pd *pd,
353                             enum ib_mr_type mr_type,
354                             u32 max_num_sg)
355 {
356         struct hfi1_mr *mr;
357
358         if (mr_type != IB_MR_TYPE_MEM_REG)
359                 return ERR_PTR(-EINVAL);
360
361         mr = alloc_mr(max_num_sg, pd);
362         if (IS_ERR(mr))
363                 return (struct ib_mr *)mr;
364
365         return &mr->ibmr;
366 }
367
368 /**
369  * hfi1_alloc_fmr - allocate a fast memory region
370  * @pd: the protection domain for this memory region
371  * @mr_access_flags: access flags for this memory region
372  * @fmr_attr: fast memory region attributes
373  *
374  * Returns the memory region on success, otherwise returns an errno.
375  */
376 struct ib_fmr *hfi1_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
377                               struct ib_fmr_attr *fmr_attr)
378 {
379         struct hfi1_fmr *fmr;
380         int m;
381         struct ib_fmr *ret;
382         int rval = -ENOMEM;
383
384         /* Allocate struct plus pointers to first level page tables. */
385         m = (fmr_attr->max_pages + HFI1_SEGSZ - 1) / HFI1_SEGSZ;
386         fmr = kzalloc(sizeof(*fmr) + m * sizeof(fmr->mr.map[0]), GFP_KERNEL);
387         if (!fmr)
388                 goto bail;
389
390         rval = init_mregion(&fmr->mr, pd, fmr_attr->max_pages);
391         if (rval)
392                 goto bail;
393
394         /*
395          * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
396          * rkey.
397          */
398         rval = hfi1_alloc_lkey(&fmr->mr, 0);
399         if (rval)
400                 goto bail_mregion;
401         fmr->ibfmr.rkey = fmr->mr.lkey;
402         fmr->ibfmr.lkey = fmr->mr.lkey;
403         /*
404          * Resources are allocated but no valid mapping (RKEY can't be
405          * used).
406          */
407         fmr->mr.access_flags = mr_access_flags;
408         fmr->mr.max_segs = fmr_attr->max_pages;
409         fmr->mr.page_shift = fmr_attr->page_shift;
410
411         ret = &fmr->ibfmr;
412 done:
413         return ret;
414
415 bail_mregion:
416         deinit_mregion(&fmr->mr);
417 bail:
418         kfree(fmr);
419         ret = ERR_PTR(rval);
420         goto done;
421 }
422
423 /**
424  * hfi1_map_phys_fmr - set up a fast memory region
425  * @ibmfr: the fast memory region to set up
426  * @page_list: the list of pages to associate with the fast memory region
427  * @list_len: the number of pages to associate with the fast memory region
428  * @iova: the virtual address of the start of the fast memory region
429  *
430  * This may be called from interrupt context.
431  */
432
433 int hfi1_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
434                       int list_len, u64 iova)
435 {
436         struct hfi1_fmr *fmr = to_ifmr(ibfmr);
437         struct hfi1_lkey_table *rkt;
438         unsigned long flags;
439         int m, n, i;
440         u32 ps;
441         int ret;
442
443         i = atomic_read(&fmr->mr.refcount);
444         if (i > 2)
445                 return -EBUSY;
446
447         if (list_len > fmr->mr.max_segs) {
448                 ret = -EINVAL;
449                 goto bail;
450         }
451         rkt = &to_idev(ibfmr->device)->lk_table;
452         spin_lock_irqsave(&rkt->lock, flags);
453         fmr->mr.user_base = iova;
454         fmr->mr.iova = iova;
455         ps = 1 << fmr->mr.page_shift;
456         fmr->mr.length = list_len * ps;
457         m = 0;
458         n = 0;
459         for (i = 0; i < list_len; i++) {
460                 fmr->mr.map[m]->segs[n].vaddr = (void *) page_list[i];
461                 fmr->mr.map[m]->segs[n].length = ps;
462                 if (++n == HFI1_SEGSZ) {
463                         m++;
464                         n = 0;
465                 }
466         }
467         spin_unlock_irqrestore(&rkt->lock, flags);
468         ret = 0;
469
470 bail:
471         return ret;
472 }
473
474 /**
475  * hfi1_unmap_fmr - unmap fast memory regions
476  * @fmr_list: the list of fast memory regions to unmap
477  *
478  * Returns 0 on success.
479  */
480 int hfi1_unmap_fmr(struct list_head *fmr_list)
481 {
482         struct hfi1_fmr *fmr;
483         struct hfi1_lkey_table *rkt;
484         unsigned long flags;
485
486         list_for_each_entry(fmr, fmr_list, ibfmr.list) {
487                 rkt = &to_idev(fmr->ibfmr.device)->lk_table;
488                 spin_lock_irqsave(&rkt->lock, flags);
489                 fmr->mr.user_base = 0;
490                 fmr->mr.iova = 0;
491                 fmr->mr.length = 0;
492                 spin_unlock_irqrestore(&rkt->lock, flags);
493         }
494         return 0;
495 }
496
497 /**
498  * hfi1_dealloc_fmr - deallocate a fast memory region
499  * @ibfmr: the fast memory region to deallocate
500  *
501  * Returns 0 on success.
502  */
503 int hfi1_dealloc_fmr(struct ib_fmr *ibfmr)
504 {
505         struct hfi1_fmr *fmr = to_ifmr(ibfmr);
506         int ret = 0;
507         unsigned long timeout;
508
509         hfi1_free_lkey(&fmr->mr);
510         hfi1_put_mr(&fmr->mr); /* will set completion if last */
511         timeout = wait_for_completion_timeout(&fmr->mr.comp,
512                 5 * HZ);
513         if (!timeout) {
514                 hfi1_get_mr(&fmr->mr);
515                 ret = -EBUSY;
516                 goto out;
517         }
518         deinit_mregion(&fmr->mr);
519         kfree(fmr);
520 out:
521         return ret;
522 }