blob: e925e84c9bc711c3c5d982270b1e12dd013e4498 [file] [log] [blame]
Heiko J Schickfab97222006-09-22 15:22:22 -07001/*
2 * IBM eServer eHCA Infiniband device driver for Linux on POWER
3 *
4 * MR/MW functions
5 *
6 * Authors: Dietmar Decker <ddecker@de.ibm.com>
7 * Christoph Raisch <raisch@de.ibm.com>
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02008 * Hoang-Nam Nguyen <hnguyen@de.ibm.com>
Heiko J Schickfab97222006-09-22 15:22:22 -07009 *
10 * Copyright (c) 2005 IBM Corporation
11 *
12 * All rights reserved.
13 *
14 * This source code is distributed under a dual license of GPL v2.0 and OpenIB
15 * BSD.
16 *
17 * OpenIB BSD License
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions are met:
21 *
22 * Redistributions of source code must retain the above copyright notice, this
23 * list of conditions and the following disclaimer.
24 *
25 * Redistributions in binary form must reproduce the above copyright notice,
26 * this list of conditions and the following disclaimer in the documentation
27 * and/or other materials
28 * provided with the distribution.
29 *
30 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
31 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
32 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
34 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
38 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 * POSSIBILITY OF SUCH DAMAGE.
41 */
42
43#include <asm/current.h>
44
Hoang-Nam Nguyen05d989f2007-07-28 08:36:32 -070045#include <rdma/ib_umem.h>
46
Heiko J Schickfab97222006-09-22 15:22:22 -070047#include "ehca_iverbs.h"
48#include "ehca_mrmw.h"
49#include "hcp_if.h"
50#include "hipz_hw.h"
51
Hoang-Nam Nguyen24923982007-07-12 17:51:43 +020052#define NUM_CHUNKS(length, chunk_size) \
53 (((length) + (chunk_size - 1)) / (chunk_size))
Hoang-Nam Nguyen4e4e74c2007-07-12 17:51:04 +020054/* max number of rpages (per hcall register_rpages) */
55#define MAX_RPAGES 512
56
Heiko J Schickfab97222006-09-22 15:22:22 -070057static struct kmem_cache *mr_cache;
58static struct kmem_cache *mw_cache;
59
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +020060enum ehca_mr_pgsize {
61 EHCA_MR_PGSIZE4K = 0x1000L,
62 EHCA_MR_PGSIZE64K = 0x10000L,
63 EHCA_MR_PGSIZE1M = 0x100000L,
64 EHCA_MR_PGSIZE16M = 0x1000000L
65};
66
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +020067static u32 ehca_encode_hwpage_size(u32 pgsize)
68{
69 u32 idx = 0;
70 pgsize >>= 12;
71 /*
72 * map mr page size into hw code:
73 * 0, 1, 2, 3 for 4K, 64K, 1M, 64M
74 */
75 while (!(pgsize & 1)) {
76 idx++;
77 pgsize >>= 4;
78 }
79 return idx;
80}
81
82static u64 ehca_get_max_hwpage_size(struct ehca_shca *shca)
83{
84 if (shca->hca_cap_mr_pgsize & HCA_CAP_MR_PGSIZE_16M)
85 return EHCA_MR_PGSIZE16M;
86 return EHCA_MR_PGSIZE4K;
87}
88
Heiko J Schickfab97222006-09-22 15:22:22 -070089static struct ehca_mr *ehca_mr_new(void)
90{
91 struct ehca_mr *me;
92
Robert P. J. Dayc3762222007-02-10 01:45:03 -080093 me = kmem_cache_zalloc(mr_cache, GFP_KERNEL);
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +020094 if (me)
Heiko J Schickfab97222006-09-22 15:22:22 -070095 spin_lock_init(&me->mrlock);
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +020096 else
Heiko J Schickfab97222006-09-22 15:22:22 -070097 ehca_gen_err("alloc failed");
98
99 return me;
100}
101
102static void ehca_mr_delete(struct ehca_mr *me)
103{
104 kmem_cache_free(mr_cache, me);
105}
106
107static struct ehca_mw *ehca_mw_new(void)
108{
109 struct ehca_mw *me;
110
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800111 me = kmem_cache_zalloc(mw_cache, GFP_KERNEL);
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200112 if (me)
Heiko J Schickfab97222006-09-22 15:22:22 -0700113 spin_lock_init(&me->mwlock);
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200114 else
Heiko J Schickfab97222006-09-22 15:22:22 -0700115 ehca_gen_err("alloc failed");
116
117 return me;
118}
119
120static void ehca_mw_delete(struct ehca_mw *me)
121{
122 kmem_cache_free(mw_cache, me);
123}
124
125/*----------------------------------------------------------------------*/
126
127struct ib_mr *ehca_get_dma_mr(struct ib_pd *pd, int mr_access_flags)
128{
129 struct ib_mr *ib_mr;
130 int ret;
131 struct ehca_mr *e_maxmr;
132 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
133 struct ehca_shca *shca =
134 container_of(pd->device, struct ehca_shca, ib_device);
135
136 if (shca->maxmr) {
137 e_maxmr = ehca_mr_new();
138 if (!e_maxmr) {
139 ehca_err(&shca->ib_device, "out of memory");
140 ib_mr = ERR_PTR(-ENOMEM);
141 goto get_dma_mr_exit0;
142 }
143
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200144 ret = ehca_reg_maxmr(shca, e_maxmr, (u64 *)KERNELBASE,
Heiko J Schickfab97222006-09-22 15:22:22 -0700145 mr_access_flags, e_pd,
146 &e_maxmr->ib.ib_mr.lkey,
147 &e_maxmr->ib.ib_mr.rkey);
148 if (ret) {
Hoang-Nam Nguyen3df78f82007-07-12 17:48:22 +0200149 ehca_mr_delete(e_maxmr);
Heiko J Schickfab97222006-09-22 15:22:22 -0700150 ib_mr = ERR_PTR(ret);
151 goto get_dma_mr_exit0;
152 }
153 ib_mr = &e_maxmr->ib.ib_mr;
154 } else {
155 ehca_err(&shca->ib_device, "no internal max-MR exist!");
156 ib_mr = ERR_PTR(-EINVAL);
157 goto get_dma_mr_exit0;
158 }
159
160get_dma_mr_exit0:
161 if (IS_ERR(ib_mr))
Joachim Fenkese3722192007-09-11 15:32:22 +0200162 ehca_err(&shca->ib_device, "h_ret=%li pd=%p mr_access_flags=%x",
Heiko J Schickfab97222006-09-22 15:22:22 -0700163 PTR_ERR(ib_mr), pd, mr_access_flags);
164 return ib_mr;
165} /* end ehca_get_dma_mr() */
166
167/*----------------------------------------------------------------------*/
168
169struct ib_mr *ehca_reg_phys_mr(struct ib_pd *pd,
170 struct ib_phys_buf *phys_buf_array,
171 int num_phys_buf,
172 int mr_access_flags,
173 u64 *iova_start)
174{
175 struct ib_mr *ib_mr;
176 int ret;
177 struct ehca_mr *e_mr;
178 struct ehca_shca *shca =
179 container_of(pd->device, struct ehca_shca, ib_device);
180 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
181
182 u64 size;
Heiko J Schickfab97222006-09-22 15:22:22 -0700183
184 if ((num_phys_buf <= 0) || !phys_buf_array) {
185 ehca_err(pd->device, "bad input values: num_phys_buf=%x "
186 "phys_buf_array=%p", num_phys_buf, phys_buf_array);
187 ib_mr = ERR_PTR(-EINVAL);
188 goto reg_phys_mr_exit0;
189 }
190 if (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
191 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
192 ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
193 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE))) {
194 /*
195 * Remote Write Access requires Local Write Access
196 * Remote Atomic Access requires Local Write Access
197 */
198 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
199 mr_access_flags);
200 ib_mr = ERR_PTR(-EINVAL);
201 goto reg_phys_mr_exit0;
202 }
203
204 /* check physical buffer list and calculate size */
205 ret = ehca_mr_chk_buf_and_calc_size(phys_buf_array, num_phys_buf,
206 iova_start, &size);
207 if (ret) {
208 ib_mr = ERR_PTR(ret);
209 goto reg_phys_mr_exit0;
210 }
211 if ((size == 0) ||
212 (((u64)iova_start + size) < (u64)iova_start)) {
213 ehca_err(pd->device, "bad input values: size=%lx iova_start=%p",
214 size, iova_start);
215 ib_mr = ERR_PTR(-EINVAL);
216 goto reg_phys_mr_exit0;
217 }
218
219 e_mr = ehca_mr_new();
220 if (!e_mr) {
221 ehca_err(pd->device, "out of memory");
222 ib_mr = ERR_PTR(-ENOMEM);
223 goto reg_phys_mr_exit0;
224 }
225
Heiko J Schickfab97222006-09-22 15:22:22 -0700226 /* register MR on HCA */
227 if (ehca_mr_is_maxmr(size, iova_start)) {
228 e_mr->flags |= EHCA_MR_FLAG_MAXMR;
229 ret = ehca_reg_maxmr(shca, e_mr, iova_start, mr_access_flags,
230 e_pd, &e_mr->ib.ib_mr.lkey,
231 &e_mr->ib.ib_mr.rkey);
232 if (ret) {
233 ib_mr = ERR_PTR(ret);
234 goto reg_phys_mr_exit1;
235 }
236 } else {
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200237 struct ehca_mr_pginfo pginfo;
238 u32 num_kpages;
239 u32 num_hwpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200240 u64 hw_pgsize;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200241
242 num_kpages = NUM_CHUNKS(((u64)iova_start % PAGE_SIZE) + size,
243 PAGE_SIZE);
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200244 /* for kernel space we try most possible pgsize */
245 hw_pgsize = ehca_get_max_hwpage_size(shca);
246 num_hwpages = NUM_CHUNKS(((u64)iova_start % hw_pgsize) + size,
247 hw_pgsize);
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200248 memset(&pginfo, 0, sizeof(pginfo));
249 pginfo.type = EHCA_MR_PGI_PHYS;
250 pginfo.num_kpages = num_kpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200251 pginfo.hwpage_size = hw_pgsize;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200252 pginfo.num_hwpages = num_hwpages;
253 pginfo.u.phy.num_phys_buf = num_phys_buf;
254 pginfo.u.phy.phys_buf_array = phys_buf_array;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200255 pginfo.next_hwpage =
256 ((u64)iova_start & ~(hw_pgsize - 1)) / hw_pgsize;
Heiko J Schickfab97222006-09-22 15:22:22 -0700257
258 ret = ehca_reg_mr(shca, e_mr, iova_start, size, mr_access_flags,
259 e_pd, &pginfo, &e_mr->ib.ib_mr.lkey,
260 &e_mr->ib.ib_mr.rkey);
261 if (ret) {
262 ib_mr = ERR_PTR(ret);
263 goto reg_phys_mr_exit1;
264 }
265 }
266
267 /* successful registration of all pages */
268 return &e_mr->ib.ib_mr;
269
270reg_phys_mr_exit1:
271 ehca_mr_delete(e_mr);
272reg_phys_mr_exit0:
273 if (IS_ERR(ib_mr))
Joachim Fenkese3722192007-09-11 15:32:22 +0200274 ehca_err(pd->device, "h_ret=%li pd=%p phys_buf_array=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -0700275 "num_phys_buf=%x mr_access_flags=%x iova_start=%p",
276 PTR_ERR(ib_mr), pd, phys_buf_array,
277 num_phys_buf, mr_access_flags, iova_start);
278 return ib_mr;
279} /* end ehca_reg_phys_mr() */
280
281/*----------------------------------------------------------------------*/
282
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200283struct ib_mr *ehca_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
284 u64 virt, int mr_access_flags,
285 struct ib_udata *udata)
Heiko J Schickfab97222006-09-22 15:22:22 -0700286{
287 struct ib_mr *ib_mr;
288 struct ehca_mr *e_mr;
289 struct ehca_shca *shca =
290 container_of(pd->device, struct ehca_shca, ib_device);
291 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200292 struct ehca_mr_pginfo pginfo;
Heiko J Schickfab97222006-09-22 15:22:22 -0700293 int ret;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200294 u32 num_kpages;
295 u32 num_hwpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200296 u64 hwpage_size;
Heiko J Schickfab97222006-09-22 15:22:22 -0700297
298 if (!pd) {
299 ehca_gen_err("bad pd=%p", pd);
300 return ERR_PTR(-EFAULT);
301 }
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800302
Heiko J Schickfab97222006-09-22 15:22:22 -0700303 if (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
304 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
305 ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
306 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE))) {
307 /*
308 * Remote Write Access requires Local Write Access
309 * Remote Atomic Access requires Local Write Access
310 */
311 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
312 mr_access_flags);
313 ib_mr = ERR_PTR(-EINVAL);
314 goto reg_user_mr_exit0;
315 }
Heiko J Schickfab97222006-09-22 15:22:22 -0700316
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800317 if (length == 0 || virt + length < virt) {
Heiko J Schickfab97222006-09-22 15:22:22 -0700318 ehca_err(pd->device, "bad input values: length=%lx "
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800319 "virt_base=%lx", length, virt);
Heiko J Schickfab97222006-09-22 15:22:22 -0700320 ib_mr = ERR_PTR(-EINVAL);
321 goto reg_user_mr_exit0;
322 }
323
324 e_mr = ehca_mr_new();
325 if (!e_mr) {
326 ehca_err(pd->device, "out of memory");
327 ib_mr = ERR_PTR(-ENOMEM);
328 goto reg_user_mr_exit0;
329 }
330
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800331 e_mr->umem = ib_umem_get(pd->uobject->context, start, length,
332 mr_access_flags);
333 if (IS_ERR(e_mr->umem)) {
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200334 ib_mr = (void *)e_mr->umem;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800335 goto reg_user_mr_exit1;
336 }
337
338 if (e_mr->umem->page_size != PAGE_SIZE) {
339 ehca_err(pd->device, "page size not supported, "
340 "e_mr->umem->page_size=%x", e_mr->umem->page_size);
341 ib_mr = ERR_PTR(-EINVAL);
342 goto reg_user_mr_exit2;
343 }
344
Heiko J Schickfab97222006-09-22 15:22:22 -0700345 /* determine number of MR pages */
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200346 num_kpages = NUM_CHUNKS((virt % PAGE_SIZE) + length, PAGE_SIZE);
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200347 /* select proper hw_pgsize */
348 if (ehca_mr_largepage &&
349 (shca->hca_cap_mr_pgsize & HCA_CAP_MR_PGSIZE_16M)) {
350 if (length <= EHCA_MR_PGSIZE4K
351 && PAGE_SIZE == EHCA_MR_PGSIZE4K)
352 hwpage_size = EHCA_MR_PGSIZE4K;
353 else if (length <= EHCA_MR_PGSIZE64K)
354 hwpage_size = EHCA_MR_PGSIZE64K;
355 else if (length <= EHCA_MR_PGSIZE1M)
356 hwpage_size = EHCA_MR_PGSIZE1M;
357 else
358 hwpage_size = EHCA_MR_PGSIZE16M;
359 } else
360 hwpage_size = EHCA_MR_PGSIZE4K;
361 ehca_dbg(pd->device, "hwpage_size=%lx", hwpage_size);
Heiko J Schickfab97222006-09-22 15:22:22 -0700362
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200363reg_user_mr_fallback:
364 num_hwpages = NUM_CHUNKS((virt % hwpage_size) + length, hwpage_size);
Heiko J Schickfab97222006-09-22 15:22:22 -0700365 /* register MR on HCA */
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200366 memset(&pginfo, 0, sizeof(pginfo));
367 pginfo.type = EHCA_MR_PGI_USER;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200368 pginfo.hwpage_size = hwpage_size;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200369 pginfo.num_kpages = num_kpages;
370 pginfo.num_hwpages = num_hwpages;
371 pginfo.u.usr.region = e_mr->umem;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200372 pginfo.next_hwpage = e_mr->umem->offset / hwpage_size;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200373 pginfo.u.usr.next_chunk = list_prepare_entry(pginfo.u.usr.next_chunk,
374 (&e_mr->umem->chunk_list),
375 list);
Heiko J Schickfab97222006-09-22 15:22:22 -0700376
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200377 ret = ehca_reg_mr(shca, e_mr, (u64 *)virt, length, mr_access_flags,
378 e_pd, &pginfo, &e_mr->ib.ib_mr.lkey,
379 &e_mr->ib.ib_mr.rkey);
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200380 if (ret == -EINVAL && pginfo.hwpage_size > PAGE_SIZE) {
381 ehca_warn(pd->device, "failed to register mr "
382 "with hwpage_size=%lx", hwpage_size);
383 ehca_info(pd->device, "try to register mr with "
384 "kpage_size=%lx", PAGE_SIZE);
385 /*
386 * this means kpages are not contiguous for a hw page
387 * try kernel page size as fallback solution
388 */
389 hwpage_size = PAGE_SIZE;
390 goto reg_user_mr_fallback;
391 }
Heiko J Schickfab97222006-09-22 15:22:22 -0700392 if (ret) {
393 ib_mr = ERR_PTR(ret);
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800394 goto reg_user_mr_exit2;
Heiko J Schickfab97222006-09-22 15:22:22 -0700395 }
396
397 /* successful registration of all pages */
398 return &e_mr->ib.ib_mr;
399
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800400reg_user_mr_exit2:
401 ib_umem_release(e_mr->umem);
Heiko J Schickfab97222006-09-22 15:22:22 -0700402reg_user_mr_exit1:
403 ehca_mr_delete(e_mr);
404reg_user_mr_exit0:
405 if (IS_ERR(ib_mr))
Joachim Fenkese3722192007-09-11 15:32:22 +0200406 ehca_err(pd->device, "rc=%li pd=%p mr_access_flags=%x udata=%p",
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800407 PTR_ERR(ib_mr), pd, mr_access_flags, udata);
Heiko J Schickfab97222006-09-22 15:22:22 -0700408 return ib_mr;
409} /* end ehca_reg_user_mr() */
410
411/*----------------------------------------------------------------------*/
412
413int ehca_rereg_phys_mr(struct ib_mr *mr,
414 int mr_rereg_mask,
415 struct ib_pd *pd,
416 struct ib_phys_buf *phys_buf_array,
417 int num_phys_buf,
418 int mr_access_flags,
419 u64 *iova_start)
420{
421 int ret;
422
423 struct ehca_shca *shca =
424 container_of(mr->device, struct ehca_shca, ib_device);
425 struct ehca_mr *e_mr = container_of(mr, struct ehca_mr, ib.ib_mr);
426 struct ehca_pd *my_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
427 u64 new_size;
428 u64 *new_start;
429 u32 new_acl;
430 struct ehca_pd *new_pd;
431 u32 tmp_lkey, tmp_rkey;
432 unsigned long sl_flags;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200433 u32 num_kpages = 0;
434 u32 num_hwpages = 0;
435 struct ehca_mr_pginfo pginfo;
Heiko J Schickfab97222006-09-22 15:22:22 -0700436 u32 cur_pid = current->tgid;
437
438 if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
439 (my_pd->ownpid != cur_pid)) {
440 ehca_err(mr->device, "Invalid caller pid=%x ownpid=%x",
441 cur_pid, my_pd->ownpid);
442 ret = -EINVAL;
443 goto rereg_phys_mr_exit0;
444 }
445
446 if (!(mr_rereg_mask & IB_MR_REREG_TRANS)) {
447 /* TODO not supported, because PHYP rereg hCall needs pages */
448 ehca_err(mr->device, "rereg without IB_MR_REREG_TRANS not "
449 "supported yet, mr_rereg_mask=%x", mr_rereg_mask);
450 ret = -EINVAL;
451 goto rereg_phys_mr_exit0;
452 }
453
454 if (mr_rereg_mask & IB_MR_REREG_PD) {
455 if (!pd) {
456 ehca_err(mr->device, "rereg with bad pd, pd=%p "
457 "mr_rereg_mask=%x", pd, mr_rereg_mask);
458 ret = -EINVAL;
459 goto rereg_phys_mr_exit0;
460 }
461 }
462
463 if ((mr_rereg_mask &
464 ~(IB_MR_REREG_TRANS | IB_MR_REREG_PD | IB_MR_REREG_ACCESS)) ||
465 (mr_rereg_mask == 0)) {
466 ret = -EINVAL;
467 goto rereg_phys_mr_exit0;
468 }
469
470 /* check other parameters */
471 if (e_mr == shca->maxmr) {
472 /* should be impossible, however reject to be sure */
473 ehca_err(mr->device, "rereg internal max-MR impossible, mr=%p "
474 "shca->maxmr=%p mr->lkey=%x",
475 mr, shca->maxmr, mr->lkey);
476 ret = -EINVAL;
477 goto rereg_phys_mr_exit0;
478 }
479 if (mr_rereg_mask & IB_MR_REREG_TRANS) { /* transl., i.e. addr/size */
480 if (e_mr->flags & EHCA_MR_FLAG_FMR) {
481 ehca_err(mr->device, "not supported for FMR, mr=%p "
482 "flags=%x", mr, e_mr->flags);
483 ret = -EINVAL;
484 goto rereg_phys_mr_exit0;
485 }
486 if (!phys_buf_array || num_phys_buf <= 0) {
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200487 ehca_err(mr->device, "bad input values mr_rereg_mask=%x"
Heiko J Schickfab97222006-09-22 15:22:22 -0700488 " phys_buf_array=%p num_phys_buf=%x",
489 mr_rereg_mask, phys_buf_array, num_phys_buf);
490 ret = -EINVAL;
491 goto rereg_phys_mr_exit0;
492 }
493 }
494 if ((mr_rereg_mask & IB_MR_REREG_ACCESS) && /* change ACL */
495 (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
496 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
497 ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
498 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)))) {
499 /*
500 * Remote Write Access requires Local Write Access
501 * Remote Atomic Access requires Local Write Access
502 */
503 ehca_err(mr->device, "bad input values: mr_rereg_mask=%x "
504 "mr_access_flags=%x", mr_rereg_mask, mr_access_flags);
505 ret = -EINVAL;
506 goto rereg_phys_mr_exit0;
507 }
508
509 /* set requested values dependent on rereg request */
510 spin_lock_irqsave(&e_mr->mrlock, sl_flags);
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200511 new_start = e_mr->start;
512 new_size = e_mr->size;
513 new_acl = e_mr->acl;
514 new_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
Heiko J Schickfab97222006-09-22 15:22:22 -0700515
516 if (mr_rereg_mask & IB_MR_REREG_TRANS) {
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200517 u64 hw_pgsize = ehca_get_max_hwpage_size(shca);
518
Heiko J Schickfab97222006-09-22 15:22:22 -0700519 new_start = iova_start; /* change address */
520 /* check physical buffer list and calculate size */
521 ret = ehca_mr_chk_buf_and_calc_size(phys_buf_array,
522 num_phys_buf, iova_start,
523 &new_size);
524 if (ret)
525 goto rereg_phys_mr_exit1;
526 if ((new_size == 0) ||
527 (((u64)iova_start + new_size) < (u64)iova_start)) {
528 ehca_err(mr->device, "bad input values: new_size=%lx "
529 "iova_start=%p", new_size, iova_start);
530 ret = -EINVAL;
531 goto rereg_phys_mr_exit1;
532 }
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200533 num_kpages = NUM_CHUNKS(((u64)new_start % PAGE_SIZE) +
534 new_size, PAGE_SIZE);
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200535 num_hwpages = NUM_CHUNKS(((u64)new_start % hw_pgsize) +
536 new_size, hw_pgsize);
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200537 memset(&pginfo, 0, sizeof(pginfo));
538 pginfo.type = EHCA_MR_PGI_PHYS;
539 pginfo.num_kpages = num_kpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200540 pginfo.hwpage_size = hw_pgsize;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200541 pginfo.num_hwpages = num_hwpages;
542 pginfo.u.phy.num_phys_buf = num_phys_buf;
543 pginfo.u.phy.phys_buf_array = phys_buf_array;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200544 pginfo.next_hwpage =
545 ((u64)iova_start & ~(hw_pgsize - 1)) / hw_pgsize;
Heiko J Schickfab97222006-09-22 15:22:22 -0700546 }
547 if (mr_rereg_mask & IB_MR_REREG_ACCESS)
548 new_acl = mr_access_flags;
549 if (mr_rereg_mask & IB_MR_REREG_PD)
550 new_pd = container_of(pd, struct ehca_pd, ib_pd);
551
552 ret = ehca_rereg_mr(shca, e_mr, new_start, new_size, new_acl,
553 new_pd, &pginfo, &tmp_lkey, &tmp_rkey);
554 if (ret)
555 goto rereg_phys_mr_exit1;
556
557 /* successful reregistration */
558 if (mr_rereg_mask & IB_MR_REREG_PD)
559 mr->pd = pd;
560 mr->lkey = tmp_lkey;
561 mr->rkey = tmp_rkey;
562
563rereg_phys_mr_exit1:
564 spin_unlock_irqrestore(&e_mr->mrlock, sl_flags);
565rereg_phys_mr_exit0:
566 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +0200567 ehca_err(mr->device, "ret=%i mr=%p mr_rereg_mask=%x pd=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -0700568 "phys_buf_array=%p num_phys_buf=%x mr_access_flags=%x "
569 "iova_start=%p",
570 ret, mr, mr_rereg_mask, pd, phys_buf_array,
571 num_phys_buf, mr_access_flags, iova_start);
572 return ret;
573} /* end ehca_rereg_phys_mr() */
574
575/*----------------------------------------------------------------------*/
576
577int ehca_query_mr(struct ib_mr *mr, struct ib_mr_attr *mr_attr)
578{
579 int ret = 0;
580 u64 h_ret;
581 struct ehca_shca *shca =
582 container_of(mr->device, struct ehca_shca, ib_device);
583 struct ehca_mr *e_mr = container_of(mr, struct ehca_mr, ib.ib_mr);
584 struct ehca_pd *my_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
585 u32 cur_pid = current->tgid;
586 unsigned long sl_flags;
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200587 struct ehca_mr_hipzout_parms hipzout;
Heiko J Schickfab97222006-09-22 15:22:22 -0700588
589 if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
590 (my_pd->ownpid != cur_pid)) {
591 ehca_err(mr->device, "Invalid caller pid=%x ownpid=%x",
592 cur_pid, my_pd->ownpid);
593 ret = -EINVAL;
594 goto query_mr_exit0;
595 }
596
597 if ((e_mr->flags & EHCA_MR_FLAG_FMR)) {
598 ehca_err(mr->device, "not supported for FMR, mr=%p e_mr=%p "
599 "e_mr->flags=%x", mr, e_mr, e_mr->flags);
600 ret = -EINVAL;
601 goto query_mr_exit0;
602 }
603
604 memset(mr_attr, 0, sizeof(struct ib_mr_attr));
605 spin_lock_irqsave(&e_mr->mrlock, sl_flags);
606
607 h_ret = hipz_h_query_mr(shca->ipz_hca_handle, e_mr, &hipzout);
608 if (h_ret != H_SUCCESS) {
Joachim Fenkese3722192007-09-11 15:32:22 +0200609 ehca_err(mr->device, "hipz_mr_query failed, h_ret=%li mr=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -0700610 "hca_hndl=%lx mr_hndl=%lx lkey=%x",
611 h_ret, mr, shca->ipz_hca_handle.handle,
612 e_mr->ipz_mr_handle.handle, mr->lkey);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +0200613 ret = ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -0700614 goto query_mr_exit1;
615 }
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200616 mr_attr->pd = mr->pd;
Heiko J Schickfab97222006-09-22 15:22:22 -0700617 mr_attr->device_virt_addr = hipzout.vaddr;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200618 mr_attr->size = hipzout.len;
619 mr_attr->lkey = hipzout.lkey;
620 mr_attr->rkey = hipzout.rkey;
Heiko J Schickfab97222006-09-22 15:22:22 -0700621 ehca_mrmw_reverse_map_acl(&hipzout.acl, &mr_attr->mr_access_flags);
622
623query_mr_exit1:
624 spin_unlock_irqrestore(&e_mr->mrlock, sl_flags);
625query_mr_exit0:
626 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +0200627 ehca_err(mr->device, "ret=%i mr=%p mr_attr=%p",
Heiko J Schickfab97222006-09-22 15:22:22 -0700628 ret, mr, mr_attr);
629 return ret;
630} /* end ehca_query_mr() */
631
632/*----------------------------------------------------------------------*/
633
634int ehca_dereg_mr(struct ib_mr *mr)
635{
636 int ret = 0;
637 u64 h_ret;
638 struct ehca_shca *shca =
639 container_of(mr->device, struct ehca_shca, ib_device);
640 struct ehca_mr *e_mr = container_of(mr, struct ehca_mr, ib.ib_mr);
641 struct ehca_pd *my_pd = container_of(mr->pd, struct ehca_pd, ib_pd);
642 u32 cur_pid = current->tgid;
643
644 if (my_pd->ib_pd.uobject && my_pd->ib_pd.uobject->context &&
645 (my_pd->ownpid != cur_pid)) {
646 ehca_err(mr->device, "Invalid caller pid=%x ownpid=%x",
647 cur_pid, my_pd->ownpid);
648 ret = -EINVAL;
649 goto dereg_mr_exit0;
650 }
651
652 if ((e_mr->flags & EHCA_MR_FLAG_FMR)) {
653 ehca_err(mr->device, "not supported for FMR, mr=%p e_mr=%p "
654 "e_mr->flags=%x", mr, e_mr, e_mr->flags);
655 ret = -EINVAL;
656 goto dereg_mr_exit0;
657 } else if (e_mr == shca->maxmr) {
658 /* should be impossible, however reject to be sure */
659 ehca_err(mr->device, "dereg internal max-MR impossible, mr=%p "
660 "shca->maxmr=%p mr->lkey=%x",
661 mr, shca->maxmr, mr->lkey);
662 ret = -EINVAL;
663 goto dereg_mr_exit0;
664 }
665
666 /* TODO: BUSY: MR still has bound window(s) */
667 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_mr);
668 if (h_ret != H_SUCCESS) {
Joachim Fenkese3722192007-09-11 15:32:22 +0200669 ehca_err(mr->device, "hipz_free_mr failed, h_ret=%li shca=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -0700670 "e_mr=%p hca_hndl=%lx mr_hndl=%lx mr->lkey=%x",
671 h_ret, shca, e_mr, shca->ipz_hca_handle.handle,
672 e_mr->ipz_mr_handle.handle, mr->lkey);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +0200673 ret = ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -0700674 goto dereg_mr_exit0;
675 }
676
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800677 if (e_mr->umem)
678 ib_umem_release(e_mr->umem);
679
Heiko J Schickfab97222006-09-22 15:22:22 -0700680 /* successful deregistration */
681 ehca_mr_delete(e_mr);
682
683dereg_mr_exit0:
684 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +0200685 ehca_err(mr->device, "ret=%i mr=%p", ret, mr);
Heiko J Schickfab97222006-09-22 15:22:22 -0700686 return ret;
687} /* end ehca_dereg_mr() */
688
689/*----------------------------------------------------------------------*/
690
691struct ib_mw *ehca_alloc_mw(struct ib_pd *pd)
692{
693 struct ib_mw *ib_mw;
694 u64 h_ret;
695 struct ehca_mw *e_mw;
696 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
697 struct ehca_shca *shca =
698 container_of(pd->device, struct ehca_shca, ib_device);
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200699 struct ehca_mw_hipzout_parms hipzout;
Heiko J Schickfab97222006-09-22 15:22:22 -0700700
701 e_mw = ehca_mw_new();
702 if (!e_mw) {
703 ib_mw = ERR_PTR(-ENOMEM);
704 goto alloc_mw_exit0;
705 }
706
707 h_ret = hipz_h_alloc_resource_mw(shca->ipz_hca_handle, e_mw,
708 e_pd->fw_pd, &hipzout);
709 if (h_ret != H_SUCCESS) {
Joachim Fenkese3722192007-09-11 15:32:22 +0200710 ehca_err(pd->device, "hipz_mw_allocate failed, h_ret=%li "
Heiko J Schickfab97222006-09-22 15:22:22 -0700711 "shca=%p hca_hndl=%lx mw=%p",
712 h_ret, shca, shca->ipz_hca_handle.handle, e_mw);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +0200713 ib_mw = ERR_PTR(ehca2ib_return_code(h_ret));
Heiko J Schickfab97222006-09-22 15:22:22 -0700714 goto alloc_mw_exit1;
715 }
716 /* successful MW allocation */
717 e_mw->ipz_mw_handle = hipzout.handle;
718 e_mw->ib_mw.rkey = hipzout.rkey;
719 return &e_mw->ib_mw;
720
721alloc_mw_exit1:
722 ehca_mw_delete(e_mw);
723alloc_mw_exit0:
724 if (IS_ERR(ib_mw))
Joachim Fenkese3722192007-09-11 15:32:22 +0200725 ehca_err(pd->device, "h_ret=%li pd=%p", PTR_ERR(ib_mw), pd);
Heiko J Schickfab97222006-09-22 15:22:22 -0700726 return ib_mw;
727} /* end ehca_alloc_mw() */
728
729/*----------------------------------------------------------------------*/
730
731int ehca_bind_mw(struct ib_qp *qp,
732 struct ib_mw *mw,
733 struct ib_mw_bind *mw_bind)
734{
735 /* TODO: not supported up to now */
736 ehca_gen_err("bind MW currently not supported by HCAD");
737
738 return -EPERM;
739} /* end ehca_bind_mw() */
740
741/*----------------------------------------------------------------------*/
742
743int ehca_dealloc_mw(struct ib_mw *mw)
744{
745 u64 h_ret;
746 struct ehca_shca *shca =
747 container_of(mw->device, struct ehca_shca, ib_device);
748 struct ehca_mw *e_mw = container_of(mw, struct ehca_mw, ib_mw);
749
750 h_ret = hipz_h_free_resource_mw(shca->ipz_hca_handle, e_mw);
751 if (h_ret != H_SUCCESS) {
Joachim Fenkese3722192007-09-11 15:32:22 +0200752 ehca_err(mw->device, "hipz_free_mw failed, h_ret=%li shca=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -0700753 "mw=%p rkey=%x hca_hndl=%lx mw_hndl=%lx",
754 h_ret, shca, mw, mw->rkey, shca->ipz_hca_handle.handle,
755 e_mw->ipz_mw_handle.handle);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +0200756 return ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -0700757 }
758 /* successful deallocation */
759 ehca_mw_delete(e_mw);
760 return 0;
761} /* end ehca_dealloc_mw() */
762
763/*----------------------------------------------------------------------*/
764
765struct ib_fmr *ehca_alloc_fmr(struct ib_pd *pd,
766 int mr_access_flags,
767 struct ib_fmr_attr *fmr_attr)
768{
769 struct ib_fmr *ib_fmr;
770 struct ehca_shca *shca =
771 container_of(pd->device, struct ehca_shca, ib_device);
772 struct ehca_pd *e_pd = container_of(pd, struct ehca_pd, ib_pd);
773 struct ehca_mr *e_fmr;
774 int ret;
775 u32 tmp_lkey, tmp_rkey;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200776 struct ehca_mr_pginfo pginfo;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200777 u64 hw_pgsize;
Heiko J Schickfab97222006-09-22 15:22:22 -0700778
779 /* check other parameters */
780 if (((mr_access_flags & IB_ACCESS_REMOTE_WRITE) &&
781 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE)) ||
782 ((mr_access_flags & IB_ACCESS_REMOTE_ATOMIC) &&
783 !(mr_access_flags & IB_ACCESS_LOCAL_WRITE))) {
784 /*
785 * Remote Write Access requires Local Write Access
786 * Remote Atomic Access requires Local Write Access
787 */
788 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
789 mr_access_flags);
790 ib_fmr = ERR_PTR(-EINVAL);
791 goto alloc_fmr_exit0;
792 }
793 if (mr_access_flags & IB_ACCESS_MW_BIND) {
794 ehca_err(pd->device, "bad input values: mr_access_flags=%x",
795 mr_access_flags);
796 ib_fmr = ERR_PTR(-EINVAL);
797 goto alloc_fmr_exit0;
798 }
799 if ((fmr_attr->max_pages == 0) || (fmr_attr->max_maps == 0)) {
800 ehca_err(pd->device, "bad input values: fmr_attr->max_pages=%x "
801 "fmr_attr->max_maps=%x fmr_attr->page_shift=%x",
802 fmr_attr->max_pages, fmr_attr->max_maps,
803 fmr_attr->page_shift);
804 ib_fmr = ERR_PTR(-EINVAL);
805 goto alloc_fmr_exit0;
806 }
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200807 hw_pgsize = ehca_get_max_hwpage_size(shca);
808 if ((1 << fmr_attr->page_shift) != hw_pgsize) {
Heiko J Schickfab97222006-09-22 15:22:22 -0700809 ehca_err(pd->device, "unsupported fmr_attr->page_shift=%x",
810 fmr_attr->page_shift);
811 ib_fmr = ERR_PTR(-EINVAL);
812 goto alloc_fmr_exit0;
813 }
814
815 e_fmr = ehca_mr_new();
816 if (!e_fmr) {
817 ib_fmr = ERR_PTR(-ENOMEM);
818 goto alloc_fmr_exit0;
819 }
820 e_fmr->flags |= EHCA_MR_FLAG_FMR;
821
822 /* register MR on HCA */
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200823 memset(&pginfo, 0, sizeof(pginfo));
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200824 /*
825 * pginfo.num_hwpages==0, ie register_rpages() will not be called
826 * but deferred to map_phys_fmr()
827 */
Heiko J Schickfab97222006-09-22 15:22:22 -0700828 ret = ehca_reg_mr(shca, e_fmr, NULL,
829 fmr_attr->max_pages * (1 << fmr_attr->page_shift),
830 mr_access_flags, e_pd, &pginfo,
831 &tmp_lkey, &tmp_rkey);
832 if (ret) {
833 ib_fmr = ERR_PTR(ret);
834 goto alloc_fmr_exit1;
835 }
836
837 /* successful */
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200838 e_fmr->hwpage_size = hw_pgsize;
Heiko J Schickfab97222006-09-22 15:22:22 -0700839 e_fmr->fmr_page_size = 1 << fmr_attr->page_shift;
840 e_fmr->fmr_max_pages = fmr_attr->max_pages;
841 e_fmr->fmr_max_maps = fmr_attr->max_maps;
842 e_fmr->fmr_map_cnt = 0;
843 return &e_fmr->ib.ib_fmr;
844
845alloc_fmr_exit1:
846 ehca_mr_delete(e_fmr);
847alloc_fmr_exit0:
Heiko J Schickfab97222006-09-22 15:22:22 -0700848 return ib_fmr;
849} /* end ehca_alloc_fmr() */
850
851/*----------------------------------------------------------------------*/
852
853int ehca_map_phys_fmr(struct ib_fmr *fmr,
854 u64 *page_list,
855 int list_len,
856 u64 iova)
857{
858 int ret;
859 struct ehca_shca *shca =
860 container_of(fmr->device, struct ehca_shca, ib_device);
861 struct ehca_mr *e_fmr = container_of(fmr, struct ehca_mr, ib.ib_fmr);
862 struct ehca_pd *e_pd = container_of(fmr->pd, struct ehca_pd, ib_pd);
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200863 struct ehca_mr_pginfo pginfo;
Heiko J Schickfab97222006-09-22 15:22:22 -0700864 u32 tmp_lkey, tmp_rkey;
865
866 if (!(e_fmr->flags & EHCA_MR_FLAG_FMR)) {
867 ehca_err(fmr->device, "not a FMR, e_fmr=%p e_fmr->flags=%x",
868 e_fmr, e_fmr->flags);
869 ret = -EINVAL;
870 goto map_phys_fmr_exit0;
871 }
872 ret = ehca_fmr_check_page_list(e_fmr, page_list, list_len);
873 if (ret)
874 goto map_phys_fmr_exit0;
875 if (iova % e_fmr->fmr_page_size) {
876 /* only whole-numbered pages */
877 ehca_err(fmr->device, "bad iova, iova=%lx fmr_page_size=%x",
878 iova, e_fmr->fmr_page_size);
879 ret = -EINVAL;
880 goto map_phys_fmr_exit0;
881 }
882 if (e_fmr->fmr_map_cnt >= e_fmr->fmr_max_maps) {
883 /* HCAD does not limit the maps, however trace this anyway */
884 ehca_info(fmr->device, "map limit exceeded, fmr=%p "
885 "e_fmr->fmr_map_cnt=%x e_fmr->fmr_max_maps=%x",
886 fmr, e_fmr->fmr_map_cnt, e_fmr->fmr_max_maps);
887 }
888
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200889 memset(&pginfo, 0, sizeof(pginfo));
890 pginfo.type = EHCA_MR_PGI_FMR;
891 pginfo.num_kpages = list_len;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200892 pginfo.hwpage_size = e_fmr->hwpage_size;
893 pginfo.num_hwpages =
894 list_len * e_fmr->fmr_page_size / pginfo.hwpage_size;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +0200895 pginfo.u.fmr.page_list = page_list;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +0200896 pginfo.next_hwpage =
897 (iova & (e_fmr->fmr_page_size-1)) / pginfo.hwpage_size;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +0200898 pginfo.u.fmr.fmr_pgsize = e_fmr->fmr_page_size;
Heiko J Schickfab97222006-09-22 15:22:22 -0700899
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200900 ret = ehca_rereg_mr(shca, e_fmr, (u64 *)iova,
Heiko J Schickfab97222006-09-22 15:22:22 -0700901 list_len * e_fmr->fmr_page_size,
902 e_fmr->acl, e_pd, &pginfo, &tmp_lkey, &tmp_rkey);
903 if (ret)
904 goto map_phys_fmr_exit0;
905
906 /* successful reregistration */
907 e_fmr->fmr_map_cnt++;
908 e_fmr->ib.ib_fmr.lkey = tmp_lkey;
909 e_fmr->ib.ib_fmr.rkey = tmp_rkey;
910 return 0;
911
912map_phys_fmr_exit0:
913 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +0200914 ehca_err(fmr->device, "ret=%i fmr=%p page_list=%p list_len=%x "
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +0200915 "iova=%lx", ret, fmr, page_list, list_len, iova);
Heiko J Schickfab97222006-09-22 15:22:22 -0700916 return ret;
917} /* end ehca_map_phys_fmr() */
918
919/*----------------------------------------------------------------------*/
920
921int ehca_unmap_fmr(struct list_head *fmr_list)
922{
923 int ret = 0;
924 struct ib_fmr *ib_fmr;
925 struct ehca_shca *shca = NULL;
926 struct ehca_shca *prev_shca;
927 struct ehca_mr *e_fmr;
928 u32 num_fmr = 0;
929 u32 unmap_fmr_cnt = 0;
930
931 /* check all FMR belong to same SHCA, and check internal flag */
932 list_for_each_entry(ib_fmr, fmr_list, list) {
933 prev_shca = shca;
934 if (!ib_fmr) {
935 ehca_gen_err("bad fmr=%p in list", ib_fmr);
936 ret = -EINVAL;
937 goto unmap_fmr_exit0;
938 }
939 shca = container_of(ib_fmr->device, struct ehca_shca,
940 ib_device);
941 e_fmr = container_of(ib_fmr, struct ehca_mr, ib.ib_fmr);
942 if ((shca != prev_shca) && prev_shca) {
943 ehca_err(&shca->ib_device, "SHCA mismatch, shca=%p "
944 "prev_shca=%p e_fmr=%p",
945 shca, prev_shca, e_fmr);
946 ret = -EINVAL;
947 goto unmap_fmr_exit0;
948 }
949 if (!(e_fmr->flags & EHCA_MR_FLAG_FMR)) {
950 ehca_err(&shca->ib_device, "not a FMR, e_fmr=%p "
951 "e_fmr->flags=%x", e_fmr, e_fmr->flags);
952 ret = -EINVAL;
953 goto unmap_fmr_exit0;
954 }
955 num_fmr++;
956 }
957
958 /* loop over all FMRs to unmap */
959 list_for_each_entry(ib_fmr, fmr_list, list) {
960 unmap_fmr_cnt++;
961 e_fmr = container_of(ib_fmr, struct ehca_mr, ib.ib_fmr);
962 shca = container_of(ib_fmr->device, struct ehca_shca,
963 ib_device);
964 ret = ehca_unmap_one_fmr(shca, e_fmr);
965 if (ret) {
966 /* unmap failed, stop unmapping of rest of FMRs */
967 ehca_err(&shca->ib_device, "unmap of one FMR failed, "
968 "stop rest, e_fmr=%p num_fmr=%x "
969 "unmap_fmr_cnt=%x lkey=%x", e_fmr, num_fmr,
970 unmap_fmr_cnt, e_fmr->ib.ib_fmr.lkey);
971 goto unmap_fmr_exit0;
972 }
973 }
974
975unmap_fmr_exit0:
976 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +0200977 ehca_gen_err("ret=%i fmr_list=%p num_fmr=%x unmap_fmr_cnt=%x",
Heiko J Schickfab97222006-09-22 15:22:22 -0700978 ret, fmr_list, num_fmr, unmap_fmr_cnt);
979 return ret;
980} /* end ehca_unmap_fmr() */
981
982/*----------------------------------------------------------------------*/
983
984int ehca_dealloc_fmr(struct ib_fmr *fmr)
985{
986 int ret;
987 u64 h_ret;
988 struct ehca_shca *shca =
989 container_of(fmr->device, struct ehca_shca, ib_device);
990 struct ehca_mr *e_fmr = container_of(fmr, struct ehca_mr, ib.ib_fmr);
991
992 if (!(e_fmr->flags & EHCA_MR_FLAG_FMR)) {
993 ehca_err(fmr->device, "not a FMR, e_fmr=%p e_fmr->flags=%x",
994 e_fmr, e_fmr->flags);
995 ret = -EINVAL;
996 goto free_fmr_exit0;
997 }
998
999 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_fmr);
1000 if (h_ret != H_SUCCESS) {
Joachim Fenkese3722192007-09-11 15:32:22 +02001001 ehca_err(fmr->device, "hipz_free_mr failed, h_ret=%li e_fmr=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -07001002 "hca_hndl=%lx fmr_hndl=%lx fmr->lkey=%x",
1003 h_ret, e_fmr, shca->ipz_hca_handle.handle,
1004 e_fmr->ipz_mr_handle.handle, fmr->lkey);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +02001005 ret = ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -07001006 goto free_fmr_exit0;
1007 }
1008 /* successful deregistration */
1009 ehca_mr_delete(e_fmr);
1010 return 0;
1011
1012free_fmr_exit0:
1013 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +02001014 ehca_err(&shca->ib_device, "ret=%i fmr=%p", ret, fmr);
Heiko J Schickfab97222006-09-22 15:22:22 -07001015 return ret;
1016} /* end ehca_dealloc_fmr() */
1017
1018/*----------------------------------------------------------------------*/
1019
1020int ehca_reg_mr(struct ehca_shca *shca,
1021 struct ehca_mr *e_mr,
1022 u64 *iova_start,
1023 u64 size,
1024 int acl,
1025 struct ehca_pd *e_pd,
1026 struct ehca_mr_pginfo *pginfo,
1027 u32 *lkey, /*OUT*/
1028 u32 *rkey) /*OUT*/
1029{
1030 int ret;
1031 u64 h_ret;
1032 u32 hipz_acl;
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +02001033 struct ehca_mr_hipzout_parms hipzout;
Heiko J Schickfab97222006-09-22 15:22:22 -07001034
1035 ehca_mrmw_map_acl(acl, &hipz_acl);
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001036 ehca_mrmw_set_pgsize_hipz_acl(pginfo->hwpage_size, &hipz_acl);
Heiko J Schickfab97222006-09-22 15:22:22 -07001037 if (ehca_use_hp_mr == 1)
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +02001038 hipz_acl |= 0x00000001;
Heiko J Schickfab97222006-09-22 15:22:22 -07001039
1040 h_ret = hipz_h_alloc_resource_mr(shca->ipz_hca_handle, e_mr,
1041 (u64)iova_start, size, hipz_acl,
1042 e_pd->fw_pd, &hipzout);
1043 if (h_ret != H_SUCCESS) {
Joachim Fenkese3722192007-09-11 15:32:22 +02001044 ehca_err(&shca->ib_device, "hipz_alloc_mr failed, h_ret=%li "
Heiko J Schickfab97222006-09-22 15:22:22 -07001045 "hca_hndl=%lx", h_ret, shca->ipz_hca_handle.handle);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +02001046 ret = ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -07001047 goto ehca_reg_mr_exit0;
1048 }
1049
1050 e_mr->ipz_mr_handle = hipzout.handle;
1051
1052 ret = ehca_reg_mr_rpages(shca, e_mr, pginfo);
1053 if (ret)
1054 goto ehca_reg_mr_exit1;
1055
1056 /* successful registration */
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001057 e_mr->num_kpages = pginfo->num_kpages;
1058 e_mr->num_hwpages = pginfo->num_hwpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001059 e_mr->hwpage_size = pginfo->hwpage_size;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001060 e_mr->start = iova_start;
1061 e_mr->size = size;
1062 e_mr->acl = acl;
Heiko J Schickfab97222006-09-22 15:22:22 -07001063 *lkey = hipzout.lkey;
1064 *rkey = hipzout.rkey;
1065 return 0;
1066
1067ehca_reg_mr_exit1:
1068 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_mr);
1069 if (h_ret != H_SUCCESS) {
Joachim Fenkese3722192007-09-11 15:32:22 +02001070 ehca_err(&shca->ib_device, "h_ret=%li shca=%p e_mr=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -07001071 "iova_start=%p size=%lx acl=%x e_pd=%p lkey=%x "
Joachim Fenkese3722192007-09-11 15:32:22 +02001072 "pginfo=%p num_kpages=%lx num_hwpages=%lx ret=%i",
Heiko J Schickfab97222006-09-22 15:22:22 -07001073 h_ret, shca, e_mr, iova_start, size, acl, e_pd,
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001074 hipzout.lkey, pginfo, pginfo->num_kpages,
1075 pginfo->num_hwpages, ret);
Heiko J Schickfab97222006-09-22 15:22:22 -07001076 ehca_err(&shca->ib_device, "internal error in ehca_reg_mr, "
1077 "not recoverable");
1078 }
1079ehca_reg_mr_exit0:
1080 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +02001081 ehca_err(&shca->ib_device, "ret=%i shca=%p e_mr=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -07001082 "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001083 "num_kpages=%lx num_hwpages=%lx",
Heiko J Schickfab97222006-09-22 15:22:22 -07001084 ret, shca, e_mr, iova_start, size, acl, e_pd, pginfo,
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001085 pginfo->num_kpages, pginfo->num_hwpages);
Heiko J Schickfab97222006-09-22 15:22:22 -07001086 return ret;
1087} /* end ehca_reg_mr() */
1088
1089/*----------------------------------------------------------------------*/
1090
1091int ehca_reg_mr_rpages(struct ehca_shca *shca,
1092 struct ehca_mr *e_mr,
1093 struct ehca_mr_pginfo *pginfo)
1094{
1095 int ret = 0;
1096 u64 h_ret;
1097 u32 rnum;
1098 u64 rpage;
1099 u32 i;
1100 u64 *kpage;
1101
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001102 if (!pginfo->num_hwpages) /* in case of fmr */
1103 return 0;
1104
Hoang-Nam Nguyenf2d91362007-01-09 18:04:14 +01001105 kpage = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
Heiko J Schickfab97222006-09-22 15:22:22 -07001106 if (!kpage) {
1107 ehca_err(&shca->ib_device, "kpage alloc failed");
1108 ret = -ENOMEM;
1109 goto ehca_reg_mr_rpages_exit0;
1110 }
1111
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001112 /* max MAX_RPAGES ehca mr pages per register call */
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001113 for (i = 0; i < NUM_CHUNKS(pginfo->num_hwpages, MAX_RPAGES); i++) {
Heiko J Schickfab97222006-09-22 15:22:22 -07001114
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001115 if (i == NUM_CHUNKS(pginfo->num_hwpages, MAX_RPAGES) - 1) {
1116 rnum = pginfo->num_hwpages % MAX_RPAGES; /* last shot */
Heiko J Schickfab97222006-09-22 15:22:22 -07001117 if (rnum == 0)
Hoang-Nam Nguyen4e4e74c2007-07-12 17:51:04 +02001118 rnum = MAX_RPAGES; /* last shot is full */
Heiko J Schickfab97222006-09-22 15:22:22 -07001119 } else
Hoang-Nam Nguyen4e4e74c2007-07-12 17:51:04 +02001120 rnum = MAX_RPAGES;
Heiko J Schickfab97222006-09-22 15:22:22 -07001121
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001122 ret = ehca_set_pagebuf(pginfo, rnum, kpage);
1123 if (ret) {
1124 ehca_err(&shca->ib_device, "ehca_set_pagebuf "
Joachim Fenkese3722192007-09-11 15:32:22 +02001125 "bad rc, ret=%i rnum=%x kpage=%p",
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001126 ret, rnum, kpage);
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001127 goto ehca_reg_mr_rpages_exit1;
1128 }
1129
1130 if (rnum > 1) {
Heiko J Schickfab97222006-09-22 15:22:22 -07001131 rpage = virt_to_abs(kpage);
1132 if (!rpage) {
1133 ehca_err(&shca->ib_device, "kpage=%p i=%x",
1134 kpage, i);
1135 ret = -EFAULT;
1136 goto ehca_reg_mr_rpages_exit1;
1137 }
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001138 } else
1139 rpage = *kpage;
Heiko J Schickfab97222006-09-22 15:22:22 -07001140
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001141 h_ret = hipz_h_register_rpage_mr(
1142 shca->ipz_hca_handle, e_mr,
1143 ehca_encode_hwpage_size(pginfo->hwpage_size),
1144 0, rpage, rnum);
Heiko J Schickfab97222006-09-22 15:22:22 -07001145
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001146 if (i == NUM_CHUNKS(pginfo->num_hwpages, MAX_RPAGES) - 1) {
Heiko J Schickfab97222006-09-22 15:22:22 -07001147 /*
1148 * check for 'registration complete'==H_SUCCESS
1149 * and for 'page registered'==H_PAGE_REGISTERED
1150 */
1151 if (h_ret != H_SUCCESS) {
1152 ehca_err(&shca->ib_device, "last "
Joachim Fenkese3722192007-09-11 15:32:22 +02001153 "hipz_reg_rpage_mr failed, h_ret=%li "
Heiko J Schickfab97222006-09-22 15:22:22 -07001154 "e_mr=%p i=%x hca_hndl=%lx mr_hndl=%lx"
1155 " lkey=%x", h_ret, e_mr, i,
1156 shca->ipz_hca_handle.handle,
1157 e_mr->ipz_mr_handle.handle,
1158 e_mr->ib.ib_mr.lkey);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +02001159 ret = ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -07001160 break;
1161 } else
1162 ret = 0;
1163 } else if (h_ret != H_PAGE_REGISTERED) {
1164 ehca_err(&shca->ib_device, "hipz_reg_rpage_mr failed, "
Joachim Fenkese3722192007-09-11 15:32:22 +02001165 "h_ret=%li e_mr=%p i=%x lkey=%x hca_hndl=%lx "
Heiko J Schickfab97222006-09-22 15:22:22 -07001166 "mr_hndl=%lx", h_ret, e_mr, i,
1167 e_mr->ib.ib_mr.lkey,
1168 shca->ipz_hca_handle.handle,
1169 e_mr->ipz_mr_handle.handle);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +02001170 ret = ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -07001171 break;
1172 } else
1173 ret = 0;
1174 } /* end for(i) */
1175
1176
1177ehca_reg_mr_rpages_exit1:
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +01001178 ehca_free_fw_ctrlblock(kpage);
Heiko J Schickfab97222006-09-22 15:22:22 -07001179ehca_reg_mr_rpages_exit0:
1180 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +02001181 ehca_err(&shca->ib_device, "ret=%i shca=%p e_mr=%p pginfo=%p "
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001182 "num_kpages=%lx num_hwpages=%lx", ret, shca, e_mr,
1183 pginfo, pginfo->num_kpages, pginfo->num_hwpages);
Heiko J Schickfab97222006-09-22 15:22:22 -07001184 return ret;
1185} /* end ehca_reg_mr_rpages() */
1186
1187/*----------------------------------------------------------------------*/
1188
1189inline int ehca_rereg_mr_rereg1(struct ehca_shca *shca,
1190 struct ehca_mr *e_mr,
1191 u64 *iova_start,
1192 u64 size,
1193 u32 acl,
1194 struct ehca_pd *e_pd,
1195 struct ehca_mr_pginfo *pginfo,
1196 u32 *lkey, /*OUT*/
1197 u32 *rkey) /*OUT*/
1198{
1199 int ret;
1200 u64 h_ret;
1201 u32 hipz_acl;
1202 u64 *kpage;
1203 u64 rpage;
1204 struct ehca_mr_pginfo pginfo_save;
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +02001205 struct ehca_mr_hipzout_parms hipzout;
Heiko J Schickfab97222006-09-22 15:22:22 -07001206
1207 ehca_mrmw_map_acl(acl, &hipz_acl);
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001208 ehca_mrmw_set_pgsize_hipz_acl(pginfo->hwpage_size, &hipz_acl);
Heiko J Schickfab97222006-09-22 15:22:22 -07001209
Hoang-Nam Nguyenf2d91362007-01-09 18:04:14 +01001210 kpage = ehca_alloc_fw_ctrlblock(GFP_KERNEL);
Heiko J Schickfab97222006-09-22 15:22:22 -07001211 if (!kpage) {
1212 ehca_err(&shca->ib_device, "kpage alloc failed");
1213 ret = -ENOMEM;
1214 goto ehca_rereg_mr_rereg1_exit0;
1215 }
1216
1217 pginfo_save = *pginfo;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001218 ret = ehca_set_pagebuf(pginfo, pginfo->num_hwpages, kpage);
Heiko J Schickfab97222006-09-22 15:22:22 -07001219 if (ret) {
1220 ehca_err(&shca->ib_device, "set pagebuf failed, e_mr=%p "
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001221 "pginfo=%p type=%x num_kpages=%lx num_hwpages=%lx "
1222 "kpage=%p", e_mr, pginfo, pginfo->type,
1223 pginfo->num_kpages, pginfo->num_hwpages, kpage);
Heiko J Schickfab97222006-09-22 15:22:22 -07001224 goto ehca_rereg_mr_rereg1_exit1;
1225 }
1226 rpage = virt_to_abs(kpage);
1227 if (!rpage) {
1228 ehca_err(&shca->ib_device, "kpage=%p", kpage);
1229 ret = -EFAULT;
1230 goto ehca_rereg_mr_rereg1_exit1;
1231 }
1232 h_ret = hipz_h_reregister_pmr(shca->ipz_hca_handle, e_mr,
1233 (u64)iova_start, size, hipz_acl,
1234 e_pd->fw_pd, rpage, &hipzout);
1235 if (h_ret != H_SUCCESS) {
1236 /*
1237 * reregistration unsuccessful, try it again with the 3 hCalls,
1238 * e.g. this is required in case H_MR_CONDITION
1239 * (MW bound or MR is shared)
1240 */
1241 ehca_warn(&shca->ib_device, "hipz_h_reregister_pmr failed "
Joachim Fenkese3722192007-09-11 15:32:22 +02001242 "(Rereg1), h_ret=%li e_mr=%p", h_ret, e_mr);
Heiko J Schickfab97222006-09-22 15:22:22 -07001243 *pginfo = pginfo_save;
1244 ret = -EAGAIN;
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +02001245 } else if ((u64 *)hipzout.vaddr != iova_start) {
Heiko J Schickfab97222006-09-22 15:22:22 -07001246 ehca_err(&shca->ib_device, "PHYP changed iova_start in "
1247 "rereg_pmr, iova_start=%p iova_start_out=%lx e_mr=%p "
1248 "mr_handle=%lx lkey=%x lkey_out=%x", iova_start,
1249 hipzout.vaddr, e_mr, e_mr->ipz_mr_handle.handle,
1250 e_mr->ib.ib_mr.lkey, hipzout.lkey);
1251 ret = -EFAULT;
1252 } else {
1253 /*
1254 * successful reregistration
1255 * note: start and start_out are identical for eServer HCAs
1256 */
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001257 e_mr->num_kpages = pginfo->num_kpages;
1258 e_mr->num_hwpages = pginfo->num_hwpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001259 e_mr->hwpage_size = pginfo->hwpage_size;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001260 e_mr->start = iova_start;
1261 e_mr->size = size;
1262 e_mr->acl = acl;
Heiko J Schickfab97222006-09-22 15:22:22 -07001263 *lkey = hipzout.lkey;
1264 *rkey = hipzout.rkey;
1265 }
1266
1267ehca_rereg_mr_rereg1_exit1:
Hoang-Nam Nguyen7e28db52006-11-07 00:56:39 +01001268 ehca_free_fw_ctrlblock(kpage);
Heiko J Schickfab97222006-09-22 15:22:22 -07001269ehca_rereg_mr_rereg1_exit0:
1270 if ( ret && (ret != -EAGAIN) )
Joachim Fenkese3722192007-09-11 15:32:22 +02001271 ehca_err(&shca->ib_device, "ret=%i lkey=%x rkey=%x "
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001272 "pginfo=%p num_kpages=%lx num_hwpages=%lx",
1273 ret, *lkey, *rkey, pginfo, pginfo->num_kpages,
1274 pginfo->num_hwpages);
Heiko J Schickfab97222006-09-22 15:22:22 -07001275 return ret;
1276} /* end ehca_rereg_mr_rereg1() */
1277
1278/*----------------------------------------------------------------------*/
1279
1280int ehca_rereg_mr(struct ehca_shca *shca,
1281 struct ehca_mr *e_mr,
1282 u64 *iova_start,
1283 u64 size,
1284 int acl,
1285 struct ehca_pd *e_pd,
1286 struct ehca_mr_pginfo *pginfo,
1287 u32 *lkey,
1288 u32 *rkey)
1289{
1290 int ret = 0;
1291 u64 h_ret;
1292 int rereg_1_hcall = 1; /* 1: use hipz_h_reregister_pmr directly */
1293 int rereg_3_hcall = 0; /* 1: use 3 hipz calls for reregistration */
1294
1295 /* first determine reregistration hCall(s) */
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001296 if ((pginfo->num_hwpages > MAX_RPAGES) ||
1297 (e_mr->num_hwpages > MAX_RPAGES) ||
1298 (pginfo->num_hwpages > e_mr->num_hwpages)) {
1299 ehca_dbg(&shca->ib_device, "Rereg3 case, "
1300 "pginfo->num_hwpages=%lx e_mr->num_hwpages=%x",
1301 pginfo->num_hwpages, e_mr->num_hwpages);
Heiko J Schickfab97222006-09-22 15:22:22 -07001302 rereg_1_hcall = 0;
1303 rereg_3_hcall = 1;
1304 }
1305
1306 if (e_mr->flags & EHCA_MR_FLAG_MAXMR) { /* check for max-MR */
1307 rereg_1_hcall = 0;
1308 rereg_3_hcall = 1;
1309 e_mr->flags &= ~EHCA_MR_FLAG_MAXMR;
1310 ehca_err(&shca->ib_device, "Rereg MR for max-MR! e_mr=%p",
1311 e_mr);
1312 }
1313
1314 if (rereg_1_hcall) {
1315 ret = ehca_rereg_mr_rereg1(shca, e_mr, iova_start, size,
1316 acl, e_pd, pginfo, lkey, rkey);
1317 if (ret) {
1318 if (ret == -EAGAIN)
1319 rereg_3_hcall = 1;
1320 else
1321 goto ehca_rereg_mr_exit0;
1322 }
1323 }
1324
1325 if (rereg_3_hcall) {
1326 struct ehca_mr save_mr;
1327
1328 /* first deregister old MR */
1329 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_mr);
1330 if (h_ret != H_SUCCESS) {
1331 ehca_err(&shca->ib_device, "hipz_free_mr failed, "
Joachim Fenkese3722192007-09-11 15:32:22 +02001332 "h_ret=%li e_mr=%p hca_hndl=%lx mr_hndl=%lx "
Heiko J Schickfab97222006-09-22 15:22:22 -07001333 "mr->lkey=%x",
1334 h_ret, e_mr, shca->ipz_hca_handle.handle,
1335 e_mr->ipz_mr_handle.handle,
1336 e_mr->ib.ib_mr.lkey);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +02001337 ret = ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -07001338 goto ehca_rereg_mr_exit0;
1339 }
1340 /* clean ehca_mr_t, without changing struct ib_mr and lock */
1341 save_mr = *e_mr;
1342 ehca_mr_deletenew(e_mr);
1343
1344 /* set some MR values */
1345 e_mr->flags = save_mr.flags;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001346 e_mr->hwpage_size = save_mr.hwpage_size;
Heiko J Schickfab97222006-09-22 15:22:22 -07001347 e_mr->fmr_page_size = save_mr.fmr_page_size;
1348 e_mr->fmr_max_pages = save_mr.fmr_max_pages;
1349 e_mr->fmr_max_maps = save_mr.fmr_max_maps;
1350 e_mr->fmr_map_cnt = save_mr.fmr_map_cnt;
1351
1352 ret = ehca_reg_mr(shca, e_mr, iova_start, size, acl,
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001353 e_pd, pginfo, lkey, rkey);
Heiko J Schickfab97222006-09-22 15:22:22 -07001354 if (ret) {
1355 u32 offset = (u64)(&e_mr->flags) - (u64)e_mr;
1356 memcpy(&e_mr->flags, &(save_mr.flags),
1357 sizeof(struct ehca_mr) - offset);
1358 goto ehca_rereg_mr_exit0;
1359 }
1360 }
1361
1362ehca_rereg_mr_exit0:
1363 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +02001364 ehca_err(&shca->ib_device, "ret=%i shca=%p e_mr=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -07001365 "iova_start=%p size=%lx acl=%x e_pd=%p pginfo=%p "
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001366 "num_kpages=%lx lkey=%x rkey=%x rereg_1_hcall=%x "
Heiko J Schickfab97222006-09-22 15:22:22 -07001367 "rereg_3_hcall=%x", ret, shca, e_mr, iova_start, size,
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001368 acl, e_pd, pginfo, pginfo->num_kpages, *lkey, *rkey,
Heiko J Schickfab97222006-09-22 15:22:22 -07001369 rereg_1_hcall, rereg_3_hcall);
1370 return ret;
1371} /* end ehca_rereg_mr() */
1372
1373/*----------------------------------------------------------------------*/
1374
1375int ehca_unmap_one_fmr(struct ehca_shca *shca,
1376 struct ehca_mr *e_fmr)
1377{
1378 int ret = 0;
1379 u64 h_ret;
Heiko J Schickfab97222006-09-22 15:22:22 -07001380 struct ehca_pd *e_pd =
1381 container_of(e_fmr->ib.ib_fmr.pd, struct ehca_pd, ib_pd);
1382 struct ehca_mr save_fmr;
1383 u32 tmp_lkey, tmp_rkey;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001384 struct ehca_mr_pginfo pginfo;
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +02001385 struct ehca_mr_hipzout_parms hipzout;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001386 struct ehca_mr save_mr;
Heiko J Schickfab97222006-09-22 15:22:22 -07001387
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001388 if (e_fmr->fmr_max_pages <= MAX_RPAGES) {
Heiko J Schickfab97222006-09-22 15:22:22 -07001389 /*
1390 * note: after using rereg hcall with len=0,
1391 * rereg hcall must be used again for registering pages
1392 */
1393 h_ret = hipz_h_reregister_pmr(shca->ipz_hca_handle, e_fmr, 0,
1394 0, 0, e_pd->fw_pd, 0, &hipzout);
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001395 if (h_ret == H_SUCCESS) {
Heiko J Schickfab97222006-09-22 15:22:22 -07001396 /* successful reregistration */
1397 e_fmr->start = NULL;
1398 e_fmr->size = 0;
1399 tmp_lkey = hipzout.lkey;
1400 tmp_rkey = hipzout.rkey;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001401 return 0;
Heiko J Schickfab97222006-09-22 15:22:22 -07001402 }
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001403 /*
1404 * should not happen, because length checked above,
1405 * FMRs are not shared and no MW bound to FMRs
1406 */
1407 ehca_err(&shca->ib_device, "hipz_reregister_pmr failed "
Joachim Fenkese3722192007-09-11 15:32:22 +02001408 "(Rereg1), h_ret=%li e_fmr=%p hca_hndl=%lx "
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001409 "mr_hndl=%lx lkey=%x lkey_out=%x",
1410 h_ret, e_fmr, shca->ipz_hca_handle.handle,
1411 e_fmr->ipz_mr_handle.handle,
1412 e_fmr->ib.ib_fmr.lkey, hipzout.lkey);
1413 /* try free and rereg */
Heiko J Schickfab97222006-09-22 15:22:22 -07001414 }
1415
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001416 /* first free old FMR */
1417 h_ret = hipz_h_free_resource_mr(shca->ipz_hca_handle, e_fmr);
1418 if (h_ret != H_SUCCESS) {
1419 ehca_err(&shca->ib_device, "hipz_free_mr failed, "
Joachim Fenkese3722192007-09-11 15:32:22 +02001420 "h_ret=%li e_fmr=%p hca_hndl=%lx mr_hndl=%lx "
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001421 "lkey=%x",
1422 h_ret, e_fmr, shca->ipz_hca_handle.handle,
1423 e_fmr->ipz_mr_handle.handle,
1424 e_fmr->ib.ib_fmr.lkey);
1425 ret = ehca2ib_return_code(h_ret);
1426 goto ehca_unmap_one_fmr_exit0;
1427 }
1428 /* clean ehca_mr_t, without changing lock */
1429 save_fmr = *e_fmr;
1430 ehca_mr_deletenew(e_fmr);
Heiko J Schickfab97222006-09-22 15:22:22 -07001431
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001432 /* set some MR values */
1433 e_fmr->flags = save_fmr.flags;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001434 e_fmr->hwpage_size = save_fmr.hwpage_size;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001435 e_fmr->fmr_page_size = save_fmr.fmr_page_size;
1436 e_fmr->fmr_max_pages = save_fmr.fmr_max_pages;
1437 e_fmr->fmr_max_maps = save_fmr.fmr_max_maps;
1438 e_fmr->fmr_map_cnt = save_fmr.fmr_map_cnt;
1439 e_fmr->acl = save_fmr.acl;
Heiko J Schickfab97222006-09-22 15:22:22 -07001440
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001441 memset(&pginfo, 0, sizeof(pginfo));
1442 pginfo.type = EHCA_MR_PGI_FMR;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001443 ret = ehca_reg_mr(shca, e_fmr, NULL,
1444 (e_fmr->fmr_max_pages * e_fmr->fmr_page_size),
1445 e_fmr->acl, e_pd, &pginfo, &tmp_lkey,
1446 &tmp_rkey);
1447 if (ret) {
1448 u32 offset = (u64)(&e_fmr->flags) - (u64)e_fmr;
1449 memcpy(&e_fmr->flags, &(save_mr.flags),
1450 sizeof(struct ehca_mr) - offset);
Heiko J Schickfab97222006-09-22 15:22:22 -07001451 }
1452
1453ehca_unmap_one_fmr_exit0:
1454 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +02001455 ehca_err(&shca->ib_device, "ret=%i tmp_lkey=%x tmp_rkey=%x "
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001456 "fmr_max_pages=%x",
1457 ret, tmp_lkey, tmp_rkey, e_fmr->fmr_max_pages);
Heiko J Schickfab97222006-09-22 15:22:22 -07001458 return ret;
1459} /* end ehca_unmap_one_fmr() */
1460
1461/*----------------------------------------------------------------------*/
1462
1463int ehca_reg_smr(struct ehca_shca *shca,
1464 struct ehca_mr *e_origmr,
1465 struct ehca_mr *e_newmr,
1466 u64 *iova_start,
1467 int acl,
1468 struct ehca_pd *e_pd,
1469 u32 *lkey, /*OUT*/
1470 u32 *rkey) /*OUT*/
1471{
1472 int ret = 0;
1473 u64 h_ret;
1474 u32 hipz_acl;
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +02001475 struct ehca_mr_hipzout_parms hipzout;
Heiko J Schickfab97222006-09-22 15:22:22 -07001476
1477 ehca_mrmw_map_acl(acl, &hipz_acl);
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001478 ehca_mrmw_set_pgsize_hipz_acl(e_origmr->hwpage_size, &hipz_acl);
Heiko J Schickfab97222006-09-22 15:22:22 -07001479
1480 h_ret = hipz_h_register_smr(shca->ipz_hca_handle, e_newmr, e_origmr,
1481 (u64)iova_start, hipz_acl, e_pd->fw_pd,
1482 &hipzout);
1483 if (h_ret != H_SUCCESS) {
Joachim Fenkese3722192007-09-11 15:32:22 +02001484 ehca_err(&shca->ib_device, "hipz_reg_smr failed, h_ret=%li "
Heiko J Schickfab97222006-09-22 15:22:22 -07001485 "shca=%p e_origmr=%p e_newmr=%p iova_start=%p acl=%x "
1486 "e_pd=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1487 h_ret, shca, e_origmr, e_newmr, iova_start, acl, e_pd,
1488 shca->ipz_hca_handle.handle,
1489 e_origmr->ipz_mr_handle.handle,
1490 e_origmr->ib.ib_mr.lkey);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +02001491 ret = ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -07001492 goto ehca_reg_smr_exit0;
1493 }
1494 /* successful registration */
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001495 e_newmr->num_kpages = e_origmr->num_kpages;
1496 e_newmr->num_hwpages = e_origmr->num_hwpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001497 e_newmr->hwpage_size = e_origmr->hwpage_size;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001498 e_newmr->start = iova_start;
1499 e_newmr->size = e_origmr->size;
1500 e_newmr->acl = acl;
Heiko J Schickfab97222006-09-22 15:22:22 -07001501 e_newmr->ipz_mr_handle = hipzout.handle;
1502 *lkey = hipzout.lkey;
1503 *rkey = hipzout.rkey;
1504 return 0;
1505
1506ehca_reg_smr_exit0:
1507 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +02001508 ehca_err(&shca->ib_device, "ret=%i shca=%p e_origmr=%p "
Heiko J Schickfab97222006-09-22 15:22:22 -07001509 "e_newmr=%p iova_start=%p acl=%x e_pd=%p",
1510 ret, shca, e_origmr, e_newmr, iova_start, acl, e_pd);
1511 return ret;
1512} /* end ehca_reg_smr() */
1513
1514/*----------------------------------------------------------------------*/
1515
1516/* register internal max-MR to internal SHCA */
1517int ehca_reg_internal_maxmr(
1518 struct ehca_shca *shca,
1519 struct ehca_pd *e_pd,
1520 struct ehca_mr **e_maxmr) /*OUT*/
1521{
1522 int ret;
1523 struct ehca_mr *e_mr;
1524 u64 *iova_start;
1525 u64 size_maxmr;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001526 struct ehca_mr_pginfo pginfo;
Heiko J Schickfab97222006-09-22 15:22:22 -07001527 struct ib_phys_buf ib_pbuf;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001528 u32 num_kpages;
1529 u32 num_hwpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001530 u64 hw_pgsize;
Heiko J Schickfab97222006-09-22 15:22:22 -07001531
1532 e_mr = ehca_mr_new();
1533 if (!e_mr) {
1534 ehca_err(&shca->ib_device, "out of memory");
1535 ret = -ENOMEM;
1536 goto ehca_reg_internal_maxmr_exit0;
1537 }
1538 e_mr->flags |= EHCA_MR_FLAG_MAXMR;
1539
1540 /* register internal max-MR on HCA */
1541 size_maxmr = (u64)high_memory - PAGE_OFFSET;
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +02001542 iova_start = (u64 *)KERNELBASE;
Heiko J Schickfab97222006-09-22 15:22:22 -07001543 ib_pbuf.addr = 0;
1544 ib_pbuf.size = size_maxmr;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001545 num_kpages = NUM_CHUNKS(((u64)iova_start % PAGE_SIZE) + size_maxmr,
1546 PAGE_SIZE);
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001547 hw_pgsize = ehca_get_max_hwpage_size(shca);
1548 num_hwpages = NUM_CHUNKS(((u64)iova_start % hw_pgsize) + size_maxmr,
1549 hw_pgsize);
Heiko J Schickfab97222006-09-22 15:22:22 -07001550
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001551 memset(&pginfo, 0, sizeof(pginfo));
1552 pginfo.type = EHCA_MR_PGI_PHYS;
1553 pginfo.num_kpages = num_kpages;
1554 pginfo.num_hwpages = num_hwpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001555 pginfo.hwpage_size = hw_pgsize;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001556 pginfo.u.phy.num_phys_buf = 1;
1557 pginfo.u.phy.phys_buf_array = &ib_pbuf;
Heiko J Schickfab97222006-09-22 15:22:22 -07001558
1559 ret = ehca_reg_mr(shca, e_mr, iova_start, size_maxmr, 0, e_pd,
1560 &pginfo, &e_mr->ib.ib_mr.lkey,
1561 &e_mr->ib.ib_mr.rkey);
1562 if (ret) {
1563 ehca_err(&shca->ib_device, "reg of internal max MR failed, "
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001564 "e_mr=%p iova_start=%p size_maxmr=%lx num_kpages=%x "
1565 "num_hwpages=%x", e_mr, iova_start, size_maxmr,
1566 num_kpages, num_hwpages);
Heiko J Schickfab97222006-09-22 15:22:22 -07001567 goto ehca_reg_internal_maxmr_exit1;
1568 }
1569
1570 /* successful registration of all pages */
1571 e_mr->ib.ib_mr.device = e_pd->ib_pd.device;
1572 e_mr->ib.ib_mr.pd = &e_pd->ib_pd;
1573 e_mr->ib.ib_mr.uobject = NULL;
1574 atomic_inc(&(e_pd->ib_pd.usecnt));
1575 atomic_set(&(e_mr->ib.ib_mr.usecnt), 0);
1576 *e_maxmr = e_mr;
1577 return 0;
1578
1579ehca_reg_internal_maxmr_exit1:
1580 ehca_mr_delete(e_mr);
1581ehca_reg_internal_maxmr_exit0:
1582 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +02001583 ehca_err(&shca->ib_device, "ret=%i shca=%p e_pd=%p e_maxmr=%p",
Heiko J Schickfab97222006-09-22 15:22:22 -07001584 ret, shca, e_pd, e_maxmr);
1585 return ret;
1586} /* end ehca_reg_internal_maxmr() */
1587
1588/*----------------------------------------------------------------------*/
1589
1590int ehca_reg_maxmr(struct ehca_shca *shca,
1591 struct ehca_mr *e_newmr,
1592 u64 *iova_start,
1593 int acl,
1594 struct ehca_pd *e_pd,
1595 u32 *lkey,
1596 u32 *rkey)
1597{
1598 u64 h_ret;
1599 struct ehca_mr *e_origmr = shca->maxmr;
1600 u32 hipz_acl;
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +02001601 struct ehca_mr_hipzout_parms hipzout;
Heiko J Schickfab97222006-09-22 15:22:22 -07001602
1603 ehca_mrmw_map_acl(acl, &hipz_acl);
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001604 ehca_mrmw_set_pgsize_hipz_acl(e_origmr->hwpage_size, &hipz_acl);
Heiko J Schickfab97222006-09-22 15:22:22 -07001605
1606 h_ret = hipz_h_register_smr(shca->ipz_hca_handle, e_newmr, e_origmr,
1607 (u64)iova_start, hipz_acl, e_pd->fw_pd,
1608 &hipzout);
1609 if (h_ret != H_SUCCESS) {
Joachim Fenkese3722192007-09-11 15:32:22 +02001610 ehca_err(&shca->ib_device, "hipz_reg_smr failed, h_ret=%li "
Heiko J Schickfab97222006-09-22 15:22:22 -07001611 "e_origmr=%p hca_hndl=%lx mr_hndl=%lx lkey=%x",
1612 h_ret, e_origmr, shca->ipz_hca_handle.handle,
1613 e_origmr->ipz_mr_handle.handle,
1614 e_origmr->ib.ib_mr.lkey);
Hoang-Nam Nguyena1a6ff12007-07-12 17:49:02 +02001615 return ehca2ib_return_code(h_ret);
Heiko J Schickfab97222006-09-22 15:22:22 -07001616 }
1617 /* successful registration */
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001618 e_newmr->num_kpages = e_origmr->num_kpages;
1619 e_newmr->num_hwpages = e_origmr->num_hwpages;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001620 e_newmr->hwpage_size = e_origmr->hwpage_size;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001621 e_newmr->start = iova_start;
1622 e_newmr->size = e_origmr->size;
1623 e_newmr->acl = acl;
Heiko J Schickfab97222006-09-22 15:22:22 -07001624 e_newmr->ipz_mr_handle = hipzout.handle;
1625 *lkey = hipzout.lkey;
1626 *rkey = hipzout.rkey;
1627 return 0;
1628} /* end ehca_reg_maxmr() */
1629
1630/*----------------------------------------------------------------------*/
1631
1632int ehca_dereg_internal_maxmr(struct ehca_shca *shca)
1633{
1634 int ret;
1635 struct ehca_mr *e_maxmr;
1636 struct ib_pd *ib_pd;
1637
1638 if (!shca->maxmr) {
1639 ehca_err(&shca->ib_device, "bad call, shca=%p", shca);
1640 ret = -EINVAL;
1641 goto ehca_dereg_internal_maxmr_exit0;
1642 }
1643
1644 e_maxmr = shca->maxmr;
1645 ib_pd = e_maxmr->ib.ib_mr.pd;
1646 shca->maxmr = NULL; /* remove internal max-MR indication from SHCA */
1647
1648 ret = ehca_dereg_mr(&e_maxmr->ib.ib_mr);
1649 if (ret) {
1650 ehca_err(&shca->ib_device, "dereg internal max-MR failed, "
Joachim Fenkese3722192007-09-11 15:32:22 +02001651 "ret=%i e_maxmr=%p shca=%p lkey=%x",
Heiko J Schickfab97222006-09-22 15:22:22 -07001652 ret, e_maxmr, shca, e_maxmr->ib.ib_mr.lkey);
1653 shca->maxmr = e_maxmr;
1654 goto ehca_dereg_internal_maxmr_exit0;
1655 }
1656
1657 atomic_dec(&ib_pd->usecnt);
1658
1659ehca_dereg_internal_maxmr_exit0:
1660 if (ret)
Joachim Fenkese3722192007-09-11 15:32:22 +02001661 ehca_err(&shca->ib_device, "ret=%i shca=%p shca->maxmr=%p",
Heiko J Schickfab97222006-09-22 15:22:22 -07001662 ret, shca, shca->maxmr);
1663 return ret;
1664} /* end ehca_dereg_internal_maxmr() */
1665
1666/*----------------------------------------------------------------------*/
1667
1668/*
1669 * check physical buffer array of MR verbs for validness and
1670 * calculates MR size
1671 */
1672int ehca_mr_chk_buf_and_calc_size(struct ib_phys_buf *phys_buf_array,
1673 int num_phys_buf,
1674 u64 *iova_start,
1675 u64 *size)
1676{
1677 struct ib_phys_buf *pbuf = phys_buf_array;
1678 u64 size_count = 0;
1679 u32 i;
1680
1681 if (num_phys_buf == 0) {
1682 ehca_gen_err("bad phys buf array len, num_phys_buf=0");
1683 return -EINVAL;
1684 }
1685 /* check first buffer */
1686 if (((u64)iova_start & ~PAGE_MASK) != (pbuf->addr & ~PAGE_MASK)) {
1687 ehca_gen_err("iova_start/addr mismatch, iova_start=%p "
1688 "pbuf->addr=%lx pbuf->size=%lx",
1689 iova_start, pbuf->addr, pbuf->size);
1690 return -EINVAL;
1691 }
1692 if (((pbuf->addr + pbuf->size) % PAGE_SIZE) &&
1693 (num_phys_buf > 1)) {
1694 ehca_gen_err("addr/size mismatch in 1st buf, pbuf->addr=%lx "
1695 "pbuf->size=%lx", pbuf->addr, pbuf->size);
1696 return -EINVAL;
1697 }
1698
1699 for (i = 0; i < num_phys_buf; i++) {
1700 if ((i > 0) && (pbuf->addr % PAGE_SIZE)) {
1701 ehca_gen_err("bad address, i=%x pbuf->addr=%lx "
1702 "pbuf->size=%lx",
1703 i, pbuf->addr, pbuf->size);
1704 return -EINVAL;
1705 }
1706 if (((i > 0) && /* not 1st */
1707 (i < (num_phys_buf - 1)) && /* not last */
1708 (pbuf->size % PAGE_SIZE)) || (pbuf->size == 0)) {
1709 ehca_gen_err("bad size, i=%x pbuf->size=%lx",
1710 i, pbuf->size);
1711 return -EINVAL;
1712 }
1713 size_count += pbuf->size;
1714 pbuf++;
1715 }
1716
1717 *size = size_count;
1718 return 0;
1719} /* end ehca_mr_chk_buf_and_calc_size() */
1720
1721/*----------------------------------------------------------------------*/
1722
1723/* check page list of map FMR verb for validness */
1724int ehca_fmr_check_page_list(struct ehca_mr *e_fmr,
1725 u64 *page_list,
1726 int list_len)
1727{
1728 u32 i;
1729 u64 *page;
1730
1731 if ((list_len == 0) || (list_len > e_fmr->fmr_max_pages)) {
1732 ehca_gen_err("bad list_len, list_len=%x "
1733 "e_fmr->fmr_max_pages=%x fmr=%p",
1734 list_len, e_fmr->fmr_max_pages, e_fmr);
1735 return -EINVAL;
1736 }
1737
1738 /* each page must be aligned */
1739 page = page_list;
1740 for (i = 0; i < list_len; i++) {
1741 if (*page % e_fmr->fmr_page_size) {
1742 ehca_gen_err("bad page, i=%x *page=%lx page=%p fmr=%p "
1743 "fmr_page_size=%x", i, *page, page, e_fmr,
1744 e_fmr->fmr_page_size);
1745 return -EINVAL;
1746 }
1747 page++;
1748 }
1749
1750 return 0;
1751} /* end ehca_fmr_check_page_list() */
1752
1753/*----------------------------------------------------------------------*/
1754
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001755/* PAGE_SIZE >= pginfo->hwpage_size */
1756static int ehca_set_pagebuf_user1(struct ehca_mr_pginfo *pginfo,
1757 u32 number,
1758 u64 *kpage)
Heiko J Schickfab97222006-09-22 15:22:22 -07001759{
1760 int ret = 0;
1761 struct ib_umem_chunk *prev_chunk;
1762 struct ib_umem_chunk *chunk;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001763 u64 pgaddr;
Heiko J Schickfab97222006-09-22 15:22:22 -07001764 u32 i = 0;
1765 u32 j = 0;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001766 int hwpages_per_kpage = PAGE_SIZE / pginfo->hwpage_size;
Heiko J Schickfab97222006-09-22 15:22:22 -07001767
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001768 /* loop over desired chunk entries */
1769 chunk = pginfo->u.usr.next_chunk;
1770 prev_chunk = pginfo->u.usr.next_chunk;
1771 list_for_each_entry_continue(
1772 chunk, (&(pginfo->u.usr.region->chunk_list)), list) {
1773 for (i = pginfo->u.usr.next_nmap; i < chunk->nmap; ) {
1774 pgaddr = page_to_pfn(chunk->page_list[i].page)
1775 << PAGE_SHIFT ;
1776 *kpage = phys_to_abs(pgaddr +
1777 (pginfo->next_hwpage *
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001778 pginfo->hwpage_size));
Heiko J Schickfab97222006-09-22 15:22:22 -07001779 if ( !(*kpage) ) {
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001780 ehca_gen_err("pgaddr=%lx "
1781 "chunk->page_list[i]=%lx "
1782 "i=%x next_hwpage=%lx",
1783 pgaddr, (u64)sg_dma_address(
1784 &chunk->page_list[i]),
1785 i, pginfo->next_hwpage);
1786 return -EFAULT;
Heiko J Schickfab97222006-09-22 15:22:22 -07001787 }
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001788 (pginfo->hwpage_cnt)++;
1789 (pginfo->next_hwpage)++;
Heiko J Schickfab97222006-09-22 15:22:22 -07001790 kpage++;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001791 if (pginfo->next_hwpage % hwpages_per_kpage == 0) {
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02001792 (pginfo->kpage_cnt)++;
1793 (pginfo->u.usr.next_nmap)++;
1794 pginfo->next_hwpage = 0;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001795 i++;
Heiko J Schickfab97222006-09-22 15:22:22 -07001796 }
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001797 j++;
1798 if (j >= number) break;
Heiko J Schickfab97222006-09-22 15:22:22 -07001799 }
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001800 if ((pginfo->u.usr.next_nmap >= chunk->nmap) &&
1801 (j >= number)) {
1802 pginfo->u.usr.next_nmap = 0;
1803 prev_chunk = chunk;
1804 break;
1805 } else if (pginfo->u.usr.next_nmap >= chunk->nmap) {
1806 pginfo->u.usr.next_nmap = 0;
1807 prev_chunk = chunk;
1808 } else if (j >= number)
1809 break;
1810 else
1811 prev_chunk = chunk;
1812 }
1813 pginfo->u.usr.next_chunk =
1814 list_prepare_entry(prev_chunk,
1815 (&(pginfo->u.usr.region->chunk_list)),
1816 list);
1817 return ret;
1818}
1819
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001820/*
1821 * check given pages for contiguous layout
1822 * last page addr is returned in prev_pgaddr for further check
1823 */
1824static int ehca_check_kpages_per_ate(struct scatterlist *page_list,
1825 int start_idx, int end_idx,
1826 u64 *prev_pgaddr)
1827{
1828 int t;
1829 for (t = start_idx; t <= end_idx; t++) {
1830 u64 pgaddr = page_to_pfn(page_list[t].page) << PAGE_SHIFT;
1831 ehca_gen_dbg("chunk_page=%lx value=%016lx", pgaddr,
1832 *(u64 *)abs_to_virt(phys_to_abs(pgaddr)));
1833 if (pgaddr - PAGE_SIZE != *prev_pgaddr) {
1834 ehca_gen_err("uncontiguous page found pgaddr=%lx "
1835 "prev_pgaddr=%lx page_list_i=%x",
1836 pgaddr, *prev_pgaddr, t);
1837 return -EINVAL;
1838 }
1839 *prev_pgaddr = pgaddr;
1840 }
1841 return 0;
1842}
1843
1844/* PAGE_SIZE < pginfo->hwpage_size */
1845static int ehca_set_pagebuf_user2(struct ehca_mr_pginfo *pginfo,
1846 u32 number,
1847 u64 *kpage)
1848{
1849 int ret = 0;
1850 struct ib_umem_chunk *prev_chunk;
1851 struct ib_umem_chunk *chunk;
1852 u64 pgaddr, prev_pgaddr;
1853 u32 i = 0;
1854 u32 j = 0;
1855 int kpages_per_hwpage = pginfo->hwpage_size / PAGE_SIZE;
1856 int nr_kpages = kpages_per_hwpage;
1857
1858 /* loop over desired chunk entries */
1859 chunk = pginfo->u.usr.next_chunk;
1860 prev_chunk = pginfo->u.usr.next_chunk;
1861 list_for_each_entry_continue(
1862 chunk, (&(pginfo->u.usr.region->chunk_list)), list) {
1863 for (i = pginfo->u.usr.next_nmap; i < chunk->nmap; ) {
1864 if (nr_kpages == kpages_per_hwpage) {
1865 pgaddr = ( page_to_pfn(chunk->page_list[i].page)
1866 << PAGE_SHIFT );
1867 *kpage = phys_to_abs(pgaddr);
1868 if ( !(*kpage) ) {
1869 ehca_gen_err("pgaddr=%lx i=%x",
1870 pgaddr, i);
1871 ret = -EFAULT;
1872 return ret;
1873 }
1874 /*
1875 * The first page in a hwpage must be aligned;
1876 * the first MR page is exempt from this rule.
1877 */
1878 if (pgaddr & (pginfo->hwpage_size - 1)) {
1879 if (pginfo->hwpage_cnt) {
1880 ehca_gen_err(
1881 "invalid alignment "
1882 "pgaddr=%lx i=%x "
1883 "mr_pgsize=%lx",
1884 pgaddr, i,
1885 pginfo->hwpage_size);
1886 ret = -EFAULT;
1887 return ret;
1888 }
1889 /* first MR page */
1890 pginfo->kpage_cnt =
1891 (pgaddr &
1892 (pginfo->hwpage_size - 1)) >>
1893 PAGE_SHIFT;
1894 nr_kpages -= pginfo->kpage_cnt;
1895 *kpage = phys_to_abs(
1896 pgaddr &
1897 ~(pginfo->hwpage_size - 1));
1898 }
1899 ehca_gen_dbg("kpage=%lx chunk_page=%lx "
1900 "value=%016lx", *kpage, pgaddr,
1901 *(u64 *)abs_to_virt(
1902 phys_to_abs(pgaddr)));
1903 prev_pgaddr = pgaddr;
1904 i++;
1905 pginfo->kpage_cnt++;
1906 pginfo->u.usr.next_nmap++;
1907 nr_kpages--;
1908 if (!nr_kpages)
1909 goto next_kpage;
1910 continue;
1911 }
1912 if (i + nr_kpages > chunk->nmap) {
1913 ret = ehca_check_kpages_per_ate(
1914 chunk->page_list, i,
1915 chunk->nmap - 1, &prev_pgaddr);
1916 if (ret) return ret;
1917 pginfo->kpage_cnt += chunk->nmap - i;
1918 pginfo->u.usr.next_nmap += chunk->nmap - i;
1919 nr_kpages -= chunk->nmap - i;
1920 break;
1921 }
1922
1923 ret = ehca_check_kpages_per_ate(chunk->page_list, i,
1924 i + nr_kpages - 1,
1925 &prev_pgaddr);
1926 if (ret) return ret;
1927 i += nr_kpages;
1928 pginfo->kpage_cnt += nr_kpages;
1929 pginfo->u.usr.next_nmap += nr_kpages;
1930next_kpage:
1931 nr_kpages = kpages_per_hwpage;
1932 (pginfo->hwpage_cnt)++;
1933 kpage++;
1934 j++;
1935 if (j >= number) break;
1936 }
1937 if ((pginfo->u.usr.next_nmap >= chunk->nmap) &&
1938 (j >= number)) {
1939 pginfo->u.usr.next_nmap = 0;
1940 prev_chunk = chunk;
1941 break;
1942 } else if (pginfo->u.usr.next_nmap >= chunk->nmap) {
1943 pginfo->u.usr.next_nmap = 0;
1944 prev_chunk = chunk;
1945 } else if (j >= number)
1946 break;
1947 else
1948 prev_chunk = chunk;
1949 }
1950 pginfo->u.usr.next_chunk =
1951 list_prepare_entry(prev_chunk,
1952 (&(pginfo->u.usr.region->chunk_list)),
1953 list);
1954 return ret;
1955}
1956
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001957int ehca_set_pagebuf_phys(struct ehca_mr_pginfo *pginfo,
1958 u32 number,
1959 u64 *kpage)
1960{
1961 int ret = 0;
1962 struct ib_phys_buf *pbuf;
1963 u64 num_hw, offs_hw;
1964 u32 i = 0;
1965
1966 /* loop over desired phys_buf_array entries */
1967 while (i < number) {
1968 pbuf = pginfo->u.phy.phys_buf_array + pginfo->u.phy.next_buf;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001969 num_hw = NUM_CHUNKS((pbuf->addr % pginfo->hwpage_size) +
1970 pbuf->size, pginfo->hwpage_size);
1971 offs_hw = (pbuf->addr & ~(pginfo->hwpage_size - 1)) /
1972 pginfo->hwpage_size;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001973 while (pginfo->next_hwpage < offs_hw + num_hw) {
1974 /* sanity check */
1975 if ((pginfo->kpage_cnt >= pginfo->num_kpages) ||
1976 (pginfo->hwpage_cnt >= pginfo->num_hwpages)) {
1977 ehca_gen_err("kpage_cnt >= num_kpages, "
1978 "kpage_cnt=%lx num_kpages=%lx "
1979 "hwpage_cnt=%lx "
1980 "num_hwpages=%lx i=%x",
1981 pginfo->kpage_cnt,
1982 pginfo->num_kpages,
1983 pginfo->hwpage_cnt,
1984 pginfo->num_hwpages, i);
1985 return -EFAULT;
1986 }
1987 *kpage = phys_to_abs(
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001988 (pbuf->addr & ~(pginfo->hwpage_size - 1)) +
1989 (pginfo->next_hwpage * pginfo->hwpage_size));
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001990 if ( !(*kpage) && pbuf->addr ) {
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001991 ehca_gen_err("pbuf->addr=%lx pbuf->size=%lx "
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001992 "next_hwpage=%lx", pbuf->addr,
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001993 pbuf->size, pginfo->next_hwpage);
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02001994 return -EFAULT;
1995 }
1996 (pginfo->hwpage_cnt)++;
1997 (pginfo->next_hwpage)++;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02001998 if (PAGE_SIZE >= pginfo->hwpage_size) {
1999 if (pginfo->next_hwpage %
2000 (PAGE_SIZE / pginfo->hwpage_size) == 0)
2001 (pginfo->kpage_cnt)++;
2002 } else
2003 pginfo->kpage_cnt += pginfo->hwpage_size /
2004 PAGE_SIZE;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02002005 kpage++;
2006 i++;
2007 if (i >= number) break;
2008 }
2009 if (pginfo->next_hwpage >= offs_hw + num_hw) {
2010 (pginfo->u.phy.next_buf)++;
2011 pginfo->next_hwpage = 0;
2012 }
2013 }
2014 return ret;
2015}
2016
2017int ehca_set_pagebuf_fmr(struct ehca_mr_pginfo *pginfo,
2018 u32 number,
2019 u64 *kpage)
2020{
2021 int ret = 0;
2022 u64 *fmrlist;
2023 u32 i;
2024
2025 /* loop over desired page_list entries */
2026 fmrlist = pginfo->u.fmr.page_list + pginfo->u.fmr.next_listelem;
2027 for (i = 0; i < number; i++) {
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02002028 *kpage = phys_to_abs((*fmrlist & ~(pginfo->hwpage_size - 1)) +
2029 pginfo->next_hwpage * pginfo->hwpage_size);
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02002030 if ( !(*kpage) ) {
Heiko J Schickfab97222006-09-22 15:22:22 -07002031 ehca_gen_err("*fmrlist=%lx fmrlist=%p "
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02002032 "next_listelem=%lx next_hwpage=%lx",
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02002033 *fmrlist, fmrlist,
2034 pginfo->u.fmr.next_listelem,
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02002035 pginfo->next_hwpage);
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02002036 return -EFAULT;
Heiko J Schickfab97222006-09-22 15:22:22 -07002037 }
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02002038 (pginfo->hwpage_cnt)++;
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02002039 if (pginfo->u.fmr.fmr_pgsize >= pginfo->hwpage_size) {
2040 if (pginfo->next_hwpage %
2041 (pginfo->u.fmr.fmr_pgsize /
2042 pginfo->hwpage_size) == 0) {
2043 (pginfo->kpage_cnt)++;
2044 (pginfo->u.fmr.next_listelem)++;
2045 fmrlist++;
2046 pginfo->next_hwpage = 0;
2047 } else
2048 (pginfo->next_hwpage)++;
2049 } else {
2050 unsigned int cnt_per_hwpage = pginfo->hwpage_size /
2051 pginfo->u.fmr.fmr_pgsize;
2052 unsigned int j;
2053 u64 prev = *kpage;
2054 /* check if adrs are contiguous */
2055 for (j = 1; j < cnt_per_hwpage; j++) {
2056 u64 p = phys_to_abs(fmrlist[j] &
2057 ~(pginfo->hwpage_size - 1));
2058 if (prev + pginfo->u.fmr.fmr_pgsize != p) {
2059 ehca_gen_err("uncontiguous fmr pages "
2060 "found prev=%lx p=%lx "
2061 "idx=%x", prev, p, i + j);
2062 return -EINVAL;
2063 }
2064 prev = p;
2065 }
2066 pginfo->kpage_cnt += cnt_per_hwpage;
2067 pginfo->u.fmr.next_listelem += cnt_per_hwpage;
2068 fmrlist += cnt_per_hwpage;
Heiko J Schickfab97222006-09-22 15:22:22 -07002069 }
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02002070 kpage++;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02002071 }
2072 return ret;
2073}
2074
2075/* setup page buffer from page info */
2076int ehca_set_pagebuf(struct ehca_mr_pginfo *pginfo,
2077 u32 number,
2078 u64 *kpage)
2079{
2080 int ret;
2081
2082 switch (pginfo->type) {
2083 case EHCA_MR_PGI_PHYS:
2084 ret = ehca_set_pagebuf_phys(pginfo, number, kpage);
2085 break;
2086 case EHCA_MR_PGI_USER:
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02002087 ret = PAGE_SIZE >= pginfo->hwpage_size ?
2088 ehca_set_pagebuf_user1(pginfo, number, kpage) :
2089 ehca_set_pagebuf_user2(pginfo, number, kpage);
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02002090 break;
2091 case EHCA_MR_PGI_FMR:
2092 ret = ehca_set_pagebuf_fmr(pginfo, number, kpage);
2093 break;
2094 default:
Heiko J Schickfab97222006-09-22 15:22:22 -07002095 ehca_gen_err("bad pginfo->type=%x", pginfo->type);
2096 ret = -EFAULT;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02002097 break;
Heiko J Schickfab97222006-09-22 15:22:22 -07002098 }
Heiko J Schickfab97222006-09-22 15:22:22 -07002099 return ret;
Hoang-Nam Nguyen187c72e2007-07-12 17:53:11 +02002100} /* end ehca_set_pagebuf() */
Heiko J Schickfab97222006-09-22 15:22:22 -07002101
2102/*----------------------------------------------------------------------*/
2103
2104/*
2105 * check MR if it is a max-MR, i.e. uses whole memory
2106 * in case it's a max-MR 1 is returned, else 0
2107 */
2108int ehca_mr_is_maxmr(u64 size,
2109 u64 *iova_start)
2110{
2111 /* a MR is treated as max-MR only if it fits following: */
2112 if ((size == ((u64)high_memory - PAGE_OFFSET)) &&
Hoang-Nam Nguyen2b943972007-07-12 17:53:47 +02002113 (iova_start == (void *)KERNELBASE)) {
Heiko J Schickfab97222006-09-22 15:22:22 -07002114 ehca_gen_dbg("this is a max-MR");
2115 return 1;
2116 } else
2117 return 0;
2118} /* end ehca_mr_is_maxmr() */
2119
2120/*----------------------------------------------------------------------*/
2121
2122/* map access control for MR/MW. This routine is used for MR and MW. */
2123void ehca_mrmw_map_acl(int ib_acl,
2124 u32 *hipz_acl)
2125{
2126 *hipz_acl = 0;
2127 if (ib_acl & IB_ACCESS_REMOTE_READ)
2128 *hipz_acl |= HIPZ_ACCESSCTRL_R_READ;
2129 if (ib_acl & IB_ACCESS_REMOTE_WRITE)
2130 *hipz_acl |= HIPZ_ACCESSCTRL_R_WRITE;
2131 if (ib_acl & IB_ACCESS_REMOTE_ATOMIC)
2132 *hipz_acl |= HIPZ_ACCESSCTRL_R_ATOMIC;
2133 if (ib_acl & IB_ACCESS_LOCAL_WRITE)
2134 *hipz_acl |= HIPZ_ACCESSCTRL_L_WRITE;
2135 if (ib_acl & IB_ACCESS_MW_BIND)
2136 *hipz_acl |= HIPZ_ACCESSCTRL_MW_BIND;
2137} /* end ehca_mrmw_map_acl() */
2138
2139/*----------------------------------------------------------------------*/
2140
2141/* sets page size in hipz access control for MR/MW. */
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02002142void ehca_mrmw_set_pgsize_hipz_acl(u32 pgsize, u32 *hipz_acl) /*INOUT*/
Heiko J Schickfab97222006-09-22 15:22:22 -07002143{
Hoang-Nam Nguyen5bb7d922007-07-20 16:01:51 +02002144 *hipz_acl |= (ehca_encode_hwpage_size(pgsize) << 24);
Heiko J Schickfab97222006-09-22 15:22:22 -07002145} /* end ehca_mrmw_set_pgsize_hipz_acl() */
2146
2147/*----------------------------------------------------------------------*/
2148
2149/*
2150 * reverse map access control for MR/MW.
2151 * This routine is used for MR and MW.
2152 */
2153void ehca_mrmw_reverse_map_acl(const u32 *hipz_acl,
2154 int *ib_acl) /*OUT*/
2155{
2156 *ib_acl = 0;
2157 if (*hipz_acl & HIPZ_ACCESSCTRL_R_READ)
2158 *ib_acl |= IB_ACCESS_REMOTE_READ;
2159 if (*hipz_acl & HIPZ_ACCESSCTRL_R_WRITE)
2160 *ib_acl |= IB_ACCESS_REMOTE_WRITE;
2161 if (*hipz_acl & HIPZ_ACCESSCTRL_R_ATOMIC)
2162 *ib_acl |= IB_ACCESS_REMOTE_ATOMIC;
2163 if (*hipz_acl & HIPZ_ACCESSCTRL_L_WRITE)
2164 *ib_acl |= IB_ACCESS_LOCAL_WRITE;
2165 if (*hipz_acl & HIPZ_ACCESSCTRL_MW_BIND)
2166 *ib_acl |= IB_ACCESS_MW_BIND;
2167} /* end ehca_mrmw_reverse_map_acl() */
2168
2169
2170/*----------------------------------------------------------------------*/
2171
2172/*
Heiko J Schickfab97222006-09-22 15:22:22 -07002173 * MR destructor and constructor
2174 * used in Reregister MR verb, sets all fields in ehca_mr_t to 0,
2175 * except struct ib_mr and spinlock
2176 */
2177void ehca_mr_deletenew(struct ehca_mr *mr)
2178{
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02002179 mr->flags = 0;
2180 mr->num_kpages = 0;
2181 mr->num_hwpages = 0;
2182 mr->acl = 0;
2183 mr->start = NULL;
Heiko J Schickfab97222006-09-22 15:22:22 -07002184 mr->fmr_page_size = 0;
2185 mr->fmr_max_pages = 0;
Hoang-Nam Nguyendf17bfd2007-07-12 17:52:29 +02002186 mr->fmr_max_maps = 0;
2187 mr->fmr_map_cnt = 0;
Heiko J Schickfab97222006-09-22 15:22:22 -07002188 memset(&mr->ipz_mr_handle, 0, sizeof(mr->ipz_mr_handle));
2189 memset(&mr->galpas, 0, sizeof(mr->galpas));
Heiko J Schickfab97222006-09-22 15:22:22 -07002190} /* end ehca_mr_deletenew() */
2191
2192int ehca_init_mrmw_cache(void)
2193{
2194 mr_cache = kmem_cache_create("ehca_cache_mr",
2195 sizeof(struct ehca_mr), 0,
2196 SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002197 NULL);
Heiko J Schickfab97222006-09-22 15:22:22 -07002198 if (!mr_cache)
2199 return -ENOMEM;
2200 mw_cache = kmem_cache_create("ehca_cache_mw",
2201 sizeof(struct ehca_mw), 0,
2202 SLAB_HWCACHE_ALIGN,
Paul Mundt20c2df82007-07-20 10:11:58 +09002203 NULL);
Heiko J Schickfab97222006-09-22 15:22:22 -07002204 if (!mw_cache) {
2205 kmem_cache_destroy(mr_cache);
2206 mr_cache = NULL;
2207 return -ENOMEM;
2208 }
2209 return 0;
2210}
2211
2212void ehca_cleanup_mrmw_cache(void)
2213{
2214 if (mr_cache)
2215 kmem_cache_destroy(mr_cache);
2216 if (mw_cache)
2217 kmem_cache_destroy(mw_cache);
2218}