iser_memory.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. #include "iscsi_iser.h"
  42. #define ISER_KMALLOC_THRESHOLD 0x20000 /* 128K - kmalloc limit */
  43. /**
  44. * Decrements the reference count for the
  45. * registered buffer & releases it
  46. *
  47. * returns 0 if released, 1 if deferred
  48. */
  49. int iser_regd_buff_release(struct iser_regd_buf *regd_buf)
  50. {
  51. struct device *dma_device;
  52. if ((atomic_read(&regd_buf->ref_count) == 0) ||
  53. atomic_dec_and_test(&regd_buf->ref_count)) {
  54. /* if we used the dma mr, unreg is just NOP */
  55. if (regd_buf->reg.rkey != 0)
  56. iser_unreg_mem(&regd_buf->reg);
  57. if (regd_buf->dma_addr) {
  58. dma_device = regd_buf->device->ib_device->dma_device;
  59. dma_unmap_single(dma_device,
  60. regd_buf->dma_addr,
  61. regd_buf->data_size,
  62. regd_buf->direction);
  63. }
  64. /* else this regd buf is associated with task which we */
  65. /* dma_unmap_single/sg later */
  66. return 0;
  67. } else {
  68. iser_dbg("Release deferred, regd.buff: 0x%p\n", regd_buf);
  69. return 1;
  70. }
  71. }
  72. /**
  73. * iser_reg_single - fills registered buffer descriptor with
  74. * registration information
  75. */
  76. void iser_reg_single(struct iser_device *device,
  77. struct iser_regd_buf *regd_buf,
  78. enum dma_data_direction direction)
  79. {
  80. dma_addr_t dma_addr;
  81. dma_addr = dma_map_single(device->ib_device->dma_device,
  82. regd_buf->virt_addr,
  83. regd_buf->data_size, direction);
  84. BUG_ON(dma_mapping_error(dma_addr));
  85. regd_buf->reg.lkey = device->mr->lkey;
  86. regd_buf->reg.rkey = 0; /* indicate there's no need to unreg */
  87. regd_buf->reg.len = regd_buf->data_size;
  88. regd_buf->reg.va = dma_addr;
  89. regd_buf->dma_addr = dma_addr;
  90. regd_buf->direction = direction;
  91. }
  92. /**
  93. * iser_start_rdma_unaligned_sg
  94. */
  95. int iser_start_rdma_unaligned_sg(struct iscsi_iser_cmd_task *iser_ctask,
  96. enum iser_data_dir cmd_dir)
  97. {
  98. int dma_nents;
  99. struct device *dma_device;
  100. char *mem = NULL;
  101. struct iser_data_buf *data = &iser_ctask->data[cmd_dir];
  102. unsigned long cmd_data_len = data->data_len;
  103. if (cmd_data_len > ISER_KMALLOC_THRESHOLD)
  104. mem = (void *)__get_free_pages(GFP_NOIO,
  105. long_log2(roundup_pow_of_two(cmd_data_len)) - PAGE_SHIFT);
  106. else
  107. mem = kmalloc(cmd_data_len, GFP_NOIO);
  108. if (mem == NULL) {
  109. iser_err("Failed to allocate mem size %d %d for copying sglist\n",
  110. data->size,(int)cmd_data_len);
  111. return -ENOMEM;
  112. }
  113. if (cmd_dir == ISER_DIR_OUT) {
  114. /* copy the unaligned sg the buffer which is used for RDMA */
  115. struct scatterlist *sg = (struct scatterlist *)data->buf;
  116. int i;
  117. char *p, *from;
  118. for (p = mem, i = 0; i < data->size; i++) {
  119. from = kmap_atomic(sg[i].page, KM_USER0);
  120. memcpy(p,
  121. from + sg[i].offset,
  122. sg[i].length);
  123. kunmap_atomic(from, KM_USER0);
  124. p += sg[i].length;
  125. }
  126. }
  127. sg_init_one(&iser_ctask->data_copy[cmd_dir].sg_single, mem, cmd_data_len);
  128. iser_ctask->data_copy[cmd_dir].buf =
  129. &iser_ctask->data_copy[cmd_dir].sg_single;
  130. iser_ctask->data_copy[cmd_dir].size = 1;
  131. iser_ctask->data_copy[cmd_dir].copy_buf = mem;
  132. dma_device = iser_ctask->iser_conn->ib_conn->device->ib_device->dma_device;
  133. if (cmd_dir == ISER_DIR_OUT)
  134. dma_nents = dma_map_sg(dma_device,
  135. &iser_ctask->data_copy[cmd_dir].sg_single,
  136. 1, DMA_TO_DEVICE);
  137. else
  138. dma_nents = dma_map_sg(dma_device,
  139. &iser_ctask->data_copy[cmd_dir].sg_single,
  140. 1, DMA_FROM_DEVICE);
  141. BUG_ON(dma_nents == 0);
  142. iser_ctask->data_copy[cmd_dir].dma_nents = dma_nents;
  143. return 0;
  144. }
  145. /**
  146. * iser_finalize_rdma_unaligned_sg
  147. */
  148. void iser_finalize_rdma_unaligned_sg(struct iscsi_iser_cmd_task *iser_ctask,
  149. enum iser_data_dir cmd_dir)
  150. {
  151. struct device *dma_device;
  152. struct iser_data_buf *mem_copy;
  153. unsigned long cmd_data_len;
  154. dma_device = iser_ctask->iser_conn->ib_conn->device->ib_device->dma_device;
  155. mem_copy = &iser_ctask->data_copy[cmd_dir];
  156. if (cmd_dir == ISER_DIR_OUT)
  157. dma_unmap_sg(dma_device, &mem_copy->sg_single, 1,
  158. DMA_TO_DEVICE);
  159. else
  160. dma_unmap_sg(dma_device, &mem_copy->sg_single, 1,
  161. DMA_FROM_DEVICE);
  162. if (cmd_dir == ISER_DIR_IN) {
  163. char *mem;
  164. struct scatterlist *sg;
  165. unsigned char *p, *to;
  166. unsigned int sg_size;
  167. int i;
  168. /* copy back read RDMA to unaligned sg */
  169. mem = mem_copy->copy_buf;
  170. sg = (struct scatterlist *)iser_ctask->data[ISER_DIR_IN].buf;
  171. sg_size = iser_ctask->data[ISER_DIR_IN].size;
  172. for (p = mem, i = 0; i < sg_size; i++){
  173. to = kmap_atomic(sg[i].page, KM_SOFTIRQ0);
  174. memcpy(to + sg[i].offset,
  175. p,
  176. sg[i].length);
  177. kunmap_atomic(to, KM_SOFTIRQ0);
  178. p += sg[i].length;
  179. }
  180. }
  181. cmd_data_len = iser_ctask->data[cmd_dir].data_len;
  182. if (cmd_data_len > ISER_KMALLOC_THRESHOLD)
  183. free_pages((unsigned long)mem_copy->copy_buf,
  184. long_log2(roundup_pow_of_two(cmd_data_len)) - PAGE_SHIFT);
  185. else
  186. kfree(mem_copy->copy_buf);
  187. mem_copy->copy_buf = NULL;
  188. }
  189. /**
  190. * iser_sg_to_page_vec - Translates scatterlist entries to physical addresses
  191. * and returns the length of resulting physical address array (may be less than
  192. * the original due to possible compaction).
  193. *
  194. * we build a "page vec" under the assumption that the SG meets the RDMA
  195. * alignment requirements. Other then the first and last SG elements, all
  196. * the "internal" elements can be compacted into a list whose elements are
  197. * dma addresses of physical pages. The code supports also the weird case
  198. * where --few fragments of the same page-- are present in the SG as
  199. * consecutive elements. Also, it handles one entry SG.
  200. */
  201. static int iser_sg_to_page_vec(struct iser_data_buf *data,
  202. struct iser_page_vec *page_vec)
  203. {
  204. struct scatterlist *sg = (struct scatterlist *)data->buf;
  205. dma_addr_t first_addr, last_addr, page;
  206. int start_aligned, end_aligned;
  207. unsigned int cur_page = 0;
  208. unsigned long total_sz = 0;
  209. int i;
  210. /* compute the offset of first element */
  211. page_vec->offset = (u64) sg[0].offset;
  212. for (i = 0; i < data->dma_nents; i++) {
  213. total_sz += sg_dma_len(&sg[i]);
  214. first_addr = sg_dma_address(&sg[i]);
  215. last_addr = first_addr + sg_dma_len(&sg[i]);
  216. start_aligned = !(first_addr & ~PAGE_MASK);
  217. end_aligned = !(last_addr & ~PAGE_MASK);
  218. /* continue to collect page fragments till aligned or SG ends */
  219. while (!end_aligned && (i + 1 < data->dma_nents)) {
  220. i++;
  221. total_sz += sg_dma_len(&sg[i]);
  222. last_addr = sg_dma_address(&sg[i]) + sg_dma_len(&sg[i]);
  223. end_aligned = !(last_addr & ~PAGE_MASK);
  224. }
  225. first_addr = first_addr & PAGE_MASK;
  226. for (page = first_addr; page < last_addr; page += PAGE_SIZE)
  227. page_vec->pages[cur_page++] = page;
  228. }
  229. page_vec->data_size = total_sz;
  230. iser_dbg("page_vec->data_size:%d cur_page %d\n", page_vec->data_size,cur_page);
  231. return cur_page;
  232. }
  233. #define MASK_4K ((1UL << 12) - 1) /* 0xFFF */
  234. #define IS_4K_ALIGNED(addr) ((((unsigned long)addr) & MASK_4K) == 0)
  235. /**
  236. * iser_data_buf_aligned_len - Tries to determine the maximal correctly aligned
  237. * for RDMA sub-list of a scatter-gather list of memory buffers, and returns
  238. * the number of entries which are aligned correctly. Supports the case where
  239. * consecutive SG elements are actually fragments of the same physcial page.
  240. */
  241. static unsigned int iser_data_buf_aligned_len(struct iser_data_buf *data)
  242. {
  243. struct scatterlist *sg;
  244. dma_addr_t end_addr, next_addr;
  245. int i, cnt;
  246. unsigned int ret_len = 0;
  247. sg = (struct scatterlist *)data->buf;
  248. for (cnt = 0, i = 0; i < data->dma_nents; i++, cnt++) {
  249. /* iser_dbg("Checking sg iobuf [%d]: phys=0x%08lX "
  250. "offset: %ld sz: %ld\n", i,
  251. (unsigned long)page_to_phys(sg[i].page),
  252. (unsigned long)sg[i].offset,
  253. (unsigned long)sg[i].length); */
  254. end_addr = sg_dma_address(&sg[i]) +
  255. sg_dma_len(&sg[i]);
  256. /* iser_dbg("Checking sg iobuf end address "
  257. "0x%08lX\n", end_addr); */
  258. if (i + 1 < data->dma_nents) {
  259. next_addr = sg_dma_address(&sg[i+1]);
  260. /* are i, i+1 fragments of the same page? */
  261. if (end_addr == next_addr)
  262. continue;
  263. else if (!IS_4K_ALIGNED(end_addr)) {
  264. ret_len = cnt + 1;
  265. break;
  266. }
  267. }
  268. }
  269. if (i == data->dma_nents)
  270. ret_len = cnt; /* loop ended */
  271. iser_dbg("Found %d aligned entries out of %d in sg:0x%p\n",
  272. ret_len, data->dma_nents, data);
  273. return ret_len;
  274. }
  275. static void iser_data_buf_dump(struct iser_data_buf *data)
  276. {
  277. struct scatterlist *sg = (struct scatterlist *)data->buf;
  278. int i;
  279. for (i = 0; i < data->size; i++)
  280. iser_err("sg[%d] dma_addr:0x%lX page:0x%p "
  281. "off:%d sz:%d dma_len:%d\n",
  282. i, (unsigned long)sg_dma_address(&sg[i]),
  283. sg[i].page, sg[i].offset,
  284. sg[i].length,sg_dma_len(&sg[i]));
  285. }
  286. static void iser_dump_page_vec(struct iser_page_vec *page_vec)
  287. {
  288. int i;
  289. iser_err("page vec length %d data size %d\n",
  290. page_vec->length, page_vec->data_size);
  291. for (i = 0; i < page_vec->length; i++)
  292. iser_err("%d %lx\n",i,(unsigned long)page_vec->pages[i]);
  293. }
  294. static void iser_page_vec_build(struct iser_data_buf *data,
  295. struct iser_page_vec *page_vec)
  296. {
  297. int page_vec_len = 0;
  298. page_vec->length = 0;
  299. page_vec->offset = 0;
  300. iser_dbg("Translating sg sz: %d\n", data->dma_nents);
  301. page_vec_len = iser_sg_to_page_vec(data,page_vec);
  302. iser_dbg("sg len %d page_vec_len %d\n", data->dma_nents,page_vec_len);
  303. page_vec->length = page_vec_len;
  304. if (page_vec_len * PAGE_SIZE < page_vec->data_size) {
  305. iser_err("page_vec too short to hold this SG\n");
  306. iser_data_buf_dump(data);
  307. iser_dump_page_vec(page_vec);
  308. BUG();
  309. }
  310. }
  311. /**
  312. * iser_reg_rdma_mem - Registers memory intended for RDMA,
  313. * obtaining rkey and va
  314. *
  315. * returns 0 on success, errno code on failure
  316. */
  317. int iser_reg_rdma_mem(struct iscsi_iser_cmd_task *iser_ctask,
  318. enum iser_data_dir cmd_dir)
  319. {
  320. struct iser_conn *ib_conn = iser_ctask->iser_conn->ib_conn;
  321. struct iser_data_buf *mem = &iser_ctask->data[cmd_dir];
  322. struct iser_regd_buf *regd_buf;
  323. int aligned_len;
  324. int err;
  325. regd_buf = &iser_ctask->rdma_regd[cmd_dir];
  326. aligned_len = iser_data_buf_aligned_len(mem);
  327. if (aligned_len != mem->size) {
  328. iser_err("rdma alignment violation %d/%d aligned\n",
  329. aligned_len, mem->size);
  330. iser_data_buf_dump(mem);
  331. /* allocate copy buf, if we are writing, copy the */
  332. /* unaligned scatterlist, dma map the copy */
  333. if (iser_start_rdma_unaligned_sg(iser_ctask, cmd_dir) != 0)
  334. return -ENOMEM;
  335. mem = &iser_ctask->data_copy[cmd_dir];
  336. }
  337. iser_page_vec_build(mem, ib_conn->page_vec);
  338. err = iser_reg_page_vec(ib_conn, ib_conn->page_vec, &regd_buf->reg);
  339. if (err)
  340. return err;
  341. /* take a reference on this regd buf such that it will not be released *
  342. * (eg in send dto completion) before we get the scsi response */
  343. atomic_inc(&regd_buf->ref_count);
  344. return 0;
  345. }