ipath_mr.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. n = 0;
  153. list_for_each_entry(chunk, &region->chunk_list, list)
  154. n += chunk->nents;
  155. mr = alloc_mr(n, &to_idev(pd->device)->lk_table);
  156. if (!mr) {
  157. ret = ERR_PTR(-ENOMEM);
  158. goto bail;
  159. }
  160. mr->mr.user_base = region->user_base;
  161. mr->mr.iova = region->virt_base;
  162. mr->mr.length = region->length;
  163. mr->mr.offset = region->offset;
  164. mr->mr.access_flags = mr_access_flags;
  165. mr->mr.max_segs = n;
  166. m = 0;
  167. n = 0;
  168. list_for_each_entry(chunk, &region->chunk_list, list) {
  169. for (i = 0; i < chunk->nmap; i++) {
  170. mr->mr.map[m]->segs[n].vaddr =
  171. page_address(chunk->page_list[i].page);
  172. mr->mr.map[m]->segs[n].length = region->page_size;
  173. n++;
  174. if (n == IPATH_SEGSZ) {
  175. m++;
  176. n = 0;
  177. }
  178. }
  179. }
  180. ret = &mr->ibmr;
  181. bail:
  182. return ret;
  183. }
  184. /**
  185. * ipath_dereg_mr - unregister and free a memory region
  186. * @ibmr: the memory region to free
  187. *
  188. * Returns 0 on success.
  189. *
  190. * Note that this is called to free MRs created by ipath_get_dma_mr()
  191. * or ipath_reg_user_mr().
  192. */
  193. int ipath_dereg_mr(struct ib_mr *ibmr)
  194. {
  195. struct ipath_mr *mr = to_imr(ibmr);
  196. int i;
  197. ipath_free_lkey(&to_idev(ibmr->device)->lk_table, ibmr->lkey);
  198. i = mr->mr.mapsz;
  199. while (i) {
  200. i--;
  201. kfree(mr->mr.map[i]);
  202. }
  203. kfree(mr);
  204. return 0;
  205. }
  206. /**
  207. * ipath_alloc_fmr - allocate a fast memory region
  208. * @pd: the protection domain for this memory region
  209. * @mr_access_flags: access flags for this memory region
  210. * @fmr_attr: fast memory region attributes
  211. *
  212. * Returns the memory region on success, otherwise returns an errno.
  213. */
  214. struct ib_fmr *ipath_alloc_fmr(struct ib_pd *pd, int mr_access_flags,
  215. struct ib_fmr_attr *fmr_attr)
  216. {
  217. struct ipath_fmr *fmr;
  218. int m, i = 0;
  219. struct ib_fmr *ret;
  220. /* Allocate struct plus pointers to first level page tables. */
  221. m = (fmr_attr->max_pages + IPATH_SEGSZ - 1) / IPATH_SEGSZ;
  222. fmr = kmalloc(sizeof *fmr + m * sizeof fmr->mr.map[0], GFP_KERNEL);
  223. if (!fmr)
  224. goto bail;
  225. /* Allocate first level page tables. */
  226. for (; i < m; i++) {
  227. fmr->mr.map[i] = kmalloc(sizeof *fmr->mr.map[0],
  228. GFP_KERNEL);
  229. if (!fmr->mr.map[i])
  230. goto bail;
  231. }
  232. fmr->mr.mapsz = m;
  233. /*
  234. * ib_alloc_fmr() will initialize fmr->ibfmr except for lkey &
  235. * rkey.
  236. */
  237. if (!ipath_alloc_lkey(&to_idev(pd->device)->lk_table, &fmr->mr))
  238. goto bail;
  239. fmr->ibfmr.rkey = fmr->ibfmr.lkey = fmr->mr.lkey;
  240. /*
  241. * Resources are allocated but no valid mapping (RKEY can't be
  242. * used).
  243. */
  244. fmr->mr.user_base = 0;
  245. fmr->mr.iova = 0;
  246. fmr->mr.length = 0;
  247. fmr->mr.offset = 0;
  248. fmr->mr.access_flags = mr_access_flags;
  249. fmr->mr.max_segs = fmr_attr->max_pages;
  250. fmr->page_shift = fmr_attr->page_shift;
  251. ret = &fmr->ibfmr;
  252. goto done;
  253. bail:
  254. while (i)
  255. kfree(fmr->mr.map[--i]);
  256. kfree(fmr);
  257. ret = ERR_PTR(-ENOMEM);
  258. done:
  259. return ret;
  260. }
  261. /**
  262. * ipath_map_phys_fmr - set up a fast memory region
  263. * @ibmfr: the fast memory region to set up
  264. * @page_list: the list of pages to associate with the fast memory region
  265. * @list_len: the number of pages to associate with the fast memory region
  266. * @iova: the virtual address of the start of the fast memory region
  267. *
  268. * This may be called from interrupt context.
  269. */
  270. int ipath_map_phys_fmr(struct ib_fmr *ibfmr, u64 * page_list,
  271. int list_len, u64 iova)
  272. {
  273. struct ipath_fmr *fmr = to_ifmr(ibfmr);
  274. struct ipath_lkey_table *rkt;
  275. unsigned long flags;
  276. int m, n, i;
  277. u32 ps;
  278. int ret;
  279. if (list_len > fmr->mr.max_segs) {
  280. ret = -EINVAL;
  281. goto bail;
  282. }
  283. rkt = &to_idev(ibfmr->device)->lk_table;
  284. spin_lock_irqsave(&rkt->lock, flags);
  285. fmr->mr.user_base = iova;
  286. fmr->mr.iova = iova;
  287. ps = 1 << fmr->page_shift;
  288. fmr->mr.length = list_len * ps;
  289. m = 0;
  290. n = 0;
  291. ps = 1 << fmr->page_shift;
  292. for (i = 0; i < list_len; i++) {
  293. fmr->mr.map[m]->segs[n].vaddr = phys_to_virt(page_list[i]);
  294. fmr->mr.map[m]->segs[n].length = ps;
  295. if (++n == IPATH_SEGSZ) {
  296. m++;
  297. n = 0;
  298. }
  299. }
  300. spin_unlock_irqrestore(&rkt->lock, flags);
  301. ret = 0;
  302. bail:
  303. return ret;
  304. }
  305. /**
  306. * ipath_unmap_fmr - unmap fast memory regions
  307. * @fmr_list: the list of fast memory regions to unmap
  308. *
  309. * Returns 0 on success.
  310. */
  311. int ipath_unmap_fmr(struct list_head *fmr_list)
  312. {
  313. struct ipath_fmr *fmr;
  314. struct ipath_lkey_table *rkt;
  315. unsigned long flags;
  316. list_for_each_entry(fmr, fmr_list, ibfmr.list) {
  317. rkt = &to_idev(fmr->ibfmr.device)->lk_table;
  318. spin_lock_irqsave(&rkt->lock, flags);
  319. fmr->mr.user_base = 0;
  320. fmr->mr.iova = 0;
  321. fmr->mr.length = 0;
  322. spin_unlock_irqrestore(&rkt->lock, flags);
  323. }
  324. return 0;
  325. }
  326. /**
  327. * ipath_dealloc_fmr - deallocate a fast memory region
  328. * @ibfmr: the fast memory region to deallocate
  329. *
  330. * Returns 0 on success.
  331. */
  332. int ipath_dealloc_fmr(struct ib_fmr *ibfmr)
  333. {
  334. struct ipath_fmr *fmr = to_ifmr(ibfmr);
  335. int i;
  336. ipath_free_lkey(&to_idev(ibfmr->device)->lk_table, ibfmr->lkey);
  337. i = fmr->mr.mapsz;
  338. while (i)
  339. kfree(fmr->mr.map[--i]);
  340. kfree(fmr);
  341. return 0;
  342. }