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
)
75 int log
= ilog2(pgsize
);
76 WARN_ON(log
< 12 || log
> 24 || log
& 3);
77 return (log
- 12) / 4;
80 static u64
ehca_get_max_hwpage_size(struct ehca_shca
*shca
)
82 return 1UL << ilog2(shca
->hca_cap_mr_pgsize
);
85 static struct ehca_mr
*ehca_mr_new(void)
89 me
= kmem_cache_zalloc(mr_cache
, GFP_KERNEL
);
91 spin_lock_init(&me
->mrlock
);
93 ehca_gen_err("alloc failed");
98 static void ehca_mr_delete(struct ehca_mr
*me
)
100 kmem_cache_free(mr_cache
, me
);
103 static struct ehca_mw
*ehca_mw_new(void)
107 me
= kmem_cache_zalloc(mw_cache
, GFP_KERNEL
);
109 spin_lock_init(&me
->mwlock
);
111 ehca_gen_err("alloc failed");
116 static void ehca_mw_delete(struct ehca_mw
*me
)
118 kmem_cache_free(mw_cache
, me
);
121 /*----------------------------------------------------------------------*/
123 struct ib_mr
*ehca_get_dma_mr(struct ib_pd
*pd
, int mr_access_flags
)
127 struct ehca_mr
*e_maxmr
;
128 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
129 struct ehca_shca
*shca
=
130 container_of(pd
->device
, struct ehca_shca
, ib_device
);
133 e_maxmr
= ehca_mr_new();
135 ehca_err(&shca
->ib_device
, "out of memory");
136 ib_mr
= ERR_PTR(-ENOMEM
);
137 goto get_dma_mr_exit0
;
140 ret
= ehca_reg_maxmr(shca
, e_maxmr
, (u64
*)KERNELBASE
,
141 mr_access_flags
, e_pd
,
142 &e_maxmr
->ib
.ib_mr
.lkey
,
143 &e_maxmr
->ib
.ib_mr
.rkey
);
145 ehca_mr_delete(e_maxmr
);
146 ib_mr
= ERR_PTR(ret
);
147 goto get_dma_mr_exit0
;
149 ib_mr
= &e_maxmr
->ib
.ib_mr
;
151 ehca_err(&shca
->ib_device
, "no internal max-MR exist!");
152 ib_mr
= ERR_PTR(-EINVAL
);
153 goto get_dma_mr_exit0
;
158 ehca_err(&shca
->ib_device
, "h_ret=%li pd=%p mr_access_flags=%x",
159 PTR_ERR(ib_mr
), pd
, mr_access_flags
);
161 } /* end ehca_get_dma_mr() */
163 /*----------------------------------------------------------------------*/
165 struct ib_mr
*ehca_reg_phys_mr(struct ib_pd
*pd
,
166 struct ib_phys_buf
*phys_buf_array
,
173 struct ehca_mr
*e_mr
;
174 struct ehca_shca
*shca
=
175 container_of(pd
->device
, struct ehca_shca
, ib_device
);
176 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
180 if ((num_phys_buf
<= 0) || !phys_buf_array
) {
181 ehca_err(pd
->device
, "bad input values: num_phys_buf=%x "
182 "phys_buf_array=%p", num_phys_buf
, phys_buf_array
);
183 ib_mr
= ERR_PTR(-EINVAL
);
184 goto reg_phys_mr_exit0
;
186 if (((mr_access_flags
& IB_ACCESS_REMOTE_WRITE
) &&
187 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)) ||
188 ((mr_access_flags
& IB_ACCESS_REMOTE_ATOMIC
) &&
189 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
))) {
191 * Remote Write Access requires Local Write Access
192 * Remote Atomic Access requires Local Write Access
194 ehca_err(pd
->device
, "bad input values: mr_access_flags=%x",
196 ib_mr
= ERR_PTR(-EINVAL
);
197 goto reg_phys_mr_exit0
;
200 /* check physical buffer list and calculate size */
201 ret
= ehca_mr_chk_buf_and_calc_size(phys_buf_array
, num_phys_buf
,
204 ib_mr
= ERR_PTR(ret
);
205 goto reg_phys_mr_exit0
;
208 (((u64
)iova_start
+ size
) < (u64
)iova_start
)) {
209 ehca_err(pd
->device
, "bad input values: size=%lx iova_start=%p",
211 ib_mr
= ERR_PTR(-EINVAL
);
212 goto reg_phys_mr_exit0
;
215 e_mr
= ehca_mr_new();
217 ehca_err(pd
->device
, "out of memory");
218 ib_mr
= ERR_PTR(-ENOMEM
);
219 goto reg_phys_mr_exit0
;
222 /* register MR on HCA */
223 if (ehca_mr_is_maxmr(size
, iova_start
)) {
224 e_mr
->flags
|= EHCA_MR_FLAG_MAXMR
;
225 ret
= ehca_reg_maxmr(shca
, e_mr
, iova_start
, mr_access_flags
,
226 e_pd
, &e_mr
->ib
.ib_mr
.lkey
,
227 &e_mr
->ib
.ib_mr
.rkey
);
229 ib_mr
= ERR_PTR(ret
);
230 goto reg_phys_mr_exit1
;
233 struct ehca_mr_pginfo pginfo
;
238 num_kpages
= NUM_CHUNKS(((u64
)iova_start
% PAGE_SIZE
) + size
,
240 /* for kernel space we try most possible pgsize */
241 hw_pgsize
= ehca_get_max_hwpage_size(shca
);
242 num_hwpages
= NUM_CHUNKS(((u64
)iova_start
% hw_pgsize
) + size
,
244 memset(&pginfo
, 0, sizeof(pginfo
));
245 pginfo
.type
= EHCA_MR_PGI_PHYS
;
246 pginfo
.num_kpages
= num_kpages
;
247 pginfo
.hwpage_size
= hw_pgsize
;
248 pginfo
.num_hwpages
= num_hwpages
;
249 pginfo
.u
.phy
.num_phys_buf
= num_phys_buf
;
250 pginfo
.u
.phy
.phys_buf_array
= phys_buf_array
;
252 ((u64
)iova_start
& ~PAGE_MASK
) / hw_pgsize
;
254 ret
= ehca_reg_mr(shca
, e_mr
, iova_start
, size
, mr_access_flags
,
255 e_pd
, &pginfo
, &e_mr
->ib
.ib_mr
.lkey
,
256 &e_mr
->ib
.ib_mr
.rkey
);
258 ib_mr
= ERR_PTR(ret
);
259 goto reg_phys_mr_exit1
;
263 /* successful registration of all pages */
264 return &e_mr
->ib
.ib_mr
;
267 ehca_mr_delete(e_mr
);
270 ehca_err(pd
->device
, "h_ret=%li pd=%p phys_buf_array=%p "
271 "num_phys_buf=%x mr_access_flags=%x iova_start=%p",
272 PTR_ERR(ib_mr
), pd
, phys_buf_array
,
273 num_phys_buf
, mr_access_flags
, iova_start
);
275 } /* end ehca_reg_phys_mr() */
277 /*----------------------------------------------------------------------*/
279 struct ib_mr
*ehca_reg_user_mr(struct ib_pd
*pd
, u64 start
, u64 length
,
280 u64 virt
, int mr_access_flags
,
281 struct ib_udata
*udata
)
284 struct ehca_mr
*e_mr
;
285 struct ehca_shca
*shca
=
286 container_of(pd
->device
, struct ehca_shca
, ib_device
);
287 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
288 struct ehca_mr_pginfo pginfo
;
295 ehca_gen_err("bad pd=%p", pd
);
296 return ERR_PTR(-EFAULT
);
299 if (((mr_access_flags
& IB_ACCESS_REMOTE_WRITE
) &&
300 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)) ||
301 ((mr_access_flags
& IB_ACCESS_REMOTE_ATOMIC
) &&
302 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
))) {
304 * Remote Write Access requires Local Write Access
305 * Remote Atomic Access requires Local Write Access
307 ehca_err(pd
->device
, "bad input values: mr_access_flags=%x",
309 ib_mr
= ERR_PTR(-EINVAL
);
310 goto reg_user_mr_exit0
;
313 if (length
== 0 || virt
+ length
< virt
) {
314 ehca_err(pd
->device
, "bad input values: length=%lx "
315 "virt_base=%lx", length
, virt
);
316 ib_mr
= ERR_PTR(-EINVAL
);
317 goto reg_user_mr_exit0
;
320 e_mr
= ehca_mr_new();
322 ehca_err(pd
->device
, "out of memory");
323 ib_mr
= ERR_PTR(-ENOMEM
);
324 goto reg_user_mr_exit0
;
327 e_mr
->umem
= ib_umem_get(pd
->uobject
->context
, start
, length
,
329 if (IS_ERR(e_mr
->umem
)) {
330 ib_mr
= (void *)e_mr
->umem
;
331 goto reg_user_mr_exit1
;
334 if (e_mr
->umem
->page_size
!= PAGE_SIZE
) {
335 ehca_err(pd
->device
, "page size not supported, "
336 "e_mr->umem->page_size=%x", e_mr
->umem
->page_size
);
337 ib_mr
= ERR_PTR(-EINVAL
);
338 goto reg_user_mr_exit2
;
341 /* determine number of MR pages */
342 num_kpages
= NUM_CHUNKS((virt
% PAGE_SIZE
) + length
, PAGE_SIZE
);
343 /* select proper hw_pgsize */
344 page_shift
= PAGE_SHIFT
;
345 if (e_mr
->umem
->hugetlb
) {
346 /* determine page_shift, clamp between 4K and 16M */
347 page_shift
= (fls64(length
- 1) + 3) & ~3;
348 page_shift
= min(max(page_shift
, EHCA_MR_PGSHIFT4K
),
351 hwpage_size
= 1UL << page_shift
;
353 /* now that we have the desired page size, shift until it's
354 * supported, too. 4K is always supported, so this terminates.
356 while (!(hwpage_size
& shca
->hca_cap_mr_pgsize
))
359 reg_user_mr_fallback
:
360 num_hwpages
= NUM_CHUNKS((virt
% hwpage_size
) + length
, hwpage_size
);
361 /* register MR on HCA */
362 memset(&pginfo
, 0, sizeof(pginfo
));
363 pginfo
.type
= EHCA_MR_PGI_USER
;
364 pginfo
.hwpage_size
= hwpage_size
;
365 pginfo
.num_kpages
= num_kpages
;
366 pginfo
.num_hwpages
= num_hwpages
;
367 pginfo
.u
.usr
.region
= e_mr
->umem
;
368 pginfo
.next_hwpage
= e_mr
->umem
->offset
/ hwpage_size
;
369 pginfo
.u
.usr
.next_chunk
= list_prepare_entry(pginfo
.u
.usr
.next_chunk
,
370 (&e_mr
->umem
->chunk_list
),
373 ret
= ehca_reg_mr(shca
, e_mr
, (u64
*)virt
, length
, mr_access_flags
,
374 e_pd
, &pginfo
, &e_mr
->ib
.ib_mr
.lkey
,
375 &e_mr
->ib
.ib_mr
.rkey
);
376 if (ret
== -EINVAL
&& pginfo
.hwpage_size
> PAGE_SIZE
) {
377 ehca_warn(pd
->device
, "failed to register mr "
378 "with hwpage_size=%lx", hwpage_size
);
379 ehca_info(pd
->device
, "try to register mr with "
380 "kpage_size=%lx", PAGE_SIZE
);
382 * this means kpages are not contiguous for a hw page
383 * try kernel page size as fallback solution
385 hwpage_size
= PAGE_SIZE
;
386 goto reg_user_mr_fallback
;
389 ib_mr
= ERR_PTR(ret
);
390 goto reg_user_mr_exit2
;
393 /* successful registration of all pages */
394 return &e_mr
->ib
.ib_mr
;
397 ib_umem_release(e_mr
->umem
);
399 ehca_mr_delete(e_mr
);
402 ehca_err(pd
->device
, "rc=%li pd=%p mr_access_flags=%x udata=%p",
403 PTR_ERR(ib_mr
), pd
, mr_access_flags
, udata
);
405 } /* end ehca_reg_user_mr() */
407 /*----------------------------------------------------------------------*/
409 int ehca_rereg_phys_mr(struct ib_mr
*mr
,
412 struct ib_phys_buf
*phys_buf_array
,
419 struct ehca_shca
*shca
=
420 container_of(mr
->device
, struct ehca_shca
, ib_device
);
421 struct ehca_mr
*e_mr
= container_of(mr
, struct ehca_mr
, ib
.ib_mr
);
422 struct ehca_pd
*my_pd
= container_of(mr
->pd
, struct ehca_pd
, ib_pd
);
426 struct ehca_pd
*new_pd
;
427 u32 tmp_lkey
, tmp_rkey
;
428 unsigned long sl_flags
;
431 struct ehca_mr_pginfo pginfo
;
432 u32 cur_pid
= current
->tgid
;
434 if (my_pd
->ib_pd
.uobject
&& my_pd
->ib_pd
.uobject
->context
&&
435 (my_pd
->ownpid
!= cur_pid
)) {
436 ehca_err(mr
->device
, "Invalid caller pid=%x ownpid=%x",
437 cur_pid
, my_pd
->ownpid
);
439 goto rereg_phys_mr_exit0
;
442 if (!(mr_rereg_mask
& IB_MR_REREG_TRANS
)) {
443 /* TODO not supported, because PHYP rereg hCall needs pages */
444 ehca_err(mr
->device
, "rereg without IB_MR_REREG_TRANS not "
445 "supported yet, mr_rereg_mask=%x", mr_rereg_mask
);
447 goto rereg_phys_mr_exit0
;
450 if (mr_rereg_mask
& IB_MR_REREG_PD
) {
452 ehca_err(mr
->device
, "rereg with bad pd, pd=%p "
453 "mr_rereg_mask=%x", pd
, mr_rereg_mask
);
455 goto rereg_phys_mr_exit0
;
460 ~(IB_MR_REREG_TRANS
| IB_MR_REREG_PD
| IB_MR_REREG_ACCESS
)) ||
461 (mr_rereg_mask
== 0)) {
463 goto rereg_phys_mr_exit0
;
466 /* check other parameters */
467 if (e_mr
== shca
->maxmr
) {
468 /* should be impossible, however reject to be sure */
469 ehca_err(mr
->device
, "rereg internal max-MR impossible, mr=%p "
470 "shca->maxmr=%p mr->lkey=%x",
471 mr
, shca
->maxmr
, mr
->lkey
);
473 goto rereg_phys_mr_exit0
;
475 if (mr_rereg_mask
& IB_MR_REREG_TRANS
) { /* transl., i.e. addr/size */
476 if (e_mr
->flags
& EHCA_MR_FLAG_FMR
) {
477 ehca_err(mr
->device
, "not supported for FMR, mr=%p "
478 "flags=%x", mr
, e_mr
->flags
);
480 goto rereg_phys_mr_exit0
;
482 if (!phys_buf_array
|| num_phys_buf
<= 0) {
483 ehca_err(mr
->device
, "bad input values mr_rereg_mask=%x"
484 " phys_buf_array=%p num_phys_buf=%x",
485 mr_rereg_mask
, phys_buf_array
, num_phys_buf
);
487 goto rereg_phys_mr_exit0
;
490 if ((mr_rereg_mask
& IB_MR_REREG_ACCESS
) && /* change ACL */
491 (((mr_access_flags
& IB_ACCESS_REMOTE_WRITE
) &&
492 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)) ||
493 ((mr_access_flags
& IB_ACCESS_REMOTE_ATOMIC
) &&
494 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)))) {
496 * Remote Write Access requires Local Write Access
497 * Remote Atomic Access requires Local Write Access
499 ehca_err(mr
->device
, "bad input values: mr_rereg_mask=%x "
500 "mr_access_flags=%x", mr_rereg_mask
, mr_access_flags
);
502 goto rereg_phys_mr_exit0
;
505 /* set requested values dependent on rereg request */
506 spin_lock_irqsave(&e_mr
->mrlock
, sl_flags
);
507 new_start
= e_mr
->start
;
508 new_size
= e_mr
->size
;
510 new_pd
= container_of(mr
->pd
, struct ehca_pd
, ib_pd
);
512 if (mr_rereg_mask
& IB_MR_REREG_TRANS
) {
513 u64 hw_pgsize
= ehca_get_max_hwpage_size(shca
);
515 new_start
= iova_start
; /* change address */
516 /* check physical buffer list and calculate size */
517 ret
= ehca_mr_chk_buf_and_calc_size(phys_buf_array
,
518 num_phys_buf
, iova_start
,
521 goto rereg_phys_mr_exit1
;
522 if ((new_size
== 0) ||
523 (((u64
)iova_start
+ new_size
) < (u64
)iova_start
)) {
524 ehca_err(mr
->device
, "bad input values: new_size=%lx "
525 "iova_start=%p", new_size
, iova_start
);
527 goto rereg_phys_mr_exit1
;
529 num_kpages
= NUM_CHUNKS(((u64
)new_start
% PAGE_SIZE
) +
530 new_size
, PAGE_SIZE
);
531 num_hwpages
= NUM_CHUNKS(((u64
)new_start
% hw_pgsize
) +
532 new_size
, hw_pgsize
);
533 memset(&pginfo
, 0, sizeof(pginfo
));
534 pginfo
.type
= EHCA_MR_PGI_PHYS
;
535 pginfo
.num_kpages
= num_kpages
;
536 pginfo
.hwpage_size
= hw_pgsize
;
537 pginfo
.num_hwpages
= num_hwpages
;
538 pginfo
.u
.phy
.num_phys_buf
= num_phys_buf
;
539 pginfo
.u
.phy
.phys_buf_array
= phys_buf_array
;
541 ((u64
)iova_start
& ~PAGE_MASK
) / hw_pgsize
;
543 if (mr_rereg_mask
& IB_MR_REREG_ACCESS
)
544 new_acl
= mr_access_flags
;
545 if (mr_rereg_mask
& IB_MR_REREG_PD
)
546 new_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
548 ret
= ehca_rereg_mr(shca
, e_mr
, new_start
, new_size
, new_acl
,
549 new_pd
, &pginfo
, &tmp_lkey
, &tmp_rkey
);
551 goto rereg_phys_mr_exit1
;
553 /* successful reregistration */
554 if (mr_rereg_mask
& IB_MR_REREG_PD
)
560 spin_unlock_irqrestore(&e_mr
->mrlock
, sl_flags
);
563 ehca_err(mr
->device
, "ret=%i mr=%p mr_rereg_mask=%x pd=%p "
564 "phys_buf_array=%p num_phys_buf=%x mr_access_flags=%x "
566 ret
, mr
, mr_rereg_mask
, pd
, phys_buf_array
,
567 num_phys_buf
, mr_access_flags
, iova_start
);
569 } /* end ehca_rereg_phys_mr() */
571 /*----------------------------------------------------------------------*/
573 int ehca_query_mr(struct ib_mr
*mr
, struct ib_mr_attr
*mr_attr
)
577 struct ehca_shca
*shca
=
578 container_of(mr
->device
, struct ehca_shca
, ib_device
);
579 struct ehca_mr
*e_mr
= container_of(mr
, struct ehca_mr
, ib
.ib_mr
);
580 struct ehca_pd
*my_pd
= container_of(mr
->pd
, struct ehca_pd
, ib_pd
);
581 u32 cur_pid
= current
->tgid
;
582 unsigned long sl_flags
;
583 struct ehca_mr_hipzout_parms hipzout
;
585 if (my_pd
->ib_pd
.uobject
&& my_pd
->ib_pd
.uobject
->context
&&
586 (my_pd
->ownpid
!= cur_pid
)) {
587 ehca_err(mr
->device
, "Invalid caller pid=%x ownpid=%x",
588 cur_pid
, my_pd
->ownpid
);
593 if ((e_mr
->flags
& EHCA_MR_FLAG_FMR
)) {
594 ehca_err(mr
->device
, "not supported for FMR, mr=%p e_mr=%p "
595 "e_mr->flags=%x", mr
, e_mr
, e_mr
->flags
);
600 memset(mr_attr
, 0, sizeof(struct ib_mr_attr
));
601 spin_lock_irqsave(&e_mr
->mrlock
, sl_flags
);
603 h_ret
= hipz_h_query_mr(shca
->ipz_hca_handle
, e_mr
, &hipzout
);
604 if (h_ret
!= H_SUCCESS
) {
605 ehca_err(mr
->device
, "hipz_mr_query failed, h_ret=%li mr=%p "
606 "hca_hndl=%lx mr_hndl=%lx lkey=%x",
607 h_ret
, mr
, shca
->ipz_hca_handle
.handle
,
608 e_mr
->ipz_mr_handle
.handle
, mr
->lkey
);
609 ret
= ehca2ib_return_code(h_ret
);
612 mr_attr
->pd
= mr
->pd
;
613 mr_attr
->device_virt_addr
= hipzout
.vaddr
;
614 mr_attr
->size
= hipzout
.len
;
615 mr_attr
->lkey
= hipzout
.lkey
;
616 mr_attr
->rkey
= hipzout
.rkey
;
617 ehca_mrmw_reverse_map_acl(&hipzout
.acl
, &mr_attr
->mr_access_flags
);
620 spin_unlock_irqrestore(&e_mr
->mrlock
, sl_flags
);
623 ehca_err(mr
->device
, "ret=%i mr=%p mr_attr=%p",
626 } /* end ehca_query_mr() */
628 /*----------------------------------------------------------------------*/
630 int ehca_dereg_mr(struct ib_mr
*mr
)
634 struct ehca_shca
*shca
=
635 container_of(mr
->device
, struct ehca_shca
, ib_device
);
636 struct ehca_mr
*e_mr
= container_of(mr
, struct ehca_mr
, ib
.ib_mr
);
637 struct ehca_pd
*my_pd
= container_of(mr
->pd
, struct ehca_pd
, ib_pd
);
638 u32 cur_pid
= current
->tgid
;
640 if (my_pd
->ib_pd
.uobject
&& my_pd
->ib_pd
.uobject
->context
&&
641 (my_pd
->ownpid
!= cur_pid
)) {
642 ehca_err(mr
->device
, "Invalid caller pid=%x ownpid=%x",
643 cur_pid
, my_pd
->ownpid
);
648 if ((e_mr
->flags
& EHCA_MR_FLAG_FMR
)) {
649 ehca_err(mr
->device
, "not supported for FMR, mr=%p e_mr=%p "
650 "e_mr->flags=%x", mr
, e_mr
, e_mr
->flags
);
653 } else if (e_mr
== shca
->maxmr
) {
654 /* should be impossible, however reject to be sure */
655 ehca_err(mr
->device
, "dereg internal max-MR impossible, mr=%p "
656 "shca->maxmr=%p mr->lkey=%x",
657 mr
, shca
->maxmr
, mr
->lkey
);
662 /* TODO: BUSY: MR still has bound window(s) */
663 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_mr
);
664 if (h_ret
!= H_SUCCESS
) {
665 ehca_err(mr
->device
, "hipz_free_mr failed, h_ret=%li shca=%p "
666 "e_mr=%p hca_hndl=%lx mr_hndl=%lx mr->lkey=%x",
667 h_ret
, shca
, e_mr
, shca
->ipz_hca_handle
.handle
,
668 e_mr
->ipz_mr_handle
.handle
, mr
->lkey
);
669 ret
= ehca2ib_return_code(h_ret
);
674 ib_umem_release(e_mr
->umem
);
676 /* successful deregistration */
677 ehca_mr_delete(e_mr
);
681 ehca_err(mr
->device
, "ret=%i mr=%p", ret
, mr
);
683 } /* end ehca_dereg_mr() */
685 /*----------------------------------------------------------------------*/
687 struct ib_mw
*ehca_alloc_mw(struct ib_pd
*pd
)
691 struct ehca_mw
*e_mw
;
692 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
693 struct ehca_shca
*shca
=
694 container_of(pd
->device
, struct ehca_shca
, ib_device
);
695 struct ehca_mw_hipzout_parms hipzout
;
697 e_mw
= ehca_mw_new();
699 ib_mw
= ERR_PTR(-ENOMEM
);
703 h_ret
= hipz_h_alloc_resource_mw(shca
->ipz_hca_handle
, e_mw
,
704 e_pd
->fw_pd
, &hipzout
);
705 if (h_ret
!= H_SUCCESS
) {
706 ehca_err(pd
->device
, "hipz_mw_allocate failed, h_ret=%li "
707 "shca=%p hca_hndl=%lx mw=%p",
708 h_ret
, shca
, shca
->ipz_hca_handle
.handle
, e_mw
);
709 ib_mw
= ERR_PTR(ehca2ib_return_code(h_ret
));
712 /* successful MW allocation */
713 e_mw
->ipz_mw_handle
= hipzout
.handle
;
714 e_mw
->ib_mw
.rkey
= hipzout
.rkey
;
718 ehca_mw_delete(e_mw
);
721 ehca_err(pd
->device
, "h_ret=%li pd=%p", PTR_ERR(ib_mw
), pd
);
723 } /* end ehca_alloc_mw() */
725 /*----------------------------------------------------------------------*/
727 int ehca_bind_mw(struct ib_qp
*qp
,
729 struct ib_mw_bind
*mw_bind
)
731 /* TODO: not supported up to now */
732 ehca_gen_err("bind MW currently not supported by HCAD");
735 } /* end ehca_bind_mw() */
737 /*----------------------------------------------------------------------*/
739 int ehca_dealloc_mw(struct ib_mw
*mw
)
742 struct ehca_shca
*shca
=
743 container_of(mw
->device
, struct ehca_shca
, ib_device
);
744 struct ehca_mw
*e_mw
= container_of(mw
, struct ehca_mw
, ib_mw
);
746 h_ret
= hipz_h_free_resource_mw(shca
->ipz_hca_handle
, e_mw
);
747 if (h_ret
!= H_SUCCESS
) {
748 ehca_err(mw
->device
, "hipz_free_mw failed, h_ret=%li shca=%p "
749 "mw=%p rkey=%x hca_hndl=%lx mw_hndl=%lx",
750 h_ret
, shca
, mw
, mw
->rkey
, shca
->ipz_hca_handle
.handle
,
751 e_mw
->ipz_mw_handle
.handle
);
752 return ehca2ib_return_code(h_ret
);
754 /* successful deallocation */
755 ehca_mw_delete(e_mw
);
757 } /* end ehca_dealloc_mw() */
759 /*----------------------------------------------------------------------*/
761 struct ib_fmr
*ehca_alloc_fmr(struct ib_pd
*pd
,
763 struct ib_fmr_attr
*fmr_attr
)
765 struct ib_fmr
*ib_fmr
;
766 struct ehca_shca
*shca
=
767 container_of(pd
->device
, struct ehca_shca
, ib_device
);
768 struct ehca_pd
*e_pd
= container_of(pd
, struct ehca_pd
, ib_pd
);
769 struct ehca_mr
*e_fmr
;
771 u32 tmp_lkey
, tmp_rkey
;
772 struct ehca_mr_pginfo pginfo
;
775 /* check other parameters */
776 if (((mr_access_flags
& IB_ACCESS_REMOTE_WRITE
) &&
777 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
)) ||
778 ((mr_access_flags
& IB_ACCESS_REMOTE_ATOMIC
) &&
779 !(mr_access_flags
& IB_ACCESS_LOCAL_WRITE
))) {
781 * Remote Write Access requires Local Write Access
782 * Remote Atomic Access requires Local Write Access
784 ehca_err(pd
->device
, "bad input values: mr_access_flags=%x",
786 ib_fmr
= ERR_PTR(-EINVAL
);
787 goto alloc_fmr_exit0
;
789 if (mr_access_flags
& IB_ACCESS_MW_BIND
) {
790 ehca_err(pd
->device
, "bad input values: mr_access_flags=%x",
792 ib_fmr
= ERR_PTR(-EINVAL
);
793 goto alloc_fmr_exit0
;
795 if ((fmr_attr
->max_pages
== 0) || (fmr_attr
->max_maps
== 0)) {
796 ehca_err(pd
->device
, "bad input values: fmr_attr->max_pages=%x "
797 "fmr_attr->max_maps=%x fmr_attr->page_shift=%x",
798 fmr_attr
->max_pages
, fmr_attr
->max_maps
,
799 fmr_attr
->page_shift
);
800 ib_fmr
= ERR_PTR(-EINVAL
);
801 goto alloc_fmr_exit0
;
804 hw_pgsize
= 1 << fmr_attr
->page_shift
;
805 if (!(hw_pgsize
& shca
->hca_cap_mr_pgsize
)) {
806 ehca_err(pd
->device
, "unsupported fmr_attr->page_shift=%x",
807 fmr_attr
->page_shift
);
808 ib_fmr
= ERR_PTR(-EINVAL
);
809 goto alloc_fmr_exit0
;
812 e_fmr
= ehca_mr_new();
814 ib_fmr
= ERR_PTR(-ENOMEM
);
815 goto alloc_fmr_exit0
;
817 e_fmr
->flags
|= EHCA_MR_FLAG_FMR
;
819 /* register MR on HCA */
820 memset(&pginfo
, 0, sizeof(pginfo
));
821 pginfo
.hwpage_size
= hw_pgsize
;
823 * pginfo.num_hwpages==0, ie register_rpages() will not be called
824 * but deferred to map_phys_fmr()
826 ret
= ehca_reg_mr(shca
, e_fmr
, NULL
,
827 fmr_attr
->max_pages
* (1 << fmr_attr
->page_shift
),
828 mr_access_flags
, e_pd
, &pginfo
,
829 &tmp_lkey
, &tmp_rkey
);
831 ib_fmr
= ERR_PTR(ret
);
832 goto alloc_fmr_exit1
;
836 e_fmr
->hwpage_size
= hw_pgsize
;
837 e_fmr
->fmr_page_size
= 1 << fmr_attr
->page_shift
;
838 e_fmr
->fmr_max_pages
= fmr_attr
->max_pages
;
839 e_fmr
->fmr_max_maps
= fmr_attr
->max_maps
;
840 e_fmr
->fmr_map_cnt
= 0;
841 return &e_fmr
->ib
.ib_fmr
;
844 ehca_mr_delete(e_fmr
);
847 } /* end ehca_alloc_fmr() */
849 /*----------------------------------------------------------------------*/
851 int ehca_map_phys_fmr(struct ib_fmr
*fmr
,
857 struct ehca_shca
*shca
=
858 container_of(fmr
->device
, struct ehca_shca
, ib_device
);
859 struct ehca_mr
*e_fmr
= container_of(fmr
, struct ehca_mr
, ib
.ib_fmr
);
860 struct ehca_pd
*e_pd
= container_of(fmr
->pd
, struct ehca_pd
, ib_pd
);
861 struct ehca_mr_pginfo pginfo
;
862 u32 tmp_lkey
, tmp_rkey
;
864 if (!(e_fmr
->flags
& EHCA_MR_FLAG_FMR
)) {
865 ehca_err(fmr
->device
, "not a FMR, e_fmr=%p e_fmr->flags=%x",
866 e_fmr
, e_fmr
->flags
);
868 goto map_phys_fmr_exit0
;
870 ret
= ehca_fmr_check_page_list(e_fmr
, page_list
, list_len
);
872 goto map_phys_fmr_exit0
;
873 if (iova
% e_fmr
->fmr_page_size
) {
874 /* only whole-numbered pages */
875 ehca_err(fmr
->device
, "bad iova, iova=%lx fmr_page_size=%x",
876 iova
, e_fmr
->fmr_page_size
);
878 goto map_phys_fmr_exit0
;
880 if (e_fmr
->fmr_map_cnt
>= e_fmr
->fmr_max_maps
) {
881 /* HCAD does not limit the maps, however trace this anyway */
882 ehca_info(fmr
->device
, "map limit exceeded, fmr=%p "
883 "e_fmr->fmr_map_cnt=%x e_fmr->fmr_max_maps=%x",
884 fmr
, e_fmr
->fmr_map_cnt
, e_fmr
->fmr_max_maps
);
887 memset(&pginfo
, 0, sizeof(pginfo
));
888 pginfo
.type
= EHCA_MR_PGI_FMR
;
889 pginfo
.num_kpages
= list_len
;
890 pginfo
.hwpage_size
= e_fmr
->hwpage_size
;
892 list_len
* e_fmr
->fmr_page_size
/ pginfo
.hwpage_size
;
893 pginfo
.u
.fmr
.page_list
= page_list
;
895 (iova
& (e_fmr
->fmr_page_size
-1)) / pginfo
.hwpage_size
;
896 pginfo
.u
.fmr
.fmr_pgsize
= e_fmr
->fmr_page_size
;
898 ret
= ehca_rereg_mr(shca
, e_fmr
, (u64
*)iova
,
899 list_len
* e_fmr
->fmr_page_size
,
900 e_fmr
->acl
, e_pd
, &pginfo
, &tmp_lkey
, &tmp_rkey
);
902 goto map_phys_fmr_exit0
;
904 /* successful reregistration */
905 e_fmr
->fmr_map_cnt
++;
906 e_fmr
->ib
.ib_fmr
.lkey
= tmp_lkey
;
907 e_fmr
->ib
.ib_fmr
.rkey
= tmp_rkey
;
912 ehca_err(fmr
->device
, "ret=%i fmr=%p page_list=%p list_len=%x "
913 "iova=%lx", ret
, fmr
, page_list
, list_len
, iova
);
915 } /* end ehca_map_phys_fmr() */
917 /*----------------------------------------------------------------------*/
919 int ehca_unmap_fmr(struct list_head
*fmr_list
)
922 struct ib_fmr
*ib_fmr
;
923 struct ehca_shca
*shca
= NULL
;
924 struct ehca_shca
*prev_shca
;
925 struct ehca_mr
*e_fmr
;
927 u32 unmap_fmr_cnt
= 0;
929 /* check all FMR belong to same SHCA, and check internal flag */
930 list_for_each_entry(ib_fmr
, fmr_list
, list
) {
933 ehca_gen_err("bad fmr=%p in list", ib_fmr
);
935 goto unmap_fmr_exit0
;
937 shca
= container_of(ib_fmr
->device
, struct ehca_shca
,
939 e_fmr
= container_of(ib_fmr
, struct ehca_mr
, ib
.ib_fmr
);
940 if ((shca
!= prev_shca
) && prev_shca
) {
941 ehca_err(&shca
->ib_device
, "SHCA mismatch, shca=%p "
942 "prev_shca=%p e_fmr=%p",
943 shca
, prev_shca
, e_fmr
);
945 goto unmap_fmr_exit0
;
947 if (!(e_fmr
->flags
& EHCA_MR_FLAG_FMR
)) {
948 ehca_err(&shca
->ib_device
, "not a FMR, e_fmr=%p "
949 "e_fmr->flags=%x", e_fmr
, e_fmr
->flags
);
951 goto unmap_fmr_exit0
;
956 /* loop over all FMRs to unmap */
957 list_for_each_entry(ib_fmr
, fmr_list
, list
) {
959 e_fmr
= container_of(ib_fmr
, struct ehca_mr
, ib
.ib_fmr
);
960 shca
= container_of(ib_fmr
->device
, struct ehca_shca
,
962 ret
= ehca_unmap_one_fmr(shca
, e_fmr
);
964 /* unmap failed, stop unmapping of rest of FMRs */
965 ehca_err(&shca
->ib_device
, "unmap of one FMR failed, "
966 "stop rest, e_fmr=%p num_fmr=%x "
967 "unmap_fmr_cnt=%x lkey=%x", e_fmr
, num_fmr
,
968 unmap_fmr_cnt
, e_fmr
->ib
.ib_fmr
.lkey
);
969 goto unmap_fmr_exit0
;
975 ehca_gen_err("ret=%i fmr_list=%p num_fmr=%x unmap_fmr_cnt=%x",
976 ret
, fmr_list
, num_fmr
, unmap_fmr_cnt
);
978 } /* end ehca_unmap_fmr() */
980 /*----------------------------------------------------------------------*/
982 int ehca_dealloc_fmr(struct ib_fmr
*fmr
)
986 struct ehca_shca
*shca
=
987 container_of(fmr
->device
, struct ehca_shca
, ib_device
);
988 struct ehca_mr
*e_fmr
= container_of(fmr
, struct ehca_mr
, ib
.ib_fmr
);
990 if (!(e_fmr
->flags
& EHCA_MR_FLAG_FMR
)) {
991 ehca_err(fmr
->device
, "not a FMR, e_fmr=%p e_fmr->flags=%x",
992 e_fmr
, e_fmr
->flags
);
997 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_fmr
);
998 if (h_ret
!= H_SUCCESS
) {
999 ehca_err(fmr
->device
, "hipz_free_mr failed, h_ret=%li e_fmr=%p "
1000 "hca_hndl=%lx fmr_hndl=%lx fmr->lkey=%x",
1001 h_ret
, e_fmr
, shca
->ipz_hca_handle
.handle
,
1002 e_fmr
->ipz_mr_handle
.handle
, fmr
->lkey
);
1003 ret
= ehca2ib_return_code(h_ret
);
1004 goto free_fmr_exit0
;
1006 /* successful deregistration */
1007 ehca_mr_delete(e_fmr
);
1012 ehca_err(&shca
->ib_device
, "ret=%i fmr=%p", ret
, fmr
);
1014 } /* end ehca_dealloc_fmr() */
1016 /*----------------------------------------------------------------------*/
1018 int ehca_reg_mr(struct ehca_shca
*shca
,
1019 struct ehca_mr
*e_mr
,
1023 struct ehca_pd
*e_pd
,
1024 struct ehca_mr_pginfo
*pginfo
,
1031 struct ehca_mr_hipzout_parms hipzout
;
1033 ehca_mrmw_map_acl(acl
, &hipz_acl
);
1034 ehca_mrmw_set_pgsize_hipz_acl(pginfo
->hwpage_size
, &hipz_acl
);
1035 if (ehca_use_hp_mr
== 1)
1036 hipz_acl
|= 0x00000001;
1038 h_ret
= hipz_h_alloc_resource_mr(shca
->ipz_hca_handle
, e_mr
,
1039 (u64
)iova_start
, size
, hipz_acl
,
1040 e_pd
->fw_pd
, &hipzout
);
1041 if (h_ret
!= H_SUCCESS
) {
1042 ehca_err(&shca
->ib_device
, "hipz_alloc_mr failed, h_ret=%li "
1043 "hca_hndl=%lx", h_ret
, shca
->ipz_hca_handle
.handle
);
1044 ret
= ehca2ib_return_code(h_ret
);
1045 goto ehca_reg_mr_exit0
;
1048 e_mr
->ipz_mr_handle
= hipzout
.handle
;
1050 ret
= ehca_reg_mr_rpages(shca
, e_mr
, pginfo
);
1052 goto ehca_reg_mr_exit1
;
1054 /* successful registration */
1055 e_mr
->num_kpages
= pginfo
->num_kpages
;
1056 e_mr
->num_hwpages
= pginfo
->num_hwpages
;
1057 e_mr
->hwpage_size
= pginfo
->hwpage_size
;
1058 e_mr
->start
= iova_start
;
1061 *lkey
= hipzout
.lkey
;
1062 *rkey
= hipzout
.rkey
;
1066 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_mr
);
1067 if (h_ret
!= H_SUCCESS
) {
1068 ehca_err(&shca
->ib_device
, "h_ret=%li shca=%p e_mr=%p "
1069 "iova_start=%p size=%lx acl=%x e_pd=%p lkey=%x "
1070 "pginfo=%p num_kpages=%lx num_hwpages=%lx ret=%i",
1071 h_ret
, shca
, e_mr
, iova_start
, size
, acl
, e_pd
,
1072 hipzout
.lkey
, pginfo
, pginfo
->num_kpages
,
1073 pginfo
->num_hwpages
, ret
);
1074 ehca_err(&shca
->ib_device
, "internal error in ehca_reg_mr, "
1079 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_mr=%p "
1080 "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
1081 "num_kpages=%lx num_hwpages=%lx",
1082 ret
, shca
, e_mr
, iova_start
, size
, acl
, e_pd
, pginfo
,
1083 pginfo
->num_kpages
, pginfo
->num_hwpages
);
1085 } /* end ehca_reg_mr() */
1087 /*----------------------------------------------------------------------*/
1089 int ehca_reg_mr_rpages(struct ehca_shca
*shca
,
1090 struct ehca_mr
*e_mr
,
1091 struct ehca_mr_pginfo
*pginfo
)
1100 if (!pginfo
->num_hwpages
) /* in case of fmr */
1103 kpage
= ehca_alloc_fw_ctrlblock(GFP_KERNEL
);
1105 ehca_err(&shca
->ib_device
, "kpage alloc failed");
1107 goto ehca_reg_mr_rpages_exit0
;
1110 /* max MAX_RPAGES ehca mr pages per register call */
1111 for (i
= 0; i
< NUM_CHUNKS(pginfo
->num_hwpages
, MAX_RPAGES
); i
++) {
1113 if (i
== NUM_CHUNKS(pginfo
->num_hwpages
, MAX_RPAGES
) - 1) {
1114 rnum
= pginfo
->num_hwpages
% MAX_RPAGES
; /* last shot */
1116 rnum
= MAX_RPAGES
; /* last shot is full */
1120 ret
= ehca_set_pagebuf(pginfo
, rnum
, kpage
);
1122 ehca_err(&shca
->ib_device
, "ehca_set_pagebuf "
1123 "bad rc, ret=%i rnum=%x kpage=%p",
1125 goto ehca_reg_mr_rpages_exit1
;
1129 rpage
= virt_to_abs(kpage
);
1131 ehca_err(&shca
->ib_device
, "kpage=%p i=%x",
1134 goto ehca_reg_mr_rpages_exit1
;
1139 h_ret
= hipz_h_register_rpage_mr(
1140 shca
->ipz_hca_handle
, e_mr
,
1141 ehca_encode_hwpage_size(pginfo
->hwpage_size
),
1144 if (i
== NUM_CHUNKS(pginfo
->num_hwpages
, MAX_RPAGES
) - 1) {
1146 * check for 'registration complete'==H_SUCCESS
1147 * and for 'page registered'==H_PAGE_REGISTERED
1149 if (h_ret
!= H_SUCCESS
) {
1150 ehca_err(&shca
->ib_device
, "last "
1151 "hipz_reg_rpage_mr failed, h_ret=%li "
1152 "e_mr=%p i=%x hca_hndl=%lx mr_hndl=%lx"
1153 " lkey=%x", h_ret
, e_mr
, i
,
1154 shca
->ipz_hca_handle
.handle
,
1155 e_mr
->ipz_mr_handle
.handle
,
1156 e_mr
->ib
.ib_mr
.lkey
);
1157 ret
= ehca2ib_return_code(h_ret
);
1161 } else if (h_ret
!= H_PAGE_REGISTERED
) {
1162 ehca_err(&shca
->ib_device
, "hipz_reg_rpage_mr failed, "
1163 "h_ret=%li e_mr=%p i=%x lkey=%x hca_hndl=%lx "
1164 "mr_hndl=%lx", h_ret
, e_mr
, i
,
1165 e_mr
->ib
.ib_mr
.lkey
,
1166 shca
->ipz_hca_handle
.handle
,
1167 e_mr
->ipz_mr_handle
.handle
);
1168 ret
= ehca2ib_return_code(h_ret
);
1175 ehca_reg_mr_rpages_exit1
:
1176 ehca_free_fw_ctrlblock(kpage
);
1177 ehca_reg_mr_rpages_exit0
:
1179 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_mr=%p pginfo=%p "
1180 "num_kpages=%lx num_hwpages=%lx", ret
, shca
, e_mr
,
1181 pginfo
, pginfo
->num_kpages
, pginfo
->num_hwpages
);
1183 } /* end ehca_reg_mr_rpages() */
1185 /*----------------------------------------------------------------------*/
1187 inline int ehca_rereg_mr_rereg1(struct ehca_shca
*shca
,
1188 struct ehca_mr
*e_mr
,
1192 struct ehca_pd
*e_pd
,
1193 struct ehca_mr_pginfo
*pginfo
,
1202 struct ehca_mr_pginfo pginfo_save
;
1203 struct ehca_mr_hipzout_parms hipzout
;
1205 ehca_mrmw_map_acl(acl
, &hipz_acl
);
1206 ehca_mrmw_set_pgsize_hipz_acl(pginfo
->hwpage_size
, &hipz_acl
);
1208 kpage
= ehca_alloc_fw_ctrlblock(GFP_KERNEL
);
1210 ehca_err(&shca
->ib_device
, "kpage alloc failed");
1212 goto ehca_rereg_mr_rereg1_exit0
;
1215 pginfo_save
= *pginfo
;
1216 ret
= ehca_set_pagebuf(pginfo
, pginfo
->num_hwpages
, kpage
);
1218 ehca_err(&shca
->ib_device
, "set pagebuf failed, e_mr=%p "
1219 "pginfo=%p type=%x num_kpages=%lx num_hwpages=%lx "
1220 "kpage=%p", e_mr
, pginfo
, pginfo
->type
,
1221 pginfo
->num_kpages
, pginfo
->num_hwpages
, kpage
);
1222 goto ehca_rereg_mr_rereg1_exit1
;
1224 rpage
= virt_to_abs(kpage
);
1226 ehca_err(&shca
->ib_device
, "kpage=%p", kpage
);
1228 goto ehca_rereg_mr_rereg1_exit1
;
1230 h_ret
= hipz_h_reregister_pmr(shca
->ipz_hca_handle
, e_mr
,
1231 (u64
)iova_start
, size
, hipz_acl
,
1232 e_pd
->fw_pd
, rpage
, &hipzout
);
1233 if (h_ret
!= H_SUCCESS
) {
1235 * reregistration unsuccessful, try it again with the 3 hCalls,
1236 * e.g. this is required in case H_MR_CONDITION
1237 * (MW bound or MR is shared)
1239 ehca_warn(&shca
->ib_device
, "hipz_h_reregister_pmr failed "
1240 "(Rereg1), h_ret=%li e_mr=%p", h_ret
, e_mr
);
1241 *pginfo
= pginfo_save
;
1243 } else if ((u64
*)hipzout
.vaddr
!= iova_start
) {
1244 ehca_err(&shca
->ib_device
, "PHYP changed iova_start in "
1245 "rereg_pmr, iova_start=%p iova_start_out=%lx e_mr=%p "
1246 "mr_handle=%lx lkey=%x lkey_out=%x", iova_start
,
1247 hipzout
.vaddr
, e_mr
, e_mr
->ipz_mr_handle
.handle
,
1248 e_mr
->ib
.ib_mr
.lkey
, hipzout
.lkey
);
1252 * successful reregistration
1253 * note: start and start_out are identical for eServer HCAs
1255 e_mr
->num_kpages
= pginfo
->num_kpages
;
1256 e_mr
->num_hwpages
= pginfo
->num_hwpages
;
1257 e_mr
->hwpage_size
= pginfo
->hwpage_size
;
1258 e_mr
->start
= iova_start
;
1261 *lkey
= hipzout
.lkey
;
1262 *rkey
= hipzout
.rkey
;
1265 ehca_rereg_mr_rereg1_exit1
:
1266 ehca_free_fw_ctrlblock(kpage
);
1267 ehca_rereg_mr_rereg1_exit0
:
1268 if ( ret
&& (ret
!= -EAGAIN
) )
1269 ehca_err(&shca
->ib_device
, "ret=%i lkey=%x rkey=%x "
1270 "pginfo=%p num_kpages=%lx num_hwpages=%lx",
1271 ret
, *lkey
, *rkey
, pginfo
, pginfo
->num_kpages
,
1272 pginfo
->num_hwpages
);
1274 } /* end ehca_rereg_mr_rereg1() */
1276 /*----------------------------------------------------------------------*/
1278 int ehca_rereg_mr(struct ehca_shca
*shca
,
1279 struct ehca_mr
*e_mr
,
1283 struct ehca_pd
*e_pd
,
1284 struct ehca_mr_pginfo
*pginfo
,
1290 int rereg_1_hcall
= 1; /* 1: use hipz_h_reregister_pmr directly */
1291 int rereg_3_hcall
= 0; /* 1: use 3 hipz calls for reregistration */
1293 /* first determine reregistration hCall(s) */
1294 if ((pginfo
->num_hwpages
> MAX_RPAGES
) ||
1295 (e_mr
->num_hwpages
> MAX_RPAGES
) ||
1296 (pginfo
->num_hwpages
> e_mr
->num_hwpages
)) {
1297 ehca_dbg(&shca
->ib_device
, "Rereg3 case, "
1298 "pginfo->num_hwpages=%lx e_mr->num_hwpages=%x",
1299 pginfo
->num_hwpages
, e_mr
->num_hwpages
);
1304 if (e_mr
->flags
& EHCA_MR_FLAG_MAXMR
) { /* check for max-MR */
1307 e_mr
->flags
&= ~EHCA_MR_FLAG_MAXMR
;
1308 ehca_err(&shca
->ib_device
, "Rereg MR for max-MR! e_mr=%p",
1312 if (rereg_1_hcall
) {
1313 ret
= ehca_rereg_mr_rereg1(shca
, e_mr
, iova_start
, size
,
1314 acl
, e_pd
, pginfo
, lkey
, rkey
);
1319 goto ehca_rereg_mr_exit0
;
1323 if (rereg_3_hcall
) {
1324 struct ehca_mr save_mr
;
1326 /* first deregister old MR */
1327 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_mr
);
1328 if (h_ret
!= H_SUCCESS
) {
1329 ehca_err(&shca
->ib_device
, "hipz_free_mr failed, "
1330 "h_ret=%li e_mr=%p hca_hndl=%lx mr_hndl=%lx "
1332 h_ret
, e_mr
, shca
->ipz_hca_handle
.handle
,
1333 e_mr
->ipz_mr_handle
.handle
,
1334 e_mr
->ib
.ib_mr
.lkey
);
1335 ret
= ehca2ib_return_code(h_ret
);
1336 goto ehca_rereg_mr_exit0
;
1338 /* clean ehca_mr_t, without changing struct ib_mr and lock */
1340 ehca_mr_deletenew(e_mr
);
1342 /* set some MR values */
1343 e_mr
->flags
= save_mr
.flags
;
1344 e_mr
->hwpage_size
= save_mr
.hwpage_size
;
1345 e_mr
->fmr_page_size
= save_mr
.fmr_page_size
;
1346 e_mr
->fmr_max_pages
= save_mr
.fmr_max_pages
;
1347 e_mr
->fmr_max_maps
= save_mr
.fmr_max_maps
;
1348 e_mr
->fmr_map_cnt
= save_mr
.fmr_map_cnt
;
1350 ret
= ehca_reg_mr(shca
, e_mr
, iova_start
, size
, acl
,
1351 e_pd
, pginfo
, lkey
, rkey
);
1353 u32 offset
= (u64
)(&e_mr
->flags
) - (u64
)e_mr
;
1354 memcpy(&e_mr
->flags
, &(save_mr
.flags
),
1355 sizeof(struct ehca_mr
) - offset
);
1356 goto ehca_rereg_mr_exit0
;
1360 ehca_rereg_mr_exit0
:
1362 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_mr=%p "
1363 "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
1364 "num_kpages=%lx lkey=%x rkey=%x rereg_1_hcall=%x "
1365 "rereg_3_hcall=%x", ret
, shca
, e_mr
, iova_start
, size
,
1366 acl
, e_pd
, pginfo
, pginfo
->num_kpages
, *lkey
, *rkey
,
1367 rereg_1_hcall
, rereg_3_hcall
);
1369 } /* end ehca_rereg_mr() */
1371 /*----------------------------------------------------------------------*/
1373 int ehca_unmap_one_fmr(struct ehca_shca
*shca
,
1374 struct ehca_mr
*e_fmr
)
1378 struct ehca_pd
*e_pd
=
1379 container_of(e_fmr
->ib
.ib_fmr
.pd
, struct ehca_pd
, ib_pd
);
1380 struct ehca_mr save_fmr
;
1381 u32 tmp_lkey
, tmp_rkey
;
1382 struct ehca_mr_pginfo pginfo
;
1383 struct ehca_mr_hipzout_parms hipzout
;
1384 struct ehca_mr save_mr
;
1386 if (e_fmr
->fmr_max_pages
<= MAX_RPAGES
) {
1388 * note: after using rereg hcall with len=0,
1389 * rereg hcall must be used again for registering pages
1391 h_ret
= hipz_h_reregister_pmr(shca
->ipz_hca_handle
, e_fmr
, 0,
1392 0, 0, e_pd
->fw_pd
, 0, &hipzout
);
1393 if (h_ret
== H_SUCCESS
) {
1394 /* successful reregistration */
1395 e_fmr
->start
= NULL
;
1397 tmp_lkey
= hipzout
.lkey
;
1398 tmp_rkey
= hipzout
.rkey
;
1402 * should not happen, because length checked above,
1403 * FMRs are not shared and no MW bound to FMRs
1405 ehca_err(&shca
->ib_device
, "hipz_reregister_pmr failed "
1406 "(Rereg1), h_ret=%li e_fmr=%p hca_hndl=%lx "
1407 "mr_hndl=%lx lkey=%x lkey_out=%x",
1408 h_ret
, e_fmr
, shca
->ipz_hca_handle
.handle
,
1409 e_fmr
->ipz_mr_handle
.handle
,
1410 e_fmr
->ib
.ib_fmr
.lkey
, hipzout
.lkey
);
1411 /* try free and rereg */
1414 /* first free old FMR */
1415 h_ret
= hipz_h_free_resource_mr(shca
->ipz_hca_handle
, e_fmr
);
1416 if (h_ret
!= H_SUCCESS
) {
1417 ehca_err(&shca
->ib_device
, "hipz_free_mr failed, "
1418 "h_ret=%li e_fmr=%p hca_hndl=%lx mr_hndl=%lx "
1420 h_ret
, e_fmr
, shca
->ipz_hca_handle
.handle
,
1421 e_fmr
->ipz_mr_handle
.handle
,
1422 e_fmr
->ib
.ib_fmr
.lkey
);
1423 ret
= ehca2ib_return_code(h_ret
);
1424 goto ehca_unmap_one_fmr_exit0
;
1426 /* clean ehca_mr_t, without changing lock */
1428 ehca_mr_deletenew(e_fmr
);
1430 /* set some MR values */
1431 e_fmr
->flags
= save_fmr
.flags
;
1432 e_fmr
->hwpage_size
= save_fmr
.hwpage_size
;
1433 e_fmr
->fmr_page_size
= save_fmr
.fmr_page_size
;
1434 e_fmr
->fmr_max_pages
= save_fmr
.fmr_max_pages
;
1435 e_fmr
->fmr_max_maps
= save_fmr
.fmr_max_maps
;
1436 e_fmr
->fmr_map_cnt
= save_fmr
.fmr_map_cnt
;
1437 e_fmr
->acl
= save_fmr
.acl
;
1439 memset(&pginfo
, 0, sizeof(pginfo
));
1440 pginfo
.type
= EHCA_MR_PGI_FMR
;
1441 ret
= ehca_reg_mr(shca
, e_fmr
, NULL
,
1442 (e_fmr
->fmr_max_pages
* e_fmr
->fmr_page_size
),
1443 e_fmr
->acl
, e_pd
, &pginfo
, &tmp_lkey
,
1446 u32 offset
= (u64
)(&e_fmr
->flags
) - (u64
)e_fmr
;
1447 memcpy(&e_fmr
->flags
, &(save_mr
.flags
),
1448 sizeof(struct ehca_mr
) - offset
);
1451 ehca_unmap_one_fmr_exit0
:
1453 ehca_err(&shca
->ib_device
, "ret=%i tmp_lkey=%x tmp_rkey=%x "
1455 ret
, tmp_lkey
, tmp_rkey
, e_fmr
->fmr_max_pages
);
1457 } /* end ehca_unmap_one_fmr() */
1459 /*----------------------------------------------------------------------*/
1461 int ehca_reg_smr(struct ehca_shca
*shca
,
1462 struct ehca_mr
*e_origmr
,
1463 struct ehca_mr
*e_newmr
,
1466 struct ehca_pd
*e_pd
,
1473 struct ehca_mr_hipzout_parms hipzout
;
1475 ehca_mrmw_map_acl(acl
, &hipz_acl
);
1476 ehca_mrmw_set_pgsize_hipz_acl(e_origmr
->hwpage_size
, &hipz_acl
);
1478 h_ret
= hipz_h_register_smr(shca
->ipz_hca_handle
, e_newmr
, e_origmr
,
1479 (u64
)iova_start
, hipz_acl
, e_pd
->fw_pd
,
1481 if (h_ret
!= H_SUCCESS
) {
1482 ehca_err(&shca
->ib_device
, "hipz_reg_smr failed, h_ret=%li "
1483 "shca=%p e_origmr=%p e_newmr=%p iova_start=%p acl=%x "
1484 "e_pd=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1485 h_ret
, shca
, e_origmr
, e_newmr
, iova_start
, acl
, e_pd
,
1486 shca
->ipz_hca_handle
.handle
,
1487 e_origmr
->ipz_mr_handle
.handle
,
1488 e_origmr
->ib
.ib_mr
.lkey
);
1489 ret
= ehca2ib_return_code(h_ret
);
1490 goto ehca_reg_smr_exit0
;
1492 /* successful registration */
1493 e_newmr
->num_kpages
= e_origmr
->num_kpages
;
1494 e_newmr
->num_hwpages
= e_origmr
->num_hwpages
;
1495 e_newmr
->hwpage_size
= e_origmr
->hwpage_size
;
1496 e_newmr
->start
= iova_start
;
1497 e_newmr
->size
= e_origmr
->size
;
1499 e_newmr
->ipz_mr_handle
= hipzout
.handle
;
1500 *lkey
= hipzout
.lkey
;
1501 *rkey
= hipzout
.rkey
;
1506 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_origmr=%p "
1507 "e_newmr=%p iova_start=%p acl=%x e_pd=%p",
1508 ret
, shca
, e_origmr
, e_newmr
, iova_start
, acl
, e_pd
);
1510 } /* end ehca_reg_smr() */
1512 /*----------------------------------------------------------------------*/
1514 /* register internal max-MR to internal SHCA */
1515 int ehca_reg_internal_maxmr(
1516 struct ehca_shca
*shca
,
1517 struct ehca_pd
*e_pd
,
1518 struct ehca_mr
**e_maxmr
) /*OUT*/
1521 struct ehca_mr
*e_mr
;
1524 struct ehca_mr_pginfo pginfo
;
1525 struct ib_phys_buf ib_pbuf
;
1530 e_mr
= ehca_mr_new();
1532 ehca_err(&shca
->ib_device
, "out of memory");
1534 goto ehca_reg_internal_maxmr_exit0
;
1536 e_mr
->flags
|= EHCA_MR_FLAG_MAXMR
;
1538 /* register internal max-MR on HCA */
1539 size_maxmr
= (u64
)high_memory
- PAGE_OFFSET
;
1540 iova_start
= (u64
*)KERNELBASE
;
1542 ib_pbuf
.size
= size_maxmr
;
1543 num_kpages
= NUM_CHUNKS(((u64
)iova_start
% PAGE_SIZE
) + size_maxmr
,
1545 hw_pgsize
= ehca_get_max_hwpage_size(shca
);
1546 num_hwpages
= NUM_CHUNKS(((u64
)iova_start
% hw_pgsize
) + size_maxmr
,
1549 memset(&pginfo
, 0, sizeof(pginfo
));
1550 pginfo
.type
= EHCA_MR_PGI_PHYS
;
1551 pginfo
.num_kpages
= num_kpages
;
1552 pginfo
.num_hwpages
= num_hwpages
;
1553 pginfo
.hwpage_size
= hw_pgsize
;
1554 pginfo
.u
.phy
.num_phys_buf
= 1;
1555 pginfo
.u
.phy
.phys_buf_array
= &ib_pbuf
;
1557 ret
= ehca_reg_mr(shca
, e_mr
, iova_start
, size_maxmr
, 0, e_pd
,
1558 &pginfo
, &e_mr
->ib
.ib_mr
.lkey
,
1559 &e_mr
->ib
.ib_mr
.rkey
);
1561 ehca_err(&shca
->ib_device
, "reg of internal max MR failed, "
1562 "e_mr=%p iova_start=%p size_maxmr=%lx num_kpages=%x "
1563 "num_hwpages=%x", e_mr
, iova_start
, size_maxmr
,
1564 num_kpages
, num_hwpages
);
1565 goto ehca_reg_internal_maxmr_exit1
;
1568 /* successful registration of all pages */
1569 e_mr
->ib
.ib_mr
.device
= e_pd
->ib_pd
.device
;
1570 e_mr
->ib
.ib_mr
.pd
= &e_pd
->ib_pd
;
1571 e_mr
->ib
.ib_mr
.uobject
= NULL
;
1572 atomic_inc(&(e_pd
->ib_pd
.usecnt
));
1573 atomic_set(&(e_mr
->ib
.ib_mr
.usecnt
), 0);
1577 ehca_reg_internal_maxmr_exit1
:
1578 ehca_mr_delete(e_mr
);
1579 ehca_reg_internal_maxmr_exit0
:
1581 ehca_err(&shca
->ib_device
, "ret=%i shca=%p e_pd=%p e_maxmr=%p",
1582 ret
, shca
, e_pd
, e_maxmr
);
1584 } /* end ehca_reg_internal_maxmr() */
1586 /*----------------------------------------------------------------------*/
1588 int ehca_reg_maxmr(struct ehca_shca
*shca
,
1589 struct ehca_mr
*e_newmr
,
1592 struct ehca_pd
*e_pd
,
1597 struct ehca_mr
*e_origmr
= shca
->maxmr
;
1599 struct ehca_mr_hipzout_parms hipzout
;
1601 ehca_mrmw_map_acl(acl
, &hipz_acl
);
1602 ehca_mrmw_set_pgsize_hipz_acl(e_origmr
->hwpage_size
, &hipz_acl
);
1604 h_ret
= hipz_h_register_smr(shca
->ipz_hca_handle
, e_newmr
, e_origmr
,
1605 (u64
)iova_start
, hipz_acl
, e_pd
->fw_pd
,
1607 if (h_ret
!= H_SUCCESS
) {
1608 ehca_err(&shca
->ib_device
, "hipz_reg_smr failed, h_ret=%li "
1609 "e_origmr=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1610 h_ret
, e_origmr
, shca
->ipz_hca_handle
.handle
,
1611 e_origmr
->ipz_mr_handle
.handle
,
1612 e_origmr
->ib
.ib_mr
.lkey
);
1613 return ehca2ib_return_code(h_ret
);
1615 /* successful registration */
1616 e_newmr
->num_kpages
= e_origmr
->num_kpages
;
1617 e_newmr
->num_hwpages
= e_origmr
->num_hwpages
;
1618 e_newmr
->hwpage_size
= e_origmr
->hwpage_size
;
1619 e_newmr
->start
= iova_start
;
1620 e_newmr
->size
= e_origmr
->size
;
1622 e_newmr
->ipz_mr_handle
= hipzout
.handle
;
1623 *lkey
= hipzout
.lkey
;
1624 *rkey
= hipzout
.rkey
;
1626 } /* end ehca_reg_maxmr() */
1628 /*----------------------------------------------------------------------*/
1630 int ehca_dereg_internal_maxmr(struct ehca_shca
*shca
)
1633 struct ehca_mr
*e_maxmr
;
1634 struct ib_pd
*ib_pd
;
1637 ehca_err(&shca
->ib_device
, "bad call, shca=%p", shca
);
1639 goto ehca_dereg_internal_maxmr_exit0
;
1642 e_maxmr
= shca
->maxmr
;
1643 ib_pd
= e_maxmr
->ib
.ib_mr
.pd
;
1644 shca
->maxmr
= NULL
; /* remove internal max-MR indication from SHCA */
1646 ret
= ehca_dereg_mr(&e_maxmr
->ib
.ib_mr
);
1648 ehca_err(&shca
->ib_device
, "dereg internal max-MR failed, "
1649 "ret=%i e_maxmr=%p shca=%p lkey=%x",
1650 ret
, e_maxmr
, shca
, e_maxmr
->ib
.ib_mr
.lkey
);
1651 shca
->maxmr
= e_maxmr
;
1652 goto ehca_dereg_internal_maxmr_exit0
;
1655 atomic_dec(&ib_pd
->usecnt
);
1657 ehca_dereg_internal_maxmr_exit0
:
1659 ehca_err(&shca
->ib_device
, "ret=%i shca=%p shca->maxmr=%p",
1660 ret
, shca
, shca
->maxmr
);
1662 } /* end ehca_dereg_internal_maxmr() */
1664 /*----------------------------------------------------------------------*/
1667 * check physical buffer array of MR verbs for validness and
1668 * calculates MR size
1670 int ehca_mr_chk_buf_and_calc_size(struct ib_phys_buf
*phys_buf_array
,
1675 struct ib_phys_buf
*pbuf
= phys_buf_array
;
1679 if (num_phys_buf
== 0) {
1680 ehca_gen_err("bad phys buf array len, num_phys_buf=0");
1683 /* check first buffer */
1684 if (((u64
)iova_start
& ~PAGE_MASK
) != (pbuf
->addr
& ~PAGE_MASK
)) {
1685 ehca_gen_err("iova_start/addr mismatch, iova_start=%p "
1686 "pbuf->addr=%lx pbuf->size=%lx",
1687 iova_start
, pbuf
->addr
, pbuf
->size
);
1690 if (((pbuf
->addr
+ pbuf
->size
) % PAGE_SIZE
) &&
1691 (num_phys_buf
> 1)) {
1692 ehca_gen_err("addr/size mismatch in 1st buf, pbuf->addr=%lx "
1693 "pbuf->size=%lx", pbuf
->addr
, pbuf
->size
);
1697 for (i
= 0; i
< num_phys_buf
; i
++) {
1698 if ((i
> 0) && (pbuf
->addr
% PAGE_SIZE
)) {
1699 ehca_gen_err("bad address, i=%x pbuf->addr=%lx "
1701 i
, pbuf
->addr
, pbuf
->size
);
1704 if (((i
> 0) && /* not 1st */
1705 (i
< (num_phys_buf
- 1)) && /* not last */
1706 (pbuf
->size
% PAGE_SIZE
)) || (pbuf
->size
== 0)) {
1707 ehca_gen_err("bad size, i=%x pbuf->size=%lx",
1711 size_count
+= pbuf
->size
;
1717 } /* end ehca_mr_chk_buf_and_calc_size() */
1719 /*----------------------------------------------------------------------*/
1721 /* check page list of map FMR verb for validness */
1722 int ehca_fmr_check_page_list(struct ehca_mr
*e_fmr
,
1729 if ((list_len
== 0) || (list_len
> e_fmr
->fmr_max_pages
)) {
1730 ehca_gen_err("bad list_len, list_len=%x "
1731 "e_fmr->fmr_max_pages=%x fmr=%p",
1732 list_len
, e_fmr
->fmr_max_pages
, e_fmr
);
1736 /* each page must be aligned */
1738 for (i
= 0; i
< list_len
; i
++) {
1739 if (*page
% e_fmr
->fmr_page_size
) {
1740 ehca_gen_err("bad page, i=%x *page=%lx page=%p fmr=%p "
1741 "fmr_page_size=%x", i
, *page
, page
, e_fmr
,
1742 e_fmr
->fmr_page_size
);
1749 } /* end ehca_fmr_check_page_list() */
1751 /*----------------------------------------------------------------------*/
1753 /* PAGE_SIZE >= pginfo->hwpage_size */
1754 static int ehca_set_pagebuf_user1(struct ehca_mr_pginfo
*pginfo
,
1759 struct ib_umem_chunk
*prev_chunk
;
1760 struct ib_umem_chunk
*chunk
;
1764 int hwpages_per_kpage
= PAGE_SIZE
/ pginfo
->hwpage_size
;
1766 /* loop over desired chunk entries */
1767 chunk
= pginfo
->u
.usr
.next_chunk
;
1768 prev_chunk
= pginfo
->u
.usr
.next_chunk
;
1769 list_for_each_entry_continue(
1770 chunk
, (&(pginfo
->u
.usr
.region
->chunk_list
)), list
) {
1771 for (i
= pginfo
->u
.usr
.next_nmap
; i
< chunk
->nmap
; ) {
1772 pgaddr
= page_to_pfn(sg_page(&chunk
->page_list
[i
]))
1774 *kpage
= phys_to_abs(pgaddr
+
1775 (pginfo
->next_hwpage
*
1776 pginfo
->hwpage_size
));
1778 ehca_gen_err("pgaddr=%lx "
1779 "chunk->page_list[i]=%lx "
1780 "i=%x next_hwpage=%lx",
1781 pgaddr
, (u64
)sg_dma_address(
1782 &chunk
->page_list
[i
]),
1783 i
, pginfo
->next_hwpage
);
1786 (pginfo
->hwpage_cnt
)++;
1787 (pginfo
->next_hwpage
)++;
1789 if (pginfo
->next_hwpage
% hwpages_per_kpage
== 0) {
1790 (pginfo
->kpage_cnt
)++;
1791 (pginfo
->u
.usr
.next_nmap
)++;
1792 pginfo
->next_hwpage
= 0;
1796 if (j
>= number
) break;
1798 if ((pginfo
->u
.usr
.next_nmap
>= chunk
->nmap
) &&
1800 pginfo
->u
.usr
.next_nmap
= 0;
1803 } else if (pginfo
->u
.usr
.next_nmap
>= chunk
->nmap
) {
1804 pginfo
->u
.usr
.next_nmap
= 0;
1806 } else if (j
>= number
)
1811 pginfo
->u
.usr
.next_chunk
=
1812 list_prepare_entry(prev_chunk
,
1813 (&(pginfo
->u
.usr
.region
->chunk_list
)),
1819 * check given pages for contiguous layout
1820 * last page addr is returned in prev_pgaddr for further check
1822 static int ehca_check_kpages_per_ate(struct scatterlist
*page_list
,
1823 int start_idx
, int end_idx
,
1827 for (t
= start_idx
; t
<= end_idx
; t
++) {
1828 u64 pgaddr
= page_to_pfn(sg_page(&page_list
[t
])) << PAGE_SHIFT
;
1829 ehca_gen_dbg("chunk_page=%lx value=%016lx", pgaddr
,
1830 *(u64
*)abs_to_virt(phys_to_abs(pgaddr
)));
1831 if (pgaddr
- PAGE_SIZE
!= *prev_pgaddr
) {
1832 ehca_gen_err("uncontiguous page found pgaddr=%lx "
1833 "prev_pgaddr=%lx page_list_i=%x",
1834 pgaddr
, *prev_pgaddr
, t
);
1837 *prev_pgaddr
= pgaddr
;
1842 /* PAGE_SIZE < pginfo->hwpage_size */
1843 static int ehca_set_pagebuf_user2(struct ehca_mr_pginfo
*pginfo
,
1848 struct ib_umem_chunk
*prev_chunk
;
1849 struct ib_umem_chunk
*chunk
;
1850 u64 pgaddr
, prev_pgaddr
;
1853 int kpages_per_hwpage
= pginfo
->hwpage_size
/ PAGE_SIZE
;
1854 int nr_kpages
= kpages_per_hwpage
;
1856 /* loop over desired chunk entries */
1857 chunk
= pginfo
->u
.usr
.next_chunk
;
1858 prev_chunk
= pginfo
->u
.usr
.next_chunk
;
1859 list_for_each_entry_continue(
1860 chunk
, (&(pginfo
->u
.usr
.region
->chunk_list
)), list
) {
1861 for (i
= pginfo
->u
.usr
.next_nmap
; i
< chunk
->nmap
; ) {
1862 if (nr_kpages
== kpages_per_hwpage
) {
1863 pgaddr
= ( page_to_pfn(sg_page(&chunk
->page_list
[i
]))
1865 *kpage
= phys_to_abs(pgaddr
);
1867 ehca_gen_err("pgaddr=%lx i=%x",
1873 * The first page in a hwpage must be aligned;
1874 * the first MR page is exempt from this rule.
1876 if (pgaddr
& (pginfo
->hwpage_size
- 1)) {
1877 if (pginfo
->hwpage_cnt
) {
1879 "invalid alignment "
1883 pginfo
->hwpage_size
);
1890 (pginfo
->hwpage_size
- 1)) >>
1892 nr_kpages
-= pginfo
->kpage_cnt
;
1893 *kpage
= phys_to_abs(
1895 ~(pginfo
->hwpage_size
- 1));
1897 ehca_gen_dbg("kpage=%lx chunk_page=%lx "
1898 "value=%016lx", *kpage
, pgaddr
,
1899 *(u64
*)abs_to_virt(
1900 phys_to_abs(pgaddr
)));
1901 prev_pgaddr
= pgaddr
;
1903 pginfo
->kpage_cnt
++;
1904 pginfo
->u
.usr
.next_nmap
++;
1910 if (i
+ nr_kpages
> chunk
->nmap
) {
1911 ret
= ehca_check_kpages_per_ate(
1912 chunk
->page_list
, i
,
1913 chunk
->nmap
- 1, &prev_pgaddr
);
1914 if (ret
) return ret
;
1915 pginfo
->kpage_cnt
+= chunk
->nmap
- i
;
1916 pginfo
->u
.usr
.next_nmap
+= chunk
->nmap
- i
;
1917 nr_kpages
-= chunk
->nmap
- i
;
1921 ret
= ehca_check_kpages_per_ate(chunk
->page_list
, i
,
1924 if (ret
) return ret
;
1926 pginfo
->kpage_cnt
+= nr_kpages
;
1927 pginfo
->u
.usr
.next_nmap
+= nr_kpages
;
1929 nr_kpages
= kpages_per_hwpage
;
1930 (pginfo
->hwpage_cnt
)++;
1933 if (j
>= number
) break;
1935 if ((pginfo
->u
.usr
.next_nmap
>= chunk
->nmap
) &&
1937 pginfo
->u
.usr
.next_nmap
= 0;
1940 } else if (pginfo
->u
.usr
.next_nmap
>= chunk
->nmap
) {
1941 pginfo
->u
.usr
.next_nmap
= 0;
1943 } else if (j
>= number
)
1948 pginfo
->u
.usr
.next_chunk
=
1949 list_prepare_entry(prev_chunk
,
1950 (&(pginfo
->u
.usr
.region
->chunk_list
)),
1955 int ehca_set_pagebuf_phys(struct ehca_mr_pginfo
*pginfo
,
1960 struct ib_phys_buf
*pbuf
;
1961 u64 num_hw
, offs_hw
;
1964 /* loop over desired phys_buf_array entries */
1965 while (i
< number
) {
1966 pbuf
= pginfo
->u
.phy
.phys_buf_array
+ pginfo
->u
.phy
.next_buf
;
1967 num_hw
= NUM_CHUNKS((pbuf
->addr
% pginfo
->hwpage_size
) +
1968 pbuf
->size
, pginfo
->hwpage_size
);
1969 offs_hw
= (pbuf
->addr
& ~(pginfo
->hwpage_size
- 1)) /
1970 pginfo
->hwpage_size
;
1971 while (pginfo
->next_hwpage
< offs_hw
+ num_hw
) {
1973 if ((pginfo
->kpage_cnt
>= pginfo
->num_kpages
) ||
1974 (pginfo
->hwpage_cnt
>= pginfo
->num_hwpages
)) {
1975 ehca_gen_err("kpage_cnt >= num_kpages, "
1976 "kpage_cnt=%lx num_kpages=%lx "
1978 "num_hwpages=%lx i=%x",
1982 pginfo
->num_hwpages
, i
);
1985 *kpage
= phys_to_abs(
1986 (pbuf
->addr
& ~(pginfo
->hwpage_size
- 1)) +
1987 (pginfo
->next_hwpage
* pginfo
->hwpage_size
));
1988 if ( !(*kpage
) && pbuf
->addr
) {
1989 ehca_gen_err("pbuf->addr=%lx pbuf->size=%lx "
1990 "next_hwpage=%lx", pbuf
->addr
,
1991 pbuf
->size
, pginfo
->next_hwpage
);
1994 (pginfo
->hwpage_cnt
)++;
1995 (pginfo
->next_hwpage
)++;
1996 if (PAGE_SIZE
>= pginfo
->hwpage_size
) {
1997 if (pginfo
->next_hwpage
%
1998 (PAGE_SIZE
/ pginfo
->hwpage_size
) == 0)
1999 (pginfo
->kpage_cnt
)++;
2001 pginfo
->kpage_cnt
+= pginfo
->hwpage_size
/
2005 if (i
>= number
) break;
2007 if (pginfo
->next_hwpage
>= offs_hw
+ num_hw
) {
2008 (pginfo
->u
.phy
.next_buf
)++;
2009 pginfo
->next_hwpage
= 0;
2015 int ehca_set_pagebuf_fmr(struct ehca_mr_pginfo
*pginfo
,
2023 /* loop over desired page_list entries */
2024 fmrlist
= pginfo
->u
.fmr
.page_list
+ pginfo
->u
.fmr
.next_listelem
;
2025 for (i
= 0; i
< number
; i
++) {
2026 *kpage
= phys_to_abs((*fmrlist
& ~(pginfo
->hwpage_size
- 1)) +
2027 pginfo
->next_hwpage
* pginfo
->hwpage_size
);
2029 ehca_gen_err("*fmrlist=%lx fmrlist=%p "
2030 "next_listelem=%lx next_hwpage=%lx",
2032 pginfo
->u
.fmr
.next_listelem
,
2033 pginfo
->next_hwpage
);
2036 (pginfo
->hwpage_cnt
)++;
2037 if (pginfo
->u
.fmr
.fmr_pgsize
>= pginfo
->hwpage_size
) {
2038 if (pginfo
->next_hwpage
%
2039 (pginfo
->u
.fmr
.fmr_pgsize
/
2040 pginfo
->hwpage_size
) == 0) {
2041 (pginfo
->kpage_cnt
)++;
2042 (pginfo
->u
.fmr
.next_listelem
)++;
2044 pginfo
->next_hwpage
= 0;
2046 (pginfo
->next_hwpage
)++;
2048 unsigned int cnt_per_hwpage
= pginfo
->hwpage_size
/
2049 pginfo
->u
.fmr
.fmr_pgsize
;
2052 /* check if adrs are contiguous */
2053 for (j
= 1; j
< cnt_per_hwpage
; j
++) {
2054 u64 p
= phys_to_abs(fmrlist
[j
] &
2055 ~(pginfo
->hwpage_size
- 1));
2056 if (prev
+ pginfo
->u
.fmr
.fmr_pgsize
!= p
) {
2057 ehca_gen_err("uncontiguous fmr pages "
2058 "found prev=%lx p=%lx "
2059 "idx=%x", prev
, p
, i
+ j
);
2064 pginfo
->kpage_cnt
+= cnt_per_hwpage
;
2065 pginfo
->u
.fmr
.next_listelem
+= cnt_per_hwpage
;
2066 fmrlist
+= cnt_per_hwpage
;
2073 /* setup page buffer from page info */
2074 int ehca_set_pagebuf(struct ehca_mr_pginfo
*pginfo
,
2080 switch (pginfo
->type
) {
2081 case EHCA_MR_PGI_PHYS
:
2082 ret
= ehca_set_pagebuf_phys(pginfo
, number
, kpage
);
2084 case EHCA_MR_PGI_USER
:
2085 ret
= PAGE_SIZE
>= pginfo
->hwpage_size
?
2086 ehca_set_pagebuf_user1(pginfo
, number
, kpage
) :
2087 ehca_set_pagebuf_user2(pginfo
, number
, kpage
);
2089 case EHCA_MR_PGI_FMR
:
2090 ret
= ehca_set_pagebuf_fmr(pginfo
, number
, kpage
);
2093 ehca_gen_err("bad pginfo->type=%x", pginfo
->type
);
2098 } /* end ehca_set_pagebuf() */
2100 /*----------------------------------------------------------------------*/
2103 * check MR if it is a max-MR, i.e. uses whole memory
2104 * in case it's a max-MR 1 is returned, else 0
2106 int ehca_mr_is_maxmr(u64 size
,
2109 /* a MR is treated as max-MR only if it fits following: */
2110 if ((size
== ((u64
)high_memory
- PAGE_OFFSET
)) &&
2111 (iova_start
== (void *)KERNELBASE
)) {
2112 ehca_gen_dbg("this is a max-MR");
2116 } /* end ehca_mr_is_maxmr() */
2118 /*----------------------------------------------------------------------*/
2120 /* map access control for MR/MW. This routine is used for MR and MW. */
2121 void ehca_mrmw_map_acl(int ib_acl
,
2125 if (ib_acl
& IB_ACCESS_REMOTE_READ
)
2126 *hipz_acl
|= HIPZ_ACCESSCTRL_R_READ
;
2127 if (ib_acl
& IB_ACCESS_REMOTE_WRITE
)
2128 *hipz_acl
|= HIPZ_ACCESSCTRL_R_WRITE
;
2129 if (ib_acl
& IB_ACCESS_REMOTE_ATOMIC
)
2130 *hipz_acl
|= HIPZ_ACCESSCTRL_R_ATOMIC
;
2131 if (ib_acl
& IB_ACCESS_LOCAL_WRITE
)
2132 *hipz_acl
|= HIPZ_ACCESSCTRL_L_WRITE
;
2133 if (ib_acl
& IB_ACCESS_MW_BIND
)
2134 *hipz_acl
|= HIPZ_ACCESSCTRL_MW_BIND
;
2135 } /* end ehca_mrmw_map_acl() */
2137 /*----------------------------------------------------------------------*/
2139 /* sets page size in hipz access control for MR/MW. */
2140 void ehca_mrmw_set_pgsize_hipz_acl(u32 pgsize
, u32
*hipz_acl
) /*INOUT*/
2142 *hipz_acl
|= (ehca_encode_hwpage_size(pgsize
) << 24);
2143 } /* end ehca_mrmw_set_pgsize_hipz_acl() */
2145 /*----------------------------------------------------------------------*/
2148 * reverse map access control for MR/MW.
2149 * This routine is used for MR and MW.
2151 void ehca_mrmw_reverse_map_acl(const u32
*hipz_acl
,
2152 int *ib_acl
) /*OUT*/
2155 if (*hipz_acl
& HIPZ_ACCESSCTRL_R_READ
)
2156 *ib_acl
|= IB_ACCESS_REMOTE_READ
;
2157 if (*hipz_acl
& HIPZ_ACCESSCTRL_R_WRITE
)
2158 *ib_acl
|= IB_ACCESS_REMOTE_WRITE
;
2159 if (*hipz_acl
& HIPZ_ACCESSCTRL_R_ATOMIC
)
2160 *ib_acl
|= IB_ACCESS_REMOTE_ATOMIC
;
2161 if (*hipz_acl
& HIPZ_ACCESSCTRL_L_WRITE
)
2162 *ib_acl
|= IB_ACCESS_LOCAL_WRITE
;
2163 if (*hipz_acl
& HIPZ_ACCESSCTRL_MW_BIND
)
2164 *ib_acl
|= IB_ACCESS_MW_BIND
;
2165 } /* end ehca_mrmw_reverse_map_acl() */
2168 /*----------------------------------------------------------------------*/
2171 * MR destructor and constructor
2172 * used in Reregister MR verb, sets all fields in ehca_mr_t to 0,
2173 * except struct ib_mr and spinlock
2175 void ehca_mr_deletenew(struct ehca_mr
*mr
)
2179 mr
->num_hwpages
= 0;
2182 mr
->fmr_page_size
= 0;
2183 mr
->fmr_max_pages
= 0;
2184 mr
->fmr_max_maps
= 0;
2185 mr
->fmr_map_cnt
= 0;
2186 memset(&mr
->ipz_mr_handle
, 0, sizeof(mr
->ipz_mr_handle
));
2187 memset(&mr
->galpas
, 0, sizeof(mr
->galpas
));
2188 } /* end ehca_mr_deletenew() */
2190 int ehca_init_mrmw_cache(void)
2192 mr_cache
= kmem_cache_create("ehca_cache_mr",
2193 sizeof(struct ehca_mr
), 0,
2198 mw_cache
= kmem_cache_create("ehca_cache_mw",
2199 sizeof(struct ehca_mw
), 0,
2203 kmem_cache_destroy(mr_cache
);
2210 void ehca_cleanup_mrmw_cache(void)
2213 kmem_cache_destroy(mr_cache
);
2215 kmem_cache_destroy(mw_cache
);