ipath_keys.c 6.1 KB

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