ipath_mr.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2006, 2007 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 <linux/slab.h>
  34. #include <rdma/ib_umem.h>
  35. #include <rdma/ib_pack.h>
  36. #include <rdma/ib_smi.h>
  37. #include "ipath_verbs.h"
  38. /* Fast memory region */
  39. struct ipath_fmr {
  40. struct ib_fmr ibfmr;
  41. u8 page_shift;
  42. struct ipath_mregion mr; /* must be last */
  43. };
  44. static inline struct ipath_fmr *to_ifmr(struct ib_fmr *ibfmr)
  45. {
  46. return container_of(ibfmr, struct ipath_fmr, ibfmr);
  47. }
  48. /**
  49. * ipath_get_dma_mr - get a DMA memory region
  50. * @pd: protection domain for this memory region
  51. * @acc: access flags
  52. *
  53. * Returns the memory region on success, otherwise returns an errno.
  54. * Note that all DMA addresses should be created via the
  55. * struct ib_dma_mapping_ops functions (see ipath_dma.c).
  56. */
  57. struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)
  58. {
  59. struct ipath_mr *mr;
  60. struct ib_mr *ret;
  61. mr = kzalloc(sizeof *mr, GFP_KERNEL);
  62. if (!mr) {
  63. ret = ERR_PTR(-ENOMEM);
  64. goto bail;
  65. }
  66. mr->mr.access_flags = acc;
  67. ret = &mr->ibmr;
  68. bail:
  69. return ret;
  70. }
  71. static struct ipath_mr *alloc_mr(int count,
  72. struct ipath_lkey_table *lk_table)
  73. {
  74. struct ipath_mr *mr;
  75. int m, i = 0;
  76. /* Allocate struct plus pointers to first level page tables. */
  77. m = (count + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
  78. mr = kmalloc(sizeof *mr + m * sizeof mr->mr.map[0], GFP_KERNEL);
  79. if (!mr)
  80. goto done;
  81. /* Allocate first level page tables. */
  82. for (; i < m; i++) {
  83. mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL);
  84. if (!mr->mr.map[i])
  85. goto bail;
  86. }
  87. mr->mr.mapsz = m;
  88. /*
  89. * ib_reg_phys_mr() will initialize mr->ibmr except for
  90. * lkey and rkey.
  91. */
  92. if (!ipath_alloc_lkey(lk_table, &mr->mr))
  93. goto bail;
  94. mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey;
  95. goto done;
  96. bail:
  97. while (i) {
  98. i--;
  99. kfree(mr->mr.map[i]);
  100. }
  101. kfree(mr);
  102. mr = NULL;
  103. done:
  104. return mr;
  105. }
  106. /**
  107. * ipath_reg_phys_mr - register a physical memory region
  108. * @pd: protection domain for this memory region
  109. * @buffer_list: pointer to the list of physical buffers to register
  110. * @num_phys_buf: the number of physical buffers to register
  111. * @iova_start: the starting address passed over IB which maps to this MR
  112. *
  113. * Returns the memory region on success, otherwise returns an errno.
  114. */
  115. struct ib_mr *ipath_reg_phys_mr(struct ib_pd *pd,
  116. struct ib_phys_buf *buffer_list,
  117. int num_phys_buf, int acc, u64 *iova_start)
  118. {
  119. struct ipath_mr *mr;
  120. int n, m, i;
  121. struct ib_mr *ret;
  122. mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table);
  123. if (mr == NULL) {
  124. ret = ERR_PTR(-ENOMEM);
  125. goto bail;
  126. }
  127. mr->mr.pd = pd;
  128. mr->mr.user_base = *iova_start;
  129. mr->mr.iova = *iova_start;
  130. mr->mr.length = 0;
  131. mr->mr.offset = 0;
  132. mr->mr.access_flags = acc;
  133. mr->mr.max_segs = num_phys_buf;
  134. mr->umem = NULL;
  135. m = 0;
  136. n = 0;
  137. for (i = 0; i < num_phys_buf; i++) {
  138. mr->mr.map[m]->segs[n].vaddr = (void *) buffer_list[i].addr;
  139. mr->mr.map[m]->segs[n].length = buffer_list[i].size;
  140. mr->mr.length += buffer_list[i].size;
  141. n++;
  142. if (n == IPATH_SEGSZ) {
  143. m++;
  144. n = 0;
  145. }
  146. }
  147. ret = &mr->ibmr;
  148. bail:
  149. return ret;
  150. }
  151. /**
  152. * ipath_reg_user_mr - register a userspace memory region
  153. * @pd: protection domain for this memory region
  154. * @start: starting userspace address
  155. * @length: length of region to register
  156. * @virt_addr: virtual address to use (from HCA's point of view)
  157. * @mr_access_flags: access flags for this memory region
  158. * @udata: unused by the InfiniPath driver
  159. *
  160. * Returns the memory region on success, otherwise returns an errno.
  161. */
  162. struct ib_mr *ipath_reg_user_mr(struct ib_pd *pd, u64 start, u64 length,
  163. u64 virt_addr, int mr_access_flags,
  164. struct ib_udata *udata)
  165. {
  166. struct ipath_mr *mr;
  167. struct ib_umem *umem;
  168. int n, m, entry;
  169. struct scatterlist *sg;
  170. struct ib_mr *ret;
  171. if (length == 0) {
  172. ret = ERR_PTR(-EINVAL);
  173. goto bail;
  174. }
  175. umem = ib_umem_get(pd->uobject->context, start, length,
  176. mr_access_flags, 0);
  177. if (IS_ERR(umem))
  178. return (void *) umem;
  179. n = umem->nmap;
  180. mr = alloc_mr(n, &to_idev(pd->device)->lk_table);
  181. if (!mr) {
  182. ret = ERR_PTR(-ENOMEM);
  183. ib_umem_release(umem);
  184. goto bail;
  185. }
  186. mr->mr.pd = pd;
  187. mr->mr.user_base = start;
  188. mr->mr.iova = virt_addr;
  189. mr->mr.length = length;
  190. mr->mr.offset = umem->offset;
  191. mr->mr.access_flags = mr_access_flags;
  192. mr->mr.max_segs = n;
  193. mr->umem = umem;
  194. m = 0;
  195. n = 0;
  196. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  197. void *vaddr;
  198. vaddr = page_address(sg_page(sg));
  199. if (!vaddr) {
  200. ret = ERR_PTR(-EINVAL);
  201. goto bail;
  202. }
  203. mr->mr.map[m]->segs[n].vaddr = vaddr;
  204. mr->mr.map[m]->segs[n].length = umem->page_size;
  205. n++;
  206. if (n == IPATH_SEGSZ) {
  207. m++;
  208. n = 0;
  209. }
  210. }
  211. ret = &mr->ibmr;
  212. bail:
  213. return ret;
  214. }
  215. /**
  216. * ipath_dereg_mr - unregister and free a memory region
  217. * @ibmr: the memory region to free
  218. *
  219. * Returns 0 on success.
  220. *
  221. * Note that this is called to free MRs created by ipath_get_dma_mr()
  222. * or ipath_reg_user_mr().
  223. */
  224. int ipath_dereg_mr(struct ib_mr *ibmr)
  225. {
  226. struct ipath_mr *mr = to_imr(ibmr);
  227. int i;
  228. ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);
  229. i = mr->mr.mapsz;
  230. while (i) {
  231. i--;
  232. kfree(mr->mr.map[i]);
  233. }
  234. if (mr->umem)
  235. ib_umem_release(mr->umem);
  236. kfree(mr);
  237. return 0;
  238. }
  239. /**
  240. * ipath_alloc_fmr - allocate a fast memory region
  241. * @pd: the protection domain for this memory region
  242. * @mr_access_flags: access flags for this memory region
  243. * @fmr_attr: fast memory region attributes
  244. *
  245. * Returns the memory region on success, otherwise returns an errno.
  246. */
  247. struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  248. struct ib_fmr_attr *fmr_attr)
  249. {
  250. struct ipath_fmr *fmr;
  251. int m, i = 0;
  252. struct ib_fmr *ret;
  253. /* Allocate struct plus pointers to first level page tables. */
  254. m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
  255. fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL);
  256. if (!fmr)
  257. goto bail;
  258. /* Allocate first level page tables. */
  259. for (; i < m; i++) {
  260. fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],
  261. GFP_KERNEL);
  262. if (!fmr->mr.map[i])
  263. goto bail;
  264. }
  265. fmr->mr.mapsz = m;
  266. /*
  267. * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
  268. * rkey.
  269. */
  270. if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))
  271. goto bail;
  272. fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;
  273. /*
  274. * Resources are allocated but no valid mapping (RKEY can't be
  275. * used).
  276. */
  277. fmr->mr.pd = pd;
  278. fmr->mr.user_base = 0;
  279. fmr->mr.iova = 0;
  280. fmr->mr.length = 0;
  281. fmr->mr.offset = 0;
  282. fmr->mr.access_flags = mr_access_flags;
  283. fmr->mr.max_segs = fmr_attr->max_pages;
  284. fmr->page_shift = fmr_attr->page_shift;
  285. ret = &fmr->ibfmr;
  286. goto done;
  287. bail:
  288. while (i)
  289. kfree(fmr->mr.map[--i]);
  290. kfree(fmr);
  291. ret = ERR_PTR(-ENOMEM);
  292. done:
  293. return ret;
  294. }
  295. /**
  296. * ipath_map_phys_fmr - set up a fast memory region
  297. * @ibmfr: the fast memory region to set up
  298. * @page_list: the list of pages to associate with the fast memory region
  299. * @list_len: the number of pages to associate with the fast memory region
  300. * @iova: the virtual address of the start of the fast memory region
  301. *
  302. * This may be called from interrupt context.
  303. */
  304. int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,
  305. int list_len, u64 iova)
  306. {
  307. struct ipath_fmr *fmr = to_ifmr(ibfmr);
  308. struct ipath_lkey_table *rkt;
  309. unsigned long flags;
  310. int m, n, i;
  311. u32 ps;
  312. int ret;
  313. if (list_len > fmr->mr.max_segs) {
  314. ret = -EINVAL;
  315. goto bail;
  316. }
  317. rkt = &to_idev(ibfmr->device)->lk_table;
  318. spin_lock_irqsave(&rkt->lock, flags);
  319. fmr->mr.user_base = iova;
  320. fmr->mr.iova = iova;
  321. ps = 1 << fmr->page_shift;
  322. fmr->mr.length = list_len * ps;
  323. m = 0;
  324. n = 0;
  325. ps = 1 << fmr->page_shift;
  326. for (i = 0; i < list_len; i++) {
  327. fmr->mr.map[m]->segs[n].vaddr = (void *) page_list[i];
  328. fmr->mr.map[m]->segs[n].length = ps;
  329. if (++n == IPATH_SEGSZ) {
  330. m++;
  331. n = 0;
  332. }
  333. }
  334. spin_unlock_irqrestore(&rkt->lock, flags);
  335. ret = 0;
  336. bail:
  337. return ret;
  338. }
  339. /**
  340. * ipath_unmap_fmr - unmap fast memory regions
  341. * @fmr_list: the list of fast memory regions to unmap
  342. *
  343. * Returns 0 on success.
  344. */
  345. int ipath_unmap_fmr(struct list_head *fmr_list)
  346. {
  347. struct ipath_fmr *fmr;
  348. struct ipath_lkey_table *rkt;
  349. unsigned long flags;
  350. list_for_each_entry(fmr, fmr_list, ibfmr.list) {
  351. rkt = &to_idev(fmr->ibfmr.device)->lk_table;
  352. spin_lock_irqsave(&rkt->lock, flags);
  353. fmr->mr.user_base = 0;
  354. fmr->mr.iova = 0;
  355. fmr->mr.length = 0;
  356. spin_unlock_irqrestore(&rkt->lock, flags);
  357. }
  358. return 0;
  359. }
  360. /**
  361. * ipath_dealloc_fmr - deallocate a fast memory region
  362. * @ibfmr: the fast memory region to deallocate
  363. *
  364. * Returns 0 on success.
  365. */
  366. int ipath_dealloc_fmr(struct ib_fmr *ibfmr)
  367. {
  368. struct ipath_fmr *fmr = to_ifmr(ibfmr);
  369. int i;
  370. ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);
  371. i = fmr->mr.mapsz;
  372. while (i)
  373. kfree(fmr->mr.map[--i]);
  374. kfree(fmr);
  375. return 0;
  376. }