ipath_mr.c 10 KB

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