iser_memory.c 14 KB

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