blob: 31e70732e36950bc4d765a74779c6a86ded4cf3a [file] [log] [blame]
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -08001/*
Bryan O'Sullivan759d5762006-07-01 04:35:49 -07002 * Copyright (c) 2006 QLogic, Inc. 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
34#include <rdma/ib_pack.h>
35#include <rdma/ib_smi.h>
36
37#include "ipath_verbs.h"
38
Bryan O'Sullivan34b2aaf2006-08-25 11:24:32 -070039/* Fast memory region */
40struct ipath_fmr {
41 struct ib_fmr ibfmr;
42 u8 page_shift;
43 struct ipath_mregion mr; /* must be last */
44};
45
46static inline struct ipath_fmr *to_ifmr(struct ib_fmr *ibfmr)
47{
48 return container_of(ibfmr, struct ipath_fmr, ibfmr);
49}
50
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080051/**
52 * ipath_get_dma_mr - get a DMA memory region
53 * @pd: protection domain for this memory region
54 * @acc: access flags
55 *
56 * Returns the memory region on success, otherwise returns an errno.
Ralph Campbellf2cbb662006-12-12 14:28:28 -080057 * Note that all DMA addresses should be created via the
58 * struct ib_dma_mapping_ops functions (see ipath_dma.c).
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -080059 */
60struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)
61{
62 struct ipath_mr *mr;
63 struct ib_mr *ret;
64
65 mr = kzalloc(sizeof *mr, GFP_KERNEL);
66 if (!mr) {
67 ret = ERR_PTR(-ENOMEM);
68 goto bail;
69 }
70
71 mr->mr.access_flags = acc;
72 ret = &mr->ibmr;
73
74bail:
75 return ret;
76}
77
78static struct ipath_mr *alloc_mr(int count,
79 struct ipath_lkey_table *lk_table)
80{
81 struct ipath_mr *mr;
82 int m, i = 0;
83
84 /* Allocate struct plus pointers to first level page tables. */
85 m = (count + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
86 mr = kmalloc(sizeof *mr + m * sizeof mr->mr.map[0], GFP_KERNEL);
87 if (!mr)
88 goto done;
89
90 /* Allocate first level page tables. */
91 for (; i < m; i++) {
92 mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL);
93 if (!mr->mr.map[i])
94 goto bail;
95 }
96 mr->mr.mapsz = m;
97
98 /*
99 * ib_reg_phys_mr() will initialize mr->ibmr except for
100 * lkey and rkey.
101 */
102 if (!ipath_alloc_lkey(lk_table, &mr->mr))
103 goto bail;
104 mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey;
105
106 goto done;
107
108bail:
109 while (i) {
110 i--;
111 kfree(mr->mr.map[i]);
112 }
113 kfree(mr);
114 mr = NULL;
115
116done:
117 return mr;
118}
119
120/**
121 * ipath_reg_phys_mr - register a physical memory region
122 * @pd: protection domain for this memory region
123 * @buffer_list: pointer to the list of physical buffers to register
124 * @num_phys_buf: the number of physical buffers to register
125 * @iova_start: the starting address passed over IB which maps to this MR
126 *
127 * Returns the memory region on success, otherwise returns an errno.
128 */
129struct ib_mr *ipath_reg_phys_mr(struct ib_pd *pd,
130 struct ib_phys_buf *buffer_list,
131 int num_phys_buf, int acc, u64 *iova_start)
132{
133 struct ipath_mr *mr;
134 int n, m, i;
135 struct ib_mr *ret;
136
137 mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table);
138 if (mr == NULL) {
139 ret = ERR_PTR(-ENOMEM);
140 goto bail;
141 }
142
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700143 mr->mr.pd = pd;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800144 mr->mr.user_base = *iova_start;
145 mr->mr.iova = *iova_start;
146 mr->mr.length = 0;
147 mr->mr.offset = 0;
148 mr->mr.access_flags = acc;
149 mr->mr.max_segs = num_phys_buf;
150
151 m = 0;
152 n = 0;
153 for (i = 0; i < num_phys_buf; i++) {
Ralph Campbellf2cbb662006-12-12 14:28:28 -0800154 mr->mr.map[m]->segs[n].vaddr = (void *) buffer_list[i].addr;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800155 mr->mr.map[m]->segs[n].length = buffer_list[i].size;
156 mr->mr.length += buffer_list[i].size;
157 n++;
158 if (n == IPATH_SEGSZ) {
159 m++;
160 n = 0;
161 }
162 }
163
164 ret = &mr->ibmr;
165
166bail:
167 return ret;
168}
169
170/**
171 * ipath_reg_user_mr - register a userspace memory region
172 * @pd: protection domain for this memory region
173 * @region: the user memory region
174 * @mr_access_flags: access flags for this memory region
175 * @udata: unused by the InfiniPath driver
176 *
177 * Returns the memory region on success, otherwise returns an errno.
178 */
179struct ib_mr *ipath_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
180 int mr_access_flags, struct ib_udata *udata)
181{
182 struct ipath_mr *mr;
183 struct ib_umem_chunk *chunk;
184 int n, m, i;
185 struct ib_mr *ret;
186
Bryan O'Sullivan4a45b7d2006-07-01 04:35:55 -0700187 if (region->length == 0) {
188 ret = ERR_PTR(-EINVAL);
189 goto bail;
190 }
191
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800192 n = 0;
193 list_for_each_entry(chunk, &region->chunk_list, list)
194 n += chunk->nents;
195
196 mr = alloc_mr(n, &to_idev(pd->device)->lk_table);
197 if (!mr) {
198 ret = ERR_PTR(-ENOMEM);
199 goto bail;
200 }
201
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700202 mr->mr.pd = pd;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800203 mr->mr.user_base = region->user_base;
204 mr->mr.iova = region->virt_base;
205 mr->mr.length = region->length;
206 mr->mr.offset = region->offset;
207 mr->mr.access_flags = mr_access_flags;
208 mr->mr.max_segs = n;
209
210 m = 0;
211 n = 0;
212 list_for_each_entry(chunk, &region->chunk_list, list) {
Bryan O'Sullivan165c5522007-03-15 14:44:46 -0700213 for (i = 0; i < chunk->nents; i++) {
214 void *vaddr;
215
216 vaddr = page_address(chunk->page_list[i].page);
217 if (!vaddr) {
218 ret = ERR_PTR(-EINVAL);
219 goto bail;
220 }
221 mr->mr.map[m]->segs[n].vaddr = vaddr;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800222 mr->mr.map[m]->segs[n].length = region->page_size;
223 n++;
224 if (n == IPATH_SEGSZ) {
225 m++;
226 n = 0;
227 }
228 }
229 }
230 ret = &mr->ibmr;
231
232bail:
233 return ret;
234}
235
236/**
237 * ipath_dereg_mr - unregister and free a memory region
238 * @ibmr: the memory region to free
239 *
240 * Returns 0 on success.
241 *
242 * Note that this is called to free MRs created by ipath_get_dma_mr()
243 * or ipath_reg_user_mr().
244 */
245int ipath_dereg_mr(struct ib_mr *ibmr)
246{
247 struct ipath_mr *mr = to_imr(ibmr);
248 int i;
249
250 ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);
251 i = mr->mr.mapsz;
252 while (i) {
253 i--;
254 kfree(mr->mr.map[i]);
255 }
256 kfree(mr);
257 return 0;
258}
259
260/**
261 * ipath_alloc_fmr - allocate a fast memory region
262 * @pd: the protection domain for this memory region
263 * @mr_access_flags: access flags for this memory region
264 * @fmr_attr: fast memory region attributes
265 *
266 * Returns the memory region on success, otherwise returns an errno.
267 */
268struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
269 struct ib_fmr_attr *fmr_attr)
270{
271 struct ipath_fmr *fmr;
272 int m, i = 0;
273 struct ib_fmr *ret;
274
275 /* Allocate struct plus pointers to first level page tables. */
276 m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
277 fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL);
278 if (!fmr)
279 goto bail;
280
281 /* Allocate first level page tables. */
282 for (; i < m; i++) {
283 fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],
284 GFP_KERNEL);
285 if (!fmr->mr.map[i])
286 goto bail;
287 }
288 fmr->mr.mapsz = m;
289
290 /*
291 * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
292 * rkey.
293 */
294 if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))
295 goto bail;
296 fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;
297 /*
298 * Resources are allocated but no valid mapping (RKEY can't be
299 * used).
300 */
Bryan O'Sullivan6a553af2006-09-28 09:00:07 -0700301 fmr->mr.pd = pd;
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800302 fmr->mr.user_base = 0;
303 fmr->mr.iova = 0;
304 fmr->mr.length = 0;
305 fmr->mr.offset = 0;
306 fmr->mr.access_flags = mr_access_flags;
307 fmr->mr.max_segs = fmr_attr->max_pages;
308 fmr->page_shift = fmr_attr->page_shift;
309
310 ret = &fmr->ibfmr;
311 goto done;
312
313bail:
314 while (i)
315 kfree(fmr->mr.map[--i]);
316 kfree(fmr);
317 ret = ERR_PTR(-ENOMEM);
318
319done:
320 return ret;
321}
322
323/**
324 * ipath_map_phys_fmr - set up a fast memory region
325 * @ibmfr: the fast memory region to set up
326 * @page_list: the list of pages to associate with the fast memory region
327 * @list_len: the number of pages to associate with the fast memory region
328 * @iova: the virtual address of the start of the fast memory region
329 *
330 * This may be called from interrupt context.
331 */
332
333int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,
334 int list_len, u64 iova)
335{
336 struct ipath_fmr *fmr = to_ifmr(ibfmr);
337 struct ipath_lkey_table *rkt;
338 unsigned long flags;
339 int m, n, i;
340 u32 ps;
341 int ret;
342
343 if (list_len > fmr->mr.max_segs) {
344 ret = -EINVAL;
345 goto bail;
346 }
347 rkt = &to_idev(ibfmr->device)->lk_table;
348 spin_lock_irqsave(&rkt->lock, flags);
349 fmr->mr.user_base = iova;
350 fmr->mr.iova = iova;
351 ps = 1 << fmr->page_shift;
352 fmr->mr.length = list_len * ps;
353 m = 0;
354 n = 0;
355 ps = 1 << fmr->page_shift;
356 for (i = 0; i < list_len; i++) {
Ralph Campbellf2cbb662006-12-12 14:28:28 -0800357 fmr->mr.map[m]->segs[n].vaddr = (void *) page_list[i];
Bryan O'Sullivancef1cce2006-03-29 15:23:36 -0800358 fmr->mr.map[m]->segs[n].length = ps;
359 if (++n == IPATH_SEGSZ) {
360 m++;
361 n = 0;
362 }
363 }
364 spin_unlock_irqrestore(&rkt->lock, flags);
365 ret = 0;
366
367bail:
368 return ret;
369}
370
371/**
372 * ipath_unmap_fmr - unmap fast memory regions
373 * @fmr_list: the list of fast memory regions to unmap
374 *
375 * Returns 0 on success.
376 */
377int ipath_unmap_fmr(struct list_head *fmr_list)
378{
379 struct ipath_fmr *fmr;
380 struct ipath_lkey_table *rkt;
381 unsigned long flags;
382
383 list_for_each_entry(fmr, fmr_list, ibfmr.list) {
384 rkt = &to_idev(fmr->ibfmr.device)->lk_table;
385 spin_lock_irqsave(&rkt->lock, flags);
386 fmr->mr.user_base = 0;
387 fmr->mr.iova = 0;
388 fmr->mr.length = 0;
389 spin_unlock_irqrestore(&rkt->lock, flags);
390 }
391 return 0;
392}
393
394/**
395 * ipath_dealloc_fmr - deallocate a fast memory region
396 * @ibfmr: the fast memory region to deallocate
397 *
398 * Returns 0 on success.
399 */
400int ipath_dealloc_fmr(struct ib_fmr *ibfmr)
401{
402 struct ipath_fmr *fmr = to_ifmr(ibfmr);
403 int i;
404
405 ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);
406 i = fmr->mr.mapsz;
407 while (i)
408 kfree(fmr->mr.map[--i]);
409 kfree(fmr);
410 return 0;
411}