qp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. /*
  2. * Copyright (c) 2004 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005, 2006, 2007 Cisco Systems, Inc. All rights reserved.
  4. * Copyright (c) 2005, 2006, 2007, 2008 Mellanox Technologies. All rights reserved.
  5. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  6. *
  7. * This software is available to you under a choice of one of two
  8. * licenses. You may choose to be licensed under the terms of the GNU
  9. * General Public License (GPL) Version 2, available from the file
  10. * COPYING in the main directory of this source tree, or the
  11. * OpenIB.org BSD license below:
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above
  18. * copyright notice, this list of conditions and the following
  19. * disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials
  24. * provided with the distribution.
  25. *
  26. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  30. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  31. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  32. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  33. * SOFTWARE.
  34. */
  35. #include <linux/gfp.h>
  36. #include <linux/export.h>
  37. #include <linux/init.h>
  38. #include <linux/mlx4/cmd.h>
  39. #include <linux/mlx4/qp.h>
  40. #include "mlx4.h"
  41. #include "icm.h"
  42. void mlx4_qp_event(struct mlx4_dev *dev, u32 qpn, int event_type)
  43. {
  44. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  45. struct mlx4_qp *qp;
  46. spin_lock(&qp_table->lock);
  47. qp = __mlx4_qp_lookup(dev, qpn);
  48. if (qp)
  49. atomic_inc(&qp->refcount);
  50. spin_unlock(&qp_table->lock);
  51. if (!qp) {
  52. mlx4_dbg(dev, "Async event for none existent QP %08x\n", qpn);
  53. return;
  54. }
  55. qp->event(qp, event_type);
  56. if (atomic_dec_and_test(&qp->refcount))
  57. complete(&qp->free);
  58. }
  59. static int is_qp0(struct mlx4_dev *dev, struct mlx4_qp *qp)
  60. {
  61. return qp->qpn >= dev->caps.sqp_start &&
  62. qp->qpn <= dev->caps.sqp_start + 1;
  63. }
  64. static int __mlx4_qp_modify(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  65. enum mlx4_qp_state cur_state, enum mlx4_qp_state new_state,
  66. struct mlx4_qp_context *context,
  67. enum mlx4_qp_optpar optpar,
  68. int sqd_event, struct mlx4_qp *qp, int native)
  69. {
  70. static const u16 op[MLX4_QP_NUM_STATE][MLX4_QP_NUM_STATE] = {
  71. [MLX4_QP_STATE_RST] = {
  72. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  73. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  74. [MLX4_QP_STATE_INIT] = MLX4_CMD_RST2INIT_QP,
  75. },
  76. [MLX4_QP_STATE_INIT] = {
  77. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  78. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  79. [MLX4_QP_STATE_INIT] = MLX4_CMD_INIT2INIT_QP,
  80. [MLX4_QP_STATE_RTR] = MLX4_CMD_INIT2RTR_QP,
  81. },
  82. [MLX4_QP_STATE_RTR] = {
  83. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  84. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  85. [MLX4_QP_STATE_RTS] = MLX4_CMD_RTR2RTS_QP,
  86. },
  87. [MLX4_QP_STATE_RTS] = {
  88. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  89. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  90. [MLX4_QP_STATE_RTS] = MLX4_CMD_RTS2RTS_QP,
  91. [MLX4_QP_STATE_SQD] = MLX4_CMD_RTS2SQD_QP,
  92. },
  93. [MLX4_QP_STATE_SQD] = {
  94. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  95. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  96. [MLX4_QP_STATE_RTS] = MLX4_CMD_SQD2RTS_QP,
  97. [MLX4_QP_STATE_SQD] = MLX4_CMD_SQD2SQD_QP,
  98. },
  99. [MLX4_QP_STATE_SQER] = {
  100. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  101. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  102. [MLX4_QP_STATE_RTS] = MLX4_CMD_SQERR2RTS_QP,
  103. },
  104. [MLX4_QP_STATE_ERR] = {
  105. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  106. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  107. }
  108. };
  109. struct mlx4_priv *priv = mlx4_priv(dev);
  110. struct mlx4_cmd_mailbox *mailbox;
  111. int ret = 0;
  112. u8 port;
  113. if (cur_state >= MLX4_QP_NUM_STATE || new_state >= MLX4_QP_NUM_STATE ||
  114. !op[cur_state][new_state])
  115. return -EINVAL;
  116. if (op[cur_state][new_state] == MLX4_CMD_2RST_QP) {
  117. ret = mlx4_cmd(dev, 0, qp->qpn, 2,
  118. MLX4_CMD_2RST_QP, MLX4_CMD_TIME_CLASS_A, native);
  119. if (mlx4_is_master(dev) && cur_state != MLX4_QP_STATE_ERR &&
  120. cur_state != MLX4_QP_STATE_RST &&
  121. is_qp0(dev, qp)) {
  122. port = (qp->qpn & 1) + 1;
  123. priv->mfunc.master.qp0_state[port].qp0_active = 0;
  124. }
  125. return ret;
  126. }
  127. mailbox = mlx4_alloc_cmd_mailbox(dev);
  128. if (IS_ERR(mailbox))
  129. return PTR_ERR(mailbox);
  130. if (cur_state == MLX4_QP_STATE_RST && new_state == MLX4_QP_STATE_INIT) {
  131. u64 mtt_addr = mlx4_mtt_addr(dev, mtt);
  132. context->mtt_base_addr_h = mtt_addr >> 32;
  133. context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
  134. context->log_page_size = mtt->page_shift - MLX4_ICM_PAGE_SHIFT;
  135. }
  136. *(__be32 *) mailbox->buf = cpu_to_be32(optpar);
  137. memcpy(mailbox->buf + 8, context, sizeof *context);
  138. ((struct mlx4_qp_context *) (mailbox->buf + 8))->local_qpn =
  139. cpu_to_be32(qp->qpn);
  140. ret = mlx4_cmd(dev, mailbox->dma,
  141. qp->qpn | (!!sqd_event << 31),
  142. new_state == MLX4_QP_STATE_RST ? 2 : 0,
  143. op[cur_state][new_state], MLX4_CMD_TIME_CLASS_C, native);
  144. mlx4_free_cmd_mailbox(dev, mailbox);
  145. return ret;
  146. }
  147. int mlx4_qp_modify(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  148. enum mlx4_qp_state cur_state, enum mlx4_qp_state new_state,
  149. struct mlx4_qp_context *context,
  150. enum mlx4_qp_optpar optpar,
  151. int sqd_event, struct mlx4_qp *qp)
  152. {
  153. return __mlx4_qp_modify(dev, mtt, cur_state, new_state, context,
  154. optpar, sqd_event, qp, 0);
  155. }
  156. EXPORT_SYMBOL_GPL(mlx4_qp_modify);
  157. int __mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align,
  158. int *base)
  159. {
  160. struct mlx4_priv *priv = mlx4_priv(dev);
  161. struct mlx4_qp_table *qp_table = &priv->qp_table;
  162. *base = mlx4_bitmap_alloc_range(&qp_table->bitmap, cnt, align);
  163. if (*base == -1)
  164. return -ENOMEM;
  165. return 0;
  166. }
  167. int mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align, int *base)
  168. {
  169. u64 in_param;
  170. u64 out_param;
  171. int err;
  172. if (mlx4_is_mfunc(dev)) {
  173. set_param_l(&in_param, cnt);
  174. set_param_h(&in_param, align);
  175. err = mlx4_cmd_imm(dev, in_param, &out_param,
  176. RES_QP, RES_OP_RESERVE,
  177. MLX4_CMD_ALLOC_RES,
  178. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  179. if (err)
  180. return err;
  181. *base = get_param_l(&out_param);
  182. return 0;
  183. }
  184. return __mlx4_qp_reserve_range(dev, cnt, align, base);
  185. }
  186. EXPORT_SYMBOL_GPL(mlx4_qp_reserve_range);
  187. void __mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
  188. {
  189. struct mlx4_priv *priv = mlx4_priv(dev);
  190. struct mlx4_qp_table *qp_table = &priv->qp_table;
  191. if (mlx4_is_qp_reserved(dev, (u32) base_qpn))
  192. return;
  193. mlx4_bitmap_free_range(&qp_table->bitmap, base_qpn, cnt);
  194. }
  195. void mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
  196. {
  197. u64 in_param;
  198. int err;
  199. if (mlx4_is_mfunc(dev)) {
  200. set_param_l(&in_param, base_qpn);
  201. set_param_h(&in_param, cnt);
  202. err = mlx4_cmd(dev, in_param, RES_QP, RES_OP_RESERVE,
  203. MLX4_CMD_FREE_RES,
  204. MLX4_CMD_TIME_CLASS_A, MLX4_CMD_WRAPPED);
  205. if (err) {
  206. mlx4_warn(dev, "Failed to release qp range"
  207. " base:%d cnt:%d\n", base_qpn, cnt);
  208. }
  209. } else
  210. __mlx4_qp_release_range(dev, base_qpn, cnt);
  211. }
  212. EXPORT_SYMBOL_GPL(mlx4_qp_release_range);
  213. int __mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn)
  214. {
  215. struct mlx4_priv *priv = mlx4_priv(dev);
  216. struct mlx4_qp_table *qp_table = &priv->qp_table;
  217. int err;
  218. err = mlx4_table_get(dev, &qp_table->qp_table, qpn);
  219. if (err)
  220. goto err_out;
  221. err = mlx4_table_get(dev, &qp_table->auxc_table, qpn);
  222. if (err)
  223. goto err_put_qp;
  224. err = mlx4_table_get(dev, &qp_table->altc_table, qpn);
  225. if (err)
  226. goto err_put_auxc;
  227. err = mlx4_table_get(dev, &qp_table->rdmarc_table, qpn);
  228. if (err)
  229. goto err_put_altc;
  230. err = mlx4_table_get(dev, &qp_table->cmpt_table, qpn);
  231. if (err)
  232. goto err_put_rdmarc;
  233. return 0;
  234. err_put_rdmarc:
  235. mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
  236. err_put_altc:
  237. mlx4_table_put(dev, &qp_table->altc_table, qpn);
  238. err_put_auxc:
  239. mlx4_table_put(dev, &qp_table->auxc_table, qpn);
  240. err_put_qp:
  241. mlx4_table_put(dev, &qp_table->qp_table, qpn);
  242. err_out:
  243. return err;
  244. }
  245. static int mlx4_qp_alloc_icm(struct mlx4_dev *dev, int qpn)
  246. {
  247. u64 param;
  248. if (mlx4_is_mfunc(dev)) {
  249. set_param_l(&param, qpn);
  250. return mlx4_cmd_imm(dev, param, &param, RES_QP, RES_OP_MAP_ICM,
  251. MLX4_CMD_ALLOC_RES, MLX4_CMD_TIME_CLASS_A,
  252. MLX4_CMD_WRAPPED);
  253. }
  254. return __mlx4_qp_alloc_icm(dev, qpn);
  255. }
  256. void __mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
  257. {
  258. struct mlx4_priv *priv = mlx4_priv(dev);
  259. struct mlx4_qp_table *qp_table = &priv->qp_table;
  260. mlx4_table_put(dev, &qp_table->cmpt_table, qpn);
  261. mlx4_table_put(dev, &qp_table->rdmarc_table, qpn);
  262. mlx4_table_put(dev, &qp_table->altc_table, qpn);
  263. mlx4_table_put(dev, &qp_table->auxc_table, qpn);
  264. mlx4_table_put(dev, &qp_table->qp_table, qpn);
  265. }
  266. static void mlx4_qp_free_icm(struct mlx4_dev *dev, int qpn)
  267. {
  268. u64 in_param;
  269. if (mlx4_is_mfunc(dev)) {
  270. set_param_l(&in_param, qpn);
  271. if (mlx4_cmd(dev, in_param, RES_QP, RES_OP_MAP_ICM,
  272. MLX4_CMD_FREE_RES, MLX4_CMD_TIME_CLASS_A,
  273. MLX4_CMD_WRAPPED))
  274. mlx4_warn(dev, "Failed to free icm of qp:%d\n", qpn);
  275. } else
  276. __mlx4_qp_free_icm(dev, qpn);
  277. }
  278. int mlx4_qp_alloc(struct mlx4_dev *dev, int qpn, struct mlx4_qp *qp)
  279. {
  280. struct mlx4_priv *priv = mlx4_priv(dev);
  281. struct mlx4_qp_table *qp_table = &priv->qp_table;
  282. int err;
  283. if (!qpn)
  284. return -EINVAL;
  285. qp->qpn = qpn;
  286. err = mlx4_qp_alloc_icm(dev, qpn);
  287. if (err)
  288. return err;
  289. spin_lock_irq(&qp_table->lock);
  290. err = radix_tree_insert(&dev->qp_table_tree, qp->qpn &
  291. (dev->caps.num_qps - 1), qp);
  292. spin_unlock_irq(&qp_table->lock);
  293. if (err)
  294. goto err_icm;
  295. atomic_set(&qp->refcount, 1);
  296. init_completion(&qp->free);
  297. return 0;
  298. err_icm:
  299. mlx4_qp_free_icm(dev, qpn);
  300. return err;
  301. }
  302. EXPORT_SYMBOL_GPL(mlx4_qp_alloc);
  303. void mlx4_qp_remove(struct mlx4_dev *dev, struct mlx4_qp *qp)
  304. {
  305. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  306. unsigned long flags;
  307. spin_lock_irqsave(&qp_table->lock, flags);
  308. radix_tree_delete(&dev->qp_table_tree, qp->qpn & (dev->caps.num_qps - 1));
  309. spin_unlock_irqrestore(&qp_table->lock, flags);
  310. }
  311. EXPORT_SYMBOL_GPL(mlx4_qp_remove);
  312. void mlx4_qp_free(struct mlx4_dev *dev, struct mlx4_qp *qp)
  313. {
  314. if (atomic_dec_and_test(&qp->refcount))
  315. complete(&qp->free);
  316. wait_for_completion(&qp->free);
  317. mlx4_qp_free_icm(dev, qp->qpn);
  318. }
  319. EXPORT_SYMBOL_GPL(mlx4_qp_free);
  320. static int mlx4_CONF_SPECIAL_QP(struct mlx4_dev *dev, u32 base_qpn)
  321. {
  322. return mlx4_cmd(dev, 0, base_qpn, 0, MLX4_CMD_CONF_SPECIAL_QP,
  323. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  324. }
  325. int mlx4_init_qp_table(struct mlx4_dev *dev)
  326. {
  327. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  328. int err;
  329. int reserved_from_top = 0;
  330. spin_lock_init(&qp_table->lock);
  331. INIT_RADIX_TREE(&dev->qp_table_tree, GFP_ATOMIC);
  332. if (mlx4_is_slave(dev))
  333. return 0;
  334. /*
  335. * We reserve 2 extra QPs per port for the special QPs. The
  336. * block of special QPs must be aligned to a multiple of 8, so
  337. * round up.
  338. *
  339. * We also reserve the MSB of the 24-bit QP number to indicate
  340. * that a QP is an XRC QP.
  341. */
  342. dev->caps.sqp_start =
  343. ALIGN(dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW], 8);
  344. {
  345. int sort[MLX4_NUM_QP_REGION];
  346. int i, j, tmp;
  347. int last_base = dev->caps.num_qps;
  348. for (i = 1; i < MLX4_NUM_QP_REGION; ++i)
  349. sort[i] = i;
  350. for (i = MLX4_NUM_QP_REGION; i > 0; --i) {
  351. for (j = 2; j < i; ++j) {
  352. if (dev->caps.reserved_qps_cnt[sort[j]] >
  353. dev->caps.reserved_qps_cnt[sort[j - 1]]) {
  354. tmp = sort[j];
  355. sort[j] = sort[j - 1];
  356. sort[j - 1] = tmp;
  357. }
  358. }
  359. }
  360. for (i = 1; i < MLX4_NUM_QP_REGION; ++i) {
  361. last_base -= dev->caps.reserved_qps_cnt[sort[i]];
  362. dev->caps.reserved_qps_base[sort[i]] = last_base;
  363. reserved_from_top +=
  364. dev->caps.reserved_qps_cnt[sort[i]];
  365. }
  366. }
  367. err = mlx4_bitmap_init(&qp_table->bitmap, dev->caps.num_qps,
  368. (1 << 23) - 1, dev->caps.sqp_start + 8,
  369. reserved_from_top);
  370. if (err)
  371. return err;
  372. return mlx4_CONF_SPECIAL_QP(dev, dev->caps.sqp_start);
  373. }
  374. void mlx4_cleanup_qp_table(struct mlx4_dev *dev)
  375. {
  376. if (mlx4_is_slave(dev))
  377. return;
  378. mlx4_CONF_SPECIAL_QP(dev, 0);
  379. mlx4_bitmap_cleanup(&mlx4_priv(dev)->qp_table.bitmap);
  380. }
  381. int mlx4_qp_query(struct mlx4_dev *dev, struct mlx4_qp *qp,
  382. struct mlx4_qp_context *context)
  383. {
  384. struct mlx4_cmd_mailbox *mailbox;
  385. int err;
  386. mailbox = mlx4_alloc_cmd_mailbox(dev);
  387. if (IS_ERR(mailbox))
  388. return PTR_ERR(mailbox);
  389. err = mlx4_cmd_box(dev, 0, mailbox->dma, qp->qpn, 0,
  390. MLX4_CMD_QUERY_QP, MLX4_CMD_TIME_CLASS_A,
  391. MLX4_CMD_WRAPPED);
  392. if (!err)
  393. memcpy(context, mailbox->buf + 8, sizeof *context);
  394. mlx4_free_cmd_mailbox(dev, mailbox);
  395. return err;
  396. }
  397. EXPORT_SYMBOL_GPL(mlx4_qp_query);
  398. int mlx4_qp_to_ready(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  399. struct mlx4_qp_context *context,
  400. struct mlx4_qp *qp, enum mlx4_qp_state *qp_state)
  401. {
  402. int err;
  403. int i;
  404. enum mlx4_qp_state states[] = {
  405. MLX4_QP_STATE_RST,
  406. MLX4_QP_STATE_INIT,
  407. MLX4_QP_STATE_RTR,
  408. MLX4_QP_STATE_RTS
  409. };
  410. for (i = 0; i < ARRAY_SIZE(states) - 1; i++) {
  411. context->flags &= cpu_to_be32(~(0xf << 28));
  412. context->flags |= cpu_to_be32(states[i + 1] << 28);
  413. err = mlx4_qp_modify(dev, mtt, states[i], states[i + 1],
  414. context, 0, 0, qp);
  415. if (err) {
  416. mlx4_err(dev, "Failed to bring QP to state: "
  417. "%d with error: %d\n",
  418. states[i + 1], err);
  419. return err;
  420. }
  421. *qp_state = states[i + 1];
  422. }
  423. return 0;
  424. }
  425. EXPORT_SYMBOL_GPL(mlx4_qp_to_ready);