rxe_mr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. /*
  2. * Copyright (c) 2016 Mellanox Technologies Ltd. All rights reserved.
  3. * Copyright (c) 2015 System Fabric Works, 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 "rxe.h"
  34. #include "rxe_loc.h"
  35. /*
  36. * lfsr (linear feedback shift register) with period 255
  37. */
  38. static u8 rxe_get_key(void)
  39. {
  40. static u32 key = 1;
  41. key = key << 1;
  42. key |= (0 != (key & 0x100)) ^ (0 != (key & 0x10))
  43. ^ (0 != (key & 0x80)) ^ (0 != (key & 0x40));
  44. key &= 0xff;
  45. return key;
  46. }
  47. int mem_check_range(struct rxe_mem *mem, u64 iova, size_t length)
  48. {
  49. switch (mem->type) {
  50. case RXE_MEM_TYPE_DMA:
  51. return 0;
  52. case RXE_MEM_TYPE_MR:
  53. case RXE_MEM_TYPE_FMR:
  54. if (iova < mem->iova ||
  55. length > mem->length ||
  56. iova > mem->iova + mem->length - length)
  57. return -EFAULT;
  58. return 0;
  59. default:
  60. return -EFAULT;
  61. }
  62. }
  63. #define IB_ACCESS_REMOTE (IB_ACCESS_REMOTE_READ \
  64. | IB_ACCESS_REMOTE_WRITE \
  65. | IB_ACCESS_REMOTE_ATOMIC)
  66. static void rxe_mem_init(int access, struct rxe_mem *mem)
  67. {
  68. u32 lkey = mem->pelem.index << 8 | rxe_get_key();
  69. u32 rkey = (access & IB_ACCESS_REMOTE) ? lkey : 0;
  70. if (mem->pelem.pool->type == RXE_TYPE_MR) {
  71. mem->ibmr.lkey = lkey;
  72. mem->ibmr.rkey = rkey;
  73. }
  74. mem->lkey = lkey;
  75. mem->rkey = rkey;
  76. mem->state = RXE_MEM_STATE_INVALID;
  77. mem->type = RXE_MEM_TYPE_NONE;
  78. mem->map_shift = ilog2(RXE_BUF_PER_MAP);
  79. }
  80. void rxe_mem_cleanup(void *arg)
  81. {
  82. struct rxe_mem *mem = arg;
  83. int i;
  84. if (mem->umem)
  85. ib_umem_release(mem->umem);
  86. if (mem->map) {
  87. for (i = 0; i < mem->num_map; i++)
  88. kfree(mem->map[i]);
  89. kfree(mem->map);
  90. }
  91. }
  92. static int rxe_mem_alloc(struct rxe_dev *rxe, struct rxe_mem *mem, int num_buf)
  93. {
  94. int i;
  95. int num_map;
  96. struct rxe_map **map = mem->map;
  97. num_map = (num_buf + RXE_BUF_PER_MAP - 1) / RXE_BUF_PER_MAP;
  98. mem->map = kmalloc_array(num_map, sizeof(*map), GFP_KERNEL);
  99. if (!mem->map)
  100. goto err1;
  101. for (i = 0; i < num_map; i++) {
  102. mem->map[i] = kmalloc(sizeof(**map), GFP_KERNEL);
  103. if (!mem->map[i])
  104. goto err2;
  105. }
  106. WARN_ON(!is_power_of_2(RXE_BUF_PER_MAP));
  107. mem->map_shift = ilog2(RXE_BUF_PER_MAP);
  108. mem->map_mask = RXE_BUF_PER_MAP - 1;
  109. mem->num_buf = num_buf;
  110. mem->num_map = num_map;
  111. mem->max_buf = num_map * RXE_BUF_PER_MAP;
  112. return 0;
  113. err2:
  114. for (i--; i >= 0; i--)
  115. kfree(mem->map[i]);
  116. kfree(mem->map);
  117. err1:
  118. return -ENOMEM;
  119. }
  120. int rxe_mem_init_dma(struct rxe_dev *rxe, struct rxe_pd *pd,
  121. int access, struct rxe_mem *mem)
  122. {
  123. rxe_mem_init(access, mem);
  124. mem->pd = pd;
  125. mem->access = access;
  126. mem->state = RXE_MEM_STATE_VALID;
  127. mem->type = RXE_MEM_TYPE_DMA;
  128. return 0;
  129. }
  130. int rxe_mem_init_user(struct rxe_dev *rxe, struct rxe_pd *pd, u64 start,
  131. u64 length, u64 iova, int access, struct ib_udata *udata,
  132. struct rxe_mem *mem)
  133. {
  134. int entry;
  135. struct rxe_map **map;
  136. struct rxe_phys_buf *buf = NULL;
  137. struct ib_umem *umem;
  138. struct scatterlist *sg;
  139. int num_buf;
  140. void *vaddr;
  141. int err;
  142. umem = ib_umem_get(pd->ibpd.uobject->context, start, length, access, 0);
  143. if (IS_ERR(umem)) {
  144. pr_warn("err %d from rxe_umem_get\n",
  145. (int)PTR_ERR(umem));
  146. err = -EINVAL;
  147. goto err1;
  148. }
  149. mem->umem = umem;
  150. num_buf = umem->nmap;
  151. rxe_mem_init(access, mem);
  152. err = rxe_mem_alloc(rxe, mem, num_buf);
  153. if (err) {
  154. pr_warn("err %d from rxe_mem_alloc\n", err);
  155. ib_umem_release(umem);
  156. goto err1;
  157. }
  158. WARN_ON(!is_power_of_2(umem->page_size));
  159. mem->page_shift = ilog2(umem->page_size);
  160. mem->page_mask = umem->page_size - 1;
  161. num_buf = 0;
  162. map = mem->map;
  163. if (length > 0) {
  164. buf = map[0]->buf;
  165. for_each_sg(umem->sg_head.sgl, sg, umem->nmap, entry) {
  166. vaddr = page_address(sg_page(sg));
  167. if (!vaddr) {
  168. pr_warn("null vaddr\n");
  169. err = -ENOMEM;
  170. goto err1;
  171. }
  172. buf->addr = (uintptr_t)vaddr;
  173. buf->size = umem->page_size;
  174. num_buf++;
  175. buf++;
  176. if (num_buf >= RXE_BUF_PER_MAP) {
  177. map++;
  178. buf = map[0]->buf;
  179. num_buf = 0;
  180. }
  181. }
  182. }
  183. mem->pd = pd;
  184. mem->umem = umem;
  185. mem->access = access;
  186. mem->length = length;
  187. mem->iova = iova;
  188. mem->va = start;
  189. mem->offset = ib_umem_offset(umem);
  190. mem->state = RXE_MEM_STATE_VALID;
  191. mem->type = RXE_MEM_TYPE_MR;
  192. return 0;
  193. err1:
  194. return err;
  195. }
  196. int rxe_mem_init_fast(struct rxe_dev *rxe, struct rxe_pd *pd,
  197. int max_pages, struct rxe_mem *mem)
  198. {
  199. int err;
  200. rxe_mem_init(0, mem);
  201. /* In fastreg, we also set the rkey */
  202. mem->ibmr.rkey = mem->ibmr.lkey;
  203. err = rxe_mem_alloc(rxe, mem, max_pages);
  204. if (err)
  205. goto err1;
  206. mem->pd = pd;
  207. mem->max_buf = max_pages;
  208. mem->state = RXE_MEM_STATE_FREE;
  209. mem->type = RXE_MEM_TYPE_MR;
  210. return 0;
  211. err1:
  212. return err;
  213. }
  214. static void lookup_iova(
  215. struct rxe_mem *mem,
  216. u64 iova,
  217. int *m_out,
  218. int *n_out,
  219. size_t *offset_out)
  220. {
  221. size_t offset = iova - mem->iova + mem->offset;
  222. int map_index;
  223. int buf_index;
  224. u64 length;
  225. if (likely(mem->page_shift)) {
  226. *offset_out = offset & mem->page_mask;
  227. offset >>= mem->page_shift;
  228. *n_out = offset & mem->map_mask;
  229. *m_out = offset >> mem->map_shift;
  230. } else {
  231. map_index = 0;
  232. buf_index = 0;
  233. length = mem->map[map_index]->buf[buf_index].size;
  234. while (offset >= length) {
  235. offset -= length;
  236. buf_index++;
  237. if (buf_index == RXE_BUF_PER_MAP) {
  238. map_index++;
  239. buf_index = 0;
  240. }
  241. length = mem->map[map_index]->buf[buf_index].size;
  242. }
  243. *m_out = map_index;
  244. *n_out = buf_index;
  245. *offset_out = offset;
  246. }
  247. }
  248. void *iova_to_vaddr(struct rxe_mem *mem, u64 iova, int length)
  249. {
  250. size_t offset;
  251. int m, n;
  252. void *addr;
  253. if (mem->state != RXE_MEM_STATE_VALID) {
  254. pr_warn("mem not in valid state\n");
  255. addr = NULL;
  256. goto out;
  257. }
  258. if (!mem->map) {
  259. addr = (void *)(uintptr_t)iova;
  260. goto out;
  261. }
  262. if (mem_check_range(mem, iova, length)) {
  263. pr_warn("range violation\n");
  264. addr = NULL;
  265. goto out;
  266. }
  267. lookup_iova(mem, iova, &m, &n, &offset);
  268. if (offset + length > mem->map[m]->buf[n].size) {
  269. pr_warn("crosses page boundary\n");
  270. addr = NULL;
  271. goto out;
  272. }
  273. addr = (void *)(uintptr_t)mem->map[m]->buf[n].addr + offset;
  274. out:
  275. return addr;
  276. }
  277. /* copy data from a range (vaddr, vaddr+length-1) to or from
  278. * a mem object starting at iova. Compute incremental value of
  279. * crc32 if crcp is not zero. caller must hold a reference to mem
  280. */
  281. int rxe_mem_copy(struct rxe_mem *mem, u64 iova, void *addr, int length,
  282. enum copy_direction dir, u32 *crcp)
  283. {
  284. int err;
  285. int bytes;
  286. u8 *va;
  287. struct rxe_map **map;
  288. struct rxe_phys_buf *buf;
  289. int m;
  290. int i;
  291. size_t offset;
  292. u32 crc = crcp ? (*crcp) : 0;
  293. if (mem->type == RXE_MEM_TYPE_DMA) {
  294. u8 *src, *dest;
  295. src = (dir == to_mem_obj) ?
  296. addr : ((void *)(uintptr_t)iova);
  297. dest = (dir == to_mem_obj) ?
  298. ((void *)(uintptr_t)iova) : addr;
  299. if (crcp)
  300. *crcp = crc32_le(*crcp, src, length);
  301. memcpy(dest, src, length);
  302. return 0;
  303. }
  304. WARN_ON(!mem->map);
  305. err = mem_check_range(mem, iova, length);
  306. if (err) {
  307. err = -EFAULT;
  308. goto err1;
  309. }
  310. lookup_iova(mem, iova, &m, &i, &offset);
  311. map = mem->map + m;
  312. buf = map[0]->buf + i;
  313. while (length > 0) {
  314. u8 *src, *dest;
  315. va = (u8 *)(uintptr_t)buf->addr + offset;
  316. src = (dir == to_mem_obj) ? addr : va;
  317. dest = (dir == to_mem_obj) ? va : addr;
  318. bytes = buf->size - offset;
  319. if (bytes > length)
  320. bytes = length;
  321. if (crcp)
  322. crc = crc32_le(crc, src, bytes);
  323. memcpy(dest, src, bytes);
  324. length -= bytes;
  325. addr += bytes;
  326. offset = 0;
  327. buf++;
  328. i++;
  329. if (i == RXE_BUF_PER_MAP) {
  330. i = 0;
  331. map++;
  332. buf = map[0]->buf;
  333. }
  334. }
  335. if (crcp)
  336. *crcp = crc;
  337. return 0;
  338. err1:
  339. return err;
  340. }
  341. /* copy data in or out of a wqe, i.e. sg list
  342. * under the control of a dma descriptor
  343. */
  344. int copy_data(
  345. struct rxe_dev *rxe,
  346. struct rxe_pd *pd,
  347. int access,
  348. struct rxe_dma_info *dma,
  349. void *addr,
  350. int length,
  351. enum copy_direction dir,
  352. u32 *crcp)
  353. {
  354. int bytes;
  355. struct rxe_sge *sge = &dma->sge[dma->cur_sge];
  356. int offset = dma->sge_offset;
  357. int resid = dma->resid;
  358. struct rxe_mem *mem = NULL;
  359. u64 iova;
  360. int err;
  361. if (length == 0)
  362. return 0;
  363. if (length > resid) {
  364. err = -EINVAL;
  365. goto err2;
  366. }
  367. if (sge->length && (offset < sge->length)) {
  368. mem = lookup_mem(pd, access, sge->lkey, lookup_local);
  369. if (!mem) {
  370. err = -EINVAL;
  371. goto err1;
  372. }
  373. }
  374. while (length > 0) {
  375. bytes = length;
  376. if (offset >= sge->length) {
  377. if (mem) {
  378. rxe_drop_ref(mem);
  379. mem = NULL;
  380. }
  381. sge++;
  382. dma->cur_sge++;
  383. offset = 0;
  384. if (dma->cur_sge >= dma->num_sge) {
  385. err = -ENOSPC;
  386. goto err2;
  387. }
  388. if (sge->length) {
  389. mem = lookup_mem(pd, access, sge->lkey,
  390. lookup_local);
  391. if (!mem) {
  392. err = -EINVAL;
  393. goto err1;
  394. }
  395. } else {
  396. continue;
  397. }
  398. }
  399. if (bytes > sge->length - offset)
  400. bytes = sge->length - offset;
  401. if (bytes > 0) {
  402. iova = sge->addr + offset;
  403. err = rxe_mem_copy(mem, iova, addr, bytes, dir, crcp);
  404. if (err)
  405. goto err2;
  406. offset += bytes;
  407. resid -= bytes;
  408. length -= bytes;
  409. addr += bytes;
  410. }
  411. }
  412. dma->sge_offset = offset;
  413. dma->resid = resid;
  414. if (mem)
  415. rxe_drop_ref(mem);
  416. return 0;
  417. err2:
  418. if (mem)
  419. rxe_drop_ref(mem);
  420. err1:
  421. return err;
  422. }
  423. int advance_dma_data(struct rxe_dma_info *dma, unsigned int length)
  424. {
  425. struct rxe_sge *sge = &dma->sge[dma->cur_sge];
  426. int offset = dma->sge_offset;
  427. int resid = dma->resid;
  428. while (length) {
  429. unsigned int bytes;
  430. if (offset >= sge->length) {
  431. sge++;
  432. dma->cur_sge++;
  433. offset = 0;
  434. if (dma->cur_sge >= dma->num_sge)
  435. return -ENOSPC;
  436. }
  437. bytes = length;
  438. if (bytes > sge->length - offset)
  439. bytes = sge->length - offset;
  440. offset += bytes;
  441. resid -= bytes;
  442. length -= bytes;
  443. }
  444. dma->sge_offset = offset;
  445. dma->resid = resid;
  446. return 0;
  447. }
  448. /* (1) find the mem (mr or mw) corresponding to lkey/rkey
  449. * depending on lookup_type
  450. * (2) verify that the (qp) pd matches the mem pd
  451. * (3) verify that the mem can support the requested access
  452. * (4) verify that mem state is valid
  453. */
  454. struct rxe_mem *lookup_mem(struct rxe_pd *pd, int access, u32 key,
  455. enum lookup_type type)
  456. {
  457. struct rxe_mem *mem;
  458. struct rxe_dev *rxe = to_rdev(pd->ibpd.device);
  459. int index = key >> 8;
  460. if (index >= RXE_MIN_MR_INDEX && index <= RXE_MAX_MR_INDEX) {
  461. mem = rxe_pool_get_index(&rxe->mr_pool, index);
  462. if (!mem)
  463. goto err1;
  464. } else {
  465. goto err1;
  466. }
  467. if ((type == lookup_local && mem->lkey != key) ||
  468. (type == lookup_remote && mem->rkey != key))
  469. goto err2;
  470. if (mem->pd != pd)
  471. goto err2;
  472. if (access && !(access & mem->access))
  473. goto err2;
  474. if (mem->state != RXE_MEM_STATE_VALID)
  475. goto err2;
  476. return mem;
  477. err2:
  478. rxe_drop_ref(mem);
  479. err1:
  480. return NULL;
  481. }
  482. int rxe_mem_map_pages(struct rxe_dev *rxe, struct rxe_mem *mem,
  483. u64 *page, int num_pages, u64 iova)
  484. {
  485. int i;
  486. int num_buf;
  487. int err;
  488. struct rxe_map **map;
  489. struct rxe_phys_buf *buf;
  490. int page_size;
  491. if (num_pages > mem->max_buf) {
  492. err = -EINVAL;
  493. goto err1;
  494. }
  495. num_buf = 0;
  496. page_size = 1 << mem->page_shift;
  497. map = mem->map;
  498. buf = map[0]->buf;
  499. for (i = 0; i < num_pages; i++) {
  500. buf->addr = *page++;
  501. buf->size = page_size;
  502. buf++;
  503. num_buf++;
  504. if (num_buf == RXE_BUF_PER_MAP) {
  505. map++;
  506. buf = map[0]->buf;
  507. num_buf = 0;
  508. }
  509. }
  510. mem->iova = iova;
  511. mem->va = iova;
  512. mem->length = num_pages << mem->page_shift;
  513. mem->state = RXE_MEM_STATE_VALID;
  514. return 0;
  515. err1:
  516. return err;
  517. }