ipath_keys.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (c) 2006, 2007 QLogic Corporation. All rights reserved.
  3. * Copyright (c) 2005, 2006 PathScale, 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/io.h>
  34. #include "ipath_verbs.h"
  35. #include "ipath_kernel.h"
  36. /**
  37. * ipath_alloc_lkey - allocate an lkey
  38. * @rkt: lkey table in which to allocate the lkey
  39. * @mr: memory region that this lkey protects
  40. *
  41. * Returns 1 if successful, otherwise returns 0.
  42. */
  43. int ipath_alloc_lkey(struct ipath_lkey_table *rkt, struct ipath_mregion *mr)
  44. {
  45. unsigned long flags;
  46. u32 r;
  47. u32 n;
  48. int ret;
  49. spin_lock_irqsave(&rkt->lock, flags);
  50. /* Find the next available LKEY */
  51. r = n = rkt->next;
  52. for (;;) {
  53. if (rkt->table[r] == NULL)
  54. break;
  55. r = (r + 1) & (rkt->max - 1);
  56. if (r == n) {
  57. spin_unlock_irqrestore(&rkt->lock, flags);
  58. ipath_dbg("LKEY table full\n");
  59. ret = 0;
  60. goto bail;
  61. }
  62. }
  63. rkt->next = (r + 1) & (rkt->max - 1);
  64. /*
  65. * Make sure lkey is never zero which is reserved to indicate an
  66. * unrestricted LKEY.
  67. */
  68. rkt->gen++;
  69. mr->lkey = (r << (32 - ib_ipath_lkey_table_size)) |
  70. ((((1 << (24 - ib_ipath_lkey_table_size)) - 1) & rkt->gen)
  71. << 8);
  72. if (mr->lkey == 0) {
  73. mr->lkey |= 1 << 8;
  74. rkt->gen++;
  75. }
  76. rkt->table[r] = mr;
  77. spin_unlock_irqrestore(&rkt->lock, flags);
  78. ret = 1;
  79. bail:
  80. return ret;
  81. }
  82. /**
  83. * ipath_free_lkey - free an lkey
  84. * @rkt: table from which to free the lkey
  85. * @lkey: lkey id to free
  86. */
  87. void ipath_free_lkey(struct ipath_lkey_table *rkt, u32 lkey)
  88. {
  89. unsigned long flags;
  90. u32 r;
  91. if (lkey == 0)
  92. return;
  93. r = lkey >> (32 - ib_ipath_lkey_table_size);
  94. spin_lock_irqsave(&rkt->lock, flags);
  95. rkt->table[r] = NULL;
  96. spin_unlock_irqrestore(&rkt->lock, flags);
  97. }
  98. /**
  99. * ipath_lkey_ok - check IB SGE for validity and initialize
  100. * @rkt: table containing lkey to check SGE against
  101. * @isge: outgoing internal SGE
  102. * @sge: SGE to check
  103. * @acc: access flags
  104. *
  105. * Return 1 if valid and successful, otherwise returns 0.
  106. *
  107. * Check the IB SGE for validity and initialize our internal version
  108. * of it.
  109. */
  110. int ipath_lkey_ok(struct ipath_qp *qp, struct ipath_sge *isge,
  111. struct ib_sge *sge, int acc)
  112. {
  113. struct ipath_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
  114. struct ipath_mregion *mr;
  115. unsigned n, m;
  116. size_t off;
  117. int ret;
  118. /*
  119. * We use LKEY == zero for kernel virtual addresses
  120. * (see ipath_get_dma_mr and ipath_dma.c).
  121. */
  122. if (sge->lkey == 0) {
  123. struct ipath_pd *pd = to_ipd(qp->ibqp.pd);
  124. if (pd->user) {
  125. ret = 0;
  126. goto bail;
  127. }
  128. isge->mr = NULL;
  129. isge->vaddr = (void *) sge->addr;
  130. isge->length = sge->length;
  131. isge->sge_length = sge->length;
  132. ret = 1;
  133. goto bail;
  134. }
  135. mr = rkt->table[(sge->lkey >> (32 - ib_ipath_lkey_table_size))];
  136. if (unlikely(mr == NULL || mr->lkey != sge->lkey ||
  137. qp->ibqp.pd != mr->pd)) {
  138. ret = 0;
  139. goto bail;
  140. }
  141. off = sge->addr - mr->user_base;
  142. if (unlikely(sge->addr < mr->user_base ||
  143. off + sge->length > mr->length ||
  144. (mr->access_flags & acc) != acc)) {
  145. ret = 0;
  146. goto bail;
  147. }
  148. off += mr->offset;
  149. m = 0;
  150. n = 0;
  151. while (off >= mr->map[m]->segs[n].length) {
  152. off -= mr->map[m]->segs[n].length;
  153. n++;
  154. if (n >= IPATH_SEGSZ) {
  155. m++;
  156. n = 0;
  157. }
  158. }
  159. isge->mr = mr;
  160. isge->vaddr = mr->map[m]->segs[n].vaddr + off;
  161. isge->length = mr->map[m]->segs[n].length - off;
  162. isge->sge_length = sge->length;
  163. isge->m = m;
  164. isge->n = n;
  165. ret = 1;
  166. bail:
  167. return ret;
  168. }
  169. /**
  170. * ipath_rkey_ok - check the IB virtual address, length, and RKEY
  171. * @dev: infiniband device
  172. * @ss: SGE state
  173. * @len: length of data
  174. * @vaddr: virtual address to place data
  175. * @rkey: rkey to check
  176. * @acc: access flags
  177. *
  178. * Return 1 if successful, otherwise 0.
  179. */
  180. int ipath_rkey_ok(struct ipath_qp *qp, struct ipath_sge_state *ss,
  181. u32 len, u64 vaddr, u32 rkey, int acc)
  182. {
  183. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  184. struct ipath_lkey_table *rkt = &dev->lk_table;
  185. struct ipath_sge *sge = &ss->sge;
  186. struct ipath_mregion *mr;
  187. unsigned n, m;
  188. size_t off;
  189. int ret;
  190. /*
  191. * We use RKEY == zero for kernel virtual addresses
  192. * (see ipath_get_dma_mr and ipath_dma.c).
  193. */
  194. if (rkey == 0) {
  195. struct ipath_pd *pd = to_ipd(qp->ibqp.pd);
  196. if (pd->user) {
  197. ret = 0;
  198. goto bail;
  199. }
  200. sge->mr = NULL;
  201. sge->vaddr = (void *) vaddr;
  202. sge->length = len;
  203. sge->sge_length = len;
  204. ss->sg_list = NULL;
  205. ss->num_sge = 1;
  206. ret = 1;
  207. goto bail;
  208. }
  209. mr = rkt->table[(rkey >> (32 - ib_ipath_lkey_table_size))];
  210. if (unlikely(mr == NULL || mr->lkey != rkey ||
  211. qp->ibqp.pd != mr->pd)) {
  212. ret = 0;
  213. goto bail;
  214. }
  215. off = vaddr - mr->iova;
  216. if (unlikely(vaddr < mr->iova || off + len > mr->length ||
  217. (mr->access_flags & acc) == 0)) {
  218. ret = 0;
  219. goto bail;
  220. }
  221. off += mr->offset;
  222. m = 0;
  223. n = 0;
  224. while (off >= mr->map[m]->segs[n].length) {
  225. off -= mr->map[m]->segs[n].length;
  226. n++;
  227. if (n >= IPATH_SEGSZ) {
  228. m++;
  229. n = 0;
  230. }
  231. }
  232. sge->mr = mr;
  233. sge->vaddr = mr->map[m]->segs[n].vaddr + off;
  234. sge->length = mr->map[m]->segs[n].length - off;
  235. sge->sge_length = len;
  236. sge->m = m;
  237. sge->n = n;
  238. ss->sg_list = NULL;
  239. ss->num_sge = 1;
  240. ret = 1;
  241. bail:
  242. return ret;
  243. }