ipath_mr.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. /*
  2. * Copyright (c) 2005, 2006 PathScale, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <rdma/ib_pack.h>
  33. #include <rdma/ib_smi.h>
  34. #include "ipath_verbs.h"
  35. /**
  36. * ipath_get_dma_mr - get a DMA memory region
  37. * @pd: protection domain for this memory region
  38. * @acc: access flags
  39. *
  40. * Returns the memory region on success, otherwise returns an errno.
  41. */
  42. struct ib_mr *ipath_get_dma_mr(struct ib_pd *pd, int acc)
  43. {
  44. struct ipath_mr *mr;
  45. struct ib_mr *ret;
  46. mr = kzalloc(sizeof *mr, GFP_KERNEL);
  47. if (!mr) {
  48. ret = ERR_PTR(-ENOMEM);
  49. goto bail;
  50. }
  51. mr->mr.access_flags = acc;
  52. ret = &mr->ibmr;
  53. bail:
  54. return ret;
  55. }
  56. static struct ipath_mr *alloc_mr(int count,
  57. struct ipath_lkey_table *lk_table)
  58. {
  59. struct ipath_mr *mr;
  60. int m, i = 0;
  61. /* Allocate struct plus pointers to first level page tables. */
  62. m = (count + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
  63. mr = kmalloc(sizeof *mr + m * sizeof mr->mr.map[0], GFP_KERNEL);
  64. if (!mr)
  65. goto done;
  66. /* Allocate first level page tables. */
  67. for (; i < m; i++) {
  68. mr->mr.map[i] = kmalloc(sizeof *mr->mr.map[0], GFP_KERNEL);
  69. if (!mr->mr.map[i])
  70. goto bail;
  71. }
  72. mr->mr.mapsz = m;
  73. /*
  74. * ib_reg_phys_mr() will initialize mr->ibmr except for
  75. * lkey and rkey.
  76. */
  77. if (!ipath_alloc_lkey(lk_table, &mr->mr))
  78. goto bail;
  79. mr->ibmr.rkey = mr->ibmr.lkey = mr->mr.lkey;
  80. goto done;
  81. bail:
  82. while (i) {
  83. i--;
  84. kfree(mr->mr.map[i]);
  85. }
  86. kfree(mr);
  87. mr = NULL;
  88. done:
  89. return mr;
  90. }
  91. /**
  92. * ipath_reg_phys_mr - register a physical memory region
  93. * @pd: protection domain for this memory region
  94. * @buffer_list: pointer to the list of physical buffers to register
  95. * @num_phys_buf: the number of physical buffers to register
  96. * @iova_start: the starting address passed over IB which maps to this MR
  97. *
  98. * Returns the memory region on success, otherwise returns an errno.
  99. */
  100. struct ib_mr *ipath_reg_phys_mr(struct ib_pd *pd,
  101. struct ib_phys_buf *buffer_list,
  102. int num_phys_buf, int acc, u64 *iova_start)
  103. {
  104. struct ipath_mr *mr;
  105. int n, m, i;
  106. struct ib_mr *ret;
  107. mr = alloc_mr(num_phys_buf, &to_idev(pd->device)->lk_table);
  108. if (mr == NULL) {
  109. ret = ERR_PTR(-ENOMEM);
  110. goto bail;
  111. }
  112. mr->mr.user_base = *iova_start;
  113. mr->mr.iova = *iova_start;
  114. mr->mr.length = 0;
  115. mr->mr.offset = 0;
  116. mr->mr.access_flags = acc;
  117. mr->mr.max_segs = num_phys_buf;
  118. m = 0;
  119. n = 0;
  120. for (i = 0; i < num_phys_buf; i++) {
  121. mr->mr.map[m]->segs[n].vaddr =
  122. phys_to_virt(buffer_list[i].addr);
  123. mr->mr.map[m]->segs[n].length = buffer_list[i].size;
  124. mr->mr.length += buffer_list[i].size;
  125. n++;
  126. if (n == IPATH_SEGSZ) {
  127. m++;
  128. n = 0;
  129. }
  130. }
  131. ret = &mr->ibmr;
  132. bail:
  133. return ret;
  134. }
  135. /**
  136. * ipath_reg_user_mr - register a userspace memory region
  137. * @pd: protection domain for this memory region
  138. * @region: the user memory region
  139. * @mr_access_flags: access flags for this memory region
  140. * @udata: unused by the InfiniPath driver
  141. *
  142. * Returns the memory region on success, otherwise returns an errno.
  143. */
  144. struct ib_mr *ipath_reg_user_mr(struct ib_pd *pd, struct ib_umem *region,
  145. int mr_access_flags, struct ib_udata *udata)
  146. {
  147. struct ipath_mr *mr;
  148. struct ib_umem_chunk *chunk;
  149. int n, m, i;
  150. struct ib_mr *ret;
  151. n = 0;
  152. list_for_each_entry(chunk, &region->chunk_list, list)
  153. n += chunk->nents;
  154. mr = alloc_mr(n, &to_idev(pd->device)->lk_table);
  155. if (!mr) {
  156. ret = ERR_PTR(-ENOMEM);
  157. goto bail;
  158. }
  159. mr->mr.user_base = region->user_base;
  160. mr->mr.iova = region->virt_base;
  161. mr->mr.length = region->length;
  162. mr->mr.offset = region->offset;
  163. mr->mr.access_flags = mr_access_flags;
  164. mr->mr.max_segs = n;
  165. m = 0;
  166. n = 0;
  167. list_for_each_entry(chunk, &region->chunk_list, list) {
  168. for (i = 0; i < chunk->nmap; i++) {
  169. mr->mr.map[m]->segs[n].vaddr =
  170. page_address(chunk->page_list[i].page);
  171. mr->mr.map[m]->segs[n].length = region->page_size;
  172. n++;
  173. if (n == IPATH_SEGSZ) {
  174. m++;
  175. n = 0;
  176. }
  177. }
  178. }
  179. ret = &mr->ibmr;
  180. bail:
  181. return ret;
  182. }
  183. /**
  184. * ipath_dereg_mr - unregister and free a memory region
  185. * @ibmr: the memory region to free
  186. *
  187. * Returns 0 on success.
  188. *
  189. * Note that this is called to free MRs created by ipath_get_dma_mr()
  190. * or ipath_reg_user_mr().
  191. */
  192. int ipath_dereg_mr(struct ib_mr *ibmr)
  193. {
  194. struct ipath_mr *mr = to_imr(ibmr);
  195. int i;
  196. ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);
  197. i = mr->mr.mapsz;
  198. while (i) {
  199. i--;
  200. kfree(mr->mr.map[i]);
  201. }
  202. kfree(mr);
  203. return 0;
  204. }
  205. /**
  206. * ipath_alloc_fmr - allocate a fast memory region
  207. * @pd: the protection domain for this memory region
  208. * @mr_access_flags: access flags for this memory region
  209. * @fmr_attr: fast memory region attributes
  210. *
  211. * Returns the memory region on success, otherwise returns an errno.
  212. */
  213. struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  214. struct ib_fmr_attr *fmr_attr)
  215. {
  216. struct ipath_fmr *fmr;
  217. int m, i = 0;
  218. struct ib_fmr *ret;
  219. /* Allocate struct plus pointers to first level page tables. */
  220. m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
  221. fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL);
  222. if (!fmr)
  223. goto bail;
  224. /* Allocate first level page tables. */
  225. for (; i < m; i++) {
  226. fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],
  227. GFP_KERNEL);
  228. if (!fmr->mr.map[i])
  229. goto bail;
  230. }
  231. fmr->mr.mapsz = m;
  232. /*
  233. * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
  234. * rkey.
  235. */
  236. if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))
  237. goto bail;
  238. fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;
  239. /*
  240. * Resources are allocated but no valid mapping (RKEY can't be
  241. * used).
  242. */
  243. fmr->mr.user_base = 0;
  244. fmr->mr.iova = 0;
  245. fmr->mr.length = 0;
  246. fmr->mr.offset = 0;
  247. fmr->mr.access_flags = mr_access_flags;
  248. fmr->mr.max_segs = fmr_attr->max_pages;
  249. fmr->page_shift = fmr_attr->page_shift;
  250. ret = &fmr->ibfmr;
  251. goto done;
  252. bail:
  253. while (i)
  254. kfree(fmr->mr.map[--i]);
  255. kfree(fmr);
  256. ret = ERR_PTR(-ENOMEM);
  257. done:
  258. return ret;
  259. }
  260. /**
  261. * ipath_map_phys_fmr - set up a fast memory region
  262. * @ibmfr: the fast memory region to set up
  263. * @page_list: the list of pages to associate with the fast memory region
  264. * @list_len: the number of pages to associate with the fast memory region
  265. * @iova: the virtual address of the start of the fast memory region
  266. *
  267. * This may be called from interrupt context.
  268. */
  269. int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,
  270. int list_len, u64 iova)
  271. {
  272. struct ipath_fmr *fmr = to_ifmr(ibfmr);
  273. struct ipath_lkey_table *rkt;
  274. unsigned long flags;
  275. int m, n, i;
  276. u32 ps;
  277. int ret;
  278. if (list_len > fmr->mr.max_segs) {
  279. ret = -EINVAL;
  280. goto bail;
  281. }
  282. rkt = &to_idev(ibfmr->device)->lk_table;
  283. spin_lock_irqsave(&rkt->lock, flags);
  284. fmr->mr.user_base = iova;
  285. fmr->mr.iova = iova;
  286. ps = 1 << fmr->page_shift;
  287. fmr->mr.length = list_len * ps;
  288. m = 0;
  289. n = 0;
  290. ps = 1 << fmr->page_shift;
  291. for (i = 0; i < list_len; i++) {
  292. fmr->mr.map[m]->segs[n].vaddr = phys_to_virt(page_list[i]);
  293. fmr->mr.map[m]->segs[n].length = ps;
  294. if (++n == IPATH_SEGSZ) {
  295. m++;
  296. n = 0;
  297. }
  298. }
  299. spin_unlock_irqrestore(&rkt->lock, flags);
  300. ret = 0;
  301. bail:
  302. return ret;
  303. }
  304. /**
  305. * ipath_unmap_fmr - unmap fast memory regions
  306. * @fmr_list: the list of fast memory regions to unmap
  307. *
  308. * Returns 0 on success.
  309. */
  310. int ipath_unmap_fmr(struct list_head *fmr_list)
  311. {
  312. struct ipath_fmr *fmr;
  313. struct ipath_lkey_table *rkt;
  314. unsigned long flags;
  315. list_for_each_entry(fmr, fmr_list, ibfmr.list) {
  316. rkt = &to_idev(fmr->ibfmr.device)->lk_table;
  317. spin_lock_irqsave(&rkt->lock, flags);
  318. fmr->mr.user_base = 0;
  319. fmr->mr.iova = 0;
  320. fmr->mr.length = 0;
  321. spin_unlock_irqrestore(&rkt->lock, flags);
  322. }
  323. return 0;
  324. }
  325. /**
  326. * ipath_dealloc_fmr - deallocate a fast memory region
  327. * @ibfmr: the fast memory region to deallocate
  328. *
  329. * Returns 0 on success.
  330. */
  331. int ipath_dealloc_fmr(struct ib_fmr *ibfmr)
  332. {
  333. struct ipath_fmr *fmr = to_ifmr(ibfmr);
  334. int i;
  335. ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);
  336. i = fmr->mr.mapsz;
  337. while (i)
  338. kfree(fmr->mr.map[--i]);
  339. kfree(fmr);
  340. return 0;
  341. }