mr.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  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/errno.h>
  35. #include <linux/export.h>
  36. #include <linux/slab.h>
  37. #include <linux/kernel.h>
  38. #include <linux/vmalloc.h>
  39. #include <linux/mlx4/cmd.h>
  40. #include "mlx4.h"
  41. #include "icm.h"
  42. static u32 mlx4_buddy_alloc(struct mlx4_buddy *buddy, int order)
  43. {
  44. int o;
  45. int m;
  46. u32 seg;
  47. spin_lock(&buddy->lock);
  48. for (o = order; o <= buddy->max_order; ++o)
  49. if (buddy->num_free[o]) {
  50. m = 1 << (buddy->max_order - o);
  51. seg = find_first_bit(buddy->bits[o], m);
  52. if (seg < m)
  53. goto found;
  54. }
  55. spin_unlock(&buddy->lock);
  56. return -1;
  57. found:
  58. clear_bit(seg, buddy->bits[o]);
  59. --buddy->num_free[o];
  60. while (o > order) {
  61. --o;
  62. seg <<= 1;
  63. set_bit(seg ^ 1, buddy->bits[o]);
  64. ++buddy->num_free[o];
  65. }
  66. spin_unlock(&buddy->lock);
  67. seg <<= order;
  68. return seg;
  69. }
  70. static void mlx4_buddy_free(struct mlx4_buddy *buddy, u32 seg, int order)
  71. {
  72. seg >>= order;
  73. spin_lock(&buddy->lock);
  74. while (test_bit(seg ^ 1, buddy->bits[order])) {
  75. clear_bit(seg ^ 1, buddy->bits[order]);
  76. --buddy->num_free[order];
  77. seg >>= 1;
  78. ++order;
  79. }
  80. set_bit(seg, buddy->bits[order]);
  81. ++buddy->num_free[order];
  82. spin_unlock(&buddy->lock);
  83. }
  84. static int mlx4_buddy_init(struct mlx4_buddy *buddy, int max_order)
  85. {
  86. int i, s;
  87. buddy->max_order = max_order;
  88. spin_lock_init(&buddy->lock);
  89. buddy->bits = kcalloc(buddy->max_order + 1, sizeof(long *),
  90. GFP_KERNEL);
  91. buddy->num_free = kcalloc(buddy->max_order + 1, sizeof(*buddy->num_free),
  92. GFP_KERNEL);
  93. if (!buddy->bits || !buddy->num_free)
  94. goto err_out;
  95. for (i = 0; i <= buddy->max_order; ++i) {
  96. s = BITS_TO_LONGS(1 << (buddy->max_order - i));
  97. buddy->bits[i] = kvmalloc_array(s, sizeof(long), GFP_KERNEL | __GFP_ZERO);
  98. if (!buddy->bits[i])
  99. goto err_out_free;
  100. }
  101. set_bit(0, buddy->bits[buddy->max_order]);
  102. buddy->num_free[buddy->max_order] = 1;
  103. return 0;
  104. err_out_free:
  105. for (i = 0; i <= buddy->max_order; ++i)
  106. kvfree(buddy->bits[i]);
  107. err_out:
  108. kfree(buddy->bits);
  109. kfree(buddy->num_free);
  110. return -ENOMEM;
  111. }
  112. static void mlx4_buddy_cleanup(struct mlx4_buddy *buddy)
  113. {
  114. int i;
  115. for (i = 0; i <= buddy->max_order; ++i)
  116. kvfree(buddy->bits[i]);
  117. kfree(buddy->bits);
  118. kfree(buddy->num_free);
  119. }
  120. u32 __mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order)
  121. {
  122. struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
  123. u32 seg;
  124. int seg_order;
  125. u32 offset;
  126. seg_order = max_t(int, order - log_mtts_per_seg, 0);
  127. seg = mlx4_buddy_alloc(&mr_table->mtt_buddy, seg_order);
  128. if (seg == -1)
  129. return -1;
  130. offset = seg * (1 << log_mtts_per_seg);
  131. if (mlx4_table_get_range(dev, &mr_table->mtt_table, offset,
  132. offset + (1 << order) - 1)) {
  133. mlx4_buddy_free(&mr_table->mtt_buddy, seg, seg_order);
  134. return -1;
  135. }
  136. return offset;
  137. }
  138. static u32 mlx4_alloc_mtt_range(struct mlx4_dev *dev, int order)
  139. {
  140. u64 in_param = 0;
  141. u64 out_param;
  142. int err;
  143. if (mlx4_is_mfunc(dev)) {
  144. set_param_l(&in_param, order);
  145. err = mlx4_cmd_imm(dev, in_param, &out_param, RES_MTT,
  146. RES_OP_RESERVE_AND_MAP,
  147. MLX4_CMD_ALLOC_RES,
  148. MLX4_CMD_TIME_CLASS_A,
  149. MLX4_CMD_WRAPPED);
  150. if (err)
  151. return -1;
  152. return get_param_l(&out_param);
  153. }
  154. return __mlx4_alloc_mtt_range(dev, order);
  155. }
  156. int mlx4_mtt_init(struct mlx4_dev *dev, int npages, int page_shift,
  157. struct mlx4_mtt *mtt)
  158. {
  159. int i;
  160. if (!npages) {
  161. mtt->order = -1;
  162. mtt->page_shift = MLX4_ICM_PAGE_SHIFT;
  163. return 0;
  164. } else
  165. mtt->page_shift = page_shift;
  166. for (mtt->order = 0, i = 1; i < npages; i <<= 1)
  167. ++mtt->order;
  168. mtt->offset = mlx4_alloc_mtt_range(dev, mtt->order);
  169. if (mtt->offset == -1)
  170. return -ENOMEM;
  171. return 0;
  172. }
  173. EXPORT_SYMBOL_GPL(mlx4_mtt_init);
  174. void __mlx4_free_mtt_range(struct mlx4_dev *dev, u32 offset, int order)
  175. {
  176. u32 first_seg;
  177. int seg_order;
  178. struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
  179. seg_order = max_t(int, order - log_mtts_per_seg, 0);
  180. first_seg = offset / (1 << log_mtts_per_seg);
  181. mlx4_buddy_free(&mr_table->mtt_buddy, first_seg, seg_order);
  182. mlx4_table_put_range(dev, &mr_table->mtt_table, offset,
  183. offset + (1 << order) - 1);
  184. }
  185. static void mlx4_free_mtt_range(struct mlx4_dev *dev, u32 offset, int order)
  186. {
  187. u64 in_param = 0;
  188. int err;
  189. if (mlx4_is_mfunc(dev)) {
  190. set_param_l(&in_param, offset);
  191. set_param_h(&in_param, order);
  192. err = mlx4_cmd(dev, in_param, RES_MTT, RES_OP_RESERVE_AND_MAP,
  193. MLX4_CMD_FREE_RES,
  194. MLX4_CMD_TIME_CLASS_A,
  195. MLX4_CMD_WRAPPED);
  196. if (err)
  197. mlx4_warn(dev, "Failed to free mtt range at:%d order:%d\n",
  198. offset, order);
  199. return;
  200. }
  201. __mlx4_free_mtt_range(dev, offset, order);
  202. }
  203. void mlx4_mtt_cleanup(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
  204. {
  205. if (mtt->order < 0)
  206. return;
  207. mlx4_free_mtt_range(dev, mtt->offset, mtt->order);
  208. }
  209. EXPORT_SYMBOL_GPL(mlx4_mtt_cleanup);
  210. u64 mlx4_mtt_addr(struct mlx4_dev *dev, struct mlx4_mtt *mtt)
  211. {
  212. return (u64) mtt->offset * dev->caps.mtt_entry_sz;
  213. }
  214. EXPORT_SYMBOL_GPL(mlx4_mtt_addr);
  215. static u32 hw_index_to_key(u32 ind)
  216. {
  217. return (ind >> 24) | (ind << 8);
  218. }
  219. static u32 key_to_hw_index(u32 key)
  220. {
  221. return (key << 24) | (key >> 8);
  222. }
  223. static int mlx4_SW2HW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  224. int mpt_index)
  225. {
  226. return mlx4_cmd(dev, mailbox->dma, mpt_index,
  227. 0, MLX4_CMD_SW2HW_MPT, MLX4_CMD_TIME_CLASS_B,
  228. MLX4_CMD_WRAPPED);
  229. }
  230. static int mlx4_HW2SW_MPT(struct mlx4_dev *dev, struct mlx4_cmd_mailbox *mailbox,
  231. int mpt_index)
  232. {
  233. return mlx4_cmd_box(dev, 0, mailbox ? mailbox->dma : 0, mpt_index,
  234. !mailbox, MLX4_CMD_HW2SW_MPT,
  235. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_WRAPPED);
  236. }
  237. /* Must protect against concurrent access */
  238. int mlx4_mr_hw_get_mpt(struct mlx4_dev *dev, struct mlx4_mr *mmr,
  239. struct mlx4_mpt_entry ***mpt_entry)
  240. {
  241. int err;
  242. int key = key_to_hw_index(mmr->key) & (dev->caps.num_mpts - 1);
  243. struct mlx4_cmd_mailbox *mailbox = NULL;
  244. if (mmr->enabled != MLX4_MPT_EN_HW)
  245. return -EINVAL;
  246. err = mlx4_HW2SW_MPT(dev, NULL, key);
  247. if (err) {
  248. mlx4_warn(dev, "HW2SW_MPT failed (%d).", err);
  249. mlx4_warn(dev, "Most likely the MR has MWs bound to it.\n");
  250. return err;
  251. }
  252. mmr->enabled = MLX4_MPT_EN_SW;
  253. if (!mlx4_is_mfunc(dev)) {
  254. **mpt_entry = mlx4_table_find(
  255. &mlx4_priv(dev)->mr_table.dmpt_table,
  256. key, NULL);
  257. } else {
  258. mailbox = mlx4_alloc_cmd_mailbox(dev);
  259. if (IS_ERR(mailbox))
  260. return PTR_ERR(mailbox);
  261. err = mlx4_cmd_box(dev, 0, mailbox->dma, key,
  262. 0, MLX4_CMD_QUERY_MPT,
  263. MLX4_CMD_TIME_CLASS_B,
  264. MLX4_CMD_WRAPPED);
  265. if (err)
  266. goto free_mailbox;
  267. *mpt_entry = (struct mlx4_mpt_entry **)&mailbox->buf;
  268. }
  269. if (!(*mpt_entry) || !(**mpt_entry)) {
  270. err = -ENOMEM;
  271. goto free_mailbox;
  272. }
  273. return 0;
  274. free_mailbox:
  275. mlx4_free_cmd_mailbox(dev, mailbox);
  276. return err;
  277. }
  278. EXPORT_SYMBOL_GPL(mlx4_mr_hw_get_mpt);
  279. int mlx4_mr_hw_write_mpt(struct mlx4_dev *dev, struct mlx4_mr *mmr,
  280. struct mlx4_mpt_entry **mpt_entry)
  281. {
  282. int err;
  283. if (!mlx4_is_mfunc(dev)) {
  284. /* Make sure any changes to this entry are flushed */
  285. wmb();
  286. *(u8 *)(*mpt_entry) = MLX4_MPT_STATUS_HW;
  287. /* Make sure the new status is written */
  288. wmb();
  289. err = mlx4_SYNC_TPT(dev);
  290. } else {
  291. int key = key_to_hw_index(mmr->key) & (dev->caps.num_mpts - 1);
  292. struct mlx4_cmd_mailbox *mailbox =
  293. container_of((void *)mpt_entry, struct mlx4_cmd_mailbox,
  294. buf);
  295. (*mpt_entry)->lkey = 0;
  296. err = mlx4_SW2HW_MPT(dev, mailbox, key);
  297. }
  298. if (!err) {
  299. mmr->pd = be32_to_cpu((*mpt_entry)->pd_flags) & MLX4_MPT_PD_MASK;
  300. mmr->enabled = MLX4_MPT_EN_HW;
  301. }
  302. return err;
  303. }
  304. EXPORT_SYMBOL_GPL(mlx4_mr_hw_write_mpt);
  305. void mlx4_mr_hw_put_mpt(struct mlx4_dev *dev,
  306. struct mlx4_mpt_entry **mpt_entry)
  307. {
  308. if (mlx4_is_mfunc(dev)) {
  309. struct mlx4_cmd_mailbox *mailbox =
  310. container_of((void *)mpt_entry, struct mlx4_cmd_mailbox,
  311. buf);
  312. mlx4_free_cmd_mailbox(dev, mailbox);
  313. }
  314. }
  315. EXPORT_SYMBOL_GPL(mlx4_mr_hw_put_mpt);
  316. int mlx4_mr_hw_change_pd(struct mlx4_dev *dev, struct mlx4_mpt_entry *mpt_entry,
  317. u32 pdn)
  318. {
  319. u32 pd_flags = be32_to_cpu(mpt_entry->pd_flags) & ~MLX4_MPT_PD_MASK;
  320. /* The wrapper function will put the slave's id here */
  321. if (mlx4_is_mfunc(dev))
  322. pd_flags &= ~MLX4_MPT_PD_VF_MASK;
  323. mpt_entry->pd_flags = cpu_to_be32(pd_flags |
  324. (pdn & MLX4_MPT_PD_MASK)
  325. | MLX4_MPT_PD_FLAG_EN_INV);
  326. return 0;
  327. }
  328. EXPORT_SYMBOL_GPL(mlx4_mr_hw_change_pd);
  329. int mlx4_mr_hw_change_access(struct mlx4_dev *dev,
  330. struct mlx4_mpt_entry *mpt_entry,
  331. u32 access)
  332. {
  333. u32 flags = (be32_to_cpu(mpt_entry->flags) & ~MLX4_PERM_MASK) |
  334. (access & MLX4_PERM_MASK);
  335. mpt_entry->flags = cpu_to_be32(flags);
  336. return 0;
  337. }
  338. EXPORT_SYMBOL_GPL(mlx4_mr_hw_change_access);
  339. static int mlx4_mr_alloc_reserved(struct mlx4_dev *dev, u32 mridx, u32 pd,
  340. u64 iova, u64 size, u32 access, int npages,
  341. int page_shift, struct mlx4_mr *mr)
  342. {
  343. mr->iova = iova;
  344. mr->size = size;
  345. mr->pd = pd;
  346. mr->access = access;
  347. mr->enabled = MLX4_MPT_DISABLED;
  348. mr->key = hw_index_to_key(mridx);
  349. return mlx4_mtt_init(dev, npages, page_shift, &mr->mtt);
  350. }
  351. static int mlx4_WRITE_MTT(struct mlx4_dev *dev,
  352. struct mlx4_cmd_mailbox *mailbox,
  353. int num_entries)
  354. {
  355. return mlx4_cmd(dev, mailbox->dma, num_entries, 0, MLX4_CMD_WRITE_MTT,
  356. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  357. }
  358. int __mlx4_mpt_reserve(struct mlx4_dev *dev)
  359. {
  360. struct mlx4_priv *priv = mlx4_priv(dev);
  361. return mlx4_bitmap_alloc(&priv->mr_table.mpt_bitmap);
  362. }
  363. static int mlx4_mpt_reserve(struct mlx4_dev *dev)
  364. {
  365. u64 out_param;
  366. if (mlx4_is_mfunc(dev)) {
  367. if (mlx4_cmd_imm(dev, 0, &out_param, RES_MPT, RES_OP_RESERVE,
  368. MLX4_CMD_ALLOC_RES,
  369. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED))
  370. return -1;
  371. return get_param_l(&out_param);
  372. }
  373. return __mlx4_mpt_reserve(dev);
  374. }
  375. void __mlx4_mpt_release(struct mlx4_dev *dev, u32 index)
  376. {
  377. struct mlx4_priv *priv = mlx4_priv(dev);
  378. mlx4_bitmap_free(&priv->mr_table.mpt_bitmap, index, MLX4_NO_RR);
  379. }
  380. static void mlx4_mpt_release(struct mlx4_dev *dev, u32 index)
  381. {
  382. u64 in_param = 0;
  383. if (mlx4_is_mfunc(dev)) {
  384. set_param_l(&in_param, index);
  385. if (mlx4_cmd(dev, in_param, RES_MPT, RES_OP_RESERVE,
  386. MLX4_CMD_FREE_RES,
  387. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED))
  388. mlx4_warn(dev, "Failed to release mr index:%d\n",
  389. index);
  390. return;
  391. }
  392. __mlx4_mpt_release(dev, index);
  393. }
  394. int __mlx4_mpt_alloc_icm(struct mlx4_dev *dev, u32 index)
  395. {
  396. struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
  397. return mlx4_table_get(dev, &mr_table->dmpt_table, index);
  398. }
  399. static int mlx4_mpt_alloc_icm(struct mlx4_dev *dev, u32 index)
  400. {
  401. u64 param = 0;
  402. if (mlx4_is_mfunc(dev)) {
  403. set_param_l(&param, index);
  404. return mlx4_cmd_imm(dev, param, &param, RES_MPT, RES_OP_MAP_ICM,
  405. MLX4_CMD_ALLOC_RES,
  406. MLX4_CMD_TIME_CLASS_A,
  407. MLX4_CMD_WRAPPED);
  408. }
  409. return __mlx4_mpt_alloc_icm(dev, index);
  410. }
  411. void __mlx4_mpt_free_icm(struct mlx4_dev *dev, u32 index)
  412. {
  413. struct mlx4_mr_table *mr_table = &mlx4_priv(dev)->mr_table;
  414. mlx4_table_put(dev, &mr_table->dmpt_table, index);
  415. }
  416. static void mlx4_mpt_free_icm(struct mlx4_dev *dev, u32 index)
  417. {
  418. u64 in_param = 0;
  419. if (mlx4_is_mfunc(dev)) {
  420. set_param_l(&in_param, index);
  421. if (mlx4_cmd(dev, in_param, RES_MPT, RES_OP_MAP_ICM,
  422. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  423. MLX4_CMD_WRAPPED))
  424. mlx4_warn(dev, "Failed to free icm of mr index:%d\n",
  425. index);
  426. return;
  427. }
  428. return __mlx4_mpt_free_icm(dev, index);
  429. }
  430. int mlx4_mr_alloc(struct mlx4_dev *dev, u32 pd, u64 iova, u64 size, u32 access,
  431. int npages, int page_shift, struct mlx4_mr *mr)
  432. {
  433. u32 index;
  434. int err;
  435. index = mlx4_mpt_reserve(dev);
  436. if (index == -1)
  437. return -ENOMEM;
  438. err = mlx4_mr_alloc_reserved(dev, index, pd, iova, size,
  439. access, npages, page_shift, mr);
  440. if (err)
  441. mlx4_mpt_release(dev, index);
  442. return err;
  443. }
  444. EXPORT_SYMBOL_GPL(mlx4_mr_alloc);
  445. static int mlx4_mr_free_reserved(struct mlx4_dev *dev, struct mlx4_mr *mr)
  446. {
  447. int err;
  448. if (mr->enabled == MLX4_MPT_EN_HW) {
  449. err = mlx4_HW2SW_MPT(dev, NULL,
  450. key_to_hw_index(mr->key) &
  451. (dev->caps.num_mpts - 1));
  452. if (err) {
  453. mlx4_warn(dev, "HW2SW_MPT failed (%d), MR has MWs bound to it\n",
  454. err);
  455. return err;
  456. }
  457. mr->enabled = MLX4_MPT_EN_SW;
  458. }
  459. mlx4_mtt_cleanup(dev, &mr->mtt);
  460. return 0;
  461. }
  462. int mlx4_mr_free(struct mlx4_dev *dev, struct mlx4_mr *mr)
  463. {
  464. int ret;
  465. ret = mlx4_mr_free_reserved(dev, mr);
  466. if (ret)
  467. return ret;
  468. if (mr->enabled)
  469. mlx4_mpt_free_icm(dev, key_to_hw_index(mr->key));
  470. mlx4_mpt_release(dev, key_to_hw_index(mr->key));
  471. return 0;
  472. }
  473. EXPORT_SYMBOL_GPL(mlx4_mr_free);
  474. void mlx4_mr_rereg_mem_cleanup(struct mlx4_dev *dev, struct mlx4_mr *mr)
  475. {
  476. mlx4_mtt_cleanup(dev, &mr->mtt);
  477. mr->mtt.order = -1;
  478. }
  479. EXPORT_SYMBOL_GPL(mlx4_mr_rereg_mem_cleanup);
  480. int mlx4_mr_rereg_mem_write(struct mlx4_dev *dev, struct mlx4_mr *mr,
  481. u64 iova, u64 size, int npages,
  482. int page_shift, struct mlx4_mpt_entry *mpt_entry)
  483. {
  484. int err;
  485. err = mlx4_mtt_init(dev, npages, page_shift, &mr->mtt);
  486. if (err)
  487. return err;
  488. mpt_entry->start = cpu_to_be64(iova);
  489. mpt_entry->length = cpu_to_be64(size);
  490. mpt_entry->entity_size = cpu_to_be32(page_shift);
  491. mpt_entry->flags &= ~(cpu_to_be32(MLX4_MPT_FLAG_FREE |
  492. MLX4_MPT_FLAG_SW_OWNS));
  493. if (mr->mtt.order < 0) {
  494. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL);
  495. mpt_entry->mtt_addr = 0;
  496. } else {
  497. mpt_entry->mtt_addr = cpu_to_be64(mlx4_mtt_addr(dev,
  498. &mr->mtt));
  499. if (mr->mtt.page_shift == 0)
  500. mpt_entry->mtt_sz = cpu_to_be32(1 << mr->mtt.order);
  501. }
  502. if (mr->mtt.order >= 0 && mr->mtt.page_shift == 0) {
  503. /* fast register MR in free state */
  504. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_FREE);
  505. mpt_entry->pd_flags |= cpu_to_be32(MLX4_MPT_PD_FLAG_FAST_REG |
  506. MLX4_MPT_PD_FLAG_RAE);
  507. } else {
  508. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS);
  509. }
  510. mr->enabled = MLX4_MPT_EN_SW;
  511. return 0;
  512. }
  513. EXPORT_SYMBOL_GPL(mlx4_mr_rereg_mem_write);
  514. int mlx4_mr_enable(struct mlx4_dev *dev, struct mlx4_mr *mr)
  515. {
  516. struct mlx4_cmd_mailbox *mailbox;
  517. struct mlx4_mpt_entry *mpt_entry;
  518. int err;
  519. err = mlx4_mpt_alloc_icm(dev, key_to_hw_index(mr->key));
  520. if (err)
  521. return err;
  522. mailbox = mlx4_alloc_cmd_mailbox(dev);
  523. if (IS_ERR(mailbox)) {
  524. err = PTR_ERR(mailbox);
  525. goto err_table;
  526. }
  527. mpt_entry = mailbox->buf;
  528. mpt_entry->flags = cpu_to_be32(MLX4_MPT_FLAG_MIO |
  529. MLX4_MPT_FLAG_REGION |
  530. mr->access);
  531. mpt_entry->key = cpu_to_be32(key_to_hw_index(mr->key));
  532. mpt_entry->pd_flags = cpu_to_be32(mr->pd | MLX4_MPT_PD_FLAG_EN_INV);
  533. mpt_entry->start = cpu_to_be64(mr->iova);
  534. mpt_entry->length = cpu_to_be64(mr->size);
  535. mpt_entry->entity_size = cpu_to_be32(mr->mtt.page_shift);
  536. if (mr->mtt.order < 0) {
  537. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_PHYSICAL);
  538. mpt_entry->mtt_addr = 0;
  539. } else {
  540. mpt_entry->mtt_addr = cpu_to_be64(mlx4_mtt_addr(dev,
  541. &mr->mtt));
  542. }
  543. if (mr->mtt.order >= 0 && mr->mtt.page_shift == 0) {
  544. /* fast register MR in free state */
  545. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_FREE);
  546. mpt_entry->pd_flags |= cpu_to_be32(MLX4_MPT_PD_FLAG_FAST_REG |
  547. MLX4_MPT_PD_FLAG_RAE);
  548. mpt_entry->mtt_sz = cpu_to_be32(1 << mr->mtt.order);
  549. } else {
  550. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_SW_OWNS);
  551. }
  552. err = mlx4_SW2HW_MPT(dev, mailbox,
  553. key_to_hw_index(mr->key) & (dev->caps.num_mpts - 1));
  554. if (err) {
  555. mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err);
  556. goto err_cmd;
  557. }
  558. mr->enabled = MLX4_MPT_EN_HW;
  559. mlx4_free_cmd_mailbox(dev, mailbox);
  560. return 0;
  561. err_cmd:
  562. mlx4_free_cmd_mailbox(dev, mailbox);
  563. err_table:
  564. mlx4_mpt_free_icm(dev, key_to_hw_index(mr->key));
  565. return err;
  566. }
  567. EXPORT_SYMBOL_GPL(mlx4_mr_enable);
  568. static int mlx4_write_mtt_chunk(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  569. int start_index, int npages, u64 *page_list)
  570. {
  571. struct mlx4_priv *priv = mlx4_priv(dev);
  572. __be64 *mtts;
  573. dma_addr_t dma_handle;
  574. int i;
  575. mtts = mlx4_table_find(&priv->mr_table.mtt_table, mtt->offset +
  576. start_index, &dma_handle);
  577. if (!mtts)
  578. return -ENOMEM;
  579. dma_sync_single_for_cpu(&dev->persist->pdev->dev, dma_handle,
  580. npages * sizeof(u64), DMA_TO_DEVICE);
  581. for (i = 0; i < npages; ++i)
  582. mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
  583. dma_sync_single_for_device(&dev->persist->pdev->dev, dma_handle,
  584. npages * sizeof(u64), DMA_TO_DEVICE);
  585. return 0;
  586. }
  587. int __mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  588. int start_index, int npages, u64 *page_list)
  589. {
  590. int err = 0;
  591. int chunk;
  592. int mtts_per_page;
  593. int max_mtts_first_page;
  594. /* compute how may mtts fit in the first page */
  595. mtts_per_page = PAGE_SIZE / sizeof(u64);
  596. max_mtts_first_page = mtts_per_page - (mtt->offset + start_index)
  597. % mtts_per_page;
  598. chunk = min_t(int, max_mtts_first_page, npages);
  599. while (npages > 0) {
  600. err = mlx4_write_mtt_chunk(dev, mtt, start_index, chunk, page_list);
  601. if (err)
  602. return err;
  603. npages -= chunk;
  604. start_index += chunk;
  605. page_list += chunk;
  606. chunk = min_t(int, mtts_per_page, npages);
  607. }
  608. return err;
  609. }
  610. int mlx4_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  611. int start_index, int npages, u64 *page_list)
  612. {
  613. struct mlx4_cmd_mailbox *mailbox = NULL;
  614. __be64 *inbox = NULL;
  615. int chunk;
  616. int err = 0;
  617. int i;
  618. if (mtt->order < 0)
  619. return -EINVAL;
  620. if (mlx4_is_mfunc(dev)) {
  621. mailbox = mlx4_alloc_cmd_mailbox(dev);
  622. if (IS_ERR(mailbox))
  623. return PTR_ERR(mailbox);
  624. inbox = mailbox->buf;
  625. while (npages > 0) {
  626. chunk = min_t(int, MLX4_MAILBOX_SIZE / sizeof(u64) - 2,
  627. npages);
  628. inbox[0] = cpu_to_be64(mtt->offset + start_index);
  629. inbox[1] = 0;
  630. for (i = 0; i < chunk; ++i)
  631. inbox[i + 2] = cpu_to_be64(page_list[i] |
  632. MLX4_MTT_FLAG_PRESENT);
  633. err = mlx4_WRITE_MTT(dev, mailbox, chunk);
  634. if (err) {
  635. mlx4_free_cmd_mailbox(dev, mailbox);
  636. return err;
  637. }
  638. npages -= chunk;
  639. start_index += chunk;
  640. page_list += chunk;
  641. }
  642. mlx4_free_cmd_mailbox(dev, mailbox);
  643. return err;
  644. }
  645. return __mlx4_write_mtt(dev, mtt, start_index, npages, page_list);
  646. }
  647. EXPORT_SYMBOL_GPL(mlx4_write_mtt);
  648. int mlx4_buf_write_mtt(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  649. struct mlx4_buf *buf)
  650. {
  651. u64 *page_list;
  652. int err;
  653. int i;
  654. page_list = kcalloc(buf->npages, sizeof(*page_list), GFP_KERNEL);
  655. if (!page_list)
  656. return -ENOMEM;
  657. for (i = 0; i < buf->npages; ++i)
  658. if (buf->nbufs == 1)
  659. page_list[i] = buf->direct.map + (i << buf->page_shift);
  660. else
  661. page_list[i] = buf->page_list[i].map;
  662. err = mlx4_write_mtt(dev, mtt, 0, buf->npages, page_list);
  663. kfree(page_list);
  664. return err;
  665. }
  666. EXPORT_SYMBOL_GPL(mlx4_buf_write_mtt);
  667. int mlx4_mw_alloc(struct mlx4_dev *dev, u32 pd, enum mlx4_mw_type type,
  668. struct mlx4_mw *mw)
  669. {
  670. u32 index;
  671. if ((type == MLX4_MW_TYPE_1 &&
  672. !(dev->caps.flags & MLX4_DEV_CAP_FLAG_MEM_WINDOW)) ||
  673. (type == MLX4_MW_TYPE_2 &&
  674. !(dev->caps.bmme_flags & MLX4_BMME_FLAG_TYPE_2_WIN)))
  675. return -EOPNOTSUPP;
  676. index = mlx4_mpt_reserve(dev);
  677. if (index == -1)
  678. return -ENOMEM;
  679. mw->key = hw_index_to_key(index);
  680. mw->pd = pd;
  681. mw->type = type;
  682. mw->enabled = MLX4_MPT_DISABLED;
  683. return 0;
  684. }
  685. EXPORT_SYMBOL_GPL(mlx4_mw_alloc);
  686. int mlx4_mw_enable(struct mlx4_dev *dev, struct mlx4_mw *mw)
  687. {
  688. struct mlx4_cmd_mailbox *mailbox;
  689. struct mlx4_mpt_entry *mpt_entry;
  690. int err;
  691. err = mlx4_mpt_alloc_icm(dev, key_to_hw_index(mw->key));
  692. if (err)
  693. return err;
  694. mailbox = mlx4_alloc_cmd_mailbox(dev);
  695. if (IS_ERR(mailbox)) {
  696. err = PTR_ERR(mailbox);
  697. goto err_table;
  698. }
  699. mpt_entry = mailbox->buf;
  700. /* Note that the MLX4_MPT_FLAG_REGION bit in mpt_entry->flags is turned
  701. * off, thus creating a memory window and not a memory region.
  702. */
  703. mpt_entry->key = cpu_to_be32(key_to_hw_index(mw->key));
  704. mpt_entry->pd_flags = cpu_to_be32(mw->pd);
  705. if (mw->type == MLX4_MW_TYPE_2) {
  706. mpt_entry->flags |= cpu_to_be32(MLX4_MPT_FLAG_FREE);
  707. mpt_entry->qpn = cpu_to_be32(MLX4_MPT_QP_FLAG_BOUND_QP);
  708. mpt_entry->pd_flags |= cpu_to_be32(MLX4_MPT_PD_FLAG_EN_INV);
  709. }
  710. err = mlx4_SW2HW_MPT(dev, mailbox,
  711. key_to_hw_index(mw->key) &
  712. (dev->caps.num_mpts - 1));
  713. if (err) {
  714. mlx4_warn(dev, "SW2HW_MPT failed (%d)\n", err);
  715. goto err_cmd;
  716. }
  717. mw->enabled = MLX4_MPT_EN_HW;
  718. mlx4_free_cmd_mailbox(dev, mailbox);
  719. return 0;
  720. err_cmd:
  721. mlx4_free_cmd_mailbox(dev, mailbox);
  722. err_table:
  723. mlx4_mpt_free_icm(dev, key_to_hw_index(mw->key));
  724. return err;
  725. }
  726. EXPORT_SYMBOL_GPL(mlx4_mw_enable);
  727. void mlx4_mw_free(struct mlx4_dev *dev, struct mlx4_mw *mw)
  728. {
  729. int err;
  730. if (mw->enabled == MLX4_MPT_EN_HW) {
  731. err = mlx4_HW2SW_MPT(dev, NULL,
  732. key_to_hw_index(mw->key) &
  733. (dev->caps.num_mpts - 1));
  734. if (err)
  735. mlx4_warn(dev, "xxx HW2SW_MPT failed (%d)\n", err);
  736. mw->enabled = MLX4_MPT_EN_SW;
  737. }
  738. if (mw->enabled)
  739. mlx4_mpt_free_icm(dev, key_to_hw_index(mw->key));
  740. mlx4_mpt_release(dev, key_to_hw_index(mw->key));
  741. }
  742. EXPORT_SYMBOL_GPL(mlx4_mw_free);
  743. int mlx4_init_mr_table(struct mlx4_dev *dev)
  744. {
  745. struct mlx4_priv *priv = mlx4_priv(dev);
  746. struct mlx4_mr_table *mr_table = &priv->mr_table;
  747. int err;
  748. /* Nothing to do for slaves - all MR handling is forwarded
  749. * to the master */
  750. if (mlx4_is_slave(dev))
  751. return 0;
  752. if (!is_power_of_2(dev->caps.num_mpts))
  753. return -EINVAL;
  754. err = mlx4_bitmap_init(&mr_table->mpt_bitmap, dev->caps.num_mpts,
  755. ~0, dev->caps.reserved_mrws, 0);
  756. if (err)
  757. return err;
  758. err = mlx4_buddy_init(&mr_table->mtt_buddy,
  759. ilog2((u32)dev->caps.num_mtts /
  760. (1 << log_mtts_per_seg)));
  761. if (err)
  762. goto err_buddy;
  763. if (dev->caps.reserved_mtts) {
  764. priv->reserved_mtts =
  765. mlx4_alloc_mtt_range(dev,
  766. fls(dev->caps.reserved_mtts - 1));
  767. if (priv->reserved_mtts < 0) {
  768. mlx4_warn(dev, "MTT table of order %u is too small\n",
  769. mr_table->mtt_buddy.max_order);
  770. err = -ENOMEM;
  771. goto err_reserve_mtts;
  772. }
  773. }
  774. return 0;
  775. err_reserve_mtts:
  776. mlx4_buddy_cleanup(&mr_table->mtt_buddy);
  777. err_buddy:
  778. mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
  779. return err;
  780. }
  781. void mlx4_cleanup_mr_table(struct mlx4_dev *dev)
  782. {
  783. struct mlx4_priv *priv = mlx4_priv(dev);
  784. struct mlx4_mr_table *mr_table = &priv->mr_table;
  785. if (mlx4_is_slave(dev))
  786. return;
  787. if (priv->reserved_mtts >= 0)
  788. mlx4_free_mtt_range(dev, priv->reserved_mtts,
  789. fls(dev->caps.reserved_mtts - 1));
  790. mlx4_buddy_cleanup(&mr_table->mtt_buddy);
  791. mlx4_bitmap_cleanup(&mr_table->mpt_bitmap);
  792. }
  793. static inline int mlx4_check_fmr(struct mlx4_fmr *fmr, u64 *page_list,
  794. int npages, u64 iova)
  795. {
  796. int i, page_mask;
  797. if (npages > fmr->max_pages)
  798. return -EINVAL;
  799. page_mask = (1 << fmr->page_shift) - 1;
  800. /* We are getting page lists, so va must be page aligned. */
  801. if (iova & page_mask)
  802. return -EINVAL;
  803. /* Trust the user not to pass misaligned data in page_list */
  804. if (0)
  805. for (i = 0; i < npages; ++i) {
  806. if (page_list[i] & ~page_mask)
  807. return -EINVAL;
  808. }
  809. if (fmr->maps >= fmr->max_maps)
  810. return -EINVAL;
  811. return 0;
  812. }
  813. int mlx4_map_phys_fmr(struct mlx4_dev *dev, struct mlx4_fmr *fmr, u64 *page_list,
  814. int npages, u64 iova, u32 *lkey, u32 *rkey)
  815. {
  816. u32 key;
  817. int i, err;
  818. err = mlx4_check_fmr(fmr, page_list, npages, iova);
  819. if (err)
  820. return err;
  821. ++fmr->maps;
  822. key = key_to_hw_index(fmr->mr.key);
  823. key += dev->caps.num_mpts;
  824. *lkey = *rkey = fmr->mr.key = hw_index_to_key(key);
  825. *(u8 *) fmr->mpt = MLX4_MPT_STATUS_SW;
  826. /* Make sure MPT status is visible before writing MTT entries */
  827. wmb();
  828. dma_sync_single_for_cpu(&dev->persist->pdev->dev, fmr->dma_handle,
  829. npages * sizeof(u64), DMA_TO_DEVICE);
  830. for (i = 0; i < npages; ++i)
  831. fmr->mtts[i] = cpu_to_be64(page_list[i] | MLX4_MTT_FLAG_PRESENT);
  832. dma_sync_single_for_device(&dev->persist->pdev->dev, fmr->dma_handle,
  833. npages * sizeof(u64), DMA_TO_DEVICE);
  834. fmr->mpt->key = cpu_to_be32(key);
  835. fmr->mpt->lkey = cpu_to_be32(key);
  836. fmr->mpt->length = cpu_to_be64(npages * (1ull << fmr->page_shift));
  837. fmr->mpt->start = cpu_to_be64(iova);
  838. /* Make MTT entries are visible before setting MPT status */
  839. wmb();
  840. *(u8 *) fmr->mpt = MLX4_MPT_STATUS_HW;
  841. /* Make sure MPT status is visible before consumer can use FMR */
  842. wmb();
  843. return 0;
  844. }
  845. EXPORT_SYMBOL_GPL(mlx4_map_phys_fmr);
  846. int mlx4_fmr_alloc(struct mlx4_dev *dev, u32 pd, u32 access, int max_pages,
  847. int max_maps, u8 page_shift, struct mlx4_fmr *fmr)
  848. {
  849. struct mlx4_priv *priv = mlx4_priv(dev);
  850. int err = -ENOMEM;
  851. if (max_maps > dev->caps.max_fmr_maps)
  852. return -EINVAL;
  853. if (page_shift < (ffs(dev->caps.page_size_cap) - 1) || page_shift >= 32)
  854. return -EINVAL;
  855. /* All MTTs must fit in the same page */
  856. if (max_pages * sizeof(*fmr->mtts) > PAGE_SIZE)
  857. return -EINVAL;
  858. fmr->page_shift = page_shift;
  859. fmr->max_pages = max_pages;
  860. fmr->max_maps = max_maps;
  861. fmr->maps = 0;
  862. err = mlx4_mr_alloc(dev, pd, 0, 0, access, max_pages,
  863. page_shift, &fmr->mr);
  864. if (err)
  865. return err;
  866. fmr->mtts = mlx4_table_find(&priv->mr_table.mtt_table,
  867. fmr->mr.mtt.offset,
  868. &fmr->dma_handle);
  869. if (!fmr->mtts) {
  870. err = -ENOMEM;
  871. goto err_free;
  872. }
  873. return 0;
  874. err_free:
  875. (void) mlx4_mr_free(dev, &fmr->mr);
  876. return err;
  877. }
  878. EXPORT_SYMBOL_GPL(mlx4_fmr_alloc);
  879. int mlx4_fmr_enable(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
  880. {
  881. struct mlx4_priv *priv = mlx4_priv(dev);
  882. int err;
  883. err = mlx4_mr_enable(dev, &fmr->mr);
  884. if (err)
  885. return err;
  886. fmr->mpt = mlx4_table_find(&priv->mr_table.dmpt_table,
  887. key_to_hw_index(fmr->mr.key), NULL);
  888. if (!fmr->mpt)
  889. return -ENOMEM;
  890. return 0;
  891. }
  892. EXPORT_SYMBOL_GPL(mlx4_fmr_enable);
  893. void mlx4_fmr_unmap(struct mlx4_dev *dev, struct mlx4_fmr *fmr,
  894. u32 *lkey, u32 *rkey)
  895. {
  896. if (!fmr->maps)
  897. return;
  898. /* To unmap: it is sufficient to take back ownership from HW */
  899. *(u8 *)fmr->mpt = MLX4_MPT_STATUS_SW;
  900. /* Make sure MPT status is visible */
  901. wmb();
  902. fmr->maps = 0;
  903. }
  904. EXPORT_SYMBOL_GPL(mlx4_fmr_unmap);
  905. int mlx4_fmr_free(struct mlx4_dev *dev, struct mlx4_fmr *fmr)
  906. {
  907. int ret;
  908. if (fmr->maps)
  909. return -EBUSY;
  910. if (fmr->mr.enabled == MLX4_MPT_EN_HW) {
  911. /* In case of FMR was enabled and unmapped
  912. * make sure to give ownership of MPT back to HW
  913. * so HW2SW_MPT command will success.
  914. */
  915. *(u8 *)fmr->mpt = MLX4_MPT_STATUS_SW;
  916. /* Make sure MPT status is visible before changing MPT fields */
  917. wmb();
  918. fmr->mpt->length = 0;
  919. fmr->mpt->start = 0;
  920. /* Make sure MPT data is visible after changing MPT status */
  921. wmb();
  922. *(u8 *)fmr->mpt = MLX4_MPT_STATUS_HW;
  923. /* make sure MPT status is visible */
  924. wmb();
  925. }
  926. ret = mlx4_mr_free(dev, &fmr->mr);
  927. if (ret)
  928. return ret;
  929. fmr->mr.enabled = MLX4_MPT_DISABLED;
  930. return 0;
  931. }
  932. EXPORT_SYMBOL_GPL(mlx4_fmr_free);
  933. int mlx4_SYNC_TPT(struct mlx4_dev *dev)
  934. {
  935. return mlx4_cmd(dev, 0, 0, 0, MLX4_CMD_SYNC_TPT,
  936. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_NATIVE);
  937. }
  938. EXPORT_SYMBOL_GPL(mlx4_SYNC_TPT);