iser_memory.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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.is_fmr)
  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.len = regd_buf->data_size;
  87. regd_buf->reg.va = dma_addr;
  88. regd_buf->reg.is_fmr = 0;
  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 & ~MASK_4K;
  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 & ~MASK_4K);
  217. end_aligned = !(last_addr & ~MASK_4K);
  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 & ~MASK_4K);
  224. }
  225. /* handle the 1st page in the 1st DMA element */
  226. if (cur_page == 0) {
  227. page = first_addr & MASK_4K;
  228. page_vec->pages[cur_page] = page;
  229. cur_page++;
  230. page += SIZE_4K;
  231. } else
  232. page = first_addr;
  233. for (; page < last_addr; page += SIZE_4K) {
  234. page_vec->pages[cur_page] = page;
  235. cur_page++;
  236. }
  237. }
  238. page_vec->data_size = total_sz;
  239. iser_dbg("page_vec->data_size:%d cur_page %d\n", page_vec->data_size,cur_page);
  240. return cur_page;
  241. }
  242. #define IS_4K_ALIGNED(addr) ((((unsigned long)addr) & ~MASK_4K) == 0)
  243. /**
  244. * iser_data_buf_aligned_len - Tries to determine the maximal correctly aligned
  245. * for RDMA sub-list of a scatter-gather list of memory buffers, and returns
  246. * the number of entries which are aligned correctly. Supports the case where
  247. * consecutive SG elements are actually fragments of the same physcial page.
  248. */
  249. static unsigned int iser_data_buf_aligned_len(struct iser_data_buf *data)
  250. {
  251. struct scatterlist *sg;
  252. dma_addr_t end_addr, next_addr;
  253. int i, cnt;
  254. unsigned int ret_len = 0;
  255. sg = (struct scatterlist *)data->buf;
  256. for (cnt = 0, i = 0; i < data->dma_nents; i++, cnt++) {
  257. /* iser_dbg("Checking sg iobuf [%d]: phys=0x%08lX "
  258. "offset: %ld sz: %ld\n", i,
  259. (unsigned long)page_to_phys(sg[i].page),
  260. (unsigned long)sg[i].offset,
  261. (unsigned long)sg[i].length); */
  262. end_addr = sg_dma_address(&sg[i]) +
  263. sg_dma_len(&sg[i]);
  264. /* iser_dbg("Checking sg iobuf end address "
  265. "0x%08lX\n", end_addr); */
  266. if (i + 1 < data->dma_nents) {
  267. next_addr = sg_dma_address(&sg[i+1]);
  268. /* are i, i+1 fragments of the same page? */
  269. if (end_addr == next_addr)
  270. continue;
  271. else if (!IS_4K_ALIGNED(end_addr)) {
  272. ret_len = cnt + 1;
  273. break;
  274. }
  275. }
  276. }
  277. if (i == data->dma_nents)
  278. ret_len = cnt; /* loop ended */
  279. iser_dbg("Found %d aligned entries out of %d in sg:0x%p\n",
  280. ret_len, data->dma_nents, data);
  281. return ret_len;
  282. }
  283. static void iser_data_buf_dump(struct iser_data_buf *data)
  284. {
  285. struct scatterlist *sg = (struct scatterlist *)data->buf;
  286. int i;
  287. for (i = 0; i < data->dma_nents; i++)
  288. iser_err("sg[%d] dma_addr:0x%lX page:0x%p "
  289. "off:0x%x sz:0x%x dma_len:0x%x\n",
  290. i, (unsigned long)sg_dma_address(&sg[i]),
  291. sg[i].page, sg[i].offset,
  292. sg[i].length,sg_dma_len(&sg[i]));
  293. }
  294. static void iser_dump_page_vec(struct iser_page_vec *page_vec)
  295. {
  296. int i;
  297. iser_err("page vec length %d data size %d\n",
  298. page_vec->length, page_vec->data_size);
  299. for (i = 0; i < page_vec->length; i++)
  300. iser_err("%d %lx\n",i,(unsigned long)page_vec->pages[i]);
  301. }
  302. static void iser_page_vec_build(struct iser_data_buf *data,
  303. struct iser_page_vec *page_vec)
  304. {
  305. int page_vec_len = 0;
  306. page_vec->length = 0;
  307. page_vec->offset = 0;
  308. iser_dbg("Translating sg sz: %d\n", data->dma_nents);
  309. page_vec_len = iser_sg_to_page_vec(data,page_vec);
  310. iser_dbg("sg len %d page_vec_len %d\n", data->dma_nents,page_vec_len);
  311. page_vec->length = page_vec_len;
  312. if (page_vec_len * SIZE_4K < page_vec->data_size) {
  313. iser_err("page_vec too short to hold this SG\n");
  314. iser_data_buf_dump(data);
  315. iser_dump_page_vec(page_vec);
  316. BUG();
  317. }
  318. }
  319. int iser_dma_map_task_data(struct iscsi_iser_cmd_task *iser_ctask,
  320. struct iser_data_buf *data,
  321. enum iser_data_dir iser_dir,
  322. enum dma_data_direction dma_dir)
  323. {
  324. struct device *dma_device;
  325. iser_ctask->dir[iser_dir] = 1;
  326. dma_device =
  327. iser_ctask->iser_conn->ib_conn->device->ib_device->dma_device;
  328. data->dma_nents = dma_map_sg(dma_device, data->buf, data->size, dma_dir);
  329. if (data->dma_nents == 0) {
  330. iser_err("dma_map_sg failed!!!\n");
  331. return -EINVAL;
  332. }
  333. return 0;
  334. }
  335. void iser_dma_unmap_task_data(struct iscsi_iser_cmd_task *iser_ctask)
  336. {
  337. struct device *dma_device;
  338. struct iser_data_buf *data;
  339. dma_device =
  340. iser_ctask->iser_conn->ib_conn->device->ib_device->dma_device;
  341. if (iser_ctask->dir[ISER_DIR_IN]) {
  342. data = &iser_ctask->data[ISER_DIR_IN];
  343. dma_unmap_sg(dma_device, data->buf, data->size, DMA_FROM_DEVICE);
  344. }
  345. if (iser_ctask->dir[ISER_DIR_OUT]) {
  346. data = &iser_ctask->data[ISER_DIR_OUT];
  347. dma_unmap_sg(dma_device, data->buf, data->size, DMA_TO_DEVICE);
  348. }
  349. }
  350. /**
  351. * iser_reg_rdma_mem - Registers memory intended for RDMA,
  352. * obtaining rkey and va
  353. *
  354. * returns 0 on success, errno code on failure
  355. */
  356. int iser_reg_rdma_mem(struct iscsi_iser_cmd_task *iser_ctask,
  357. enum iser_data_dir cmd_dir)
  358. {
  359. struct iser_conn *ib_conn = iser_ctask->iser_conn->ib_conn;
  360. struct iser_device *device = ib_conn->device;
  361. struct iser_data_buf *mem = &iser_ctask->data[cmd_dir];
  362. struct iser_regd_buf *regd_buf;
  363. int aligned_len;
  364. int err;
  365. int i;
  366. struct scatterlist *sg;
  367. regd_buf = &iser_ctask->rdma_regd[cmd_dir];
  368. aligned_len = iser_data_buf_aligned_len(mem);
  369. if (aligned_len != mem->dma_nents) {
  370. iser_err("rdma alignment violation %d/%d aligned\n",
  371. aligned_len, mem->size);
  372. iser_data_buf_dump(mem);
  373. /* unmap the command data before accessing it */
  374. iser_dma_unmap_task_data(iser_ctask);
  375. /* allocate copy buf, if we are writing, copy the */
  376. /* unaligned scatterlist, dma map the copy */
  377. if (iser_start_rdma_unaligned_sg(iser_ctask, cmd_dir) != 0)
  378. return -ENOMEM;
  379. mem = &iser_ctask->data_copy[cmd_dir];
  380. }
  381. /* if there a single dma entry, FMR is not needed */
  382. if (mem->dma_nents == 1) {
  383. sg = (struct scatterlist *)mem->buf;
  384. regd_buf->reg.lkey = device->mr->lkey;
  385. regd_buf->reg.rkey = device->mr->rkey;
  386. regd_buf->reg.len = sg_dma_len(&sg[0]);
  387. regd_buf->reg.va = sg_dma_address(&sg[0]);
  388. regd_buf->reg.is_fmr = 0;
  389. iser_dbg("PHYSICAL Mem.register: lkey: 0x%08X rkey: 0x%08X "
  390. "va: 0x%08lX sz: %ld]\n",
  391. (unsigned int)regd_buf->reg.lkey,
  392. (unsigned int)regd_buf->reg.rkey,
  393. (unsigned long)regd_buf->reg.va,
  394. (unsigned long)regd_buf->reg.len);
  395. } else { /* use FMR for multiple dma entries */
  396. iser_page_vec_build(mem, ib_conn->page_vec);
  397. err = iser_reg_page_vec(ib_conn, ib_conn->page_vec, &regd_buf->reg);
  398. if (err) {
  399. iser_data_buf_dump(mem);
  400. iser_err("mem->dma_nents = %d (dlength = 0x%x)\n", mem->dma_nents,
  401. ntoh24(iser_ctask->desc.iscsi_header.dlength));
  402. iser_err("page_vec: data_size = 0x%x, length = %d, offset = 0x%x\n",
  403. ib_conn->page_vec->data_size, ib_conn->page_vec->length,
  404. ib_conn->page_vec->offset);
  405. for (i=0 ; i<ib_conn->page_vec->length ; i++)
  406. iser_err("page_vec[%d] = 0x%llx\n", i,
  407. (unsigned long long) ib_conn->page_vec->pages[i]);
  408. return err;
  409. }
  410. }
  411. /* take a reference on this regd buf such that it will not be released *
  412. * (eg in send dto completion) before we get the scsi response */
  413. atomic_inc(&regd_buf->ref_count);
  414. return 0;
  415. }