ipath_keys.c 5.7 KB

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