ipath_keys.c 6.0 KB

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