ipath_mr.c 9.2 KB

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