qp.c 10 KB

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