ipath_keys.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. /* always a kernel port, no locking needed */
  124. struct ipath_pd *pd = to_ipd(qp->ibqp.pd);
  125. if (pd->user) {
  126. ret = 0;
  127. goto bail;
  128. }
  129. isge->mr = NULL;
  130. isge->vaddr = (void *) sge->addr;
  131. isge->length = sge->length;
  132. isge->sge_length = sge->length;
  133. ret = 1;
  134. goto bail;
  135. }
  136. mr = rkt->table[(sge->lkey >> (32 - ib_ipath_lkey_table_size))];
  137. if (unlikely(mr == NULL || mr->lkey != sge->lkey ||
  138. qp->ibqp.pd != mr->pd)) {
  139. ret = 0;
  140. goto bail;
  141. }
  142. off = sge->addr - mr->user_base;
  143. if (unlikely(sge->addr < mr->user_base ||
  144. off + sge->length > mr->length ||
  145. (mr->access_flags & acc) != acc)) {
  146. ret = 0;
  147. goto bail;
  148. }
  149. off += mr->offset;
  150. m = 0;
  151. n = 0;
  152. while (off >= mr->map[m]->segs[n].length) {
  153. off -= mr->map[m]->segs[n].length;
  154. n++;
  155. if (n >= IPATH_SEGSZ) {
  156. m++;
  157. n = 0;
  158. }
  159. }
  160. isge->mr = mr;
  161. isge->vaddr = mr->map[m]->segs[n].vaddr + off;
  162. isge->length = mr->map[m]->segs[n].length - off;
  163. isge->sge_length = sge->length;
  164. isge->m = m;
  165. isge->n = n;
  166. ret = 1;
  167. bail:
  168. return ret;
  169. }
  170. /**
  171. * ipath_rkey_ok - check the IB virtual address, length, and RKEY
  172. * @dev: infiniband device
  173. * @ss: SGE state
  174. * @len: length of data
  175. * @vaddr: virtual address to place data
  176. * @rkey: rkey to check
  177. * @acc: access flags
  178. *
  179. * Return 1 if successful, otherwise 0.
  180. */
  181. int ipath_rkey_ok(struct ipath_qp *qp, struct ipath_sge_state *ss,
  182. u32 len, u64 vaddr, u32 rkey, int acc)
  183. {
  184. struct ipath_ibdev *dev = to_idev(qp->ibqp.device);
  185. struct ipath_lkey_table *rkt = &dev->lk_table;
  186. struct ipath_sge *sge = &ss->sge;
  187. struct ipath_mregion *mr;
  188. unsigned n, m;
  189. size_t off;
  190. int ret;
  191. /*
  192. * We use RKEY == zero for kernel virtual addresses
  193. * (see ipath_get_dma_mr and ipath_dma.c).
  194. */
  195. if (rkey == 0) {
  196. /* always a kernel port, no locking needed */
  197. struct ipath_pd *pd = to_ipd(qp->ibqp.pd);
  198. if (pd->user) {
  199. ret = 0;
  200. goto bail;
  201. }
  202. sge->mr = NULL;
  203. sge->vaddr = (void *) vaddr;
  204. sge->length = len;
  205. sge->sge_length = len;
  206. ss->sg_list = NULL;
  207. ss->num_sge = 1;
  208. ret = 1;
  209. goto bail;
  210. }
  211. mr = rkt->table[(rkey >> (32 - ib_ipath_lkey_table_size))];
  212. if (unlikely(mr == NULL || mr->lkey != rkey ||
  213. qp->ibqp.pd != mr->pd)) {
  214. ret = 0;
  215. goto bail;
  216. }
  217. off = vaddr - mr->iova;
  218. if (unlikely(vaddr < mr->iova || off + len > mr->length ||
  219. (mr->access_flags & acc) == 0)) {
  220. ret = 0;
  221. goto bail;
  222. }
  223. off += mr->offset;
  224. m = 0;
  225. n = 0;
  226. while (off >= mr->map[m]->segs[n].length) {
  227. off -= mr->map[m]->segs[n].length;
  228. n++;
  229. if (n >= IPATH_SEGSZ) {
  230. m++;
  231. n = 0;
  232. }
  233. }
  234. sge->mr = mr;
  235. sge->vaddr = mr->map[m]->segs[n].vaddr + off;
  236. sge->length = mr->map[m]->segs[n].length - off;
  237. sge->sge_length = len;
  238. sge->m = m;
  239. sge->n = n;
  240. ss->sg_list = NULL;
  241. ss->num_sge = 1;
  242. ret = 1;
  243. bail:
  244. return ret;
  245. }