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