]> err.no Git - linux-2.6/blob - drivers/infiniband/hw/ehca/ehca_mrmw.c
IB/ehca: Make symbols used only in a single source file static
[linux-2.6] / drivers / infiniband / hw / ehca / ehca_mrmw.c
1 /*
2  *  IBM eServer eHCA Infiniband device driver for Linux on POWER
3  *
4  *  MR/MW functions
5  *
6  *  Authors: Dietmar Decker <ddecker@de.ibm.com>
7  *           Christoph Raisch <raisch@de.ibm.com>
8  *           Hoang-Nam Nguyen <hnguyen@de.ibm.com>
9  *
10  *  Copyright (c) 2005 IBM Corporation
11  *
12  *  All rights reserved.
13  *
14  *  This source code is distributed under a dual license of GPL v2.0 and OpenIB
15  *  BSD.
16  *
17  * OpenIB BSD License
18  *
19  * Redistribution and use in source and binary forms, with or without
20  * modification, are permitted provided that the following conditions are met:
21  *
22  * Redistributions of source code must retain the above copyright notice, this
23  * list of conditions and the following disclaimer.
24  *
25  * Redistributions in binary form must reproduce the above copyright notice,
26  * this list of conditions and the following disclaimer in the documentation
27  * and/or other materials
28  * provided with the distribution.
29  *
30  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
38  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40  * POSSIBILITY OF SUCH DAMAGE.
41  */
42
43 #include <asm/current.h>
44
45 #include <rdma/ib_umem.h>
46
47 #include "ehca_iverbs.h"
48 #include "ehca_mrmw.h"
49 #include "hcp_if.h"
50 #include "hipz_hw.h"
51
52 #define NUM_CHUNKS(length, chunk_size) \
53         (((length) + (chunk_size - 1)) / (chunk_size))
54
55 /* max number of rpages (per hcall register_rpages) */
56 #define MAX_RPAGES 512
57
58 static struct kmem_cache *mr_cache;
59 static struct kmem_cache *mw_cache;
60
61 enum ehca_mr_pgsize {
62         EHCA_MR_PGSIZE4K  = 0x1000L,
63         EHCA_MR_PGSIZE64K = 0x10000L,
64         EHCA_MR_PGSIZE1M  = 0x100000L,
65         EHCA_MR_PGSIZE16M = 0x1000000L
66 };
67
68 #define EHCA_MR_PGSHIFT4K  12
69 #define EHCA_MR_PGSHIFT64K 16
70 #define EHCA_MR_PGSHIFT1M  20
71 #define EHCA_MR_PGSHIFT16M 24
72
73 static u32 ehca_encode_hwpage_size(u32 pgsize)
74 {
75         int log = ilog2(pgsize);
76         WARN_ON(log < 12 || log > 24 || log & 3);
77         return (log - 12) / 4;
78 }
79
80 static u64 ehca_get_max_hwpage_size(struct ehca_shca *shca)
81 {
82         return 1UL << ilog2(shca->hca_cap_mr_pgsize);
83 }
84
85 static struct ehca_mr *ehca_mr_new(void)
86 {
87         struct ehca_mr *me;
88
89         me = kmem_cache_zalloc(mr_cache, GFP_KERNEL);
90         if (me)
91                 spin_lock_init(&me->mrlock);
92         else
93                 ehca_gen_err("alloc failed");
94
95         return me;
96 }
97
98 static void ehca_mr_delete(struct ehca_mr *me)
99 {
100         kmem_cache_free(mr_cache, me);
101 }
102
103 static struct ehca_mw *ehca_mw_new(void)
104 {
105         struct ehca_mw *me;
106
107         me = kmem_cache_zalloc(mw_cache, GFP_KERNEL);
108         if (me)
109                 spin_lock_init(&me->mwlock);
110         else
111                 ehca_gen_err("alloc failed");
112
113         return me;
114 }
115
116 static void ehca_mw_delete(struct ehca_mw *me)
117 {
118         kmem_cache_free(mw_cache, me);
119 }
120
121 /*----------------------------------------------------------------------*/
122
123 struct ib_mr *ehca_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
124 {
125         struct ib_mr *ib_mr;
126         int ret;
127         struct ehca_mr *e_maxmr;
128         struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
129         struct ehca_shca *shca =
130                 container_of(pd->device, struct ehca_shca, ib_device);
131
132         if (shca->maxmr) {
133                 e_maxmr = ehca_mr_new();
134                 if (!e_maxmr) {
135                         ehca_err(&shca->ib_device, "out of memory");
136                         ib_mr = ERR_PTR(-ENOMEM);
137                         goto get_dma_mr_exit0;
138                 }
139
140                 ret = ehca_reg_maxmr(shca, e_maxmr, (u64 *)KERNELBASE,
141                                      mr_access_flags, e_pd,
142                                      &e_maxmr->ib.ib_mr.lkey,
143                                      &e_maxmr->ib.ib_mr.rkey);
144                 if (ret) {
145                         ehca_mr_delete(e_maxmr);
146                         ib_mr = ERR_PTR(ret);
147                         goto get_dma_mr_exit0;
148                 }
149                 ib_mr = &e_maxmr->ib.ib_mr;
150         } else {
151                 ehca_err(&shca->ib_device, "no internal max-MR exist!");
152                 ib_mr = ERR_PTR(-EINVAL);
153                 goto get_dma_mr_exit0;
154         }
155
156 get_dma_mr_exit0:
157         if (IS_ERR(ib_mr))
158                 ehca_err(&shca->ib_device, "h_ret=%li pd=%p mr_access_flags=%x",
159                          PTR_ERR(ib_mr), pd, mr_access_flags);
160         return ib_mr;
161 } /* end ehca_get_dma_mr() */
162
163 /*----------------------------------------------------------------------*/
164
165 struct ib_mr *ehca_reg_phys_mr(struct ib_pd *pd,
166                                struct ib_phys_buf *phys_buf_array,
167                                int num_phys_buf,
168                                int mr_access_flags,
169                                u64 *iova_start)
170 {
171         struct ib_mr *ib_mr;
172         int ret;
173         struct ehca_mr *e_mr;
174         struct ehca_shca *shca =
175                 container_of(pd->device, struct ehca_shca, ib_device);
176         struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
177
178         u64 size;
179
180         if ((num_phys_buf <= 0) || !phys_buf_array) {
181                 ehca_err(pd->device, "bad input values: num_phys_buf=%x "
182                          "phys_buf_array=%p", num_phys_buf, phys_buf_array);
183                 ib_mr = ERR_PTR(-EINVAL);
184                 goto reg_phys_mr_exit0;
185         }
186         if (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
187              !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
188             ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
189              !(mr_access_flags & IB_ACCESS_LOCAL_WRITE))) {
190                 /*
191                  * Remote Write Access requires Local Write Access
192                  * Remote Atomic Access requires Local Write Access
193                  */
194                 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
195                          mr_access_flags);
196                 ib_mr = ERR_PTR(-EINVAL);
197                 goto reg_phys_mr_exit0;
198         }
199
200         /* check physical buffer list and calculate size */
201         ret = ehca_mr_chk_buf_and_calc_size(phys_buf_array, num_phys_buf,
202                                             iova_start, &size);
203         if (ret) {
204                 ib_mr = ERR_PTR(ret);
205                 goto reg_phys_mr_exit0;
206         }
207         if ((size == 0) ||
208             (((u64)iova_start + size) < (u64)iova_start)) {
209                 ehca_err(pd->device, "bad input values: size=%lx iova_start=%p",
210                          size, iova_start);
211                 ib_mr = ERR_PTR(-EINVAL);
212                 goto reg_phys_mr_exit0;
213         }
214
215         e_mr = ehca_mr_new();
216         if (!e_mr) {
217                 ehca_err(pd->device, "out of memory");
218                 ib_mr = ERR_PTR(-ENOMEM);
219                 goto reg_phys_mr_exit0;
220         }
221
222         /* register MR on HCA */
223         if (ehca_mr_is_maxmr(size, iova_start)) {
224                 e_mr->flags |= EHCA_MR_FLAG_MAXMR;
225                 ret = ehca_reg_maxmr(shca, e_mr, iova_start, mr_access_flags,
226                                      e_pd, &e_mr->ib.ib_mr.lkey,
227                                      &e_mr->ib.ib_mr.rkey);
228                 if (ret) {
229                         ib_mr = ERR_PTR(ret);
230                         goto reg_phys_mr_exit1;
231                 }
232         } else {
233                 struct ehca_mr_pginfo pginfo;
234                 u32 num_kpages;
235                 u32 num_hwpages;
236                 u64 hw_pgsize;
237
238                 num_kpages = NUM_CHUNKS(((u64)iova_start % PAGE_SIZE) + size,
239                                         PAGE_SIZE);
240                 /* for kernel space we try most possible pgsize */
241                 hw_pgsize = ehca_get_max_hwpage_size(shca);
242                 num_hwpages = NUM_CHUNKS(((u64)iova_start % hw_pgsize) + size,
243                                          hw_pgsize);
244                 memset(&pginfo, 0, sizeof(pginfo));
245                 pginfo.type = EHCA_MR_PGI_PHYS;
246                 pginfo.num_kpages = num_kpages;
247                 pginfo.hwpage_size = hw_pgsize;
248                 pginfo.num_hwpages = num_hwpages;
249                 pginfo.u.phy.num_phys_buf = num_phys_buf;
250                 pginfo.u.phy.phys_buf_array = phys_buf_array;
251                 pginfo.next_hwpage =
252                         ((u64)iova_start & ~PAGE_MASK) / hw_pgsize;
253
254                 ret = ehca_reg_mr(shca, e_mr, iova_start, size, mr_access_flags,
255                                   e_pd, &pginfo, &e_mr->ib.ib_mr.lkey,
256                                   &e_mr->ib.ib_mr.rkey);
257                 if (ret) {
258                         ib_mr = ERR_PTR(ret);
259                         goto reg_phys_mr_exit1;
260                 }
261         }
262
263         /* successful registration of all pages */
264         return &e_mr->ib.ib_mr;
265
266 reg_phys_mr_exit1:
267         ehca_mr_delete(e_mr);
268 reg_phys_mr_exit0:
269         if (IS_ERR(ib_mr))
270                 ehca_err(pd->device, "h_ret=%li pd=%p phys_buf_array=%p "
271                          "num_phys_buf=%x mr_access_flags=%x iova_start=%p",
272                          PTR_ERR(ib_mr), pd, phys_buf_array,
273                          num_phys_buf, mr_access_flags, iova_start);
274         return ib_mr;
275 } /* end ehca_reg_phys_mr() */
276
277 /*----------------------------------------------------------------------*/
278
279 struct ib_mr *ehca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
280                                u64 virt, int mr_access_flags,
281                                struct ib_udata *udata)
282 {
283         struct ib_mr *ib_mr;
284         struct ehca_mr *e_mr;
285         struct ehca_shca *shca =
286                 container_of(pd->device, struct ehca_shca, ib_device);
287         struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
288         struct ehca_mr_pginfo pginfo;
289         int ret, page_shift;
290         u32 num_kpages;
291         u32 num_hwpages;
292         u64 hwpage_size;
293
294         if (!pd) {
295                 ehca_gen_err("bad pd=%p", pd);
296                 return ERR_PTR(-EFAULT);
297         }
298
299         if (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
300              !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
301             ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
302              !(mr_access_flags & IB_ACCESS_LOCAL_WRITE))) {
303                 /*
304                  * Remote Write Access requires Local Write Access
305                  * Remote Atomic Access requires Local Write Access
306                  */
307                 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
308                          mr_access_flags);
309                 ib_mr = ERR_PTR(-EINVAL);
310                 goto reg_user_mr_exit0;
311         }
312
313         if (length == 0 || virt + length < virt) {
314                 ehca_err(pd->device, "bad input values: length=%lx "
315                          "virt_base=%lx", length, virt);
316                 ib_mr = ERR_PTR(-EINVAL);
317                 goto reg_user_mr_exit0;
318         }
319
320         e_mr = ehca_mr_new();
321         if (!e_mr) {
322                 ehca_err(pd->device, "out of memory");
323                 ib_mr = ERR_PTR(-ENOMEM);
324                 goto reg_user_mr_exit0;
325         }
326
327         e_mr->umem = ib_umem_get(pd->uobject->context, start, length,
328                                  mr_access_flags);
329         if (IS_ERR(e_mr->umem)) {
330                 ib_mr = (void *)e_mr->umem;
331                 goto reg_user_mr_exit1;
332         }
333
334         if (e_mr->umem->page_size != PAGE_SIZE) {
335                 ehca_err(pd->device, "page size not supported, "
336                          "e_mr->umem->page_size=%x", e_mr->umem->page_size);
337                 ib_mr = ERR_PTR(-EINVAL);
338                 goto reg_user_mr_exit2;
339         }
340
341         /* determine number of MR pages */
342         num_kpages = NUM_CHUNKS((virt % PAGE_SIZE) + length, PAGE_SIZE);
343         /* select proper hw_pgsize */
344         page_shift = PAGE_SHIFT;
345         if (e_mr->umem->hugetlb) {
346                 /* determine page_shift, clamp between 4K and 16M */
347                 page_shift = (fls64(length - 1) + 3) & ~3;
348                 page_shift = min(max(page_shift, EHCA_MR_PGSHIFT4K),
349                                  EHCA_MR_PGSHIFT16M);
350         }
351         hwpage_size = 1UL << page_shift;
352
353         /* now that we have the desired page size, shift until it's
354          * supported, too. 4K is always supported, so this terminates.
355          */
356         while (!(hwpage_size & shca->hca_cap_mr_pgsize))
357                 hwpage_size >>= 4;
358
359 reg_user_mr_fallback:
360         num_hwpages = NUM_CHUNKS((virt % hwpage_size) + length, hwpage_size);
361         /* register MR on HCA */
362         memset(&pginfo, 0, sizeof(pginfo));
363         pginfo.type = EHCA_MR_PGI_USER;
364         pginfo.hwpage_size = hwpage_size;
365         pginfo.num_kpages = num_kpages;
366         pginfo.num_hwpages = num_hwpages;
367         pginfo.u.usr.region = e_mr->umem;
368         pginfo.next_hwpage = e_mr->umem->offset / hwpage_size;
369         pginfo.u.usr.next_chunk = list_prepare_entry(pginfo.u.usr.next_chunk,
370                                                      (&e_mr->umem->chunk_list),
371                                                      list);
372
373         ret = ehca_reg_mr(shca, e_mr, (u64 *)virt, length, mr_access_flags,
374                           e_pd, &pginfo, &e_mr->ib.ib_mr.lkey,
375                           &e_mr->ib.ib_mr.rkey);
376         if (ret == -EINVAL && pginfo.hwpage_size > PAGE_SIZE) {
377                 ehca_warn(pd->device, "failed to register mr "
378                           "with hwpage_size=%lx", hwpage_size);
379                 ehca_info(pd->device, "try to register mr with "
380                           "kpage_size=%lx", PAGE_SIZE);
381                 /*
382                  * this means kpages are not contiguous for a hw page
383                  * try kernel page size as fallback solution
384                  */
385                 hwpage_size = PAGE_SIZE;
386                 goto reg_user_mr_fallback;
387         }
388         if (ret) {
389                 ib_mr = ERR_PTR(ret);
390                 goto reg_user_mr_exit2;
391         }
392
393         /* successful registration of all pages */
394         return &e_mr->ib.ib_mr;
395
396 reg_user_mr_exit2:
397         ib_umem_release(e_mr->umem);
398 reg_user_mr_exit1:
399         ehca_mr_delete(e_mr);
400 reg_user_mr_exit0:
401         if (IS_ERR(ib_mr))
402                 ehca_err(pd->device, "rc=%li pd=%p mr_access_flags=%x udata=%p",
403                          PTR_ERR(ib_mr), pd, mr_access_flags, udata);
404         return ib_mr;
405 } /* end ehca_reg_user_mr() */
406
407 /*----------------------------------------------------------------------*/
408
409 int ehca_rereg_phys_mr(struct ib_mr *mr,
410                        int mr_rereg_mask,
411                        struct ib_pd *pd,
412                        struct ib_phys_buf *phys_buf_array,
413                        int num_phys_buf,
414                        int mr_access_flags,
415                        u64 *iova_start)
416 {
417         int ret;
418
419         struct ehca_shca *shca =
420                 container_of(mr->device, struct ehca_shca, ib_device);
421         struct ehca_mr *e_mr = container_of(mr, struct ehca_mr, ib.ib_mr);
422         struct ehca_pd *my_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
423         u64 new_size;
424         u64 *new_start;
425         u32 new_acl;
426         struct ehca_pd *new_pd;
427         u32 tmp_lkey, tmp_rkey;
428         unsigned long sl_flags;
429         u32 num_kpages = 0;
430         u32 num_hwpages = 0;
431         struct ehca_mr_pginfo pginfo;
432         u32 cur_pid = current->tgid;
433
434         if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
435             (my_pd->ownpid != cur_pid)) {
436                 ehca_err(mr->device, "Invalid caller pid=%x ownpid=%x",
437                          cur_pid, my_pd->ownpid);
438                 ret = -EINVAL;
439                 goto rereg_phys_mr_exit0;
440         }
441
442         if (!(mr_rereg_mask & IB_MR_REREG_TRANS)) {
443                 /* TODO not supported, because PHYP rereg hCall needs pages */
444                 ehca_err(mr->device, "rereg without IB_MR_REREG_TRANS not "
445                          "supported yet, mr_rereg_mask=%x", mr_rereg_mask);
446                 ret = -EINVAL;
447                 goto rereg_phys_mr_exit0;
448         }
449
450         if (mr_rereg_mask & IB_MR_REREG_PD) {
451                 if (!pd) {
452                         ehca_err(mr->device, "rereg with bad pd, pd=%p "
453                                  "mr_rereg_mask=%x", pd, mr_rereg_mask);
454                         ret = -EINVAL;
455                         goto rereg_phys_mr_exit0;
456                 }
457         }
458
459         if ((mr_rereg_mask &
460              ~(IB_MR_REREG_TRANS | IB_MR_REREG_PD | IB_MR_REREG_ACCESS)) ||
461             (mr_rereg_mask == 0)) {
462                 ret = -EINVAL;
463                 goto rereg_phys_mr_exit0;
464         }
465
466         /* check other parameters */
467         if (e_mr == shca->maxmr) {
468                 /* should be impossible, however reject to be sure */
469                 ehca_err(mr->device, "rereg internal max-MR impossible, mr=%p "
470                          "shca->maxmr=%p mr->lkey=%x",
471                          mr, shca->maxmr, mr->lkey);
472                 ret = -EINVAL;
473                 goto rereg_phys_mr_exit0;
474         }
475         if (mr_rereg_mask & IB_MR_REREG_TRANS) { /* transl., i.e. addr/size */
476                 if (e_mr->flags & EHCA_MR_FLAG_FMR) {
477                         ehca_err(mr->device, "not supported for FMR, mr=%p "
478                                  "flags=%x", mr, e_mr->flags);
479                         ret = -EINVAL;
480                         goto rereg_phys_mr_exit0;
481                 }
482                 if (!phys_buf_array || num_phys_buf <= 0) {
483                         ehca_err(mr->device, "bad input values mr_rereg_mask=%x"
484                                  " phys_buf_array=%p num_phys_buf=%x",
485                                  mr_rereg_mask, phys_buf_array, num_phys_buf);
486                         ret = -EINVAL;
487                         goto rereg_phys_mr_exit0;
488                 }
489         }
490         if ((mr_rereg_mask & IB_MR_REREG_ACCESS) &&     /* change ACL */
491             (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
492               !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
493              ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
494               !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)))) {
495                 /*
496                  * Remote Write Access requires Local Write Access
497                  * Remote Atomic Access requires Local Write Access
498                  */
499                 ehca_err(mr->device, "bad input values: mr_rereg_mask=%x "
500                          "mr_access_flags=%x", mr_rereg_mask, mr_access_flags);
501                 ret = -EINVAL;
502                 goto rereg_phys_mr_exit0;
503         }
504
505         /* set requested values dependent on rereg request */
506         spin_lock_irqsave(&e_mr->mrlock, sl_flags);
507         new_start = e_mr->start;
508         new_size = e_mr->size;
509         new_acl = e_mr->acl;
510         new_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
511
512         if (mr_rereg_mask & IB_MR_REREG_TRANS) {
513                 u64 hw_pgsize = ehca_get_max_hwpage_size(shca);
514
515                 new_start = iova_start; /* change address */
516                 /* check physical buffer list and calculate size */
517                 ret = ehca_mr_chk_buf_and_calc_size(phys_buf_array,
518                                                     num_phys_buf, iova_start,
519                                                     &new_size);
520                 if (ret)
521                         goto rereg_phys_mr_exit1;
522                 if ((new_size == 0) ||
523                     (((u64)iova_start + new_size) < (u64)iova_start)) {
524                         ehca_err(mr->device, "bad input values: new_size=%lx "
525                                  "iova_start=%p", new_size, iova_start);
526                         ret = -EINVAL;
527                         goto rereg_phys_mr_exit1;
528                 }
529                 num_kpages = NUM_CHUNKS(((u64)new_start % PAGE_SIZE) +
530                                         new_size, PAGE_SIZE);
531                 num_hwpages = NUM_CHUNKS(((u64)new_start % hw_pgsize) +
532                                          new_size, hw_pgsize);
533                 memset(&pginfo, 0, sizeof(pginfo));
534                 pginfo.type = EHCA_MR_PGI_PHYS;
535                 pginfo.num_kpages = num_kpages;
536                 pginfo.hwpage_size = hw_pgsize;
537                 pginfo.num_hwpages = num_hwpages;
538                 pginfo.u.phy.num_phys_buf = num_phys_buf;
539                 pginfo.u.phy.phys_buf_array = phys_buf_array;
540                 pginfo.next_hwpage =
541                         ((u64)iova_start & ~PAGE_MASK) / hw_pgsize;
542         }
543         if (mr_rereg_mask & IB_MR_REREG_ACCESS)
544                 new_acl = mr_access_flags;
545         if (mr_rereg_mask & IB_MR_REREG_PD)
546                 new_pd = container_of(pd, struct ehca_pd, ib_pd);
547
548         ret = ehca_rereg_mr(shca, e_mr, new_start, new_size, new_acl,
549                             new_pd, &pginfo, &tmp_lkey, &tmp_rkey);
550         if (ret)
551                 goto rereg_phys_mr_exit1;
552
553         /* successful reregistration */
554         if (mr_rereg_mask & IB_MR_REREG_PD)
555                 mr->pd = pd;
556         mr->lkey = tmp_lkey;
557         mr->rkey = tmp_rkey;
558
559 rereg_phys_mr_exit1:
560         spin_unlock_irqrestore(&e_mr->mrlock, sl_flags);
561 rereg_phys_mr_exit0:
562         if (ret)
563                 ehca_err(mr->device, "ret=%i mr=%p mr_rereg_mask=%x pd=%p "
564                          "phys_buf_array=%p num_phys_buf=%x mr_access_flags=%x "
565                          "iova_start=%p",
566                          ret, mr, mr_rereg_mask, pd, phys_buf_array,
567                          num_phys_buf, mr_access_flags, iova_start);
568         return ret;
569 } /* end ehca_rereg_phys_mr() */
570
571 /*----------------------------------------------------------------------*/
572
573 int ehca_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
574 {
575         int ret = 0;
576         u64 h_ret;
577         struct ehca_shca *shca =
578                 container_of(mr->device, struct ehca_shca, ib_device);
579         struct ehca_mr *e_mr = container_of(mr, struct ehca_mr, ib.ib_mr);
580         struct ehca_pd *my_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
581         u32 cur_pid = current->tgid;
582         unsigned long sl_flags;
583         struct ehca_mr_hipzout_parms hipzout;
584
585         if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
586             (my_pd->ownpid != cur_pid)) {
587                 ehca_err(mr->device, "Invalid caller pid=%x ownpid=%x",
588                          cur_pid, my_pd->ownpid);
589                 ret = -EINVAL;
590                 goto query_mr_exit0;
591         }
592
593         if ((e_mr->flags & EHCA_MR_FLAG_FMR)) {
594                 ehca_err(mr->device, "not supported for FMR, mr=%p e_mr=%p "
595                          "e_mr->flags=%x", mr, e_mr, e_mr->flags);
596                 ret = -EINVAL;
597                 goto query_mr_exit0;
598         }
599
600         memset(mr_attr, 0, sizeof(struct ib_mr_attr));
601         spin_lock_irqsave(&e_mr->mrlock, sl_flags);
602
603         h_ret = hipz_h_query_mr(shca->ipz_hca_handle, e_mr, &hipzout);
604         if (h_ret != H_SUCCESS) {
605                 ehca_err(mr->device, "hipz_mr_query failed, h_ret=%li mr=%p "
606                          "hca_hndl=%lx mr_hndl=%lx lkey=%x",
607                          h_ret, mr, shca->ipz_hca_handle.handle,
608                          e_mr->ipz_mr_handle.handle, mr->lkey);
609                 ret = ehca2ib_return_code(h_ret);
610                 goto query_mr_exit1;
611         }
612         mr_attr->pd = mr->pd;
613         mr_attr->device_virt_addr = hipzout.vaddr;
614         mr_attr->size = hipzout.len;
615         mr_attr->lkey = hipzout.lkey;
616         mr_attr->rkey = hipzout.rkey;
617         ehca_mrmw_reverse_map_acl(&hipzout.acl, &mr_attr->mr_access_flags);
618
619 query_mr_exit1:
620         spin_unlock_irqrestore(&e_mr->mrlock, sl_flags);
621 query_mr_exit0:
622         if (ret)
623                 ehca_err(mr->device, "ret=%i mr=%p mr_attr=%p",
624                          ret, mr, mr_attr);
625         return ret;
626 } /* end ehca_query_mr() */
627
628 /*----------------------------------------------------------------------*/
629
630 int ehca_dereg_mr(struct ib_mr *mr)
631 {
632         int ret = 0;
633         u64 h_ret;
634         struct ehca_shca *shca =
635                 container_of(mr->device, struct ehca_shca, ib_device);
636         struct ehca_mr *e_mr = container_of(mr, struct ehca_mr, ib.ib_mr);
637         struct ehca_pd *my_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
638         u32 cur_pid = current->tgid;
639
640         if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
641             (my_pd->ownpid != cur_pid)) {
642                 ehca_err(mr->device, "Invalid caller pid=%x ownpid=%x",
643                          cur_pid, my_pd->ownpid);
644                 ret = -EINVAL;
645                 goto dereg_mr_exit0;
646         }
647
648         if ((e_mr->flags & EHCA_MR_FLAG_FMR)) {
649                 ehca_err(mr->device, "not supported for FMR, mr=%p e_mr=%p "
650                          "e_mr->flags=%x", mr, e_mr, e_mr->flags);
651                 ret = -EINVAL;
652                 goto dereg_mr_exit0;
653         } else if (e_mr == shca->maxmr) {
654                 /* should be impossible, however reject to be sure */
655                 ehca_err(mr->device, "dereg internal max-MR impossible, mr=%p "
656                          "shca->maxmr=%p mr->lkey=%x",
657                          mr, shca->maxmr, mr->lkey);
658                 ret = -EINVAL;
659                 goto dereg_mr_exit0;
660         }
661
662         /* TODO: BUSY: MR still has bound window(s) */
663         h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_mr);
664         if (h_ret != H_SUCCESS) {
665                 ehca_err(mr->device, "hipz_free_mr failed, h_ret=%li shca=%p "
666                          "e_mr=%p hca_hndl=%lx mr_hndl=%lx mr->lkey=%x",
667                          h_ret, shca, e_mr, shca->ipz_hca_handle.handle,
668                          e_mr->ipz_mr_handle.handle, mr->lkey);
669                 ret = ehca2ib_return_code(h_ret);
670                 goto dereg_mr_exit0;
671         }
672
673         if (e_mr->umem)
674                 ib_umem_release(e_mr->umem);
675
676         /* successful deregistration */
677         ehca_mr_delete(e_mr);
678
679 dereg_mr_exit0:
680         if (ret)
681                 ehca_err(mr->device, "ret=%i mr=%p", ret, mr);
682         return ret;
683 } /* end ehca_dereg_mr() */
684
685 /*----------------------------------------------------------------------*/
686
687 struct ib_mw *ehca_alloc_mw(struct ib_pd *pd)
688 {
689         struct ib_mw *ib_mw;
690         u64 h_ret;
691         struct ehca_mw *e_mw;
692         struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
693         struct ehca_shca *shca =
694                 container_of(pd->device, struct ehca_shca, ib_device);
695         struct ehca_mw_hipzout_parms hipzout;
696
697         e_mw = ehca_mw_new();
698         if (!e_mw) {
699                 ib_mw = ERR_PTR(-ENOMEM);
700                 goto alloc_mw_exit0;
701         }
702
703         h_ret = hipz_h_alloc_resource_mw(shca->ipz_hca_handle, e_mw,
704                                          e_pd->fw_pd, &hipzout);
705         if (h_ret != H_SUCCESS) {
706                 ehca_err(pd->device, "hipz_mw_allocate failed, h_ret=%li "
707                          "shca=%p hca_hndl=%lx mw=%p",
708                          h_ret, shca, shca->ipz_hca_handle.handle, e_mw);
709                 ib_mw = ERR_PTR(ehca2ib_return_code(h_ret));
710                 goto alloc_mw_exit1;
711         }
712         /* successful MW allocation */
713         e_mw->ipz_mw_handle = hipzout.handle;
714         e_mw->ib_mw.rkey    = hipzout.rkey;
715         return &e_mw->ib_mw;
716
717 alloc_mw_exit1:
718         ehca_mw_delete(e_mw);
719 alloc_mw_exit0:
720         if (IS_ERR(ib_mw))
721                 ehca_err(pd->device, "h_ret=%li pd=%p", PTR_ERR(ib_mw), pd);
722         return ib_mw;
723 } /* end ehca_alloc_mw() */
724
725 /*----------------------------------------------------------------------*/
726
727 int ehca_bind_mw(struct ib_qp *qp,
728                  struct ib_mw *mw,
729                  struct ib_mw_bind *mw_bind)
730 {
731         /* TODO: not supported up to now */
732         ehca_gen_err("bind MW currently not supported by HCAD");
733
734         return -EPERM;
735 } /* end ehca_bind_mw() */
736
737 /*----------------------------------------------------------------------*/
738
739 int ehca_dealloc_mw(struct ib_mw *mw)
740 {
741         u64 h_ret;
742         struct ehca_shca *shca =
743                 container_of(mw->device, struct ehca_shca, ib_device);
744         struct ehca_mw *e_mw = container_of(mw, struct ehca_mw, ib_mw);
745
746         h_ret = hipz_h_free_resource_mw(shca->ipz_hca_handle, e_mw);
747         if (h_ret != H_SUCCESS) {
748                 ehca_err(mw->device, "hipz_free_mw failed, h_ret=%li shca=%p "
749                          "mw=%p rkey=%x hca_hndl=%lx mw_hndl=%lx",
750                          h_ret, shca, mw, mw->rkey, shca->ipz_hca_handle.handle,
751                          e_mw->ipz_mw_handle.handle);
752                 return ehca2ib_return_code(h_ret);
753         }
754         /* successful deallocation */
755         ehca_mw_delete(e_mw);
756         return 0;
757 } /* end ehca_dealloc_mw() */
758
759 /*----------------------------------------------------------------------*/
760
761 struct ib_fmr *ehca_alloc_fmr(struct ib_pd *pd,
762                               int mr_access_flags,
763                               struct ib_fmr_attr *fmr_attr)
764 {
765         struct ib_fmr *ib_fmr;
766         struct ehca_shca *shca =
767                 container_of(pd->device, struct ehca_shca, ib_device);
768         struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
769         struct ehca_mr *e_fmr;
770         int ret;
771         u32 tmp_lkey, tmp_rkey;
772         struct ehca_mr_pginfo pginfo;
773         u64 hw_pgsize;
774
775         /* check other parameters */
776         if (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
777              !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
778             ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
779              !(mr_access_flags & IB_ACCESS_LOCAL_WRITE))) {
780                 /*
781                  * Remote Write Access requires Local Write Access
782                  * Remote Atomic Access requires Local Write Access
783                  */
784                 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
785                          mr_access_flags);
786                 ib_fmr = ERR_PTR(-EINVAL);
787                 goto alloc_fmr_exit0;
788         }
789         if (mr_access_flags & IB_ACCESS_MW_BIND) {
790                 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
791                          mr_access_flags);
792                 ib_fmr = ERR_PTR(-EINVAL);
793                 goto alloc_fmr_exit0;
794         }
795         if ((fmr_attr->max_pages == 0) || (fmr_attr->max_maps == 0)) {
796                 ehca_err(pd->device, "bad input values: fmr_attr->max_pages=%x "
797                          "fmr_attr->max_maps=%x fmr_attr->page_shift=%x",
798                          fmr_attr->max_pages, fmr_attr->max_maps,
799                          fmr_attr->page_shift);
800                 ib_fmr = ERR_PTR(-EINVAL);
801                 goto alloc_fmr_exit0;
802         }
803
804         hw_pgsize = 1 << fmr_attr->page_shift;
805         if (!(hw_pgsize & shca->hca_cap_mr_pgsize)) {
806                 ehca_err(pd->device, "unsupported fmr_attr->page_shift=%x",
807                          fmr_attr->page_shift);
808                 ib_fmr = ERR_PTR(-EINVAL);
809                 goto alloc_fmr_exit0;
810         }
811
812         e_fmr = ehca_mr_new();
813         if (!e_fmr) {
814                 ib_fmr = ERR_PTR(-ENOMEM);
815                 goto alloc_fmr_exit0;
816         }
817         e_fmr->flags |= EHCA_MR_FLAG_FMR;
818
819         /* register MR on HCA */
820         memset(&pginfo, 0, sizeof(pginfo));
821         pginfo.hwpage_size = hw_pgsize;
822         /*
823          * pginfo.num_hwpages==0, ie register_rpages() will not be called
824          * but deferred to map_phys_fmr()
825          */
826         ret = ehca_reg_mr(shca, e_fmr, NULL,
827                           fmr_attr->max_pages * (1 << fmr_attr->page_shift),
828                           mr_access_flags, e_pd, &pginfo,
829                           &tmp_lkey, &tmp_rkey);
830         if (ret) {
831                 ib_fmr = ERR_PTR(ret);
832                 goto alloc_fmr_exit1;
833         }
834
835         /* successful */
836         e_fmr->hwpage_size = hw_pgsize;
837         e_fmr->fmr_page_size = 1 << fmr_attr->page_shift;
838         e_fmr->fmr_max_pages = fmr_attr->max_pages;
839         e_fmr->fmr_max_maps = fmr_attr->max_maps;
840         e_fmr->fmr_map_cnt = 0;
841         return &e_fmr->ib.ib_fmr;
842
843 alloc_fmr_exit1:
844         ehca_mr_delete(e_fmr);
845 alloc_fmr_exit0:
846         return ib_fmr;
847 } /* end ehca_alloc_fmr() */
848
849 /*----------------------------------------------------------------------*/
850
851 int ehca_map_phys_fmr(struct ib_fmr *fmr,
852                       u64 *page_list,
853                       int list_len,
854                       u64 iova)
855 {
856         int ret;
857         struct ehca_shca *shca =
858                 container_of(fmr->device, struct ehca_shca, ib_device);
859         struct ehca_mr *e_fmr = container_of(fmr, struct ehca_mr, ib.ib_fmr);
860         struct ehca_pd *e_pd = container_of(fmr->pd, struct ehca_pd, ib_pd);
861         struct ehca_mr_pginfo pginfo;
862         u32 tmp_lkey, tmp_rkey;
863
864         if (!(e_fmr->flags & EHCA_MR_FLAG_FMR)) {
865                 ehca_err(fmr->device, "not a FMR, e_fmr=%p e_fmr->flags=%x",
866                          e_fmr, e_fmr->flags);
867                 ret = -EINVAL;
868                 goto map_phys_fmr_exit0;
869         }
870         ret = ehca_fmr_check_page_list(e_fmr, page_list, list_len);
871         if (ret)
872                 goto map_phys_fmr_exit0;
873         if (iova % e_fmr->fmr_page_size) {
874                 /* only whole-numbered pages */
875                 ehca_err(fmr->device, "bad iova, iova=%lx fmr_page_size=%x",
876                          iova, e_fmr->fmr_page_size);
877                 ret = -EINVAL;
878                 goto map_phys_fmr_exit0;
879         }
880         if (e_fmr->fmr_map_cnt >= e_fmr->fmr_max_maps) {
881                 /* HCAD does not limit the maps, however trace this anyway */
882                 ehca_info(fmr->device, "map limit exceeded, fmr=%p "
883                           "e_fmr->fmr_map_cnt=%x e_fmr->fmr_max_maps=%x",
884                           fmr, e_fmr->fmr_map_cnt, e_fmr->fmr_max_maps);
885         }
886
887         memset(&pginfo, 0, sizeof(pginfo));
888         pginfo.type = EHCA_MR_PGI_FMR;
889         pginfo.num_kpages = list_len;
890         pginfo.hwpage_size = e_fmr->hwpage_size;
891         pginfo.num_hwpages =
892                 list_len * e_fmr->fmr_page_size / pginfo.hwpage_size;
893         pginfo.u.fmr.page_list = page_list;
894         pginfo.next_hwpage =
895                 (iova & (e_fmr->fmr_page_size-1)) / pginfo.hwpage_size;
896         pginfo.u.fmr.fmr_pgsize = e_fmr->fmr_page_size;
897
898         ret = ehca_rereg_mr(shca, e_fmr, (u64 *)iova,
899                             list_len * e_fmr->fmr_page_size,
900                             e_fmr->acl, e_pd, &pginfo, &tmp_lkey, &tmp_rkey);
901         if (ret)
902                 goto map_phys_fmr_exit0;
903
904         /* successful reregistration */
905         e_fmr->fmr_map_cnt++;
906         e_fmr->ib.ib_fmr.lkey = tmp_lkey;
907         e_fmr->ib.ib_fmr.rkey = tmp_rkey;
908         return 0;
909
910 map_phys_fmr_exit0:
911         if (ret)
912                 ehca_err(fmr->device, "ret=%i fmr=%p page_list=%p list_len=%x "
913                          "iova=%lx", ret, fmr, page_list, list_len, iova);
914         return ret;
915 } /* end ehca_map_phys_fmr() */
916
917 /*----------------------------------------------------------------------*/
918
919 int ehca_unmap_fmr(struct list_head *fmr_list)
920 {
921         int ret = 0;
922         struct ib_fmr *ib_fmr;
923         struct ehca_shca *shca = NULL;
924         struct ehca_shca *prev_shca;
925         struct ehca_mr *e_fmr;
926         u32 num_fmr = 0;
927         u32 unmap_fmr_cnt = 0;
928
929         /* check all FMR belong to same SHCA, and check internal flag */
930         list_for_each_entry(ib_fmr, fmr_list, list) {
931                 prev_shca = shca;
932                 if (!ib_fmr) {
933                         ehca_gen_err("bad fmr=%p in list", ib_fmr);
934                         ret = -EINVAL;
935                         goto unmap_fmr_exit0;
936                 }
937                 shca = container_of(ib_fmr->device, struct ehca_shca,
938                                     ib_device);
939                 e_fmr = container_of(ib_fmr, struct ehca_mr, ib.ib_fmr);
940                 if ((shca != prev_shca) && prev_shca) {
941                         ehca_err(&shca->ib_device, "SHCA mismatch, shca=%p "
942                                  "prev_shca=%p e_fmr=%p",
943                                  shca, prev_shca, e_fmr);
944                         ret = -EINVAL;
945                         goto unmap_fmr_exit0;
946                 }
947                 if (!(e_fmr->flags & EHCA_MR_FLAG_FMR)) {
948                         ehca_err(&shca->ib_device, "not a FMR, e_fmr=%p "
949                                  "e_fmr->flags=%x", e_fmr, e_fmr->flags);
950                         ret = -EINVAL;
951                         goto unmap_fmr_exit0;
952                 }
953                 num_fmr++;
954         }
955
956         /* loop over all FMRs to unmap */
957         list_for_each_entry(ib_fmr, fmr_list, list) {
958                 unmap_fmr_cnt++;
959                 e_fmr = container_of(ib_fmr, struct ehca_mr, ib.ib_fmr);
960                 shca = container_of(ib_fmr->device, struct ehca_shca,
961                                     ib_device);
962                 ret = ehca_unmap_one_fmr(shca, e_fmr);
963                 if (ret) {
964                         /* unmap failed, stop unmapping of rest of FMRs */
965                         ehca_err(&shca->ib_device, "unmap of one FMR failed, "
966                                  "stop rest, e_fmr=%p num_fmr=%x "
967                                  "unmap_fmr_cnt=%x lkey=%x", e_fmr, num_fmr,
968                                  unmap_fmr_cnt, e_fmr->ib.ib_fmr.lkey);
969                         goto unmap_fmr_exit0;
970                 }
971         }
972
973 unmap_fmr_exit0:
974         if (ret)
975                 ehca_gen_err("ret=%i fmr_list=%p num_fmr=%x unmap_fmr_cnt=%x",
976                              ret, fmr_list, num_fmr, unmap_fmr_cnt);
977         return ret;
978 } /* end ehca_unmap_fmr() */
979
980 /*----------------------------------------------------------------------*/
981
982 int ehca_dealloc_fmr(struct ib_fmr *fmr)
983 {
984         int ret;
985         u64 h_ret;
986         struct ehca_shca *shca =
987                 container_of(fmr->device, struct ehca_shca, ib_device);
988         struct ehca_mr *e_fmr = container_of(fmr, struct ehca_mr, ib.ib_fmr);
989
990         if (!(e_fmr->flags & EHCA_MR_FLAG_FMR)) {
991                 ehca_err(fmr->device, "not a FMR, e_fmr=%p e_fmr->flags=%x",
992                          e_fmr, e_fmr->flags);
993                 ret = -EINVAL;
994                 goto free_fmr_exit0;
995         }
996
997         h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_fmr);
998         if (h_ret != H_SUCCESS) {
999                 ehca_err(fmr->device, "hipz_free_mr failed, h_ret=%li e_fmr=%p "
1000                          "hca_hndl=%lx fmr_hndl=%lx fmr->lkey=%x",
1001                          h_ret, e_fmr, shca->ipz_hca_handle.handle,
1002                          e_fmr->ipz_mr_handle.handle, fmr->lkey);
1003                 ret = ehca2ib_return_code(h_ret);
1004                 goto free_fmr_exit0;
1005         }
1006         /* successful deregistration */
1007         ehca_mr_delete(e_fmr);
1008         return 0;
1009
1010 free_fmr_exit0:
1011         if (ret)
1012                 ehca_err(&shca->ib_device, "ret=%i fmr=%p", ret, fmr);
1013         return ret;
1014 } /* end ehca_dealloc_fmr() */
1015
1016 /*----------------------------------------------------------------------*/
1017
1018 int ehca_reg_mr(struct ehca_shca *shca,
1019                 struct ehca_mr *e_mr,
1020                 u64 *iova_start,
1021                 u64 size,
1022                 int acl,
1023                 struct ehca_pd *e_pd,
1024                 struct ehca_mr_pginfo *pginfo,
1025                 u32 *lkey, /*OUT*/
1026                 u32 *rkey) /*OUT*/
1027 {
1028         int ret;
1029         u64 h_ret;
1030         u32 hipz_acl;
1031         struct ehca_mr_hipzout_parms hipzout;
1032
1033         ehca_mrmw_map_acl(acl, &hipz_acl);
1034         ehca_mrmw_set_pgsize_hipz_acl(pginfo->hwpage_size, &hipz_acl);
1035         if (ehca_use_hp_mr == 1)
1036                 hipz_acl |= 0x00000001;
1037
1038         h_ret = hipz_h_alloc_resource_mr(shca->ipz_hca_handle, e_mr,
1039                                          (u64)iova_start, size, hipz_acl,
1040                                          e_pd->fw_pd, &hipzout);
1041         if (h_ret != H_SUCCESS) {
1042                 ehca_err(&shca->ib_device, "hipz_alloc_mr failed, h_ret=%li "
1043                          "hca_hndl=%lx", h_ret, shca->ipz_hca_handle.handle);
1044                 ret = ehca2ib_return_code(h_ret);
1045                 goto ehca_reg_mr_exit0;
1046         }
1047
1048         e_mr->ipz_mr_handle = hipzout.handle;
1049
1050         ret = ehca_reg_mr_rpages(shca, e_mr, pginfo);
1051         if (ret)
1052                 goto ehca_reg_mr_exit1;
1053
1054         /* successful registration */
1055         e_mr->num_kpages = pginfo->num_kpages;
1056         e_mr->num_hwpages = pginfo->num_hwpages;
1057         e_mr->hwpage_size = pginfo->hwpage_size;
1058         e_mr->start = iova_start;
1059         e_mr->size = size;
1060         e_mr->acl = acl;
1061         *lkey = hipzout.lkey;
1062         *rkey = hipzout.rkey;
1063         return 0;
1064
1065 ehca_reg_mr_exit1:
1066         h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_mr);
1067         if (h_ret != H_SUCCESS) {
1068                 ehca_err(&shca->ib_device, "h_ret=%li shca=%p e_mr=%p "
1069                          "iova_start=%p size=%lx acl=%x e_pd=%p lkey=%x "
1070                          "pginfo=%p num_kpages=%lx num_hwpages=%lx ret=%i",
1071                          h_ret, shca, e_mr, iova_start, size, acl, e_pd,
1072                          hipzout.lkey, pginfo, pginfo->num_kpages,
1073                          pginfo->num_hwpages, ret);
1074                 ehca_err(&shca->ib_device, "internal error in ehca_reg_mr, "
1075                          "not recoverable");
1076         }
1077 ehca_reg_mr_exit0:
1078         if (ret)
1079                 ehca_err(&shca->ib_device, "ret=%i shca=%p e_mr=%p "
1080                          "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
1081                          "num_kpages=%lx num_hwpages=%lx",
1082                          ret, shca, e_mr, iova_start, size, acl, e_pd, pginfo,
1083                          pginfo->num_kpages, pginfo->num_hwpages);
1084         return ret;
1085 } /* end ehca_reg_mr() */
1086
1087 /*----------------------------------------------------------------------*/
1088
1089 int ehca_reg_mr_rpages(struct ehca_shca *shca,
1090                        struct ehca_mr *e_mr,
1091                        struct ehca_mr_pginfo *pginfo)
1092 {
1093         int ret = 0;
1094         u64 h_ret;
1095         u32 rnum;
1096         u64 rpage;
1097         u32 i;
1098         u64 *kpage;
1099
1100         if (!pginfo->num_hwpages) /* in case of fmr */
1101                 return 0;
1102
1103         kpage = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
1104         if (!kpage) {
1105                 ehca_err(&shca->ib_device, "kpage alloc failed");
1106                 ret = -ENOMEM;
1107                 goto ehca_reg_mr_rpages_exit0;
1108         }
1109
1110         /* max MAX_RPAGES ehca mr pages per register call */
1111         for (i = 0; i < NUM_CHUNKS(pginfo->num_hwpages, MAX_RPAGES); i++) {
1112
1113                 if (i == NUM_CHUNKS(pginfo->num_hwpages, MAX_RPAGES) - 1) {
1114                         rnum = pginfo->num_hwpages % MAX_RPAGES; /* last shot */
1115                         if (rnum == 0)
1116                                 rnum = MAX_RPAGES;      /* last shot is full */
1117                 } else
1118                         rnum = MAX_RPAGES;
1119
1120                 ret = ehca_set_pagebuf(pginfo, rnum, kpage);
1121                 if (ret) {
1122                         ehca_err(&shca->ib_device, "ehca_set_pagebuf "
1123                                  "bad rc, ret=%i rnum=%x kpage=%p",
1124                                  ret, rnum, kpage);
1125                         goto ehca_reg_mr_rpages_exit1;
1126                 }
1127
1128                 if (rnum > 1) {
1129                         rpage = virt_to_abs(kpage);
1130                         if (!rpage) {
1131                                 ehca_err(&shca->ib_device, "kpage=%p i=%x",
1132                                          kpage, i);
1133                                 ret = -EFAULT;
1134                                 goto ehca_reg_mr_rpages_exit1;
1135                         }
1136                 } else
1137                         rpage = *kpage;
1138
1139                 h_ret = hipz_h_register_rpage_mr(
1140                         shca->ipz_hca_handle, e_mr,
1141                         ehca_encode_hwpage_size(pginfo->hwpage_size),
1142                         0, rpage, rnum);
1143
1144                 if (i == NUM_CHUNKS(pginfo->num_hwpages, MAX_RPAGES) - 1) {
1145                         /*
1146                          * check for 'registration complete'==H_SUCCESS
1147                          * and for 'page registered'==H_PAGE_REGISTERED
1148                          */
1149                         if (h_ret != H_SUCCESS) {
1150                                 ehca_err(&shca->ib_device, "last "
1151                                          "hipz_reg_rpage_mr failed, h_ret=%li "
1152                                          "e_mr=%p i=%x hca_hndl=%lx mr_hndl=%lx"
1153                                          " lkey=%x", h_ret, e_mr, i,
1154                                          shca->ipz_hca_handle.handle,
1155                                          e_mr->ipz_mr_handle.handle,
1156                                          e_mr->ib.ib_mr.lkey);
1157                                 ret = ehca2ib_return_code(h_ret);
1158                                 break;
1159                         } else
1160                                 ret = 0;
1161                 } else if (h_ret != H_PAGE_REGISTERED) {
1162                         ehca_err(&shca->ib_device, "hipz_reg_rpage_mr failed, "
1163                                  "h_ret=%li e_mr=%p i=%x lkey=%x hca_hndl=%lx "
1164                                  "mr_hndl=%lx", h_ret, e_mr, i,
1165                                  e_mr->ib.ib_mr.lkey,
1166                                  shca->ipz_hca_handle.handle,
1167                                  e_mr->ipz_mr_handle.handle);
1168                         ret = ehca2ib_return_code(h_ret);
1169                         break;
1170                 } else
1171                         ret = 0;
1172         } /* end for(i) */
1173
1174
1175 ehca_reg_mr_rpages_exit1:
1176         ehca_free_fw_ctrlblock(kpage);
1177 ehca_reg_mr_rpages_exit0:
1178         if (ret)
1179                 ehca_err(&shca->ib_device, "ret=%i shca=%p e_mr=%p pginfo=%p "
1180                          "num_kpages=%lx num_hwpages=%lx", ret, shca, e_mr,
1181                          pginfo, pginfo->num_kpages, pginfo->num_hwpages);
1182         return ret;
1183 } /* end ehca_reg_mr_rpages() */
1184
1185 /*----------------------------------------------------------------------*/
1186
1187 inline int ehca_rereg_mr_rereg1(struct ehca_shca *shca,
1188                                 struct ehca_mr *e_mr,
1189                                 u64 *iova_start,
1190                                 u64 size,
1191                                 u32 acl,
1192                                 struct ehca_pd *e_pd,
1193                                 struct ehca_mr_pginfo *pginfo,
1194                                 u32 *lkey, /*OUT*/
1195                                 u32 *rkey) /*OUT*/
1196 {
1197         int ret;
1198         u64 h_ret;
1199         u32 hipz_acl;
1200         u64 *kpage;
1201         u64 rpage;
1202         struct ehca_mr_pginfo pginfo_save;
1203         struct ehca_mr_hipzout_parms hipzout;
1204
1205         ehca_mrmw_map_acl(acl, &hipz_acl);
1206         ehca_mrmw_set_pgsize_hipz_acl(pginfo->hwpage_size, &hipz_acl);
1207
1208         kpage = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
1209         if (!kpage) {
1210                 ehca_err(&shca->ib_device, "kpage alloc failed");
1211                 ret = -ENOMEM;
1212                 goto ehca_rereg_mr_rereg1_exit0;
1213         }
1214
1215         pginfo_save = *pginfo;
1216         ret = ehca_set_pagebuf(pginfo, pginfo->num_hwpages, kpage);
1217         if (ret) {
1218                 ehca_err(&shca->ib_device, "set pagebuf failed, e_mr=%p "
1219                          "pginfo=%p type=%x num_kpages=%lx num_hwpages=%lx "
1220                          "kpage=%p", e_mr, pginfo, pginfo->type,
1221                          pginfo->num_kpages, pginfo->num_hwpages, kpage);
1222                 goto ehca_rereg_mr_rereg1_exit1;
1223         }
1224         rpage = virt_to_abs(kpage);
1225         if (!rpage) {
1226                 ehca_err(&shca->ib_device, "kpage=%p", kpage);
1227                 ret = -EFAULT;
1228                 goto ehca_rereg_mr_rereg1_exit1;
1229         }
1230         h_ret = hipz_h_reregister_pmr(shca->ipz_hca_handle, e_mr,
1231                                       (u64)iova_start, size, hipz_acl,
1232                                       e_pd->fw_pd, rpage, &hipzout);
1233         if (h_ret != H_SUCCESS) {
1234                 /*
1235                  * reregistration unsuccessful, try it again with the 3 hCalls,
1236                  * e.g. this is required in case H_MR_CONDITION
1237                  * (MW bound or MR is shared)
1238                  */
1239                 ehca_warn(&shca->ib_device, "hipz_h_reregister_pmr failed "
1240                           "(Rereg1), h_ret=%li e_mr=%p", h_ret, e_mr);
1241                 *pginfo = pginfo_save;
1242                 ret = -EAGAIN;
1243         } else if ((u64 *)hipzout.vaddr != iova_start) {
1244                 ehca_err(&shca->ib_device, "PHYP changed iova_start in "
1245                          "rereg_pmr, iova_start=%p iova_start_out=%lx e_mr=%p "
1246                          "mr_handle=%lx lkey=%x lkey_out=%x", iova_start,
1247                          hipzout.vaddr, e_mr, e_mr->ipz_mr_handle.handle,
1248                          e_mr->ib.ib_mr.lkey, hipzout.lkey);
1249                 ret = -EFAULT;
1250         } else {
1251                 /*
1252                  * successful reregistration
1253                  * note: start and start_out are identical for eServer HCAs
1254                  */
1255                 e_mr->num_kpages = pginfo->num_kpages;
1256                 e_mr->num_hwpages = pginfo->num_hwpages;
1257                 e_mr->hwpage_size = pginfo->hwpage_size;
1258                 e_mr->start = iova_start;
1259                 e_mr->size = size;
1260                 e_mr->acl = acl;
1261                 *lkey = hipzout.lkey;
1262                 *rkey = hipzout.rkey;
1263         }
1264
1265 ehca_rereg_mr_rereg1_exit1:
1266         ehca_free_fw_ctrlblock(kpage);
1267 ehca_rereg_mr_rereg1_exit0:
1268         if ( ret && (ret != -EAGAIN) )
1269                 ehca_err(&shca->ib_device, "ret=%i lkey=%x rkey=%x "
1270                          "pginfo=%p num_kpages=%lx num_hwpages=%lx",
1271                          ret, *lkey, *rkey, pginfo, pginfo->num_kpages,
1272                          pginfo->num_hwpages);
1273         return ret;
1274 } /* end ehca_rereg_mr_rereg1() */
1275
1276 /*----------------------------------------------------------------------*/
1277
1278 int ehca_rereg_mr(struct ehca_shca *shca,
1279                   struct ehca_mr *e_mr,
1280                   u64 *iova_start,
1281                   u64 size,
1282                   int acl,
1283                   struct ehca_pd *e_pd,
1284                   struct ehca_mr_pginfo *pginfo,
1285                   u32 *lkey,
1286                   u32 *rkey)
1287 {
1288         int ret = 0;
1289         u64 h_ret;
1290         int rereg_1_hcall = 1; /* 1: use hipz_h_reregister_pmr directly */
1291         int rereg_3_hcall = 0; /* 1: use 3 hipz calls for reregistration */
1292
1293         /* first determine reregistration hCall(s) */
1294         if ((pginfo->num_hwpages > MAX_RPAGES) ||
1295             (e_mr->num_hwpages > MAX_RPAGES) ||
1296             (pginfo->num_hwpages > e_mr->num_hwpages)) {
1297                 ehca_dbg(&shca->ib_device, "Rereg3 case, "
1298                          "pginfo->num_hwpages=%lx e_mr->num_hwpages=%x",
1299                          pginfo->num_hwpages, e_mr->num_hwpages);
1300                 rereg_1_hcall = 0;
1301                 rereg_3_hcall = 1;
1302         }
1303
1304         if (e_mr->flags & EHCA_MR_FLAG_MAXMR) { /* check for max-MR */
1305                 rereg_1_hcall = 0;
1306                 rereg_3_hcall = 1;
1307                 e_mr->flags &= ~EHCA_MR_FLAG_MAXMR;
1308                 ehca_err(&shca->ib_device, "Rereg MR for max-MR! e_mr=%p",
1309                          e_mr);
1310         }
1311
1312         if (rereg_1_hcall) {
1313                 ret = ehca_rereg_mr_rereg1(shca, e_mr, iova_start, size,
1314                                            acl, e_pd, pginfo, lkey, rkey);
1315                 if (ret) {
1316                         if (ret == -EAGAIN)
1317                                 rereg_3_hcall = 1;
1318                         else
1319                                 goto ehca_rereg_mr_exit0;
1320                 }
1321         }
1322
1323         if (rereg_3_hcall) {
1324                 struct ehca_mr save_mr;
1325
1326                 /* first deregister old MR */
1327                 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_mr);
1328                 if (h_ret != H_SUCCESS) {
1329                         ehca_err(&shca->ib_device, "hipz_free_mr failed, "
1330                                  "h_ret=%li e_mr=%p hca_hndl=%lx mr_hndl=%lx "
1331                                  "mr->lkey=%x",
1332                                  h_ret, e_mr, shca->ipz_hca_handle.handle,
1333                                  e_mr->ipz_mr_handle.handle,
1334                                  e_mr->ib.ib_mr.lkey);
1335                         ret = ehca2ib_return_code(h_ret);
1336                         goto ehca_rereg_mr_exit0;
1337                 }
1338                 /* clean ehca_mr_t, without changing struct ib_mr and lock */
1339                 save_mr = *e_mr;
1340                 ehca_mr_deletenew(e_mr);
1341
1342                 /* set some MR values */
1343                 e_mr->flags = save_mr.flags;
1344                 e_mr->hwpage_size = save_mr.hwpage_size;
1345                 e_mr->fmr_page_size = save_mr.fmr_page_size;
1346                 e_mr->fmr_max_pages = save_mr.fmr_max_pages;
1347                 e_mr->fmr_max_maps = save_mr.fmr_max_maps;
1348                 e_mr->fmr_map_cnt = save_mr.fmr_map_cnt;
1349
1350                 ret = ehca_reg_mr(shca, e_mr, iova_start, size, acl,
1351                                   e_pd, pginfo, lkey, rkey);
1352                 if (ret) {
1353                         u32 offset = (u64)(&e_mr->flags) - (u64)e_mr;
1354                         memcpy(&e_mr->flags, &(save_mr.flags),
1355                                sizeof(struct ehca_mr) - offset);
1356                         goto ehca_rereg_mr_exit0;
1357                 }
1358         }
1359
1360 ehca_rereg_mr_exit0:
1361         if (ret)
1362                 ehca_err(&shca->ib_device, "ret=%i shca=%p e_mr=%p "
1363                          "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
1364                          "num_kpages=%lx lkey=%x rkey=%x rereg_1_hcall=%x "
1365                          "rereg_3_hcall=%x", ret, shca, e_mr, iova_start, size,
1366                          acl, e_pd, pginfo, pginfo->num_kpages, *lkey, *rkey,
1367                          rereg_1_hcall, rereg_3_hcall);
1368         return ret;
1369 } /* end ehca_rereg_mr() */
1370
1371 /*----------------------------------------------------------------------*/
1372
1373 int ehca_unmap_one_fmr(struct ehca_shca *shca,
1374                        struct ehca_mr *e_fmr)
1375 {
1376         int ret = 0;
1377         u64 h_ret;
1378         struct ehca_pd *e_pd =
1379                 container_of(e_fmr->ib.ib_fmr.pd, struct ehca_pd, ib_pd);
1380         struct ehca_mr save_fmr;
1381         u32 tmp_lkey, tmp_rkey;
1382         struct ehca_mr_pginfo pginfo;
1383         struct ehca_mr_hipzout_parms hipzout;
1384         struct ehca_mr save_mr;
1385
1386         if (e_fmr->fmr_max_pages <= MAX_RPAGES) {
1387                 /*
1388                  * note: after using rereg hcall with len=0,
1389                  * rereg hcall must be used again for registering pages
1390                  */
1391                 h_ret = hipz_h_reregister_pmr(shca->ipz_hca_handle, e_fmr, 0,
1392                                               0, 0, e_pd->fw_pd, 0, &hipzout);
1393                 if (h_ret == H_SUCCESS) {
1394                         /* successful reregistration */
1395                         e_fmr->start = NULL;
1396                         e_fmr->size = 0;
1397                         tmp_lkey = hipzout.lkey;
1398                         tmp_rkey = hipzout.rkey;
1399                         return 0;
1400                 }
1401                 /*
1402                  * should not happen, because length checked above,
1403                  * FMRs are not shared and no MW bound to FMRs
1404                  */
1405                 ehca_err(&shca->ib_device, "hipz_reregister_pmr failed "
1406                          "(Rereg1), h_ret=%li e_fmr=%p hca_hndl=%lx "
1407                          "mr_hndl=%lx lkey=%x lkey_out=%x",
1408                          h_ret, e_fmr, shca->ipz_hca_handle.handle,
1409                          e_fmr->ipz_mr_handle.handle,
1410                          e_fmr->ib.ib_fmr.lkey, hipzout.lkey);
1411                 /* try free and rereg */
1412         }
1413
1414         /* first free old FMR */
1415         h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_fmr);
1416         if (h_ret != H_SUCCESS) {
1417                 ehca_err(&shca->ib_device, "hipz_free_mr failed, "
1418                          "h_ret=%li e_fmr=%p hca_hndl=%lx mr_hndl=%lx "
1419                          "lkey=%x",
1420                          h_ret, e_fmr, shca->ipz_hca_handle.handle,
1421                          e_fmr->ipz_mr_handle.handle,
1422                          e_fmr->ib.ib_fmr.lkey);
1423                 ret = ehca2ib_return_code(h_ret);
1424                 goto ehca_unmap_one_fmr_exit0;
1425         }
1426         /* clean ehca_mr_t, without changing lock */
1427         save_fmr = *e_fmr;
1428         ehca_mr_deletenew(e_fmr);
1429
1430         /* set some MR values */
1431         e_fmr->flags = save_fmr.flags;
1432         e_fmr->hwpage_size = save_fmr.hwpage_size;
1433         e_fmr->fmr_page_size = save_fmr.fmr_page_size;
1434         e_fmr->fmr_max_pages = save_fmr.fmr_max_pages;
1435         e_fmr->fmr_max_maps = save_fmr.fmr_max_maps;
1436         e_fmr->fmr_map_cnt = save_fmr.fmr_map_cnt;
1437         e_fmr->acl = save_fmr.acl;
1438
1439         memset(&pginfo, 0, sizeof(pginfo));
1440         pginfo.type = EHCA_MR_PGI_FMR;
1441         ret = ehca_reg_mr(shca, e_fmr, NULL,
1442                           (e_fmr->fmr_max_pages * e_fmr->fmr_page_size),
1443                           e_fmr->acl, e_pd, &pginfo, &tmp_lkey,
1444                           &tmp_rkey);
1445         if (ret) {
1446                 u32 offset = (u64)(&e_fmr->flags) - (u64)e_fmr;
1447                 memcpy(&e_fmr->flags, &(save_mr.flags),
1448                        sizeof(struct ehca_mr) - offset);
1449         }
1450
1451 ehca_unmap_one_fmr_exit0:
1452         if (ret)
1453                 ehca_err(&shca->ib_device, "ret=%i tmp_lkey=%x tmp_rkey=%x "
1454                          "fmr_max_pages=%x",
1455                          ret, tmp_lkey, tmp_rkey, e_fmr->fmr_max_pages);
1456         return ret;
1457 } /* end ehca_unmap_one_fmr() */
1458
1459 /*----------------------------------------------------------------------*/
1460
1461 int ehca_reg_smr(struct ehca_shca *shca,
1462                  struct ehca_mr *e_origmr,
1463                  struct ehca_mr *e_newmr,
1464                  u64 *iova_start,
1465                  int acl,
1466                  struct ehca_pd *e_pd,
1467                  u32 *lkey, /*OUT*/
1468                  u32 *rkey) /*OUT*/
1469 {
1470         int ret = 0;
1471         u64 h_ret;
1472         u32 hipz_acl;
1473         struct ehca_mr_hipzout_parms hipzout;
1474
1475         ehca_mrmw_map_acl(acl, &hipz_acl);
1476         ehca_mrmw_set_pgsize_hipz_acl(e_origmr->hwpage_size, &hipz_acl);
1477
1478         h_ret = hipz_h_register_smr(shca->ipz_hca_handle, e_newmr, e_origmr,
1479                                     (u64)iova_start, hipz_acl, e_pd->fw_pd,
1480                                     &hipzout);
1481         if (h_ret != H_SUCCESS) {
1482                 ehca_err(&shca->ib_device, "hipz_reg_smr failed, h_ret=%li "
1483                          "shca=%p e_origmr=%p e_newmr=%p iova_start=%p acl=%x "
1484                          "e_pd=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1485                          h_ret, shca, e_origmr, e_newmr, iova_start, acl, e_pd,
1486                          shca->ipz_hca_handle.handle,
1487                          e_origmr->ipz_mr_handle.handle,
1488                          e_origmr->ib.ib_mr.lkey);
1489                 ret = ehca2ib_return_code(h_ret);
1490                 goto ehca_reg_smr_exit0;
1491         }
1492         /* successful registration */
1493         e_newmr->num_kpages = e_origmr->num_kpages;
1494         e_newmr->num_hwpages = e_origmr->num_hwpages;
1495         e_newmr->hwpage_size   = e_origmr->hwpage_size;
1496         e_newmr->start = iova_start;
1497         e_newmr->size = e_origmr->size;
1498         e_newmr->acl = acl;
1499         e_newmr->ipz_mr_handle = hipzout.handle;
1500         *lkey = hipzout.lkey;
1501         *rkey = hipzout.rkey;
1502         return 0;
1503
1504 ehca_reg_smr_exit0:
1505         if (ret)
1506                 ehca_err(&shca->ib_device, "ret=%i shca=%p e_origmr=%p "
1507                          "e_newmr=%p iova_start=%p acl=%x e_pd=%p",
1508                          ret, shca, e_origmr, e_newmr, iova_start, acl, e_pd);
1509         return ret;
1510 } /* end ehca_reg_smr() */
1511
1512 /*----------------------------------------------------------------------*/
1513
1514 /* register internal max-MR to internal SHCA */
1515 int ehca_reg_internal_maxmr(
1516         struct ehca_shca *shca,
1517         struct ehca_pd *e_pd,
1518         struct ehca_mr **e_maxmr)  /*OUT*/
1519 {
1520         int ret;
1521         struct ehca_mr *e_mr;
1522         u64 *iova_start;
1523         u64 size_maxmr;
1524         struct ehca_mr_pginfo pginfo;
1525         struct ib_phys_buf ib_pbuf;
1526         u32 num_kpages;
1527         u32 num_hwpages;
1528         u64 hw_pgsize;
1529
1530         e_mr = ehca_mr_new();
1531         if (!e_mr) {
1532                 ehca_err(&shca->ib_device, "out of memory");
1533                 ret = -ENOMEM;
1534                 goto ehca_reg_internal_maxmr_exit0;
1535         }
1536         e_mr->flags |= EHCA_MR_FLAG_MAXMR;
1537
1538         /* register internal max-MR on HCA */
1539         size_maxmr = (u64)high_memory - PAGE_OFFSET;
1540         iova_start = (u64 *)KERNELBASE;
1541         ib_pbuf.addr = 0;
1542         ib_pbuf.size = size_maxmr;
1543         num_kpages = NUM_CHUNKS(((u64)iova_start % PAGE_SIZE) + size_maxmr,
1544                                 PAGE_SIZE);
1545         hw_pgsize = ehca_get_max_hwpage_size(shca);
1546         num_hwpages = NUM_CHUNKS(((u64)iova_start % hw_pgsize) + size_maxmr,
1547                                  hw_pgsize);
1548
1549         memset(&pginfo, 0, sizeof(pginfo));
1550         pginfo.type = EHCA_MR_PGI_PHYS;
1551         pginfo.num_kpages = num_kpages;
1552         pginfo.num_hwpages = num_hwpages;
1553         pginfo.hwpage_size = hw_pgsize;
1554         pginfo.u.phy.num_phys_buf = 1;
1555         pginfo.u.phy.phys_buf_array = &ib_pbuf;
1556
1557         ret = ehca_reg_mr(shca, e_mr, iova_start, size_maxmr, 0, e_pd,
1558                           &pginfo, &e_mr->ib.ib_mr.lkey,
1559                           &e_mr->ib.ib_mr.rkey);
1560         if (ret) {
1561                 ehca_err(&shca->ib_device, "reg of internal max MR failed, "
1562                          "e_mr=%p iova_start=%p size_maxmr=%lx num_kpages=%x "
1563                          "num_hwpages=%x", e_mr, iova_start, size_maxmr,
1564                          num_kpages, num_hwpages);
1565                 goto ehca_reg_internal_maxmr_exit1;
1566         }
1567
1568         /* successful registration of all pages */
1569         e_mr->ib.ib_mr.device = e_pd->ib_pd.device;
1570         e_mr->ib.ib_mr.pd = &e_pd->ib_pd;
1571         e_mr->ib.ib_mr.uobject = NULL;
1572         atomic_inc(&(e_pd->ib_pd.usecnt));
1573         atomic_set(&(e_mr->ib.ib_mr.usecnt), 0);
1574         *e_maxmr = e_mr;
1575         return 0;
1576
1577 ehca_reg_internal_maxmr_exit1:
1578         ehca_mr_delete(e_mr);
1579 ehca_reg_internal_maxmr_exit0:
1580         if (ret)
1581                 ehca_err(&shca->ib_device, "ret=%i shca=%p e_pd=%p e_maxmr=%p",
1582                          ret, shca, e_pd, e_maxmr);
1583         return ret;
1584 } /* end ehca_reg_internal_maxmr() */
1585
1586 /*----------------------------------------------------------------------*/
1587
1588 int ehca_reg_maxmr(struct ehca_shca *shca,
1589                    struct ehca_mr *e_newmr,
1590                    u64 *iova_start,
1591                    int acl,
1592                    struct ehca_pd *e_pd,
1593                    u32 *lkey,
1594                    u32 *rkey)
1595 {
1596         u64 h_ret;
1597         struct ehca_mr *e_origmr = shca->maxmr;
1598         u32 hipz_acl;
1599         struct ehca_mr_hipzout_parms hipzout;
1600
1601         ehca_mrmw_map_acl(acl, &hipz_acl);
1602         ehca_mrmw_set_pgsize_hipz_acl(e_origmr->hwpage_size, &hipz_acl);
1603
1604         h_ret = hipz_h_register_smr(shca->ipz_hca_handle, e_newmr, e_origmr,
1605                                     (u64)iova_start, hipz_acl, e_pd->fw_pd,
1606                                     &hipzout);
1607         if (h_ret != H_SUCCESS) {
1608                 ehca_err(&shca->ib_device, "hipz_reg_smr failed, h_ret=%li "
1609                          "e_origmr=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1610                          h_ret, e_origmr, shca->ipz_hca_handle.handle,
1611                          e_origmr->ipz_mr_handle.handle,
1612                          e_origmr->ib.ib_mr.lkey);
1613                 return ehca2ib_return_code(h_ret);
1614         }
1615         /* successful registration */
1616         e_newmr->num_kpages = e_origmr->num_kpages;
1617         e_newmr->num_hwpages = e_origmr->num_hwpages;
1618         e_newmr->hwpage_size = e_origmr->hwpage_size;
1619         e_newmr->start = iova_start;
1620         e_newmr->size = e_origmr->size;
1621         e_newmr->acl = acl;
1622         e_newmr->ipz_mr_handle = hipzout.handle;
1623         *lkey = hipzout.lkey;
1624         *rkey = hipzout.rkey;
1625         return 0;
1626 } /* end ehca_reg_maxmr() */
1627
1628 /*----------------------------------------------------------------------*/
1629
1630 int ehca_dereg_internal_maxmr(struct ehca_shca *shca)
1631 {
1632         int ret;
1633         struct ehca_mr *e_maxmr;
1634         struct ib_pd *ib_pd;
1635
1636         if (!shca->maxmr) {
1637                 ehca_err(&shca->ib_device, "bad call, shca=%p", shca);
1638                 ret = -EINVAL;
1639                 goto ehca_dereg_internal_maxmr_exit0;
1640         }
1641
1642         e_maxmr = shca->maxmr;
1643         ib_pd = e_maxmr->ib.ib_mr.pd;
1644         shca->maxmr = NULL; /* remove internal max-MR indication from SHCA */
1645
1646         ret = ehca_dereg_mr(&e_maxmr->ib.ib_mr);
1647         if (ret) {
1648                 ehca_err(&shca->ib_device, "dereg internal max-MR failed, "
1649                          "ret=%i e_maxmr=%p shca=%p lkey=%x",
1650                          ret, e_maxmr, shca, e_maxmr->ib.ib_mr.lkey);
1651                 shca->maxmr = e_maxmr;
1652                 goto ehca_dereg_internal_maxmr_exit0;
1653         }
1654
1655         atomic_dec(&ib_pd->usecnt);
1656
1657 ehca_dereg_internal_maxmr_exit0:
1658         if (ret)
1659                 ehca_err(&shca->ib_device, "ret=%i shca=%p shca->maxmr=%p",
1660                          ret, shca, shca->maxmr);
1661         return ret;
1662 } /* end ehca_dereg_internal_maxmr() */
1663
1664 /*----------------------------------------------------------------------*/
1665
1666 /*
1667  * check physical buffer array of MR verbs for validness and
1668  * calculates MR size
1669  */
1670 int ehca_mr_chk_buf_and_calc_size(struct ib_phys_buf *phys_buf_array,
1671                                   int num_phys_buf,
1672                                   u64 *iova_start,
1673                                   u64 *size)
1674 {
1675         struct ib_phys_buf *pbuf = phys_buf_array;
1676         u64 size_count = 0;
1677         u32 i;
1678
1679         if (num_phys_buf == 0) {
1680                 ehca_gen_err("bad phys buf array len, num_phys_buf=0");
1681                 return -EINVAL;
1682         }
1683         /* check first buffer */
1684         if (((u64)iova_start & ~PAGE_MASK) != (pbuf->addr & ~PAGE_MASK)) {
1685                 ehca_gen_err("iova_start/addr mismatch, iova_start=%p "
1686                              "pbuf->addr=%lx pbuf->size=%lx",
1687                              iova_start, pbuf->addr, pbuf->size);
1688                 return -EINVAL;
1689         }
1690         if (((pbuf->addr + pbuf->size) % PAGE_SIZE) &&
1691             (num_phys_buf > 1)) {
1692                 ehca_gen_err("addr/size mismatch in 1st buf, pbuf->addr=%lx "
1693                              "pbuf->size=%lx", pbuf->addr, pbuf->size);
1694                 return -EINVAL;
1695         }
1696
1697         for (i = 0; i < num_phys_buf; i++) {
1698                 if ((i > 0) && (pbuf->addr % PAGE_SIZE)) {
1699                         ehca_gen_err("bad address, i=%x pbuf->addr=%lx "
1700                                      "pbuf->size=%lx",
1701                                      i, pbuf->addr, pbuf->size);
1702                         return -EINVAL;
1703                 }
1704                 if (((i > 0) && /* not 1st */
1705                      (i < (num_phys_buf - 1)) &&        /* not last */
1706                      (pbuf->size % PAGE_SIZE)) || (pbuf->size == 0)) {
1707                         ehca_gen_err("bad size, i=%x pbuf->size=%lx",
1708                                      i, pbuf->size);
1709                         return -EINVAL;
1710                 }
1711                 size_count += pbuf->size;
1712                 pbuf++;
1713         }
1714
1715         *size = size_count;
1716         return 0;
1717 } /* end ehca_mr_chk_buf_and_calc_size() */
1718
1719 /*----------------------------------------------------------------------*/
1720
1721 /* check page list of map FMR verb for validness */
1722 int ehca_fmr_check_page_list(struct ehca_mr *e_fmr,
1723                              u64 *page_list,
1724                              int list_len)
1725 {
1726         u32 i;
1727         u64 *page;
1728
1729         if ((list_len == 0) || (list_len > e_fmr->fmr_max_pages)) {
1730                 ehca_gen_err("bad list_len, list_len=%x "
1731                              "e_fmr->fmr_max_pages=%x fmr=%p",
1732                              list_len, e_fmr->fmr_max_pages, e_fmr);
1733                 return -EINVAL;
1734         }
1735
1736         /* each page must be aligned */
1737         page = page_list;
1738         for (i = 0; i < list_len; i++) {
1739                 if (*page % e_fmr->fmr_page_size) {
1740                         ehca_gen_err("bad page, i=%x *page=%lx page=%p fmr=%p "
1741                                      "fmr_page_size=%x", i, *page, page, e_fmr,
1742                                      e_fmr->fmr_page_size);
1743                         return -EINVAL;
1744                 }
1745                 page++;
1746         }
1747
1748         return 0;
1749 } /* end ehca_fmr_check_page_list() */
1750
1751 /*----------------------------------------------------------------------*/
1752
1753 /* PAGE_SIZE >= pginfo->hwpage_size */
1754 static int ehca_set_pagebuf_user1(struct ehca_mr_pginfo *pginfo,
1755                                   u32 number,
1756                                   u64 *kpage)
1757 {
1758         int ret = 0;
1759         struct ib_umem_chunk *prev_chunk;
1760         struct ib_umem_chunk *chunk;
1761         u64 pgaddr;
1762         u32 i = 0;
1763         u32 j = 0;
1764         int hwpages_per_kpage = PAGE_SIZE / pginfo->hwpage_size;
1765
1766         /* loop over desired chunk entries */
1767         chunk      = pginfo->u.usr.next_chunk;
1768         prev_chunk = pginfo->u.usr.next_chunk;
1769         list_for_each_entry_continue(
1770                 chunk, (&(pginfo->u.usr.region->chunk_list)), list) {
1771                 for (i = pginfo->u.usr.next_nmap; i < chunk->nmap; ) {
1772                         pgaddr = page_to_pfn(sg_page(&chunk->page_list[i]))
1773                                 << PAGE_SHIFT ;
1774                         *kpage = phys_to_abs(pgaddr +
1775                                              (pginfo->next_hwpage *
1776                                               pginfo->hwpage_size));
1777                         if ( !(*kpage) ) {
1778                                 ehca_gen_err("pgaddr=%lx "
1779                                              "chunk->page_list[i]=%lx "
1780                                              "i=%x next_hwpage=%lx",
1781                                              pgaddr, (u64)sg_dma_address(
1782                                                      &chunk->page_list[i]),
1783                                              i, pginfo->next_hwpage);
1784                                 return -EFAULT;
1785                         }
1786                         (pginfo->hwpage_cnt)++;
1787                         (pginfo->next_hwpage)++;
1788                         kpage++;
1789                         if (pginfo->next_hwpage % hwpages_per_kpage == 0) {
1790                                 (pginfo->kpage_cnt)++;
1791                                 (pginfo->u.usr.next_nmap)++;
1792                                 pginfo->next_hwpage = 0;
1793                                 i++;
1794                         }
1795                         j++;
1796                         if (j >= number) break;
1797                 }
1798                 if ((pginfo->u.usr.next_nmap >= chunk->nmap) &&
1799                     (j >= number)) {
1800                         pginfo->u.usr.next_nmap = 0;
1801                         prev_chunk = chunk;
1802                         break;
1803                 } else if (pginfo->u.usr.next_nmap >= chunk->nmap) {
1804                         pginfo->u.usr.next_nmap = 0;
1805                         prev_chunk = chunk;
1806                 } else if (j >= number)
1807                         break;
1808                 else
1809                         prev_chunk = chunk;
1810         }
1811         pginfo->u.usr.next_chunk =
1812                 list_prepare_entry(prev_chunk,
1813                                    (&(pginfo->u.usr.region->chunk_list)),
1814                                    list);
1815         return ret;
1816 }
1817
1818 /*
1819  * check given pages for contiguous layout
1820  * last page addr is returned in prev_pgaddr for further check
1821  */
1822 static int ehca_check_kpages_per_ate(struct scatterlist *page_list,
1823                                      int start_idx, int end_idx,
1824                                      u64 *prev_pgaddr)
1825 {
1826         int t;
1827         for (t = start_idx; t <= end_idx; t++) {
1828                 u64 pgaddr = page_to_pfn(sg_page(&page_list[t])) << PAGE_SHIFT;
1829                 ehca_gen_dbg("chunk_page=%lx value=%016lx", pgaddr,
1830                              *(u64 *)abs_to_virt(phys_to_abs(pgaddr)));
1831                 if (pgaddr - PAGE_SIZE != *prev_pgaddr) {
1832                         ehca_gen_err("uncontiguous page found pgaddr=%lx "
1833                                      "prev_pgaddr=%lx page_list_i=%x",
1834                                      pgaddr, *prev_pgaddr, t);
1835                         return -EINVAL;
1836                 }
1837                 *prev_pgaddr = pgaddr;
1838         }
1839         return 0;
1840 }
1841
1842 /* PAGE_SIZE < pginfo->hwpage_size */
1843 static int ehca_set_pagebuf_user2(struct ehca_mr_pginfo *pginfo,
1844                                   u32 number,
1845                                   u64 *kpage)
1846 {
1847         int ret = 0;
1848         struct ib_umem_chunk *prev_chunk;
1849         struct ib_umem_chunk *chunk;
1850         u64 pgaddr, prev_pgaddr;
1851         u32 i = 0;
1852         u32 j = 0;
1853         int kpages_per_hwpage = pginfo->hwpage_size / PAGE_SIZE;
1854         int nr_kpages = kpages_per_hwpage;
1855
1856         /* loop over desired chunk entries */
1857         chunk      = pginfo->u.usr.next_chunk;
1858         prev_chunk = pginfo->u.usr.next_chunk;
1859         list_for_each_entry_continue(
1860                 chunk, (&(pginfo->u.usr.region->chunk_list)), list) {
1861                 for (i = pginfo->u.usr.next_nmap; i < chunk->nmap; ) {
1862                         if (nr_kpages == kpages_per_hwpage) {
1863                                 pgaddr = ( page_to_pfn(sg_page(&chunk->page_list[i]))
1864                                            << PAGE_SHIFT );
1865                                 *kpage = phys_to_abs(pgaddr);
1866                                 if ( !(*kpage) ) {
1867                                         ehca_gen_err("pgaddr=%lx i=%x",
1868                                                      pgaddr, i);
1869                                         ret = -EFAULT;
1870                                         return ret;
1871                                 }
1872                                 /*
1873                                  * The first page in a hwpage must be aligned;
1874                                  * the first MR page is exempt from this rule.
1875                                  */
1876                                 if (pgaddr & (pginfo->hwpage_size - 1)) {
1877                                         if (pginfo->hwpage_cnt) {
1878                                                 ehca_gen_err(
1879                                                         "invalid alignment "
1880                                                         "pgaddr=%lx i=%x "
1881                                                         "mr_pgsize=%lx",
1882                                                         pgaddr, i,
1883                                                         pginfo->hwpage_size);
1884                                                 ret = -EFAULT;
1885                                                 return ret;
1886                                         }
1887                                         /* first MR page */
1888                                         pginfo->kpage_cnt =
1889                                                 (pgaddr &
1890                                                  (pginfo->hwpage_size - 1)) >>
1891                                                 PAGE_SHIFT;
1892                                         nr_kpages -= pginfo->kpage_cnt;
1893                                         *kpage = phys_to_abs(
1894                                                 pgaddr &
1895                                                 ~(pginfo->hwpage_size - 1));
1896                                 }
1897                                 ehca_gen_dbg("kpage=%lx chunk_page=%lx "
1898                                              "value=%016lx", *kpage, pgaddr,
1899                                              *(u64 *)abs_to_virt(
1900                                                      phys_to_abs(pgaddr)));
1901                                 prev_pgaddr = pgaddr;
1902                                 i++;
1903                                 pginfo->kpage_cnt++;
1904                                 pginfo->u.usr.next_nmap++;
1905                                 nr_kpages--;
1906                                 if (!nr_kpages)
1907                                         goto next_kpage;
1908                                 continue;
1909                         }
1910                         if (i + nr_kpages > chunk->nmap) {
1911                                 ret = ehca_check_kpages_per_ate(
1912                                         chunk->page_list, i,
1913                                         chunk->nmap - 1, &prev_pgaddr);
1914                                 if (ret) return ret;
1915                                 pginfo->kpage_cnt += chunk->nmap - i;
1916                                 pginfo->u.usr.next_nmap += chunk->nmap - i;
1917                                 nr_kpages -= chunk->nmap - i;
1918                                 break;
1919                         }
1920
1921                         ret = ehca_check_kpages_per_ate(chunk->page_list, i,
1922                                                         i + nr_kpages - 1,
1923                                                         &prev_pgaddr);
1924                         if (ret) return ret;
1925                         i += nr_kpages;
1926                         pginfo->kpage_cnt += nr_kpages;
1927                         pginfo->u.usr.next_nmap += nr_kpages;
1928 next_kpage:
1929                         nr_kpages = kpages_per_hwpage;
1930                         (pginfo->hwpage_cnt)++;
1931                         kpage++;
1932                         j++;
1933                         if (j >= number) break;
1934                 }
1935                 if ((pginfo->u.usr.next_nmap >= chunk->nmap) &&
1936                     (j >= number)) {
1937                         pginfo->u.usr.next_nmap = 0;
1938                         prev_chunk = chunk;
1939                         break;
1940                 } else if (pginfo->u.usr.next_nmap >= chunk->nmap) {
1941                         pginfo->u.usr.next_nmap = 0;
1942                         prev_chunk = chunk;
1943                 } else if (j >= number)
1944                         break;
1945                 else
1946                         prev_chunk = chunk;
1947         }
1948         pginfo->u.usr.next_chunk =
1949                 list_prepare_entry(prev_chunk,
1950                                    (&(pginfo->u.usr.region->chunk_list)),
1951                                    list);
1952         return ret;
1953 }
1954
1955 static int ehca_set_pagebuf_phys(struct ehca_mr_pginfo *pginfo,
1956                                  u32 number, u64 *kpage)
1957 {
1958         int ret = 0;
1959         struct ib_phys_buf *pbuf;
1960         u64 num_hw, offs_hw;
1961         u32 i = 0;
1962
1963         /* loop over desired phys_buf_array entries */
1964         while (i < number) {
1965                 pbuf   = pginfo->u.phy.phys_buf_array + pginfo->u.phy.next_buf;
1966                 num_hw  = NUM_CHUNKS((pbuf->addr % pginfo->hwpage_size) +
1967                                      pbuf->size, pginfo->hwpage_size);
1968                 offs_hw = (pbuf->addr & ~(pginfo->hwpage_size - 1)) /
1969                         pginfo->hwpage_size;
1970                 while (pginfo->next_hwpage < offs_hw + num_hw) {
1971                         /* sanity check */
1972                         if ((pginfo->kpage_cnt >= pginfo->num_kpages) ||
1973                             (pginfo->hwpage_cnt >= pginfo->num_hwpages)) {
1974                                 ehca_gen_err("kpage_cnt >= num_kpages, "
1975                                              "kpage_cnt=%lx num_kpages=%lx "
1976                                              "hwpage_cnt=%lx "
1977                                              "num_hwpages=%lx i=%x",
1978                                              pginfo->kpage_cnt,
1979                                              pginfo->num_kpages,
1980                                              pginfo->hwpage_cnt,
1981                                              pginfo->num_hwpages, i);
1982                                 return -EFAULT;
1983                         }
1984                         *kpage = phys_to_abs(
1985                                 (pbuf->addr & ~(pginfo->hwpage_size - 1)) +
1986                                 (pginfo->next_hwpage * pginfo->hwpage_size));
1987                         if ( !(*kpage) && pbuf->addr ) {
1988                                 ehca_gen_err("pbuf->addr=%lx pbuf->size=%lx "
1989                                              "next_hwpage=%lx", pbuf->addr,
1990                                              pbuf->size, pginfo->next_hwpage);
1991                                 return -EFAULT;
1992                         }
1993                         (pginfo->hwpage_cnt)++;
1994                         (pginfo->next_hwpage)++;
1995                         if (PAGE_SIZE >= pginfo->hwpage_size) {
1996                                 if (pginfo->next_hwpage %
1997                                     (PAGE_SIZE / pginfo->hwpage_size) == 0)
1998                                         (pginfo->kpage_cnt)++;
1999                         } else
2000                                 pginfo->kpage_cnt += pginfo->hwpage_size /
2001                                         PAGE_SIZE;
2002                         kpage++;
2003                         i++;
2004                         if (i >= number) break;
2005                 }
2006                 if (pginfo->next_hwpage >= offs_hw + num_hw) {
2007                         (pginfo->u.phy.next_buf)++;
2008                         pginfo->next_hwpage = 0;
2009                 }
2010         }
2011         return ret;
2012 }
2013
2014 static int ehca_set_pagebuf_fmr(struct ehca_mr_pginfo *pginfo,
2015                                 u32 number, u64 *kpage)
2016 {
2017         int ret = 0;
2018         u64 *fmrlist;
2019         u32 i;
2020
2021         /* loop over desired page_list entries */
2022         fmrlist = pginfo->u.fmr.page_list + pginfo->u.fmr.next_listelem;
2023         for (i = 0; i < number; i++) {
2024                 *kpage = phys_to_abs((*fmrlist & ~(pginfo->hwpage_size - 1)) +
2025                                      pginfo->next_hwpage * pginfo->hwpage_size);
2026                 if ( !(*kpage) ) {
2027                         ehca_gen_err("*fmrlist=%lx fmrlist=%p "
2028                                      "next_listelem=%lx next_hwpage=%lx",
2029                                      *fmrlist, fmrlist,
2030                                      pginfo->u.fmr.next_listelem,
2031                                      pginfo->next_hwpage);
2032                         return -EFAULT;
2033                 }
2034                 (pginfo->hwpage_cnt)++;
2035                 if (pginfo->u.fmr.fmr_pgsize >= pginfo->hwpage_size) {
2036                         if (pginfo->next_hwpage %
2037                             (pginfo->u.fmr.fmr_pgsize /
2038                              pginfo->hwpage_size) == 0) {
2039                                 (pginfo->kpage_cnt)++;
2040                                 (pginfo->u.fmr.next_listelem)++;
2041                                 fmrlist++;
2042                                 pginfo->next_hwpage = 0;
2043                         } else
2044                                 (pginfo->next_hwpage)++;
2045                 } else {
2046                         unsigned int cnt_per_hwpage = pginfo->hwpage_size /
2047                                 pginfo->u.fmr.fmr_pgsize;
2048                         unsigned int j;
2049                         u64 prev = *kpage;
2050                         /* check if adrs are contiguous */
2051                         for (j = 1; j < cnt_per_hwpage; j++) {
2052                                 u64 p = phys_to_abs(fmrlist[j] &
2053                                                     ~(pginfo->hwpage_size - 1));
2054                                 if (prev + pginfo->u.fmr.fmr_pgsize != p) {
2055                                         ehca_gen_err("uncontiguous fmr pages "
2056                                                      "found prev=%lx p=%lx "
2057                                                      "idx=%x", prev, p, i + j);
2058                                         return -EINVAL;
2059                                 }
2060                                 prev = p;
2061                         }
2062                         pginfo->kpage_cnt += cnt_per_hwpage;
2063                         pginfo->u.fmr.next_listelem += cnt_per_hwpage;
2064                         fmrlist += cnt_per_hwpage;
2065                 }
2066                 kpage++;
2067         }
2068         return ret;
2069 }
2070
2071 /* setup page buffer from page info */
2072 int ehca_set_pagebuf(struct ehca_mr_pginfo *pginfo,
2073                      u32 number,
2074                      u64 *kpage)
2075 {
2076         int ret;
2077
2078         switch (pginfo->type) {
2079         case EHCA_MR_PGI_PHYS:
2080                 ret = ehca_set_pagebuf_phys(pginfo, number, kpage);
2081                 break;
2082         case EHCA_MR_PGI_USER:
2083                 ret = PAGE_SIZE >= pginfo->hwpage_size ?
2084                         ehca_set_pagebuf_user1(pginfo, number, kpage) :
2085                         ehca_set_pagebuf_user2(pginfo, number, kpage);
2086                 break;
2087         case EHCA_MR_PGI_FMR:
2088                 ret = ehca_set_pagebuf_fmr(pginfo, number, kpage);
2089                 break;
2090         default:
2091                 ehca_gen_err("bad pginfo->type=%x", pginfo->type);
2092                 ret = -EFAULT;
2093                 break;
2094         }
2095         return ret;
2096 } /* end ehca_set_pagebuf() */
2097
2098 /*----------------------------------------------------------------------*/
2099
2100 /*
2101  * check MR if it is a max-MR, i.e. uses whole memory
2102  * in case it's a max-MR 1 is returned, else 0
2103  */
2104 int ehca_mr_is_maxmr(u64 size,
2105                      u64 *iova_start)
2106 {
2107         /* a MR is treated as max-MR only if it fits following: */
2108         if ((size == ((u64)high_memory - PAGE_OFFSET)) &&
2109             (iova_start == (void *)KERNELBASE)) {
2110                 ehca_gen_dbg("this is a max-MR");
2111                 return 1;
2112         } else
2113                 return 0;
2114 } /* end ehca_mr_is_maxmr() */
2115
2116 /*----------------------------------------------------------------------*/
2117
2118 /* map access control for MR/MW. This routine is used for MR and MW. */
2119 void ehca_mrmw_map_acl(int ib_acl,
2120                        u32 *hipz_acl)
2121 {
2122         *hipz_acl = 0;
2123         if (ib_acl & IB_ACCESS_REMOTE_READ)
2124                 *hipz_acl |= HIPZ_ACCESSCTRL_R_READ;
2125         if (ib_acl & IB_ACCESS_REMOTE_WRITE)
2126                 *hipz_acl |= HIPZ_ACCESSCTRL_R_WRITE;
2127         if (ib_acl & IB_ACCESS_REMOTE_ATOMIC)
2128                 *hipz_acl |= HIPZ_ACCESSCTRL_R_ATOMIC;
2129         if (ib_acl & IB_ACCESS_LOCAL_WRITE)
2130                 *hipz_acl |= HIPZ_ACCESSCTRL_L_WRITE;
2131         if (ib_acl & IB_ACCESS_MW_BIND)
2132                 *hipz_acl |= HIPZ_ACCESSCTRL_MW_BIND;
2133 } /* end ehca_mrmw_map_acl() */
2134
2135 /*----------------------------------------------------------------------*/
2136
2137 /* sets page size in hipz access control for MR/MW. */
2138 void ehca_mrmw_set_pgsize_hipz_acl(u32 pgsize, u32 *hipz_acl) /*INOUT*/
2139 {
2140         *hipz_acl |= (ehca_encode_hwpage_size(pgsize) << 24);
2141 } /* end ehca_mrmw_set_pgsize_hipz_acl() */
2142
2143 /*----------------------------------------------------------------------*/
2144
2145 /*
2146  * reverse map access control for MR/MW.
2147  * This routine is used for MR and MW.
2148  */
2149 void ehca_mrmw_reverse_map_acl(const u32 *hipz_acl,
2150                                int *ib_acl) /*OUT*/
2151 {
2152         *ib_acl = 0;
2153         if (*hipz_acl & HIPZ_ACCESSCTRL_R_READ)
2154                 *ib_acl |= IB_ACCESS_REMOTE_READ;
2155         if (*hipz_acl & HIPZ_ACCESSCTRL_R_WRITE)
2156                 *ib_acl |= IB_ACCESS_REMOTE_WRITE;
2157         if (*hipz_acl & HIPZ_ACCESSCTRL_R_ATOMIC)
2158                 *ib_acl |= IB_ACCESS_REMOTE_ATOMIC;
2159         if (*hipz_acl & HIPZ_ACCESSCTRL_L_WRITE)
2160                 *ib_acl |= IB_ACCESS_LOCAL_WRITE;
2161         if (*hipz_acl & HIPZ_ACCESSCTRL_MW_BIND)
2162                 *ib_acl |= IB_ACCESS_MW_BIND;
2163 } /* end ehca_mrmw_reverse_map_acl() */
2164
2165
2166 /*----------------------------------------------------------------------*/
2167
2168 /*
2169  * MR destructor and constructor
2170  * used in Reregister MR verb, sets all fields in ehca_mr_t to 0,
2171  * except struct ib_mr and spinlock
2172  */
2173 void ehca_mr_deletenew(struct ehca_mr *mr)
2174 {
2175         mr->flags = 0;
2176         mr->num_kpages = 0;
2177         mr->num_hwpages = 0;
2178         mr->acl = 0;
2179         mr->start = NULL;
2180         mr->fmr_page_size = 0;
2181         mr->fmr_max_pages = 0;
2182         mr->fmr_max_maps = 0;
2183         mr->fmr_map_cnt = 0;
2184         memset(&mr->ipz_mr_handle, 0, sizeof(mr->ipz_mr_handle));
2185         memset(&mr->galpas, 0, sizeof(mr->galpas));
2186 } /* end ehca_mr_deletenew() */
2187
2188 int ehca_init_mrmw_cache(void)
2189 {
2190         mr_cache = kmem_cache_create("ehca_cache_mr",
2191                                      sizeof(struct ehca_mr), 0,
2192                                      SLAB_HWCACHE_ALIGN,
2193                                      NULL);
2194         if (!mr_cache)
2195                 return -ENOMEM;
2196         mw_cache = kmem_cache_create("ehca_cache_mw",
2197                                      sizeof(struct ehca_mw), 0,
2198                                      SLAB_HWCACHE_ALIGN,
2199                                      NULL);
2200         if (!mw_cache) {
2201                 kmem_cache_destroy(mr_cache);
2202                 mr_cache = NULL;
2203                 return -ENOMEM;
2204         }
2205         return 0;
2206 }
2207
2208 void ehca_cleanup_mrmw_cache(void)
2209 {
2210         if (mr_cache)
2211                 kmem_cache_destroy(mr_cache);
2212         if (mw_cache)
2213                 kmem_cache_destroy(mw_cache);
2214 }