qib_keys.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * Copyright (c) 2006, 2007, 2009 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 "qib.h"
  34. /**
  35. * qib_alloc_lkey - allocate an lkey
  36. * @mr: memory region that this lkey protects
  37. * @dma_region: 0->normal key, 1->restricted DMA key
  38. *
  39. * Returns 0 if successful, otherwise returns -errno.
  40. *
  41. * Increments mr reference count as required.
  42. *
  43. * Sets the lkey field mr for non-dma regions.
  44. *
  45. */
  46. int qib_alloc_lkey(struct rvt_mregion *mr, int dma_region)
  47. {
  48. unsigned long flags;
  49. u32 r;
  50. u32 n;
  51. int ret = 0;
  52. struct qib_ibdev *dev = to_idev(mr->pd->device);
  53. struct rvt_lkey_table *rkt = &dev->lk_table;
  54. spin_lock_irqsave(&rkt->lock, flags);
  55. /* special case for dma_mr lkey == 0 */
  56. if (dma_region) {
  57. struct rvt_mregion *tmr;
  58. tmr = rcu_access_pointer(dev->dma_mr);
  59. if (!tmr) {
  60. qib_get_mr(mr);
  61. rcu_assign_pointer(dev->dma_mr, mr);
  62. mr->lkey_published = 1;
  63. }
  64. goto success;
  65. }
  66. /* Find the next available LKEY */
  67. r = rkt->next;
  68. n = r;
  69. for (;;) {
  70. if (rkt->table[r] == NULL)
  71. break;
  72. r = (r + 1) & (rkt->max - 1);
  73. if (r == n)
  74. goto bail;
  75. }
  76. rkt->next = (r + 1) & (rkt->max - 1);
  77. /*
  78. * Make sure lkey is never zero which is reserved to indicate an
  79. * unrestricted LKEY.
  80. */
  81. rkt->gen++;
  82. /*
  83. * bits are capped in qib_verbs.c to insure enough bits
  84. * for generation number
  85. */
  86. mr->lkey = (r << (32 - ib_rvt_lkey_table_size)) |
  87. ((((1 << (24 - ib_rvt_lkey_table_size)) - 1) & rkt->gen)
  88. << 8);
  89. if (mr->lkey == 0) {
  90. mr->lkey |= 1 << 8;
  91. rkt->gen++;
  92. }
  93. qib_get_mr(mr);
  94. rcu_assign_pointer(rkt->table[r], mr);
  95. mr->lkey_published = 1;
  96. success:
  97. spin_unlock_irqrestore(&rkt->lock, flags);
  98. out:
  99. return ret;
  100. bail:
  101. spin_unlock_irqrestore(&rkt->lock, flags);
  102. ret = -ENOMEM;
  103. goto out;
  104. }
  105. /**
  106. * qib_free_lkey - free an lkey
  107. * @mr: mr to free from tables
  108. */
  109. void qib_free_lkey(struct rvt_mregion *mr)
  110. {
  111. unsigned long flags;
  112. u32 lkey = mr->lkey;
  113. u32 r;
  114. struct qib_ibdev *dev = to_idev(mr->pd->device);
  115. struct rvt_lkey_table *rkt = &dev->lk_table;
  116. spin_lock_irqsave(&rkt->lock, flags);
  117. if (!mr->lkey_published)
  118. goto out;
  119. if (lkey == 0)
  120. RCU_INIT_POINTER(dev->dma_mr, NULL);
  121. else {
  122. r = lkey >> (32 - ib_rvt_lkey_table_size);
  123. RCU_INIT_POINTER(rkt->table[r], NULL);
  124. }
  125. qib_put_mr(mr);
  126. mr->lkey_published = 0;
  127. out:
  128. spin_unlock_irqrestore(&rkt->lock, flags);
  129. }
  130. /**
  131. * qib_rkey_ok - check the IB virtual address, length, and RKEY
  132. * @qp: qp for validation
  133. * @sge: SGE state
  134. * @len: length of data
  135. * @vaddr: virtual address to place data
  136. * @rkey: rkey to check
  137. * @acc: access flags
  138. *
  139. * Return 1 if successful, otherwise 0.
  140. *
  141. * increments the reference count upon success
  142. */
  143. int qib_rkey_ok(struct rvt_qp *qp, struct rvt_sge *sge,
  144. u32 len, u64 vaddr, u32 rkey, int acc)
  145. {
  146. struct rvt_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
  147. struct rvt_mregion *mr;
  148. unsigned n, m;
  149. size_t off;
  150. /*
  151. * We use RKEY == zero for kernel virtual addresses
  152. * (see qib_get_dma_mr and qib_dma.c).
  153. */
  154. rcu_read_lock();
  155. if (rkey == 0) {
  156. struct rvt_pd *pd = ibpd_to_rvtpd(qp->ibqp.pd);
  157. struct qib_ibdev *dev = to_idev(pd->ibpd.device);
  158. if (pd->user)
  159. goto bail;
  160. mr = rcu_dereference(dev->dma_mr);
  161. if (!mr)
  162. goto bail;
  163. if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
  164. goto bail;
  165. rcu_read_unlock();
  166. sge->mr = mr;
  167. sge->vaddr = (void *) vaddr;
  168. sge->length = len;
  169. sge->sge_length = len;
  170. sge->m = 0;
  171. sge->n = 0;
  172. goto ok;
  173. }
  174. mr = rcu_dereference(
  175. rkt->table[(rkey >> (32 - ib_rvt_lkey_table_size))]);
  176. if (unlikely(!mr || mr->lkey != rkey || qp->ibqp.pd != mr->pd))
  177. goto bail;
  178. off = vaddr - mr->iova;
  179. if (unlikely(vaddr < mr->iova || off + len > mr->length ||
  180. (mr->access_flags & acc) == 0))
  181. goto bail;
  182. if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
  183. goto bail;
  184. rcu_read_unlock();
  185. off += mr->offset;
  186. if (mr->page_shift) {
  187. /*
  188. page sizes are uniform power of 2 so no loop is necessary
  189. entries_spanned_by_off is the number of times the loop below
  190. would have executed.
  191. */
  192. size_t entries_spanned_by_off;
  193. entries_spanned_by_off = off >> mr->page_shift;
  194. off -= (entries_spanned_by_off << mr->page_shift);
  195. m = entries_spanned_by_off / RVT_SEGSZ;
  196. n = entries_spanned_by_off % RVT_SEGSZ;
  197. } else {
  198. m = 0;
  199. n = 0;
  200. while (off >= mr->map[m]->segs[n].length) {
  201. off -= mr->map[m]->segs[n].length;
  202. n++;
  203. if (n >= RVT_SEGSZ) {
  204. m++;
  205. n = 0;
  206. }
  207. }
  208. }
  209. sge->mr = mr;
  210. sge->vaddr = mr->map[m]->segs[n].vaddr + off;
  211. sge->length = mr->map[m]->segs[n].length - off;
  212. sge->sge_length = len;
  213. sge->m = m;
  214. sge->n = n;
  215. ok:
  216. return 1;
  217. bail:
  218. rcu_read_unlock();
  219. return 0;
  220. }