2 * IBM eServer eHCA Infiniband device driver for Linux on POWER
6 * Authors: Dietmar Decker <ddecker@de.ibm.com>
7 * Christoph Raisch <raisch@de.ibm.com>
8 * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
10 * Copyright (c) 2005 IBM Corporation
12 * All rights reserved.
14 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are met:
22 * Redistributions of source code must retain the above copyright notice, this
23 * list of conditions and the following disclaimer.
25 * Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials
28 * provided with the distribution.
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
38 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
43 #include <asm/current.h>
45 #include <rdma/ib_umem.h>
47 #include "ehca_iverbs.h"
48 #include "ehca_mrmw.h"
52 #define NUM_CHUNKS(length, chunk_size) \
53 (((length) + (chunk_size - 1)) / (chunk_size))
54 /* max number of rpages (per hcall register_rpages) */
55 #define MAX_RPAGES 512
57 static struct kmem_cache *mr_cache;
58 static struct kmem_cache *mw_cache;
61 EHCA_MR_PGSIZE4K = 0x1000L,
62 EHCA_MR_PGSIZE64K = 0x10000L,
63 EHCA_MR_PGSIZE1M = 0x100000L,
64 EHCA_MR_PGSIZE16M = 0x1000000L
67 static u32 ehca_encode_hwpage_size(u32 pgsize)
72 * map mr page size into hw code:
73 * 0, 1, 2, 3 for 4K, 64K, 1M, 64M
75 while (!(pgsize & 1)) {
82 static u64 ehca_get_max_hwpage_size(struct ehca_shca *shca)
84 if (shca->hca_cap_mr_pgsize & HCA_CAP_MR_PGSIZE_16M)
85 return EHCA_MR_PGSIZE16M;
86 return EHCA_MR_PGSIZE4K;
89 static struct ehca_mr *ehca_mr_new(void)
93 me = kmem_cache_zalloc(mr_cache, GFP_KERNEL);
95 spin_lock_init(&me->mrlock);
97 ehca_gen_err("alloc failed");
102 static void ehca_mr_delete(struct ehca_mr *me)
104 kmem_cache_free(mr_cache, me);
107 static struct ehca_mw *ehca_mw_new(void)
111 me = kmem_cache_zalloc(mw_cache, GFP_KERNEL);
113 spin_lock_init(&me->mwlock);
115 ehca_gen_err("alloc failed");
120 static void ehca_mw_delete(struct ehca_mw *me)
122 kmem_cache_free(mw_cache, me);
125 /*----------------------------------------------------------------------*/
127 struct ib_mr *ehca_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
131 struct ehca_mr *e_maxmr;
132 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
133 struct ehca_shca *shca =
134 container_of(pd->device, struct ehca_shca, ib_device);
137 e_maxmr = ehca_mr_new();
139 ehca_err(&shca->ib_device, "out of memory");
140 ib_mr = ERR_PTR(-ENOMEM);
141 goto get_dma_mr_exit0;
144 ret = ehca_reg_maxmr(shca, e_maxmr, (u64 *)KERNELBASE,
145 mr_access_flags, e_pd,
146 &e_maxmr->ib.ib_mr.lkey,
147 &e_maxmr->ib.ib_mr.rkey);
149 ehca_mr_delete(e_maxmr);
150 ib_mr = ERR_PTR(ret);
151 goto get_dma_mr_exit0;
153 ib_mr = &e_maxmr->ib.ib_mr;
155 ehca_err(&shca->ib_device, "no internal max-MR exist!");
156 ib_mr = ERR_PTR(-EINVAL);
157 goto get_dma_mr_exit0;
162 ehca_err(&shca->ib_device, "h_ret=%li pd=%p mr_access_flags=%x",
163 PTR_ERR(ib_mr), pd, mr_access_flags);
165 } /* end ehca_get_dma_mr() */
167 /*----------------------------------------------------------------------*/
169 struct ib_mr *ehca_reg_phys_mr(struct ib_pd *pd,
170 struct ib_phys_buf *phys_buf_array,
177 struct ehca_mr *e_mr;
178 struct ehca_shca *shca =
179 container_of(pd->device, struct ehca_shca, ib_device);
180 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
184 if ((num_phys_buf <= 0) || !phys_buf_array) {
185 ehca_err(pd->device, "bad input values: num_phys_buf=%x "
186 "phys_buf_array=%p", num_phys_buf, phys_buf_array);
187 ib_mr = ERR_PTR(-EINVAL);
188 goto reg_phys_mr_exit0;
190 if (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
191 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
192 ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
193 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE))) {
195 * Remote Write Access requires Local Write Access
196 * Remote Atomic Access requires Local Write Access
198 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
200 ib_mr = ERR_PTR(-EINVAL);
201 goto reg_phys_mr_exit0;
204 /* check physical buffer list and calculate size */
205 ret = ehca_mr_chk_buf_and_calc_size(phys_buf_array, num_phys_buf,
208 ib_mr = ERR_PTR(ret);
209 goto reg_phys_mr_exit0;
212 (((u64)iova_start + size) < (u64)iova_start)) {
213 ehca_err(pd->device, "bad input values: size=%lx iova_start=%p",
215 ib_mr = ERR_PTR(-EINVAL);
216 goto reg_phys_mr_exit0;
219 e_mr = ehca_mr_new();
221 ehca_err(pd->device, "out of memory");
222 ib_mr = ERR_PTR(-ENOMEM);
223 goto reg_phys_mr_exit0;
226 /* register MR on HCA */
227 if (ehca_mr_is_maxmr(size, iova_start)) {
228 e_mr->flags |= EHCA_MR_FLAG_MAXMR;
229 ret = ehca_reg_maxmr(shca, e_mr, iova_start, mr_access_flags,
230 e_pd, &e_mr->ib.ib_mr.lkey,
231 &e_mr->ib.ib_mr.rkey);
233 ib_mr = ERR_PTR(ret);
234 goto reg_phys_mr_exit1;
237 struct ehca_mr_pginfo pginfo;
242 num_kpages = NUM_CHUNKS(((u64)iova_start % PAGE_SIZE) + size,
244 /* for kernel space we try most possible pgsize */
245 hw_pgsize = ehca_get_max_hwpage_size(shca);
246 num_hwpages = NUM_CHUNKS(((u64)iova_start % hw_pgsize) + size,
248 memset(&pginfo, 0, sizeof(pginfo));
249 pginfo.type = EHCA_MR_PGI_PHYS;
250 pginfo.num_kpages = num_kpages;
251 pginfo.hwpage_size = hw_pgsize;
252 pginfo.num_hwpages = num_hwpages;
253 pginfo.u.phy.num_phys_buf = num_phys_buf;
254 pginfo.u.phy.phys_buf_array = phys_buf_array;
256 ((u64)iova_start & ~(hw_pgsize - 1)) / hw_pgsize;
258 ret = ehca_reg_mr(shca, e_mr, iova_start, size, mr_access_flags,
259 e_pd, &pginfo, &e_mr->ib.ib_mr.lkey,
260 &e_mr->ib.ib_mr.rkey);
262 ib_mr = ERR_PTR(ret);
263 goto reg_phys_mr_exit1;
267 /* successful registration of all pages */
268 return &e_mr->ib.ib_mr;
271 ehca_mr_delete(e_mr);
274 ehca_err(pd->device, "h_ret=%li pd=%p phys_buf_array=%p "
275 "num_phys_buf=%x mr_access_flags=%x iova_start=%p",
276 PTR_ERR(ib_mr), pd, phys_buf_array,
277 num_phys_buf, mr_access_flags, iova_start);
279 } /* end ehca_reg_phys_mr() */
281 /*----------------------------------------------------------------------*/
283 struct ib_mr *ehca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
284 u64 virt, int mr_access_flags,
285 struct ib_udata *udata)
288 struct ehca_mr *e_mr;
289 struct ehca_shca *shca =
290 container_of(pd->device, struct ehca_shca, ib_device);
291 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
292 struct ehca_mr_pginfo pginfo;
299 ehca_gen_err("bad pd=%p", pd);
300 return ERR_PTR(-EFAULT);
303 if (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
304 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
305 ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
306 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE))) {
308 * Remote Write Access requires Local Write Access
309 * Remote Atomic Access requires Local Write Access
311 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
313 ib_mr = ERR_PTR(-EINVAL);
314 goto reg_user_mr_exit0;
317 if (length == 0 || virt + length < virt) {
318 ehca_err(pd->device, "bad input values: length=%lx "
319 "virt_base=%lx", length, virt);
320 ib_mr = ERR_PTR(-EINVAL);
321 goto reg_user_mr_exit0;
324 e_mr = ehca_mr_new();
326 ehca_err(pd->device, "out of memory");
327 ib_mr = ERR_PTR(-ENOMEM);
328 goto reg_user_mr_exit0;
331 e_mr->umem = ib_umem_get(pd->uobject->context, start, length,
333 if (IS_ERR(e_mr->umem)) {
334 ib_mr = (void *)e_mr->umem;
335 goto reg_user_mr_exit1;
338 if (e_mr->umem->page_size != PAGE_SIZE) {
339 ehca_err(pd->device, "page size not supported, "
340 "e_mr->umem->page_size=%x", e_mr->umem->page_size);
341 ib_mr = ERR_PTR(-EINVAL);
342 goto reg_user_mr_exit2;
345 /* determine number of MR pages */
346 num_kpages = NUM_CHUNKS((virt % PAGE_SIZE) + length, PAGE_SIZE);
347 /* select proper hw_pgsize */
348 if (ehca_mr_largepage &&
349 (shca->hca_cap_mr_pgsize & HCA_CAP_MR_PGSIZE_16M)) {
350 if (length <= EHCA_MR_PGSIZE4K
351 && PAGE_SIZE == EHCA_MR_PGSIZE4K)
352 hwpage_size = EHCA_MR_PGSIZE4K;
353 else if (length <= EHCA_MR_PGSIZE64K)
354 hwpage_size = EHCA_MR_PGSIZE64K;
355 else if (length <= EHCA_MR_PGSIZE1M)
356 hwpage_size = EHCA_MR_PGSIZE1M;
358 hwpage_size = EHCA_MR_PGSIZE16M;
360 hwpage_size = EHCA_MR_PGSIZE4K;
361 ehca_dbg(pd->device, "hwpage_size=%lx", hwpage_size);
363 reg_user_mr_fallback:
364 num_hwpages = NUM_CHUNKS((virt % hwpage_size) + length, hwpage_size);
365 /* register MR on HCA */
366 memset(&pginfo, 0, sizeof(pginfo));
367 pginfo.type = EHCA_MR_PGI_USER;
368 pginfo.hwpage_size = hwpage_size;
369 pginfo.num_kpages = num_kpages;
370 pginfo.num_hwpages = num_hwpages;
371 pginfo.u.usr.region = e_mr->umem;
372 pginfo.next_hwpage = e_mr->umem->offset / hwpage_size;
373 pginfo.u.usr.next_chunk = list_prepare_entry(pginfo.u.usr.next_chunk,
374 (&e_mr->umem->chunk_list),
377 ret = ehca_reg_mr(shca, e_mr, (u64 *)virt, length, mr_access_flags,
378 e_pd, &pginfo, &e_mr->ib.ib_mr.lkey,
379 &e_mr->ib.ib_mr.rkey);
380 if (ret == -EINVAL && pginfo.hwpage_size > PAGE_SIZE) {
381 ehca_warn(pd->device, "failed to register mr "
382 "with hwpage_size=%lx", hwpage_size);
383 ehca_info(pd->device, "try to register mr with "
384 "kpage_size=%lx", PAGE_SIZE);
386 * this means kpages are not contiguous for a hw page
387 * try kernel page size as fallback solution
389 hwpage_size = PAGE_SIZE;
390 goto reg_user_mr_fallback;
393 ib_mr = ERR_PTR(ret);
394 goto reg_user_mr_exit2;
397 /* successful registration of all pages */
398 return &e_mr->ib.ib_mr;
401 ib_umem_release(e_mr->umem);
403 ehca_mr_delete(e_mr);
406 ehca_err(pd->device, "rc=%li pd=%p mr_access_flags=%x udata=%p",
407 PTR_ERR(ib_mr), pd, mr_access_flags, udata);
409 } /* end ehca_reg_user_mr() */
411 /*----------------------------------------------------------------------*/
413 int ehca_rereg_phys_mr(struct ib_mr *mr,
416 struct ib_phys_buf *phys_buf_array,
423 struct ehca_shca *shca =
424 container_of(mr->device, struct ehca_shca, ib_device);
425 struct ehca_mr *e_mr = container_of(mr, struct ehca_mr, ib.ib_mr);
426 struct ehca_pd *my_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
430 struct ehca_pd *new_pd;
431 u32 tmp_lkey, tmp_rkey;
432 unsigned long sl_flags;
435 struct ehca_mr_pginfo pginfo;
436 u32 cur_pid = current->tgid;
438 if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
439 (my_pd->ownpid != cur_pid)) {
440 ehca_err(mr->device, "Invalid caller pid=%x ownpid=%x",
441 cur_pid, my_pd->ownpid);
443 goto rereg_phys_mr_exit0;
446 if (!(mr_rereg_mask & IB_MR_REREG_TRANS)) {
447 /* TODO not supported, because PHYP rereg hCall needs pages */
448 ehca_err(mr->device, "rereg without IB_MR_REREG_TRANS not "
449 "supported yet, mr_rereg_mask=%x", mr_rereg_mask);
451 goto rereg_phys_mr_exit0;
454 if (mr_rereg_mask & IB_MR_REREG_PD) {
456 ehca_err(mr->device, "rereg with bad pd, pd=%p "
457 "mr_rereg_mask=%x", pd, mr_rereg_mask);
459 goto rereg_phys_mr_exit0;
464 ~(IB_MR_REREG_TRANS | IB_MR_REREG_PD | IB_MR_REREG_ACCESS)) ||
465 (mr_rereg_mask == 0)) {
467 goto rereg_phys_mr_exit0;
470 /* check other parameters */
471 if (e_mr == shca->maxmr) {
472 /* should be impossible, however reject to be sure */
473 ehca_err(mr->device, "rereg internal max-MR impossible, mr=%p "
474 "shca->maxmr=%p mr->lkey=%x",
475 mr, shca->maxmr, mr->lkey);
477 goto rereg_phys_mr_exit0;
479 if (mr_rereg_mask & IB_MR_REREG_TRANS) { /* transl., i.e. addr/size */
480 if (e_mr->flags & EHCA_MR_FLAG_FMR) {
481 ehca_err(mr->device, "not supported for FMR, mr=%p "
482 "flags=%x", mr, e_mr->flags);
484 goto rereg_phys_mr_exit0;
486 if (!phys_buf_array || num_phys_buf <= 0) {
487 ehca_err(mr->device, "bad input values mr_rereg_mask=%x"
488 " phys_buf_array=%p num_phys_buf=%x",
489 mr_rereg_mask, phys_buf_array, num_phys_buf);
491 goto rereg_phys_mr_exit0;
494 if ((mr_rereg_mask & IB_MR_REREG_ACCESS) && /* change ACL */
495 (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
496 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
497 ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
498 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)))) {
500 * Remote Write Access requires Local Write Access
501 * Remote Atomic Access requires Local Write Access
503 ehca_err(mr->device, "bad input values: mr_rereg_mask=%x "
504 "mr_access_flags=%x", mr_rereg_mask, mr_access_flags);
506 goto rereg_phys_mr_exit0;
509 /* set requested values dependent on rereg request */
510 spin_lock_irqsave(&e_mr->mrlock, sl_flags);
511 new_start = e_mr->start;
512 new_size = e_mr->size;
514 new_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
516 if (mr_rereg_mask & IB_MR_REREG_TRANS) {
517 u64 hw_pgsize = ehca_get_max_hwpage_size(shca);
519 new_start = iova_start; /* change address */
520 /* check physical buffer list and calculate size */
521 ret = ehca_mr_chk_buf_and_calc_size(phys_buf_array,
522 num_phys_buf, iova_start,
525 goto rereg_phys_mr_exit1;
526 if ((new_size == 0) ||
527 (((u64)iova_start + new_size) < (u64)iova_start)) {
528 ehca_err(mr->device, "bad input values: new_size=%lx "
529 "iova_start=%p", new_size, iova_start);
531 goto rereg_phys_mr_exit1;
533 num_kpages = NUM_CHUNKS(((u64)new_start % PAGE_SIZE) +
534 new_size, PAGE_SIZE);
535 num_hwpages = NUM_CHUNKS(((u64)new_start % hw_pgsize) +
536 new_size, hw_pgsize);
537 memset(&pginfo, 0, sizeof(pginfo));
538 pginfo.type = EHCA_MR_PGI_PHYS;
539 pginfo.num_kpages = num_kpages;
540 pginfo.hwpage_size = hw_pgsize;
541 pginfo.num_hwpages = num_hwpages;
542 pginfo.u.phy.num_phys_buf = num_phys_buf;
543 pginfo.u.phy.phys_buf_array = phys_buf_array;
545 ((u64)iova_start & ~(hw_pgsize - 1)) / hw_pgsize;
547 if (mr_rereg_mask & IB_MR_REREG_ACCESS)
548 new_acl = mr_access_flags;
549 if (mr_rereg_mask & IB_MR_REREG_PD)
550 new_pd = container_of(pd, struct ehca_pd, ib_pd);
552 ret = ehca_rereg_mr(shca, e_mr, new_start, new_size, new_acl,
553 new_pd, &pginfo, &tmp_lkey, &tmp_rkey);
555 goto rereg_phys_mr_exit1;
557 /* successful reregistration */
558 if (mr_rereg_mask & IB_MR_REREG_PD)
564 spin_unlock_irqrestore(&e_mr->mrlock, sl_flags);
567 ehca_err(mr->device, "ret=%i mr=%p mr_rereg_mask=%x pd=%p "
568 "phys_buf_array=%p num_phys_buf=%x mr_access_flags=%x "
570 ret, mr, mr_rereg_mask, pd, phys_buf_array,
571 num_phys_buf, mr_access_flags, iova_start);
573 } /* end ehca_rereg_phys_mr() */
575 /*----------------------------------------------------------------------*/
577 int ehca_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
581 struct ehca_shca *shca =
582 container_of(mr->device, struct ehca_shca, ib_device);
583 struct ehca_mr *e_mr = container_of(mr, struct ehca_mr, ib.ib_mr);
584 struct ehca_pd *my_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
585 u32 cur_pid = current->tgid;
586 unsigned long sl_flags;
587 struct ehca_mr_hipzout_parms hipzout;
589 if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
590 (my_pd->ownpid != cur_pid)) {
591 ehca_err(mr->device, "Invalid caller pid=%x ownpid=%x",
592 cur_pid, my_pd->ownpid);
597 if ((e_mr->flags & EHCA_MR_FLAG_FMR)) {
598 ehca_err(mr->device, "not supported for FMR, mr=%p e_mr=%p "
599 "e_mr->flags=%x", mr, e_mr, e_mr->flags);
604 memset(mr_attr, 0, sizeof(struct ib_mr_attr));
605 spin_lock_irqsave(&e_mr->mrlock, sl_flags);
607 h_ret = hipz_h_query_mr(shca->ipz_hca_handle, e_mr, &hipzout);
608 if (h_ret != H_SUCCESS) {
609 ehca_err(mr->device, "hipz_mr_query failed, h_ret=%li mr=%p "
610 "hca_hndl=%lx mr_hndl=%lx lkey=%x",
611 h_ret, mr, shca->ipz_hca_handle.handle,
612 e_mr->ipz_mr_handle.handle, mr->lkey);
613 ret = ehca2ib_return_code(h_ret);
616 mr_attr->pd = mr->pd;
617 mr_attr->device_virt_addr = hipzout.vaddr;
618 mr_attr->size = hipzout.len;
619 mr_attr->lkey = hipzout.lkey;
620 mr_attr->rkey = hipzout.rkey;
621 ehca_mrmw_reverse_map_acl(&hipzout.acl, &mr_attr->mr_access_flags);
624 spin_unlock_irqrestore(&e_mr->mrlock, sl_flags);
627 ehca_err(mr->device, "ret=%i mr=%p mr_attr=%p",
630 } /* end ehca_query_mr() */
632 /*----------------------------------------------------------------------*/
634 int ehca_dereg_mr(struct ib_mr *mr)
638 struct ehca_shca *shca =
639 container_of(mr->device, struct ehca_shca, ib_device);
640 struct ehca_mr *e_mr = container_of(mr, struct ehca_mr, ib.ib_mr);
641 struct ehca_pd *my_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
642 u32 cur_pid = current->tgid;
644 if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
645 (my_pd->ownpid != cur_pid)) {
646 ehca_err(mr->device, "Invalid caller pid=%x ownpid=%x",
647 cur_pid, my_pd->ownpid);
652 if ((e_mr->flags & EHCA_MR_FLAG_FMR)) {
653 ehca_err(mr->device, "not supported for FMR, mr=%p e_mr=%p "
654 "e_mr->flags=%x", mr, e_mr, e_mr->flags);
657 } else if (e_mr == shca->maxmr) {
658 /* should be impossible, however reject to be sure */
659 ehca_err(mr->device, "dereg internal max-MR impossible, mr=%p "
660 "shca->maxmr=%p mr->lkey=%x",
661 mr, shca->maxmr, mr->lkey);
666 /* TODO: BUSY: MR still has bound window(s) */
667 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_mr);
668 if (h_ret != H_SUCCESS) {
669 ehca_err(mr->device, "hipz_free_mr failed, h_ret=%li shca=%p "
670 "e_mr=%p hca_hndl=%lx mr_hndl=%lx mr->lkey=%x",
671 h_ret, shca, e_mr, shca->ipz_hca_handle.handle,
672 e_mr->ipz_mr_handle.handle, mr->lkey);
673 ret = ehca2ib_return_code(h_ret);
678 ib_umem_release(e_mr->umem);
680 /* successful deregistration */
681 ehca_mr_delete(e_mr);
685 ehca_err(mr->device, "ret=%i mr=%p", ret, mr);
687 } /* end ehca_dereg_mr() */
689 /*----------------------------------------------------------------------*/
691 struct ib_mw *ehca_alloc_mw(struct ib_pd *pd)
695 struct ehca_mw *e_mw;
696 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
697 struct ehca_shca *shca =
698 container_of(pd->device, struct ehca_shca, ib_device);
699 struct ehca_mw_hipzout_parms hipzout;
701 e_mw = ehca_mw_new();
703 ib_mw = ERR_PTR(-ENOMEM);
707 h_ret = hipz_h_alloc_resource_mw(shca->ipz_hca_handle, e_mw,
708 e_pd->fw_pd, &hipzout);
709 if (h_ret != H_SUCCESS) {
710 ehca_err(pd->device, "hipz_mw_allocate failed, h_ret=%li "
711 "shca=%p hca_hndl=%lx mw=%p",
712 h_ret, shca, shca->ipz_hca_handle.handle, e_mw);
713 ib_mw = ERR_PTR(ehca2ib_return_code(h_ret));
716 /* successful MW allocation */
717 e_mw->ipz_mw_handle = hipzout.handle;
718 e_mw->ib_mw.rkey = hipzout.rkey;
722 ehca_mw_delete(e_mw);
725 ehca_err(pd->device, "h_ret=%li pd=%p", PTR_ERR(ib_mw), pd);
727 } /* end ehca_alloc_mw() */
729 /*----------------------------------------------------------------------*/
731 int ehca_bind_mw(struct ib_qp *qp,
733 struct ib_mw_bind *mw_bind)
735 /* TODO: not supported up to now */
736 ehca_gen_err("bind MW currently not supported by HCAD");
739 } /* end ehca_bind_mw() */
741 /*----------------------------------------------------------------------*/
743 int ehca_dealloc_mw(struct ib_mw *mw)
746 struct ehca_shca *shca =
747 container_of(mw->device, struct ehca_shca, ib_device);
748 struct ehca_mw *e_mw = container_of(mw, struct ehca_mw, ib_mw);
750 h_ret = hipz_h_free_resource_mw(shca->ipz_hca_handle, e_mw);
751 if (h_ret != H_SUCCESS) {
752 ehca_err(mw->device, "hipz_free_mw failed, h_ret=%li shca=%p "
753 "mw=%p rkey=%x hca_hndl=%lx mw_hndl=%lx",
754 h_ret, shca, mw, mw->rkey, shca->ipz_hca_handle.handle,
755 e_mw->ipz_mw_handle.handle);
756 return ehca2ib_return_code(h_ret);
758 /* successful deallocation */
759 ehca_mw_delete(e_mw);
761 } /* end ehca_dealloc_mw() */
763 /*----------------------------------------------------------------------*/
765 struct ib_fmr *ehca_alloc_fmr(struct ib_pd *pd,
767 struct ib_fmr_attr *fmr_attr)
769 struct ib_fmr *ib_fmr;
770 struct ehca_shca *shca =
771 container_of(pd->device, struct ehca_shca, ib_device);
772 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
773 struct ehca_mr *e_fmr;
775 u32 tmp_lkey, tmp_rkey;
776 struct ehca_mr_pginfo pginfo;
779 /* check other parameters */
780 if (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
781 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
782 ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
783 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE))) {
785 * Remote Write Access requires Local Write Access
786 * Remote Atomic Access requires Local Write Access
788 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
790 ib_fmr = ERR_PTR(-EINVAL);
791 goto alloc_fmr_exit0;
793 if (mr_access_flags & IB_ACCESS_MW_BIND) {
794 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
796 ib_fmr = ERR_PTR(-EINVAL);
797 goto alloc_fmr_exit0;
799 if ((fmr_attr->max_pages == 0) || (fmr_attr->max_maps == 0)) {
800 ehca_err(pd->device, "bad input values: fmr_attr->max_pages=%x "
801 "fmr_attr->max_maps=%x fmr_attr->page_shift=%x",
802 fmr_attr->max_pages, fmr_attr->max_maps,
803 fmr_attr->page_shift);
804 ib_fmr = ERR_PTR(-EINVAL);
805 goto alloc_fmr_exit0;
807 hw_pgsize = ehca_get_max_hwpage_size(shca);
808 if ((1 << fmr_attr->page_shift) != hw_pgsize) {
809 ehca_err(pd->device, "unsupported fmr_attr->page_shift=%x",
810 fmr_attr->page_shift);
811 ib_fmr = ERR_PTR(-EINVAL);
812 goto alloc_fmr_exit0;
815 e_fmr = ehca_mr_new();
817 ib_fmr = ERR_PTR(-ENOMEM);
818 goto alloc_fmr_exit0;
820 e_fmr->flags |= EHCA_MR_FLAG_FMR;
822 /* register MR on HCA */
823 memset(&pginfo, 0, sizeof(pginfo));
825 * pginfo.num_hwpages==0, ie register_rpages() will not be called
826 * but deferred to map_phys_fmr()
828 ret = ehca_reg_mr(shca, e_fmr, NULL,
829 fmr_attr->max_pages * (1 << fmr_attr->page_shift),
830 mr_access_flags, e_pd, &pginfo,
831 &tmp_lkey, &tmp_rkey);
833 ib_fmr = ERR_PTR(ret);
834 goto alloc_fmr_exit1;
838 e_fmr->hwpage_size = hw_pgsize;
839 e_fmr->fmr_page_size = 1 << fmr_attr->page_shift;
840 e_fmr->fmr_max_pages = fmr_attr->max_pages;
841 e_fmr->fmr_max_maps = fmr_attr->max_maps;
842 e_fmr->fmr_map_cnt = 0;
843 return &e_fmr->ib.ib_fmr;
846 ehca_mr_delete(e_fmr);
849 } /* end ehca_alloc_fmr() */
851 /*----------------------------------------------------------------------*/
853 int ehca_map_phys_fmr(struct ib_fmr *fmr,
859 struct ehca_shca *shca =
860 container_of(fmr->device, struct ehca_shca, ib_device);
861 struct ehca_mr *e_fmr = container_of(fmr, struct ehca_mr, ib.ib_fmr);
862 struct ehca_pd *e_pd = container_of(fmr->pd, struct ehca_pd, ib_pd);
863 struct ehca_mr_pginfo pginfo;
864 u32 tmp_lkey, tmp_rkey;
866 if (!(e_fmr->flags & EHCA_MR_FLAG_FMR)) {
867 ehca_err(fmr->device, "not a FMR, e_fmr=%p e_fmr->flags=%x",
868 e_fmr, e_fmr->flags);
870 goto map_phys_fmr_exit0;
872 ret = ehca_fmr_check_page_list(e_fmr, page_list, list_len);
874 goto map_phys_fmr_exit0;
875 if (iova % e_fmr->fmr_page_size) {
876 /* only whole-numbered pages */
877 ehca_err(fmr->device, "bad iova, iova=%lx fmr_page_size=%x",
878 iova, e_fmr->fmr_page_size);
880 goto map_phys_fmr_exit0;
882 if (e_fmr->fmr_map_cnt >= e_fmr->fmr_max_maps) {
883 /* HCAD does not limit the maps, however trace this anyway */
884 ehca_info(fmr->device, "map limit exceeded, fmr=%p "
885 "e_fmr->fmr_map_cnt=%x e_fmr->fmr_max_maps=%x",
886 fmr, e_fmr->fmr_map_cnt, e_fmr->fmr_max_maps);
889 memset(&pginfo, 0, sizeof(pginfo));
890 pginfo.type = EHCA_MR_PGI_FMR;
891 pginfo.num_kpages = list_len;
892 pginfo.hwpage_size = e_fmr->hwpage_size;
894 list_len * e_fmr->fmr_page_size / pginfo.hwpage_size;
895 pginfo.u.fmr.page_list = page_list;
897 (iova & (e_fmr->fmr_page_size-1)) / pginfo.hwpage_size;
898 pginfo.u.fmr.fmr_pgsize = e_fmr->fmr_page_size;
900 ret = ehca_rereg_mr(shca, e_fmr, (u64 *)iova,
901 list_len * e_fmr->fmr_page_size,
902 e_fmr->acl, e_pd, &pginfo, &tmp_lkey, &tmp_rkey);
904 goto map_phys_fmr_exit0;
906 /* successful reregistration */
907 e_fmr->fmr_map_cnt++;
908 e_fmr->ib.ib_fmr.lkey = tmp_lkey;
909 e_fmr->ib.ib_fmr.rkey = tmp_rkey;
914 ehca_err(fmr->device, "ret=%i fmr=%p page_list=%p list_len=%x "
915 "iova=%lx", ret, fmr, page_list, list_len, iova);
917 } /* end ehca_map_phys_fmr() */
919 /*----------------------------------------------------------------------*/
921 int ehca_unmap_fmr(struct list_head *fmr_list)
924 struct ib_fmr *ib_fmr;
925 struct ehca_shca *shca = NULL;
926 struct ehca_shca *prev_shca;
927 struct ehca_mr *e_fmr;
929 u32 unmap_fmr_cnt = 0;
931 /* check all FMR belong to same SHCA, and check internal flag */
932 list_for_each_entry(ib_fmr, fmr_list, list) {
935 ehca_gen_err("bad fmr=%p in list", ib_fmr);
937 goto unmap_fmr_exit0;
939 shca = container_of(ib_fmr->device, struct ehca_shca,
941 e_fmr = container_of(ib_fmr, struct ehca_mr, ib.ib_fmr);
942 if ((shca != prev_shca) && prev_shca) {
943 ehca_err(&shca->ib_device, "SHCA mismatch, shca=%p "
944 "prev_shca=%p e_fmr=%p",
945 shca, prev_shca, e_fmr);
947 goto unmap_fmr_exit0;
949 if (!(e_fmr->flags & EHCA_MR_FLAG_FMR)) {
950 ehca_err(&shca->ib_device, "not a FMR, e_fmr=%p "
951 "e_fmr->flags=%x", e_fmr, e_fmr->flags);
953 goto unmap_fmr_exit0;
958 /* loop over all FMRs to unmap */
959 list_for_each_entry(ib_fmr, fmr_list, list) {
961 e_fmr = container_of(ib_fmr, struct ehca_mr, ib.ib_fmr);
962 shca = container_of(ib_fmr->device, struct ehca_shca,
964 ret = ehca_unmap_one_fmr(shca, e_fmr);
966 /* unmap failed, stop unmapping of rest of FMRs */
967 ehca_err(&shca->ib_device, "unmap of one FMR failed, "
968 "stop rest, e_fmr=%p num_fmr=%x "
969 "unmap_fmr_cnt=%x lkey=%x", e_fmr, num_fmr,
970 unmap_fmr_cnt, e_fmr->ib.ib_fmr.lkey);
971 goto unmap_fmr_exit0;
977 ehca_gen_err("ret=%i fmr_list=%p num_fmr=%x unmap_fmr_cnt=%x",
978 ret, fmr_list, num_fmr, unmap_fmr_cnt);
980 } /* end ehca_unmap_fmr() */
982 /*----------------------------------------------------------------------*/
984 int ehca_dealloc_fmr(struct ib_fmr *fmr)
988 struct ehca_shca *shca =
989 container_of(fmr->device, struct ehca_shca, ib_device);
990 struct ehca_mr *e_fmr = container_of(fmr, struct ehca_mr, ib.ib_fmr);
992 if (!(e_fmr->flags & EHCA_MR_FLAG_FMR)) {
993 ehca_err(fmr->device, "not a FMR, e_fmr=%p e_fmr->flags=%x",
994 e_fmr, e_fmr->flags);
999 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_fmr);
1000 if (h_ret != H_SUCCESS) {
1001 ehca_err(fmr->device, "hipz_free_mr failed, h_ret=%li e_fmr=%p "
1002 "hca_hndl=%lx fmr_hndl=%lx fmr->lkey=%x",
1003 h_ret, e_fmr, shca->ipz_hca_handle.handle,
1004 e_fmr->ipz_mr_handle.handle, fmr->lkey);
1005 ret = ehca2ib_return_code(h_ret);
1006 goto free_fmr_exit0;
1008 /* successful deregistration */
1009 ehca_mr_delete(e_fmr);
1014 ehca_err(&shca->ib_device, "ret=%i fmr=%p", ret, fmr);
1016 } /* end ehca_dealloc_fmr() */
1018 /*----------------------------------------------------------------------*/
1020 int ehca_reg_mr(struct ehca_shca *shca,
1021 struct ehca_mr *e_mr,
1025 struct ehca_pd *e_pd,
1026 struct ehca_mr_pginfo *pginfo,
1033 struct ehca_mr_hipzout_parms hipzout;
1035 ehca_mrmw_map_acl(acl, &hipz_acl);
1036 ehca_mrmw_set_pgsize_hipz_acl(pginfo->hwpage_size, &hipz_acl);
1037 if (ehca_use_hp_mr == 1)
1038 hipz_acl |= 0x00000001;
1040 h_ret = hipz_h_alloc_resource_mr(shca->ipz_hca_handle, e_mr,
1041 (u64)iova_start, size, hipz_acl,
1042 e_pd->fw_pd, &hipzout);
1043 if (h_ret != H_SUCCESS) {
1044 ehca_err(&shca->ib_device, "hipz_alloc_mr failed, h_ret=%li "
1045 "hca_hndl=%lx", h_ret, shca->ipz_hca_handle.handle);
1046 ret = ehca2ib_return_code(h_ret);
1047 goto ehca_reg_mr_exit0;
1050 e_mr->ipz_mr_handle = hipzout.handle;
1052 ret = ehca_reg_mr_rpages(shca, e_mr, pginfo);
1054 goto ehca_reg_mr_exit1;
1056 /* successful registration */
1057 e_mr->num_kpages = pginfo->num_kpages;
1058 e_mr->num_hwpages = pginfo->num_hwpages;
1059 e_mr->hwpage_size = pginfo->hwpage_size;
1060 e_mr->start = iova_start;
1063 *lkey = hipzout.lkey;
1064 *rkey = hipzout.rkey;
1068 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_mr);
1069 if (h_ret != H_SUCCESS) {
1070 ehca_err(&shca->ib_device, "h_ret=%li shca=%p e_mr=%p "
1071 "iova_start=%p size=%lx acl=%x e_pd=%p lkey=%x "
1072 "pginfo=%p num_kpages=%lx num_hwpages=%lx ret=%i",
1073 h_ret, shca, e_mr, iova_start, size, acl, e_pd,
1074 hipzout.lkey, pginfo, pginfo->num_kpages,
1075 pginfo->num_hwpages, ret);
1076 ehca_err(&shca->ib_device, "internal error in ehca_reg_mr, "
1081 ehca_err(&shca->ib_device, "ret=%i shca=%p e_mr=%p "
1082 "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
1083 "num_kpages=%lx num_hwpages=%lx",
1084 ret, shca, e_mr, iova_start, size, acl, e_pd, pginfo,
1085 pginfo->num_kpages, pginfo->num_hwpages);
1087 } /* end ehca_reg_mr() */
1089 /*----------------------------------------------------------------------*/
1091 int ehca_reg_mr_rpages(struct ehca_shca *shca,
1092 struct ehca_mr *e_mr,
1093 struct ehca_mr_pginfo *pginfo)
1102 if (!pginfo->num_hwpages) /* in case of fmr */
1105 kpage = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
1107 ehca_err(&shca->ib_device, "kpage alloc failed");
1109 goto ehca_reg_mr_rpages_exit0;
1112 /* max MAX_RPAGES ehca mr pages per register call */
1113 for (i = 0; i < NUM_CHUNKS(pginfo->num_hwpages, MAX_RPAGES); i++) {
1115 if (i == NUM_CHUNKS(pginfo->num_hwpages, MAX_RPAGES) - 1) {
1116 rnum = pginfo->num_hwpages % MAX_RPAGES; /* last shot */
1118 rnum = MAX_RPAGES; /* last shot is full */
1122 ret = ehca_set_pagebuf(pginfo, rnum, kpage);
1124 ehca_err(&shca->ib_device, "ehca_set_pagebuf "
1125 "bad rc, ret=%i rnum=%x kpage=%p",
1127 goto ehca_reg_mr_rpages_exit1;
1131 rpage = virt_to_abs(kpage);
1133 ehca_err(&shca->ib_device, "kpage=%p i=%x",
1136 goto ehca_reg_mr_rpages_exit1;
1141 h_ret = hipz_h_register_rpage_mr(
1142 shca->ipz_hca_handle, e_mr,
1143 ehca_encode_hwpage_size(pginfo->hwpage_size),
1146 if (i == NUM_CHUNKS(pginfo->num_hwpages, MAX_RPAGES) - 1) {
1148 * check for 'registration complete'==H_SUCCESS
1149 * and for 'page registered'==H_PAGE_REGISTERED
1151 if (h_ret != H_SUCCESS) {
1152 ehca_err(&shca->ib_device, "last "
1153 "hipz_reg_rpage_mr failed, h_ret=%li "
1154 "e_mr=%p i=%x hca_hndl=%lx mr_hndl=%lx"
1155 " lkey=%x", h_ret, e_mr, i,
1156 shca->ipz_hca_handle.handle,
1157 e_mr->ipz_mr_handle.handle,
1158 e_mr->ib.ib_mr.lkey);
1159 ret = ehca2ib_return_code(h_ret);
1163 } else if (h_ret != H_PAGE_REGISTERED) {
1164 ehca_err(&shca->ib_device, "hipz_reg_rpage_mr failed, "
1165 "h_ret=%li e_mr=%p i=%x lkey=%x hca_hndl=%lx "
1166 "mr_hndl=%lx", h_ret, e_mr, i,
1167 e_mr->ib.ib_mr.lkey,
1168 shca->ipz_hca_handle.handle,
1169 e_mr->ipz_mr_handle.handle);
1170 ret = ehca2ib_return_code(h_ret);
1177 ehca_reg_mr_rpages_exit1:
1178 ehca_free_fw_ctrlblock(kpage);
1179 ehca_reg_mr_rpages_exit0:
1181 ehca_err(&shca->ib_device, "ret=%i shca=%p e_mr=%p pginfo=%p "
1182 "num_kpages=%lx num_hwpages=%lx", ret, shca, e_mr,
1183 pginfo, pginfo->num_kpages, pginfo->num_hwpages);
1185 } /* end ehca_reg_mr_rpages() */
1187 /*----------------------------------------------------------------------*/
1189 inline int ehca_rereg_mr_rereg1(struct ehca_shca *shca,
1190 struct ehca_mr *e_mr,
1194 struct ehca_pd *e_pd,
1195 struct ehca_mr_pginfo *pginfo,
1204 struct ehca_mr_pginfo pginfo_save;
1205 struct ehca_mr_hipzout_parms hipzout;
1207 ehca_mrmw_map_acl(acl, &hipz_acl);
1208 ehca_mrmw_set_pgsize_hipz_acl(pginfo->hwpage_size, &hipz_acl);
1210 kpage = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
1212 ehca_err(&shca->ib_device, "kpage alloc failed");
1214 goto ehca_rereg_mr_rereg1_exit0;
1217 pginfo_save = *pginfo;
1218 ret = ehca_set_pagebuf(pginfo, pginfo->num_hwpages, kpage);
1220 ehca_err(&shca->ib_device, "set pagebuf failed, e_mr=%p "
1221 "pginfo=%p type=%x num_kpages=%lx num_hwpages=%lx "
1222 "kpage=%p", e_mr, pginfo, pginfo->type,
1223 pginfo->num_kpages, pginfo->num_hwpages, kpage);
1224 goto ehca_rereg_mr_rereg1_exit1;
1226 rpage = virt_to_abs(kpage);
1228 ehca_err(&shca->ib_device, "kpage=%p", kpage);
1230 goto ehca_rereg_mr_rereg1_exit1;
1232 h_ret = hipz_h_reregister_pmr(shca->ipz_hca_handle, e_mr,
1233 (u64)iova_start, size, hipz_acl,
1234 e_pd->fw_pd, rpage, &hipzout);
1235 if (h_ret != H_SUCCESS) {
1237 * reregistration unsuccessful, try it again with the 3 hCalls,
1238 * e.g. this is required in case H_MR_CONDITION
1239 * (MW bound or MR is shared)
1241 ehca_warn(&shca->ib_device, "hipz_h_reregister_pmr failed "
1242 "(Rereg1), h_ret=%li e_mr=%p", h_ret, e_mr);
1243 *pginfo = pginfo_save;
1245 } else if ((u64 *)hipzout.vaddr != iova_start) {
1246 ehca_err(&shca->ib_device, "PHYP changed iova_start in "
1247 "rereg_pmr, iova_start=%p iova_start_out=%lx e_mr=%p "
1248 "mr_handle=%lx lkey=%x lkey_out=%x", iova_start,
1249 hipzout.vaddr, e_mr, e_mr->ipz_mr_handle.handle,
1250 e_mr->ib.ib_mr.lkey, hipzout.lkey);
1254 * successful reregistration
1255 * note: start and start_out are identical for eServer HCAs
1257 e_mr->num_kpages = pginfo->num_kpages;
1258 e_mr->num_hwpages = pginfo->num_hwpages;
1259 e_mr->hwpage_size = pginfo->hwpage_size;
1260 e_mr->start = iova_start;
1263 *lkey = hipzout.lkey;
1264 *rkey = hipzout.rkey;
1267 ehca_rereg_mr_rereg1_exit1:
1268 ehca_free_fw_ctrlblock(kpage);
1269 ehca_rereg_mr_rereg1_exit0:
1270 if ( ret && (ret != -EAGAIN) )
1271 ehca_err(&shca->ib_device, "ret=%i lkey=%x rkey=%x "
1272 "pginfo=%p num_kpages=%lx num_hwpages=%lx",
1273 ret, *lkey, *rkey, pginfo, pginfo->num_kpages,
1274 pginfo->num_hwpages);
1276 } /* end ehca_rereg_mr_rereg1() */
1278 /*----------------------------------------------------------------------*/
1280 int ehca_rereg_mr(struct ehca_shca *shca,
1281 struct ehca_mr *e_mr,
1285 struct ehca_pd *e_pd,
1286 struct ehca_mr_pginfo *pginfo,
1292 int rereg_1_hcall = 1; /* 1: use hipz_h_reregister_pmr directly */
1293 int rereg_3_hcall = 0; /* 1: use 3 hipz calls for reregistration */
1295 /* first determine reregistration hCall(s) */
1296 if ((pginfo->num_hwpages > MAX_RPAGES) ||
1297 (e_mr->num_hwpages > MAX_RPAGES) ||
1298 (pginfo->num_hwpages > e_mr->num_hwpages)) {
1299 ehca_dbg(&shca->ib_device, "Rereg3 case, "
1300 "pginfo->num_hwpages=%lx e_mr->num_hwpages=%x",
1301 pginfo->num_hwpages, e_mr->num_hwpages);
1306 if (e_mr->flags & EHCA_MR_FLAG_MAXMR) { /* check for max-MR */
1309 e_mr->flags &= ~EHCA_MR_FLAG_MAXMR;
1310 ehca_err(&shca->ib_device, "Rereg MR for max-MR! e_mr=%p",
1314 if (rereg_1_hcall) {
1315 ret = ehca_rereg_mr_rereg1(shca, e_mr, iova_start, size,
1316 acl, e_pd, pginfo, lkey, rkey);
1321 goto ehca_rereg_mr_exit0;
1325 if (rereg_3_hcall) {
1326 struct ehca_mr save_mr;
1328 /* first deregister old MR */
1329 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_mr);
1330 if (h_ret != H_SUCCESS) {
1331 ehca_err(&shca->ib_device, "hipz_free_mr failed, "
1332 "h_ret=%li e_mr=%p hca_hndl=%lx mr_hndl=%lx "
1334 h_ret, e_mr, shca->ipz_hca_handle.handle,
1335 e_mr->ipz_mr_handle.handle,
1336 e_mr->ib.ib_mr.lkey);
1337 ret = ehca2ib_return_code(h_ret);
1338 goto ehca_rereg_mr_exit0;
1340 /* clean ehca_mr_t, without changing struct ib_mr and lock */
1342 ehca_mr_deletenew(e_mr);
1344 /* set some MR values */
1345 e_mr->flags = save_mr.flags;
1346 e_mr->hwpage_size = save_mr.hwpage_size;
1347 e_mr->fmr_page_size = save_mr.fmr_page_size;
1348 e_mr->fmr_max_pages = save_mr.fmr_max_pages;
1349 e_mr->fmr_max_maps = save_mr.fmr_max_maps;
1350 e_mr->fmr_map_cnt = save_mr.fmr_map_cnt;
1352 ret = ehca_reg_mr(shca, e_mr, iova_start, size, acl,
1353 e_pd, pginfo, lkey, rkey);
1355 u32 offset = (u64)(&e_mr->flags) - (u64)e_mr;
1356 memcpy(&e_mr->flags, &(save_mr.flags),
1357 sizeof(struct ehca_mr) - offset);
1358 goto ehca_rereg_mr_exit0;
1362 ehca_rereg_mr_exit0:
1364 ehca_err(&shca->ib_device, "ret=%i shca=%p e_mr=%p "
1365 "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
1366 "num_kpages=%lx lkey=%x rkey=%x rereg_1_hcall=%x "
1367 "rereg_3_hcall=%x", ret, shca, e_mr, iova_start, size,
1368 acl, e_pd, pginfo, pginfo->num_kpages, *lkey, *rkey,
1369 rereg_1_hcall, rereg_3_hcall);
1371 } /* end ehca_rereg_mr() */
1373 /*----------------------------------------------------------------------*/
1375 int ehca_unmap_one_fmr(struct ehca_shca *shca,
1376 struct ehca_mr *e_fmr)
1380 struct ehca_pd *e_pd =
1381 container_of(e_fmr->ib.ib_fmr.pd, struct ehca_pd, ib_pd);
1382 struct ehca_mr save_fmr;
1383 u32 tmp_lkey, tmp_rkey;
1384 struct ehca_mr_pginfo pginfo;
1385 struct ehca_mr_hipzout_parms hipzout;
1386 struct ehca_mr save_mr;
1388 if (e_fmr->fmr_max_pages <= MAX_RPAGES) {
1390 * note: after using rereg hcall with len=0,
1391 * rereg hcall must be used again for registering pages
1393 h_ret = hipz_h_reregister_pmr(shca->ipz_hca_handle, e_fmr, 0,
1394 0, 0, e_pd->fw_pd, 0, &hipzout);
1395 if (h_ret == H_SUCCESS) {
1396 /* successful reregistration */
1397 e_fmr->start = NULL;
1399 tmp_lkey = hipzout.lkey;
1400 tmp_rkey = hipzout.rkey;
1404 * should not happen, because length checked above,
1405 * FMRs are not shared and no MW bound to FMRs
1407 ehca_err(&shca->ib_device, "hipz_reregister_pmr failed "
1408 "(Rereg1), h_ret=%li e_fmr=%p hca_hndl=%lx "
1409 "mr_hndl=%lx lkey=%x lkey_out=%x",
1410 h_ret, e_fmr, shca->ipz_hca_handle.handle,
1411 e_fmr->ipz_mr_handle.handle,
1412 e_fmr->ib.ib_fmr.lkey, hipzout.lkey);
1413 /* try free and rereg */
1416 /* first free old FMR */
1417 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_fmr);
1418 if (h_ret != H_SUCCESS) {
1419 ehca_err(&shca->ib_device, "hipz_free_mr failed, "
1420 "h_ret=%li e_fmr=%p hca_hndl=%lx mr_hndl=%lx "
1422 h_ret, e_fmr, shca->ipz_hca_handle.handle,
1423 e_fmr->ipz_mr_handle.handle,
1424 e_fmr->ib.ib_fmr.lkey);
1425 ret = ehca2ib_return_code(h_ret);
1426 goto ehca_unmap_one_fmr_exit0;
1428 /* clean ehca_mr_t, without changing lock */
1430 ehca_mr_deletenew(e_fmr);
1432 /* set some MR values */
1433 e_fmr->flags = save_fmr.flags;
1434 e_fmr->hwpage_size = save_fmr.hwpage_size;
1435 e_fmr->fmr_page_size = save_fmr.fmr_page_size;
1436 e_fmr->fmr_max_pages = save_fmr.fmr_max_pages;
1437 e_fmr->fmr_max_maps = save_fmr.fmr_max_maps;
1438 e_fmr->fmr_map_cnt = save_fmr.fmr_map_cnt;
1439 e_fmr->acl = save_fmr.acl;
1441 memset(&pginfo, 0, sizeof(pginfo));
1442 pginfo.type = EHCA_MR_PGI_FMR;
1443 ret = ehca_reg_mr(shca, e_fmr, NULL,
1444 (e_fmr->fmr_max_pages * e_fmr->fmr_page_size),
1445 e_fmr->acl, e_pd, &pginfo, &tmp_lkey,
1448 u32 offset = (u64)(&e_fmr->flags) - (u64)e_fmr;
1449 memcpy(&e_fmr->flags, &(save_mr.flags),
1450 sizeof(struct ehca_mr) - offset);
1453 ehca_unmap_one_fmr_exit0:
1455 ehca_err(&shca->ib_device, "ret=%i tmp_lkey=%x tmp_rkey=%x "
1457 ret, tmp_lkey, tmp_rkey, e_fmr->fmr_max_pages);
1459 } /* end ehca_unmap_one_fmr() */
1461 /*----------------------------------------------------------------------*/
1463 int ehca_reg_smr(struct ehca_shca *shca,
1464 struct ehca_mr *e_origmr,
1465 struct ehca_mr *e_newmr,
1468 struct ehca_pd *e_pd,
1475 struct ehca_mr_hipzout_parms hipzout;
1477 ehca_mrmw_map_acl(acl, &hipz_acl);
1478 ehca_mrmw_set_pgsize_hipz_acl(e_origmr->hwpage_size, &hipz_acl);
1480 h_ret = hipz_h_register_smr(shca->ipz_hca_handle, e_newmr, e_origmr,
1481 (u64)iova_start, hipz_acl, e_pd->fw_pd,
1483 if (h_ret != H_SUCCESS) {
1484 ehca_err(&shca->ib_device, "hipz_reg_smr failed, h_ret=%li "
1485 "shca=%p e_origmr=%p e_newmr=%p iova_start=%p acl=%x "
1486 "e_pd=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1487 h_ret, shca, e_origmr, e_newmr, iova_start, acl, e_pd,
1488 shca->ipz_hca_handle.handle,
1489 e_origmr->ipz_mr_handle.handle,
1490 e_origmr->ib.ib_mr.lkey);
1491 ret = ehca2ib_return_code(h_ret);
1492 goto ehca_reg_smr_exit0;
1494 /* successful registration */
1495 e_newmr->num_kpages = e_origmr->num_kpages;
1496 e_newmr->num_hwpages = e_origmr->num_hwpages;
1497 e_newmr->hwpage_size = e_origmr->hwpage_size;
1498 e_newmr->start = iova_start;
1499 e_newmr->size = e_origmr->size;
1501 e_newmr->ipz_mr_handle = hipzout.handle;
1502 *lkey = hipzout.lkey;
1503 *rkey = hipzout.rkey;
1508 ehca_err(&shca->ib_device, "ret=%i shca=%p e_origmr=%p "
1509 "e_newmr=%p iova_start=%p acl=%x e_pd=%p",
1510 ret, shca, e_origmr, e_newmr, iova_start, acl, e_pd);
1512 } /* end ehca_reg_smr() */
1514 /*----------------------------------------------------------------------*/
1516 /* register internal max-MR to internal SHCA */
1517 int ehca_reg_internal_maxmr(
1518 struct ehca_shca *shca,
1519 struct ehca_pd *e_pd,
1520 struct ehca_mr **e_maxmr) /*OUT*/
1523 struct ehca_mr *e_mr;
1526 struct ehca_mr_pginfo pginfo;
1527 struct ib_phys_buf ib_pbuf;
1532 e_mr = ehca_mr_new();
1534 ehca_err(&shca->ib_device, "out of memory");
1536 goto ehca_reg_internal_maxmr_exit0;
1538 e_mr->flags |= EHCA_MR_FLAG_MAXMR;
1540 /* register internal max-MR on HCA */
1541 size_maxmr = (u64)high_memory - PAGE_OFFSET;
1542 iova_start = (u64 *)KERNELBASE;
1544 ib_pbuf.size = size_maxmr;
1545 num_kpages = NUM_CHUNKS(((u64)iova_start % PAGE_SIZE) + size_maxmr,
1547 hw_pgsize = ehca_get_max_hwpage_size(shca);
1548 num_hwpages = NUM_CHUNKS(((u64)iova_start % hw_pgsize) + size_maxmr,
1551 memset(&pginfo, 0, sizeof(pginfo));
1552 pginfo.type = EHCA_MR_PGI_PHYS;
1553 pginfo.num_kpages = num_kpages;
1554 pginfo.num_hwpages = num_hwpages;
1555 pginfo.hwpage_size = hw_pgsize;
1556 pginfo.u.phy.num_phys_buf = 1;
1557 pginfo.u.phy.phys_buf_array = &ib_pbuf;
1559 ret = ehca_reg_mr(shca, e_mr, iova_start, size_maxmr, 0, e_pd,
1560 &pginfo, &e_mr->ib.ib_mr.lkey,
1561 &e_mr->ib.ib_mr.rkey);
1563 ehca_err(&shca->ib_device, "reg of internal max MR failed, "
1564 "e_mr=%p iova_start=%p size_maxmr=%lx num_kpages=%x "
1565 "num_hwpages=%x", e_mr, iova_start, size_maxmr,
1566 num_kpages, num_hwpages);
1567 goto ehca_reg_internal_maxmr_exit1;
1570 /* successful registration of all pages */
1571 e_mr->ib.ib_mr.device = e_pd->ib_pd.device;
1572 e_mr->ib.ib_mr.pd = &e_pd->ib_pd;
1573 e_mr->ib.ib_mr.uobject = NULL;
1574 atomic_inc(&(e_pd->ib_pd.usecnt));
1575 atomic_set(&(e_mr->ib.ib_mr.usecnt), 0);
1579 ehca_reg_internal_maxmr_exit1:
1580 ehca_mr_delete(e_mr);
1581 ehca_reg_internal_maxmr_exit0:
1583 ehca_err(&shca->ib_device, "ret=%i shca=%p e_pd=%p e_maxmr=%p",
1584 ret, shca, e_pd, e_maxmr);
1586 } /* end ehca_reg_internal_maxmr() */
1588 /*----------------------------------------------------------------------*/
1590 int ehca_reg_maxmr(struct ehca_shca *shca,
1591 struct ehca_mr *e_newmr,
1594 struct ehca_pd *e_pd,
1599 struct ehca_mr *e_origmr = shca->maxmr;
1601 struct ehca_mr_hipzout_parms hipzout;
1603 ehca_mrmw_map_acl(acl, &hipz_acl);
1604 ehca_mrmw_set_pgsize_hipz_acl(e_origmr->hwpage_size, &hipz_acl);
1606 h_ret = hipz_h_register_smr(shca->ipz_hca_handle, e_newmr, e_origmr,
1607 (u64)iova_start, hipz_acl, e_pd->fw_pd,
1609 if (h_ret != H_SUCCESS) {
1610 ehca_err(&shca->ib_device, "hipz_reg_smr failed, h_ret=%li "
1611 "e_origmr=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1612 h_ret, e_origmr, shca->ipz_hca_handle.handle,
1613 e_origmr->ipz_mr_handle.handle,
1614 e_origmr->ib.ib_mr.lkey);
1615 return ehca2ib_return_code(h_ret);
1617 /* successful registration */
1618 e_newmr->num_kpages = e_origmr->num_kpages;
1619 e_newmr->num_hwpages = e_origmr->num_hwpages;
1620 e_newmr->hwpage_size = e_origmr->hwpage_size;
1621 e_newmr->start = iova_start;
1622 e_newmr->size = e_origmr->size;
1624 e_newmr->ipz_mr_handle = hipzout.handle;
1625 *lkey = hipzout.lkey;
1626 *rkey = hipzout.rkey;
1628 } /* end ehca_reg_maxmr() */
1630 /*----------------------------------------------------------------------*/
1632 int ehca_dereg_internal_maxmr(struct ehca_shca *shca)
1635 struct ehca_mr *e_maxmr;
1636 struct ib_pd *ib_pd;
1639 ehca_err(&shca->ib_device, "bad call, shca=%p", shca);
1641 goto ehca_dereg_internal_maxmr_exit0;
1644 e_maxmr = shca->maxmr;
1645 ib_pd = e_maxmr->ib.ib_mr.pd;
1646 shca->maxmr = NULL; /* remove internal max-MR indication from SHCA */
1648 ret = ehca_dereg_mr(&e_maxmr->ib.ib_mr);
1650 ehca_err(&shca->ib_device, "dereg internal max-MR failed, "
1651 "ret=%i e_maxmr=%p shca=%p lkey=%x",
1652 ret, e_maxmr, shca, e_maxmr->ib.ib_mr.lkey);
1653 shca->maxmr = e_maxmr;
1654 goto ehca_dereg_internal_maxmr_exit0;
1657 atomic_dec(&ib_pd->usecnt);
1659 ehca_dereg_internal_maxmr_exit0:
1661 ehca_err(&shca->ib_device, "ret=%i shca=%p shca->maxmr=%p",
1662 ret, shca, shca->maxmr);
1664 } /* end ehca_dereg_internal_maxmr() */
1666 /*----------------------------------------------------------------------*/
1669 * check physical buffer array of MR verbs for validness and
1670 * calculates MR size
1672 int ehca_mr_chk_buf_and_calc_size(struct ib_phys_buf *phys_buf_array,
1677 struct ib_phys_buf *pbuf = phys_buf_array;
1681 if (num_phys_buf == 0) {
1682 ehca_gen_err("bad phys buf array len, num_phys_buf=0");
1685 /* check first buffer */
1686 if (((u64)iova_start & ~PAGE_MASK) != (pbuf->addr & ~PAGE_MASK)) {
1687 ehca_gen_err("iova_start/addr mismatch, iova_start=%p "
1688 "pbuf->addr=%lx pbuf->size=%lx",
1689 iova_start, pbuf->addr, pbuf->size);
1692 if (((pbuf->addr + pbuf->size) % PAGE_SIZE) &&
1693 (num_phys_buf > 1)) {
1694 ehca_gen_err("addr/size mismatch in 1st buf, pbuf->addr=%lx "
1695 "pbuf->size=%lx", pbuf->addr, pbuf->size);
1699 for (i = 0; i < num_phys_buf; i++) {
1700 if ((i > 0) && (pbuf->addr % PAGE_SIZE)) {
1701 ehca_gen_err("bad address, i=%x pbuf->addr=%lx "
1703 i, pbuf->addr, pbuf->size);
1706 if (((i > 0) && /* not 1st */
1707 (i < (num_phys_buf - 1)) && /* not last */
1708 (pbuf->size % PAGE_SIZE)) || (pbuf->size == 0)) {
1709 ehca_gen_err("bad size, i=%x pbuf->size=%lx",
1713 size_count += pbuf->size;
1719 } /* end ehca_mr_chk_buf_and_calc_size() */
1721 /*----------------------------------------------------------------------*/
1723 /* check page list of map FMR verb for validness */
1724 int ehca_fmr_check_page_list(struct ehca_mr *e_fmr,
1731 if ((list_len == 0) || (list_len > e_fmr->fmr_max_pages)) {
1732 ehca_gen_err("bad list_len, list_len=%x "
1733 "e_fmr->fmr_max_pages=%x fmr=%p",
1734 list_len, e_fmr->fmr_max_pages, e_fmr);
1738 /* each page must be aligned */
1740 for (i = 0; i < list_len; i++) {
1741 if (*page % e_fmr->fmr_page_size) {
1742 ehca_gen_err("bad page, i=%x *page=%lx page=%p fmr=%p "
1743 "fmr_page_size=%x", i, *page, page, e_fmr,
1744 e_fmr->fmr_page_size);
1751 } /* end ehca_fmr_check_page_list() */
1753 /*----------------------------------------------------------------------*/
1755 /* PAGE_SIZE >= pginfo->hwpage_size */
1756 static int ehca_set_pagebuf_user1(struct ehca_mr_pginfo *pginfo,
1761 struct ib_umem_chunk *prev_chunk;
1762 struct ib_umem_chunk *chunk;
1766 int hwpages_per_kpage = PAGE_SIZE / pginfo->hwpage_size;
1768 /* loop over desired chunk entries */
1769 chunk = pginfo->u.usr.next_chunk;
1770 prev_chunk = pginfo->u.usr.next_chunk;
1771 list_for_each_entry_continue(
1772 chunk, (&(pginfo->u.usr.region->chunk_list)), list) {
1773 for (i = pginfo->u.usr.next_nmap; i < chunk->nmap; ) {
1774 pgaddr = page_to_pfn(chunk->page_list[i].page)
1776 *kpage = phys_to_abs(pgaddr +
1777 (pginfo->next_hwpage *
1778 pginfo->hwpage_size));
1780 ehca_gen_err("pgaddr=%lx "
1781 "chunk->page_list[i]=%lx "
1782 "i=%x next_hwpage=%lx",
1783 pgaddr, (u64)sg_dma_address(
1784 &chunk->page_list[i]),
1785 i, pginfo->next_hwpage);
1788 (pginfo->hwpage_cnt)++;
1789 (pginfo->next_hwpage)++;
1791 if (pginfo->next_hwpage % hwpages_per_kpage == 0) {
1792 (pginfo->kpage_cnt)++;
1793 (pginfo->u.usr.next_nmap)++;
1794 pginfo->next_hwpage = 0;
1798 if (j >= number) break;
1800 if ((pginfo->u.usr.next_nmap >= chunk->nmap) &&
1802 pginfo->u.usr.next_nmap = 0;
1805 } else if (pginfo->u.usr.next_nmap >= chunk->nmap) {
1806 pginfo->u.usr.next_nmap = 0;
1808 } else if (j >= number)
1813 pginfo->u.usr.next_chunk =
1814 list_prepare_entry(prev_chunk,
1815 (&(pginfo->u.usr.region->chunk_list)),
1821 * check given pages for contiguous layout
1822 * last page addr is returned in prev_pgaddr for further check
1824 static int ehca_check_kpages_per_ate(struct scatterlist *page_list,
1825 int start_idx, int end_idx,
1829 for (t = start_idx; t <= end_idx; t++) {
1830 u64 pgaddr = page_to_pfn(page_list[t].page) << PAGE_SHIFT;
1831 ehca_gen_dbg("chunk_page=%lx value=%016lx", pgaddr,
1832 *(u64 *)abs_to_virt(phys_to_abs(pgaddr)));
1833 if (pgaddr - PAGE_SIZE != *prev_pgaddr) {
1834 ehca_gen_err("uncontiguous page found pgaddr=%lx "
1835 "prev_pgaddr=%lx page_list_i=%x",
1836 pgaddr, *prev_pgaddr, t);
1839 *prev_pgaddr = pgaddr;
1844 /* PAGE_SIZE < pginfo->hwpage_size */
1845 static int ehca_set_pagebuf_user2(struct ehca_mr_pginfo *pginfo,
1850 struct ib_umem_chunk *prev_chunk;
1851 struct ib_umem_chunk *chunk;
1852 u64 pgaddr, prev_pgaddr;
1855 int kpages_per_hwpage = pginfo->hwpage_size / PAGE_SIZE;
1856 int nr_kpages = kpages_per_hwpage;
1858 /* loop over desired chunk entries */
1859 chunk = pginfo->u.usr.next_chunk;
1860 prev_chunk = pginfo->u.usr.next_chunk;
1861 list_for_each_entry_continue(
1862 chunk, (&(pginfo->u.usr.region->chunk_list)), list) {
1863 for (i = pginfo->u.usr.next_nmap; i < chunk->nmap; ) {
1864 if (nr_kpages == kpages_per_hwpage) {
1865 pgaddr = ( page_to_pfn(chunk->page_list[i].page)
1867 *kpage = phys_to_abs(pgaddr);
1869 ehca_gen_err("pgaddr=%lx i=%x",
1875 * The first page in a hwpage must be aligned;
1876 * the first MR page is exempt from this rule.
1878 if (pgaddr & (pginfo->hwpage_size - 1)) {
1879 if (pginfo->hwpage_cnt) {
1881 "invalid alignment "
1885 pginfo->hwpage_size);
1892 (pginfo->hwpage_size - 1)) >>
1894 nr_kpages -= pginfo->kpage_cnt;
1895 *kpage = phys_to_abs(
1897 ~(pginfo->hwpage_size - 1));
1899 ehca_gen_dbg("kpage=%lx chunk_page=%lx "
1900 "value=%016lx", *kpage, pgaddr,
1901 *(u64 *)abs_to_virt(
1902 phys_to_abs(pgaddr)));
1903 prev_pgaddr = pgaddr;
1905 pginfo->kpage_cnt++;
1906 pginfo->u.usr.next_nmap++;
1912 if (i + nr_kpages > chunk->nmap) {
1913 ret = ehca_check_kpages_per_ate(
1914 chunk->page_list, i,
1915 chunk->nmap - 1, &prev_pgaddr);
1916 if (ret) return ret;
1917 pginfo->kpage_cnt += chunk->nmap - i;
1918 pginfo->u.usr.next_nmap += chunk->nmap - i;
1919 nr_kpages -= chunk->nmap - i;
1923 ret = ehca_check_kpages_per_ate(chunk->page_list, i,
1926 if (ret) return ret;
1928 pginfo->kpage_cnt += nr_kpages;
1929 pginfo->u.usr.next_nmap += nr_kpages;
1931 nr_kpages = kpages_per_hwpage;
1932 (pginfo->hwpage_cnt)++;
1935 if (j >= number) break;
1937 if ((pginfo->u.usr.next_nmap >= chunk->nmap) &&
1939 pginfo->u.usr.next_nmap = 0;
1942 } else if (pginfo->u.usr.next_nmap >= chunk->nmap) {
1943 pginfo->u.usr.next_nmap = 0;
1945 } else if (j >= number)
1950 pginfo->u.usr.next_chunk =
1951 list_prepare_entry(prev_chunk,
1952 (&(pginfo->u.usr.region->chunk_list)),
1957 int ehca_set_pagebuf_phys(struct ehca_mr_pginfo *pginfo,
1962 struct ib_phys_buf *pbuf;
1963 u64 num_hw, offs_hw;
1966 /* loop over desired phys_buf_array entries */
1967 while (i < number) {
1968 pbuf = pginfo->u.phy.phys_buf_array + pginfo->u.phy.next_buf;
1969 num_hw = NUM_CHUNKS((pbuf->addr % pginfo->hwpage_size) +
1970 pbuf->size, pginfo->hwpage_size);
1971 offs_hw = (pbuf->addr & ~(pginfo->hwpage_size - 1)) /
1972 pginfo->hwpage_size;
1973 while (pginfo->next_hwpage < offs_hw + num_hw) {
1975 if ((pginfo->kpage_cnt >= pginfo->num_kpages) ||
1976 (pginfo->hwpage_cnt >= pginfo->num_hwpages)) {
1977 ehca_gen_err("kpage_cnt >= num_kpages, "
1978 "kpage_cnt=%lx num_kpages=%lx "
1980 "num_hwpages=%lx i=%x",
1984 pginfo->num_hwpages, i);
1987 *kpage = phys_to_abs(
1988 (pbuf->addr & ~(pginfo->hwpage_size - 1)) +
1989 (pginfo->next_hwpage * pginfo->hwpage_size));
1990 if ( !(*kpage) && pbuf->addr ) {
1991 ehca_gen_err("pbuf->addr=%lx pbuf->size=%lx "
1992 "next_hwpage=%lx", pbuf->addr,
1993 pbuf->size, pginfo->next_hwpage);
1996 (pginfo->hwpage_cnt)++;
1997 (pginfo->next_hwpage)++;
1998 if (PAGE_SIZE >= pginfo->hwpage_size) {
1999 if (pginfo->next_hwpage %
2000 (PAGE_SIZE / pginfo->hwpage_size) == 0)
2001 (pginfo->kpage_cnt)++;
2003 pginfo->kpage_cnt += pginfo->hwpage_size /
2007 if (i >= number) break;
2009 if (pginfo->next_hwpage >= offs_hw + num_hw) {
2010 (pginfo->u.phy.next_buf)++;
2011 pginfo->next_hwpage = 0;
2017 int ehca_set_pagebuf_fmr(struct ehca_mr_pginfo *pginfo,
2025 /* loop over desired page_list entries */
2026 fmrlist = pginfo->u.fmr.page_list + pginfo->u.fmr.next_listelem;
2027 for (i = 0; i < number; i++) {
2028 *kpage = phys_to_abs((*fmrlist & ~(pginfo->hwpage_size - 1)) +
2029 pginfo->next_hwpage * pginfo->hwpage_size);
2031 ehca_gen_err("*fmrlist=%lx fmrlist=%p "
2032 "next_listelem=%lx next_hwpage=%lx",
2034 pginfo->u.fmr.next_listelem,
2035 pginfo->next_hwpage);
2038 (pginfo->hwpage_cnt)++;
2039 if (pginfo->u.fmr.fmr_pgsize >= pginfo->hwpage_size) {
2040 if (pginfo->next_hwpage %
2041 (pginfo->u.fmr.fmr_pgsize /
2042 pginfo->hwpage_size) == 0) {
2043 (pginfo->kpage_cnt)++;
2044 (pginfo->u.fmr.next_listelem)++;
2046 pginfo->next_hwpage = 0;
2048 (pginfo->next_hwpage)++;
2050 unsigned int cnt_per_hwpage = pginfo->hwpage_size /
2051 pginfo->u.fmr.fmr_pgsize;
2054 /* check if adrs are contiguous */
2055 for (j = 1; j < cnt_per_hwpage; j++) {
2056 u64 p = phys_to_abs(fmrlist[j] &
2057 ~(pginfo->hwpage_size - 1));
2058 if (prev + pginfo->u.fmr.fmr_pgsize != p) {
2059 ehca_gen_err("uncontiguous fmr pages "
2060 "found prev=%lx p=%lx "
2061 "idx=%x", prev, p, i + j);
2066 pginfo->kpage_cnt += cnt_per_hwpage;
2067 pginfo->u.fmr.next_listelem += cnt_per_hwpage;
2068 fmrlist += cnt_per_hwpage;
2075 /* setup page buffer from page info */
2076 int ehca_set_pagebuf(struct ehca_mr_pginfo *pginfo,
2082 switch (pginfo->type) {
2083 case EHCA_MR_PGI_PHYS:
2084 ret = ehca_set_pagebuf_phys(pginfo, number, kpage);
2086 case EHCA_MR_PGI_USER:
2087 ret = PAGE_SIZE >= pginfo->hwpage_size ?
2088 ehca_set_pagebuf_user1(pginfo, number, kpage) :
2089 ehca_set_pagebuf_user2(pginfo, number, kpage);
2091 case EHCA_MR_PGI_FMR:
2092 ret = ehca_set_pagebuf_fmr(pginfo, number, kpage);
2095 ehca_gen_err("bad pginfo->type=%x", pginfo->type);
2100 } /* end ehca_set_pagebuf() */
2102 /*----------------------------------------------------------------------*/
2105 * check MR if it is a max-MR, i.e. uses whole memory
2106 * in case it's a max-MR 1 is returned, else 0
2108 int ehca_mr_is_maxmr(u64 size,
2111 /* a MR is treated as max-MR only if it fits following: */
2112 if ((size == ((u64)high_memory - PAGE_OFFSET)) &&
2113 (iova_start == (void *)KERNELBASE)) {
2114 ehca_gen_dbg("this is a max-MR");
2118 } /* end ehca_mr_is_maxmr() */
2120 /*----------------------------------------------------------------------*/
2122 /* map access control for MR/MW. This routine is used for MR and MW. */
2123 void ehca_mrmw_map_acl(int ib_acl,
2127 if (ib_acl & IB_ACCESS_REMOTE_READ)
2128 *hipz_acl |= HIPZ_ACCESSCTRL_R_READ;
2129 if (ib_acl & IB_ACCESS_REMOTE_WRITE)
2130 *hipz_acl |= HIPZ_ACCESSCTRL_R_WRITE;
2131 if (ib_acl & IB_ACCESS_REMOTE_ATOMIC)
2132 *hipz_acl |= HIPZ_ACCESSCTRL_R_ATOMIC;
2133 if (ib_acl & IB_ACCESS_LOCAL_WRITE)
2134 *hipz_acl |= HIPZ_ACCESSCTRL_L_WRITE;
2135 if (ib_acl & IB_ACCESS_MW_BIND)
2136 *hipz_acl |= HIPZ_ACCESSCTRL_MW_BIND;
2137 } /* end ehca_mrmw_map_acl() */
2139 /*----------------------------------------------------------------------*/
2141 /* sets page size in hipz access control for MR/MW. */
2142 void ehca_mrmw_set_pgsize_hipz_acl(u32 pgsize, u32 *hipz_acl) /*INOUT*/
2144 *hipz_acl |= (ehca_encode_hwpage_size(pgsize) << 24);
2145 } /* end ehca_mrmw_set_pgsize_hipz_acl() */
2147 /*----------------------------------------------------------------------*/
2150 * reverse map access control for MR/MW.
2151 * This routine is used for MR and MW.
2153 void ehca_mrmw_reverse_map_acl(const u32 *hipz_acl,
2154 int *ib_acl) /*OUT*/
2157 if (*hipz_acl & HIPZ_ACCESSCTRL_R_READ)
2158 *ib_acl |= IB_ACCESS_REMOTE_READ;
2159 if (*hipz_acl & HIPZ_ACCESSCTRL_R_WRITE)
2160 *ib_acl |= IB_ACCESS_REMOTE_WRITE;
2161 if (*hipz_acl & HIPZ_ACCESSCTRL_R_ATOMIC)
2162 *ib_acl |= IB_ACCESS_REMOTE_ATOMIC;
2163 if (*hipz_acl & HIPZ_ACCESSCTRL_L_WRITE)
2164 *ib_acl |= IB_ACCESS_LOCAL_WRITE;
2165 if (*hipz_acl & HIPZ_ACCESSCTRL_MW_BIND)
2166 *ib_acl |= IB_ACCESS_MW_BIND;
2167 } /* end ehca_mrmw_reverse_map_acl() */
2170 /*----------------------------------------------------------------------*/
2173 * MR destructor and constructor
2174 * used in Reregister MR verb, sets all fields in ehca_mr_t to 0,
2175 * except struct ib_mr and spinlock
2177 void ehca_mr_deletenew(struct ehca_mr *mr)
2181 mr->num_hwpages = 0;
2184 mr->fmr_page_size = 0;
2185 mr->fmr_max_pages = 0;
2186 mr->fmr_max_maps = 0;
2187 mr->fmr_map_cnt = 0;
2188 memset(&mr->ipz_mr_handle, 0, sizeof(mr->ipz_mr_handle));
2189 memset(&mr->galpas, 0, sizeof(mr->galpas));
2190 } /* end ehca_mr_deletenew() */
2192 int ehca_init_mrmw_cache(void)
2194 mr_cache = kmem_cache_create("ehca_cache_mr",
2195 sizeof(struct ehca_mr), 0,
2200 mw_cache = kmem_cache_create("ehca_cache_mw",
2201 sizeof(struct ehca_mw), 0,
2205 kmem_cache_destroy(mr_cache);
2212 void ehca_cleanup_mrmw_cache(void)
2215 kmem_cache_destroy(mr_cache);
2217 kmem_cache_destroy(mw_cache);