ipath_mr.c 10 KB

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