blob: fe2c2e94a5f87031de3c68591c299b0c38bf353f [file] [log] [blame]
Roland Dreier225c7b12007-05-08 18:00:38 -07001/*
2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33#include "mlx4_ib.h"
34
35static u32 convert_access(int acc)
36{
37 return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX4_PERM_ATOMIC : 0) |
38 (acc & IB_ACCESS_REMOTE_WRITE ? MLX4_PERM_REMOTE_WRITE : 0) |
39 (acc & IB_ACCESS_REMOTE_READ ? MLX4_PERM_REMOTE_READ : 0) |
40 (acc & IB_ACCESS_LOCAL_WRITE ? MLX4_PERM_LOCAL_WRITE : 0) |
41 MLX4_PERM_LOCAL_READ;
42}
43
44struct ib_mr *mlx4_ib_get_dma_mr(struct ib_pd *pd, int acc)
45{
46 struct mlx4_ib_mr *mr;
47 int err;
48
49 mr = kmalloc(sizeof *mr, GFP_KERNEL);
50 if (!mr)
51 return ERR_PTR(-ENOMEM);
52
53 err = mlx4_mr_alloc(to_mdev(pd->device)->dev, to_mpd(pd)->pdn, 0,
54 ~0ull, convert_access(acc), 0, 0, &mr->mmr);
55 if (err)
56 goto err_free;
57
58 err = mlx4_mr_enable(to_mdev(pd->device)->dev, &mr->mmr);
59 if (err)
60 goto err_mr;
61
62 mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
63 mr->umem = NULL;
64
65 return &mr->ibmr;
66
67err_mr:
68 mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
69
70err_free:
71 kfree(mr);
72
73 return ERR_PTR(err);
74}
75
76int mlx4_ib_umem_write_mtt(struct mlx4_ib_dev *dev, struct mlx4_mtt *mtt,
77 struct ib_umem *umem)
78{
79 u64 *pages;
80 struct ib_umem_chunk *chunk;
81 int i, j, k;
82 int n;
83 int len;
84 int err = 0;
85
86 pages = (u64 *) __get_free_page(GFP_KERNEL);
87 if (!pages)
88 return -ENOMEM;
89
90 i = n = 0;
91
92 list_for_each_entry(chunk, &umem->chunk_list, list)
93 for (j = 0; j < chunk->nmap; ++j) {
94 len = sg_dma_len(&chunk->page_list[j]) >> mtt->page_shift;
95 for (k = 0; k < len; ++k) {
96 pages[i++] = sg_dma_address(&chunk->page_list[j]) +
97 umem->page_size * k;
98 /*
Jack Morgensteind7bb58f2007-08-01 12:28:53 +030099 * Be friendly to mlx4_write_mtt() and
100 * pass it chunks of appropriate size.
Roland Dreier225c7b12007-05-08 18:00:38 -0700101 */
Jack Morgensteind7bb58f2007-08-01 12:28:53 +0300102 if (i == PAGE_SIZE / sizeof (u64)) {
Roland Dreier225c7b12007-05-08 18:00:38 -0700103 err = mlx4_write_mtt(dev->dev, mtt, n,
104 i, pages);
105 if (err)
106 goto out;
107 n += i;
108 i = 0;
109 }
110 }
111 }
112
113 if (i)
114 err = mlx4_write_mtt(dev->dev, mtt, n, i, pages);
115
116out:
117 free_page((unsigned long) pages);
118 return err;
119}
120
121struct ib_mr *mlx4_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
122 u64 virt_addr, int access_flags,
123 struct ib_udata *udata)
124{
125 struct mlx4_ib_dev *dev = to_mdev(pd->device);
126 struct mlx4_ib_mr *mr;
127 int shift;
128 int err;
129 int n;
130
131 mr = kmalloc(sizeof *mr, GFP_KERNEL);
132 if (!mr)
133 return ERR_PTR(-ENOMEM);
134
135 mr->umem = ib_umem_get(pd->uobject->context, start, length, access_flags);
136 if (IS_ERR(mr->umem)) {
137 err = PTR_ERR(mr->umem);
138 goto err_free;
139 }
140
141 n = ib_umem_page_count(mr->umem);
142 shift = ilog2(mr->umem->page_size);
143
144 err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, virt_addr, length,
145 convert_access(access_flags), n, shift, &mr->mmr);
146 if (err)
147 goto err_umem;
148
149 err = mlx4_ib_umem_write_mtt(dev, &mr->mmr.mtt, mr->umem);
150 if (err)
151 goto err_mr;
152
153 err = mlx4_mr_enable(dev->dev, &mr->mmr);
154 if (err)
155 goto err_mr;
156
157 mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
158
159 return &mr->ibmr;
160
161err_mr:
162 mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
163
164err_umem:
165 ib_umem_release(mr->umem);
166
167err_free:
168 kfree(mr);
169
170 return ERR_PTR(err);
171}
172
173int mlx4_ib_dereg_mr(struct ib_mr *ibmr)
174{
175 struct mlx4_ib_mr *mr = to_mmr(ibmr);
176
177 mlx4_mr_free(to_mdev(ibmr->device)->dev, &mr->mmr);
178 if (mr->umem)
179 ib_umem_release(mr->umem);
180 kfree(mr);
181
182 return 0;
183}
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300184
185struct ib_fmr *mlx4_ib_fmr_alloc(struct ib_pd *pd, int acc,
186 struct ib_fmr_attr *fmr_attr)
187{
188 struct mlx4_ib_dev *dev = to_mdev(pd->device);
189 struct mlx4_ib_fmr *fmr;
190 int err = -ENOMEM;
191
192 fmr = kmalloc(sizeof *fmr, GFP_KERNEL);
193 if (!fmr)
194 return ERR_PTR(-ENOMEM);
195
196 err = mlx4_fmr_alloc(dev->dev, to_mpd(pd)->pdn, convert_access(acc),
197 fmr_attr->max_pages, fmr_attr->max_maps,
198 fmr_attr->page_shift, &fmr->mfmr);
199 if (err)
200 goto err_free;
201
Jack Morgensteine6028c02008-02-14 10:39:36 -0800202 err = mlx4_fmr_enable(to_mdev(pd->device)->dev, &fmr->mfmr);
Jack Morgenstein8ad11fb2007-08-01 12:29:05 +0300203 if (err)
204 goto err_mr;
205
206 fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mfmr.mr.key;
207
208 return &fmr->ibfmr;
209
210err_mr:
211 mlx4_mr_free(to_mdev(pd->device)->dev, &fmr->mfmr.mr);
212
213err_free:
214 kfree(fmr);
215
216 return ERR_PTR(err);
217}
218
219int mlx4_ib_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
220 int npages, u64 iova)
221{
222 struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
223 struct mlx4_ib_dev *dev = to_mdev(ifmr->ibfmr.device);
224
225 return mlx4_map_phys_fmr(dev->dev, &ifmr->mfmr, page_list, npages, iova,
226 &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
227}
228
229int mlx4_ib_unmap_fmr(struct list_head *fmr_list)
230{
231 struct ib_fmr *ibfmr;
232 int err;
233 struct mlx4_dev *mdev = NULL;
234
235 list_for_each_entry(ibfmr, fmr_list, list) {
236 if (mdev && to_mdev(ibfmr->device)->dev != mdev)
237 return -EINVAL;
238 mdev = to_mdev(ibfmr->device)->dev;
239 }
240
241 if (!mdev)
242 return 0;
243
244 list_for_each_entry(ibfmr, fmr_list, list) {
245 struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
246
247 mlx4_fmr_unmap(mdev, &ifmr->mfmr, &ifmr->ibfmr.lkey, &ifmr->ibfmr.rkey);
248 }
249
250 /*
251 * Make sure all MPT status updates are visible before issuing
252 * SYNC_TPT firmware command.
253 */
254 wmb();
255
256 err = mlx4_SYNC_TPT(mdev);
257 if (err)
258 printk(KERN_WARNING "mlx4_ib: SYNC_TPT error %d when "
259 "unmapping FMRs\n", err);
260
261 return 0;
262}
263
264int mlx4_ib_fmr_dealloc(struct ib_fmr *ibfmr)
265{
266 struct mlx4_ib_fmr *ifmr = to_mfmr(ibfmr);
267 struct mlx4_ib_dev *dev = to_mdev(ibfmr->device);
268 int err;
269
270 err = mlx4_fmr_free(dev->dev, &ifmr->mfmr);
271
272 if (!err)
273 kfree(ifmr);
274
275 return err;
276}