ipath_mr.c 9.6 KB

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