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