qp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. MLX4_CMD_WRAPPED);
  111. mailbox = mlx4_alloc_cmd_mailbox(dev);
  112. if (IS_ERR(mailbox))
  113. return PTR_ERR(mailbox);
  114. if (cur_state == MLX4_QP_STATE_RST && new_state == MLX4_QP_STATE_INIT) {
  115. u64 mtt_addr = mlx4_mtt_addr(dev, mtt);
  116. context->mtt_base_addr_h = mtt_addr >> 32;
  117. context->mtt_base_addr_l = cpu_to_be32(mtt_addr & 0xffffffff);
  118. context->log_page_size = mtt->page_shift - MLX4_ICM_PAGE_SHIFT;
  119. }
  120. *(__be32 *) mailbox->buf = cpu_to_be32(optpar);
  121. memcpy(mailbox->buf + 8, context, sizeof *context);
  122. ((struct mlx4_qp_context *) (mailbox->buf + 8))->local_qpn =
  123. cpu_to_be32(qp->qpn);
  124. ret = mlx4_cmd(dev, mailbox->dma, qp->qpn | (!!sqd_event << 31),
  125. new_state == MLX4_QP_STATE_RST ? 2 : 0,
  126. op[cur_state][new_state], MLX4_CMD_TIME_CLASS_C,
  127. MLX4_CMD_WRAPPED);
  128. mlx4_free_cmd_mailbox(dev, mailbox);
  129. return ret;
  130. }
  131. EXPORT_SYMBOL_GPL(mlx4_qp_modify);
  132. int mlx4_qp_reserve_range(struct mlx4_dev *dev, int cnt, int align, int *base)
  133. {
  134. struct mlx4_priv *priv = mlx4_priv(dev);
  135. struct mlx4_qp_table *qp_table = &priv->qp_table;
  136. int qpn;
  137. qpn = mlx4_bitmap_alloc_range(&qp_table->bitmap, cnt, align);
  138. if (qpn == -1)
  139. return -ENOMEM;
  140. *base = qpn;
  141. return 0;
  142. }
  143. EXPORT_SYMBOL_GPL(mlx4_qp_reserve_range);
  144. void mlx4_qp_release_range(struct mlx4_dev *dev, int base_qpn, int cnt)
  145. {
  146. struct mlx4_priv *priv = mlx4_priv(dev);
  147. struct mlx4_qp_table *qp_table = &priv->qp_table;
  148. if (base_qpn < dev->caps.sqp_start + 8)
  149. return;
  150. mlx4_bitmap_free_range(&qp_table->bitmap, base_qpn, cnt);
  151. }
  152. EXPORT_SYMBOL_GPL(mlx4_qp_release_range);
  153. int mlx4_qp_alloc(struct mlx4_dev *dev, int qpn, struct mlx4_qp *qp)
  154. {
  155. struct mlx4_priv *priv = mlx4_priv(dev);
  156. struct mlx4_qp_table *qp_table = &priv->qp_table;
  157. int err;
  158. if (!qpn)
  159. return -EINVAL;
  160. qp->qpn = qpn;
  161. err = mlx4_table_get(dev, &qp_table->qp_table, qp->qpn);
  162. if (err)
  163. goto err_out;
  164. err = mlx4_table_get(dev, &qp_table->auxc_table, qp->qpn);
  165. if (err)
  166. goto err_put_qp;
  167. err = mlx4_table_get(dev, &qp_table->altc_table, qp->qpn);
  168. if (err)
  169. goto err_put_auxc;
  170. err = mlx4_table_get(dev, &qp_table->rdmarc_table, qp->qpn);
  171. if (err)
  172. goto err_put_altc;
  173. err = mlx4_table_get(dev, &qp_table->cmpt_table, qp->qpn);
  174. if (err)
  175. goto err_put_rdmarc;
  176. spin_lock_irq(&qp_table->lock);
  177. err = radix_tree_insert(&dev->qp_table_tree, qp->qpn & (dev->caps.num_qps - 1), qp);
  178. spin_unlock_irq(&qp_table->lock);
  179. if (err)
  180. goto err_put_cmpt;
  181. atomic_set(&qp->refcount, 1);
  182. init_completion(&qp->free);
  183. return 0;
  184. err_put_cmpt:
  185. mlx4_table_put(dev, &qp_table->cmpt_table, qp->qpn);
  186. err_put_rdmarc:
  187. mlx4_table_put(dev, &qp_table->rdmarc_table, qp->qpn);
  188. err_put_altc:
  189. mlx4_table_put(dev, &qp_table->altc_table, qp->qpn);
  190. err_put_auxc:
  191. mlx4_table_put(dev, &qp_table->auxc_table, qp->qpn);
  192. err_put_qp:
  193. mlx4_table_put(dev, &qp_table->qp_table, qp->qpn);
  194. err_out:
  195. return err;
  196. }
  197. EXPORT_SYMBOL_GPL(mlx4_qp_alloc);
  198. void mlx4_qp_remove(struct mlx4_dev *dev, struct mlx4_qp *qp)
  199. {
  200. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  201. unsigned long flags;
  202. spin_lock_irqsave(&qp_table->lock, flags);
  203. radix_tree_delete(&dev->qp_table_tree, qp->qpn & (dev->caps.num_qps - 1));
  204. spin_unlock_irqrestore(&qp_table->lock, flags);
  205. }
  206. EXPORT_SYMBOL_GPL(mlx4_qp_remove);
  207. void mlx4_qp_free(struct mlx4_dev *dev, struct mlx4_qp *qp)
  208. {
  209. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  210. if (atomic_dec_and_test(&qp->refcount))
  211. complete(&qp->free);
  212. wait_for_completion(&qp->free);
  213. mlx4_table_put(dev, &qp_table->cmpt_table, qp->qpn);
  214. mlx4_table_put(dev, &qp_table->rdmarc_table, qp->qpn);
  215. mlx4_table_put(dev, &qp_table->altc_table, qp->qpn);
  216. mlx4_table_put(dev, &qp_table->auxc_table, qp->qpn);
  217. mlx4_table_put(dev, &qp_table->qp_table, qp->qpn);
  218. }
  219. EXPORT_SYMBOL_GPL(mlx4_qp_free);
  220. static int mlx4_CONF_SPECIAL_QP(struct mlx4_dev *dev, u32 base_qpn)
  221. {
  222. return mlx4_cmd(dev, 0, base_qpn, 0, MLX4_CMD_CONF_SPECIAL_QP,
  223. MLX4_CMD_TIME_CLASS_B, MLX4_CMD_NATIVE);
  224. }
  225. int mlx4_init_qp_table(struct mlx4_dev *dev)
  226. {
  227. struct mlx4_qp_table *qp_table = &mlx4_priv(dev)->qp_table;
  228. int err;
  229. int reserved_from_top = 0;
  230. spin_lock_init(&qp_table->lock);
  231. INIT_RADIX_TREE(&dev->qp_table_tree, GFP_ATOMIC);
  232. /*
  233. * We reserve 2 extra QPs per port for the special QPs. The
  234. * block of special QPs must be aligned to a multiple of 8, so
  235. * round up.
  236. *
  237. * We also reserve the MSB of the 24-bit QP number to indicate
  238. * that a QP is an XRC QP.
  239. */
  240. dev->caps.sqp_start =
  241. ALIGN(dev->caps.reserved_qps_cnt[MLX4_QP_REGION_FW], 8);
  242. {
  243. int sort[MLX4_NUM_QP_REGION];
  244. int i, j, tmp;
  245. int last_base = dev->caps.num_qps;
  246. for (i = 1; i < MLX4_NUM_QP_REGION; ++i)
  247. sort[i] = i;
  248. for (i = MLX4_NUM_QP_REGION; i > 0; --i) {
  249. for (j = 2; j < i; ++j) {
  250. if (dev->caps.reserved_qps_cnt[sort[j]] >
  251. dev->caps.reserved_qps_cnt[sort[j - 1]]) {
  252. tmp = sort[j];
  253. sort[j] = sort[j - 1];
  254. sort[j - 1] = tmp;
  255. }
  256. }
  257. }
  258. for (i = 1; i < MLX4_NUM_QP_REGION; ++i) {
  259. last_base -= dev->caps.reserved_qps_cnt[sort[i]];
  260. dev->caps.reserved_qps_base[sort[i]] = last_base;
  261. reserved_from_top +=
  262. dev->caps.reserved_qps_cnt[sort[i]];
  263. }
  264. }
  265. err = mlx4_bitmap_init(&qp_table->bitmap, dev->caps.num_qps,
  266. (1 << 23) - 1, dev->caps.sqp_start + 8,
  267. reserved_from_top);
  268. if (err)
  269. return err;
  270. return mlx4_CONF_SPECIAL_QP(dev, dev->caps.sqp_start);
  271. }
  272. void mlx4_cleanup_qp_table(struct mlx4_dev *dev)
  273. {
  274. mlx4_CONF_SPECIAL_QP(dev, 0);
  275. mlx4_bitmap_cleanup(&mlx4_priv(dev)->qp_table.bitmap);
  276. }
  277. int mlx4_qp_query(struct mlx4_dev *dev, struct mlx4_qp *qp,
  278. struct mlx4_qp_context *context)
  279. {
  280. struct mlx4_cmd_mailbox *mailbox;
  281. int err;
  282. mailbox = mlx4_alloc_cmd_mailbox(dev);
  283. if (IS_ERR(mailbox))
  284. return PTR_ERR(mailbox);
  285. err = mlx4_cmd_box(dev, 0, mailbox->dma, qp->qpn, 0,
  286. MLX4_CMD_QUERY_QP, MLX4_CMD_TIME_CLASS_A,
  287. MLX4_CMD_WRAPPED);
  288. if (!err)
  289. memcpy(context, mailbox->buf + 8, sizeof *context);
  290. mlx4_free_cmd_mailbox(dev, mailbox);
  291. return err;
  292. }
  293. EXPORT_SYMBOL_GPL(mlx4_qp_query);
  294. int mlx4_qp_to_ready(struct mlx4_dev *dev, struct mlx4_mtt *mtt,
  295. struct mlx4_qp_context *context,
  296. struct mlx4_qp *qp, enum mlx4_qp_state *qp_state)
  297. {
  298. int err;
  299. int i;
  300. enum mlx4_qp_state states[] = {
  301. MLX4_QP_STATE_RST,
  302. MLX4_QP_STATE_INIT,
  303. MLX4_QP_STATE_RTR,
  304. MLX4_QP_STATE_RTS
  305. };
  306. for (i = 0; i < ARRAY_SIZE(states) - 1; i++) {
  307. context->flags &= cpu_to_be32(~(0xf << 28));
  308. context->flags |= cpu_to_be32(states[i + 1] << 28);
  309. err = mlx4_qp_modify(dev, mtt, states[i], states[i + 1],
  310. context, 0, 0, qp);
  311. if (err) {
  312. mlx4_err(dev, "Failed to bring QP to state: "
  313. "%d with error: %d\n",
  314. states[i + 1], err);
  315. return err;
  316. }
  317. *qp_state = states[i + 1];
  318. }
  319. return 0;
  320. }
  321. EXPORT_SYMBOL_GPL(mlx4_qp_to_ready);