blob: db4ba92f79fcd60736a8bd3d1e1752caa0ab3f5d [file] [log] [blame]
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08001/*
John Gregor87427da2007-06-11 10:21:14 -07002 * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08003 * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
4 *
5 * This software is available to you under a choice of one of two
6 * licenses. You may choose to be licensed under the terms of the GNU
7 * General Public License (GPL) Version 2, available from the file
8 * COPYING in the main directory of this source tree, or the
9 * OpenIB.org BSD license below:
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 *
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
17 * disclaimer.
18 *
19 * - Redistributions in binary form must reproduce the above
20 * copyright notice, this list of conditions and the following
21 * disclaimer in the documentation and/or other materials
22 * provided with the distribution.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 * SOFTWARE.
32 */
33
Roland Dreierf7c6a7b2007-03-04 16:15:11 -080034#include <rdma/ib_umem.h>
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080035#include <rdma/ib_pack.h>
36#include <rdma/ib_smi.h>
37
38#include "ipath_verbs.h"
39
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -070040/* Fast memory region */
41struct ipath_fmr {
42 struct ib_fmr ibfmr;
43 u8 page_shift;
44 struct ipath_mregion mr; /* must be last */
45};
46
47static inline struct ipath_fmr *to_ifmr(struct ib_fmr *ibfmr)
48{
49 return container_of(ibfmr, struct ipath_fmr, ibfmr);
50}
51
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080052/**
53 * ipath_get_dma_mr - get a DMA memory region
54 * @pd: protection domain for this memory region
55 * @acc: access flags
56 *
57 * Returns the memory region on success, otherwise returns an errno.
Ralph Campbellf2cbb662006-12-12 14:28:28 -080058 * Note that all DMA addresses should be created via the
59 * struct ib_dma_mapping_ops functions (see ipath_dma.c).
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080060 */
61struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)
62{
63 struct ipath_mr *mr;
64 struct ib_mr *ret;
65
66 mr = kzalloc(sizeof *mr, GFP_KERNEL);
67 if (!mr) {
68 ret = ERR_PTR(-ENOMEM);
69 goto bail;
70 }
71
72 mr->mr.access_flags = acc;
73 ret = &mr->ibmr;
74
75bail:
76 return ret;
77}
78
79static struct ipath_mr *alloc_mr(int count,
80 struct ipath_lkey_table *lk_table)
81{
82 struct ipath_mr *mr;
83 int m, i = 0;
84
85 /* Allocate struct plus pointers to first level page tables. */
86 m = (count + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
87 mr = kmalloc(sizeof *mr + m * sizeof mr->mr.map[0], GFP_KERNEL);
88 if (!mr)
89 goto done;
90
91 /* Allocate first level page tables. */
92 for (; i < m; i++) {
93 mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL);
94 if (!mr->mr.map[i])
95 goto bail;
96 }
97 mr->mr.mapsz = m;
98
99 /*
100 * ib_reg_phys_mr() will initialize mr->ibmr except for
101 * lkey and rkey.
102 */
103 if (!ipath_alloc_lkey(lk_table, &mr->mr))
104 goto bail;
105 mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey;
106
107 goto done;
108
109bail:
110 while (i) {
111 i--;
112 kfree(mr->mr.map[i]);
113 }
114 kfree(mr);
115 mr = NULL;
116
117done:
118 return mr;
119}
120
121/**
122 * ipath_reg_phys_mr - register a physical memory region
123 * @pd: protection domain for this memory region
124 * @buffer_list: pointer to the list of physical buffers to register
125 * @num_phys_buf: the number of physical buffers to register
126 * @iova_start: the starting address passed over IB which maps to this MR
127 *
128 * Returns the memory region on success, otherwise returns an errno.
129 */
130struct ib_mr *ipath_reg_phys_mr(struct ib_pd *pd,
131 struct ib_phys_buf *buffer_list,
132 int num_phys_buf, int acc, u64 *iova_start)
133{
134 struct ipath_mr *mr;
135 int n, m, i;
136 struct ib_mr *ret;
137
138 mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table);
139 if (mr == NULL) {
140 ret = ERR_PTR(-ENOMEM);
141 goto bail;
142 }
143
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700144 mr->mr.pd = pd;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800145 mr->mr.user_base = *iova_start;
146 mr->mr.iova = *iova_start;
147 mr->mr.length = 0;
148 mr->mr.offset = 0;
149 mr->mr.access_flags = acc;
150 mr->mr.max_segs = num_phys_buf;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800151 mr->umem = NULL;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800152
153 m = 0;
154 n = 0;
155 for (i = 0; i < num_phys_buf; i++) {
Ralph Campbellf2cbb662006-12-12 14:28:28 -0800156 mr->mr.map[m]->segs[n].vaddr = (void *) buffer_list[i].addr;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800157 mr->mr.map[m]->segs[n].length = buffer_list[i].size;
158 mr->mr.length += buffer_list[i].size;
159 n++;
160 if (n == IPATH_SEGSZ) {
161 m++;
162 n = 0;
163 }
164 }
165
166 ret = &mr->ibmr;
167
168bail:
169 return ret;
170}
171
172/**
173 * ipath_reg_user_mr - register a userspace memory region
174 * @pd: protection domain for this memory region
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800175 * @start: starting userspace address
176 * @length: length of region to register
177 * @virt_addr: virtual address to use (from HCA's point of view)
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800178 * @mr_access_flags: access flags for this memory region
179 * @udata: unused by the InfiniPath driver
180 *
181 * Returns the memory region on success, otherwise returns an errno.
182 */
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800183struct ib_mr *ipath_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
184 u64 virt_addr, int mr_access_flags,
185 struct ib_udata *udata)
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800186{
187 struct ipath_mr *mr;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800188 struct ib_umem *umem;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800189 struct ib_umem_chunk *chunk;
190 int n, m, i;
191 struct ib_mr *ret;
192
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800193 if (length == 0) {
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -0700194 ret = ERR_PTR(-EINVAL);
195 goto bail;
196 }
197
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800198 umem = ib_umem_get(pd->uobject->context, start, length, mr_access_flags);
199 if (IS_ERR(umem))
200 return (void *) umem;
201
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800202 n = 0;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800203 list_for_each_entry(chunk, &umem->chunk_list, list)
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800204 n += chunk->nents;
205
206 mr = alloc_mr(n, &to_idev(pd->device)->lk_table);
207 if (!mr) {
208 ret = ERR_PTR(-ENOMEM);
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800209 ib_umem_release(umem);
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800210 goto bail;
211 }
212
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700213 mr->mr.pd = pd;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800214 mr->mr.user_base = start;
215 mr->mr.iova = virt_addr;
216 mr->mr.length = length;
217 mr->mr.offset = umem->offset;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800218 mr->mr.access_flags = mr_access_flags;
219 mr->mr.max_segs = n;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800220 mr->umem = umem;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800221
222 m = 0;
223 n = 0;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800224 list_for_each_entry(chunk, &umem->chunk_list, list) {
Bryan O'Sullivan165c5522007-03-15 14:44:46 -0700225 for (i = 0; i < chunk->nents; i++) {
226 void *vaddr;
227
Jens Axboe45711f12007-10-22 21:19:53 +0200228 vaddr = page_address(sg_page(&chunk->page_list[i]));
Bryan O'Sullivan165c5522007-03-15 14:44:46 -0700229 if (!vaddr) {
230 ret = ERR_PTR(-EINVAL);
231 goto bail;
232 }
233 mr->mr.map[m]->segs[n].vaddr = vaddr;
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800234 mr->mr.map[m]->segs[n].length = umem->page_size;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800235 n++;
236 if (n == IPATH_SEGSZ) {
237 m++;
238 n = 0;
239 }
240 }
241 }
242 ret = &mr->ibmr;
243
244bail:
245 return ret;
246}
247
248/**
249 * ipath_dereg_mr - unregister and free a memory region
250 * @ibmr: the memory region to free
251 *
252 * Returns 0 on success.
253 *
254 * Note that this is called to free MRs created by ipath_get_dma_mr()
255 * or ipath_reg_user_mr().
256 */
257int ipath_dereg_mr(struct ib_mr *ibmr)
258{
259 struct ipath_mr *mr = to_imr(ibmr);
260 int i;
261
262 ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);
263 i = mr->mr.mapsz;
264 while (i) {
265 i--;
266 kfree(mr->mr.map[i]);
267 }
Roland Dreierf7c6a7b2007-03-04 16:15:11 -0800268
269 if (mr->umem)
270 ib_umem_release(mr->umem);
271
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800272 kfree(mr);
273 return 0;
274}
275
276/**
277 * ipath_alloc_fmr - allocate a fast memory region
278 * @pd: the protection domain for this memory region
279 * @mr_access_flags: access flags for this memory region
280 * @fmr_attr: fast memory region attributes
281 *
282 * Returns the memory region on success, otherwise returns an errno.
283 */
284struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
285 struct ib_fmr_attr *fmr_attr)
286{
287 struct ipath_fmr *fmr;
288 int m, i = 0;
289 struct ib_fmr *ret;
290
291 /* Allocate struct plus pointers to first level page tables. */
292 m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
293 fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL);
294 if (!fmr)
295 goto bail;
296
297 /* Allocate first level page tables. */
298 for (; i < m; i++) {
299 fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],
300 GFP_KERNEL);
301 if (!fmr->mr.map[i])
302 goto bail;
303 }
304 fmr->mr.mapsz = m;
305
306 /*
307 * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
308 * rkey.
309 */
310 if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))
311 goto bail;
312 fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;
313 /*
314 * Resources are allocated but no valid mapping (RKEY can't be
315 * used).
316 */
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700317 fmr->mr.pd = pd;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800318 fmr->mr.user_base = 0;
319 fmr->mr.iova = 0;
320 fmr->mr.length = 0;
321 fmr->mr.offset = 0;
322 fmr->mr.access_flags = mr_access_flags;
323 fmr->mr.max_segs = fmr_attr->max_pages;
324 fmr->page_shift = fmr_attr->page_shift;
325
326 ret = &fmr->ibfmr;
327 goto done;
328
329bail:
330 while (i)
331 kfree(fmr->mr.map[--i]);
332 kfree(fmr);
333 ret = ERR_PTR(-ENOMEM);
334
335done:
336 return ret;
337}
338
339/**
340 * ipath_map_phys_fmr - set up a fast memory region
341 * @ibmfr: the fast memory region to set up
342 * @page_list: the list of pages to associate with the fast memory region
343 * @list_len: the number of pages to associate with the fast memory region
344 * @iova: the virtual address of the start of the fast memory region
345 *
346 * This may be called from interrupt context.
347 */
348
349int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,
350 int list_len, u64 iova)
351{
352 struct ipath_fmr *fmr = to_ifmr(ibfmr);
353 struct ipath_lkey_table *rkt;
354 unsigned long flags;
355 int m, n, i;
356 u32 ps;
357 int ret;
358
359 if (list_len > fmr->mr.max_segs) {
360 ret = -EINVAL;
361 goto bail;
362 }
363 rkt = &to_idev(ibfmr->device)->lk_table;
364 spin_lock_irqsave(&rkt->lock, flags);
365 fmr->mr.user_base = iova;
366 fmr->mr.iova = iova;
367 ps = 1 << fmr->page_shift;
368 fmr->mr.length = list_len * ps;
369 m = 0;
370 n = 0;
371 ps = 1 << fmr->page_shift;
372 for (i = 0; i < list_len; i++) {
Ralph Campbellf2cbb662006-12-12 14:28:28 -0800373 fmr->mr.map[m]->segs[n].vaddr = (void *) page_list[i];
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800374 fmr->mr.map[m]->segs[n].length = ps;
375 if (++n == IPATH_SEGSZ) {
376 m++;
377 n = 0;
378 }
379 }
380 spin_unlock_irqrestore(&rkt->lock, flags);
381 ret = 0;
382
383bail:
384 return ret;
385}
386
387/**
388 * ipath_unmap_fmr - unmap fast memory regions
389 * @fmr_list: the list of fast memory regions to unmap
390 *
391 * Returns 0 on success.
392 */
393int ipath_unmap_fmr(struct list_head *fmr_list)
394{
395 struct ipath_fmr *fmr;
396 struct ipath_lkey_table *rkt;
397 unsigned long flags;
398
399 list_for_each_entry(fmr, fmr_list, ibfmr.list) {
400 rkt = &to_idev(fmr->ibfmr.device)->lk_table;
401 spin_lock_irqsave(&rkt->lock, flags);
402 fmr->mr.user_base = 0;
403 fmr->mr.iova = 0;
404 fmr->mr.length = 0;
405 spin_unlock_irqrestore(&rkt->lock, flags);
406 }
407 return 0;
408}
409
410/**
411 * ipath_dealloc_fmr - deallocate a fast memory region
412 * @ibfmr: the fast memory region to deallocate
413 *
414 * Returns 0 on success.
415 */
416int ipath_dealloc_fmr(struct ib_fmr *ibfmr)
417{
418 struct ipath_fmr *fmr = to_ifmr(ibfmr);
419 int i;
420
421 ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);
422 i = fmr->mr.mapsz;
423 while (i)
424 kfree(fmr->mr.map[--i]);
425 kfree(fmr);
426 return 0;
427}