qib_mr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * Copyright (c) 2006, 2007, 2008, 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 <rdma/ib_umem.h>
  34. #include <rdma/ib_smi.h>
  35. #include "qib.h"
  36. /* Fast memory region */
  37. struct qib_fmr {
  38. struct ib_fmr ibfmr;
  39. struct qib_mregion mr; /* must be last */
  40. };
  41. static inline struct qib_fmr *to_ifmr(struct ib_fmr *ibfmr)
  42. {
  43. return container_of(ibfmr, struct qib_fmr, ibfmr);
  44. }
  45. static int init_qib_mregion(struct qib_mregion *mr, struct ib_pd *pd,
  46. int count)
  47. {
  48. int m, i = 0;
  49. int rval = 0;
  50. m = (count + QIB_SEGSZ - 1) / QIB_SEGSZ;
  51. for (; i < m; i++) {
  52. mr->map[i] = kzalloc(sizeof(*mr->map[0]), GFP_KERNEL);
  53. if (!mr->map[i])
  54. goto bail;
  55. }
  56. mr->mapsz = m;
  57. init_completion(&mr->comp);
  58. /* count returning the ptr to user */
  59. atomic_set(&mr->refcount, 1);
  60. mr->pd = pd;
  61. mr->max_segs = count;
  62. out:
  63. return rval;
  64. bail:
  65. while (i)
  66. kfree(mr->map[--i]);
  67. rval = -ENOMEM;
  68. goto out;
  69. }
  70. static void deinit_qib_mregion(struct qib_mregion *mr)
  71. {
  72. int i = mr->mapsz;
  73. mr->mapsz = 0;
  74. while (i)
  75. kfree(mr->map[--i]);
  76. }
  77. /**
  78. * qib_get_dma_mr - get a DMA memory region
  79. * @pd: protection domain for this memory region
  80. * @acc: access flags
  81. *
  82. * Returns the memory region on success, otherwise returns an errno.
  83. * Note that all DMA addresses should be created via the
  84. * struct ib_dma_mapping_ops functions (see qib_dma.c).
  85. */
  86. struct ib_mr *qib_get_dma_mr(struct ib_pd *pd, int acc)
  87. {
  88. struct qib_mr *mr = NULL;
  89. struct ib_mr *ret;
  90. int rval;
  91. if (to_ipd(pd)->user) {
  92. ret = ERR_PTR(-EPERM);
  93. goto bail;
  94. }
  95. mr = kzalloc(sizeof(*mr), GFP_KERNEL);
  96. if (!mr) {
  97. ret = ERR_PTR(-ENOMEM);
  98. goto bail;
  99. }
  100. rval = init_qib_mregion(&mr->mr, pd, 0);
  101. if (rval) {
  102. ret = ERR_PTR(rval);
  103. goto bail;
  104. }
  105. rval = qib_alloc_lkey(&mr->mr, 1);
  106. if (rval) {
  107. ret = ERR_PTR(rval);
  108. goto bail_mregion;
  109. }
  110. mr->mr.access_flags = acc;
  111. ret = &mr->ibmr;
  112. done:
  113. return ret;
  114. bail_mregion:
  115. deinit_qib_mregion(&mr->mr);
  116. bail:
  117. kfree(mr);
  118. goto done;
  119. }
  120. static struct qib_mr *alloc_mr(int count, struct ib_pd *pd)
  121. {
  122. struct qib_mr *mr;
  123. int rval = -ENOMEM;
  124. int m;
  125. /* Allocate struct plus pointers to first level page tables. */
  126. m = (count + QIB_SEGSZ - 1) / QIB_SEGSZ;
  127. mr = kzalloc(sizeof(*mr) + m * sizeof(mr->mr.map[0]), GFP_KERNEL);
  128. if (!mr)
  129. goto bail;
  130. rval = init_qib_mregion(&mr->mr, pd, count);
  131. if (rval)
  132. goto bail;
  133. /*
  134. * ib_reg_phys_mr() will initialize mr->ibmr except for
  135. * lkey and rkey.
  136. */
  137. rval = qib_alloc_lkey(&mr->mr, 0);
  138. if (rval)
  139. goto bail_mregion;
  140. mr->ibmr.lkey = mr->mr.lkey;
  141. mr->ibmr.rkey = mr->mr.lkey;
  142. done:
  143. return mr;
  144. bail_mregion:
  145. deinit_qib_mregion(&mr->mr);
  146. bail:
  147. kfree(mr);
  148. mr = ERR_PTR(rval);
  149. goto done;
  150. }
  151. /**
  152. * qib_reg_phys_mr - register a physical memory region
  153. * @pd: protection domain for this memory region
  154. * @buffer_list: pointer to the list of physical buffers to register
  155. * @num_phys_buf: the number of physical buffers to register
  156. * @iova_start: the starting address passed over IB which maps to this MR
  157. *
  158. * Returns the memory region on success, otherwise returns an errno.
  159. */
  160. struct ib_mr *qib_reg_phys_mr(struct ib_pd *pd,
  161. struct ib_phys_buf *buffer_list,
  162. int num_phys_buf, int acc, u64 *iova_start)
  163. {
  164. struct qib_mr *mr;
  165. int n, m, i;
  166. struct ib_mr *ret;
  167. mr = alloc_mr(num_phys_buf, pd);
  168. if (IS_ERR(mr)) {
  169. ret = (struct ib_mr *)mr;
  170. goto bail;
  171. }
  172. mr->mr.user_base = *iova_start;
  173. mr->mr.iova = *iova_start;
  174. mr->mr.access_flags = acc;
  175. m = 0;
  176. n = 0;
  177. for (i = 0; i < num_phys_buf; i++) {
  178. mr->mr.map[m]->segs[n].vaddr = (void *) buffer_list[i].addr;
  179. mr->mr.map[m]->segs[n].length = buffer_list[i].size;
  180. mr->mr.length += buffer_list[i].size;
  181. n++;
  182. if (n == QIB_SEGSZ) {
  183. m++;
  184. n = 0;
  185. }
  186. }
  187. ret = &mr->ibmr;
  188. bail:
  189. return ret;
  190. }
  191. /**
  192. * qib_reg_user_mr - register a userspace memory region
  193. * @pd: protection domain for this memory region
  194. * @start: starting userspace address
  195. * @length: length of region to register
  196. * @mr_access_flags: access flags for this memory region
  197. * @udata: unused by the QLogic_IB driver
  198. *
  199. * Returns the memory region on success, otherwise returns an errno.
  200. */
  201. struct ib_mr *qib_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  202. u64 virt_addr, int mr_access_flags,
  203. struct ib_udata *udata)
  204. {
  205. struct qib_mr *mr;
  206. struct ib_umem *umem;
  207. struct scatterlist *sg;
  208. int n, m, entry;
  209. struct ib_mr *ret;
  210. if (length == 0) {
  211. ret = ERR_PTR(-EINVAL);
  212. goto bail;
  213. }
  214. umem = ib_umem_get(pd->uobject->context, start, length,
  215. mr_access_flags, 0);
  216. if (IS_ERR(umem))
  217. return (void *) umem;
  218. n = umem->nmap;
  219. mr = alloc_mr(n, pd);
  220. if (IS_ERR(mr)) {
  221. ret = (struct ib_mr *)mr;
  222. ib_umem_release(umem);
  223. goto bail;
  224. }
  225. mr->mr.user_base = start;
  226. mr->mr.iova = virt_addr;
  227. mr->mr.length = length;
  228. mr->mr.offset = ib_umem_offset(umem);
  229. mr->mr.access_flags = mr_access_flags;
  230. mr->umem = umem;
  231. if (is_power_of_2(umem->page_size))
  232. mr->mr.page_shift = ilog2(umem->page_size);
  233. m = 0;
  234. n = 0;
  235. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  236. void *vaddr;
  237. vaddr = page_address(sg_page(sg));
  238. if (!vaddr) {
  239. ret = ERR_PTR(-EINVAL);
  240. goto bail;
  241. }
  242. mr->mr.map[m]->segs[n].vaddr = vaddr;
  243. mr->mr.map[m]->segs[n].length = umem->page_size;
  244. n++;
  245. if (n == QIB_SEGSZ) {
  246. m++;
  247. n = 0;
  248. }
  249. }
  250. ret = &mr->ibmr;
  251. bail:
  252. return ret;
  253. }
  254. /**
  255. * qib_dereg_mr - unregister and free a memory region
  256. * @ibmr: the memory region to free
  257. *
  258. * Returns 0 on success.
  259. *
  260. * Note that this is called to free MRs created by qib_get_dma_mr()
  261. * or qib_reg_user_mr().
  262. */
  263. int qib_dereg_mr(struct ib_mr *ibmr)
  264. {
  265. struct qib_mr *mr = to_imr(ibmr);
  266. int ret = 0;
  267. unsigned long timeout;
  268. kfree(mr->pages);
  269. qib_free_lkey(&mr->mr);
  270. qib_put_mr(&mr->mr); /* will set completion if last */
  271. timeout = wait_for_completion_timeout(&mr->mr.comp,
  272. 5 * HZ);
  273. if (!timeout) {
  274. qib_get_mr(&mr->mr);
  275. ret = -EBUSY;
  276. goto out;
  277. }
  278. deinit_qib_mregion(&mr->mr);
  279. if (mr->umem)
  280. ib_umem_release(mr->umem);
  281. kfree(mr);
  282. out:
  283. return ret;
  284. }
  285. /*
  286. * Allocate a memory region usable with the
  287. * IB_WR_REG_MR send work request.
  288. *
  289. * Return the memory region on success, otherwise return an errno.
  290. */
  291. struct ib_mr *qib_alloc_mr(struct ib_pd *pd,
  292. enum ib_mr_type mr_type,
  293. u32 max_num_sg)
  294. {
  295. struct qib_mr *mr;
  296. if (mr_type != IB_MR_TYPE_MEM_REG)
  297. return ERR_PTR(-EINVAL);
  298. mr = alloc_mr(max_num_sg, pd);
  299. if (IS_ERR(mr))
  300. return (struct ib_mr *)mr;
  301. mr->pages = kcalloc(max_num_sg, sizeof(u64), GFP_KERNEL);
  302. if (!mr->pages)
  303. goto err;
  304. return &mr->ibmr;
  305. err:
  306. qib_dereg_mr(&mr->ibmr);
  307. return ERR_PTR(-ENOMEM);
  308. }
  309. static int qib_set_page(struct ib_mr *ibmr, u64 addr)
  310. {
  311. struct qib_mr *mr = to_imr(ibmr);
  312. if (unlikely(mr->npages == mr->mr.max_segs))
  313. return -ENOMEM;
  314. mr->pages[mr->npages++] = addr;
  315. return 0;
  316. }
  317. int qib_map_mr_sg(struct ib_mr *ibmr,
  318. struct scatterlist *sg,
  319. int sg_nents)
  320. {
  321. struct qib_mr *mr = to_imr(ibmr);
  322. mr->npages = 0;
  323. return ib_sg_to_pages(ibmr, sg, sg_nents, qib_set_page);
  324. }
  325. /**
  326. * qib_alloc_fmr - allocate a fast memory region
  327. * @pd: the protection domain for this memory region
  328. * @mr_access_flags: access flags for this memory region
  329. * @fmr_attr: fast memory region attributes
  330. *
  331. * Returns the memory region on success, otherwise returns an errno.
  332. */
  333. struct ib_fmr *qib_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  334. struct ib_fmr_attr *fmr_attr)
  335. {
  336. struct qib_fmr *fmr;
  337. int m;
  338. struct ib_fmr *ret;
  339. int rval = -ENOMEM;
  340. /* Allocate struct plus pointers to first level page tables. */
  341. m = (fmr_attr->max_pages + QIB_SEGSZ - 1) / QIB_SEGSZ;
  342. fmr = kzalloc(sizeof(*fmr) + m * sizeof(fmr->mr.map[0]), GFP_KERNEL);
  343. if (!fmr)
  344. goto bail;
  345. rval = init_qib_mregion(&fmr->mr, pd, fmr_attr->max_pages);
  346. if (rval)
  347. goto bail;
  348. /*
  349. * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
  350. * rkey.
  351. */
  352. rval = qib_alloc_lkey(&fmr->mr, 0);
  353. if (rval)
  354. goto bail_mregion;
  355. fmr->ibfmr.rkey = fmr->mr.lkey;
  356. fmr->ibfmr.lkey = fmr->mr.lkey;
  357. /*
  358. * Resources are allocated but no valid mapping (RKEY can't be
  359. * used).
  360. */
  361. fmr->mr.access_flags = mr_access_flags;
  362. fmr->mr.max_segs = fmr_attr->max_pages;
  363. fmr->mr.page_shift = fmr_attr->page_shift;
  364. ret = &fmr->ibfmr;
  365. done:
  366. return ret;
  367. bail_mregion:
  368. deinit_qib_mregion(&fmr->mr);
  369. bail:
  370. kfree(fmr);
  371. ret = ERR_PTR(rval);
  372. goto done;
  373. }
  374. /**
  375. * qib_map_phys_fmr - set up a fast memory region
  376. * @ibmfr: the fast memory region to set up
  377. * @page_list: the list of pages to associate with the fast memory region
  378. * @list_len: the number of pages to associate with the fast memory region
  379. * @iova: the virtual address of the start of the fast memory region
  380. *
  381. * This may be called from interrupt context.
  382. */
  383. int qib_map_phys_fmr(struct ib_fmr *ibfmr, u64 *page_list,
  384. int list_len, u64 iova)
  385. {
  386. struct qib_fmr *fmr = to_ifmr(ibfmr);
  387. struct qib_lkey_table *rkt;
  388. unsigned long flags;
  389. int m, n, i;
  390. u32 ps;
  391. int ret;
  392. i = atomic_read(&fmr->mr.refcount);
  393. if (i > 2)
  394. return -EBUSY;
  395. if (list_len > fmr->mr.max_segs) {
  396. ret = -EINVAL;
  397. goto bail;
  398. }
  399. rkt = &to_idev(ibfmr->device)->lk_table;
  400. spin_lock_irqsave(&rkt->lock, flags);
  401. fmr->mr.user_base = iova;
  402. fmr->mr.iova = iova;
  403. ps = 1 << fmr->mr.page_shift;
  404. fmr->mr.length = list_len * ps;
  405. m = 0;
  406. n = 0;
  407. for (i = 0; i < list_len; i++) {
  408. fmr->mr.map[m]->segs[n].vaddr = (void *) page_list[i];
  409. fmr->mr.map[m]->segs[n].length = ps;
  410. if (++n == QIB_SEGSZ) {
  411. m++;
  412. n = 0;
  413. }
  414. }
  415. spin_unlock_irqrestore(&rkt->lock, flags);
  416. ret = 0;
  417. bail:
  418. return ret;
  419. }
  420. /**
  421. * qib_unmap_fmr - unmap fast memory regions
  422. * @fmr_list: the list of fast memory regions to unmap
  423. *
  424. * Returns 0 on success.
  425. */
  426. int qib_unmap_fmr(struct list_head *fmr_list)
  427. {
  428. struct qib_fmr *fmr;
  429. struct qib_lkey_table *rkt;
  430. unsigned long flags;
  431. list_for_each_entry(fmr, fmr_list, ibfmr.list) {
  432. rkt = &to_idev(fmr->ibfmr.device)->lk_table;
  433. spin_lock_irqsave(&rkt->lock, flags);
  434. fmr->mr.user_base = 0;
  435. fmr->mr.iova = 0;
  436. fmr->mr.length = 0;
  437. spin_unlock_irqrestore(&rkt->lock, flags);
  438. }
  439. return 0;
  440. }
  441. /**
  442. * qib_dealloc_fmr - deallocate a fast memory region
  443. * @ibfmr: the fast memory region to deallocate
  444. *
  445. * Returns 0 on success.
  446. */
  447. int qib_dealloc_fmr(struct ib_fmr *ibfmr)
  448. {
  449. struct qib_fmr *fmr = to_ifmr(ibfmr);
  450. int ret = 0;
  451. unsigned long timeout;
  452. qib_free_lkey(&fmr->mr);
  453. qib_put_mr(&fmr->mr); /* will set completion if last */
  454. timeout = wait_for_completion_timeout(&fmr->mr.comp,
  455. 5 * HZ);
  456. if (!timeout) {
  457. qib_get_mr(&fmr->mr);
  458. ret = -EBUSY;
  459. goto out;
  460. }
  461. deinit_qib_mregion(&fmr->mr);
  462. kfree(fmr);
  463. out:
  464. return ret;
  465. }
  466. void mr_rcu_callback(struct rcu_head *list)
  467. {
  468. struct qib_mregion *mr = container_of(list, struct qib_mregion, list);
  469. complete(&mr->comp);
  470. }