Commit | Line | Data |
---|---|---|
6461f64a OG |
1 | /* |
2 | * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved. | |
28f292e8 | 3 | * Copyright (c) 2013 Mellanox Technologies. All rights reserved. |
6461f64a OG |
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. | |
6461f64a OG |
32 | */ |
33 | #include <linux/module.h> | |
34 | #include <linux/kernel.h> | |
35 | #include <linux/slab.h> | |
36 | #include <linux/mm.h> | |
a1f8e7f7 | 37 | #include <linux/highmem.h> |
6461f64a OG |
38 | #include <linux/scatterlist.h> |
39 | ||
40 | #include "iscsi_iser.h" | |
41 | ||
42 | #define ISER_KMALLOC_THRESHOLD 0x20000 /* 128K - kmalloc limit */ | |
8dfa0876 | 43 | |
6461f64a OG |
44 | /** |
45 | * iser_start_rdma_unaligned_sg | |
46 | */ | |
2261ec3d | 47 | static int iser_start_rdma_unaligned_sg(struct iscsi_iser_task *iser_task, |
41179e2d | 48 | enum iser_data_dir cmd_dir) |
6461f64a OG |
49 | { |
50 | int dma_nents; | |
5180311f | 51 | struct ib_device *dev; |
6461f64a | 52 | char *mem = NULL; |
2261ec3d | 53 | struct iser_data_buf *data = &iser_task->data[cmd_dir]; |
6461f64a OG |
54 | unsigned long cmd_data_len = data->data_len; |
55 | ||
56 | if (cmd_data_len > ISER_KMALLOC_THRESHOLD) | |
528f4e8c | 57 | mem = (void *)__get_free_pages(GFP_ATOMIC, |
f0d1b0b3 | 58 | ilog2(roundup_pow_of_two(cmd_data_len)) - PAGE_SHIFT); |
6461f64a | 59 | else |
528f4e8c | 60 | mem = kmalloc(cmd_data_len, GFP_ATOMIC); |
6461f64a OG |
61 | |
62 | if (mem == NULL) { | |
63 | iser_err("Failed to allocate mem size %d %d for copying sglist\n", | |
64 | data->size,(int)cmd_data_len); | |
65 | return -ENOMEM; | |
66 | } | |
67 | ||
68 | if (cmd_dir == ISER_DIR_OUT) { | |
69 | /* copy the unaligned sg the buffer which is used for RDMA */ | |
53d412fc JA |
70 | struct scatterlist *sgl = (struct scatterlist *)data->buf; |
71 | struct scatterlist *sg; | |
6461f64a OG |
72 | int i; |
73 | char *p, *from; | |
74 | ||
53d412fc JA |
75 | p = mem; |
76 | for_each_sg(sgl, sg, data->size, i) { | |
2a156d09 | 77 | from = kmap_atomic(sg_page(sg)); |
6461f64a | 78 | memcpy(p, |
53d412fc JA |
79 | from + sg->offset, |
80 | sg->length); | |
2a156d09 | 81 | kunmap_atomic(from); |
53d412fc | 82 | p += sg->length; |
6461f64a OG |
83 | } |
84 | } | |
85 | ||
2261ec3d MC |
86 | sg_init_one(&iser_task->data_copy[cmd_dir].sg_single, mem, cmd_data_len); |
87 | iser_task->data_copy[cmd_dir].buf = | |
88 | &iser_task->data_copy[cmd_dir].sg_single; | |
89 | iser_task->data_copy[cmd_dir].size = 1; | |
6461f64a | 90 | |
2261ec3d | 91 | iser_task->data_copy[cmd_dir].copy_buf = mem; |
6461f64a | 92 | |
2261ec3d | 93 | dev = iser_task->iser_conn->ib_conn->device->ib_device; |
5180311f | 94 | dma_nents = ib_dma_map_sg(dev, |
2261ec3d | 95 | &iser_task->data_copy[cmd_dir].sg_single, |
5180311f RC |
96 | 1, |
97 | (cmd_dir == ISER_DIR_OUT) ? | |
98 | DMA_TO_DEVICE : DMA_FROM_DEVICE); | |
6461f64a OG |
99 | BUG_ON(dma_nents == 0); |
100 | ||
2261ec3d | 101 | iser_task->data_copy[cmd_dir].dma_nents = dma_nents; |
6461f64a OG |
102 | return 0; |
103 | } | |
104 | ||
105 | /** | |
106 | * iser_finalize_rdma_unaligned_sg | |
107 | */ | |
2261ec3d | 108 | void iser_finalize_rdma_unaligned_sg(struct iscsi_iser_task *iser_task, |
6461f64a OG |
109 | enum iser_data_dir cmd_dir) |
110 | { | |
5180311f | 111 | struct ib_device *dev; |
6461f64a OG |
112 | struct iser_data_buf *mem_copy; |
113 | unsigned long cmd_data_len; | |
114 | ||
2261ec3d MC |
115 | dev = iser_task->iser_conn->ib_conn->device->ib_device; |
116 | mem_copy = &iser_task->data_copy[cmd_dir]; | |
6461f64a | 117 | |
5180311f RC |
118 | ib_dma_unmap_sg(dev, &mem_copy->sg_single, 1, |
119 | (cmd_dir == ISER_DIR_OUT) ? | |
120 | DMA_TO_DEVICE : DMA_FROM_DEVICE); | |
6461f64a OG |
121 | |
122 | if (cmd_dir == ISER_DIR_IN) { | |
123 | char *mem; | |
53d412fc | 124 | struct scatterlist *sgl, *sg; |
6461f64a OG |
125 | unsigned char *p, *to; |
126 | unsigned int sg_size; | |
127 | int i; | |
128 | ||
129 | /* copy back read RDMA to unaligned sg */ | |
130 | mem = mem_copy->copy_buf; | |
131 | ||
2261ec3d MC |
132 | sgl = (struct scatterlist *)iser_task->data[ISER_DIR_IN].buf; |
133 | sg_size = iser_task->data[ISER_DIR_IN].size; | |
6461f64a | 134 | |
53d412fc JA |
135 | p = mem; |
136 | for_each_sg(sgl, sg, sg_size, i) { | |
2a156d09 | 137 | to = kmap_atomic(sg_page(sg)); |
53d412fc | 138 | memcpy(to + sg->offset, |
6461f64a | 139 | p, |
53d412fc | 140 | sg->length); |
2a156d09 | 141 | kunmap_atomic(to); |
53d412fc | 142 | p += sg->length; |
6461f64a OG |
143 | } |
144 | } | |
145 | ||
2261ec3d | 146 | cmd_data_len = iser_task->data[cmd_dir].data_len; |
6461f64a OG |
147 | |
148 | if (cmd_data_len > ISER_KMALLOC_THRESHOLD) | |
149 | free_pages((unsigned long)mem_copy->copy_buf, | |
f0d1b0b3 | 150 | ilog2(roundup_pow_of_two(cmd_data_len)) - PAGE_SHIFT); |
6461f64a OG |
151 | else |
152 | kfree(mem_copy->copy_buf); | |
153 | ||
154 | mem_copy->copy_buf = NULL; | |
155 | } | |
156 | ||
c1ccaf24 OG |
157 | #define IS_4K_ALIGNED(addr) ((((unsigned long)addr) & ~MASK_4K) == 0) |
158 | ||
6461f64a OG |
159 | /** |
160 | * iser_sg_to_page_vec - Translates scatterlist entries to physical addresses | |
161 | * and returns the length of resulting physical address array (may be less than | |
162 | * the original due to possible compaction). | |
163 | * | |
164 | * we build a "page vec" under the assumption that the SG meets the RDMA | |
165 | * alignment requirements. Other then the first and last SG elements, all | |
166 | * the "internal" elements can be compacted into a list whose elements are | |
167 | * dma addresses of physical pages. The code supports also the weird case | |
168 | * where --few fragments of the same page-- are present in the SG as | |
169 | * consecutive elements. Also, it handles one entry SG. | |
170 | */ | |
c1ccaf24 | 171 | |
6461f64a | 172 | static int iser_sg_to_page_vec(struct iser_data_buf *data, |
919fc274 SG |
173 | struct ib_device *ibdev, u64 *pages, |
174 | int *offset, int *data_size) | |
6461f64a | 175 | { |
c1ccaf24 OG |
176 | struct scatterlist *sg, *sgl = (struct scatterlist *)data->buf; |
177 | u64 start_addr, end_addr, page, chunk_start = 0; | |
6461f64a | 178 | unsigned long total_sz = 0; |
c1ccaf24 OG |
179 | unsigned int dma_len; |
180 | int i, new_chunk, cur_page, last_ent = data->dma_nents - 1; | |
6461f64a OG |
181 | |
182 | /* compute the offset of first element */ | |
919fc274 | 183 | *offset = (u64) sgl[0].offset & ~MASK_4K; |
6461f64a | 184 | |
c1ccaf24 OG |
185 | new_chunk = 1; |
186 | cur_page = 0; | |
53d412fc | 187 | for_each_sg(sgl, sg, data->dma_nents, i) { |
c1ccaf24 OG |
188 | start_addr = ib_sg_dma_address(ibdev, sg); |
189 | if (new_chunk) | |
190 | chunk_start = start_addr; | |
191 | dma_len = ib_sg_dma_len(ibdev, sg); | |
192 | end_addr = start_addr + dma_len; | |
5180311f | 193 | total_sz += dma_len; |
6461f64a | 194 | |
c1ccaf24 OG |
195 | /* collect page fragments until aligned or end of SG list */ |
196 | if (!IS_4K_ALIGNED(end_addr) && i < last_ent) { | |
197 | new_chunk = 0; | |
198 | continue; | |
6461f64a | 199 | } |
c1ccaf24 OG |
200 | new_chunk = 1; |
201 | ||
202 | /* address of the first page in the contiguous chunk; | |
203 | masking relevant for the very first SG entry, | |
204 | which might be unaligned */ | |
205 | page = chunk_start & MASK_4K; | |
206 | do { | |
919fc274 | 207 | pages[cur_page++] = page; |
8dfa0876 | 208 | page += SIZE_4K; |
c1ccaf24 | 209 | } while (page < end_addr); |
6461f64a | 210 | } |
c1ccaf24 | 211 | |
919fc274 SG |
212 | *data_size = total_sz; |
213 | iser_dbg("page_vec->data_size:%d cur_page %d\n", | |
214 | *data_size, cur_page); | |
6461f64a OG |
215 | return cur_page; |
216 | } | |
217 | ||
6461f64a OG |
218 | |
219 | /** | |
220 | * iser_data_buf_aligned_len - Tries to determine the maximal correctly aligned | |
221 | * for RDMA sub-list of a scatter-gather list of memory buffers, and returns | |
222 | * the number of entries which are aligned correctly. Supports the case where | |
223 | * consecutive SG elements are actually fragments of the same physcial page. | |
224 | */ | |
c1ccaf24 OG |
225 | static int iser_data_buf_aligned_len(struct iser_data_buf *data, |
226 | struct ib_device *ibdev) | |
6461f64a | 227 | { |
c1ccaf24 OG |
228 | struct scatterlist *sgl, *sg, *next_sg = NULL; |
229 | u64 start_addr, end_addr; | |
230 | int i, ret_len, start_check = 0; | |
231 | ||
232 | if (data->dma_nents == 1) | |
233 | return 1; | |
6461f64a | 234 | |
53d412fc | 235 | sgl = (struct scatterlist *)data->buf; |
c1ccaf24 | 236 | start_addr = ib_sg_dma_address(ibdev, sgl); |
6461f64a | 237 | |
53d412fc | 238 | for_each_sg(sgl, sg, data->dma_nents, i) { |
c1ccaf24 OG |
239 | if (start_check && !IS_4K_ALIGNED(start_addr)) |
240 | break; | |
241 | ||
242 | next_sg = sg_next(sg); | |
243 | if (!next_sg) | |
244 | break; | |
245 | ||
246 | end_addr = start_addr + ib_sg_dma_len(ibdev, sg); | |
247 | start_addr = ib_sg_dma_address(ibdev, next_sg); | |
248 | ||
249 | if (end_addr == start_addr) { | |
250 | start_check = 0; | |
251 | continue; | |
252 | } else | |
253 | start_check = 1; | |
254 | ||
255 | if (!IS_4K_ALIGNED(end_addr)) | |
256 | break; | |
6461f64a | 257 | } |
c1ccaf24 | 258 | ret_len = (next_sg) ? i : i+1; |
6461f64a OG |
259 | iser_dbg("Found %d aligned entries out of %d in sg:0x%p\n", |
260 | ret_len, data->dma_nents, data); | |
261 | return ret_len; | |
262 | } | |
263 | ||
5180311f RC |
264 | static void iser_data_buf_dump(struct iser_data_buf *data, |
265 | struct ib_device *ibdev) | |
6461f64a | 266 | { |
53d412fc JA |
267 | struct scatterlist *sgl = (struct scatterlist *)data->buf; |
268 | struct scatterlist *sg; | |
6461f64a OG |
269 | int i; |
270 | ||
53d412fc | 271 | for_each_sg(sgl, sg, data->dma_nents, i) |
f91424cf | 272 | iser_dbg("sg[%d] dma_addr:0x%lX page:0x%p " |
e981f1d4 | 273 | "off:0x%x sz:0x%x dma_len:0x%x\n", |
53d412fc | 274 | i, (unsigned long)ib_sg_dma_address(ibdev, sg), |
45711f1a | 275 | sg_page(sg), sg->offset, |
53d412fc | 276 | sg->length, ib_sg_dma_len(ibdev, sg)); |
6461f64a OG |
277 | } |
278 | ||
279 | static void iser_dump_page_vec(struct iser_page_vec *page_vec) | |
280 | { | |
281 | int i; | |
282 | ||
283 | iser_err("page vec length %d data size %d\n", | |
284 | page_vec->length, page_vec->data_size); | |
285 | for (i = 0; i < page_vec->length; i++) | |
286 | iser_err("%d %lx\n",i,(unsigned long)page_vec->pages[i]); | |
287 | } | |
288 | ||
289 | static void iser_page_vec_build(struct iser_data_buf *data, | |
5180311f RC |
290 | struct iser_page_vec *page_vec, |
291 | struct ib_device *ibdev) | |
6461f64a OG |
292 | { |
293 | int page_vec_len = 0; | |
294 | ||
295 | page_vec->length = 0; | |
296 | page_vec->offset = 0; | |
297 | ||
298 | iser_dbg("Translating sg sz: %d\n", data->dma_nents); | |
919fc274 SG |
299 | page_vec_len = iser_sg_to_page_vec(data, ibdev, page_vec->pages, |
300 | &page_vec->offset, | |
301 | &page_vec->data_size); | |
302 | iser_dbg("sg len %d page_vec_len %d\n", data->dma_nents, page_vec_len); | |
6461f64a OG |
303 | |
304 | page_vec->length = page_vec_len; | |
305 | ||
8dfa0876 | 306 | if (page_vec_len * SIZE_4K < page_vec->data_size) { |
6461f64a | 307 | iser_err("page_vec too short to hold this SG\n"); |
5180311f | 308 | iser_data_buf_dump(data, ibdev); |
6461f64a OG |
309 | iser_dump_page_vec(page_vec); |
310 | BUG(); | |
311 | } | |
312 | } | |
313 | ||
2261ec3d MC |
314 | int iser_dma_map_task_data(struct iscsi_iser_task *iser_task, |
315 | struct iser_data_buf *data, | |
316 | enum iser_data_dir iser_dir, | |
317 | enum dma_data_direction dma_dir) | |
74a20780 | 318 | { |
5180311f | 319 | struct ib_device *dev; |
74a20780 | 320 | |
2261ec3d MC |
321 | iser_task->dir[iser_dir] = 1; |
322 | dev = iser_task->iser_conn->ib_conn->device->ib_device; | |
74a20780 | 323 | |
5180311f | 324 | data->dma_nents = ib_dma_map_sg(dev, data->buf, data->size, dma_dir); |
74a20780 EZ |
325 | if (data->dma_nents == 0) { |
326 | iser_err("dma_map_sg failed!!!\n"); | |
327 | return -EINVAL; | |
328 | } | |
329 | return 0; | |
330 | } | |
331 | ||
2261ec3d | 332 | void iser_dma_unmap_task_data(struct iscsi_iser_task *iser_task) |
74a20780 | 333 | { |
5180311f | 334 | struct ib_device *dev; |
74a20780 EZ |
335 | struct iser_data_buf *data; |
336 | ||
2261ec3d | 337 | dev = iser_task->iser_conn->ib_conn->device->ib_device; |
74a20780 | 338 | |
2261ec3d MC |
339 | if (iser_task->dir[ISER_DIR_IN]) { |
340 | data = &iser_task->data[ISER_DIR_IN]; | |
5180311f | 341 | ib_dma_unmap_sg(dev, data->buf, data->size, DMA_FROM_DEVICE); |
74a20780 EZ |
342 | } |
343 | ||
2261ec3d MC |
344 | if (iser_task->dir[ISER_DIR_OUT]) { |
345 | data = &iser_task->data[ISER_DIR_OUT]; | |
5180311f | 346 | ib_dma_unmap_sg(dev, data->buf, data->size, DMA_TO_DEVICE); |
74a20780 EZ |
347 | } |
348 | } | |
349 | ||
919fc274 SG |
350 | static int fall_to_bounce_buf(struct iscsi_iser_task *iser_task, |
351 | struct ib_device *ibdev, | |
352 | enum iser_data_dir cmd_dir, | |
353 | int aligned_len) | |
354 | { | |
355 | struct iscsi_conn *iscsi_conn = iser_task->iser_conn->iscsi_conn; | |
356 | struct iser_data_buf *mem = &iser_task->data[cmd_dir]; | |
357 | ||
358 | iscsi_conn->fmr_unalign_cnt++; | |
359 | iser_warn("rdma alignment violation (%d/%d aligned) or FMR not supported\n", | |
360 | aligned_len, mem->size); | |
361 | ||
362 | if (iser_debug_level > 0) | |
363 | iser_data_buf_dump(mem, ibdev); | |
364 | ||
365 | /* unmap the command data before accessing it */ | |
366 | iser_dma_unmap_task_data(iser_task); | |
367 | ||
368 | /* allocate copy buf, if we are writing, copy the */ | |
369 | /* unaligned scatterlist, dma map the copy */ | |
370 | if (iser_start_rdma_unaligned_sg(iser_task, cmd_dir) != 0) | |
371 | return -ENOMEM; | |
372 | ||
373 | return 0; | |
374 | } | |
375 | ||
6461f64a | 376 | /** |
e657571b SG |
377 | * iser_reg_rdma_mem_fmr - Registers memory intended for RDMA, |
378 | * using FMR (if possible) obtaining rkey and va | |
6461f64a OG |
379 | * |
380 | * returns 0 on success, errno code on failure | |
381 | */ | |
e657571b SG |
382 | int iser_reg_rdma_mem_fmr(struct iscsi_iser_task *iser_task, |
383 | enum iser_data_dir cmd_dir) | |
6461f64a | 384 | { |
2261ec3d | 385 | struct iser_conn *ib_conn = iser_task->iser_conn->ib_conn; |
d8111028 | 386 | struct iser_device *device = ib_conn->device; |
5180311f | 387 | struct ib_device *ibdev = device->ib_device; |
2261ec3d | 388 | struct iser_data_buf *mem = &iser_task->data[cmd_dir]; |
6461f64a OG |
389 | struct iser_regd_buf *regd_buf; |
390 | int aligned_len; | |
391 | int err; | |
e981f1d4 | 392 | int i; |
d8111028 | 393 | struct scatterlist *sg; |
6461f64a | 394 | |
2261ec3d | 395 | regd_buf = &iser_task->rdma_regd[cmd_dir]; |
6461f64a | 396 | |
5180311f | 397 | aligned_len = iser_data_buf_aligned_len(mem, ibdev); |
5525d210 | 398 | if (aligned_len != mem->dma_nents || |
e657571b | 399 | (!ib_conn->fastreg.fmr.pool && mem->dma_nents > 1)) { |
919fc274 SG |
400 | err = fall_to_bounce_buf(iser_task, ibdev, |
401 | cmd_dir, aligned_len); | |
402 | if (err) { | |
403 | iser_err("failed to allocate bounce buffer\n"); | |
404 | return err; | |
405 | } | |
2261ec3d | 406 | mem = &iser_task->data_copy[cmd_dir]; |
6461f64a OG |
407 | } |
408 | ||
d8111028 EZ |
409 | /* if there a single dma entry, FMR is not needed */ |
410 | if (mem->dma_nents == 1) { | |
411 | sg = (struct scatterlist *)mem->buf; | |
412 | ||
413 | regd_buf->reg.lkey = device->mr->lkey; | |
414 | regd_buf->reg.rkey = device->mr->rkey; | |
5180311f RC |
415 | regd_buf->reg.len = ib_sg_dma_len(ibdev, &sg[0]); |
416 | regd_buf->reg.va = ib_sg_dma_address(ibdev, &sg[0]); | |
d8111028 EZ |
417 | regd_buf->reg.is_fmr = 0; |
418 | ||
419 | iser_dbg("PHYSICAL Mem.register: lkey: 0x%08X rkey: 0x%08X " | |
420 | "va: 0x%08lX sz: %ld]\n", | |
421 | (unsigned int)regd_buf->reg.lkey, | |
422 | (unsigned int)regd_buf->reg.rkey, | |
423 | (unsigned long)regd_buf->reg.va, | |
424 | (unsigned long)regd_buf->reg.len); | |
425 | } else { /* use FMR for multiple dma entries */ | |
e657571b SG |
426 | iser_page_vec_build(mem, ib_conn->fastreg.fmr.page_vec, ibdev); |
427 | err = iser_reg_page_vec(ib_conn, ib_conn->fastreg.fmr.page_vec, | |
428 | ®d_buf->reg); | |
819a0873 | 429 | if (err && err != -EAGAIN) { |
5180311f | 430 | iser_data_buf_dump(mem, ibdev); |
2261ec3d MC |
431 | iser_err("mem->dma_nents = %d (dlength = 0x%x)\n", |
432 | mem->dma_nents, | |
433 | ntoh24(iser_task->desc.iscsi_header.dlength)); | |
d8111028 | 434 | iser_err("page_vec: data_size = 0x%x, length = %d, offset = 0x%x\n", |
e657571b SG |
435 | ib_conn->fastreg.fmr.page_vec->data_size, |
436 | ib_conn->fastreg.fmr.page_vec->length, | |
437 | ib_conn->fastreg.fmr.page_vec->offset); | |
438 | for (i = 0; i < ib_conn->fastreg.fmr.page_vec->length; i++) | |
d8111028 | 439 | iser_err("page_vec[%d] = 0x%llx\n", i, |
e657571b | 440 | (unsigned long long) ib_conn->fastreg.fmr.page_vec->pages[i]); |
e981f1d4 | 441 | } |
450d1e40 OG |
442 | if (err) |
443 | return err; | |
e981f1d4 | 444 | } |
6461f64a OG |
445 | return 0; |
446 | } |