mr.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
  4. * Copyright (c) 2006, 2007 Cisco Systems, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/init.h>
  35. #include <linux/errno.h>
  36. #include <linux/export.h>
  37. #include <linux/slab.h>
  38. #include <linux/kernel.h>
  39. #include <linux/mlx4/cmd.h>
  40. #include "mlx4.h"
  41. #include "icm.h"
  42. #define MLX4_MPT_FLAG_SW_OWNS (0xfUL << 28)
  43. #define MLX4_MPT_FLAG_FREE (0x3UL << 28)
  44. #define MLX4_MPT_FLAG_MIO (1 << 17)
  45. #define MLX4_MPT_FLAG_BIND_ENABLE (1 << 15)
  46. #define MLX4_MPT_FLAG_PHYSICAL (1 << 9)
  47. #define MLX4_MPT_FLAG_REGION (1 << 8)
  48. #define MLX4_MPT_PD_FLAG_FAST_REG (1 << 27)
  49. #define MLX4_MPT_PD_FLAG_RAE (1 << 28)
  50. #define MLX4_MPT_PD_FLAG_EN_INV (3 << 24)
  51. #define MLX4_MPT_STATUS_SW 0xF0
  52. #define MLX4_MPT_STATUS_HW 0x00
  53. static u32 mlx4_buddy_alloc(struct mlx4_buddy *buddy, int order)
  54. {
  55. int o;
  56. int m;
  57. u32 seg;
  58. spin_lock(&buddy->lock);
  59. for (o = order; o <= buddy->max_order; ++o)
  60. if (buddy->num_free[o]) {
  61. m = 1 << (buddy->max_order - o);
  62. seg = find_first_bit(buddy->bits[o], m);
  63. if (seg < m)
  64. goto found;
  65. }
  66. spin_unlock(&buddy->lock);
  67. return -1;
  68. found:
  69. clear_bit(seg, buddy->bits[o]);
  70. --buddy->num_free[o];
  71. while (o > order) {
  72. --o;
  73. seg <<= 1;
  74. set_bit(seg ^ 1, buddy->bits[o]);
  75. ++buddy->num_free[o];
  76. }
  77. spin_unlock(&buddy->lock);
  78. seg <<= order;
  79. return seg;
  80. }
  81. static void mlx4_buddy_free(struct mlx4_buddy *buddy, u32 seg, int order)
  82. {
  83. seg >>= order;
  84. spin_lock(&buddy->lock);
  85. while (test_bit(seg ^ 1, buddy->bits[order])) {
  86. clear_bit(seg ^ 1, buddy->bits[order]);
  87. --buddy->num_free[order];
  88. seg >>= 1;
  89. ++order;
  90. }
  91. set_bit(seg, buddy->bits[order]);
  92. ++buddy->num_free[order];
  93. spin_unlock(&buddy->lock);
  94. }
  95. static int mlx4_buddy_init(struct mlx4_buddy *buddy, int max_order)
  96. {
  97. int i, s;
  98. buddy->max_order = max_order;
  99. spin_lock_init(&buddy->lock);
  100. buddy->bits = kzalloc((buddy->max_order + 1) * sizeof (long *),
  101. GFP_KERNEL);
  102. buddy->num_free = kcalloc((buddy->max_order + 1), sizeof *buddy->num_free,
  103. GFP_KERNEL);
  104. if (!buddy->bits || !buddy->num_free)
  105. goto err_out;
  106. for (i = 0; i <= buddy->max_order; ++i) {
  107. s = BITS_TO_LONGS(1 << (buddy->max_order - i));
  108. buddy->bits[i] = kmalloc(s * sizeof (long), GFP_KERNEL);
  109. if (!buddy->bits[i])
  110. goto err_out_free;
  111. bitmap_zero(buddy->bits[i], 1 << (buddy->max_order - i));
  112. }
  113. set_bit(0, buddy->bits[buddy->max_order]);
  114. buddy->num_free[buddy->max_order] = 1;
  115. return 0;
  116. err_out_free:
  117. for (i = 0; i <= buddy->max_order; ++i)
  118. kfree(buddy->bits[i]);
  119. err_out:
  120. kfree(buddy->bits);
  121. kfree(buddy->num_free);
  122. return -ENOMEM;
  123. }
  124. static void mlx4_buddy_cleanup(struct mlx4_buddy *buddy)
  125. {
  126. int i;
  127. for (i = 0; i <= buddy->max_order; ++i)
  128. kfree(buddy->bits[i]);
  129. kfree(buddy->bits);
  130. kfree(buddy->num_free);
  131. }
  132. u32 __mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order)
  133. {
  134. struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
  135. u32 seg;
  136. int seg_order;
  137. u32 offset;
  138. seg_order = max_t(int, order - log_mtts_per_seg, 0);
  139. seg = mlx4_buddy_alloc(&mr_table->mtt_buddy, seg_order);
  140. if (seg == -1)
  141. return -1;
  142. offset = seg * (1 << log_mtts_per_seg);
  143. if (mlx4_table_get_range(dev, &mr_table->mtt_table, offset,
  144. offset + (1 << order) - 1)) {
  145. mlx4_buddy_free(&mr_table->mtt_buddy, seg, seg_order);
  146. return -1;
  147. }
  148. return offset;
  149. }
  150. static u32 mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order)
  151. {
  152. u64 in_param;
  153. u64 out_param;
  154. int err;
  155. if (mlx4_is_mfunc(dev)) {
  156. set_param_l(&in_param, order);
  157. err = mlx4_cmd_imm(dev, in_param, &out_param, RES_MTT,
  158. RES_OP_RESERVE_AND_MAP,
  159. MLX4_CMD_ALLOC_RES,
  160. MLX4_CMD_TIME_CLASS_A,
  161. MLX4_CMD_WRAPPED);
  162. if (err)
  163. return -1;
  164. return get_param_l(&out_param);
  165. }
  166. return __mlx4_alloc_mtt_range(dev, order);
  167. }
  168. int mlx4_mtt_init(struct mlx4_dev *dev, int npages, int page_shift,
  169. struct mlx4_mtt *mtt)
  170. {
  171. int i;
  172. if (!npages) {
  173. mtt->order = -1;
  174. mtt->page_shift = MLX4_ICM_PAGE_SHIFT;
  175. return 0;
  176. } else
  177. mtt->page_shift = page_shift;
  178. for (mtt->order = 0, i = 1; i < npages; i <<= 1)
  179. ++mtt->order;
  180. mtt->offset = mlx4_alloc_mtt_range(dev, mtt->order);
  181. if (mtt->offset == -1)
  182. return -ENOMEM;
  183. return 0;
  184. }
  185. EXPORT_SYMBOL_GPL(mlx4_mtt_init);
  186. void __mlx4_free_mtt_range(struct mlx4_dev *dev, u32 offset, int order)
  187. {
  188. u32 first_seg;
  189. int seg_order;
  190. struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
  191. seg_order = max_t(int, order - log_mtts_per_seg, 0);
  192. first_seg = offset / (1 << log_mtts_per_seg);
  193. mlx4_buddy_free(&mr_table->mtt_buddy, first_seg, seg_order);
  194. mlx4_table_put_range(dev, &mr_table->mtt_table, offset,
  195. offset + (1 << order) - 1);
  196. }
  197. static void mlx4_free_mtt_range(struct mlx4_dev *dev, u32 offset, int order)
  198. {
  199. u64 in_param;
  200. int err;
  201. if (mlx4_is_mfunc(dev)) {
  202. set_param_l(&in_param, offset);
  203. set_param_h(&in_param, order);
  204. err = mlx4_cmd(dev, in_param, RES_MTT, RES_OP_RESERVE_AND_MAP,
  205. MLX4_CMD_FREE_RES,
  206. MLX4_CMD_TIME_CLASS_A,
  207. MLX4_CMD_WRAPPED);
  208. if (err)
  209. mlx4_warn(dev, "Failed to free mtt range at:"
  210. "%d order:%d\n", offset, order);
  211. return;
  212. }
  213. __mlx4_free_mtt_range(dev, offset, order);
  214. }
  215. void mlx4_mtt_cleanup(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
  216. {
  217. if (mtt->order < 0)
  218. return;
  219. mlx4_free_mtt_range(dev, mtt->offset, mtt->order);
  220. }
  221. EXPORT_SYMBOL_GPL(mlx4_mtt_cleanup);
  222. u64 mlx4_mtt_addr(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
  223. {
  224. return (u64) mtt->offset * dev->caps.mtt_entry_sz;
  225. }
  226. EXPORT_SYMBOL_GPL(mlx4_mtt_addr);
  227. static u32 hw_index_to_key(u32 ind)
  228. {
  229. return (ind >> 24) | (ind << 8);
  230. }
  231. static u32 key_to_hw_index(u32 key)
  232. {
  233. return (key << 24) | (key >> 8);
  234. }
  235. static int mlx4_SW2HW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  236. int mpt_index)
  237. {
  238. return mlx4_cmd(dev, mailbox->dma, mpt_index,
  239. 0, MLX4_CMD_SW2HW_MPT, MLX4_CMD_TIME_CLASS_B,
  240. MLX4_CMD_WRAPPED);
  241. }
  242. static int mlx4_HW2SW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  243. int mpt_index)
  244. {
  245. return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index,
  246. !mailbox, MLX4_CMD_HW2SW_MPT,
  247. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  248. }
  249. static int mlx4_mr_alloc_reserved(struct mlx4_dev *dev, u32 mridx, u32 pd,
  250. u64 iova, u64 size, u32 access, int npages,
  251. int page_shift, struct mlx4_mr *mr)
  252. {
  253. mr->iova = iova;
  254. mr->size = size;
  255. mr->pd = pd;
  256. mr->access = access;
  257. mr->enabled = MLX4_MR_DISABLED;
  258. mr->key = hw_index_to_key(mridx);
  259. return mlx4_mtt_init(dev, npages, page_shift, &mr->mtt);
  260. }
  261. static int mlx4_WRITE_MTT(struct mlx4_dev *dev,
  262. struct mlx4_cmd_mailbox *mailbox,
  263. int num_entries)
  264. {
  265. return mlx4_cmd(dev, mailbox->dma, num_entries, 0, MLX4_CMD_WRITE_MTT,
  266. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  267. }
  268. int __mlx4_mr_reserve(struct mlx4_dev *dev)
  269. {
  270. struct mlx4_priv *priv = mlx4_priv(dev);
  271. return mlx4_bitmap_alloc(&priv->mr_table.mpt_bitmap);
  272. }
  273. static int mlx4_mr_reserve(struct mlx4_dev *dev)
  274. {
  275. u64 out_param;
  276. if (mlx4_is_mfunc(dev)) {
  277. if (mlx4_cmd_imm(dev, 0, &out_param, RES_MPT, RES_OP_RESERVE,
  278. MLX4_CMD_ALLOC_RES,
  279. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED))
  280. return -1;
  281. return get_param_l(&out_param);
  282. }
  283. return __mlx4_mr_reserve(dev);
  284. }
  285. void __mlx4_mr_release(struct mlx4_dev *dev, u32 index)
  286. {
  287. struct mlx4_priv *priv = mlx4_priv(dev);
  288. mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, index);
  289. }
  290. static void mlx4_mr_release(struct mlx4_dev *dev, u32 index)
  291. {
  292. u64 in_param;
  293. if (mlx4_is_mfunc(dev)) {
  294. set_param_l(&in_param, index);
  295. if (mlx4_cmd(dev, in_param, RES_MPT, RES_OP_RESERVE,
  296. MLX4_CMD_FREE_RES,
  297. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED))
  298. mlx4_warn(dev, "Failed to release mr index:%d\n",
  299. index);
  300. return;
  301. }
  302. __mlx4_mr_release(dev, index);
  303. }
  304. int __mlx4_mr_alloc_icm(struct mlx4_dev *dev, u32 index)
  305. {
  306. struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
  307. return mlx4_table_get(dev, &mr_table->dmpt_table, index);
  308. }
  309. static int mlx4_mr_alloc_icm(struct mlx4_dev *dev, u32 index)
  310. {
  311. u64 param;
  312. if (mlx4_is_mfunc(dev)) {
  313. set_param_l(&param, index);
  314. return mlx4_cmd_imm(dev, param, &param, RES_MPT, RES_OP_MAP_ICM,
  315. MLX4_CMD_ALLOC_RES,
  316. MLX4_CMD_TIME_CLASS_A,
  317. MLX4_CMD_WRAPPED);
  318. }
  319. return __mlx4_mr_alloc_icm(dev, index);
  320. }
  321. void __mlx4_mr_free_icm(struct mlx4_dev *dev, u32 index)
  322. {
  323. struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
  324. mlx4_table_put(dev, &mr_table->dmpt_table, index);
  325. }
  326. static void mlx4_mr_free_icm(struct mlx4_dev *dev, u32 index)
  327. {
  328. u64 in_param;
  329. if (mlx4_is_mfunc(dev)) {
  330. set_param_l(&in_param, index);
  331. if (mlx4_cmd(dev, in_param, RES_MPT, RES_OP_MAP_ICM,
  332. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  333. MLX4_CMD_WRAPPED))
  334. mlx4_warn(dev, "Failed to free icm of mr index:%d\n",
  335. index);
  336. return;
  337. }
  338. return __mlx4_mr_free_icm(dev, index);
  339. }
  340. int mlx4_mr_alloc(struct mlx4_dev *dev, u32 pd, u64 iova, u64 size, u32 access,
  341. int npages, int page_shift, struct mlx4_mr *mr)
  342. {
  343. u32 index;
  344. int err;
  345. index = mlx4_mr_reserve(dev);
  346. if (index == -1)
  347. return -ENOMEM;
  348. err = mlx4_mr_alloc_reserved(dev, index, pd, iova, size,
  349. access, npages, page_shift, mr);
  350. if (err)
  351. mlx4_mr_release(dev, index);
  352. return err;
  353. }
  354. EXPORT_SYMBOL_GPL(mlx4_mr_alloc);
  355. static void mlx4_mr_free_reserved(struct mlx4_dev *dev, struct mlx4_mr *mr)
  356. {
  357. int err;
  358. if (mr->enabled == MLX4_MR_EN_HW) {
  359. err = mlx4_HW2SW_MPT(dev, NULL,
  360. key_to_hw_index(mr->key) &
  361. (dev->caps.num_mpts - 1));
  362. if (err)
  363. mlx4_warn(dev, "xxx HW2SW_MPT failed (%d)\n", err);
  364. mr->enabled = MLX4_MR_EN_SW;
  365. }
  366. mlx4_mtt_cleanup(dev, &mr->mtt);
  367. }
  368. void mlx4_mr_free(struct mlx4_dev *dev, struct mlx4_mr *mr)
  369. {
  370. mlx4_mr_free_reserved(dev, mr);
  371. if (mr->enabled)
  372. mlx4_mr_free_icm(dev, key_to_hw_index(mr->key));
  373. mlx4_mr_release(dev, key_to_hw_index(mr->key));
  374. }
  375. EXPORT_SYMBOL_GPL(mlx4_mr_free);
  376. int mlx4_mr_enable(struct mlx4_dev *dev, struct mlx4_mr *mr)
  377. {
  378. struct mlx4_cmd_mailbox *mailbox;
  379. struct mlx4_mpt_entry *mpt_entry;
  380. int err;
  381. err = mlx4_mr_alloc_icm(dev, key_to_hw_index(mr->key));
  382. if (err)
  383. return err;
  384. mailbox = mlx4_alloc_cmd_mailbox(dev);
  385. if (IS_ERR(mailbox)) {
  386. err = PTR_ERR(mailbox);
  387. goto err_table;
  388. }
  389. mpt_entry = mailbox->buf;
  390. memset(mpt_entry, 0, sizeof *mpt_entry);
  391. mpt_entry->flags = cpu_to_be32(MLX4_MPT_FLAG_MIO |
  392. MLX4_MPT_FLAG_REGION |
  393. mr->access);
  394. mpt_entry->key = cpu_to_be32(key_to_hw_index(mr->key));
  395. mpt_entry->pd_flags = cpu_to_be32(mr->pd | MLX4_MPT_PD_FLAG_EN_INV);
  396. mpt_entry->start = cpu_to_be64(mr->iova);
  397. mpt_entry->length = cpu_to_be64(mr->size);
  398. mpt_entry->entity_size = cpu_to_be32(mr->mtt.page_shift);
  399. if (mr->mtt.order < 0) {
  400. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL);
  401. mpt_entry->mtt_addr = 0;
  402. } else {
  403. mpt_entry->mtt_addr = cpu_to_be64(mlx4_mtt_addr(dev,
  404. &mr->mtt));
  405. }
  406. if (mr->mtt.order >= 0 && mr->mtt.page_shift == 0) {
  407. /* fast register MR in free state */
  408. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_FREE);
  409. mpt_entry->pd_flags |= cpu_to_be32(MLX4_MPT_PD_FLAG_FAST_REG |
  410. MLX4_MPT_PD_FLAG_RAE);
  411. mpt_entry->mtt_sz = cpu_to_be32(1 << mr->mtt.order);
  412. } else {
  413. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS);
  414. }
  415. err = mlx4_SW2HW_MPT(dev, mailbox,
  416. key_to_hw_index(mr->key) & (dev->caps.num_mpts - 1));
  417. if (err) {
  418. mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err);
  419. goto err_cmd;
  420. }
  421. mr->enabled = MLX4_MR_EN_HW;
  422. mlx4_free_cmd_mailbox(dev, mailbox);
  423. return 0;
  424. err_cmd:
  425. mlx4_free_cmd_mailbox(dev, mailbox);
  426. err_table:
  427. mlx4_mr_free_icm(dev, key_to_hw_index(mr->key));
  428. return err;
  429. }
  430. EXPORT_SYMBOL_GPL(mlx4_mr_enable);
  431. static int mlx4_write_mtt_chunk(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  432. int start_index, int npages, u64 *page_list)
  433. {
  434. struct mlx4_priv *priv = mlx4_priv(dev);
  435. __be64 *mtts;
  436. dma_addr_t dma_handle;
  437. int i;
  438. mtts = mlx4_table_find(&priv->mr_table.mtt_table, mtt->offset +
  439. start_index, &dma_handle);
  440. if (!mtts)
  441. return -ENOMEM;
  442. dma_sync_single_for_cpu(&dev->pdev->dev, dma_handle,
  443. npages * sizeof (u64), DMA_TO_DEVICE);
  444. for (i = 0; i < npages; ++i)
  445. mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
  446. dma_sync_single_for_device(&dev->pdev->dev, dma_handle,
  447. npages * sizeof (u64), DMA_TO_DEVICE);
  448. return 0;
  449. }
  450. int __mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  451. int start_index, int npages, u64 *page_list)
  452. {
  453. int err = 0;
  454. int chunk;
  455. int mtts_per_page;
  456. int max_mtts_first_page;
  457. /* compute how may mtts fit in the first page */
  458. mtts_per_page = PAGE_SIZE / sizeof(u64);
  459. max_mtts_first_page = mtts_per_page - (mtt->offset + start_index)
  460. % mtts_per_page;
  461. chunk = min_t(int, max_mtts_first_page, npages);
  462. while (npages > 0) {
  463. err = mlx4_write_mtt_chunk(dev, mtt, start_index, chunk, page_list);
  464. if (err)
  465. return err;
  466. npages -= chunk;
  467. start_index += chunk;
  468. page_list += chunk;
  469. chunk = min_t(int, mtts_per_page, npages);
  470. }
  471. return err;
  472. }
  473. int mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  474. int start_index, int npages, u64 *page_list)
  475. {
  476. struct mlx4_cmd_mailbox *mailbox = NULL;
  477. __be64 *inbox = NULL;
  478. int chunk;
  479. int err = 0;
  480. int i;
  481. if (mtt->order < 0)
  482. return -EINVAL;
  483. if (mlx4_is_mfunc(dev)) {
  484. mailbox = mlx4_alloc_cmd_mailbox(dev);
  485. if (IS_ERR(mailbox))
  486. return PTR_ERR(mailbox);
  487. inbox = mailbox->buf;
  488. while (npages > 0) {
  489. chunk = min_t(int, MLX4_MAILBOX_SIZE / sizeof(u64) - 2,
  490. npages);
  491. inbox[0] = cpu_to_be64(mtt->offset + start_index);
  492. inbox[1] = 0;
  493. for (i = 0; i < chunk; ++i)
  494. inbox[i + 2] = cpu_to_be64(page_list[i] |
  495. MLX4_MTT_FLAG_PRESENT);
  496. err = mlx4_WRITE_MTT(dev, mailbox, chunk);
  497. if (err) {
  498. mlx4_free_cmd_mailbox(dev, mailbox);
  499. return err;
  500. }
  501. npages -= chunk;
  502. start_index += chunk;
  503. page_list += chunk;
  504. }
  505. mlx4_free_cmd_mailbox(dev, mailbox);
  506. return err;
  507. }
  508. return __mlx4_write_mtt(dev, mtt, start_index, npages, page_list);
  509. }
  510. EXPORT_SYMBOL_GPL(mlx4_write_mtt);
  511. int mlx4_buf_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  512. struct mlx4_buf *buf)
  513. {
  514. u64 *page_list;
  515. int err;
  516. int i;
  517. page_list = kmalloc(buf->npages * sizeof *page_list, GFP_KERNEL);
  518. if (!page_list)
  519. return -ENOMEM;
  520. for (i = 0; i < buf->npages; ++i)
  521. if (buf->nbufs == 1)
  522. page_list[i] = buf->direct.map + (i << buf->page_shift);
  523. else
  524. page_list[i] = buf->page_list[i].map;
  525. err = mlx4_write_mtt(dev, mtt, 0, buf->npages, page_list);
  526. kfree(page_list);
  527. return err;
  528. }
  529. EXPORT_SYMBOL_GPL(mlx4_buf_write_mtt);
  530. int mlx4_init_mr_table(struct mlx4_dev *dev)
  531. {
  532. struct mlx4_priv *priv = mlx4_priv(dev);
  533. struct mlx4_mr_table *mr_table = &priv->mr_table;
  534. int err;
  535. if (!is_power_of_2(dev->caps.num_mpts))
  536. return -EINVAL;
  537. /* Nothing to do for slaves - all MR handling is forwarded
  538. * to the master */
  539. if (mlx4_is_slave(dev))
  540. return 0;
  541. err = mlx4_bitmap_init(&mr_table->mpt_bitmap, dev->caps.num_mpts,
  542. ~0, dev->caps.reserved_mrws, 0);
  543. if (err)
  544. return err;
  545. err = mlx4_buddy_init(&mr_table->mtt_buddy,
  546. ilog2(dev->caps.num_mtts /
  547. (1 << log_mtts_per_seg)));
  548. if (err)
  549. goto err_buddy;
  550. if (dev->caps.reserved_mtts) {
  551. priv->reserved_mtts =
  552. mlx4_alloc_mtt_range(dev,
  553. fls(dev->caps.reserved_mtts - 1));
  554. if (priv->reserved_mtts < 0) {
  555. mlx4_warn(dev, "MTT table of order %d is too small.\n",
  556. mr_table->mtt_buddy.max_order);
  557. err = -ENOMEM;
  558. goto err_reserve_mtts;
  559. }
  560. }
  561. return 0;
  562. err_reserve_mtts:
  563. mlx4_buddy_cleanup(&mr_table->mtt_buddy);
  564. err_buddy:
  565. mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
  566. return err;
  567. }
  568. void mlx4_cleanup_mr_table(struct mlx4_dev *dev)
  569. {
  570. struct mlx4_priv *priv = mlx4_priv(dev);
  571. struct mlx4_mr_table *mr_table = &priv->mr_table;
  572. if (mlx4_is_slave(dev))
  573. return;
  574. if (priv->reserved_mtts >= 0)
  575. mlx4_free_mtt_range(dev, priv->reserved_mtts,
  576. fls(dev->caps.reserved_mtts - 1));
  577. mlx4_buddy_cleanup(&mr_table->mtt_buddy);
  578. mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
  579. }
  580. static inline int mlx4_check_fmr(struct mlx4_fmr *fmr, u64 *page_list,
  581. int npages, u64 iova)
  582. {
  583. int i, page_mask;
  584. if (npages > fmr->max_pages)
  585. return -EINVAL;
  586. page_mask = (1 << fmr->page_shift) - 1;
  587. /* We are getting page lists, so va must be page aligned. */
  588. if (iova & page_mask)
  589. return -EINVAL;
  590. /* Trust the user not to pass misaligned data in page_list */
  591. if (0)
  592. for (i = 0; i < npages; ++i) {
  593. if (page_list[i] & ~page_mask)
  594. return -EINVAL;
  595. }
  596. if (fmr->maps >= fmr->max_maps)
  597. return -EINVAL;
  598. return 0;
  599. }
  600. int mlx4_map_phys_fmr(struct mlx4_dev *dev, struct mlx4_fmr *fmr, u64 *page_list,
  601. int npages, u64 iova, u32 *lkey, u32 *rkey)
  602. {
  603. u32 key;
  604. int i, err;
  605. err = mlx4_check_fmr(fmr, page_list, npages, iova);
  606. if (err)
  607. return err;
  608. ++fmr->maps;
  609. key = key_to_hw_index(fmr->mr.key);
  610. key += dev->caps.num_mpts;
  611. *lkey = *rkey = fmr->mr.key = hw_index_to_key(key);
  612. *(u8 *) fmr->mpt = MLX4_MPT_STATUS_SW;
  613. /* Make sure MPT status is visible before writing MTT entries */
  614. wmb();
  615. dma_sync_single_for_cpu(&dev->pdev->dev, fmr->dma_handle,
  616. npages * sizeof(u64), DMA_TO_DEVICE);
  617. for (i = 0; i < npages; ++i)
  618. fmr->mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
  619. dma_sync_single_for_device(&dev->pdev->dev, fmr->dma_handle,
  620. npages * sizeof(u64), DMA_TO_DEVICE);
  621. fmr->mpt->key = cpu_to_be32(key);
  622. fmr->mpt->lkey = cpu_to_be32(key);
  623. fmr->mpt->length = cpu_to_be64(npages * (1ull << fmr->page_shift));
  624. fmr->mpt->start = cpu_to_be64(iova);
  625. /* Make MTT entries are visible before setting MPT status */
  626. wmb();
  627. *(u8 *) fmr->mpt = MLX4_MPT_STATUS_HW;
  628. /* Make sure MPT status is visible before consumer can use FMR */
  629. wmb();
  630. return 0;
  631. }
  632. EXPORT_SYMBOL_GPL(mlx4_map_phys_fmr);
  633. int mlx4_fmr_alloc(struct mlx4_dev *dev, u32 pd, u32 access, int max_pages,
  634. int max_maps, u8 page_shift, struct mlx4_fmr *fmr)
  635. {
  636. struct mlx4_priv *priv = mlx4_priv(dev);
  637. u64 mtt_offset;
  638. int err = -ENOMEM;
  639. if (max_maps > dev->caps.max_fmr_maps)
  640. return -EINVAL;
  641. if (page_shift < (ffs(dev->caps.page_size_cap) - 1) || page_shift >= 32)
  642. return -EINVAL;
  643. /* All MTTs must fit in the same page */
  644. if (max_pages * sizeof *fmr->mtts > PAGE_SIZE)
  645. return -EINVAL;
  646. fmr->page_shift = page_shift;
  647. fmr->max_pages = max_pages;
  648. fmr->max_maps = max_maps;
  649. fmr->maps = 0;
  650. err = mlx4_mr_alloc(dev, pd, 0, 0, access, max_pages,
  651. page_shift, &fmr->mr);
  652. if (err)
  653. return err;
  654. mtt_offset = fmr->mr.mtt.offset * dev->caps.mtt_entry_sz;
  655. fmr->mtts = mlx4_table_find(&priv->mr_table.mtt_table,
  656. fmr->mr.mtt.offset,
  657. &fmr->dma_handle);
  658. if (!fmr->mtts) {
  659. err = -ENOMEM;
  660. goto err_free;
  661. }
  662. return 0;
  663. err_free:
  664. mlx4_mr_free(dev, &fmr->mr);
  665. return err;
  666. }
  667. EXPORT_SYMBOL_GPL(mlx4_fmr_alloc);
  668. int mlx4_fmr_enable(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
  669. {
  670. struct mlx4_priv *priv = mlx4_priv(dev);
  671. int err;
  672. err = mlx4_mr_enable(dev, &fmr->mr);
  673. if (err)
  674. return err;
  675. fmr->mpt = mlx4_table_find(&priv->mr_table.dmpt_table,
  676. key_to_hw_index(fmr->mr.key), NULL);
  677. if (!fmr->mpt)
  678. return -ENOMEM;
  679. return 0;
  680. }
  681. EXPORT_SYMBOL_GPL(mlx4_fmr_enable);
  682. void mlx4_fmr_unmap(struct mlx4_dev *dev, struct mlx4_fmr *fmr,
  683. u32 *lkey, u32 *rkey)
  684. {
  685. struct mlx4_cmd_mailbox *mailbox;
  686. int err;
  687. if (!fmr->maps)
  688. return;
  689. fmr->maps = 0;
  690. mailbox = mlx4_alloc_cmd_mailbox(dev);
  691. if (IS_ERR(mailbox)) {
  692. err = PTR_ERR(mailbox);
  693. printk(KERN_WARNING "mlx4_ib: mlx4_alloc_cmd_mailbox"
  694. " failed (%d)\n", err);
  695. return;
  696. }
  697. err = mlx4_HW2SW_MPT(dev, NULL,
  698. key_to_hw_index(fmr->mr.key) &
  699. (dev->caps.num_mpts - 1));
  700. mlx4_free_cmd_mailbox(dev, mailbox);
  701. if (err) {
  702. printk(KERN_WARNING "mlx4_ib: mlx4_HW2SW_MPT failed (%d)\n",
  703. err);
  704. return;
  705. }
  706. fmr->mr.enabled = MLX4_MR_EN_SW;
  707. }
  708. EXPORT_SYMBOL_GPL(mlx4_fmr_unmap);
  709. int mlx4_fmr_free(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
  710. {
  711. if (fmr->maps)
  712. return -EBUSY;
  713. mlx4_mr_free(dev, &fmr->mr);
  714. fmr->mr.enabled = MLX4_MR_DISABLED;
  715. return 0;
  716. }
  717. EXPORT_SYMBOL_GPL(mlx4_fmr_free);
  718. int mlx4_SYNC_TPT(struct mlx4_dev *dev)
  719. {
  720. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_SYNC_TPT, 1000,
  721. MLX4_CMD_WRAPPED);
  722. }
  723. EXPORT_SYMBOL_GPL(mlx4_SYNC_TPT);