qib_keys.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 qib_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 qib_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 qib_mregion *tmr;
  58. tmr = rcu_dereference(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. mr->lkey = (r << (32 - ib_qib_lkey_table_size)) |
  83. ((((1 << (24 - ib_qib_lkey_table_size)) - 1) & rkt->gen)
  84. << 8);
  85. if (mr->lkey == 0) {
  86. mr->lkey |= 1 << 8;
  87. rkt->gen++;
  88. }
  89. qib_get_mr(mr);
  90. rcu_assign_pointer(rkt->table[r], mr);
  91. mr->lkey_published = 1;
  92. success:
  93. spin_unlock_irqrestore(&rkt->lock, flags);
  94. out:
  95. return ret;
  96. bail:
  97. spin_unlock_irqrestore(&rkt->lock, flags);
  98. ret = -ENOMEM;
  99. goto out;
  100. }
  101. /**
  102. * qib_free_lkey - free an lkey
  103. * @mr: mr to free from tables
  104. */
  105. void qib_free_lkey(struct qib_mregion *mr)
  106. {
  107. unsigned long flags;
  108. u32 lkey = mr->lkey;
  109. u32 r;
  110. struct qib_ibdev *dev = to_idev(mr->pd->device);
  111. struct qib_lkey_table *rkt = &dev->lk_table;
  112. spin_lock_irqsave(&rkt->lock, flags);
  113. if (!mr->lkey_published)
  114. goto out;
  115. if (lkey == 0)
  116. rcu_assign_pointer(dev->dma_mr, NULL);
  117. else {
  118. r = lkey >> (32 - ib_qib_lkey_table_size);
  119. rcu_assign_pointer(rkt->table[r], NULL);
  120. }
  121. qib_put_mr(mr);
  122. mr->lkey_published = 0;
  123. out:
  124. spin_unlock_irqrestore(&rkt->lock, flags);
  125. }
  126. /**
  127. * qib_lkey_ok - check IB SGE for validity and initialize
  128. * @rkt: table containing lkey to check SGE against
  129. * @pd: protection domain
  130. * @isge: outgoing internal SGE
  131. * @sge: SGE to check
  132. * @acc: access flags
  133. *
  134. * Return 1 if valid and successful, otherwise returns 0.
  135. *
  136. * increments the reference count upon success
  137. *
  138. * Check the IB SGE for validity and initialize our internal version
  139. * of it.
  140. */
  141. int qib_lkey_ok(struct qib_lkey_table *rkt, struct qib_pd *pd,
  142. struct qib_sge *isge, struct ib_sge *sge, int acc)
  143. {
  144. struct qib_mregion *mr;
  145. unsigned n, m;
  146. size_t off;
  147. /*
  148. * We use LKEY == zero for kernel virtual addresses
  149. * (see qib_get_dma_mr and qib_dma.c).
  150. */
  151. rcu_read_lock();
  152. if (sge->lkey == 0) {
  153. struct qib_ibdev *dev = to_idev(pd->ibpd.device);
  154. if (pd->user)
  155. goto bail;
  156. mr = rcu_dereference(dev->dma_mr);
  157. if (!mr)
  158. goto bail;
  159. if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
  160. goto bail;
  161. rcu_read_unlock();
  162. isge->mr = mr;
  163. isge->vaddr = (void *) sge->addr;
  164. isge->length = sge->length;
  165. isge->sge_length = sge->length;
  166. isge->m = 0;
  167. isge->n = 0;
  168. goto ok;
  169. }
  170. mr = rcu_dereference(
  171. rkt->table[(sge->lkey >> (32 - ib_qib_lkey_table_size))]);
  172. if (unlikely(!mr || mr->lkey != sge->lkey || mr->pd != &pd->ibpd))
  173. goto bail;
  174. off = sge->addr - mr->user_base;
  175. if (unlikely(sge->addr < mr->iova || off + sge->length > mr->length ||
  176. (mr->access_flags & acc) == 0))
  177. goto bail;
  178. if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
  179. goto bail;
  180. rcu_read_unlock();
  181. off += mr->offset;
  182. if (mr->page_shift) {
  183. /*
  184. page sizes are uniform power of 2 so no loop is necessary
  185. entries_spanned_by_off is the number of times the loop below
  186. would have executed.
  187. */
  188. size_t entries_spanned_by_off;
  189. entries_spanned_by_off = off >> mr->page_shift;
  190. off -= (entries_spanned_by_off << mr->page_shift);
  191. m = entries_spanned_by_off/QIB_SEGSZ;
  192. n = entries_spanned_by_off%QIB_SEGSZ;
  193. } else {
  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 >= QIB_SEGSZ) {
  200. m++;
  201. n = 0;
  202. }
  203. }
  204. }
  205. isge->mr = mr;
  206. isge->vaddr = mr->map[m]->segs[n].vaddr + off;
  207. isge->length = mr->map[m]->segs[n].length - off;
  208. isge->sge_length = sge->length;
  209. isge->m = m;
  210. isge->n = n;
  211. ok:
  212. return 1;
  213. bail:
  214. rcu_read_unlock();
  215. return 0;
  216. }
  217. /**
  218. * qib_rkey_ok - check the IB virtual address, length, and RKEY
  219. * @qp: qp for validation
  220. * @sge: SGE state
  221. * @len: length of data
  222. * @vaddr: virtual address to place data
  223. * @rkey: rkey to check
  224. * @acc: access flags
  225. *
  226. * Return 1 if successful, otherwise 0.
  227. *
  228. * increments the reference count upon success
  229. */
  230. int qib_rkey_ok(struct qib_qp *qp, struct qib_sge *sge,
  231. u32 len, u64 vaddr, u32 rkey, int acc)
  232. {
  233. struct qib_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
  234. struct qib_mregion *mr;
  235. unsigned n, m;
  236. size_t off;
  237. /*
  238. * We use RKEY == zero for kernel virtual addresses
  239. * (see qib_get_dma_mr and qib_dma.c).
  240. */
  241. rcu_read_lock();
  242. if (rkey == 0) {
  243. struct qib_pd *pd = to_ipd(qp->ibqp.pd);
  244. struct qib_ibdev *dev = to_idev(pd->ibpd.device);
  245. if (pd->user)
  246. goto bail;
  247. mr = rcu_dereference(dev->dma_mr);
  248. if (!mr)
  249. goto bail;
  250. if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
  251. goto bail;
  252. rcu_read_unlock();
  253. sge->mr = mr;
  254. sge->vaddr = (void *) vaddr;
  255. sge->length = len;
  256. sge->sge_length = len;
  257. sge->m = 0;
  258. sge->n = 0;
  259. goto ok;
  260. }
  261. mr = rcu_dereference(
  262. rkt->table[(rkey >> (32 - ib_qib_lkey_table_size))]);
  263. if (unlikely(!mr || mr->lkey != rkey || qp->ibqp.pd != mr->pd))
  264. goto bail;
  265. off = vaddr - mr->iova;
  266. if (unlikely(vaddr < mr->iova || off + len > mr->length ||
  267. (mr->access_flags & acc) == 0))
  268. goto bail;
  269. if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
  270. goto bail;
  271. rcu_read_unlock();
  272. off += mr->offset;
  273. if (mr->page_shift) {
  274. /*
  275. page sizes are uniform power of 2 so no loop is necessary
  276. entries_spanned_by_off is the number of times the loop below
  277. would have executed.
  278. */
  279. size_t entries_spanned_by_off;
  280. entries_spanned_by_off = off >> mr->page_shift;
  281. off -= (entries_spanned_by_off << mr->page_shift);
  282. m = entries_spanned_by_off/QIB_SEGSZ;
  283. n = entries_spanned_by_off%QIB_SEGSZ;
  284. } else {
  285. m = 0;
  286. n = 0;
  287. while (off >= mr->map[m]->segs[n].length) {
  288. off -= mr->map[m]->segs[n].length;
  289. n++;
  290. if (n >= QIB_SEGSZ) {
  291. m++;
  292. n = 0;
  293. }
  294. }
  295. }
  296. sge->mr = mr;
  297. sge->vaddr = mr->map[m]->segs[n].vaddr + off;
  298. sge->length = mr->map[m]->segs[n].length - off;
  299. sge->sge_length = len;
  300. sge->m = m;
  301. sge->n = n;
  302. ok:
  303. return 1;
  304. bail:
  305. rcu_read_unlock();
  306. return 0;
  307. }
  308. /*
  309. * Initialize the memory region specified by the work reqeust.
  310. */
  311. int qib_fast_reg_mr(struct qib_qp *qp, struct ib_send_wr *wr)
  312. {
  313. struct qib_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
  314. struct qib_pd *pd = to_ipd(qp->ibqp.pd);
  315. struct qib_mregion *mr;
  316. u32 rkey = wr->wr.fast_reg.rkey;
  317. unsigned i, n, m;
  318. int ret = -EINVAL;
  319. unsigned long flags;
  320. u64 *page_list;
  321. size_t ps;
  322. spin_lock_irqsave(&rkt->lock, flags);
  323. if (pd->user || rkey == 0)
  324. goto bail;
  325. mr = rcu_dereference_protected(
  326. rkt->table[(rkey >> (32 - ib_qib_lkey_table_size))],
  327. lockdep_is_held(&rkt->lock));
  328. if (unlikely(mr == NULL || qp->ibqp.pd != mr->pd))
  329. goto bail;
  330. if (wr->wr.fast_reg.page_list_len > mr->max_segs)
  331. goto bail;
  332. ps = 1UL << wr->wr.fast_reg.page_shift;
  333. if (wr->wr.fast_reg.length > ps * wr->wr.fast_reg.page_list_len)
  334. goto bail;
  335. mr->user_base = wr->wr.fast_reg.iova_start;
  336. mr->iova = wr->wr.fast_reg.iova_start;
  337. mr->lkey = rkey;
  338. mr->length = wr->wr.fast_reg.length;
  339. mr->access_flags = wr->wr.fast_reg.access_flags;
  340. page_list = wr->wr.fast_reg.page_list->page_list;
  341. m = 0;
  342. n = 0;
  343. for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
  344. mr->map[m]->segs[n].vaddr = (void *) page_list[i];
  345. mr->map[m]->segs[n].length = ps;
  346. if (++n == QIB_SEGSZ) {
  347. m++;
  348. n = 0;
  349. }
  350. }
  351. ret = 0;
  352. bail:
  353. spin_unlock_irqrestore(&rkt->lock, flags);
  354. return ret;
  355. }