qp.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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/mlx4/cmd.h>
  36. #include <linux/mlx4/qp.h>
  37. #include "mlx4.h"
  38. #include "icm.h"
  39. void mlx4_qp_event(struct mlx4_dev *dev, u32 qpn, int event_type)
  40. {
  41. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  42. struct mlx4_qp *qp;
  43. spin_lock(&qp_table->lock);
  44. qp = __mlx4_qp_lookup(dev, qpn);
  45. if (qp)
  46. atomic_inc(&qp->refcount);
  47. spin_unlock(&qp_table->lock);
  48. if (!qp) {
  49. mlx4_warn(dev, "Async event for bogus QP %08x\n", qpn);
  50. return;
  51. }
  52. qp->event(qp, event_type);
  53. if (atomic_dec_and_test(&qp->refcount))
  54. complete(&qp->free);
  55. }
  56. int mlx4_qp_modify(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  57. enum mlx4_qp_state cur_state, enum mlx4_qp_state new_state,
  58. struct mlx4_qp_context *context, enum mlx4_qp_optpar optpar,
  59. int sqd_event, struct mlx4_qp *qp)
  60. {
  61. static const u16 op[MLX4_QP_NUM_STATE][MLX4_QP_NUM_STATE] = {
  62. [MLX4_QP_STATE_RST] = {
  63. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  64. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  65. [MLX4_QP_STATE_INIT] = MLX4_CMD_RST2INIT_QP,
  66. },
  67. [MLX4_QP_STATE_INIT] = {
  68. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  69. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  70. [MLX4_QP_STATE_INIT] = MLX4_CMD_INIT2INIT_QP,
  71. [MLX4_QP_STATE_RTR] = MLX4_CMD_INIT2RTR_QP,
  72. },
  73. [MLX4_QP_STATE_RTR] = {
  74. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  75. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  76. [MLX4_QP_STATE_RTS] = MLX4_CMD_RTR2RTS_QP,
  77. },
  78. [MLX4_QP_STATE_RTS] = {
  79. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  80. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  81. [MLX4_QP_STATE_RTS] = MLX4_CMD_RTS2RTS_QP,
  82. [MLX4_QP_STATE_SQD] = MLX4_CMD_RTS2SQD_QP,
  83. },
  84. [MLX4_QP_STATE_SQD] = {
  85. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  86. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  87. [MLX4_QP_STATE_RTS] = MLX4_CMD_SQD2RTS_QP,
  88. [MLX4_QP_STATE_SQD] = MLX4_CMD_SQD2SQD_QP,
  89. },
  90. [MLX4_QP_STATE_SQER] = {
  91. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  92. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  93. [MLX4_QP_STATE_RTS] = MLX4_CMD_SQERR2RTS_QP,
  94. },
  95. [MLX4_QP_STATE_ERR] = {
  96. [MLX4_QP_STATE_RST] = MLX4_CMD_2RST_QP,
  97. [MLX4_QP_STATE_ERR] = MLX4_CMD_2ERR_QP,
  98. }
  99. };
  100. struct mlx4_cmd_mailbox *mailbox;
  101. int ret = 0;
  102. if (cur_state >= MLX4_QP_NUM_STATE || new_state >= MLX4_QP_NUM_STATE ||
  103. !op[cur_state][new_state])
  104. return -EINVAL;
  105. if (op[cur_state][new_state] == MLX4_CMD_2RST_QP)
  106. return mlx4_cmd(dev, 0, qp->qpn, 2,
  107. MLX4_CMD_2RST_QP, MLX4_CMD_TIME_CLASS_A);
  108. mailbox = mlx4_alloc_cmd_mailbox(dev);
  109. if (IS_ERR(mailbox))
  110. return PTR_ERR(mailbox);
  111. if (cur_state == MLX4_QP_STATE_RST && new_state == MLX4_QP_STATE_INIT) {
  112. u64 mtt_addr = mlx4_mtt_addr(dev, mtt);
  113. context->mtt_base_addr_h = mtt_addr >> 32;
  114. context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
  115. context->log_page_size = mtt->page_shift - MLX4_ICM_PAGE_SHIFT;
  116. }
  117. *(__be32 *) mailbox->buf = cpu_to_be32(optpar);
  118. memcpy(mailbox->buf + 8, context, sizeof *context);
  119. ((struct mlx4_qp_context *) (mailbox->buf + 8))->local_qpn =
  120. cpu_to_be32(qp->qpn);
  121. ret = mlx4_cmd(dev, mailbox->dma, qp->qpn | (!!sqd_event << 31),
  122. new_state == MLX4_QP_STATE_RST ? 2 : 0,
  123. op[cur_state][new_state], MLX4_CMD_TIME_CLASS_C);
  124. mlx4_free_cmd_mailbox(dev, mailbox);
  125. return ret;
  126. }
  127. EXPORT_SYMBOL_GPL(mlx4_qp_modify);
  128. int mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align, int *base)
  129. {
  130. struct mlx4_priv *priv = mlx4_priv(dev);
  131. struct mlx4_qp_table *qp_table = &priv->qp_table;
  132. int qpn;
  133. qpn = mlx4_bitmap_alloc_range(&qp_table->bitmap, cnt, align);
  134. if (qpn == -1)
  135. return -ENOMEM;
  136. *base = qpn;
  137. return 0;
  138. }
  139. EXPORT_SYMBOL_GPL(mlx4_qp_reserve_range);
  140. void mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
  141. {
  142. struct mlx4_priv *priv = mlx4_priv(dev);
  143. struct mlx4_qp_table *qp_table = &priv->qp_table;
  144. if (base_qpn < dev->caps.sqp_start + 8)
  145. return;
  146. mlx4_bitmap_free_range(&qp_table->bitmap, base_qpn, cnt);
  147. }
  148. EXPORT_SYMBOL_GPL(mlx4_qp_release_range);
  149. int mlx4_qp_alloc(struct mlx4_dev *dev, int qpn, struct mlx4_qp *qp)
  150. {
  151. struct mlx4_priv *priv = mlx4_priv(dev);
  152. struct mlx4_qp_table *qp_table = &priv->qp_table;
  153. int err;
  154. if (!qpn)
  155. return -EINVAL;
  156. qp->qpn = qpn;
  157. err = mlx4_table_get(dev, &qp_table->qp_table, qp->qpn);
  158. if (err)
  159. goto err_out;
  160. err = mlx4_table_get(dev, &qp_table->auxc_table, qp->qpn);
  161. if (err)
  162. goto err_put_qp;
  163. err = mlx4_table_get(dev, &qp_table->altc_table, qp->qpn);
  164. if (err)
  165. goto err_put_auxc;
  166. err = mlx4_table_get(dev, &qp_table->rdmarc_table, qp->qpn);
  167. if (err)
  168. goto err_put_altc;
  169. err = mlx4_table_get(dev, &qp_table->cmpt_table, qp->qpn);
  170. if (err)
  171. goto err_put_rdmarc;
  172. spin_lock_irq(&qp_table->lock);
  173. err = radix_tree_insert(&dev->qp_table_tree, qp->qpn & (dev->caps.num_qps - 1), qp);
  174. spin_unlock_irq(&qp_table->lock);
  175. if (err)
  176. goto err_put_cmpt;
  177. atomic_set(&qp->refcount, 1);
  178. init_completion(&qp->free);
  179. return 0;
  180. err_put_cmpt:
  181. mlx4_table_put(dev, &qp_table->cmpt_table, qp->qpn);
  182. err_put_rdmarc:
  183. mlx4_table_put(dev, &qp_table->rdmarc_table, qp->qpn);
  184. err_put_altc:
  185. mlx4_table_put(dev, &qp_table->altc_table, qp->qpn);
  186. err_put_auxc:
  187. mlx4_table_put(dev, &qp_table->auxc_table, qp->qpn);
  188. err_put_qp:
  189. mlx4_table_put(dev, &qp_table->qp_table, qp->qpn);
  190. err_out:
  191. return err;
  192. }
  193. EXPORT_SYMBOL_GPL(mlx4_qp_alloc);
  194. void mlx4_qp_remove(struct mlx4_dev *dev, struct mlx4_qp *qp)
  195. {
  196. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  197. unsigned long flags;
  198. spin_lock_irqsave(&qp_table->lock, flags);
  199. radix_tree_delete(&dev->qp_table_tree, qp->qpn & (dev->caps.num_qps - 1));
  200. spin_unlock_irqrestore(&qp_table->lock, flags);
  201. }
  202. EXPORT_SYMBOL_GPL(mlx4_qp_remove);
  203. void mlx4_qp_free(struct mlx4_dev *dev, struct mlx4_qp *qp)
  204. {
  205. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  206. if (atomic_dec_and_test(&qp->refcount))
  207. complete(&qp->free);
  208. wait_for_completion(&qp->free);
  209. mlx4_table_put(dev, &qp_table->cmpt_table, qp->qpn);
  210. mlx4_table_put(dev, &qp_table->rdmarc_table, qp->qpn);
  211. mlx4_table_put(dev, &qp_table->altc_table, qp->qpn);
  212. mlx4_table_put(dev, &qp_table->auxc_table, qp->qpn);
  213. mlx4_table_put(dev, &qp_table->qp_table, qp->qpn);
  214. }
  215. EXPORT_SYMBOL_GPL(mlx4_qp_free);
  216. static int mlx4_CONF_SPECIAL_QP(struct mlx4_dev *dev, u32 base_qpn)
  217. {
  218. return mlx4_cmd(dev, 0, base_qpn, 0, MLX4_CMD_CONF_SPECIAL_QP,
  219. MLX4_CMD_TIME_CLASS_B);
  220. }
  221. int mlx4_init_qp_table(struct mlx4_dev *dev)
  222. {
  223. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  224. int err;
  225. int reserved_from_top = 0;
  226. spin_lock_init(&qp_table->lock);
  227. INIT_RADIX_TREE(&dev->qp_table_tree, GFP_ATOMIC);
  228. /*
  229. * We reserve 2 extra QPs per port for the special QPs. The
  230. * block of special QPs must be aligned to a multiple of 8, so
  231. * round up.
  232. */
  233. dev->caps.sqp_start =
  234. ALIGN(dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW], 8);
  235. {
  236. int sort[MLX4_NUM_QP_REGION];
  237. int i, j, tmp;
  238. int last_base = dev->caps.num_qps;
  239. for (i = 1; i < MLX4_NUM_QP_REGION; ++i)
  240. sort[i] = i;
  241. for (i = MLX4_NUM_QP_REGION; i > 0; --i) {
  242. for (j = 2; j < i; ++j) {
  243. if (dev->caps.reserved_qps_cnt[sort[j]] >
  244. dev->caps.reserved_qps_cnt[sort[j - 1]]) {
  245. tmp = sort[j];
  246. sort[j] = sort[j - 1];
  247. sort[j - 1] = tmp;
  248. }
  249. }
  250. }
  251. for (i = 1; i < MLX4_NUM_QP_REGION; ++i) {
  252. last_base -= dev->caps.reserved_qps_cnt[sort[i]];
  253. dev->caps.reserved_qps_base[sort[i]] = last_base;
  254. reserved_from_top +=
  255. dev->caps.reserved_qps_cnt[sort[i]];
  256. }
  257. }
  258. err = mlx4_bitmap_init(&qp_table->bitmap, dev->caps.num_qps,
  259. (1 << 23) - 1, dev->caps.sqp_start + 8,
  260. reserved_from_top);
  261. if (err)
  262. return err;
  263. return mlx4_CONF_SPECIAL_QP(dev, dev->caps.sqp_start);
  264. }
  265. void mlx4_cleanup_qp_table(struct mlx4_dev *dev)
  266. {
  267. mlx4_CONF_SPECIAL_QP(dev, 0);
  268. mlx4_bitmap_cleanup(&mlx4_priv(dev)->qp_table.bitmap);
  269. }
  270. int mlx4_qp_query(struct mlx4_dev *dev, struct mlx4_qp *qp,
  271. struct mlx4_qp_context *context)
  272. {
  273. struct mlx4_cmd_mailbox *mailbox;
  274. int err;
  275. mailbox = mlx4_alloc_cmd_mailbox(dev);
  276. if (IS_ERR(mailbox))
  277. return PTR_ERR(mailbox);
  278. err = mlx4_cmd_box(dev, 0, mailbox->dma, qp->qpn, 0,
  279. MLX4_CMD_QUERY_QP, MLX4_CMD_TIME_CLASS_A);
  280. if (!err)
  281. memcpy(context, mailbox->buf + 8, sizeof *context);
  282. mlx4_free_cmd_mailbox(dev, mailbox);
  283. return err;
  284. }
  285. EXPORT_SYMBOL_GPL(mlx4_qp_query);
  286. int mlx4_qp_to_ready(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  287. struct mlx4_qp_context *context,
  288. struct mlx4_qp *qp, enum mlx4_qp_state *qp_state)
  289. {
  290. int err;
  291. int i;
  292. enum mlx4_qp_state states[] = {
  293. MLX4_QP_STATE_RST,
  294. MLX4_QP_STATE_INIT,
  295. MLX4_QP_STATE_RTR,
  296. MLX4_QP_STATE_RTS
  297. };
  298. for (i = 0; i < ARRAY_SIZE(states) - 1; i++) {
  299. context->flags &= cpu_to_be32(~(0xf << 28));
  300. context->flags |= cpu_to_be32(states[i + 1] << 28);
  301. err = mlx4_qp_modify(dev, mtt, states[i], states[i + 1],
  302. context, 0, 0, qp);
  303. if (err) {
  304. mlx4_err(dev, "Failed to bring QP to state: "
  305. "%d with error: %d\n",
  306. states[i + 1], err);
  307. return err;
  308. }
  309. *qp_state = states[i + 1];
  310. }
  311. return 0;
  312. }
  313. EXPORT_SYMBOL_GPL(mlx4_qp_to_ready);