iwch_mem.c 4.7 KB

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