Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[linux-2.6] / drivers / infiniband / ulp / iser / iser_memory.c
1 /*
2  * Copyright (c) 2004, 2005, 2006 Voltaire, 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  * $Id: iser_memory.c 6964 2006-05-07 11:11:43Z ogerlitz $
33  */
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/mm.h>
38 #include <asm/io.h>
39 #include <asm/scatterlist.h>
40 #include <linux/scatterlist.h>
41
42 #include "iscsi_iser.h"
43
44 #define ISER_KMALLOC_THRESHOLD 0x20000 /* 128K - kmalloc limit */
45
46 /**
47  * Decrements the reference count for the
48  * registered buffer & releases it
49  *
50  * returns 0 if released, 1 if deferred
51  */
52 int iser_regd_buff_release(struct iser_regd_buf *regd_buf)
53 {
54         struct device *dma_device;
55
56         if ((atomic_read(&regd_buf->ref_count) == 0) ||
57             atomic_dec_and_test(&regd_buf->ref_count)) {
58                 /* if we used the dma mr, unreg is just NOP */
59                 if (regd_buf->reg.is_fmr)
60                         iser_unreg_mem(&regd_buf->reg);
61
62                 if (regd_buf->dma_addr) {
63                         dma_device = regd_buf->device->ib_device->dma_device;
64                         dma_unmap_single(dma_device,
65                                          regd_buf->dma_addr,
66                                          regd_buf->data_size,
67                                          regd_buf->direction);
68                 }
69                 /* else this regd buf is associated with task which we */
70                 /* dma_unmap_single/sg later */
71                 return 0;
72         } else {
73                 iser_dbg("Release deferred, regd.buff: 0x%p\n", regd_buf);
74                 return 1;
75         }
76 }
77
78 /**
79  * iser_reg_single - fills registered buffer descriptor with
80  *                   registration information
81  */
82 void iser_reg_single(struct iser_device *device,
83                      struct iser_regd_buf *regd_buf,
84                      enum dma_data_direction direction)
85 {
86         dma_addr_t dma_addr;
87
88         dma_addr  = dma_map_single(device->ib_device->dma_device,
89                                    regd_buf->virt_addr,
90                                    regd_buf->data_size, direction);
91         BUG_ON(dma_mapping_error(dma_addr));
92
93         regd_buf->reg.lkey = device->mr->lkey;
94         regd_buf->reg.len  = regd_buf->data_size;
95         regd_buf->reg.va   = dma_addr;
96         regd_buf->reg.is_fmr = 0;
97
98         regd_buf->dma_addr  = dma_addr;
99         regd_buf->direction = direction;
100 }
101
102 /**
103  * iser_start_rdma_unaligned_sg
104  */
105 int iser_start_rdma_unaligned_sg(struct iscsi_iser_cmd_task  *iser_ctask,
106                                  enum iser_data_dir cmd_dir)
107 {
108         int dma_nents;
109         struct device *dma_device;
110         char *mem = NULL;
111         struct iser_data_buf *data = &iser_ctask->data[cmd_dir];
112         unsigned long  cmd_data_len = data->data_len;
113
114         if (cmd_data_len > ISER_KMALLOC_THRESHOLD)
115                 mem = (void *)__get_free_pages(GFP_NOIO,
116                       long_log2(roundup_pow_of_two(cmd_data_len)) - PAGE_SHIFT);
117         else
118                 mem = kmalloc(cmd_data_len, GFP_NOIO);
119
120         if (mem == NULL) {
121                 iser_err("Failed to allocate mem size %d %d for copying sglist\n",
122                          data->size,(int)cmd_data_len);
123                 return -ENOMEM;
124         }
125
126         if (cmd_dir == ISER_DIR_OUT) {
127                 /* copy the unaligned sg the buffer which is used for RDMA */
128                 struct scatterlist *sg = (struct scatterlist *)data->buf;
129                 int i;
130                 char *p, *from;
131
132                 for (p = mem, i = 0; i < data->size; i++) {
133                         from = kmap_atomic(sg[i].page, KM_USER0);
134                         memcpy(p,
135                                from + sg[i].offset,
136                                sg[i].length);
137                         kunmap_atomic(from, KM_USER0);
138                         p += sg[i].length;
139                 }
140         }
141
142         sg_init_one(&iser_ctask->data_copy[cmd_dir].sg_single, mem, cmd_data_len);
143         iser_ctask->data_copy[cmd_dir].buf  =
144                 &iser_ctask->data_copy[cmd_dir].sg_single;
145         iser_ctask->data_copy[cmd_dir].size = 1;
146
147         iser_ctask->data_copy[cmd_dir].copy_buf  = mem;
148
149         dma_device = iser_ctask->iser_conn->ib_conn->device->ib_device->dma_device;
150
151         if (cmd_dir == ISER_DIR_OUT)
152                 dma_nents = dma_map_sg(dma_device,
153                                        &iser_ctask->data_copy[cmd_dir].sg_single,
154                                        1, DMA_TO_DEVICE);
155         else
156                 dma_nents = dma_map_sg(dma_device,
157                                        &iser_ctask->data_copy[cmd_dir].sg_single,
158                                        1, DMA_FROM_DEVICE);
159
160         BUG_ON(dma_nents == 0);
161
162         iser_ctask->data_copy[cmd_dir].dma_nents = dma_nents;
163         return 0;
164 }
165
166 /**
167  * iser_finalize_rdma_unaligned_sg
168  */
169 void iser_finalize_rdma_unaligned_sg(struct iscsi_iser_cmd_task *iser_ctask,
170                                      enum iser_data_dir         cmd_dir)
171 {
172         struct device *dma_device;
173         struct iser_data_buf *mem_copy;
174         unsigned long  cmd_data_len;
175
176         dma_device = iser_ctask->iser_conn->ib_conn->device->ib_device->dma_device;
177         mem_copy   = &iser_ctask->data_copy[cmd_dir];
178
179         if (cmd_dir == ISER_DIR_OUT)
180                 dma_unmap_sg(dma_device, &mem_copy->sg_single, 1,
181                              DMA_TO_DEVICE);
182         else
183                 dma_unmap_sg(dma_device, &mem_copy->sg_single, 1,
184                              DMA_FROM_DEVICE);
185
186         if (cmd_dir == ISER_DIR_IN) {
187                 char *mem;
188                 struct scatterlist *sg;
189                 unsigned char *p, *to;
190                 unsigned int sg_size;
191                 int i;
192
193                 /* copy back read RDMA to unaligned sg */
194                 mem     = mem_copy->copy_buf;
195
196                 sg      = (struct scatterlist *)iser_ctask->data[ISER_DIR_IN].buf;
197                 sg_size = iser_ctask->data[ISER_DIR_IN].size;
198
199                 for (p = mem, i = 0; i < sg_size; i++){
200                         to = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
201                         memcpy(to + sg[i].offset,
202                                p,
203                                sg[i].length);
204                         kunmap_atomic(to, KM_SOFTIRQ0);
205                         p += sg[i].length;
206                 }
207         }
208
209         cmd_data_len = iser_ctask->data[cmd_dir].data_len;
210
211         if (cmd_data_len > ISER_KMALLOC_THRESHOLD)
212                 free_pages((unsigned long)mem_copy->copy_buf,
213                            long_log2(roundup_pow_of_two(cmd_data_len)) - PAGE_SHIFT);
214         else
215                 kfree(mem_copy->copy_buf);
216
217         mem_copy->copy_buf = NULL;
218 }
219
220 /**
221  * iser_sg_to_page_vec - Translates scatterlist entries to physical addresses
222  * and returns the length of resulting physical address array (may be less than
223  * the original due to possible compaction).
224  *
225  * we build a "page vec" under the assumption that the SG meets the RDMA
226  * alignment requirements. Other then the first and last SG elements, all
227  * the "internal" elements can be compacted into a list whose elements are
228  * dma addresses of physical pages. The code supports also the weird case
229  * where --few fragments of the same page-- are present in the SG as
230  * consecutive elements. Also, it handles one entry SG.
231  */
232 static int iser_sg_to_page_vec(struct iser_data_buf *data,
233                                struct iser_page_vec *page_vec)
234 {
235         struct scatterlist *sg = (struct scatterlist *)data->buf;
236         dma_addr_t first_addr, last_addr, page;
237         int start_aligned, end_aligned;
238         unsigned int cur_page = 0;
239         unsigned long total_sz = 0;
240         int i;
241
242         /* compute the offset of first element */
243         page_vec->offset = (u64) sg[0].offset & ~MASK_4K;
244
245         for (i = 0; i < data->dma_nents; i++) {
246                 total_sz += sg_dma_len(&sg[i]);
247
248                 first_addr = sg_dma_address(&sg[i]);
249                 last_addr  = first_addr + sg_dma_len(&sg[i]);
250
251                 start_aligned = !(first_addr & ~MASK_4K);
252                 end_aligned   = !(last_addr  & ~MASK_4K);
253
254                 /* continue to collect page fragments till aligned or SG ends */
255                 while (!end_aligned && (i + 1 < data->dma_nents)) {
256                         i++;
257                         total_sz += sg_dma_len(&sg[i]);
258                         last_addr = sg_dma_address(&sg[i]) + sg_dma_len(&sg[i]);
259                         end_aligned = !(last_addr  & ~MASK_4K);
260                 }
261
262                 /* handle the 1st page in the 1st DMA element */
263                 if (cur_page == 0) {
264                         page = first_addr & MASK_4K;
265                         page_vec->pages[cur_page] = page;
266                         cur_page++;
267                         page += SIZE_4K;
268                 } else
269                         page = first_addr;
270
271                 for (; page < last_addr; page += SIZE_4K) {
272                         page_vec->pages[cur_page] = page;
273                         cur_page++;
274                 }
275
276         }
277         page_vec->data_size = total_sz;
278         iser_dbg("page_vec->data_size:%d cur_page %d\n", page_vec->data_size,cur_page);
279         return cur_page;
280 }
281
282 #define IS_4K_ALIGNED(addr)     ((((unsigned long)addr) & ~MASK_4K) == 0)
283
284 /**
285  * iser_data_buf_aligned_len - Tries to determine the maximal correctly aligned
286  * for RDMA sub-list of a scatter-gather list of memory buffers, and  returns
287  * the number of entries which are aligned correctly. Supports the case where
288  * consecutive SG elements are actually fragments of the same physcial page.
289  */
290 static unsigned int iser_data_buf_aligned_len(struct iser_data_buf *data)
291 {
292         struct scatterlist *sg;
293         dma_addr_t end_addr, next_addr;
294         int i, cnt;
295         unsigned int ret_len = 0;
296
297         sg = (struct scatterlist *)data->buf;
298
299         for (cnt = 0, i = 0; i < data->dma_nents; i++, cnt++) {
300                 /* iser_dbg("Checking sg iobuf [%d]: phys=0x%08lX "
301                    "offset: %ld sz: %ld\n", i,
302                    (unsigned long)page_to_phys(sg[i].page),
303                    (unsigned long)sg[i].offset,
304                    (unsigned long)sg[i].length); */
305                 end_addr = sg_dma_address(&sg[i]) +
306                            sg_dma_len(&sg[i]);
307                 /* iser_dbg("Checking sg iobuf end address "
308                        "0x%08lX\n", end_addr); */
309                 if (i + 1 < data->dma_nents) {
310                         next_addr = sg_dma_address(&sg[i+1]);
311                         /* are i, i+1 fragments of the same page? */
312                         if (end_addr == next_addr)
313                                 continue;
314                         else if (!IS_4K_ALIGNED(end_addr)) {
315                                 ret_len = cnt + 1;
316                                 break;
317                         }
318                 }
319         }
320         if (i == data->dma_nents)
321                 ret_len = cnt;  /* loop ended */
322         iser_dbg("Found %d aligned entries out of %d in sg:0x%p\n",
323                  ret_len, data->dma_nents, data);
324         return ret_len;
325 }
326
327 static void iser_data_buf_dump(struct iser_data_buf *data)
328 {
329         struct scatterlist *sg = (struct scatterlist *)data->buf;
330         int i;
331
332         for (i = 0; i < data->dma_nents; i++)
333                 iser_err("sg[%d] dma_addr:0x%lX page:0x%p "
334                          "off:0x%x sz:0x%x dma_len:0x%x\n",
335                          i, (unsigned long)sg_dma_address(&sg[i]),
336                          sg[i].page, sg[i].offset,
337                          sg[i].length,sg_dma_len(&sg[i]));
338 }
339
340 static void iser_dump_page_vec(struct iser_page_vec *page_vec)
341 {
342         int i;
343
344         iser_err("page vec length %d data size %d\n",
345                  page_vec->length, page_vec->data_size);
346         for (i = 0; i < page_vec->length; i++)
347                 iser_err("%d %lx\n",i,(unsigned long)page_vec->pages[i]);
348 }
349
350 static void iser_page_vec_build(struct iser_data_buf *data,
351                                 struct iser_page_vec *page_vec)
352 {
353         int page_vec_len = 0;
354
355         page_vec->length = 0;
356         page_vec->offset = 0;
357
358         iser_dbg("Translating sg sz: %d\n", data->dma_nents);
359         page_vec_len = iser_sg_to_page_vec(data,page_vec);
360         iser_dbg("sg len %d page_vec_len %d\n", data->dma_nents,page_vec_len);
361
362         page_vec->length = page_vec_len;
363
364         if (page_vec_len * SIZE_4K < page_vec->data_size) {
365                 iser_err("page_vec too short to hold this SG\n");
366                 iser_data_buf_dump(data);
367                 iser_dump_page_vec(page_vec);
368                 BUG();
369         }
370 }
371
372 int iser_dma_map_task_data(struct iscsi_iser_cmd_task *iser_ctask,
373                             struct iser_data_buf       *data,
374                             enum   iser_data_dir       iser_dir,
375                             enum   dma_data_direction  dma_dir)
376 {
377         struct device *dma_device;
378
379         iser_ctask->dir[iser_dir] = 1;
380         dma_device =
381                 iser_ctask->iser_conn->ib_conn->device->ib_device->dma_device;
382
383         data->dma_nents = dma_map_sg(dma_device, data->buf, data->size, dma_dir);
384         if (data->dma_nents == 0) {
385                 iser_err("dma_map_sg failed!!!\n");
386                 return -EINVAL;
387         }
388         return 0;
389 }
390
391 void iser_dma_unmap_task_data(struct iscsi_iser_cmd_task *iser_ctask)
392 {
393         struct device  *dma_device;
394         struct iser_data_buf *data;
395
396         dma_device =
397                 iser_ctask->iser_conn->ib_conn->device->ib_device->dma_device;
398
399         if (iser_ctask->dir[ISER_DIR_IN]) {
400                 data = &iser_ctask->data[ISER_DIR_IN];
401                 dma_unmap_sg(dma_device, data->buf, data->size, DMA_FROM_DEVICE);
402         }
403
404         if (iser_ctask->dir[ISER_DIR_OUT]) {
405                 data = &iser_ctask->data[ISER_DIR_OUT];
406                 dma_unmap_sg(dma_device, data->buf, data->size, DMA_TO_DEVICE);
407         }
408 }
409
410 /**
411  * iser_reg_rdma_mem - Registers memory intended for RDMA,
412  * obtaining rkey and va
413  *
414  * returns 0 on success, errno code on failure
415  */
416 int iser_reg_rdma_mem(struct iscsi_iser_cmd_task *iser_ctask,
417                       enum   iser_data_dir        cmd_dir)
418 {
419         struct iser_conn     *ib_conn = iser_ctask->iser_conn->ib_conn;
420         struct iser_device   *device = ib_conn->device;
421         struct iser_data_buf *mem = &iser_ctask->data[cmd_dir];
422         struct iser_regd_buf *regd_buf;
423         int aligned_len;
424         int err;
425         int i;
426         struct scatterlist *sg;
427
428         regd_buf = &iser_ctask->rdma_regd[cmd_dir];
429
430         aligned_len = iser_data_buf_aligned_len(mem);
431         if (aligned_len != mem->dma_nents) {
432                 iser_err("rdma alignment violation %d/%d aligned\n",
433                          aligned_len, mem->size);
434                 iser_data_buf_dump(mem);
435
436                 /* unmap the command data before accessing it */
437                 iser_dma_unmap_task_data(iser_ctask);
438
439                 /* allocate copy buf, if we are writing, copy the */
440                 /* unaligned scatterlist, dma map the copy        */
441                 if (iser_start_rdma_unaligned_sg(iser_ctask, cmd_dir) != 0)
442                                 return -ENOMEM;
443                 mem = &iser_ctask->data_copy[cmd_dir];
444         }
445
446         /* if there a single dma entry, FMR is not needed */
447         if (mem->dma_nents == 1) {
448                 sg = (struct scatterlist *)mem->buf;
449
450                 regd_buf->reg.lkey = device->mr->lkey;
451                 regd_buf->reg.rkey = device->mr->rkey;
452                 regd_buf->reg.len  = sg_dma_len(&sg[0]);
453                 regd_buf->reg.va   = sg_dma_address(&sg[0]);
454                 regd_buf->reg.is_fmr = 0;
455
456                 iser_dbg("PHYSICAL Mem.register: lkey: 0x%08X rkey: 0x%08X  "
457                          "va: 0x%08lX sz: %ld]\n",
458                          (unsigned int)regd_buf->reg.lkey,
459                          (unsigned int)regd_buf->reg.rkey,
460                          (unsigned long)regd_buf->reg.va,
461                          (unsigned long)regd_buf->reg.len);
462         } else { /* use FMR for multiple dma entries */
463                 iser_page_vec_build(mem, ib_conn->page_vec);
464                 err = iser_reg_page_vec(ib_conn, ib_conn->page_vec, &regd_buf->reg);
465                 if (err) {
466                         iser_data_buf_dump(mem);
467                         iser_err("mem->dma_nents = %d (dlength = 0x%x)\n", mem->dma_nents,
468                                  ntoh24(iser_ctask->desc.iscsi_header.dlength));
469                         iser_err("page_vec: data_size = 0x%x, length = %d, offset = 0x%x\n",
470                                  ib_conn->page_vec->data_size, ib_conn->page_vec->length,
471                                  ib_conn->page_vec->offset);
472                         for (i=0 ; i<ib_conn->page_vec->length ; i++)
473                                 iser_err("page_vec[%d] = 0x%llx\n", i,
474                                          (unsigned long long) ib_conn->page_vec->pages[i]);
475                         return err;
476                 }
477         }
478
479         /* take a reference on this regd buf such that it will not be released *
480          * (eg in send dto completion) before we get the scsi response         */
481         atomic_inc(&regd_buf->ref_count);
482         return 0;
483 }