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))
55 /* max number of rpages (per hcall register_rpages) */
56 #define MAX_RPAGES 512
58 static struct kmem_cache
*mr_cache
;
59 static struct kmem_cache
*mw_cache
;
62 EHCA_MR_PGSIZE4K
= 0x1000L
,
63 EHCA_MR_PGSIZE64K
= 0x10000L
,
64 EHCA_MR_PGSIZE1M
= 0x100000L
,
65 EHCA_MR_PGSIZE16M
= 0x1000000L
68 #define EHCA_MR_PGSHIFT4K 12
69 #define EHCA_MR_PGSHIFT64K 16
70 #define EHCA_MR_PGSHIFT1M 20
71 #define EHCA_MR_PGSHIFT16M 24
73 static u32
ehca_encode_hwpage_size(u32 pgsize
)
78 * map mr page size into hw code:
79 * 0, 1, 2, 3 for 4K, 64K, 1M, 64M
81 while (!(pgsize
& 1)) {
88 static u64
ehca_get_max_hwpage_size(struct ehca_shca
*shca
)
90 if (shca
->hca_cap_mr_pgsize
& HCA_CAP_MR_PGSIZE_16M
)
91 return EHCA_MR_PGSIZE16M
;
92 return EHCA_MR_PGSIZE4K
;
95 static struct ehca_mr
*ehca_mr_new(void)
99 me
= kmem_cache_zalloc(mr_cache
, GFP_KERNEL
);
101 spin_lock_init(&me
->mrlock
);
103 ehca_gen_err("alloc failed");
108 static void ehca_mr_delete(struct ehca_mr
*me
)
110 kmem_cache_free(mr_cache
, me
);
113 static struct ehca_mw
*ehca_mw_new(void)
117 me
= kmem_cache_zalloc(mw_cache
, GFP_KERNEL
);
119 spin_lock_init(&me
->mwlock
);
121 ehca_gen_err("alloc failed");
126 static void ehca_mw_delete(struct ehca_mw
*me
)
128 kmem_cache_free(mw_cache
, me
);
131 /*----------------------------------------------------------------------*/
133 struct ib_mr
*ehca_get_dma_mr(struct ib_pd
*pd
, int mr_access_flags
)
137 struct ehca_mr
*e_maxmr
;
138 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
139 struct ehca_shca
*shca
=
140 container_of(pd
->device
, struct ehca_shca
, ib_device
);
143 e_maxmr
= ehca_mr_new();
145 ehca_err(&shca
->ib_device
, "out of memory");
146 ib_mr
= ERR_PTR(-ENOMEM
);
147 goto get_dma_mr_exit0
;
150 ret
= ehca_reg_maxmr(shca
, e_maxmr
, (u64
*)KERNELBASE
,
151 mr_access_flags
, e_pd
,
152 &e_maxmr
->ib
.ib_mr
.lkey
,
153 &e_maxmr
->ib
.ib_mr
.rkey
);
155 ehca_mr_delete(e_maxmr
);
156 ib_mr
= ERR_PTR(ret
);
157 goto get_dma_mr_exit0
;
159 ib_mr
= &e_maxmr
->ib
.ib_mr
;
161 ehca_err(&shca
->ib_device
, "no internal max-MR exist!");
162 ib_mr
= ERR_PTR(-EINVAL
);
163 goto get_dma_mr_exit0
;
168 ehca_err(&shca
->ib_device
, "h_ret=%li pd=%p mr_access_flags=%x",
169 PTR_ERR(ib_mr
), pd
, mr_access_flags
);
171 } /* end ehca_get_dma_mr() */
173 /*----------------------------------------------------------------------*/
175 struct ib_mr
*ehca_reg_phys_mr(struct ib_pd
*pd
,
176 struct ib_phys_buf
*phys_buf_array
,
183 struct ehca_mr
*e_mr
;
184 struct ehca_shca
*shca
=
185 container_of(pd
->device
, struct ehca_shca
, ib_device
);
186 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
190 if ((num_phys_buf
<= 0) || !phys_buf_array
) {
191 ehca_err(pd
->device
, "bad input values: num_phys_buf=%x "
192 "phys_buf_array=%p", num_phys_buf
, phys_buf_array
);
193 ib_mr
= ERR_PTR(-EINVAL
);
194 goto reg_phys_mr_exit0
;
196 if (((mr_access_flags
& IB_ACCESS_REMOTE_WRITE
) &&
197 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)) ||
198 ((mr_access_flags
& IB_ACCESS_REMOTE_ATOMIC
) &&
199 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
))) {
201 * Remote Write Access requires Local Write Access
202 * Remote Atomic Access requires Local Write Access
204 ehca_err(pd
->device
, "bad input values: mr_access_flags=%x",
206 ib_mr
= ERR_PTR(-EINVAL
);
207 goto reg_phys_mr_exit0
;
210 /* check physical buffer list and calculate size */
211 ret
= ehca_mr_chk_buf_and_calc_size(phys_buf_array
, num_phys_buf
,
214 ib_mr
= ERR_PTR(ret
);
215 goto reg_phys_mr_exit0
;
218 (((u64
)iova_start
+ size
) < (u64
)iova_start
)) {
219 ehca_err(pd
->device
, "bad input values: size=%lx iova_start=%p",
221 ib_mr
= ERR_PTR(-EINVAL
);
222 goto reg_phys_mr_exit0
;
225 e_mr
= ehca_mr_new();
227 ehca_err(pd
->device
, "out of memory");
228 ib_mr
= ERR_PTR(-ENOMEM
);
229 goto reg_phys_mr_exit0
;
232 /* register MR on HCA */
233 if (ehca_mr_is_maxmr(size
, iova_start
)) {
234 e_mr
->flags
|= EHCA_MR_FLAG_MAXMR
;
235 ret
= ehca_reg_maxmr(shca
, e_mr
, iova_start
, mr_access_flags
,
236 e_pd
, &e_mr
->ib
.ib_mr
.lkey
,
237 &e_mr
->ib
.ib_mr
.rkey
);
239 ib_mr
= ERR_PTR(ret
);
240 goto reg_phys_mr_exit1
;
243 struct ehca_mr_pginfo pginfo
;
248 num_kpages
= NUM_CHUNKS(((u64
)iova_start
% PAGE_SIZE
) + size
,
250 /* for kernel space we try most possible pgsize */
251 hw_pgsize
= ehca_get_max_hwpage_size(shca
);
252 num_hwpages
= NUM_CHUNKS(((u64
)iova_start
% hw_pgsize
) + size
,
254 memset(&pginfo
, 0, sizeof(pginfo
));
255 pginfo
.type
= EHCA_MR_PGI_PHYS
;
256 pginfo
.num_kpages
= num_kpages
;
257 pginfo
.hwpage_size
= hw_pgsize
;
258 pginfo
.num_hwpages
= num_hwpages
;
259 pginfo
.u
.phy
.num_phys_buf
= num_phys_buf
;
260 pginfo
.u
.phy
.phys_buf_array
= phys_buf_array
;
262 ((u64
)iova_start
& ~(hw_pgsize
- 1)) / hw_pgsize
;
264 ret
= ehca_reg_mr(shca
, e_mr
, iova_start
, size
, mr_access_flags
,
265 e_pd
, &pginfo
, &e_mr
->ib
.ib_mr
.lkey
,
266 &e_mr
->ib
.ib_mr
.rkey
);
268 ib_mr
= ERR_PTR(ret
);
269 goto reg_phys_mr_exit1
;
273 /* successful registration of all pages */
274 return &e_mr
->ib
.ib_mr
;
277 ehca_mr_delete(e_mr
);
280 ehca_err(pd
->device
, "h_ret=%li pd=%p phys_buf_array=%p "
281 "num_phys_buf=%x mr_access_flags=%x iova_start=%p",
282 PTR_ERR(ib_mr
), pd
, phys_buf_array
,
283 num_phys_buf
, mr_access_flags
, iova_start
);
285 } /* end ehca_reg_phys_mr() */
287 /*----------------------------------------------------------------------*/
289 struct ib_mr
*ehca_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 length
,
290 u64 virt
, int mr_access_flags
,
291 struct ib_udata
*udata
)
294 struct ehca_mr
*e_mr
;
295 struct ehca_shca
*shca
=
296 container_of(pd
->device
, struct ehca_shca
, ib_device
);
297 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
298 struct ehca_mr_pginfo pginfo
;
305 ehca_gen_err("bad pd=%p", pd
);
306 return ERR_PTR(-EFAULT
);
309 if (((mr_access_flags
& IB_ACCESS_REMOTE_WRITE
) &&
310 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)) ||
311 ((mr_access_flags
& IB_ACCESS_REMOTE_ATOMIC
) &&
312 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
))) {
314 * Remote Write Access requires Local Write Access
315 * Remote Atomic Access requires Local Write Access
317 ehca_err(pd
->device
, "bad input values: mr_access_flags=%x",
319 ib_mr
= ERR_PTR(-EINVAL
);
320 goto reg_user_mr_exit0
;
323 if (length
== 0 || virt
+ length
< virt
) {
324 ehca_err(pd
->device
, "bad input values: length=%lx "
325 "virt_base=%lx", length
, virt
);
326 ib_mr
= ERR_PTR(-EINVAL
);
327 goto reg_user_mr_exit0
;
330 e_mr
= ehca_mr_new();
332 ehca_err(pd
->device
, "out of memory");
333 ib_mr
= ERR_PTR(-ENOMEM
);
334 goto reg_user_mr_exit0
;
337 e_mr
->umem
= ib_umem_get(pd
->uobject
->context
, start
, length
,
339 if (IS_ERR(e_mr
->umem
)) {
340 ib_mr
= (void *)e_mr
->umem
;
341 goto reg_user_mr_exit1
;
344 if (e_mr
->umem
->page_size
!= PAGE_SIZE
) {
345 ehca_err(pd
->device
, "page size not supported, "
346 "e_mr->umem->page_size=%x", e_mr
->umem
->page_size
);
347 ib_mr
= ERR_PTR(-EINVAL
);
348 goto reg_user_mr_exit2
;
351 /* determine number of MR pages */
352 num_kpages
= NUM_CHUNKS((virt
% PAGE_SIZE
) + length
, PAGE_SIZE
);
353 /* select proper hw_pgsize */
354 if (ehca_mr_largepage
&&
355 (shca
->hca_cap_mr_pgsize
& HCA_CAP_MR_PGSIZE_16M
)) {
356 int page_shift
= PAGE_SHIFT
;
357 if (e_mr
->umem
->hugetlb
) {
358 /* determine page_shift, clamp between 4K and 16M */
359 page_shift
= (fls64(length
- 1) + 3) & ~3;
360 page_shift
= min(max(page_shift
, EHCA_MR_PGSHIFT4K
),
363 hwpage_size
= 1UL << page_shift
;
365 hwpage_size
= EHCA_MR_PGSIZE4K
; /* ehca1 only supports 4k */
366 ehca_dbg(pd
->device
, "hwpage_size=%lx", hwpage_size
);
368 reg_user_mr_fallback
:
369 num_hwpages
= NUM_CHUNKS((virt
% hwpage_size
) + length
, hwpage_size
);
370 /* register MR on HCA */
371 memset(&pginfo
, 0, sizeof(pginfo
));
372 pginfo
.type
= EHCA_MR_PGI_USER
;
373 pginfo
.hwpage_size
= hwpage_size
;
374 pginfo
.num_kpages
= num_kpages
;
375 pginfo
.num_hwpages
= num_hwpages
;
376 pginfo
.u
.usr
.region
= e_mr
->umem
;
377 pginfo
.next_hwpage
= e_mr
->umem
->offset
/ hwpage_size
;
378 pginfo
.u
.usr
.next_chunk
= list_prepare_entry(pginfo
.u
.usr
.next_chunk
,
379 (&e_mr
->umem
->chunk_list
),
382 ret
= ehca_reg_mr(shca
, e_mr
, (u64
*)virt
, length
, mr_access_flags
,
383 e_pd
, &pginfo
, &e_mr
->ib
.ib_mr
.lkey
,
384 &e_mr
->ib
.ib_mr
.rkey
);
385 if (ret
== -EINVAL
&& pginfo
.hwpage_size
> PAGE_SIZE
) {
386 ehca_warn(pd
->device
, "failed to register mr "
387 "with hwpage_size=%lx", hwpage_size
);
388 ehca_info(pd
->device
, "try to register mr with "
389 "kpage_size=%lx", PAGE_SIZE
);
391 * this means kpages are not contiguous for a hw page
392 * try kernel page size as fallback solution
394 hwpage_size
= PAGE_SIZE
;
395 goto reg_user_mr_fallback
;
398 ib_mr
= ERR_PTR(ret
);
399 goto reg_user_mr_exit2
;
402 /* successful registration of all pages */
403 return &e_mr
->ib
.ib_mr
;
406 ib_umem_release(e_mr
->umem
);
408 ehca_mr_delete(e_mr
);
411 ehca_err(pd
->device
, "rc=%li pd=%p mr_access_flags=%x udata=%p",
412 PTR_ERR(ib_mr
), pd
, mr_access_flags
, udata
);
414 } /* end ehca_reg_user_mr() */
416 /*----------------------------------------------------------------------*/
418 int ehca_rereg_phys_mr(struct ib_mr
*mr
,
421 struct ib_phys_buf
*phys_buf_array
,
428 struct ehca_shca
*shca
=
429 container_of(mr
->device
, struct ehca_shca
, ib_device
);
430 struct ehca_mr
*e_mr
= container_of(mr
, struct ehca_mr
, ib
.ib_mr
);
431 struct ehca_pd
*my_pd
= container_of(mr
->pd
, struct ehca_pd
, ib_pd
);
435 struct ehca_pd
*new_pd
;
436 u32 tmp_lkey
, tmp_rkey
;
437 unsigned long sl_flags
;
440 struct ehca_mr_pginfo pginfo
;
441 u32 cur_pid
= current
->tgid
;
443 if (my_pd
->ib_pd
.uobject
&& my_pd
->ib_pd
.uobject
->context
&&
444 (my_pd
->ownpid
!= cur_pid
)) {
445 ehca_err(mr
->device
, "Invalid caller pid=%x ownpid=%x",
446 cur_pid
, my_pd
->ownpid
);
448 goto rereg_phys_mr_exit0
;
451 if (!(mr_rereg_mask
& IB_MR_REREG_TRANS
)) {
452 /* TODO not supported, because PHYP rereg hCall needs pages */
453 ehca_err(mr
->device
, "rereg without IB_MR_REREG_TRANS not "
454 "supported yet, mr_rereg_mask=%x", mr_rereg_mask
);
456 goto rereg_phys_mr_exit0
;
459 if (mr_rereg_mask
& IB_MR_REREG_PD
) {
461 ehca_err(mr
->device
, "rereg with bad pd, pd=%p "
462 "mr_rereg_mask=%x", pd
, mr_rereg_mask
);
464 goto rereg_phys_mr_exit0
;
469 ~(IB_MR_REREG_TRANS
| IB_MR_REREG_PD
| IB_MR_REREG_ACCESS
)) ||
470 (mr_rereg_mask
== 0)) {
472 goto rereg_phys_mr_exit0
;
475 /* check other parameters */
476 if (e_mr
== shca
->maxmr
) {
477 /* should be impossible, however reject to be sure */
478 ehca_err(mr
->device
, "rereg internal max-MR impossible, mr=%p "
479 "shca->maxmr=%p mr->lkey=%x",
480 mr
, shca
->maxmr
, mr
->lkey
);
482 goto rereg_phys_mr_exit0
;
484 if (mr_rereg_mask
& IB_MR_REREG_TRANS
) { /* transl., i.e. addr/size */
485 if (e_mr
->flags
& EHCA_MR_FLAG_FMR
) {
486 ehca_err(mr
->device
, "not supported for FMR, mr=%p "
487 "flags=%x", mr
, e_mr
->flags
);
489 goto rereg_phys_mr_exit0
;
491 if (!phys_buf_array
|| num_phys_buf
<= 0) {
492 ehca_err(mr
->device
, "bad input values mr_rereg_mask=%x"
493 " phys_buf_array=%p num_phys_buf=%x",
494 mr_rereg_mask
, phys_buf_array
, num_phys_buf
);
496 goto rereg_phys_mr_exit0
;
499 if ((mr_rereg_mask
& IB_MR_REREG_ACCESS
) && /* change ACL */
500 (((mr_access_flags
& IB_ACCESS_REMOTE_WRITE
) &&
501 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)) ||
502 ((mr_access_flags
& IB_ACCESS_REMOTE_ATOMIC
) &&
503 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)))) {
505 * Remote Write Access requires Local Write Access
506 * Remote Atomic Access requires Local Write Access
508 ehca_err(mr
->device
, "bad input values: mr_rereg_mask=%x "
509 "mr_access_flags=%x", mr_rereg_mask
, mr_access_flags
);
511 goto rereg_phys_mr_exit0
;
514 /* set requested values dependent on rereg request */
515 spin_lock_irqsave(&e_mr
->mrlock
, sl_flags
);
516 new_start
= e_mr
->start
;
517 new_size
= e_mr
->size
;
519 new_pd
= container_of(mr
->pd
, struct ehca_pd
, ib_pd
);
521 if (mr_rereg_mask
& IB_MR_REREG_TRANS
) {
522 u64 hw_pgsize
= ehca_get_max_hwpage_size(shca
);
524 new_start
= iova_start
; /* change address */
525 /* check physical buffer list and calculate size */
526 ret
= ehca_mr_chk_buf_and_calc_size(phys_buf_array
,
527 num_phys_buf
, iova_start
,
530 goto rereg_phys_mr_exit1
;
531 if ((new_size
== 0) ||
532 (((u64
)iova_start
+ new_size
) < (u64
)iova_start
)) {
533 ehca_err(mr
->device
, "bad input values: new_size=%lx "
534 "iova_start=%p", new_size
, iova_start
);
536 goto rereg_phys_mr_exit1
;
538 num_kpages
= NUM_CHUNKS(((u64
)new_start
% PAGE_SIZE
) +
539 new_size
, PAGE_SIZE
);
540 num_hwpages
= NUM_CHUNKS(((u64
)new_start
% hw_pgsize
) +
541 new_size
, hw_pgsize
);
542 memset(&pginfo
, 0, sizeof(pginfo
));
543 pginfo
.type
= EHCA_MR_PGI_PHYS
;
544 pginfo
.num_kpages
= num_kpages
;
545 pginfo
.hwpage_size
= hw_pgsize
;
546 pginfo
.num_hwpages
= num_hwpages
;
547 pginfo
.u
.phy
.num_phys_buf
= num_phys_buf
;
548 pginfo
.u
.phy
.phys_buf_array
= phys_buf_array
;
550 ((u64
)iova_start
& ~(hw_pgsize
- 1)) / hw_pgsize
;
552 if (mr_rereg_mask
& IB_MR_REREG_ACCESS
)
553 new_acl
= mr_access_flags
;
554 if (mr_rereg_mask
& IB_MR_REREG_PD
)
555 new_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
557 ret
= ehca_rereg_mr(shca
, e_mr
, new_start
, new_size
, new_acl
,
558 new_pd
, &pginfo
, &tmp_lkey
, &tmp_rkey
);
560 goto rereg_phys_mr_exit1
;
562 /* successful reregistration */
563 if (mr_rereg_mask
& IB_MR_REREG_PD
)
569 spin_unlock_irqrestore(&e_mr
->mrlock
, sl_flags
);
572 ehca_err(mr
->device
, "ret=%i mr=%p mr_rereg_mask=%x pd=%p "
573 "phys_buf_array=%p num_phys_buf=%x mr_access_flags=%x "
575 ret
, mr
, mr_rereg_mask
, pd
, phys_buf_array
,
576 num_phys_buf
, mr_access_flags
, iova_start
);
578 } /* end ehca_rereg_phys_mr() */
580 /*----------------------------------------------------------------------*/
582 int ehca_query_mr(struct ib_mr
*mr
, struct ib_mr_attr
*mr_attr
)
586 struct ehca_shca
*shca
=
587 container_of(mr
->device
, struct ehca_shca
, ib_device
);
588 struct ehca_mr
*e_mr
= container_of(mr
, struct ehca_mr
, ib
.ib_mr
);
589 struct ehca_pd
*my_pd
= container_of(mr
->pd
, struct ehca_pd
, ib_pd
);
590 u32 cur_pid
= current
->tgid
;
591 unsigned long sl_flags
;
592 struct ehca_mr_hipzout_parms hipzout
;
594 if (my_pd
->ib_pd
.uobject
&& my_pd
->ib_pd
.uobject
->context
&&
595 (my_pd
->ownpid
!= cur_pid
)) {
596 ehca_err(mr
->device
, "Invalid caller pid=%x ownpid=%x",
597 cur_pid
, my_pd
->ownpid
);
602 if ((e_mr
->flags
& EHCA_MR_FLAG_FMR
)) {
603 ehca_err(mr
->device
, "not supported for FMR, mr=%p e_mr=%p "
604 "e_mr->flags=%x", mr
, e_mr
, e_mr
->flags
);
609 memset(mr_attr
, 0, sizeof(struct ib_mr_attr
));
610 spin_lock_irqsave(&e_mr
->mrlock
, sl_flags
);
612 h_ret
= hipz_h_query_mr(shca
->ipz_hca_handle
, e_mr
, &hipzout
);
613 if (h_ret
!= H_SUCCESS
) {
614 ehca_err(mr
->device
, "hipz_mr_query failed, h_ret=%li mr=%p "
615 "hca_hndl=%lx mr_hndl=%lx lkey=%x",
616 h_ret
, mr
, shca
->ipz_hca_handle
.handle
,
617 e_mr
->ipz_mr_handle
.handle
, mr
->lkey
);
618 ret
= ehca2ib_return_code(h_ret
);
621 mr_attr
->pd
= mr
->pd
;
622 mr_attr
->device_virt_addr
= hipzout
.vaddr
;
623 mr_attr
->size
= hipzout
.len
;
624 mr_attr
->lkey
= hipzout
.lkey
;
625 mr_attr
->rkey
= hipzout
.rkey
;
626 ehca_mrmw_reverse_map_acl(&hipzout
.acl
, &mr_attr
->mr_access_flags
);
629 spin_unlock_irqrestore(&e_mr
->mrlock
, sl_flags
);
632 ehca_err(mr
->device
, "ret=%i mr=%p mr_attr=%p",
635 } /* end ehca_query_mr() */
637 /*----------------------------------------------------------------------*/
639 int ehca_dereg_mr(struct ib_mr
*mr
)
643 struct ehca_shca
*shca
=
644 container_of(mr
->device
, struct ehca_shca
, ib_device
);
645 struct ehca_mr
*e_mr
= container_of(mr
, struct ehca_mr
, ib
.ib_mr
);
646 struct ehca_pd
*my_pd
= container_of(mr
->pd
, struct ehca_pd
, ib_pd
);
647 u32 cur_pid
= current
->tgid
;
649 if (my_pd
->ib_pd
.uobject
&& my_pd
->ib_pd
.uobject
->context
&&
650 (my_pd
->ownpid
!= cur_pid
)) {
651 ehca_err(mr
->device
, "Invalid caller pid=%x ownpid=%x",
652 cur_pid
, my_pd
->ownpid
);
657 if ((e_mr
->flags
& EHCA_MR_FLAG_FMR
)) {
658 ehca_err(mr
->device
, "not supported for FMR, mr=%p e_mr=%p "
659 "e_mr->flags=%x", mr
, e_mr
, e_mr
->flags
);
662 } else if (e_mr
== shca
->maxmr
) {
663 /* should be impossible, however reject to be sure */
664 ehca_err(mr
->device
, "dereg internal max-MR impossible, mr=%p "
665 "shca->maxmr=%p mr->lkey=%x",
666 mr
, shca
->maxmr
, mr
->lkey
);
671 /* TODO: BUSY: MR still has bound window(s) */
672 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_mr
);
673 if (h_ret
!= H_SUCCESS
) {
674 ehca_err(mr
->device
, "hipz_free_mr failed, h_ret=%li shca=%p "
675 "e_mr=%p hca_hndl=%lx mr_hndl=%lx mr->lkey=%x",
676 h_ret
, shca
, e_mr
, shca
->ipz_hca_handle
.handle
,
677 e_mr
->ipz_mr_handle
.handle
, mr
->lkey
);
678 ret
= ehca2ib_return_code(h_ret
);
683 ib_umem_release(e_mr
->umem
);
685 /* successful deregistration */
686 ehca_mr_delete(e_mr
);
690 ehca_err(mr
->device
, "ret=%i mr=%p", ret
, mr
);
692 } /* end ehca_dereg_mr() */
694 /*----------------------------------------------------------------------*/
696 struct ib_mw
*ehca_alloc_mw(struct ib_pd
*pd
)
700 struct ehca_mw
*e_mw
;
701 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
702 struct ehca_shca
*shca
=
703 container_of(pd
->device
, struct ehca_shca
, ib_device
);
704 struct ehca_mw_hipzout_parms hipzout
;
706 e_mw
= ehca_mw_new();
708 ib_mw
= ERR_PTR(-ENOMEM
);
712 h_ret
= hipz_h_alloc_resource_mw(shca
->ipz_hca_handle
, e_mw
,
713 e_pd
->fw_pd
, &hipzout
);
714 if (h_ret
!= H_SUCCESS
) {
715 ehca_err(pd
->device
, "hipz_mw_allocate failed, h_ret=%li "
716 "shca=%p hca_hndl=%lx mw=%p",
717 h_ret
, shca
, shca
->ipz_hca_handle
.handle
, e_mw
);
718 ib_mw
= ERR_PTR(ehca2ib_return_code(h_ret
));
721 /* successful MW allocation */
722 e_mw
->ipz_mw_handle
= hipzout
.handle
;
723 e_mw
->ib_mw
.rkey
= hipzout
.rkey
;
727 ehca_mw_delete(e_mw
);
730 ehca_err(pd
->device
, "h_ret=%li pd=%p", PTR_ERR(ib_mw
), pd
);
732 } /* end ehca_alloc_mw() */
734 /*----------------------------------------------------------------------*/
736 int ehca_bind_mw(struct ib_qp
*qp
,
738 struct ib_mw_bind
*mw_bind
)
740 /* TODO: not supported up to now */
741 ehca_gen_err("bind MW currently not supported by HCAD");
744 } /* end ehca_bind_mw() */
746 /*----------------------------------------------------------------------*/
748 int ehca_dealloc_mw(struct ib_mw
*mw
)
751 struct ehca_shca
*shca
=
752 container_of(mw
->device
, struct ehca_shca
, ib_device
);
753 struct ehca_mw
*e_mw
= container_of(mw
, struct ehca_mw
, ib_mw
);
755 h_ret
= hipz_h_free_resource_mw(shca
->ipz_hca_handle
, e_mw
);
756 if (h_ret
!= H_SUCCESS
) {
757 ehca_err(mw
->device
, "hipz_free_mw failed, h_ret=%li shca=%p "
758 "mw=%p rkey=%x hca_hndl=%lx mw_hndl=%lx",
759 h_ret
, shca
, mw
, mw
->rkey
, shca
->ipz_hca_handle
.handle
,
760 e_mw
->ipz_mw_handle
.handle
);
761 return ehca2ib_return_code(h_ret
);
763 /* successful deallocation */
764 ehca_mw_delete(e_mw
);
766 } /* end ehca_dealloc_mw() */
768 /*----------------------------------------------------------------------*/
770 struct ib_fmr
*ehca_alloc_fmr(struct ib_pd
*pd
,
772 struct ib_fmr_attr
*fmr_attr
)
774 struct ib_fmr
*ib_fmr
;
775 struct ehca_shca
*shca
=
776 container_of(pd
->device
, struct ehca_shca
, ib_device
);
777 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
778 struct ehca_mr
*e_fmr
;
780 u32 tmp_lkey
, tmp_rkey
;
781 struct ehca_mr_pginfo pginfo
;
784 /* check other parameters */
785 if (((mr_access_flags
& IB_ACCESS_REMOTE_WRITE
) &&
786 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)) ||
787 ((mr_access_flags
& IB_ACCESS_REMOTE_ATOMIC
) &&
788 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
))) {
790 * Remote Write Access requires Local Write Access
791 * Remote Atomic Access requires Local Write Access
793 ehca_err(pd
->device
, "bad input values: mr_access_flags=%x",
795 ib_fmr
= ERR_PTR(-EINVAL
);
796 goto alloc_fmr_exit0
;
798 if (mr_access_flags
& IB_ACCESS_MW_BIND
) {
799 ehca_err(pd
->device
, "bad input values: mr_access_flags=%x",
801 ib_fmr
= ERR_PTR(-EINVAL
);
802 goto alloc_fmr_exit0
;
804 if ((fmr_attr
->max_pages
== 0) || (fmr_attr
->max_maps
== 0)) {
805 ehca_err(pd
->device
, "bad input values: fmr_attr->max_pages=%x "
806 "fmr_attr->max_maps=%x fmr_attr->page_shift=%x",
807 fmr_attr
->max_pages
, fmr_attr
->max_maps
,
808 fmr_attr
->page_shift
);
809 ib_fmr
= ERR_PTR(-EINVAL
);
810 goto alloc_fmr_exit0
;
812 hw_pgsize
= ehca_get_max_hwpage_size(shca
);
813 if ((1 << fmr_attr
->page_shift
) != hw_pgsize
) {
814 ehca_err(pd
->device
, "unsupported fmr_attr->page_shift=%x",
815 fmr_attr
->page_shift
);
816 ib_fmr
= ERR_PTR(-EINVAL
);
817 goto alloc_fmr_exit0
;
820 e_fmr
= ehca_mr_new();
822 ib_fmr
= ERR_PTR(-ENOMEM
);
823 goto alloc_fmr_exit0
;
825 e_fmr
->flags
|= EHCA_MR_FLAG_FMR
;
827 /* register MR on HCA */
828 memset(&pginfo
, 0, sizeof(pginfo
));
830 * pginfo.num_hwpages==0, ie register_rpages() will not be called
831 * but deferred to map_phys_fmr()
833 ret
= ehca_reg_mr(shca
, e_fmr
, NULL
,
834 fmr_attr
->max_pages
* (1 << fmr_attr
->page_shift
),
835 mr_access_flags
, e_pd
, &pginfo
,
836 &tmp_lkey
, &tmp_rkey
);
838 ib_fmr
= ERR_PTR(ret
);
839 goto alloc_fmr_exit1
;
843 e_fmr
->hwpage_size
= hw_pgsize
;
844 e_fmr
->fmr_page_size
= 1 << fmr_attr
->page_shift
;
845 e_fmr
->fmr_max_pages
= fmr_attr
->max_pages
;
846 e_fmr
->fmr_max_maps
= fmr_attr
->max_maps
;
847 e_fmr
->fmr_map_cnt
= 0;
848 return &e_fmr
->ib
.ib_fmr
;
851 ehca_mr_delete(e_fmr
);
854 } /* end ehca_alloc_fmr() */
856 /*----------------------------------------------------------------------*/
858 int ehca_map_phys_fmr(struct ib_fmr
*fmr
,
864 struct ehca_shca
*shca
=
865 container_of(fmr
->device
, struct ehca_shca
, ib_device
);
866 struct ehca_mr
*e_fmr
= container_of(fmr
, struct ehca_mr
, ib
.ib_fmr
);
867 struct ehca_pd
*e_pd
= container_of(fmr
->pd
, struct ehca_pd
, ib_pd
);
868 struct ehca_mr_pginfo pginfo
;
869 u32 tmp_lkey
, tmp_rkey
;
871 if (!(e_fmr
->flags
& EHCA_MR_FLAG_FMR
)) {
872 ehca_err(fmr
->device
, "not a FMR, e_fmr=%p e_fmr->flags=%x",
873 e_fmr
, e_fmr
->flags
);
875 goto map_phys_fmr_exit0
;
877 ret
= ehca_fmr_check_page_list(e_fmr
, page_list
, list_len
);
879 goto map_phys_fmr_exit0
;
880 if (iova
% e_fmr
->fmr_page_size
) {
881 /* only whole-numbered pages */
882 ehca_err(fmr
->device
, "bad iova, iova=%lx fmr_page_size=%x",
883 iova
, e_fmr
->fmr_page_size
);
885 goto map_phys_fmr_exit0
;
887 if (e_fmr
->fmr_map_cnt
>= e_fmr
->fmr_max_maps
) {
888 /* HCAD does not limit the maps, however trace this anyway */
889 ehca_info(fmr
->device
, "map limit exceeded, fmr=%p "
890 "e_fmr->fmr_map_cnt=%x e_fmr->fmr_max_maps=%x",
891 fmr
, e_fmr
->fmr_map_cnt
, e_fmr
->fmr_max_maps
);
894 memset(&pginfo
, 0, sizeof(pginfo
));
895 pginfo
.type
= EHCA_MR_PGI_FMR
;
896 pginfo
.num_kpages
= list_len
;
897 pginfo
.hwpage_size
= e_fmr
->hwpage_size
;
899 list_len
* e_fmr
->fmr_page_size
/ pginfo
.hwpage_size
;
900 pginfo
.u
.fmr
.page_list
= page_list
;
902 (iova
& (e_fmr
->fmr_page_size
-1)) / pginfo
.hwpage_size
;
903 pginfo
.u
.fmr
.fmr_pgsize
= e_fmr
->fmr_page_size
;
905 ret
= ehca_rereg_mr(shca
, e_fmr
, (u64
*)iova
,
906 list_len
* e_fmr
->fmr_page_size
,
907 e_fmr
->acl
, e_pd
, &pginfo
, &tmp_lkey
, &tmp_rkey
);
909 goto map_phys_fmr_exit0
;
911 /* successful reregistration */
912 e_fmr
->fmr_map_cnt
++;
913 e_fmr
->ib
.ib_fmr
.lkey
= tmp_lkey
;
914 e_fmr
->ib
.ib_fmr
.rkey
= tmp_rkey
;
919 ehca_err(fmr
->device
, "ret=%i fmr=%p page_list=%p list_len=%x "
920 "iova=%lx", ret
, fmr
, page_list
, list_len
, iova
);
922 } /* end ehca_map_phys_fmr() */
924 /*----------------------------------------------------------------------*/
926 int ehca_unmap_fmr(struct list_head
*fmr_list
)
929 struct ib_fmr
*ib_fmr
;
930 struct ehca_shca
*shca
= NULL
;
931 struct ehca_shca
*prev_shca
;
932 struct ehca_mr
*e_fmr
;
934 u32 unmap_fmr_cnt
= 0;
936 /* check all FMR belong to same SHCA, and check internal flag */
937 list_for_each_entry(ib_fmr
, fmr_list
, list
) {
940 ehca_gen_err("bad fmr=%p in list", ib_fmr
);
942 goto unmap_fmr_exit0
;
944 shca
= container_of(ib_fmr
->device
, struct ehca_shca
,
946 e_fmr
= container_of(ib_fmr
, struct ehca_mr
, ib
.ib_fmr
);
947 if ((shca
!= prev_shca
) && prev_shca
) {
948 ehca_err(&shca
->ib_device
, "SHCA mismatch, shca=%p "
949 "prev_shca=%p e_fmr=%p",
950 shca
, prev_shca
, e_fmr
);
952 goto unmap_fmr_exit0
;
954 if (!(e_fmr
->flags
& EHCA_MR_FLAG_FMR
)) {
955 ehca_err(&shca
->ib_device
, "not a FMR, e_fmr=%p "
956 "e_fmr->flags=%x", e_fmr
, e_fmr
->flags
);
958 goto unmap_fmr_exit0
;
963 /* loop over all FMRs to unmap */
964 list_for_each_entry(ib_fmr
, fmr_list
, list
) {
966 e_fmr
= container_of(ib_fmr
, struct ehca_mr
, ib
.ib_fmr
);
967 shca
= container_of(ib_fmr
->device
, struct ehca_shca
,
969 ret
= ehca_unmap_one_fmr(shca
, e_fmr
);
971 /* unmap failed, stop unmapping of rest of FMRs */
972 ehca_err(&shca
->ib_device
, "unmap of one FMR failed, "
973 "stop rest, e_fmr=%p num_fmr=%x "
974 "unmap_fmr_cnt=%x lkey=%x", e_fmr
, num_fmr
,
975 unmap_fmr_cnt
, e_fmr
->ib
.ib_fmr
.lkey
);
976 goto unmap_fmr_exit0
;
982 ehca_gen_err("ret=%i fmr_list=%p num_fmr=%x unmap_fmr_cnt=%x",
983 ret
, fmr_list
, num_fmr
, unmap_fmr_cnt
);
985 } /* end ehca_unmap_fmr() */
987 /*----------------------------------------------------------------------*/
989 int ehca_dealloc_fmr(struct ib_fmr
*fmr
)
993 struct ehca_shca
*shca
=
994 container_of(fmr
->device
, struct ehca_shca
, ib_device
);
995 struct ehca_mr
*e_fmr
= container_of(fmr
, struct ehca_mr
, ib
.ib_fmr
);
997 if (!(e_fmr
->flags
& EHCA_MR_FLAG_FMR
)) {
998 ehca_err(fmr
->device
, "not a FMR, e_fmr=%p e_fmr->flags=%x",
999 e_fmr
, e_fmr
->flags
);
1001 goto free_fmr_exit0
;
1004 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_fmr
);
1005 if (h_ret
!= H_SUCCESS
) {
1006 ehca_err(fmr
->device
, "hipz_free_mr failed, h_ret=%li e_fmr=%p "
1007 "hca_hndl=%lx fmr_hndl=%lx fmr->lkey=%x",
1008 h_ret
, e_fmr
, shca
->ipz_hca_handle
.handle
,
1009 e_fmr
->ipz_mr_handle
.handle
, fmr
->lkey
);
1010 ret
= ehca2ib_return_code(h_ret
);
1011 goto free_fmr_exit0
;
1013 /* successful deregistration */
1014 ehca_mr_delete(e_fmr
);
1019 ehca_err(&shca
->ib_device
, "ret=%i fmr=%p", ret
, fmr
);
1021 } /* end ehca_dealloc_fmr() */
1023 /*----------------------------------------------------------------------*/
1025 int ehca_reg_mr(struct ehca_shca
*shca
,
1026 struct ehca_mr
*e_mr
,
1030 struct ehca_pd
*e_pd
,
1031 struct ehca_mr_pginfo
*pginfo
,
1038 struct ehca_mr_hipzout_parms hipzout
;
1040 ehca_mrmw_map_acl(acl
, &hipz_acl
);
1041 ehca_mrmw_set_pgsize_hipz_acl(pginfo
->hwpage_size
, &hipz_acl
);
1042 if (ehca_use_hp_mr
== 1)
1043 hipz_acl
|= 0x00000001;
1045 h_ret
= hipz_h_alloc_resource_mr(shca
->ipz_hca_handle
, e_mr
,
1046 (u64
)iova_start
, size
, hipz_acl
,
1047 e_pd
->fw_pd
, &hipzout
);
1048 if (h_ret
!= H_SUCCESS
) {
1049 ehca_err(&shca
->ib_device
, "hipz_alloc_mr failed, h_ret=%li "
1050 "hca_hndl=%lx", h_ret
, shca
->ipz_hca_handle
.handle
);
1051 ret
= ehca2ib_return_code(h_ret
);
1052 goto ehca_reg_mr_exit0
;
1055 e_mr
->ipz_mr_handle
= hipzout
.handle
;
1057 ret
= ehca_reg_mr_rpages(shca
, e_mr
, pginfo
);
1059 goto ehca_reg_mr_exit1
;
1061 /* successful registration */
1062 e_mr
->num_kpages
= pginfo
->num_kpages
;
1063 e_mr
->num_hwpages
= pginfo
->num_hwpages
;
1064 e_mr
->hwpage_size
= pginfo
->hwpage_size
;
1065 e_mr
->start
= iova_start
;
1068 *lkey
= hipzout
.lkey
;
1069 *rkey
= hipzout
.rkey
;
1073 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_mr
);
1074 if (h_ret
!= H_SUCCESS
) {
1075 ehca_err(&shca
->ib_device
, "h_ret=%li shca=%p e_mr=%p "
1076 "iova_start=%p size=%lx acl=%x e_pd=%p lkey=%x "
1077 "pginfo=%p num_kpages=%lx num_hwpages=%lx ret=%i",
1078 h_ret
, shca
, e_mr
, iova_start
, size
, acl
, e_pd
,
1079 hipzout
.lkey
, pginfo
, pginfo
->num_kpages
,
1080 pginfo
->num_hwpages
, ret
);
1081 ehca_err(&shca
->ib_device
, "internal error in ehca_reg_mr, "
1086 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_mr=%p "
1087 "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
1088 "num_kpages=%lx num_hwpages=%lx",
1089 ret
, shca
, e_mr
, iova_start
, size
, acl
, e_pd
, pginfo
,
1090 pginfo
->num_kpages
, pginfo
->num_hwpages
);
1092 } /* end ehca_reg_mr() */
1094 /*----------------------------------------------------------------------*/
1096 int ehca_reg_mr_rpages(struct ehca_shca
*shca
,
1097 struct ehca_mr
*e_mr
,
1098 struct ehca_mr_pginfo
*pginfo
)
1107 if (!pginfo
->num_hwpages
) /* in case of fmr */
1110 kpage
= ehca_alloc_fw_ctrlblock(GFP_KERNEL
);
1112 ehca_err(&shca
->ib_device
, "kpage alloc failed");
1114 goto ehca_reg_mr_rpages_exit0
;
1117 /* max MAX_RPAGES ehca mr pages per register call */
1118 for (i
= 0; i
< NUM_CHUNKS(pginfo
->num_hwpages
, MAX_RPAGES
); i
++) {
1120 if (i
== NUM_CHUNKS(pginfo
->num_hwpages
, MAX_RPAGES
) - 1) {
1121 rnum
= pginfo
->num_hwpages
% MAX_RPAGES
; /* last shot */
1123 rnum
= MAX_RPAGES
; /* last shot is full */
1127 ret
= ehca_set_pagebuf(pginfo
, rnum
, kpage
);
1129 ehca_err(&shca
->ib_device
, "ehca_set_pagebuf "
1130 "bad rc, ret=%i rnum=%x kpage=%p",
1132 goto ehca_reg_mr_rpages_exit1
;
1136 rpage
= virt_to_abs(kpage
);
1138 ehca_err(&shca
->ib_device
, "kpage=%p i=%x",
1141 goto ehca_reg_mr_rpages_exit1
;
1146 h_ret
= hipz_h_register_rpage_mr(
1147 shca
->ipz_hca_handle
, e_mr
,
1148 ehca_encode_hwpage_size(pginfo
->hwpage_size
),
1151 if (i
== NUM_CHUNKS(pginfo
->num_hwpages
, MAX_RPAGES
) - 1) {
1153 * check for 'registration complete'==H_SUCCESS
1154 * and for 'page registered'==H_PAGE_REGISTERED
1156 if (h_ret
!= H_SUCCESS
) {
1157 ehca_err(&shca
->ib_device
, "last "
1158 "hipz_reg_rpage_mr failed, h_ret=%li "
1159 "e_mr=%p i=%x hca_hndl=%lx mr_hndl=%lx"
1160 " lkey=%x", h_ret
, e_mr
, i
,
1161 shca
->ipz_hca_handle
.handle
,
1162 e_mr
->ipz_mr_handle
.handle
,
1163 e_mr
->ib
.ib_mr
.lkey
);
1164 ret
= ehca2ib_return_code(h_ret
);
1168 } else if (h_ret
!= H_PAGE_REGISTERED
) {
1169 ehca_err(&shca
->ib_device
, "hipz_reg_rpage_mr failed, "
1170 "h_ret=%li e_mr=%p i=%x lkey=%x hca_hndl=%lx "
1171 "mr_hndl=%lx", h_ret
, e_mr
, i
,
1172 e_mr
->ib
.ib_mr
.lkey
,
1173 shca
->ipz_hca_handle
.handle
,
1174 e_mr
->ipz_mr_handle
.handle
);
1175 ret
= ehca2ib_return_code(h_ret
);
1182 ehca_reg_mr_rpages_exit1
:
1183 ehca_free_fw_ctrlblock(kpage
);
1184 ehca_reg_mr_rpages_exit0
:
1186 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_mr=%p pginfo=%p "
1187 "num_kpages=%lx num_hwpages=%lx", ret
, shca
, e_mr
,
1188 pginfo
, pginfo
->num_kpages
, pginfo
->num_hwpages
);
1190 } /* end ehca_reg_mr_rpages() */
1192 /*----------------------------------------------------------------------*/
1194 inline int ehca_rereg_mr_rereg1(struct ehca_shca
*shca
,
1195 struct ehca_mr
*e_mr
,
1199 struct ehca_pd
*e_pd
,
1200 struct ehca_mr_pginfo
*pginfo
,
1209 struct ehca_mr_pginfo pginfo_save
;
1210 struct ehca_mr_hipzout_parms hipzout
;
1212 ehca_mrmw_map_acl(acl
, &hipz_acl
);
1213 ehca_mrmw_set_pgsize_hipz_acl(pginfo
->hwpage_size
, &hipz_acl
);
1215 kpage
= ehca_alloc_fw_ctrlblock(GFP_KERNEL
);
1217 ehca_err(&shca
->ib_device
, "kpage alloc failed");
1219 goto ehca_rereg_mr_rereg1_exit0
;
1222 pginfo_save
= *pginfo
;
1223 ret
= ehca_set_pagebuf(pginfo
, pginfo
->num_hwpages
, kpage
);
1225 ehca_err(&shca
->ib_device
, "set pagebuf failed, e_mr=%p "
1226 "pginfo=%p type=%x num_kpages=%lx num_hwpages=%lx "
1227 "kpage=%p", e_mr
, pginfo
, pginfo
->type
,
1228 pginfo
->num_kpages
, pginfo
->num_hwpages
, kpage
);
1229 goto ehca_rereg_mr_rereg1_exit1
;
1231 rpage
= virt_to_abs(kpage
);
1233 ehca_err(&shca
->ib_device
, "kpage=%p", kpage
);
1235 goto ehca_rereg_mr_rereg1_exit1
;
1237 h_ret
= hipz_h_reregister_pmr(shca
->ipz_hca_handle
, e_mr
,
1238 (u64
)iova_start
, size
, hipz_acl
,
1239 e_pd
->fw_pd
, rpage
, &hipzout
);
1240 if (h_ret
!= H_SUCCESS
) {
1242 * reregistration unsuccessful, try it again with the 3 hCalls,
1243 * e.g. this is required in case H_MR_CONDITION
1244 * (MW bound or MR is shared)
1246 ehca_warn(&shca
->ib_device
, "hipz_h_reregister_pmr failed "
1247 "(Rereg1), h_ret=%li e_mr=%p", h_ret
, e_mr
);
1248 *pginfo
= pginfo_save
;
1250 } else if ((u64
*)hipzout
.vaddr
!= iova_start
) {
1251 ehca_err(&shca
->ib_device
, "PHYP changed iova_start in "
1252 "rereg_pmr, iova_start=%p iova_start_out=%lx e_mr=%p "
1253 "mr_handle=%lx lkey=%x lkey_out=%x", iova_start
,
1254 hipzout
.vaddr
, e_mr
, e_mr
->ipz_mr_handle
.handle
,
1255 e_mr
->ib
.ib_mr
.lkey
, hipzout
.lkey
);
1259 * successful reregistration
1260 * note: start and start_out are identical for eServer HCAs
1262 e_mr
->num_kpages
= pginfo
->num_kpages
;
1263 e_mr
->num_hwpages
= pginfo
->num_hwpages
;
1264 e_mr
->hwpage_size
= pginfo
->hwpage_size
;
1265 e_mr
->start
= iova_start
;
1268 *lkey
= hipzout
.lkey
;
1269 *rkey
= hipzout
.rkey
;
1272 ehca_rereg_mr_rereg1_exit1
:
1273 ehca_free_fw_ctrlblock(kpage
);
1274 ehca_rereg_mr_rereg1_exit0
:
1275 if ( ret
&& (ret
!= -EAGAIN
) )
1276 ehca_err(&shca
->ib_device
, "ret=%i lkey=%x rkey=%x "
1277 "pginfo=%p num_kpages=%lx num_hwpages=%lx",
1278 ret
, *lkey
, *rkey
, pginfo
, pginfo
->num_kpages
,
1279 pginfo
->num_hwpages
);
1281 } /* end ehca_rereg_mr_rereg1() */
1283 /*----------------------------------------------------------------------*/
1285 int ehca_rereg_mr(struct ehca_shca
*shca
,
1286 struct ehca_mr
*e_mr
,
1290 struct ehca_pd
*e_pd
,
1291 struct ehca_mr_pginfo
*pginfo
,
1297 int rereg_1_hcall
= 1; /* 1: use hipz_h_reregister_pmr directly */
1298 int rereg_3_hcall
= 0; /* 1: use 3 hipz calls for reregistration */
1300 /* first determine reregistration hCall(s) */
1301 if ((pginfo
->num_hwpages
> MAX_RPAGES
) ||
1302 (e_mr
->num_hwpages
> MAX_RPAGES
) ||
1303 (pginfo
->num_hwpages
> e_mr
->num_hwpages
)) {
1304 ehca_dbg(&shca
->ib_device
, "Rereg3 case, "
1305 "pginfo->num_hwpages=%lx e_mr->num_hwpages=%x",
1306 pginfo
->num_hwpages
, e_mr
->num_hwpages
);
1311 if (e_mr
->flags
& EHCA_MR_FLAG_MAXMR
) { /* check for max-MR */
1314 e_mr
->flags
&= ~EHCA_MR_FLAG_MAXMR
;
1315 ehca_err(&shca
->ib_device
, "Rereg MR for max-MR! e_mr=%p",
1319 if (rereg_1_hcall
) {
1320 ret
= ehca_rereg_mr_rereg1(shca
, e_mr
, iova_start
, size
,
1321 acl
, e_pd
, pginfo
, lkey
, rkey
);
1326 goto ehca_rereg_mr_exit0
;
1330 if (rereg_3_hcall
) {
1331 struct ehca_mr save_mr
;
1333 /* first deregister old MR */
1334 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_mr
);
1335 if (h_ret
!= H_SUCCESS
) {
1336 ehca_err(&shca
->ib_device
, "hipz_free_mr failed, "
1337 "h_ret=%li e_mr=%p hca_hndl=%lx mr_hndl=%lx "
1339 h_ret
, e_mr
, shca
->ipz_hca_handle
.handle
,
1340 e_mr
->ipz_mr_handle
.handle
,
1341 e_mr
->ib
.ib_mr
.lkey
);
1342 ret
= ehca2ib_return_code(h_ret
);
1343 goto ehca_rereg_mr_exit0
;
1345 /* clean ehca_mr_t, without changing struct ib_mr and lock */
1347 ehca_mr_deletenew(e_mr
);
1349 /* set some MR values */
1350 e_mr
->flags
= save_mr
.flags
;
1351 e_mr
->hwpage_size
= save_mr
.hwpage_size
;
1352 e_mr
->fmr_page_size
= save_mr
.fmr_page_size
;
1353 e_mr
->fmr_max_pages
= save_mr
.fmr_max_pages
;
1354 e_mr
->fmr_max_maps
= save_mr
.fmr_max_maps
;
1355 e_mr
->fmr_map_cnt
= save_mr
.fmr_map_cnt
;
1357 ret
= ehca_reg_mr(shca
, e_mr
, iova_start
, size
, acl
,
1358 e_pd
, pginfo
, lkey
, rkey
);
1360 u32 offset
= (u64
)(&e_mr
->flags
) - (u64
)e_mr
;
1361 memcpy(&e_mr
->flags
, &(save_mr
.flags
),
1362 sizeof(struct ehca_mr
) - offset
);
1363 goto ehca_rereg_mr_exit0
;
1367 ehca_rereg_mr_exit0
:
1369 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_mr=%p "
1370 "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
1371 "num_kpages=%lx lkey=%x rkey=%x rereg_1_hcall=%x "
1372 "rereg_3_hcall=%x", ret
, shca
, e_mr
, iova_start
, size
,
1373 acl
, e_pd
, pginfo
, pginfo
->num_kpages
, *lkey
, *rkey
,
1374 rereg_1_hcall
, rereg_3_hcall
);
1376 } /* end ehca_rereg_mr() */
1378 /*----------------------------------------------------------------------*/
1380 int ehca_unmap_one_fmr(struct ehca_shca
*shca
,
1381 struct ehca_mr
*e_fmr
)
1385 struct ehca_pd
*e_pd
=
1386 container_of(e_fmr
->ib
.ib_fmr
.pd
, struct ehca_pd
, ib_pd
);
1387 struct ehca_mr save_fmr
;
1388 u32 tmp_lkey
, tmp_rkey
;
1389 struct ehca_mr_pginfo pginfo
;
1390 struct ehca_mr_hipzout_parms hipzout
;
1391 struct ehca_mr save_mr
;
1393 if (e_fmr
->fmr_max_pages
<= MAX_RPAGES
) {
1395 * note: after using rereg hcall with len=0,
1396 * rereg hcall must be used again for registering pages
1398 h_ret
= hipz_h_reregister_pmr(shca
->ipz_hca_handle
, e_fmr
, 0,
1399 0, 0, e_pd
->fw_pd
, 0, &hipzout
);
1400 if (h_ret
== H_SUCCESS
) {
1401 /* successful reregistration */
1402 e_fmr
->start
= NULL
;
1404 tmp_lkey
= hipzout
.lkey
;
1405 tmp_rkey
= hipzout
.rkey
;
1409 * should not happen, because length checked above,
1410 * FMRs are not shared and no MW bound to FMRs
1412 ehca_err(&shca
->ib_device
, "hipz_reregister_pmr failed "
1413 "(Rereg1), h_ret=%li e_fmr=%p hca_hndl=%lx "
1414 "mr_hndl=%lx lkey=%x lkey_out=%x",
1415 h_ret
, e_fmr
, shca
->ipz_hca_handle
.handle
,
1416 e_fmr
->ipz_mr_handle
.handle
,
1417 e_fmr
->ib
.ib_fmr
.lkey
, hipzout
.lkey
);
1418 /* try free and rereg */
1421 /* first free old FMR */
1422 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_fmr
);
1423 if (h_ret
!= H_SUCCESS
) {
1424 ehca_err(&shca
->ib_device
, "hipz_free_mr failed, "
1425 "h_ret=%li e_fmr=%p hca_hndl=%lx mr_hndl=%lx "
1427 h_ret
, e_fmr
, shca
->ipz_hca_handle
.handle
,
1428 e_fmr
->ipz_mr_handle
.handle
,
1429 e_fmr
->ib
.ib_fmr
.lkey
);
1430 ret
= ehca2ib_return_code(h_ret
);
1431 goto ehca_unmap_one_fmr_exit0
;
1433 /* clean ehca_mr_t, without changing lock */
1435 ehca_mr_deletenew(e_fmr
);
1437 /* set some MR values */
1438 e_fmr
->flags
= save_fmr
.flags
;
1439 e_fmr
->hwpage_size
= save_fmr
.hwpage_size
;
1440 e_fmr
->fmr_page_size
= save_fmr
.fmr_page_size
;
1441 e_fmr
->fmr_max_pages
= save_fmr
.fmr_max_pages
;
1442 e_fmr
->fmr_max_maps
= save_fmr
.fmr_max_maps
;
1443 e_fmr
->fmr_map_cnt
= save_fmr
.fmr_map_cnt
;
1444 e_fmr
->acl
= save_fmr
.acl
;
1446 memset(&pginfo
, 0, sizeof(pginfo
));
1447 pginfo
.type
= EHCA_MR_PGI_FMR
;
1448 ret
= ehca_reg_mr(shca
, e_fmr
, NULL
,
1449 (e_fmr
->fmr_max_pages
* e_fmr
->fmr_page_size
),
1450 e_fmr
->acl
, e_pd
, &pginfo
, &tmp_lkey
,
1453 u32 offset
= (u64
)(&e_fmr
->flags
) - (u64
)e_fmr
;
1454 memcpy(&e_fmr
->flags
, &(save_mr
.flags
),
1455 sizeof(struct ehca_mr
) - offset
);
1458 ehca_unmap_one_fmr_exit0
:
1460 ehca_err(&shca
->ib_device
, "ret=%i tmp_lkey=%x tmp_rkey=%x "
1462 ret
, tmp_lkey
, tmp_rkey
, e_fmr
->fmr_max_pages
);
1464 } /* end ehca_unmap_one_fmr() */
1466 /*----------------------------------------------------------------------*/
1468 int ehca_reg_smr(struct ehca_shca
*shca
,
1469 struct ehca_mr
*e_origmr
,
1470 struct ehca_mr
*e_newmr
,
1473 struct ehca_pd
*e_pd
,
1480 struct ehca_mr_hipzout_parms hipzout
;
1482 ehca_mrmw_map_acl(acl
, &hipz_acl
);
1483 ehca_mrmw_set_pgsize_hipz_acl(e_origmr
->hwpage_size
, &hipz_acl
);
1485 h_ret
= hipz_h_register_smr(shca
->ipz_hca_handle
, e_newmr
, e_origmr
,
1486 (u64
)iova_start
, hipz_acl
, e_pd
->fw_pd
,
1488 if (h_ret
!= H_SUCCESS
) {
1489 ehca_err(&shca
->ib_device
, "hipz_reg_smr failed, h_ret=%li "
1490 "shca=%p e_origmr=%p e_newmr=%p iova_start=%p acl=%x "
1491 "e_pd=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1492 h_ret
, shca
, e_origmr
, e_newmr
, iova_start
, acl
, e_pd
,
1493 shca
->ipz_hca_handle
.handle
,
1494 e_origmr
->ipz_mr_handle
.handle
,
1495 e_origmr
->ib
.ib_mr
.lkey
);
1496 ret
= ehca2ib_return_code(h_ret
);
1497 goto ehca_reg_smr_exit0
;
1499 /* successful registration */
1500 e_newmr
->num_kpages
= e_origmr
->num_kpages
;
1501 e_newmr
->num_hwpages
= e_origmr
->num_hwpages
;
1502 e_newmr
->hwpage_size
= e_origmr
->hwpage_size
;
1503 e_newmr
->start
= iova_start
;
1504 e_newmr
->size
= e_origmr
->size
;
1506 e_newmr
->ipz_mr_handle
= hipzout
.handle
;
1507 *lkey
= hipzout
.lkey
;
1508 *rkey
= hipzout
.rkey
;
1513 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_origmr=%p "
1514 "e_newmr=%p iova_start=%p acl=%x e_pd=%p",
1515 ret
, shca
, e_origmr
, e_newmr
, iova_start
, acl
, e_pd
);
1517 } /* end ehca_reg_smr() */
1519 /*----------------------------------------------------------------------*/
1521 /* register internal max-MR to internal SHCA */
1522 int ehca_reg_internal_maxmr(
1523 struct ehca_shca
*shca
,
1524 struct ehca_pd
*e_pd
,
1525 struct ehca_mr
**e_maxmr
) /*OUT*/
1528 struct ehca_mr
*e_mr
;
1531 struct ehca_mr_pginfo pginfo
;
1532 struct ib_phys_buf ib_pbuf
;
1537 e_mr
= ehca_mr_new();
1539 ehca_err(&shca
->ib_device
, "out of memory");
1541 goto ehca_reg_internal_maxmr_exit0
;
1543 e_mr
->flags
|= EHCA_MR_FLAG_MAXMR
;
1545 /* register internal max-MR on HCA */
1546 size_maxmr
= (u64
)high_memory
- PAGE_OFFSET
;
1547 iova_start
= (u64
*)KERNELBASE
;
1549 ib_pbuf
.size
= size_maxmr
;
1550 num_kpages
= NUM_CHUNKS(((u64
)iova_start
% PAGE_SIZE
) + size_maxmr
,
1552 hw_pgsize
= ehca_get_max_hwpage_size(shca
);
1553 num_hwpages
= NUM_CHUNKS(((u64
)iova_start
% hw_pgsize
) + size_maxmr
,
1556 memset(&pginfo
, 0, sizeof(pginfo
));
1557 pginfo
.type
= EHCA_MR_PGI_PHYS
;
1558 pginfo
.num_kpages
= num_kpages
;
1559 pginfo
.num_hwpages
= num_hwpages
;
1560 pginfo
.hwpage_size
= hw_pgsize
;
1561 pginfo
.u
.phy
.num_phys_buf
= 1;
1562 pginfo
.u
.phy
.phys_buf_array
= &ib_pbuf
;
1564 ret
= ehca_reg_mr(shca
, e_mr
, iova_start
, size_maxmr
, 0, e_pd
,
1565 &pginfo
, &e_mr
->ib
.ib_mr
.lkey
,
1566 &e_mr
->ib
.ib_mr
.rkey
);
1568 ehca_err(&shca
->ib_device
, "reg of internal max MR failed, "
1569 "e_mr=%p iova_start=%p size_maxmr=%lx num_kpages=%x "
1570 "num_hwpages=%x", e_mr
, iova_start
, size_maxmr
,
1571 num_kpages
, num_hwpages
);
1572 goto ehca_reg_internal_maxmr_exit1
;
1575 /* successful registration of all pages */
1576 e_mr
->ib
.ib_mr
.device
= e_pd
->ib_pd
.device
;
1577 e_mr
->ib
.ib_mr
.pd
= &e_pd
->ib_pd
;
1578 e_mr
->ib
.ib_mr
.uobject
= NULL
;
1579 atomic_inc(&(e_pd
->ib_pd
.usecnt
));
1580 atomic_set(&(e_mr
->ib
.ib_mr
.usecnt
), 0);
1584 ehca_reg_internal_maxmr_exit1
:
1585 ehca_mr_delete(e_mr
);
1586 ehca_reg_internal_maxmr_exit0
:
1588 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_pd=%p e_maxmr=%p",
1589 ret
, shca
, e_pd
, e_maxmr
);
1591 } /* end ehca_reg_internal_maxmr() */
1593 /*----------------------------------------------------------------------*/
1595 int ehca_reg_maxmr(struct ehca_shca
*shca
,
1596 struct ehca_mr
*e_newmr
,
1599 struct ehca_pd
*e_pd
,
1604 struct ehca_mr
*e_origmr
= shca
->maxmr
;
1606 struct ehca_mr_hipzout_parms hipzout
;
1608 ehca_mrmw_map_acl(acl
, &hipz_acl
);
1609 ehca_mrmw_set_pgsize_hipz_acl(e_origmr
->hwpage_size
, &hipz_acl
);
1611 h_ret
= hipz_h_register_smr(shca
->ipz_hca_handle
, e_newmr
, e_origmr
,
1612 (u64
)iova_start
, hipz_acl
, e_pd
->fw_pd
,
1614 if (h_ret
!= H_SUCCESS
) {
1615 ehca_err(&shca
->ib_device
, "hipz_reg_smr failed, h_ret=%li "
1616 "e_origmr=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1617 h_ret
, e_origmr
, shca
->ipz_hca_handle
.handle
,
1618 e_origmr
->ipz_mr_handle
.handle
,
1619 e_origmr
->ib
.ib_mr
.lkey
);
1620 return ehca2ib_return_code(h_ret
);
1622 /* successful registration */
1623 e_newmr
->num_kpages
= e_origmr
->num_kpages
;
1624 e_newmr
->num_hwpages
= e_origmr
->num_hwpages
;
1625 e_newmr
->hwpage_size
= e_origmr
->hwpage_size
;
1626 e_newmr
->start
= iova_start
;
1627 e_newmr
->size
= e_origmr
->size
;
1629 e_newmr
->ipz_mr_handle
= hipzout
.handle
;
1630 *lkey
= hipzout
.lkey
;
1631 *rkey
= hipzout
.rkey
;
1633 } /* end ehca_reg_maxmr() */
1635 /*----------------------------------------------------------------------*/
1637 int ehca_dereg_internal_maxmr(struct ehca_shca
*shca
)
1640 struct ehca_mr
*e_maxmr
;
1641 struct ib_pd
*ib_pd
;
1644 ehca_err(&shca
->ib_device
, "bad call, shca=%p", shca
);
1646 goto ehca_dereg_internal_maxmr_exit0
;
1649 e_maxmr
= shca
->maxmr
;
1650 ib_pd
= e_maxmr
->ib
.ib_mr
.pd
;
1651 shca
->maxmr
= NULL
; /* remove internal max-MR indication from SHCA */
1653 ret
= ehca_dereg_mr(&e_maxmr
->ib
.ib_mr
);
1655 ehca_err(&shca
->ib_device
, "dereg internal max-MR failed, "
1656 "ret=%i e_maxmr=%p shca=%p lkey=%x",
1657 ret
, e_maxmr
, shca
, e_maxmr
->ib
.ib_mr
.lkey
);
1658 shca
->maxmr
= e_maxmr
;
1659 goto ehca_dereg_internal_maxmr_exit0
;
1662 atomic_dec(&ib_pd
->usecnt
);
1664 ehca_dereg_internal_maxmr_exit0
:
1666 ehca_err(&shca
->ib_device
, "ret=%i shca=%p shca->maxmr=%p",
1667 ret
, shca
, shca
->maxmr
);
1669 } /* end ehca_dereg_internal_maxmr() */
1671 /*----------------------------------------------------------------------*/
1674 * check physical buffer array of MR verbs for validness and
1675 * calculates MR size
1677 int ehca_mr_chk_buf_and_calc_size(struct ib_phys_buf
*phys_buf_array
,
1682 struct ib_phys_buf
*pbuf
= phys_buf_array
;
1686 if (num_phys_buf
== 0) {
1687 ehca_gen_err("bad phys buf array len, num_phys_buf=0");
1690 /* check first buffer */
1691 if (((u64
)iova_start
& ~PAGE_MASK
) != (pbuf
->addr
& ~PAGE_MASK
)) {
1692 ehca_gen_err("iova_start/addr mismatch, iova_start=%p "
1693 "pbuf->addr=%lx pbuf->size=%lx",
1694 iova_start
, pbuf
->addr
, pbuf
->size
);
1697 if (((pbuf
->addr
+ pbuf
->size
) % PAGE_SIZE
) &&
1698 (num_phys_buf
> 1)) {
1699 ehca_gen_err("addr/size mismatch in 1st buf, pbuf->addr=%lx "
1700 "pbuf->size=%lx", pbuf
->addr
, pbuf
->size
);
1704 for (i
= 0; i
< num_phys_buf
; i
++) {
1705 if ((i
> 0) && (pbuf
->addr
% PAGE_SIZE
)) {
1706 ehca_gen_err("bad address, i=%x pbuf->addr=%lx "
1708 i
, pbuf
->addr
, pbuf
->size
);
1711 if (((i
> 0) && /* not 1st */
1712 (i
< (num_phys_buf
- 1)) && /* not last */
1713 (pbuf
->size
% PAGE_SIZE
)) || (pbuf
->size
== 0)) {
1714 ehca_gen_err("bad size, i=%x pbuf->size=%lx",
1718 size_count
+= pbuf
->size
;
1724 } /* end ehca_mr_chk_buf_and_calc_size() */
1726 /*----------------------------------------------------------------------*/
1728 /* check page list of map FMR verb for validness */
1729 int ehca_fmr_check_page_list(struct ehca_mr
*e_fmr
,
1736 if ((list_len
== 0) || (list_len
> e_fmr
->fmr_max_pages
)) {
1737 ehca_gen_err("bad list_len, list_len=%x "
1738 "e_fmr->fmr_max_pages=%x fmr=%p",
1739 list_len
, e_fmr
->fmr_max_pages
, e_fmr
);
1743 /* each page must be aligned */
1745 for (i
= 0; i
< list_len
; i
++) {
1746 if (*page
% e_fmr
->fmr_page_size
) {
1747 ehca_gen_err("bad page, i=%x *page=%lx page=%p fmr=%p "
1748 "fmr_page_size=%x", i
, *page
, page
, e_fmr
,
1749 e_fmr
->fmr_page_size
);
1756 } /* end ehca_fmr_check_page_list() */
1758 /*----------------------------------------------------------------------*/
1760 /* PAGE_SIZE >= pginfo->hwpage_size */
1761 static int ehca_set_pagebuf_user1(struct ehca_mr_pginfo
*pginfo
,
1766 struct ib_umem_chunk
*prev_chunk
;
1767 struct ib_umem_chunk
*chunk
;
1771 int hwpages_per_kpage
= PAGE_SIZE
/ pginfo
->hwpage_size
;
1773 /* loop over desired chunk entries */
1774 chunk
= pginfo
->u
.usr
.next_chunk
;
1775 prev_chunk
= pginfo
->u
.usr
.next_chunk
;
1776 list_for_each_entry_continue(
1777 chunk
, (&(pginfo
->u
.usr
.region
->chunk_list
)), list
) {
1778 for (i
= pginfo
->u
.usr
.next_nmap
; i
< chunk
->nmap
; ) {
1779 pgaddr
= page_to_pfn(chunk
->page_list
[i
].page
)
1781 *kpage
= phys_to_abs(pgaddr
+
1782 (pginfo
->next_hwpage
*
1783 pginfo
->hwpage_size
));
1785 ehca_gen_err("pgaddr=%lx "
1786 "chunk->page_list[i]=%lx "
1787 "i=%x next_hwpage=%lx",
1788 pgaddr
, (u64
)sg_dma_address(
1789 &chunk
->page_list
[i
]),
1790 i
, pginfo
->next_hwpage
);
1793 (pginfo
->hwpage_cnt
)++;
1794 (pginfo
->next_hwpage
)++;
1796 if (pginfo
->next_hwpage
% hwpages_per_kpage
== 0) {
1797 (pginfo
->kpage_cnt
)++;
1798 (pginfo
->u
.usr
.next_nmap
)++;
1799 pginfo
->next_hwpage
= 0;
1803 if (j
>= number
) break;
1805 if ((pginfo
->u
.usr
.next_nmap
>= chunk
->nmap
) &&
1807 pginfo
->u
.usr
.next_nmap
= 0;
1810 } else if (pginfo
->u
.usr
.next_nmap
>= chunk
->nmap
) {
1811 pginfo
->u
.usr
.next_nmap
= 0;
1813 } else if (j
>= number
)
1818 pginfo
->u
.usr
.next_chunk
=
1819 list_prepare_entry(prev_chunk
,
1820 (&(pginfo
->u
.usr
.region
->chunk_list
)),
1826 * check given pages for contiguous layout
1827 * last page addr is returned in prev_pgaddr for further check
1829 static int ehca_check_kpages_per_ate(struct scatterlist
*page_list
,
1830 int start_idx
, int end_idx
,
1834 for (t
= start_idx
; t
<= end_idx
; t
++) {
1835 u64 pgaddr
= page_to_pfn(page_list
[t
].page
) << PAGE_SHIFT
;
1836 ehca_gen_dbg("chunk_page=%lx value=%016lx", pgaddr
,
1837 *(u64
*)abs_to_virt(phys_to_abs(pgaddr
)));
1838 if (pgaddr
- PAGE_SIZE
!= *prev_pgaddr
) {
1839 ehca_gen_err("uncontiguous page found pgaddr=%lx "
1840 "prev_pgaddr=%lx page_list_i=%x",
1841 pgaddr
, *prev_pgaddr
, t
);
1844 *prev_pgaddr
= pgaddr
;
1849 /* PAGE_SIZE < pginfo->hwpage_size */
1850 static int ehca_set_pagebuf_user2(struct ehca_mr_pginfo
*pginfo
,
1855 struct ib_umem_chunk
*prev_chunk
;
1856 struct ib_umem_chunk
*chunk
;
1857 u64 pgaddr
, prev_pgaddr
;
1860 int kpages_per_hwpage
= pginfo
->hwpage_size
/ PAGE_SIZE
;
1861 int nr_kpages
= kpages_per_hwpage
;
1863 /* loop over desired chunk entries */
1864 chunk
= pginfo
->u
.usr
.next_chunk
;
1865 prev_chunk
= pginfo
->u
.usr
.next_chunk
;
1866 list_for_each_entry_continue(
1867 chunk
, (&(pginfo
->u
.usr
.region
->chunk_list
)), list
) {
1868 for (i
= pginfo
->u
.usr
.next_nmap
; i
< chunk
->nmap
; ) {
1869 if (nr_kpages
== kpages_per_hwpage
) {
1870 pgaddr
= ( page_to_pfn(chunk
->page_list
[i
].page
)
1872 *kpage
= phys_to_abs(pgaddr
);
1874 ehca_gen_err("pgaddr=%lx i=%x",
1880 * The first page in a hwpage must be aligned;
1881 * the first MR page is exempt from this rule.
1883 if (pgaddr
& (pginfo
->hwpage_size
- 1)) {
1884 if (pginfo
->hwpage_cnt
) {
1886 "invalid alignment "
1890 pginfo
->hwpage_size
);
1897 (pginfo
->hwpage_size
- 1)) >>
1899 nr_kpages
-= pginfo
->kpage_cnt
;
1900 *kpage
= phys_to_abs(
1902 ~(pginfo
->hwpage_size
- 1));
1904 ehca_gen_dbg("kpage=%lx chunk_page=%lx "
1905 "value=%016lx", *kpage
, pgaddr
,
1906 *(u64
*)abs_to_virt(
1907 phys_to_abs(pgaddr
)));
1908 prev_pgaddr
= pgaddr
;
1910 pginfo
->kpage_cnt
++;
1911 pginfo
->u
.usr
.next_nmap
++;
1917 if (i
+ nr_kpages
> chunk
->nmap
) {
1918 ret
= ehca_check_kpages_per_ate(
1919 chunk
->page_list
, i
,
1920 chunk
->nmap
- 1, &prev_pgaddr
);
1921 if (ret
) return ret
;
1922 pginfo
->kpage_cnt
+= chunk
->nmap
- i
;
1923 pginfo
->u
.usr
.next_nmap
+= chunk
->nmap
- i
;
1924 nr_kpages
-= chunk
->nmap
- i
;
1928 ret
= ehca_check_kpages_per_ate(chunk
->page_list
, i
,
1931 if (ret
) return ret
;
1933 pginfo
->kpage_cnt
+= nr_kpages
;
1934 pginfo
->u
.usr
.next_nmap
+= nr_kpages
;
1936 nr_kpages
= kpages_per_hwpage
;
1937 (pginfo
->hwpage_cnt
)++;
1940 if (j
>= number
) break;
1942 if ((pginfo
->u
.usr
.next_nmap
>= chunk
->nmap
) &&
1944 pginfo
->u
.usr
.next_nmap
= 0;
1947 } else if (pginfo
->u
.usr
.next_nmap
>= chunk
->nmap
) {
1948 pginfo
->u
.usr
.next_nmap
= 0;
1950 } else if (j
>= number
)
1955 pginfo
->u
.usr
.next_chunk
=
1956 list_prepare_entry(prev_chunk
,
1957 (&(pginfo
->u
.usr
.region
->chunk_list
)),
1962 int ehca_set_pagebuf_phys(struct ehca_mr_pginfo
*pginfo
,
1967 struct ib_phys_buf
*pbuf
;
1968 u64 num_hw
, offs_hw
;
1971 /* loop over desired phys_buf_array entries */
1972 while (i
< number
) {
1973 pbuf
= pginfo
->u
.phy
.phys_buf_array
+ pginfo
->u
.phy
.next_buf
;
1974 num_hw
= NUM_CHUNKS((pbuf
->addr
% pginfo
->hwpage_size
) +
1975 pbuf
->size
, pginfo
->hwpage_size
);
1976 offs_hw
= (pbuf
->addr
& ~(pginfo
->hwpage_size
- 1)) /
1977 pginfo
->hwpage_size
;
1978 while (pginfo
->next_hwpage
< offs_hw
+ num_hw
) {
1980 if ((pginfo
->kpage_cnt
>= pginfo
->num_kpages
) ||
1981 (pginfo
->hwpage_cnt
>= pginfo
->num_hwpages
)) {
1982 ehca_gen_err("kpage_cnt >= num_kpages, "
1983 "kpage_cnt=%lx num_kpages=%lx "
1985 "num_hwpages=%lx i=%x",
1989 pginfo
->num_hwpages
, i
);
1992 *kpage
= phys_to_abs(
1993 (pbuf
->addr
& ~(pginfo
->hwpage_size
- 1)) +
1994 (pginfo
->next_hwpage
* pginfo
->hwpage_size
));
1995 if ( !(*kpage
) && pbuf
->addr
) {
1996 ehca_gen_err("pbuf->addr=%lx pbuf->size=%lx "
1997 "next_hwpage=%lx", pbuf
->addr
,
1998 pbuf
->size
, pginfo
->next_hwpage
);
2001 (pginfo
->hwpage_cnt
)++;
2002 (pginfo
->next_hwpage
)++;
2003 if (PAGE_SIZE
>= pginfo
->hwpage_size
) {
2004 if (pginfo
->next_hwpage
%
2005 (PAGE_SIZE
/ pginfo
->hwpage_size
) == 0)
2006 (pginfo
->kpage_cnt
)++;
2008 pginfo
->kpage_cnt
+= pginfo
->hwpage_size
/
2012 if (i
>= number
) break;
2014 if (pginfo
->next_hwpage
>= offs_hw
+ num_hw
) {
2015 (pginfo
->u
.phy
.next_buf
)++;
2016 pginfo
->next_hwpage
= 0;
2022 int ehca_set_pagebuf_fmr(struct ehca_mr_pginfo
*pginfo
,
2030 /* loop over desired page_list entries */
2031 fmrlist
= pginfo
->u
.fmr
.page_list
+ pginfo
->u
.fmr
.next_listelem
;
2032 for (i
= 0; i
< number
; i
++) {
2033 *kpage
= phys_to_abs((*fmrlist
& ~(pginfo
->hwpage_size
- 1)) +
2034 pginfo
->next_hwpage
* pginfo
->hwpage_size
);
2036 ehca_gen_err("*fmrlist=%lx fmrlist=%p "
2037 "next_listelem=%lx next_hwpage=%lx",
2039 pginfo
->u
.fmr
.next_listelem
,
2040 pginfo
->next_hwpage
);
2043 (pginfo
->hwpage_cnt
)++;
2044 if (pginfo
->u
.fmr
.fmr_pgsize
>= pginfo
->hwpage_size
) {
2045 if (pginfo
->next_hwpage
%
2046 (pginfo
->u
.fmr
.fmr_pgsize
/
2047 pginfo
->hwpage_size
) == 0) {
2048 (pginfo
->kpage_cnt
)++;
2049 (pginfo
->u
.fmr
.next_listelem
)++;
2051 pginfo
->next_hwpage
= 0;
2053 (pginfo
->next_hwpage
)++;
2055 unsigned int cnt_per_hwpage
= pginfo
->hwpage_size
/
2056 pginfo
->u
.fmr
.fmr_pgsize
;
2059 /* check if adrs are contiguous */
2060 for (j
= 1; j
< cnt_per_hwpage
; j
++) {
2061 u64 p
= phys_to_abs(fmrlist
[j
] &
2062 ~(pginfo
->hwpage_size
- 1));
2063 if (prev
+ pginfo
->u
.fmr
.fmr_pgsize
!= p
) {
2064 ehca_gen_err("uncontiguous fmr pages "
2065 "found prev=%lx p=%lx "
2066 "idx=%x", prev
, p
, i
+ j
);
2071 pginfo
->kpage_cnt
+= cnt_per_hwpage
;
2072 pginfo
->u
.fmr
.next_listelem
+= cnt_per_hwpage
;
2073 fmrlist
+= cnt_per_hwpage
;
2080 /* setup page buffer from page info */
2081 int ehca_set_pagebuf(struct ehca_mr_pginfo
*pginfo
,
2087 switch (pginfo
->type
) {
2088 case EHCA_MR_PGI_PHYS
:
2089 ret
= ehca_set_pagebuf_phys(pginfo
, number
, kpage
);
2091 case EHCA_MR_PGI_USER
:
2092 ret
= PAGE_SIZE
>= pginfo
->hwpage_size
?
2093 ehca_set_pagebuf_user1(pginfo
, number
, kpage
) :
2094 ehca_set_pagebuf_user2(pginfo
, number
, kpage
);
2096 case EHCA_MR_PGI_FMR
:
2097 ret
= ehca_set_pagebuf_fmr(pginfo
, number
, kpage
);
2100 ehca_gen_err("bad pginfo->type=%x", pginfo
->type
);
2105 } /* end ehca_set_pagebuf() */
2107 /*----------------------------------------------------------------------*/
2110 * check MR if it is a max-MR, i.e. uses whole memory
2111 * in case it's a max-MR 1 is returned, else 0
2113 int ehca_mr_is_maxmr(u64 size
,
2116 /* a MR is treated as max-MR only if it fits following: */
2117 if ((size
== ((u64
)high_memory
- PAGE_OFFSET
)) &&
2118 (iova_start
== (void *)KERNELBASE
)) {
2119 ehca_gen_dbg("this is a max-MR");
2123 } /* end ehca_mr_is_maxmr() */
2125 /*----------------------------------------------------------------------*/
2127 /* map access control for MR/MW. This routine is used for MR and MW. */
2128 void ehca_mrmw_map_acl(int ib_acl
,
2132 if (ib_acl
& IB_ACCESS_REMOTE_READ
)
2133 *hipz_acl
|= HIPZ_ACCESSCTRL_R_READ
;
2134 if (ib_acl
& IB_ACCESS_REMOTE_WRITE
)
2135 *hipz_acl
|= HIPZ_ACCESSCTRL_R_WRITE
;
2136 if (ib_acl
& IB_ACCESS_REMOTE_ATOMIC
)
2137 *hipz_acl
|= HIPZ_ACCESSCTRL_R_ATOMIC
;
2138 if (ib_acl
& IB_ACCESS_LOCAL_WRITE
)
2139 *hipz_acl
|= HIPZ_ACCESSCTRL_L_WRITE
;
2140 if (ib_acl
& IB_ACCESS_MW_BIND
)
2141 *hipz_acl
|= HIPZ_ACCESSCTRL_MW_BIND
;
2142 } /* end ehca_mrmw_map_acl() */
2144 /*----------------------------------------------------------------------*/
2146 /* sets page size in hipz access control for MR/MW. */
2147 void ehca_mrmw_set_pgsize_hipz_acl(u32 pgsize
, u32
*hipz_acl
) /*INOUT*/
2149 *hipz_acl
|= (ehca_encode_hwpage_size(pgsize
) << 24);
2150 } /* end ehca_mrmw_set_pgsize_hipz_acl() */
2152 /*----------------------------------------------------------------------*/
2155 * reverse map access control for MR/MW.
2156 * This routine is used for MR and MW.
2158 void ehca_mrmw_reverse_map_acl(const u32
*hipz_acl
,
2159 int *ib_acl
) /*OUT*/
2162 if (*hipz_acl
& HIPZ_ACCESSCTRL_R_READ
)
2163 *ib_acl
|= IB_ACCESS_REMOTE_READ
;
2164 if (*hipz_acl
& HIPZ_ACCESSCTRL_R_WRITE
)
2165 *ib_acl
|= IB_ACCESS_REMOTE_WRITE
;
2166 if (*hipz_acl
& HIPZ_ACCESSCTRL_R_ATOMIC
)
2167 *ib_acl
|= IB_ACCESS_REMOTE_ATOMIC
;
2168 if (*hipz_acl
& HIPZ_ACCESSCTRL_L_WRITE
)
2169 *ib_acl
|= IB_ACCESS_LOCAL_WRITE
;
2170 if (*hipz_acl
& HIPZ_ACCESSCTRL_MW_BIND
)
2171 *ib_acl
|= IB_ACCESS_MW_BIND
;
2172 } /* end ehca_mrmw_reverse_map_acl() */
2175 /*----------------------------------------------------------------------*/
2178 * MR destructor and constructor
2179 * used in Reregister MR verb, sets all fields in ehca_mr_t to 0,
2180 * except struct ib_mr and spinlock
2182 void ehca_mr_deletenew(struct ehca_mr
*mr
)
2186 mr
->num_hwpages
= 0;
2189 mr
->fmr_page_size
= 0;
2190 mr
->fmr_max_pages
= 0;
2191 mr
->fmr_max_maps
= 0;
2192 mr
->fmr_map_cnt
= 0;
2193 memset(&mr
->ipz_mr_handle
, 0, sizeof(mr
->ipz_mr_handle
));
2194 memset(&mr
->galpas
, 0, sizeof(mr
->galpas
));
2195 } /* end ehca_mr_deletenew() */
2197 int ehca_init_mrmw_cache(void)
2199 mr_cache
= kmem_cache_create("ehca_cache_mr",
2200 sizeof(struct ehca_mr
), 0,
2205 mw_cache
= kmem_cache_create("ehca_cache_mw",
2206 sizeof(struct ehca_mw
), 0,
2210 kmem_cache_destroy(mr_cache
);
2217 void ehca_cleanup_mrmw_cache(void)
2220 kmem_cache_destroy(mr_cache
);
2222 kmem_cache_destroy(mw_cache
);