1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/slab.h>
34
35#include "c2.h"
36#include "c2_vq.h"
37
38#define PBL_VIRT 1
39#define PBL_PHYS 2
40
41
42
43
44
45
46
47
48
49static int
50send_pbl_messages(struct c2_dev *c2dev, __be32 stag_index,
51 unsigned long va, u32 pbl_depth,
52 struct c2_vq_req *vq_req, int pbl_type)
53{
54 u32 pbe_count;
55 u32 count;
56 struct c2wr_nsmr_pbl_req *wr;
57 struct c2wr_nsmr_pbl_rep *reply;
58 int err, pbl_virt, pbl_index, i;
59
60 switch (pbl_type) {
61 case PBL_VIRT:
62 pbl_virt = 1;
63 break;
64 case PBL_PHYS:
65 pbl_virt = 0;
66 break;
67 default:
68 return -EINVAL;
69 break;
70 }
71
72 pbe_count = (c2dev->req_vq.msg_size -
73 sizeof(struct c2wr_nsmr_pbl_req)) / sizeof(u64);
74 wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);
75 if (!wr) {
76 return -ENOMEM;
77 }
78 c2_wr_set_id(wr, CCWR_NSMR_PBL);
79
80
81
82
83
84
85 wr->hdr.context = 0;
86 wr->rnic_handle = c2dev->adapter_handle;
87 wr->stag_index = stag_index;
88 wr->flags = 0;
89 pbl_index = 0;
90 while (pbl_depth) {
91 count = min(pbe_count, pbl_depth);
92 wr->addrs_length = cpu_to_be32(count);
93
94
95
96
97
98
99 if (count == pbl_depth) {
100
101
102
103
104 vq_req_get(c2dev, vq_req);
105 wr->flags = cpu_to_be32(MEM_PBL_COMPLETE);
106
107
108
109
110
111
112 wr->hdr.context = (unsigned long) vq_req;
113 }
114
115
116
117
118
119
120
121
122
123
124 for (i = 0; i < count; i++) {
125 if (pbl_virt) {
126 va += PAGE_SIZE;
127 } else {
128 wr->paddrs[i] =
129 cpu_to_be64(((u64 *)va)[pbl_index + i]);
130 }
131 }
132
133
134
135
136 err = vq_send_wr(c2dev, (union c2wr *) wr);
137 if (err) {
138 if (count <= pbe_count) {
139 vq_req_put(c2dev, vq_req);
140 }
141 goto bail0;
142 }
143 pbl_depth -= count;
144 pbl_index += count;
145 }
146
147
148
149
150 err = vq_wait_for_reply(c2dev, vq_req);
151 if (err) {
152 goto bail0;
153 }
154
155
156
157
158 reply = (struct c2wr_nsmr_pbl_rep *) (unsigned long) vq_req->reply_msg;
159 if (!reply) {
160 err = -ENOMEM;
161 goto bail0;
162 }
163
164 err = c2_errno(reply);
165
166 vq_repbuf_free(c2dev, reply);
167 bail0:
168 kfree(wr);
169 return err;
170}
171
172#define C2_PBL_MAX_DEPTH 131072
173int
174c2_nsmr_register_phys_kern(struct c2_dev *c2dev, u64 *addr_list,
175 int page_size, int pbl_depth, u32 length,
176 u32 offset, u64 *va, enum c2_acf acf,
177 struct c2_mr *mr)
178{
179 struct c2_vq_req *vq_req;
180 struct c2wr_nsmr_register_req *wr;
181 struct c2wr_nsmr_register_rep *reply;
182 u16 flags;
183 int i, pbe_count, count;
184 int err;
185
186 if (!va || !length || !addr_list || !pbl_depth)
187 return -EINTR;
188
189
190
191
192 if (pbl_depth > C2_PBL_MAX_DEPTH) {
193 return -EINTR;
194 }
195
196
197
198
199 vq_req = vq_req_alloc(c2dev);
200 if (!vq_req)
201 return -ENOMEM;
202
203 wr = kmalloc(c2dev->req_vq.msg_size, GFP_KERNEL);
204 if (!wr) {
205 err = -ENOMEM;
206 goto bail0;
207 }
208
209
210
211
212 c2_wr_set_id(wr, CCWR_NSMR_REGISTER);
213 wr->hdr.context = (unsigned long) vq_req;
214 wr->rnic_handle = c2dev->adapter_handle;
215
216 flags = (acf | MEM_VA_BASED | MEM_REMOTE);
217
218
219
220
221 pbe_count = (c2dev->req_vq.msg_size -
222 sizeof(struct c2wr_nsmr_register_req)) / sizeof(u64);
223
224 if (pbl_depth <= pbe_count) {
225 flags |= MEM_PBL_COMPLETE;
226 }
227 wr->flags = cpu_to_be16(flags);
228 wr->stag_key = 0;
229 wr->va = cpu_to_be64(*va);
230 wr->pd_id = mr->pd->pd_id;
231 wr->pbe_size = cpu_to_be32(page_size);
232 wr->length = cpu_to_be32(length);
233 wr->pbl_depth = cpu_to_be32(pbl_depth);
234 wr->fbo = cpu_to_be32(offset);
235 count = min(pbl_depth, pbe_count);
236 wr->addrs_length = cpu_to_be32(count);
237
238
239
240
241 for (i = 0; i < count; i++) {
242 wr->paddrs[i] = cpu_to_be64(addr_list[i]);
243 }
244
245
246
247
248 vq_req_get(c2dev, vq_req);
249
250
251
252
253 err = vq_send_wr(c2dev, (union c2wr *) wr);
254 if (err) {
255 vq_req_put(c2dev, vq_req);
256 goto bail1;
257 }
258
259
260
261
262 err = vq_wait_for_reply(c2dev, vq_req);
263 if (err) {
264 goto bail1;
265 }
266
267
268
269
270 reply =
271 (struct c2wr_nsmr_register_rep *) (unsigned long) (vq_req->reply_msg);
272 if (!reply) {
273 err = -ENOMEM;
274 goto bail1;
275 }
276 if ((err = c2_errno(reply))) {
277 goto bail2;
278 }
279
280 mr->ibmr.lkey = mr->ibmr.rkey = be32_to_cpu(reply->stag_index);
281 vq_repbuf_free(c2dev, reply);
282
283
284
285
286
287
288 pbl_depth -= count;
289 if (pbl_depth) {
290
291 vq_req->reply_msg = (unsigned long) NULL;
292 atomic_set(&vq_req->reply_ready, 0);
293 err = send_pbl_messages(c2dev,
294 cpu_to_be32(mr->ibmr.lkey),
295 (unsigned long) &addr_list[i],
296 pbl_depth, vq_req, PBL_PHYS);
297 if (err) {
298 goto bail1;
299 }
300 }
301
302 vq_req_free(c2dev, vq_req);
303 kfree(wr);
304
305 return err;
306
307 bail2:
308 vq_repbuf_free(c2dev, reply);
309 bail1:
310 kfree(wr);
311 bail0:
312 vq_req_free(c2dev, vq_req);
313 return err;
314}
315
316int c2_stag_dealloc(struct c2_dev *c2dev, u32 stag_index)
317{
318 struct c2_vq_req *vq_req;
319 struct c2wr_stag_dealloc_req wr;
320 struct c2wr_stag_dealloc_rep *reply;
321 int err;
322
323
324
325
326
327 vq_req = vq_req_alloc(c2dev);
328 if (!vq_req) {
329 return -ENOMEM;
330 }
331
332
333
334
335 c2_wr_set_id(&wr, CCWR_STAG_DEALLOC);
336 wr.hdr.context = (u64) (unsigned long) vq_req;
337 wr.rnic_handle = c2dev->adapter_handle;
338 wr.stag_index = cpu_to_be32(stag_index);
339
340
341
342
343 vq_req_get(c2dev, vq_req);
344
345
346
347
348 err = vq_send_wr(c2dev, (union c2wr *) & wr);
349 if (err) {
350 vq_req_put(c2dev, vq_req);
351 goto bail0;
352 }
353
354
355
356
357 err = vq_wait_for_reply(c2dev, vq_req);
358 if (err) {
359 goto bail0;
360 }
361
362
363
364
365 reply = (struct c2wr_stag_dealloc_rep *) (unsigned long) vq_req->reply_msg;
366 if (!reply) {
367 err = -ENOMEM;
368 goto bail0;
369 }
370
371 err = c2_errno(reply);
372
373 vq_repbuf_free(c2dev, reply);
374 bail0:
375 vq_req_free(c2dev, vq_req);
376 return err;
377}
378