qib_keys.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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->user_base ||
  176. off + sge->length > mr->length ||
  177. (mr->access_flags & acc) != acc))
  178. goto bail;
  179. if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
  180. goto bail;
  181. rcu_read_unlock();
  182. off += mr->offset;
  183. if (mr->page_shift) {
  184. /*
  185. page sizes are uniform power of 2 so no loop is necessary
  186. entries_spanned_by_off is the number of times the loop below
  187. would have executed.
  188. */
  189. size_t entries_spanned_by_off;
  190. entries_spanned_by_off = off >> mr->page_shift;
  191. off -= (entries_spanned_by_off << mr->page_shift);
  192. m = entries_spanned_by_off/QIB_SEGSZ;
  193. n = entries_spanned_by_off%QIB_SEGSZ;
  194. } else {
  195. m = 0;
  196. n = 0;
  197. while (off >= mr->map[m]->segs[n].length) {
  198. off -= mr->map[m]->segs[n].length;
  199. n++;
  200. if (n >= QIB_SEGSZ) {
  201. m++;
  202. n = 0;
  203. }
  204. }
  205. }
  206. isge->mr = mr;
  207. isge->vaddr = mr->map[m]->segs[n].vaddr + off;
  208. isge->length = mr->map[m]->segs[n].length - off;
  209. isge->sge_length = sge->length;
  210. isge->m = m;
  211. isge->n = n;
  212. ok:
  213. return 1;
  214. bail:
  215. rcu_read_unlock();
  216. return 0;
  217. }
  218. /**
  219. * qib_rkey_ok - check the IB virtual address, length, and RKEY
  220. * @qp: qp for validation
  221. * @sge: SGE state
  222. * @len: length of data
  223. * @vaddr: virtual address to place data
  224. * @rkey: rkey to check
  225. * @acc: access flags
  226. *
  227. * Return 1 if successful, otherwise 0.
  228. *
  229. * increments the reference count upon success
  230. */
  231. int qib_rkey_ok(struct qib_qp *qp, struct qib_sge *sge,
  232. u32 len, u64 vaddr, u32 rkey, int acc)
  233. {
  234. struct qib_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
  235. struct qib_mregion *mr;
  236. unsigned n, m;
  237. size_t off;
  238. /*
  239. * We use RKEY == zero for kernel virtual addresses
  240. * (see qib_get_dma_mr and qib_dma.c).
  241. */
  242. rcu_read_lock();
  243. if (rkey == 0) {
  244. struct qib_pd *pd = to_ipd(qp->ibqp.pd);
  245. struct qib_ibdev *dev = to_idev(pd->ibpd.device);
  246. if (pd->user)
  247. goto bail;
  248. mr = rcu_dereference(dev->dma_mr);
  249. if (!mr)
  250. goto bail;
  251. if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
  252. goto bail;
  253. rcu_read_unlock();
  254. sge->mr = mr;
  255. sge->vaddr = (void *) vaddr;
  256. sge->length = len;
  257. sge->sge_length = len;
  258. sge->m = 0;
  259. sge->n = 0;
  260. goto ok;
  261. }
  262. mr = rcu_dereference(
  263. rkt->table[(rkey >> (32 - ib_qib_lkey_table_size))]);
  264. if (unlikely(!mr || mr->lkey != rkey || qp->ibqp.pd != mr->pd))
  265. goto bail;
  266. off = vaddr - mr->iova;
  267. if (unlikely(vaddr < mr->iova || off + len > mr->length ||
  268. (mr->access_flags & acc) == 0))
  269. goto bail;
  270. if (unlikely(!atomic_inc_not_zero(&mr->refcount)))
  271. goto bail;
  272. rcu_read_unlock();
  273. off += mr->offset;
  274. if (mr->page_shift) {
  275. /*
  276. page sizes are uniform power of 2 so no loop is necessary
  277. entries_spanned_by_off is the number of times the loop below
  278. would have executed.
  279. */
  280. size_t entries_spanned_by_off;
  281. entries_spanned_by_off = off >> mr->page_shift;
  282. off -= (entries_spanned_by_off << mr->page_shift);
  283. m = entries_spanned_by_off/QIB_SEGSZ;
  284. n = entries_spanned_by_off%QIB_SEGSZ;
  285. } else {
  286. m = 0;
  287. n = 0;
  288. while (off >= mr->map[m]->segs[n].length) {
  289. off -= mr->map[m]->segs[n].length;
  290. n++;
  291. if (n >= QIB_SEGSZ) {
  292. m++;
  293. n = 0;
  294. }
  295. }
  296. }
  297. sge->mr = mr;
  298. sge->vaddr = mr->map[m]->segs[n].vaddr + off;
  299. sge->length = mr->map[m]->segs[n].length - off;
  300. sge->sge_length = len;
  301. sge->m = m;
  302. sge->n = n;
  303. ok:
  304. return 1;
  305. bail:
  306. rcu_read_unlock();
  307. return 0;
  308. }
  309. /*
  310. * Initialize the memory region specified by the work reqeust.
  311. */
  312. int qib_fast_reg_mr(struct qib_qp *qp, struct ib_send_wr *wr)
  313. {
  314. struct qib_lkey_table *rkt = &to_idev(qp->ibqp.device)->lk_table;
  315. struct qib_pd *pd = to_ipd(qp->ibqp.pd);
  316. struct qib_mregion *mr;
  317. u32 rkey = wr->wr.fast_reg.rkey;
  318. unsigned i, n, m;
  319. int ret = -EINVAL;
  320. unsigned long flags;
  321. u64 *page_list;
  322. size_t ps;
  323. spin_lock_irqsave(&rkt->lock, flags);
  324. if (pd->user || rkey == 0)
  325. goto bail;
  326. mr = rcu_dereference_protected(
  327. rkt->table[(rkey >> (32 - ib_qib_lkey_table_size))],
  328. lockdep_is_held(&rkt->lock));
  329. if (unlikely(mr == NULL || qp->ibqp.pd != mr->pd))
  330. goto bail;
  331. if (wr->wr.fast_reg.page_list_len > mr->max_segs)
  332. goto bail;
  333. ps = 1UL << wr->wr.fast_reg.page_shift;
  334. if (wr->wr.fast_reg.length > ps * wr->wr.fast_reg.page_list_len)
  335. goto bail;
  336. mr->user_base = wr->wr.fast_reg.iova_start;
  337. mr->iova = wr->wr.fast_reg.iova_start;
  338. mr->lkey = rkey;
  339. mr->length = wr->wr.fast_reg.length;
  340. mr->access_flags = wr->wr.fast_reg.access_flags;
  341. page_list = wr->wr.fast_reg.page_list->page_list;
  342. m = 0;
  343. n = 0;
  344. for (i = 0; i < wr->wr.fast_reg.page_list_len; i++) {
  345. mr->map[m]->segs[n].vaddr = (void *) page_list[i];
  346. mr->map[m]->segs[n].length = ps;
  347. if (++n == QIB_SEGSZ) {
  348. m++;
  349. n = 0;
  350. }
  351. }
  352. ret = 0;
  353. bail:
  354. spin_unlock_irqrestore(&rkt->lock, flags);
  355. return ret;
  356. }