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