ipath_mr.c 9.5 KB

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