iser_memory.c 12 KB

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