iwch_mem.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright (c) 2006 Chelsio, 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 <asm/byteorder.h>
  33. #include <rdma/iw_cm.h>
  34. #include <rdma/ib_verbs.h>
  35. #include "cxio_hal.h"
  36. #include "cxio_resource.h"
  37. #include "iwch.h"
  38. #include "iwch_provider.h"
  39. static void iwch_finish_mem_reg(struct iwch_mr *mhp, u32 stag)
  40. {
  41. u32 mmid;
  42. mhp->attr.state = 1;
  43. mhp->attr.stag = stag;
  44. mmid = stag >> 8;
  45. mhp->ibmr.rkey = mhp->ibmr.lkey = stag;
  46. insert_handle(mhp->rhp, &mhp->rhp->mmidr, mhp, mmid);
  47. PDBG("%s mmid 0x%x mhp %p\n", __func__, mmid, mhp);
  48. }
  49. int iwch_register_mem(struct iwch_dev *rhp, struct iwch_pd *php,
  50. struct iwch_mr *mhp, int shift)
  51. {
  52. u32 stag;
  53. if (cxio_register_phys_mem(&rhp->rdev,
  54. &stag, mhp->attr.pdid,
  55. mhp->attr.perms,
  56. mhp->attr.zbva,
  57. mhp->attr.va_fbo,
  58. mhp->attr.len,
  59. shift - 12,
  60. mhp->attr.pbl_size, mhp->attr.pbl_addr))
  61. return -ENOMEM;
  62. iwch_finish_mem_reg(mhp, stag);
  63. return 0;
  64. }
  65. int iwch_reregister_mem(struct iwch_dev *rhp, struct iwch_pd *php,
  66. struct iwch_mr *mhp,
  67. int shift,
  68. int npages)
  69. {
  70. u32 stag;
  71. /* We could support this... */
  72. if (npages > mhp->attr.pbl_size)
  73. return -ENOMEM;
  74. stag = mhp->attr.stag;
  75. if (cxio_reregister_phys_mem(&rhp->rdev,
  76. &stag, mhp->attr.pdid,
  77. mhp->attr.perms,
  78. mhp->attr.zbva,
  79. mhp->attr.va_fbo,
  80. mhp->attr.len,
  81. shift - 12,
  82. mhp->attr.pbl_size, mhp->attr.pbl_addr))
  83. return -ENOMEM;
  84. iwch_finish_mem_reg(mhp, stag);
  85. return 0;
  86. }
  87. int iwch_alloc_pbl(struct iwch_mr *mhp, int npages)
  88. {
  89. mhp->attr.pbl_addr = cxio_hal_pblpool_alloc(&mhp->rhp->rdev,
  90. npages << 3);
  91. if (!mhp->attr.pbl_addr)
  92. return -ENOMEM;
  93. mhp->attr.pbl_size = npages;
  94. return 0;
  95. }
  96. void iwch_free_pbl(struct iwch_mr *mhp)
  97. {
  98. cxio_hal_pblpool_free(&mhp->rhp->rdev, mhp->attr.pbl_addr,
  99. mhp->attr.pbl_size << 3);
  100. }
  101. int iwch_write_pbl(struct iwch_mr *mhp, __be64 *pages, int npages, int offset)
  102. {
  103. return cxio_write_pbl(&mhp->rhp->rdev, pages,
  104. mhp->attr.pbl_addr + (offset << 3), npages);
  105. }
  106. int build_phys_page_list(struct ib_phys_buf *buffer_list,
  107. int num_phys_buf,
  108. u64 *iova_start,
  109. u64 *total_size,
  110. int *npages,
  111. int *shift,
  112. __be64 **page_list)
  113. {
  114. u64 mask;
  115. int i, j, n;
  116. mask = 0;
  117. *total_size = 0;
  118. for (i = 0; i < num_phys_buf; ++i) {
  119. if (i != 0 && buffer_list[i].addr & ~PAGE_MASK)
  120. return -EINVAL;
  121. if (i != 0 && i != num_phys_buf - 1 &&
  122. (buffer_list[i].size & ~PAGE_MASK))
  123. return -EINVAL;
  124. *total_size += buffer_list[i].size;
  125. if (i > 0)
  126. mask |= buffer_list[i].addr;
  127. else
  128. mask |= buffer_list[i].addr & PAGE_MASK;
  129. if (i != num_phys_buf - 1)
  130. mask |= buffer_list[i].addr + buffer_list[i].size;
  131. else
  132. mask |= (buffer_list[i].addr + buffer_list[i].size +
  133. PAGE_SIZE - 1) & PAGE_MASK;
  134. }
  135. if (*total_size > 0xFFFFFFFFULL)
  136. return -ENOMEM;
  137. /* Find largest page shift we can use to cover buffers */
  138. for (*shift = PAGE_SHIFT; *shift < 27; ++(*shift))
  139. if ((1ULL << *shift) & mask)
  140. break;
  141. buffer_list[0].size += buffer_list[0].addr & ((1ULL << *shift) - 1);
  142. buffer_list[0].addr &= ~0ull << *shift;
  143. *npages = 0;
  144. for (i = 0; i < num_phys_buf; ++i)
  145. *npages += (buffer_list[i].size +
  146. (1ULL << *shift) - 1) >> *shift;
  147. if (!*npages)
  148. return -EINVAL;
  149. *page_list = kmalloc(sizeof(u64) * *npages, GFP_KERNEL);
  150. if (!*page_list)
  151. return -ENOMEM;
  152. n = 0;
  153. for (i = 0; i < num_phys_buf; ++i)
  154. for (j = 0;
  155. j < (buffer_list[i].size + (1ULL << *shift) - 1) >> *shift;
  156. ++j)
  157. (*page_list)[n++] = cpu_to_be64(buffer_list[i].addr +
  158. ((u64) j << *shift));
  159. PDBG("%s va 0x%llx mask 0x%llx shift %d len %lld pbl_size %d\n",
  160. __func__, (unsigned long long) *iova_start,
  161. (unsigned long long) mask, *shift, (unsigned long long) *total_size,
  162. *npages);
  163. return 0;
  164. }