qib_mr.c 12 KB

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