ipath_mr.c 9.5 KB

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