qib_keys.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. * @rkt: lkey table in which to allocate the lkey
  37. * @mr: memory region that this lkey protects
  38. *
  39. * Returns 1 if successful, otherwise returns 0.
  40. */
  41. int qib_alloc_lkey(struct qib_lkey_table *rkt, struct qib_mregion *mr)
  42. {
  43. unsigned long flags;
  44. u32 r;
  45. u32 n;
  46. int ret;
  47. spin_lock_irqsave(&rkt->lock, flags);
  48. /* Find the next available LKEY */
  49. r = rkt->next;
  50. n = r;
  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. ret = 0;
  58. goto bail;
  59. }
  60. }
  61. rkt->next = (r + 1) & (rkt->max - 1);
  62. /*
  63. * Make sure lkey is never zero which is reserved to indicate an
  64. * unrestricted LKEY.
  65. */
  66. rkt->gen++;
  67. /*
  68. * bits are capped in qib_verbs.c to insure enough bits
  69. * for generation number
  70. */
  71. mr->lkey = (r << (32 - ib_qib_lkey_table_size)) |
  72. ((((1 << (24 - ib_qib_lkey_table_size)) - 1) & rkt->gen)
  73. << 8);
  74. if (mr->lkey == 0) {
  75. mr->lkey |= 1 << 8;
  76. rkt->gen++;
  77. }
  78. rkt->table[r] = mr;
  79. spin_unlock_irqrestore(&rkt->lock, flags);
  80. ret = 1;
  81. bail:
  82. return ret;
  83. }
  84. /**
  85. * qib_free_lkey - free an lkey
  86. * @rkt: table from which to free the lkey
  87. * @lkey: lkey id to free
  88. */
  89. int qib_free_lkey(struct qib_ibdev *dev, struct qib_mregion *mr)
  90. {
  91. unsigned long flags;
  92. u32 lkey = mr->lkey;
  93. u32 r;
  94. int ret;
  95. spin_lock_irqsave(&dev->lk_table.lock, flags);
  96. if (lkey == 0) {
  97. if (dev->dma_mr && dev->dma_mr == mr) {
  98. ret = atomic_read(&dev->dma_mr->refcount);
  99. if (!ret)
  100. dev->dma_mr = NULL;
  101. } else
  102. ret = 0;
  103. } else {
  104. r = lkey >> (32 - ib_qib_lkey_table_size);
  105. ret = atomic_read(&dev->lk_table.table[r]->refcount);
  106. if (!ret)
  107. dev->lk_table.table[r] = NULL;
  108. }
  109. spin_unlock_irqrestore(&dev->lk_table.lock, flags);
  110. if (ret)
  111. ret = -EBUSY;
  112. return ret;
  113. }
  114. /**
  115. * qib_lkey_ok - check IB SGE for validity and initialize
  116. * @rkt: table containing lkey to check SGE against
  117. * @isge: outgoing internal SGE
  118. * @sge: SGE to check
  119. * @acc: access flags
  120. *
  121. * Return 1 if valid and successful, otherwise returns 0.
  122. *
  123. * Check the IB SGE for validity and initialize our internal version
  124. * of it.
  125. */
  126. int qib_lkey_ok(struct qib_lkey_table *rkt, struct qib_pd *pd,
  127. struct qib_sge *isge, struct ib_sge *sge, int acc)
  128. {
  129. struct qib_mregion *mr;
  130. unsigned n, m;
  131. size_t off;
  132. unsigned long flags;
  133. /*
  134. * We use LKEY == zero for kernel virtual addresses
  135. * (see qib_get_dma_mr and qib_dma.c).
  136. */
  137. spin_lock_irqsave(&rkt->lock, flags);
  138. if (sge->lkey == 0) {
  139. struct qib_ibdev *dev = to_idev(pd->ibpd.device);
  140. if (pd->user)
  141. goto bail;
  142. if (!dev->dma_mr)
  143. goto bail;
  144. atomic_inc(&dev->dma_mr->refcount);
  145. spin_unlock_irqrestore(&rkt->lock, flags);
  146. isge->mr = dev->dma_mr;
  147. isge->vaddr = (void *) sge->addr;
  148. isge->length = sge->length;
  149. isge->sge_length = sge->length;
  150. isge->m = 0;
  151. isge->n = 0;
  152. goto ok;
  153. }
  154. mr = rkt->table[(sge->lkey >> (32 - ib_qib_lkey_table_size))];
  155. if (unlikely(mr == NULL || mr->lkey != sge->lkey ||
  156. mr->pd != &pd->ibpd))
  157. goto bail;
  158. off = sge->addr - mr->user_base;
  159. if (unlikely(sge->addr < mr->user_base ||
  160. off + sge->length > mr->length ||
  161. (mr->access_flags & acc) != acc))
  162. goto bail;
  163. atomic_inc(&mr->refcount);
  164. spin_unlock_irqrestore(&rkt->lock, flags);
  165. off += mr->offset;
  166. if (mr->page_shift) {
  167. /*
  168. page sizes are uniform power of 2 so no loop is necessary
  169. entries_spanned_by_off is the number of times the loop below
  170. would have executed.
  171. */
  172. size_t entries_spanned_by_off;
  173. entries_spanned_by_off = off >> mr->page_shift;
  174. off -= (entries_spanned_by_off << mr->page_shift);
  175. m = entries_spanned_by_off/QIB_SEGSZ;
  176. n = entries_spanned_by_off%QIB_SEGSZ;
  177. } else {
  178. m = 0;
  179. n = 0;
  180. while (off >= mr->map[m]->segs[n].length) {
  181. off -= mr->map[m]->segs[n].length;
  182. n++;
  183. if (n >= QIB_SEGSZ) {
  184. m++;
  185. n = 0;
  186. }
  187. }
  188. }
  189. isge->mr = mr;
  190. isge->vaddr = mr->map[m]->segs[n].vaddr + off;
  191. isge->length = mr->map[m]->segs[n].length - off;
  192. isge->sge_length = sge->length;
  193. isge->m = m;
  194. isge->n = n;
  195. ok:
  196. return 1;
  197. bail:
  198. spin_unlock_irqrestore(&rkt->lock, flags);
  199. return 0;
  200. }
  201. /**
  202. * qib_rkey_ok - check the IB virtual address, length, and RKEY
  203. * @dev: infiniband device
  204. * @ss: SGE state
  205. * @len: length of data
  206. * @vaddr: virtual address to place data
  207. * @rkey: rkey to check
  208. * @acc: access flags
  209. *
  210. * Return 1 if successful, otherwise 0.
  211. */
  212. int qib_rkey_ok(struct qib_qp *qp, struct qib_sge *sge,
  213. u32 len, u64 vaddr, u32 rkey, int acc)
  214. {
  215. struct qib_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
  216. struct qib_mregion *mr;
  217. unsigned n, m;
  218. size_t off;
  219. unsigned long flags;
  220. /*
  221. * We use RKEY == zero for kernel virtual addresses
  222. * (see qib_get_dma_mr and qib_dma.c).
  223. */
  224. spin_lock_irqsave(&rkt->lock, flags);
  225. if (rkey == 0) {
  226. struct qib_pd *pd = to_ipd(qp->ibqp.pd);
  227. struct qib_ibdev *dev = to_idev(pd->ibpd.device);
  228. if (pd->user)
  229. goto bail;
  230. if (!dev->dma_mr)
  231. goto bail;
  232. atomic_inc(&dev->dma_mr->refcount);
  233. spin_unlock_irqrestore(&rkt->lock, flags);
  234. sge->mr = dev->dma_mr;
  235. sge->vaddr = (void *) vaddr;
  236. sge->length = len;
  237. sge->sge_length = len;
  238. sge->m = 0;
  239. sge->n = 0;
  240. goto ok;
  241. }
  242. mr = rkt->table[(rkey >> (32 - ib_qib_lkey_table_size))];
  243. if (unlikely(mr == NULL || mr->lkey != rkey || qp->ibqp.pd != mr->pd))
  244. goto bail;
  245. off = vaddr - mr->iova;
  246. if (unlikely(vaddr < mr->iova || off + len > mr->length ||
  247. (mr->access_flags & acc) == 0))
  248. goto bail;
  249. atomic_inc(&mr->refcount);
  250. spin_unlock_irqrestore(&rkt->lock, flags);
  251. off += mr->offset;
  252. if (mr->page_shift) {
  253. /*
  254. page sizes are uniform power of 2 so no loop is necessary
  255. entries_spanned_by_off is the number of times the loop below
  256. would have executed.
  257. */
  258. size_t entries_spanned_by_off;
  259. entries_spanned_by_off = off >> mr->page_shift;
  260. off -= (entries_spanned_by_off << mr->page_shift);
  261. m = entries_spanned_by_off/QIB_SEGSZ;
  262. n = entries_spanned_by_off%QIB_SEGSZ;
  263. } else {
  264. m = 0;
  265. n = 0;
  266. while (off >= mr->map[m]->segs[n].length) {
  267. off -= mr->map[m]->segs[n].length;
  268. n++;
  269. if (n >= QIB_SEGSZ) {
  270. m++;
  271. n = 0;
  272. }
  273. }
  274. }
  275. sge->mr = mr;
  276. sge->vaddr = mr->map[m]->segs[n].vaddr + off;
  277. sge->length = mr->map[m]->segs[n].length - off;
  278. sge->sge_length = len;
  279. sge->m = m;
  280. sge->n = n;
  281. ok:
  282. return 1;
  283. bail:
  284. spin_unlock_irqrestore(&rkt->lock, flags);
  285. return 0;
  286. }
  287. /*
  288. * Initialize the memory region specified by the work reqeust.
  289. */
  290. int qib_fast_reg_mr(struct qib_qp *qp, struct ib_send_wr *wr)
  291. {
  292. struct qib_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
  293. struct qib_pd *pd = to_ipd(qp->ibqp.pd);
  294. struct qib_mregion *mr;
  295. u32 rkey = wr->wr.fast_reg.rkey;
  296. unsigned i, n, m;
  297. int ret = -EINVAL;
  298. unsigned long flags;
  299. u64 *page_list;
  300. size_t ps;
  301. spin_lock_irqsave(&rkt->lock, flags);
  302. if (pd->user || rkey == 0)
  303. goto bail;
  304. mr = rkt->table[(rkey >> (32 - ib_qib_lkey_table_size))];
  305. if (unlikely(mr == NULL || qp->ibqp.pd != mr->pd))
  306. goto bail;
  307. if (wr->wr.fast_reg.page_list_len > mr->max_segs)
  308. goto bail;
  309. ps = 1UL << wr->wr.fast_reg.page_shift;
  310. if (wr->wr.fast_reg.length > ps * wr->wr.fast_reg.page_list_len)
  311. goto bail;
  312. mr->user_base = wr->wr.fast_reg.iova_start;
  313. mr->iova = wr->wr.fast_reg.iova_start;
  314. mr->lkey = rkey;
  315. mr->length = wr->wr.fast_reg.length;
  316. mr->access_flags = wr->wr.fast_reg.access_flags;
  317. page_list = wr->wr.fast_reg.page_list->page_list;
  318. m = 0;
  319. n = 0;
  320. for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
  321. mr->map[m]->segs[n].vaddr = (void *) page_list[i];
  322. mr->map[m]->segs[n].length = ps;
  323. if (++n == QIB_SEGSZ) {
  324. m++;
  325. n = 0;
  326. }
  327. }
  328. ret = 0;
  329. bail:
  330. spin_unlock_irqrestore(&rkt->lock, flags);
  331. return ret;
  332. }