ipath_mr.c 10 KB

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