mr.c 28 KB

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