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