mr.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. /*
  2. * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2007, 2008 Mellanox Technologies. All rights reserved.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. */
  33. #include <linux/slab.h>
  34. #include <rdma/ib_user_verbs.h>
  35. #include "mlx4_ib.h"
  36. static u32 convert_access(int acc)
  37. {
  38. return (acc & IB_ACCESS_REMOTE_ATOMIC ? MLX4_PERM_ATOMIC : 0) |
  39. (acc & IB_ACCESS_REMOTE_WRITE ? MLX4_PERM_REMOTE_WRITE : 0) |
  40. (acc & IB_ACCESS_REMOTE_READ ? MLX4_PERM_REMOTE_READ : 0) |
  41. (acc & IB_ACCESS_LOCAL_WRITE ? MLX4_PERM_LOCAL_WRITE : 0) |
  42. (acc & IB_ACCESS_MW_BIND ? MLX4_PERM_BIND_MW : 0) |
  43. MLX4_PERM_LOCAL_READ;
  44. }
  45. static enum mlx4_mw_type to_mlx4_type(enum ib_mw_type type)
  46. {
  47. switch (type) {
  48. case IB_MW_TYPE_1: return MLX4_MW_TYPE_1;
  49. case IB_MW_TYPE_2: return MLX4_MW_TYPE_2;
  50. default: return -1;
  51. }
  52. }
  53. struct ib_mr *mlx4_ib_get_dma_mr(struct ib_pd *pd, int acc)
  54. {
  55. struct mlx4_ib_mr *mr;
  56. int err;
  57. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  58. if (!mr)
  59. return ERR_PTR(-ENOMEM);
  60. err = mlx4_mr_alloc(to_mdev(pd->device)->dev, to_mpd(pd)->pdn, 0,
  61. ~0ull, convert_access(acc), 0, 0, &mr->mmr);
  62. if (err)
  63. goto err_free;
  64. err = mlx4_mr_enable(to_mdev(pd->device)->dev, &mr->mmr);
  65. if (err)
  66. goto err_mr;
  67. mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
  68. mr->umem = NULL;
  69. return &mr->ibmr;
  70. err_mr:
  71. (void) mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
  72. err_free:
  73. kfree(mr);
  74. return ERR_PTR(err);
  75. }
  76. enum {
  77. MLX4_MAX_MTT_SHIFT = 31
  78. };
  79. static int mlx4_ib_umem_write_mtt_block(struct mlx4_ib_dev *dev,
  80. struct mlx4_mtt *mtt,
  81. u64 mtt_size, u64 mtt_shift, u64 len,
  82. u64 cur_start_addr, u64 *pages,
  83. int *start_index, int *npages)
  84. {
  85. u64 cur_end_addr = cur_start_addr + len;
  86. u64 cur_end_addr_aligned = 0;
  87. u64 mtt_entries;
  88. int err = 0;
  89. int k;
  90. len += (cur_start_addr & (mtt_size - 1ULL));
  91. cur_end_addr_aligned = round_up(cur_end_addr, mtt_size);
  92. len += (cur_end_addr_aligned - cur_end_addr);
  93. if (len & (mtt_size - 1ULL)) {
  94. pr_warn("write_block: len %llx is not aligned to mtt_size %llx\n",
  95. len, mtt_size);
  96. return -EINVAL;
  97. }
  98. mtt_entries = (len >> mtt_shift);
  99. /*
  100. * Align the MTT start address to the mtt_size.
  101. * Required to handle cases when the MR starts in the middle of an MTT
  102. * record. Was not required in old code since the physical addresses
  103. * provided by the dma subsystem were page aligned, which was also the
  104. * MTT size.
  105. */
  106. cur_start_addr = round_down(cur_start_addr, mtt_size);
  107. /* A new block is started ... */
  108. for (k = 0; k < mtt_entries; ++k) {
  109. pages[*npages] = cur_start_addr + (mtt_size * k);
  110. (*npages)++;
  111. /*
  112. * Be friendly to mlx4_write_mtt() and pass it chunks of
  113. * appropriate size.
  114. */
  115. if (*npages == PAGE_SIZE / sizeof(u64)) {
  116. err = mlx4_write_mtt(dev->dev, mtt, *start_index,
  117. *npages, pages);
  118. if (err)
  119. return err;
  120. (*start_index) += *npages;
  121. *npages = 0;
  122. }
  123. }
  124. return 0;
  125. }
  126. static inline u64 alignment_of(u64 ptr)
  127. {
  128. return ilog2(ptr & (~(ptr - 1)));
  129. }
  130. static int mlx4_ib_umem_calc_block_mtt(u64 next_block_start,
  131. u64 current_block_end,
  132. u64 block_shift)
  133. {
  134. /* Check whether the alignment of the new block is aligned as well as
  135. * the previous block.
  136. * Block address must start with zeros till size of entity_size.
  137. */
  138. if ((next_block_start & ((1ULL << block_shift) - 1ULL)) != 0)
  139. /*
  140. * It is not as well aligned as the previous block-reduce the
  141. * mtt size accordingly. Here we take the last right bit which
  142. * is 1.
  143. */
  144. block_shift = alignment_of(next_block_start);
  145. /*
  146. * Check whether the alignment of the end of previous block - is it
  147. * aligned as well as the start of the block
  148. */
  149. if (((current_block_end) & ((1ULL << block_shift) - 1ULL)) != 0)
  150. /*
  151. * It is not as well aligned as the start of the block -
  152. * reduce the mtt size accordingly.
  153. */
  154. block_shift = alignment_of(current_block_end);
  155. return block_shift;
  156. }
  157. int mlx4_ib_umem_write_mtt(struct mlx4_ib_dev *dev, struct mlx4_mtt *mtt,
  158. struct ib_umem *umem)
  159. {
  160. u64 *pages;
  161. u64 len = 0;
  162. int err = 0;
  163. u64 mtt_size;
  164. u64 cur_start_addr = 0;
  165. u64 mtt_shift;
  166. int start_index = 0;
  167. int npages = 0;
  168. struct scatterlist *sg;
  169. int i;
  170. pages = (u64 *) __get_free_page(GFP_KERNEL);
  171. if (!pages)
  172. return -ENOMEM;
  173. mtt_shift = mtt->page_shift;
  174. mtt_size = 1ULL << mtt_shift;
  175. for_each_sgtable_dma_sg(&umem->sgt_append.sgt, sg, i) {
  176. if (cur_start_addr + len == sg_dma_address(sg)) {
  177. /* still the same block */
  178. len += sg_dma_len(sg);
  179. continue;
  180. }
  181. /*
  182. * A new block is started ...
  183. * If len is malaligned, write an extra mtt entry to cover the
  184. * misaligned area (round up the division)
  185. */
  186. err = mlx4_ib_umem_write_mtt_block(dev, mtt, mtt_size,
  187. mtt_shift, len,
  188. cur_start_addr,
  189. pages, &start_index,
  190. &npages);
  191. if (err)
  192. goto out;
  193. cur_start_addr = sg_dma_address(sg);
  194. len = sg_dma_len(sg);
  195. }
  196. /* Handle the last block */
  197. if (len > 0) {
  198. /*
  199. * If len is malaligned, write an extra mtt entry to cover
  200. * the misaligned area (round up the division)
  201. */
  202. err = mlx4_ib_umem_write_mtt_block(dev, mtt, mtt_size,
  203. mtt_shift, len,
  204. cur_start_addr, pages,
  205. &start_index, &npages);
  206. if (err)
  207. goto out;
  208. }
  209. if (npages)
  210. err = mlx4_write_mtt(dev->dev, mtt, start_index, npages, pages);
  211. out:
  212. free_page((unsigned long) pages);
  213. return err;
  214. }
  215. /*
  216. * Calculate optimal mtt size based on contiguous pages.
  217. * Function will return also the number of pages that are not aligned to the
  218. * calculated mtt_size to be added to total number of pages. For that we should
  219. * check the first chunk length & last chunk length and if not aligned to
  220. * mtt_size we should increment the non_aligned_pages number. All chunks in the
  221. * middle already handled as part of mtt shift calculation for both their start
  222. * & end addresses.
  223. */
  224. int mlx4_ib_umem_calc_optimal_mtt_size(struct ib_umem *umem, u64 start_va,
  225. int *num_of_mtts)
  226. {
  227. u64 block_shift = MLX4_MAX_MTT_SHIFT;
  228. u64 min_shift = PAGE_SHIFT;
  229. u64 last_block_aligned_end = 0;
  230. u64 current_block_start = 0;
  231. u64 first_block_start = 0;
  232. u64 current_block_len = 0;
  233. u64 last_block_end = 0;
  234. struct scatterlist *sg;
  235. u64 current_block_end;
  236. u64 misalignment_bits;
  237. u64 next_block_start;
  238. u64 total_len = 0;
  239. int i;
  240. *num_of_mtts = ib_umem_num_dma_blocks(umem, PAGE_SIZE);
  241. for_each_sgtable_dma_sg(&umem->sgt_append.sgt, sg, i) {
  242. /*
  243. * Initialization - save the first chunk start as the
  244. * current_block_start - block means contiguous pages.
  245. */
  246. if (current_block_len == 0 && current_block_start == 0) {
  247. current_block_start = sg_dma_address(sg);
  248. first_block_start = current_block_start;
  249. /*
  250. * Find the bits that are different between the physical
  251. * address and the virtual address for the start of the
  252. * MR.
  253. * umem_get aligned the start_va to a page boundary.
  254. * Therefore, we need to align the start va to the same
  255. * boundary.
  256. * misalignment_bits is needed to handle the case of a
  257. * single memory region. In this case, the rest of the
  258. * logic will not reduce the block size. If we use a
  259. * block size which is bigger than the alignment of the
  260. * misalignment bits, we might use the virtual page
  261. * number instead of the physical page number, resulting
  262. * in access to the wrong data.
  263. */
  264. misalignment_bits =
  265. (start_va & (~(((u64)(PAGE_SIZE)) - 1ULL))) ^
  266. current_block_start;
  267. block_shift = min(alignment_of(misalignment_bits),
  268. block_shift);
  269. }
  270. /*
  271. * Go over the scatter entries and check if they continue the
  272. * previous scatter entry.
  273. */
  274. next_block_start = sg_dma_address(sg);
  275. current_block_end = current_block_start + current_block_len;
  276. /* If we have a split (non-contig.) between two blocks */
  277. if (current_block_end != next_block_start) {
  278. block_shift = mlx4_ib_umem_calc_block_mtt
  279. (next_block_start,
  280. current_block_end,
  281. block_shift);
  282. /*
  283. * If we reached the minimum shift for 4k page we stop
  284. * the loop.
  285. */
  286. if (block_shift <= min_shift)
  287. goto end;
  288. /*
  289. * If not saved yet we are in first block - we save the
  290. * length of first block to calculate the
  291. * non_aligned_pages number at the end.
  292. */
  293. total_len += current_block_len;
  294. /* Start a new block */
  295. current_block_start = next_block_start;
  296. current_block_len = sg_dma_len(sg);
  297. continue;
  298. }
  299. /* The scatter entry is another part of the current block,
  300. * increase the block size.
  301. * An entry in the scatter can be larger than 4k (page) as of
  302. * dma mapping which merge some blocks together.
  303. */
  304. current_block_len += sg_dma_len(sg);
  305. }
  306. /* Account for the last block in the total len */
  307. total_len += current_block_len;
  308. /* Add to the first block the misalignment that it suffers from. */
  309. total_len += (first_block_start & ((1ULL << block_shift) - 1ULL));
  310. last_block_end = current_block_start + current_block_len;
  311. last_block_aligned_end = round_up(last_block_end, 1ULL << block_shift);
  312. total_len += (last_block_aligned_end - last_block_end);
  313. if (total_len & ((1ULL << block_shift) - 1ULL))
  314. pr_warn("misaligned total length detected (%llu, %llu)!",
  315. total_len, block_shift);
  316. *num_of_mtts = total_len >> block_shift;
  317. end:
  318. if (block_shift < min_shift) {
  319. /*
  320. * If shift is less than the min we set a warning and return the
  321. * min shift.
  322. */
  323. pr_warn("umem_calc_optimal_mtt_size - unexpected shift %lld\n", block_shift);
  324. block_shift = min_shift;
  325. }
  326. return block_shift;
  327. }
  328. static struct ib_umem *mlx4_get_umem_mr(struct ib_device *device, u64 start,
  329. u64 length, int access_flags)
  330. {
  331. /*
  332. * Force registering the memory as writable if the underlying pages
  333. * are writable. This is so rereg can change the access permissions
  334. * from readable to writable without having to run through ib_umem_get
  335. * again
  336. */
  337. if (!ib_access_writable(access_flags)) {
  338. unsigned long untagged_start = untagged_addr(start);
  339. struct vm_area_struct *vma;
  340. mmap_read_lock(current->mm);
  341. /*
  342. * FIXME: Ideally this would iterate over all the vmas that
  343. * cover the memory, but for now it requires a single vma to
  344. * entirely cover the MR to support RO mappings.
  345. */
  346. vma = find_vma(current->mm, untagged_start);
  347. if (vma && vma->vm_end >= untagged_start + length &&
  348. vma->vm_start <= untagged_start) {
  349. if (vma->vm_flags & VM_WRITE)
  350. access_flags |= IB_ACCESS_LOCAL_WRITE;
  351. } else {
  352. access_flags |= IB_ACCESS_LOCAL_WRITE;
  353. }
  354. mmap_read_unlock(current->mm);
  355. }
  356. return ib_umem_get(device, start, length, access_flags);
  357. }
  358. struct ib_mr *mlx4_ib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  359. u64 virt_addr, int access_flags,
  360. struct ib_udata *udata)
  361. {
  362. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  363. struct mlx4_ib_mr *mr;
  364. int shift;
  365. int err;
  366. int n;
  367. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  368. if (!mr)
  369. return ERR_PTR(-ENOMEM);
  370. mr->umem = mlx4_get_umem_mr(pd->device, start, length, access_flags);
  371. if (IS_ERR(mr->umem)) {
  372. err = PTR_ERR(mr->umem);
  373. goto err_free;
  374. }
  375. shift = mlx4_ib_umem_calc_optimal_mtt_size(mr->umem, start, &n);
  376. err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, virt_addr, length,
  377. convert_access(access_flags), n, shift, &mr->mmr);
  378. if (err)
  379. goto err_umem;
  380. err = mlx4_ib_umem_write_mtt(dev, &mr->mmr.mtt, mr->umem);
  381. if (err)
  382. goto err_mr;
  383. err = mlx4_mr_enable(dev->dev, &mr->mmr);
  384. if (err)
  385. goto err_mr;
  386. mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
  387. mr->ibmr.page_size = 1U << shift;
  388. return &mr->ibmr;
  389. err_mr:
  390. (void) mlx4_mr_free(to_mdev(pd->device)->dev, &mr->mmr);
  391. err_umem:
  392. ib_umem_release(mr->umem);
  393. err_free:
  394. kfree(mr);
  395. return ERR_PTR(err);
  396. }
  397. struct ib_mr *mlx4_ib_rereg_user_mr(struct ib_mr *mr, int flags, u64 start,
  398. u64 length, u64 virt_addr,
  399. int mr_access_flags, struct ib_pd *pd,
  400. struct ib_udata *udata)
  401. {
  402. struct mlx4_ib_dev *dev = to_mdev(mr->device);
  403. struct mlx4_ib_mr *mmr = to_mmr(mr);
  404. struct mlx4_mpt_entry *mpt_entry;
  405. struct mlx4_mpt_entry **pmpt_entry = &mpt_entry;
  406. int err;
  407. /* Since we synchronize this call and mlx4_ib_dereg_mr via uverbs,
  408. * we assume that the calls can't run concurrently. Otherwise, a
  409. * race exists.
  410. */
  411. err = mlx4_mr_hw_get_mpt(dev->dev, &mmr->mmr, &pmpt_entry);
  412. if (err)
  413. return ERR_PTR(err);
  414. if (flags & IB_MR_REREG_PD) {
  415. err = mlx4_mr_hw_change_pd(dev->dev, *pmpt_entry,
  416. to_mpd(pd)->pdn);
  417. if (err)
  418. goto release_mpt_entry;
  419. }
  420. if (flags & IB_MR_REREG_ACCESS) {
  421. if (ib_access_writable(mr_access_flags) &&
  422. !mmr->umem->writable) {
  423. err = -EPERM;
  424. goto release_mpt_entry;
  425. }
  426. err = mlx4_mr_hw_change_access(dev->dev, *pmpt_entry,
  427. convert_access(mr_access_flags));
  428. if (err)
  429. goto release_mpt_entry;
  430. }
  431. if (flags & IB_MR_REREG_TRANS) {
  432. int shift;
  433. int n;
  434. mlx4_mr_rereg_mem_cleanup(dev->dev, &mmr->mmr);
  435. ib_umem_release(mmr->umem);
  436. mmr->umem = mlx4_get_umem_mr(mr->device, start, length,
  437. mr_access_flags);
  438. if (IS_ERR(mmr->umem)) {
  439. err = PTR_ERR(mmr->umem);
  440. /* Prevent mlx4_ib_dereg_mr from free'ing invalid pointer */
  441. mmr->umem = NULL;
  442. goto release_mpt_entry;
  443. }
  444. n = ib_umem_num_dma_blocks(mmr->umem, PAGE_SIZE);
  445. shift = PAGE_SHIFT;
  446. err = mlx4_mr_rereg_mem_write(dev->dev, &mmr->mmr,
  447. virt_addr, length, n, shift,
  448. *pmpt_entry);
  449. if (err) {
  450. ib_umem_release(mmr->umem);
  451. goto release_mpt_entry;
  452. }
  453. mmr->mmr.iova = virt_addr;
  454. mmr->mmr.size = length;
  455. err = mlx4_ib_umem_write_mtt(dev, &mmr->mmr.mtt, mmr->umem);
  456. if (err) {
  457. mlx4_mr_rereg_mem_cleanup(dev->dev, &mmr->mmr);
  458. ib_umem_release(mmr->umem);
  459. goto release_mpt_entry;
  460. }
  461. }
  462. /* If we couldn't transfer the MR to the HCA, just remember to
  463. * return a failure. But dereg_mr will free the resources.
  464. */
  465. err = mlx4_mr_hw_write_mpt(dev->dev, &mmr->mmr, pmpt_entry);
  466. if (!err && flags & IB_MR_REREG_ACCESS)
  467. mmr->mmr.access = mr_access_flags;
  468. release_mpt_entry:
  469. mlx4_mr_hw_put_mpt(dev->dev, pmpt_entry);
  470. if (err)
  471. return ERR_PTR(err);
  472. return NULL;
  473. }
  474. static int
  475. mlx4_alloc_priv_pages(struct ib_device *device,
  476. struct mlx4_ib_mr *mr,
  477. int max_pages)
  478. {
  479. int ret;
  480. /* Ensure that size is aligned to DMA cacheline
  481. * requirements.
  482. * max_pages is limited to MLX4_MAX_FAST_REG_PAGES
  483. * so page_map_size will never cross PAGE_SIZE.
  484. */
  485. mr->page_map_size = roundup(max_pages * sizeof(u64),
  486. MLX4_MR_PAGES_ALIGN);
  487. /* Prevent cross page boundary allocation. */
  488. mr->pages = (__be64 *)get_zeroed_page(GFP_KERNEL);
  489. if (!mr->pages)
  490. return -ENOMEM;
  491. mr->page_map = dma_map_single(device->dev.parent, mr->pages,
  492. mr->page_map_size, DMA_TO_DEVICE);
  493. if (dma_mapping_error(device->dev.parent, mr->page_map)) {
  494. ret = -ENOMEM;
  495. goto err;
  496. }
  497. return 0;
  498. err:
  499. free_page((unsigned long)mr->pages);
  500. return ret;
  501. }
  502. static void
  503. mlx4_free_priv_pages(struct mlx4_ib_mr *mr)
  504. {
  505. if (mr->pages) {
  506. struct ib_device *device = mr->ibmr.device;
  507. dma_unmap_single(device->dev.parent, mr->page_map,
  508. mr->page_map_size, DMA_TO_DEVICE);
  509. free_page((unsigned long)mr->pages);
  510. mr->pages = NULL;
  511. }
  512. }
  513. int mlx4_ib_dereg_mr(struct ib_mr *ibmr, struct ib_udata *udata)
  514. {
  515. struct mlx4_ib_mr *mr = to_mmr(ibmr);
  516. int ret;
  517. mlx4_free_priv_pages(mr);
  518. ret = mlx4_mr_free(to_mdev(ibmr->device)->dev, &mr->mmr);
  519. if (ret)
  520. return ret;
  521. if (mr->umem)
  522. ib_umem_release(mr->umem);
  523. kfree(mr);
  524. return 0;
  525. }
  526. int mlx4_ib_alloc_mw(struct ib_mw *ibmw, struct ib_udata *udata)
  527. {
  528. struct mlx4_ib_dev *dev = to_mdev(ibmw->device);
  529. struct mlx4_ib_mw *mw = to_mmw(ibmw);
  530. int err;
  531. err = mlx4_mw_alloc(dev->dev, to_mpd(ibmw->pd)->pdn,
  532. to_mlx4_type(ibmw->type), &mw->mmw);
  533. if (err)
  534. return err;
  535. err = mlx4_mw_enable(dev->dev, &mw->mmw);
  536. if (err)
  537. goto err_mw;
  538. ibmw->rkey = mw->mmw.key;
  539. return 0;
  540. err_mw:
  541. mlx4_mw_free(dev->dev, &mw->mmw);
  542. return err;
  543. }
  544. int mlx4_ib_dealloc_mw(struct ib_mw *ibmw)
  545. {
  546. struct mlx4_ib_mw *mw = to_mmw(ibmw);
  547. mlx4_mw_free(to_mdev(ibmw->device)->dev, &mw->mmw);
  548. return 0;
  549. }
  550. struct ib_mr *mlx4_ib_alloc_mr(struct ib_pd *pd, enum ib_mr_type mr_type,
  551. u32 max_num_sg)
  552. {
  553. struct mlx4_ib_dev *dev = to_mdev(pd->device);
  554. struct mlx4_ib_mr *mr;
  555. int err;
  556. if (mr_type != IB_MR_TYPE_MEM_REG ||
  557. max_num_sg > MLX4_MAX_FAST_REG_PAGES)
  558. return ERR_PTR(-EINVAL);
  559. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  560. if (!mr)
  561. return ERR_PTR(-ENOMEM);
  562. err = mlx4_mr_alloc(dev->dev, to_mpd(pd)->pdn, 0, 0, 0,
  563. max_num_sg, 0, &mr->mmr);
  564. if (err)
  565. goto err_free;
  566. err = mlx4_alloc_priv_pages(pd->device, mr, max_num_sg);
  567. if (err)
  568. goto err_free_mr;
  569. mr->max_pages = max_num_sg;
  570. err = mlx4_mr_enable(dev->dev, &mr->mmr);
  571. if (err)
  572. goto err_free_pl;
  573. mr->ibmr.rkey = mr->ibmr.lkey = mr->mmr.key;
  574. mr->umem = NULL;
  575. return &mr->ibmr;
  576. err_free_pl:
  577. mr->ibmr.device = pd->device;
  578. mlx4_free_priv_pages(mr);
  579. err_free_mr:
  580. (void) mlx4_mr_free(dev->dev, &mr->mmr);
  581. err_free:
  582. kfree(mr);
  583. return ERR_PTR(err);
  584. }
  585. static int mlx4_set_page(struct ib_mr *ibmr, u64 addr)
  586. {
  587. struct mlx4_ib_mr *mr = to_mmr(ibmr);
  588. if (unlikely(mr->npages == mr->max_pages))
  589. return -ENOMEM;
  590. mr->pages[mr->npages++] = cpu_to_be64(addr | MLX4_MTT_FLAG_PRESENT);
  591. return 0;
  592. }
  593. int mlx4_ib_map_mr_sg(struct ib_mr *ibmr, struct scatterlist *sg, int sg_nents,
  594. unsigned int *sg_offset)
  595. {
  596. struct mlx4_ib_mr *mr = to_mmr(ibmr);
  597. int rc;
  598. mr->npages = 0;
  599. ib_dma_sync_single_for_cpu(ibmr->device, mr->page_map,
  600. mr->page_map_size, DMA_TO_DEVICE);
  601. rc = ib_sg_to_pages(ibmr, sg, sg_nents, sg_offset, mlx4_set_page);
  602. ib_dma_sync_single_for_device(ibmr->device, mr->page_map,
  603. mr->page_map_size, DMA_TO_DEVICE);
  604. return rc;
  605. }