2 * Copyright (c) 2004, 2005, 2006 Voltaire, Inc. All rights reserved.
3 * Copyright (c) 2013 Mellanox Technologies. All rights reserved.
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:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * - Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/slab.h>
37 #include <linux/highmem.h>
38 #include <linux/scatterlist.h>
40 #include "iscsi_iser.h"
42 #define ISER_KMALLOC_THRESHOLD 0x20000 /* 128K - kmalloc limit */
45 * iser_start_rdma_unaligned_sg
47 static int iser_start_rdma_unaligned_sg(struct iscsi_iser_task *iser_task,
48 enum iser_data_dir cmd_dir)
51 struct ib_device *dev;
53 struct iser_data_buf *data = &iser_task->data[cmd_dir];
54 unsigned long cmd_data_len = data->data_len;
56 if (cmd_data_len > ISER_KMALLOC_THRESHOLD)
57 mem = (void *)__get_free_pages(GFP_ATOMIC,
58 ilog2(roundup_pow_of_two(cmd_data_len)) - PAGE_SHIFT);
60 mem = kmalloc(cmd_data_len, GFP_ATOMIC);
63 iser_err("Failed to allocate mem size %d %d for copying sglist\n",
64 data->size,(int)cmd_data_len);
68 if (cmd_dir == ISER_DIR_OUT) {
69 /* copy the unaligned sg the buffer which is used for RDMA */
70 struct scatterlist *sgl = (struct scatterlist *)data->buf;
71 struct scatterlist *sg;
76 for_each_sg(sgl, sg, data->size, i) {
77 from = kmap_atomic(sg_page(sg));
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;
91 iser_task->data_copy[cmd_dir].copy_buf = mem;
93 dev = iser_task->iser_conn->ib_conn->device->ib_device;
94 dma_nents = ib_dma_map_sg(dev,
95 &iser_task->data_copy[cmd_dir].sg_single,
97 (cmd_dir == ISER_DIR_OUT) ?
98 DMA_TO_DEVICE : DMA_FROM_DEVICE);
99 BUG_ON(dma_nents == 0);
101 iser_task->data_copy[cmd_dir].dma_nents = dma_nents;
106 * iser_finalize_rdma_unaligned_sg
108 void iser_finalize_rdma_unaligned_sg(struct iscsi_iser_task *iser_task,
109 enum iser_data_dir cmd_dir)
111 struct ib_device *dev;
112 struct iser_data_buf *mem_copy;
113 unsigned long cmd_data_len;
115 dev = iser_task->iser_conn->ib_conn->device->ib_device;
116 mem_copy = &iser_task->data_copy[cmd_dir];
118 ib_dma_unmap_sg(dev, &mem_copy->sg_single, 1,
119 (cmd_dir == ISER_DIR_OUT) ?
120 DMA_TO_DEVICE : DMA_FROM_DEVICE);
122 if (cmd_dir == ISER_DIR_IN) {
124 struct scatterlist *sgl, *sg;
125 unsigned char *p, *to;
126 unsigned int sg_size;
129 /* copy back read RDMA to unaligned sg */
130 mem = mem_copy->copy_buf;
132 sgl = (struct scatterlist *)iser_task->data[ISER_DIR_IN].buf;
133 sg_size = iser_task->data[ISER_DIR_IN].size;
136 for_each_sg(sgl, sg, sg_size, i) {
137 to = kmap_atomic(sg_page(sg));
138 memcpy(to + sg->offset,
146 cmd_data_len = iser_task->data[cmd_dir].data_len;
148 if (cmd_data_len > ISER_KMALLOC_THRESHOLD)
149 free_pages((unsigned long)mem_copy->copy_buf,
150 ilog2(roundup_pow_of_two(cmd_data_len)) - PAGE_SHIFT);
152 kfree(mem_copy->copy_buf);
154 mem_copy->copy_buf = NULL;
157 #define IS_4K_ALIGNED(addr) ((((unsigned long)addr) & ~MASK_4K) == 0)
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).
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.
172 static int iser_sg_to_page_vec(struct iser_data_buf *data,
173 struct ib_device *ibdev, u64 *pages,
174 int *offset, int *data_size)
176 struct scatterlist *sg, *sgl = (struct scatterlist *)data->buf;
177 u64 start_addr, end_addr, page, chunk_start = 0;
178 unsigned long total_sz = 0;
179 unsigned int dma_len;
180 int i, new_chunk, cur_page, last_ent = data->dma_nents - 1;
182 /* compute the offset of first element */
183 *offset = (u64) sgl[0].offset & ~MASK_4K;
187 for_each_sg(sgl, sg, data->dma_nents, i) {
188 start_addr = ib_sg_dma_address(ibdev, sg);
190 chunk_start = start_addr;
191 dma_len = ib_sg_dma_len(ibdev, sg);
192 end_addr = start_addr + dma_len;
195 /* collect page fragments until aligned or end of SG list */
196 if (!IS_4K_ALIGNED(end_addr) && i < last_ent) {
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;
207 pages[cur_page++] = page;
209 } while (page < end_addr);
212 *data_size = total_sz;
213 iser_dbg("page_vec->data_size:%d cur_page %d\n",
214 *data_size, cur_page);
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.
225 static int iser_data_buf_aligned_len(struct iser_data_buf *data,
226 struct ib_device *ibdev)
228 struct scatterlist *sgl, *sg, *next_sg = NULL;
229 u64 start_addr, end_addr;
230 int i, ret_len, start_check = 0;
232 if (data->dma_nents == 1)
235 sgl = (struct scatterlist *)data->buf;
236 start_addr = ib_sg_dma_address(ibdev, sgl);
238 for_each_sg(sgl, sg, data->dma_nents, i) {
239 if (start_check && !IS_4K_ALIGNED(start_addr))
242 next_sg = sg_next(sg);
246 end_addr = start_addr + ib_sg_dma_len(ibdev, sg);
247 start_addr = ib_sg_dma_address(ibdev, next_sg);
249 if (end_addr == start_addr) {
255 if (!IS_4K_ALIGNED(end_addr))
258 ret_len = (next_sg) ? i : i+1;
259 iser_dbg("Found %d aligned entries out of %d in sg:0x%p\n",
260 ret_len, data->dma_nents, data);
264 static void iser_data_buf_dump(struct iser_data_buf *data,
265 struct ib_device *ibdev)
267 struct scatterlist *sgl = (struct scatterlist *)data->buf;
268 struct scatterlist *sg;
271 for_each_sg(sgl, sg, data->dma_nents, i)
272 iser_dbg("sg[%d] dma_addr:0x%lX page:0x%p "
273 "off:0x%x sz:0x%x dma_len:0x%x\n",
274 i, (unsigned long)ib_sg_dma_address(ibdev, sg),
275 sg_page(sg), sg->offset,
276 sg->length, ib_sg_dma_len(ibdev, sg));
279 static void iser_dump_page_vec(struct iser_page_vec *page_vec)
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]);
289 static void iser_page_vec_build(struct iser_data_buf *data,
290 struct iser_page_vec *page_vec,
291 struct ib_device *ibdev)
293 int page_vec_len = 0;
295 page_vec->length = 0;
296 page_vec->offset = 0;
298 iser_dbg("Translating sg sz: %d\n", data->dma_nents);
299 page_vec_len = iser_sg_to_page_vec(data, ibdev, page_vec->pages,
301 &page_vec->data_size);
302 iser_dbg("sg len %d page_vec_len %d\n", data->dma_nents, page_vec_len);
304 page_vec->length = page_vec_len;
306 if (page_vec_len * SIZE_4K < page_vec->data_size) {
307 iser_err("page_vec too short to hold this SG\n");
308 iser_data_buf_dump(data, ibdev);
309 iser_dump_page_vec(page_vec);
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)
319 struct ib_device *dev;
321 iser_task->dir[iser_dir] = 1;
322 dev = iser_task->iser_conn->ib_conn->device->ib_device;
324 data->dma_nents = ib_dma_map_sg(dev, data->buf, data->size, dma_dir);
325 if (data->dma_nents == 0) {
326 iser_err("dma_map_sg failed!!!\n");
332 void iser_dma_unmap_task_data(struct iscsi_iser_task *iser_task)
334 struct ib_device *dev;
335 struct iser_data_buf *data;
337 dev = iser_task->iser_conn->ib_conn->device->ib_device;
339 if (iser_task->dir[ISER_DIR_IN]) {
340 data = &iser_task->data[ISER_DIR_IN];
341 ib_dma_unmap_sg(dev, data->buf, data->size, DMA_FROM_DEVICE);
344 if (iser_task->dir[ISER_DIR_OUT]) {
345 data = &iser_task->data[ISER_DIR_OUT];
346 ib_dma_unmap_sg(dev, data->buf, data->size, DMA_TO_DEVICE);
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,
355 struct iscsi_conn *iscsi_conn = iser_task->iser_conn->iscsi_conn;
356 struct iser_data_buf *mem = &iser_task->data[cmd_dir];
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);
362 if (iser_debug_level > 0)
363 iser_data_buf_dump(mem, ibdev);
365 /* unmap the command data before accessing it */
366 iser_dma_unmap_task_data(iser_task);
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)
377 * iser_reg_rdma_mem - Registers memory intended for RDMA,
378 * obtaining rkey and va
380 * returns 0 on success, errno code on failure
382 int iser_reg_rdma_mem(struct iscsi_iser_task *iser_task,
383 enum iser_data_dir cmd_dir)
385 struct iser_conn *ib_conn = iser_task->iser_conn->ib_conn;
386 struct iser_device *device = ib_conn->device;
387 struct ib_device *ibdev = device->ib_device;
388 struct iser_data_buf *mem = &iser_task->data[cmd_dir];
389 struct iser_regd_buf *regd_buf;
393 struct scatterlist *sg;
395 regd_buf = &iser_task->rdma_regd[cmd_dir];
397 aligned_len = iser_data_buf_aligned_len(mem, ibdev);
398 if (aligned_len != mem->dma_nents ||
399 (!ib_conn->fmr_pool && mem->dma_nents > 1)) {
400 err = fall_to_bounce_buf(iser_task, ibdev,
401 cmd_dir, aligned_len);
403 iser_err("failed to allocate bounce buffer\n");
406 mem = &iser_task->data_copy[cmd_dir];
409 /* if there a single dma entry, FMR is not needed */
410 if (mem->dma_nents == 1) {
411 sg = (struct scatterlist *)mem->buf;
413 regd_buf->reg.lkey = device->mr->lkey;
414 regd_buf->reg.rkey = device->mr->rkey;
415 regd_buf->reg.len = ib_sg_dma_len(ibdev, &sg[0]);
416 regd_buf->reg.va = ib_sg_dma_address(ibdev, &sg[0]);
417 regd_buf->reg.is_fmr = 0;
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 */
426 iser_page_vec_build(mem, ib_conn->page_vec, ibdev);
427 err = iser_reg_page_vec(ib_conn, ib_conn->page_vec, ®d_buf->reg);
428 if (err && err != -EAGAIN) {
429 iser_data_buf_dump(mem, ibdev);
430 iser_err("mem->dma_nents = %d (dlength = 0x%x)\n",
432 ntoh24(iser_task->desc.iscsi_header.dlength));
433 iser_err("page_vec: data_size = 0x%x, length = %d, offset = 0x%x\n",
434 ib_conn->page_vec->data_size, ib_conn->page_vec->length,
435 ib_conn->page_vec->offset);
436 for (i=0 ; i<ib_conn->page_vec->length ; i++)
437 iser_err("page_vec[%d] = 0x%llx\n", i,
438 (unsigned long long) ib_conn->page_vec->pages[i]);